libkernel: ioctl with a malformed request aborts the guest on files and pipes instead of returning ENOTTY; fix branch ready #5564

Closed
opened 2026-10-13 23:59:34 +00:00 by husseinkad · 3 comments
husseinkad commented 2026-10-13 23:59:34 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T23:59:34Z.


ioctl (and _ioctl) with a malformed request throws "request ... is not implemented" on a file, directory, pipe, stdin or stdout, which ends the process, and returns the wrong error on other descriptors. FreeBSD 11.4 sys_ioctl returns ENOTTY for a malformed request before it reads the argument or looks at the descriptor. "Malformed" means: no direction bit (IOC_VOID, IOC_IN or IOC_OUT), an output request with length 0, or a void request with a length other than 4. A Linux request number such as TCGETS (0x5401) is malformed by that rule, so a guest or library that probes with it gets ENOTTY on a console. Here the same call aborts the guest on a file or pipe, returns EBADF on a closed descriptor, and returns EFAULT on a socket given a null argument.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-ioctl-malformed-request (one commit on a recent main: File/src/DescriptorControl.cpp and .hpp with a new File::IoctlRequestMalformed checked first in File::Ioctl, the socket path of ioctl_nid_postfix in Udp.cpp calling the same check, tests in tests/GuestKernelFcntl.cpp, and a debt note in docs/dev/debt/libkernel.md).

Checks:

  • The new guest_kernel_fcntl cases fail on main (abort) and pass with the change; guest_udp and guest_sendfile pass.
  • A fuzz of 20,144 request words (10,000 random, 10,000 structured, 100 with garbage in the upper bits, 44 named) against file, directory, pipe read and write ends, UDP, TCP, a closed fd, -1, stdin, stdout and fd 4000, each with a null and a valid argument: 886,336 results per build. All 12,063 distinct malformed requests give ENOTTY (25) on every descriptor kind (main threw, or returned EBADF or EFAULT). All 7,602 distinct well-formed requests are unchanged: unknown ones still throw on files, sockets still give ENOTTY or EFAULT as before, and FIONBIO, FIONREAD and every termios request behave identically. I computed about two dozen FreeBSD 11 FIO*, TIOC* and SIOC* requests; none is classified as malformed, and only the deliberately bad probes are (0, 0x7b, 0x5401, 0x5413 and others).
  • Two independent reviews found nothing wrong. They checked the rule against the 11.4 sys_ioctl conditions and its order (the malformed check before the argument copy and before the descriptor lookup). check_conventions.py is clean and pr_overlap.py found only docs-line overlap with #4954, #5020 and #5053, which edit the same ioctl line in docs/dev/debt/libkernel.md (a one-line rebase).

Not covered: an input-only request with length 0 is deliberately not rejected, because FreeBSD treats it differently with and without the compatibility options and the console's kernel configuration is unknown (recorded in the debt note). FIOCLEX is claimed by #5186. There is no sceKernelIoctl export in the repo. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5839570149 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5564). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T23:59:34Z. --- `ioctl` (and `_ioctl`) with a malformed request throws "request ... is not implemented" on a file, directory, pipe, `stdin` or `stdout`, which ends the process, and returns the wrong error on other descriptors. FreeBSD 11.4 `sys_ioctl` returns `ENOTTY` for a malformed request before it reads the argument or looks at the descriptor. "Malformed" means: no direction bit (`IOC_VOID`, `IOC_IN` or `IOC_OUT`), an output request with length 0, or a void request with a length other than 4. A Linux request number such as `TCGETS` (0x5401) is malformed by that rule, so a guest or library that probes with it gets `ENOTTY` on a console. Here the same call aborts the guest on a file or pipe, returns `EBADF` on a closed descriptor, and returns `EFAULT` on a socket given a null argument. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-ioctl-malformed-request (one commit on a recent main: `File/src/DescriptorControl.cpp` and `.hpp` with a new `File::IoctlRequestMalformed` checked first in `File::Ioctl`, the socket path of `ioctl_nid_postfix` in `Udp.cpp` calling the same check, tests in `tests/GuestKernelFcntl.cpp`, and a debt note in `docs/dev/debt/libkernel.md`). Checks: - The new `guest_kernel_fcntl` cases fail on main (abort) and pass with the change; `guest_udp` and `guest_sendfile` pass. - A fuzz of 20,144 request words (10,000 random, 10,000 structured, 100 with garbage in the upper bits, 44 named) against file, directory, pipe read and write ends, UDP, TCP, a closed fd, -1, `stdin`, `stdout` and fd 4000, each with a null and a valid argument: 886,336 results per build. All 12,063 distinct malformed requests give `ENOTTY` (25) on every descriptor kind (main threw, or returned `EBADF` or `EFAULT`). All 7,602 distinct well-formed requests are unchanged: unknown ones still throw on files, sockets still give `ENOTTY` or `EFAULT` as before, and `FIONBIO`, `FIONREAD` and every termios request behave identically. I computed about two dozen FreeBSD 11 `FIO*`, `TIOC*` and `SIOC*` requests; none is classified as malformed, and only the deliberately bad probes are (0, 0x7b, 0x5401, 0x5413 and others). - Two independent reviews found nothing wrong. They checked the rule against the 11.4 `sys_ioctl` conditions and its order (the malformed check before the argument copy and before the descriptor lookup). `check_conventions.py` is clean and `pr_overlap.py` found only docs-line overlap with #4954, #5020 and #5053, which edit the same `ioctl` line in `docs/dev/debt/libkernel.md` (a one-line rebase). Not covered: an input-only request with length 0 is deliberately not rejected, because FreeBSD treats it differently with and without the compatibility options and the console's kernel configuration is unknown (recorded in the debt note). `FIOCLEX` is claimed by #5186. There is no `sceKernelIoctl` export in the repo. Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-14 00:08:59 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T00:08:59Z.


