| // SPDX-License-Identifier: GPL-2.0-only | |
| // Copyright (c) 2026 Jarkko Sakkinen | |
| #include <linux/landlock.h> | |
| #include <linux/sched.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/prctl.h> | |
| #include <sys/socket.h> | |
| #include "base.h" | |
| #include "sandbox.h" | |
| #include "seccomp_filter.h" | |
| #include "uapi.h" | |
| #define SOCK_TYPE_MASK 0x0f | |
| #define CLONE_NS_MASK \ | |
| (CLONE_NEWTIME | CLONE_NEWNS | CLONE_NEWCGROUP | CLONE_NEWUTS | \ | |
| CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWPID | CLONE_NEWNET) | |
| #define LANDLOCK_SCOPE_ABI 6 | |
| #define SC_ERRNO(err) (SECCOMP_RET_ERRNO | (err)) | |
| static int build_filter(uint64_t net_rights, bool no_net, | |
| struct sock_fprog *out) | |
| { | |
| static const struct sock_filter common[] = { | |
| SC_LOAD(arch), | |
| SC_EQ(SC_AUDIT_ARCH, 1, 0), | |
| SC_RET(SECCOMP_RET_KILL_PROCESS), | |
| 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_PROCESS), | |
| #endif | |
| /* Force libc to fall back to clone(), whose flags we inspect. | |
| */ | |
| SC_EQ(__NR_clone3, 0, 1), | |
| SC_RET(SC_ERRNO(ENOSYS)), | |
| SC_EQ(__NR_unshare, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_setns, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_mount, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_umount2, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_pivot_root, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_open_tree, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_move_mount, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_fsopen, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_fsconfig, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_fsmount, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_mount_setattr, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_open_tree_attr, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_fchroot, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(SYS_CELL_CREATE, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(SYS_CELL_CONFIGURE, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(SYS_CELL_OPEN, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(SYS_CELL_KILL, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(SYS_PIDFD_CONFIGURE, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_init_module, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_finit_module, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_EQ(__NR_delete_module, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| #if __NR_runfd != SYS_CELL_CREATE | |
| SC_EQ(__NR_runfd, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| #endif | |
| /* Ignore the direction and size of the NS_SET_IDMAP request. */ | |
| SC_EQ(__NR_ioctl, 0, 4), | |
| SC_ARG_LO(1), | |
| BPF_STMT(BPF_ALU | BPF_AND | BPF_K, | |
| _IO(_IOC_TYPEMASK, _IOC_NRMASK)), | |
| SC_EQ(_IO(NSIO, _IOC_NR(NS_SET_IDMAP)), 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_LOAD(nr), | |
| SC_EQ(__NR_clone, 0, 3), | |
| SC_ARG_LO(0), | |
| BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, CLONE_NS_MASK, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| }; | |
| static const struct sock_filter unix_only[] = { | |
| SC_LOAD(nr), | |
| SC_EQ(__NR_socket, 1, 0), | |
| SC_EQ(__NR_socketpair, 0, 5), | |
| SC_ARG_HI(0), | |
| SC_EQ(0, 0, 2), | |
| SC_ARG_LO(0), | |
| SC_EQ(AF_UNIX, 1, 0), | |
| SC_RET(SC_ERRNO(EAFNOSUPPORT)), | |
| }; | |
| static const struct sock_filter no_raw[] = { | |
| SC_LOAD(nr), | |
| SC_EQ(__NR_socket, 0, 7), | |
| SC_ARG_LO(1), | |
| BPF_STMT(BPF_ALU | BPF_AND | BPF_K, SOCK_TYPE_MASK), | |
| SC_EQ(SOCK_RAW, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| SC_ARG_LO(0), | |
| SC_EQ(AF_PACKET, 0, 1), | |
| SC_RET(SC_ERRNO(EPERM)), | |
| }; | |
| static const struct sock_filter allow = SC_RET(SECCOMP_RET_ALLOW); | |
| const struct sock_filter *network = NULL; | |
| struct sock_filter *filter; | |
| size_t net_len = 0; | |
| if (no_net) { | |
| network = unix_only; | |
| net_len = ARRAY_SIZE(unix_only); | |
| } else if (!(net_rights & CELL_NET_RAW)) { | |
| network = no_raw; | |
| net_len = ARRAY_SIZE(no_raw); | |
| } | |
| out->len = ARRAY_SIZE(common) + net_len + 1; | |
| filter = malloc(out->len * sizeof(*filter)); | |
| if (!filter) | |
| return err_ctx(-ENOMEM, "allocate cell seccomp filter"); | |
| memcpy(filter, common, sizeof(common)); | |
| if (net_len) | |
| memcpy(filter + ARRAY_SIZE(common), network, | |
| net_len * sizeof(*filter)); | |
| filter[out->len - 1] = allow; | |
| out->filter = filter; | |
| return 0; | |
| } | |
| int cell_sandbox_compile(uint64_t caps, uint64_t net_rights, bool no_net, | |
| struct cell_sandbox *out) | |
| { | |
| int ret; | |
| ret = build_filter(net_rights, no_net, &out->prog); | |
| if (ret) | |
| return ret; | |
| out->caps = caps; | |
| return 0; | |
| } | |
| void cell_sandbox_destroy(struct cell_sandbox *sandbox) | |
| { | |
| free(sandbox->prog.filter); | |
| *sandbox = (struct cell_sandbox){}; | |
| } | |
| static int last_error(void) | |
| { | |
| return errno > 0 ? -errno : -EIO; | |
| } | |
| static int apply_capabilities(uint64_t caps) | |
| { | |
| struct __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0}; | |
| uint32_t low = caps, high = caps >> 32; | |
| struct __user_cap_data_struct data[2] = {{low, low, 0}, | |
| {high, high, 0}}; | |
| unsigned long cap; | |
| int present; | |
| prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); | |
| for (cap = 0; cap <= CAP_LAST_CAP; cap++) { | |
| if (caps & (1ULL << cap)) | |
| continue; | |
| present = prctl(PR_CAPBSET_READ, cap, 0, 0, 0); | |
| if (present < 0) { | |
| if (errno == EINVAL) | |
| break; | |
| return last_error(); | |
| } | |
| if (present && prctl(PR_CAPBSET_DROP, cap, 0, 0, 0) < 0 && | |
| errno != EPERM) | |
| return last_error(); | |
| } | |
| if (syscall(__NR_capset, &hdr, data) < 0) | |
| return last_error(); | |
| return 0; | |
| } | |
| int cell_sandbox_apply(const struct cell_sandbox *sandbox, | |
| uint32_t effective_uid) | |
| { | |
| int ret; | |
| ret = apply_capabilities(effective_uid ? 0 : sandbox->caps); | |
| if (ret) | |
| return ret; | |
| if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) | |
| return last_error(); | |
| if (syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0U, &sandbox->prog) < | |
| 0) | |
| return last_error(); | |
| return 0; | |
| } | |
| int landlock_prepare(void) | |
| { | |
| struct landlock_ruleset_attr attr = { | |
| .scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | | |
| LANDLOCK_SCOPE_SIGNAL, | |
| }; | |
| long abi, fd; | |
| abi = syscall(__NR_landlock_create_ruleset, NULL, 0, | |
| LANDLOCK_CREATE_RULESET_VERSION); | |
| if (abi < LANDLOCK_SCOPE_ABI) | |
| return -1; | |
| fd = syscall(__NR_landlock_create_ruleset, &attr, | |
| offsetof(struct landlock_ruleset_attr, scoped) + | |
| sizeof(attr.scoped), | |
| 0U); | |
| return fd < 0 ? -1 : (int)fd; | |
| } | |
| void landlock_restrict(int fd) | |
| { | |
| int saved_errno = errno; | |
| if (fd < 0) | |
| return; | |
| prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); | |
| syscall(__NR_landlock_restrict_self, fd, 0U); | |
| close_fd(fd); | |
| errno = saved_errno; | |
| } |