Play your Steam library's Windows games on a jailbroken PS5.
C++
Serialize private saves with root access
Leonardo Lobato3daa8186390a
diff --git a/cmake/PlaydenCore.cmake b/cmake/PlaydenCore.cmakeindex 7194348..a56a36a 100644--- a/cmake/PlaydenCore.cmake+++ b/cmake/PlaydenCore.cmake@@ -24,6 +24,7 @@ target_compile_definitions(playden_library PRIVATE UTF8PROC_STATIC)target_compile_features(playden_library PUBLIC cxx_std_17)add_library(playden_storage STATIC+ "${CMAKE_SOURCE_DIR}/src/platform/effective_uid.cpp""${CMAKE_SOURCE_DIR}/src/platform/storage.cpp""${CMAKE_SOURCE_DIR}/src/platform/durable_storage.cpp")target_include_directories(playden_storage PUBLIC "${CMAKE_SOURCE_DIR}/src")@@ -283,6 +284,9 @@ if(BUILD_TESTING AND NOT PLAYDEN_PLATFORM_PS5)add_executable(title_remover_test "${CMAKE_SOURCE_DIR}/tests/title_remover_test.cpp")target_link_libraries(title_remover_test PRIVATE playden_native_coordinator)add_test(NAME title_remover COMMAND title_remover_test)+ add_executable(private_storage_uid_test "${CMAKE_SOURCE_DIR}/tests/private_storage_uid_test.cpp")+ target_link_libraries(private_storage_uid_test PRIVATE playden_native_coordinator playden_storage Threads::Threads)+ add_test(NAME private_storage_uid COMMAND private_storage_uid_test)add_executable(title_allow_list_test "${CMAKE_SOURCE_DIR}/tests/title_allow_list_test.cpp")target_link_libraries(title_allow_list_test PRIVATE playden_native_coordinator)add_test(NAME title_allow_list COMMAND title_allow_list_test)diff --git a/src/platform/durable_storage.cpp b/src/platform/durable_storage.cppindex de513d8..c6cfd50 100644--- a/src/platform/durable_storage.cpp+++ b/src/platform/durable_storage.cpp@@ -1,4 +1,5 @@#include "platform/storage.h"+#include "platform/ps5/title_allow_list.h"#include "platform/managed_directory.h"#include <atomic>@@ -84,6 +85,7 @@ Result<void> Storage::WriteDurableExclusive(const std::string& relative_path,Result<void> Storage::WriteDurableImpl(const std::string& relative_path,const Bytes& bytes,bool replace_existing) const {+ const auto uid = ps5::HoldEffectiveUid();const auto resolved = Resolve(relative_path);if (!resolved.ok()) return Failure(resolved.error(), "Invalid journal path.");auto parent = Parent(root_, relative_path, traversable_root_);diff --git a/src/platform/effective_uid.cpp b/src/platform/effective_uid.cppnew file mode 100644index 0000000..5384264--- /dev/null+++ b/src/platform/effective_uid.cpp@@ -0,0 +1,28 @@+// The shared credential lock is also used by storage outside the runtime worker.+#include "platform/ps5/title_allow_list.h"+#include <mutex>+#include <unistd.h>++namespace playden::ps5 {++namespace {+std::recursive_mutex& RootLock() {+ static std::recursive_mutex lock;+ return lock;+}+} // namespace++bool WithRoot(const std::function<bool()>& work) {+ std::lock_guard<std::recursive_mutex> lock(RootLock());+ const uid_t user = ::geteuid();+ if (user == 0) return work();+ if (::seteuid(0) != 0) return false;+ const bool ok = work();+ return ::seteuid(user) == 0 && ok;+}++std::shared_ptr<void> HoldEffectiveUid() {+ return std::make_shared<std::unique_lock<std::recursive_mutex>>(RootLock());+}++} // namespace playden::ps5diff --git a/src/platform/ps5/title_allow_list.cpp b/src/platform/ps5/title_allow_list.cppindex cb22c5f..83c7d89 100644--- a/src/platform/ps5/title_allow_list.cpp+++ b/src/platform/ps5/title_allow_list.cpp@@ -35,6 +35,7 @@ bool Private(const struct stat& info, uid_t owner, mode_t mode) {bool WriteRootOwnedFile(const std::filesystem::path& directory, const std::string& name,const std::string& contents, const RootFileOps& ops, std::string* error) {+ const auto uid = HoldEffectiveUid();if (!ops.raise || !ops.lower || name.empty() || name.find('/') != std::string::npos)return Fail(error, "invalid request", EINVAL);const auto target = directory / name;@@ -77,29 +78,9 @@ bool WriteRootOwnedFile(const std::filesystem::path& directory, const std::strinreturn true;}-namespace {-std::recursive_mutex& RootLock() {- static std::recursive_mutex lock;- return lock;-}-} // namespace--bool WithRoot(const std::function<bool()>& work) {- std::lock_guard<std::recursive_mutex> lock(RootLock());- const uid_t user = ::geteuid();- if (user == 0) return work();- if (::seteuid(0) != 0) return false;- const bool ok = work();- return ::seteuid(user) == 0 && ok;-}--std::shared_ptr<void> HoldEffectiveUid() {- return std::make_shared<std::unique_lock<std::recursive_mutex>>(RootLock());-}-bool WriteTitleAllowList(const std::string& contents, std::string* error) {const std::filesystem::path list = runtime::kTitleRuleAllowList;- std::lock_guard<std::recursive_mutex> lock(RootLock());+ const auto uid = HoldEffectiveUid();const uid_t user = ::geteuid();RootFileOps ops;ops.raise = [] { return ::seteuid(0) == 0; };diff --git a/src/platform/storage.cpp b/src/platform/storage.cppindex 8fea818..60ece1d 100644--- a/src/platform/storage.cpp+++ b/src/platform/storage.cpp@@ -1,4 +1,5 @@#include "platform/storage.h"+#include "platform/ps5/title_allow_list.h"#include <cerrno>#include <cstdio>@@ -133,12 +134,18 @@ Result<void> Storage::WriteAtomic(const std::string& relative_path,Result<void> Storage::WriteAtomicPrivate(const std::string& relative_path,const Bytes& bytes) const {- return WriteAtomicImpl(relative_path, bytes, true);+ const auto uid = ps5::HoldEffectiveUid();+ for (unsigned attempt = 0; attempt < 3; ++attempt) {+ bool retry = false;+ auto written = WriteAtomicImpl(relative_path, bytes, true, &retry);+ if (!retry) return written;+ }+ return Result<void>::Failure(StorageError::kPermissionFailed, "Private storage owner changed repeatedly.");}Result<void> Storage::WriteAtomicImpl(const std::string& relative_path,const Bytes& bytes,- bool private_file) const {+ bool private_file, bool* retry) const {const auto resolved = Resolve(relative_path);if (!resolved.ok()) {return Result<void>::Failure(resolved.error(), resolved.message());@@ -148,7 +155,10 @@ Result<void> Storage::WriteAtomicImpl(const std::string& relative_path,errno = 0;std::FILE* file = nullptr;if (private_file) {- const int descriptor = open(temporary_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0600);+ // Discard a stale temp from an interrupted save, including one created during a root raise.+ if (::unlink(temporary_path.c_str()) != 0 && errno != ENOENT)+ return Result<void>::Failure(StorageError::kOpenFailed, "Private storage temporary file could not be removed.");+ const int descriptor = open(temporary_path.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);if (descriptor < 0) {return Result<void>::Failure(StorageError::kOpenFailed,"private storage open failed for '" + relative_path +@@ -216,6 +226,16 @@ Result<void> Storage::WriteAtomicImpl(const std::string& relative_path,StorageError::kWriteFailed,WriteErrorMessage("could not flush", relative_path, flush_error, private_file));}+ if (private_file) {+ struct stat status{};+ const bool inspected = ::fstat(fileno(file), &status) == 0;+ if (!inspected || status.st_uid != ::geteuid()) {+ std::fclose(file);+ const bool removed = ::unlink(temporary_path.c_str()) == 0;+ if (retry) *retry = inspected && removed;+ return Result<void>::Failure(StorageError::kPermissionFailed, "Private storage temporary owner is not the app user.");+ }+ }if (std::fclose(file) != 0) {const int close_error = errno;std::remove(temporary_path.c_str());diff --git a/src/platform/storage.h b/src/platform/storage.hindex 2b034fe..c3487d8 100644--- a/src/platform/storage.h+++ b/src/platform/storage.h@@ -130,7 +130,7 @@ class Storage {private:Result<std::string> Resolve(const std::string& relative_path) const;Result<void> WriteAtomicImpl(const std::string& relative_path,- const Bytes& bytes, bool private_file) const;+ const Bytes& bytes, bool private_file, bool* retry = nullptr) const;Result<void> WriteDurableImpl(const std::string& relative_path,const Bytes& bytes, bool replace_existing) const;diff --git a/tests/private_storage_uid_test.cpp b/tests/private_storage_uid_test.cppnew file mode 100644index 0000000..78fc4ff--- /dev/null+++ b/tests/private_storage_uid_test.cpp@@ -0,0 +1,67 @@+#include "platform/storage.h"+#include "platform/ps5/title_allow_list.h"+#include <atomic>+#include <chrono>+#include <cstdio>+#include <cstdlib>+#include <filesystem>+#include <future>+#include <dlfcn.h>+#include <sys/stat.h>+#include <unistd.h>++// Model the title's process-wide credentials without requiring host root.+static std::atomic<uid_t> effective_uid{getuid()};+extern "C" uid_t geteuid() { return effective_uid.load(); }+extern "C" int seteuid(uid_t user) { effective_uid.store(user); return 0; }+static std::atomic<bool> wrong_owner{false};+static std::atomic<unsigned> owner_retries{0};+extern "C" int fstat(int fd, struct stat* info) {+ using Stat = int (*)(int, struct stat*);+ static const auto real_stat = reinterpret_cast<Stat>(dlsym(RTLD_NEXT, "fstat"));+ const int result = real_stat(fd, info);+ if (result == 0 && info->st_size > 0 && wrong_owner.exchange(false)) {+ info->st_uid = getuid() + 1;+ ++owner_retries;+ }+ return result;+}+#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "%d: %s\n", __LINE__, #x); std::exit(1); } } while (0)++int main() {+ char scratch[] = "/tmp/playden-private-uid-XXXXXX";+ CHECK(mkdtemp(scratch));+ const auto root = std::filesystem::canonical(scratch);+ playden::Storage storage(root.string());+ std::promise<void> raised, release, saving;+ auto release_root = release.get_future();+ auto root_work = std::async(std::launch::async, [&] {+ return playden::ps5::WithRoot([&] {+ CHECK(geteuid() == 0);+ raised.set_value();+ release_root.wait();+ return true;+ });+ });+ raised.get_future().wait();+ auto save = std::async(std::launch::async, [&] {+ saving.set_value();+ return storage.WriteAtomicPrivate("session.v1", {'n', 'e', 'w'});+ });+ saving.get_future().wait();+ const bool blocked = save.wait_for(std::chrono::milliseconds(100)) == std::future_status::timeout;+ const bool absent = !std::filesystem::exists(root / "session.v1.tmp") && !std::filesystem::exists(root / "session.v1");+ release.set_value();+ CHECK(root_work.get());+ CHECK(save.get().ok());+ CHECK(blocked && absent && geteuid() == getuid());+ struct stat info{};+ CHECK(stat((root / "session.v1").c_str(), &info) == 0 && info.st_uid == geteuid() && (info.st_mode & 07777) == 0600);+ CHECK(storage.Read("session.v1").value() == playden::Bytes({'n', 'e', 'w'}));+ wrong_owner = true;+ CHECK(storage.WriteAtomicPrivate("session.v1", {'r', 'e', 't', 'r', 'y'}).ok());+ CHECK(owner_retries == 1 && !wrong_owner);+ CHECK(storage.Read("session.v1").value() == playden::Bytes({'r', 'e', 't', 'r', 'y'}));+ CHECK(!std::filesystem::exists(root / "session.v1.tmp"));+ std::filesystem::remove_all(root);+}