fix(libkernel): follow FreeBSD in setgroups and getgroups #4734

Closed
frvnzz wants to merge 2 commits from upstream-pr/4734 into main
frvnzz commented 2026-10-12 20:02:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frvnzz on 2026-10-12T20:02:53Z.


What

Follow-up to #3893 (#3862), which added setgroups and getgroups to libkernel. Three of their results differ from FreeBSD 11.4, which their technical debt entry says they follow:

  • setgroups rejected 1024 entries with EINVAL. sys_setgroups fails only above ngroups_max + 1 (kern_prot.c:819), and ngroups_max defaults to NGROUPS_MAX, 1023 (syslimits.h:58, subr_param.c:210), so 1024 entries (the effective group and 1023 supplementary groups) are accepted. guest_credentials asserted the EINVAL.
  • setgroups kept the entries in the caller's order. crsetgroups_locked sorts the entries after the effective group (kern_prot.c:2076-2081), so getgroups returns them sorted.
  • getgroups failed a negative count with EINVAL. Both system calls take the count as u_int gidsetsize (syscalls.master:186-189) while libc declares it int (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 an int and fail a negative count with EINVAL (4bc2174a1b48).

guest_credentials now 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 in docs/dev/debt/libkernel.md now states the limit, the order and that a negative getgroups count follows FreeBSD 11.

Tested

  • macOS 27.0.1 (Apple M4 Pro), Apple clang 21.0.0: the credential functions of Process.cpp, copied verbatim with their constants, built with the unmodified GuestCredentials.cpp and run under AddressSanitizer and UndefinedBehaviorSanitizer. The test passes with this change and aborts with main'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).
  • The Build workflow, run manually on my fork with the first commit merged into main at 2a5eca11c (run), before the test change from the review: every job passes, and ctest passes 967 of 967 tests on Linux and 973 of 973 on Windows, guest_credentials included. A build of main alone gives the same counts (run), and the Linux slow-test warnings for agc_driver_* tests appear there too.
  • tools/check_conventions.py --base upstream/main and git diff --check pass.

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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834393201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734). Originally posted by [frvnzz](https://github.com/frvnzz) on 2026-10-12T20:02:53Z. --- ### What Follow-up to #3893 (#3862), which added `setgroups` and `getgroups` to libkernel. Three of their results differ from FreeBSD 11.4, which their technical debt entry says they follow: - `setgroups` rejected 1024 entries with `EINVAL`. `sys_setgroups` fails only above `ngroups_max + 1` ([kern_prot.c:819](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_prot.c#L819)), and `ngroups_max` defaults to `NGROUPS_MAX`, 1023 ([syslimits.h:58](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/sys/syslimits.h#L58), [subr_param.c:210](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/subr_param.c#L210)), so 1024 entries (the effective group and 1023 supplementary groups) are accepted. `guest_credentials` asserted the `EINVAL`. - `setgroups` kept the entries in the caller's order. `crsetgroups_locked` sorts the entries after the effective group ([kern_prot.c:2076-2081](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_prot.c#L2076-L2081)), so `getgroups` returns them sorted. - `getgroups` failed a negative count with `EINVAL`. Both system calls take the count as `u_int gidsetsize` ([syscalls.master:186-189](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/syscalls.master#L186-L189)) while libc declares it `int` ([unistd.h:342](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/include/unistd.h#L342), [558](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/include/unistd.h#L558)), so a negative count is a large one and the list is copied out ([kern_prot.c:313-320](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_prot.c#L313-L320)), into a buffer that must hold it. FreeBSD 13.1 and later take an `int` and fail a negative count with `EINVAL` ([4bc2174a1b48](https://github.com/freebsd/freebsd-src/commit/4bc2174a1b489c36195ccc8cfc15e0775b817c69)). `guest_credentials` now 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 in `docs/dev/debt/libkernel.md` now states the limit, the order and that a negative `getgroups` count follows FreeBSD 11. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. --> - macOS 27.0.1 (Apple M4 Pro), Apple clang 21.0.0: the credential functions of `Process.cpp`, copied verbatim with their constants, built with the unmodified `GuestCredentials.cpp` and run under AddressSanitizer and UndefinedBehaviorSanitizer. The test passes with this change and aborts with `main`'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). - The Build workflow, run manually on my fork with the first commit merged into `main` at 2a5eca11c ([run](https://github.com/frvnzz/AnyPS5/actions/runs/38291059628)), before the test change from the review: every job passes, and `ctest` passes 967 of 967 tests on Linux and 973 of 973 on Windows, `guest_credentials` included. A build of `main` alone gives the same counts ([run](https://github.com/frvnzz/AnyPS5/actions/runs/38286563962)), and the Linux slow-test warnings for `agc_driver_*` tests appear there too. - `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-13 04:33:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T04:33:50Z.

Review: COMMENTED


The new unsigned comparison makes a negative capacity look very large. getgroups_nid_postfix(-1, groups.data()) therefore passes the check and copies count elements 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's gidsetsize as int and returns EINVAL when a nonzero size is less than the number of groups, so -1 is rejected before copyout (see kern_prot.c, sys_getgroups). Please keep the signed capacity check and assert getgroups(-1, groups.data()) returns EINVAL.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5498853766 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734#pullrequestreview-5498853766). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T04:33:50Z. **Review:** COMMENTED --- The new unsigned comparison makes a negative `capacity` look very large. `getgroups_nid_postfix(-1, groups.data())` therefore passes the check and copies `count` elements 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`'s `gidsetsize` as `int` and returns `EINVAL` when a nonzero size is less than the number of groups, so `-1` is rejected before `copyout` (see [`kern_prot.c`, `sys_getgroups`](https://cgit.freebsd.org/src/tree/sys/kern/kern_prot.c#n3623)). Please keep the signed capacity check and assert `getgroups(-1, groups.data())` returns `EINVAL`.
frvnzz commented 2026-10-13 18:12:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frvnzz on 2026-10-13T18:12:20Z.


Thanks for the review. A negative size failing with EINVAL is FreeBSD 13.1 and later. The libkernel entries in docs/dev/debt/libkernel.md cite FreeBSD 11, where the size is unsigned:

  • FreeBSD 11 and 12 declare getgroups(u_int gidsetsize, gid_t *gidset) (syscalls.master:186-187), and sys_getgroups compares it with the unsigned ngrp before the copyout (kern_prot.c:297-324). -1 is UINT_MAX there, passes the < ngrp check, and the list is copied out.
  • The int size 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 take u_int.

The copy is count entries, as FreeBSD 11 copies ngrp, so a caller that passes -1 with 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152095325 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734#issuecomment-6152095325). Originally posted by [frvnzz](https://github.com/frvnzz) on 2026-10-13T18:12:20Z. --- Thanks for the review. A negative size failing with `EINVAL` is FreeBSD 13.1 and later. The libkernel entries in `docs/dev/debt/libkernel.md` cite FreeBSD 11, where the size is unsigned: - FreeBSD 11 and 12 declare `getgroups(u_int gidsetsize, gid_t *gidset)` ([syscalls.master:186-187](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/syscalls.master#L186-L187)), and `sys_getgroups` compares it with the unsigned `ngrp` before the `copyout` ([kern_prot.c:297-324](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_prot.c#L297-L324)). `-1` is `UINT_MAX` there, passes the `< ngrp` check, and the list is copied out. - The `int` size came with [4bc2174a1b48](https://github.com/freebsd/freebsd-src/commit/4bc2174a1b489c36195ccc8cfc15e0775b817c69) ("kern: fail getgroup and setgroup with negative int", June 2021), merged to stable/13 for 13.1. stable/11 and stable/12 still take `u_int`. The copy is `count` entries, as FreeBSD 11 copies `ngrp`, so a caller that passes `-1` with 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.
MarcZX10 commented 2026-10-13 18:37:15 +00:00 (Migrated from github.com)

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 getgroups case more consequential: with capacity == -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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152543498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734#issuecomment-6152543498). Originally posted by [MarcZX10](https://github.com/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 `getgroups` case more consequential: with `capacity == -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.
frvnzz commented 2026-10-13 18:48:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frvnzz on 2026-10-13T18:48:53Z.


Thanks, both are in e7f05b3f7:

  • The negative-count read now uses a 1024-entry buffer and checks that only the three entries of the list are written; the fourth stays unchanged. A second negative-count read returns all 1024 entries of the largest list.
  • The debt entry now says the count is FreeBSD 11's unsigned 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 with EINVAL.

On bounding the write: it is bounded by the list, at most 1024 entries, as FreeBSD 11 copies ngrp. Neither copyout nor 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 with EFAULT as copyout does would apply to every libkernel function that fills a caller's buffer, so I'd keep that out of this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152751239 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734#issuecomment-6152751239). Originally posted by [frvnzz](https://github.com/frvnzz) on 2026-10-13T18:48:53Z. --- Thanks, both are in e7f05b3f7: - The negative-count read now uses a 1024-entry buffer and checks that only the three entries of the list are written; the fourth stays unchanged. A second negative-count read returns all 1024 entries of the largest list. - The debt entry now says the count is FreeBSD 11's unsigned `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 with `EINVAL`. On bounding the write: it is bounded by the list, at most 1024 entries, as FreeBSD 11 copies `ngrp`. Neither `copyout` nor 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 with `EFAULT` as `copyout` does would apply to every libkernel function that fills a caller's buffer, so I'd keep that out of this PR.
MarcZX10 commented 2026-10-13 18:50:38 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152782633 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734#issuecomment-6152782633). Originally posted by [MarcZX10](https://github.com/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.
oneandonlydean commented 2026-10-14 02:38:32 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159653026 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4734#issuecomment-6159653026). Originally posted by [oneandonlydean](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4734
No description provided.