From dfb9b774fc5c58b509a4ee7f17c7868b8014230c Mon Sep 17 00:00:00 2001 From: Jota <139649225+jotitadev@users.noreply.github.com> Date: Fri, 30 Oct 2026 19:03:35 -0300 Subject: [PATCH 1/2] fix(libc): keep errno when a checked guest copy faults on Linux GuestCopyIn and GuestCopyOut report a fault through their return value. On Linux, process_vm_readv/process_vm_writev also left EFAULT in errno, which the guest shares, so a caller that reports the fault through a return code (or a test that checks errno is untouched) saw errno 14 on Linux only. Windows (ReadProcessMemory/VirtualQuery) and macOS (mach_vm_*) never touch errno. Restore the caller's errno on the EFAULT path so the three hosts behave the same and each caller sets the guest errno itself. --- core/libs/prx/libc/src/General.cpp | 8 +++++++- core/libs/tests/GuestOutputPointerFault.cpp | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libc/src/General.cpp b/core/libs/prx/libc/src/General.cpp index 9ed57a5fb066..704bf21bda07 100644 --- a/core/libs/prx/libc/src/General.cpp +++ b/core/libs/prx/libc/src/General.cpp @@ -91,8 +91,10 @@ bool CopyGuestBytes(void* destination, const void* source, std::size_t bytes) { #elif defined(__linux__) iovec local{destination, bytes}; iovec remote{const_cast(source), bytes}; + const int saved = errno; const auto copied = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); if (copied < 0 && errno != EFAULT) throw std::runtime_error("guest path: process_vm_readv failed with errno " + std::to_string(errno)); + errno = saved; return copied >= 0 && static_cast(copied) == bytes; #elif defined(__APPLE__) mach_vm_size_t copied = 0; @@ -839,9 +841,13 @@ extern "C" bool GuestCopyOut_nid_no_patch(void* destination, const void* source, #elif defined(__linux__) iovec local{const_cast(source), bytes}; iovec remote{destination, bytes}; + const int saved = errno; const ssize_t copied = ::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0); if (copied < 0) { - if (errno == EFAULT) return false; + if (errno == EFAULT) { + errno = saved; + return false; + } throw std::runtime_error("GuestCopyOut: process_vm_writev failed with errno " + std::to_string(errno)); } return static_cast(copied) == bytes; diff --git a/core/libs/tests/GuestOutputPointerFault.cpp b/core/libs/tests/GuestOutputPointerFault.cpp index 6877c50c331a..5f76730c8c8e 100644 --- a/core/libs/tests/GuestOutputPointerFault.cpp +++ b/core/libs/tests/GuestOutputPointerFault.cpp @@ -60,7 +60,21 @@ static void RequireNonEfaultFailureThrows() { } #endif +static void RequireFaultKeepsErrno() { + std::uint64_t value = 0; + for (const std::uintptr_t address : {std::uintptr_t{1}, ~std::uintptr_t{0xfff}}) { + void* bad = reinterpret_cast(address); + *__error_nid_postfix() = 12345; + Require(!GuestCopyOut_nid_no_patch(bad, &value, sizeof(value))); + Require(*__error_nid_postfix() == 12345); + *__error_nid_postfix() = 12345; + Require(!GuestCopyIn_nid_no_patch(&value, bad, sizeof(value))); + Require(*__error_nid_postfix() == 12345); + } +} + int main() { + RequireFaultKeepsErrno(); alignas(8) unsigned char valid[512]{}; alignas(8) unsigned char zeroTimer[32]{}; auto* timeValid = reinterpret_cast(valid); From 829f40c5235ac5d526217fbb081bf72c1af51266 Mon Sep 17 00:00:00 2001 From: Jota <139649225+jotitadev@users.noreply.github.com> Date: Sat, 31 Oct 2026 06:43:55 -0300 Subject: [PATCH 2/2] fix(libkernel): keep errno when libkernel guest copies fault on Linux libkernel has its own checked guest copy helpers that call process_vm_readv/process_vm_writev directly: ReadGuest, WriteGuest and StoreGuestId in Process.cpp, ReadGuest/WriteGuest in Udp.cpp (also exported as GuestSocketReadGuest), CopyIovecs in Stdio.cpp and ReadGuestTime in Time.cpp. On EFAULT they left errno at 14, which is the guest errno, so a caller that reports the fault through a return code (sceKernelReadv, or sceKernelGetAppInfo in #13798) still changed the guest's errno. Windows and macOS do not touch errno there. Apply the same rule as GuestCopyIn/GuestCopyOut: save errno before the call and restore it when the call fails with EFAULT. Failures other than EFAULT behave as before. --- core/libs/prx/libkernel/File/src/Stdio.cpp | 2 ++ core/libs/prx/libkernel/Socket/src/Udp.cpp | 13 +++++++++---- core/libs/prx/libkernel/System/src/Process.cpp | 9 ++++++++- core/libs/prx/libkernel/Time/Time.cpp | 2 ++ core/libs/tests/GuestOutputPointerFault.cpp | 7 +++++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index a0d6f8055096..0acc7516496f 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -1380,8 +1380,10 @@ static int CopyIovecs(const KernelIovec* iov, int iovcnt, std::vector(iov), bytes}; + const int saved = errno; const auto copied = ::process_vm_readv(::getpid(), &local, 1, &remote, 1, 0); if (copied < 0 && errno != EFAULT) throw std::runtime_error("iovec: process_vm_readv failed with errno " + std::to_string(errno)); + errno = saved; if (copied < 0 || static_cast(copied) != bytes) return SceErrorFromErrno(GUEST_EFAULT); #elif defined(__APPLE__) mach_vm_size_t copied = 0; diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 71a3ada8b07b..c0aa8757577d 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -1666,9 +1666,12 @@ static bool GuestPagesAllow(std::uintptr_t start, std::size_t bytes, DWORD allow return true; } #elif defined(__linux__) -static bool Transferred(ssize_t copied, std::size_t bytes, const char* call) { +static bool Transferred(ssize_t copied, std::size_t bytes, const char* call, int saved) { if (copied >= 0) return static_cast(copied) == bytes; - if (errno == EFAULT) return false; + if (errno == EFAULT) { + errno = saved; + return false; + } throw std::runtime_error(std::string("recvmsg: ") + call + " failed with errno " + std::to_string(errno)); } #endif @@ -1688,7 +1691,8 @@ static bool ReadGuest(void* destination, const void* source, std::size_t bytes) #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"); + const int saved = errno; + return Transferred(::process_vm_readv(::getpid(), &local, 1, &remote, 1, 0), bytes, "process_vm_readv", saved); #elif defined(__APPLE__) mach_vm_size_t copied = 0; return ::mach_vm_read_overwrite(::mach_task_self(), reinterpret_cast(source), bytes, @@ -1711,7 +1715,8 @@ static bool WriteGuest(void* destination, const void* source, std::size_t bytes) #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"); + const int saved = errno; + return Transferred(::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0), bytes, "process_vm_writev", saved); #elif defined(__APPLE__) return ::mach_vm_write(::mach_task_self(), reinterpret_cast(destination), reinterpret_cast(source), static_cast(bytes)) == KERN_SUCCESS; diff --git a/core/libs/prx/libkernel/System/src/Process.cpp b/core/libs/prx/libkernel/System/src/Process.cpp index ec1de2cc9d0f..4525b19490a4 100644 --- a/core/libs/prx/libkernel/System/src/Process.cpp +++ b/core/libs/prx/libkernel/System/src/Process.cpp @@ -108,7 +108,10 @@ bool StoreGuestId(std::uint32_t* output) { #elif defined(__linux__) iovec local{const_cast(&zero), sizeof(zero)}; iovec remote{output, sizeof(zero)}; - return ::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0) == static_cast(sizeof(zero)); + const int saved = errno; + const auto copied = ::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0); + if (copied < 0 && errno == EFAULT) errno = saved; + return copied == static_cast(sizeof(zero)); #elif defined(__APPLE__) return ::mach_vm_write(::mach_task_self(), reinterpret_cast(output), reinterpret_cast(const_cast(&zero)), sizeof(zero)) == KERN_SUCCESS; @@ -133,8 +136,10 @@ bool ReadGuest(void* destination, const void* source, std::size_t bytes) { #elif defined(__linux__) iovec local{destination, bytes}; iovec remote{const_cast(source), bytes}; + const int saved = errno; const auto copied = ::process_vm_readv(::getpid(), &local, 1, &remote, 1, 0); if (copied < 0 && errno != EFAULT) throw std::runtime_error("credentials: process_vm_readv failed with errno " + std::to_string(errno)); + errno = saved; return copied >= 0 && static_cast(copied) == bytes; #elif defined(__APPLE__) mach_vm_size_t copied = 0; @@ -156,8 +161,10 @@ bool WriteGuest(void* destination, const void* source, std::size_t bytes) { #elif defined(__linux__) iovec local{const_cast(source), bytes}; iovec remote{destination, bytes}; + const int saved = errno; const auto copied = ::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0); if (copied < 0 && errno != EFAULT) throw std::runtime_error("credentials: process_vm_writev failed with errno " + std::to_string(errno)); + errno = saved; return copied >= 0 && static_cast(copied) == bytes; #elif defined(__APPLE__) return ::mach_vm_write(::mach_task_self(), reinterpret_cast(destination), diff --git a/core/libs/prx/libkernel/Time/Time.cpp b/core/libs/prx/libkernel/Time/Time.cpp index fefda9aeb85c..997dbbe9fc6f 100644 --- a/core/libs/prx/libkernel/Time/Time.cpp +++ b/core/libs/prx/libkernel/Time/Time.cpp @@ -65,8 +65,10 @@ static bool ReadGuestTime(void* destination, const void* source, std::size_t byt #elif defined(__linux__) iovec local{destination, bytes}; iovec remote{const_cast(source), bytes}; + const int saved = errno; const auto copied = ::process_vm_readv(::getpid(), &local, 1, &remote, 1, 0); if (copied < 0 && errno != EFAULT) throw std::runtime_error("time: process_vm_readv failed with errno " + std::to_string(errno)); + errno = saved; return copied >= 0 && static_cast(copied) == bytes; #elif defined(__APPLE__) mach_vm_size_t copied = 0; diff --git a/core/libs/tests/GuestOutputPointerFault.cpp b/core/libs/tests/GuestOutputPointerFault.cpp index 5f76730c8c8e..a39d110190af 100644 --- a/core/libs/tests/GuestOutputPointerFault.cpp +++ b/core/libs/tests/GuestOutputPointerFault.cpp @@ -1,6 +1,7 @@ #include "prx/libc/include/Api.hpp" #include "prx/libc/include/General.hpp" #include "prx/libkernel/Time/include/Time.hpp" +#include "prx/libkernel/Socket/include/SocketRuntime.hpp" #include "prx/libkernel/include/Api.hpp" #include @@ -70,6 +71,12 @@ static void RequireFaultKeepsErrno() { *__error_nid_postfix() = 12345; Require(!GuestCopyIn_nid_no_patch(&value, bad, sizeof(value))); Require(*__error_nid_postfix() == 12345); + *__error_nid_postfix() = 12345; + Require(!GuestSocketReadGuest_nid_no_patch(&value, bad, sizeof(value))); + Require(*__error_nid_postfix() == 12345); + *__error_nid_postfix() = 12345; + Require(sceKernelReadv(0, static_cast(bad), 1) == KernelEfault); + Require(*__error_nid_postfix() == 12345); } }