From 99aa2824add2b452d33cca24af67bdc734e14343 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Thu, 26 Nov 2026 00:59:39 -0500 Subject: [PATCH] perf(nativeguest): keep relative parent walks relative inside mounts without links A relative path with .. made the hook rebuild the full absolute host path, so every component was looked up again from the root of the card. Inside a mount that cannot hold symlinks, a path that never climbs above the mount resolves the same way relative to the working directory, so the kernel now gets it unchanged. --- tools/nativeguest/hook/paths.c | 11 +++++++++++ tools/tests/test_native_hook_mounts.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tools/nativeguest/hook/paths.c b/tools/nativeguest/hook/paths.c index 99e98f953d..cc64a17dad 100644 --- a/tools/nativeguest/hook/paths.c +++ b/tools/nativeguest/hook/paths.c @@ -1081,6 +1081,16 @@ static void ddnatv_build_at_dirs(void) { } #if defined(__aarch64__) +static int stays_in_plain_bind(const char *host, const char *rel) { + struct ddnatv_bind *best = NULL; + for (int i = 0; i < g_nbinds; i++) { + struct ddnatv_bind *b = &g_binds[i]; + if (b->identity || !is_path_prefix(host, b->src, b->src_len)) continue; + if (!best || b->src_len > best->src_len) best = b; + } + return best && !climbs_past_root(host + best->src_len, rel) && bind_holds_no_links(best); +} + 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"); @@ -1088,6 +1098,7 @@ static const char *map_parent_walk(int dirfd, const char *rel, char *buf, size_t long n = raw_readlinkat(AT_FDCWD, link, host, sizeof(host) - 1); if (n <= 0 || host[0] != '/') return NULL; host[n] = '\0'; + if (stays_in_plain_bind(host, rel)) return NULL; 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; diff --git a/tools/tests/test_native_hook_mounts.py b/tools/tests/test_native_hook_mounts.py index 08498207e9..5f675724be 100644 --- a/tools/tests/test_native_hook_mounts.py +++ b/tools/tests/test_native_hook_mounts.py @@ -482,6 +482,23 @@ class NativeHookMountTest(unittest.TestCase): self.assertNotEqual(self.readlinks_of(log, f"{self.links}/guest"), [], table) self.assertNotEqual(self.readlinks_of(log, f"{self.root}/etc/hostname"), [], table) + def test_relative_parent_walks_inside_mounts_without_links_stay_relative(self): + real, _, _ = self.links_volume() + binds = f"{self.links}:/mnt/links" + paths = {"sub/deeper/../file.txt": ["1", "deep"], "sub/deeper/more/../more/leaf.txt": ["1", "leaf"], + "../real/sub/file.txt": ["1", "deep"], "../../../etc/hostname": ["1", "rootfs"], + "sub/../../../../etc/hostname": ["1", "rootfs"]} + for kind, relative in (("exfat", True), ("ext4", False)): + table = [("/", "ext4", "/dev/root"), (real, kind, "/dev/sdb1")] + result, log = self.run_probe("cd:/mnt/links/real", *paths, binds=binds, mounts=table, syscalls=True) + for path, want in paths.items(): + self.assertEqual(result[path], want, (path, kind)) + stats = [m.group(1) for m in re.finditer(r'(?:fstatat|statx)\([^,"]*,\s*"([^"]*)"', log)] + for path in ("sub/deeper/../file.txt", "sub/deeper/more/../more/leaf.txt", "../real/sub/file.txt"): + self.assertEqual(path in stats, relative, (path, kind, stats)) + for path in ("../../../etc/hostname", "sub/../../../../etc/hostname"): + self.assertNotIn(path, stats, (path, kind)) + if __name__ == "__main__": unittest.main()