blob: d51a824930d27a4cbcb0af7080def94c28b685b8 [file]
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Jarkko Sakkinen
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "base.h"
#include "sandbox.h"
#include "spawn.h"
#include "uapi.h"
static void report_child_error(int fd, int err)
{
int code = -err;
while (write(fd, &code, sizeof(code)) < 0 && errno == EINTR)
;
}
static int reset_signals_and_session(void)
{
struct sigaction sa = {.sa_handler = SIG_DFL};
sigset_t empty;
int sig;
for (sig = 1; sig < 64; sig++) {
if (sig != SIGKILL && sig != SIGSTOP)
sigaction(sig, &sa, NULL);
}
sigemptyset(&empty);
sigprocmask(SIG_SETMASK, &empty, NULL);
if (setsid() < 0 && errno != EPERM)
return -errno;
return 0;
}
static int apply_credentials(const struct cred_spec *cred, bool allow_groups)
{
if (cred->nr_groups) {
if (!allow_groups)
return -EPERM;
static_assert(sizeof(gid_t) == sizeof(uint32_t));
if (setgroups(cred->nr_groups, (const gid_t *)cred->groups) < 0)
return -errno;
} else if (allow_groups) {
setgroups(0, NULL);
}
if (setresgid(cred->gid, cred->gid, cred->gid) < 0)
return -errno;
if (setresuid(cred->uid, cred->uid, cred->uid) < 0)
return -errno;
return 0;
}
static int close_all_except(const int *kept, size_t count, int err_fd)
{
unsigned int cursor = 0;
size_t i;
/* The error pipe is above every staged target. */
for (i = 0; i < count; i++) {
unsigned int fd = kept[i];
if (fd < cursor)
continue;
if (fd > cursor &&
syscall(__NR_close_range, cursor, fd - 1, 0U) < 0)
return -errno;
cursor = fd + 1;
}
if ((unsigned int)err_fd > cursor &&
syscall(__NR_close_range, cursor, (unsigned int)err_fd - 1, 0U) < 0)
return -errno;
if (syscall(__NR_close_range, (unsigned int)err_fd + 1, UINT_MAX, 0U) <
0)
return -errno;
return 0;
}
static int child_steps(const struct child_bootstrap *boot, int err_fd)
{
const struct spawn_plan *plan = boot->plan;
const char *hostname = plan->hostname ? plan->hostname : "";
char *const empty_env[] = {NULL};
uint32_t target_uid;
size_t i;
int ret;
ret = reset_signals_and_session();
if (ret)
return ret;
if (plan->is_init && sethostname(hostname, strlen(hostname)) < 0 &&
errno != EPERM)
return -errno;
landlock_restrict(boot->landlock_fd);
for (i = 0; i < plan->nr_staged_fds; i++) {
ret = fcntl(plan->staged_fds[i].source_fd, F_DUPFD_CLOEXEC,
err_fd + 1);
if (ret < 0)
return -errno;
boot->temp_slots[i] = ret;
}
for (i = 0; i < plan->nr_staged_fds; i++) {
int dst = plan->staged_fds[i].target;
if (dup2(boot->temp_slots[i], dst) < 0)
return -errno;
}
ret = close_all_except(boot->sorted_kept, plan->nr_staged_fds, err_fd);
if (ret)
return ret;
if (chdir("/") < 0)
return -errno;
if (plan->cred) {
ret = apply_credentials(plan->cred, plan->allow_setgroups);
if (ret)
return ret;
target_uid = plan->cred->uid;
} else {
target_uid = geteuid();
}
ret = cell_sandbox_apply(plan->sandbox, target_uid);
if (ret)
return ret;
execve(plan->exec_path, plan->argv,
plan->envp ? plan->envp : empty_env);
return -errno;
}
static int compare_fds(const void *a, const void *b)
{
int left = *(const int *)a;
int right = *(const int *)b;
return (left > right) - (left < right);
}
int child_bootstrap_init(struct child_bootstrap *boot,
const struct spawn_plan *plan)
{
size_t count = plan->nr_staged_fds;
int max_target = 3;
int *slots = NULL;
size_t i;
if (count) {
slots = calloc(count, sizeof(int[2]));
if (!slots)
return err_ctx(-ENOMEM, "allocate bootstrap fd slots");
for (i = 0; i < count; i++) {
slots[i] = plan->staged_fds[i].target;
if (slots[i] > max_target)
max_target = slots[i];
}
qsort(slots, count, sizeof(*slots), compare_fds);
}
*boot = (struct child_bootstrap){
.plan = plan,
.sorted_kept = slots,
.temp_slots = slots ? slots + count : NULL,
.safe_fd_base =
max_target > INT_MAX - 16 ? INT_MAX : max_target + 16,
.landlock_fd = landlock_prepare(),
};
return 0;
}
void child_bootstrap_destroy(struct child_bootstrap *boot)
{
close_fd(take_fd(&boot->landlock_fd));
free(boot->sorted_kept);
}
/* The child must stay allocation-free and never return to its caller. */
[[noreturn]] void child_bootstrap_run(const struct child_bootstrap *boot,
int initial_err_fd)
{
int err_fd, ret;
err_fd = fcntl(initial_err_fd, F_DUPFD_CLOEXEC, boot->safe_fd_base);
if (err_fd < 0) {
report_child_error(initial_err_fd, -errno);
_exit(126);
}
close_fd(initial_err_fd);
ret = child_steps(boot, err_fd);
if (ret)
report_child_error(err_fd, ret);
close_fd(err_fd);
_exit(127);
}
int await_child_exec(int err_read, int pidfd)
{
int child_errno;
ssize_t n;
do {
n = read(err_read, &child_errno, sizeof(child_errno));
} while (n < 0 && errno == EINTR);
if (!n)
return 0;
if (n == sizeof(child_errno)) {
syscall(__NR_pidfd_send_signal, pidfd, SIGKILL, NULL, 0U);
return err_ctx(-child_errno, "cell task bootstrap failed");
}
if (n > 0)
return err_ctx(-EIO, "partial bootstrap status from child");
return err_ctx(-errno, "read bootstrap pipe");
}
int spawn_wait_status(const siginfo_t *info)
{
int status = info->si_status;
switch (info->si_code) {
case CLD_EXITED:
return (status & 0xff) << 8;
case CLD_DUMPED:
return (status & 0x7f) | 0x80;
default:
return status & 0x7f;
}
}