Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
slopgobblercobbler:fix/libkernel-socket-ioctl-efault
Nov 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
slopgobblercobbler:fix/libkernel-socket-ioctl-efault

Conversation

@slopgobblercobbler

@slopgobblercobbler slopgobblercobbler commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

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 fix(libkernel): return EFAULT from ioctl FIONBIO for an unreadable argument #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

@oneandonlydean oneandonlydean self-assigned this Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 11219 core/libs/tests/GuestKernelFcntl.cpp L222

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.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/tests/GuestUdp.cpp
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/tests/GuestUdp.cpp
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/tests/GuestUdp.cpp
PR 13486 core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/tests/GuestUdp.cpp
PR 13560 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 15785 core/libs/prx/libkernel/Socket/src/Udp.cpp

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Host-side review of ac2f2b0 (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 (3a2b919). 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.

@rooklz

rooklz commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Correction to my suggested rows: the read-only page is zero-filled, so ioctl_nid_postfix(receiver, 0x8004667e, page) == 0 also switches receiver back to blocking mode. Either drop that FIONBIO row (the FIONREAD row alone kills both mutants) or write the current mode into the page before sceKernelMprotect.

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Windows run of this head (ac2f2b0fc0), merged onto main 52c09d567d (clean merge, local only). Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, no Vulkan driver visible.

  • This PR: guest_kernel_fcntl, guest_kernel_fcntl_sync, guest_tcp, guest_udp pass 3 of 3 (ctest --repeat until-fail:3).
  • Negative control: with this PR's source files (core/libs/prx/libkernel/Socket/src/Udp.cpp) put back to main's version and its tests kept, 3 of the 4 fail.

AI-assisted: yes (Claude Code).

@slopgobblercobbler
slopgobblercobbler force-pushed the fix/libkernel-socket-ioctl-efault branch from ac2f2b0 to 6d16460 Compare October 29, 2026 09:45
@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

@rooklz Added the read-only direct memory page tests for 0x4004667f (FIONREAD) using sceKernelAllocateDirectMemory and sceKernelMprotect in both core/libs/tests/GuestUdp.cpp and core/libs/tests/GuestTcp.cpp (omitting the FIONBIO row to preserve blocking mode, as suggested). Verified conventions (python tools/check_conventions.py --base upstream/main --head HEAD, 0 findings), amended commit, and force-pushed to fix/libkernel-socket-ioctl-efault (head 6d16460ee4).

@slopgobblercobbler
slopgobblercobbler force-pushed the fix/libkernel-socket-ioctl-efault branch from 6d16460 to f8c4e2c Compare October 29, 2026 10:44
…inters

ioctl(FIONBIO) on a guest socket read its int argument directly, while ioctl(FIONREAD) wrote its int result directly, so passing a non-null pointer into unmapped or read-only memory crashed the host with an access violation. FIONBIO and FIONREAD are _IOW and _IOR requests, which FreeBSD copies in and out using copyin and copyout, returning EFAULT on inaccessible memory. The argument is now safely checked using GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch.
@slopgobblercobbler
slopgobblercobbler force-pushed the fix/libkernel-socket-ioctl-efault branch from f8c4e2c to 60874d0 Compare October 29, 2026 11:03
@slopgobblercobbler

Copy link
Copy Markdown
Contributor Author

Thanks @JJenkx for running this on Windows 11! I've updated the Tested section in the description with your test results.

@oneandonlydean
oneandonlydean merged commit 9c267bc into boykopovar:main Nov 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants