Repository navigation
fix(libkernel): return EFAULT from ioctl FIONREAD for an unwritable argument - #12705
Conversation
rooklz
left a comment
There was a problem hiding this comment.
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
GuestCopyOutreturn is killed by both new rows on each descriptor. sizeof(available)→ 2 is not killed by these rows. The existingreadable == 3rows 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 directmemcpy. It survives becausenullptrand0x10are unmapped, not merely unwritable. A read-only mapped page is what tells "checked copy-out" apart from "guarded direct write".GuestDirectoryBuffer.cppL99-113 already shows how to get one (sceKernelAllocateDirectMemory+sceKernelMapDirectMemory+sceKernelMprotect(mapping, page, 1)). OneRequireFailure(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.
|
@DeveshB-1 the change looks right to me: |
|
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.
|
|
Windows run of this head (
AI-assisted: yes (Claude Code). |
|
@adriadam10 we'll merge this once exact-head CI is green, unless you'd rather merge it yourself. |
What
ioctl(fd, FIONREAD, argument)on a file or pipe wrote to its int argument usingstd::memcpyafter checkingdestination.Open(). On Linux,GuestArenaBeginHostWrite_nid_postfixalways returnstrue, so an invalid, unmapped, or uncommitted destination pointer (such as(void*)0x10) crashed the host process with aSIGSEGVsegmentation fault.FIONREAD(0x4004667f) is an_IORrequest, which FreeBSD kernel copies out viacopyoutand returnsEFAULTwhen the address is not writable (sys/kern/sys_generic.c). The argument is now copied out withGuestCopyOut_nid_no_patch, returning-1witherrno = EFAULT(14) on unmapped or unwritable addresses.Complements #12579 (which addressed
FIONBIOinDescriptorControl.cpp). Adds test coverage incore/libs/tests/GuestKernelFcntl.cppforReadableRequeston both files and pipes withnullptrand0x10.Tested
Arch Linux x86_64, GCC 15.2.1, Release, based on
mainb433c40f2f: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).main'sDescriptorControl.cpp,guest_kernel_fcntldeterministically crashed withSIGSEGV(***Exception: SegFault). RestoringGuestCopyOut_nid_no_patchresolves 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
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)