diff --git a/tools/nativeguest/hook/paths.c b/tools/nativeguest/hook/paths.c index fc6c49e507..fcfe12cc43 100644 --- a/tools/nativeguest/hook/paths.c +++ b/tools/nativeguest/hook/paths.c @@ -188,6 +188,25 @@ static __thread int t_bind_link_depth = 0; static void normalize_path(char *path); +static int climbs_past_root(const char *dir, const char *target) { + long depth = 0; + for (const char *p = dir; *p; p++) + if (*p != '/' && (p == dir || p[-1] == '/')) depth++; + for (const char *p = target; *p;) { + while (*p == '/') p++; + const char *end = p; + while (*end && *end != '/') end++; + size_t len = (size_t)(end - p); + if (len == 2 && p[0] == '.' && p[1] == '.') { + if (--depth < 0) return 1; + } else if (len > 0 && !(len == 1 && p[0] == '.')) { + depth++; + } + p = end; + } + return 0; +} + #if defined(__aarch64__) static long raw_link_dir(const char *link, char *out, size_t size) { const char *slash = strrchr(link, '/'); @@ -215,25 +234,6 @@ static long raw_link_dir(const char *link, char *out, size_t size) { return n; } -static int climbs_past_root(const char *dir, const char *target) { - long depth = 0; - for (const char *p = dir; *p; p++) - if (*p != '/' && (p == dir || p[-1] == '/')) depth++; - for (const char *p = target; *p;) { - while (*p == '/') p++; - const char *end = p; - while (*end && *end != '/') end++; - size_t len = (size_t)(end - p); - if (len == 2 && p[0] == '.' && p[1] == '.') { - if (--depth < 0) return 1; - } else if (len > 0 && !(len == 1 && p[0] == '.')) { - depth++; - } - p = end; - } - return 0; -} - static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap, int *converted); static const char *guest_path_of_host_dir(const char *dir, char *out, size_t size) { @@ -335,6 +335,15 @@ static const char *resolve_drive_links(const char *host, char *buffer, size_t bu // Maps an absolute Ubuntu path into the rootfs. Returns `path` (possibly advanced past // redundant leading "/" and "/." components) when it needs no change, else `buffer`. +static const char *rewrite_normalized_in_root(const char *path, char *buffer, size_t bufsize) { + char normalized[PATH_MAX]; + ddnatv_copy_cstr(normalized, sizeof(normalized), path); + normalize_path(normalized); + const char *mapped = rewrite_path_in_root(normalized, buffer, bufsize); + if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped); + return buffer; +} + static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize) { if (!path) return NULL; if (path[0] != '/' || g_ddnatv_root_len == 0) return path; @@ -354,6 +363,8 @@ static const char *rewrite_path_in_root(const char *path, char *buffer, size_t b } } + if (strstr(path, "/..") && climbs_past_root("", path)) return rewrite_normalized_in_root(path, buffer, bufsize); + // Root directory if (path[1] == '\0') { ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); @@ -364,6 +375,8 @@ static const char *rewrite_path_in_root(const char *path, char *buffer, size_t b const struct ddnatv_bind *b = &g_binds[i]; if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { if (b->identity) return path; + if (strstr(path + b->dst_len, "/..") && climbs_past_root("", path + b->dst_len)) + return rewrite_normalized_in_root(path, buffer, bufsize); size_t n = ddnatv_append(buffer, bufsize, 0, b->src, b->src_len); ddnatv_append(buffer, bufsize, n, path + b->dst_len, strlen(path + b->dst_len)); return resolve_bind_links(buffer, b->src_len, buffer, bufsize); @@ -806,10 +819,36 @@ static void ddnatv_build_at_dirs(void) { } } +#if defined(__aarch64__) +static const char *map_parent_walk(int dirfd, const char *rel, char *buf, size_t size) { + char link[32], host[PATH_MAX], dir_buf[PATH_MAX], guest[PATH_MAX]; + if (dirfd == AT_FDCWD) ddnatv_copy_cstr(link, sizeof(link), "/proc/self/cwd"); + else snprintf(link, sizeof(link), "/proc/self/fd/%d", dirfd); + long n = raw_readlinkat(AT_FDCWD, link, host, sizeof(host) - 1); + if (n <= 0 || host[0] != '/') return NULL; + host[n] = '\0'; + const char *dir = guest_path_of_host_dir(host, dir_buf, sizeof(dir_buf)); + if (!dir || strcmp(dir, host) == 0) return NULL; + if (get_rootfs_subpath(host) && !climbs_past_root(dir, rel)) return NULL; + size_t used = ddnatv_append(guest, sizeof(guest), 0, dir, strlen(dir)); + used = ddnatv_append(guest, sizeof(guest), used, "/", 1); + used = ddnatv_append(guest, sizeof(guest), used, rel, strlen(rel)); + if (used + 1 >= sizeof(guest)) return NULL; + const char *mapped = rewrite_path_in_root(guest, buf, size); + if (mapped != buf) ddnatv_copy_cstr(buf, size, mapped); + return buf; +} +#endif + // The absolute host path for `rel` given relative to `dirfd`, or NULL when the call needs no change. static const char *map_at(int dirfd, const char *rel, char *buf, size_t size) { #if defined(__aarch64__) - if (dirfd == AT_FDCWD || !rel || rel[0] == '\0' || rel[0] == '/' || g_nbinds == 0) return NULL; + if (!rel || rel[0] == '\0' || rel[0] == '/') return NULL; + if (strstr(rel, "..")) { + const char *walked = map_parent_walk(dirfd, rel, buf, size); + if (walked || dirfd == AT_FDCWD) return walked; + } + if (dirfd == AT_FDCWD || g_nbinds == 0) return NULL; pthread_once(&g_at_once, ddnatv_build_at_dirs); if (g_n_at_dirs == 0) return NULL; struct stat st; @@ -818,6 +857,7 @@ static const char *map_at(int dirfd, const char *rel, char *buf, size_t size) { if (g_at_dirs[i].ino != (unsigned long)st.st_ino || g_at_dirs[i].dev != (unsigned long)st.st_dev) continue; char guest[PATH_MAX]; const char *base = g_at_dirs[i].guest; + if (base[1] && climbs_past_root(base, rel)) return NULL; size_t used = ddnatv_append(guest, sizeof(guest), 0, base, strlen(base)); if (used > 1) used = ddnatv_append(guest, sizeof(guest), used, "/", 1); ddnatv_append(guest, sizeof(guest), used, rel, strlen(rel)); diff --git a/tools/nativeguest/hook/setup.c b/tools/nativeguest/hook/setup.c index bce43c5ad4..e0587d0b4f 100644 --- a/tools/nativeguest/hook/setup.c +++ b/tools/nativeguest/hook/setup.c @@ -363,8 +363,11 @@ static ssize_t handle_proc_exe_at(int dirfd, const char *pathname, char *buf, si static const char *rewrite_path(const char *path, char *buffer, size_t bufsize) { if (!path) return NULL; // Relative paths must NEVER be rewritten to rootfs root - if (path[0] != '/') return path; init_ddnatv_hook(); + if (path[0] != '/') { + const char *walked = map_at(AT_FDCWD, path, buffer, bufsize); + return walked ? walked : path; + } return rewrite_path_in_root(path, buffer, bufsize); } @@ -374,7 +377,7 @@ static const char *rewrite_path(const char *path, char *buffer, size_t bufsize) // followed in the guest's view first. The rootfs's own links were made relative already. static const char *rewrite_path_follow(const char *path, char *buffer, size_t bufsize) { if (!path) return NULL; - if (path[0] != '/') return path; + if (path[0] != '/') return rewrite_path(path, buffer, bufsize); init_ddnatv_hook(); int moved = 0; for (int i = 0; i < g_nbinds; i++) { diff --git a/tools/tests/test_native_hook_mounts.py b/tools/tests/test_native_hook_mounts.py index eaed2dded7..9f82bb0149 100644 --- a/tools/tests/test_native_hook_mounts.py +++ b/tools/tests/test_native_hook_mounts.py @@ -26,6 +26,19 @@ int main(int argc, char **argv) { printf("%s\t%s\n", argv[i], value ? value : "-"); continue; } + if (strncmp(argv[i], "parent:", 7) == 0) { + char dir[4096]; + snprintf(dir, sizeof(dir), "%s", argv[i] + 7); + char *rel = strchr(dir, '|'), *want = NULL; + if (rel) *rel++ = 0; + if (rel && (want = strchr(rel, '|'))) *want++ = 0; + int fd = open(dir, O_RDONLY | O_DIRECTORY); + struct stat expected; + int ok = fd >= 0 && fstatat(fd, rel ? rel : "..", &st, 0) == 0 && stat(want ? want : "/", &expected) == 0; + printf("%s\t%d\n", argv[i], ok && st.st_dev == expected.st_dev && st.st_ino == expected.st_ino); + if (fd >= 0) close(fd); + continue; + } if (strncmp(argv[i], "cd:", 3) == 0) { int ok = chdir(argv[i] + 3) == 0 && getcwd(cwd, sizeof(cwd)) != NULL; printf("%s\t%s\n", argv[i], ok ? cwd : "-"); @@ -82,6 +95,8 @@ class NativeHookMountTest(unittest.TestCase): cls.root = tmp / "root" cls.library = tmp / "storage/PC GAMES" (cls.root / "etc").mkdir(parents=True) + (cls.root / "deep/er/est").mkdir(parents=True) + (cls.root / "linked").symlink_to("deep/er/est") (cls.root / "etc/hostname").write_text("rootfs") (cls.root / "mnt/library/Some Game").mkdir(parents=True) (cls.root / "mnt/library/Some Game/game.txt").write_text("mount point") @@ -123,8 +138,9 @@ class NativeHookMountTest(unittest.TestCase): def tearDownClass(cls): cls.tmp.cleanup() - def run_probe(self, *args): - binds = f"{self.library}:/mnt/library|{self.private}:/mnt/private|{self.eqlen_src}:{self.eqlen_dst}" + def run_probe(self, *args, binds=None): + if binds is None: + binds = f"{self.library}:/mnt/library|{self.private}:/mnt/private|{self.eqlen_src}:{self.eqlen_dst}" hook_env = {"DDNATV_ROOT": str(self.root), "DDNATV_BINDS": binds, "LD_PRELOAD": str(self.hook)} env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "LOCPATH", "GCONV_PATH")} if self.prefix: @@ -158,6 +174,37 @@ class NativeHookMountTest(unittest.TestCase): self.assertEqual(result["env:LOCPATH"], [f"{self.root}/usr/lib/locale"]) self.assertEqual(result["env:GCONV_PATH"], [f"{self.root}/usr/lib/aarch64-linux-gnu/gconv"]) + def test_parent_of_root_stays_at_root(self): + self.assertEqual(self.run_probe("parent:/")["parent:/"], ["1"]) + + def test_parent_of_root_without_mounts_stays_at_root(self): + self.assertEqual(self.run_probe("parent:/", binds="")["parent:/"], ["1"]) + + def test_parent_of_mount_ancestor_is_guest_root(self): + self.assertEqual(self.run_probe("parent:/mnt")["parent:/mnt"], ["1"]) + + def test_parent_walks_from_any_directory_stay_at_root(self): + cases = ["parent:/etc|../..", "parent:/mnt/library|../../..", "parent:/|../etc/.."] + self.assertEqual(self.run_probe(*cases), {case: ["1"] for case in cases}) + + def test_parent_walk_from_a_linked_mount_ancestor_follows_the_real_directory(self): + binds = f"{self.library}:/linked/library" + case = "parent:/linked|../..|/deep" + self.assertEqual(self.run_probe(case, binds=binds)[case], ["1"]) + + def test_absolute_paths_above_root_stay_at_root(self): + result = self.run_probe("cd:/..", "cd:/etc/../..", "/../etc/hostname") + self.assertEqual(result["cd:/.."], [str(self.root)]) + self.assertEqual(result["cd:/etc/../.."], [str(self.root)]) + self.assertEqual(result["/../etc/hostname"], ["1", "rootfs"]) + + def test_relative_parent_of_cwd_stays_in_guest(self): + result = self.run_probe("cd:/", "cd:..", "cd:./../..", "../etc/hostname", f"cd:{GAME}", "cd:../..") + self.assertEqual(result["cd:.."], [str(self.root)]) + self.assertEqual(result["cd:./../.."], [str(self.root)]) + self.assertEqual(result["cd:../.."], [str(self.root / "mnt")]) + self.assertEqual(result["../etc/hostname"], ["1", "rootfs"]) + def test_rootfs_paths_outside_mounts_are_unchanged(self): paths = ["/pfx/native/dosdevices/z:/etc/hostname", f"{self.root}/etc/hostname", "/etc/hostname"] result = self.run_probe(*paths)