fix(libkernel): refuse read-only guest mappings for getdirentries on Windows #7672
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7672"
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 valenciana-nc on 2026-10-18T21:32:04Z.
What
sceKernelGetdirentriesnow guards itsbufandbasepoutputs withGuestArena::HostWritein both the Windows and Linux branches ofcore/libs/prx/libkernel/File/src/Stdio.cpp. This matches the other output-buffer syscalls in that file. Both guards stay open until the call returns. They open after the descriptor checks, so a bad descriptor still fails withEBADForEINVALbeforeEFAULT. FreeBSD 11.4'skern_getdirentriesalso checks the descriptor before reading.What this does: on Windows, a
buforbasepin a read-only guest mapping (for example, a read-only direct-memory mapping) now fails withEFAULT(0x8002000e). Previously the process crashed with an access violation.What this does not do:
HostWriteonly refuses ranges that overlap a non-writable shared view; it does not establish that the pointer is in the guest arena.GuestArenaBeginHostWritealways returnstruethere, so the read-only mapping still crashes on Linux. On Linux the guard only reports the write to the write watch.Tested
Merged at
950eeb847on 2026-10-19. The subsequent fresh-descriptor test improvement is tracked separately in #7892; it was not part of this merge.Each build is this branch merged onto
maina373e85e5, Release,BUILD_TESTING=ON.guest_*tests registered undercore/libs: all pass, the same asmain.guest_video_out_wqhd_detection_no_vulkanis skipped on both.guest_directory_buffer,guest_directory_rewindandguest_directory_close.Stdio.cppfrommain, the original regression test fails at its read-only output check. As @wachi35676 clarified, thebufcall was already at EOF; the distinctbasepprobe is the one that demonstrates the access violation. #7892 opens a fresh descriptor so thebufcheck also exercises the crash.guest_*tests pass, includingguest_directory_entries,guest_filesystem,guest_kernel_sysandguest_kernel_app_archive_*.guest_system_load_exec_standard_errorandguest_named_shm_concurrency, fail onmainin the same container too; it runs as root.CheckReadOnlyDestinationincore/libs/tests/GuestDirectoryBuffer.cppis part of the Windows-only test binary. It covers:bufsucceeds.EFAULT, both asbufand asbasep.bufreturnsEBADF.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T21:46:28Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libkernel/File/src/Stdio.cppcore/libs/tests/GuestDirectoryBuffer.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppImported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T22:33:47Z.
Windows build and run, since the description has a syntax check only. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON. Build:main8a456334fwith #7677, #7673, #7671, #7666, #7676, #7675, #7672, #7668, #7664 and #7658 merged together (each merge clean, built once).The build is clean. The full
guest_*run on that build: 474 of 476 pass, withguest_video_out_wqhd_detection_no_vulkanskipped. The 2 failures,guest_sysarchandguest_offline_imports, come from #7658 in the same build: they fail with #7658 alone and pass without it. I didn't check which of them reach the changed function, and no test checks the new buffer validation itself.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T00:01:09Z.
Review: COMMENTED
Linux run, since the description has a syntax check only and the existing run is Windows.
Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, full build.Head
7709eda9amerged ontomain84c1a4d9a(clean merge).ctest -R "director|dirent|getdents|filesystem|kernel_sys", 8 tests), and the non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out") is 739/739, same asmain.GuestArenaBeginHostWrite_nid_postfix(GuestArena.cpp) only notifies the write watch and returnstrue, soHostWrite::Open()is always true and theEFAULTreturn is unreachable. A small probe (each call in a forked child, fresh descriptor per call, on an empty directory) gives the same result with and without this PR:bufandbasep: returns 24;buf = 0x1000(unmapped, outside the arena): SIGSEGV;basep = 0x1000: SIGSEGV.So on Linux this changes nothing, and the "forged pointer fails with
EFAULT" part of the description holds only whereBeginHostWriterejects the range. @MarcZX10 raised the same point aboutHostWrite::Open()for #7675, which uses the same guard.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:12:52Z.
Tested at
7709eda9a, built on Windows (GCC 15.2) and Linux (gcc 13.3) onmain8a456334f. Theguest_directory*tests pass (4 on Windows, 2 on Linux).The change helps on Windows. With
buf(orbasep) in a read-only direct-memory mapping (sceKernelMapDirectMemory, prot 1),maincrashes withunhandled exception 0xc0000005 … write of 0x0000000200000000in libkernel.prx, and this branch returns0x8002000e.The description says more than the change does, though.
GuestArena::HostWriteonly refuses ranges that overlap a non-writable shared view; it doesn't check that the pointer is in the guest arena. With this branch:GuestArenaContains= 0) is still written, and 44 bytes are returned;GuestArenaBeginHostWritealways returns true, so the read-only mapping still dumps core there.Could the title and description say "refuses read-only guest mappings on Windows", and could the PR add a test for that case?
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-19T00:22:58Z.
Thanks for the independent build and test run — that was genuinely useful.
You're right that the description claimed more than the change does, so I've narrowed it: the PR is retitled to
fix(libkernel): refuse read-only guest mappings for getdirentries on Windows, and the body now states the non-goals explicitly (no arena-containment check, Linux behavior unchanged).I've also added the test you asked for:
CheckReadOnlyDestinationincore/libs/tests/GuestDirectoryBuffer.cpp(the Windows-only test binary, since the read-only refusal only manifests on Windows). It maps a page read/write via direct memory, verifiesgetdirentriessucceeds into the writable mapping, thenmprotects it read-only and assertsSCE_KERNEL_ERROR_EFAULTfor both a read-onlybufand a read-onlybasep.I couldn't execute the test suite in this environment (no Windows toolchain here), so CI will have to run it — your finding that unpatched
maincrashes on this case is cited in the PR as the regression property the test pins down.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T01:14:16Z.
Ran
CheckReadOnlyDestinationat25d26b54aon Windows 11 (MinGW GCC 15.2), merged ontomainad502d717.guest_directory_buffer,guest_directory,guest_directory_rewindandguest_directory_closepass. WithStdio.cppfrommain,guest_directory_bufferfails at line 114.guest_directoryandguest_directory_entriespass, and a read-only mapping still dumps core with and without the PR, as the description now says.One detail in the test: when the read-only
bufcall runs (line 114), the first call has already read the directory to the end. Onmainthat call returns 0 and writes nothing, so the test fails on the return value rather than on the crash. Thebasepcase is the one that crashesmain(unhandled exception 0xc0000005 … write of 0x0000000200000000).Opening a fresh descriptor, or rewinding, before the read-only
bufcall would cover that crash too. With this PR, a fresh descriptor also returns0x8002000e.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-19T01:37:03Z.
Updated in
950eeb847:basepguard was scoped to itsifblock, so it closed before*basepwas written. Both guards now stay open until the call returns.EBADF/EINVALbeforeEFAULT, askern_getdirentrieschecks the descriptor first.Built and ran on Windows and Linux; details are in the description.
CheckReadOnlyDestinationpasses, and withStdio.cppfrommainit crashes.Imported from boykopovar/AnyPS5. Originally posted by valenciana-nc on 2026-10-19T12:31:14Z.
@wachi35676 thanks — #7672 merged at
950eeb847while I was preparing the fresh-descriptor adjustment, so that improvement did not enter this merge. It is now a separate test-only follow-up: #7892 (413b1237b), based on the merge commitb106f6f8c.The follow-up closes the descriptor after the writable control, opens a fresh one before the read-only
bufcall, then verifies entries remain unread after both refused outputs.All four Windows directory tests pass. With only
Stdio.cpprestored from pre-fix8982791bc, the updated test segfaults writing to0x200000000in the read-onlybufcall, so it covers the crash rather than the EOF return. No runtime changes in the follow-up.AI-assisted: yes.