// access bits and deny list follow linux landlock uapi and pacman sandbox_*.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os $if linux { #flag -D_GNU_SOURCE #flag -lseccomp } fn C.v_alpm_no_new_privileges() i32 fn C.v_alpm_landlock_abi() i32 fn C.v_alpm_landlock_create(u64) i32 fn C.v_alpm_landlock_path(i32, i32, u64) i32 fn C.v_alpm_landlock_apply(i32) i32 fn C.seccomp_init(u32) voidptr fn C.seccomp_release(voidptr) fn C.seccomp_load(voidptr) i32 fn C.seccomp_syscall_resolve_name(&char) i32 fn C.v_alpm_seccomp_deny(voidptr, i32, i32) i32 const sandbox_denied_syscalls = [ 'delete_module', 'finit_module', 'init_module', 'chroot', 'fsconfig', 'fsmount', 'fsopen', 'fspick', 'mount', 'mount_setattr', 'move_mount', 'open_tree', 'pivot_root', 'umount', 'umount2', 'add_key', 'keyctl', 'request_key', 'modify_ldt', 'subpage_prot', 'switch_endian', 'vm86', 'vm86old', 'kcmp', 'lookup_dcookie', 'perf_event_open', 'pidfd_getfd', 'ptrace', 'rtas', 'sys_debug_setcontext', 'adjtimex', 'clock_adjtime', 'clock_adjtime64', 'clock_settime', 'clock_settime64', 'settimeofday', 'ioperm', 'iopl', 'pciconfig_iobase', 'pciconfig_read', 'pciconfig_write', 'kexec_file_load', 'kexec_load', 'reboot', 'acct', 'bpf', 'capset', 'chroot', 'fanotify_init', 'fanotify_mark', 'nfsservctl', 'open_by_handle_at', 'pivot_root', 'personality', '_sysctl', 'afs_syscall', 'bdflush', 'break', 'create_module', 'ftime', 'get_kernel_syms', 'getpmsg', 'gtty', 'idle', 'lock', 'mpx', 'prof', 'profil', 'putpmsg', 'query_module', 'security', 'sgetmask', 'ssetmask', 'stime', 'stty', 'sysfs', 'tuxcall', 'ulimit', 'uselib', 'ustat', 'vserver', 'swapon', 'swapoff', ] fn sandbox_filesystem_mask(abi int) u64 { mut mask := u64(0x1fff) if abi >= 2 { mask |= u64(1) << 13 } if abi >= 3 { mask |= u64(1) << 14 } return mask } fn (mut handle Handle) restrict_sandbox_filesystem(path string) !int { $if linux { abi := C.v_alpm_landlock_abi() if abi < 0 { problem := os.last_error().msg() handle.log(.error, localized_message('restricting filesystem access failed because Landlock is not supported by the kernel!\n')) return AlpmError{ kind: .retrieve_prepare, detail: 'Landlock is not supported: ${problem}' } } if abi < 2 { handle.log(.debug, localized_message('Landlock ABI < 2, LANDLOCK_ACCESS_FS_REFER is not supported\n')) } if abi < 3 { handle.log(.debug, localized_message('Landlock ABI < 3, LANDLOCK_ACCESS_FS_TRUNCATE is not supported\n')) } mask := sandbox_filesystem_mask(abi) ruleset := C.v_alpm_landlock_create(mask) if ruleset < 0 { problem := os.last_error().msg() handle.log(.error, localized_message('restricting filesystem access failed because the Landlock ruleset could not be created: %s\n', problem)) return AlpmError{ kind: .retrieve_prepare, detail: 'creating Landlock ruleset: ${problem}' } } defer { if C.close(ruleset) != 0 { handle.diagnostics << 'could not close Landlock ruleset: ${os.last_error().msg()}' } } root_fd := C.open(c'/', C.O_PATH | C.O_CLOEXEC | C.O_DIRECTORY) if root_fd < 0 { problem := os.last_error().msg() handle.log(.error, localized_message('opening the root filesystem to make it read-only via Landlock failed: %s\n', problem)) return AlpmError{ kind: .retrieve_prepare, detail: 'opening filesystem root: ${problem}' } } defer { if C.close(root_fd) != 0 { handle.diagnostics << 'could not close sandbox root: ${os.last_error().msg()}' } } if C.v_alpm_landlock_path(ruleset, root_fd, 0x0c) != 0 { problem := os.last_error().msg() handle.log(.error, localized_message('restricting filesystem access failed because the Landlock rule for / could not be added: %s\n', problem)) return AlpmError{ kind: .retrieve_prepare, detail: 'adding read-only root rule: ${problem}' } } directory_fd := C.open(unsafe { &char(path.str) }, C.O_PATH | C.O_CLOEXEC | C.O_DIRECTORY) if directory_fd < 0 { problem := os.last_error().msg() handle.log(.error, localized_message('opening the download directory to make it writable via Landlock failed: %s\n', problem)) return AlpmError{ kind: .retrieve_prepare, detail: 'opening writable sandbox directory: ${problem}' } } defer { if C.close(directory_fd) != 0 { handle.diagnostics << 'could not close sandbox directory: ${os.last_error().msg()}' } } // cross-directory references and execution stay denied, including within the write tree. allowed := (u64(0x1ffe) | (u64(1) << 14)) & mask if C.v_alpm_landlock_path(ruleset, directory_fd, allowed) != 0 { problem := os.last_error().msg() handle.log(.error, localized_message('restricting filesystem access failed because the Landlock rule for the temporary download directory could not be added: %s\n', problem)) return AlpmError{ kind: .retrieve_prepare, detail: 'adding writable directory rule: ${problem}' } } sandbox_no_new_privileges() or { return AlpmError{ kind: .retrieve_prepare, detail: err.msg() } } if C.v_alpm_landlock_apply(ruleset) != 0 { problem := os.last_error().msg() handle.log(.error, localized_message('restricting filesystem access failed because the Landlock ruleset could not be applied: %s\n', problem)) return AlpmError{ kind: .retrieve_prepare, detail: 'applying Landlock ruleset: ${problem}' } } handle.log(.debug, localized_message('filesystem access has been restricted to %s, Landlock ABI is %d\n', path, abi)) return abi } $else { return 0 } } fn (mut handle Handle) sandbox_syscall_context() !(voidptr, int) { $if linux { context := C.seccomp_init(C.SCMP_ACT_ALLOW) if context == unsafe { nil } { handle.log(.error, localized_message('error initializing seccomp to filter system calls in the download sandbox!\n')) return AlpmError{ kind: .retrieve_prepare, detail: 'initializing seccomp' } } mut keep := false defer { if !keep { C.seccomp_release(context) } } mut count := 0 for name in sandbox_denied_syscalls { number := C.seccomp_syscall_resolve_name(unsafe { &char(name.str) }) if number == C.__NR_SCMP_ERROR { continue } result := C.v_alpm_seccomp_deny(context, number, C.EPERM) if result != 0 { handle.log(.error, localized_message('error restricting syscall %s via seccomp!\n', name)) // a partially installed deny list is not a usable sandbox. return AlpmError{ kind: .retrieve_prepare, detail: 'restricting syscall ${name}: seccomp returned ${result}' } } count++ } keep = true return context, count } $else { return unsafe { nil }, 0 } } fn (mut handle Handle) restrict_sandbox_syscalls() ! { $if linux { context, count := handle.sandbox_syscall_context()! defer { C.seccomp_release(context) } result := C.seccomp_load(context) if result != 0 { failure := i32(C.errno) handle.log(.error, localized_message('error restricting syscalls via seccomp: %d!\n', failure)) return AlpmError{ kind: .retrieve_prepare, detail: 'applying syscall filter: seccomp returned ${result}' } } handle.log(.debug, localized_message('successfully restricted %d syscalls via seccomp\n', count)) } }