fix(libkernel): fail poll with EINVAL when nfds exceeds the descriptor limit #13738
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13738"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-31T03:29:14Z.
What
Guest
pollwith a hugenfds(for example0xffffffff) allocated a host vector of that many descriptors and crashed the emulator. FreeBSD 11.4kern_pollchecks, after the timeout,nfds > maxfilesperproc && nfds > FD_SETSIZEand returnsEINVALbefore it reads the array.pollnow does the same, keeping the order: bad timeoutEINVAL, nfds limitEINVAL, thenEFAULTfor a null or unreadable array or an unwritable result (both already onmainsince #12462).kern.maxfilesperprocis not modelled (the sysctl is still unanswered), so the guestRLIMIT_NOFILEhard limit stands in for it; it is read only whennfdsis above 1024, so the common path is unchanged. 8192 on Windows, the host hard limit elsewhere.Tested
Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0, Ninja, Release,
-DBUILD_TESTING=ON, currentmain.guest_poll:0xffffffffdescriptors with a valid, a null array givesEINVAL; at the limit with a null array givesEFAULT, limit + 1 givesEINVALeven with a0x10array. The check on the limit boundary runs when the hard limit is between 1024 and 100000 (it is on Windows). Onmainthe test crashes (0xc0000409) three runs out of three; with this change it passes three runs out of three.vulkan_validation_gate_reportsandcheck_compile_test_windows_gcc_response_text_mode_is_not_reinterpreted, Python tooling tests that fail here because the Microsoft Storepython.execannot be read (OSError: Invalid argument); they fail alone three runs out of three and have nothing to do withpoll.pr_overlap_encoding,agc_host_import_cut_lifetimeandagc_driver_depth_plane_partial_storepass alone.python tools/check_conventions.py --base upstream/main: passes.RLIMIT_NOFILEhard limit and so lower thepolllimit, whichmaxfilesperprocwould not follow; on a Linux host with an extremenr_opena count just below the limit can still fail to allocate.Overlaps
tools/pr_overlap.pycould not compareUdp.cppwith #5021, #7088, #8605, #9713, #10645, #12682, #12688, #12700 and #12965 because they conflict withmainthemselves; I read their diffs and none touchespoll_nid_postfix. #11223 and #11247 change whatpollreports for sockets, not argument checks.Checklist
main; no other open PR implements the same functionNotImplemented_nid_no_patch); no silent stubs added.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T03:41:33Z.
Review: COMMENTED
Tests run and reviewed — Linux path remains unverified
I found no confirmed code defect in head
757c08436833a189e22c7510f536c19591b91ac9, but the Linux product job was skipped, so I am withholding approval pending Linux-specific validation.What I ran
Reviewed head
757c08436833a189e22c7510f536c19591b91ac9against PR base6475267b456de6a0a23b230aa751f0b917d12688; no local build or C++ test was run. Environment: Linux x86_64, GCC 12.2.0, Python 3.11.2.Results
git diff --checkmainde824c2919ca65086c6324a1e0388866a7327eea.Raw output for locally run convention check and precheck helper errors
Findings
Blocking: None confirmed.
Non-blocking: The changed path relies on the guest
RLIMIT_NOFILEhard limit on non-Windows hosts; the PR notes this is a substitute for unmodelledkern.maxfilesperproc. Windows CI passed, but the Linux product job was skipped, so the primary non-Windows path has not been independently exercised here. The PR's Windows test results are author-reported, not locally reproduced.Sources
poll_nid_postfixand boundary tests at the reviewed headRLIMIT_NOFILEimplementation at the reviewed headNot verified
GuestPolltest locally; it requires an exact-head build, which was not run.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-31T04:11:13Z.
Review: COMMENTED
Linux run of this head (
757c084368), merged ontomain6475267b45(local only). Ubuntu 26.04 LTS (kernel 7.0), Intel Core i7-8700, GCC 15.2.0, Ninja, Release,BUILD_TESTING=ON.guest_pollpasses 3 of 3;public_headerspasses;ctest -R "poll|socket|epoll|fcntl"28/28.Udp.cppback tomain(test kept):guest_pollaborts 3 of 3 (~19 s, the0xffffffffallocation).The at-limit block does not run on Linux with the default limits. The guest
RLIMIT_NOFILEhard limit is the host one, 524288 here (systemd's defaultnofilehard limit on current distros), sofileLimit.rlim_max < 100000is false and the three boundary checks are skipped. I confirmed it with a temporaryfprintfinside the block:rlim_maxseen by the testguest_pollulimit -n 4096So the fix itself is fine on Linux, but only the
0xffffffffchecks cover it in CI-like setups. On macOS the hard limit is usuallyunlimited, so the block is skipped there too.Two ways to make it always run:
nullptrat the limit returnsEFAULTand limit + 1 returnsEINVALbefore any allocation, so the block only needsrlim_maxto fit inuint32_t(e.g.rlim_max >= 1024 && rlim_max < 0xffffffff). That covers Linux's 524288.rlimit r{}; getrlimit(RLIMIT_NOFILE, &r); r.rlim_max = 4096; r.rlim_cur = std::min<rlim_t>(r.rlim_cur, 4096); setrlimit(RLIMIT_NOFILE, &r);before readingfileLimit(lowering the hard limit needs no privileges).Also checked together with #13695 (both touch
Udp.cpp) onmain2364892547: they merge cleanly, the full build passes,guest_pollandguest_kernel_fcntlpass 3 of 3, the socket/poll/fcntl subset 52/52 andpublic_headerspass.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.