fix(libkernel_sys): fail fchdir on a pipe or socket with EINVAL #9131

Merged
xXPBSXx merged 3 commits from upstream-pr/9131 into main 2026-10-22 18:32:29 +00:00
xXPBSXx commented 2026-10-21 12:48:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T12:48:01Z.


What

fchdir returned ENOTDIR for a pipe or a socket. FreeBSD 11.4's sys_fchdir resolves the descriptor with getvnode, which fails with EINVAL for a descriptor without a vnode; only a vnode that is not a directory gets ENOTDIR from change_dir. A descriptor that is neither a vnode nor the random device now fails with EINVAL; EBADF for a closed descriptor and ENOTDIR for a regular file are unchanged. The fchdir debt entry names the three errors.

Tested

Windows 10 (19045), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, on main 865166414f plus this commit; Linux: WSL2 Ubuntu 24.04, GCC 13.3.0, Release, this head alone:

  • ctest -R '^guest_kernel_sys$': Windows passes 3 of 3, Linux passes 3 of 3; with libkernel_sys/Export.cpp restored to main (the new pipe and socket cases kept) it fails (Windows exit code 0xc0000409 3 of 3, Linux Subprocess aborted).
  • Windows: -R 'guest_kernel_sys|guest_filesystem$|guest_posix_filesystem|guest_kernel_rename|guest_dup|openat|directory|working_directory': 12 of 12 pass.
  • python tools/check_conventions.py --base upstream/main passes; git merge-tree against main is clean.
  • Not tested: macOS, a console, a title run.

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; silent stubs are listed in technical debt (none added)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #9130
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951799992 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9131). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T12:48:01Z. --- ### What `fchdir` returned `ENOTDIR` for a pipe or a socket. FreeBSD 11.4's `sys_fchdir` resolves the descriptor with `getvnode`, which fails with `EINVAL` for a descriptor without a vnode; only a vnode that is not a directory gets `ENOTDIR` from `change_dir`. A descriptor that is neither a vnode nor the random device now fails with `EINVAL`; `EBADF` for a closed descriptor and `ENOTDIR` for a regular file are unchanged. The `fchdir` debt entry names the three errors. ### Tested Windows 10 (19045), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, on `main` 865166414f plus this commit; Linux: WSL2 Ubuntu 24.04, GCC 13.3.0, Release, this head alone: - `ctest -R '^guest_kernel_sys$'`: Windows passes 3 of 3, Linux passes 3 of 3; with `libkernel_sys/Export.cpp` restored to `main` (the new pipe and socket cases kept) it fails (Windows exit code `0xc0000409` 3 of 3, Linux `Subprocess aborted`). - Windows: `-R 'guest_kernel_sys|guest_filesystem$|guest_posix_filesystem|guest_kernel_rename|guest_dup|openat|directory|working_directory'`: 12 of 12 pass. - `python tools/check_conventions.py --base upstream/main` passes; `git merge-tree` against `main` is clean. - Not tested: macOS, a console, a title run. ### 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; silent stubs are listed in technical debt (none 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) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #9130 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
adriadam10 commented 2026-10-21 18:14:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T18:14:03Z.

