perf(nativeguest): keep relative parent walks relative inside mounts without links (#3249)

* 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.

* Keep the absolute walk when another guest bind sits inside the mount

* Check every guest path of the card's folder for nested binds

* Keep the absolute walk inside the rootfs and under identity binds

* Keep the absolute walk when two binds share a guest path
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-26 09:48:56 -05:00
1 parent 96e4bd4eab
commit dbfcd764f3
2 files changed
+84 -1

No files matched your search

+22
View File
@@ -1081,6 +1081,27 @@ static void ddnatv_build_at_dirs(void) {
}
#if defined(__aarch64__)
static int stays_in_plain_bind(const char *host, const char *rel) {
if (get_rootfs_subpath(host)) return 0;
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;
}
if (!best) return 0;
for (int i = 0; i < g_nbinds; i++) {
struct ddnatv_bind *cover = &g_binds[i];
if (cover->identity || !is_path_prefix(host, cover->src, cover->src_len)) continue;
for (int j = 0; j < g_nbinds; j++) {
struct ddnatv_bind *b = &g_binds[j];
if (j != i && b->dst_len >= cover->dst_len && is_path_prefix(b->dst, cover->dst, cover->dst_len))
return 0;
}
}
return !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 +1109,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;
+62 -1
View File
@@ -195,7 +195,7 @@ class NativeHookMountTest(unittest.TestCase):
if syscalls and not shutil.which("strace"):
self.skipTest("no strace")
if syscalls:
cmd = ["strace", "-f", "-qq", "-e", "trace=readlinkat"]
cmd = ["strace", "-f", "-qq", "-e", "trace=readlinkat,newfstatat,fstatat64,statx"]
cmd += [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
else:
cmd = []
@@ -482,6 +482,67 @@ 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))
nested = f"{binds}|{self.library}:/mnt/links/real/sub"
table = [("/", "ext4", "/dev/root"), (real, "exfat", "/dev/sdb1")]
result, log = self.run_probe("cd:/mnt/links/real", "sub/deeper/../game.txt", binds=nested, mounts=table,
syscalls=True)
stats = [m.group(1) for m in re.finditer(r'(?:fstatat|statx)\([^,"]*,\s*"([^"]*)"', log)]
self.assertNotIn("sub/deeper/../game.txt", stats)
walk = "sub/Some Game/../Some Game/game.txt"
for order in ((f"{self.links}:/mnt/links", f"{self.links}:/mnt/longer/alias"),
(f"{self.links}:/mnt/longer/alias", f"{self.links}:/mnt/links")):
shared = "|".join(order) + f"|{self.library}:/mnt/links/real/sub"
results = [self.run_probe("cd:/mnt/links/real", walk, binds=shared,
mounts=[("/", "ext4", "/dev/root"), (real, kind, "/dev/sdb1")])[walk]
for kind in ("ext4", "exfat")]
self.assertEqual(results[0], results[1], order)
def test_relative_parent_walks_honour_identity_binds_shared_guest_paths_and_the_rootfs(self):
real, _, _ = self.links_volume()
tmp = os.path.realpath(self.tmp.name)
card = f"{tmp}/identity-card"
os.makedirs(f"{card}/real/sub/deeper", exist_ok=True)
Path(f"{card}/real/sub/file.txt").write_text("identity")
binds = f"{self.links}:{card}|{card}/real/sub:{card}/real/sub"
walk = "sub/deeper/../file.txt"
results = [self.run_probe(f"cd:{card}/real", walk, binds=binds,
mounts=[("/", "ext4", "/dev/root"), (real, kind, "/dev/sdb1")])[walk]
for kind in ("ext4", "exfat")]
self.assertEqual(results[0], results[1])
twins = (f"{tmp}/twin-a", f"{tmp}/twin-b")
for twin in twins:
os.makedirs(f"{twin}/only/sub/deeper", exist_ok=True)
Path(f"{twin}/only/sub/file.txt").write_text(os.path.basename(twin))
for order in (twins, twins[::-1]):
shared = "|".join(f"{src}:/mnt/twin" for src in order)
result, log = self.run_probe("cd:/mnt/twin/only", walk, binds=shared, syscalls=True,
mounts=[("/", "ext4", "/dev/root")] +
[(twin, "exfat", f"/dev/sd{i}1") for i, twin in zip("bc", twins)])
stats = [m.group(1) for m in re.finditer(r'(?:fstatat|statx)\([^,"]*,\s*"([^"]*)"', log)]
self.assertEqual(result[walk], ["1", os.path.basename(order[0])], order)
self.assertNotIn(walk, stats, order)
climb = "../../etc/hostname"
for kind in ("ext4", "exfat"):
result = self.run_probe("cd:/etc", climb, binds=f"{tmp}:/mnt/card",
mounts=[("/", "ext4", "/dev/root"), (tmp, kind, "/dev/sdb1")])
self.assertEqual(result[climb], ["1", "rootfs"], kind)
if __name__ == "__main__":
unittest.main()