| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <dirent.h> | |
| #include <fcntl.h> | |
| #include <limits.h> | |
| #include <linux/magic.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/stat.h> | |
| #include <sys/statfs.h> | |
| #include "base.h" | |
| #include "budget.h" | |
| #include "uapi.h" | |
| struct budget_path { | |
| struct budget_path *next; | |
| char name[]; | |
| }; | |
| static const struct { | |
| const char *name; | |
| const char *error; | |
| } delegated_files[] = { | |
| {"cgroup.procs", "delegate budget cgroup.procs"}, | |
| {"cgroup.threads", "delegate budget cgroup.threads"}, | |
| {"cgroup.subtree_control", "delegate budget cgroup.subtree_control"}, | |
| }; | |
| static bool path_starts_with(const char *path, const char *base) | |
| { | |
| size_t len = strlen(base); | |
| if (len == 1 && base[0] == '/') | |
| return path[0] == '/'; | |
| return !strncmp(path, base, len) && (path[len] == '/' || !path[len]); | |
| } | |
| static int fd_canonical_path(int fd, char path[PATH_MAX]) | |
| { | |
| static const char deleted[] = " (deleted)"; | |
| char link[sizeof("/proc/self/fd/") + 12]; | |
| ssize_t len; | |
| snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); | |
| len = readlink(link, path, PATH_MAX); | |
| if (len < 0) | |
| return err_ctx(-errno, "read budget fd link"); | |
| if (len == PATH_MAX) | |
| return err_ctx(-ENAMETOOLONG, "read budget fd link"); | |
| path[len] = '\0'; | |
| if ((size_t)len >= sizeof(deleted) - 1 && | |
| !strcmp(path + len - (sizeof(deleted) - 1), deleted)) | |
| return err_ctx(-ENODEV, "cgroup directory deleted"); | |
| return 0; | |
| } | |
| static DIR *open_dir_at(int parent_fd, const char *name) | |
| { | |
| int fd; | |
| DIR *dir; | |
| fd = openat(parent_fd, name, | |
| O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW); | |
| if (fd < 0) | |
| return NULL; | |
| dir = fdopendir(fd); | |
| if (!dir) | |
| close_fd(fd); | |
| return dir; | |
| } | |
| static int is_subdir(DIR *dir, const struct dirent *entry) | |
| { | |
| struct stat st; | |
| if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) | |
| return 0; | |
| if (entry->d_type != DT_UNKNOWN) | |
| return entry->d_type == DT_DIR; | |
| if (fstatat(dirfd(dir), entry->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) | |
| return -errno; | |
| return S_ISDIR(st.st_mode); | |
| } | |
| static int ensure_empty_cgroup(int fd, const struct caller_cred *caller) | |
| { | |
| struct dirent *entry; | |
| bool blank = true; | |
| int procs_fd, ret; | |
| struct stat st; | |
| char buf[4096]; | |
| ssize_t n; | |
| DIR *dir; | |
| procs_fd = openat(fd, "cgroup.procs", O_RDONLY | O_CLOEXEC); | |
| if (procs_fd < 0) | |
| return err_ctx(-errno, "open budget cgroup.procs"); | |
| for (;;) { | |
| n = read(procs_fd, buf, sizeof(buf)); | |
| if (n < 0) { | |
| if (errno == EINTR) | |
| continue; | |
| ret = err_ctx(-errno, "read budget cgroup.procs"); | |
| goto close_procs; | |
| } | |
| if (!n) | |
| break; | |
| for (ssize_t i = 0; blank && i < n; i++) | |
| blank = buf[i] && strchr(" \t\n\r\v\f", buf[i]); | |
| } | |
| if (!blank) { | |
| ret = err_ctx(-EBUSY, "budget cgroup already contains tasks"); | |
| goto close_procs; | |
| } | |
| if (caller->uid) { | |
| if (fstat(procs_fd, &st) < 0) { | |
| ret = err_ctx(-errno, "stat budget cgroup.procs"); | |
| goto close_procs; | |
| } | |
| if (st.st_uid != caller->uid) { | |
| ret = err_ctx(-EPERM, | |
| "cannot write budget cgroup.procs"); | |
| goto close_procs; | |
| } | |
| } | |
| close_fd(procs_fd); | |
| dir = open_dir_at(fd, "."); | |
| if (!dir) | |
| return err_ctx(-errno, "read budget cgroup directory"); | |
| for (;;) { | |
| errno = 0; | |
| entry = readdir(dir); | |
| if (!entry) { | |
| ret = -errno; | |
| break; | |
| } | |
| ret = is_subdir(dir, entry); | |
| if (ret) | |
| break; | |
| } | |
| closedir(dir); | |
| if (ret > 0) | |
| return err_ctx(-EBUSY, | |
| "budget cgroup already has child cgroups"); | |
| if (ret < 0) | |
| return err_ctx(ret, "read budget cgroup directory"); | |
| return 0; | |
| close_procs: | |
| close_fd(procs_fd); | |
| return ret; | |
| } | |
| int budget_claim(int fd, const struct caller_cred *caller, | |
| struct budget_registry *registry, struct budget *out) | |
| { | |
| struct budget_path *entry; | |
| char path[PATH_MAX]; | |
| struct statfs stfs; | |
| struct stat st; | |
| size_t len; | |
| int ret; | |
| if (fstatfs(fd, &stfs) < 0) | |
| return err_ctx(-errno, "fstatfs budget fd"); | |
| if (stfs.f_type != CGROUP2_SUPER_MAGIC) | |
| return err_ctx(-EINVAL, "budget fd is not on cgroup2fs"); | |
| if (fstat(fd, &st) < 0) | |
| return err_ctx(-errno, "fstat budget fd"); | |
| if (!S_ISDIR(st.st_mode)) | |
| return err_ctx(-ENOTDIR, "budget fd is not a directory"); | |
| if (caller->uid && st.st_uid != caller->uid) | |
| return err_ctx(-EPERM, | |
| "caller does not own budget cgroup directory"); | |
| ret = fd_canonical_path(fd, path); | |
| if (ret) | |
| return ret; | |
| ret = ensure_empty_cgroup(fd, caller); | |
| if (ret) | |
| return ret; | |
| for (entry = registry->paths; entry; entry = entry->next) { | |
| if (path_starts_with(path, entry->name) || | |
| path_starts_with(entry->name, path)) | |
| return err_ctx(-EBUSY, "overlapping cgroup budgets"); | |
| } | |
| len = strlen(path) + 1; | |
| entry = malloc(sizeof(*entry) + len); | |
| if (!entry) | |
| return err_ctx(-ENOMEM, "claim budget cgroup path"); | |
| memcpy(entry->name, path, len); | |
| entry->next = registry->paths; | |
| registry->paths = entry; | |
| *out = (struct budget){ | |
| .fd = fd, | |
| .cgroup_id = st.st_ino, | |
| .path = entry, | |
| .orig_uid = st.st_uid, | |
| .orig_gid = st.st_gid, | |
| }; | |
| return 0; | |
| } | |
| static bool restore_owner(const struct budget *budget) | |
| { | |
| uid_t uid = budget->orig_uid; | |
| gid_t gid = budget->orig_gid; | |
| int fd = budget->fd; | |
| bool clean; | |
| size_t i; | |
| clean = fchownat(fd, "", uid, gid, AT_EMPTY_PATH) == 0; | |
| for (i = 0; i < ARRAY_SIZE(delegated_files); i++) { | |
| if (fchownat(fd, delegated_files[i].name, uid, gid, 0) < 0) | |
| clean = false; | |
| } | |
| return clean; | |
| } | |
| int budget_delegate(struct budget *budget, uint32_t uid, uint32_t gid) | |
| { | |
| int fd = budget->fd; | |
| size_t i; | |
| int ret; | |
| if (uid == budget->orig_uid && gid == budget->orig_gid) | |
| goto delegated; | |
| if (fchownat(fd, "", uid, gid, AT_EMPTY_PATH) < 0) | |
| return err_ctx(-errno, "delegate budget directory owner"); | |
| for (i = 0; i < ARRAY_SIZE(delegated_files); i++) { | |
| if (fchownat(fd, delegated_files[i].name, uid, gid, 0) < 0) { | |
| ret = err_ctx(-errno, delegated_files[i].error); | |
| restore_owner(budget); | |
| return ret; | |
| } | |
| } | |
| delegated: | |
| budget->delegated = true; | |
| return 0; | |
| } | |
| int budget_validate_spawn_cgroup(const struct budget *budget, int fd) | |
| { | |
| char path[PATH_MAX]; | |
| struct statfs stfs; | |
| struct stat st; | |
| int ret; | |
| if (fstatfs(fd, &stfs) < 0) | |
| return err_ctx(-errno, "fstatfs spawn cgroup"); | |
| if (stfs.f_type != CGROUP2_SUPER_MAGIC) | |
| return err_ctx(-EINVAL, "spawn cgroup fd is not on cgroup2fs"); | |
| if (fstat(fd, &st) < 0) | |
| return err_ctx(-errno, "fstat spawn cgroup"); | |
| if (!st.st_nlink) | |
| return err_ctx(-ENODEV, "spawn cgroup was removed"); | |
| ret = fd_canonical_path(fd, path); | |
| if (ret) | |
| return ret; | |
| if (!path_starts_with(path, budget->path->name)) | |
| return err_ctx(-EPERM, "spawn cgroup lies outside cell budget"); | |
| return 0; | |
| } | |
| static void kill_cgroup(int fd) | |
| { | |
| int kill_fd = openat(fd, "cgroup.kill", O_WRONLY | O_CLOEXEC); | |
| if (kill_fd >= 0) { | |
| (void)write(kill_fd, "1", 1); | |
| close_fd(kill_fd); | |
| } | |
| } | |
| void budget_kill_all(const struct budget *budget) | |
| { | |
| kill_cgroup(budget->fd); | |
| } | |
| struct cgroup_walk { | |
| DIR *dir; | |
| struct cgroup_walk *parent; | |
| char name[]; | |
| }; | |
| static bool remove_subcgroups_bottom_up(int fd) | |
| { | |
| struct cgroup_walk *frame, *child, root = {0}; | |
| struct dirent *entry; | |
| bool clean = true; | |
| size_t len; | |
| int ret; | |
| root.dir = open_dir_at(fd, "."); | |
| if (!root.dir) | |
| return false; | |
| frame = &root; | |
| for (;;) { | |
| errno = 0; | |
| entry = readdir(frame->dir); | |
| if (!entry) { | |
| if (errno) | |
| clean = false; | |
| closedir(frame->dir); | |
| if (frame == &root) | |
| return clean; | |
| child = frame; | |
| frame = frame->parent; | |
| if (unlinkat(dirfd(frame->dir), child->name, | |
| AT_REMOVEDIR) < 0) | |
| clean = false; | |
| free(child); | |
| continue; | |
| } | |
| ret = is_subdir(frame->dir, entry); | |
| if (ret < 0) | |
| clean = false; | |
| if (ret <= 0) | |
| continue; | |
| len = strlen(entry->d_name) + 1; | |
| child = malloc(sizeof(*child) + len); | |
| if (!child) { | |
| clean = false; | |
| continue; | |
| } | |
| child->dir = open_dir_at(dirfd(frame->dir), entry->d_name); | |
| if (!child->dir) { | |
| clean = false; | |
| unlinkat(dirfd(frame->dir), entry->d_name, | |
| AT_REMOVEDIR); | |
| free(child); | |
| continue; | |
| } | |
| kill_cgroup(dirfd(child->dir)); | |
| memcpy(child->name, entry->d_name, len); | |
| child->parent = frame; | |
| frame = child; | |
| } | |
| } | |
| bool budget_teardown(struct budget *budget, struct budget_registry *registry) | |
| { | |
| struct budget_path **entry; | |
| bool clean; | |
| if (!budget->path) | |
| return true; | |
| budget_kill_all(budget); | |
| clean = remove_subcgroups_bottom_up(budget->fd); | |
| if (budget->delegated && | |
| (budget->orig_uid || budget->orig_gid || clean)) { | |
| if (!restore_owner(budget)) | |
| clean = false; | |
| } | |
| for (entry = ®istry->paths; *entry; entry = &(*entry)->next) { | |
| if (*entry == budget->path) { | |
| *entry = budget->path->next; | |
| break; | |
| } | |
| } | |
| free(budget->path); | |
| close_fd(budget->fd); | |
| *budget = (struct budget){.fd = -1}; | |
| return clean; | |
| } |