Repository navigation
fix(libSceJxr): validate guest memory pointers in WMPAlloc and WMPFree - #18247
Conversation
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — changes requested
WMPFree_nid_postfix can free an allocation before discovering that the guest pointer slot is not writable, leaving the caller with a dangling pointer after the function throws.
What I ran
Reviewed head cfd85e3edb7bc50d37114dd3e07dab097f8f2765 against base bc94a8e423ea026aadbb29165fed1ad2db471154; the merge preview was clean. I read both changed files and inspected the checked guest-copy helpers.
Environment: Linux 6.1, Python 3.11.2, GCC 12.2.0. I ran:
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 18247 --output-dir /tmp/anyps5-pr-precheck-18247
gh pr checks 18247 -R boykopovar/AnyPS5I did not build or run CTest. The author reports that guest_jxr_stream_tests passed and that the unpatched invalid-pointer case segfaulted; I did not reproduce those runs.
Results
| Check | Result | Note |
|---|---|---|
| No-build precheck | passed | Applicable static checks passed; build-dependent checks were not run. |
| Merge preview | passed | Clean against the stated base. |
| Source review | failed | WMPFree_nid_postfix frees before validating the output write. |
| Local C++ tests | not run | No build or CTest run by me. |
| Hosted Windows build | not run | GitHub job was pending at review time. |
Precheck results and command output
| Check | Status | Reason |
|---|---|---|
| build_and_compile | not run | Builds are excluded from the no-build precheck. |
| diff_check | passed | git diff --check on the pinned base and head. |
| conventions_and_pr_template | passed | Repository conventions and PR template passed. |
| changed_export_abi_audit | passed | Audit completed without findings. |
| python_syntax | not applicable | No Python implementation changed. |
| related_python_unit_tests | not applicable | No Python tool module changed. |
| python_test_registration | not applicable | No Python tests changed. |
| live_dependency_and_mergeability_check | passed | Clean merge preview; no declared dependency found. |
| build_dependent_cpp_tests | not run | This no-build pass does not build changed C++ code. |
| test_quality_heuristics | passed | Heuristics completed; source and test registration were reviewed manually. |
| negative_controls | not applicable | No eligible Python-tool negative control. |
Commands and captured output:
$ git diff --check bc94a8e423ea026aadbb29165fed1ad2db471154...cfd85e3edb7bc50d37114dd3e07dab097f8f2765
(no output; exit 0)
$ /usr/bin/python3 tools/check_conventions.py --base bc94a8e423ea026aadbb29165fed1ad2db471154 --head cfd85e3edb7bc50d37114dd3e07dab097f8f2765 --title 'fix(libSceJxr): validate guest memory pointers in WMPAlloc and WMPFree' --body /tmp/anyps5-pr-precheck-18247/pr-body.md
All conventions checks passed.
Negative control
The author reports that the unpatched 0x10 pointer case segfaulted and the patched version throws. I did not run this control; it does not exercise a readable but read-only pointer slot.
Findings
Blocking
- In
WMPFree_nid_postfix, the function reads the pointer, frees its pointee, then checks whether it can write null back through the guest slot. A readable, read-only slot passesGuestCopyIn_nid_no_patch, but the laterGuestCopyOut_nid_no_patchfails after the allocation has already been freed. The function throws while the slot still contains the freed address, so retrying can free it again. Validate that the output slot is writable before releasing the allocation, and add a regression test proving a rejected read-only slot leaves the allocation valid.
Non-blocking
None.
Sources
Not verified
- A local build, CTest, and the reported negative control were not run by me.
- The hosted Windows build was pending; no hardware or console behavior was checked.
To get this to a good state
Validate the output slot before freeing its pointee and add the read-only-slot regression test. I’ll rerun the same precheck and review the complete diff on the updated head.
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against cfd85e3edb:
- Conventions:
tools/check_conventions.pypassed;git diff --checkclean. - Build:
guest_jxr_stream_testsbuilt cleanly. - CTest (
guest_jxr_stream):1/1 Test #3394: guest_jxr_stream ................. Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libSceJxr/Export.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #3394: guest_jxr_stream .................***Exception: SegFault 0.12 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- core/libs/prx/libSceJxr/Export.cpp: Checked WMPAlloc_nid_postfix return values on allocation failure (returning WmpErrOutOfMemory / -101) and success (WmpErrSuccess / 0), confirming that failed allocations clear *memory to nullptr.
- core/libs/prx/libSceJxr/Export.cpp: Verified null-pointer argument validation in WMPAlloc_nid_postfix and WMPFree_nid_postfix against APS5_INVALID_ARG_EX throwing std::invalid_argument.
- core/libs/prx/libSceJxr/Export.cpp: Verified resource cleanup in WMPAlloc_nid_postfix, confirming allocated heap memory is freed via free_nid_postfix if GuestCopyOut_nid_no_patch fails.
- core/libs/prx/libSceJxr/Export.cpp: Evaluated exception safety and resource lifetimes in WMPFree_nid_postfix (lines 358-361) when memory points to readable but read-only memory, checking for premature deallocation before copy-out.
- core/libs/tests/JxrStream.cpp: Verified unit test coverage in Allocation() for unmapped pointers (0x10) using RequireInvalidArgument on WMPAlloc_nid_postfix and WMPFree_nid_postfix.
- core/libs/prx/libSceJxr/Export.cpp & core/libs/tests/JxrStream.cpp: Verified AGENTS.md rule compliance, confirming zero forbidden code comments were introduced and conventional commit formatting was followed.
- Concerns:
- core/libs/prx/libSceJxr/Export.cpp:358-361: In WMPFree_nid_postfix, free_nid_postfix(ptr) is called before verifying that memory is writable via GuestCopyOut_nid_no_patch. If memory is mapped read-only (or write-protected), GuestCopyIn_nid_no_patch succeeds, the memory at ptr is freed, and then GuestCopyOut_nid_no_patch fails, throwing APS5_INVALID_ARG_EX. This leaves *memory containing a dangling pointer to freed memory while reporting failure via an exception, creating use-after-free and double-free hazards upon subsequent error handling or cleanup. The null pointer must be written to memory via GuestCopyOut_nid_no_patch before deallocating ptr.
- Note: the AI-assisted checkbox can stay.
Requesting changes for the points above.
|
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.
|
|
Linux check of head Setup: Linux x86_64, 13th Gen Intel(R) Core(TM) i7-13700T, GCC 16.2.1, Release.
AI-assisted: yes. |
cfd85e3 to
85b345e
Compare
|
Updated in head
|
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 85b345eb9a:
- Conventions:
tools/check_conventions.pypassed;git diff --checkclean. - Build:
guest_jxr_stream_testsbuilt cleanly. - CTest (
guest_jxr_stream):1/1 Test #3394: guest_jxr_stream ................. Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libSceJxr/Export.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #3394: guest_jxr_stream .................***Exception: SegFault 0.12 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- core/libs/prx/libSceJxr/Export.cpp: Verified that WMPAlloc_nid_postfix validates that memory is non-null, allocates zeroed memory via calloc_nid_postfix(1, size), and writes the pointer to guest memory via GuestCopyOut_nid_no_patch; if the copy fails on unmapped/read-only memory, any allocated buffer is promptly freed via free_nid_postfix before throwing std::invalid_argument via APS5_INVALID_ARG_EX.
- core/libs/prx/libSceJxr/Export.cpp: Verified that WMPAlloc_nid_postfix properly handles allocation failure (e.g. size == Max): calloc_nid_postfix returns nullptr, GuestCopyOut_nid_no_patch commits nullptr to guest memory, and WmpErrOutOfMemory (-101) is returned.
- core/libs/prx/libSceJxr/Export.cpp: Verified that WMPFree_nid_postfix validates memory via GuestCopyIn_nid_no_patch before reading the target pointer; when ptr is non-null, it verifies writability and clears the guest slot to nullptr with GuestCopyOut_nid_no_patch before invoking free_nid_postfix(ptr), ensuring that a write failure (e.g. read-only memory) aborts via APS5_INVALID_ARG_EX without freeing the buffer.
- core/libs/prx/libSceJxr/Export.cpp: Verified that WMPFree_nid_postfix handles a valid guest pointer holding nullptr cleanly without attempting redundant writes, returning WmpErrSuccess (0).
- core/libs/tests/JxrStream.cpp: Verified that Allocation() exercises unmapped pointers (0x10) for both allocation and deallocation, validates read-only direct memory behavior using sceKernelAllocateDirectMemory, sceKernelMapDirectMemory, and sceKernelMprotect, and ensures complete cleanup with sceKernelMunmap and sceKernelReleaseDirectMemory.
- AGENTS.md & Repository Conventions: Verified that no comments were added to code, Conventional Commit guidelines were adhered to, and no unexpected files or title-specific workarounds were introduced.
- Note: the AI-assisted checkbox can stay.
Looks Good To Me
|
Thanks for the review. Fixed in head |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed WMPAlloc and WMPFree guest pointer checks — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
- Head reviewed:
85b345eb9ae4398eb2aa40a027e9162264db013c(basemain). - Read the diff of
core/libs/prx/libSceJxr/Export.cppandcore/libs/tests/JxrStream.cpp, then the helpers they call at the head:GuestCopyOut_nid_no_patch(core/libs/prx/libc/src/General.cpp), the kernel declarations incore/libs/prx/libkernel/include/Api.hpp, and theRequireandRequireInvalidArgumenthelpers in the test file. - Reader commands, run against the pinned head:
repo_read.py search GuestCopyOut_nid_no_patch
repo_read.py search calloc_nid_postfix
repo_read.py search GuestCopyIn_nid_no_patch(void
repo_read.py cat core/libs/prx/libc/src/General.cpp 820 862
repo_read.py cat core/libs/tests/JxrStream.cpp 1 60
repo_read.py cat core/libs/prx/libkernel/include/Api.hpp 205 230
repo_read.py cat core/libs/prx/libSceJxr/Export.cpp 50 64
- No build and no test run: this review does not compile the code.
Results
| Check | Result | Note |
|---|---|---|
| Diff read, both files | passed | Two hunks, one per file |
| Kernel calls used by the new test | passed | Signatures match Api.hpp |
| Guest copy-out on read-only pages (Linux) | passed | process_vm_writev path returns false on EFAULT |
| Build and unit test | not run | No builds in this review |
Author-reported checks
- Author:
ninja -C build guest_jxr_stream_tests && ./build/tests/guest_jxr_stream_testspasses (Linux, WSL2, Release). - Author: negative control, unpatched code segfaults on
0x10; patched code throwsstd::invalid_argument.
These are the author's runs, not runs I performed.
Findings
Blocking
None. The guarded reads and writes in WMPAlloc_nid_postfix and WMPFree_nid_postfix do what the description says, and the new assertions in core/libs/tests/JxrStream.cpp exercise both the unmapped pointer and the read-only slot paths.
Non-blocking
core/libs/prx/libSceJxr/Export.cpp:345—WMPAlloc_nid_postfixallocates before it checks that thememorydestination is writable. A bad pointer therefore costs onecallocand onefree_nid_postfixbefore the throw. This is harmless. Checking the destination first would read more clearly, but this is a style choice, not a defect.
Not verified
- The test suite passing on Linux and the unpatched-segfault negative control: author-reported only; not reproduced here.
GuestCopyIn_nid_no_patch: I saw its callers but did not read its definition at the head. TheWMPFreepath depends on it.- The Windows branch of
GuestCopyOut_nid_no_patch(VirtualQuerychecks): not read in this review. AllocateZeroedatcore/libs/prx/libSceJxr/Export.cpp:58is not part of this diff and its callers were not checked.- The full text of the two changed files was not read; only the changed hunks and the helpers listed above.
- The review-body structure validator was not run: it sits outside the directories this session can read.
To get this to a good state
Nothing is required.
|
@MarcZX10 Thanks for re-reviewing |
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
18247 MarcZX10 CR 5851320762 (10 Nov 23:48Z on cfd85e3): met
Claim: WMPFree frees the pointee before checking the slot is writable, so a read-only slot keeps the freed address.
Live head 85b345e, core/libs/prx/libSceJxr/Export.cpp:358-360 writes null to the slot first and frees only after that write succeeds.
PASS LOW NO by pr-review3 on 85b345e at 11 Nov 23:14Z, after the CR. Checked at source by pr-review1.
What
memorydouble pointer inWMPAlloc_nid_postfixandWMPFree_nid_postfix(core/libs/prx/libSceJxr/Export.cpp):WMPAlloc_nid_postfixallocates into a temporaryallocatedpointer and commits it viaGuestCopyOut_nid_no_patch(memory, &allocated, sizeof(allocated)); ifGuestCopyOut_nid_no_patchfails, it freesallocated(when non-null) before throwingstd::invalid_argumentviaAPS5_INVALID_ARG_EXso no host block leaks.WMPFree_nid_postfixreads*memoryviaGuestCopyIn_nid_no_patch, and when non-null, writesnullptrback tomemoryviaGuestCopyOut_nid_no_patchbefore callingfree_nid_postfix(ptr)so unwritable guest pointer slots throwstd::invalid_argumentwithout freeing the host block.core/libs/tests/JxrStream.cpp(Allocation()) to verify that unmapped pointers (0x10) throwstd::invalid_argumentin bothWMPAlloc_nid_postfixandWMPFree_nid_postfix, and that a read-only direct-memory slot (sceKernelMprotect(..., 1)) causesWMPFree_nid_postfixto throwstd::invalid_argument, leavesslotPtr[0]untouched, and succeeds once writability is restored.Tested
85b345eb9a: builtguest_jxr_stream_testsand ranctest(guest_jxr_stream) covering the unmapped (0x10) and read-only direct-memory slot regression tests (100%passed).python3 tools/check_conventions.py --base upstream/main --head HEAD: passed (0 violations).0x10, whereas patched code safely throwsstd::invalid_argument.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)