8000 mnt: mark staging tmpfs MS_UNBINDABLE before processing config mounts by matepad007 · Pull Request #415 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content
Merged
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/util_msg_test tests/nstun_buffer_budget_test tests/nstun_flow_limit_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/nstun_encap_test tests/nstun_tcp_capacity_test tests/mount_source_anchor_test tests/mnt_options_test tests/unotify_syscall_test tests/unotify_stats_test
TEST_BINS = tests/util_msg_test tests/nstun_buffer_budget_test tests/nstun_flow_limit_test tests/nstun_policy_test tests/nstun_ip_test tests/nstun_udp_peer_test tests/nstun_encap_test tests/nstun_tcp_capacity_test tests/mount_source_anchor_test tests/mount_staging_unbindable_test tests/mnt_options_test tests/unotify_syscall_test tests/unotify_stats_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 @@ -169,6 +169,9 @@ test-mnt-dst-symlink: $(BIN)
tests/mount_source_anchor_test: tests/mount_source_anchor_test.c
$(CC) -O2 -Wall -o $@ $<

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

test: $(BIN) $(TEST_BINS) test-cmdline test-newapi-recursive-readonly test-mnt-dst-symlink test-nstun-tcp-capacity
$(call run_test, ./tests/util_msg_test, 0)
$(call run_test, ./tests/nstun_buffer_budget_test, 0)
Expand All @@ -178,6 +181,7 @@ test: $(BIN) $(TEST_BINS) test-cmdline test-newapi-recursive-readonly test-mnt-d
$(call run_test, ./tests/nstun_udp_peer_test, 0)
$(call run_test, ./tests/nstun_encap_test, 0)
$(call run_test, ./tests/mount_source_anchor_test, 0)
$(call run_test, ./tests/mount_staging_unbindable_test, 0)
$(call run_test, ./tests/mnt_options_test, 0)
$(call run_test, ./tests/unotify_syscall_test, 0)
$(call run_test, ./tests/unotif 8000 y_stats_test, 0)
Expand Down
12 changes: 12 additions & 0 deletions mnt_legacy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,18 @@ std::unique_ptr<std::string> buildMountTree(nsj_t* nsj, std::vector<mnt::mount_t
return nullptr;
}

/*
* Keep the staging root out of any later recursive bind. Without this, a config
* mount that recursively binds a host directory which happens to be an ancestor
* of destdir (e.g. binding "/tmp" while destdir is "/tmp/nsjail.<uid>.root") would
* clone this tmpfs into the jail before its own ro/nosuid/nodev/noexec flags are
* applied below, leaking a writable copy of the jail root through the bind.
*/
if (mount(nullptr, destdir->c_str(), nullptr, MS_UNBINDABLE, nullptr) == -1) {
PLOG_E("mount('%s', MS_UNBINDABLE)", destdir->c_str());
return nullptr;
}

