libSceNet: sceNetSendmsg crashes the host on unreadable guest payload instead of returning EFAULT #11467

Closed
opened 2026-10-26 15:13:27 +00:00 by lgqdaxia · 0 comments
lgqdaxia commented 2026-10-26 15:13:27 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by lgqdaxia on 2026-10-26T15:13:27Z.


What happens

In a source-linked Windows test, calling libSceNet's sceNetSendmsg with an iovec payload that crosses from a readable guest page into an unreadable guest page terminates the host test process with access violation 0xC0000005, instead of returning SCE Net EFAULT.

Title, test or build step

Local source-based test fixture guest_net_sendmsg_boundary_tests (the relevant call sequence is included below). No retail title or console module was used.

AnyPS5 commit

Built and tested at 263fbcd2d3ab888292062a5dbd441387309b66e0, with no production-source changes.

I also checked current main at 7903201bb426f7f1e8565e98cc3df299d849edc7. core/libs/prx/libSceNet/Export.cpp is identical between those commits, including the affected path. I did not rerun the executable against the newer commit.

System

Windows 11 x64 10.0.26200.9711, Intel Core i7-12700H, CMake Release, Ninja, MinGW-w64 GCC 15.2.0 r7, BUILD_TESTING=ON. GPU/driver: not involved in this CPU/network test.

Steps to reproduce

  1. Initialize libSceNet, create valid sender and receiver UDP sockets, bind the receiver to loopback, and use its address as the destination.
  2. Allocate and map three 0x4000-byte guest pages.
  3. Put a valid NetMsghdr and one-entry NetIovec in readable guest memory.
  4. Set the iovec base to eight bytes before the end of the second page and its length to 16 bytes.
  5. Make the third page unreadable with sceKernelMprotect(page3, 0x4000, 0).
  6. Call sceNetSendmsg(sender, message, 0).

This is a local fixture; the target below is not present in upstream main. Save the complete source below as core/libs/tests/GuestNetSendmsgBoundary.cpp, then add the following lines inside the existing Windows test block in core/libs/prx/libSceNet/CMakeLists.txt:

add_test_executable(guest_net_sendmsg_boundary_tests ${LIBS_INCLUDE_DIR}/tests/GuestNetSendmsgBoundary.cpp)
target_include_directories(guest_net_sendmsg_boundary_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_net_sendmsg_boundary_tests PRIVATE libSceNet libkernel libc)
configure_windows_unwind(guest_net_sendmsg_boundary_tests)
Complete GuestNetSendmsgBoundary.cpp
#include "prx/libkernel/include/Api.hpp"
#include "prx/libSceNet/include/Net.hpp"
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <source_location>

static void Require(bool condition, std::source_location location = std::source_location::current()) {
    if (!condition) {
        std::fprintf(stderr, "check failed at line %u\n", location.line());
        std::exit(1);
    }
}

int main(int argc, char** argv) {
    constexpr std::size_t page = 0x4000;
    Require(argc == 2);
    const bool dataCrash = std::strcmp(argv[1], "data-crash") == 0;
    const bool iovCrash = std::strcmp(argv[1], "iov-crash") == 0;
    const bool messageCrash = std::strcmp(argv[1], "message-crash") == 0;
    const bool sendtoControl = std::strcmp(argv[1], "sendto-control") == 0;
    const bool accessible = std::strcmp(argv[1], "accessible") == 0;
    Require(dataCrash || iovCrash || messageCrash || sendtoControl || accessible);

    Require(sceNetInit_nid_postfix() == 0);
    const int receiver = sceNetSocket("sendmsg-boundary-receiver", 2, 2, 17);
    const int sender = sceNetSocket("sendmsg-boundary-sender", 2, 2, 17);
    Require(receiver >= 0 && sender >= 0);
    std::array<unsigned char, 16> destination{16, 2, 0, 0, 127, 0, 0, 1};
    Require(sceNetBind_nid_postfix(receiver, destination.data(), destination.size()) == 0);
    std::uint32_t destinationSize = destination.size();
    Require(sceNetGetsockname(receiver, destination.data(), &destinationSize) == 0);
    Require(destinationSize == destination.size());

    std::int64_t physical = 0;
    Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page * 3, 0, 0, &physical) == 0);
    void* mapping = nullptr;
    Require(sceKernelMapDirectMemory(&mapping, page * 3, 3, 0, physical, 0) == 0);
    auto* bytes = static_cast<unsigned char*>(mapping);
    std::memset(bytes, 0x5a, page * 3);

    auto* message = reinterpret_cast<NetMsghdr*>(bytes + 0x100);
    auto* vector = reinterpret_cast<NetIovec*>(bytes + 0x200);
    auto* boundary = bytes + page * 2 - 8;
    *vector = NetIovec{boundary, accessible ? 8u : 16u};
    *message = NetMsghdr{destination.data(), destination.size(), vector, 1, nullptr, 0, 0};

    if (iovCrash) {
        vector = reinterpret_cast<NetIovec*>(bytes + page - sizeof(void*));
        std::memcpy(vector, &boundary, sizeof(boundary));
        const std::uint64_t length = 8;
        std::memcpy(bytes + page, &length, sizeof(length));
        message->iov = vector;
        Require(sceKernelMprotect(bytes + page, page, 0) == 0);
        return static_cast<int>(sceNetSendmsg(sender, message, 0));
    }

    if (messageCrash) {
        auto* crossing = reinterpret_cast<NetMsghdr*>(bytes + page - sizeof(void*));
        std::memcpy(crossing, message, sizeof(*message));
        Require(sceKernelMprotect(bytes + page, page, 0) == 0);
        return static_cast<int>(sceNetSendmsg(sender, crossing, 0));
    }

    Require(sceKernelMprotect(bytes + page * 2, page, 0) == 0);
    if (sendtoControl) {
        *sceNetErrnoLoc() = 123;
        const auto result = sceNetSendto(sender, boundary, 16, 0, destination.data(), destination.size());
        Require(result == static_cast<int>(0x8041010eu));
        Require(*sceNetErrnoLoc() == 14);
        std::puts("sendto-control: EFAULT");
        return 0;
    }

    const auto result = sceNetSendmsg(sender, message, 0);
    if (dataCrash) {
        std::fprintf(stderr, "unexpected sendmsg return: %lld errno=%d\n",
            static_cast<long long>(result), *sceNetErrnoLoc());
        return 2;
    }
    Require(result == 8);
    std::puts("accessible: sent 8 bytes");
    return 0;
}

