fix(libkernel_sys): reject a null aio_suspend list and a negative kenv length #15115

Merged
MarcZX10 merged 2 commits from upstream-pr/15115 into main 2026-11-10 21:13:16 +00:00
MarcZX10 commented 2026-11-02 17:14:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T17:14:38Z.


What

Two guest-input checks in core/libs/prx/libkernel_sys:

  • aio_suspend dereferenced a null guest list when count > 0. It now returns EFAULT.
  • kenv (KENV_GET) converted a negative len into a huge size_t and copied the whole value out. It now returns EINVAL.

Commits:

  • 194dde4b82 fix(libkernel_sys): aio_suspend rejects a null list with a count
  • a034f502a1 test(libkernel_sys): aio_suspend null list returns EFAULT
  • eada14c49d fix(libkernel_sys): kenv get rejects a negative length
  • dd814262ac test(libkernel_sys): kenv get rejects a negative length

Tested

  • Host: Linux, archive build, lane 1, on the final head.
  • ctest -R '^(guest_kernel_sys|guest_kernel_sys_aio)$': 2 of 2 passed (guest_kernel_sys Test #469, guest_kernel_sys_aio Test #538).
  • tools/check_conventions.py --base upstream/main and git diff --check upstream/main: clean.

Not verified

  • Negative control (old code, new tests) not run.
  • The errno values (EFAULT for the null list, EINVAL for a negative length) are the POSIX choices, not values measured on a console.
  • Not run on Windows or macOS.

Checklist

  • Based on current main; #15025 (kenv set and unset) and #14403 (aio_read/write/fsync) do not touch these lines
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5124359580 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15115). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T17:14:38Z. --- ### What Two guest-input checks in `core/libs/prx/libkernel_sys`: - `aio_suspend` dereferenced a null guest list when `count > 0`. It now returns `EFAULT`. - `kenv` (KENV_GET) converted a negative `len` into a huge `size_t` and copied the whole value out. It now returns `EINVAL`. Commits: - 194dde4b82 fix(libkernel_sys): aio_suspend rejects a null list with a count - a034f502a1 test(libkernel_sys): aio_suspend null list returns EFAULT - eada14c49d fix(libkernel_sys): kenv get rejects a negative length - dd814262ac test(libkernel_sys): kenv get rejects a negative length ### Tested - Host: Linux, archive build, lane 1, on the final head. - `ctest -R '^(guest_kernel_sys|guest_kernel_sys_aio)$'`: 2 of 2 passed (`guest_kernel_sys` Test #469, `guest_kernel_sys_aio` Test #538). - `tools/check_conventions.py --base upstream/main` and `git diff --check upstream/main`: clean. ### Not verified - Negative control (old code, new tests) not run. - The errno values (`EFAULT` for the null list, `EINVAL` for a negative length) are the POSIX choices, not values measured on a console. - Not run on Windows or macOS. ### Checklist - [x] Based on current `main`; #15025 (kenv set and unset) and #14403 (aio_read/write/fsync) do not touch these lines - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-02 17:50:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T17:50:38Z.

Review: DISMISSED


The aio_suspend half matches FreeBSD 11.4's sys_aio_suspend (vfs_aio.c). It rejects nent < 0 or above the limit with EINVAL, then copies in the timeout and then the list, so for a NULL list with nent > 0 the copyin returns EFAULT. The new check sits after the count check and before the timeout is used, which gives the same result.

The kenv half moves away from the kernel it follows, though. In FreeBSD 11.4's sys_kenv, KENV_GET (kern_environment.c) has no length check:

len = strlen(value) + 1;     // len is size_t, uap->len is int
if (len > uap->len)
        len = uap->len;
error = copyout(value, uap->value, len);
td->td_retval[0] = len;

