fix(libSceSaveData.native): validate pattern termination in sceSaveDataDirNameSearch #12650

Merged
slopgobblercobbler merged 1 commit from upstream-pr/12650 into main 2026-11-05 11:05:19 +00:00
slopgobblercobbler commented 2026-10-29 00:12:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-29T00:12:12Z.


What

Validates null-termination of cond->dir_name in dirNameSearch (sceSaveDataDirNameSearch and sceSaveDataDirNameSearch2), returning SAVE_DATA_ERROR_PARAMETER if the 32-byte fixed-size pattern array is not null-terminated within its bounds.

Without this check, an unterminated pattern string passed in dir_name causes unbounded reads / buffer overreads in dir_name_match past the 32-byte data buffer. Other save data APIs such as sceSaveDataMount3, sceSaveDataDelete, sceSaveDataConvert, and sceSaveDataBackup similarly enforce null-termination of 32-byte directory names using std::memchr.

Tested

  • Tested on Windows 11 with Clang++ 21.1.0 using core/libs/tests/GuestSaveDataSearch.cpp (CheckNullArguments). Confirmed that passing an unterminated 32-byte directory pattern to both sceSaveDataDirNameSearch and sceSaveDataDirNameSearch2 returns SaveDataErrorParameter (0x809f0001 / -2137063424).
  • Conventions check verified with python tools/check_conventions.py --base upstream/main --head HEAD (0 findings).
  • Windows 11 host verification courtesy of @JJenkx: Windows 11 Enterprise (Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7), guest_savedata_search passed 3/3; negative control confirmed failing against base Export.cpp.
  • Linux host verification courtesy of @jotitadev: Ubuntu 26.04 (i7-8700, GCC 15.2 Release), head 46b89c8f4f merged onto ed1e0ed08f, guest_savedata_search (3/3), all 7 guest_savedata_* suites, and public_headers passed; control run confirmed failing at unterminated dir_name check.

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:5059772378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12650). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-29T00:12:12Z. --- ### What Validates null-termination of `cond->dir_name` in `dirNameSearch` (`sceSaveDataDirNameSearch` and `sceSaveDataDirNameSearch2`), returning `SAVE_DATA_ERROR_PARAMETER` if the 32-byte fixed-size pattern array is not null-terminated within its bounds. Without this check, an unterminated pattern string passed in `dir_name` causes unbounded reads / buffer overreads in `dir_name_match` past the 32-byte `data` buffer. Other save data APIs such as `sceSaveDataMount3`, `sceSaveDataDelete`, `sceSaveDataConvert`, and `sceSaveDataBackup` similarly enforce null-termination of 32-byte directory names using `std::memchr`. ### Tested - Tested on Windows 11 with Clang++ 21.1.0 using `core/libs/tests/GuestSaveDataSearch.cpp` (`CheckNullArguments`). Confirmed that passing an unterminated 32-byte directory pattern to both `sceSaveDataDirNameSearch` and `sceSaveDataDirNameSearch2` returns `SaveDataErrorParameter` (`0x809f0001` / `-2137063424`). - Conventions check verified with `python tools/check_conventions.py --base upstream/main --head HEAD` (0 findings). - Windows 11 host verification courtesy of @JJenkx: Windows 11 Enterprise (Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7), `guest_savedata_search` passed 3/3; negative control confirmed failing against base `Export.cpp`. - Linux host verification courtesy of @jotitadev: Ubuntu 26.04 (i7-8700, GCC 15.2 Release), head `46b89c8f4f` merged onto `ed1e0ed08f`, `guest_savedata_search` (3/3), all 7 `guest_savedata_*` suites, and `public_headers` passed; control run confirmed failing at unterminated `dir_name` check. ### 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: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 01:15:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T01:15:25Z.

Review: COMMENTED


Reviewed on macOS arm64 (Apple clang, -fsanitize=address,undefined -fno-sanitize-recover=all), standalone Export.cpp + GuestSaveDataSearch.cpp + a 4-symbol shim (GetAppTitleId_nid_postfix, Add/RemovePathAlias_nid_no_patch, NotImplemented_nid_no_patch), at 346f268170.

  • Head: guest_savedata_search passes. With main's Export.cpp it fails at unterminated dir_name in sceSaveDataDirNameSearch returns PARAMETER, so the new test does detect the bug.
  • The over-read is still there when tracing is on. With APS5_SAVEDATA_TRACE=1 the head test aborts (rc 134): ASan reports stack-buffer-overflow in fprintf, called from sceSaveDataDirNameSearch Export.cpp:482. The wrapper logs pattern=%s from cond->dir_name->data after dirNameSearch has returned PARAMETER for the unterminated name (unterminatedPattern is [224, 256) in the frame). sceSaveDataDirNameSearch2 at L1004 has the same problem. sceSaveDataMount3's trace (L672) already bounds its name with %.*s. Without the env var only the first 3 trace lines print, so CI does not see this.
  • Mutants on the guard: dropping it or disabling it is killed. memchr(..., sizeof(data) - 1) survives, because no test passes a 32-byte name whose terminator is in its last byte.

