Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Self-update leaves app files writable by everyone

Leonardo Lobato9d9e3488c57f
diff --git a/src/update/update_service.cpp b/src/update/update_service.cpp
index 32af4d6..d172c1a 100644
--- a/src/update/update_service.cpp
+++ b/src/update/update_service.cpp
@@ -67,17 +67,19 @@ std::string Gigabytes(std::uint64_t bytes) {
return text;
}
-// An app file's mode on the console: executables 0755 (as the FTP deploy tool sets them),
-// everything else as the zip has it.
-std::optional<unsigned> AppMode(const std::string& relative, unsigned zip_mode) {
+// An app file's mode on the console: 0777 for executables, 0666 for everything else, whatever the
+// zip has. Someone may install a build by hand over a self-updated app, with FTP or the web file
+// manager. They run as other users, and root has no DAC bypass on /data, so they can only write over
+// files that everyone can write. Nothing in Playden needs the app files to be read-only.
+std::optional<unsigned> AppMode(const std::string& relative, unsigned) {
const auto slash = relative.rfind('/');
const std::string name = slash == std::string::npos ? relative : relative.substr(slash + 1);
const auto ends = [&](const char* suffix) {
const std::string tail = suffix;
return name.size() > tail.size() && name.compare(name.size() - tail.size(), tail.size(), tail) == 0;
};
- if (name == "eboot.bin" || ends(".prx") || ends(".elf")) return 0755u;
- return zip_mode;
+ if (name == "eboot.bin" || ends(".prx") || ends(".elf")) return 0777u;
+ return 0666u;
}
} // namespace
@@ -1011,6 +1013,7 @@ bool UpdateService::StageApp(const Release& release, const UpdateManifest& manif
return config_.as_root([&] { return chmod(path.c_str(), mode) == 0; });
};
unzip.mode_for = AppMode;
+ unzip.folder_mode = 0777u; // for the same reason as AppMode
unzip.log = [this](const std::string& line) { Log(line); };
unzip.progress = [this](std::uint64_t done, std::uint64_t total) {
Set([&](UpdateSnapshot& s) { s.done = done; s.total = total; });
diff --git a/src/update/zip_extract.cpp b/src/update/zip_extract.cpp
index 35aebcd..bc5b62d 100644
--- a/src/update/zip_extract.cpp
+++ b/src/update/zip_extract.cpp
@@ -165,6 +165,30 @@ ZipExtractResult ExtractZip(const std::string& zip_path, const std::string& dest
entries.push_back({i, relative, folder, mode, stat.m_uncomp_size});
}
// A file's folders are created even when the zip has no folder entries for them.
+ std::set<std::string> made;
+ const auto make_folder = [&](const std::string& relative) {
+ for (std::size_t i = 0; i <= relative.size(); ++i) {
+ if (i != relative.size() && relative[i] != '/') continue;
+ const std::string part = relative.substr(0, i);
+ if (!made.insert(part).second) continue;
+ const std::string path = dest + "/" + part;
+ if (mkdir(path.c_str(), 0777) != 0 && errno != EEXIST) return false;
+ if (!IsFolder(path)) return false;
+ if (!options.folder_mode) continue;
+ const unsigned want = *options.folder_mode & 07777;
+ struct stat st{};
+ if (stat(path.c_str(), &st) != 0) return false;
+ if ((st.st_mode & 07777) == want) continue;
+ const int chmod_rc = options.set_folder_mode ? options.set_folder_mode(path, want) : chmod(path.c_str(), want);
+ // Created while another thread held root, like the files below.
+ if (chmod_rc != 0 && !(errno == EPERM && options.fix_mode && options.fix_mode(path, want))) {
+ if (options.log) options.log("unzip: cannot set the mode of folder " + part + " errno=" + std::to_string(errno));
+ return false;
+ }
+ if (stat(path.c_str(), &st) != 0 || (st.st_mode & 07777) != want) return false;
+ }
+ return true;
+ };
Sink sink;
sink.buffer.resize(kWriteBuffer);
sink.cancel = options.cancel;
@@ -172,11 +196,13 @@ ZipExtractResult ExtractZip(const std::string& zip_path, const std::string& dest
if (cancelled()) { result.cancelled = true; return fail("cancelled"); }
const std::string path = dest + "/" + entry.relative;
if (entry.folder) {
- if (!MakeFolders(path)) return fail("cannot create folder " + entry.relative);
+ if (!make_folder(entry.relative)) return fail("cannot create folder " + entry.relative);
++result.folders;
continue;
}
- if (!MakeParents(path)) return fail("cannot create folder for " + entry.relative);
+ const auto slash = entry.relative.rfind('/');
+ if (slash != std::string::npos && !make_folder(entry.relative.substr(0, slash)))
+ return fail("cannot create folder for " + entry.relative);
bool written = false;
for (int attempt = 0; attempt < 3 && !written; ++attempt) {
// A new file each time: one created with someone else's owner can't take our mode.
diff --git a/src/update/zip_extract.h b/src/update/zip_extract.h
index 343a498..d1cfbfe 100644
--- a/src/update/zip_extract.h
+++ b/src/update/zip_extract.h
@@ -28,6 +28,11 @@ struct ZipExtractOptions {
// Sets the mode of a file the app user doesn't own, as root (see ExtractZip). Without it such a
// file is deleted and written again instead.
std::function<bool(const std::string& path, unsigned mode)> fix_mode;
+ // When set, every folder it creates below dest gets this mode, also the ones the zip only implies
+ // (mkdir alone is umasked). A folder the app user can't chmod gets it through fix_mode.
+ std::optional<unsigned> folder_mode;
+ // Sets a created folder's mode; chmod by default (tests replace it).
+ std::function<int(const std::string& path, unsigned mode)> set_folder_mode;
};
struct ZipExtractResult {
@@ -41,7 +46,8 @@ struct ZipExtractResult {
std::uint64_t mode_fixed = 0; // root-owned files whose mode fix_mode set
};
-// Unpacks into dest, which must be an existing empty folder. Folders are created 0777. Every
+// Unpacks into dest, which must be an existing empty folder. Folders are created 0777 (less the
+// umask, unless folder_mode is set). Every
// file is checked after it is written (size and mode, by fstat). On the PS5 another thread may
// hold root's effective user id for a moment (euid is per process): a file created then belongs to
// root and the app user can't change its mode (results/20261015-self-update-probes). Such a file
diff --git a/tests/self_update_service_test.cpp b/tests/self_update_service_test.cpp
index 6a1ec70..b7c7a5d 100644
--- a/tests/self_update_service_test.cpp
+++ b/tests/self_update_service_test.cpp
@@ -2,6 +2,7 @@
#include "update/update_service.h"
#include "zip_fixture.h"
+#include <sys/stat.h>
#include <unistd.h>
#include <chrono>
@@ -38,6 +39,19 @@ std::string FakeSha(const std::string& text) {
std::snprintf(hex, sizeof hex, "%016llx", static_cast<unsigned long long>(hash));
return std::string(hex) + hex + hex + hex;
}
+unsigned Mode(const std::string& path) {
+ struct stat st{};
+ CHECK(stat(path.c_str(), &st) == 0);
+ return st.st_mode & 07777;
+}
+// Everyone can write over the app's files and folders (web file manager and FTP run as other users).
+void CheckWritableApp(const std::string& app) {
+ CHECK(Mode(app + "/eboot.bin") == 0777);
+ CHECK(Mode(app + "/LICENSE") == 0666 && Mode(app + "/sce_sys/param.json") == 0666);
+ CHECK(Mode(app + "/assets/a.txt") == 0666 && Mode(app + "/assets/run.elf") == 0777);
+ CHECK(Mode(app + "/sce_sys") == 0777 && Mode(app + "/assets") == 0777 && Mode(app + "/assets/deep") == 0777);
+ CHECK(Mode(app + "/assets/deep/x.prx") == 0777);
+}
void Put(const std::string& path, const std::string& text) {
CHECK(MakeParents(path));
CHECK(zip_fixture::Write(path, text));
@@ -95,7 +109,9 @@ std::string AppZip(const std::string& version_text, const std::string& content_v
{"FAKE77031/playden-version", full_version.empty() ? version_text + "\n" : full_version},
{"FAKE77031/sce_sys/param.json",
R"({"titleId":"FAKE77031","contentVersion":")" + content_version + R"("})"},
- {"FAKE77031/assets/a.txt", "asset " + version_text},
+ {"FAKE77031/assets/a.txt", "asset " + version_text, 0100600},
+ {"FAKE77031/assets/run.elf", "elf", 0100755},
+ {"FAKE77031/assets/deep/x.prx", "prx", 0100644},
{"FAKE77031/logs/", "", 040755}});
}
@@ -515,6 +531,7 @@ int main() {
CHECK(snapshot.notes_url == "https://github.com/o/r/releases/tag/v0.3.0"); // no html_url: built from the tag
CHECK(snapshot.download_bytes == fixture.server.files[kDownload + "playden-ps5-0.3.0.zip"].size() +
fixture.server.files[kDownload + "playden-kit-h-r9.zip"].size());
+ const mode_t old_umask = umask(022); // the app's folders get 0777 whatever the umask
service.Install();
Wait(service);
snapshot = service.Snapshot();
@@ -525,6 +542,8 @@ int main() {
CHECK(!Exists(kits + "/.next-kitid-r9/LICENSES"));
CHECK(Read(kits + "/kitid/manifest.json") == std::string(64, 'b'));
CHECK(Read(fixture.root + "/playden/self-update/stage/0.3.0/eboot.bin") == "eboot 0.3.0");
+ CheckWritableApp(fixture.root + "/playden/self-update/stage/0.3.0");
+ umask(old_umask);
CHECK(Read(fixture.root + "/playden/self-update/state.json").find("\"staged\"") != std::string::npos);
CHECK(ListFolder(fixture.root + "/playden/self-update/downloads").empty()); // zips removed after unpacking
// Nothing is swapped without a restart request.
@@ -536,6 +555,7 @@ int main() {
const std::string app = fixture.root + "/homebrew/FAKE77031";
CHECK(Read(app + "/eboot.bin") == "eboot 0.3.0" && Read(app + "/assets/a.txt") == "asset 0.3.0");
CHECK(Read(app + "/playden-data/session") == "session");
+ CheckWritableApp(app); // the swap's renames keep the modes
CHECK(Read(fixture.root + "/user/appmeta/FAKE77031/param.json").find("00.003.000") != std::string::npos);
CHECK(Read(fixture.root + "/user/app/FAKE77031/sce_sys/param.json").find("00.003.000") != std::string::npos);
CHECK(Read(fixture.root + "/playden/self-update/backup/0.2.0-1000/eboot.bin") == "eboot 0.2.0");
diff --git a/tests/self_update_zip_test.cpp b/tests/self_update_zip_test.cpp
index 0721607..88528d6 100644
--- a/tests/self_update_zip_test.cpp
+++ b/tests/self_update_zip_test.cpp
@@ -9,6 +9,7 @@
#include <cstdio>
#include <cstdlib>
#include <string>
+#include <vector>
using namespace playden::update;
using zip_fixture::Entry;
@@ -141,6 +142,38 @@ int main() {
options.fix_mode = [](const std::string&, unsigned) { return false; };
CHECK(!Run({{"a.dll", "aa"}}, options).ok);
}
+ // folder_mode sets every created folder's mode, also folders the zip only implies, whatever the umask.
+ {
+ std::string out;
+ ZipExtractOptions options;
+ options.folder_mode = 0777;
+ const mode_t old_umask = umask(022);
+ const auto result = Run({{"a/b/c.txt", "c"}, {"d/", "", 040755}, {"d/e/", "", 040700}}, options, &out);
+ umask(old_umask);
+ CHECK(result.ok && result.files == 1 && result.folders == 2);
+ CHECK(Mode(out + "/a") == 0777 && Mode(out + "/a/b") == 0777);
+ CHECK(Mode(out + "/d") == 0777 && Mode(out + "/d/e") == 0777);
+ // Without it (the kit unpack) folders keep mkdir's 0777 less the umask.
+ umask(022);
+ const auto plain = Run({{"a/b/c.txt", "c"}, {"d/", "", 040777}}, {}, &out);
+ umask(old_umask);
+ CHECK(plain.ok && Mode(out + "/a") == 0755 && Mode(out + "/a/b") == 0755 && Mode(out + "/d") == 0755);
+ // A folder the app user can't chmod (made while another thread held root) gets its mode as root.
+ options.set_folder_mode = [](const std::string&, unsigned) { errno = EPERM; return -1; };
+ std::vector<std::string> fixed;
+ options.fix_mode = [&fixed](const std::string& path, unsigned mode) {
+ fixed.push_back(path);
+ return chmod(path.c_str(), mode) == 0;
+ };
+ umask(022);
+ const auto rescued = Run({{"x/y.txt", "y"}}, options, &out);
+ CHECK(rescued.ok && fixed.size() == 1 && fixed[0] == out + "/x" && Mode(out + "/x") == 0777);
+ // Without a working root hook the unpack fails.
+ options.fix_mode = [](const std::string&, unsigned) { return false; };
+ const auto stuck = Run({{"x/y.txt", "y"}}, options);
+ umask(old_umask);
+ CHECK(!stuck.ok && stuck.error.find("x/y.txt") != std::string::npos);
+ }
// A damaged file fails its CRC check.
{
Entry damaged{"data.bin", "payload"};