Repository navigation
fix(libkernel): return EFAULT from socket ioctl for invalid memory pointers - #12646
Conversation
|
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.
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.
|
rooklz
left a comment
There was a problem hiding this comment.
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.nonblockingnor the hostO_NONBLOCK. FIONBIO from a read-only page still succeeds, which is correct because it isIOC_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-treeagainst main is clean. It does conflict with #11219 inGuestKernelFcntl.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:
- The shutdown path checks readability (
GuestCopyIn) and then writes directly. - 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.
|
Correction to my suggested rows: the read-only page is zero-filled, so |
|
Windows run of this head (
AI-assisted: yes (Claude Code). |
ac2f2b0 to
6d16460
Compare
|
@rooklz Added the read-only direct memory page tests for |
6d16460 to
f8c4e2c
Compare
…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.
f8c4e2c to
60874d0
Compare
|
Thanks @JJenkx for running this on Windows 11! I've updated the Tested section in the description with your test results. |
What
argumentpointer passed toioctl_nid_postfixand_ioctl_nid_postfixfor guest sockets usingGuestCopyIn_nid_no_patch(forFIONBIO/ 0x8004667e) andGuestCopyOut_nid_no_patch(forFIONREAD/ 0x4004667f).-1witherrno = 14(EFAULT) whenargumentpoints to unmapped, invalid, or read-only guest memory, preventing host access violations.sys_ioctl(sys/kern/sys_generic.c), whereIOC_INrequests copy the argument viacopyinandIOC_OUTrequests copy the result viacopyout, returningEFAULTimmediately if memory access fails.DescriptorControl.cpp).GuestKernelFcntl.cpp,GuestTcp.cpp, andGuestUdp.cppverifyingEFAULTon invalid pointers with bothioctland_ioctl.Tested
python tools/check_conventions.py --base upstream/main --head HEADreports 0 findings.python core/libs/tests/test_public_headers.py --audit-onlypassed (373 libraries, 13414 exports verified).guest_kernel_fcntl,guest_kernel_fcntl_sync,guest_tcp, andguest_udppassed 3/3 runs; negative control confirmed failing against baseUdp.cpp.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)