| 1 | module engine |
| 2 | |
| 3 | import json2 |
| 4 | import os |
| 5 | import time |
| 6 | |
| 7 | fn test_sandbox_protocol_rejects_forged_and_incomplete_results() ! { |
| 8 | dir := os.join_path(os.vtmp_dir(), 'native-sandbox-protocol-${os.getpid()}') |
| 9 | os.mkdir_all(dir)! |
| 10 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 11 | mut handle := initialize('/', dir)! |
| 12 | defer { handle.release() or { eprintln(err) } } |
| 13 | request := DownloadRequest{ filename: 'app', download_signature: true } |
| 14 | for frame in [ |
| 15 | '{', |
| 16 | '{"kind":"unknown"}', |
| 17 | '{"kind":"download","event":{"kind":"unknown","filename":"app"}}', |
| 18 | json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: '../victim' } }), |
| 19 | json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ kind: .completed, filename: 'app' } }), |
| 20 | json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: 'app', total: -1 } }), |
| 21 | json2.encode(SandboxMessage{ kind: .diagnostic, message: 'x'.repeat(4097) }), |
| 22 | ] { |
| 23 | mut state := new_sandbox_protocol([request]) |
| 24 | if _ := state.accept(mut handle, frame) { |
| 25 | assert false, frame |
| 26 | } |
| 27 | } |
| 28 | mut state := new_sandbox_protocol([request]) |
| 29 | state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: 'app' } }))! |
| 30 | if _ := state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: 'app' } })) { |
| 31 | assert false |
| 32 | } |
| 33 | state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ kind: .completed, filename: 'app', result: 0 } }))! |
| 34 | state.accept(mut handle, json2.encode(SandboxMessage{ kind: .result, updated: true }))! |
| 35 | if _ := state.report(dir) { |
| 36 | assert false, 'missing required signature was accepted' |
| 37 | } |
| 38 | if _ := state.accept(mut handle, json2.encode(SandboxMessage{ kind: .result })) { |
| 39 | assert false |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn test_sandbox_protocol_keeps_request_order_and_signature_start_order() ! { |
| 44 | dir := os.join_path(os.vtmp_dir(), 'native-sandbox-order-${os.getpid()}') |
| 45 | os.mkdir_all(dir)! |
| 46 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 47 | mut handle := initialize('/', dir)! |
| 48 | defer { handle.release() or { eprintln(err) } } |
| 49 | mut state := new_sandbox_protocol([ |
| 50 | DownloadRequest{ filename: 'payload.sig' }, |
| 51 | DownloadRequest{ filename: 'first', download_signature: true }, |
| 52 | DownloadRequest{ filename: 'second', download_signature: true, signature_optional: true }, |
| 53 | ]) |
| 54 | for name in ['second', 'second.sig', 'payload.sig', 'first', 'first.sig'] { |
| 55 | state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ filename: name } }))! |
| 56 | state.accept(mut handle, json2.encode(SandboxMessage{ kind: .download, event: DownloadEvent{ kind: .completed, filename: name, result: 0 } }))! |
| 57 | } |
| 58 | state.accept(mut handle, json2.encode(SandboxMessage{ kind: .result, updated: true }))! |
| 59 | report := state.report(dir)! |
| 60 | assert report.files.map(os.file_name(it.path)) == ['payload.sig', 'first', 'second', 'second.sig', |
| 61 | 'first.sig'] |
| 62 | assert report.files.map(it.optional) == [false, false, false, true, false] |
| 63 | assert report.updated |
| 64 | } |
| 65 | |
| 66 | fn test_sandbox_worker_closes_inherited_descriptors_and_redirects_stdio() ! { |
| 67 | dir := os.join_path(os.vtmp_dir(), 'native-sandbox-descriptors-${os.getpid()}') |
| 68 | os.mkdir_all(dir)! |
| 69 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 70 | path := os.join_path(dir, 'private') |
| 71 | os.write_file(path, 'untouched')! |
| 72 | fd := C.open(unsafe { &char(path.str) }, C.O_WRONLY) |
| 73 | assert fd >= 0 |
| 74 | defer { assert C.close(fd) == 0 } |
| 75 | mut pipes := [i32(0), 0] |
| 76 | assert C.pipe(unsafe { &i32(pipes.data) }) == 0 |
| 77 | defer { assert C.close(pipes[0]) == 0 } |
| 78 | child := C.fork() |
| 79 | assert child >= 0 |
| 80 | if child == 0 { |
| 81 | if C.dup2(fd, 200) != 200 { C._exit(2) } |
| 82 | writer := C.v_alpm_duplicate_pipe(pipes[1]) |
| 83 | if writer < 0 { C._exit(2) } |
| 84 | sandbox_close_descriptors(writer) or { C._exit(2) } |
| 85 | if C.write(200, c'x', 1) != -1 || C.errno != C.EBADF { C._exit(2) } |
| 86 | for stream := i32(0); stream < 3; stream++ { |
| 87 | mut size := i64(0) |
| 88 | mut mode := u32(0) |
| 89 | mut links := u64(0) |
| 90 | if C.v_alpm_download_stat(stream, &size, &mode, &links) != 0 || mode & 0o170000 != 0o020000 { |
| 91 | C._exit(2) |
| 92 | } |
| 93 | } |
| 94 | if C.write(writer, c'y', 1) != 1 { C._exit(2) } |
| 95 | C._exit(0) |
| 96 | } |
| 97 | assert C.close(pipes[1]) == 0 |
| 98 | mut result := [u8(0)] |
| 99 | count := C.read(pipes[0], result.data, 1) |
| 100 | mut status := i32(0) |
| 101 | for { |
| 102 | if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } |
| 103 | if C.errno != C.EINTR { return os.last_error() } |
| 104 | } |
| 105 | assert C.v_alpm_exit_status(status) == 0 |
| 106 | assert count == 1 && result[0] == `y` |
| 107 | assert os.read_file(path)! == 'untouched' |
| 108 | } |
| 109 | |
| 110 | fn keep_collector_thread_registered(ready chan bool, finish chan bool) { |
| 111 | ready <- true |
| 112 | _ := <-finish |
| 113 | } |
| 114 | |
| 115 | fn test_native_fork_collects_with_other_parent_threads_without_stale_thread_entries() ! { |
| 116 | ready := chan bool{} |
| 117 | finish := chan bool{} |
| 118 | worker := spawn keep_collector_thread_registered(ready, finish) |
| 119 | _ := <-ready |
| 120 | defer { |
| 121 | finish <- true |
| 122 | worker.wait() |
| 123 | } |
| 124 | child := fork_process() |
| 125 | assert child >= 0 |
| 126 | if child == 0 { |
| 127 | // force the failure rather than depending on the worker's allocation volume. |
| 128 | gc_collect() |
| 129 | C._exit(0) |
| 130 | } |
| 131 | deadline := time.now().unix_milli() + 5000 |
| 132 | mut status := i32(0) |
| 133 | for { |
| 134 | result := C.v_alpm_wait_child(child, &status, C.WNOHANG) |
| 135 | if result == child { break } |
| 136 | if result < 0 && C.errno != C.EINTR { return os.last_error() } |
| 137 | if time.now().unix_milli() >= deadline { |
| 138 | C.kill(child, C.SIGKILL) |
| 139 | for C.v_alpm_wait_child(child, &status, 0) < 0 { |
| 140 | if C.errno != C.EINTR { return os.last_error() } |
| 141 | } |
| 142 | assert false, 'collector did not finish after fork from a threaded parent' |
| 143 | } |
| 144 | time.sleep(10 * time.millisecond) |
| 145 | } |
| 146 | assert C.v_alpm_exit_status(status) == 0 |
| 147 | assert C.v_alpm_term_signal(status) == 0 |
| 148 | } |
| 149 | |