fix(libkernel): return EFAULT from socket ioctl for invalid memory pointers #12646

Merged
slopgobblercobbler merged 1 commit from upstream-pr/12646 into main 2026-11-05 11:05:12 +00:00
slopgobblercobbler commented 2026-10-29 00:01:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-29T00:01:57Z.


What

  • Validates the argument pointer passed to ioctl_nid_postfix and _ioctl_nid_postfix for guest sockets using GuestCopyIn_nid_no_patch (for FIONBIO / 0x8004667e) and GuestCopyOut_nid_no_patch (for FIONREAD / 0x4004667f).
  • Returns -1 with errno = 14 (EFAULT) when argument points to unmapped, invalid, or read-only guest memory, preventing host access violations.
  • Matches FreeBSD 11.4 sys_ioctl (sys/kern/sys_generic.c), where IOC_IN requests copy the argument via copyin and IOC_OUT requests copy the result via copyout, returning EFAULT immediately if memory access fails.
  • Completes memory safety coverage for socket ioctl following #12579 (which addressed file descriptor ioctl in DescriptorControl.cpp).
  • Adds regression test assertions in GuestKernelFcntl.cpp, GuestTcp.cpp, and GuestUdp.cpp verifying EFAULT on invalid pointers with both ioctl and _ioctl.

Tested

  • Verified conventions: python tools/check_conventions.py --base upstream/main --head HEAD reports 0 findings.
  • Verified public headers: python core/libs/tests/test_public_headers.py --audit-only passed (373 libraries, 13414 exports verified).
  • Windows 11 host verification courtesy of @JJenkx: Windows 11 Enterprise (Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7), guest_kernel_fcntl, guest_kernel_fcntl_sync, guest_tcp, and guest_udp passed 3/3 runs; negative control confirmed failing against base Udp.cpp.
  • Host-side memory fault matrix courtesy of @rooklz: 30/30 test cases verified under ASan+UBSan across macOS and Ubuntu glibc.

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: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5059671850 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12646). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-29T00:01:57Z. --- ### What - Validates the `argument` pointer passed to `ioctl_nid_postfix` and `_ioctl_nid_postfix` for guest sockets using `GuestCopyIn_nid_no_patch` (for `FIONBIO` / 0x8004667e) and `GuestCopyOut_nid_no_patch` (for `FIONREAD` / 0x4004667f). - Returns `-1` with `errno = 14` (`EFAULT`) when `argument` points to unmapped, invalid, or read-only guest memory, preventing host access violations. - Matches FreeBSD 11.4 `sys_ioctl` (`sys/kern/sys_generic.c`), where `IOC_IN` requests copy the argument via `copyin` and `IOC_OUT` requests copy the result via `copyout`, returning `EFAULT` immediately if memory access fails. - Completes memory safety coverage for socket ioctl following #12579 (which addressed file descriptor ioctl in `DescriptorControl.cpp`). - Adds regression test assertions in `GuestKernelFcntl.cpp`, `GuestTcp.cpp`, and `GuestUdp.cpp` verifying `EFAULT` on invalid pointers with both `ioctl` and `_ioctl`. ### Tested - Verified conventions: `python tools/check_conventions.py --base upstream/main --head HEAD` reports 0 findings. - Verified public headers: `python core/libs/tests/test_public_headers.py --audit-only` passed (373 libraries, 13414 exports verified). - Windows 11 host verification courtesy of @JJenkx: Windows 11 Enterprise (Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7), `guest_kernel_fcntl`, `guest_kernel_fcntl_sync`, `guest_tcp`, and `guest_udp` passed 3/3 runs; negative control confirmed failing against base `Udp.cpp`. - Host-side memory fault matrix courtesy of @rooklz: 30/30 test cases verified under ASan+UBSan across macOS and Ubuntu glibc. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 02:19:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T02:19:34Z.

Review: COMMENTED


Host-side review of ac2f2b0fc0 (macOS 26 arm64 and Ubuntu 24.04 glibc 2.39, no console). The full guest build was not run here because submodules are not initialised.

