module coracle import json2 import nspkg as facade import nspkg.engine import os fn test_unprivileged_sandbox_failure_preserves_error_state_like_c() ! { if C.getuid() == 0 { return } base := os.join_path(os.vtmp_dir(), 'alpm-sandbox-error-${os.getpid()}') os.mkdir_all(base)! defer { os.rmdir_all(base) or { eprintln(err) } } old := initialize('/', base)! defer { old.release() or { eprintln(err) } } public := facade.initialize('/', base)! defer { public.release() or { eprintln(err) } } mut native := engine.initialize('/', base)! defer { native.release() or { eprintln(err) } } for prior in [false, true] { for user in ['', 'root', 'nobody', 'alpm-nonexistent-user', 'root\x00suffix'] { old.option_get_root() public.option_get_root() native.validate_handle()! if prior { if _ := old.register_syncdb('', 0) { assert false } if _ := public.register_syncdb('', 0) { assert false } if _ := native.register_syncdb('', 0) { assert false } } mut expected := -1 old.sandbox_setup_child(user, '', true) or { expected = err.code() } mut actual := -1 public.sandbox_setup_child(user, '', true) or { actual = err.code() } mut core := -1 native.sandbox_setup_child(user, none, true) or { core = err.code() } assert expected == if prior { 6 } else { 0 } assert [actual, core, int(public.errno()), int(native.errno())] == [ expected, expected, expected, expected, ], '${prior}/${user.bytes()}' assert int(old.errno()) == expected } } } struct SandboxObservation { success bool uid u32 gid i32 groups int inside_write bool outside_write bool outside_read bool can_execute bool can_query_personality bool can_regain_root bool no_new_privileges bool automatic bool code int logs []string failure_code int = -1 faults int translated bool } fn sandbox_command(executable string, args []string) !string { mut process := os.new_process(executable) process.set_args(args) process.set_redirect_stdio() process.run() defer { process.close() } output := process.stdout_slurp() diagnostic := process.stderr_slurp() process.wait() if process.code != 0 { return error('${executable} ${args} failed ${process.code}: ${diagnostic}') } return output } fn test_native_sandbox_privileges_and_kernel_restrictions_match_libalpm() ! { $if linux { dir := os.join_path(os.temp_dir(), 'alpm-sandbox-parity-${os.getpid()}') os.mkdir_all(dir, mode: 0o755)! defer { os.rmdir_all(dir) or { eprintln(err) } } probe := os.join_path(dir, 'probe') sandbox_command('v', ['-path', os.dir(@VMODROOT) + '|@vlib|@vmodules', '-o', probe, os.join_path(@VMODROOT, 'tests', 'sandbox_probe.v')])! for disable_fs in [false, true] { for disable_syscalls in [false, true] { for restrict_syscalls in [false, true] { for has_path in [false, true] { for mode in ['clear', 'prior', 'callback', 'translated'] { mut results := []SandboxObservation{} for backend in ['reference', 'native', 'public'] { base := os.join_path(dir, '${disable_fs}-${disable_syscalls}-${restrict_syscalls}-${has_path}-${mode}-${backend}') os.mkdir_all(base, mode: 0o755)! output := sandbox_command('timeout', ['15s', 'unshare', '--user', '--map-root-user', '--map-auto', '--setgroups=allow', probe, backend, base, if disable_fs { 'off' } else { 'on' }, if disable_syscalls { 'off' } else { 'on' }, if restrict_syscalls { 'yes' } else { 'no' }, if has_path { 'path' } else { 'none' }, mode, 'valid'])! results << json2.decode[SandboxObservation](output)! } assert results[0] == results[1], results.str() assert results[0] == results[2], results.str() result := results[1] assert result.success assert result.uid == 65534 && result.gid == 65534 assert result.groups == 0 assert result.inside_write && result.outside_read assert result.outside_write == (disable_fs || !has_path) assert result.can_execute == (disable_fs || !has_path) assert result.can_query_personality == (disable_syscalls || !restrict_syscalls) assert !result.can_regain_root && result.no_new_privileges assert result.automatic == (!disable_fs || !disable_syscalls) has_log := (!disable_fs && has_path) || (!disable_syscalls && restrict_syscalls) assert result.code == if mode == 'prior' || (mode == 'callback' && has_log) { 6 } else { 0 } assert (result.logs.len > 0) == has_log, '${disable_fs}/${disable_syscalls}/${restrict_syscalls}/${has_path}/${mode}: ${result.logs}' assert result.faults == 0 && result.failure_code == -1 if result.translated && has_log { assert result.logs.any(it.contains('zz:')) } } } } } } for scenario in ['missing-user', 'empty-user', 'nul-user', 'missing-path', 'file-path', 'empty-path', 'nul-path', 'init', 'load', 'rule', 'gid', 'groups', 'uid', 'nnp', 'landlock-abi', 'landlock-create', 'landlock-rule', 'landlock-apply'] { for mode in ['clear', 'prior', 'callback', 'translated'] { mut results := []SandboxObservation{} for backend in ['reference', 'native', 'public'] { base := os.join_path(dir, '${scenario}-${mode}-${backend}') os.mkdir_all(base, mode: 0o755)! output := sandbox_command('timeout', ['15s', 'unshare', '--user', '--map-root-user', '--map-auto', '--setgroups=allow', probe, backend, base, 'on', 'on', if scenario == 'nnp' { 'no' } else { 'yes' }, if scenario in ['init', 'load', 'rule', 'nnp'] { 'none' } else { 'path' }, mode, scenario])! results << json2.decode[SandboxObservation](output)! } if scenario in ['rule', 'nnp'] { // C continues after these failures; native rejects an incomplete sandbox. assert results[0].success && results[0].uid == 65534, '${scenario}: ${results}' assert !results[1].success && !results[2].success assert results[1] == results[2], '${scenario}: ${results}' if scenario == 'rule' { assert results[1].logs.len == 1 assert results[0].logs[0] == results[1].logs[0] } } else { assert results[0] == results[1], '${scenario}/${mode}: ${results}' assert results[0] == results[2], '${scenario}/${mode}: ${results}' assert results[0].success == (scenario == 'nul-user') } result := results[1] assert result.code == if mode == 'prior' || (mode == 'callback' && result.logs.len > 0) { 6 } else { 0 } if !result.success { assert result.failure_code == result.code } assert result.faults == if scenario.starts_with('landlock-') || scenario in [ 'init', 'load', 'rule', 'gid', 'groups', 'uid', 'nnp', ] { 1 } else { 0 } } } } }