Play your Steam library's Windows games on a jailbroken PS5.
C++
Confine folder repairs and protect shared parents
Leonardo Lobato6ce067f7ac87
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 8246303..ec8683d 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -966,7 +966,8 @@ std::string RuntimeHost::EnsureWritable(const std::string& install_path, const sstd::none_of(relative.begin(), relative.end(), [](const auto& part) { return part == ".."; }))folders.push_back(HostPath(install_path) / relative);}- return rt::EnsureWritableFolders(folders, config_.as_root, [this](const std::string& line) { Log(line); });+ return rt::EnsureWritableFolders(folders, config_.as_root, [this](const std::string& line) { Log(line); },+ config_.host_namespace_prefix.empty() ? "/" : config_.host_namespace_prefix);}bool RuntimeHost::NeedsUnownedPublication(const Composition& c, const rt::RuntimeInstall& record) {diff --git a/src/platform/directory_walk.h b/src/platform/directory_walk.hnew file mode 100644index 0000000..5aa3f16--- /dev/null+++ b/src/platform/directory_walk.h@@ -0,0 +1,44 @@+#pragma once+// Descriptor walks used by permission repair; never fall back to privileged path operations.+#include <cerrno>+#include <filesystem>+#include <fcntl.h>+#include <unistd.h>+#include <sys/stat.h>++#if defined(__PROSPERO__)+extern "C" int __wrap_openat(int, const char*, int, ...);+extern "C" int __wrap_mkdirat(int, const char*, mode_t);+#endif++namespace playden::platform {+inline int OpenWalkChild(int parent, const char* name, int flags) {+#if defined(__PROSPERO__)+ return __wrap_openat(parent, name, flags);+#else+ return ::openat(parent, name, flags);+#endif+}+inline int MakeWalkChild(int parent, const char* name, mode_t mode) {+#if defined(__PROSPERO__)+ return __wrap_mkdirat(parent, name, mode);+#else+ return ::mkdirat(parent, name, mode);+#endif+}+// Starts at the filesystem root, opening each component without following links.+inline int OpenDirectoryWalk(const std::filesystem::path& path) {+ if (!path.is_absolute() || path.string().find('\0') != std::string::npos) { errno = EINVAL; return -1; }+ int fd = ::open("/", O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ for (const auto& part : path.relative_path()) {+ if (part.empty() || part == "." || part == "..") { ::close(fd); errno = EINVAL; return -1; }+ if (fd < 0) return -1;+ const int child = OpenWalkChild(fd, part.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ const int error = errno;+ ::close(fd);+ fd = child;+ errno = error;+ }+ return fd;+}+} // namespace playden::platformdiff --git a/src/platform/managed_directory.cpp b/src/platform/managed_directory.cppindex 6ca6976..8feb60e 100644--- a/src/platform/managed_directory.cpp+++ b/src/platform/managed_directory.cpp@@ -192,7 +192,8 @@ bool ManagedDirectory::InspectChild(const std::string& name, struct stat* resultstd::optional<ManagedDirectory> ManagedDirectory::OpenChild(const std::string& name, DirectoryPolicy policy, bool create, mode_t created_mode) const {if (!ValidName(name) || !Validate()) return std::nullopt;- if ((created_mode & ~0777) != 0 || (policy == DirectoryPolicy::kPrivate && created_mode != 0700) ||+ if ((created_mode & ~01777) != 0 ||+ ((created_mode & 01000) && policy != DirectoryPolicy::kApprovedTree) || (policy == DirectoryPolicy::kPrivate && created_mode != 0700) ||(policy == DirectoryPolicy::kPrivateTraversable && created_mode != 0700 && created_mode != 0711) ||(policy_ == DirectoryPolicy::kPrivate && policy != DirectoryPolicy::kPrivate) ||(policy_ == DirectoryPolicy::kPrivateTraversable && !PrivateFamily(policy))) {diff --git a/src/platform/ps5/native_access.cpp b/src/platform/ps5/native_access.cppindex d95c708..3e23c68 100644--- a/src/platform/ps5/native_access.cpp+++ b/src/platform/ps5/native_access.cpp@@ -278,6 +278,9 @@ NativeAccessResult NativeAccessBootstrap::PrepareAccess() {}runtime_error.clear();+ if (ops_.prepare_runtime_root &&+ !ops_.prepare_runtime_root(ops_.context, options_.managed_runtime_root.c_str(), &runtime_error))+ return granted_fallback(runtime_error);if (!ValidateRuntimeData(ops_, options_, &runtime_error))return granted_fallback(runtime_error);diff --git a/src/platform/ps5/native_access.h b/src/platform/ps5/native_access.hindex a584a8a..4c1e4b2 100644--- a/src/platform/ps5/native_access.h+++ b/src/platform/ps5/native_access.h@@ -27,6 +27,7 @@ struct NativeAccessOps {NativePathKind (*path_kind)(void*, const char*) = nullptr;bool (*path_accessible)(void*, const char*, NativePathAccess) = nullptr;bool (*read_source_identity)(void*, const char*, std::string*) = nullptr;+ bool (*prepare_runtime_root)(void*, const char*, std::string*) = nullptr;};struct NativeAccessOptions {diff --git a/src/platform/ps5/native_startup.cpp b/src/platform/ps5/native_startup.cppindex 80f0dbe..950cbe5 100644--- a/src/platform/ps5/native_startup.cpp+++ b/src/platform/ps5/native_startup.cpp@@ -3,6 +3,7 @@#include "platform/ps5/title_activity.h"#include "platform/ps5/title_allow_list.h"#include "runtime/layout_migration.h"+#include "runtime/writable_folders.h"#include <EGL/egl.h>#include "steam/backend/native_network.h"#include "app/diagnostic_log.h"@@ -149,7 +150,14 @@ NativeAccessResult PrepareNativeStartup(const std::string& title_id) {options.require_grant = true;const auto ops = filesystem.Ops();ops.read_source_identity(ops.context, "/app0", &context.source_identity);- NativeAccessBootstrap bootstrap(filesystem.Ops(), options);+ auto startup_ops = filesystem.Ops();+ startup_ops.prepare_runtime_root = [](void*, const char* root, std::string* error) {+ *error = runtime::PrepareRuntimeRoot(root, WithRoot, [](const std::string& line) {+ app::AppendLogLine("/app0/logs", "native-startup.log", line);+ });+ return error->empty();+ };+ NativeAccessBootstrap bootstrap(startup_ops, options);auto result = bootstrap.Prepare();if (!result.runtime_ready && !context.grant_diagnostic.empty())result.error += "; " + context.grant_diagnostic;diff --git a/src/runtime/layout_migration.cpp b/src/runtime/layout_migration.cppindex f6dfaa5..1f905f5 100644--- a/src/runtime/layout_migration.cpp+++ b/src/runtime/layout_migration.cpp@@ -2,6 +2,7 @@#include "runtime/game_install.h"#include "runtime/private_directories.h"#include "platform/storage.h"+#include "platform/directory_walk.h"#include "steam/backend/json.h"#include <algorithm>@@ -104,13 +105,24 @@ bool EmptyDirectory(const fs::path& directory) {return names && names->empty();}-// A shared (0777) folder, made as root when the app user can't create it there.-bool EnsureSharedDirectory(const LayoutOps& ops, const fs::path& directory) {+// Shared parents are sticky; the games root stays non-sticky for imported root-owned games.+bool EnsureSharedDirectory(const LayoutOps& ops, const fs::path& directory, mode_t mode = 01777) {const int kind = Kind(directory);if (kind == 1) return true;if (kind == 2) return false;const auto make = [&] {- return (::mkdir(directory.c_str(), 0777) == 0 || Kind(directory) == 1) && ::chmod(directory.c_str(), 0777) == 0;+ const int parent = platform::OpenDirectoryWalk(directory.parent_path());+ if (parent < 0) return false;+ const auto name = directory.filename().string();+ const bool made = platform::MakeWalkChild(parent, name.c_str(), mode) == 0;+ const int fd = made ? platform::OpenWalkChild(parent, name.c_str(),+ O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC) : -1;+ const bool ok = fd >= 0 && ::fchmod(fd, mode) == 0;+ const int error = errno;+ if (fd >= 0) ::close(fd);+ ::close(parent);+ errno = error;+ return ok;};if (make()) return true;return (errno == EACCES || errno == EPERM) && AsRoot(ops, make) && Kind(directory) == 1;@@ -353,7 +365,7 @@ LayoutResult MigrateLayout(const LayoutPaths& paths, const LayoutOps& ops) {// Once the games step is done the games folder exists, shared: a fresh install has// none to move. One that came with the moved games keeps its mode.const auto ensure_games_root = [&] {- if (Kind(paths.games_root) == 0 && !EnsureSharedDirectory(ops, paths.games_root))+ if (Kind(paths.games_root) == 0 && !EnsureSharedDirectory(ops, paths.games_root, 0777))Log(ops, paths.games_root.string() + " can't be created: " + std::strerror(errno));};const Step initial = StepIndex(journal.step);diff --git a/src/runtime/package_installer.cpp b/src/runtime/package_installer.cppindex b0b11d7..04bead8 100644--- a/src/runtime/package_installer.cpp+++ b/src/runtime/package_installer.cpp@@ -1148,8 +1148,8 @@ PackageActionResult FilesystemPackageInstaller::CopyMappings(if (staging_parent_relative.empty() ||!PathComponentsHaveNoSymlink(paths_.host_data_root, staging_parent_relative, true))return Failed("staging-unavailable", "owned staging root is unavailable");- // /data/playden and its staging folder are shared parents: 0777 when created here.- CreateDirectoriesWithMode(staging.parent_path(), 0777, error);+ // /data/playden and its staging folder are shared parents: 01777 when created here.+ CreateDirectoriesWithMode(staging.parent_path(), 01777, error);if (error)return Failed("staging-unavailable", "owned staging root is unavailable");if (!SyncAncestors(staging.parent_path()))diff --git a/src/runtime/private_directories.cpp b/src/runtime/private_directories.cppindex b67b1e5..9309ace 100644--- a/src/runtime/private_directories.cpp+++ b/src/runtime/private_directories.cpp@@ -70,10 +70,10 @@ bool EnsurePrivateDirectories(const fs::path& boundary, const fs::path& private_for (const auto& part : relative) {path /= part;if (path == private_root) in_private = true;- // Shared parents it creates above the private root are 0777, so FTP and other+ // Shared parents it creates above the private root are 01777, so FTP and other// apps can write beside them; existing ones keep their mode.const auto policy = PolicyFor(traversal, path);- auto next = !in_private ? current->OpenChild(part.string(), DirectoryPolicy::kApprovedTree, true, 0777)+ auto next = !in_private ? current->OpenChild(part.string(), DirectoryPolicy::kApprovedTree, true, 01777): policy == DirectoryPolicy::kPrivateTraversable? current->OpenChild(part.string(), policy, true, traversal.created_mode): current->OpenChild(part.string(), policy, true);diff --git a/src/runtime/private_directories.h b/src/runtime/private_directories.hindex 8c57ab4..111d7b0 100644--- a/src/runtime/private_directories.h+++ b/src/runtime/private_directories.h@@ -19,7 +19,7 @@ struct PrivateTraversal {// belong to that user and have mode 0700 (or 0711 for traversal folders).// Existing foreign/public directories in the private tree are rejected, never// chmodded or taken over. Missing folders between boundary and private_root are-// created 0777.+// created 01777.bool EnsurePrivateDirectories(const std::filesystem::path& boundary,const std::filesystem::path& private_root,const std::filesystem::path& directory,diff --git a/src/runtime/writable_folders.cpp b/src/runtime/writable_folders.cppindex f86ed49..fd501c2 100644--- a/src/runtime/writable_folders.cpp+++ b/src/runtime/writable_folders.cpp@@ -2,6 +2,7 @@// Copyright (c) 2026 Playden contributors// SPDX-License-Identifier: MIT#include "runtime/writable_folders.h"+#include "platform/directory_walk.h"#include <atomic>#include <cerrno>@@ -31,12 +32,22 @@ int Probe(const std::filesystem::path& folder) {return EEXIST;}+mode_t RepairMode(const std::filesystem::path& folder, const std::filesystem::path& namespace_root) {+ const auto relative = folder.lexically_relative(namespace_root).generic_string();+ if (relative == "data/playden" || relative == "data/homebrew") return 01777;+ if (relative == "data/playden-games") return 0777;+ for (const auto* volume : {"ext0", "ext1", "usb0", "usb1"})+ if (relative == std::string("mnt/") + volume + "/playden/games") return 0777;+ return 0;+}+std::string Error(int error) { return std::to_string(error) + " (" + std::strerror(error) + ")"; }} // namespacestd::string EnsureWritableFolders(const std::vector<std::filesystem::path>& folders,const WritableFoldersAsRoot& as_root,- const std::function<void(const std::string&)>& log) {+ const std::function<void(const std::string&)>& log,+ const std::filesystem::path& namespace_root) {const auto note = [&log](const std::string& line) { if (log) log(line); };for (const auto& folder : folders) {if (folder.empty()) continue;@@ -48,31 +59,37 @@ std::string EnsureWritableFolders(const std::vector<std::filesystem::path>& fold}if (!S_ISDIR(info.st_mode)) continue;const int first = Probe(folder);- if (first == 0) continue;+ const mode_t mode = RepairMode(folder, namespace_root);+ if (first == 0 && (!mode || (info.st_mode & 07777) == mode)) continue;int fix_error = 0;bool ran = false;- const bool fixed = as_root && as_root([&] {+ const auto repair = [&] {ran = true;// By descriptor: a link swapped in after the lstat is refused, never followed as root.- const int fd = open(folder.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ const int fd = platform::OpenDirectoryWalk(folder);if (fd < 0) { fix_error = errno; return false; }struct stat current{};bool ok = fstat(fd, ¤t) == 0 || (fix_error = errno, false);if (ok && !S_ISDIR(current.st_mode)) ok = (fix_error = ENOTDIR, false);- if (ok && fchmod(fd, 0777) != 0) ok = (fix_error = errno, false);+ if (ok && !mode && current.st_uid != geteuid()) ok = (fix_error = EPERM, false);+ if (ok && fchmod(fd, mode ? mode : 0777) != 0) ok = (fix_error = errno, false);close(fd);return ok;- });+ };+ // Arbitrary game/working folders may only be repaired by their own app user.+ const bool fixed = mode ? as_root && as_root(repair) : repair();const int second = fixed ? Probe(folder) : first;if (fixed && second == 0) {- char mode[8]; std::snprintf(mode, sizeof(mode), "%04o", unsigned(info.st_mode & 07777));- note("writable folder fixed path=" + folder.string() + " mode=" + mode + " -> 0777 uid=" ++ char before[8], after[8];+ std::snprintf(before, sizeof(before), "%04o", unsigned(info.st_mode & 07777));+ std::snprintf(after, sizeof(after), "%04o", unsigned(mode ? mode : 0777));+ note("writable folder fixed path=" + folder.string() + " mode=" + before + " -> " + after + " uid=" +std::to_string(info.st_uid) + " probe=" + Error(first));continue;}note("writable folder failed path=" + folder.string() + " uid=" + std::to_string(info.st_uid) +" probe=" + Error(first) +- (!as_root ? " fix=unavailable"+ (!mode ? " fix=outside-approved-roots" : !as_root ? " fix=unavailable": !ran ? " fix=root-refused": fixed ? " after-fix=" + Error(second) : " chmod=" + Error(fix_error)));return "Playden can't write to " + folder.string() +@@ -81,4 +98,28 @@ std::string EnsureWritableFolders(const std::vector<std::filesystem::path>& foldreturn {};}+std::string PrepareRuntimeRoot(const std::filesystem::path& root, const WritableFoldersAsRoot& as_root,+ const std::function<void(const std::string&)>& log,+ const std::filesystem::path& namespace_root) {+ if (root != namespace_root / "data/playden") return "Playden can't fix this runtime folder.";+ const bool prepared = as_root && as_root([&] {+ const int parent = platform::OpenDirectoryWalk(root.parent_path());+ if (parent < 0) return false;+ int fd = platform::OpenWalkChild(parent, "playden", O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ if (fd < 0 && errno == ENOENT &&+ (platform::MakeWalkChild(parent, "playden", 01777) == 0 || errno == EEXIST))+ fd = platform::OpenWalkChild(parent, "playden", O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ const bool ok = fd >= 0 && ::fchmod(fd, 01777) == 0 && ::fsync(fd) == 0 && ::fsync(parent) == 0;+ const int error = errno;+ if (fd >= 0) ::close(fd);+ ::close(parent);+ errno = error;+ return ok;+ });+ if (prepared) return {};+ if (log) log("runtime root repair failed path=" + root.string() + " error=" + Error(errno));+ return "Playden can't write to " + root.string() ++ ". Make the folder writable over FTP (permissions 1777), then try again.";+}+} // namespace playden::runtimediff --git a/src/runtime/writable_folders.h b/src/runtime/writable_folders.hindex af684d0..307b1c3 100644--- a/src/runtime/writable_folders.h+++ b/src/runtime/writable_folders.h@@ -15,11 +15,17 @@ using WritableFoldersAsRoot = std::function<bool(const std::function<bool()>& wo// Folders made over FTP belong to root and are often 0755, so the app user can't write into them.// For each existing folder (missing ones and links are skipped), a probe file is created and-// removed as the app user. When that fails, the folder is made 0777 as root (root owns it, so no+// removed as the app user. Only fixed, approved shared roots may be repaired as root (root owns them, so no// permission bypass is needed) and probed again. Returns an empty string when every folder can be// written, or a message naming the first folder that still can't. Each fix and failure is logged.std::string EnsureWritableFolders(const std::vector<std::filesystem::path>& folders,const WritableFoldersAsRoot& as_root,- const std::function<void(const std::string&)>& log);+ const std::function<void(const std::string&)>& log,+ const std::filesystem::path& namespace_root = "/");++// Single-threaded startup, after the grant and before the first runtime write check.+std::string PrepareRuntimeRoot(const std::filesystem::path& root, const WritableFoldersAsRoot& as_root,+ const std::function<void(const std::string&)>& log,+ const std::filesystem::path& namespace_root = "/");} // namespace playden::runtimediff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 4549d96..125856c 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -576,7 +576,7 @@ void PrepareFixesRootOwnedHomebrewFolder() {CHECK(done.installs.size() == 1 && done.installs[0].state == PreparationState::kReady);CHECK(raises == 1); // only the folder that failed its probestruct stat info{};- CHECK(stat(homebrew.c_str(), &info) == 0 && (info.st_mode & 07777) == 0777);+ CHECK(stat(homebrew.c_str(), &info) == 0 && (info.st_mode & 07777) == 01777);CHECK(fs::is_regular_file(fixture.root / "data/homebrew/FAKE78001/game-launch.json"));host.Shutdown();}diff --git a/tests/layout_migration_test.cpp b/tests/layout_migration_test.cppindex e215bd8..cc2329a 100644--- a/tests/layout_migration_test.cpp+++ b/tests/layout_migration_test.cpp@@ -105,7 +105,7 @@ void MovesUserDataLogsAndGames() {CHECK(result.user_root == f->paths.user_root && result.log_root == f->paths.log_root &&result.games_root == f->paths.games_root);CHECK(Moved(f->paths));- CHECK(Mode(f->paths.user_root) == 0777 && Mode(f->paths.log_root) == 0777);+ CHECK(Mode(f->paths.user_root) == 01777 && Mode(f->paths.log_root) == 01777);CHECK(Mode(f->paths.user_root / "session.v1") == 0600);CHECK(Mode(f->paths.user_root / "game-settings-private") == 0700);CHECK(Read(f->paths.user_root / "session.v1") == "credentials");diff --git a/tests/native_access_test.cpp b/tests/native_access_test.cppindex cf5b911..ffba220 100644--- a/tests/native_access_test.cpp+++ b/tests/native_access_test.cpp@@ -503,7 +503,36 @@ void WritableManagedParentAllowsFirstRuntimeDirectoryCreation() {} // namespace+void RuntimeRepairRunsBetweenGrantAndWriteCheck() {+ for (bool missing : {false, true}) {+ FakeNative fake;+ AddTree(fake, kAppRoot);+ fake.identities[kAppRoot] = "playden-package-v1";+ fake.expose_runtime_after_grant = !missing;+ fake.read_only.insert(kRuntimeRoot);+ auto ops = Ops(fake);+ ops.prepare_runtime_root = [](void* context, const char* root, std::string*) {+ auto& state = *static_cast<FakeNative*>(context);+ CHECK(state.grants == 1);+ CHECK(std::find(state.calls.begin(), state.calls.end(), "write:/data/playden") == state.calls.end());+ state.paths[root] = NativePathKind::kDirectory;+ state.read_only.erase(root);+ state.calls.push_back("repair");+ return true;+ };+ NativeAccessOptions options;+ options.title_id = "FAKE77031";+ options.require_grant = true;+ options.user_service_after_grant = true;+ NativeAccessBootstrap bootstrap(ops, options);+ CHECK(bootstrap.Prepare().runtime_ready);+ CHECK(std::find(fake.calls.begin(), fake.calls.end(), "repair") <+ std::find(fake.calls.begin(), fake.calls.end(), "user"));+ }+}+int main() {+ RuntimeRepairRunsBetweenGrantAndWriteCheck();DeferredUserServiceRunsAfterGrantAndBeforeReturning();PathsRemainValidAfterGrant();GrantFailureDisablesRuntimeOnly();diff --git a/tests/runtime_package_installer_test.cpp b/tests/runtime_package_installer_test.cppindex e4b5027..1f9ba45 100644--- a/tests/runtime_package_installer_test.cpp+++ b/tests/runtime_package_installer_test.cpp@@ -1001,7 +1001,7 @@ void PrivateTitleFoldersAreRepublished() {}// Folders the installer creates get an explicit mode whatever the umask: the-// shared staging parents are 0777, the title stays 0755.+// shared staging parents are 01777, the title stays 0755.void CreatedFoldersIgnoreUmask() {for (const mode_t mask : {mode_t{0}, mode_t{077}}) {const auto previous = ::umask(mask);@@ -1011,7 +1011,7 @@ void CreatedFoldersIgnoreUmask() {::umask(previous);struct stat info{};for (const auto* shared : {"playden", "playden/staging"})- CHECK(::stat((fixture.host_data / shared).c_str(), &info) == 0 && (info.st_mode & 07777) == 0777);+ CHECK(::stat((fixture.host_data / shared).c_str(), &info) == 0 && (info.st_mode & 07777) == 01777);CHECK(::stat((fixture.host_data / "homebrew/FAKE78001").c_str(), &info) == 0 && (info.st_mode & 07777) == 0755);}}diff --git a/tests/runtime_private_directories_native_policy_test.cpp b/tests/runtime_private_directories_native_policy_test.cppindex 0ef307b..240dbe1 100644--- a/tests/runtime_private_directories_native_policy_test.cpp+++ b/tests/runtime_private_directories_native_policy_test.cpp@@ -13,24 +13,24 @@ extern "C" int mkdirat(int, const char*, mode_t) { errno = EINVAL; return -1; }namespace fs = std::filesystem;-// Folders created above the private root are shared (0777) whatever the umask;+// Folders created above the private root are shared (01777) whatever the umask;// existing ones keep their mode, and the private tree stays 0700.void CreatedPublicAncestorsAreShared(const fs::path& boundary) {const auto mode = [](const fs::path& path) {struct stat info{};assert(stat(path.c_str(), &info) == 0);- return info.st_mode & 07777;+ return info.st_mode & 017777;};const auto previous = umask(022);std::string error;const auto root = boundary / "shared/parent/install";assert(playden::runtime::EnsurePrivateDirectories(boundary, root, root / "child", &error));- assert(mode(boundary / "shared") == 0777 && mode(boundary / "shared/parent") == 0777);+ assert(mode(boundary / "shared") == 01777 && mode(boundary / "shared/parent") == 01777);assert(mode(root) == 0700 && mode(root / "child") == 0700);assert(fs::create_directory(boundary / "existing") && chmod((boundary / "existing").c_str(), 0755) == 0);const auto kept = boundary / "existing/new/install";assert(playden::runtime::EnsurePrivateDirectories(boundary, kept, kept, &error));- assert(mode(boundary / "existing") == 0755 && mode(boundary / "existing/new") == 0777 && mode(kept) == 0700);+ assert(mode(boundary / "existing") == 0755 && mode(boundary / "existing/new") == 01777 && mode(kept) == 0700);// Opening again finds the recorded modes unchanged.assert(playden::runtime::EnsurePrivateDirectories(boundary, kept, kept / "child", &error));umask(previous);@@ -45,7 +45,7 @@ void TraversalChainAdmits0711(const fs::path& boundary) {const auto mode = [](const fs::path& path) {struct stat info{};assert(stat(path.c_str(), &info) == 0);- return info.st_mode & 07777;+ return info.st_mode & 017777;};const auto previous = umask(077);const auto game = boundary / "playden/games/install-a";@@ -55,7 +55,7 @@ void TraversalChainAdmits0711(const fs::path& boundary) {assert(EnsurePrivateDirectories(boundary, game, bottle / "drive_c/users", &error, traversal));assert(EnsurePrivateDirectories(boundary, game, game / "library/profiles", &error, traversal));assert(EnsurePrivateDirectories(boundary, game, game / ".playden-control", &error, traversal));- assert(mode(boundary / "playden") == 0777 && mode(boundary / "playden/games") == 0777);+ assert(mode(boundary / "playden") == 01777 && mode(boundary / "playden/games") == 01777);for (const auto& folder : traversal.folders) assert(mode(folder) == 0711);assert(mode(bottle / "drive_c") == 0700 && mode(bottle / "drive_c/users") == 0700);assert(mode(game / "library/profiles") == 0700 && mode(game / ".playden-control") == 0700);diff --git a/tests/runtime_writable_folders_test.cpp b/tests/runtime_writable_folders_test.cppindex 0d62b6f..4d6925b 100644--- a/tests/runtime_writable_folders_test.cpp+++ b/tests/runtime_writable_folders_test.cpp@@ -25,7 +25,7 @@ int main() {char name[] = "/tmp/playden-writable-XXXXXX";CHECK(mkdtemp(name));const fs::path root = fs::canonical(name);- const auto open = root / "open", locked = root / "locked", target = root / "target", link = root / "link";+ const auto open = root / "open", locked = root / "data/playden-games", target = root / "target", link = root / "link";fs::create_directories(open);fs::create_directories(locked);fs::create_directories(target);@@ -37,27 +37,40 @@ int main() {const WritableFoldersAsRoot refuses = [&calls](const std::function<bool()>&) { ++calls; return false; };// Writable, missing and linked folders need nothing; the probe leaves no file behind.- CHECK(EnsureWritableFolders({open, root / "missing", link, {}}, runs, log).empty());+ CHECK(EnsureWritableFolders({open, root / "missing", link, {}}, runs, log, root).empty());CHECK(calls == 0 && lines.empty() && Empty(open));if (geteuid() != 0) {// A linked folder is never followed (nor fixed), even when its target can't be written.CHECK(chmod(target.c_str(), 0555) == 0);- CHECK(EnsureWritableFolders({link}, runs, log).empty());+ CHECK(EnsureWritableFolders({link}, runs, log, root).empty());CHECK(calls == 0 && Mode(target) == 0555);CHECK(chmod(target.c_str(), 0755) == 0);+ // O_NOFOLLOW on the final folder must also reject linked ancestors.+ fs::create_directories(root / "mnt/usb1");+ fs::create_directory_symlink(target, root / "mnt/usb1/playden");+ fs::create_directories(target / "games");+ CHECK(chmod((target / "games").c_str(), 0555) == 0);+ CHECK(!EnsureWritableFolders({root / "mnt/usb1/playden/games"}, runs, log, root).empty());+ CHECK(Mode(target / "games") == 0555);+ CHECK(chmod((target / "games").c_str(), 0755) == 0);+ lines.clear();+ calls = 0;+// A link swapped in between the probe and the fix is refused, its target left alone.- const auto swapped = root / "swapped";+ const auto swapped = root / "mnt/usb0/playden/games";fs::create_directories(swapped);CHECK(chmod(swapped.c_str(), 0555) == 0 && chmod(target.c_str(), 0555) == 0);const WritableFoldersAsRoot swaps = [&](const std::function<bool()>& work) {++calls;+ CHECK(chmod(swapped.c_str(), 0755) == 0);fs::rename(swapped, root / "swapped-away");+ CHECK(chmod((root / "swapped-away").c_str(), 0555) == 0);fs::create_directory_symlink(target, swapped);return work();};- CHECK(!EnsureWritableFolders({swapped}, swaps, log).empty());+ CHECK(!EnsureWritableFolders({swapped}, swaps, log, root).empty());CHECK(calls == 1 && Mode(target) == 0555 && Mode(root / "swapped-away") == 0555);CHECK(lines.size() == 1 && lines[0].find("chmod=") != std::string::npos);CHECK(chmod(target.c_str(), 0755) == 0 && chmod((root / "swapped-away").c_str(), 0755) == 0);@@ -66,13 +79,13 @@ int main() {// No way to raise root: the folder is named.CHECK(chmod(locked.c_str(), 0555) == 0);- auto message = EnsureWritableFolders({open, locked}, {}, log);+ auto message = EnsureWritableFolders({open, locked}, {}, log, root);CHECK(message.find("can't write to " + locked.string() + ".") != std::string::npos);CHECK(Mode(locked) == 0555 && lines.size() == 1 && lines[0].find("fix=unavailable") != std::string::npos);// Root refused.lines.clear();- message = EnsureWritableFolders({locked}, refuses, log);+ message = EnsureWritableFolders({locked}, refuses, log, root);CHECK(calls == 1 && message.find(locked.string()) != std::string::npos && Mode(locked) == 0555);CHECK(lines.size() == 1 && lines[0].find("fix=root-refused") != std::string::npos);@@ -85,20 +98,34 @@ int main() {chmod(locked.c_str(), 0555);return ok;};- message = EnsureWritableFolders({locked}, undone, log);+ message = EnsureWritableFolders({locked}, undone, log, root);CHECK(calls == 1 && message.find(locked.string()) != std::string::npos);CHECK(lines.size() == 1 && lines[0].find("after-fix=") != std::string::npos);// Fixed: 0777, logged, then writable without another raise.lines.clear();calls = 0;- CHECK(EnsureWritableFolders({open, locked}, runs, log).empty());+ CHECK(EnsureWritableFolders({open, locked}, runs, log, root).empty());CHECK(calls == 1 && Mode(locked) == 0777 && Empty(locked));CHECK(lines.size() == 1 && lines[0].find("writable folder fixed") != std::string::npos &&lines[0].find("mode=0555 -> 0777") != std::string::npos);- CHECK(EnsureWritableFolders({locked}, runs, log).empty() && calls == 1);+ CHECK(EnsureWritableFolders({locked}, runs, log, root).empty() && calls == 1);}+ // A fresh /data and an FTP-created 0755 runtime root both become sticky.+ CHECK(PrepareRuntimeRoot(root / "data/playden", runs, log, root).empty());+ CHECK(Mode(root / "data/playden") == 01777);+ CHECK(chmod((root / "data/playden").c_str(), 0755) == 0);+ CHECK(PrepareRuntimeRoot(root / "data/playden", runs, log, root).empty());+ CHECK(Mode(root / "data/playden") == 01777 && Mode(locked) == 0777);+ CHECK(chmod((root / "data/playden").c_str(), 0777) == 0);+ CHECK(EnsureWritableFolders({root / "data/playden"}, runs, log, root).empty());+ CHECK(Mode(root / "data/playden") == 01777);+ CHECK(chmod(target.c_str(), 0555) == 0);+ const auto before_calls = calls;+ CHECK(EnsureWritableFolders({target}, runs, log, root).empty());+ CHECK(Mode(target) == 0777 && calls == before_calls);+ CHECK(chmod(target.c_str(), 0755) == 0);std::error_code ignored;fs::remove_all(root, ignored);std::puts("writable folders tests passed");