fix(libkernel): follow FreeBSD in setgroups and getgroups #4734
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4734"
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 frvnzz on 2026-10-12T20:02:53Z.
What
Follow-up to #3893 (#3862), which added
setgroupsandgetgroupsto libkernel. Three of their results differ from FreeBSD 11.4, which their technical debt entry says they follow:setgroupsrejected 1024 entries withEINVAL.sys_setgroupsfails only abovengroups_max + 1(kern_prot.c:819), andngroups_maxdefaults toNGROUPS_MAX, 1023 (syslimits.h:58, subr_param.c:210), so 1024 entries (the effective group and 1023 supplementary groups) are accepted.guest_credentialsasserted theEINVAL.setgroupskept the entries in the caller's order.crsetgroups_lockedsorts the entries after the effective group (kern_prot.c:2076-2081), sogetgroupsreturns them sorted.getgroupsfailed a negative count withEINVAL. Both system calls take the count asu_int gidsetsize(syscalls.master:186-189) while libc declares itint(unistd.h:342, 558), so a negative count is a large one and the list is copied out (kern_prot.c:313-320), into a buffer that must hold it. FreeBSD 13.1 and later take anintand fail a negative count withEINVAL(4bc2174a1b48).guest_credentialsnow sets an unsorted list, reads it back with a negative count into a 1024-entry buffer and checks that only the list is written, checks that 1024 entries are accepted and 1025 rejected, and reads all 1024 back with a negative count. Their entry indocs/dev/debt/libkernel.mdnow states the limit, the order and that a negativegetgroupscount follows FreeBSD 11.Tested
Process.cpp, copied verbatim with their constants, built with the unmodifiedGuestCredentials.cppand run under AddressSanitizer and UndefinedBehaviorSanitizer. The test passes with this change and aborts withmain's functions. With one change reverted at a time it aborts at the order check (line 89), the negative-count check (line 92) and the 1024-entry check (line 104).mainat2a5eca11c(run), before the test change from the review: every job passes, andctestpasses 967 of 967 tests on Linux and 973 of 973 on Windows,guest_credentialsincluded. A build ofmainalone gives the same counts (run), and the Linux slow-test warnings foragc_driver_*tests appear there too.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T04:33:50Z.
Review: COMMENTED
The new unsigned comparison makes a negative
capacitylook very large.getgroups_nid_postfix(-1, groups.data())therefore passes the check and copiescountelements into the caller's buffer; a caller that supplied storage based on the negative capacity can be overrun. The current test explicitly expects this call to succeed.FreeBSD declares
sys_getgroups'sgidsetsizeasintand returnsEINVALwhen a nonzero size is less than the number of groups, so-1is rejected beforecopyout(seekern_prot.c,sys_getgroups). Please keep the signed capacity check and assertgetgroups(-1, groups.data())returnsEINVAL.Imported from boykopovar/AnyPS5. Originally posted by frvnzz on 2026-10-13T18:12:20Z.
Thanks for the review. A negative size failing with
EINVALis FreeBSD 13.1 and later. The libkernel entries indocs/dev/debt/libkernel.mdcite FreeBSD 11, where the size is unsigned:getgroups(u_int gidsetsize, gid_t *gidset)(syscalls.master:186-187), andsys_getgroupscompares it with the unsignedngrpbefore thecopyout(kern_prot.c:297-324).-1isUINT_MAXthere, passes the< ngrpcheck, and the list is copied out.intsize came with 4bc2174a1b48 ("kern: fail getgroup and setgroup with negative int", June 2021), merged to stable/13 for 13.1. stable/11 and stable/12 still takeu_int.The copy is
countentries, as FreeBSD 11 copiesngrp, so a caller that passes-1with a smaller buffer is overrun the same way there. If the maintainers would rather follow 13.1 and later here, I'll drop this part and keep the limit and order fixes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T18:37:15Z.
Thanks for the FreeBSD 11 evidence. That settles which ABI this repo is targeting, but it also makes the negative-count
getgroupscase more consequential: withcapacity == -1, the implementation copies the entire supplementary-group vector to the caller's pointer. The new regression passes a three-entry buffer, so it verifies the ABI return/count behavior while relying on a buffer that happens to fit the current three groups.Could the test use a 1024-entry buffer and should the implementation bound the guest write using the guest allocation/access model? If matching FreeBSD 11 means an undersized buffer is inherently caller error, please state that choice explicitly; as written, the test appears to bless an unsafe negative-count call without exercising the real overrun case. I agree the version distinction belongs in the compatibility note.
Imported from boykopovar/AnyPS5. Originally posted by frvnzz on 2026-10-13T18:48:53Z.
Thanks, both are in
e7f05b3f7:gidsetsize, so a negative count copies the whole list and a smaller buffer is overrun as on FreeBSD 11, and that FreeBSD 13.1 and later fail a negative count withEINVAL.On bounding the write: it is bounded by the list, at most 1024 entries, as FreeBSD 11 copies
ngrp. Neithercopyoutnor the emulator's records of guest memory (GuestAllocations,GuestRangeMapped) know the size of the buffer a caller passes, only which ranges are mapped. Failing an unmapped destination withEFAULTascopyoutdoes would apply to every libkernel function that fills a caller's buffer, so I'd keep that out of this PR.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T18:50:38Z.
Thanks, this addresses my concern. I checked the updated diff: the negative-count regression now uses a 1024-entry buffer, confirms the three-entry list leaves the next slot untouched, and covers the maximum 1024-entry list; the debt note also states the FreeBSD 11 vs 13.1 behavior. I agree a general guest-pointer bounds change is outside this PR.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:38:32Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed