fix(native): drive links stored as rootfs paths reach the game library mount (#2522)

* fix(native): drive links stored as rootfs paths reach the game library mount

* fix(native): relative drive links reach the game library mount

* fix(native): keep the path after a relative drive link as written

* fix(native): relative links that climb past the rootfs root stop at the guest root

Proton copies default_pfx/dosdevices/z: into each prefix with readlink and
symlink. The relinked target climbs as many folders as default_pfx is deep,
which is one more than a Steam prefix, so natively the kernel left the rootfs
and Z: pointed at the app's files folder. Proot stops ".." at the guest root;
the hook now does the same for links it resolves.

* fix(native): resolve drive links in prefixes stored in bind sources outside the rootfs

* fix(native): detect equal-length mount conversion for bound prefixes
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-13 18:09:15 -05:00
1 parent bc929c08b2
commit 6d1a0d9fd0
3 files changed
+353 -8

No files matched your search

+2 -2
View File
@@ -700,7 +700,7 @@ char *getcwd(char *buf, size_t size) {
char *res = orig_getcwd ? orig_getcwd(buf, size) : NULL;
if (res) {
size_t len = strlen(res);
guest_path_of_mount_source(res, len, buf ? size : len + 1);
guest_path_of_mount_source(res, len, buf ? size : len + 1, NULL);
}
return res;
}
@@ -749,7 +749,7 @@ char *realpath(const char *path, char *resolved_path) {
}
}
}
if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1);
if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1, NULL);
return res;
}
+127 -6
View File
@@ -163,6 +163,108 @@ static long raw_readlinkat(int dirfd, const char *path, char *buf, size_t size)
static __thread int t_bind_link_depth = 0;
static void normalize_path(char *path);
#if defined(__aarch64__)
static long raw_link_dir(const char *link, char *out, size_t size) {
const char *slash = strrchr(link, '/');
if (!slash || (size_t)(slash - link) >= PATH_MAX) return -1;
char dir[PATH_MAX];
size_t dlen = slash == link ? 1 : (size_t)(slash - link);
memcpy(dir, link, dlen);
dir[dlen] = '\0';
register long x8 __asm__("x8") = 56;
register long x0 __asm__("x0") = AT_FDCWD;
register long x1 __asm__("x1") = (long)dir;
register long x2 __asm__("x2") = O_PATH | O_DIRECTORY | O_CLOEXEC;
register long x3 __asm__("x3") = 0;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
long fd = x0;
if (fd < 0) return -1;
char proc[32];
snprintf(proc, sizeof(proc), "/proc/self/fd/%ld", fd);
long n = raw_readlinkat(AT_FDCWD, proc, out, size - 1);
register long c8 __asm__("x8") = 57;
register long c0 __asm__("x0") = fd;
__asm__ volatile("svc #0" : "+r"(c0) : "r"(c8) : "memory");
if (n <= 0 || out[0] != '/') return -1;
out[n] = '\0';
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) {
const char *sub = get_rootfs_subpath(dir);
if (sub) {
ddnatv_copy_cstr(out, size, sub[0] ? sub : "/");
return out;
}
if (dir[0] != '/') return NULL;
size_t len = ddnatv_copy_cstr(out, size, dir);
int converted = 0;
guest_path_of_mount_source(out, len, size, &converted);
if (converted) return out;
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];
if (b->identity && is_path_prefix(dir, b->dst, b->dst_len)) {
ddnatv_copy_cstr(out, size, dir);
return out;
}
}
return NULL;
}
static int join_into_bind(const char *dir, const char *target, const char *rest, char *out, size_t size) {
char guest_dir_buf[PATH_MAX];
const char *guest_dir = guest_path_of_host_dir(dir, guest_dir_buf, sizeof(guest_dir_buf));
if (!guest_dir) return 0;
char joined[PATH_MAX];
size_t used = ddnatv_append(joined, sizeof(joined), 0, guest_dir, strlen(guest_dir));
used = ddnatv_append(joined, sizeof(joined), used, "/", 1);
used = ddnatv_append(joined, sizeof(joined), used, target, strlen(target));
if (used + 1 >= sizeof(joined)) return 0;
normalize_path(joined);
used = ddnatv_append(out, size, 0, joined, strlen(joined));
if (used + 1 >= size) return 0;
if (rest[0] == '/' && used > 0 && out[used - 1] == '/') rest++;
size_t rest_len = strlen(rest);
if (used + rest_len + 1 >= size) return 0;
ddnatv_append(out, size, used, rest, rest_len);
return out[0] == '/';
}
static int relative_link_into_bind(const char *link, const char *target, const char *rest, char *out, size_t size) {
char dir[PATH_MAX];
if (raw_link_dir(link, dir, sizeof(dir)) >= 0) return join_into_bind(dir, target, rest, out, size);
const char *slash = strrchr(link, '/');
if (!slash) return 0;
size_t dlen = (size_t)(slash - link);
memcpy(dir, link, dlen);
dir[dlen] = '\0';
return join_into_bind(dir, target, rest, out, size);
}
#endif
// A mount stand-in can hold symbolic links with absolute guest targets (/sys/class/hidraw/hidraw16 ->
// /sys/devices/droiddeck/...): proot follows them inside the guest, the kernel on Android's own /sys.
// A link met on the way to a deeper path is replaced by its target, mapped again; the last component is
@@ -180,10 +282,13 @@ static const char *resolve_bind_links(const char *host, size_t src_len, char *bu
long n = raw_readlinkat(AT_FDCWD, prefix, target, sizeof(target) - 1);
if (n <= 0) continue;
target[n] = '\0';
if (target[0] != '/') continue;
char guest[PATH_MAX];
size_t used = ddnatv_append(guest, sizeof(guest), 0, target, (size_t)n);
ddnatv_append(guest, sizeof(guest), used, host + i, len - i);
if (target[0] != '/') {
if (!relative_link_into_bind(prefix, target, host + i, guest, sizeof(guest))) continue;
} else {
size_t used = ddnatv_append(guest, sizeof(guest), 0, target, (size_t)n);
ddnatv_append(guest, sizeof(guest), used, host + i, len - i);
}
t_bind_link_depth++;
const char *mapped = rewrite_path_in_root(guest, buffer, bufsize);
if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped);
@@ -243,7 +348,21 @@ static const char *rewrite_path_in_root(const char *path, char *buffer, size_t b
}
// Already inside The native userland rootfs
if (get_rootfs_subpath(path) != NULL) {
const char *sub = get_rootfs_subpath(path);
if (sub != NULL) {
if (sub[0] == '/' && t_bind_link_depth < 8) {
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];
if (b->identity || sub[1] != b->dst[1] || !is_path_prefix(sub, b->dst, b->dst_len)) continue;
char guest[PATH_MAX];
ddnatv_copy_cstr(guest, sizeof(guest), sub);
t_bind_link_depth++;
const char *mapped = rewrite_path_in_root(guest, buffer, bufsize);
if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped);
t_bind_link_depth--;
return buffer;
}
}
return resolve_drive_links(path, buffer, bufsize);
}
@@ -446,7 +565,8 @@ static int is_proc_fd_link(const char *path) {
// reports as its real place (getcwd(), /proc/self/cwd and fd links). Wine found no DOS drive for
// Sekiro's folder on the SD card library ("starting in the Windows directory"), and the game quit.
// Rewrites buf[0..len) in place when the guest path fits in cap; returns the new length.
static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap) {
static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap, int *converted) {
if (converted) *converted = 0;
const struct ddnatv_bind *best = NULL;
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];
@@ -460,6 +580,7 @@ static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap) {
memmove(buf + best->dst_len, buf + best->src_len, rest);
memcpy(buf, best->dst, best->dst_len);
buf[best->dst_len + rest] = '\0';
if (converted) *converted = 1;
return best->dst_len + rest;
}
@@ -469,7 +590,7 @@ static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret
buf[ret] = '\0';
ret = (ssize_t)link_root_guest_target(buf, (size_t)ret);
}
ret = (ssize_t)guest_path_of_mount_source(buf, (size_t)ret, bufsiz);
ret = (ssize_t)guest_path_of_mount_source(buf, (size_t)ret, bufsiz, NULL);
// 1. Strip g_ddnatv_root if target starts with it
if (g_ddnatv_root_len > 0 && !is_proc_fd_link(linkpath)) {
+224
View File
@@ -0,0 +1,224 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from tools.tests.test_usr2_preload import runner
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
PROBE = r"""
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char **argv) {
char cwd[4096];
for (int i = 1; i < argc; i++) {
struct stat st;
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 : "-");
continue;
}
if (strncmp(argv[i], "at:", 3) == 0) {
int dfd = open(argv[i] + 3, O_RDONLY | O_DIRECTORY);
printf("%s\t%d\t%d\n", argv[i], fstatat(AT_FDCWD, argv[i] + 3, &st, 0) == 0, dfd >= 0);
if (dfd >= 0) close(dfd);
continue;
}
if (strncmp(argv[i], "ls:", 3) == 0) {
DIR *dir = opendir(argv[i] + 3);
char names[1024] = "";
struct dirent *de;
while (dir && (de = readdir(dir)) != NULL) {
if (de->d_name[0] == '.') continue;
if (names[0]) strncat(names, ",", sizeof(names) - strlen(names) - 1);
strncat(names, de->d_name, sizeof(names) - strlen(names) - 1);
}
if (dir) closedir(dir);
printf("%s\t%s\n", argv[i], dir ? names : "-");
continue;
}
int fd = open(argv[i], O_RDONLY);
char data[64] = "-";
if (fd >= 0) {
ssize_t n = read(fd, data, sizeof(data) - 1);
data[n > 0 ? n : 0] = 0;
close(fd);
}
printf("%s\t%d\t%s\n", argv[i], stat(argv[i], &st) == 0, data);
}
return 0;
}
"""
GAME = "/mnt/library/Some Game"
class NativeHookMountTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cc, cls.prefix = runner()
if cls.cc is None:
raise unittest.SkipTest("no aarch64 compiler and runner")
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 / "probe.c").write_text(PROBE)
subprocess.run([cls.cc, "-O1", "-o", str(tmp / "probe"), str(tmp / "probe.c")], check=True)
cls.probe = tmp / "probe"
cls.root = tmp / "root"
cls.library = tmp / "storage/PC GAMES"
(cls.root / "etc").mkdir(parents=True)
(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")
(cls.library / "Some Game").mkdir(parents=True)
(cls.library / "Some Game/game.txt").write_text("library")
for name, target in (("native", str(cls.root)), ("guest", "/")):
devices = cls.root / "pfx" / name / "dosdevices"
devices.mkdir(parents=True)
os.symlink(target, devices / "z:")
relinked = cls.root / "pfx/relinked"
(relinked / "dosdevices").mkdir(parents=True)
(relinked / "drive_c").mkdir()
(relinked / "drive_c/c.txt").write_text("drive c")
os.symlink(os.path.relpath(cls.root, relinked / "dosdevices"), relinked / "dosdevices/z:")
os.symlink("../drive_c", relinked / "dosdevices/c:")
copied = cls.root / "pfx/copied/dosdevices"
copied.mkdir(parents=True)
os.symlink("../" * 12 + "..", copied / "z:")
elsewhere = tmp / "elsewhere"
(elsewhere / "outside/pfx/dosdevices").mkdir(parents=True)
(elsewhere / "mnt/library/Some Game").mkdir(parents=True)
(elsewhere / "mnt/library/Some Game/game.txt").write_text("elsewhere")
os.symlink("../../..", elsewhere / "outside/pfx/dosdevices/z:")
os.symlink(str(elsewhere / "outside/pfx"), cls.root / "pfx/outside")
cls.private = tmp / "private"
private_pfx = cls.private / "123/pfx/dosdevices"
private_pfx.mkdir(parents=True)
os.symlink(os.path.relpath("/", "/mnt/private/123/pfx/dosdevices"), private_pfx / "z:")
private_copy = cls.private / "456/pfx/dosdevices"
private_copy.mkdir(parents=True)
os.symlink("../" * 12 + "..", private_copy / "z:")
cls.eqlen_src = tmp / "eqsrcdir"
cls.eqlen_dst = "/" + "d" * (len(str(cls.eqlen_src)) - 1)
eqlen_pfx = cls.eqlen_src / "123/pfx/dosdevices"
eqlen_pfx.mkdir(parents=True)
os.symlink(os.path.relpath("/", f"{cls.eqlen_dst}/123/pfx/dosdevices"), eqlen_pfx / "z:")
@classmethod
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}"
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 != "LD_PRELOAD"}
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), *args], env=env, capture_output=True, text=True, timeout=120,
cwd=self.tmp.name)
self.assertEqual(out.returncode, 0, out.stderr)
return {line.split("\t")[0]: line.split("\t")[1:] for line in out.stdout.splitlines()}
def test_drive_link_stored_as_rootfs_path_reaches_the_mount(self):
path = f"/pfx/native/dosdevices/z:{GAME}/game.txt"
self.assertEqual(self.run_probe(path)[path], ["1", "library"])
def test_working_directory_through_a_drive_link_is_the_mount(self):
arg = f"cd:/pfx/native/dosdevices/z:{GAME}/"
self.assertEqual(self.run_probe(arg)[arg], [GAME])
def test_rootfs_path_under_a_mount_point_reaches_the_mount(self):
path = f"{self.root}{GAME}/game.txt"
self.assertEqual(self.run_probe(path)[path], ["1", "library"])
def test_guest_drive_link_is_unchanged(self):
path = f"/pfx/guest/dosdevices/z:{GAME}/game.txt"
self.assertEqual(self.run_probe(path)[path], ["1", "library"])
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)
for path in paths:
self.assertEqual(result[path], ["1", "rootfs"], path)
def test_wine_lookup_steps_reach_the_mount(self):
for pfx in ("/pfx/native", "/pfx/guest", "/pfx/relinked", f"{self.root}/pfx/native", f"{self.root}/pfx/guest",
f"{self.root}/pfx/relinked"):
drive = f"{pfx}/dosdevices/z:"
args = [f"at:{drive}{GAME}/", f"at:{drive}{GAME}", f"ls:{drive}/mnt/library", f"ls:{drive}{GAME}"]
result = self.run_probe(*args)
self.assertEqual(result[args[0]], ["1", "1"], pfx)
self.assertEqual(result[args[1]], ["1", "1"], pfx)
self.assertEqual(result[args[2]], ["Some Game"], pfx)
self.assertEqual(result[args[3]], ["game.txt"], pfx)
def test_relative_drive_link_reaches_the_mount(self):
path = f"/pfx/relinked/dosdevices/z:{GAME}/game.txt"
cd = f"cd:/pfx/relinked/dosdevices/z:{GAME}/"
result = self.run_probe(path, cd)
self.assertEqual(result[path], ["1", "library"])
self.assertEqual(result[cd], [GAME])
def test_relative_links_outside_mounts_are_unchanged(self):
paths = ["/pfx/relinked/dosdevices/z:/etc/hostname", "/pfx/relinked/dosdevices/c:/c.txt",
f"{self.root}/pfx/relinked/dosdevices/c:/c.txt"]
result = self.run_probe(*paths)
self.assertEqual(result[paths[0]], ["1", "rootfs"])
self.assertEqual(result[paths[1]], ["1", "drive c"])
self.assertEqual(result[paths[2]], ["1", "drive c"])
def test_suffix_after_a_relative_link_is_not_collapsed(self):
path = "/pfx/relinked/dosdevices/z:/mnt/library/missing/../Some Game/game.txt"
self.assertEqual(self.run_probe(path)[path], ["0", "-"])
def test_relative_drive_link_climbing_past_the_root_reaches_the_mount(self):
path = f"/pfx/copied/dosdevices/z:{GAME}/game.txt"
cd = f"cd:/pfx/copied/dosdevices/z:{GAME}/"
result = self.run_probe(path, cd)
self.assertEqual(result[path], ["1", "library"])
self.assertEqual(result[cd], [GAME])
def test_relative_link_climbing_past_the_root_stays_in_the_rootfs(self):
path = "/pfx/copied/dosdevices/z:/etc/hostname"
self.assertEqual(self.run_probe(path)[path], ["1", "rootfs"])
def test_relative_link_is_resolved_from_its_real_directory(self):
path = f"/pfx/outside/dosdevices/z:{GAME}/game.txt"
self.assertEqual(self.run_probe(path)[path], ["1", "elsewhere"])
def test_drive_link_in_a_bound_prefix_reaches_the_mount(self):
path = f"/mnt/private/123/pfx/dosdevices/z:{GAME}/game.txt"
cd = f"cd:/mnt/private/123/pfx/dosdevices/z:{GAME}/"
result = self.run_probe(path, cd)
self.assertEqual(result[path], ["1", "library"])
self.assertEqual(result[cd], [GAME])
def test_drive_link_copied_into_a_bound_prefix_reaches_the_mount(self):
path = f"/mnt/private/456/pfx/dosdevices/z:{GAME}/game.txt"
cd = f"cd:/mnt/private/456/pfx/dosdevices/z:{GAME}/"
result = self.run_probe(path, cd)
self.assertEqual(result[path], ["1", "library"])
self.assertEqual(result[cd], [GAME])
def test_drive_link_in_equal_length_bound_prefix_reaches_the_mount(self):
path = f"{self.eqlen_dst}/123/pfx/dosdevices/z:{GAME}/game.txt"
cd = f"cd:{self.eqlen_dst}/123/pfx/dosdevices/z:{GAME}/"
result = self.run_probe(path, cd)
self.assertEqual(result[path], ["1", "library"])
self.assertEqual(result[cd], [GAME])
if __name__ == "__main__":
unittest.main()