From fe8782adc3cc09e383ef68e6458d37ef56bbaa7e Mon Sep 17 00:00:00 2001 From: valenciana-nc Date: Sun, 18 Oct 2026 21:35:23 +0000 Subject: [PATCH 1/4] fix(libkernel): validate recvmsg guest output buffers in the guest arena recvmsg_nid_postfix wrote the msghdr, every iovec base and the sender address with only null checks, unlike recv_nid_postfix and the sceNetRecvfrom sender outputs which all gate on GuestArena::HostWrite. Validate the message, each iovec base and the name buffer the same way so a forged pointer fails with EFAULT instead of writing received bytes to an arbitrary host address. --- core/libs/prx/libkernel/Socket/src/Udp.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index b4c71263e09f..7375ca243ce0 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -1322,6 +1322,9 @@ std::int64_t APS5_VABI sendmsg_nid_postfix(int descriptor, const GuestMsghdr* me return sendto_nid_postfix(descriptor, buffer.data(), buffer.size(), flags, message->name, message->name ? message->nameLength : 0u); } std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, int flags) { + if (message == nullptr) return Fail(14); + const GuestArena::HostWrite messageDestination(message, sizeof(*message)); + if (!messageDestination.Open()) return Fail(14); const auto total = MessageLength(message); if (total < 0) return -1; const auto socket = Lookup(descriptor); @@ -1354,14 +1357,18 @@ std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, } std::size_t offset = 0; for (int i = 0; i < message->iovLength && offset < static_cast(received); ++i) { + const GuestArena::HostWrite destination(message->iov[i].base, message->iov[i].length); + if (!destination.Open()) return Fail(14); const auto count = std::min(message->iov[i].length, static_cast(received) - offset); std::memcpy(message->iov[i].base, buffer.data() + offset, count); offset += count; } - if (message->name && socket->family == GuestUnix) { - if (socket->type == 1) message->nameLength = 0; else UnnamedAddress(message->name, &message->nameLength); - } else if (message->name) { - if (size > 0 && (native.ss_family == AF_INET || native.ss_family == AF_INET6)) { + if (message->name != nullptr) { + const GuestArena::HostWrite nameDestination(message->name, message->nameLength); + if (!nameDestination.Open()) return Fail(14); + if (socket->family == GuestUnix) { + if (socket->type == 1) message->nameLength = 0; else UnnamedAddress(message->name, &message->nameLength); + } else if (size > 0 && (native.ss_family == AF_INET || native.ss_family == AF_INET6)) { GuestAddress(native, message->name, &message->nameLength); } else { message->nameLength = 0; From 370f48be82bc86ec188eef5fcda62a0767f18b32 Mon Sep 17 00:00:00 2001 From: valenciana-nc Date: Sun, 18 Oct 2026 23:34:56 +0000 Subject: [PATCH 2/4] fix(libkernel): validate recvmsg guest pointer ranges are committed Address review: check the msghdr, iovec array, iovec bases and name are mapped before MessageLength or the receive loop touches them, pairing the HostWrite guard with committed-range validation. Add regression tests for forged iovec-array and output pointers. --- core/libs/prx/libkernel/Socket/src/Udp.cpp | 50 ++++++++++++++++++++-- core/libs/tests/GuestSocketPair.cpp | 10 +++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 7375ca243ce0..271a4bcd30a0 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #endif #include "SceTypes.hpp" #include "prx/libc/include/GuestArena.hpp" @@ -49,6 +50,7 @@ #include #include #include +#include extern "C" int* APS5_VABI __error_nid_postfix(); namespace { @@ -1321,15 +1323,59 @@ std::int64_t APS5_VABI sendmsg_nid_postfix(int descriptor, const GuestMsghdr* me } return sendto_nid_postfix(descriptor, buffer.data(), buffer.size(), flags, message->name, message->name ? message->nameLength : 0u); } +static bool CommittedRange(const void* pointer, std::size_t bytes) { + if (bytes == 0) return true; + if (pointer == nullptr) return false; + const auto start = reinterpret_cast(pointer); + if (start + bytes < start) return false; +#ifdef _WIN32 + auto address = start & ~static_cast(0xFFF); + const auto end = start + bytes; + while (address < end) { + MEMORY_BASIC_INFORMATION info{}; + if (VirtualQuery(reinterpret_cast(address), &info, sizeof(info)) == 0) return false; + if (info.State != MEM_COMMIT) return false; + address = reinterpret_cast(info.BaseAddress) + info.RegionSize; + } + return true; +#else + const long page = sysconf(_SC_PAGESIZE); + if (page <= 0) return false; + const auto unit = static_cast(page); + const auto end = start + bytes; + const auto alignedEnd = (end + unit - 1) & ~(unit - 1); + if (alignedEnd < end) return false; + const auto aligned = start & ~(unit - 1); + const auto length = alignedEnd - aligned; + std::vector resident((length + unit - 1) / unit); + return mincore(reinterpret_cast(aligned), length, resident.data()) == 0; +#endif +} std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, int flags) { if (message == nullptr) return Fail(14); + if (!CommittedRange(message, sizeof(*message))) return Fail(14); const GuestArena::HostWrite messageDestination(message, sizeof(*message)); if (!messageDestination.Open()) return Fail(14); + if (message->iovLength < 0 || message->iovLength > 1024) return Fail(40); + if (message->iovLength != 0 && !CommittedRange(message->iov, static_cast(message->iovLength) * sizeof(GuestIovec))) return Fail(14); const auto total = MessageLength(message); if (total < 0) return -1; const auto socket = Lookup(descriptor); if (!socket) return -1; if ((flags & ~GuestReceiveFlags) != 0) return Fail(45); + std::deque destinations; + for (int i = 0; i < message->iovLength; ++i) { + auto* base = message->iov[i].base; + const auto length = static_cast(message->iov[i].length); + if (!CommittedRange(base, length)) return Fail(14); + destinations.emplace_back(base, length); + if (!destinations.back().Open()) return Fail(14); + } + if (message->name != nullptr) { + if (!CommittedRange(message->name, message->nameLength)) return Fail(14); + destinations.emplace_back(message->name, message->nameLength); + if (!destinations.back().Open()) return Fail(14); + } const bool records = socket->type == 2 || socket->type == 5; std::vector buffer; try { @@ -1357,15 +1403,11 @@ std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, } std::size_t offset = 0; for (int i = 0; i < message->iovLength && offset < static_cast(received); ++i) { - const GuestArena::HostWrite destination(message->iov[i].base, message->iov[i].length); - if (!destination.Open()) return Fail(14); const auto count = std::min(message->iov[i].length, static_cast(received) - offset); std::memcpy(message->iov[i].base, buffer.data() + offset, count); offset += count; } if (message->name != nullptr) { - const GuestArena::HostWrite nameDestination(message->name, message->nameLength); - if (!nameDestination.Open()) return Fail(14); if (socket->family == GuestUnix) { if (socket->type == 1) message->nameLength = 0; else UnnamedAddress(message->name, &message->nameLength); } else if (size > 0 && (native.ss_family == AF_INET || native.ss_family == AF_INET6)) { diff --git a/core/libs/tests/GuestSocketPair.cpp b/core/libs/tests/GuestSocketPair.cpp index 15dabd1ef689..694812a7ea98 100644 --- a/core/libs/tests/GuestSocketPair.cpp +++ b/core/libs/tests/GuestSocketPair.cpp @@ -192,6 +192,16 @@ int main() { piece = {whole, sizeof(whole)}; message.flags = -1; Require(recvmsg_nid_postfix(pair[1], &message, 0) == 4 && message.flags == 0 && std::memcmp(whole, "next", 4) == 0); + Require(socketpair_nid_postfix(Unix, Datagram, 0, pair) == 0); + Require(send_nid_postfix(pair[0], "x", 1, 0) == 1); + Msghdr forgedIov{nullptr, 0, reinterpret_cast(0x1), 1, nullptr, 0, -1}; + Require(recvmsg_nid_postfix(pair[1], &forgedIov, 0) == -1 && *__error_nid_postfix() == 14); + Iovec forgedBase{reinterpret_cast(0x1), 16}; + Msghdr forgedOutput{nullptr, 0, &forgedBase, 1, nullptr, 0, -1}; + Require(recvmsg_nid_postfix(pair[1], &forgedOutput, 0) == -1 && *__error_nid_postfix() == 14); + Iovec good{part, sizeof(part)}; + Msghdr forgedName{reinterpret_cast(0x1), 16, &good, 1, nullptr, 0, -1}; + Require(recvmsg_nid_postfix(pair[1], &forgedName, 0) == -1 && *__error_nid_postfix() == 14); Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0); #endif From 7ef31e91cfba57cdbfba9a790025c46c5fac3529 Mon Sep 17 00:00:00 2001 From: valenciana-nc Date: Sun, 18 Oct 2026 19:09:30 -0600 Subject: [PATCH 3/4] fix(libkernel): check only the guest bytes recvmsg reads and writes --- core/libs/prx/libkernel/Socket/src/Udp.cpp | 131 ++++++++++++--------- core/libs/tests/GuestSocketPair.cpp | 83 +++++++++++-- docs/dev/debt/libkernel.md | 1 + 3 files changed, 152 insertions(+), 63 deletions(-) diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 271a4bcd30a0..cd69fd0b1ecc 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -18,7 +18,9 @@ #include #include #include -#include +#ifdef __linux__ +#include +#endif #endif #include "SceTypes.hpp" #include "prx/libc/include/GuestArena.hpp" @@ -50,7 +52,6 @@ #include #include #include -#include extern "C" int* APS5_VABI __error_nid_postfix(); namespace { @@ -1323,59 +1324,77 @@ std::int64_t APS5_VABI sendmsg_nid_postfix(int descriptor, const GuestMsghdr* me } return sendto_nid_postfix(descriptor, buffer.data(), buffer.size(), flags, message->name, message->name ? message->nameLength : 0u); } -static bool CommittedRange(const void* pointer, std::size_t bytes) { - if (bytes == 0) return true; - if (pointer == nullptr) return false; - const auto start = reinterpret_cast(pointer); - if (start + bytes < start) return false; #ifdef _WIN32 - auto address = start & ~static_cast(0xFFF); +static bool GuestPagesAllow(std::uintptr_t start, std::size_t bytes, DWORD allowed) { const auto end = start + bytes; - while (address < end) { - MEMORY_BASIC_INFORMATION info{}; - if (VirtualQuery(reinterpret_cast(address), &info, sizeof(info)) == 0) return false; - if (info.State != MEM_COMMIT) return false; - address = reinterpret_cast(info.BaseAddress) + info.RegionSize; + for (auto address = start; address < end;) { + MEMORY_BASIC_INFORMATION memory{}; + if (VirtualQuery(reinterpret_cast(address), &memory, sizeof(memory)) != sizeof(memory)) return false; + if (memory.State != MEM_COMMIT || (memory.Protect & PAGE_GUARD) != 0 || (memory.Protect & allowed) == 0) return false; + address = reinterpret_cast(memory.BaseAddress) + memory.RegionSize; } return true; +} +#elif defined(__linux__) +static bool Transferred(ssize_t copied, std::size_t bytes, const char* call) { + if (copied >= 0) return static_cast(copied) == bytes; + if (errno == EFAULT) return false; + throw std::runtime_error(std::string("recvmsg: ") + call + " failed with errno " + std::to_string(errno)); +} +#endif +static bool GuestRange(const void* pointer, std::size_t bytes) { + const auto start = reinterpret_cast(pointer); + return start != 0 && bytes <= UINTPTR_MAX - start; +} +static bool ReadGuest(void* destination, const void* source, std::size_t bytes) { + if (bytes == 0) return true; + if (!GuestRange(source, bytes)) return false; + GuestArena::GuestArenaPrepareHostRead_nid_no_patch(source, bytes); +#ifdef _WIN32 + if (!GuestPagesAllow(reinterpret_cast(source), bytes, + PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) return false; + std::memcpy(destination, source, bytes); + return true; +#elif defined(__linux__) + iovec local{destination, bytes}; + iovec remote{const_cast(source), bytes}; + return Transferred(::process_vm_readv(::getpid(), &local, 1, &remote, 1, 0), bytes, "process_vm_readv"); #else - const long page = sysconf(_SC_PAGESIZE); - if (page <= 0) return false; - const auto unit = static_cast(page); - const auto end = start + bytes; - const auto alignedEnd = (end + unit - 1) & ~(unit - 1); - if (alignedEnd < end) return false; - const auto aligned = start & ~(unit - 1); - const auto length = alignedEnd - aligned; - std::vector resident((length + unit - 1) / unit); - return mincore(reinterpret_cast(aligned), length, resident.data()) == 0; + std::memcpy(destination, source, bytes); + return true; +#endif +} +static bool WriteGuest(void* destination, const void* source, std::size_t bytes) { + if (bytes == 0) return true; + if (!GuestRange(destination, bytes)) return false; + const GuestArena::HostWrite guard(destination, bytes); + if (!guard.Open()) return false; +#ifdef _WIN32 + if (!GuestPagesAllow(reinterpret_cast(destination), bytes, + PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) return false; + std::memcpy(destination, source, bytes); + return true; +#elif defined(__linux__) + iovec local{const_cast(source), bytes}; + iovec remote{destination, bytes}; + return Transferred(::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0), bytes, "process_vm_writev"); +#else + std::memcpy(destination, source, bytes); + return true; #endif } std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, int flags) { - if (message == nullptr) return Fail(14); - if (!CommittedRange(message, sizeof(*message))) return Fail(14); - const GuestArena::HostWrite messageDestination(message, sizeof(*message)); - if (!messageDestination.Open()) return Fail(14); - if (message->iovLength < 0 || message->iovLength > 1024) return Fail(40); - if (message->iovLength != 0 && !CommittedRange(message->iov, static_cast(message->iovLength) * sizeof(GuestIovec))) return Fail(14); - const auto total = MessageLength(message); + GuestMsghdr header{}; + if (!message || !ReadGuest(&header, message, sizeof(header))) return Fail(14); + if (header.iovLength < 0 || header.iovLength > 1024) return Fail(40); + std::vector vectors(static_cast(header.iovLength)); + if (!ReadGuest(vectors.data(), header.iov, vectors.size() * sizeof(GuestIovec))) return Fail(14); + header.iov = vectors.data(); + const auto total = MessageLength(&header); if (total < 0) return -1; const auto socket = Lookup(descriptor); if (!socket) return -1; if ((flags & ~GuestReceiveFlags) != 0) return Fail(45); - std::deque destinations; - for (int i = 0; i < message->iovLength; ++i) { - auto* base = message->iov[i].base; - const auto length = static_cast(message->iov[i].length); - if (!CommittedRange(base, length)) return Fail(14); - destinations.emplace_back(base, length); - if (!destinations.back().Open()) return Fail(14); - } - if (message->name != nullptr) { - if (!CommittedRange(message->name, message->nameLength)) return Fail(14); - destinations.emplace_back(message->name, message->nameLength); - if (!destinations.back().Open()) return Fail(14); - } const bool records = socket->type == 2 || socket->type == 5; std::vector buffer; try { @@ -1396,27 +1415,33 @@ std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message, return Fail(NativeError()); #endif } - message->flags = 0; + header.flags = 0; if (received > total) { received = total; - message->flags = 0x10; + header.flags = 0x10; } std::size_t offset = 0; - for (int i = 0; i < message->iovLength && offset < static_cast(received); ++i) { - const auto count = std::min(message->iov[i].length, static_cast(received) - offset); - std::memcpy(message->iov[i].base, buffer.data() + offset, count); + for (const auto& vector : vectors) { + if (offset >= static_cast(received)) break; + const auto count = std::min(vector.length, static_cast(received) - offset); + if (!WriteGuest(vector.base, buffer.data() + offset, count)) return Fail(14); offset += count; } - if (message->name != nullptr) { + if (header.name) { + unsigned char address[28]{}; if (socket->family == GuestUnix) { - if (socket->type == 1) message->nameLength = 0; else UnnamedAddress(message->name, &message->nameLength); + if (socket->type == 1) header.nameLength = 0; else UnnamedAddress(address, &header.nameLength); } else if (size > 0 && (native.ss_family == AF_INET || native.ss_family == AF_INET6)) { - GuestAddress(native, message->name, &message->nameLength); + GuestAddress(native, address, &header.nameLength); } else { - message->nameLength = 0; + header.nameLength = 0; } + if (!WriteGuest(header.name, address, header.nameLength)) return Fail(14); + if (!WriteGuest(&message->nameLength, &header.nameLength, sizeof(header.nameLength))) return Fail(14); } - message->controlLength = 0; + header.controlLength = 0; + constexpr auto results = offsetof(GuestMsghdr, controlLength); + if (!WriteGuest(reinterpret_cast(message) + results, reinterpret_cast(&header) + results, sizeof(header) - results)) return Fail(14); return received; } struct GuestSendfileHeaders { diff --git a/core/libs/tests/GuestSocketPair.cpp b/core/libs/tests/GuestSocketPair.cpp index 694812a7ea98..bf0c712d3fea 100644 --- a/core/libs/tests/GuestSocketPair.cpp +++ b/core/libs/tests/GuestSocketPair.cpp @@ -41,6 +41,9 @@ int APS5_VABI poll_nid_postfix(PollDescriptor*, std::uint32_t, int); int APS5_VABI close_nid_postfix(int); std::int64_t APS5_VABI fstat_nid_disambig1_nid_postfix(int, FileStat*); int APS5_VABI sceKernelFstat(int, FileStat*); +int APS5_VABI sceKernelMapFlexibleMemory(void**, std::size_t, int, int); +int APS5_VABI sceKernelMprotect(const void*, std::size_t, int); +int APS5_VABI sceKernelMunmap(void*, std::size_t); int* APS5_VABI __error_nid_postfix(); } @@ -90,6 +93,75 @@ static bool Unnamed(int descriptor, int (APS5_VABI *query)(int, void*, std::uint return query(descriptor, name, &length) == 0 && length == 4 && name[0] == 16 && name[1] == 1 && name[4] == 0xaa; } +static bool ReceiveFaults(int descriptor, Msghdr* message) { + *__error_nid_postfix() = 0; + return recvmsg_nid_postfix(descriptor, message, 0) == -1 && *__error_nid_postfix() == 14; +} + +static bool Queue(int descriptor, int receiver, const char* text) { + const auto length = static_cast(std::strlen(text)); + return send_nid_postfix(descriptor, text, static_cast(length), 0) == length && Readable(receiver); +} + +static void CheckReceiveMessageFaults() { + constexpr std::size_t page = 0x4000; + void* memory = nullptr; + Require(sceKernelMapFlexibleMemory(&memory, page * 3, 3, 0) == 0 && memory != nullptr); + auto* writable = static_cast(memory); + auto* readOnly = writable + page; + auto* inaccessible = writable + 2 * page; + char data[16]{}; + Iovec good{data, sizeof(data)}; + auto* readOnlyMessage = reinterpret_cast(readOnly); + *readOnlyMessage = {nullptr, 0, &good, 1, nullptr, 0, -1}; + auto* readOnlyVector = reinterpret_cast(readOnly + 64); + *readOnlyVector = {data, sizeof(data)}; + Require(sceKernelMprotect(readOnly, page, 1) == 0 && sceKernelMprotect(inaccessible, page, 0) == 0); + int pair[2] = {-1, -1}; + Require(socketpair_nid_postfix(Unix, Datagram, 0, pair) == 0); + + Require(Queue(pair[0], pair[1], "x")); + Require(ReceiveFaults(pair[1], reinterpret_cast(0x1000))); + Require(ReceiveFaults(pair[1], reinterpret_cast(inaccessible))); + Msghdr message{nullptr, 0, reinterpret_cast(0x1000), 1, nullptr, 0, -1}; + Require(ReceiveFaults(pair[1], &message)); + message.iov = reinterpret_cast(inaccessible); + Require(ReceiveFaults(pair[1], &message)); + char peeked[4]{}; + Require(recv_nid_postfix(pair[1], peeked, sizeof(peeked), Peek) == 1 && peeked[0] == 'x'); + + Iovec vector{reinterpret_cast(0x1000), sizeof(data)}; + message = {nullptr, 0, &vector, 1, nullptr, 0, -1}; + Require(ReceiveFaults(pair[1], &message)); + vector.base = readOnly + 128; + Require(Queue(pair[0], pair[1], "x") && ReceiveFaults(pair[1], &message)); + message = {reinterpret_cast(0x1000), 16, &good, 1, nullptr, 0, -1}; + Require(Queue(pair[0], pair[1], "x") && ReceiveFaults(pair[1], &message)); + message.name = readOnly + 128; + Require(Queue(pair[0], pair[1], "x") && ReceiveFaults(pair[1], &message)); + Require(Queue(pair[0], pair[1], "x") && ReceiveFaults(pair[1], readOnlyMessage)); + Require(fcntl_nid_postfix(pair[1], 4, 4) == 0); + Require(recv_nid_postfix(pair[1], peeked, sizeof(peeked), 0) == -1 && *__error_nid_postfix() == 35); + Require(fcntl_nid_postfix(pair[1], 4, 0) == 0); + + std::uint8_t name[32]; + std::memset(name, 0xa5, sizeof(name)); + vector = {writable, 1u << 20}; + message = {name, UINT32_MAX, &vector, 1, nullptr, 7, -1}; + Require(Queue(pair[0], pair[1], "abcd") && recvmsg_nid_postfix(pair[1], &message, 0) == 4); + Require(std::memcmp(writable, "abcd", 4) == 0 && message.flags == 0 && message.controlLength == 0); + Require(message.nameLength == 16 && name[0] == 16 && name[1] == Unix && name[16] == 0xa5); + Iovec vectors[2]{{writable, 2}, {inaccessible, 16}}; + message = {nullptr, 0, vectors, 2, nullptr, 0, -1}; + Require(Queue(pair[0], pair[1], "ef") && recvmsg_nid_postfix(pair[1], &message, 0) == 2 && std::memcmp(writable, "ef", 2) == 0); + message = {readOnly + 128, 0, readOnlyVector, 1, nullptr, 0, -1}; + Require(Queue(pair[0], pair[1], "gh") && recvmsg_nid_postfix(pair[1], &message, 0) == 2); + Require(std::memcmp(data, "gh", 2) == 0 && message.nameLength == 0); + + Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0); + Require(sceKernelMprotect(memory, page * 3, 3) == 0 && sceKernelMunmap(memory, page * 3) == 0); +} + int main() { int pair[2] = {-1, -1}; Require(socketpair_nid_postfix(Unix, Stream, 0, pair) == 0); @@ -192,18 +264,9 @@ int main() { piece = {whole, sizeof(whole)}; message.flags = -1; Require(recvmsg_nid_postfix(pair[1], &message, 0) == 4 && message.flags == 0 && std::memcmp(whole, "next", 4) == 0); - Require(socketpair_nid_postfix(Unix, Datagram, 0, pair) == 0); - Require(send_nid_postfix(pair[0], "x", 1, 0) == 1); - Msghdr forgedIov{nullptr, 0, reinterpret_cast(0x1), 1, nullptr, 0, -1}; - Require(recvmsg_nid_postfix(pair[1], &forgedIov, 0) == -1 && *__error_nid_postfix() == 14); - Iovec forgedBase{reinterpret_cast(0x1), 16}; - Msghdr forgedOutput{nullptr, 0, &forgedBase, 1, nullptr, 0, -1}; - Require(recvmsg_nid_postfix(pair[1], &forgedOutput, 0) == -1 && *__error_nid_postfix() == 14); - Iovec good{part, sizeof(part)}; - Msghdr forgedName{reinterpret_cast(0x1), 16, &good, 1, nullptr, 0, -1}; - Require(recvmsg_nid_postfix(pair[1], &forgedName, 0) == -1 && *__error_nid_postfix() == 14); Require(close_nid_postfix(pair[0]) == 0 && close_nid_postfix(pair[1]) == 0); #endif + CheckReceiveMessageFaults(); Require(Fails(2, Stream, 0, 45)); Require(Fails(28, Datagram, 0, 45)); diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 20db1591ed64..a1072eec7c8d 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -171,6 +171,7 @@ - [nmount](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) throws: mounting file systems is not supported. Prospero Themes (PPSA90210) imports it and fails to load without it; it opens its theme library without calling it, and its strings (`/dev/ssd0.system_ex`, `/system_ex`, `exfatfs`, `fstype`, `fspath`, `from`, `ignoreacl`) point to remounting the console's `/system_ex` partition to write theme files there. - [socketpair](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - the pair is the host's `AF_UNIX` pair on Linux and, on Windows, a loopback TCP connection for `SOCK_STREAM` or two connected UDP sockets for `SOCK_DGRAM`; `SOCK_SEQPACKET` throws on Windows. A datagram sent to a full receiver gets `ENOBUFS` on the console, waits or gets `EAGAIN` on Linux and is dropped on Windows, and on Windows a datagram `recv` after the peer closed reports `ECONNRESET` instead of the console's `ENOTCONN`, and a stream `send` after the peer closed reports `ECONNABORTED` instead of the console's `EPIPE`. `read` and `write` on a pair end go to the host like on every guest socket. `getpeername` on a pair end succeeds after the peer closed, where the console returns `ENOTCONN`, and `connect`, `bind` and `sendto` with an address on a pair end report an address-family error instead of the console's `EISCONN`. On Windows `poll` on a stream end reports `POLLHUP` without `POLLIN` after the peer's `shutdown(SHUT_WR)` or `close`, where the console reports `POLLIN`, with `POLLHUP` only after `close` - [UDP recv/recvfrom](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) and [sceNetRecv/sceNetRecvfrom](../../../core/libs/prx/libSceNet/Export.cpp) translate Windows WSAEMSGSIZE into the copied byte count for datagram sockets, including MSG_PEEK, and retain the sender address. This follows FreeBSD 11.4 recv(2) and Winsock's documented truncated-datagram behavior; console behavior and existing title impact are unverified. +- [recvmsg](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) reads the `msghdr` and the iovec array and writes the data, the sender address and the `msghdr` results through copies that fail with `EFAULT` when a page is not mapped with the access the copy needs: `process_vm_readv` and `process_vm_writev` on the own process on Linux, `VirtualQuery` before the copy on Windows; on macOS hosts the copies are not checked. Only the bytes copied are checked, so iovec and name lengths larger than the received data are accepted. The outputs are checked after the host receive, so a fault drops the received data. FreeBSD 11.4 also drops a datagram on a copy fault ([`soreceive_dgram`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2308-L2312), [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1758-L1774)) but leaves unread stream data queued, which is lost here. A `process_vm_readv` or `process_vm_writev` failure other than `EFAULT` throws - [MSG_DONTWAIT](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) in `send`, `sendto`, `sendmsg`, `recv`, `recvfrom` and `recvmsg` makes that one call non-blocking through the host's `MSG_DONTWAIT`, as FreeBSD 11.4's `soreceive_generic` returns `EWOULDBLOCK` for it (`sys/kern/uipc_socket.c`). Winsock has no per-call non-blocking flag, so on Windows these calls still fail with `EOPNOTSUPP` for it - [send, sendto](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - send and sendto without an address on an unconnected datagram socket map Windows `WSAENOTCONN` to FreeBSD's `EDESTADDRREQ` (39). - [read, write and vector I/O](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) on a guest socket use `recv` and `send` without flags; `sceKernelReadv` and `sceKernelWritev` gather or scatter through one socket operation, preserving stream short reads and datagram boundaries. The positioned `pread`, `pwrite`, `preadv` and `pwritev` forms, and their `sceKernel` forms, return `ESPIPE` for an open guest socket and `EBADF` for a closed one. `EPIPE` from a write raises the host's `SIGPIPE` on Linux unless the socket has `SO_NOSIGPIPE` set, and the guest cannot ignore it: `signal(SIGPIPE, SIG_IGN)` and `sigaction` record the disposition, as they do for every number from 1 to 128, but only the six signals with host equivalents (2, 4, 6, 8, 11 and 15) take effect on the host or are delivered, so the host `SIGPIPE` is not stopped; nothing is raised on Windows; a `write` longer than `INT_MAX` bytes is `EMSGSIZE` where the console sends it. AIO on a guest socket still goes to the host, which has no such descriptor From f7fd9dbf9f6ca5c2441044a0be0c6d9af6c1e362 Mon Sep 17 00:00:00 2001 From: valenciana-nc Date: Mon, 19 Oct 2026 06:25:31 -0600 Subject: [PATCH 4/4] fix(libkernel): check recvmsg guest copies on macOS --- core/libs/prx/libkernel/CMakeLists.txt | 5 +++-- core/libs/prx/libkernel/Socket/src/Udp.cpp | 10 ++++++++++ core/libs/tests/GuestSocketPair.cpp | 9 ++++++++- docs/dev/debt/libkernel.md | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 46d29099ff31..cd91bcf8e59a 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -1388,9 +1388,10 @@ if(BUILD_TESTING) target_link_libraries(guest_socket_pair_tests PRIVATE libkernel libc) configure_windows_unwind(guest_socket_pair_tests) add_test(NAME guest_socket_pair COMMAND guest_socket_pair_tests) - set_tests_properties(guest_socket_pair PROPERTIES TIMEOUT 20) + add_test(NAME guest_recvmsg_memory COMMAND guest_socket_pair_tests recvmsg-memory) + set_tests_properties(guest_socket_pair guest_recvmsg_memory PROPERTIES TIMEOUT 20) if(WIN32) - set_tests_properties(guest_socket_pair PROPERTIES ENVIRONMENT_MODIFICATION + set_tests_properties(guest_socket_pair guest_recvmsg_memory PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test_executable(guest_kernel_app_archive_tests ${LIBS_INCLUDE_DIR}/tests/GuestKernelAppArchive.cpp) diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index cd69fd0b1ecc..a7596e713de0 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -20,6 +20,9 @@ #include #ifdef __linux__ #include +#elif defined(__APPLE__) +#include +#include #endif #endif #include "SceTypes.hpp" @@ -1359,6 +1362,10 @@ static bool ReadGuest(void* destination, const void* source, std::size_t bytes) iovec local{destination, bytes}; iovec remote{const_cast(source), bytes}; return Transferred(::process_vm_readv(::getpid(), &local, 1, &remote, 1, 0), bytes, "process_vm_readv"); +#elif defined(__APPLE__) + mach_vm_size_t copied = 0; + return ::mach_vm_read_overwrite(::mach_task_self(), reinterpret_cast(source), bytes, + reinterpret_cast(destination), &copied) == KERN_SUCCESS && copied == bytes; #else std::memcpy(destination, source, bytes); return true; @@ -1378,6 +1385,9 @@ static bool WriteGuest(void* destination, const void* source, std::size_t bytes) iovec local{const_cast(source), bytes}; iovec remote{destination, bytes}; return Transferred(::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0), bytes, "process_vm_writev"); +#elif defined(__APPLE__) + return ::mach_vm_write(::mach_task_self(), reinterpret_cast(destination), + reinterpret_cast(source), static_cast(bytes)) == KERN_SUCCESS; #else std::memcpy(destination, source, bytes); return true; diff --git a/core/libs/tests/GuestSocketPair.cpp b/core/libs/tests/GuestSocketPair.cpp index bf0c712d3fea..51d909e85d56 100644 --- a/core/libs/tests/GuestSocketPair.cpp +++ b/core/libs/tests/GuestSocketPair.cpp @@ -133,6 +133,8 @@ static void CheckReceiveMessageFaults() { Iovec vector{reinterpret_cast(0x1000), sizeof(data)}; message = {nullptr, 0, &vector, 1, nullptr, 0, -1}; Require(ReceiveFaults(pair[1], &message)); + vector.base = inaccessible; + Require(Queue(pair[0], pair[1], "x") && ReceiveFaults(pair[1], &message)); vector.base = readOnly + 128; Require(Queue(pair[0], pair[1], "x") && ReceiveFaults(pair[1], &message)); message = {reinterpret_cast(0x1000), 16, &good, 1, nullptr, 0, -1}; @@ -162,7 +164,12 @@ static void CheckReceiveMessageFaults() { Require(sceKernelMprotect(memory, page * 3, 3) == 0 && sceKernelMunmap(memory, page * 3) == 0); } -int main() { +int main(int argc, char** argv) { + if (argc == 2 && std::strcmp(argv[1], "recvmsg-memory") == 0) { + CheckReceiveMessageFaults(); + return 0; + } + Require(argc == 1); int pair[2] = {-1, -1}; Require(socketpair_nid_postfix(Unix, Stream, 0, pair) == 0); Require(pair[0] >= 0 && pair[0] < DescriptorSetSize && pair[1] >= 0 && pair[1] < DescriptorSetSize && pair[0] != pair[1]); diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index a1072eec7c8d..ae843e355c8c 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -171,7 +171,7 @@ - [nmount](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) throws: mounting file systems is not supported. Prospero Themes (PPSA90210) imports it and fails to load without it; it opens its theme library without calling it, and its strings (`/dev/ssd0.system_ex`, `/system_ex`, `exfatfs`, `fstype`, `fspath`, `from`, `ignoreacl`) point to remounting the console's `/system_ex` partition to write theme files there. - [socketpair](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - the pair is the host's `AF_UNIX` pair on Linux and, on Windows, a loopback TCP connection for `SOCK_STREAM` or two connected UDP sockets for `SOCK_DGRAM`; `SOCK_SEQPACKET` throws on Windows. A datagram sent to a full receiver gets `ENOBUFS` on the console, waits or gets `EAGAIN` on Linux and is dropped on Windows, and on Windows a datagram `recv` after the peer closed reports `ECONNRESET` instead of the console's `ENOTCONN`, and a stream `send` after the peer closed reports `ECONNABORTED` instead of the console's `EPIPE`. `read` and `write` on a pair end go to the host like on every guest socket. `getpeername` on a pair end succeeds after the peer closed, where the console returns `ENOTCONN`, and `connect`, `bind` and `sendto` with an address on a pair end report an address-family error instead of the console's `EISCONN`. On Windows `poll` on a stream end reports `POLLHUP` without `POLLIN` after the peer's `shutdown(SHUT_WR)` or `close`, where the console reports `POLLIN`, with `POLLHUP` only after `close` - [UDP recv/recvfrom](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) and [sceNetRecv/sceNetRecvfrom](../../../core/libs/prx/libSceNet/Export.cpp) translate Windows WSAEMSGSIZE into the copied byte count for datagram sockets, including MSG_PEEK, and retain the sender address. This follows FreeBSD 11.4 recv(2) and Winsock's documented truncated-datagram behavior; console behavior and existing title impact are unverified. -- [recvmsg](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) reads the `msghdr` and the iovec array and writes the data, the sender address and the `msghdr` results through copies that fail with `EFAULT` when a page is not mapped with the access the copy needs: `process_vm_readv` and `process_vm_writev` on the own process on Linux, `VirtualQuery` before the copy on Windows; on macOS hosts the copies are not checked. Only the bytes copied are checked, so iovec and name lengths larger than the received data are accepted. The outputs are checked after the host receive, so a fault drops the received data. FreeBSD 11.4 also drops a datagram on a copy fault ([`soreceive_dgram`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2308-L2312), [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1758-L1774)) but leaves unread stream data queued, which is lost here. A `process_vm_readv` or `process_vm_writev` failure other than `EFAULT` throws +- [recvmsg](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) loses received stream data on an output copy fault: outputs are checked after the host receive. FreeBSD 11.4 leaves unread stream data queued, though it also drops a datagram on a copy fault ([`soreceive_dgram`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2308-L2312), [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1758-L1774)). A Linux `process_vm_readv` or `process_vm_writev` failure other than `EFAULT` throws - [MSG_DONTWAIT](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) in `send`, `sendto`, `sendmsg`, `recv`, `recvfrom` and `recvmsg` makes that one call non-blocking through the host's `MSG_DONTWAIT`, as FreeBSD 11.4's `soreceive_generic` returns `EWOULDBLOCK` for it (`sys/kern/uipc_socket.c`). Winsock has no per-call non-blocking flag, so on Windows these calls still fail with `EOPNOTSUPP` for it - [send, sendto](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - send and sendto without an address on an unconnected datagram socket map Windows `WSAENOTCONN` to FreeBSD's `EDESTADDRREQ` (39). - [read, write and vector I/O](../../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) on a guest socket use `recv` and `send` without flags; `sceKernelReadv` and `sceKernelWritev` gather or scatter through one socket operation, preserving stream short reads and datagram boundaries. The positioned `pread`, `pwrite`, `preadv` and `pwritev` forms, and their `sceKernel` forms, return `ESPIPE` for an open guest socket and `EBADF` for a closed one. `EPIPE` from a write raises the host's `SIGPIPE` on Linux unless the socket has `SO_NOSIGPIPE` set, and the guest cannot ignore it: `signal(SIGPIPE, SIG_IGN)` and `sigaction` record the disposition, as they do for every number from 1 to 128, but only the six signals with host equivalents (2, 4, 6, 8, 11 and 15) take effect on the host or are delivered, so the host `SIGPIPE` is not stopped; nothing is raised on Windows; a `write` longer than `INT_MAX` bytes is `EMSGSIZE` where the console sends it. AIO on a guest socket still goes to the host, which has no such descriptor