I configured, built, and ran it with the following PowerShell commands. X: was a local drive mapping for the AnyPS5 workspace:

$toolchain = 'X:\AnyPS5\build-10753\toolchain\mingw64\bin'
$env:PATH = "$toolchain;$env:PATH"

cmake -S . -B build-guest-sendmsg -G Ninja `
  -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON `
  -DCMAKE_C_COMPILER="$toolchain\gcc.exe" `
  -DCMAKE_CXX_COMPILER="$toolchain\g++.exe" `
  -DCMAKE_MAKE_PROGRAM="$toolchain\ninja.exe"
cmake --build build-guest-sendmsg --target guest_net_sendmsg_boundary_tests

$env:PATH = "$PWD\build-guest-sendmsg\core\libs\libs\unpatched;$toolchain;$env:PATH"
./build-guest-sendmsg/tests/guest_net_sendmsg_boundary_tests.exe accessible
./build-guest-sendmsg/tests/guest_net_sendmsg_boundary_tests.exe sendto-control
./build-guest-sendmsg/tests/guest_net_sendmsg_boundary_tests.exe data-crash

accessible uses the same setup with an 8-byte iovec that stays within the readable page. sendto-control passes the same unreadable 16-byte range to sceNetSendto and checks for 0x8041010e / errno 14 (EFAULT).

Log

accessible: sent 8 bytes
sendto-control: EFAULT

FATAL: unhandled exception 0xc0000005
  rip ... ucrtbase.dll
  read of 0x0000000200008000
  ...
  stack return address candidates:
    ... libSceNet.prx+0x1013d

Ten fresh runs of each mode produced:

accessible:     10/10 exit 0, sent 8 bytes
sendto-control: 10/10 exit 0, EFAULT
data-crash:     10/10 exit 0xC0000005

What was checked

  • The message header, iovec table, sockets, and destination are valid and readable. Only the last eight bytes of the requested 16-byte payload are intentionally unreadable. This is an error argument that should be reported to the guest, not a fully readable payload.
  • There is no race or private test hook in the trigger.
  • The positive control confirms that sceNetSendmsg succeeds when the whole payload is readable.
  • The sceNetSendto control confirms that the same unreadable range can be translated to guest EFAULT without terminating the process.
  • Windows was tested; Linux and macOS were not.
  • No real title has been shown to reach this invalid-buffer state.

The direct cause is the unchecked host std::memcpy in sceNetSendmsg. The preceding message_length walk also directly dereferences guest-owned header and iovec memory.

Related work: #8462 / #8463 cover the analogous libkernel sendmsg_nid_postfix implementation, not this separate libSceNet path; #8463 does not change core/libs/prx/libSceNet/Export.cpp. PR #5930 added protected guest writes for sceNetRecvmsg, but does not validate sceNetSendmsg inputs.

