fix(libkernel): return EFAULT from sendfile for invalid memory pointers #13537

Merged
pushkarreddyy merged 4 commits from upstream-pr/13537 into main 2026-10-31 02:00:47 +00:00
pushkarreddyy commented 2026-10-30 17:47:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-30T17:47:32Z.


What

In sendfile_nid_postfix (core/libs/prx/libkernel/Socket/src/Udp.cpp), passing an unreadable guest pointer for headers or its internal headers/trailers KernelIovec arrays, or an unwritable pointer for sbytes, caused direct host pointer dereferences resulting in host segmentation faults (SIGSEGV).

This change copies guest data safely via GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch, returning EFAULT (14) per FreeBSD 11.4 sys_sendfile / kern_sendfile semantics when memory is inaccessible. Also adds regression test coverage in core/libs/tests/GuestSendfile.cpp and updates technical debt documentation in docs/dev/debt/libkernel.md.

Fixes #13525.

Tested

Windows 11, unit test suite (core/libs/tests/GuestSendfile.cpp) verifying rejection of invalid memory pointers (0x10) for headers, headers->headers, headers->trailers, and sbytes returning 14 (EFAULT).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #13525
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5088669745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13537). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-30T17:47:32Z. --- ### What In `sendfile_nid_postfix` (`core/libs/prx/libkernel/Socket/src/Udp.cpp`), passing an unreadable guest pointer for `headers` or its internal `headers`/`trailers` `KernelIovec` arrays, or an unwritable pointer for `sbytes`, caused direct host pointer dereferences resulting in host segmentation faults (`SIGSEGV`). This change copies guest data safely via `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch`, returning `EFAULT` (`14`) per FreeBSD 11.4 `sys_sendfile` / `kern_sendfile` semantics when memory is inaccessible. Also adds regression test coverage in `core/libs/tests/GuestSendfile.cpp` and updates technical debt documentation in `docs/dev/debt/libkernel.md`. Fixes #13525. ### Tested Windows 11, unit test suite (`core/libs/tests/GuestSendfile.cpp`) verifying rejection of invalid memory pointers (`0x10`) for `headers`, `headers->headers`, `headers->trailers`, and `sbytes` returning `14` (`EFAULT`). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #13525 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-30 17:54:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T17:54:39Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The invalid guest-pointer paths are routed through safe copy helpers, but the new iovec-vector allocations can throw instead of returning the existing ENOMEM error.

What I ran

Reviewed head 299c2fe0cbd4246d62bdee1f49d04597164e4037 against base 223b19ba0a; the exact-head merge preview was clean. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. I did not build or compile.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13537
git diff --check 223b19ba0adb11b66893a57fa656ecf6bc4a45cf...299c2fe0cbd4246d62bdee1f49d04597164e4037
python3 tools/check_conventions.py --base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf --head 299c2fe0cbd4246d62bdee1f49d04597164e4037 --title 'fix(libkernel): return EFAULT from sendfile for invalid memory pointers' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13537-299c2fe0cbd4/pr-body.md
python3 tools/pr_abi_audit.py --base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf --head 299c2fe0cbd4246d62bdee1f49d04597164e4037 --json | python3 -c 'import json,sys; d=json.load(sys.stdin); print("status=" + d["status"] + "; findings=" + str(len(d.get("findings", []))))'
python3 tools/pr_test_quality.py --root /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13537-299c2fe0cbd4/head-299c2fe0cbd4-9cb150d2 --json core/libs/prx/libkernel/Socket/src/Udp.cpp core/libs/tests/GuestSendfile.cpp docs/dev/debt/libkernel.md
git grep -n 'guest_sendfile' 223b19ba0adb11b66893a57fa656ecf6bc4a45cf -- core/libs/prx/libkernel/CMakeLists.txt

Conventions, diff check, ABI audit, and base merge preview passed. The test-quality heuristic flagged test registration, but CMake directly registers this test; that heuristic finding is a false positive. The precheck could not invoke two private helper scripts because they are not present in the PR snapshot. Hosted checks were queued at last refresh.

