| 1 | module coracle |
| 2 | |
| 3 | import json2 |
| 4 | import nspkg as facade |
| 5 | import nspkg.engine |
| 6 | import os |
| 7 | |
| 8 | fn test_unprivileged_sandbox_failure_preserves_error_state_like_c() ! { |
| 9 | if C.getuid() == 0 { return } |
| 10 | base := os.join_path(os.vtmp_dir(), 'alpm-sandbox-error-${os.getpid()}') |
| 11 | os.mkdir_all(base)! |
| 12 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 13 | old := initialize('/', base)! |
| 14 | defer { old.release() or { eprintln(err) } } |
| 15 | public := facade.initialize('/', base)! |
| 16 | defer { public.release() or { eprintln(err) } } |
| 17 | mut native := engine.initialize('/', base)! |
| 18 | defer { native.release() or { eprintln(err) } } |
| 19 | for prior in [false, true] { |
| 20 | for user in ['', 'root', 'nobody', 'alpm-nonexistent-user', 'root\x00suffix'] { |
| 21 | old.option_get_root() |
| 22 | public.option_get_root() |
| 23 | native.validate_handle()! |
| 24 | if prior { |
| 25 | if _ := old.register_syncdb('', 0) { |
| 26 | assert false |
| 27 | } |
| 28 | if _ := public.register_syncdb('', 0) { |
| 29 | assert false |
| 30 | } |
| 31 | if _ := native.register_syncdb('', 0) { |
| 32 | assert false |
| 33 | } |
| 34 | } |
| 35 | mut expected := -1 |
| 36 | old.sandbox_setup_child(user, '', true) or { expected = err.code() } |
| 37 | mut actual := -1 |
| 38 | public.sandbox_setup_child(user, '', true) or { actual = err.code() } |
| 39 | mut core := -1 |
| 40 | native.sandbox_setup_child(user, none, true) or { core = err.code() } |
| 41 | assert expected == if prior { 6 } else { 0 } |
| 42 | assert [actual, core, int(public.errno()), int(native.errno())] == [ |
| 43 | expected, |
| 44 | expected, |
| 45 | expected, |
| 46 | expected, |
| 47 | ], '${prior}/${user.bytes()}' |
| 48 | assert int(old.errno()) == expected |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | struct SandboxObservation { |
| 54 | success bool |
| 55 | uid u32 |
| 56 | gid i32 |
| 57 | groups int |
| 58 | inside_write bool |
| 59 | outside_write bool |
| 60 | outside_read bool |
| 61 | can_execute bool |
| 62 | can_query_personality bool |
| 63 | can_regain_root bool |
| 64 | no_new_privileges bool |
| 65 | automatic bool |
| 66 | code int |
| 67 | logs []string |
| 68 | failure_code int = -1 |
| 69 | faults int |
| 70 | translated bool |
| 71 | } |
| 72 | |
| 73 | fn sandbox_command(executable string, args []string) !string { |
| 74 | mut process := os.new_process(executable) |
| 75 | process.set_args(args) |
| 76 | process.set_redirect_stdio() |
| 77 | process.run() |
| 78 | defer { process.close() } |
| 79 | output := process.stdout_slurp() |
| 80 | diagnostic := process.stderr_slurp() |
| 81 | process.wait() |
| 82 | if process.code != 0 { |
| 83 | return error('${executable} ${args} failed ${process.code}: ${diagnostic}') |
| 84 | } |
| 85 | return output |
| 86 | } |
| 87 | |
| 88 | fn test_native_sandbox_privileges_and_kernel_restrictions_match_libalpm() ! { |
| 89 | $if linux { |
| 90 | dir := os.join_path(os.temp_dir(), 'alpm-sandbox-parity-${os.getpid()}') |
| 91 | os.mkdir_all(dir, mode: 0o755)! |
| 92 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 93 | probe := os.join_path(dir, 'probe') |
| 94 | sandbox_command('v', ['-path', os.dir(@VMODROOT) + '|@vlib|@vmodules', '-o', probe, |
| 95 | os.join_path(@VMODROOT, 'tests', 'sandbox_probe.v')])! |
| 96 | for disable_fs in [false, true] { |
| 97 | for disable_syscalls in [false, true] { |
| 98 | for restrict_syscalls in [false, true] { |
| 99 | for has_path in [false, true] { |
| 100 | for mode in ['clear', 'prior', 'callback', 'translated'] { |
| 101 | mut results := []SandboxObservation{} |
| 102 | for backend in ['reference', 'native', 'public'] { |
| 103 | base := os.join_path(dir, '${disable_fs}-${disable_syscalls}-${restrict_syscalls}-${has_path}-${mode}-${backend}') |
| 104 | os.mkdir_all(base, mode: 0o755)! |
| 105 | output := sandbox_command('timeout', ['15s', 'unshare', '--user', |
| 106 | '--map-root-user', '--map-auto', '--setgroups=allow', probe, |
| 107 | backend, base, if disable_fs { 'off' } else { 'on' }, |
| 108 | if disable_syscalls { 'off' } else { 'on' }, |
| 109 | if restrict_syscalls { 'yes' } else { 'no' }, |
| 110 | if has_path { 'path' } else { 'none' }, mode, 'valid'])! |
| 111 | results << json2.decode[SandboxObservation](output)! |
| 112 | } |
| 113 | assert results[0] == results[1], results.str() |
| 114 | assert results[0] == results[2], results.str() |
| 115 | result := results[1] |
| 116 | assert result.success |
| 117 | assert result.uid == 65534 && result.gid == 65534 |
| 118 | assert result.groups == 0 |
| 119 | assert result.inside_write && result.outside_read |
| 120 | assert result.outside_write == (disable_fs || !has_path) |
| 121 | assert result.can_execute == (disable_fs || !has_path) |
| 122 | assert result.can_query_personality == (disable_syscalls || !restrict_syscalls) |
| 123 | assert !result.can_regain_root && result.no_new_privileges |
| 124 | assert result.automatic == (!disable_fs || !disable_syscalls) |
| 125 | has_log := (!disable_fs && has_path) || (!disable_syscalls && restrict_syscalls) |
| 126 | assert result.code == if mode == 'prior' || (mode == 'callback' && has_log) { |
| 127 | 6 |
| 128 | } else { |
| 129 | 0 |
| 130 | } |
| 131 | assert (result.logs.len > 0) == has_log, '${disable_fs}/${disable_syscalls}/${restrict_syscalls}/${has_path}/${mode}: ${result.logs}' |
| 132 | assert result.faults == 0 && result.failure_code == -1 |
| 133 | if result.translated && has_log { |
| 134 | assert result.logs.any(it.contains('zz:')) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | for scenario in ['missing-user', 'empty-user', 'nul-user', 'missing-path', 'file-path', |
| 142 | 'empty-path', 'nul-path', 'init', 'load', 'rule', 'gid', 'groups', 'uid', 'nnp', |
| 143 | 'landlock-abi', 'landlock-create', 'landlock-rule', 'landlock-apply'] { |
| 144 | for mode in ['clear', 'prior', 'callback', 'translated'] { |
| 145 | mut results := []SandboxObservation{} |
| 146 | for backend in ['reference', 'native', 'public'] { |
| 147 | base := os.join_path(dir, '${scenario}-${mode}-${backend}') |
| 148 | os.mkdir_all(base, mode: 0o755)! |
| 149 | output := sandbox_command('timeout', ['15s', 'unshare', '--user', '--map-root-user', |
| 150 | '--map-auto', '--setgroups=allow', probe, backend, base, 'on', 'on', |
| 151 | if scenario == 'nnp' { 'no' } else { 'yes' }, |
| 152 | if scenario in ['init', 'load', 'rule', 'nnp'] { 'none' } else { 'path' }, |
| 153 | mode, scenario])! |
| 154 | results << json2.decode[SandboxObservation](output)! |
| 155 | } |
| 156 | if scenario in ['rule', 'nnp'] { |
| 157 | // C continues after these failures; native rejects an incomplete sandbox. |
| 158 | assert results[0].success && results[0].uid == 65534, '${scenario}: ${results}' |
| 159 | assert !results[1].success && !results[2].success |
| 160 | assert results[1] == results[2], '${scenario}: ${results}' |
| 161 | if scenario == 'rule' { |
| 162 | assert results[1].logs.len == 1 |
| 163 | assert results[0].logs[0] == results[1].logs[0] |
| 164 | } |
| 165 | } else { |
| 166 | assert results[0] == results[1], '${scenario}/${mode}: ${results}' |
| 167 | assert results[0] == results[2], '${scenario}/${mode}: ${results}' |
| 168 | assert results[0].success == (scenario == 'nul-user') |
| 169 | } |
| 170 | result := results[1] |
| 171 | assert result.code == if mode == 'prior' || (mode == 'callback' && result.logs.len > 0) { |
| 172 | 6 |
| 173 | } else { |
| 174 | 0 |
| 175 | } |
| 176 | if !result.success { |
| 177 | assert result.failure_code == result.code |
| 178 | } |
| 179 | assert result.faults == if scenario.starts_with('landlock-') || scenario in [ |
| 180 | 'init', |
| 181 | 'load', |
| 182 | 'rule', |
| 183 | 'gid', |
| 184 | 'groups', |
| 185 | 'uid', |
| 186 | 'nnp', |
| 187 | ] { |
| 188 | 1 |
| 189 | } else { |
| 190 | 0 |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |