fix(libkernel): fail F_DUPFD_CLOEXEC and make F_DUP2FD_CLOEXEC ignore its target as measured on a PS5 #11135

Merged
orezraey merged 3 commits from upstream-pr/11135 into main 2026-10-26 22:01:17 +00:00
orezraey commented 2026-10-25 19:18:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-25T19:18:46Z.


What

F_DUPFD_CLOEXEC (17) now fails with EOPNOTSUPP (45, 0x8002002d through sceKernelFcntl) instead of duplicating the descriptor with FD_CLOEXEC set, as a retail PS5 (firmware 13.60) does. Measured with homebrew probes from a payload process through both fcntl and sceKernelFcntl; the sources and console logs are in this gist (pkfcntl, pkfcntl2).

On the console:

  • On a file, a UDP socket, a TCP socket, both pipe ends and a kqueue, F_DUPFD_CLOEXEC fails with EOPNOTSUPP for a start of 0, 200 and -1, so the start is not checked first.
  • Descriptors -1 and 9999 fail with EBADF first.
  • F_DUPFD on the same descriptors duplicates normally, which is unchanged here.

The file path (File::Fcntl) and the guest socket path (fcntl in Udp.cpp) both fail the command now. The socket helper loses its close-on-exec parameter, which only this command used.

Left as they are, and recorded in a new docs/dev/debt/libkernel.md entry:

  • On an SHM_ANON descriptor the console returns 0 without setting errno, whatever the start. Descriptor 0 was already open, so whether anything is duplicated is not known. Here it fails like the other kinds, because on Windows an anonymous shared memory descriptor cannot be told apart from a temporary file.
  • F_DUP2FD_CLOEXEC (18) is now handled too: a later probe (gist, pdup2cx) showed what it does on the console. It ignores the target: it neither opens nor closes it, for 300, 301, 20, INT_MAX, -1, -2, the source itself and an open descriptor alike. It returns a new descriptor for the same open file (same inode, shared offset, same F_GETFL) without FD_CLOEXEC, numbered somewhere from 17 to 1012 with no visible pattern. A bad source gets EBADF. Here the file and socket paths now return a new duplicate at the lowest free number (the console's numbering is not modelled), without FD_CLOEXEC, and leave the target alone; F_DUP2FD (10) is unchanged.

The sceKernelFcntl entry now records what was measured: the same return values as fcntl, and 0x80020000 plus the errno, which is also set, for F_GETFD, F_SETFD, F_GETFL, F_DUPFD, F_DUPFD_CLOEXEC, F_GETLK, an unknown command and bad descriptors. AnyPS5 already matched all of these except F_DUPFD_CLOEXEC. Sockets created through libSceNet take their own status path, which still answers every command except F_GETFL and F_SETFL with EINVAL; the probes used libkernel sockets only, so that path is unchanged.

Before and after

fcntl / sceKernelFcntl PS5 AnyPS5 before AnyPS5 now
F_DUPFD_CLOEXEC on a file, socket or pipe EOPNOTSUPP (0x8002002d) duplicate with FD_CLOEXEC EOPNOTSUPP (0x8002002d)
F_DUP2FD_CLOEXEC new descriptor, target untouched, no FD_CLOEXEC duplicate onto the target with FD_CLOEXEC new descriptor at the lowest free number, target untouched, no FD_CLOEXEC
bad descriptor (-1, 9999) EBADF the same the same
F_DUPFD, F_GETFD, F_GETFL and the others listed above as listed above the same the same
F_DUPFD_CLOEXEC on SHM_ANON 0 duplicate with FD_CLOEXEC EOPNOTSUPP (debt)

Tested

Windows 11, WinLibs GCC 15.2, full build.

  • Head 85a1bf2b31 (main 9f386f3a29 merged in): only the fcntl debt entry met; it keeps main's new O_FSYNC wording and drops F_DUPFD_CLOEXEC from the socket path list and the lowest-descriptor sentence as before. guest_kernel_fcntl, guest_socket_descriptors and guest_socket_descriptor_flags pass three runs in a row on Windows and on Linux (WSL Ubuntu, GCC), and the 189 Windows tests matching fcntl|descriptor|shm|socket|dup|pipe|kqueue|net_|fsync|sync pass.
  • guest_kernel_fcntl, guest_socket_descriptors and guest_socket_descriptor_flags pass 3 times out of 3. They check EOPNOTSUPP on a file for starts 0, 20, 200 and -1, on a socket, on both pipe ends and on an SHM_ANON descriptor, through both entry points, and EBADF for -1 and 9999.
  • The 165 tests matching fcntl|descriptor|shm|socket|dup|pipe|kqueue|net_ pass.
  • tools/check_conventions.py gives one warning: the existing fcntl entry is over 1000 characters (3872). This PR only removes F_DUPFD_CLOEXEC from it.

With the F_DUP2FD_CLOEXEC change the three tests pass 3 times out of 3 again. They check that the command returns a new descriptor different from the target and the source, without FD_CLOEXEC, for targets that are open, the source itself, -1, -2 and 300, on a file, a socket and both pipe ends. They also check that the target keeps its flags, that the offset is shared, and that a bad source (-1, 9999) gets EBADF.

Head 0868498fc2 on main cceb6c3c41.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #9561 and #9557 (they edit the fcntl debt entry this PR edits), #9566 (it edits the fcntl and ioctl debt entries) and #10319 (it edits the entry between the two this PR edits); merge order only
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5007861501 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11135). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-25T19:18:46Z. --- ### What `F_DUPFD_CLOEXEC` (17) now fails with `EOPNOTSUPP` (45, `0x8002002d` through `sceKernelFcntl`) instead of duplicating the descriptor with `FD_CLOEXEC` set, as a retail PS5 (firmware 13.60) does. Measured with homebrew probes from a payload process through both `fcntl` and `sceKernelFcntl`; the sources and console logs are in [this gist](https://gist.github.com/orezraey/a4073ad46786ffdb71aed92cc2c4be74) (`pkfcntl`, `pkfcntl2`). On the console: - On a file, a UDP socket, a TCP socket, both pipe ends and a kqueue, `F_DUPFD_CLOEXEC` fails with `EOPNOTSUPP` for a start of 0, 200 and -1, so the start is not checked first. - Descriptors -1 and 9999 fail with `EBADF` first. - `F_DUPFD` on the same descriptors duplicates normally, which is unchanged here. The file path (`File::Fcntl`) and the guest socket path (`fcntl` in `Udp.cpp`) both fail the command now. The socket helper loses its close-on-exec parameter, which only this command used. Left as they are, and recorded in a new `docs/dev/debt/libkernel.md` entry: - On an `SHM_ANON` descriptor the console returns 0 without setting `errno`, whatever the start. Descriptor 0 was already open, so whether anything is duplicated is not known. Here it fails like the other kinds, because on Windows an anonymous shared memory descriptor cannot be told apart from a temporary file. - `F_DUP2FD_CLOEXEC` (18) is now handled too: a later probe ([gist](https://gist.github.com/orezraey/018d737f7abdf8186c88ad7728a80c86), `pdup2cx`) showed what it does on the console. It ignores the target: it neither opens nor closes it, for 300, 301, 20, `INT_MAX`, -1, -2, the source itself and an open descriptor alike. It returns a new descriptor for the same open file (same inode, shared offset, same `F_GETFL`) without `FD_CLOEXEC`, numbered somewhere from 17 to 1012 with no visible pattern. A bad source gets `EBADF`. Here the file and socket paths now return a new duplicate at the lowest free number (the console's numbering is not modelled), without `FD_CLOEXEC`, and leave the target alone; `F_DUP2FD` (10) is unchanged. The `sceKernelFcntl` entry now records what was measured: the same return values as `fcntl`, and `0x80020000` plus the errno, which is also set, for `F_GETFD`, `F_SETFD`, `F_GETFL`, `F_DUPFD`, `F_DUPFD_CLOEXEC`, `F_GETLK`, an unknown command and bad descriptors. AnyPS5 already matched all of these except `F_DUPFD_CLOEXEC`. Sockets created through libSceNet take their own status path, which still answers every command except `F_GETFL` and `F_SETFL` with `EINVAL`; the probes used libkernel sockets only, so that path is unchanged. **Before and after** | `fcntl` / `sceKernelFcntl` | PS5 | AnyPS5 before | AnyPS5 now | |---|---|---|---| | `F_DUPFD_CLOEXEC` on a file, socket or pipe | `EOPNOTSUPP` (`0x8002002d`) | duplicate with `FD_CLOEXEC` | `EOPNOTSUPP` (`0x8002002d`) | | `F_DUP2FD_CLOEXEC` | new descriptor, target untouched, no `FD_CLOEXEC` | duplicate onto the target with `FD_CLOEXEC` | new descriptor at the lowest free number, target untouched, no `FD_CLOEXEC` | | bad descriptor (-1, 9999) | `EBADF` | the same | the same | | `F_DUPFD`, `F_GETFD`, `F_GETFL` and the others listed above | as listed above | the same | the same | | `F_DUPFD_CLOEXEC` on `SHM_ANON` | 0 | duplicate with `FD_CLOEXEC` | `EOPNOTSUPP` (debt) | ### 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. --> Windows 11, WinLibs GCC 15.2, full build. - Head `85a1bf2b31` (`main` `9f386f3a29` merged in): only the `fcntl` debt entry met; it keeps `main`'s new `O_FSYNC` wording and drops `F_DUPFD_CLOEXEC` from the socket path list and the lowest-descriptor sentence as before. `guest_kernel_fcntl`, `guest_socket_descriptors` and `guest_socket_descriptor_flags` pass three runs in a row on Windows and on Linux (WSL Ubuntu, GCC), and the 189 Windows tests matching `fcntl|descriptor|shm|socket|dup|pipe|kqueue|net_|fsync|sync` pass. - `guest_kernel_fcntl`, `guest_socket_descriptors` and `guest_socket_descriptor_flags` pass 3 times out of 3. They check `EOPNOTSUPP` on a file for starts 0, 20, 200 and -1, on a socket, on both pipe ends and on an `SHM_ANON` descriptor, through both entry points, and `EBADF` for -1 and 9999. - The 165 tests matching `fcntl|descriptor|shm|socket|dup|pipe|kqueue|net_` pass. - `tools/check_conventions.py` gives one warning: the existing `fcntl` entry is over 1000 characters (3872). This PR only removes `F_DUPFD_CLOEXEC` from it. With the `F_DUP2FD_CLOEXEC` change the three tests pass 3 times out of 3 again. They check that the command returns a new descriptor different from the target and the source, without `FD_CLOEXEC`, for targets that are open, the source itself, -1, -2 and 300, on a file, a socket and both pipe ends. They also check that the target keeps its flags, that the offset is shared, and that a bad source (-1, 9999) gets `EBADF`. Head `0868498fc2` on `main` `cceb6c3c41`. ### 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) - [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: #9561 and #9557 (they edit the `fcntl` debt entry this PR edits), #9566 (it edits the `fcntl` and `ioctl` debt entries) and #10319 (it edits the entry between the two this PR edits); merge order only - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 19:22:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:22:11Z.