Results

Check Result
Diff whitespace check Passed
Conventions and PR template Passed
Changed export ABI audit Passed; zero findings
CTest registration Confirmed
Exact-head merge preview Clean
Test-quality heuristic Registration warning; false positive after CMake inspection
Build and C++ tests Not run
Hosted CI Six checks queued at last refresh
Unedited command output
All conventions checks passed.
status=passed; findings=0
223b19ba0adb11b66893a57fa656ecf6bc4a45cf:core/libs/prx/libkernel/CMakeLists.txt:1453:    add_test(NAME guest_sendfile COMMAND guest_sendfile_tests)

The heuristic's registration warning was:

{
  "findings": [
    {
      "path": "core/libs/tests/GuestSendfile.cpp",
      "line": 1,
      "kind": "possible-unregistered-cpp-test",
      "message": "Changed C++ test source path was not found literally in CMake files; inspect registration, generated source lists, and workflow coverage."
    }
  ],
  "negative_control_candidates": [],
  "limits": [
    "Heuristics can miss weak assertions and can flag valid tests; inspect every finding.",
    "A test that passes against base is not by itself proof that the PR test is inadequate.",
    "Negative controls are only candidates until the test actually runs against base and fails by assertion rather than import/setup error."
  ]
}

The precheck's private helper paths were unavailable in the PR snapshot; for example:

/usr/bin/python3: can't open file '.../tools/pr_abi_audit.py': [Errno 2] No such file or directory
/usr/bin/python3: can't open file '.../tools/pr_test_quality.py': [Errno 2] No such file or directory

Findings

Blocking

  • Both new vector allocations, for headerIovecs and trailerIovecs, occur outside a bad_alloc handler. Under memory pressure either resize can throw out of sendfile_nid_postfix, while the later 64 KiB chunk allocation explicitly catches bad_alloc and returns ENOMEM (12). Catch allocation failure for both iovec vectors and return ENOMEM consistently.

Non-blocking

  • The added invalid-pointer cases exercise the requested input-copy and output-copy paths. The underlying guest-copy helpers have platform-specific implementations; the PR description reports Windows tests, but I did not run them independently.

Sources

Not verified

  • I did not compile or run guest_sendfile locally or independently reproduce the author's Windows test.
  • Linux and macOS runtime behavior was not tested locally; hosted checks were queued at the last refresh.
  • I did not run a negative-control mutation.

To get this to a good state

