Play your Steam library's Windows games on a jailbroken PS5.
C++
Walk folders by path on the PS5 so 0.6.0 starts
Leonardo Lobato2d62a7b73273
diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cppindex 67a1dfe..3ce51ed 100644--- a/src/homebrew/simulation.cpp+++ b/src/homebrew/simulation.cpp@@ -1021,9 +1021,9 @@ void Simulation::ShowLayoutNotice(const std::string& text) {void Simulation::ShowRootAccessMissing(const std::string& raw, bool granted) {if (!live()) return;- // One short line: the first failure and the helper's diagnostic are enough to report. The- // unused fallback launcher's path check is left out.- constexpr std::size_t kDetailLimit = 140;+ // The whole failure and the helper's diagnostic, which the screen wraps: they are what the+ // player reports. Only a runaway message is cut. The unused fallback launcher's path check is left out.+ constexpr std::size_t kDetailLimit = 1000;std::string detail = raw;if (const auto from = detail.find("; launcher paths unavailable: "); from != std::string::npos) {const auto to = detail.find("; ", from + 2);diff --git a/src/platform/directory_walk.h b/src/platform/directory_walk.hindex 2d652ae..a971f50 100644--- a/src/platform/directory_walk.h+++ b/src/platform/directory_walk.h@@ -1,35 +1,136 @@#pragma once// Descriptor walks used by permission repair; never fall back to privileged path operations.#include <cerrno>+#include <cstdint>#include <filesystem>+#include <mutex>+#include <string>+#include <unordered_map>+#include <dirent.h>#include <fcntl.h>#include <unistd.h>#include <sys/stat.h>#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);+// A PS5 title can't open relative to a directory descriptor: its openat/mkdirat take only+// AT_FDCWD (any real descriptor is EINVAL), and fchdir crashes it. Each step opens the absolute+// path without following its last component, then checks that what it opened is the entry the+// held parent descriptor lists under that name, so a link or folder swapped in above is refused.+#define PLAYDEN_WALK_BY_PATH 1#endifnamespace playden::platform {-inline int OpenWalkChild(int parent, const char* name, int flags) {-#if defined(__PROSPERO__) || (defined(PLAYDEN_PLATFORM_PS5) && PLAYDEN_PLATFORM_PS5)- return __wrap_openat(parent, name, flags);+#if defined(PLAYDEN_WALK_BY_PATH)+namespace walk_detail {+struct Opened {+ std::filesystem::path path;+ dev_t device;+ ino_t inode;+};+inline std::mutex& Lock() {+ static std::mutex lock;+ return lock;+}+// The path each walked descriptor was opened at. A closed descriptor's entry stays until the+// number is reused; the identity check below refuses a reused number for another folder.+inline std::unordered_map<int, Opened>& Paths() {+ static std::unordered_map<int, Opened> paths;+ return paths;+}+inline void Remember(int fd, const std::filesystem::path& path) {+ struct stat info{};+ if (fd < 0 || ::fstat(fd, &info) != 0 || !S_ISDIR(info.st_mode)) return;+ std::lock_guard<std::mutex> lock(Lock());+ Paths()[fd] = {path, info.st_dev, info.st_ino};+}+inline bool PathOf(int fd, std::filesystem::path* path) {+ struct stat info{};+ if (::fstat(fd, &info) != 0) return false;+ std::lock_guard<std::mutex> lock(Lock());+ const auto found = Paths().find(fd);+ if (found == Paths().end() || found->second.device != info.st_dev || found->second.inode != info.st_ino) {+ errno = EBADF;+ return false;+ }+ *path = found->second.path;+ return true;+}+// The inode the parent's listing gives `name`, or 0 when it has none.+inline std::uint64_t ListedInode(int parent, const char* name) {+ const int listing_fd = ::dup(parent);+ DIR* listing = listing_fd >= 0 ? ::fdopendir(listing_fd) : nullptr;+ if (!listing) {+ if (listing_fd >= 0) ::close(listing_fd);+ return 0;+ }+ std::uint64_t inode = 0;+ while (const auto* entry = ::readdir(listing))+ if (std::string(entry->d_name) == name) { inode = entry->d_fileno; break; }+ ::closedir(listing);+ return inode;+}+inline bool ValidName(const char* name) {+ const std::string text = name ? name : "";+ return !text.empty() && text != "." && text != ".." && text.find('/') == std::string::npos;+}+} // namespace walk_detail++// `listed_inode`: the inode the caller read from the parent's listing, saving a second scan.+inline int OpenWalkChild(int parent, const char* name, int flags, std::uint64_t listed_inode = 0) {+ std::filesystem::path parent_path;+ if (!walk_detail::ValidName(name)) { errno = EINVAL; return -1; }+ if (!walk_detail::PathOf(parent, &parent_path)) return -1;+ struct stat parent_info{};+ if (::fstat(parent, &parent_info) != 0) return -1;+ const auto path = parent_path / name;+ const int fd = ::open(path.c_str(), flags | O_NOFOLLOW, 0);+ if (fd < 0) return -1;+ struct stat opened{};+ bool same = ::fstat(fd, &opened) == 0;+ // Across a mount point the listing shows the covered folder's inode, and the entries of / are+ // system folders (firmlinks on macOS): O_NOFOLLOW still refused a link there, and the app user+ // can't plant a mount or replace an entry of /.+ if (same && opened.st_dev == parent_info.st_dev && parent_path != "/") {+ const std::uint64_t listed = listed_inode ? listed_inode : walk_detail::ListedInode(parent, name);+ same = listed != 0 && listed == static_cast<std::uint64_t>(opened.st_ino);+ }+ if (!same) {+ ::close(fd);+ errno = ESTALE;+ return -1;+ }+ walk_detail::Remember(fd, path);+ return fd;+}+inline int MakeWalkChild(int parent, const char* name, mode_t mode) {+ std::filesystem::path parent_path;+ if (!walk_detail::ValidName(name)) { errno = EINVAL; return -1; }+ if (!walk_detail::PathOf(parent, &parent_path)) return -1;+ // The parent path must still name the held parent: mkdir never follows its last component.+ struct stat held{}, named{};+ if (::fstat(parent, &held) != 0) return -1;+ if (::lstat(parent_path.c_str(), &named) != 0) return -1;+ if (!S_ISDIR(named.st_mode) || named.st_dev != held.st_dev || named.st_ino != held.st_ino) {+ errno = ESTALE;+ return -1;+ }+ return ::mkdir((parent_path / name).c_str(), mode);+}#else+inline int OpenWalkChild(int parent, const char* name, int flags, std::uint64_t = 0) {return ::openat(parent, name, flags);-#endif}inline int MakeWalkChild(int parent, const char* name, mode_t mode) {-#if defined(__PROSPERO__) || (defined(PLAYDEN_PLATFORM_PS5) && PLAYDEN_PLATFORM_PS5)- return __wrap_mkdirat(parent, name, mode);-#elsereturn ::mkdirat(parent, name, mode);-#endif}+#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);+#if defined(PLAYDEN_WALK_BY_PATH)+ walk_detail::Remember(fd, "/");+#endiffor (const auto& part : path.relative_path()) {if (part.empty() || part == "." || part == "..") { ::close(fd); errno = EINVAL; return -1; }if (fd < 0) return -1;diff --git a/src/runtime/private_directories.cpp b/src/runtime/private_directories.cppindex 96bc61c..48722be 100644--- a/src/runtime/private_directories.cpp+++ b/src/runtime/private_directories.cpp@@ -161,7 +161,8 @@ 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.- const int child = platform::OpenWalkChild(fd, name.c_str(), O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);+ const int child = platform::OpenWalkChild(fd, name.c_str(), O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC,+ entry->d_fileno);if (child < 0) {if (errno == ENOSYS || errno == EINVAL) { ok = false; break; }if (errno != ELOOP && errno != EMLINK) ++result->skipped;diff --git a/tests/homebrew_live_model_test.cpp b/tests/homebrew_live_model_test.cppindex 98a0c48..731e625 100644--- a/tests/homebrew_live_model_test.cpp+++ b/tests/homebrew_live_model_test.cpp@@ -633,9 +633,15 @@ void live_root_access_missing() {Simulation long_error({}, {}, ModelMode::Live);long_error.ShowRootAccessMissing(std::string(400, 'x'));const auto& rows = long_error.view().rows;- const auto shortened = std::find_if(rows.begin(), rows.end(), [](const auto& row) { return row.key == "root-detail"; });- check(shortened != rows.end() && shortened->title.size() < 200 && shortened->title.find("…") != std::string::npos,- "root access: a long startup error is shortened");+ const auto whole = std::find_if(rows.begin(), rows.end(), [](const auto& row) { return row.key == "root-detail"; });+ check(whole != rows.end() && whole->title.find(std::string(400, 'x')) != std::string::npos,+ "root access: a startup error with its diagnostics is shown whole");+ Simulation runaway_error({}, {}, ModelMode::Live);+ runaway_error.ShowRootAccessMissing(std::string(3000, 'x'));+ const auto& runaway_rows = runaway_error.view().rows;+ const auto shortened = std::find_if(runaway_rows.begin(), runaway_rows.end(), [](const auto& row) { return row.key == "root-detail"; });+ check(shortened != runaway_rows.end() && shortened->title.size() < 1100 && shortened->title.find("…") != std::string::npos,+ "root access: a runaway startup error is shortened");// Without the grant the FTP-made stubs fail the fallback's check too: that clause is left out.Simulation stubs({}, {}, ModelMode::Live);diff --git a/tests/runtime_package_installer_test.cpp b/tests/runtime_package_installer_test.cppindex 665d056..b9261ba 100644--- a/tests/runtime_package_installer_test.cpp+++ b/tests/runtime_package_installer_test.cpp@@ -29,7 +29,6 @@#include <vector>#ifdef PLAYDEN_TEST_AT_UNAVAILABLE-extern int playden_test_gateway_error;extern "C" int playden_ps5_file_lock(int fd, int operation) { return ::flock(fd, operation); }#include <cerrno>extern "C" int openat(int, const char*, int, ...) { errno = EINVAL; return -1; }@@ -748,14 +747,6 @@ void SharedTreeWalksByPath() {CHECK(::link((temporary.path / "private/secret").c_str(), (root / "Documents/secret").c_str()) == 0);SharedTreeOptions options;-#ifdef PLAYDEN_TEST_AT_UNAVAILABLE- for (const int error : {EINVAL, ENOSYS}) {- playden_test_gateway_error = error;- CHECK(!OpenTreeToEveryone(root, options).ok);- CHECK(ModeOf(root / "Documents/notes.txt") == 0600);- playden_test_gateway_error = 0;- }-#endifauto 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);diff --git a/tests/runtime_writable_folders_test.cpp b/tests/runtime_writable_folders_test.cppindex b00a6bf..ae1cac6 100644--- a/tests/runtime_writable_folders_test.cpp+++ b/tests/runtime_writable_folders_test.cpp@@ -3,7 +3,6 @@#include <cstdio>#include <cerrno>#if PLAYDEN_PLATFORM_PS5-extern int playden_test_gateway_error;#endif#include <cstdlib>#include <filesystem>@@ -132,20 +131,18 @@ int main() {CHECK(EnsureWritableFolders({target}, runs, log, root).empty());CHECK(Mode(target) == 0777 && calls == before_calls);CHECK(chmod(target.c_str(), 0755) == 0);-#if PLAYDEN_PLATFORM_PS5- for (const int error : {EINVAL, ENOSYS}) {- CHECK(chmod(locked.c_str(), 0555) == 0);- CHECK(chmod((root / "data/playden").c_str(), 0755) == 0);- playden_test_gateway_error = error;- CHECK(!EnsureWritableFolders({locked}, runs, log, root).empty());- CHECK(Mode(locked) == 0555);+ // /data swapped for a link to an outside folder: neither repair follows it as root.+ {+ const auto outside = root / "outside-data";+ CHECK(rename((root / "data").c_str(), outside.c_str()) == 0);+ CHECK(symlink(outside.c_str(), (root / "data").c_str()) == 0);+ CHECK(chmod((outside / "playden").c_str(), 0755) == 0 && chmod(locked.c_str(), 0555) == 0);CHECK(!PrepareRuntimeRoot(root / "data/playden", runs, log, root).empty());- CHECK(Mode(root / "data/playden") == 0755);- CHECK(lines.back().find("error=" + std::to_string(error)) != std::string::npos);- playden_test_gateway_error = 0;+ CHECK(Mode(outside / "playden") == 0755);+ CHECK(lines.back().find("open /data failed") != std::string::npos);+ CHECK(unlink((root / "data").c_str()) == 0 && rename(outside.c_str(), (root / "data").c_str()) == 0);CHECK(chmod(locked.c_str(), 0777) == 0);}-#endifstd::error_code ignored;fs::remove_all(root, ignored);std::puts("writable folders tests passed");