Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions core/libs/prx/libkernel/Socket/src/Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2643,6 +2643,10 @@ int APS5_VABI getpeername_nid_postfix(int descriptor, void* address, std::uint32
if (::getpeername(nativeSocket, reinterpret_cast<sockaddr*>(&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<std::uint32_t>(request);
Expand Down Expand Up @@ -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<char>((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) {
Expand Down
38 changes: 38 additions & 0 deletions core/libs/tests/GuestKernelFcntl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(0x80020009u);
static constexpr int SceEinval = static_cast<int>(0x80020016u);
static constexpr int SceEnotsup = static_cast<int>(0x8002002du);
Expand Down Expand Up @@ -249,6 +252,40 @@ static void SocketsKeepTheirPath() {
Require(named >= 0 && sceKernelFcntl(named, GetFlags) == ReadWrite && close_nid_postfix(named) == 0);
}

template <std::size_t TCount> 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);
Expand Down Expand Up @@ -711,6 +748,7 @@ static void CharacterDevicesHaveNoDeviceType() {

int main() {
SocketsKeepTheirPath();
LocalAndInternet6SocketsLeaveProtocolRequestsUnsupported();
FileFlagsRoundTrip();
RecordLocksBelongToTheProcess();
PipeFlagsRoundTrip();
Expand Down
4 changes: 4 additions & 0 deletions core/libs/tests/GuestKernelSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

- [ioctl on a pipe](../../../core/libs/prx/libkernel/File/src/DescriptorControl.cpp) (libkernel) - `FIOSETOWN` and `FIOGETOWN` throw, as the pipe's `SIGIO` owner is not kept, and `TIOCSPGRP` and `TIOCGPGRP` fail with `ENOTTY` like every termios request, where FreeBSD 11.4's [`pipe_ioctl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_pipe.c#L1346-L1419) sets and reads that owner. Descriptors 0 to 2 are not taken as pipes, whatever the host attaches to them, so other requests there still throw. Above them, any other request without a case for every descriptor fails with `ENOTTY`, as the `default` of `pipe_ioctl` does, after an input argument is copied in (`EFAULT` if it cannot be read) unless `IOC_VOID` passes it by value, as `sys_ioctl` does; a FIFO takes the same path, as [`fifo_open`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/fs/fifofs/fifo_vnops.c#L252) gives it the pipe operations

## change any other well-formed request not listed here

Check warning on line 7 in docs/dev/debt/changes/fix-libkernel-ioctl-pipe-unknown-request-enotty.md

View workflow job for this annotation

GitHub Actions / conventions

debt-length

Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 1336 characters

- [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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libkernel_sys

## change scePthreadMutexIsowned, utrace and tcsetsid

Check warning on line 3 in docs/dev/debt/changes/fix-libkernel-socket-ioctl-eopnotsupp.md

View workflow job for this annotation

GitHub Actions / conventions

debt-length

Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 1033 characters

- [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)
Loading