From 31c943c82aa722bdebc40744f5e3ba868059e26b Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Thu, 29 Oct 2026 19:12:22 -0400 Subject: [PATCH] fix: Windows installs to Android storage no longer ask for an administrator (#1791) --- tools/linuxfs/preload/owner.c | 41 +++++++++ tools/linuxfs/preload/storage.c | 8 +- tools/tests/test_storage_owner_preload.py | 105 ++++++++++++++++++++++ tools/tests/test_storage_preload.py | 2 +- 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 tools/linuxfs/preload/owner.c create mode 100644 tools/tests/test_storage_owner_preload.py diff --git a/tools/linuxfs/preload/owner.c b/tools/linuxfs/preload/owner.c new file mode 100644 index 000000000..d4e42964c --- /dev/null +++ b/tools/linuxfs/preload/owner.c @@ -0,0 +1,41 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#define FUSE_MAGIC 0x65735546 +#define SDCARDFS_MAGIC 0x5dca2df5 + +static int is_wineserver(void) { + static int cached = -1; + char exe[PATH_MAX]; + const char *base; + ssize_t n; + + if (cached >= 0) return cached; + n = readlink("/proc/self/exe", exe, sizeof(exe) - 1); + if (n <= 0) { + cached = 0; + return cached; + } + exe[n] = '\0'; + base = strrchr(exe, '/'); + cached = strcmp(base ? base + 1 : exe, "wineserver") == 0; + return cached; +} + +__attribute__((visibility("hidden"))) +void bl_storage_owner(int fd, struct stat *st) { + struct statfs fs; + int saved = errno; + uid_t uid; + + if (st != NULL && is_wineserver()) { + uid = getuid(); + if (st->st_uid != uid && fstatfs(fd, &fs) == 0 && (fs.f_type == FUSE_MAGIC || fs.f_type == SDCARDFS_MAGIC)) st->st_uid = uid; + } + errno = saved; +} diff --git a/tools/linuxfs/preload/storage.c b/tools/linuxfs/preload/storage.c index 1e454b864..97fdaabeb 100644 --- a/tools/linuxfs/preload/storage.c +++ b/tools/linuxfs/preload/storage.c @@ -13,6 +13,7 @@ #include static int (*next_fstat)(int, struct stat *); +void bl_storage_owner(int fd, struct stat *st) __attribute__((visibility("hidden"))); static int (*next_fallocate)(int, int, off_t, off_t); static int (*next_posix_fallocate)(int, off_t, off_t); static ssize_t (*next_pwrite)(int, const void *, size_t, off_t); @@ -285,12 +286,17 @@ int fstat(int fd, struct stat *out) { if (next_fstat == NULL) { errno = ENOSYS; return -1; } initialize(); errno = entry_errno; - if (!enabled) return next_fstat(fd, out); + if (!enabled) { + int result = next_fstat(fd, out); + if (result == 0) bl_storage_owner(fd, out); + return result; + } uint64_t start = now_ns(); errno = entry_errno; int result = next_fstat(fd, out); int call_errno = errno; uint64_t elapsed = now_ns() - start; + if (result == 0) bl_storage_owner(fd, out); if (result == 0 && on_selected_device(fd, out)) { add(&stats.fstat_calls, 1); add(&stats.fstat_elapsed_ns, elapsed); diff --git a/tools/tests/test_storage_owner_preload.py b/tools/tests/test_storage_owner_preload.py new file mode 100644 index 000000000..a66719860 --- /dev/null +++ b/tools/tests/test_storage_owner_preload.py @@ -0,0 +1,105 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" + +FAKE = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +uid_t getuid(void) { + return (uid_t)syscall(SYS_getuid) + 1; +} + +int fstatfs(int fd, struct statfs *out) { + char link[32], path[4096]; + snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); + long n = syscall(SYS_readlinkat, AT_FDCWD, link, path, sizeof(path) - 1); + path[n > 0 ? n : 0] = '\0'; + int result = (int)syscall(SYS_fstatfs, fd, out); + if (result == 0 && strstr(path, "/fuse/")) out->f_type = 0x65735546; + if (result == 0 && strstr(path, "/sdcardfs/")) out->f_type = 0x5dca2df5; + if (result == 0 && strstr(path, "/ext4/")) out->f_type = 0xef53; + return result; +} +""" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + uid_t real = (uid_t)syscall(SYS_getuid); + for (int i = 1; i < argc; i++) { + int fd = open(argv[i], O_RDONLY); + struct stat st; + errno = 1234; + int result = fstat(fd, &st); + printf("%s %d\n", st.st_uid == real ? "real" : st.st_uid == real + 1 ? "user" : "other", result == 0 && errno == 1234); + close(fd); + } + return 0; +} +""" + + +@unittest.skipUnless(shutil.which("cc"), "needs a C compiler") +class StorageOwnerPreloadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.temp = tempfile.TemporaryDirectory() + cls.dir = Path(cls.temp.name) + build = cls.dir / "build" + build.mkdir() + (build / "fake.c").write_text(FAKE) + (build / "program.c").write_text(PROGRAM) + cls.preload = build / "libblsession.so" + cls.fake = build / "fake.so" + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.preload), + *map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True) + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.fake), str(build / "fake.c")], check=True) + cls.wineserver = build / "wineserver" + subprocess.run(["cc", "-O2", "-o", str(cls.wineserver), str(build / "program.c")], check=True) + cls.other = build / "wine64-preloader" + shutil.copy(cls.wineserver, cls.other) + for kind in ("fuse", "sdcardfs", "ext4"): + (cls.dir / kind).mkdir() + (cls.dir / kind / "file").write_text("x") + + @classmethod + def tearDownClass(cls): + cls.temp.cleanup() + + def owners(self, program, *kinds): + env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.fake, self.preload)) + env.pop("BL_STORAGE_DIAGNOSTICS", None) + paths = [str(self.dir / kind / "file") for kind in kinds] + result = subprocess.run([str(program)] + paths, env=env, capture_output=True, text=True, check=True) + return result.stdout.split() + + def test_wineserver_owns_what_is_on_android_storage(self): + self.assertEqual(["user", "1", "user", "1"], self.owners(self.wineserver, "fuse", "sdcardfs")) + + def test_other_filesystems_keep_their_owner(self): + self.assertEqual(["real", "1"], self.owners(self.wineserver, "ext4")) + + def test_other_programs_see_the_real_owner(self): + self.assertEqual(["real", "1", "real", "1"], self.owners(self.other, "fuse", "sdcardfs")) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_storage_preload.py b/tools/tests/test_storage_preload.py index 4bdee7826..f2408fd1e 100644 --- a/tools/tests/test_storage_preload.py +++ b/tools/tests/test_storage_preload.py @@ -81,7 +81,7 @@ class StoragePreloadTest(unittest.TestCase): (build / "program.c").write_text(PROGRAM) cls.storage = build / "storage.so" cls.fake = build / "fake.so" - subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.storage), str(PRELOAD / "storage.c"), "-ldl"], check=True) + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.storage), str(PRELOAD / "storage.c"), str(PRELOAD / "owner.c"), "-ldl"], check=True) subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.fake), str(build / "fake.c")], check=True) cls.steam = cls.dir / "steamrtarm64/steam" cls.steam.parent.mkdir()