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

+82
View File
@@ -0,0 +1,82 @@
#!/usr/bin/env bash
# Builds the code the native userland adds to static Go programs (gosys/gosys.c) into the C header
# the hook includes (gosys/gosys_blob.h, not kept in the repository; build.sh makes it each time).
#
# The code is AArch64 whatever the machine this runs on: it is compiled freestanding with an
# aarch64 cross compiler (gcc-aarch64-linux-gnu: the one that builds the hook; or clang with lld).
#
# usage: tools/nativeguest/build-gosys.sh [output header]
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
src=$here/gosys
out=${1:-$src/gosys_blob.h}
if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then
CC=(aarch64-linux-gnu-gcc)
LD=(aarch64-linux-gnu-ld)
OBJCOPY=(aarch64-linux-gnu-objcopy)
NM=(aarch64-linux-gnu-nm)
OBJDUMP=(aarch64-linux-gnu-objdump)
elif command -v clang >/dev/null 2>&1 && command -v ld.lld >/dev/null 2>&1; then
CC=(clang --target=aarch64-linux-gnu)
LD=(ld.lld)
OBJCOPY=(llvm-objcopy)
NM=(llvm-nm)
OBJDUMP=(llvm-objdump)
else
echo "build-gosys.sh: needs aarch64-linux-gnu-gcc (apt install gcc-aarch64-linux-gnu) or clang with lld" >&2
exit 1
fi
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
"${CC[@]}" -std=gnu11 -O2 -Wall -Wextra -Werror \
-ffreestanding -fno-builtin -nostdlib -fPIE -fvisibility=hidden -fno-stack-protector \
-fno-asynchronous-unwind-tables -fno-unwind-tables -fno-tree-loop-distribute-patterns \
-mcmodel=tiny -mgeneral-regs-only -mno-outline-atomics -ffixed-x18 -ffixed-x28 \
-c "$src/gosys.c" -o "$tmp/gosys.o"
"${LD[@]}" -T "$src/gosys.ld" -nostdlib -o "$tmp/gosys.elf" "$tmp/gosys.o"
"${OBJCOPY[@]}" -O binary "$tmp/gosys.elf" "$tmp/gosys.bin"
# It must stand on its own: nothing undefined, no writable data, and nothing that depends on the
# address it is loaded at (adrp assumes the page offset of the link address; adr does not)
if [ -n "$("${NM[@]}" -u "$tmp/gosys.elf")" ]; then
echo "build-gosys.sh: gosys.c needs symbols it does not define:" >&2
"${NM[@]}" -u "$tmp/gosys.elf" >&2
exit 1
fi
if "${OBJDUMP[@]}" -d "$tmp/gosys.elf" | grep -q -w adrp; then
echo "build-gosys.sh: gosys.c uses adrp, which is not position-independent at any address" >&2
exit 1
fi
sections=$("${OBJDUMP[@]}" -h "$tmp/gosys.elf" | awk '$1 ~ /^[0-9]+$/ {print $2}' | tr '\n' ' ')
for s in $sections; do
case $s in
.hdr|.thunks|.text|.rodata|.trailer|.symtab|.strtab|.shstrtab) ;;
*) echo "build-gosys.sh: unexpected section $s in gosys.elf (sections: $sections)" >&2; exit 1 ;;
esac
done
size=$(stat -c %s "$tmp/gosys.bin")
entry=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "cxgo_entry" {print $1}')
thunks=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "g_thunks" {print $1}')
if [ -z "$entry" ] || [ -z "$thunks" ]; then
echo "build-gosys.sh: cxgo_entry or g_thunks not found" >&2
exit 1
fi
[ "$(head -c 8 "$tmp/gosys.bin")" = "CXGOSYS1" ] || { echo "build-gosys.sh: the header is not first" >&2; exit 1; }
[ "$(tail -c 64 "$tmp/gosys.bin" | head -c 8)" = "CXGOEND1" ] || { echo "build-gosys.sh: the trailer is not last" >&2; exit 1; }
{
echo "// Generated by tools/nativeguest/build-gosys.sh from gosys/gosys.c. Do not edit."
echo "#define GOSYS_BLOB_SIZE $size"
echo "#define GOSYS_ENTRY_OFF 0x$entry"
echo "#define GOSYS_THUNKS_OFF 0x$thunks"
echo "static const unsigned char gosys_blob[GOSYS_BLOB_SIZE] = {"
od -An -v -tx1 "$tmp/gosys.bin" | sed -e 's/^ *//' -e 's/ /, 0x/g' -e 's/^/0x/' -e 's/$/,/'
echo "};"
} > "$out.tmp"
mv "$out.tmp" "$out"
echo "build-gosys.sh: $out ($size bytes, entry 0x$entry)"
+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;