fix(libSceSaveData.native): return SAVE_DATA_ERROR_PARAMETER from sceSaveDataSetupSaveDataMemory2 for invalid pointers #18674

Closed
Kikoarl wants to merge 1 commit from upstream-pr/18674 into main
Kikoarl commented 2026-11-12 01:45:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-11-12T01:45:28Z.


What

Safely copy in setup_param (and its nested init_param when required) with GuestCopyIn_nid_no_patch and validate result writeability with GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch in sceSaveDataSetupSaveDataMemory2, preserving caller errno across copy failures and returning SAVE_DATA_ERROR_PARAMETER (0x809f0000). Guard trace logging against unmapped guest pointer dereferences.

Tested

Linux (x86_64). Built in RAM-disk /tmp/anyps5_build (ninja -C /tmp/anyps5_build savedata_memory_growth_tests), verified unit test suite savedata_memory_growth and savedata_memory_growth_read_failure both passing (100% tests passed out of 2), and ran full ctest -R savedata with 35/35 tests passing cleanly. Verified python3 tools/check_conventions.py --base origin/main with zero errors.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5269793887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18674). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-11-12T01:45:28Z. --- ### What Safely copy in `setup_param` (and its nested `init_param` when required) with `GuestCopyIn_nid_no_patch` and validate `result` writeability with `GuestCopyIn_nid_no_patch`/`GuestCopyOut_nid_no_patch` in `sceSaveDataSetupSaveDataMemory2`, preserving caller `errno` across copy failures and returning `SAVE_DATA_ERROR_PARAMETER` (`0x809f0000`). Guard trace logging against unmapped guest pointer dereferences. ### Tested Linux (x86_64). Built in RAM-disk `/tmp/anyps5_build` (`ninja -C /tmp/anyps5_build savedata_memory_growth_tests`), verified unit test suite `savedata_memory_growth` and `savedata_memory_growth_read_failure` both passing (`100% tests passed out of 2`), and ran full `ctest -R savedata` with 35/35 tests passing cleanly. Verified `python3 tools/check_conventions.py --base origin/main` with zero errors. ### Checklist - [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: none - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-12 06:11:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:11:40Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 72bad97e2e:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: savedata_memory_growth_tests built cleanly.
  • CTest (savedata_memory_growth, savedata_memory_growth_read_failure):
    1/2 Test #838: savedata_memory_growth_read_failure ...   Passed    0.00 sec
    2/2 Test #837: savedata_memory_growth ................   Passed    0.36 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceSaveData.native/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #838: savedata_memory_growth_read_failure ...***Exception: SegFault  0.14 sec
    2/2 Test #837: savedata_memory_growth ................***Exception: SegFault  0.16 sec
    0% tests passed, 2 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified pointer safety in setupSaveDataMemory2: setup_param, result, and optional init_param are validated and copied via GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch before any direct host dereference, preventing crashes on invalid or unmapped addresses.
    • Verified error handling and errno preservation: failures during guest memory copy safely restore savedErrno and return SAVE_DATA_ERROR_PARAMETER (0x809f0000), preventing internal EFAULT leakage to caller code.
    • Verified trace logging safety in sceSaveDataSetupSaveDataMemory2: SAVEDATA_TRACE guards dereferencing setup_param and result with rc == SAVE_DATA_OK, avoiding unmapped memory faults when logging invalid parameter calls.
    • Verified synchronization: g_mem_mutex is locked after upfront pointer and size validation, avoiding lock contention and deadlocks during parameter fault handling while properly guarding journal replay and file modifications.
    • Verified AGENTS.md conventions: no comments were added in code (one preexisting non-compliant comment was removed), technical debt was properly registered in docs/dev/debt/libSceSaveData.native.md, and no spurious files were introduced.
    • Verified test coverage in core/libs/tests/SaveDataMemoryGrowth.cpp: invalid setup_param and result pointers (0x10) are explicitly exercised and verified to return ErrorParameter (0x809f0000) while confirming errno preservation.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869918530 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18674#pullrequestreview-5869918530). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:11:40Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `72bad97e2e`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `savedata_memory_growth_tests` built cleanly. - **CTest** (`savedata_memory_growth`, `savedata_memory_growth_read_failure`): ``` 1/2 Test #838: savedata_memory_growth_read_failure ... Passed 0.00 sec 2/2 Test #837: savedata_memory_growth ................ Passed 0.36 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libSceSaveData.native/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #838: savedata_memory_growth_read_failure ...***Exception: SegFault 0.14 sec 2/2 Test #837: savedata_memory_growth ................***Exception: SegFault 0.16 sec 0% tests passed, 2 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - Verified pointer safety in setupSaveDataMemory2: setup_param, result, and optional init_param are validated and copied via GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch before any direct host dereference, preventing crashes on invalid or unmapped addresses. - Verified error handling and errno preservation: failures during guest memory copy safely restore savedErrno and return SAVE_DATA_ERROR_PARAMETER (0x809f0000), preventing internal EFAULT leakage to caller code. - Verified trace logging safety in sceSaveDataSetupSaveDataMemory2: SAVEDATA_TRACE guards dereferencing setup_param and result with rc == SAVE_DATA_OK, avoiding unmapped memory faults when logging invalid parameter calls. - Verified synchronization: g_mem_mutex is locked after upfront pointer and size validation, avoiding lock contention and deadlocks during parameter fault handling while properly guarding journal replay and file modifications. - Verified AGENTS.md conventions: no comments were added in code (one preexisting non-compliant comment was removed), technical debt was properly registered in docs/dev/debt/libSceSaveData.native.md, and no spurious files were introduced. - Verified test coverage in core/libs/tests/SaveDataMemoryGrowth.cpp: invalid setup_param and result pointers (0x10) are explicitly exercised and verified to return ErrorParameter (0x809f0000) while confirming errno preservation. - Note: the AI-assisted checkbox can stay. Looks Good To Me

Pull request closed

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!18674
No description provided.