mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 15:26:04 +00:00
fix(nativeguest): a spawned "ld.so --list PROGRAM" loads the program from the rootfs (#3054)
* fix(nativeguest): a spawned "ld.so --list PROGRAM" loads the program from the rootfs * fix(nativeguest): keep the existing ld.so start comments --------- Co-authored-by: maxjivi05 <maxjividen@gmail.com>
This commit is contained in:
1 parent
3b8b14b06c
commit
bf7f7ecda7
2 files changed
+220
-57
No files matched your search
@@ -263,6 +263,60 @@ static int is_ldconfig_name(const char *name) {
|
||||
return strcmp(name, "ldconfig") == 0 || strcmp(name, "ldconfig.real") == 0;
|
||||
}
|
||||
|
||||
struct ldso_direct {
|
||||
char prog[PATH_MAX];
|
||||
char lib_path[PATH_MAX * 6];
|
||||
};
|
||||
|
||||
// ld.so started directly (ldd, "ld.so --list PROGRAM", "ld.so PROGRAM"): the program it loads
|
||||
// gets the --library-path a start of its own gets, or ldd reports its libraries as not found,
|
||||
// and its path mapped into the rootfs, since ld.so opens it without the hook
|
||||
static char **ldso_direct_argv(char *const argv[], char *const envp[], struct ldso_direct *buf, int *err) {
|
||||
*err = 0;
|
||||
int caller_lib_path = 0;
|
||||
int pi = ldso_program_index(argv, &caller_lib_path);
|
||||
if (pi <= 0) return NULL;
|
||||
int argc = 0;
|
||||
while (argv[argc]) argc++;
|
||||
const char *prog = argv[pi];
|
||||
if (caller_lib_path) {
|
||||
// the caller's own --library-path stays as it is; only the program is mapped (when it is
|
||||
// in the rootfs: a caller can also name a file by its real path)
|
||||
if (prog[0] == '/') {
|
||||
const char *mapped = rewrite_path(prog, buf->prog, sizeof(buf->prog));
|
||||
int fd = mapped != prog ? (int)syscall(SYS_openat, AT_FDCWD, mapped, O_PATH | O_CLOEXEC) : -1;
|
||||
if (fd >= 0) {
|
||||
syscall(SYS_close, fd);
|
||||
prog = mapped;
|
||||
}
|
||||
}
|
||||
char **out = (char **)malloc(sizeof(char *) * (size_t)(argc + 1));
|
||||
if (!out) {
|
||||
*err = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
for (int i = 0; i <= argc; i++) out[i] = argv[i];
|
||||
out[pi] = (char *)prog;
|
||||
return out;
|
||||
}
|
||||
if (prog[0] == '/') prog = rewrite_path(prog, buf->prog, sizeof(buf->prog));
|
||||
struct exec_probe prog_probe;
|
||||
probe_exec_file(prog, &prog_probe);
|
||||
build_ld_library_path(envp, prog_probe.runpath, prog_probe.runpath_is_rpath,
|
||||
prog[0] == '/' ? prog : NULL, buf->lib_path, sizeof(buf->lib_path));
|
||||
char **out = (char **)malloc(sizeof(char *) * (size_t)(argc + 3));
|
||||
if (!out) {
|
||||
*err = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
out[0] = argv[0];
|
||||
out[1] = (char *)"--library-path";
|
||||
out[2] = buf->lib_path;
|
||||
for (int i = 1; i <= argc; i++) out[i + 2] = argv[i];
|
||||
out[pi + 2] = (char *)prog; // ld.so opens it itself: "ldd /usr/bin/adb" as well
|
||||
return out;
|
||||
}
|
||||
|
||||
static int ddnatv_execve(const char *filename, char *const argv[], char *const envp[], int depth) {
|
||||
orig_execve_f_type orig_execve = real_execve();
|
||||
if (!orig_execve) {
|
||||
@@ -388,63 +442,15 @@ static int ddnatv_execve(const char *filename, char *const argv[], char *const e
|
||||
}
|
||||
}
|
||||
|
||||
// ld.so started directly (ldd, "ld.so --list PROGRAM", "ld.so PROGRAM"): the program it loads
|
||||
// gets the --library-path a start of its own gets, or ldd reports its libraries as not found,
|
||||
// and its path mapped into the rootfs, since ld.so opens it without the hook
|
||||
if (probe.is_elf && !probe.is_dynamic && is_ld_linux(elf_target)) {
|
||||
int caller_lib_path = 0;
|
||||
int pi = ldso_program_index(argv, &caller_lib_path);
|
||||
if (pi > 0 && caller_lib_path) {
|
||||
// the caller's own --library-path stays as it is; only the program is mapped (when it is
|
||||
// in the rootfs: a caller can also name a file by its real path)
|
||||
char prog_buf[PATH_MAX];
|
||||
const char *prog = argv[pi];
|
||||
if (prog[0] == '/') {
|
||||
const char *mapped = rewrite_path(prog, prog_buf, sizeof(prog_buf));
|
||||
int fd = mapped != prog ? (int)syscall(SYS_openat, AT_FDCWD, mapped, O_PATH | O_CLOEXEC) : -1;
|
||||
if (fd >= 0) {
|
||||
syscall(SYS_close, fd);
|
||||
prog = mapped;
|
||||
}
|
||||
}
|
||||
int argc = 0;
|
||||
while (argv[argc]) argc++;
|
||||
char **new_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 1));
|
||||
if (!new_argv) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i <= argc; i++) new_argv[i] = argv[i];
|
||||
new_argv[pi] = (char *)prog;
|
||||
prepare_static_program(static_elf); // ld.so is a static program itself
|
||||
char **ddnatv_env = prepare_ddnatv_env(envp, target);
|
||||
int ret = run_execve(orig_execve, target, new_argv, ddnatv_env);
|
||||
int saved_errno = errno;
|
||||
free_modified_env(ddnatv_env, envp);
|
||||
free(new_argv);
|
||||
errno = saved_errno;
|
||||
return ret;
|
||||
struct ldso_direct direct;
|
||||
int err;
|
||||
char **new_argv = ldso_direct_argv(argv, envp, &direct, &err);
|
||||
if (err) {
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
if (pi > 0) {
|
||||
char prog_buf[PATH_MAX];
|
||||
const char *prog = argv[pi][0] == '/' ? rewrite_path(argv[pi], prog_buf, sizeof(prog_buf)) : argv[pi];
|
||||
struct exec_probe prog_probe;
|
||||
probe_exec_file(prog, &prog_probe);
|
||||
char ld_lib_path[PATH_MAX * 6];
|
||||
build_ld_library_path(envp, prog_probe.runpath, prog_probe.runpath_is_rpath,
|
||||
prog[0] == '/' ? prog : NULL, ld_lib_path, sizeof(ld_lib_path));
|
||||
int argc = 0;
|
||||
while (argv[argc]) argc++;
|
||||
char **new_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 3));
|
||||
if (!new_argv) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
new_argv[0] = argv[0];
|
||||
new_argv[1] = (char *)"--library-path";
|
||||
new_argv[2] = ld_lib_path;
|
||||
for (int i = 1; i <= argc; i++) new_argv[i + 2] = argv[i];
|
||||
new_argv[pi + 2] = (char *)prog; // ld.so opens it itself: "ldd /usr/bin/adb" as well
|
||||
if (new_argv) {
|
||||
prepare_static_program(static_elf); // ld.so is a static program itself
|
||||
char **ddnatv_env = prepare_ddnatv_env(envp, target);
|
||||
int ret = run_execve(orig_execve, target, new_argv, ddnatv_env);
|
||||
@@ -968,9 +974,19 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path,
|
||||
}
|
||||
} else {
|
||||
if (in_root && probe.is_elf && !probe.is_dynamic) prepare_static_program(elf_target);
|
||||
if (orig_posix_spawn) {
|
||||
ret = run_spawn(orig_posix_spawn, pid, target, file_actions, attrp, argv, new_envp);
|
||||
char **spawn_argv = (char **)argv;
|
||||
int err = 0;
|
||||
struct ldso_direct direct;
|
||||
if (in_root && probe.is_elf && !probe.is_dynamic && is_ld_linux(elf_target)) {
|
||||
char **direct_argv = ldso_direct_argv(argv, orig_ep, &direct, &err);
|
||||
if (direct_argv) spawn_argv = direct_argv;
|
||||
}
|
||||
if (err) {
|
||||
ret = err;
|
||||
} else if (orig_posix_spawn) {
|
||||
ret = run_spawn(orig_posix_spawn, pid, target, file_actions, attrp, spawn_argv, new_envp);
|
||||
}
|
||||
if (spawn_argv != argv) free(spawn_argv);
|
||||
}
|
||||
|
||||
if (!orig_posix_spawn) {
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
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"
|
||||
|
||||
RECORDER = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void show(const char *path, char *const argv[]) {
|
||||
printf("call\t%s", path);
|
||||
for (int i = 0; argv[i]; i++) printf("\t%s", argv[i]);
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *actions,
|
||||
const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) {
|
||||
show(path, argv);
|
||||
if (pid) *pid = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int execve(const char *path, char *const argv[], char *const envp[]) {
|
||||
show(path, argv);
|
||||
_exit(0);
|
||||
}
|
||||
"""
|
||||
|
||||
PROBE = r"""
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern char **environ;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 3) return 2;
|
||||
if (strcmp(argv[1], "spawn") == 0) {
|
||||
pid_t pid;
|
||||
posix_spawn_file_actions_t actions;
|
||||
posix_spawn_file_actions_init(&actions);
|
||||
posix_spawn_file_actions_adddup2(&actions, 1, 1);
|
||||
int ret = posix_spawn(&pid, argv[2], &actions, NULL, argv + 2, environ);
|
||||
posix_spawn_file_actions_destroy(&actions);
|
||||
printf("ret\t%d\n", ret);
|
||||
return 0;
|
||||
}
|
||||
execve(argv[2], argv + 2, environ);
|
||||
printf("ret\t-1\n");
|
||||
return 1;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def guest_loader(prefix):
|
||||
roots = [Path(prefix[prefix.index("-L") + 1])] if prefix and "-L" in prefix else [Path("/")]
|
||||
for root in roots:
|
||||
for name in ("lib/ld-linux-aarch64.so.1", "lib64/ld-linux-aarch64.so.1", "usr/lib/ld-linux-aarch64.so.1"):
|
||||
if (root / name).exists():
|
||||
return root / name
|
||||
return None
|
||||
|
||||
|
||||
class NativeHookLdsoSpawnTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.cc, cls.prefix = runner()
|
||||
if cls.cc is None:
|
||||
raise unittest.SkipTest("no aarch64 compiler and runner")
|
||||
loader = guest_loader(cls.prefix)
|
||||
if loader is None:
|
||||
raise unittest.SkipTest("no aarch64 glibc loader to put in the rootfs")
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
tmp = Path(cls.tmp.name)
|
||||
cls.hook = tmp / "hook.so"
|
||||
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
|
||||
(tmp / "recorder.c").write_text(RECORDER)
|
||||
cls.recorder = tmp / "recorder.so"
|
||||
subprocess.run([cls.cc, "-shared", "-fPIC", "-O1", "-o", str(cls.recorder), str(tmp / "recorder.c")], check=True)
|
||||
(tmp / "probe.c").write_text(PROBE)
|
||||
cls.probe = tmp / "probe"
|
||||
subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True)
|
||||
cls.root = tmp / "root"
|
||||
for guest, source in ((GUEST_LDSO, loader), (GUEST_TOOL, cls.probe)):
|
||||
host = cls.root / guest.lstrip("/")
|
||||
host.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy(source, host)
|
||||
host.chmod(0o755)
|
||||
(cls.root / "usr/lib/aarch64-linux-gnu").mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def call(self, how, *argv):
|
||||
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": f"{self.hook}:{self.recorder}"}
|
||||
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}")]
|
||||
else:
|
||||
cmd = []
|
||||
env.update(hook_env)
|
||||
out = subprocess.run(cmd + [str(self.probe), how, *argv], env=env, capture_output=True, text=True, timeout=120)
|
||||
calls = [line.split("\t")[1:] for line in out.stdout.splitlines() if line.startswith("call\t")]
|
||||
self.assertEqual(1, len(calls), out.stdout + out.stderr)
|
||||
if how == "spawn":
|
||||
self.assertIn("ret\t0", out.stdout.splitlines(), out.stdout + out.stderr)
|
||||
return calls[0]
|
||||
|
||||
def test_spawned_ldso_list_loads_the_program_from_the_rootfs(self):
|
||||
call = self.call("spawn", GUEST_LDSO, "--list", GUEST_TOOL)
|
||||
argv = call[1:]
|
||||
self.assertEqual(str(self.root / GUEST_TOOL.lstrip("/")), argv[-1])
|
||||
self.assertEqual("--list", argv[-2])
|
||||
self.assertIn("--library-path", argv[:-2])
|
||||
|
||||
def test_spawn_and_exec_start_ldso_with_the_same_arguments(self):
|
||||
for args in (("--list", GUEST_TOOL), (GUEST_TOOL, "x"), ("--library-path", "/opt/own", "--list", GUEST_TOOL)):
|
||||
with self.subTest(args=args):
|
||||
self.assertEqual(self.call("exec", GUEST_LDSO, *args), self.call("spawn", GUEST_LDSO, *args))
|
||||
|
||||
def test_a_callers_own_library_path_is_kept(self):
|
||||
call = self.call("spawn", GUEST_LDSO, "--library-path", "/opt/own", "--list", GUEST_TOOL)
|
||||
self.assertEqual([GUEST_LDSO, "--library-path", "/opt/own", "--list", str(self.root / GUEST_TOOL.lstrip("/"))], call[1:])
|
||||
|
||||
def test_ldso_without_a_program_is_spawned_unchanged(self):
|
||||
call = self.call("spawn", GUEST_LDSO, "--version")
|
||||
self.assertEqual([GUEST_LDSO, "--version"], call[1:])
|
||||
|
||||
def test_spawn_and_exec_start_a_dynamic_program_the_same_way(self):
|
||||
self.assertEqual(self.call("exec", GUEST_TOOL, "x"), self.call("spawn", GUEST_TOOL, "x"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user