blob: e2cd5c745e518cf062fe547d0bb5e9477089b909 [file]
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Jarkko Sakkinen
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/mount.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base.h"
#include "handler.h"
#include "memory.h"
#include "notify.h"
#include "supervisor.h"
#include "uapi.h"
#define CELL_STRING_READ_MAX 256
#define TRACEE_WRITE_CHUNK 4096
static int send_cloexec(int fd, struct dispatch_outcome *out)
{
if (fd < 0)
return fd;
*out = (struct dispatch_outcome){
.type = DISPATCH_SEND_FD,
.fd = fd,
.cloexec = true,
};
return 0;
}
static int cell_configure(int listener, struct supervisor *supervisor,
const struct seccomp_notif *notif)
{
const __u64 *args = notif->data.args;
uint32_t cmd = (uint32_t)args[1];
uint64_t key_ptr = args[2], val_ptr = args[3];
int32_t aux = (int32_t)args[4];
uint64_t id = notif->id;
uint32_t pid = notif->pid;
char key[CELL_KEY_MAX];
struct caller_cred caller;
struct slot_action action;
int cfd, slot_fd = -1;
int ret;
if (cmd > CELL_CMD_SEAL)
return -EOPNOTSUPP;
cfd = dup_tracee_fd(listener, id, pid, (int32_t)args[0]);
if (cfd < 0)
return cfd;
ret = caller_cred_from_pid(pid, &caller);
if (ret < 0)
goto close_cell;
if (!notif_is_id_valid(listener, id)) {
ret = -ENOENT;
goto close_cell;
}
if (cmd == CELL_CMD_SEAL) {
ret = key_ptr || val_ptr || aux
? -EINVAL
: supervisor_cell_seal(supervisor, cfd, &caller);
goto close_cell;
}
if (!key_ptr || (cmd == CELL_SET_FLAG && (val_ptr || aux)) ||
(cmd == CELL_SET_STRING && (!val_ptr || aux)) ||
(cmd == CELL_SET_BINARY && (!val_ptr || aux <= 0)) ||
(cmd == CELL_SET_FD && (val_ptr || aux < 0))) {
ret = -EINVAL;
goto close_cell;
}
ret = read_tracee_cstring(listener, id, pid, key_ptr, key, sizeof(key));
if (ret < 0)
goto close_cell;
switch (cmd) {
case CELL_SET_FLAG:
ret = slot_action_flag(key);
goto close_cell;
case CELL_SET_STRING: {
char value[CELL_STRING_READ_MAX];
ret = read_tracee_cstring(listener, id, pid, val_ptr, value,
sizeof(value));
if (!ret)
ret = slot_action_string(key, value, &action);
break;
}
case CELL_SET_BINARY: {
void *bytes;
ret = read_tracee_alloc(listener, id, pid, val_ptr, (size_t)aux,
&bytes);
if (!ret) {
ret = slot_action_binary(key, bytes, (size_t)aux,
&action);
free(bytes);
}
break;
}
case CELL_SET_FD:
slot_fd = dup_tracee_fd(listener, id, pid, aux);
if (slot_fd < 0) {
ret = slot_fd;
goto close_cell;
}
ret = slot_action_fd(key, &action);
break;
default:
ret = -EOPNOTSUPP;
goto close_cell;
}
if (!ret)
ret = supervisor_cell_configure(supervisor, cfd, &action,
&slot_fd, &caller);
close_fd(slot_fd);
close_cell:
close_fd(cfd);
return ret;
}
static int write_cell_info(int listener, uint64_t id, uint32_t pid,
uint64_t addr, size_t size,
const struct cell_info *info)
{
unsigned char chunk[TRACEE_WRITE_CHUNK] = {};
size_t len = size < sizeof(chunk) ? size : sizeof(chunk);
size_t offset = len;
int ret;
if (!addr || size > UINT64_MAX - addr)
return -EFAULT;
memcpy(chunk, info, sizeof(*info));
ret = write_tracee_bytes(listener, id, pid, addr, chunk, len);
if (ret < 0)
return ret;
memset(chunk, 0, sizeof(*info));
while (offset < size) {
len = size - offset;
if (len > sizeof(chunk))
len = sizeof(chunk);
ret = write_tracee_bytes(listener, id, pid, addr + offset,
chunk, len);
if (ret < 0)
return ret;
offset += len;
}
return 0;
}
static int cell_info(int listener, const struct supervisor *supervisor,
const struct seccomp_notif *notif)
{
const __u64 *args = notif->data.args;
int cfd_num = (int32_t)args[0];
uint64_t addr = args[1];
uint64_t id = notif->id;
uint32_t pid = notif->pid;
struct cell_info info = {};
uint64_t mask;
int cfd, ret;
if (args[2] > SIZE_MAX || args[2] < CELL_INFO_SIZE_VER0 ||
(uint32_t)args[3])
return -EINVAL;
ret = read_tracee_bytes(listener, id, pid, addr, &mask, sizeof(mask));
if (ret < 0)
return ret;
if (cfd_num == CELL_SELF) {
supervisor_cell_info_self(supervisor, mask, pid, &info);
} else {
cfd = dup_tracee_fd(listener, id, pid, cfd_num);
if (cfd < 0)
return cfd;
ret = supervisor_cell_info(supervisor, cfd, mask, &info);
close_fd(cfd);
if (ret < 0)
return ret;
}
return write_cell_info(listener, id, pid, addr, (size_t)args[2], &info);
}
static int cell_open(int listener, struct supervisor *supervisor,
const struct seccomp_notif *notif,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
uint64_t id = notif->id;
uint32_t pid = notif->pid;
char key[CELL_KEY_MAX];
enum open_slot slot;
int cfd, ret;
if ((uint32_t)args[1] != CELL_OPEN_SLOT)
return -EOPNOTSUPP;
if (!args[2] || args[3] || (uint32_t)args[4])
return -EINVAL;
cfd = dup_tracee_fd(listener, id, pid, (int32_t)args[0]);
if (cfd < 0)
return cfd;
ret = read_tracee_cstring(listener, id, pid, args[2], key, sizeof(key));
if (ret < 0)
goto close_cell;
ret = parse_open_slot(key, &slot);
if (ret < 0)
goto close_cell;
ret = send_cloexec(supervisor_cell_open(supervisor, cfd, slot), out);
close_cell:
close_fd(cfd);
return ret;
}
static int decode_pidfd_binary(int listener, const struct seccomp_notif *notif,
const char *key, uint64_t addr, size_t len,
struct embryo_action *action)
{
struct pidfd_cred raw;
uint64_t id = notif->id;
uint32_t pid = notif->pid;
void *bytes = NULL;
int ret;
if (!strcmp(key, "argv") || !strcmp(key, "envp")) {
ret = read_tracee_alloc(listener, id, pid, addr, len, &bytes);
if (ret < 0)
return ret;
action->type = !strcmp(key, "argv") ? EMBRYO_ARGV : EMBRYO_ENVP;
ret = string_block_parse(bytes, len, &action->strings);
if (ret < 0)
free(bytes);
return ret;
}
if (strcmp(key, "cred") || len != sizeof(raw))
return -EINVAL;
ret = read_tracee_bytes(listener, id, pid, addr, &raw, sizeof(raw));
if (ret < 0)
return ret;
if (raw.spare || raw.ngroups > CELL_NGROUPS_MAX)
return -EINVAL;
if (raw.ngroups) {
ret = read_tracee_alloc(listener, id, pid, raw.groups,
raw.ngroups * sizeof(uint32_t), &bytes);
if (ret < 0)
return ret;
}
action->type = EMBRYO_CRED;
action->cred = (struct cred_spec){
.uid = raw.uid,
.gid = raw.gid,
.groups = bytes,
.nr_groups = raw.ngroups,
};
cred_spec_sort_groups(&action->cred);
return 0;
}
static int pidfd_configure(int listener, struct supervisor *supervisor,
const struct seccomp_notif *notif,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
int pfd_num = (int32_t)args[0];
uint32_t cmd = (uint32_t)args[1];
uint64_t key_ptr = args[2], val_ptr = args[3];
int32_t aux = (int32_t)args[4];
uint64_t id = notif->id;
uint32_t pid = notif->pid;
char key[EMBRYO_KEY_MAX];
struct embryo_action action = {};
int efd, src_fd = -1;
int ret;
if (cmd > PIDFD_CMD_START)
return -EOPNOTSUPP;
efd = dup_tracee_fd(listener, id, pid, pfd_num);
if (efd < 0)
return efd;
if (cmd == PIDFD_CMD_START) {
if (key_ptr || val_ptr || aux) {
ret = -EINVAL;
goto close_embryo;
}
ret = supervisor_pidfd_start(supervisor, efd);
if (ret >= 0) {
*out = (struct dispatch_outcome){
.type = DISPATCH_REPLACE_FD,
.src = ret,
.target_num = (uint32_t)pfd_num,
};
ret = 0;
}
goto close_embryo;
}
if (!key_ptr || (cmd == PIDFD_SET_STRING && (!val_ptr || aux)) ||
(cmd == PIDFD_SET_BINARY && (!val_ptr || aux <= 0)) ||
(cmd == PIDFD_SET_FD && (val_ptr || aux < 0))) {
ret = -EINVAL;
goto close_embryo;
}
ret = read_tracee_cstring(listener, id, pid, key_ptr, key, sizeof(key));
if (ret < 0)
goto close_embryo;
switch (cmd) {
case PIDFD_SET_STRING: {
char value[PATH_MAX];
char *owned;
ret = read_tracee_cstring(listener, id, pid, val_ptr, value,
sizeof(value));
if (ret < 0)
goto close_embryo;
owned = strdup(value);
if (!owned) {
ret = -ENOMEM;
goto close_embryo;
}
ret = embryo_action_string(key, owned, &action);
if (ret < 0)
free(owned);
break;
}
case PIDFD_SET_BINARY:
ret = decode_pidfd_binary(listener, notif, key, val_ptr,
(size_t)aux, &action);
break;
case PIDFD_SET_FD:
src_fd = dup_tracee_fd(listener, id, pid, aux);
if (src_fd < 0) {
ret = src_fd;
goto close_embryo;
}
ret = embryo_action_fd(key, &action);
break;
default:
ret = -EOPNOTSUPP;
goto close_embryo;
}
if (!ret) {
ret = supervisor_pidfd_configure(supervisor, efd, &action,
&src_fd);
embryo_action_destroy(&action);
}
close_fd(src_fd);
close_embryo:
close_fd(efd);
return ret;
}
static int handle_fsconfig(int listener, const struct supervisor *supervisor,
const struct seccomp_notif *notif,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
int32_t aux = (int32_t)args[4];
uint64_t id = notif->id;
uint32_t pid = notif->pid;
char key[CELL_KEY_MAX];
int fsfd, slot_fd, ret;
if (!args[2] || args[3] || aux < 0)
goto continue_syscall;
ret = read_tracee_cstring(listener, id, pid, args[2], key, sizeof(key));
if (ret < 0 || strcmp(key, "pidns"))
goto continue_syscall;
fsfd = dup_tracee_fd(listener, id, pid, (int32_t)args[0]);
if (fsfd < 0)
return fsfd;
slot_fd = dup_tracee_fd(listener, id, pid, aux);
if (slot_fd < 0) {
close_fd(fsfd);
return slot_fd;
}
ret = supervisor_fsconfig_pidns(supervisor, fsfd, slot_fd);
close_fd(slot_fd);
close_fd(fsfd);
if (ret != -EINVAL)
return ret;
continue_syscall:
out->type = DISPATCH_CONTINUE;
return 0;
}
static int resolve_mount_target(int listener, const struct seccomp_notif *notif,
char path[PATH_MAX])
{
const __u64 *args = notif->data.args;
char proc_path[sizeof("/proc//root") + 10];
int dfd_num = (int32_t)args[0];
const char *entry;
int ret;
path[0] = '\0';
if (args[1]) {
ret = read_tracee_cstring(listener, notif->id, notif->pid,
args[1], path, PATH_MAX);
if (ret < 0)
return ret;
}
if (path[0] == '/')
entry = "root";
else if (dfd_num == AT_FDCWD)
entry = "cwd";
else
return dup_tracee_fd(listener, notif->id, notif->pid, dfd_num);
snprintf(proc_path, sizeof(proc_path), "/proc/%" PRIu32 "/%s",
notif->pid, entry);
ret = open(proc_path, O_RDONLY | O_PATH | O_CLOEXEC);
return ret < 0 ? -errno : ret;
}
static int handle_idmap_mount(int listener, const struct supervisor *supervisor,
const struct seccomp_notif *notif,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
bool open_tree = notif->data.nr == __NR_open_tree_attr;
uint64_t id = notif->id;
uint32_t pid = notif->pid;
struct mount_attr attr;
char path[PATH_MAX];
const char *rel_path;
void *bytes;
int tracee_cfd, userns_fd, dfd, ret;
long page_size, rc;
static_assert(sizeof(attr) == MOUNT_ATTR_SIZE_VER0);
if (args[4] > SIZE_MAX)
return -EINVAL;
if (!args[3] || args[4] < MOUNT_ATTR_SIZE_VER0)
goto continue_syscall;
page_size = sysconf(_SC_PAGESIZE);
if (page_size <= 0)
return -EIO;
if (args[4] > (uint64_t)page_size)
goto continue_syscall;
ret = read_tracee_alloc(listener, id, pid, args[3], (size_t)args[4],
&bytes);
if (ret < 0)
return ret;
memcpy(&attr, bytes, sizeof(attr));
if (!(attr.attr_set & MOUNT_ATTR_IDMAP)) {
out->type = DISPATCH_CONTINUE;
goto free_bytes;
}
if (attr.userns_fd > INT32_MAX) {
ret = -EBADF;
goto free_bytes;
}
tracee_cfd = dup_tracee_fd(listener, id, pid, (int32_t)attr.userns_fd);
if (tracee_cfd < 0) {
ret = tracee_cfd;
goto free_bytes;
}
userns_fd = supervisor_cell_idmap_fd(supervisor, tracee_cfd);
if (userns_fd < 0) {
struct cell_info info;
ret = userns_fd;
if (ret == -EINVAL &&
supervisor_cell_info(supervisor, tracee_cfd, 0, &info) <
0) {
out->type = DISPATCH_CONTINUE;
ret = 0;
}
goto close_cell;
}
attr.userns_fd = (uint64_t)userns_fd;
memcpy(bytes, &attr, sizeof(attr));
dfd = resolve_mount_target(listener, notif, path);
if (dfd < 0) {
ret = dfd;
goto close_userns;
}
rel_path = path[0] == '/' ? path + 1 : path;
rc = syscall(open_tree ? __NR_open_tree_attr : __NR_mount_setattr, dfd,
rel_path, (uint32_t)args[2], bytes, (size_t)args[4]);
if (rc < 0)
ret = -errno;
else if (open_tree)
ret = rc > INT_MAX ? -EINVAL : send_cloexec((int)rc, out);
else
ret = 0;
close_fd(dfd);
close_userns:
close_fd(userns_fd);
close_cell:
close_fd(tracee_cfd);
free_bytes:
free(bytes);
return ret;
continue_syscall:
out->type = DISPATCH_CONTINUE;
return 0;
}
static int dispatch(int listener, struct supervisor *supervisor,
const struct seccomp_notif *notif,
struct dispatch_outcome *out)
{
const __u64 *args = notif->data.args;
uint64_t id = notif->id;
uint32_t pid = notif->pid;
int cfd_num = (int32_t)args[0];
uint32_t flags = (uint32_t)args[1];
int cfd, ret;
switch (notif->data.nr) {
case SYS_CELL_CREATE:
ret = supervisor_cell_create(supervisor, cfd_num, flags, pid);
return send_cloexec(ret, out);
case SYS_CELL_CONFIGURE:
return cell_configure(listener, supervisor, notif);
case SYS_CELL_INFO:
return cell_info(listener, supervisor, notif);
case SYS_CELL_OPEN:
return cell_open(listener, supervisor, notif, out);
case SYS_CELL_KILL:
if (flags)
return -EINVAL;
cfd = dup_tracee_fd(listener, id, pid, cfd_num);
if (cfd < 0)
return cfd;
ret = supervisor_cell_kill(supervisor, cfd);
close_fd(cfd);
return ret;
case __NR_pidfd_open:
if (!(flags & PIDFD_EMPTY)) {
out->type = DISPATCH_CONTINUE;
return 0;
}
if (cfd_num == -EBADF)
return -EOPNOTSUPP;
cfd = dup_tracee_fd(listener, id, pid, cfd_num);
if (cfd < 0)
return cfd;
ret = supervisor_pidfd_open_empty(supervisor, cfd, flags, pid);
close_fd(cfd);
return send_cloexec(ret, out);
case SYS_PIDFD_CONFIGURE:
return pidfd_configure(listener, supervisor, notif, out);
case __NR_fsconfig:
return handle_fsconfig(listener, supervisor, notif, out);
case __NR_mount_setattr:
case __NR_open_tree_attr:
return handle_idmap_mount(listener, supervisor, notif, out);
default:
return supervisor_sysctl_notification(supervisor, listener,
notif, out);
}
}
int handle_notification(int listener, struct supervisor *supervisor,
const struct seccomp_notif *notif)
{
struct dispatch_outcome out = {.type = DISPATCH_VALUE, .value = 0};
uint64_t id = notif->id;
int ret;
if (!notif_is_id_valid(listener, id))
return 0;
ret = dispatch(listener, supervisor, notif, &out);
if (ret < 0)
return notif_send_errno(listener, id, ret);
switch (out.type) {
case DISPATCH_CONTINUE:
return notif_send_continue(listener, id);
case DISPATCH_VALUE:
return notif_send_value(listener, id, out.value);
case DISPATCH_SEND_FD:
ret = notif_add_fd(listener, id, out.fd, 0,
SECCOMP_ADDFD_FLAG_SEND,
out.cloexec ? O_CLOEXEC : 0);
close_fd(out.fd);
return ret < 0 ? notif_send_errno(listener, id, ret) : 0;
case DISPATCH_REPLACE_FD:
ret = notif_add_fd(listener, id, out.src, out.target_num,
SECCOMP_ADDFD_FLAG_SETFD, O_CLOEXEC);
close_fd(out.src);
if (ret < 0)
return notif_send_errno(listener, id, ret);
return notif_send_value(listener, id, 0);
}
return notif_send_errno(listener, id, -EINVAL);
}