8000 mnt: anchor bind-mount sources to the inode that was inspected by derioii · Pull Request #392 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ endif

BIN = nsjail
LIBS = kafel/libkafel.a
TEST_BINS = tests/nstun_buffer_budget_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test
TEST_BINS = tests/nstun_buffer_budget_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/mount_source_anchor_test

# If PASTA_BIN_PATH is not provided in env, dynamically search for it if EMBED_PASTA is requested
# or fallback to it naturally.
Expand Down Expand Up @@ -166,11 +166,15 @@ test-newapi-recursive-readonly: $(BIN)
if [ "$$ret" -eq 77 ]; then echo "SKIP: new mount API integration test"; \
elif [ "$$ret" -ne 0 ]; then exit "$$ret"; fi

tests/mount_source_anchor_test: tests/mount_source_anchor_test.c
$(CC) -O2 -Wall -o $@ $<

test: $(BIN) $(TEST_BINS) test-cmdline test-newapi-recursive-readonly
$(call run_test, ./tests/nstun_buffer_budget_test, 0)
$(call run_test, ./tests/nstun_policy_test, 0)
$(call run_test, ./tests/nstun_ip_test, 0)
$(call run_test, ./tests/nstun_udp_peer_test, 0)
$(call run_test, ./tests/mount_source_anchor_test, 0)
# --- Basic sanity tests ---
$(call run_test, ./nsjail -q -Mo --chroot / --user 99999 --group 99999 -- /bin/true, 0)
$(call run_test, ./nsjail -q -Mo --chroot / --user 99999 --group 99999 -- /bin/false, 1)
Expand Down
4 changes: 4 additions & 0 deletions mnt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ static bool initCloneNs(nsj_t* nsj) {
close(p.fd);
p.fd = -1;
}
if (p.src_fd >= 0) {
close(p.src_fd);
p.src_fd = -1;
}
}
};

Expand Down
1 change: 1 addition & 0 deletions mnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct mount_t {
bool is_dir;
bool mounted;
int fd; /* Used by new API for deferred remount */
int src_fd; /* O_PATH handle to a bind source, used to anchor the mount */
};

bool initNs(nsj_t* nsj);
Expand Down
53 changes: 53 additions & 0 deletions mnt_legacy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static mount_t prepareMountPoint(const nsjail::MountPt& proto) {
.is_dir = true,
.mounted = false,
.fd = -1,
.src_fd = -1,
};

if (!proto.prefix_src_env().empty()) {
Expand Down Expand Up @@ -117,6 +118,27 @@ static mount_t prepareMountPoint(const nsjail::MountPt& proto) {
mpt.is_dir = true;
}

/*
* Anchor bind sources to a single O_PATH handle (see mountSinglePoint):
* the is_dir decision above and the object mounted below must refer to
* the same inode, otherwise a concurrent rename of the source can make
* nsjail mount a different host directory than the one it inspected.
*/
if ((mpt.flags & MS_BIND) && !mpt.src.empty() && !mpt.mpt->is_symlink() &&
mpt.mpt->src_content().empty()) {
struct stat st;
mpt.src_fd = TEMP_FAILURE_RETRY(open(mpt.src.c_str(), O_PATH | O_CLOEXEC));
if (mpt.src_fd == -1) {
PLOG_W("open('%s', O_PATH | O_CLOEXEC) for bind source", QC(mpt.src));
} else if (fstat(mpt.src_fd, &st) == -1) {
PLOG_W("fstat('%s') for bind source", QC(mpt.src));
close(mpt.src_fd);
mpt.src_fd = -1;
} else if (!proto.has_is_dir()) {
mpt.is_dir = S_ISDIR(st.st_mode);
}
}

return mpt;
}

