| 1 | // access bits and deny list follow linux landlock uapi and pacman sandbox_*.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | $if linux { |
| 8 | #flag -D_GNU_SOURCE |
| 9 | #flag -lseccomp |
| 10 | } |
| 11 | |
| 12 | fn C.v_alpm_no_new_privileges() i32 |
| 13 | fn C.v_alpm_landlock_abi() i32 |
| 14 | fn C.v_alpm_landlock_create(u64) i32 |
| 15 | fn C.v_alpm_landlock_path(i32, i32, u64) i32 |
| 16 | fn C.v_alpm_landlock_apply(i32) i32 |
| 17 | fn C.seccomp_init(u32) voidptr |
| 18 | fn C.seccomp_release(voidptr) |
| 19 | fn C.seccomp_load(voidptr) i32 |
| 20 | fn C.seccomp_syscall_resolve_name(&char) i32 |
| 21 | fn C.v_alpm_seccomp_deny(voidptr, i32, i32) i32 |
| 22 | |
| 23 | const sandbox_denied_syscalls = [ |
| 24 | 'delete_module', |
| 25 | 'finit_module', |
| 26 | 'init_module', |
| 27 | 'chroot', |
| 28 | 'fsconfig', |
| 29 | 'fsmount', |
| 30 | 'fsopen', |
| 31 | 'fspick', |
| 32 | 'mount', |
| 33 | 'mount_setattr', |
| 34 | 'move_mount', |
| 35 | 'open_tree', |
| 36 | 'pivot_root', |
| 37 | 'umount', |
| 38 | 'umount2', |
| 39 | 'add_key', |
| 40 | 'keyctl', |
| 41 | 'request_key', |
| 42 | 'modify_ldt', |
| 43 | 'subpage_prot', |
| 44 | 'switch_endian', |
| 45 | 'vm86', |
| 46 | 'vm86old', |
| 47 | 'kcmp', |
| 48 | 'lookup_dcookie', |
| 49 | 'perf_event_open', |
| 50 | 'pidfd_getfd', |
| 51 | 'ptrace', |
| 52 | 'rtas', |
| 53 | 'sys_debug_setcontext', |
| 54 | 'adjtimex', |
| 55 | 'clock_adjtime', |
| 56 | 'clock_adjtime64', |
| 57 | 'clock_settime', |
| 58 | 'clock_settime64', |
| 59 | 'settimeofday', |
| 60 | 'ioperm', |
| 61 | 'iopl', |
| 62 | 'pciconfig_iobase', |
| 63 | 'pciconfig_read', |
| 64 | 'pciconfig_write', |
| 65 | 'kexec_file_load', |
| 66 | 'kexec_load', |
| 67 | 'reboot', |
| 68 | 'acct', |
| 69 | 'bpf', |
| 70 | 'capset', |
| 71 | 'chroot', |
| 72 | 'fanotify_init', |
| 73 | 'fanotify_mark', |
| 74 | 'nfsservctl', |
| 75 | 'open_by_handle_at', |
| 76 | 'pivot_root', |
| 77 | 'personality', |
| 78 | '_sysctl', |
| 79 | 'afs_syscall', |
| 80 | 'bdflush', |
| 81 | 'break', |
| 82 | 'create_module', |
| 83 | 'ftime', |
| 84 | 'get_kernel_syms', |
| 85 | 'getpmsg', |
| 86 | 'gtty', |
| 87 | 'idle', |
| 88 | 'lock', |
| 89 | 'mpx', |
| 90 | 'prof', |
| 91 | 'profil', |
| 92 | 'putpmsg', |
| 93 | 'query_module', |
| 94 | 'security', |
| 95 | 'sgetmask', |
| 96 | 'ssetmask', |
| 97 | 'stime', |
| 98 | 'stty', |
| 99 | 'sysfs', |
| 100 | 'tuxcall', |
| 101 | 'ulimit', |
| 102 | 'uselib', |
| 103 | 'ustat', |
| 104 | 'vserver', |
| 105 | 'swapon', |
| 106 | 'swapoff', |
| 107 | ] |
| 108 | |
| 109 | fn sandbox_filesystem_mask(abi int) u64 { |
| 110 | mut mask := u64(0x1fff) |
| 111 | if abi >= 2 { mask |= u64(1) << 13 } |
| 112 | if abi >= 3 { mask |= u64(1) << 14 } |
| 113 | return mask |
| 114 | } |
| 115 | |
| 116 | fn (mut handle Handle) restrict_sandbox_filesystem(path string) !int { |
| 117 | $if linux { |
| 118 | abi := C.v_alpm_landlock_abi() |
| 119 | if abi < 0 { |
| 120 | problem := os.last_error().msg() |
| 121 | handle.log(.error, localized_message('restricting filesystem access failed because Landlock is not supported by the kernel!\n')) |
| 122 | return AlpmError{ kind: .retrieve_prepare, detail: 'Landlock is not supported: ${problem}' } |
| 123 | } |
| 124 | if abi < 2 { |
| 125 | handle.log(.debug, localized_message('Landlock ABI < 2, LANDLOCK_ACCESS_FS_REFER is not supported\n')) |
| 126 | } |
| 127 | if abi < 3 { |
| 128 | handle.log(.debug, localized_message('Landlock ABI < 3, LANDLOCK_ACCESS_FS_TRUNCATE is not supported\n')) |
| 129 | } |
| 130 | mask := sandbox_filesystem_mask(abi) |
| 131 | ruleset := C.v_alpm_landlock_create(mask) |
| 132 | if ruleset < 0 { |
| 133 | problem := os.last_error().msg() |
| 134 | handle.log(.error, localized_message('restricting filesystem access failed because the Landlock ruleset could not be created: %s\n', problem)) |
| 135 | return AlpmError{ kind: .retrieve_prepare, detail: 'creating Landlock ruleset: ${problem}' } |
| 136 | } |
| 137 | defer { |
| 138 | if C.close(ruleset) != 0 { |
| 139 | handle.diagnostics << 'could not close Landlock ruleset: ${os.last_error().msg()}' |
| 140 | } |
| 141 | } |
| 142 | root_fd := C.open(c'/', C.O_PATH | C.O_CLOEXEC | C.O_DIRECTORY) |
| 143 | if root_fd < 0 { |
| 144 | problem := os.last_error().msg() |
| 145 | handle.log(.error, localized_message('opening the root filesystem to make it read-only via Landlock failed: %s\n', problem)) |
| 146 | return AlpmError{ kind: .retrieve_prepare, detail: 'opening filesystem root: ${problem}' } |
| 147 | } |
| 148 | defer { |
| 149 | if C.close(root_fd) != 0 { |
| 150 | handle.diagnostics << 'could not close sandbox root: ${os.last_error().msg()}' |
| 151 | } |
| 152 | } |
| 153 | if C.v_alpm_landlock_path(ruleset, root_fd, 0x0c) != 0 { |
| 154 | problem := os.last_error().msg() |
| 155 | handle.log(.error, localized_message('restricting filesystem access failed because the Landlock rule for / could not be added: %s\n', problem)) |
| 156 | return AlpmError{ kind: .retrieve_prepare, detail: 'adding read-only root rule: ${problem}' } |
| 157 | } |
| 158 | directory_fd := C.open(unsafe { &char(path.str) }, C.O_PATH | C.O_CLOEXEC | C.O_DIRECTORY) |
| 159 | if directory_fd < 0 { |
| 160 | problem := os.last_error().msg() |
| 161 | handle.log(.error, localized_message('opening the download directory to make it writable via Landlock failed: %s\n', problem)) |
| 162 | return AlpmError{ kind: .retrieve_prepare, detail: 'opening writable sandbox directory: ${problem}' } |
| 163 | } |
| 164 | defer { |
| 165 | if C.close(directory_fd) != 0 { |
| 166 | handle.diagnostics << 'could not close sandbox directory: ${os.last_error().msg()}' |
| 167 | } |
| 168 | } |
| 169 | // cross-directory references and execution stay denied, including within the write tree. |
| 170 | allowed := (u64(0x1ffe) | (u64(1) << 14)) & mask |
| 171 | if C.v_alpm_landlock_path(ruleset, directory_fd, allowed) != 0 { |
| 172 | problem := os.last_error().msg() |
| 173 | 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)) |
| 174 | return AlpmError{ kind: .retrieve_prepare, detail: 'adding writable directory rule: ${problem}' } |
| 175 | } |
| 176 | sandbox_no_new_privileges() or { return AlpmError{ kind: .retrieve_prepare, detail: err.msg() } } |
| 177 | if C.v_alpm_landlock_apply(ruleset) != 0 { |
| 178 | problem := os.last_error().msg() |
| 179 | handle.log(.error, localized_message('restricting filesystem access failed because the Landlock ruleset could not be applied: %s\n', problem)) |
| 180 | return AlpmError{ kind: .retrieve_prepare, detail: 'applying Landlock ruleset: ${problem}' } |
| 181 | } |
| 182 | handle.log(.debug, localized_message('filesystem access has been restricted to %s, Landlock ABI is %d\n', path, abi)) |
| 183 | return abi |
| 184 | } $else { |
| 185 | return 0 |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn (mut handle Handle) sandbox_syscall_context() !(voidptr, int) { |
| 190 | $if linux { |
| 191 | context := C.seccomp_init(C.SCMP_ACT_ALLOW) |
| 192 | if context == unsafe { nil } { |
| 193 | handle.log(.error, localized_message('error initializing seccomp to filter system calls in the download sandbox!\n')) |
| 194 | return AlpmError{ kind: .retrieve_prepare, detail: 'initializing seccomp' } |
| 195 | } |
| 196 | mut keep := false |
| 197 | defer { if !keep { C.seccomp_release(context) } } |
| 198 | mut count := 0 |
| 199 | for name in sandbox_denied_syscalls { |
| 200 | number := C.seccomp_syscall_resolve_name(unsafe { &char(name.str) }) |
| 201 | if number == C.__NR_SCMP_ERROR { continue } |
| 202 | result := C.v_alpm_seccomp_deny(context, number, C.EPERM) |
| 203 | if result != 0 { |
| 204 | handle.log(.error, localized_message('error restricting syscall %s via seccomp!\n', name)) |
| 205 | // a partially installed deny list is not a usable sandbox. |
| 206 | return AlpmError{ kind: .retrieve_prepare, detail: 'restricting syscall ${name}: seccomp returned ${result}' } |
| 207 | } |
| 208 | count++ |
| 209 | } |
| 210 | keep = true |
| 211 | return context, count |
| 212 | } $else { |
| 213 | return unsafe { nil }, 0 |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | fn (mut handle Handle) restrict_sandbox_syscalls() ! { |
| 218 | $if linux { |
| 219 | context, count := handle.sandbox_syscall_context()! |
| 220 | defer { C.seccomp_release(context) } |
| 221 | result := C.seccomp_load(context) |
| 222 | if result != 0 { |
| 223 | failure := i32(C.errno) |
| 224 | handle.log(.error, localized_message('error restricting syscalls via seccomp: %d!\n', failure)) |
| 225 | return AlpmError{ kind: .retrieve_prepare, detail: 'applying syscall filter: seccomp returned ${result}' } |
| 226 | } |
| 227 | handle.log(.debug, localized_message('successfully restricted %d syscalls via seccomp\n', count)) |
| 228 | } |
| 229 | } |
| 230 | |