fix(native): at-calls, uid lookups and relative programs follow the session's mounts

This commit is contained in:
Kurt Himebauch committed 2026-11-09 12:26:36 -05:00
1 parent b53068dbef
commit d6363d3b48
5 files changed
+113 -9

No files matched your search

+15 -3
View File
@@ -93,6 +93,7 @@ int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL;
if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat");
if (!orig_fchmodat) return -1;
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
int ret = orig_fchmodat(dirfd, target, mode, flags);
@@ -130,6 +131,7 @@ int unlink(const char *pathname) {
int unlinkat(int dirfd, const char *pathname, int flags) {
static int (*orig_unlinkat)(int, const char *, int) = NULL;
if (!orig_unlinkat) orig_unlinkat = (int (*)(int, const char *, int))dlsym(RTLD_NEXT, "unlinkat");
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
dev_t hl_dev = 0;
@@ -181,6 +183,7 @@ int mkdir(const char *pathname, mode_t mode) {
int mkdirat(int dirfd, const char *pathname, mode_t mode) {
static int (*orig_mkdirat)(int, const char *, mode_t) = NULL;
if (!orig_mkdirat) orig_mkdirat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkdirat");
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
int ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS);
@@ -250,6 +253,8 @@ int rename(const char *oldpath, const char *newpath) {
int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
static int (*orig_renameat)(int, const char *, int, const char *) = NULL;
if (!orig_renameat) orig_renameat = (int (*)(int, const char *, int, const char *))dlsym(RTLD_NEXT, "renameat");
DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old);
DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new);
char buf1[PATH_MAX];
char buf2[PATH_MAX];
const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath;
@@ -279,6 +284,8 @@ int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpa
orig_renameat2 = (int (*)(int, const char *, int, const char *, unsigned int))dlsym(RTLD_NEXT, "renameat2");
if (!orig_renameat2) return renameat(olddirfd, oldpath, newdirfd, newpath);
}
DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old);
DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new);
char buf1[PATH_MAX];
char buf2[PATH_MAX];
const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath;
@@ -323,13 +330,14 @@ ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) {
// Hook readlinkat
ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL;
if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
DDNATV_AT_REMAP(dirfd, pathname);
// after the remap: "proc/self/exe" relative to a descriptor for "/" is /proc/self/exe as well
if (is_proc_self_exe(pathname)) {
ssize_t exe_len = handle_proc_self_exe(buf, bufsiz);
if (exe_len >= 0) return exe_len;
}
static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL;
if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat");
DDNATV_AT_REMAP(dirfd, pathname);
char pbuf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, pbuf, sizeof(pbuf)) : pathname;
ssize_t ret = orig_readlinkat ? orig_readlinkat(dirfd, target, buf, bufsiz) : -1;
@@ -461,6 +469,7 @@ int symlink(const char *target, const char *linkpath) {
int symlinkat(const char *target, int newdirfd, const char *linkpath) {
static int (*orig_symlinkat)(const char *, int, const char *) = NULL;
if (!orig_symlinkat) orig_symlinkat = (int (*)(const char *, int, const char *))dlsym(RTLD_NEXT, "symlinkat");
DDNATV_AT_REMAP(newdirfd, linkpath);
char pbuf[PATH_MAX], tbuf[PATH_MAX];
const char *newlink = (linkpath && linkpath[0] == '/') ? rewrite_path(linkpath, pbuf, sizeof(pbuf)) : linkpath;
const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target;
@@ -473,6 +482,8 @@ int symlinkat(const char *target, int newdirfd, const char *linkpath) {
int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
static int (*orig_linkat)(int, const char *, int, const char *, int) = NULL;
if (!orig_linkat) orig_linkat = (int (*)(int, const char *, int, const char *, int))dlsym(RTLD_NEXT, "linkat");
DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old);
DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new);
char obuf[PATH_MAX], nbuf[PATH_MAX];
const char *rold = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, obuf, sizeof(obuf)) : oldpath;
const char *rnew = (newpath && newpath[0] == '/') ? rewrite_path(newpath, nbuf, sizeof(nbuf)) : newpath;
@@ -525,6 +536,7 @@ int futimesat(int dirfd, const char *pathname, const struct timeval times[2]) {
int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
static int (*orig_utimensat)(int, const char *, const struct timespec[2], int) = NULL;
if (!orig_utimensat) orig_utimensat = (int (*)(int, const char *, const struct timespec[2], int))dlsym(RTLD_NEXT, "utimensat");
DDNATV_AT_REMAP(dirfd, pathname);
char buf[PATH_MAX];
const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname;
return orig_utimensat ? orig_utimensat(dirfd, target, times, flags) : -1;
+5 -1
View File
@@ -764,7 +764,10 @@ int DDNATV_NSS_SYM(getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t
int ret = g_test_orig_nss_r_ret;
if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL;
#endif
if (ret == 0 && result && *result != NULL) return 0;
if (ret == 0 && result && *result != NULL) {
present_privsep_user_as_root(*result);
return 0;
}
if (ret != 0 && ret != ENOENT) {
if (result) *result = NULL;
return ret;
@@ -772,6 +775,7 @@ int DDNATV_NSS_SYM(getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t
int err = ddnatv_lookup_passwd(NULL, uid, 0, pwd, buf, buflen);
if (err == 0) {
present_privsep_user_as_root(pwd);
if (result) *result = pwd;
return 0;
}
+9 -5
View File
@@ -109,7 +109,10 @@ static int is_mapped_guest_path(const char *path) {
if (!path || path[0] != '/') return 0;
for (int i = 0; i < g_nbinds; i++) {
const struct ddnatv_bind *b = &g_binds[i];
if (!b->identity && is_path_prefix(path, b->dst, b->dst_len)) return 1;
if (b->identity) continue;
if (is_path_prefix(path, b->dst, b->dst_len)) return 1;
// its real place too: a relative program is made absolute from the real working directory
if (is_path_prefix(path, b->src, b->src_len) && get_rootfs_subpath(b->src) == NULL) return 1;
}
return 0;
}
@@ -593,10 +596,11 @@ static const char *map_at(int dirfd, const char *rel, char *buf, size_t size) {
return NULL;
}
#define DDNATV_AT_REMAP(dirfd, pathname) \
char ddnatv_at_buf[PATH_MAX]; \
if (map_at((dirfd), (pathname), ddnatv_at_buf, sizeof(ddnatv_at_buf))) { \
#define DDNATV_AT_REMAP_NAMED(dirfd, pathname, buf) \
char buf[PATH_MAX]; \
if (map_at((dirfd), (pathname), buf, sizeof(buf))) { \
(dirfd) = AT_FDCWD; \
(pathname) = ddnatv_at_buf; \
(pathname) = buf; \
}
#define DDNATV_AT_REMAP(dirfd, pathname) DDNATV_AT_REMAP_NAMED(dirfd, pathname, ddnatv_at_buf)
#endif /* !DDNATV_HOST_TEST */
+2
View File
@@ -130,6 +130,7 @@ int chown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (voi
int fchown(int fd, uid_t owner, gid_t group) { (void)fd; (void)owner; (void)group; return 0; }
int lchown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; }
int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) {
DDNATV_AT_REMAP(dirfd, pathname);
(void)dirfd; (void)pathname; (void)owner; (void)group; (void)flags; return 0;
}
int chroot(const char *path) { (void)path; return 0; }
@@ -139,6 +140,7 @@ int mkfifo(const char *pathname, mode_t mode);
int mkfifoat(int dirfd, const char *pathname, mode_t mode);
int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) {
DDNATV_AT_REMAP(dirfd, pathname);
(void)dev;
if (!pathname) {
errno = EFAULT;