auto tmpdir = mnt::findWorkDir(nsj, "tmp");
if (!tmpdir) {
return nullptr;
Expand Down
23 changes: 18 additions & 5 deletions mnt_newapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,18 @@ std::unique_ptr<std::string> buildMountTree(nsj_t* nsj, std::vector<mnt::mount_t
}
close(root_mfd);

/*
* Keep the staging root out of any later recursive bind. Without this, a config
* mount that recursively binds a host directory which happens to be an ancestor
* of destdir (e.g. binding "/tmp" while destdir is "/tmp/nsjail.<uid>.root") would
* clone this tmpfs into the jail before the per-mount restrictions applied in the
* loop below, or the final read-only remount, ever reach it.
*/
if (mount(nullptr, destdir->c_str(), nullptr, MS_UNBINDABLE, nullptr) == -1) {
PLOG_E("mount('%s', MS_UNBINDABLE)", destdir->c_str());
return nullptr;
}

int root_fd =
openat(AT_FDCWD, destdir->c_str(), O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
if (root_fd < 0) {
Expand All @@ -673,13 +685,14 @@ std::unique_ptr<std::string> buildMountTree(nsj_t* nsj, std::vector<mnt::mount_t
};

/*
* A stable handle to the writable staging tmpfs. root_fd is refreshed to the
* (read-only) visible root once a root overmount happens, but dynamic-content
* mounts still need a writable directory to stage their backing file in.
* A writable directory to stage dynamic-content backing files in. This cannot be a
* dup of root_fd: destdir is now MS_UNBINDABLE, and open_tree(OPEN_TREE_CLONE) on
* anything nested inside an unbindable mount fails, including a single file, so
* dynamic content gets its own detached tmpfs instead of sharing destdir's.
*/
int staging_fd = TEMP_FAILURE_RETRY(fcntl(root_fd, F_DUPFD_CLOEXEC, 0));
int staging_fd = createDetachedTmpfs(16 * 1024 * 1024);
if (staging_fd < 0) {
PLOG_E("fcntl(root_fd, F_DUPFD_CLOEXEC)");
LOG_E("Failed to create dynamic-content staging tmpfs");
return nullptr;
}
defer {
Expand Down
112 changes: 112 additions & 0 deletions tests/mount_staging_unbindable_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Regression test for the staging-tmpfs mount leak: a recursive bind of an
* ancestor directory of nsjail's own staging tmpfs (e.g. a config mount with
* src:"/tmp" when the staging dir is under /tmp) used to sweep that tmpfs
* into its own copy_tree() clone, carrying its contents along with it.
*
* Fix: mark the staging tmpfs MS_UNBINDABLE right after creating it, before
* any config mount is processed. Per Documentation/filesystems/sharedsubtree.rst,
* an unbindable mount is pruned from any later recursive bind's copy_tree.
*
* This reproduces the staging setup and the ancestor bind directly with
* mount(2), rather than linking mnt_legacy.cc/mnt_newapi.cc, so it can run as
* a standalone binary (same approach as tests/mount_source_anchor_test.c).
*
* 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 <unistd.h>

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

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

static int marker_visible(const char* path) {
return access(path, F_OK) == 0;
}

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[] = "/tmp/nsjail-stagingubnd.XXXXXX";
if (!mkdtemp(base)) {
perror("mkdtemp");
return 1;
}

char staging[600], marker[600], clone_a[600], marker_a[600], clone_b[600], marker_b[600];
snprintf(staging, sizeof(staging), "%s/staging", base);
snprintf(marker, sizeof(marker), "%s/marker", staging);
snprintf(clone_a, sizeof(clone_a), "%s/clone_a", base);
snprintf(marker_a, sizeof(marker_a), "%s/staging/marker", clone_a);
snprintf(clone_b, sizeof(clone_b), "%s/clone_b", base);
snprintf(marker_b, sizeof(marker_b), "%s/staging/marker", clone_b);

/* Mirror buildMountTree: create destdir and mount a tmpfs on it. */
if (make_dir(staging) != 0) { perror("mkdir(staging)"); return 1; }
if (mount(NULL, staging, "tmpfs", 0, "size=1048576") != 0) {
perror("mount(staging, tmpfs)");
return 1;
}
if (write_marker(marker) != 0) { perror("write marker"); return 1; }

/* A: before the fix is applied, a recursive bind of the ancestor (base)
* sweeps the staging tmpfs into its clone. */
if (make_dir(clone_a) != 0) { perror("mkdir(clone_a)"); return 1; }
if (mount(base, clone_a, NULL, MS_BIND | MS_REC, NULL) != 0) {
perror("mount(base -> clone_a, MS_BIND|MS_REC)");
return 1;
}
int leaked = marker_visible(marker_a);
umount2(clone_a, MNT_DETACH);

/* Apply the fix: mark the staging tmpfs unbindable. */
if (mount(NULL, staging, NULL, MS_UNBINDABLE, NULL) != 0) {
perror("mount(staging, MS_UNBINDABLE)");
return 1;
}

/* B: after the fix, the same ancestor bind must not carry the staging
* tmpfs (or its contents) along with it. */
if (make_dir(clone_b) != 0) { perror("mkdir(clone_b)"); return 1; }
if (mount(base, clone_b, NULL, MS_BIND | MS_REC, NULL) != 0) {
perror("mount(base -> clone_b, MS_BIND|MS_REC)");
return 1;
}
int still_leaked = marker_visible(marker_b);
umount2(clone_b, MNT_DETACH);

printf("before fix: ancestor bind %s the staging tmpfs's contents\n",
leaked ? "carried along" : "did NOT carry along");
printf("after fix: ancestor bind %s the staging tmpfs's contents\n",
still_leaked ? "carried along" : "did NOT carry along");

int ok = leaked && !still_leaked;
printf("%s\n", ok ? "PASS: MS_UNBINDABLE prevents the staging-mount leak"
: "FAIL: staging-mount leak not reproduced/fixed as expected");
return ok ? 0 : 1;
}
0