| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <linux/magic.h> | |
| #include <linux/nsfs.h> | |
| #include <linux/sched.h> | |
| #include <sched.h> | |
| #include <signal.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/statfs.h> | |
| #include <sys/wait.h> | |
| #include "base.h" | |
| #include "cell.h" | |
| #include "fdpass.h" | |
| #include "runfd.h" | |
| static int ensure_netns(int fd) | |
| { | |
| struct statfs st; | |
| int type; | |
| if (fstatfs(fd, &st) < 0) | |
| return -errno; | |
| if (st.f_type != NSFS_MAGIC) | |
| return -EINVAL; | |
| type = ioctl(fd, NS_GET_NSTYPE); | |
| if (type < 0) | |
| return -errno; | |
| return type == CLONE_NEWNET ? 0 : -EINVAL; | |
| } | |
| /* The helper creates the netns in the builder's user namespace. */ | |
| static int allocate_builder_netns(void) | |
| { | |
| int fds[2], ret; | |
| pid_t pid; | |
| ret = fd_pair(fds); | |
| if (ret < 0) | |
| return ret; | |
| pid = fork(); | |
| if (pid < 0) { | |
| ret = -errno; | |
| close_fd(fds[1]); | |
| goto close_parent; | |
| } | |
| if (!pid) { | |
| close_fd(fds[0]); | |
| if (unshare(CLONE_NEWNET) == 0) { | |
| int fd = | |
| open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC); | |
| if (fd >= 0) | |
| send_fd(fds[1], fd); | |
| } | |
| _exit(0); | |
| } | |
| close_fd(fds[1]); | |
| ret = recv_fd(fds[0]); | |
| while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) | |
| ; | |
| close_parent: | |
| close_fd(fds[0]); | |
| return ret; | |
| } | |
| static void configure_new_net(struct cell *cell) | |
| { | |
| int fd = allocate_builder_netns(); | |
| if (fd >= 0) { | |
| runfd_share_ns_fd(cell->runfd, fd); | |
| close_fd(fd); | |
| } | |
| cell->net_mode = NET_SLOT_NEW; | |
| } | |
| static int apply_ids_shape(struct cell *cell, enum id_shape shape, | |
| const struct caller_cred *caller, | |
| struct id_block_pool *pool) | |
| { | |
| struct userns_idmap_extent uid, gid; | |
| int ret; | |
| ret = id_shape_validate_caller(shape, caller->uid, caller->gid); | |
| if (ret < 0) | |
| return ret; | |
| if (cell->runfd < 0) | |
| return -EBUSY; | |
| switch (shape) { | |
| case ID_SINGLE: | |
| uid = (struct userns_idmap_extent){caller->uid, caller->uid, 1, | |
| 0}; | |
| gid = (struct userns_idmap_extent){caller->gid, caller->gid, 1, | |
| 0}; | |
| ret = runfd_set_idmap(cell->runfd, &uid, &gid, 0); | |
| break; | |
| case ID_BLOCK: | |
| ret = id_block_allocate(pool, caller->uid, &cell->id_lease); | |
| if (ret < 0) | |
| return ret; | |
| uid = (struct userns_idmap_extent){ | |
| .lower_first = cell->id_lease.first_id, | |
| .count = ID_BLOCK_SIZE, | |
| }; | |
| ret = runfd_set_idmap(cell->runfd, &uid, &uid, | |
| USERNS_SETGROUPS_ALLOWED); | |
| if (ret < 0) | |
| id_block_release(pool, &cell->id_lease, false); | |
| break; | |
| default: | |
| return -EINVAL; | |
| } | |
| if (ret < 0) | |
| return ret; | |
| cell->id_shape = shape; | |
| return 0; | |
| } | |
| int cell_init(struct cell *cell, uint64_t id, const struct caller_cred *creator) | |
| { | |
| uint32_t userns_inum; | |
| int fd; | |
| fd = runfd_create(&userns_inum); | |
| if (fd < 0) | |
| return fd; | |
| *cell = (struct cell){ | |
| .id = id, | |
| .creator = *creator, | |
| .runfd = fd, | |
| .userns_inum = userns_inum, | |
| .state = CELL_BUILDING, | |
| .root.fd = -1, | |
| .budget.fd = -1, | |
| }; | |
| return 0; | |
| } | |
| int cell_configure(struct cell *cell, const struct slot_action *action, | |
| int *aux_fd, const struct caller_cred *caller, | |
| struct cell_resources *resources) | |
| { | |
| int ret; | |
| if (cell->state != CELL_BUILDING) | |
| return -EBUSY; | |
| switch (action->type) { | |
| case SLOT_HOSTNAME: | |
| if (cell->has_hostname) | |
| return -EBUSY; | |
| memcpy(cell->hostname, action->hostname, | |
| sizeof(cell->hostname)); | |
| cell->has_hostname = true; | |
| return 0; | |
| case SLOT_IDS: | |
| if (cell->id_shape != ID_UNSET) | |
| return -EBUSY; | |
| return apply_ids_shape(cell, action->shape, caller, | |
| &resources->id_pool); | |
| case SLOT_CAPS: | |
| if (cell->has_caps) | |
| return -EBUSY; | |
| cell->caps = action->caps; | |
| cell->has_caps = true; | |
| return 0; | |
| case SLOT_NET_NEW: | |
| if (cell->net_mode != NET_SLOT_NONE || cell->runfd < 0) | |
| return -EBUSY; | |
| configure_new_net(cell); | |
| return 0; | |
| case SLOT_NET_FD: | |
| if (cell->net_mode != NET_SLOT_NONE) | |
| return -EBUSY; | |
| if (*aux_fd < 0) | |
| return -EBADF; | |
| ret = ensure_netns(*aux_fd); | |
| if (ret < 0) | |
| return ret; | |
| if (cell->runfd < 0) | |
| return -EBUSY; | |
| ret = runfd_share_ns_fd(cell->runfd, *aux_fd); | |
| if (ret < 0) | |
| return ret; | |
| cell->net_mode = NET_SLOT_PROVIDED_FD; | |
| return 0; | |
| case SLOT_NET_RIGHTS: | |
| if (cell->has_net_rights) | |
| return -EBUSY; | |
| if (cell->net_mode == NET_SLOT_NONE) | |
| return -EINVAL; | |
| cell->net_rights = action->rights; | |
| cell->has_net_rights = true; | |
| return 0; | |
| case SLOT_ROOT_FD: | |
| if (cell->root.fd >= 0 || cell->root_installed) | |
| return -EBUSY; | |
| if (*aux_fd < 0) | |
| return -EBADF; | |
| ret = mount_tree_from_slot_fd(*aux_fd, &cell->root); | |
| if (!ret) | |
| take_fd(aux_fd); | |
| return ret; | |
| case SLOT_BUDGET_FD: | |
| if (cell->budget.fd >= 0) | |
| return -EBUSY; | |
| if (*aux_fd < 0) | |
| return -EBADF; | |
| ret = budget_claim(*aux_fd, caller, &resources->budgets, | |
| &cell->budget); | |
| if (ret < 0) | |
| return ret; | |
| take_fd(aux_fd); | |
| cell->budget_cgroup_id = cell->budget.cgroup_id; | |
| return 0; | |
| } | |
| return -EINVAL; | |
| } | |
| int cell_open_net(struct cell *cell) | |
| { | |
| if (cell->net_mode == NET_SLOT_NONE) | |
| configure_new_net(cell); | |
| return runfd_get_ns_fd(cell->runfd, CLONE_NEWNET); | |
| } | |
| int cell_idmap_fd(const struct cell *cell) | |
| { | |
| if (cell->id_shape == ID_UNSET) | |
| return -EINVAL; | |
| if (cell->runfd < 0) | |
| return -ESRCH; | |
| return runfd_get_ns_fd(cell->runfd, CLONE_NEWUSER); | |
| } | |
| int cell_seal(struct cell *cell, const struct caller_cred *caller, | |
| struct cell_resources *resources) | |
| { | |
| uint32_t uid = cell->creator.uid, gid = cell->creator.gid; | |
| bool no_net = cell->net_mode == NET_SLOT_NONE; | |
| bool single_id; | |
| int ret; | |
| if (cell->state != CELL_BUILDING) | |
| return -EBUSY; | |
| if ((cell->root.fd < 0 && !cell->root_installed) || cell->budget.fd < 0) | |
| return -EINVAL; | |
| if (cell->id_shape == ID_UNSET) { | |
| ret = apply_ids_shape(cell, ID_SINGLE, caller, | |
| &resources->id_pool); | |
| if (ret < 0) | |
| return ret; | |
| } | |
| single_id = cell->id_shape == ID_SINGLE; | |
| if (single_id && cell->caps) | |
| return -EINVAL; | |
| ret = cell_sandbox_compile(cell->caps, cell->net_rights, no_net, | |
| &cell->sandbox); | |
| if (ret < 0) | |
| return ret; | |
| if (!single_id) { | |
| uid = cell->id_lease.first_id; | |
| gid = cell->id_lease.first_id; | |
| } | |
| if (cell->root.fd >= 0) { | |
| ret = -EBUSY; | |
| if (cell->runfd < 0) | |
| goto destroy_sandbox; | |
| ret = runfd_seal_root(cell->runfd, &cell->root); | |
| if (ret < 0) | |
| goto destroy_sandbox; | |
| mount_tree_destroy(&cell->root); | |
| cell->root_installed = true; | |
| } | |
| ret = budget_delegate(&cell->budget, uid, gid); | |
| if (ret < 0) | |
| goto destroy_sandbox; | |
| ret = bpf_lsm_upsert(&resources->bpf_lsm, cell->userns_inum, cell->id, | |
| cell->net_rights, no_net, single_id); | |
| if (ret < 0) | |
| goto destroy_sandbox; | |
| cell->has_sandbox = true; | |
| cell->state = CELL_SEALED; | |
| return 0; | |
| destroy_sandbox: | |
| cell_sandbox_destroy(&cell->sandbox); | |
| return ret; | |
| } | |
| void cell_snapshot(const struct cell *cell, uint64_t mask, | |
| struct cell_info *out) | |
| { | |
| *out = (struct cell_info){.supported_mask = CELL_INFO_ALL}; | |
| if (mask & CELL_INFO_ID) { | |
| out->id = cell->id; | |
| out->mask |= CELL_INFO_ID; | |
| } | |
| if (mask & CELL_INFO_STATE) { | |
| out->state = cell->state; | |
| out->mask |= CELL_INFO_STATE; | |
| } | |
| if ((mask & CELL_INFO_EXIT) && cell->state == CELL_DEAD) { | |
| out->exit_status = cell->exit_status; | |
| out->mask |= CELL_INFO_EXIT; | |
| } | |
| if ((mask & CELL_INFO_BUDGET) && cell->budget_cgroup_id) { | |
| out->budget_cgroupid = cell->budget_cgroup_id; | |
| out->mask |= CELL_INFO_BUDGET; | |
| } | |
| if ((mask & CELL_INFO_IDS) && cell->id_shape != ID_UNSET) { | |
| out->ids_count = cell->id_shape == ID_BLOCK ? ID_BLOCK_SIZE : 1; | |
| out->mask |= CELL_INFO_IDS; | |
| } | |
| if (mask & CELL_INFO_CAPS) { | |
| out->caps = cell->caps; | |
| out->mask |= CELL_INFO_CAPS; | |
| } | |
| if (mask & CELL_INFO_NET_RIGHTS) { | |
| out->net_rights = cell->net_rights; | |
| out->mask |= CELL_INFO_NET_RIGHTS; | |
| } | |
| if ((mask & CELL_INFO_IDS_FIRST) && cell->id_shape == ID_BLOCK && | |
| cell->state != CELL_DEAD) { | |
| out->ids_first = cell->id_lease.first_id; | |
| out->mask |= CELL_INFO_IDS_FIRST; | |
| } | |
| } | |
| int cell_check_spawnable(const struct cell *cell) | |
| { | |
| switch (cell->state) { | |
| case CELL_SEALED: | |
| case CELL_RUNNING: | |
| return 0; | |
| case CELL_DYING: | |
| case CELL_DEAD: | |
| return -ESRCH; | |
| case CELL_BUILDING: | |
| return -EINVAL; | |
| } | |
| return -EINVAL; | |
| } | |
| static int validate_cred(const struct cell *cell, const struct cred_spec *cred) | |
| { | |
| if (cell->id_shape != ID_BLOCK) { | |
| if (cred->uid != cell->creator.uid || | |
| cred->gid != cell->creator.gid) | |
| return -EINVAL; | |
| return cred->nr_groups ? -EPERM : 0; | |
| } | |
| if (cred->uid >= ID_BLOCK_SIZE || cred->gid >= ID_BLOCK_SIZE) | |
| return -EINVAL; | |
| for (size_t i = 0; i < cred->nr_groups; i++) { | |
| if (cred->groups[i] >= ID_BLOCK_SIZE) | |
| return -EINVAL; | |
| } | |
| return 0; | |
| } | |
| static void finish_teardown(struct cell *cell, int status, | |
| struct cell_resources *resources) | |
| { | |
| bool clean_budget = true; | |
| if (cell->root.fd >= 0) | |
| mount_tree_destroy(&cell->root); | |
| if (cell->has_sandbox) | |
| cell_sandbox_destroy(&cell->sandbox); | |
| if (cell->budget.fd >= 0) | |
| clean_budget = | |
| budget_teardown(&cell->budget, &resources->budgets); | |
| /* Close the runfd before releasing the ID block. */ | |
| close_fd(take_fd(&cell->runfd)); | |
| bpf_lsm_remove(&resources->bpf_lsm, cell->userns_inum); | |
| if (cell->id_shape == ID_BLOCK) | |
| id_block_release(&resources->id_pool, &cell->id_lease, | |
| !clean_budget); | |
| cell->exit_status = status; | |
| cell->state = CELL_DEAD; | |
| } | |
| int cell_start_embryo(struct cell *cell, const struct embryo *embryo, | |
| struct cell_resources *resources) | |
| { | |
| char *const default_argv[] = {embryo->exec, NULL}; | |
| struct spawn_plan plan; | |
| int cgroup_fd, status, ret; | |
| ret = cell_check_spawnable(cell); | |
| if (ret < 0) | |
| return ret; | |
| if (!embryo->exec) | |
| return -EINVAL; | |
| if (embryo->has_cred) { | |
| ret = validate_cred(cell, &embryo->cred); | |
| if (ret < 0) | |
| return ret; | |
| } | |
| if (cell->budget.fd < 0) | |
| return -ESRCH; | |
| cgroup_fd = cell->budget.fd; | |
| if (embryo->cgroup_fd >= 0) { | |
| ret = budget_validate_spawn_cgroup(&cell->budget, | |
| embryo->cgroup_fd); | |
| if (ret < 0) | |
| return ret; | |
| cgroup_fd = embryo->cgroup_fd; | |
| } | |
| if (!cell->has_sandbox) | |
| return -EINVAL; | |
| if (cell->runfd < 0) | |
| return -ESRCH; | |
| plan = (struct spawn_plan){ | |
| .exec_path = embryo->exec, | |
| .argv = embryo->argv.items ? embryo->argv.items : default_argv, | |
| .envp = embryo->envp.items, | |
| .cred = embryo->has_cred ? &embryo->cred : NULL, | |
| .allow_setgroups = cell->id_shape == ID_BLOCK, | |
| .is_init = !cell->has_init, | |
| .hostname = cell->has_hostname ? cell->hostname : "(none)", | |
| .staged_fds = embryo->staged_fds, | |
| .nr_staged_fds = embryo->nr_staged_fds, | |
| .cgroup_fd = cgroup_fd, | |
| .sandbox = &cell->sandbox, | |
| }; | |
| ret = runfd_spawn(cell->runfd, &plan); | |
| if (ret >= 0) { | |
| cell->has_init = true; | |
| cell->state = CELL_RUNNING; | |
| } else if (plan.is_init && runfd_poll_exit(cell->runfd, &status) > 0) { | |
| finish_teardown(cell, status, resources); | |
| } | |
| return ret; | |
| } | |
| void cell_kill(struct cell *cell, struct cell_resources *resources) | |
| { | |
| int status; | |
| switch (cell->state) { | |
| case CELL_BUILDING: | |
| case CELL_SEALED: | |
| finish_teardown(cell, SIGKILL, resources); | |
| break; | |
| case CELL_RUNNING: | |
| case CELL_DYING: | |
| cell->state = CELL_DYING; | |
| if (cell->budget.fd >= 0) | |
| budget_kill_all(&cell->budget); | |
| if (cell->runfd >= 0) { | |
| runfd_send_signal(cell->runfd, SIGKILL); | |
| if (runfd_poll_exit(cell->runfd, &status) > 0) | |
| finish_teardown(cell, status, resources); | |
| } | |
| break; | |
| case CELL_DEAD: | |
| break; | |
| } | |
| } | |
| void cell_on_run_exit(struct cell *cell, struct cell_resources *resources) | |
| { | |
| int status; | |
| if (cell->state == CELL_DEAD) | |
| return; | |
| if (cell->runfd < 0 || runfd_poll_exit(cell->runfd, &status) <= 0) | |
| status = SIGKILL; | |
| finish_teardown(cell, status, resources); | |
| } | |
| static int embryo_stage_fd(struct embryo *embryo, int target, int *aux_fd) | |
| { | |
| struct staged_fd *fds = embryo->staged_fds; | |
| size_t count = embryo->nr_staged_fds, pos = 0; | |
| while (pos < count && fds[pos].target < target) | |
| pos++; | |
| if (pos < count && fds[pos].target == target) { | |
| close_fd(fds[pos].source_fd); | |
| fds[pos].source_fd = take_fd(aux_fd); | |
| return 0; | |
| } | |
| if (count == EMBRYO_MAX_FDS) | |
| return -EMFILE; | |
| if (count == embryo->staged_capacity) { | |
| size_t cap = count ? count * 2 : 8; | |
| if (cap > EMBRYO_MAX_FDS) | |
| cap = EMBRYO_MAX_FDS; | |
| fds = realloc(fds, cap * sizeof(*fds)); | |
| if (!fds) | |
| return -ENOMEM; | |
| embryo->staged_fds = fds; | |
| embryo->staged_capacity = cap; | |
| } | |
| memmove(fds + pos + 1, fds + pos, (count - pos) * sizeof(*fds)); | |
| fds[pos] = (struct staged_fd){target, take_fd(aux_fd)}; | |
| embryo->nr_staged_fds++; | |
| return 0; | |
| } | |
| int embryo_configure(struct embryo *embryo, struct embryo_action *action, | |
| int *aux_fd) | |
| { | |
| struct string_block *block; | |
| switch (action->type) { | |
| case EMBRYO_EXEC: | |
| free(embryo->exec); | |
| embryo->exec = action->exec; | |
| action->exec = NULL; | |
| return 0; | |
| case EMBRYO_ARGV: | |
| case EMBRYO_ENVP: | |
| block = action->type == EMBRYO_ARGV ? &embryo->argv | |
| : &embryo->envp; | |
| string_block_destroy(block); | |
| *block = action->strings; | |
| action->strings = (struct string_block){}; | |
| return 0; | |
| case EMBRYO_CRED: | |
| free(embryo->cred.groups); | |
| embryo->cred = action->cred; | |
| embryo->has_cred = true; | |
| action->cred = (struct cred_spec){}; | |
| return 0; | |
| case EMBRYO_PLACE_FD: | |
| return embryo_stage_fd(embryo, action->target_fd, aux_fd); | |
| case EMBRYO_CGROUP_FD: | |
| if (*aux_fd < 0) | |
| return -EBADF; | |
| close_fd(embryo->cgroup_fd); | |
| embryo->cgroup_fd = take_fd(aux_fd); | |
| return 0; | |
| } | |
| return -EINVAL; | |
| } | |
| void embryo_destroy(struct embryo *embryo) | |
| { | |
| for (size_t i = 0; i < embryo->nr_staged_fds; i++) | |
| close_fd(embryo->staged_fds[i].source_fd); | |
| free(embryo->staged_fds); | |
| close_fd(embryo->cgroup_fd); | |
| free(embryo->cred.groups); | |
| string_block_destroy(&embryo->envp); | |
| string_block_destroy(&embryo->argv); | |
| free(embryo->exec); | |
| } |