| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/stat.h> | |
| #include "base.h" | |
| #include "uapi.h" | |
| thread_local const char *ucells_error; | |
| static_assert(__NR_pidfd_send_signal == 424); | |
| static_assert(__NR_open_tree == 428); | |
| static_assert(__NR_move_mount == 429); | |
| static_assert(__NR_fsopen == 430); | |
| static_assert(__NR_fsconfig == 431); | |
| static_assert(__NR_fsmount == 432); | |
| static_assert(__NR_pidfd_open == 434); | |
| static_assert(__NR_clone3 == 435); | |
| static_assert(__NR_pidfd_getfd == 438); | |
| static_assert(__NR_mount_setattr == 442); | |
| int file_identity_of_fd(int fd, struct file_identity *id) | |
| { | |
| struct stat st; | |
| if (fstat(fd, &st) < 0) | |
| return -errno; | |
| *id = (struct file_identity){.dev = st.st_dev, .ino = st.st_ino}; | |
| return 0; | |
| } | |
| bool file_identity_equal(const struct file_identity *a, | |
| const struct file_identity *b) | |
| { | |
| return a->dev == b->dev && a->ino == b->ino; | |
| } | |
| static bool id_in_window(uint32_t id) | |
| { | |
| return id >= ID_WINDOW_FIRST && id <= ID_WINDOW_LAST; | |
| } | |
| int id_shape_validate_caller(enum id_shape shape, uint32_t uid, uint32_t gid) | |
| { | |
| if (shape != ID_SINGLE) | |
| return 0; | |
| if (!uid || !gid) | |
| return -EPERM; | |
| if (id_in_window(uid) || id_in_window(gid)) | |
| return -EINVAL; | |
| return 0; | |
| } | |
| bool valid_utf8(const char *data, size_t len) | |
| { | |
| const unsigned char *bytes = (const unsigned char *)data; | |
| size_t i = 0; | |
| while (i < len) { | |
| unsigned char b = bytes[i]; | |
| unsigned char lo = 0x80; | |
| unsigned char hi = 0xbf; | |
| size_t width; | |
| if (b < 0x80) { | |
| i++; | |
| continue; | |
| } | |
| if (b >= 0xc2 && b <= 0xdf) { | |
| width = 2; | |
| } else if (b >= 0xe0 && b <= 0xef) { | |
| width = 3; | |
| if (b == 0xe0) | |
| lo = 0xa0; | |
| else if (b == 0xed) | |
| hi = 0x9f; | |
| } else if (b >= 0xf0 && b <= 0xf4) { | |
| width = 4; | |
| if (b == 0xf0) | |
| lo = 0x90; | |
| else if (b == 0xf4) | |
| hi = 0x8f; | |
| } else { | |
| return false; | |
| } | |
| if (len - i < width || bytes[i + 1] < lo || bytes[i + 1] > hi) | |
| return false; | |
| for (size_t j = 2; j < width; j++) { | |
| if ((bytes[i + j] & 0xc0) != 0x80) | |
| return false; | |
| } | |
| i += width; | |
| } | |
| return true; | |
| } | |
| int string_block_parse(char *data, size_t len, struct string_block *out) | |
| { | |
| char *end, *p, **items; | |
| size_t count = 0; | |
| if (!len || len > EMBRYO_MAX_BLOCK) | |
| return -E2BIG; | |
| if (data[len - 1]) | |
| return -EINVAL; | |
| end = data + len; | |
| for (p = data; p < end;) { | |
| size_t part = strlen(p); | |
| if (++count > EMBRYO_MAX_STRINGS) | |
| return -E2BIG; | |
| if (!valid_utf8(p, part)) | |
| return -EINVAL; | |
| p += part + 1; | |
| } | |
| items = malloc((count + 1) * sizeof(*items)); | |
| if (!items) | |
| return -ENOMEM; | |
| p = data; | |
| for (size_t i = 0; i < count; i++) { | |
| items[i] = p; | |
| p += strlen(p) + 1; | |
| } | |
| items[count] = NULL; | |
| *out = (struct string_block){.data = data, .items = items}; | |
| return 0; | |
| } | |
| void string_block_destroy(struct string_block *block) | |
| { | |
| free(block->items); | |
| free(block->data); | |
| *block = (struct string_block){}; | |
| } | |
| static int compare_groups(const void *p, const void *q) | |
| { | |
| uint32_t a = *(const uint32_t *)p; | |
| uint32_t b = *(const uint32_t *)q; | |
| return (a > b) - (a < b); | |
| } | |
| void cred_spec_sort_groups(struct cred_spec *cred) | |
| { | |
| size_t kept = 1; | |
| if (cred->nr_groups < 2) | |
| return; | |
| qsort(cred->groups, cred->nr_groups, sizeof(*cred->groups), | |
| compare_groups); | |
| for (size_t i = 1; i < cred->nr_groups; i++) { | |
| if (cred->groups[i] != cred->groups[kept - 1]) | |
| cred->groups[kept++] = cred->groups[i]; | |
| } | |
| cred->nr_groups = kept; | |
| } | |
| static bool valid_key(const char *key, size_t max) | |
| { | |
| return *key && strnlen(key, max) < max; | |
| } | |
| int embryo_action_string(const char *key, char *value, | |
| struct embryo_action *out) | |
| { | |
| if (strcmp(key, "exec") != 0 || !*value) | |
| return -EINVAL; | |
| if (strnlen(value, PATH_MAX) == PATH_MAX) | |
| return -ENAMETOOLONG; | |
| *out = (struct embryo_action){.type = EMBRYO_EXEC, .exec = value}; | |
| return 0; | |
| } | |
| static int parse_target_fd(const char *key) | |
| { | |
| int fd = 0; | |
| if (!*key || (*key == '0' && key[1])) | |
| return -EINVAL; | |
| for (; *key; key++) { | |
| if (*key < '0' || *key > '9') | |
| return -EINVAL; | |
| fd = fd * 10 + (*key - '0'); | |
| if (fd > EMBRYO_MAX_FD_NUM) | |
| return -EINVAL; | |
| } | |
| return fd; | |
| } | |
| int embryo_action_fd(const char *key, struct embryo_action *out) | |
| { | |
| int fd; | |
| if (!valid_key(key, EMBRYO_KEY_MAX)) | |
| return -EINVAL; | |
| if (!strcmp(key, "cgroup")) { | |
| *out = (struct embryo_action){.type = EMBRYO_CGROUP_FD}; | |
| return 0; | |
| } | |
| fd = parse_target_fd(key); | |
| if (fd < 0) | |
| return fd; | |
| *out = (struct embryo_action){.type = EMBRYO_PLACE_FD, .target_fd = fd}; | |
| return 0; | |
| } | |
| void embryo_action_destroy(struct embryo_action *action) | |
| { | |
| switch (action->type) { | |
| case EMBRYO_EXEC: | |
| free(action->exec); | |
| break; | |
| case EMBRYO_ARGV: | |
| case EMBRYO_ENVP: | |
| string_block_destroy(&action->strings); | |
| break; | |
| case EMBRYO_CRED: | |
| free(action->cred.groups); | |
| break; | |
| case EMBRYO_PLACE_FD: | |
| case EMBRYO_CGROUP_FD: | |
| break; | |
| } | |
| *action = (struct embryo_action){}; | |
| } | |
| static int validate_slot_key(const char *key) | |
| { | |
| if (!valid_key(key, CELL_KEY_MAX)) | |
| return -EINVAL; | |
| if (!strcmp(key, "spawn")) | |
| return -EOPNOTSUPP; | |
| return 0; | |
| } | |
| int slot_action_string(const char *key, const char *val, | |
| struct slot_action *out) | |
| { | |
| int ret = validate_slot_key(key); | |
| if (ret) | |
| return ret; | |
| if (!strcmp(key, "hostname")) { | |
| size_t len = strnlen(val, sizeof(out->hostname)); | |
| if (len > UTS_HOSTNAME_MAX) | |
| return -ENAMETOOLONG; | |
| out->type = SLOT_HOSTNAME; | |
| memcpy(out->hostname, val, len + 1); | |
| return 0; | |
| } | |
| if (!strcmp(key, "net") && !strcmp(val, "new")) { | |
| *out = (struct slot_action){.type = SLOT_NET_NEW}; | |
| return 0; | |
| } | |
| return -EINVAL; | |
| } | |
| static int parse_id_slot(const void *value, size_t len, struct slot_action *out) | |
| { | |
| uint32_t count; | |
| if (len != sizeof(count)) | |
| return -EINVAL; | |
| memcpy(&count, value, sizeof(count)); | |
| if (count != 1 && count != ID_BLOCK_SIZE) | |
| return -EINVAL; | |
| *out = (struct slot_action){ | |
| .type = SLOT_IDS, | |
| .shape = count == 1 ? ID_SINGLE : ID_BLOCK, | |
| }; | |
| return 0; | |
| } | |
| int slot_action_binary(const char *key, const void *value, size_t len, | |
| struct slot_action *out) | |
| { | |
| uint64_t bits; | |
| int ret = validate_slot_key(key); | |
| if (ret) | |
| return ret; | |
| if (!strcmp(key, "ids")) | |
| return parse_id_slot(value, len, out); | |
| if (len != sizeof(bits)) | |
| return -EINVAL; | |
| memcpy(&bits, value, sizeof(bits)); | |
| if (!strcmp(key, "caps")) { | |
| if (bits >> (CELL_CAP_LAST + 1) || bits & (1ULL << CAP_SETFCAP)) | |
| return -EINVAL; | |
| *out = (struct slot_action){.type = SLOT_CAPS, .caps = bits}; | |
| } else if (!strcmp(key, "net-rights")) { | |
| if (bits & ~CELL_NET_ALL) | |
| return -EINVAL; | |
| *out = (struct slot_action){ | |
| .type = SLOT_NET_RIGHTS, | |
| .rights = bits, | |
| }; | |
| } else { | |
| return -EINVAL; | |
| } | |
| return 0; | |
| } | |
| int slot_action_fd(const char *key, struct slot_action *out) | |
| { | |
| int ret = validate_slot_key(key); | |
| if (ret) | |
| return ret; | |
| if (!strcmp(key, "root")) | |
| out->type = SLOT_ROOT_FD; | |
| else if (!strcmp(key, "budget")) | |
| out->type = SLOT_BUDGET_FD; | |
| else if (!strcmp(key, "net")) | |
| out->type = SLOT_NET_FD; | |
| else | |
| return -EINVAL; | |
| return 0; | |
| } | |
| int slot_action_flag(const char *key) | |
| { | |
| int ret = validate_slot_key(key); | |
| return ret ? ret : -EINVAL; | |
| } | |
| int parse_open_slot(const char *key, enum open_slot *out) | |
| { | |
| if (!strcmp(key, "net")) | |
| *out = OPEN_SLOT_NET; | |
| else if (!strcmp(key, "pids")) | |
| *out = OPEN_SLOT_PIDS; | |
| else | |
| return -EINVAL; | |
| return 0; | |
| } |