From d5f00579c4d58c9c0d1aa74e6a12aba48e336baa Mon Sep 17 00:00:00 2001 From: louisdibiasi <59670693+louisdibiasi@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:57:44 -0500 Subject: [PATCH] runtime: keep save name handling safe on allocation failure --- src/startup_stubs/runtime_proxy.cpp | 2 +- src/startup_stubs/save_store.h | 19 ++++++++++++++----- tests/startup_gamesave_submit.cpp | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/startup_stubs/runtime_proxy.cpp b/src/startup_stubs/runtime_proxy.cpp index eac6941c85..bf117e9e82 100644 --- a/src/startup_stubs/runtime_proxy.cpp +++ b/src/startup_stubs/runtime_proxy.cpp @@ -832,8 +832,8 @@ std::string update_container(u64 update) { std::string name; AcquireSRWLockShared(&save_lock); + struct Unlock { ~Unlock() { ReleaseSRWLockShared(&save_lock); } } unlock; if (update >= save_update_handle) name = save_table.update_container(static_cast(update - save_update_handle)); - ReleaseSRWLockShared(&save_lock); return name; } #include "startup_stubs/save_read.inl" diff --git a/src/startup_stubs/save_store.h b/src/startup_stubs/save_store.h index 26d0ec8c9f..2237dd77bd 100644 --- a/src/startup_stubs/save_store.h +++ b/src/startup_stubs/save_store.h @@ -57,12 +57,21 @@ inline bool valid_name(std::string_view name) noexcept same = std::tolower(static_cast(tail[i])) == suffix[i]; if (same) return false; } - std::string stem(name.substr(0, name.find('.'))); - for (char& c : stem) c = static_cast(c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c); - while (!stem.empty() && stem.back() == ' ') stem.pop_back(); + std::string_view stem = name.substr(0, name.find('.')); + while (!stem.empty() && stem.back() == ' ') stem.remove_suffix(1); + const auto upper = [](char c) { return static_cast(c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c); }; + const auto matches = [&](std::string_view device) { + if (stem.size() != device.size()) return false; + for (std::size_t i = 0; i < stem.size(); ++i) + if (upper(stem[i]) != device[i]) return false; + return true; + }; for (const char* device : {"CON", "PRN", "AUX", "NUL"}) - if (stem == device) return false; - if (stem.size() == 4 && (stem.compare(0, 3, "COM") == 0 || stem.compare(0, 3, "LPT") == 0) && stem[3] >= '0' && stem[3] <= '9') return false; + if (matches(device)) return false; + if (stem.size() == 4 && stem[3] >= '0' && stem[3] <= '9') { + stem.remove_suffix(1); + if (matches("COM") || matches("LPT")) return false; + } return true; } diff --git a/tests/startup_gamesave_submit.cpp b/tests/startup_gamesave_submit.cpp index d17489a9fc..687b361a37 100644 --- a/tests/startup_gamesave_submit.cpp +++ b/tests/startup_gamesave_submit.cpp @@ -108,6 +108,23 @@ int run(bool system) call(16,provider_handle,ptr("profile"),ptr(&container)) || call(23,container,0,ptr(&good)) || call(23,container,0,ptr(&bad))) return 1; unsigned char byte=0x11; + const std::string long_name(80, 'c'); + const std::string name_copy(long_name); + const std::size_t name_allocation = name_copy.capacity() + 1; + fail_allocation_size = name_allocation; + if (!x2w_save::valid_name(long_name) || fail_allocation_size != name_allocation) return 26; + fail_allocation_size = 0; + u64 long_container{}, long_update{}; + if (call(16,provider_handle,ptr(long_name.c_str()),ptr(&long_container)) || + call(23,long_container,0,ptr(&long_update))) return 22; + fail_allocation_size = name_allocation; // Fail the same string copy under save_lock. + const u64 name_copy_result = call(25,long_update,ptr("preference"),ptr(&byte),1); + if (fail_allocation_size || name_copy_result != e_outofmemory) return 23; + if (!TryAcquireSRWLockExclusive(&save_lock)) return 24; + ReleaseSRWLockExclusive(&save_lock); + if (call(25,long_update,ptr("preference"),ptr(&byte),1) || call(27,long_update)) return 25; + call(24,long_update); + call(17,long_container); const std::array large_blob{}; for (const auto data_size : {std::size_t{1}, large_blob.size()}) { fail_allocation_size = data_size == 1 ? sizeof(x2w_save::Action) : data_size;