libSceNpCommon: terminating a callout context from its own callback crashes the host after teardown returns #11567

Closed
opened 2026-10-26 20:31:36 +00:00 by lgqdaxia · 0 comments
lgqdaxia commented 2026-10-26 20:31:36 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by lgqdaxia on 2026-10-26T20:31:36Z.


What happens

In a source-linked Windows test, a libSceNpCommon callout handler invokes sceNpCalloutTermCtx on its own context. The function returns after its self-join reports EDEADLK, and it destroys the context's condition and mutex. If the caller then releases the context mapping, the handler returns and the same worker unconditionally reads the now-unmapped mutex field, terminating the AnyPS5 host process with access violation 0xC0000005.

The public SDK contract for terminating a callout context from its own handler is unknown. This report does not claim that the console permits that sequence. The behavior demonstrated here is that AnyPS5 completes and returns from teardown while its worker still uses the caller-owned context.

Title, test or build step

An opt-in mode added to the existing guest_np_common_runtime_tests source-linked fixture. The exact test-only patch is included below. No retail title or console module was used.

AnyPS5 commit

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

I also checked current main at e61a7c7e2433dfccab7917a68065882deee96aa5. The relevant production files are byte-for-byte identical between the tested commit and current main: libSceNpCommon/Runtime.cpp, Export.cpp, include/Api.hpp, and libkernel's Pthread/src/Thread.cpp and Mutex.cpp. I did not rerun the executable against the newer commit.

System

Windows 11 Pro x64, Windows NT 10.0.26200.0, Intel Core i7-12700H, CMake Release, Ninja, MinGW-w64 GCC 15.2.0, BUILD_TESTING=ON. GPU/driver: not involved in this CPU/thread-lifecycle test.

Steps to reproduce

  1. Apply the test-only patch below to core/libs/tests/GuestNpCommonRuntime.cpp.
  2. Configure the project with BUILD_TESTING=ON using the normal supported Windows toolchain.
  3. Build guest_np_common_runtime_tests.
  4. Put the build's core/libs/libs/unpatched directory first on PATH.
  5. Run a fresh process with APS5_NP_CALLOUT_SELF_TERM_UAF=control; it should complete normally.
  6. Run a fresh process with APS5_NP_CALLOUT_SELF_TERM_UAF=positive; on the tested production code it prints that self-termination returned and unmapping succeeded, then terminates with 0xC0000005.
Test-only patch
diff --git a/core/libs/tests/GuestNpCommonRuntime.cpp b/core/libs/tests/GuestNpCommonRuntime.cpp
index 1747776629..4d503b6e2e 100644
--- a/core/libs/tests/GuestNpCommonRuntime.cpp
+++ b/core/libs/tests/GuestNpCommonRuntime.cpp
@@ -1,4 +1,5 @@
 #include "prx/libSceNpCommon/include/Api.hpp"
+#include "prx/libkernel/include/Api.hpp"
 #include "SceTypes.hpp"
 
 #include <atomic>
@@ -163,6 +164,65 @@ struct CalloutRecord {
 
 CalloutRecord g_callouts;
 
+constexpr std::size_t CalloutContextMappingSize = 0x4000;
+
+struct SelfTermCalloutState {
+    NpCalloutContext* context {};
+    std::atomic<int> callbackReached {0};
+};
+
+SelfTermCalloutState g_selfTermState;
+NpCallout g_selfTermCallout;
+
+NpCalloutContext* MapCalloutContext() {
+    void* mapping = nullptr;
+    Require(sceKernelMmap(nullptr, CalloutContextMappingSize, 3, 0x1002, -1, 0, &mapping) == 0 && mapping != nullptr,
+        "the callout context mapping is created");
+    std::memset(mapping, 0, CalloutContextMappingSize);
+    return static_cast<NpCalloutContext*>(mapping);
+}
+
+void APS5_VABI TerminateAndUnmapFromCallout(void*) {
+    std::puts("positive: callback entered");
+    std::fflush(stdout);
+    sceNpCalloutTermCtx(g_selfTermState.context);
+    std::puts("positive: self termination returned");
+    std::fflush(stdout);
+    const int result = sceKernelMunmap(reinterpret_cast<std::uint64_t>(g_selfTermState.context), CalloutContextMappingSize);
+    std::printf("positive: context unmapped (%d)\n", result);
+    std::fflush(stdout);
+}
+
+void APS5_VABI MarkCalloutReached(void*) {
+    g_selfTermState.callbackReached.store(1, std::memory_order_release);
+}
+
+int RunSelfTermCalloutRepro(bool positive) {
+    g_selfTermState.context = MapCalloutContext();
+    g_selfTermState.callbackReached.store(0, std::memory_order_relaxed);
+    g_selfTermCallout = {};
+    Require(sceNpCalloutInitCtx(g_selfTermState.context, "np-self-term", 0x4000, 0, 0) == 0,
+        "the mapped callout context starts");
+    const auto handler = positive ? TerminateAndUnmapFromCallout : MarkCalloutReached;
+    Require(sceNpCalloutStartOnCtx(g_selfTermState.context, &g_selfTermCallout, 0, handler, nullptr) == 0,
+        "the lifecycle callout is queued");
+
+    if (positive) {
+        std::this_thread::sleep_for(std::chrono::seconds(2));
+        std::puts("positive: unexpectedly survived worker access after unmap");
+        return 91;
+    }
+
+    for (int i = 0; i < 2000 && g_selfTermState.callbackReached.load(std::memory_order_acquire) == 0; ++i)
+        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+    Require(g_selfTermState.callbackReached.load(std::memory_order_acquire) == 1, "the control callback completes");
+    sceNpCalloutTermCtx(g_selfTermState.context);
+    Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(g_selfTermState.context), CalloutContextMappingSize) == 0,
+        "the joined control context is unmapped");
+    std::puts("control: external termination and unmap completed");
+    return 0;
+}
+
 void APS5_VABI RecordFirst(void*) {
     g_callouts.first = ++g_callouts.order;
 }