The diff below bounds both traces the way Mount3 does and adds the last-byte acceptance case. With it, the test passes with and without APS5_SAVEDATA_TRACE=1, all 3 guard mutants are killed, and check_conventions and git merge-tree against main 3a2b919816 are clean.

diff --git a/core/libs/prx/libSceSaveData.native/Export.cpp b/core/libs/prx/libSceSaveData.native/Export.cpp
index 465d5bdfd9..01009cbf92 100644
--- a/core/libs/prx/libSceSaveData.native/Export.cpp
+++ b/core/libs/prx/libSceSaveData.native/Export.cpp
@@ -479,7 +479,7 @@ static int dirNameSearch(const SaveDataDirNameSearchCond* cond, SaveDataDirNameS
 
 int APS5_VABI sceSaveDataDirNameSearch(const SaveDataDirNameSearchCond* cond, SaveDataDirNameSearchResult* result) {
     const int rc = dirNameSearch(cond, result);
-    SAVEDATA_TRACE("dirNameSearch user=%d pattern=%s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc));
+    SAVEDATA_TRACE("dirNameSearch user=%d pattern=%.*s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, static_cast<int>(sizeof(SceSaveDataDirName::data)), cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc));
     return rc;
 }
 
@@ -1001,7 +1001,7 @@ int APS5_VABI sceSaveDataCancel(std::uint32_t cancelArgument) {
 
 int APS5_VABI sceSaveDataDirNameSearch2(const SaveDataDirNameSearchCond* cond, SaveDataDirNameSearchResult* result) {
     const int rc = dirNameSearch(cond, result);
-    SAVEDATA_TRACE("dirNameSearch2 user=%d pattern=%s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc));
+    SAVEDATA_TRACE("dirNameSearch2 user=%d pattern=%.*s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, static_cast<int>(sizeof(SceSaveDataDirName::data)), cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc));
     return rc;
 }
 
diff --git a/core/libs/tests/GuestSaveDataSearch.cpp b/core/libs/tests/GuestSaveDataSearch.cpp
index 460f10d7b4..c2f84e8a99 100644
--- a/core/libs/tests/GuestSaveDataSearch.cpp
+++ b/core/libs/tests/GuestSaveDataSearch.cpp
@@ -287,6 +287,9 @@ static void CheckNullArguments() {
     unterminatedCond.dir_name = reinterpret_cast<const SceSaveDataDirName*>(unterminatedPattern);
     Require(sceSaveDataDirNameSearch(&unterminatedCond, &result) == SaveDataErrorParameter, "unterminated dir_name in sceSaveDataDirNameSearch returns PARAMETER");
     Require(sceSaveDataDirNameSearch2(&unterminatedCond, &result) == SaveDataErrorParameter, "unterminated dir_name in sceSaveDataDirNameSearch2 returns PARAMETER");
+    unterminatedPattern[sizeof(unterminatedPattern) - 1] = '\0';
+    Require(sceSaveDataDirNameSearch(&unterminatedCond, &result) == 0, "a dir_name ending in its last byte is accepted by sceSaveDataDirNameSearch");
+    Require(sceSaveDataDirNameSearch2(&unterminatedCond, &result) == 0, "a dir_name ending in its last byte is accepted by sceSaveDataDirNameSearch2");
     Require(sceSaveDataGetConvertProgress(nullptr) == SaveDataErrorParameter, "null progress returns PARAMETER in sceSaveDataGetConvertProgress");
     float progress = 0.0f;
     Require(sceSaveDataGetConvertProgress(&progress) == -2137063423, "uninitialized convert progress returns NOT_INITIALIZED");
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5691712663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12650#pullrequestreview-5691712663). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T01:15:25Z. **Review:** COMMENTED --- Reviewed on macOS arm64 (Apple clang, `-fsanitize=address,undefined -fno-sanitize-recover=all`), standalone `Export.cpp` + `GuestSaveDataSearch.cpp` + a 4-symbol shim (`GetAppTitleId_nid_postfix`, `Add/RemovePathAlias_nid_no_patch`, `NotImplemented_nid_no_patch`), at 346f268170. - Head: `guest_savedata_search` passes. With `main`'s `Export.cpp` it fails at `unterminated dir_name in sceSaveDataDirNameSearch returns PARAMETER`, so the new test does detect the bug. - **The over-read is still there when tracing is on.** With `APS5_SAVEDATA_TRACE=1` the head test aborts (rc 134): ASan reports `stack-buffer-overflow` in `fprintf`, called from `sceSaveDataDirNameSearch` Export.cpp:482. The wrapper logs `pattern=%s` from `cond->dir_name->data` after `dirNameSearch` has returned PARAMETER for the unterminated name (`unterminatedPattern` is [224, 256) in the frame). `sceSaveDataDirNameSearch2` at L1004 has the same problem. `sceSaveDataMount3`'s trace (L672) already bounds its name with `%.*s`. Without the env var only the first 3 trace lines print, so CI does not see this. - Mutants on the guard: dropping it or disabling it is killed. **`memchr(..., sizeof(data) - 1)` survives**, because no test passes a 32-byte name whose terminator is in its last byte. The diff below bounds both traces the way Mount3 does and adds the last-byte acceptance case. With it, the test passes with and without `APS5_SAVEDATA_TRACE=1`, all 3 guard mutants are killed, and `check_conventions` and `git merge-tree` against main 3a2b919816 are clean. ```diff diff --git a/core/libs/prx/libSceSaveData.native/Export.cpp b/core/libs/prx/libSceSaveData.native/Export.cpp index 465d5bdfd9..01009cbf92 100644 --- a/core/libs/prx/libSceSaveData.native/Export.cpp +++ b/core/libs/prx/libSceSaveData.native/Export.cpp @@ -479,7 +479,7 @@ static int dirNameSearch(const SaveDataDirNameSearchCond* cond, SaveDataDirNameS int APS5_VABI sceSaveDataDirNameSearch(const SaveDataDirNameSearchCond* cond, SaveDataDirNameSearchResult* result) { const int rc = dirNameSearch(cond, result); - SAVEDATA_TRACE("dirNameSearch user=%d pattern=%s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc)); + SAVEDATA_TRACE("dirNameSearch user=%d pattern=%.*s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, static_cast<int>(sizeof(SceSaveDataDirName::data)), cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc)); return rc; } @@ -1001,7 +1001,7 @@ int APS5_VABI sceSaveDataCancel(std::uint32_t cancelArgument) { int APS5_VABI sceSaveDataDirNameSearch2(const SaveDataDirNameSearchCond* cond, SaveDataDirNameSearchResult* result) { const int rc = dirNameSearch(cond, result); - SAVEDATA_TRACE("dirNameSearch2 user=%d pattern=%s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc)); + SAVEDATA_TRACE("dirNameSearch2 user=%d pattern=%.*s hit=%u set=%u -> 0x%08x", cond != nullptr ? cond->user_id : -1, static_cast<int>(sizeof(SceSaveDataDirName::data)), cond != nullptr && cond->dir_name != nullptr ? cond->dir_name->data : "(all)", result != nullptr ? result->hit_num : 0u, result != nullptr ? result->set_num : 0u, static_cast<unsigned>(rc)); return rc; } diff --git a/core/libs/tests/GuestSaveDataSearch.cpp b/core/libs/tests/GuestSaveDataSearch.cpp index 460f10d7b4..c2f84e8a99 100644 --- a/core/libs/tests/GuestSaveDataSearch.cpp +++ b/core/libs/tests/GuestSaveDataSearch.cpp @@ -287,6 +287,9 @@ static void CheckNullArguments() { unterminatedCond.dir_name = reinterpret_cast<const SceSaveDataDirName*>(unterminatedPattern); Require(sceSaveDataDirNameSearch(&unterminatedCond, &result) == SaveDataErrorParameter, "unterminated dir_name in sceSaveDataDirNameSearch returns PARAMETER"); Require(sceSaveDataDirNameSearch2(&unterminatedCond, &result) == SaveDataErrorParameter, "unterminated dir_name in sceSaveDataDirNameSearch2 returns PARAMETER"); + unterminatedPattern[sizeof(unterminatedPattern) - 1] = '\0'; + Require(sceSaveDataDirNameSearch(&unterminatedCond, &result) == 0, "a dir_name ending in its last byte is accepted by sceSaveDataDirNameSearch"); + Require(sceSaveDataDirNameSearch2(&unterminatedCond, &result) == 0, "a dir_name ending in its last byte is accepted by sceSaveDataDirNameSearch2"); Require(sceSaveDataGetConvertProgress(nullptr) == SaveDataErrorParameter, "null progress returns PARAMETER in sceSaveDataGetConvertProgress"); float progress = 0.0f; Require(sceSaveDataGetConvertProgress(&progress) == -2137063423, "uninitialized convert progress returns NOT_INITIALIZED"); ```
jotitadev commented 2026-10-30 15:38:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T15:38:47Z.

Review: COMMENTED


First Linux run of this head (the thread has Windows and a standalone macOS harness).

Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON. Head 46b89c8f4f merged (--no-commit) onto current main ed1e0ed08f; the merge is clean.

  • guest_savedata_search: passes 3/3.
  • All 7 guest_savedata_* tests pass.
  • public_headers: passes.
  • Control: with Export.cpp from main and this PR's test kept, guest_savedata_search fails 3/3 at unterminated dir_name in sceSaveDataDirNameSearch returns PARAMETER.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5714729036 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12650#pullrequestreview-5714729036). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T15:38:47Z. **Review:** COMMENTED --- First Linux run of this head (the thread has Windows and a standalone macOS harness). Ubuntu 26.04, i7-8700, GCC 15.2, Release, `BUILD_TESTING=ON`. Head `46b89c8f4f` merged (`--no-commit`) onto current `main` `ed1e0ed08f`; the merge is clean. - `guest_savedata_search`: passes 3/3. - All 7 `guest_savedata_*` tests pass. - `public_headers`: passes. - Control: with `Export.cpp` from `main` and this PR's test kept, `guest_savedata_search` fails 3/3 at `unterminated dir_name in sceSaveDataDirNameSearch returns PARAMETER`. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!12650
No description provided.