From 0d0465d19eed6f160ca37fa4d674d87af3d4ba58 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 13 Oct 2026 21:31:26 -0400 Subject: [PATCH] Fix Flatpak installs on older Android kernels (#752) --- tools/proot/PATCHES.md | 10 +- tools/proot/build.sh | 1 + .../patches/0015-statx-mnt-id-fdinfo.patch | 44 ++++++ tools/proot/statx_mnt_id.h | 95 +++++++++++++ tools/tests/test_proot_statx_mnt_id.py | 133 ++++++++++++++++++ 5 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 tools/proot/patches/0015-statx-mnt-id-fdinfo.patch create mode 100644 tools/proot/statx_mnt_id.h create mode 100644 tools/tests/test_proot_statx_mnt_id.py diff --git a/tools/proot/PATCHES.md b/tools/proot/PATCHES.md index 342bf79e6..5e501eab9 100644 --- a/tools/proot/PATCHES.md +++ b/tools/proot/PATCHES.md @@ -78,8 +78,16 @@ Added by DroidDeck: exit.c act on (`TCSETSF`, the four termios2 requests, `FICLONE`) and allows the rest at once, and the ioctl block is emitted first (after WinNative 53836ca9, which traces only the termios2 ones). The `faccessat2` exit stop (termux 5ba8b95, for glibc's ENOSYS fallback on kernels before 5.8) and - the `statx` one (emulation on kernels before 4.11) are dropped when the running kernel is newer. + the `statx` one (emulation on kernels before 4.11) are dropped on kernels 5.8 and newer; patch + `0015` keeps it on 4.11–5.7 to return the mount ID added in 5.8. Host build: `ioctl` 31.6 -> 0.5 us; `stat`/`statx`/`faccessat2` about 44 -> 28 us (one stop). +- `0015-statx-mnt-id-fdinfo.patch` - kernels before 5.8 can complete `statx` without the mount ID + that libglnx requires while chasing an open descriptor. PRoot keeps the exit stop on those + kernels and fills only a missing ordinary `STATX_MNT_ID` for `AT_EMPTY_PATH` after pinning the + tracee descriptor, matching its device and inode to the successful statx result, and reading + `mnt_id` from the pin's fdinfo. A later close/reuse of the tracee fd cannot redirect that read; + a concurrent reuse for the same inode through a different bind mount is indistinguishable at + the exit stop. It does not synthesize `STATX_MNT_ID_UNIQUE`. Prototype (inert unless `PROOT_FASTPATH` is set; see `docs/development/proot-performance.md`): diff --git a/tools/proot/build.sh b/tools/proot/build.sh index a899370fd..718190552 100755 --- a/tools/proot/build.sh +++ b/tools/proot/build.sh @@ -30,6 +30,7 @@ fetch() { } fetch proot.tar.gz "https://github.com/termux/proot/archive/${PROOT_COMMIT}.tar.gz" "$PROOT_SHA256" fetch talloc.tar.gz "https://www.samba.org/ftp/talloc/talloc-${TALLOC_VERSION}.tar.gz" "$TALLOC_SHA256" +cp "$HERE/statx_mnt_id.h" "$WORK/proot/src/tracee/statx_mnt_id.h" for patch in "$HERE"/patches/*.patch; do patch -d "$WORK/proot" -p1 --forward --quiet < "$patch" done diff --git a/tools/proot/patches/0015-statx-mnt-id-fdinfo.patch b/tools/proot/patches/0015-statx-mnt-id-fdinfo.patch new file mode 100644 index 000000000..aa5b564f8 --- /dev/null +++ b/tools/proot/patches/0015-statx-mnt-id-fdinfo.patch @@ -0,0 +1,44 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: DroidDeck +Date: Tue, 13 Oct 2026 00:00:00 +0000 +Subject: [PATCH] statx: supply mount IDs on kernels before 5.8 + +On kernels before 5.8, statx can succeed without STATX_MNT_ID. Recover it +from the already-open descriptor after verifying its identity. Keep the +compatibility exit stop only on kernels that need the missing field. +--- +diff --git a/src/syscall/seccomp.c b/src/syscall/seccomp.c +index ee7b8acd68660f38fd52f56e951ffb94e9061d68..ff3a79b60e9092a296bb444c919bb3861338727f 100644 +--- a/src/syscall/seccomp.c ++++ b/src/syscall/seccomp.c +@@ -738,4 +738,4 @@ int enable_syscall_filtering(const Tracee *tracee) +- * the statx one emulates the call when it fails, which only a kernel +- * before 4.11 does for want of it - a statx Android's policy traps is +- * emulated from SIGSYS instead. Both stops re-read and re-translate +- * the path, and are paid on every call. ++ * the statx one emulates the syscall before 4.11 and supplies MNT_ID ++ * from fdinfo through 5.7. Android policy traps are still emulated from ++ * SIGSYS instead. Both stops re-read and re-translate the path, and are ++ * paid on every call. +@@ -764 +764,4 @@ static void drop_unneeded_exit_stops(void) +- if (major > 4 || (major == 4 && minor >= 11)) ++ /* Linux 4.11 introduced statx; Linux 5.8 added MNT_ID. ++ * Keep its exit stop through 5.7 so the missing ID can be ++ * recovered from the descriptor's fdinfo. */ ++ if (major > 5 || (major == 5 && minor >= 8)) +diff --git a/src/tracee/statx.c b/src/tracee/statx.c +index ca972f892e84461cb694dd13a37bfbc67527b320..824771ccf5934e1f870fc36a29fb22fdcad0a856 100644 +--- a/src/tracee/statx.c ++++ b/src/tracee/statx.c +@@ -4,0 +5 @@ ++#include "tracee/statx_mnt_id.h" +@@ -125,0 +127,9 @@ int handle_statx_syscall(Tracee *tracee, bool from_sigsys) { ++ /* libglnx chases an already-open fd with AT_EMPTY_PATH and requires a ++ * mount ID. Kernels before 5.8 can succeed without returning that field. ++ * The tracee is stopped at this syscall exit; accept only the ordinary ++ * mount ID and verify the fdinfo inode against the statx result. */ ++ if (droiddeck_proot_statx_fill_mnt_id(tracee->pid, (int) dirfd, ++ (int) flags, guest_path, (unsigned int) mask, ++ &state.statx_buf)) ++ state.updated_stats = true; ++ diff --git a/tools/proot/statx_mnt_id.h b/tools/proot/statx_mnt_id.h new file mode 100644 index 000000000..deb1789b8 --- /dev/null +++ b/tools/proot/statx_mnt_id.h @@ -0,0 +1,95 @@ +#ifndef DROIDDECK_PROOT_STATX_MNT_ID_H +#define DROIDDECK_PROOT_STATX_MNT_ID_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static inline bool droiddeck_proot_read_fdinfo(int fd, uint64_t *mnt_id, uint64_t *ino) { + if (fd < 0 || !mnt_id || !ino) return false; + + char fdinfo_path[64]; + int n = snprintf(fdinfo_path, sizeof fdinfo_path, "/proc/self/fdinfo/%d", fd); + if (n < 0 || (size_t)n >= sizeof fdinfo_path) return false; + + FILE *fdinfo = fopen(fdinfo_path, "r"); + if (!fdinfo) return false; + + bool have_mnt_id = false, have_ino = false; + uint64_t parsed_mnt_id = 0, parsed_ino = 0; + char line[256]; + while (fgets(line, sizeof line, fdinfo)) { + const char *value = NULL; + uint64_t *field = NULL; + if (strncmp(line, "mnt_id:", 7) == 0) { + value = line + 7; + field = &parsed_mnt_id; + have_mnt_id = true; + } else if (strncmp(line, "ino:", 4) == 0) { + value = line + 4; + field = &parsed_ino; + have_ino = true; + } + if (!value) continue; + + errno = 0; + char *end = NULL; + unsigned long long parsed = strtoull(value, &end, 10); + if (errno || end == value || (*end && *end != '\n' && *end != '\r')) { + if (field == &parsed_mnt_id) have_mnt_id = false; + else have_ino = false; + continue; + } + *field = (uint64_t)parsed; + } + bool read_error = ferror(fdinfo); + fclose(fdinfo); + + if (read_error || !have_mnt_id || !have_ino || parsed_mnt_id == 0) return false; + *mnt_id = parsed_mnt_id; + *ino = parsed_ino; + return true; +} + +static inline bool droiddeck_proot_statx_fill_mnt_id(pid_t pid, int fd, + int flags, const char *path, + unsigned int mask, + struct statx *buf) { + if (fd < 0 || !(flags & AT_EMPTY_PATH) || !path || path[0] != '\0' + || !(mask & STATX_MNT_ID) || (buf->stx_mask & STATX_MNT_ID) + || !(buf->stx_mask & STATX_INO)) + return false; + + char tracee_fd_path[64]; + int n = snprintf(tracee_fd_path, sizeof tracee_fd_path, "/proc/%ld/fd/%d", (long)pid, fd); + if (n < 0 || (size_t)n >= sizeof tracee_fd_path) return false; + + /* Another thread sharing the tracee's fd table can close/reuse fd after statx returns. Pin the + * current referent, then require both device and inode to match statx before trusting its mount. + * Read fdinfo from our pin so a later tracee fd reuse cannot change which mount ID is observed. + * A same-inode bind-mount alias has the same device/inode pair; at this exit stop we cannot + * distinguish that rare concurrent replacement from the mount used by the completed syscall. */ + int pinned = open(tracee_fd_path, O_PATH | O_CLOEXEC); + if (pinned < 0) return false; + struct stat st; + bool same = fstat(pinned, &st) == 0 && st.st_ino == buf->stx_ino && + major(st.st_dev) == buf->stx_dev_major && minor(st.st_dev) == buf->stx_dev_minor; + uint64_t mnt_id = 0, ino = 0; + bool have_fdinfo = same && droiddeck_proot_read_fdinfo(pinned, &mnt_id, &ino); + close(pinned); + if (!have_fdinfo || ino != st.st_ino) return false; + + buf->stx_mnt_id = mnt_id; + buf->stx_mask |= STATX_MNT_ID; + return true; +} + +#endif diff --git a/tools/tests/test_proot_statx_mnt_id.py b/tools/tests/test_proot_statx_mnt_id.py new file mode 100644 index 000000000..e3dd59658 --- /dev/null +++ b/tools/tests/test_proot_statx_mnt_id.py @@ -0,0 +1,133 @@ +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path +import unittest + +PROOT = Path(__file__).resolve().parents[1] / "proot" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include "statx_mnt_id.h" + +static int fdinfo_mnt_id(int fd, uint64_t *out) { + char path[64], line[256]; + snprintf(path, sizeof path, "/proc/self/fdinfo/%d", fd); + FILE *f = fopen(path, "r"); + if (!f) return 0; + int found = 0; + while (fgets(line, sizeof line, f)) { + unsigned long long id; + if (sscanf(line, "mnt_id:%llu", &id) == 1) { + *out = (uint64_t)id; + found = 1; + break; + } + } + fclose(f); + return found; +} + +int main(void) { + char name[] = "/tmp/droiddeck-statx-mnt-id-XXXXXX"; + int fd = mkstemp(name); + if (fd < 0) return 1; + + struct stat st; + if (fstat(fd, &st) < 0) return 2; + uint64_t expected; + if (!fdinfo_mnt_id(fd, &expected) || expected == 0) return 3; + + /* Model a successful pre-5.8 statx: inode fields are present, MNT_ID is absent. */ + struct statx result = {0}; + result.stx_mask = STATX_TYPE | STATX_INO; + result.stx_mode = st.st_mode; + result.stx_ino = st.st_ino; + result.stx_dev_major = major(st.st_dev); + result.stx_dev_minor = minor(st.st_dev); + unsigned flags = AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW; + unsigned mask = STATX_TYPE | STATX_INO | STATX_MNT_ID; + if (!droiddeck_proot_statx_fill_mnt_id(getpid(), fd, flags, "", mask, &result)) return 4; + if (!(result.stx_mask & STATX_MNT_ID) || result.stx_mnt_id != expected) return 5; + + /* A reused fd can name the same inode number on another device. Inode alone is not identity. */ + result.stx_mask = STATX_TYPE | STATX_INO; + result.stx_dev_major = major(st.st_dev) ^ 1U; + result.stx_dev_minor = minor(st.st_dev); + if (droiddeck_proot_statx_fill_mnt_id(getpid(), fd, flags, "", mask, &result)) return 6; + if (result.stx_mask & STATX_MNT_ID) return 7; + + /* Only ordinary MNT_ID is reconstructed; never claim the newer UNIQUE bit. */ + result.stx_ino = st.st_ino; + result.stx_dev_major = major(st.st_dev); + result.stx_dev_minor = minor(st.st_dev); + if (droiddeck_proot_statx_fill_mnt_id(getpid(), fd, flags, "", 0x4000U, &result)) return 8; + if (result.stx_mask & STATX_MNT_ID) return 9; + + if (droiddeck_proot_statx_fill_mnt_id(getpid(), AT_FDCWD, flags, "", mask, &result)) return 10; + if (droiddeck_proot_statx_fill_mnt_id(getpid(), -1, flags, "", mask, &result)) return 11; + if (droiddeck_proot_statx_fill_mnt_id(getpid(), fd, AT_SYMLINK_NOFOLLOW, "", mask, &result)) return 12; + if (droiddeck_proot_statx_fill_mnt_id(getpid(), fd, flags, "file", mask, &result)) return 13; + int stale = dup(fd); + if (stale < 0 || close(stale) < 0) return 14; + if (droiddeck_proot_statx_fill_mnt_id(getpid(), stale, flags, "", mask, &result)) return 15; + + /* Pin the original referent, then reuse its tracee fd number. The pin's fdinfo stays stable, + * while recovery against the reused number must reject the different inode/device. */ + char replacement_name[] = "/tmp/droiddeck-statx-mnt-id-replacement-XXXXXX"; + int replacement = mkstemp(replacement_name); + if (replacement < 0) return 16; + int pinned; + char path[64]; + snprintf(path, sizeof path, "/proc/%ld/fd/%d", (long)getpid(), fd); + pinned = open(path, O_PATH | O_CLOEXEC); + if (pinned < 0) return 17; + uint64_t pinned_mnt_id, pinned_ino; + if (!droiddeck_proot_read_fdinfo(pinned, &pinned_mnt_id, &pinned_ino) || + pinned_mnt_id != expected || pinned_ino != st.st_ino) return 18; + + struct stat replacement_st; + if (fstat(replacement, &replacement_st) < 0) return 19; + if (replacement_st.st_dev == st.st_dev && replacement_st.st_ino == st.st_ino) return 20; + unlink(name); + if (close(fd) < 0 || dup2(replacement, fd) != fd) return 21; + close(replacement); + unlink(replacement_name); + + result.stx_mask = STATX_TYPE | STATX_INO; + result.stx_ino = st.st_ino; + result.stx_dev_major = major(st.st_dev); + result.stx_dev_minor = minor(st.st_dev); + if (droiddeck_proot_statx_fill_mnt_id(getpid(), fd, flags, "", mask, &result)) return 22; + if (result.stx_mask & STATX_MNT_ID) return 23; + if (!droiddeck_proot_read_fdinfo(pinned, &pinned_mnt_id, &pinned_ino) || + pinned_mnt_id != expected || pinned_ino != st.st_ino) return 24; + + close(pinned); + close(fd); + return 0; +} +""" + + +@unittest.skipUnless(shutil.which("cc") and sys.platform.startswith("linux"), "requires Linux and a C compiler") +class ProotStatxMountIdTest(unittest.TestCase): + def test_missing_mount_id_is_recovered_only_for_the_same_open_inode(self): + with tempfile.TemporaryDirectory() as tmp: + source = Path(tmp) / "test.c" + program = Path(tmp) / "test" + source.write_text(PROGRAM) + subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-I", str(PROOT), "-o", str(program), str(source)], check=True) + subprocess.run([str(program)], check=True) + + +if __name__ == "__main__": + unittest.main()