fix(libkernel_sys): read the kenv name within its 129-byte bound, as FreeBSD 11.4 does #21984

Merged
brajankw merged 1 commit from upstream-pr/21984 into main 2026-11-20 04:47:03 +00:00
brajankw commented 2026-11-17 00:45:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-17T00:45:16Z.


What

kenv copied the name with the guest path copy, which reads up to 1024 bytes, and checked its length afterwards. An unterminated name of 129 readable bytes followed by an unreadable page therefore failed with EFAULT, where FreeBSD 11.4's sys_kenv reads it with copyinstr(name, KENV_MNAMELEN + 1), stops at 129 bytes and fails with ENAMETOOLONG (the last sentence of the kenv entry in docs/dev/debt/libkernel_sys.md).

  • The name is now copied with CopyGuestString_nid_no_patch(name, KenvNameLength + 1, ...), which returns ENAMETOOLONG when no NUL is found within the bound and EFAULT when it faults first. The helper that did the length check afterwards is gone.
  • GuestKernelSys.cpp: 129 n bytes ending at a PROT_NONE page give ENAMETOOLONG; the last 128 of them give EFAULT, as copyinstr faults on the 129th byte.
  • The debt fragment drops that sentence; the KENV_DUMP size the console reports stays open.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main 258ab7b6bd.

  • ctest -R '^guest_kernel_sys$': passed.
  • Negative control: with main's Export.cpp and this branch's test, guest_kernel_sys fails with Kernel sys check failed at line 1244 (guest errno 14), the 129-byte case.
  • Full ctest (5891 tests, -j 12) with this branch merged together with four unrelated fixes of mine: 5875 passed. The 16 failures are the set that fails on main in the same environment: six agc_driver cases (agc_driver_image_table_packed_guards passes when run on its own) and ten that need a .git directory, network or a passwd entry for the user.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch: failed closed on #9389 and #17936, which conflict with main; neither diff touches kenv or its test.
  • Not tested on Windows or macOS. No title.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5348077708 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21984). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T00:45:16Z. --- ### What `kenv` copied the name with the guest path copy, which reads up to 1024 bytes, and checked its length afterwards. An unterminated name of 129 readable bytes followed by an unreadable page therefore failed with `EFAULT`, where FreeBSD 11.4's [`sys_kenv`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_environment.c#L87-L204) reads it with `copyinstr(name, KENV_MNAMELEN + 1)`, stops at 129 bytes and fails with `ENAMETOOLONG` (the last sentence of the `kenv` entry in `docs/dev/debt/libkernel_sys.md`). - The name is now copied with `CopyGuestString_nid_no_patch(name, KenvNameLength + 1, ...)`, which returns `ENAMETOOLONG` when no NUL is found within the bound and `EFAULT` when it faults first. The helper that did the length check afterwards is gone. - `GuestKernelSys.cpp`: 129 `n` bytes ending at a `PROT_NONE` page give `ENAMETOOLONG`; the last 128 of them give `EFAULT`, as `copyinstr` faults on the 129th byte. - The debt fragment drops that sentence; the `KENV_DUMP` size the console reports stays open. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Debian 13, GCC 14.2.0, Release), on `main` `258ab7b6bd`. - `ctest -R '^guest_kernel_sys$'`: passed. - Negative control: with `main`'s `Export.cpp` and this branch's test, `guest_kernel_sys` fails with `Kernel sys check failed at line 1244 (guest errno 14)`, the 129-byte case. - Full `ctest` (5891 tests, `-j 12`) with this branch merged together with four unrelated fixes of mine: 5875 passed. The 16 failures are the set that fails on `main` in the same environment: six `agc_driver` cases (`agc_driver_image_table_packed_guards` passes when run on its own) and ten that need a `.git` directory, network or a passwd entry for the user. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch`: failed closed on #9389 and #17936, which conflict with `main`; neither diff touches `kenv` or its test. - Not tested on Windows or macOS. No title. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-17 00:48:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T00:48:49Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 98d7559fe5:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_tests built cleanly.
  • CTest (guest_kernel_sys):
    1/1 Test #848: guest_kernel_sys .................   Passed    0.23 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel_sys/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #848: guest_kernel_sys .................Subprocess aborted***Exception:   0.67 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified CopyGuestString_nid_no_patch is called with KenvNameLength + 1 (129 bytes) matching FreeBSD 11.4 copyinstr(name, KENV_MNAMELEN + 1)
    • Verified bounds handling: strings >= 129 bytes without a NUL byte produce GuestEnametoolong (63) without touching subsequent unmapped memory
    • Verified fault handling: strings within 128 bytes adjacent to unmapped memory fault on unmapped read and return GuestEfault (14)
    • Verified removal of CopyKernelEnvString and direct error propagation via Fail(error)
    • Verified GuestKernelSys.cpp unit tests appropriately verify 129-byte ENAMETOOLONG and 128-byte EFAULT edge cases using GuardedPaths
    • Verified docs/dev/debt/changes/ fragment complies with repository conventions and documentation rules

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5930669507 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21984#pullrequestreview-5930669507). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T00:48:49Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `98d7559fe5`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_sys_tests` built cleanly. - **CTest** (`guest_kernel_sys`): ``` 1/1 Test #848: guest_kernel_sys ................. Passed 0.23 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel_sys/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #848: guest_kernel_sys .................Subprocess aborted***Exception: 0.67 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified CopyGuestString_nid_no_patch is called with KenvNameLength + 1 (129 bytes) matching FreeBSD 11.4 copyinstr(name, KENV_MNAMELEN + 1) - Verified bounds handling: strings >= 129 bytes without a NUL byte produce GuestEnametoolong (63) without touching subsequent unmapped memory - Verified fault handling: strings within 128 bytes adjacent to unmapped memory fault on unmapped read and return GuestEfault (14) - Verified removal of CopyKernelEnvString and direct error propagation via Fail(error) - Verified GuestKernelSys.cpp unit tests appropriately verify 129-byte ENAMETOOLONG and 128-byte EFAULT edge cases using GuardedPaths - Verified docs/dev/debt/changes/ fragment complies with repository conventions and documentation rules Looks Good To Me
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!21984
No description provided.