Review: CHANGES_REQUESTED


  • core/libs/prx/libkernel_sys/Export.cpp:363: the condition returns EINVAL for every descriptor where File::IsVnode is false. IsVnode is true only for regular files and directories (DescriptorControl.cpp:586-595: S_ISREG/S_ISDIR on Linux, FILE_TYPE_DISK on Windows). A character device (tty, /dev/null, NUL, a console handle) is a vnode on FreeBSD, so getvnode succeeds and change_dir returns ENOTDIR. With this change those would now return EINVAL, which is a new deviation from FreeBSD. Please scope it to what the title and #9130 describe: a pipe or a socket (e.g. S_ISFIFO/S_ISSOCK on Linux, FILE_TYPE_PIPE or the descriptor table's pipe/socket kind on Windows), and keep ENOTDIR for everything else.
  • core/libs/tests/GuestKernelSys.cpp: once scoped, add a character device case that still expects 20 (ENOTDIR) so the narrower condition is pinned.
  • docs/dev/debt/libkernel_sys.md:8: fine once the code matches; keep "pipe or socket".
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5604196953 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9131#pullrequestreview-5604196953). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T18:14:03Z. **Review:** CHANGES_REQUESTED --- - `core/libs/prx/libkernel_sys/Export.cpp:363`: the condition returns `EINVAL` for every descriptor where `File::IsVnode` is false. `IsVnode` is true only for regular files and directories (`DescriptorControl.cpp:586-595`: `S_ISREG`/`S_ISDIR` on Linux, `FILE_TYPE_DISK` on Windows). A character device (tty, `/dev/null`, `NUL`, a console handle) is a vnode on FreeBSD, so `getvnode` succeeds and `change_dir` returns `ENOTDIR`. With this change those would now return `EINVAL`, which is a new deviation from FreeBSD. Please scope it to what the title and #9130 describe: a pipe or a socket (e.g. `S_ISFIFO`/`S_ISSOCK` on Linux, `FILE_TYPE_PIPE` or the descriptor table's pipe/socket kind on Windows), and keep `ENOTDIR` for everything else. - `core/libs/tests/GuestKernelSys.cpp`: once scoped, add a character device case that still expects 20 (`ENOTDIR`) so the narrower condition is pinned. - `docs/dev/debt/libkernel_sys.md:8`: fine once the code matches; keep "pipe or socket".
xXPBSXx commented 2026-10-21 19:43:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T19:43:50Z.


@adriadam10 thanks, you are right: File::IsVnode is false for a character device, so the first version returned EINVAL for a tty, NUL or /dev/null where FreeBSD's getvnode succeeds and change_dir gives ENOTDIR.

Pushed 77be50c54d on top of the head you reviewed (no force push): the EINVAL now applies only when the descriptor is neither a vnode (File::IsVnode) nor a character device (new File::IsCharacterDevice: FILE_TYPE_CHAR on Windows with guest sockets excluded, S_ISCHR elsewhere) nor the guest random device; a pipe or socket gets EINVAL, everything else keeps ENOTDIR. The debt line keeps "pipe or socket". guest_kernel_sys gets a /dev/random case that expects ENOTDIR; it pins the narrower condition but does not fail on the earlier head (the guest random device was already excluded there), so I did not find a case that distinguishes the two heads for a host tty.

Windows 10 19045, WinLibs GCC 15.2.0 r7, Release, this head: ctest -R '^guest_kernel_sys$' passes 3 of 3. (The pipe and socket cases failed without the change when I measured them on the first head: exit code 0xc0000409, 3 of 3; I did not repeat the run without the change on this head.) The kernel_sys, filesystem, posix_filesystem, rename, dup, fcntl and file_lock tests pass (12 of 12). check_conventions passes; git merge-tree against main is clean. Linux (WSL2 Ubuntu 24.04, GCC 13.3.0, Release, this head alone): guest_kernel_sys passes 3 of 3; with libkernel_sys/Export.cpp restored to the merge-base it fails (Subprocess aborted).
Not tested: macOS, a console.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324970147 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9131#issuecomment-6324970147). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T19:43:50Z. --- @adriadam10 thanks, you are right: `File::IsVnode` is false for a character device, so the first version returned `EINVAL` for a tty, `NUL` or `/dev/null` where FreeBSD's `getvnode` succeeds and `change_dir` gives `ENOTDIR`. Pushed `77be50c54d` on top of the head you reviewed (no force push): the `EINVAL` now applies only when the descriptor is neither a vnode (`File::IsVnode`) nor a character device (new `File::IsCharacterDevice`: `FILE_TYPE_CHAR` on Windows with guest sockets excluded, `S_ISCHR` elsewhere) nor the guest random device; a pipe or socket gets `EINVAL`, everything else keeps `ENOTDIR`. The debt line keeps "pipe or socket". `guest_kernel_sys` gets a `/dev/random` case that expects `ENOTDIR`; it pins the narrower condition but does not fail on the earlier head (the guest random device was already excluded there), so I did not find a case that distinguishes the two heads for a host tty. Windows 10 19045, WinLibs GCC 15.2.0 r7, Release, this head: `ctest -R '^guest_kernel_sys$'` passes 3 of 3. (The pipe and socket cases failed without the change when I measured them on the first head: exit code `0xc0000409`, 3 of 3; I did not repeat the run without the change on this head.) The kernel_sys, filesystem, posix_filesystem, rename, dup, fcntl and file_lock tests pass (12 of 12). `check_conventions` passes; `git merge-tree` against `main` is clean. Linux (WSL2 Ubuntu 24.04, GCC 13.3.0, Release, this head alone): `guest_kernel_sys` passes 3 of 3; with `libkernel_sys/Export.cpp` restored to the merge-base it fails (`Subprocess aborted`). Not tested: macOS, a console. AI-assisted (Claude Code).
MarcZX10 commented 2026-10-21 22:47:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T22:47:14Z.