The comparison converts a negative uap->len to a huge size_t, so len stays strlen(value) + 1: the whole value is copied out and that length returned, with no EINVAL. sys_kenv doesn't check uap->len anywhere else for KENV_GET (it is only read at lines 110, 114, 172, 173 and 181). That is what main does now with static_cast<std::size_t>(static_cast<std::int64_t>(len)), so the old cast reads as deliberate, not a bug. Could the kenv change and its test be dropped, unless there's a console measurement showing EINVAL? The PR text calls EINVAL the POSIX choice, but kenv isn't POSIX; this library follows FreeBSD 11.

I haven't built this branch; the above is from the FreeBSD 11.4 sources and the diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5740872386 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15115#pullrequestreview-5740872386). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T17:50:38Z. **Review:** DISMISSED --- The `aio_suspend` half matches FreeBSD 11.4's `sys_aio_suspend` ([vfs_aio.c](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_aio.c#L1937-L1949)). It rejects `nent < 0` or above the limit with `EINVAL`, then copies in the timeout and then the list, so for a NULL list with `nent > 0` the `copyin` returns `EFAULT`. The new check sits after the count check and before the timeout is used, which gives the same result. The `kenv` half moves away from the kernel it follows, though. In FreeBSD 11.4's `sys_kenv`, `KENV_GET` ([kern_environment.c](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_environment.c#L160-L179)) has no length check: ```c len = strlen(value) + 1; // len is size_t, uap->len is int if (len > uap->len) len = uap->len; error = copyout(value, uap->value, len); td->td_retval[0] = len; ``` The comparison converts a negative `uap->len` to a huge `size_t`, so `len` stays `strlen(value) + 1`: the whole value is copied out and that length returned, with no `EINVAL`. `sys_kenv` doesn't check `uap->len` anywhere else for `KENV_GET` (it is only read at lines 110, 114, 172, 173 and 181). That is what `main` does now with `static_cast<std::size_t>(static_cast<std::int64_t>(len))`, so the old cast reads as deliberate, not a bug. Could the `kenv` change and its test be dropped, unless there's a console measurement showing `EINVAL`? The PR text calls `EINVAL` the POSIX choice, but `kenv` isn't POSIX; this library follows FreeBSD 11. I haven't built this branch; the above is from the FreeBSD 11.4 sources and the diff.
adriadam10 commented 2026-11-06 23:29:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-06T23:29:39Z.

Review: CHANGES_REQUESTED


  • aio_suspend (Aio.cpp:244): matches FreeBSD 11.4 sys_aio_suspend, where the copyin of a NULL list returns EFAULT. Fine as is.
  • kenv (Export.cpp:550) and its test (GuestKernelSys.cpp:1189): please drop them. In FreeBSD 11.4 sys_kenv (kern_environment.c), len is a size_t and KENV_GET does if (len > uap->len) len = uap->len;, so a negative uap->len converts to a huge size_t and there is no EINVAL. This also matches BunnyFl's review. EINVAL is not a POSIX choice here (kenv isn't POSIX); without a console measurement we follow the FreeBSD source.

Once the kenv part is gone, the rest is good to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5808424318 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15115#pullrequestreview-5808424318). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-06T23:29:39Z. **Review:** CHANGES_REQUESTED --- - `aio_suspend` (Aio.cpp:244): matches FreeBSD 11.4 `sys_aio_suspend`, where the `copyin` of a NULL list returns `EFAULT`. Fine as is. - `kenv` (Export.cpp:550) and its test (GuestKernelSys.cpp:1189): please drop them. In FreeBSD 11.4 `sys_kenv` (`kern_environment.c`), `len` is a `size_t` and `KENV_GET` does `if (len > uap->len) len = uap->len;`, so a negative `uap->len` converts to a huge `size_t` and there is no `EINVAL`. This also matches BunnyFl's review. `EINVAL` is not a POSIX choice here (`kenv` isn't POSIX); without a console measurement we follow the FreeBSD source. Once the kenv part is gone, the rest is good to merge.
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!15115
No description provided.