fix(libSceNpUniversalDataSystem): copy context, event, record and property outputs through GuestCopy #19606

Merged
gnavarro24 merged 3 commits from upstream-pr/19606 into main 2026-11-17 10:43:53 +00:00
gnavarro24 commented 2026-11-13 15:47:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gnavarro24 on 2026-11-13T15:47:28Z.


What

Twenty-two exports in core/libs/prx/libSceNpUniversalDataSystem wrote through an output pointer, read a guest string, or read an input struct directly, so an unwritable output, an unreadable string, or an unreadable struct faulted inside the library:

  • sceNpUniversalDataSystemCreateContext, CreateHandle, GetMemoryStat, GetStorageStat wrote their output (context, handle, stat) with a raw *ptr = value instead of GuestCopyOut_nid_no_patch.
  • sceNpUniversalDataSystemInitialize and sceNpUniversalDataSystemIntInitialize read param->size/param->pool_size (or ->memory) directly off the guest-supplied init struct instead of GuestCopyIn_nid_no_patch.
  • sceNpUniversalDataSystemCreateEvent read event_name with event_name[0] and an implicit std::string construction, and wrote both of its outputs (new_event, prop_ptr) directly; it now reads the name with CopyGuestString_nid_no_patch and writes both outputs through GuestCopyOut_nid_no_patch, releasing the newly created event if the second write fails so no event is left created-but-unreachable.
  • sceNpUniversalDataSystemEventEstimateSize and EventToString wrote their outputs directly (EventToString also memcpy'd straight into the guest buffer); both now also validate event against the existing events() registry before dereferencing it, the same check PostEvent already did — previously a destroyed or forged event handle faulted here instead of being rejected.
  • sceNpUniversalDataSystemCreateEventPropertyArray/CreateEventPropertyObject and the SetObject/SetArray variants of both the array and object property API wrote their new_array/new_object/value_ptr output directly; all now go through GuestCopyOut_nid_no_patch, and the created/cloned value is only added to its parent after that write succeeds, so a rejected call leaves no orphaned child.
  • sceNpUniversalDataSystemIntCreateRecordArray/CreateRecordObject/CreateRecordData and the IntRecordArraySetObject/SetArray/IntRecordObjectSetObject/SetArray internal record functions had the same record/valuePtr output-write and (for IntInitialize) input-struct-read issue; fixed the same way, releasing the record on a failed create.

python3 tools/guest_pointer_audit.py libSceNpUniversalDataSystem goes from 49 direct guest pointer accesses in 36 exported functions to 29 in 25, all of them either the now-validated event reads in EventEstimateSize/EventToString (the audit does not see the new events().Contains() check) or the array/object/value opaque-handle parameters of the public EventPropertyArraySet*/EventPropertyObjectSet* functions, which this PR does not touch — see Known gap below.

Known gap: unlike the Int* record functions in Record.cpp, which validate an array/object handle with FindArray/FindObject before dereferencing it, the public sceNpUniversalDataSystemEventPropertyArraySet*/EventPropertyObjectSet* functions still dereference their array/object/value handle without checking it is a live one, so a stale, destroyed or forged handle still crashes the host. Closing this needs the same handle-registry validation Record.cpp already has, which is a larger, separate change; recorded in docs/dev/debt/libSceNpUniversalDataSystem.md for a follow-up PR.

Tested

Linux x86-64 (Ubuntu 24.04, GCC 13.3.0, Ninja, Release, -DBUILD_TESTING=ON), branch head on main b975a42ca5. All affected test executables pass: np_uds_events_tests, guest_np_uds_queries_tests, guest_np_uds_records_tests, guest_np_trophy_unlocks_tests, guest_np_trophy_data_tests (with its required library-path argument) and guest_np_trophy_stats_tests (these last three link against libSceNpUniversalDataSystem and exercise it through libSceNpTrophy2). guest_np_uds_queries_tests and guest_np_uds_records_tests, which hold the new cases, each ran 3/3.

New cases cover every changed export, each passing 0x10 (an unmapped guest address) as the output pointer, string, struct or handle under test with every other argument valid, checking errno (set to EBUSY first) is preserved and, where applicable, that the refused call left no state change (the output variable untouched, the registered size of an array/object unchanged, a destroyed event's handle rejected instead of dereferenced):

  • GuestNpUniversalDataSystemQueries.cpp: Initialize, GetMemoryStat, GetStorageStat, CreateContext, CreateHandle, CreateEvent (unwritable new_event, unwritable prop_ptr, unreadable event_name), EventEstimateSize (unwritable size, and a destroyed/unknown event handle), EventToString (unwritable buf, unwritable string_size, and a destroyed/unknown event handle), CreateEventPropertyArray/CreateEventPropertyObject and their SetObject/SetArray value_ptr output.
  • GuestNpUniversalDataSystemRecords.cpp: IntInitialize (unreadable param), IntCreateRecordArray/CreateRecordObject/CreateRecordData (unwritable record), and IntRecordArraySetObject/SetArray/IntRecordObjectSetObject/SetArray (unwritable valuePtr), each confirming the rejected call did not still add its copy.

Negative control: the seven implementation files from main with the new test cases kept — guest_np_uds_queries_tests and guest_np_uds_records_tests both end with Segmentation fault (exit 139) in each of 3 runs.

python3 tools/check_conventions.py --base origin/main passes and git diff --check origin/main is clean. tools/pr_overlap.py --branch fix/libSceNpUniversalDataSystem-guest-copy reports no overlap. The only other open PRs naming this library (#13606, #14427) touch different files (LegacyExports.cpp, UdsEvent.cpp); git merge-tree confirms this branch merges cleanly with #13606 despite both editing docs/dev/debt/libSceNpUniversalDataSystem.md.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5298494009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19606). Originally posted by [gnavarro24](https://github.com/gnavarro24) on 2026-11-13T15:47:28Z. --- ### What Twenty-two exports in `core/libs/prx/libSceNpUniversalDataSystem` wrote through an output pointer, read a guest string, or read an input struct directly, so an unwritable output, an unreadable string, or an unreadable struct faulted inside the library: - `sceNpUniversalDataSystemCreateContext`, `CreateHandle`, `GetMemoryStat`, `GetStorageStat` wrote their output (`context`, `handle`, `stat`) with a raw `*ptr = value` instead of `GuestCopyOut_nid_no_patch`. - `sceNpUniversalDataSystemInitialize` and `sceNpUniversalDataSystemIntInitialize` read `param->size`/`param->pool_size` (or `->memory`) directly off the guest-supplied init struct instead of `GuestCopyIn_nid_no_patch`. - `sceNpUniversalDataSystemCreateEvent` read `event_name` with `event_name[0]` and an implicit `std::string` construction, and wrote both of its outputs (`new_event`, `prop_ptr`) directly; it now reads the name with `CopyGuestString_nid_no_patch` and writes both outputs through `GuestCopyOut_nid_no_patch`, releasing the newly created event if the second write fails so no event is left created-but-unreachable. - `sceNpUniversalDataSystemEventEstimateSize` and `EventToString` wrote their outputs directly (`EventToString` also `memcpy`'d straight into the guest buffer); both now also validate `event` against the existing `events()` registry before dereferencing it, the same check `PostEvent` already did — previously a destroyed or forged event handle faulted here instead of being rejected. - `sceNpUniversalDataSystemCreateEventPropertyArray`/`CreateEventPropertyObject` and the `SetObject`/`SetArray` variants of both the array and object property API wrote their `new_array`/`new_object`/`value_ptr` output directly; all now go through `GuestCopyOut_nid_no_patch`, and the created/cloned value is only added to its parent after that write succeeds, so a rejected call leaves no orphaned child. - `sceNpUniversalDataSystemIntCreateRecordArray`/`CreateRecordObject`/`CreateRecordData` and the `IntRecordArraySetObject`/`SetArray`/`IntRecordObjectSetObject`/`SetArray` internal record functions had the same `record`/`valuePtr` output-write and (for `IntInitialize`) input-struct-read issue; fixed the same way, releasing the record on a failed create. `python3 tools/guest_pointer_audit.py libSceNpUniversalDataSystem` goes from 49 direct guest pointer accesses in 36 exported functions to 29 in 25, all of them either the now-validated `event` reads in `EventEstimateSize`/`EventToString` (the audit does not see the new `events().Contains()` check) or the `array`/`object`/`value` opaque-handle parameters of the public `EventPropertyArraySet*`/`EventPropertyObjectSet*` functions, which this PR does not touch — see **Known gap** below. **Known gap**: unlike the `Int*` record functions in `Record.cpp`, which validate an array/object handle with `FindArray`/`FindObject` before dereferencing it, the public `sceNpUniversalDataSystemEventPropertyArraySet*`/`EventPropertyObjectSet*` functions still dereference their `array`/`object`/`value` handle without checking it is a live one, so a stale, destroyed or forged handle still crashes the host. Closing this needs the same handle-registry validation `Record.cpp` already has, which is a larger, separate change; recorded in `docs/dev/debt/libSceNpUniversalDataSystem.md` for a follow-up PR. ### Tested Linux x86-64 (Ubuntu 24.04, GCC 13.3.0, Ninja, Release, `-DBUILD_TESTING=ON`), branch head on `main` `b975a42ca5`. All affected test executables pass: `np_uds_events_tests`, `guest_np_uds_queries_tests`, `guest_np_uds_records_tests`, `guest_np_trophy_unlocks_tests`, `guest_np_trophy_data_tests` (with its required library-path argument) and `guest_np_trophy_stats_tests` (these last three link against `libSceNpUniversalDataSystem` and exercise it through `libSceNpTrophy2`). `guest_np_uds_queries_tests` and `guest_np_uds_records_tests`, which hold the new cases, each ran 3/3. New cases cover every changed export, each passing `0x10` (an unmapped guest address) as the output pointer, string, struct or handle under test with every other argument valid, checking `errno` (set to `EBUSY` first) is preserved and, where applicable, that the refused call left no state change (the output variable untouched, the registered size of an array/object unchanged, a destroyed event's handle rejected instead of dereferenced): - `GuestNpUniversalDataSystemQueries.cpp`: `Initialize`, `GetMemoryStat`, `GetStorageStat`, `CreateContext`, `CreateHandle`, `CreateEvent` (unwritable `new_event`, unwritable `prop_ptr`, unreadable `event_name`), `EventEstimateSize` (unwritable `size`, and a destroyed/unknown event handle), `EventToString` (unwritable `buf`, unwritable `string_size`, and a destroyed/unknown event handle), `CreateEventPropertyArray`/`CreateEventPropertyObject` and their `SetObject`/`SetArray` `value_ptr` output. - `GuestNpUniversalDataSystemRecords.cpp`: `IntInitialize` (unreadable param), `IntCreateRecordArray`/`CreateRecordObject`/`CreateRecordData` (unwritable `record`), and `IntRecordArraySetObject`/`SetArray`/`IntRecordObjectSetObject`/`SetArray` (unwritable `valuePtr`), each confirming the rejected call did not still add its copy. Negative control: the seven implementation files from `main` with the new test cases kept — `guest_np_uds_queries_tests` and `guest_np_uds_records_tests` both end with `Segmentation fault` (exit 139) in each of 3 runs. `python3 tools/check_conventions.py --base origin/main` passes and `git diff --check origin/main` is clean. `tools/pr_overlap.py --branch fix/libSceNpUniversalDataSystem-guest-copy` reports no overlap. The only other open PRs naming this library (#13606, #14427) touch different files (`LegacyExports.cpp`, `UdsEvent.cpp`); `git merge-tree` confirms this branch merges cleanly with #13606 despite both editing `docs/dev/debt/libSceNpUniversalDataSystem.md`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-13 19:33:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-13T19:33:04Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 21da2bec01:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_np_uds_queries_tests, guest_np_uds_records_tests built cleanly.
  • CTest (guest_np_uds_records, guest_np_uds_queries):
    1/2 Test #4110: guest_np_uds_queries .............   Passed    0.02 sec
    2/2 Test #4109: guest_np_uds_records .............   Passed    1.19 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceNpUniversalDataSystem/src/Context.cpp, core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp, core/libs/prx/libSceNpUniversalDataSystem/src/EventPropertyArray.cpp, core/libs/prx/libSceNpUniversalDataSystem/src/EventPropertyObject.cpp, core/libs/prx/libSceNpUniversalDataSystem/src/Handle.cpp, core/libs/prx/libSceNpUniversalDataSystem/src/Record.cpp, core/libs/prx/libSceNpUniversalDataSystem/src/System.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4109: guest_np_uds_records .............***Exception: SegFault  0.45 sec
    2/2 Test #4110: guest_np_uds_queries .............***Exception: SegFault  0.55 sec
    0% tests passed, 2 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp: Verified sceNpUniversalDataSystemEventToString guards against unsigned underflow by checking buf_size != 0 before computing std::min(text.size(), buf_size - 1), writes truncated text with null termination via GuestCopyOut_nid_no_patch, and returns SCE_NP_UNIVERSAL_DATA_SYSTEM_ERROR_INVALID_ARGUMENT (0x80553102u) on copy failure while properly handling buf == nullptr query modes.
    • core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp: Verified sceNpUniversalDataSystemCreateEvent safely reads guest event names through CopyGuestString_nid_no_patch(event_name, 0x1000, name), validates non-empty strings, and releases the adopted event via events().Release(created) if writing either new_event or prop_ptr through GuestCopyOut_nid_no_patch fails.
    • core/libs/prx/libSceNpUniversalDataSystem/src/Record.cpp: Verified sceNpUniversalDataSystemIntInitialize reads guest init parameters into a local struct with GuestCopyIn_nid_no_patch under g_recordsMutex, validates copy.size == sizeof(NpUniversalDataSystemIntInitParam) and copy.memory != nullptr, returns kInvalidArgument on unreadable guest memory, and preserves caller errno.
    • core/libs/prx/libSceNpUniversalDataSystem/src/Record.cpp: Verified sceNpUniversalDataSystemIntCreateRecordArray, CreateRecordObject, and CreateRecordData release newly adopted records from g_arrays / g_objects if GuestCopyOut_nid_no_patch to the guest output pointer fails, and setters only attach child objects to parent containers after writing valuePtr succeeds.
    • core/libs/prx/libSceNpUniversalDataSystem/src/Context.cpp and Handle.cpp: Verified sceNpUniversalDataSystemCreateContext and sceNpUniversalDataSystemCreateHandle write context IDs and default handles via GuestCopyOut_nid_no_patch, return SCE_NP_UNIVERSAL_DATA_SYSTEM_ERROR_INVALID_ARGUMENT without inserting into table.owners on failure, and preserve errno.
    • core/libs/prx/libSceNpUniversalDataSystem/src/System.cpp: Verified sceNpUniversalDataSystemInitialize, GetMemoryStat, and GetStorageStat perform guest reads/writes exclusively through GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch, zero-initialize stat structs before copying out, validate parameter sizes, and preserve errno.
    • docs/dev/debt/libSceNpUniversalDataSystem.md and AGENTS.md compliance: Verified zero code comments were introduced, LP64/LLP64 type compatibility is preserved across size_t operations, and known gaps regarding public handle validation are documented in the debt file.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5896891762 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19606#pullrequestreview-5896891762). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-13T19:33:04Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `21da2bec01`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_np_uds_queries_tests`, `guest_np_uds_records_tests` built cleanly. - **CTest** (`guest_np_uds_records`, `guest_np_uds_queries`): ``` 1/2 Test #4110: guest_np_uds_queries ............. Passed 0.02 sec 2/2 Test #4109: guest_np_uds_records ............. Passed 1.19 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libSceNpUniversalDataSystem/src/Context.cpp`, `core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp`, `core/libs/prx/libSceNpUniversalDataSystem/src/EventPropertyArray.cpp`, `core/libs/prx/libSceNpUniversalDataSystem/src/EventPropertyObject.cpp`, `core/libs/prx/libSceNpUniversalDataSystem/src/Handle.cpp`, `core/libs/prx/libSceNpUniversalDataSystem/src/Record.cpp`, `core/libs/prx/libSceNpUniversalDataSystem/src/System.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #4109: guest_np_uds_records .............***Exception: SegFault 0.45 sec 2/2 Test #4110: guest_np_uds_queries .............***Exception: SegFault 0.55 sec 0% tests passed, 2 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp: Verified sceNpUniversalDataSystemEventToString guards against unsigned underflow by checking buf_size != 0 before computing std::min(text.size(), buf_size - 1), writes truncated text with null termination via GuestCopyOut_nid_no_patch, and returns SCE_NP_UNIVERSAL_DATA_SYSTEM_ERROR_INVALID_ARGUMENT (0x80553102u) on copy failure while properly handling buf == nullptr query modes. - core/libs/prx/libSceNpUniversalDataSystem/src/Event.cpp: Verified sceNpUniversalDataSystemCreateEvent safely reads guest event names through CopyGuestString_nid_no_patch(event_name, 0x1000, name), validates non-empty strings, and releases the adopted event via events().Release(created) if writing either new_event or prop_ptr through GuestCopyOut_nid_no_patch fails. - core/libs/prx/libSceNpUniversalDataSystem/src/Record.cpp: Verified sceNpUniversalDataSystemIntInitialize reads guest init parameters into a local struct with GuestCopyIn_nid_no_patch under g_recordsMutex, validates copy.size == sizeof(NpUniversalDataSystemIntInitParam) and copy.memory != nullptr, returns kInvalidArgument on unreadable guest memory, and preserves caller errno. - core/libs/prx/libSceNpUniversalDataSystem/src/Record.cpp: Verified sceNpUniversalDataSystemIntCreateRecordArray, CreateRecordObject, and CreateRecordData release newly adopted records from g_arrays / g_objects if GuestCopyOut_nid_no_patch to the guest output pointer fails, and setters only attach child objects to parent containers after writing valuePtr succeeds. - core/libs/prx/libSceNpUniversalDataSystem/src/Context.cpp and Handle.cpp: Verified sceNpUniversalDataSystemCreateContext and sceNpUniversalDataSystemCreateHandle write context IDs and default handles via GuestCopyOut_nid_no_patch, return SCE_NP_UNIVERSAL_DATA_SYSTEM_ERROR_INVALID_ARGUMENT without inserting into table.owners on failure, and preserve errno. - core/libs/prx/libSceNpUniversalDataSystem/src/System.cpp: Verified sceNpUniversalDataSystemInitialize, GetMemoryStat, and GetStorageStat perform guest reads/writes exclusively through GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch, zero-initialize stat structs before copying out, validate parameter sizes, and preserve errno. - docs/dev/debt/libSceNpUniversalDataSystem.md and AGENTS.md compliance: Verified zero code comments were introduced, LP64/LLP64 type compatibility is preserved across size_t operations, and known gaps regarding public handle validation are documented in the debt file. - Note: the AI-assisted checkbox can stay. Looks Good To Me
Sign in to join this conversation.
No reviewers
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!19606
No description provided.