mirror of
https://github.com/linux-msm/laptops-kernel.git
synced 2026-08-13 14:19:53 -07:00
selftests/filesystems: add tests for empty mount namespaces
Add a test suite for the UNSHARE_EMPTY_MNTNS and CLONE_EMPTY_MNTNS flags exercising the empty mount namespace functionality through the kselftest harness. The tests cover: - basic functionality: unshare succeeds, exactly one mount exists in the new namespace, root and cwd point to the same mount - flag interactions: UNSHARE_EMPTY_MNTNS works standalone without explicit CLONE_NEWNS, combines correctly with CLONE_NEWUSER and other namespace flags (CLONE_NEWUTS, CLONE_NEWIPC) - edge cases: EPERM without capabilities, works from a user namespace, many source mounts still result in one mount, cwd on a different mount gets reset to root - error paths: invalid flags return EINVAL - regression: plain CLONE_NEWNS still copies the full mount tree, other namespace unshares are unaffected - mount properties: the root mount has the expected statmount properties, is its own parent, and is the only entry returned by listmount - repeated unshare: consecutive UNSHARE_EMPTY_MNTNS calls each produce a new namespace with a distinct mount ID - overmount workflow: verifies the intended usage pattern of creating an empty mount namespace with a nullfs root and then mounting tmpfs over it to build a writable filesystem from scratch Link: https://patch.msgid.link/20260306-work-empty-mntns-consolidated-v1-2-6eb30529bbb0@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
empty_mntns_test
|
||||
overmount_chroot_test
|
||||
@@ -0,0 +1,11 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
|
||||
LDLIBS += -lcap
|
||||
|
||||
TEST_GEN_PROGS := empty_mntns_test overmount_chroot_test
|
||||
|
||||
include ../../lib.mk
|
||||
|
||||
$(OUTPUT)/empty_mntns_test: ../utils.c
|
||||
$(OUTPUT)/overmount_chroot_test: ../utils.c
|
||||
@@ -0,0 +1,50 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#ifndef EMPTY_MNTNS_H
|
||||
#define EMPTY_MNTNS_H
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../statmount/statmount.h"
|
||||
|
||||
#ifndef UNSHARE_EMPTY_MNTNS
|
||||
#define UNSHARE_EMPTY_MNTNS 0x00100000
|
||||
#endif
|
||||
|
||||
#ifndef CLONE_EMPTY_MNTNS
|
||||
#define CLONE_EMPTY_MNTNS (1ULL << 37)
|
||||
#endif
|
||||
|
||||
static inline ssize_t count_mounts(void)
|
||||
{
|
||||
uint64_t list[4096];
|
||||
|
||||
return listmount(LSMT_ROOT, 0, 0, list, sizeof(list) / sizeof(list[0]), 0);
|
||||
}
|
||||
|
||||
static inline struct statmount *statmount_alloc(uint64_t mnt_id,
|
||||
uint64_t mnt_ns_id,
|
||||
uint64_t mask)
|
||||
{
|
||||
size_t bufsize = 1 << 15;
|
||||
struct statmount *buf;
|
||||
int ret;
|
||||
|
||||
for (;;) {
|
||||
buf = malloc(bufsize);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
|
||||
ret = statmount(mnt_id, mnt_ns_id, 0, mask, buf, bufsize, 0);
|
||||
if (ret == 0)
|
||||
return buf;
|
||||
|
||||
free(buf);
|
||||
if (errno != EOVERFLOW)
|
||||
return NULL;
|
||||
|
||||
bufsize <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* EMPTY_MNTNS_H */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,225 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Test: rootfs overmounted multiple times with chroot into topmost
|
||||
*
|
||||
* This test creates a scenario where:
|
||||
* 1. A new mount namespace is created with a tmpfs root (via pivot_root)
|
||||
* 2. A mountpoint is created and overmounted multiple times
|
||||
* 3. The caller chroots into the topmost mount layer
|
||||
*
|
||||
* The test verifies that:
|
||||
* - Multiple overmounts create separate mount layers
|
||||
* - Each layer's files are isolated
|
||||
* - chroot correctly sets the process's root to the topmost layer
|
||||
* - After chroot, only the topmost layer's files are visible
|
||||
*
|
||||
* Copyright (c) 2024 Christian Brauner <brauner@kernel.org>
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <linux/mount.h>
|
||||
#include <linux/stat.h>
|
||||
#include <sched.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../utils.h"
|
||||
#include "empty_mntns.h"
|
||||
#include "kselftest_harness.h"
|
||||
|
||||
#define NR_OVERMOUNTS 5
|
||||
|
||||
/*
|
||||
* Setup a proper root filesystem using pivot_root.
|
||||
* This ensures we own the root directory in our user namespace.
|
||||
*/
|
||||
static int setup_root(void)
|
||||
{
|
||||
char tmpdir[] = "/tmp/overmount_test.XXXXXX";
|
||||
char oldroot[256];
|
||||
|
||||
if (!mkdtemp(tmpdir))
|
||||
return -1;
|
||||
|
||||
/* Mount tmpfs at the temporary directory */
|
||||
if (mount("tmpfs", tmpdir, "tmpfs", 0, "size=10M"))
|
||||
return -1;
|
||||
|
||||
/* Create directory for old root */
|
||||
snprintf(oldroot, sizeof(oldroot), "%s/oldroot", tmpdir);
|
||||
if (mkdir(oldroot, 0755))
|
||||
return -1;
|
||||
|
||||
/* pivot_root to use the tmpfs as new root */
|
||||
if (syscall(SYS_pivot_root, tmpdir, oldroot))
|
||||
return -1;
|
||||
|
||||
if (chdir("/"))
|
||||
return -1;
|
||||
|
||||
/* Unmount old root */
|
||||
if (umount2("/oldroot", MNT_DETACH))
|
||||
return -1;
|
||||
|
||||
/* Remove oldroot directory */
|
||||
if (rmdir("/oldroot"))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Test scenario:
|
||||
* 1. Enter a user namespace to gain CAP_SYS_ADMIN
|
||||
* 2. Create a new mount namespace
|
||||
* 3. Setup a tmpfs root via pivot_root
|
||||
* 4. Create a mountpoint /newroot and overmount it multiple times
|
||||
* 5. Create a marker file in each layer
|
||||
* 6. Chroot into /newroot (the topmost overmount)
|
||||
* 7. Verify we're in the topmost layer (only topmost marker visible)
|
||||
*/
|
||||
TEST(overmount_chroot)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
pid = fork();
|
||||
ASSERT_GE(pid, 0);
|
||||
|
||||
if (pid == 0) {
|
||||
ssize_t nr_mounts;
|
||||
uint64_t mnt_ids[NR_OVERMOUNTS + 1];
|
||||
uint64_t root_id_before, root_id_after;
|
||||
struct statmount *sm;
|
||||
char marker[64];
|
||||
int fd, i;
|
||||
|
||||
/* Step 1: Enter user namespace for privileges */
|
||||
if (enter_userns())
|
||||
_exit(1);
|
||||
|
||||
/* Step 2: Create a new mount namespace */
|
||||
if (unshare(CLONE_NEWNS))
|
||||
_exit(2);
|
||||
|
||||
/* Step 3: Make the mount tree private */
|
||||
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
|
||||
_exit(3);
|
||||
|
||||
/* Step 4: Setup a proper tmpfs root via pivot_root */
|
||||
if (setup_root())
|
||||
_exit(4);
|
||||
|
||||
/* Create the base mount point for overmounting */
|
||||
if (mkdir("/newroot", 0755))
|
||||
_exit(5);
|
||||
|
||||
/* Mount base tmpfs on /newroot */
|
||||
if (mount("tmpfs", "/newroot", "tmpfs", 0, "size=1M"))
|
||||
_exit(6);
|
||||
|
||||
/* Record base mount ID */
|
||||
mnt_ids[0] = get_unique_mnt_id("/newroot");
|
||||
if (!mnt_ids[0])
|
||||
_exit(7);
|
||||
|
||||
/* Create marker in base layer */
|
||||
fd = open("/newroot/layer_0", O_CREAT | O_RDWR, 0644);
|
||||
if (fd < 0)
|
||||
_exit(8);
|
||||
if (write(fd, "layer_0", 7) != 7) {
|
||||
close(fd);
|
||||
_exit(9);
|
||||
}
|
||||
close(fd);
|
||||
|
||||
/* Step 5: Overmount /newroot multiple times with tmpfs */
|
||||
for (i = 0; i < NR_OVERMOUNTS; i++) {
|
||||
if (mount("tmpfs", "/newroot", "tmpfs", 0, "size=1M"))
|
||||
_exit(10);
|
||||
|
||||
/* Record mount ID for this layer */
|
||||
mnt_ids[i + 1] = get_unique_mnt_id("/newroot");
|
||||
if (!mnt_ids[i + 1])
|
||||
_exit(11);
|
||||
|
||||
/* Create a marker file in each layer */
|
||||
snprintf(marker, sizeof(marker), "/newroot/layer_%d", i + 1);
|
||||
fd = open(marker, O_CREAT | O_RDWR, 0644);
|
||||
if (fd < 0)
|
||||
_exit(12);
|
||||
|
||||
if (write(fd, marker, strlen(marker)) != (ssize_t)strlen(marker)) {
|
||||
close(fd);
|
||||
_exit(13);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/* Verify mount count increased */
|
||||
nr_mounts = count_mounts();
|
||||
if (nr_mounts < NR_OVERMOUNTS + 2)
|
||||
_exit(14);
|
||||
|
||||
/* Record root mount ID before chroot */
|
||||
root_id_before = get_unique_mnt_id("/newroot");
|
||||
|
||||
/* Verify this is the topmost layer's mount */
|
||||
if (root_id_before != mnt_ids[NR_OVERMOUNTS])
|
||||
_exit(15);
|
||||
|
||||
/* Step 6: Chroot into /newroot (the topmost overmount) */
|
||||
if (chroot("/newroot"))
|
||||
_exit(16);
|
||||
|
||||
/* Change to root directory within the chroot */
|
||||
if (chdir("/"))
|
||||
_exit(17);
|
||||
|
||||
/* Step 7: Verify we're in the topmost layer */
|
||||
root_id_after = get_unique_mnt_id("/");
|
||||
|
||||
/* The mount ID should be the same as the topmost layer */
|
||||
if (root_id_after != mnt_ids[NR_OVERMOUNTS])
|
||||
_exit(18);
|
||||
|
||||
/* Verify the topmost layer's marker file exists */
|
||||
snprintf(marker, sizeof(marker), "/layer_%d", NR_OVERMOUNTS);
|
||||
if (access(marker, F_OK))
|
||||
_exit(19);
|
||||
|
||||
/* Verify we cannot see markers from lower layers (they're hidden) */
|
||||
for (i = 0; i < NR_OVERMOUNTS; i++) {
|
||||
snprintf(marker, sizeof(marker), "/layer_%d", i);
|
||||
if (access(marker, F_OK) == 0)
|
||||
_exit(20);
|
||||
}
|
||||
|
||||
/* Verify the root mount is tmpfs */
|
||||
sm = statmount_alloc(root_id_after, 0,
|
||||
STATMOUNT_MNT_BASIC | STATMOUNT_MNT_ROOT |
|
||||
STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE);
|
||||
if (!sm)
|
||||
_exit(21);
|
||||
|
||||
if (sm->mask & STATMOUNT_FS_TYPE) {
|
||||
if (strcmp(sm->str + sm->fs_type, "tmpfs") != 0) {
|
||||
free(sm);
|
||||
_exit(22);
|
||||
}
|
||||
}
|
||||
|
||||
free(sm);
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
ASSERT_EQ(wait_for_pid(pid), 0);
|
||||
}
|
||||
|
||||
TEST_HARNESS_MAIN
|
||||
@@ -158,7 +158,7 @@ static int get_userns_fd_cb(void *data)
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
static int wait_for_pid(pid_t pid)
|
||||
int wait_for_pid(pid_t pid)
|
||||
{
|
||||
int status, ret;
|
||||
|
||||
@@ -450,7 +450,7 @@ out_close:
|
||||
return fret;
|
||||
}
|
||||
|
||||
static int write_file(const char *path, const char *val)
|
||||
int write_file(const char *path, const char *val)
|
||||
{
|
||||
int fd = open(path, O_WRONLY);
|
||||
size_t len = strlen(val);
|
||||
|
||||
@@ -44,6 +44,8 @@ static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
|
||||
return true;
|
||||
}
|
||||
|
||||
extern int wait_for_pid(pid_t pid);
|
||||
extern int write_file(const char *path, const char *val);
|
||||
extern uint64_t get_unique_mnt_id(const char *path);
|
||||
|
||||
#endif /* __IDMAP_UTILS_H */
|
||||
|
||||
Reference in New Issue
Block a user