Expand Down Expand Up @@ -205,6 +227,14 @@ static bool mountWithDynamicContent(
static bool mountSinglePoint(mount_t* mpt, const char* newroot, const char* tmpdir) {
LOG_D("Mounting (legacy): %s", mnt::describeMountPt(*mpt->mpt).c_str());

/* The anchored source handle is only needed while this mount is set up. */
defer {
if (mpt->src_fd >= 0) {
close(mpt->src_fd);
mpt->src_fd = -1;
}
};

if (!util::isSafeContainmentPath(mpt->dst)) {
LOG_E("Mount destination escapes containment via '.'/'..'/NUL: %s", QC(mpt->dst));
return false;
Expand All @@ -217,6 +247,29 @@ static bool mountSinglePoint(mount_t* mpt, const char* newroot, const char* tmpd
}
std::string srcpath = mpt->src.empty() ? "none" : mpt->src;

/*
* Mount the inode opened by prepareMountPoint(), not the path again: a
* rename/symlink swap between validation and mount would otherwise let
* an unprivileged user substitute a host directory the operator never
* granted to the sandbox. /proc/self/fd is only used as a stable name
* for an already-open file descriptor, no path is re-resolved.
*/
if (mpt->src_fd >= 0 && !mpt->mpt->is_symlink()) {
static const bool have_proc_self_fd = (access("/proc/self/fd", X_OK) == 0);
if (have_proc_self_fd) {
srcpath = "/proc/self/fd/" + std::to_string(mpt->src_fd);
LOG_D("Bind source anchored to fd=%d %s", mpt->src_fd,
QC(mpt->src));
} else {
static bool warned = false;
if (!warned) {
warned = true;
LOG_W("Bind source cannot be anchored without /proc/self/fd; "
"falling back to the source path");
}
}
}

if (!util::createDirRecursively(dstpath.c_str())) {
LOG_W("Failed to create parent directories for '%s'", dstpath.c_str());
return false;
Expand Down
73 changes: 69 additions & 4 deletions mnt_newapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,42 @@ static bool doBindMountAt(mount_t* mpt, int parent_fd, const char* basename) {
flags |= AT_RECURSIVE;
}

LOG_D("open_tree('%s', flags=0x%x)", mpt->src.c_str(), flags);
int mnt_fd = syscall(__NR_open_tree, AT_FDCWD, mpt->src.c_str(), flags);
/*
* Mount the inode that prepareMountPoint() opened and validated, not the
* path again: re-resolving mpt->src here would let a rename/symlink swap
* between validation and mount substitute an unrelated host directory.
*/
if (mpt->src_fd >= 0) {
LOG_D("open_tree(fd=%d, '%s', flags=0x%x)", mpt->src_fd, mpt->src.c_str(),
flags | AT_EMPTY_PATH);
} else {
LOG_D("open_tree('%s', flags=0x%x)", mpt->src.c_str(), flags);
}
int mnt_fd;
if (mpt->src_fd >= 0) {
mnt_fd = syscall(__NR_open_tree, mpt->src_fd, "", flags | AT_EMPTY_PATH);
if (mnt_fd < 0 && (errno == EINVAL || errno == ENOSYS || errno == EOPNOTSUPP)) {
/*
* Kernels older than the AT_EMPTY_PATH support in open_tree(2) can
* still clone the same inode through the descriptor's magic link.
*/
std::string fdpath = "/proc/self/fd/" + std::to_string(mpt->src_fd);
mnt_fd = syscall(__NR_open_tree, AT_FDCWD, fdpath.c_str(), flags);
if (mnt_fd >= 0) {
LOG_D("open_tree('%s', flags=0x%x)", fdpath.c_str(), flags);
}
}
if (mnt_fd < 0) {
static bool warned = false;
if (!warned) {
warned = true;
LOG_W("open_tree(source fd) failed, falling back to the source path");
}
mnt_fd = syscall(__NR_open_tree, AT_FDCWD, mpt->src.c_str(), flags);
}
} else {
mnt_fd = syscall(__NR_open_tree, AT_FDCWD, mpt->src.c_str(), flags);
}
if (mnt_fd < 0) {
PLOG_W("open_tree('%s')", mpt->src.c_str());
return false;
Expand Down Expand Up @@ -537,6 +571,14 @@ static bool doBindMountAt(mount_t* mpt, int parent_fd, const char* basename) {
static bool mountSinglePointAt(mount_t* mpt, int root_fd, int staging_fd) {
LOG_D("Mounting (new API): %s", mnt::describeMountPt(*mpt->mpt).c_str());

/* The bind source handle is only needed while the mount is being set up. */
defer {
if (mpt->src_fd >= 0) {
close(mpt->src_fd);
mpt->src_fd = -1;
}
};

const char* rel_dst = util::stripLeadingSlashes(mpt->dst.c_str());
/* Empty / only-slashes dst means the jail root (represented as "."). */
if (!rel_dst[0]) {
Expand Down Expand Up @@ -621,6 +663,7 @@ static mount_t prepareMountPoint(const nsjail::MountPt& proto) {
.is_dir = true,
.mounted = false,
.fd = -1,
.src_fd = -1,
};

if (!proto.prefix_src_env().empty()) {
Expand Down Expand Up @@ -669,15 +712,37 @@ static mount_t prepareMountPoint(const nsjail::MountPt& proto) {
}
}

/*
* Anchor every bind source to a single O_PATH handle. doBindMountAt()
* clones exactly this handle, so the object mounted is the object
* inspected here even if the path is concurrently renamed or replaced.
*/
bool src_is_dir = false;
bool src_stat_ok = false;
if ((mpt.flags & MS_BIND) && !mpt.src.empty()) {
struct stat st;
mpt.src_fd = TEMP_FAILURE_RETRY(open(mpt.src.c_str(), O_PATH | O_CLOEXEC));
if (mpt.src_fd == -1) {
PLOG_W("open('%s', O_PATH | O_CLOEXEC) for bind source", QC(mpt.src));
} else if (fstat(mpt.src_fd, &st) == -1) {
PLOG_W("fstat('%s') for bind source", QC(mpt.src));
close(mpt.src_fd);
mpt.src_fd = -1;
} else {
src_stat_ok = true;
src_is_dir = S_ISDIR(st.st_mode);
}
}

if (proto.has_is_dir()) {
mpt.is_dir = proto.is_dir();
} else if (!proto.src_content().empty()) {
mpt.is_dir = false;
} else if (mpt.src.empty()) {
mpt.is_dir = true;
} else if (mpt.flags & MS_BIND) {
struct stat st;
mpt.is_dir = (stat(mpt.src.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
/* An unreadable/absent source keeps is_dir=false so the mount fails. */
mpt.is_dir = src_stat_ok && src_is_dir;
} else {
mpt.is_dir = true;
}
Expand Down
148 changes: 148 additions & 0 deletions tests/mount_source_anchor_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Tests that a bind-mount source is anchored to the inode nsjail inspected,
* so a rename/symlink swap between validation and mount cannot substitute a
* different host directory.
*
* Sequence A (path re-resolved, behaviour before the fix):
* stat(path) -> swap path -> open_tree(AT_FDCWD, path) => attacker wins
* Sequence B (descriptor anchored, behaviour after the fix):
* fd = open(path, O_PATH) -> swap path ->
* open_tree(fd, "", AT_EMPTY_PATH) => validated inode
*
* Needs root (mount(2) in a private mount namespace); skips when not root.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#ifndef OPEN_TREE_CLONE
#define OPEN_TREE_CLONE 1
#endif
#ifndef OPEN_TREE_CLOEXEC
#define OPEN_TREE_CLOEXEC 02000000
#endif
#ifndef MOVE_MOUNT_F_EMPTY_PATH
#define MOVE_MOUNT_F_EMPTY_PATH 0x00000004
#endif

struct fixture {
char base[256];
char root[512];
char evil[512];
char mnt[512];
char file[512];
};

static int make_dir(const char* path) {
return mkdir(path, 0755) == 0 || errno == EEXIST ? 0 : -1;
}

static int make_file(const char* path, char c) {
int fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd < 0) return -1;
int ok = write(fd, &c, 1) == 1;
close(fd);
return ok ? 0 : -1;
}

/* root/root.txt = 'R', evil/root.txt = 'E', root/evil_admits_swap = absent */
static int prepare(struct fixture* f, const char* base) {
snprintf(f->base, sizeof(f->base), "%s", base);
snprintf(f->root, sizeof(f->root), "%s/root", base);
snprintf(f->evil, sizeof(f->evil), "%s/evil", base);
snprintf(f->mnt, sizeof(f->mnt), "%s/mnt", base);
snprintf(f->file, sizeof(f->file), "%s/root.txt", f->mnt);
if (make_dir(f->root) || make_dir(f->evil) || make_dir(f->mnt)) return -1;
char p[600];
snprintf(p, sizeof(p), "%s/root.txt", f->root);
if (make_file(p, 'R')) return -1;
snprintf(p, sizeof(p), "%s/root.txt", f->evil);
return make_file(p, 'E');
}

static char mounted_first_byte(const char* file) {
char c = '?';
int fd = open(file, O_RDONLY);
if (fd < 0) return '?';
if (read(fd, &c, 1) != 1) c = '?';
close(fd);
return c;
}

/* Attacker replaces the configured source path with their own directory. */
static int swap(struct fixture* f, const char* savename) {
char saved[600];
snprintf(saved, sizeof(saved), "%s/%s", f->base, savename);
if (rename(f->root, saved) != 0) return -1;
if (rename(f->evil, f->root) != 0) return -1;
return 0;
}

static int attach(int tree_fd, const char* dst) {
int rc = syscall(__NR_move_mount, tree_fd, "", AT_FDCWD, dst, MOVE_MOUNT_F_EMPTY_PATH);
close(tree_fd);
return rc;
}

int main(void) {
if (geteuid() != 0) {
printf("SKIP: needs root for mount(2)\n");
return 0;
}
if (unshare(CLONE_NEWNS) != 0) {
printf("SKIP: cannot create a mount namespace: %s\n", strerror(errno));
return 0;
}
mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);

char base_a[] = "/tmp/nsjail-srcanchor-A.XXXXXX";
char base_b[] = "/tmp/nsjail-srcanchor-B.XXXXXX";
if (!mkdtemp(base_a) || !mkdtemp(base_b)) {
perror("mkdtemp");
return 1;
}

/* A: path is resolved again after the swap -> attacker's tree wins. */
struct fixture a;
if (prepare(&a, base_a) != 0) { perror("prepare A"); return 1; }
struct stat st;
if (stat(a.root, &st) != 0) { perror("stat"); return 1; }
if (swap(&a, "root.saved") != 0) { perror("swap A"); return 1; }
int tree = syscall(__NR_open_tree, AT_FDCWD, a.root, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
if (tree < 0) { perror("open_tree(path)"); return 1; }
if (attach(tree, a.mnt) != 0) { perror("move_mount A"); return 1; }
char path_result = mounted_first_byte(a.file);
umount2(a.mnt, MNT_DETACH);

/* B: the descriptor opened before the swap pins the validated inode. */
struct fixture b;
if (prepare(&b, base_b) != 0) { perror("prepare B"); return 1; }
int src_fd = open(b.root, O_PATH | O_CLOEXEC);
if (src_fd < 0) { perror("open(O_PATH)"); return 1; }
if (fstat(src_fd, &st) != 0) { perror("fstat"); return 1; }
if (swap(&b, "root.saved") != 0) { perror("swap B"); return 1; }
tree = syscall(__NR_open_tree, src_fd, "", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
if (tree < 0) { perror("open_tree(fd)"); return 1; }
close(src_fd);
if (attach(tree, b.mnt) != 0) { perror("move_mount B"); return 1; }
char fd_result = mounted_first_byte(b.file);
umount2(b.mnt, MNT_DETACH);

printf("path re-resolved after swap : mounted '%c' (attacker's directory)\n",
path_result);
printf("descriptor anchored, swap : mounted '%c' (validated directory)\n",
fd_result);
int ok = (path_result == 'E' && fd_result == 'R');
printf("%s\n", ok ? "PASS: anchoring prevents mount-source substitution"
: "FAIL: unexpected mount contents");
return ok ? 0 : 1;
}
Loading
0