fix(native): remove, glob and nftw take guest paths, and links in a mount are followed as the guest sees them

This commit is contained in:
Kurt Himebauch committed 2026-11-09 14:57:50 -05:00
1 parent 89dd9fa161
commit d9c694858c
7 files changed
+269 -25

No files matched your search

+2
View File
@@ -12,6 +12,8 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <ftw.h>
#include <glob.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
+11 -1
View File
@@ -95,6 +95,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
int has_nt = 0;
int has_binds = 0;
int has_hostname = 0;
int has_link_root = 0;
int has_preload_list = 0;
const char *preload_value = NULL;
while (envp && envp[count]) {
@@ -123,6 +124,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
else if (strncmp(e, "DDNATV_BINDS=", 13) == 0) has_binds = 1;
else if (strncmp(e, "DDNATV_PRELOAD=", 15) == 0) has_preload_list = 1;
else if (strncmp(e, "DDNATV_HOSTNAME=", 16) == 0) has_hostname = 1;
else if (strncmp(e, "DDNATV_LINK_ROOT=", 17) == 0) has_link_root = 1;
else if (strncmp(e, "DPKG_DEB_THREADS_MAX=", 21) == 0) has_threads_max = 1;
else if (strncmp(e, "DEBIAN_FRONTEND=", 16) == 0) has_frontend = 1;
else if (strncmp(e, "DEBCONF_FRONTEND=", 17) == 0) has_debconf_frontend = 1;
@@ -134,7 +136,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
count++;
}
char **new_env = calloc(count + 23, sizeof(char *));
char **new_env = calloc(count + 24, sizeof(char *));
int dst = 0;
for (int i = 0; i < count; i++) {
if (has_preload && envp[i][0] == 'L' && strncmp(envp[i], "LD_PRELOAD=", 11) == 0) {
@@ -220,6 +222,14 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
new_env[dst++] = str;
}
}
if (!has_link_root && g_link_root_len) {
char *str = malloc(17 + g_link_root_len + 1);
if (str) {
memcpy(str, "DDNATV_LINK_ROOT=", 17);
memcpy(str + 17, g_link_root, g_link_root_len + 1);
new_env[dst++] = str;
}
}
if (!has_hostname && g_hostname[0]) {
char *str = malloc(16 + sizeof(g_hostname));
if (str) {
+117 -10
View File
@@ -60,7 +60,7 @@ int chmod(const char *pathname, mode_t mode) {
if (!orig_chmod) orig_chmod = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "chmod");
if (!orig_chmod) return -1;
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
int ret = orig_chmod(target, mode);
if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) {
ret = orig_chmod(target, mode & 01777);
@@ -161,6 +161,113 @@ int rmdir(const char *pathname) {
return ret;
}
// glibc's remove() calls its internal unlinkat, which no preload can see: GLib's g_file_delete()
// got ENOENT for every guest path, and Flatpak could not replace an app's "current" link
int remove(const char *pathname) {
if (unlink(pathname) == 0) return 0;
if (errno != EISDIR) return -1;
return rmdir(pathname);
}
// glob() and nftw() walk directories with glibc's internal calls too. glob() is given the hook's
// own through GLOB_ALTDIRFUNC; nftw() walks the mapped directory and its callback is given guest paths.
static int glob_alt_stat(const char *path, void *st) { return stat(path, (struct stat *)st); }
static int glob_alt_lstat(const char *path, void *st) { return lstat(path, (struct stat *)st); }
static void *glob_alt_opendir(const char *path) { return opendir(path); }
static void *glob_alt_readdir(void *dir) { return readdir((DIR *)dir); }
static void glob_alt_closedir(void *dir) { closedir((DIR *)dir); }
int glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) {
static int (*orig_glob)(const char *, int, int (*)(const char *, int), glob_t *) = NULL;
if (!orig_glob) orig_glob = (int (*)(const char *, int, int (*)(const char *, int), glob_t *))dlsym(RTLD_NEXT, "glob");
if (pglob && !(flags & GLOB_ALTDIRFUNC)) {
init_ddnatv_hook();
pglob->gl_closedir = glob_alt_closedir;
pglob->gl_readdir = (struct dirent *(*)(void *))glob_alt_readdir;
pglob->gl_opendir = glob_alt_opendir;
pglob->gl_lstat = (int (*)(const char *, struct stat *))glob_alt_lstat;
pglob->gl_stat = (int (*)(const char *, struct stat *))glob_alt_stat;
flags |= GLOB_ALTDIRFUNC;
}
return orig_glob ? orig_glob(pattern, flags, errfunc, pglob) : GLOB_NOSPACE;
}
int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int), glob64_t *pglob) {
return glob(pattern, flags, errfunc, (glob_t *)pglob);
}
static __thread struct {
const char *host, *guest;
size_t host_len, guest_len;
int (*nftw_fn)(const char *, const struct stat *, int, struct FTW *);
int (*ftw_fn)(const char *, const struct stat *, int);
} t_walk;
static const char *walk_guest_path(const char *path, char *buf, size_t size, int *base) {
if (!t_walk.host || strncmp(path, t_walk.host, t_walk.host_len) != 0) return path;
const char *rest = path + t_walk.host_len;
if (*rest != '\0' && *rest != '/') return path;
size_t n = ddnatv_append(buf, size, 0, t_walk.guest, t_walk.guest_len);
ddnatv_append(buf, size, n, rest, strlen(rest));
if (base) *base = *base - (int)t_walk.host_len + (int)t_walk.guest_len;
return buf;
}
static int walk_nftw_cb(const char *path, const struct stat *st, int type, struct FTW *ftwbuf) {
char buf[PATH_MAX];
struct FTW guest_ftw = *ftwbuf;
return t_walk.nftw_fn(walk_guest_path(path, buf, sizeof(buf), &guest_ftw.base), st, type, &guest_ftw);
}
static int walk_ftw_cb(const char *path, const struct stat *st, int type) {
char buf[PATH_MAX];
return t_walk.ftw_fn(walk_guest_path(path, buf, sizeof(buf), NULL), st, type);
}
int nftw(const char *dir, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fds, int flags) {
static int (*orig_nftw)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int) = NULL;
if (!orig_nftw) orig_nftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int))dlsym(RTLD_NEXT, "nftw");
if (!orig_nftw) { errno = ENOSYS; return -1; }
char buf[PATH_MAX];
const char *target = rewrite_path(dir, buf, sizeof(buf));
if (target == dir || !fn) return orig_nftw(dir, fn, fds, flags);
__typeof__(t_walk) saved = t_walk;
t_walk.host = target; t_walk.host_len = strlen(target);
while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--;
t_walk.guest = dir; t_walk.guest_len = strlen(dir);
while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--;
t_walk.nftw_fn = fn;
int ret = orig_nftw(target, walk_nftw_cb, fds, flags);
t_walk = saved;
return ret;
}
int nftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int, struct FTW *), int fds, int flags) {
return nftw(dir, (int (*)(const char *, const struct stat *, int, struct FTW *))fn, fds, flags);
}
int ftw(const char *dir, int (*fn)(const char *, const struct stat *, int), int fds) {
static int (*orig_ftw)(const char *, int (*)(const char *, const struct stat *, int), int) = NULL;
if (!orig_ftw) orig_ftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int), int))dlsym(RTLD_NEXT, "ftw");
if (!orig_ftw) { errno = ENOSYS; return -1; }
char buf[PATH_MAX];
const char *target = rewrite_path(dir, buf, sizeof(buf));
if (target == dir || !fn) return orig_ftw(dir, fn, fds);
__typeof__(t_walk) saved = t_walk;
t_walk.host = target; t_walk.host_len = strlen(target);
while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--;
t_walk.guest = dir; t_walk.guest_len = strlen(dir);
while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--;
t_walk.ftw_fn = fn;
int ret = orig_ftw(target, walk_ftw_cb, fds);
t_walk = saved;
return ret;
}
int ftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int), int fds) {
return ftw(dir, (int (*)(const char *, const struct stat *, int))fn, fds);
}
// The app is root for the programs it runs, and root enters and writes every directory whatever its
// mode; the app does not. dpkg makes each new directory with mode 0 and sets the mode a moment later,
// and a chmod that fails is reported as done (see chmod), which left directories nobody could put a
@@ -378,7 +485,7 @@ ssize_t getxattr(const char *path, const char *name, void *value, size_t size) {
return -1;
}
char pbuf[PATH_MAX];
return orig_getxattr ? orig_getxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size) : -1;
return orig_getxattr ? orig_getxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size) : -1;
}
ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size) {
@@ -396,7 +503,7 @@ ssize_t listxattr(const char *path, char *list, size_t size) {
static ssize_t (*orig_listxattr)(const char *, char *, size_t) = NULL;
if (!orig_listxattr) orig_listxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "listxattr");
char pbuf[PATH_MAX];
ssize_t ret = orig_listxattr ? orig_listxattr(rewrite_path(path, pbuf, sizeof(pbuf)), list, size) : -1;
ssize_t ret = orig_listxattr ? orig_listxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), list, size) : -1;
return size > 0 ? drop_selinux_xattr(list, ret) : ret;
}
@@ -429,7 +536,7 @@ int setxattr(const char *path, const char *name, const void *value, size_t size,
static int (*orig_setxattr)(const char *, const char *, const void *, size_t, int) = NULL;
if (!orig_setxattr) orig_setxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "setxattr");
char pbuf[PATH_MAX];
return orig_setxattr ? orig_setxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1;
return orig_setxattr ? orig_setxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1;
}
int lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags) {
@@ -505,7 +612,7 @@ int utime(const char *filename, const struct utimbuf *times) {
static int (*orig_utime)(const char *, const struct utimbuf *) = NULL;
if (!orig_utime) orig_utime = (int (*)(const char *, const struct utimbuf *))dlsym(RTLD_NEXT, "utime");
char buf[PATH_MAX];
const char *target = rewrite_path(filename, buf, sizeof(buf));
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
return orig_utime ? orig_utime(target, times) : -1;
}
@@ -513,7 +620,7 @@ int utimes(const char *filename, const struct timeval times[2]) {
static int (*orig_utimes)(const char *, const struct timeval[2]) = NULL;
if (!orig_utimes) orig_utimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "utimes");
char buf[PATH_MAX];
const char *target = rewrite_path(filename, buf, sizeof(buf));
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
return orig_utimes ? orig_utimes(target, times) : -1;
}
@@ -547,7 +654,7 @@ int truncate(const char *path, off_t length) {
static int (*orig_truncate)(const char *, off_t) = NULL;
if (!orig_truncate) orig_truncate = (int (*)(const char *, off_t))dlsym(RTLD_NEXT, "truncate");
char buf[PATH_MAX];
const char *target = rewrite_path(path, buf, sizeof(buf));
const char *target = rewrite_path_follow(path, buf, sizeof(buf));
return orig_truncate(target, length);
}
@@ -556,7 +663,7 @@ int statfs(const char *path, struct statfs *buf) {
static int (*orig_statfs)(const char *, struct statfs *) = NULL;
if (!orig_statfs) orig_statfs = (int (*)(const char *, struct statfs *))dlsym(RTLD_NEXT, "statfs");
char pbuf[PATH_MAX];
const char *target = rewrite_path(path, pbuf, sizeof(pbuf));
const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf));
return orig_statfs ? orig_statfs(target, buf) : -1;
}
@@ -564,7 +671,7 @@ int statvfs(const char *path, struct statvfs *buf) {
static int (*orig_statvfs)(const char *, struct statvfs *) = NULL;
if (!orig_statvfs) orig_statvfs = (int (*)(const char *, struct statvfs *))dlsym(RTLD_NEXT, "statvfs");
char pbuf[PATH_MAX];
const char *target = rewrite_path(path, pbuf, sizeof(pbuf));
const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf));
return orig_statvfs ? orig_statvfs(target, buf) : -1;
}
@@ -573,7 +680,7 @@ int chdir(const char *path) {
static int (*orig_chdir)(const char *) = NULL;
if (!orig_chdir) orig_chdir = (int (*)(const char *))dlsym(RTLD_NEXT, "chdir");
char buf[PATH_MAX];
const char *target = rewrite_path(path, buf, sizeof(buf));
const char *target = rewrite_path_follow(path, buf, sizeof(buf));
return orig_chdir ? orig_chdir(target) : -1;
}
+13 -13
View File
@@ -68,7 +68,7 @@ void *dlopen(const char *filename, int flags) {
if (!orig_dlopen) return NULL;
char buf[PATH_MAX];
const char *target = rewrite_path(filename, buf, sizeof(buf));
const char *target = rewrite_path_follow(filename, buf, sizeof(buf));
void *handle = orig_dlopen(target, flags);
if (!handle && target[0] == '/' && !(flags & RTLD_NOLOAD)) {
// Its dependencies may be in its RUNPATH, which ld.so looked for outside the rootfs
@@ -103,7 +103,7 @@ int open(const char *pathname, int flags, ...) {
}
}
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
const char *target = ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf));
if (open_needs_mode(flags)) {
va_list args;
va_start(args, flags);
@@ -131,7 +131,7 @@ int openat(int dirfd, const char *pathname, int flags, ...) {
}
}
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
const char *target = (pathname && pathname[0] == '/') ? ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
if (open_needs_mode(flags)) {
va_list args;
va_start(args, flags);
@@ -275,7 +275,7 @@ FILE *fopen(const char *pathname, const char *mode) {
static FILE *(*orig_fopen)(const char *, const char *) = NULL;
if (!orig_fopen) orig_fopen = (FILE *(*)(const char *, const char *))dlsym(RTLD_NEXT, "fopen");
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
return orig_fopen(target, mode);
}
@@ -284,7 +284,7 @@ FILE *freopen(const char *pathname, const char *mode, FILE *stream) {
static FILE *(*orig_freopen)(const char *, const char *, FILE *) = NULL;
if (!orig_freopen) orig_freopen = (FILE *(*)(const char *, const char *, FILE *))dlsym(RTLD_NEXT, "freopen");
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
return orig_freopen(target, mode, stream);
}
@@ -465,7 +465,7 @@ DIR *opendir(const char *name) {
static DIR *(*orig_opendir)(const char *) = NULL;
if (!orig_opendir) orig_opendir = (DIR *(*)(const char *))dlsym(RTLD_NEXT, "opendir");
char buf[PATH_MAX];
const char *target = rewrite_path(name, buf, sizeof(buf));
const char *target = rewrite_path_follow(name, buf, sizeof(buf));
return orig_opendir(target);
}
@@ -474,7 +474,7 @@ int stat(const char *pathname, struct stat *statbuf) {
static int (*orig_stat)(const char *, struct stat *) = NULL;
if (!orig_stat) orig_stat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "stat");
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
const char *target = rewrite_path_follow(pathname, buf, sizeof(buf));
int ret = orig_stat ? orig_stat(target, statbuf) : -1;
if (ret != 0 && errno == ENOENT) {
char alt[PATH_MAX];
@@ -523,7 +523,7 @@ int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) {
if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat");
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
int ret = orig_fstatat ? orig_fstatat(dirfd, target, statbuf, flags) : -1;
char alt[PATH_MAX];
if (ret != 0 && errno == ENOENT && pathname && pathname[0] == '/') {
@@ -567,7 +567,7 @@ int __xstat(int ver, const char *pathname, struct stat *statbuf) {
if (!orig___xstat) orig___xstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__xstat");
if (!orig___xstat) return stat(pathname, statbuf);
char buf[PATH_MAX];
int ret = orig___xstat(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf);
int ret = orig___xstat(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf);
if (ret == 0) apply_stat_emulation(statbuf);
return ret;
}
@@ -577,7 +577,7 @@ int __xstat64(int ver, const char *pathname, struct stat64 *statbuf) {
if (!orig___xstat64) orig___xstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__xstat64");
if (!orig___xstat64) return stat(pathname, (struct stat *)statbuf);
char buf[PATH_MAX];
int ret = orig___xstat64(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf);
int ret = orig___xstat64(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf);
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
return ret;
}
@@ -626,7 +626,7 @@ int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *statbuf, i
if (!orig___fxstatat) return fstatat(dirfd, pathname, statbuf, flags);
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
int ret = orig___fxstatat(ver, dirfd, target, statbuf, flags);
if (ret == 0) apply_stat_emulation(statbuf);
return ret;
@@ -638,7 +638,7 @@ int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *statbu
if (!orig___fxstatat64) return fstatat(dirfd, pathname, (struct stat *)statbuf, flags);
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
int ret = orig___fxstatat64(ver, dirfd, target, statbuf, flags);
if (ret == 0) apply_stat_emulation((struct stat *)statbuf);
return ret;
@@ -654,7 +654,7 @@ int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct
if (!orig_statx) orig_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(RTLD_NEXT, "statx");
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname;
int ret = orig_statx ? orig_statx(dirfd, target, flags, mask, statxbuf) : -1;
if (ret == 0 && statxbuf && hardlinks_maybe_any()) {
const unsigned int needed = STATX_INO | STATX_NLINK | STATX_MTIME;
+76
View File
@@ -34,6 +34,10 @@ static int g_nbinds = 0;
static char *g_binds_raw = NULL;
// The session's host name (DDNATV_HOSTNAME), what proot put in uname(): Android's kernel says "localhost"
static char g_hostname[65];
// A Flatpak sandbox's: the session's rootfs (DDNATV_LINK_ROOT), whose hook stored absolute guest
// link targets as paths in it, as the kernel follows them there; in the sandbox they are guest paths
static char g_link_root[PATH_MAX];
static size_t g_link_root_len = 0;
static int ddnatv_bind_longer_first(const void *a, const void *b) {
const struct ddnatv_bind *x = a, *y = b;
@@ -260,6 +264,74 @@ static const char *rewrite_path_in_root(const char *path, char *buffer, size_t b
return buffer;
}
// A link target the session's hook stored as a path in its rootfs, given back as the guest path it
// stands for (see g_link_root). Rewrites target[0..len) in place; returns the new length.
static size_t link_root_guest_target(char *target, size_t len) {
if (!g_link_root_len || len < g_link_root_len || memcmp(target, g_link_root, g_link_root_len) != 0) return len;
if (len == g_link_root_len) { target[0] = '/'; target[1] = '\0'; return 1; }
if (target[g_link_root_len] != '/') return len;
memmove(target, target + g_link_root_len, len - g_link_root_len + 1);
return len - g_link_root_len;
}
// The guest path a symbolic link at absolute guest path `path` leads to, followed in the guest's
// view: the kernel resolves a relative target next to the link's real place, and in a mount that is
// elsewhere (Flatpak's /run/user/0/wayland-0 -> ../../flatpak/wayland-0, where /run/flatpak/wayland-0
// is another mount). Returns `path` when it is no link.
static const char *follow_guest_link(const char *path, char *out, size_t outsz) {
#if defined(__aarch64__)
const char *cur = path;
for (int depth = 0; depth < 8; depth++) {
char host_buf[PATH_MAX];
const char *host = rewrite_path_in_root(cur, host_buf, sizeof(host_buf));
// /dev/stdin and /dev/fd are mounts of /proc/self/fd, whose links ("pipe:[123]") only the kernel opens
if (strncmp(host, "/proc/", 6) == 0) return cur;
char target[PATH_MAX];
long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1);
if (n <= 0) return cur;
target[n] = '\0';
n = (long)link_root_guest_target(target, (size_t)n);
char joined[PATH_MAX];
if (target[0] == '/') {
ddnatv_copy_cstr(joined, sizeof(joined), target);
} else {
const char *slash = strrchr(cur, '/');
size_t dir_len = slash ? (size_t)(slash - cur) : 0;
size_t used = ddnatv_append(joined, sizeof(joined), 0, cur, dir_len);
used = ddnatv_append(joined, sizeof(joined), used, "/", 1);
ddnatv_append(joined, sizeof(joined), used, target, strlen(target));
}
// lexically, as the guest sees it
size_t len = 0;
out[0] = '\0';
for (const char *p = joined; *p;) {
while (*p == '/') p++;
const char *end = strchr(p, '/');
size_t part = end ? (size_t)(end - p) : strlen(p);
if (part == 0) break;
if (part == 1 && p[0] == '.') {
} else if (part == 2 && p[0] == '.' && p[1] == '.') {
while (len > 0 && out[len - 1] != '/') len--;
if (len > 0) len--;
out[len] = '\0';
} else if (len + 1 + part < outsz) {
out[len++] = '/';
memcpy(out + len, p, part);
len += part;
out[len] = '\0';
}
p += part;
}
if (len == 0) { out[0] = '/'; out[1] = '\0'; }
cur = out;
}
return cur;
#else
(void)out; (void)outsz;
return path;
#endif
}
// On ENOENT, stat()/lstat()/readlink() retry /usr/bin <-> /bin and /usr/sbin <-> /sbin, for
// rootfs trees where bin and sbin are real directories. Returns 1 for the bin pair, 2 for the
// sbin pair (with the other spelling in `alt`), 0 when `sub` is in neither.
@@ -383,6 +455,10 @@ static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap) {
static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret, size_t bufsiz) {
if (ret <= 0) return ret;
if ((size_t)ret < bufsiz) {
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);
// 1. Strip g_ddnatv_root if target starts with it
+42
View File
@@ -36,6 +36,12 @@ static void init_ddnatv_hook(void) {
ddnatv_load_binds(getenv("DDNATV_BINDS"));
const char *hostname = getenv("DDNATV_HOSTNAME");
if (hostname && hostname[0] && strlen(hostname) < sizeof(g_hostname)) ddnatv_copy_cstr(g_hostname, sizeof(g_hostname), hostname);
const char *link_root = getenv("DDNATV_LINK_ROOT");
if (link_root && link_root[0] == '/' && strlen(link_root) < sizeof(g_link_root)) {
ddnatv_copy_cstr(g_link_root, sizeof(g_link_root), link_root);
g_link_root_len = strlen(g_link_root);
while (g_link_root_len > 1 && g_link_root[g_link_root_len - 1] == '/') g_link_root[--g_link_root_len] = 0;
}
const char *preload = getenv("DDNATV_PRELOAD");
if (preload && preload[0] && g_ddnatv_root_len > 0) {
size_t libs = 1;
@@ -214,6 +220,42 @@ static const char *rewrite_path(const char *path, char *buffer, size_t bufsize)
return rewrite_path_in_root(path, buffer, bufsize);
}
// rewrite_path() for calls that follow a symbolic link in the last component. In a mount whose
// source is elsewhere the kernel would follow the link's absolute guest target on Android's own
// tree (a Flatpak app's /app/bin/PINBALL.DAT -> /app/extra/Pinball/PINBALL.DAT), so the link is
// 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;
init_ddnatv_hook();
int moved = 0;
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];
if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { moved = !b->identity; break; }
}
char from_source[PATH_MAX];
if (!moved) {
// or a mount's real place, as /proc/self/exe and getcwd() give it (a Flatpak app's checkout)
const struct ddnatv_bind *best = NULL;
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];
if (b->identity || !is_path_prefix(path, b->src, b->src_len)) continue;
if (!best || b->src_len > best->src_len) best = b;
}
if (!best) return rewrite_path_in_root(path, buffer, bufsize);
size_t n = ddnatv_append(from_source, sizeof(from_source), 0, best->dst, best->dst_len);
ddnatv_append(from_source, sizeof(from_source), n, path + best->src_len, strlen(path + best->src_len));
path = from_source;
}
char guest[PATH_MAX];
const char *followed = follow_guest_link(path, guest, sizeof(guest));
char mapped[PATH_MAX];
const char *host = rewrite_path_in_root(followed, mapped, sizeof(mapped));
// the result may be `followed` or `path` itself, which can live here
ddnatv_copy_cstr(buffer, bufsize, host);
return buffer;
}
// Whether the rootfs keeps bin (pair DDNATV_USRMERGE_BIN) or sbin as a symlink into /usr, as
// Ubuntu 24.04 does. Checked once per process with one readlink().
static int rootfs_pair_is_merged(int pair) {
+8 -1
View File
@@ -132,7 +132,14 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
struct sockaddr_un mod_sun;
memset(&mod_sun, 0, sizeof(mod_sun));
mod_sun.sun_family = AF_UNIX;
rewrite_unix_socket_path(sun->sun_path, mod_sun.sun_path, sizeof(mod_sun.sun_path));
char guest[sizeof(sun->sun_path) + 1];
size_t max = addrlen - offsetof(struct sockaddr_un, sun_path);
if (max > sizeof(sun->sun_path)) max = sizeof(sun->sun_path);
memcpy(guest, sun->sun_path, max);
guest[max] = '\0';
char followed[PATH_MAX];
const char *path = guest[0] == '/' ? follow_guest_link(guest, followed, sizeof(followed)) : guest;
rewrite_unix_socket_path(path, mod_sun.sun_path, sizeof(mod_sun.sun_path));
return orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
}
}