fix(libkernel_sys): validate descriptor first in fchown #8457

Merged
vaibhavindalia merged 1 commit from upstream-pr/8457 into main 2026-10-26 03:30:36 +00:00
vaibhavindalia commented 2026-10-20 12:53:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by vaibhavindalia on 2026-10-20T12:53:04Z.


What

Validate the file descriptor before checking ownership arguments in fchown_nid_postfix. If the descriptor is not open or invalid, return guest EBADF (-1 with errno 9). Use RequireGuestOwner to maintain the fail-fast NotImplemented_nid_no_patch throw for unmodeled owner or group values on valid descriptors, matching chown, lchown and fchownat. Add regression tests in GuestKernelSys.cpp covering supported mixed values and invalid descriptor error precedence.

Tested

Windows 11 (NT 10.0.26300), WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r7), Release, Ninja:

  • cmake --build build --target guest_kernel_sys_tests: compiled and linked cleanly.
  • ctest --test-dir build -R "^guest_kernel_sys$" -V: 1/1 tests passed (0.11s, 100% tests passed, 0 failures).
  • python tools/check_conventions.py --base upstream/main: All conventions checks passed.

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
  • 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:4936012596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8457). Originally posted by [vaibhavindalia](https://github.com/vaibhavindalia) on 2026-10-20T12:53:04Z. --- ### What Validate the file descriptor before checking ownership arguments in `fchown_nid_postfix`. If the descriptor is not open or invalid, return guest `EBADF` (-1 with errno 9). Use `RequireGuestOwner` to maintain the fail-fast `NotImplemented_nid_no_patch` throw for unmodeled owner or group values on valid descriptors, matching `chown`, `lchown` and `fchownat`. Add regression tests in `GuestKernelSys.cpp` covering supported mixed values and invalid descriptor error precedence. ### Tested Windows 11 (NT 10.0.26300), WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r7), Release, Ninja: - `cmake --build build --target guest_kernel_sys_tests`: compiled and linked cleanly. - `ctest --test-dir build -R "^guest_kernel_sys$" -V`: 1/1 tests passed (0.11s, 100% tests passed, 0 failures). - `python tools/check_conventions.py --base upstream/main`: All conventions checks passed. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [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
oneandonlydean commented 2026-10-20 15:03:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T15:03:21Z.


Thanks for this. Checking the descriptor first is right: FreeBSD 11 resolves the descriptor before it looks at the owner, so EBADF should win over an unsupported owner. The EPERM part doesn't fit the rest of the tree, though:

  • The guest is modelled as root here: getuid and geteuid return 0 (core/libs/prx/libkernel/System/src/Process.cpp:297-303), and other libkernel_sys entries (for example chroot) follow FreeBSD "for a root caller".
  • FreeBSD 11's ufs_chown returns EPERM only when priv_check_cred(cred, PRIV_VFS_CHOWN, 0) fails (sys/ufs/ufs/ufs_vnops.c:795-797). A root caller passes that check, and jailed root keeps PRIV_VFS_CHOWN, so the reference answer for fchown(fd, 1, 0) from this guest is success, not EPERM.
  • Ownership isn't stored anywhere, so returning 0 would be a silent stub. Under the repo's rule that unimplemented paths throw, the existing NotImplemented throw is still the honest answer for a real owner change.

Could you keep the descriptor check first, keep the throw for an owner or group other than 0 and -1, and adjust the tests to match? Another option is a console measurement showing that a title process really gets EPERM. If you have one, please add it to the description and the debt entry, and EPERM can stay.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293992394 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8457#issuecomment-6293992394). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T15:03:21Z. --- Thanks for this. Checking the descriptor first is right: FreeBSD 11 resolves the descriptor before it looks at the owner, so `EBADF` should win over an unsupported owner. The `EPERM` part doesn't fit the rest of the tree, though: - The guest is modelled as root here: `getuid` and `geteuid` return 0 (`core/libs/prx/libkernel/System/src/Process.cpp:297-303`), and other libkernel_sys entries (for example `chroot`) follow FreeBSD "for a root caller". - FreeBSD 11's `ufs_chown` returns `EPERM` only when `priv_check_cred(cred, PRIV_VFS_CHOWN, 0)` fails (`sys/ufs/ufs/ufs_vnops.c:795-797`). A root caller passes that check, and jailed root keeps `PRIV_VFS_CHOWN`, so the reference answer for `fchown(fd, 1, 0)` from this guest is success, not `EPERM`. - Ownership isn't stored anywhere, so returning 0 would be a silent stub. Under the repo's rule that unimplemented paths throw, the existing `NotImplemented` throw is still the honest answer for a real owner change. Could you keep the descriptor check first, keep the throw for an owner or group other than 0 and -1, and adjust the tests to match? Another option is a console measurement showing that a title process really gets `EPERM`. If you have one, please add it to the description and the debt entry, and `EPERM` can stay.
xXPBSXx commented 2026-10-21 03:52:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T03:52:27Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_kernel_sys_tests; ctest -N -R guest_kernel_sys lists exactly one test.

Results (3 runs each):

  • PR head: guest_kernel_sys passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel_sys/Export.cpp with the version from the PR's merge base (5f7aa5570c), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6307606542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8457#issuecomment-6307606542). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T03:52:27Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_kernel_sys_tests`; `ctest -N -R guest_kernel_sys` lists exactly one test. **Results (3 runs each):** - PR head: `guest_kernel_sys` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel_sys/Export.cpp` with the version from the PR's merge base (`5f7aa5570c`), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-21 09:45:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T09:45:15Z.


Linux data point for 2814d6c7cb: WSL2 Ubuntu 24.04 (a virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, BUILD_TESTING=ON, PR head alone (not merged onto main).

  • With the PR: ctest -R '^guest_kernel_sys$' passes 3 of 3.
  • With core/libs/prx/libkernel_sys/Export.cpp restored to the merge-base (new test cases kept): fails (Subprocess aborted).
    Not tested: macOS, Windows, a title run.
    AI-assisted (Claude Code).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6312961097 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8457#issuecomment-6312961097). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T09:45:15Z. --- Linux data point for `2814d6c7cb`: WSL2 Ubuntu 24.04 (a virtualised kernel, not bare-metal Linux), GCC 13.3.0, Release, `BUILD_TESTING=ON`, PR head alone (not merged onto `main`). - With the PR: `ctest -R '^guest_kernel_sys$'` passes 3 of 3. - With `core/libs/prx/libkernel_sys/Export.cpp` restored to the merge-base (new test cases kept): fails (`Subprocess aborted`). Not tested: macOS, Windows, a title run. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 13:51:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:51:18Z.

Review: COMMENTED


Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/vfs_syscalls.c / sys/ufs/ufs/ufs_vnops.c:

  • Main has moved on since this branch's base. #9103 added RequireGuestOwner (Export.cpp:193-197), and chown, lchown and fchownat (Export.cpp:724-744) now look up the file first and then call it. Following @oneandonlydean's suggestion, fchown can call RequireGuestOwner("fchown", uid, gid) after the fstat check, the same way those three do; the descriptor-first order is still needed, since main's fchown(-1, 1, 0) throws before reaching EBADF.
  • git merge-tree against main conflicts in docs/dev/debt/libkernel_sys.md, where main added the chown, lchown and fchownat entry right after the fchown line. That entry says ownership is handled "as for fchown", so keeping the throw also keeps the two entries consistent.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638480497 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8457#pullrequestreview-5638480497). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:51:18Z. **Review:** COMMENTED --- Checked against main 522e32e3b6 and FreeBSD 11.4 sys/kern/vfs_syscalls.c / sys/ufs/ufs/ufs_vnops.c: - Main has moved on since this branch's base. #9103 added `RequireGuestOwner` (Export.cpp:193-197), and `chown`, `lchown` and `fchownat` (Export.cpp:724-744) now look up the file first and then call it. Following @oneandonlydean's suggestion, `fchown` can call `RequireGuestOwner("fchown", uid, gid)` after the `fstat` check, the same way those three do; the descriptor-first order is still needed, since main's `fchown(-1, 1, 0)` throws before reaching EBADF. - `git merge-tree` against main conflicts in `docs/dev/debt/libkernel_sys.md`, where main added the `chown, lchown and fchownat` entry right after the fchown line. That entry says ownership is handled "as for `fchown`", so keeping the throw also keeps the two entries consistent. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!8457
No description provided.