blob: bf59cf480f373167083786340fe9a56cdb15f37c [file]
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Jarkko Sakkinen
#include <fcntl.h>
#include <linux/magic.h>
#include <linux/mount.h>
#include <linux/nsfs.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/statfs.h>
#include <sys/wait.h>
#include "base.h"
#include "tree.h"
#include "uapi.h"
static bool is_locked_submount(char *line)
{
char *point, *opts, *save;
const char *sep = " \t\n";
int i;
if (!strtok_r(line, sep, &save))
return false;
for (i = 0; i < 3; i++) {
if (!strtok_r(NULL, sep, &save))
return false;
}
point = strtok_r(NULL, sep, &save);
if (!point || !strcmp(point, "/"))
return false;
opts = strtok_r(NULL, sep, &save);
if (!opts)
return false;
for (opts = strtok_r(opts, ",", &save); opts;
opts = strtok_r(NULL, ",", &save)) {
if (!strcmp(opts, "locked"))
return true;
}
return false;
}
static int validate_submounts(int proc_dir)
{
char path[32], *line = NULL;
size_t cap = 0;
int fd, ret = 0;
FILE *file;
snprintf(path, sizeof(path), "%d/mountinfo", gettid());
fd = openat(proc_dir, path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return 0;
file = fdopen(fd, "r");
if (!file) {
ret = -errno;
close_fd(fd);
return ret;
}
for (;;) {
errno = 0;
if (getline(&line, &cap, file) < 0) {
if (!feof(file))
ret = errno ? -errno : -EIO;
break;
}
if (is_locked_submount(line)) {
ret = -EPERM;
break;
}
}
free(line);
fclose(file);
return ret;
}
static int enter_source_mntns(int fd)
{
int proc_dir, ret;
/* The source namespace may not mount procfs. */
proc_dir = open("/proc", O_RDONLY | O_CLOEXEC);
if (proc_dir < 0)
return -errno;
if (setns(fd, CLONE_NEWNS) < 0)
ret = -errno;
else
ret = validate_submounts(proc_dir);
close_fd(proc_dir);
if (ret)
return ret;
mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);
return 0;
}
static int child_seal(const struct mount_tree *tree, int mntns_fd)
{
int ret;
ret = enter_source_mntns(tree->fd);
if (ret)
return ret;
if (syscall(__NR_move_mount, AT_FDCWD, "/", mntns_fd, "/",
MOVE_MOUNT_T_MNTNS | MOVE_MOUNT_CLONE) < 0)
return -errno;
return 0;
}
static int seal_from_mntns(const struct mount_tree *tree, int mntns_fd)
{
int pipe_fds[2], ret;
int32_t status;
pid_t child;
ssize_t n;
if (pipe2(pipe_fds, O_CLOEXEC) < 0)
return err_ctx(-errno, "pipe2 tree helper");
child = fork();
if (child < 0) {
ret = err_ctx(-errno, "fork tree helper");
close_fd(pipe_fds[0]);
close_fd(pipe_fds[1]);
return ret;
}
if (!child) {
close_fd(pipe_fds[0]);
status = child_seal(tree, mntns_fd);
while (write(pipe_fds[1], &status, sizeof(status)) < 0 &&
errno == EINTR)
;
_exit(0);
}
close_fd(pipe_fds[1]);
do {
n = read(pipe_fds[0], &status, sizeof(status));
} while (n < 0 && errno == EINTR);
ret = n < 0 ? -errno : n == sizeof(status) ? status : -EIO;
close_fd(pipe_fds[0]);
while (waitpid(child, NULL, 0) < 0 && errno == EINTR)
;
return ret;
}
int mount_tree_from_slot_fd(int fd, struct mount_tree *out)
{
struct statfs st;
bool is_mntns;
int type;
if (fstatfs(fd, &st) < 0)
return err_ctx(-errno, "fstatfs root slot fd");
is_mntns = st.f_type == NSFS_MAGIC;
if (is_mntns) {
type = ioctl(fd, NS_GET_NSTYPE);
if (type < 0)
return err_ctx(-errno, "NS_GET_NSTYPE root slot fd");
if (type != CLONE_NEWNS)
return err_ctx(-EINVAL,
"root slot fd is not a mount namespace");
}
*out = (struct mount_tree){.fd = fd, .is_mntns = is_mntns};
return 0;
}
void mount_tree_destroy(struct mount_tree *tree)
{
close_fd(take_fd(&tree->fd));
}
int mount_tree_seal_into(const struct mount_tree *tree, int mntns_fd)
{
if (tree->is_mntns) {
int ret = seal_from_mntns(tree, mntns_fd);
return ret ? err_ctx(ret, "seal mount namespace into runfd")
: 0;
}
if (syscall(__NR_move_mount, tree->fd, "", mntns_fd, "/",
MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_MNTNS) < 0)
return err_ctx(-errno, "move_mount root tree");
return 0;
}