| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <fcntl.h> | |
| #include <limits.h> | |
| #include <linux/runfd.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/stat.h> | |
| #include <sys/wait.h> | |
| #include <linux/wait.h> | |
| #include "base.h" | |
| #include "runfd.h" | |
| #include "spawn.h" | |
| #include "tree.h" | |
| #include "uapi.h" | |
| int runfd_get_ns_fd(int runfd, uint64_t type) | |
| { | |
| int fd = ioctl(runfd, RUNFD_GET_NSFD, (unsigned long)type); | |
| return fd < 0 ? err_ctx(-errno, "ioctl RUNFD_GET_NSFD") : fd; | |
| } | |
| int runfd_share_ns_fd(int runfd, int nsfd) | |
| { | |
| if (nsfd < 0) | |
| return err_ctx(-EBADF, "negative namespace fd"); | |
| if (ioctl(runfd, RUNFD_SHARE_NSFD, (unsigned long)nsfd) < 0) | |
| return err_ctx(-errno, "ioctl RUNFD_SHARE_NSFD"); | |
| return 0; | |
| } | |
| int runfd_set_idmap(int runfd, const struct userns_idmap_extent *uid, | |
| const struct userns_idmap_extent *gid, uint32_t flags) | |
| { | |
| struct userns_idmap_extent uid_ext = *uid, gid_ext = *gid; | |
| struct userns_idmap map = { | |
| .nr_uid_extents = 1, | |
| .nr_gid_extents = 1, | |
| .flags = flags, | |
| .uid_extents = (uintptr_t)&uid_ext, | |
| .gid_extents = (uintptr_t)&gid_ext, | |
| }; | |
| int fd, ret = 0; | |
| fd = runfd_get_ns_fd(runfd, CLONE_NEWUSER); | |
| if (fd < 0) | |
| return fd; | |
| uid_ext.reserved = 0; | |
| gid_ext.reserved = 0; | |
| if (ioctl(fd, NS_SET_IDMAP, &map) < 0) | |
| ret = err_ctx(-errno, "ioctl NS_SET_IDMAP"); | |
| close_fd(fd); | |
| return ret; | |
| } | |
| int runfd_bind_fsconfig(int runfd, int fsfd) | |
| { | |
| if (syscall(__NR_fsconfig, fsfd, FSCONFIG_SET_RUN, NULL, NULL, runfd) < | |
| 0) | |
| return err_ctx(-errno, "fsconfig FSCONFIG_SET_RUN"); | |
| return 0; | |
| } | |
| int runfd_seal_root(int runfd, const struct mount_tree *tree) | |
| { | |
| int fd, ret; | |
| fd = runfd_get_ns_fd(runfd, CLONE_NEWNS); | |
| if (fd < 0) | |
| return fd; | |
| ret = mount_tree_seal_into(tree, fd); | |
| close_fd(fd); | |
| return ret; | |
| } | |
| int runfd_send_signal(int runfd, int sig) | |
| { | |
| if (syscall(__NR_pidfd_send_signal, runfd, sig, NULL, 0U) < 0 && | |
| errno != ESRCH) | |
| return err_ctx(-errno, "pidfd_send_signal runfd"); | |
| return 0; | |
| } | |
| int runfd_poll_exit(int runfd, int *status) | |
| { | |
| siginfo_t info = {}; | |
| if (syscall(__NR_waitid, P_RUNFD, runfd, &info, | |
| WEXITED | WNOHANG | WNOWAIT, NULL) < 0) { | |
| if (errno == ECHILD || errno == EAGAIN) | |
| return 0; | |
| return err_ctx(-errno, "waitid P_RUNFD"); | |
| } | |
| if (info.si_pid <= 0) | |
| return 0; | |
| *status = spawn_wait_status(&info); | |
| return 1; | |
| } | |
| int runfd_spawn(int runfd, const struct spawn_plan *plan) | |
| { | |
| int pidfd = -1; | |
| struct clone_args args = { | |
| .flags = CLONE_INTO_RUN | CLONE_PIDFD | CLONE_INTO_CGROUP | | |
| CLONE_AUTOREAP | CLONE_NNP | CLONE_CLEAR_SIGHAND, | |
| .pidfd = (uintptr_t)&pidfd, | |
| .cgroup = plan->cgroup_fd, | |
| .runfd = runfd, | |
| }; | |
| struct child_bootstrap boot; | |
| int err_pipe[2], ret; | |
| long child; | |
| if (!plan->sandbox || !plan->argv || !plan->argv[0]) | |
| return err_ctx(-EINVAL, "incomplete spawn plan"); | |
| if (plan->cgroup_fd < 0) | |
| return err_ctx(-EBADF, "negative cgroup fd"); | |
| ret = child_bootstrap_init(&boot, plan); | |
| if (ret) | |
| return ret; | |
| if (pipe2(err_pipe, O_CLOEXEC) < 0) { | |
| ret = err_ctx(-errno, "pipe2 spawn sync"); | |
| goto destroy_boot; | |
| } | |
| child = syscall(__NR_clone3, &args, sizeof(args)); | |
| if (child < 0) { | |
| ret = err_ctx(-errno, "clone3 CLONE_INTO_RUN"); | |
| close_fd(err_pipe[1]); | |
| close_fd(err_pipe[0]); | |
| goto destroy_boot; | |
| } | |
| if (!child) { | |
| close_fd(err_pipe[0]); | |
| child_bootstrap_run(&boot, err_pipe[1]); | |
| } | |
| close_fd(err_pipe[1]); | |
| child_bootstrap_destroy(&boot); | |
| ret = await_child_exec(err_pipe[0], pidfd); | |
| close_fd(err_pipe[0]); | |
| if (ret < 0) { | |
| close_fd(pidfd); | |
| return ret; | |
| } | |
| return pidfd; | |
| destroy_boot: | |
| child_bootstrap_destroy(&boot); | |
| return ret; | |
| } | |
| static int open_runfd(void) | |
| { | |
| long fd = syscall(__NR_runfd, RUNFD_AUTOKILL); | |
| if (fd < 0) | |
| return err_ctx(-errno, "runfd"); | |
| if (fd > INT_MAX) | |
| return err_ctx(-EINVAL, "runfd fd overflow"); | |
| return fd; | |
| } | |
| int runfd_create(uint32_t *userns_inum) | |
| { | |
| int fd, userns, type, ret; | |
| struct stat st; | |
| fd = open_runfd(); | |
| if (fd < 0) | |
| return fd; | |
| userns = runfd_get_ns_fd(fd, CLONE_NEWUSER); | |
| if (userns < 0) { | |
| ret = userns; | |
| goto close_runfd; | |
| } | |
| type = ioctl(userns, NS_GET_NSTYPE); | |
| if (type < 0) { | |
| ret = err_ctx(-errno, "ioctl NS_GET_NSTYPE"); | |
| goto close_userns; | |
| } | |
| if (type != CLONE_NEWUSER) { | |
| ret = err_ctx(-EINVAL, | |
| "runfd returned the wrong namespace type"); | |
| goto close_userns; | |
| } | |
| if (fstat(userns, &st) < 0) { | |
| ret = err_ctx(-errno, "fstat userns"); | |
| goto close_userns; | |
| } | |
| if (st.st_ino > UINT32_MAX) { | |
| ret = err_ctx(-EINVAL, "userns inode overflow"); | |
| goto close_userns; | |
| } | |
| *userns_inum = st.st_ino; | |
| close_fd(userns); | |
| return fd; | |
| close_userns: | |
| close_fd(userns); | |
| close_runfd: | |
| close_fd(fd); | |
| return ret; | |
| } | |
| int runfd_probe(void) | |
| { | |
| int fd, userns, ret = 0; | |
| fd = open_runfd(); | |
| if (fd < 0) | |
| return fd; | |
| userns = runfd_get_ns_fd(fd, CLONE_NEWUSER); | |
| if (userns < 0) { | |
| ret = userns; | |
| goto close_runfd; | |
| } | |
| if (ioctl(userns, NS_SET_IDMAP, NULL) >= 0) | |
| ret = err_ctx(-EINVAL, "NS_SET_IDMAP accepted a null map"); | |
| else if (errno != EFAULT) | |
| ret = err_ctx(-errno, "probe NS_SET_IDMAP"); | |
| close_fd(userns); | |
| close_runfd: | |
| close_fd(fd); | |
| return ret; | |
| } |