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
8 changes: 7 additions & 1 deletion core/libs/prx/libc/src/General.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*>(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<std::size_t>(copied) == bytes;
#elif defined(__APPLE__)
mach_vm_size_t copied = 0;
Expand Down Expand Up @@ -839,9 +841,13 @@ extern "C" bool GuestCopyOut_nid_no_patch(void* destination, const void* source,
#elif defined(__linux__)
iovec local{const_cast<void*>(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<std::size_t>(copied) == bytes;
Expand Down
2 changes: 2 additions & 0 deletions core/libs/prx/libkernel/File/src/Stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1485,8 +1485,10 @@ static int CopyIovecs(const KernelIovec* iov, int iovcnt, std::vector<KernelIove
#elif defined(__linux__)
iovec local{vectors.data(), bytes};
iovec remote{const_cast<KernelIovec*>(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<std::size_t>(copied) != bytes) return SceErrorFromErrno(GUEST_EFAULT);
#elif defined(__APPLE__)
mach_vm_size_t copied = 0;
Expand Down
13 changes: 9 additions & 4 deletions core/libs/prx/libkernel/Socket/src/Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1935,9 +1935,12 @@ static bool GuestPagesAllow(std::uintptr_t start, std::size_t bytes, DWORD allow
return GuestPrefix(reinterpret_cast<const void*>(start), bytes, allowed) == bytes;
}
#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<std::size_t>(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
Expand All @@ -1957,7 +1960,8 @@ static bool ReadGuest(void* destination, const void* source, std::size_t bytes)
#elif defined(__linux__)
iovec local{destination, bytes};
iovec remote{const_cast<void*>(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<mach_vm_address_t>(source), bytes,
Expand All @@ -1980,7 +1984,8 @@ static bool WriteGuest(void* destination, const void* source, std::size_t bytes)
#elif defined(__linux__)
iovec local{const_cast<void*>(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<mach_vm_address_t>(destination),
reinterpret_cast<vm_offset_t>(source), static_cast<mach_msg_type_number_t>(bytes)) == KERN_SUCCESS;
Expand Down
9 changes: 8 additions & 1 deletion core/libs/prx/libkernel/System/src/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ bool StoreGuestId(std::uint32_t* output) {
#elif defined(__linux__)
iovec local{const_cast<std::uint32_t*>(&zero), sizeof(zero)};
iovec remote{output, sizeof(zero)};
return ::process_vm_writev(::getpid(), &local, 1, &remote, 1, 0) == static_cast<ssize_t>(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<ssize_t>(sizeof(zero));
#elif defined(__APPLE__)
return ::mach_vm_write(::mach_task_self(), reinterpret_cast<mach_vm_address_t>(output),
reinterpret_cast<vm_offset_t>(const_cast<std::uint32_t*>(&zero)), sizeof(zero)) == KERN_SUCCESS;
Expand All @@ -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<void*>(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<std::size_t>(copied) == bytes;
#elif defined(__APPLE__)
mach_vm_size_t copied = 0;
Expand All @@ -156,8 +161,10 @@ bool WriteGuest(void* destination, const void* source, std::size_t bytes) {
#elif defined(__linux__)
iovec local{const_cast<void*>(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<std::size_t>(copied) == bytes;
#elif defined(__APPLE__)
return ::mach_vm_write(::mach_task_self(), reinterpret_cast<mach_vm_address_t>(destination),
Expand Down
2 changes: 2 additions & 0 deletions core/libs/prx/libkernel/Time/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void*>(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<std::size_t>(copied) == bytes;
#elif defined(__APPLE__)
mach_vm_size_t copied = 0;
Expand Down
21 changes: 21 additions & 0 deletions core/libs/tests/GuestOutputPointerFault.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
Expand Down Expand Up @@ -60,7 +61,27 @@ 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<void*>(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);
*__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<const KernelIovec*>(bad), 1) == KernelEfault);
Require(*__error_nid_postfix() == 12345);
}
}

int main() {
RequireFaultKeepsErrno();
alignas(8) unsigned char valid[512]{};
alignas(8) unsigned char zeroTimer[32]{};
auto* timeValid = reinterpret_cast<KernelTimeval*>(valid);
Expand Down
Loading