From 8f9c4ba64cbb9a45e6f631c9aad0c46f460270e3 Mon Sep 17 00:00:00 2001 From: Mo Date: Sun, 29 Nov 2026 01:04:58 -0800 Subject: [PATCH] fix(libkernel): fail an unhandled ioctl on a local or AF_INET6 socket with EOPNOTSUPP, as tcgetattr and FreeBSD 11.4's pru_control do A request that FreeBSD's soo_ioctl hands to pru_control gave ENOTTY on a local socket and EADDRNOTAVAIL on an AF_INET6 socket. tcgetattr, which is the same ioctl on the console, was measured to give EOPNOTSUPP on a local socket pair (firmware 11.60), and the tc* exports already answer that. AF_INET keeps EADDRNOTAVAIL and groups i, r and f keep ENOTTY. The requests FreeBSD answers before that point keep their results: SIOCATMARK, SIOCSPGRP and SIOCGPGRP, and on AF_INET6 group u 106 to 109. Co-Authored-By: Claude Fable 5.2 --- core/libs/prx/libkernel/Socket/src/Udp.cpp | 9 ++++- core/libs/tests/GuestKernelFcntl.cpp | 38 +++++++++++++++++++ core/libs/tests/GuestKernelSys.cpp | 4 ++ ...ix-libSceNet-ioctl-inet-unknown-request.md | 2 +- ...ernel-ioctl-pipe-unknown-request-enotty.md | 2 +- .../fix-libkernel-socket-ioctl-eopnotsupp.md | 5 +++ 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 docs/dev/debt/changes/fix-libkernel-socket-ioctl-eopnotsupp.md diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 35395a421f59..129ac9c328d9 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -2643,6 +2643,10 @@ int APS5_VABI getpeername_nid_postfix(int descriptor, void* address, std::uint32 if (::getpeername(nativeSocket, reinterpret_cast(&native), &size)) return Fail(NativeError()); return CopyAddressOut(native, false, address, length, capacity); } +static bool AnsweredBeforeInterfaceCheck(int family, std::uint32_t command) { + if (command == 0x40047307 || command == 0x80047308 || command == 0x40047309) return true; + return family == 28 && (command == 0xc050756a || command == 0xc028756b || command == 0x8048756c || command == 0x8048756d); +} int APS5_VABI ioctl_nid_postfix(int descriptor, std::uint64_t request, void* argument) { if (!GuestSockets::IsOpen(descriptor)) { const auto command = static_cast(request); @@ -2671,8 +2675,9 @@ int APS5_VABI ioctl_nid_postfix(int descriptor, std::uint64_t request, void* arg } if (request != 0x8004667e && request != 0x4004667f) { const char group = static_cast((request >> 8) & 0xff); - const bool internet = socket->family == 2 || socket->family == 28; - return Fail(internet && group != 'i' && group != 'r' && group != 'f' ? 49 : 25); + if (group == 'i' || group == 'r' || group == 'f') return Fail(25); + if (AnsweredBeforeInterfaceCheck(socket->family, command)) return Fail(socket->family == 2 || socket->family == 28 ? 49 : 25); + return Fail(socket->family == 2 ? 49 : 45); } std::lock_guard lock(socket->modeMutex); if (request == 0x4004667f && socket->receiveShut) { diff --git a/core/libs/tests/GuestKernelFcntl.cpp b/core/libs/tests/GuestKernelFcntl.cpp index 274cf4eb9faf..b88a2bba3a43 100644 --- a/core/libs/tests/GuestKernelFcntl.cpp +++ b/core/libs/tests/GuestKernelFcntl.cpp @@ -79,6 +79,9 @@ static constexpr std::uint64_t KeepOnExecRequest = 0x20006602; static constexpr std::uint64_t MalformedRequests[] = {0, 123, 0x5401, 0x5413, 0x40007468, 0x20087468, 0x00087468}; static constexpr std::uint64_t IntegerArgumentRequest = 0x20047801; static constexpr std::uint64_t EmptyInputRequest = 0x8000667f; +static constexpr std::uint64_t InterfaceFlagsRequest = 0xc0206911; +static constexpr std::uint64_t SocketStateRequests[] = {0x40047307, 0x80047308, 0x40047309}; +static constexpr std::uint64_t Internet6ControlRequests[] = {0xc050756a, 0xc028756b, 0x8048756c, 0x8048756d}; static constexpr int SceEbadf = static_cast(0x80020009u); static constexpr int SceEinval = static_cast(0x80020016u); static constexpr int SceEnotsup = static_cast(0x8002002du); @@ -249,6 +252,40 @@ static void SocketsKeepTheirPath() { Require(named >= 0 && sceKernelFcntl(named, GetFlags) == ReadWrite && close_nid_postfix(named) == 0); } +template static void RequireRequestsFail(int descriptor, const std::uint64_t (&requests)[TCount], int error) { + for (const std::uint64_t request : requests) { + unsigned char argument[80] = {}; + RequireFailure(ioctl_nid_postfix(descriptor, request, argument), error); + } +} + +static void RequireProtocolRequestsUnsupported(int descriptor) { + char windowSize[8] = {}; + RequireFailure(ioctl_nid_postfix(descriptor, WindowSizeRequest, windowSize), GuestEopnotsupp); + RequireFailure(ioctl_nid_postfix(descriptor, IntegerArgumentRequest, nullptr), GuestEopnotsupp); +} + +static void LocalAndInternet6SocketsLeaveProtocolRequestsUnsupported() { + int pair[2] = {-1, -1}; + Require(socketpair_nid_postfix(1, 1, 0, pair) == 0); + RequireProtocolRequestsUnsupported(pair[0]); + RequireRequestsFail(pair[0], SocketStateRequests, GuestEnotty); + RequireRequestsFail(pair[0], Internet6ControlRequests, GuestEopnotsupp); + unsigned char attributes[44] = {}; + RequireFailure(tcgetattr_nid_postfix(pair[0], attributes), GuestEopnotsupp); + int queued = -1; + Require(ioctl_nid_postfix(pair[0], ReadableRequest, &queued) == 0 && queued == 0); + char interfaceRequest[32] = {}; + RequireFailure(ioctl_nid_postfix(pair[0], InterfaceFlagsRequest, interfaceRequest), GuestEnotty); + Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0); + const int socket = socket_nid_postfix(28, 2, 0); + Require(socket >= 0); + RequireProtocolRequestsUnsupported(socket); + RequireRequestsFail(socket, SocketStateRequests, GuestEaddrnotavail); + RequireRequestsFail(socket, Internet6ControlRequests, GuestEaddrnotavail); + Require(close_nid_postfix(socket) == 0); +} + static void FileFlagsRoundTrip() { std::remove("kernel_fcntl_probe.bin"); const int file = sceKernelOpen("kernel_fcntl_probe.bin", CreateReadWrite, 0644); @@ -711,6 +748,7 @@ static void CharacterDevicesHaveNoDeviceType() { int main() { SocketsKeepTheirPath(); + LocalAndInternet6SocketsLeaveProtocolRequestsUnsupported(); FileFlagsRoundTrip(); RecordLocksBelongToTheProcess(); PipeFlagsRoundTrip(); diff --git a/core/libs/tests/GuestKernelSys.cpp b/core/libs/tests/GuestKernelSys.cpp index add7ec90d38a..6daaf544c7ea 100644 --- a/core/libs/tests/GuestKernelSys.cpp +++ b/core/libs/tests/GuestKernelSys.cpp @@ -101,6 +101,10 @@ void ThreadAndSessionTests(int descriptor) { Require(socketDescriptor >= 0); Require(tcsetsid_nid_postfix(socketDescriptor, session) == -1 && Error() == 49); Require(close_nid_postfix(socketDescriptor) == 0); + int localPair[2] = {-1, -1}; + Require(socketpair_nid_postfix(1, 1, 0, localPair) == 0); + Require(tcsetsid_nid_postfix(localPair[0], session) == -1 && Error() == 45); + Require(close_nid_postfix(localPair[0]) == 0 && close_nid_postfix(localPair[1]) == 0); } bool ChrootThrows(const std::string& path) { diff --git a/docs/dev/debt/changes/fix-libSceNet-ioctl-inet-unknown-request.md b/docs/dev/debt/changes/fix-libSceNet-ioctl-inet-unknown-request.md index 3d2ac1fba188..8f81c802a866 100644 --- a/docs/dev/debt/changes/fix-libSceNet-ioctl-inet-unknown-request.md +++ b/docs/dev/debt/changes/fix-libSceNet-ioctl-inet-unknown-request.md @@ -6,4 +6,4 @@ ## add Functional -- [sceNetIoctl requests it does not handle](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - assumed, not measured, to be the `ioctl` system call on a kernel socket. The requests FreeBSD 11.4's `kern_ioctl` and `soo_ioctl` answer, other than `FIONBIO` and `FIONREAD`, fail with `ENOTTY` instead, as do `u` 106 to 109 on `AF_INET6`, which `in6_control` answers. First, a malformed request fails with `ENOTTY` and an `IOC_IN` argument is copied in (`EFAULT`), as [`sys_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L709-L739) does. `i`, `r` and `f` requests fail with `ENOTTY` (FreeBSD sends `i` and `r` elsewhere; `f` is unmeasured). Others fail with `EADDRNOTAVAIL` on `AF_INET`, as `in_control` does with no interface (measured on a libkernel socket), and `EOPNOTSUPP` on `AF_INET6`, as [`in6_control`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet6/in6.c#L244-L270) does; libkernel's `ioctl` gives `EADDRNOTAVAIL` there +- [sceNetIoctl requests it does not handle](../../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) - assumed, not measured, to be the `ioctl` system call on a kernel socket. The requests FreeBSD 11.4's `kern_ioctl` and `soo_ioctl` answer, other than `FIONBIO` and `FIONREAD`, fail with `ENOTTY` instead, as do `u` 106 to 109 on `AF_INET6`, which `in6_control` answers. First, a malformed request fails with `ENOTTY` and an `IOC_IN` argument is copied in (`EFAULT`), as [`sys_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L709-L739) does. `i`, `r` and `f` requests fail with `ENOTTY` (FreeBSD sends `i` and `r` elsewhere; `f` is unmeasured). Others fail with `EADDRNOTAVAIL` on `AF_INET`, as `in_control` does with no interface (measured on a libkernel socket), and `EOPNOTSUPP` on `AF_INET6`, as [`in6_control`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/netinet6/in6.c#L244-L270) does diff --git a/docs/dev/debt/changes/fix-libkernel-ioctl-pipe-unknown-request-enotty.md b/docs/dev/debt/changes/fix-libkernel-ioctl-pipe-unknown-request-enotty.md index 7a91a9fbd127..3da46d88fa05 100644 --- a/docs/dev/debt/changes/fix-libkernel-ioctl-pipe-unknown-request-enotty.md +++ b/docs/dev/debt/changes/fix-libkernel-ioctl-pipe-unknown-request-enotty.md @@ -6,4 +6,4 @@ ## change any other well-formed request not listed here -- [ioctl, _ioctl](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - `FIONREAD` on an ordinary directory returns a constant 65536 here, matching one PS5 firmware 13.60 payload measurement of a directory at offset 0; archive-backed virtual directories keep their previous `EINVAL` result, and whether the console value varies with directory size or offset is unmeasured. Any other well-formed request not listed here or in the `ioctl on` entries throws. On a socket only `FIONBIO`, `FIONREAD`, `FIOCLEX` and `FIONCLEX` are handled; another request fails with `EADDRNOTAVAIL` on an `AF_INET` or `AF_INET6` socket when its group is not `i`, `r` or `f` (`TIOCGWINSZ` measured), as FreeBSD's `soo_ioctl` hands it to `in_control`, and with `ENOTTY` otherwise (`i` and `r` go elsewhere there, `f` is unmeasured). Other descriptors follow FreeBSD's `kern_ioctl`, `vn_ioctl` and `pipe_ioctl`: `FIONBIO`, `FIOCLEX`, `FIONCLEX`, `FIONREAD` on files and pipes, `ENOTTY` for termios requests. An input or output request with length 0 fails with `ENOTTY` on every descriptor; only `0x8000667f` was measured; `sys_ioctl` checks just direction and length. Stremio Plus (PPSA74126) imports `_ioctl` for its `tc*` functions +- [ioctl, _ioctl](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - `FIONREAD` on an ordinary directory returns a constant 65536 here, matching one PS5 firmware 13.60 payload measurement of a directory at offset 0; archive-backed virtual directories keep their previous `EINVAL` result, and whether the console value varies with directory size or offset is unmeasured. Any other well-formed request not listed here or in the `ioctl on` entries throws. On a socket only `FIONBIO`, `FIONREAD`, `FIOCLEX` and `FIONCLEX` are handled; groups `i`, `r` (sent elsewhere by FreeBSD) and `f` (unmeasured) give `ENOTTY`, others `EADDRNOTAVAIL` on `AF_INET` (`TIOCGWINSZ` measured) and `EOPNOTSUPP` elsewhere (`tcgetattr` on a local pair measured, `AF_INET6` not), as `pru_control` does, except `SIOCATMARK`, `SIOCSPGRP`, `SIOCGPGRP` and `AF_INET6`'s `u` 106 to 109, answered earlier: `EADDRNOTAVAIL`, or `ENOTTY` on local sockets. Other descriptors follow FreeBSD's `kern_ioctl`, `vn_ioctl` and `pipe_ioctl`: `FIONBIO`, `FIOCLEX`, `FIONCLEX`, `FIONREAD` on files and pipes, `ENOTTY` for termios requests. An input or output request with length 0 fails with `ENOTTY` on every descriptor; only `0x8000667f` was measured; `sys_ioctl` checks just direction and length. Stremio Plus (PPSA74126) imports `_ioctl` for its `tc*` functions diff --git a/docs/dev/debt/changes/fix-libkernel-socket-ioctl-eopnotsupp.md b/docs/dev/debt/changes/fix-libkernel-socket-ioctl-eopnotsupp.md new file mode 100644 index 000000000000..33671540aa47 --- /dev/null +++ b/docs/dev/debt/changes/fix-libkernel-socket-ioctl-eopnotsupp.md @@ -0,0 +1,5 @@ +# libkernel_sys + +## change scePthreadMutexIsowned, utrace and tcsetsid + +- [scePthreadMutexIsowned, utrace and tcsetsid](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) follow FreeBSD 11.4 except where stated. `scePthreadMutexIsowned` returns 0 when libkernel's `pthread_mutex_isowned_np` does and `0x80020000` plus its result (`0x80020001`) for a mutex the calling thread holds, as firmware 13.42's `libkernel_sys.sprx` was measured to do in an emulator ([#9713](https://github.com/boykopovar/AnyPS5/pull/9713)); a console run is not measured. `utrace` returns 0 without reading its record, as [`sys_utrace`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/kern_ktrace.c) does for an untraced process; that the PS5 kernel has `KTRACE` (else `ENOSYS`) is assumed. `tcsetsid` fails with `EINVAL` unless `pid` is `getsid(0)`, then issues `TIOCSCTTY`, which guest descriptors refuse (`ENOTTY`, `AF_INET` sockets `EADDRNOTAVAIL`, other sockets `EOPNOTSUPP`, closed `EBADF`), as libc's [termios.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/gen/termios.c)