diff --git a/tools/nativeguest/hook/exec.c b/tools/nativeguest/hook/exec.c index eb2659f37d..546531c6dd 100644 --- a/tools/nativeguest/hook/exec.c +++ b/tools/nativeguest/hook/exec.c @@ -452,7 +452,8 @@ static int ddnatv_execve(const char *filename, char *const argv[], char *const e char resolved_target[PATH_MAX]; // Only intercept binaries/scripts within DDNATV_ROOT - if ((g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(filename)) { + if ((g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(filename) || + is_bound_guest_program(target)) { struct exec_probe probe; const char *elf_target = select_exec_target(target, resolved_target, sizeof(resolved_target), &probe); if (probe.is_elf && !probe.is_dynamic) static_elf = elf_target; @@ -969,7 +970,8 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path, } char *const *orig_ep = envp ? envp : environ; - int in_root = (g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(path); + int in_root = (g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(path) || + is_bound_guest_program(target); struct exec_probe probe; char resolved_target[PATH_MAX]; diff --git a/tools/nativeguest/hook/exec_probe.c b/tools/nativeguest/hook/exec_probe.c index be708e9158..32dabeb982 100644 --- a/tools/nativeguest/hook/exec_probe.c +++ b/tools/nativeguest/hook/exec_probe.c @@ -983,6 +983,14 @@ static int is_host_interpreter(const char *interp, const char *rewritten) { return probe.android_interp; } +static int is_bound_guest_program(const char *path) { + if (g_ddnatv_root[0] == '\0' || !is_identity_bound_path(path)) return 0; + struct exec_probe probe; + probe_exec_file(path, &probe); + if (probe.is_elf) return probe.is_dynamic && !probe.android_interp; + return probe.n >= 2 && probe.hdr[0] == '#' && probe.hdr[1] == '!'; +} + // argv for running `script` with its interpreter: interp [arg] script args... (the script's own // argv[0] is dropped, as the kernel does). free() the result. static char **build_script_argv(const char *interp, const char *interp_arg, const char *script, char *const argv[]) { diff --git a/tools/nativeguest/hook/paths.c b/tools/nativeguest/hook/paths.c index 616098df04..89605e1ebb 100644 --- a/tools/nativeguest/hook/paths.c +++ b/tools/nativeguest/hook/paths.c @@ -234,6 +234,13 @@ static int is_mapped_guest_path(const char *path) { return 0; } +static int is_identity_bound_path(const char *path) { + if (!path || path[0] != '/') return 0; + for (int i = 0; i < g_nbinds; i++) + if (g_binds[i].identity && is_path_prefix(path, g_binds[i].dst, g_binds[i].dst_len)) return 1; + return 0; +} + // Android's own mounts and files, which must never be redirected into the rootfs. // `path` starts with '/'; a prefix can only match when its second character does. static int is_android_host_path(const char *path) { diff --git a/tools/tests/test_native_hook_ldso_spawn.py b/tools/tests/test_native_hook_ldso_spawn.py index 8d8b417f82..37cd7402e9 100644 --- a/tools/tests/test_native_hook_ldso_spawn.py +++ b/tools/tests/test_native_hook_ldso_spawn.py @@ -120,6 +120,14 @@ class NativeHookLdsoSpawnTest(unittest.TestCase): script.parent.mkdir(exist_ok=True) script.write_text(f"#!{interpreter}\n") script.chmod(0o755) + cls.app_files = tmp / "app-files" + cls.app_files.mkdir() + shutil.copy(cls.probe, cls.app_files / "tool") + shutil.copy(cls.android / "sh", cls.app_files / "android-tool") + for name, interpreter in (("guest.py", GUEST_TOOL), ("android.sh", f"{ANDROID_DIR}/sh")): + (cls.app_files / name).write_text(f"#!{interpreter}\n") + for entry in cls.app_files.iterdir(): + entry.chmod(0o755) @classmethod def tearDownClass(cls): @@ -220,6 +228,32 @@ class NativeHookLdsoSpawnTest(unittest.TestCase): 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_a_script_on_a_mount_at_its_host_path_runs_with_the_guest_interpreter(self): + script = str(self.app_files / "guest.py") + for how in ("exec", "spawn"): + with self.subTest(how=how): + call = self.call(how, script, "x", binds=f"{self.app_files}:{self.app_files}") + self.assertEqual(str(self.root / GUEST_LDSO.lstrip("/")), call[0]) + self.assertEqual([str(self.root / GUEST_TOOL.lstrip("/")), script, "x"], call[-3:]) + + def test_a_glibc_program_on_a_mount_at_its_host_path_starts_through_the_guest_ldso(self): + tool = str(self.app_files / "tool") + for how in ("exec", "spawn"): + with self.subTest(how=how): + call = self.call(how, tool, "x", binds=f"{self.app_files}:{self.app_files}") + self.assertEqual(str(self.root / GUEST_LDSO.lstrip("/")), call[0]) + self.assertEqual([tool, "x"], call[-2:]) + + def test_android_programs_on_a_mount_at_its_host_path_stay_on_the_host(self): + tool = str(self.app_files / "android-tool") + script = str(self.app_files / "android.sh") + sh = str(self.android / "sh") + binds = f"{self.app_files}:{self.app_files}|{self.android}:{ANDROID_DIR}" + for how in ("exec", "spawn"): + with self.subTest(how=how): + self.assertEqual([tool, tool, "x"], self.call(how, tool, "x", binds=binds)) + self.assertEqual([sh, sh, script, "x"], self.call(how, script, "x", binds=binds)) + def test_an_android_interpreter_reached_through_a_bind_stays_on_the_host(self): sh = str(self.android / "sh") for how in ("exec", "spawn"):