I carried the ready malformed-ioctl change into PR #5569. Local guest_kernel_fcntl and guest_sendfile passed three consecutive runs each, and conventions/diff checks pass. guest_udp was intermittent: it passed once, then aborted on repetition; restoring the current-main Udp.cpp reproduced the same pass-then-abort behavior, so I have not treated UDP as a reliable pass. The PR checks currently pass conventions, eligibility, intake, and report; platform jobs are skipped by path filters.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157873393 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5564#issuecomment-6157873393). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T00:08:59Z. --- I carried the ready malformed-ioctl change into [PR #5569](https://github.com/boykopovar/AnyPS5/pull/5569). Local `guest_kernel_fcntl` and `guest_sendfile` passed three consecutive runs each, and conventions/diff checks pass. `guest_udp` was intermittent: it passed once, then aborted on repetition; restoring the current-main `Udp.cpp` reproduced the same pass-then-abort behavior, so I have not treated UDP as a reliable pass. The PR checks currently pass conventions, eligibility, intake, and report; platform jobs are skipped by path filters.
oneandonlydean commented 2026-10-14 01:07:52 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:07:52Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5569 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158587742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5564#issuecomment-6158587742). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:07:52Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5569 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
Get-fix commented 2026-10-14 18:38:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Get-fix on 2026-10-14T18:38:44Z.


@oneandonlydean A program that reaches this on current main: the OpenOrbis PS4 toolchain sample net_http. Its musl C library sends the Linux TIOCGWINSZ (0x5413, no direction bits) to standard output on the first printf, and the run ends there:

ioctl: request 0x00005413 on a file is not implemented, fd=1

This was on Windows 11 with main e442f4c2e plus #5990 to #5994, which the sample needs to start (#3093). The throw comes from File::Ioctl on main, which those PRs don't change.

With #5569 merged as well, the sample runs its whole flow. sceNet, sceSsl and sceHttp initialize, and the sample prints its own report of the offline HTTP backend's SCE_HTTP_ERROR_NETWORK (sceHttpCreateRequestWithURL (80431063)) instead of aborting. guest_kernel_fcntl, guest_udp, guest_sendfile and guest_sce_net pass in that build.

#5569's head no longer merges cleanly into main. I resolved its two conflicts locally to test it, and nothing else:

  • the GuestKernelFcntl.cpp constants: kept both sets;
  • the ioctl line in docs/dev/debt/libkernel.md: kept main's FIOCLEX wording and added #5569's malformed-request sentence.

AI-assisted: yes (Claude Code). I ran the sample and the tests myself on Windows 11.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175617921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5564#issuecomment-6175617921). Originally posted by [Get-fix](https://github.com/Get-fix) on 2026-10-14T18:38:44Z. --- @oneandonlydean A program that reaches this on current `main`: the OpenOrbis PS4 toolchain sample `net_http`. Its musl C library sends the Linux `TIOCGWINSZ` (0x5413, no direction bits) to standard output on the first `printf`, and the run ends there: ``` ioctl: request 0x00005413 on a file is not implemented, fd=1 ``` This was on Windows 11 with `main` `e442f4c2e` plus #5990 to #5994, which the sample needs to start (#3093). The throw comes from `File::Ioctl` on `main`, which those PRs don't change. With #5569 merged as well, the sample runs its whole flow. `sceNet`, `sceSsl` and `sceHttp` initialize, and the sample prints its own report of the offline HTTP backend's `SCE_HTTP_ERROR_NETWORK` (`sceHttpCreateRequestWithURL (80431063)`) instead of aborting. `guest_kernel_fcntl`, `guest_udp`, `guest_sendfile` and `guest_sce_net` pass in that build. #5569's head no longer merges cleanly into `main`. I resolved its two conflicts locally to test it, and nothing else: - the `GuestKernelFcntl.cpp` constants: kept both sets; - the `ioctl` line in `docs/dev/debt/libkernel.md`: kept `main`'s `FIOCLEX` wording and added #5569's malformed-request sentence. AI-assisted: yes (Claude Code). I ran the sample and the tests myself on Windows 11. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
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#5564
No description provided.