@@ -415,6 +475,10 @@ void TestHexAndCopies() {
 }
 
 int main() {
+    if (const char* mode = std::getenv("APS5_NP_CALLOUT_SELF_TERM_UAF")) {
+        if (std::strcmp(mode, "positive") == 0) return RunSelfTermCalloutRepro(true);
+        if (std::strcmp(mode, "control") == 0) return RunSelfTermCalloutRepro(false);
+    }
     TestAtomics();
     TestSemaphoresAndFlags();
     TestSharedEventFlags();

Example PowerShell commands for an already configured build tree:

cmake --build build --target guest_np_common_runtime_tests

$env:PATH = "$PWD\build\core\libs\libs\unpatched;$env:PATH"
$env:APS5_NP_CALLOUT_SELF_TERM_UAF = 'control'
& .\build\tests\guest_np_common_runtime_tests.exe

$env:APS5_NP_CALLOUT_SELF_TERM_UAF = 'positive'
& .\build\tests\guest_np_common_runtime_tests.exe

Log

The positive process reaches both post-teardown messages before the worker faults:

positive: callback entered
positive: self termination returned
positive: context unmapped (0)

FATAL: unhandled exception 0xc0000005 on thread ... 'np-self-term'
  rip ... libkernel.prx+0x6b331
  read of 0x0000000200000008
  ...
  ... libSceNpCommon.prx+0x1f47

The external-termination control completes:

control: external termination and unmap completed

Twenty fresh processes produced:

positive: 10/10 exit 0xC0000005 after TermCtx returned and unmap returned 0
control:  10/10 exit 0, no fatal report
timeouts: 0/20

What was checked

  • The context is a valid zeroed 16 KiB guest mapping initialized through sceNpCalloutInitCtx; the callout is scheduled through sceNpCalloutStartOnCtx with zero delay.
  • The negative control changes only teardown ownership: the handler marks completion, then the external thread calls sceNpCalloutTermCtx and releases the same kind of context mapping.
  • The ordinary guest_np_common_runtime CTest still passes (1/1).
  • The executable, libSceNpCommon.prx, and libkernel.prx hashes were recorded before and after the 20-process matrix and did not change. The executable directory contained no competing copies of those PRX files.
  • A temporary two-part causal control handled self-termination separately and avoided touching the context after that callback. It survived the same post-unmap interval in 3/3 logs with no fatal report. This was an experimental control, not a complete proposed fix.
  • Windows was tested; Linux and macOS were not. No real title has been shown to use callback-owned termination.
  • The observed consequence is host-process termination. No data disclosure, controllable host write, cross-process effect, or sandbox escape was demonstrated.
  • The public SDK contract for callback-owned termination remains unknown.

The code path is visible in current main: CalloutThread invokes the handler without the context mutex and then unconditionally locks context->mutex after the handler returns. sceNpCalloutTermCtx ignores the join result and completes teardown. The underlying self-join path returns EDEADLK.

Related work, but not duplicates: #9063 introduced the NpCommon callout worker; #4052 / #4063 cover libkernel's self-join return value; and #5944 fixes an analogous AvPlayer close-from-callback lifecycle, in a different module and implementation.

AI-assisted: Codex

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6047649325 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/11567). Originally posted by [lgqdaxia](https://github.com/lgqdaxia) on 2026-10-26T20:31:36Z. --- ### What happens In a source-linked Windows test, a `libSceNpCommon` callout handler invokes `sceNpCalloutTermCtx` on its own context. The function returns after its self-join reports `EDEADLK`, and it destroys the context's condition and mutex. If the caller then releases the context mapping, the handler returns and the same worker unconditionally reads the now-unmapped mutex field, terminating the AnyPS5 host process with access violation `0xC0000005`. The public SDK contract for terminating a callout context from its own handler is unknown. This report does not claim that the console permits that sequence. The behavior demonstrated here is that AnyPS5 completes and returns from teardown while its worker still uses the caller-owned context. ### Title, test or build step An opt-in mode added to the existing `guest_np_common_runtime_tests` source-linked fixture. The exact test-only patch is included below. No retail title or console module was used. ### AnyPS5 commit Built and tested at `e0b13f2990f31a949a6da21d81a7789ebdfeaa26`, with no production-source changes. I also checked current `main` at `e61a7c7e2433dfccab7917a68065882deee96aa5`. The relevant production files are byte-for-byte identical between the tested commit and current `main`: `libSceNpCommon/Runtime.cpp`, `Export.cpp`, `include/Api.hpp`, and libkernel's `Pthread/src/Thread.cpp` and `Mutex.cpp`. I did not rerun the executable against the newer commit. ### System Windows 11 Pro x64, Windows NT `10.0.26200.0`, Intel Core i7-12700H, CMake Release, Ninja, MinGW-w64 GCC 15.2.0, `BUILD_TESTING=ON`. GPU/driver: not involved in this CPU/thread-lifecycle test. ### Steps to reproduce 1. Apply the test-only patch below to `core/libs/tests/GuestNpCommonRuntime.cpp`. 2. Configure the project with `BUILD_TESTING=ON` using the normal supported Windows toolchain. 3. Build `guest_np_common_runtime_tests`. 4. Put the build's `core/libs/libs/unpatched` directory first on `PATH`. 5. Run a fresh process with `APS5_NP_CALLOUT_SELF_TERM_UAF=control`; it should complete normally. 6. Run a fresh process with `APS5_NP_CALLOUT_SELF_TERM_UAF=positive`; on the tested production code it prints that self-termination returned and unmapping succeeded, then terminates with `0xC0000005`. <details> <summary>Test-only patch</summary> ```diff diff --git a/core/libs/tests/GuestNpCommonRuntime.cpp b/core/libs/tests/GuestNpCommonRuntime.cpp index 1747776629..4d503b6e2e 100644 --- a/core/libs/tests/GuestNpCommonRuntime.cpp +++ b/core/libs/tests/GuestNpCommonRuntime.cpp @@ -1,4 +1,5 @@ #include "prx/libSceNpCommon/include/Api.hpp" +#include "prx/libkernel/include/Api.hpp" #include "SceTypes.hpp" #include <atomic> @@ -163,6 +164,65 @@ struct CalloutRecord { CalloutRecord g_callouts; +constexpr std::size_t CalloutContextMappingSize = 0x4000; + +struct SelfTermCalloutState { + NpCalloutContext* context {}; + std::atomic<int> callbackReached {0}; +}; + +SelfTermCalloutState g_selfTermState; +NpCallout g_selfTermCallout; + +NpCalloutContext* MapCalloutContext() { + void* mapping = nullptr; + Require(sceKernelMmap(nullptr, CalloutContextMappingSize, 3, 0x1002, -1, 0, &mapping) == 0 && mapping != nullptr, + "the callout context mapping is created"); + std::memset(mapping, 0, CalloutContextMappingSize); + return static_cast<NpCalloutContext*>(mapping); +} + +void APS5_VABI TerminateAndUnmapFromCallout(void*) { + std::puts("positive: callback entered"); + std::fflush(stdout); + sceNpCalloutTermCtx(g_selfTermState.context); + std::puts("positive: self termination returned"); + std::fflush(stdout); + const int result = sceKernelMunmap(reinterpret_cast<std::uint64_t>(g_selfTermState.context), CalloutContextMappingSize); + std::printf("positive: context unmapped (%d)\n", result); + std::fflush(stdout); +} + +void APS5_VABI MarkCalloutReached(void*) { + g_selfTermState.callbackReached.store(1, std::memory_order_release); +} + +int RunSelfTermCalloutRepro(bool positive) { + g_selfTermState.context = MapCalloutContext(); + g_selfTermState.callbackReached.store(0, std::memory_order_relaxed); + g_selfTermCallout = {}; + Require(sceNpCalloutInitCtx(g_selfTermState.context, "np-self-term", 0x4000, 0, 0) == 0, + "the mapped callout context starts"); + const auto handler = positive ? TerminateAndUnmapFromCallout : MarkCalloutReached; + Require(sceNpCalloutStartOnCtx(g_selfTermState.context, &g_selfTermCallout, 0, handler, nullptr) == 0, + "the lifecycle callout is queued"); + + if (positive) { + std::this_thread::sleep_for(std::chrono::seconds(2)); + std::puts("positive: unexpectedly survived worker access after unmap"); + return 91; + } + + for (int i = 0; i < 2000 && g_selfTermState.callbackReached.load(std::memory_order_acquire) == 0; ++i) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + Require(g_selfTermState.callbackReached.load(std::memory_order_acquire) == 1, "the control callback completes"); + sceNpCalloutTermCtx(g_selfTermState.context); + Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(g_selfTermState.context), CalloutContextMappingSize) == 0, + "the joined control context is unmapped"); + std::puts("control: external termination and unmap completed"); + return 0; +} + void APS5_VABI RecordFirst(void*) { g_callouts.first = ++g_callouts.order; } @@ -415,6 +475,10 @@ void TestHexAndCopies() { } int main() { + if (const char* mode = std::getenv("APS5_NP_CALLOUT_SELF_TERM_UAF")) { + if (std::strcmp(mode, "positive") == 0) return RunSelfTermCalloutRepro(true); + if (std::strcmp(mode, "control") == 0) return RunSelfTermCalloutRepro(false); + } TestAtomics(); TestSemaphoresAndFlags(); TestSharedEventFlags(); ``` </details> Example PowerShell commands for an already configured build tree: ```powershell cmake --build build --target guest_np_common_runtime_tests $env:PATH = "$PWD\build\core\libs\libs\unpatched;$env:PATH" $env:APS5_NP_CALLOUT_SELF_TERM_UAF = 'control' & .\build\tests\guest_np_common_runtime_tests.exe $env:APS5_NP_CALLOUT_SELF_TERM_UAF = 'positive' & .\build\tests\guest_np_common_runtime_tests.exe ``` ### Log The positive process reaches both post-teardown messages before the worker faults: ```text positive: callback entered positive: self termination returned positive: context unmapped (0) FATAL: unhandled exception 0xc0000005 on thread ... 'np-self-term' rip ... libkernel.prx+0x6b331 read of 0x0000000200000008 ... ... libSceNpCommon.prx+0x1f47 ``` The external-termination control completes: ```text control: external termination and unmap completed ``` Twenty fresh processes produced: ```text positive: 10/10 exit 0xC0000005 after TermCtx returned and unmap returned 0 control: 10/10 exit 0, no fatal report timeouts: 0/20 ``` ### What was checked - The context is a valid zeroed 16 KiB guest mapping initialized through `sceNpCalloutInitCtx`; the callout is scheduled through `sceNpCalloutStartOnCtx` with zero delay. - The negative control changes only teardown ownership: the handler marks completion, then the external thread calls `sceNpCalloutTermCtx` and releases the same kind of context mapping. - The ordinary `guest_np_common_runtime` CTest still passes (1/1). - The executable, `libSceNpCommon.prx`, and `libkernel.prx` hashes were recorded before and after the 20-process matrix and did not change. The executable directory contained no competing copies of those PRX files. - A temporary two-part causal control handled self-termination separately and avoided touching the context after that callback. It survived the same post-unmap interval in 3/3 logs with no fatal report. This was an experimental control, not a complete proposed fix. - Windows was tested; Linux and macOS were not. No real title has been shown to use callback-owned termination. - The observed consequence is host-process termination. No data disclosure, controllable host write, cross-process effect, or sandbox escape was demonstrated. - The public SDK contract for callback-owned termination remains unknown. The code path is visible in current `main`: [`CalloutThread`](https://github.com/boykopovar/AnyPS5/blob/e61a7c7e2433dfccab7917a68065882deee96aa5/core/libs/prx/libSceNpCommon/Runtime.cpp#L574-L598) invokes the handler without the context mutex and then unconditionally locks `context->mutex` after the handler returns. [`sceNpCalloutTermCtx`](https://github.com/boykopovar/AnyPS5/blob/e61a7c7e2433dfccab7917a68065882deee96aa5/core/libs/prx/libSceNpCommon/Runtime.cpp#L623-L635) ignores the join result and completes teardown. The underlying [self-join path](https://github.com/boykopovar/AnyPS5/blob/e61a7c7e2433dfccab7917a68065882deee96aa5/core/libs/prx/libkernel/Pthread/src/Thread.cpp#L1069-L1073) returns `EDEADLK`. Related work, but not duplicates: #9063 introduced the NpCommon callout worker; #4052 / #4063 cover libkernel's self-join return value; and #5944 fixes an analogous AvPlayer close-from-callback lifecycle, in a different module and implementation. 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#11567
No description provided.