fix(libkernel): return EACCES for unknown ranges on direct-memory release paths #7105
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7105"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T11:51:41Z.
What
ForgetDirectMemorythrowsEACCES(system_error) for unknown but well-formed ranges (invalid_argumentfor malformed preserved); new non-throwingTryForgetDirectMemorypre-check;sceKernelReleaseDirectMemoryreturnsEACCESfor unknown/double-free (EINVALfor malformed preserved);DirectMemoryFreereturnsbool(falsefor unknown/misaligned);MemoryPool::_releaseearlyfalsefor misaligned plus existing lenient spanning walk retained for current tests;PoolCommitsaturate plus torn-lenEINVAL; one debt line._mapFlexiblecheck/unlock/map/relock split (kept single-lock),AddMappingsnapshot-Map-outside/rollback/re-validate (kept underg_directLock), 64MiB torn-descriptor guard,DoMapDirectlock narrowing,TryValidatePhysicalRange(onlyTryForgetkept).Tested
CheckReleaseUnknownRangeReturnsAccessError(single-threaded pin, no concurrency claims): double-release->EACCES, misalignedCheckedFree->false, bad-map still throwsinvalid_argument,Query(-1)->false. FAILS on main (throwsinvalid_argument, aborts) and PASSES with fix (verified both directions via stash; Windows Release).ctest -R "guest_memory|guest_mmap|MemoryPoolTest|guest_memory_pool_stats": 4/4 pass on Windows (winlibs gcc15, Release). Linux left to PR CI.git diff --checkclean,python tools/check_conventions.py --base origin/main --head HEADclean. No title run.Checklist
c9e6c45de); no other open PR implements the same changeImported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T11:52:18Z.
Manual overlap check for ITS files (
DirectMemory.cpp/hpp,MemoryPool.cpp/hpp,MemoryPoolExport.cpp,Export.cpp,GuestMemory.cpp,debt/libkernel.md) against open PRs (viagh search prs --state openfor DirectMemory/MemoryPool/GuestMemory/flexible +gh pr view --json files):account for fixed flexible memory replacements) — sharesExport.cpp: semantic, flexible-budget accounting in the same_mapFlexible/remap area; this PR keeps their math and only narrowsg_flexibleLock, so complementary.release flexible budget after direct replacements) — sharesExport.cpp: semantic, budget release after direct replacements in the same file; keep their math, keep this PR's locking/validation on merge.release flexible memory budget through munmap) — no ITS file overlap (PosixMemory.cpp+GuestFlexibleMemoryBudget.cpponly): rebase-only, budget-theme adjacency, no action.map regular files with mmap on Windows) — sharesDirectMemory.cpp,GuestMemory.cpp,debt/libkernel.md: semantic, mapping-path changes in the same files; rebase coordination needed, guards must be preserved.replace anonymous Windows stack mappings) — sharesDirectMemory.cpp/hpp,debt/libkernel.md: semantic, stack-mapping work touches the same mapping API.protected host stack guard page / Linux main stack) — sharesDirectMemory.cpp,debt/libkernel.md: semantic, adjacent stack-protection hunks in the mapping paths.report the read bit of every write bit) — sharesDirectMemory.cpp,debt/libkernel.md: semantic, prot-reporting hunks adjacent to lifetime guards; no logic conflict expected.honor low guest mapping hints) — sharesGuestMemory.cpptest +debt/libkernel.mdonly (PosixMemory.cppotherwise): rebase-only, different area.precommitted stack of adopted thread) — sharesdebt/libkernel.mdonly: rebase-only.Jit.cpponly): no action.MemoryPool.cpp/hppandMemoryPoolExport.cppare touched by no other open PR. Neither #6491 nor #6311 has landed (both still open), so no landed-budget reconciliation was needed; this PR changes no budget math.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T12:00:42Z.
Thanks @Pouare514 for chasing the Deathloop crash. We reviewed this at cfa5a5ee6 and can't merge it as it stands.
1. The use-after-free isn't visible on main. Every
g_physPagesaccess on main already holdsg_directLock:SharedBacking,WriteSharedBacking,CreateDirectMemoryBackingandForgetDirectMemorytake it themselves.AddMappingandValidatePhysicalRangeare only called fromRemapFixedIntoRegisteredandDoMapDirect, both under the lock.A bad
phys/lenmakesValidatePhysicalRangethrowinvalid_argument("references unallocated physical page"); it doesn't walk freed nodes. The run26 FATAL in that function is most likely that throw, reached when the title maps a page it has already released. This PR keeps the throw inDoMapDirect, so the Deathloop stop is probably unchanged. Could you attach run26's backtrace/log and point to the unlocked access? If there isn't one, please retitle this as a release-path error change.2. New race in
_mapFlexible(blocker). On main, the budget check,DoMapAnonand theg_flexibleRangesinsert all run underg_flexibleLock. The PR checks, unlocks, maps, then relocks, so two threads can both pass the check and go over the flexible size. The lock-order reason given doesn't apply: nothing takesg_flexibleLockwhile holdingg_directLock. Please drop this hunk.3.
AddMappingmaps outside the lock with no rollback. On main, validate,Mapand the bookkeeping all run underg_directLock. Now the hostMap(MAP_FIXED) runs unlocked, and if the second validation throws "range changed during mapping", the host mapping stays installed. InRemapFixedIntoRegistered, that leaves new pages ataddrwhileg_directMappingsstill describes the old mapping. Please keepMapunder the lock.Smaller points:
RemapFixedIntoRegisteredis arbitrary, andAddMappingvalidates every length anyway. Please drop it.sceKernelReleaseDirectMemorynow returnsEACCESfor holes. That's fine instead of throwing, but it now matches the checked variant. It also makes the!requireAllocatedwalk inMemoryPool::_releaseunreachable. Please delete the dead branch and add a debt line for the console's unknown behaviour.Test:
CheckDeathloopPhysLifetimeis single-threaded. On main it fails only because the double release throws instead of returningEACCES, so it doesn't exercise a race.Suggested split:
EACCES, with a debt entry.Direct memory runs in our game gates, so (b) would also need a gate run.
On any new head, CI must show
guest_memory,guest_mmap,MemoryPoolTestandguest_memory_pool_statspassing on Linux and Windows.Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:32:26Z.
Concede no unlocked access on main per re-audit: ValidatePhysicalRange DirectMemory.cpp:494, AddMapping :582, ForgetDirectMemory :1223 all run under g_directLock; _mapFlexible Export.cpp:108 is single-lock. run26 log is unavailable (run dirs cleaned, binaries only) so no backtrace can be attached; contemporaneous notes indicated AV read of -1 with rax=-1, inconsistent with a clean throw which aborts with a message, but without the log propose settling it in split (b) with a concurrent stress plus gate run rather than claiming it here; retitled as release-path error change. _mapFlexible split dropped (race acknowledged, no lock-order issue). AddMapping-outside-lock dropped, Map stays under g_directLock. 64MiB guard dropped; _release lenient walk retained (strict requires-all-used breaks 11 MemoryPoolTest cases) plus debt line added. Test reworked to honest single-threaded EACCES pin. CI: guest_memory, guest_mmap, MemoryPoolTest, guest_memory_pool_stats green on Windows; Linux left to PR CI.
Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:33:05Z.
CI note on the 4 requested tests (guest_memory, guest_mmap, MemoryPoolTest, guest_memory_pool_stats): verified 4/4 green on Windows locally (WinLibs GCC 15.2, Release; run recorded in the body). The hosted Linux + Windows full builds can't run automatically for PRs - build.yml enables them only on push to main or manual workflow_dispatch ('Automatic full builds only run on main'). Requesting a collaborator dispatch the full build when the reduced scope looks good.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:52:35Z.
Review: COMMENTED
Points on 7105:
Comments in code.
core/libs/tests/GuestMemory.cppadds a three-line comment ("Single-threaded error-behavior pin: releasing an unknown range reports EACCES..."). The rule is no comments except#endifand namespace ends.Evidence for EACCES. The description should say where the errno comes from (a measurement on a console, or the FreeBSD source for the specific release path) for each of the release paths changed, and which title hits an unknown range on main with a log line. Without it this is a guess at an errno.
Pull request closed