fix(libkernel_sys): fail kenv set and unset with EPERM for a jailed caller #15025

Merged
Gruzzberry merged 1 commit from upstream-pr/15025 into main 2026-11-03 05:13:21 +00:00
Gruzzberry commented 2026-11-02 10:50:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-11-02T10:50:30Z.


What

kenv(KENV_SET, ...) and kenv(KENV_UNSET, ...) kept an in-memory environment and succeeded. FreeBSD 11.4's sys_kenv calls priv_check(td, PRIV_KENV_SET) or priv_check(td, PRIV_KENV_UNSET) before it copies the name in, and prison_priv_check does not list either privilege, so a jailed caller reaches default: return (EPERM). A title runs as jailed uid 0 (the credential model agreed on #11216).

Both operations now fail with EPERM without touching the name argument, so a null or 0x10 name gives EPERM too, as the source order says. KENV_DUMP still returns 0 for any arguments, and KENV_GET still copies the name in first (EFAULT, ENAMETOOLONG) and then gives ENOENT. An unknown what value gives EINVAL after the same name copy-in. The environment is empty and cannot be changed, so the table, its mutex and its entry limit are removed.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on current main (rebased).

  • guest_kernel_sys: SET and UNSET give EPERM for a valid name, a null name, a 0x10 name and an over-long name, and GET is still ENOENT afterwards; DUMP, GET and the unknown what checks are as listed above. With main's Export.cpp the test aborts; with this change it passes, three runs out of three.
  • Full suite: the only failures are vulkan_validation_gate_reports and response_text_mode, which fail on main in this environment too.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux, macOS, a console, a title run. The jail model follows prison_priv_check and has not been measured on a console; the debt entry in docs/dev/debt/libkernel_sys.md says so.

Overlaps

tools/pr_overlap.py could not compare this branch against 12 older pull requests (#5021, #9097, #9297, #9302, #9389, #9713, #11212, #12642, #12925, #13129, #13253, #13256) because each already conflicts with main on its own. None of their diffs mentions kenv or KernelEnv, so no overlap is known.

Checklist

  • Based on current main; no other open PR implements the same function
  • 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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5119077023 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15025). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-11-02T10:50:30Z. --- ### What `kenv(KENV_SET, ...)` and `kenv(KENV_UNSET, ...)` kept an in-memory environment and succeeded. FreeBSD 11.4's [`sys_kenv`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_environment.c) calls `priv_check(td, PRIV_KENV_SET)` or `priv_check(td, PRIV_KENV_UNSET)` before it copies the name in, and [`prison_priv_check`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_jail.c) does not list either privilege, so a jailed caller reaches `default: return (EPERM)`. A title runs as jailed uid 0 (the credential model agreed on #11216). Both operations now fail with `EPERM` without touching the name argument, so a null or `0x10` name gives `EPERM` too, as the source order says. `KENV_DUMP` still returns 0 for any arguments, and `KENV_GET` still copies the name in first (`EFAULT`, `ENAMETOOLONG`) and then gives `ENOENT`. An unknown `what` value gives `EINVAL` after the same name copy-in. The environment is empty and cannot be changed, so the table, its mutex and its entry limit are removed. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on current `main` (rebased). - `guest_kernel_sys`: `SET` and `UNSET` give `EPERM` for a valid name, a null name, a `0x10` name and an over-long name, and `GET` is still `ENOENT` afterwards; `DUMP`, `GET` and the unknown `what` checks are as listed above. With `main`'s `Export.cpp` the test aborts; with this change it passes, three runs out of three. - Full suite: the only failures are `vulkan_validation_gate_reports` and `response_text_mode`, which fail on `main` in this environment too. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux, macOS, a console, a title run. The jail model follows `prison_priv_check` and has not been measured on a console; the debt entry in `docs/dev/debt/libkernel_sys.md` says so. ### Overlaps `tools/pr_overlap.py` could not compare this branch against 12 older pull requests (#5021, #9097, #9297, #9302, #9389, #9713, #11212, #12642, #12925, #13129, #13253, #13256) because each already conflicts with `main` on its own. None of their diffs mentions `kenv` or `KernelEnv`, so no overlap is known. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [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 AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
BunnyFl commented 2026-11-02 11:20:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T11:20:55Z.

Review: APPROVED


Looks right to me against the cited FreeBSD 11.4 source (release/11.4.0):

  • Order. In sys_kenv, KENV_DUMP returns before anything else. KENV_SET/KENV_UNSET call priv_check(td, PRIV_KENV_SET/UNSET) and return on error before the copyinstr of the name, so a null or 0x10 name giving EPERM follows the source order. KENV_GET copies the name first (EFAULT/ENAMETOOLONG) and then gives ENOENT. An unknown what reaches default: EINVAL after the copy.
  • Jail. kern_jail.c has no PRIV_KENV_* case in prison_priv_check, so a jailed uid 0 falls through to EPERM.

Tested on Windows 11, WinLibs GCC 15.2.0, Release, with head 683fa0329c merged onto main:

  • guest_kernel_sys passes.
  • With libkernel_sys/Export.cpp put back to main's version, it fails in the new SET/UNSET checks (Kernel sys check failed … guest errno 14, where EPERM was expected).

Non-blocking: DUMP returning 0 and GET returning ENOENT both rest on the environment being empty, which main already assumed. On FreeBSD a jailed process can still read the host's kernel environment, so a console could report loader entries here. That, and Sony changes to sys_kenv, aren't settled without a console run. It might be worth one line in the debt entry.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735768476 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15025#pullrequestreview-5735768476). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T11:20:55Z. **Review:** APPROVED --- Looks right to me against the cited FreeBSD 11.4 source (`release/11.4.0`): - **Order.** In `sys_kenv`, `KENV_DUMP` returns before anything else. `KENV_SET`/`KENV_UNSET` call `priv_check(td, PRIV_KENV_SET/UNSET)` and return on error before the `copyinstr` of the name, so a null or `0x10` name giving `EPERM` follows the source order. `KENV_GET` copies the name first (`EFAULT`/`ENAMETOOLONG`) and then gives `ENOENT`. An unknown `what` reaches `default: EINVAL` after the copy. - **Jail.** `kern_jail.c` has no `PRIV_KENV_*` case in `prison_priv_check`, so a jailed uid 0 falls through to `EPERM`. Tested on Windows 11, WinLibs GCC 15.2.0, Release, with head `683fa0329c` merged onto `main`: - `guest_kernel_sys` passes. - With `libkernel_sys/Export.cpp` put back to `main`'s version, it fails in the new SET/UNSET checks (`Kernel sys check failed … guest errno 14`, where `EPERM` was expected). Non-blocking: DUMP returning 0 and GET returning `ENOENT` both rest on the environment being empty, which `main` already assumed. On FreeBSD a jailed process can still read the host's kernel environment, so a console could report loader entries here. That, and Sony changes to `sys_kenv`, aren't settled without a console run. It might be worth one line in the debt entry.
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!15025
No description provided.