Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
orezraey:fix/libkernel-dupfd-cloexec-measured-ps5
Oct 26, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
orezraey:fix/libkernel-dupfd-cloexec-measured-ps5

Conversation

@orezraey

@orezraey orezraey commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

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

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10319 docs/dev/debt/libkernel.md L270-273

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7447 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8605 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 8916 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9284 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9442 docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/File/src/DescriptorControl.cpp
docs/dev/debt/libkernel.md
PR 9575 docs/dev/debt/libkernel.md
PR 9713 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9947 docs/dev/debt/libkernel.md
PR 9948 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 10442 docs/dev/debt/libkernel.md
PR 10749 docs/dev/debt/libkernel.md
PR 11002 docs/dev/debt/libkernel.md
PR 11042 docs/dev/debt/libkernel.md
PR 11085 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11223 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 11264 docs/dev/debt/libkernel.md
PR 11267 docs/dev/debt/libkernel.md
PR 11341 docs/dev/debt/libkernel.md

@orezraey

Copy link
Copy Markdown
Contributor Author

@DavidSamir your #9557 and #9566 rewrite the fcntl entry of docs/dev/debt/libkernel.md, which this PR also rewrites, and #9566 also edits the ioctl entry right after it. In DescriptorControl.cpp they change the F_GETOWN/F_SETOWN cases of Fcntl while this PR changes only the F_DUPFD_CLOEXEC case, so the code itself does not overlap. I propose yours land first; I will then merge main into this branch, keep your SIGIO owner wording in both entries and re-apply this PR's F_DUPFD_CLOEXEC change on top. Both are now in Depends on (merge order only).

@orezraey
orezraey force-pushed the fix/libkernel-dupfd-cloexec-measured-ps5 branch from c3f20ae to 0868498 Compare October 25, 2026 21:04
@orezraey orezraey changed the title fix(libkernel): fail F_DUPFD_CLOEXEC with EOPNOTSUPP as measured on a PS5 fix(libkernel): fail F_DUPFD_CLOEXEC and make F_DUP2FD_CLOEXEC ignore its target as measured on a PS5 Oct 25, 2026
@wachi35676

Copy link
Copy Markdown
Contributor

Tested head 0868498fc2 merged onto main 49d1f9b07e, Windows 11 (WinLibs GCC 15.2.0, Release) and WSL2 Ubuntu 24.04 (GCC 13.3), BUILD_TESTING=ON.

  • guest_kernel_fcntl, guest_socket_descriptor_flags, guest_socket_descriptors, guest_socket_pair, guest_socket_options, guest_udp: 6/6 pass, 3 runs, on both hosts. So the Linux pipe and socket paths are covered too.
  • With DescriptorControl.cpp and Udp.cpp from main and the tests kept, guest_kernel_fcntl, guest_socket_descriptors and guest_socket_descriptor_flags fail on both hosts.
  • The console log also covers a kqueue. A probe here: fcntl(kqueue_nid_postfix(), F_DUPFD_CLOEXEC, 0 or 300) now returns -1 with EOPNOTSUPP, as on the console (main threw the kqueue dup error). F_DUP2FD_CLOEXEC on a kqueue still throws duplicating a kqueue descriptor is not supported, as F_DUPFD does, which the kqueue debt entry already covers.

AI-assisted: yes (Claude Code); results above were run on this machine.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 26, 2026
@oneandonlydean
oneandonlydean merged commit 5dd28e5 into boykopovar:main Oct 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants