Skip to content

fix(libkernel): return EFAULT from ioctl FIONREAD for an unwritable argument - #12705

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
DeveshB-1:fix/fionread-efault
Oct 29, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
DeveshB-1:fix/fionread-efault

Conversation

@DeveshB-1

Copy link
Copy Markdown
Contributor

What

ioctl(fd, FIONREAD, argument) on a file or pipe wrote to its int argument using std::memcpy after checking destination.Open(). On Linux, GuestArenaBeginHostWrite_nid_postfix always returns true, so an invalid, unmapped, or uncommitted destination pointer (such as (void*)0x10) crashed the host process with a SIGSEGV segmentation fault.

FIONREAD (0x4004667f) is an _IOR request, which FreeBSD kernel copies out via copyout and returns EFAULT when the address is not writable (sys/kern/sys_generic.c). The argument is now copied out with GuestCopyOut_nid_no_patch, returning -1 with errno = EFAULT (14) on unmapped or unwritable addresses.

Complements #12579 (which addressed FIONBIO in DescriptorControl.cpp). Adds test coverage in core/libs/tests/GuestKernelFcntl.cpp for ReadableRequest on both files and pipes with nullptr and 0x10.

Tested

Arch Linux x86_64, GCC 15.2.1, Release, based on main b433c40f2f:

  • ctest --test-dir build -R '^guest_kernel_fcntl$' -V: passed (1/1).
  • ctest --test-dir build -R 'guest_kernel_app_archive' -V: passed (3/3).
  • Negative control: with this PR's test assertions against main's DescriptorControl.cpp, guest_kernel_fcntl deterministically crashed with SIGSEGV (***Exception: SegFault). Restoring GuestCopyOut_nid_no_patch resolves the crash and passes cleanly.
  • python3 tools/check_conventions.py --base origin/main: passed cleanly.
  • python3 core/libs/tests/test_public_headers.py --audit-only: passed (373 libraries, 13414 exports verified).

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

@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 1a47f9f on macOS 26 arm64 and Ubuntu 24.04 glibc 2.39 (x86_64 container, ASan+UBSan). No console was used, and the full guest build was not run because submodules are not initialised here.

Method. I took the FIONREAD tail of Ioctl verbatim from head and from main (b433c40) and compiled it against main's GuestCopyOut_nid_no_patch (General.cpp L815-846). HostWrite::Open() is shimmed to true, which is what GuestArenaBeginHostWrite_nid_postfix returns off Windows. The descriptor was a real pipe holding 3 queued bytes, and each case ran in a forked child.

destination head (macOS = glibc) main (macOS / glibc ASan)
RW page, aligned / +13 misaligned / last 4 bytes of page 0, value 3 0, value 3
nullptr -1, errno 14 -1, errno 14
0x10 -1, errno 14 SIGSEGV / ASan SEGV
read-only page -1, errno 14 SIGBUS / ASan SEGV
PROT_NONE page -1, errno 14 SIGBUS / ASan SEGV
int straddling RW→RO, RO→NONE -1, errno 14 SIGBUS / ASan SEGV
UINTPTR_MAX-1 (wrap), 0xffff800000000000 -1, errno 14 SIGSEGV / ASan SEGV

Head is correct in 11/11 cases and identical on both hosts. Main crashes in 7/11. Folding the nullptr check into GuestCopyOut (start 0 → false) is equivalent. DescriptorControl.cpp already has the declaration through File.hpp → General.hpp on POSIX, as the FIONBIO line from #12579 relies on.

Mutants.

  • The unchecked GuestCopyOut return is killed by both new rows on each descriptor.
  • sizeof(available) → 2 is not killed by these rows. The existing readable == 3 rows still pass because the value fits in the low bytes. It is only visible with a 0xAA-prefilled int (0300aaaa), so it is low value.
  • A third mutant survives: an explicit nullptr / low-address check followed by a direct memcpy. It survives because nullptr and 0x10 are unmapped, not merely unwritable. A read-only mapped page is what tells "checked copy-out" apart from "guarded direct write". GuestDirectoryBuffer.cpp L99-113 already shows how to get one (sceKernelAllocateDirectMemory + sceKernelMapDirectMemory + sceKernelMprotect(mapping, page, 1)). One RequireFailure(ioctl_nid_postfix(pipe[0], ReadableRequest, mapping), GuestEfault) row there would kill it. This is the same gap I noted on #12646's socket path. It is optional, not blocking.

Note. With the straddling int, head writes the 2 bytes on the writable side before reporting EFAULT. FreeBSD's copyout can also leave a partial write, so I would not change this.

Overlap. git merge-tree against main is clean, and so is the pairwise merge with #12646, which covers the socket FIONREAD/FIONBIO path in Udp.cpp and complements this PR. pr_overlap --branch lists only uncomparable file-level shares (#7121, #9019, #9557, #9566), and none of their diffs touch the FIONREAD hunk. check_conventions --base upstream/main is clean.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@DeveshB-1 the change looks right to me: GuestCopyOut_nid_no_patch is used as at the other copy-out sites, nullptr and 0x10 return EFAULT on every host, and file, pipe, socket and archived file all share that one block. Optional, for a follow-up PR rather than this one: sceNetIoctl on a native socket (libSceNet/Export.cpp) still writes available through *static_cast<int*>(argument) (and reads the NBIO argument directly), with only a nullptr check, so 0x10 still crashes through that entry.

@github-actions

Copy link
Copy Markdown

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 7121 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
PR 9019 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
PR 9557 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
PR 9566 core/libs/prx/libkernel/File/src/DescriptorControl.cpp

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Windows run of this head (1a47f9f0ee), merged onto main 3a2b919816 (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 pass 3 of 3 (ctest --repeat until-fail:3).
  • Negative control: with core/libs/prx/libkernel/File/src/DescriptorControl.cpp put back to main's version and everything else from this PR, 1 of the 2 fails.

AI-assisted: yes (Claude Code).

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@adriadam10 we'll merge this once exact-head CI is green, unless you'd rather merge it yourself.

@oneandonlydean oneandonlydean self-assigned this Oct 29, 2026
@oneandonlydean
oneandonlydean merged commit 2960e9a into boykopovar:main Oct 29, 2026
11 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.

5 participants