Play your Steam library's Windows games on a jailbroken PS5.
C++
Keep shared profiles inside their bottle
Leonardo Lobato063cf1262a22
diff --git a/cmake/PlaydenCore.cmake b/cmake/PlaydenCore.cmakeindex f85c1e6..7194348 100644--- a/cmake/PlaydenCore.cmake+++ b/cmake/PlaydenCore.cmake@@ -153,6 +153,7 @@ if(BUILD_TESTING AND NOT PLAYDEN_PLATFORM_PS5)add_executable(runtime_private_directories_native_policy_test"${CMAKE_SOURCE_DIR}/tests/runtime_private_directories_native_policy_test.cpp""${CMAKE_SOURCE_DIR}/src/runtime/private_directories.cpp"+ "${CMAKE_SOURCE_DIR}/tests/directory_gateway_host.cpp""${CMAKE_SOURCE_DIR}/src/platform/managed_directory.cpp")target_include_directories(runtime_private_directories_native_policy_test PRIVATE "${CMAKE_SOURCE_DIR}/src")target_compile_features(runtime_private_directories_native_policy_test PRIVATE cxx_std_17)@@ -191,6 +192,7 @@ if(BUILD_TESTING AND NOT PLAYDEN_PLATFORM_PS5)"${CMAKE_SOURCE_DIR}/tests/runtime_package_installer_test.cpp""${CMAKE_SOURCE_DIR}/src/runtime/package_installer.cpp""${CMAKE_SOURCE_DIR}/src/runtime/private_directories.cpp"+ "${CMAKE_SOURCE_DIR}/tests/directory_gateway_host.cpp""${CMAKE_SOURCE_DIR}/src/platform/managed_directory.cpp")target_compile_definitions(runtime_package_installer_native_policy_test PRIVATEPLAYDEN_PLATFORM_PS5=1 PLAYDEN_TEST_AT_UNAVAILABLE=1)diff --git a/src/platform/directory_walk.h b/src/platform/directory_walk.hindex 5aa3f16..2d652ae 100644--- a/src/platform/directory_walk.h+++ b/src/platform/directory_walk.h@@ -6,21 +6,21 @@#include <unistd.h>#include <sys/stat.h>-#if defined(__PROSPERO__)+#if defined(__PROSPERO__) || (defined(PLAYDEN_PLATFORM_PS5) && PLAYDEN_PLATFORM_PS5)extern "C" int __wrap_openat(int, const char*, int, ...);extern "C" int __wrap_mkdirat(int, const char*, mode_t);#endifnamespace playden::platform {inline int OpenWalkChild(int parent, const char* name, int flags) {-#if defined(__PROSPERO__)+#if defined(__PROSPERO__) || (defined(PLAYDEN_PLATFORM_PS5) && PLAYDEN_PLATFORM_PS5)return __wrap_openat(parent, name, flags);#elsereturn ::openat(parent, name, flags);#endif}inline int MakeWalkChild(int parent, const char* name, mode_t mode) {-#if defined(__PROSPERO__)+#if defined(__PROSPERO__) || (defined(PLAYDEN_PLATFORM_PS5) && PLAYDEN_PLATFORM_PS5)return __wrap_mkdirat(parent, name, mode);#elsereturn ::mkdirat(parent, name, mode);diff --git a/src/runtime/package_installer.cpp b/src/runtime/package_installer.cppindex 04bead8..a88d2a6 100644--- a/src/runtime/package_installer.cpp+++ b/src/runtime/package_installer.cpp@@ -893,6 +893,8 @@ PackageActionResult FilesystemPackageInstaller::SettleBottleModes(const RuntimeIif (!paths) return Failed("kit-invalid", "record or verified kit view is invalid");const bool shared = ProfileShared(record, *paths);const auto traversal = BottleTraversal(*paths, shared);+ if (shared && !SetTraversableMode(paths->game.parent_path(), 0711))+ return Failed("repair-mode", "the games profile parent could not be opened");if (!IsPrivateDirectoryTree(paths->game, paths->bottle, traversal))return Failed("repair-ownership", "the bottle is not this install's private directory");// A kit without bottle-profile-shared admits only 0700 there (after a kit downgrade too), and@@ -906,7 +908,7 @@ PackageActionResult FilesystemPackageInstaller::SettleBottleModes(const RuntimeIreturn Failed("repair-mode", "the permissions of " + folder.string() + " could not be set: " + strerror(errno));// The profile only matters to FTP: what can't be opened is logged, never a failure of the game.SharedTreeResult opened;- if (shared) opened = OpenTreeToEveryone(paths->bottle / "drive_c/users/prospero");+ if (shared) opened = OpenTreeToEveryone(paths->bottle / "drive_c/users/prospero", {operations_.private_profile_paths, {}});if (operations_.log)operations_.log("bottle modes install=" + record.install_id + " way=" + (shared ? "0711" : "0700") +(shared ? " profile_opened=" + std::to_string(opened.changed) + " skipped=" +@@ -1242,7 +1244,9 @@ PackageActionResult FilesystemPackageInstaller::CopyMappings(const bool profile_file = shared_profile &&std::mismatch(profile_relative.begin(), profile_relative.end(), relative.begin(), relative.end()).first ==profile_relative.end();- const unsigned mode = profile_file ? (0666 | (mapping.file->mode & 0111))+ const bool private_profile_file = profile_file &&+ IsPrivateProfilePath(relative.lexically_relative(profile_relative), operations_.private_profile_paths);+ const unsigned mode = private_profile_file ? 0600 : profile_file ? (0666 | (mapping.file->mode & 0111)): template_stage ? (0600 | (mapping.file->mode & 0100)) : mapping.file->mode;const int output = ::open(destination.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, mode);if (input < 0 || output < 0) {@@ -1325,7 +1329,7 @@ PackageActionResult FilesystemPackageInstaller::CopyMappings(current = std::move(next);}}- if (!OpenTreeToEveryone(user).ok)+ if (!OpenTreeToEveryone(user, {operations_.private_profile_paths, {}}).ok)return Failed("private-directory", "cannot open the bottle's user profile: " + user.string());}for (const char* folder : kProfileFolders) {@@ -1364,6 +1368,8 @@ PackageActionResult FilesystemPackageInstaller::CopyMappings(const auto game = paths_.host_data_root / "playden/games" / record.install_id;const PrivateTraversal parents{{game, game / "library", game / "library/prefixes"},static_cast<mode_t>(shared_profile ? 0711 : 0700)};+ if (shared_profile && !SetTraversableMode(game.parent_path(), 0711))+ return Failed("repair-mode", "the games profile parent could not be opened");if (!EnsurePrivateDirectories(paths_.host_data_root, game, final.parent_path(), &detail, parents))return Failed("private-directory", detail);} else {diff --git a/src/runtime/package_installer.h b/src/runtime/package_installer.hindex 569f7d4..229458d 100644--- a/src/runtime/package_installer.h+++ b/src/runtime/package_installer.h@@ -1,4 +1,5 @@#pragma once+#include "runtime/private_directories.h"#include "runtime/install_service.h"#include "runtime/private_directories.h"@@ -124,6 +125,7 @@ struct PublicationPaths {};struct PackageInstallerOperations {+ std::vector<std::filesystem::path> private_profile_paths = PrivateProfilePaths();std::function<std::optional<std::string>(const std::filesystem::path&)> hash_file;std::function<std::optional<std::string>(std::string_view)> hash_bytes;std::function<std::optional<std::uint64_t>(const std::filesystem::path&)> free_bytes;diff --git a/src/runtime/private_directories.cpp b/src/runtime/private_directories.cppindex 9309ace..96bc61c 100644--- a/src/runtime/private_directories.cpp+++ b/src/runtime/private_directories.cpp@@ -1,5 +1,6 @@#include "runtime/private_directories.h"#include "platform/managed_directory.h"+#include "platform/directory_walk.h"#include <cerrno>#include <cstring>#include <dirent.h>@@ -124,7 +125,7 @@ int OpenEntry(int folder, const struct stat& folder_info, const fs::path& folderconstexpr int kSharedTreeDepth = 64;constexpr std::size_t kSharedTreeEntries = 200000;struct Walk {- bool by_path;+ fs::path root;const SharedTreeOptions& options;std::size_t entries = 0;SharedTreeResult* result;@@ -136,8 +137,10 @@ bool ShareFolder(int fd, const fs::path& path, int depth, Walk& walk) {struct stat info{};if (::fstat(fd, &info) != 0 || !S_ISDIR(info.st_mode)) { ::close(fd); return false; }if (info.st_uid != ::geteuid()) { ::close(fd); ++result->skipped; return true; }- if ((info.st_mode & 07777) != 0777) {- if (::fchmod(fd, 0777) != 0) { ::close(fd); ++result->skipped; return true; }+ const bool private_folder = IsPrivateProfilePath(path.lexically_relative(walk.root), walk.options.private_paths);+ const mode_t folder_mode = private_folder ? 0700 : 0777;+ if ((info.st_mode & 07777) != folder_mode) {+ if (::fchmod(fd, folder_mode) != 0) { ::close(fd); ++result->skipped; return true; }++result->changed;}const int listing_fd = ::dup(fd);@@ -158,10 +161,9 @@ bool ShareFolder(int fd, const fs::path& path, int depth, Walk& walk) {if (entry->d_type == DT_LNK) continue;if (walk.options.before_open) walk.options.before_open(path / name);// O_NOFOLLOW: a link swapped in is refused, never followed.- bool stop = false;- const int child = OpenEntry(fd, info, path, name.c_str(), walk.by_path, 0, &stop);- if (stop) { result->changed_under = true; ok = false; break; }+ const int child = platform::OpenWalkChild(fd, name.c_str(), O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);if (child < 0) {+ if (errno == ENOSYS || errno == EINVAL) { ok = false; break; }if (errno != ELOOP && errno != EMLINK) ++result->skipped;continue;}@@ -177,7 +179,8 @@ bool ShareFolder(int fd, const fs::path& path, int depth, Walk& walk) {if (S_ISREG(opened.st_mode) && opened.st_nlink != 1) {++result->linked;} else if (S_ISREG(opened.st_mode) && opened.st_uid == ::geteuid()) {- const mode_t wanted = 0666 | (opened.st_mode & 0111);+ const mode_t wanted = IsPrivateProfilePath((path / name).lexically_relative(walk.root), walk.options.private_paths)+ ? 0600 : 0666 | (opened.st_mode & 0111);if ((opened.st_mode & 07777) != wanted) {if (::fchmod(child, wanted) == 0) ++result->changed;else ++result->skipped;@@ -224,35 +227,38 @@ int OpenChildFolderNoFollow(int parent_fd, const fs::path& parent_path, const st}bool SetTraversableMode(const fs::path& folder, mode_t mode) {if (mode != 0700 && mode != 0711) { errno = EINVAL; return false; }- struct stat before{};- if (::lstat(folder.c_str(), &before) != 0) return errno == ENOENT;- const auto current = before.st_mode & 07777;- if (!S_ISDIR(before.st_mode) || before.st_uid != ::geteuid() || (current != 0700 && current != 0711) ||- current == mode)- return true;- const int fd = ::open(folder.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);- if (fd < 0) return false;- struct stat opened{};- const bool same = ::fstat(fd, &opened) == 0 && opened.st_dev == before.st_dev && opened.st_ino == before.st_ino &&- opened.st_uid == ::geteuid();- const bool changed = same && ::fchmod(fd, mode) == 0 && ::fsync(fd) == 0;+ const int fd = platform::OpenDirectoryWalk(folder);+ if (fd < 0) return errno == ENOENT;+ struct stat info{};+ bool ok = ::fstat(fd, &info) == 0;+ const auto current = info.st_mode & 07777;+ if (ok && info.st_uid == ::geteuid() && (current == 0700 || current == 0711) && current != mode)+ ok = ::fchmod(fd, mode) == 0 && ::fsync(fd) == 0;const int error = errno;::close(fd);errno = error;- return changed;+ return ok;+}+const std::vector<fs::path>& PrivateProfilePaths() {+ // Main writes no launcher sign-in tokens inside drive_c/users/prospero yet. Add each+ // credential file or dedicated credential subtree here when its writer is introduced.+ static const std::vector<fs::path> paths;+ return paths;+}+bool IsPrivateProfilePath(const fs::path& relative, const std::vector<fs::path>& private_paths) {+ for (const auto& path : private_paths) {+ const auto within = relative.lexically_relative(path);+ if (within == "." || SafeRelative(within)) return true;+ }+ return false;}SharedTreeResult OpenTreeToEveryone(const fs::path& root, const SharedTreeOptions& options) {SharedTreeResult result;- struct stat before{};- if (::lstat(root.c_str(), &before) != 0) { result.ok = errno == ENOENT; return result; }- if (!S_ISDIR(before.st_mode) || before.st_uid != ::geteuid()) return result;- const int fd = ::open(root.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);- struct stat opened{};- if (fd < 0 || ::fstat(fd, &opened) != 0 || opened.st_dev != before.st_dev || opened.st_ino != before.st_ino) {- if (fd >= 0) ::close(fd);- return result;- }- Walk walk{options.by_path || !kDescriptorRelative, options, 0, &result};+ for (const auto& path : options.private_paths)+ if (!SafeRelative(path)) { errno = EINVAL; return result; }+ const int fd = platform::OpenDirectoryWalk(root);+ if (fd < 0) { result.ok = errno == ENOENT; return result; }+ Walk walk{root, options, 0, &result};result.ok = ShareFolder(fd, root, 0, walk);return result;}diff --git a/src/runtime/private_directories.h b/src/runtime/private_directories.hindex 111d7b0..c55a786 100644--- a/src/runtime/private_directories.h+++ b/src/runtime/private_directories.h@@ -37,9 +37,9 @@ bool SetTraversableMode(const std::filesystem::path& folder, mode_t mode);// Opens a tree to everyone (FTP runs as root, which the console gives no permission bypass):// app-owned folders become 0777, app-owned regular files 0666 with their execute bits kept.// Links are never followed, entries of other owners are neither changed nor entered, files-// with more than one link are left alone, and the walk is bounded. On the console (by path)-// each folder must still be the entry listed, in a folder its path still names; the walk ends-// where one changed. ok: the root is missing, or an owned folder that could be listed and+// with more than one link are left alone, and the walk is bounded. Every ancestor and child+// opens by descriptor without following links, including on the console via the syscall gateway.+// Listed credential subtrees stay 0700/0600. ok: the root is missing, or an owned folder that could be listed and// nothing changed under the walk; truncated: the walk stopped at its bound; skipped counts// entries that could not be opened or changed, linked the files with other links.struct SharedTreeResult {@@ -50,9 +50,14 @@ struct SharedTreeResult {std::size_t skipped = 0;std::size_t linked = 0;};-// Test seams: by_path walks the console's way on the host; before_open runs before each entry opens.+// Launcher credentials under the Windows profile. Empty until a launcher writes credentials+// here; game saves and emulator settings are not sign-in tokens.+const std::vector<std::filesystem::path>& PrivateProfilePaths();+bool IsPrivateProfilePath(const std::filesystem::path& relative,+ const std::vector<std::filesystem::path>& private_paths);+// before_open runs before each entry opens (race tests).struct SharedTreeOptions {- bool by_path = false;+ std::vector<std::filesystem::path> private_paths = PrivateProfilePaths();std::function<void(const std::filesystem::path&)> before_open;};SharedTreeResult OpenTreeToEveryone(const std::filesystem::path& root, const SharedTreeOptions& options = {});diff --git a/tests/directory_gateway_host.cpp b/tests/directory_gateway_host.cppnew file mode 100644index 0000000..ce40428--- /dev/null+++ b/tests/directory_gateway_host.cpp@@ -0,0 +1,14 @@+// Native-policy tests: direct *at imports fail; the separately named gateway works.+#include <dlfcn.h>+#include <fcntl.h>+#include <sys/stat.h>+extern "C" int __wrap_openat(int parent, const char* name, int flags, ...) {+ using OpenAt = int (*)(int, const char*, int, ...);+ static const auto open_at = reinterpret_cast<OpenAt>(dlsym(RTLD_NEXT, "openat"));+ return open_at(parent, name, flags);+}+extern "C" int __wrap_mkdirat(int parent, const char* name, mode_t mode) {+ using MkdirAt = int (*)(int, const char*, mode_t);+ static const auto mkdir_at = reinterpret_cast<MkdirAt>(dlsym(RTLD_NEXT, "mkdirat"));+ return mkdir_at(parent, name, mode);+}diff --git a/tests/runtime_package_installer_test.cpp b/tests/runtime_package_installer_test.cppindex 1f9ba45..b9261ba 100644--- a/tests/runtime_package_installer_test.cpp+++ b/tests/runtime_package_installer_test.cpp@@ -181,6 +181,7 @@ struct Fixture {bool legacy_descriptor = false;std::function<void(const std::filesystem::path&, const std::filesystem::path&, std::error_code&)> rename_path;std::vector<std::string> logs;+ std::vector<std::filesystem::path> private_profile_paths;Fixture() {Write(kit / "titles/variant/eboot.bin", "native", 0755);@@ -251,6 +252,7 @@ struct Fixture {FilesystemPackageInstaller Installer(bool inject_sync = true) {PackageInstallerOperations operations;+ operations.private_profile_paths = private_profile_paths;operations.rename_path = rename_path;operations.log = [&](const std::string& message) { logs.push_back(message); };operations.hash_file = TestHash;@@ -620,6 +622,12 @@ unsigned ModeOf(const std::filesystem::path& path) {// 0711 there whatever the kit; the other private folders stay 0700.void SharedProfileBottleModes() {Fixture fixture;+ fixture.private_profile_paths = {"AppData/Launcher", "token.txt"};+ for (const char* relative : {"AppData/Launcher/auth/token.txt", "token.txt"}) {+ const auto path = std::string("template/bottle/drive_c/users/prospero/") + relative;+ Write(fixture.kit / path, "token", 0644);+ fixture.view.files.push_back({path, 5, *TestHash(fixture.kit / path), 0644});+ }Write(fixture.kit / "template/bottle/drive_c/users/prospero/Documents/kit.ini", "kit", 0644);fixture.view.files.push_back({"template/bottle/drive_c/users/prospero/Documents/kit.ini", 3,*TestHash(fixture.kit / "template/bottle/drive_c/users/prospero/Documents/kit.ini"), 0644});@@ -639,7 +647,9 @@ void SharedProfileBottleModes() {CHECK(metadata.state == PackageActionState::kComplete);CHECK(ModeOf(game) == 0700 && ModeOf(game / "title-metadata") == 0700);CHECK(installer.StageTitle(record).state == PackageActionState::kComplete);+ CHECK(::chmod(game.parent_path().c_str(), 0700) == 0);CHECK(installer.CopyTemplate(record).state == PackageActionState::kComplete);+ CHECK(ModeOf(game.parent_path()) == 0711);for (const auto& path : {game, game / "library", game / "library/prefixes", bottle, bottle / "drive_c",bottle / "drive_c/users"})CHECK(ModeOf(path) == 0711);@@ -647,6 +657,7 @@ void SharedProfileBottleModes() {CHECK(ModeOf(bottle / "drive_c/windows/system.reg") == 0600);for (const char* folder : {"", "AppData", "AppData/LocalLow", "AppData/Roaming", "Documents", "Saved Games"})CHECK(ModeOf(profile / folder) == 0777);+ CHECK(ModeOf(profile / "token.txt") == 0600 && ModeOf(profile / "AppData/Launcher") == 0700);CHECK(ModeOf(profile / "Documents/kit.ini") == 0666 && Read(profile / "Documents/kit.ini") == "kit");CHECK(installer.ValidateArtifacts(record).state == PackageActionState::kComplete);CHECK(installer.Inspect(record).mutable_bottle_owned);@@ -662,6 +673,16 @@ void SharedProfileBottleModes() {for (const auto& path : {game / ".playden-control", game / "library/profiles", game / "library/input"})if (std::filesystem::exists(path)) CHECK(ModeOf(path) == 0700);+ // Credentials stay private through initial copy, repeat copy, settling and repair.+ for (unsigned pass = 0; pass < 3; ++pass) {+ CHECK(ModeOf(profile / "AppData/Launcher") == 0700);+ CHECK(ModeOf(profile / "AppData/Launcher/auth/token.txt") == 0600);+ CHECK(ModeOf(profile / "token.txt") == 0600);+ CHECK(installer.CopyTemplate(record).state == PackageActionState::kComplete);+ CHECK(installer.SettleBottleModes(record).state == PackageActionState::kComplete);+ CHECK(installer.RepairBottle(record).state == PackageActionState::kComplete);+ }+// Wine (umask 077) saves 0600 in 0700 folders; settling opens them again. Links are never// followed, a file with another link (a private one linked in) is left alone, and the// registry stays private.@@ -698,23 +719,34 @@ void SharedProfileBottleModes() {// A 0700 bottle (made before the capability) opens on Repair with the capable kit.CHECK(installer.RepairBottle(record).state == PackageActionState::kComplete);CHECK(ModeOf(game) == 0711 && ModeOf(bottle / "drive_c/users") == 0711);+ for (const auto& ancestor : {bottle / "drive_c/users", bottle / "drive_c"}) {+ const auto saved = ancestor.parent_path() / "saved-chain";+ std::filesystem::rename(ancestor, saved);+ std::filesystem::create_directory_symlink(saved, ancestor);+ CHECK(installer.SettleBottleModes(record).state == PackageActionState::kFailed);+ std::filesystem::remove(ancestor);+ std::filesystem::rename(saved, ancestor);+ }// Other modes on the way are not private: refused, never chmodded.CHECK(::chmod((game / "library").c_str(), 0755) == 0);CHECK(installer.SettleBottleModes(record).code == "repair-ownership");CHECK(ModeOf(game / "library") == 0755);}-// The console walks by path: the same profile opens, a hard link stays private, and a folder-// swapped for another mid-walk (its path names something else) ends the walk there.+// The profile walk keeps using the opened directory when its name is swapped.void SharedTreeWalksByPath() {TemporaryDirectory temporary;const auto root = temporary.path / "profile";+ Write(temporary.path / "outside/users/prospero/secret", "secret", 0600);+ std::filesystem::create_directory_symlink(temporary.path / "outside", temporary.path / "drive_c");+ CHECK(!OpenTreeToEveryone(temporary.path / "drive_c/users/prospero").ok);+ CHECK(ModeOf(temporary.path / "outside/users/prospero/secret") == 0600);Write(root / "AppData/Roaming/Game/save.dat", "save", 0600);Write(root / "Documents/notes.txt", "notes", 0600);Write(temporary.path / "private/secret", "secret", 0600);CHECK(::link((temporary.path / "private/secret").c_str(), (root / "Documents/secret").c_str()) == 0);SharedTreeOptions options;- options.by_path = true;+auto opened = OpenTreeToEveryone(root, options);CHECK(opened.ok && !opened.changed_under && opened.linked == 1);CHECK(ModeOf(root / "AppData/Roaming/Game") == 0777 && ModeOf(root / "AppData/Roaming/Game/save.dat") == 0666);@@ -732,7 +764,7 @@ void SharedTreeWalksByPath() {std::filesystem::rename(temporary.path / "outside", root / "Documents");};opened = OpenTreeToEveryone(root, options);- CHECK(swapped && !opened.ok && opened.changed_under);+ CHECK(swapped && opened.ok && !opened.changed_under);CHECK(ModeOf(root / "Documents") == 0700 && ModeOf(root / "Documents/inside.txt") == 0600);// Folder by folder (OpenGameFolders): a link component is refused, whatever it points at.