Code review location: core/libs/tests/GuestKernelSys.cpp; line 374

@@ -364,6 +366,19 @@ int main() {
     Require(fileDescriptor >= 0);
     Require(fchdir_nid_postfix(-1) == -1 && Error() == 9);
     Require(fchdir_nid_postfix(fileDescriptor) == -1 && Error() == 20);
+    int pipeEnds[2] = {-1, -1};
+    Require(pipe_nid_postfix(pipeEnds) == 0);
+    Require(fchdir_nid_postfix(pipeEnds[0]) == -1 && Error() == 22);
+    Require(fchdir_nid_postfix(pipeEnds[1]) == -1 && Error() == 22);
+    Require(close_nid_postfix(pipeEnds[0]) == 0 && close_nid_postfix(pipeEnds[1]) == 0);
+    const int characterDevice = open_nid_postfix("/dev/random", 0, 0);

The implementation now preserves ENOTDIR for character devices via IsCharacterDevice, but this test opens /dev/random, which is also explicitly excluded by !File::IsRandomDevice(fd) in the fchdir condition. It would still pass if the new character-device check always returned false. Could you add an ordinary character device case (/dev/null on POSIX and NUL on Windows, if available) so the branch that fixed the review finding is directly covered? The existing regular-file assertion already pins the ENOTDIR path for vnodes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4342751887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9131#discussion_r4342751887). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T22:47:14Z. **Code review location:** `core/libs/tests/GuestKernelSys.cpp`; line 374 ~~~~diff @@ -364,6 +366,19 @@ int main() { Require(fileDescriptor >= 0); Require(fchdir_nid_postfix(-1) == -1 && Error() == 9); Require(fchdir_nid_postfix(fileDescriptor) == -1 && Error() == 20); + int pipeEnds[2] = {-1, -1}; + Require(pipe_nid_postfix(pipeEnds) == 0); + Require(fchdir_nid_postfix(pipeEnds[0]) == -1 && Error() == 22); + Require(fchdir_nid_postfix(pipeEnds[1]) == -1 && Error() == 22); + Require(close_nid_postfix(pipeEnds[0]) == 0 && close_nid_postfix(pipeEnds[1]) == 0); + const int characterDevice = open_nid_postfix("/dev/random", 0, 0); ~~~~ --- The implementation now preserves `ENOTDIR` for character devices via `IsCharacterDevice`, but this test opens `/dev/random`, which is also explicitly excluded by `!File::IsRandomDevice(fd)` in the `fchdir` condition. It would still pass if the new character-device check always returned false. Could you add an ordinary character device case (`/dev/null` on POSIX and `NUL` on Windows, if available) so the branch that fixed the review finding is directly covered? The existing regular-file assertion already pins the `ENOTDIR` path for vnodes.
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!9131
No description provided.