fix(nativeguest): keep parent lookups inside the guest root (#3094)

* fix(nativeguest): keep the parent of the guest root inside the rootfs

* fix(nativeguest): keep every .. walk inside the guest root and its mounts

Absolute paths that climb above /, relative paths from the working
directory or any descriptor, and paths that climb out of a mount were
resolved by the kernel from the rootfs folder or the mount's real place,
so /.., chdir("..") from / and fstatat(fd, "../..") listed the app's
files folder. The walk is now resolved in the guest's view first.

* fix(nativeguest): .. from a linked mount ancestor follows the real directory

---------

Co-authored-by: maxjivi05 <6025299+maxjivi05@users.noreply.github.com>
This commit is contained in:
MaxsTechReviewandmaxjivi05 authored and GitHub committed 2026-11-23 18:55:37 -05:00
1 parent ba3f840b8d
commit f539660650
3 files changed
+114 -24

No files matched your search

+60 -20
View File
@@ -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));
+5 -2
View File
@@ -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++) {