Method. I extracted ioctl_nid_postfix verbatim from head and from main (3a2b919816). It was compiled under ASan+UBSan against real AF_UNIX stream and datagram socketpairs, using a minimal Socket/Lookup/Fail shim and the same mach_vm_* / process_vm_* copy primitives that General.cpp uses. The test matrix was {FIONBIO, FIONREAD, FIONREAD after receive shutdown} × {RW page, read-only page, PROT_NONE page, 0x10, an int that straddles RW→PROT_NONE}. Each case ran in a forked child.

  • Head: 30/30 cases correct and byte-identical on macOS and glibc. Every fault returns -1/errno 14. A faulting FIONBIO changes neither options.nonblocking nor the host O_NONBLOCK. FIONBIO from a read-only page still succeeds, which is correct because it is IOC_IN. RW pages give 5 queued bytes, or 0 after shutdown.
  • Main: 22/30 cases crash (SIGSEGV/SIGBUS), including both FIONREAD paths into a read-only page.
  • git merge-tree against main is clean. It does conflict with #11219 in GuestKernelFcntl.cpp, which is the row the bot flagged, so one of the two will need a rebase.

Test gap. Two mutants survive the new tests. They do so because 0x10 and nullptr are unreadable as well as unwritable:

  1. The shutdown path checks readability (GuestCopyIn) and then writes directly.
  2. The same change on the queued-bytes path.

On a read-only page each mutant crashes (SIGBUS in the harness), but every new assertion passes. A row with a read-only destination would kill both. For example, in GuestUdp.cpp next to the new rows, following the GuestDirectoryBuffer.cpp CheckReadOnlyDestination pattern:

    std::int64_t physical = 0;
    void* page = nullptr;
    Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, 0x4000, 0, 0, &physical) == 0);
    Require(sceKernelMapDirectMemory(&page, 0x4000, 3, 0, physical, 0) == 0);
    Require(sceKernelMprotect(page, 0x4000, 1) == 0);
    Require(ioctl_nid_postfix(receiver, 0x4004667f, page) == -1 && *__error_nid_postfix() == 14);
    Require(ioctl_nid_postfix(receiver, 0x8004667e, page) == 0);

Add the same 0x4004667f row after shutdown_nid_postfix in GuestTcp.cpp. I have not built this against the real test target, so please check that the target links the direct-memory exports.

The code change looks correct to me.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5692098356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12646#pullrequestreview-5692098356). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T02:19:34Z. **Review:** COMMENTED --- Host-side review of ac2f2b0fc0 (macOS 26 arm64 and Ubuntu 24.04 glibc 2.39, no console). The full guest build was not run here because submodules are not initialised. **Method.** I extracted `ioctl_nid_postfix` verbatim from head and from main (3a2b919816). It was compiled under ASan+UBSan against real AF_UNIX stream and datagram socketpairs, using a minimal `Socket`/`Lookup`/`Fail` shim and the same `mach_vm_*` / `process_vm_*` copy primitives that `General.cpp` uses. The test matrix was {FIONBIO, FIONREAD, FIONREAD after receive shutdown} × {RW page, read-only page, PROT_NONE page, `0x10`, an int that straddles RW→PROT_NONE}. Each case ran in a forked child. - **Head:** 30/30 cases correct and byte-identical on macOS and glibc. Every fault returns -1/errno 14. A faulting FIONBIO changes neither `options.nonblocking` nor the host `O_NONBLOCK`. FIONBIO from a read-only page still succeeds, which is correct because it is `IOC_IN`. RW pages give 5 queued bytes, or 0 after shutdown. - **Main:** 22/30 cases crash (SIGSEGV/SIGBUS), including both FIONREAD paths into a read-only page. - `git merge-tree` against main is clean. It does conflict with #11219 in `GuestKernelFcntl.cpp`, which is the row the bot flagged, so one of the two will need a rebase. **Test gap.** Two mutants survive the new tests. They do so because `0x10` and `nullptr` are unreadable as well as unwritable: 1. The shutdown path checks readability (`GuestCopyIn`) and then writes directly. 2. The same change on the queued-bytes path. On a read-only page each mutant crashes (SIGBUS in the harness), but every new assertion passes. A row with a read-only destination would kill both. For example, in `GuestUdp.cpp` next to the new rows, following the `GuestDirectoryBuffer.cpp` `CheckReadOnlyDestination` pattern: ```cpp std::int64_t physical = 0; void* page = nullptr; Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, 0x4000, 0, 0, &physical) == 0); Require(sceKernelMapDirectMemory(&page, 0x4000, 3, 0, physical, 0) == 0); Require(sceKernelMprotect(page, 0x4000, 1) == 0); Require(ioctl_nid_postfix(receiver, 0x4004667f, page) == -1 && *__error_nid_postfix() == 14); Require(ioctl_nid_postfix(receiver, 0x8004667e, page) == 0); ``` Add the same `0x4004667f` row after `shutdown_nid_postfix` in `GuestTcp.cpp`. I have not built this against the real test target, so please check that the target links the direct-memory exports. The code change looks correct to me.
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!12646
No description provided.