| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <linux/mount.h> | |
| #include <string.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/prctl.h> | |
| #include "base.h" | |
| #include "notify.h" | |
| #include "seccomp_filter.h" | |
| #include "uapi.h" | |
| int install_cells_notif_filter(void) | |
| { | |
| static struct sock_filter filter[] = { | |
| SC_LOAD(arch), | |
| SC_EQ(SC_AUDIT_ARCH, 1, 0), | |
| SC_RET(SECCOMP_RET_KILL_THREAD), | |
| SC_LOAD(nr), | |
| #ifdef __x86_64__ | |
| /* Reject x32, except for the skipped-syscall marker (-1). */ | |
| BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, __X32_SYSCALL_BIT, 0, 2), | |
| SC_EQ(-1U, 1, 0), | |
| SC_RET(SECCOMP_RET_KILL_THREAD), | |
| #endif | |
| SC_EQ(SYS_CELL_CREATE, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(SYS_CELL_CONFIGURE, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(SYS_CELL_INFO, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(SYS_CELL_OPEN, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(SYS_CELL_KILL, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(SYS_PIDFD_CONFIGURE, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_mount_setattr, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_open_tree_attr, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_openat, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_newfstatat, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_readlinkat, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_write, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| #ifdef __x86_64__ | |
| SC_EQ(__NR_open, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_stat, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_lstat, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_EQ(__NR_readlink, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| #endif | |
| /* pidfd_open() with PIDFD_EMPTY. */ | |
| SC_EQ(__NR_pidfd_open, 0, 4), | |
| SC_ARG_LO(1), | |
| BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, PIDFD_EMPTY, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_RET(SECCOMP_RET_ALLOW), | |
| /* fsconfig() with FSCONFIG_SET_FD. */ | |
| SC_EQ(__NR_fsconfig, 0, 3), | |
| SC_ARG_LO(1), | |
| SC_EQ(FSCONFIG_SET_FD, 0, 1), | |
| SC_RET(SECCOMP_RET_USER_NOTIF), | |
| SC_RET(SECCOMP_RET_ALLOW), | |
| }; | |
| const struct sock_fprog prog = { | |
| .len = ARRAY_SIZE(filter), | |
| .filter = filter, | |
| }; | |
| uint32_t action = SECCOMP_RET_USER_NOTIF; | |
| long fd; | |
| if (syscall(__NR_seccomp, SECCOMP_GET_ACTION_AVAIL, 0U, &action) < 0) | |
| return err_ctx(-errno, | |
| "kernel lacks SECCOMP_RET_USER_NOTIF support"); | |
| if (prctl(PR_SET_NO_NEW_PRIVS, 1L, 0L, 0L, 0L) < 0) | |
| return err_ctx(-errno, "PR_SET_NO_NEW_PRIVS"); | |
| fd = syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, | |
| SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog); | |
| if (fd < 0) | |
| return err_ctx(-errno, "install seccomp user-notif filter"); | |
| return fd; | |
| } | |
| static int notif_ioctl(int listener, unsigned long request, void *arg) | |
| { | |
| int ret; | |
| do { | |
| ret = ioctl(listener, request, arg); | |
| } while (ret < 0 && errno == EINTR); | |
| return ret < 0 ? -errno : ret; | |
| } | |
| int notif_recv(int listener, struct seccomp_notif *out) | |
| { | |
| int ret; | |
| memset(out, 0, sizeof(*out)); | |
| ret = notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, out); | |
| if (ret == -ENOENT) | |
| return 0; | |
| return ret < 0 ? ret : 1; | |
| } | |
| bool notif_is_id_valid(int listener, uint64_t id) | |
| { | |
| return notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &id) == 0; | |
| } | |
| int notif_add_fd(int listener, uint64_t id, int src_fd, uint32_t newfd, | |
| uint32_t flags, uint32_t newfd_flags) | |
| { | |
| struct seccomp_notif_addfd req = { | |
| .id = id, | |
| .flags = flags, | |
| .srcfd = (uint32_t)src_fd, | |
| .newfd = newfd, | |
| .newfd_flags = newfd_flags, | |
| }; | |
| if (src_fd < 0) | |
| return -EBADF; | |
| return notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &req); | |
| } | |
| static int notif_send(int listener, struct seccomp_notif_resp *resp) | |
| { | |
| int ret = notif_ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, resp); | |
| return ret == -ENOENT ? 0 : ret; | |
| } | |
| int notif_send_value(int listener, uint64_t id, int64_t val) | |
| { | |
| struct seccomp_notif_resp resp = {.id = id, .val = val}; | |
| return notif_send(listener, &resp); | |
| } | |
| int notif_send_errno(int listener, uint64_t id, int err) | |
| { | |
| struct seccomp_notif_resp resp = {.id = id, .error = err}; | |
| return notif_send(listener, &resp); | |
| } | |
| int notif_send_continue(int listener, uint64_t id) | |
| { | |
| struct seccomp_notif_resp resp = { | |
| .id = id, | |
| .flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE, | |
| }; | |
| return notif_send(listener, &resp); | |
| } |