Catch allocation failure for both vector resizes and return ENOMEM, then rerun guest_sendfile on the updated head. I will repeat the same checks there.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5716430647 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13537#pullrequestreview-5716430647). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T17:54:39Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The invalid guest-pointer paths are routed through safe copy helpers, but the new iovec-vector allocations can throw instead of returning the existing ENOMEM error. ## What I ran Reviewed head 299c2fe0cbd4246d62bdee1f49d04597164e4037 against base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf; the exact-head merge preview was clean. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. I did not build or compile. ~~~sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13537 git diff --check 223b19ba0adb11b66893a57fa656ecf6bc4a45cf...299c2fe0cbd4246d62bdee1f49d04597164e4037 python3 tools/check_conventions.py --base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf --head 299c2fe0cbd4246d62bdee1f49d04597164e4037 --title 'fix(libkernel): return EFAULT from sendfile for invalid memory pointers' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13537-299c2fe0cbd4/pr-body.md python3 tools/pr_abi_audit.py --base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf --head 299c2fe0cbd4246d62bdee1f49d04597164e4037 --json | python3 -c 'import json,sys; d=json.load(sys.stdin); print("status=" + d["status"] + "; findings=" + str(len(d.get("findings", []))))' python3 tools/pr_test_quality.py --root /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13537-299c2fe0cbd4/head-299c2fe0cbd4-9cb150d2 --json core/libs/prx/libkernel/Socket/src/Udp.cpp core/libs/tests/GuestSendfile.cpp docs/dev/debt/libkernel.md git grep -n 'guest_sendfile' 223b19ba0adb11b66893a57fa656ecf6bc4a45cf -- core/libs/prx/libkernel/CMakeLists.txt ~~~ Conventions, diff check, ABI audit, and base merge preview passed. The test-quality heuristic flagged test registration, but CMake directly registers this test; that heuristic finding is a false positive. The precheck could not invoke two private helper scripts because they are not present in the PR snapshot. Hosted checks were queued at last refresh. ## Results | Check | Result | |---|---| | Diff whitespace check | Passed | | Conventions and PR template | Passed | | Changed export ABI audit | Passed; zero findings | | CTest registration | Confirmed | | Exact-head merge preview | Clean | | Test-quality heuristic | Registration warning; false positive after CMake inspection | | Build and C++ tests | Not run | | Hosted CI | Six checks queued at last refresh | <details><summary>Unedited command output</summary> ~~~text All conventions checks passed. status=passed; findings=0 223b19ba0adb11b66893a57fa656ecf6bc4a45cf:core/libs/prx/libkernel/CMakeLists.txt:1453: add_test(NAME guest_sendfile COMMAND guest_sendfile_tests) ~~~ The heuristic's registration warning was: ~~~text { "findings": [ { "path": "core/libs/tests/GuestSendfile.cpp", "line": 1, "kind": "possible-unregistered-cpp-test", "message": "Changed C++ test source path was not found literally in CMake files; inspect registration, generated source lists, and workflow coverage." } ], "negative_control_candidates": [], "limits": [ "Heuristics can miss weak assertions and can flag valid tests; inspect every finding.", "A test that passes against base is not by itself proof that the PR test is inadequate.", "Negative controls are only candidates until the test actually runs against base and fails by assertion rather than import/setup error." ] } ~~~ The precheck's private helper paths were unavailable in the PR snapshot; for example: ~~~text /usr/bin/python3: can't open file '.../tools/pr_abi_audit.py': [Errno 2] No such file or directory /usr/bin/python3: can't open file '.../tools/pr_test_quality.py': [Errno 2] No such file or directory ~~~ </details> ## Findings ### Blocking - Both new vector allocations, for headerIovecs and trailerIovecs, occur outside a bad_alloc handler. Under memory pressure either resize can throw out of sendfile_nid_postfix, while the later 64 KiB chunk allocation explicitly catches bad_alloc and returns ENOMEM (12). Catch allocation failure for both iovec vectors and return ENOMEM consistently. ### Non-blocking - The added invalid-pointer cases exercise the requested input-copy and output-copy paths. The underlying guest-copy helpers have platform-specific implementations; the PR description reports Windows tests, but I did not run them independently. ## Sources - [New vector allocations and existing ENOMEM handling at head 299c2fe0cbd4246d62bdee1f49d04597164e4037](https://github.com/boykopovar/AnyPS5/blob/299c2fe0cbd4246d62bdee1f49d04597164e4037/core/libs/prx/libkernel/Socket/src/Udp.cpp#L1861-L1895) - [Guest-copy helpers and platform guards at base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf](https://github.com/boykopovar/AnyPS5/blob/223b19ba0adb11b66893a57fa656ecf6bc4a45cf/core/libs/prx/libkernel/Socket/src/Udp.cpp#L1678-L1721) - [guest_sendfile CTest registration at base 223b19ba0adb11b66893a57fa656ecf6bc4a45cf](https://github.com/boykopovar/AnyPS5/blob/223b19ba0adb11b66893a57fa656ecf6bc4a45cf/core/libs/prx/libkernel/CMakeLists.txt#L1449-L1457) ## Not verified - I did not compile or run guest_sendfile locally or independently reproduce the author's Windows test. - Linux and macOS runtime behavior was not tested locally; hosted checks were queued at the last refresh. - I did not run a negative-control mutation. ## To get this to a good state Catch allocation failure for both vector resizes and return ENOMEM, then rerun guest_sendfile on the updated head. I will repeat the same checks there.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!13537
No description provided.