AI-assisted: Codex

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6042520521 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/11467). Originally posted by [lgqdaxia](https://github.com/lgqdaxia) on 2026-10-26T15:13:27Z. --- ### What happens In a source-linked Windows test, calling `libSceNet`'s `sceNetSendmsg` with an iovec payload that crosses from a readable guest page into an unreadable guest page terminates the host test process with access violation `0xC0000005`, instead of returning SCE Net `EFAULT`. ### Title, test or build step Local source-based test fixture `guest_net_sendmsg_boundary_tests` (the relevant call sequence is included below). No retail title or console module was used. ### AnyPS5 commit Built and tested at `263fbcd2d3ab888292062a5dbd441387309b66e0`, with no production-source changes. I also checked current `main` at `7903201bb426f7f1e8565e98cc3df299d849edc7`. `core/libs/prx/libSceNet/Export.cpp` is identical between those commits, including the affected path. I did not rerun the executable against the newer commit. ### System Windows 11 x64 `10.0.26200.9711`, Intel Core i7-12700H, CMake Release, Ninja, MinGW-w64 GCC 15.2.0 r7, `BUILD_TESTING=ON`. GPU/driver: not involved in this CPU/network test. ### Steps to reproduce 1. Initialize `libSceNet`, create valid sender and receiver UDP sockets, bind the receiver to loopback, and use its address as the destination. 2. Allocate and map three 0x4000-byte guest pages. 3. Put a valid `NetMsghdr` and one-entry `NetIovec` in readable guest memory. 4. Set the iovec base to eight bytes before the end of the second page and its length to 16 bytes. 5. Make the third page unreadable with `sceKernelMprotect(page3, 0x4000, 0)`. 6. Call `sceNetSendmsg(sender, message, 0)`. This is a local fixture; the target below is not present in upstream `main`. Save the complete source below as `core/libs/tests/GuestNetSendmsgBoundary.cpp`, then add the following lines inside the existing Windows test block in `core/libs/prx/libSceNet/CMakeLists.txt`: ```cmake add_test_executable(guest_net_sendmsg_boundary_tests ${LIBS_INCLUDE_DIR}/tests/GuestNetSendmsgBoundary.cpp) target_include_directories(guest_net_sendmsg_boundary_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_net_sendmsg_boundary_tests PRIVATE libSceNet libkernel libc) configure_windows_unwind(guest_net_sendmsg_boundary_tests) ``` <details> <summary>Complete GuestNetSendmsgBoundary.cpp</summary> ```cpp #include "prx/libkernel/include/Api.hpp" #include "prx/libSceNet/include/Net.hpp" #include <array> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <source_location> static void Require(bool condition, std::source_location location = std::source_location::current()) { if (!condition) { std::fprintf(stderr, "check failed at line %u\n", location.line()); std::exit(1); } } int main(int argc, char** argv) { constexpr std::size_t page = 0x4000; Require(argc == 2); const bool dataCrash = std::strcmp(argv[1], "data-crash") == 0; const bool iovCrash = std::strcmp(argv[1], "iov-crash") == 0; const bool messageCrash = std::strcmp(argv[1], "message-crash") == 0; const bool sendtoControl = std::strcmp(argv[1], "sendto-control") == 0; const bool accessible = std::strcmp(argv[1], "accessible") == 0; Require(dataCrash || iovCrash || messageCrash || sendtoControl || accessible); Require(sceNetInit_nid_postfix() == 0); const int receiver = sceNetSocket("sendmsg-boundary-receiver", 2, 2, 17); const int sender = sceNetSocket("sendmsg-boundary-sender", 2, 2, 17); Require(receiver >= 0 && sender >= 0); std::array<unsigned char, 16> destination{16, 2, 0, 0, 127, 0, 0, 1}; Require(sceNetBind_nid_postfix(receiver, destination.data(), destination.size()) == 0); std::uint32_t destinationSize = destination.size(); Require(sceNetGetsockname(receiver, destination.data(), &destinationSize) == 0); Require(destinationSize == destination.size()); std::int64_t physical = 0; Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page * 3, 0, 0, &physical) == 0); void* mapping = nullptr; Require(sceKernelMapDirectMemory(&mapping, page * 3, 3, 0, physical, 0) == 0); auto* bytes = static_cast<unsigned char*>(mapping); std::memset(bytes, 0x5a, page * 3); auto* message = reinterpret_cast<NetMsghdr*>(bytes + 0x100); auto* vector = reinterpret_cast<NetIovec*>(bytes + 0x200); auto* boundary = bytes + page * 2 - 8; *vector = NetIovec{boundary, accessible ? 8u : 16u}; *message = NetMsghdr{destination.data(), destination.size(), vector, 1, nullptr, 0, 0}; if (iovCrash) { vector = reinterpret_cast<NetIovec*>(bytes + page - sizeof(void*)); std::memcpy(vector, &boundary, sizeof(boundary)); const std::uint64_t length = 8; std::memcpy(bytes + page, &length, sizeof(length)); message->iov = vector; Require(sceKernelMprotect(bytes + page, page, 0) == 0); return static_cast<int>(sceNetSendmsg(sender, message, 0)); } if (messageCrash) { auto* crossing = reinterpret_cast<NetMsghdr*>(bytes + page - sizeof(void*)); std::memcpy(crossing, message, sizeof(*message)); Require(sceKernelMprotect(bytes + page, page, 0) == 0); return static_cast<int>(sceNetSendmsg(sender, crossing, 0)); } Require(sceKernelMprotect(bytes + page * 2, page, 0) == 0); if (sendtoControl) { *sceNetErrnoLoc() = 123; const auto result = sceNetSendto(sender, boundary, 16, 0, destination.data(), destination.size()); Require(result == static_cast<int>(0x8041010eu)); Require(*sceNetErrnoLoc() == 14); std::puts("sendto-control: EFAULT"); return 0; } const auto result = sceNetSendmsg(sender, message, 0); if (dataCrash) { std::fprintf(stderr, "unexpected sendmsg return: %lld errno=%d\n", static_cast<long long>(result), *sceNetErrnoLoc()); return 2; } Require(result == 8); std::puts("accessible: sent 8 bytes"); return 0; } ``` </details> I configured, built, and ran it with the following PowerShell commands. `X:` was a local drive mapping for the AnyPS5 workspace: ```powershell $toolchain = 'X:\AnyPS5\build-10753\toolchain\mingw64\bin' $env:PATH = "$toolchain;$env:PATH" cmake -S . -B build-guest-sendmsg -G Ninja ` -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON ` -DCMAKE_C_COMPILER="$toolchain\gcc.exe" ` -DCMAKE_CXX_COMPILER="$toolchain\g++.exe" ` -DCMAKE_MAKE_PROGRAM="$toolchain\ninja.exe" cmake --build build-guest-sendmsg --target guest_net_sendmsg_boundary_tests $env:PATH = "$PWD\build-guest-sendmsg\core\libs\libs\unpatched;$toolchain;$env:PATH" ./build-guest-sendmsg/tests/guest_net_sendmsg_boundary_tests.exe accessible ./build-guest-sendmsg/tests/guest_net_sendmsg_boundary_tests.exe sendto-control ./build-guest-sendmsg/tests/guest_net_sendmsg_boundary_tests.exe data-crash ``` `accessible` uses the same setup with an 8-byte iovec that stays within the readable page. `sendto-control` passes the same unreadable 16-byte range to `sceNetSendto` and checks for `0x8041010e` / errno 14 (`EFAULT`). ### Log ```text accessible: sent 8 bytes sendto-control: EFAULT FATAL: unhandled exception 0xc0000005 rip ... ucrtbase.dll read of 0x0000000200008000 ... stack return address candidates: ... libSceNet.prx+0x1013d ``` Ten fresh runs of each mode produced: ```text accessible: 10/10 exit 0, sent 8 bytes sendto-control: 10/10 exit 0, EFAULT data-crash: 10/10 exit 0xC0000005 ``` ### What was checked - The message header, iovec table, sockets, and destination are valid and readable. Only the last eight bytes of the requested 16-byte payload are intentionally unreadable. This is an error argument that should be reported to the guest, not a fully readable payload. - There is no race or private test hook in the trigger. - The positive control confirms that `sceNetSendmsg` succeeds when the whole payload is readable. - The `sceNetSendto` control confirms that the same unreadable range can be translated to guest `EFAULT` without terminating the process. - Windows was tested; Linux and macOS were not. - No real title has been shown to reach this invalid-buffer state. The direct cause is the unchecked host `std::memcpy` in [`sceNetSendmsg`](https://github.com/boykopovar/AnyPS5/blob/7903201bb426f7f1e8565e98cc3df299d849edc7/core/libs/prx/libSceNet/Export.cpp#L1333-L1348). The preceding [`message_length`](https://github.com/boykopovar/AnyPS5/blob/7903201bb426f7f1e8565e98cc3df299d849edc7/core/libs/prx/libSceNet/Export.cpp#L490-L500) walk also directly dereferences guest-owned header and iovec memory. Related work: #8462 / #8463 cover the analogous `libkernel` `sendmsg_nid_postfix` implementation, not this separate `libSceNet` path; #8463 does not change `core/libs/prx/libSceNet/Export.cpp`. PR #5930 added protected guest writes for `sceNetRecvmsg`, but does not validate `sceNetSendmsg` inputs. AI-assisted: Codex
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#11467
No description provided.