fix(nativeguest): a script interpreter reached through a bind runs as a guest program (#3112)

* fix(nativeguest): a script interpreter reached through a bind runs as a guest program

A Flatpak runtime binds its own /usr outside the sandbox root, so the hook
took #!/usr/bin/bash there for an Android program and handed it to the
kernel, which failed with "bad interpreter". An interpreter whose guest
path is a bind now starts like any guest program.

* Native hook: keep Android interpreters a bind reaches on the host

* Native hook: match the root of an interpreter on a folder boundary
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-23 23:42:04 -05:00
1 parent 5f2699e56b
commit e349d2544e
3 files changed
+50 -7

No files matched your search

+2 -2
View File
@@ -476,7 +476,7 @@ static int ddnatv_execve(const char *filename, char *const argv[], char *const e
return -1;
}
int ret;
if (is_host_interpreter(rewritten_interp)) {
if (is_host_interpreter(interp, rewritten_interp)) {
char **sys_env = clean_env_for_system(envp);
ret = run_execve(orig_execve, rewritten_interp, new_argv, sys_env);
int saved_errno = errno;
@@ -829,7 +829,7 @@ static int spawn_script(pid_t *pid, const char *script, struct exec_probe *probe
char **new_argv = build_script_argv(rewritten_interp, interp_arg, script, argv);
if (!new_argv) return ENOMEM;
int ret;
if (is_host_interpreter(rewritten_interp)) {
if (is_host_interpreter(interp, rewritten_interp)) {
orig_posix_spawn_f_type orig_posix_spawn = real_posix_spawn();
char **sys_env = clean_env_for_system(envp);
ret = orig_posix_spawn ? orig_posix_spawn(pid, rewritten_interp, file_actions, attrp, new_argv, sys_env) : ENOSYS;
+8 -4
View File
@@ -966,10 +966,14 @@ static void parse_shebang(char *hdr, char **interp, char **interp_arg) {
*interp_arg = arg;
}
// Interpreters outside the rootfs (Android's /system/bin/sh) run with Android's environment.
static int is_host_interpreter(const char *interp) {
return strncmp(interp, "/system", 7) == 0 ||
(g_ddnatv_root[0] != '\0' && strncmp(interp, g_ddnatv_root, g_ddnatv_root_len) != 0);
// Interpreters outside the rootfs and its binds (Android's /system/bin/sh) run with Android's environment.
static int is_host_interpreter(const char *interp, const char *rewritten) {
if (strncmp(rewritten, "/system", 7) == 0) return 1;
if (g_ddnatv_root[0] == '\0' || get_rootfs_subpath(rewritten)) return 0;
if (!is_mapped_guest_path(interp)) return 1;
struct exec_probe probe;
probe_exec_file(rewritten, &probe);
return probe.android_interp;
}
// argv for running `script` with its interpreter: interp [arg] script args... (the script's own
+40 -1
View File
@@ -10,6 +10,9 @@ from tools.tests.test_usr2_preload import runner
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
GUEST_LDSO = "/usr/lib/ld-linux-aarch64.so.1"
GUEST_TOOL = "/usr/local/bin/tool"
BOUND_DIR = "/opt/runtime"
ANDROID_DIR = "/opt/android"
HOST_SHELL = "/data/local/tmp/droiddeck-hook-probe-sh"
RECORDER = r"""
#define _GNU_SOURCE
@@ -102,13 +105,29 @@ class NativeHookLdsoSpawnTest(unittest.TestCase):
shutil.copy(source, host)
host.chmod(0o755)
(cls.root / "usr/lib/aarch64-linux-gnu").mkdir(parents=True, exist_ok=True)
cls.runtime = tmp / "runtime"
cls.runtime.mkdir()
shutil.copy(cls.probe, cls.runtime / "bash")
(cls.runtime / "bash").chmod(0o755)
cls.android = tmp / "root-android"
cls.android.mkdir()
subprocess.run([cls.cc, "-O1", "-Wl,--dynamic-linker=/system/bin/linker64", "-o", str(cls.android / "sh"),
str(tmp / "probe.c")], check=True)
for name, interpreter in (("bound.sh", f"{BOUND_DIR}/bash"), ("host.sh", HOST_SHELL),
("android.sh", f"{ANDROID_DIR}/sh")):
script = cls.root / "opt" / name
script.parent.mkdir(exist_ok=True)
script.write_text(f"#!{interpreter}\n")
script.chmod(0o755)
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def call(self, how, *argv):
def call(self, how, *argv, binds=None):
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": f"{self.hook}:{self.recorder}"}
if binds:
hook_env["DDNATV_BINDS"] = binds
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_EXEC", "LD_LIBRARY_PATH")}
if self.prefix:
cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
@@ -158,6 +177,26 @@ class NativeHookLdsoSpawnTest(unittest.TestCase):
self.call(how, GUEST_LDSO, "--list", GUEST_TOOL)
self.assertEqual([], self.library_paths)
def test_a_script_interpreter_from_a_bind_runs_as_a_guest_program(self):
for how in ("exec", "spawn"):
with self.subTest(how=how):
call = self.call(how, "/opt/bound.sh", "x", binds=f"{self.runtime}:{BOUND_DIR}")
self.assertEqual(str(self.root / GUEST_LDSO.lstrip("/")), call[0])
self.assertEqual([str(self.runtime / "bash"), str(self.root / "opt/bound.sh"), "x"], call[-3:])
def test_a_script_interpreter_outside_the_rootfs_and_its_binds_stays_on_the_host(self):
for how in ("exec", "spawn"):
with self.subTest(how=how):
call = self.call(how, "/opt/host.sh", "x", binds=f"{self.runtime}:{BOUND_DIR}")
self.assertEqual([HOST_SHELL, HOST_SHELL, str(self.root / "opt/host.sh"), "x"], call)
def test_an_android_interpreter_reached_through_a_bind_stays_on_the_host(self):
sh = str(self.android / "sh")
for how in ("exec", "spawn"):
with self.subTest(how=how):
call = self.call(how, "/opt/android.sh", "x", binds=f"{self.android}:{ANDROID_DIR}")
self.assertEqual([sh, sh, str(self.root / "opt/android.sh"), "x"], call)
if __name__ == "__main__":
unittest.main()