Review: APPROVED


Reviewed the file and kernel-owned socket dispatch at this exact head: invalid descriptors are rejected as EBADF before the unsupported-command result, while valid descriptors return EOPNOTSUPP; F_DUPFD remains on its existing duplication path. The updated tests cover both entry points and descriptor classes, including the measured start-value precedence. GCC 12.2 syntax compilation passed for the changed implementation and test translation units. The Windows 11 runtime suites and retail PS5 probe results are author-reported; hosted functional jobs are skipped by path policy. The documented SHM_ANON discrepancy remains explicit rather than being represented as measured parity.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644857044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11135#pullrequestreview-5644857044). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T19:22:11Z. **Review:** APPROVED --- Reviewed the file and kernel-owned socket dispatch at this exact head: invalid descriptors are rejected as `EBADF` before the unsupported-command result, while valid descriptors return `EOPNOTSUPP`; `F_DUPFD` remains on its existing duplication path. The updated tests cover both entry points and descriptor classes, including the measured start-value precedence. GCC 12.2 syntax compilation passed for the changed implementation and test translation units. The Windows 11 runtime suites and retail PS5 probe results are author-reported; hosted functional jobs are skipped by path policy. The documented `SHM_ANON` discrepancy remains explicit rather than being represented as measured parity.
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!11135
No description provided.