| 1 | module engine |
| 2 | |
| 3 | import nspkg.tests.archivefixtures |
| 4 | import nspkg.tests.signaturefixtures |
| 5 | import os |
| 6 | import strconv |
| 7 | |
| 8 | #include "@VMODROOT/tests/extraction_probe.h" |
| 9 | #include "@VMODROOT/tests/local_storage_probe.h" |
| 10 | |
| 11 | fn C.v_alpm_extraction_probe(&char, &i64) i32 |
| 12 | fn C.v_alpm_limit_database_write() i32 |
| 13 | |
| 14 | fn C.archive_entry_clone(&C.Archive_entry) &C.Archive_entry |
| 15 | fn C.archive_entry_free(&C.Archive_entry) |
| 16 | fn C.archive_entry_set_pathname(&C.Archive_entry, &char) |
| 17 | fn C.archive_entry_set_hardlink(&C.Archive_entry, &char) |
| 18 | fn C.archive_write_disk_new() &C.Archive |
| 19 | fn C.archive_write_disk_set_options(&C.Archive, int) int |
| 20 | fn C.archive_write_free(&C.Archive) int |
| 21 | fn C.archive_read_extract2(&C.Archive, &C.Archive_entry, &C.Archive) int |
| 22 | fn C.archive_errno(&C.Archive) int |
| 23 | fn C.getxattr(&char, &char, voidptr, uint) i64 |
| 24 | fn C.setxattr(&char, &char, voidptr, uint, int) int |
| 25 | |
| 26 | fn extraction_fixture_time(path string) { |
| 27 | assert C.v_alpm_extract_times(-1, unsafe { &char(path.str) }, true, 12345, 0, true, 12345, 0, 12345) == 0 |
| 28 | } |
| 29 | |
| 30 | fn extraction_pax_field(name string, value string) string { |
| 31 | payload := '${name}=${value}\n' |
| 32 | mut length := payload.len + 2 |
| 33 | for { |
| 34 | actual := payload.len + length.str().len + 1 |
| 35 | if actual == length { return '${length} ${payload}' } |
| 36 | length = actual |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn extraction_cpio_entry(name string, text string, inode u64, links u64) []u8 { |
| 41 | mut header := '070701' |
| 42 | for field in [inode, u64(0o100644), u64(C.geteuid()), u64(C.getegid()), links, u64(12345), |
| 43 | u64(text.len), u64(0), u64(0), u64(0), u64(0), u64(name.len + 1), u64(0)] { |
| 44 | digits := strconv.format_uint(field, 16) |
| 45 | header += '0'.repeat(8 - digits.len) + digits |
| 46 | } |
| 47 | mut bytes := (header + name + '\x00').bytes() |
| 48 | bytes << []u8{len: (4 - bytes.len % 4) % 4} |
| 49 | bytes << text.bytes() |
| 50 | bytes << []u8{len: (4 - text.len % 4) % 4} |
| 51 | return bytes |
| 52 | } |
| 53 | |
| 54 | fn extraction_reference(path string, archive string) ! { |
| 55 | mut reader := open_archive(archive)! |
| 56 | defer { reader.close() or { eprintln(err) } } |
| 57 | for { |
| 58 | record := reader.next_record()! |
| 59 | if record.eof { break } |
| 60 | destination := os.join_path(path, record.name) |
| 61 | // llstat in util-common.c strips trailing slashes before lstat. |
| 62 | if info := os.lstat(destination.trim_right('/')) { |
| 63 | if info.mode & 0o170000 == 0o040000 { |
| 64 | if record.mode & 0o170000 != 0o040000 { |
| 65 | return error('cannot overwrite directory with file') |
| 66 | } |
| 67 | reader.skip_data()! |
| 68 | continue |
| 69 | } |
| 70 | } |
| 71 | entry := C.archive_entry_clone(record.entry) |
| 72 | assert entry != unsafe { nil } |
| 73 | C.archive_entry_set_pathname(entry, unsafe { &char(destination.str) }) |
| 74 | link := C.archive_entry_hardlink(entry) |
| 75 | if link != unsafe { nil } { |
| 76 | source := os.join_path(path, unsafe { cstring_to_vstring(link) }) |
| 77 | C.archive_entry_set_hardlink(entry, unsafe { &char(source.str) }) |
| 78 | } |
| 79 | writer := C.archive_write_disk_new() |
| 80 | assert writer != unsafe { nil } |
| 81 | assert C.archive_write_disk_set_options(writer, C.ARCHIVE_EXTRACT_OWNER | C.ARCHIVE_EXTRACT_PERM | C.ARCHIVE_EXTRACT_TIME | C.ARCHIVE_EXTRACT_UNLINK | C.ARCHIVE_EXTRACT_XATTR | C.ARCHIVE_EXTRACT_SECURE_SYMLINKS) == C.ARCHIVE_OK |
| 82 | status := C.archive_read_extract2(reader.handle, entry, writer) |
| 83 | failed := status != C.ARCHIVE_OK && (status != C.ARCHIVE_WARN || C.archive_errno(reader.handle) == C.ENOSPC) |
| 84 | problem := reader.archive_error('reference extraction') |
| 85 | assert C.archive_write_free(writer) == C.ARCHIVE_OK |
| 86 | C.archive_entry_free(entry) |
| 87 | if failed { return problem } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn extraction_snapshot(base string, relative string) ![]string { |
| 92 | mut names := os.ls(os.join_path(base, relative))! |
| 93 | names.sort() |
| 94 | mut out := []string{} |
| 95 | for name in names { |
| 96 | path := if relative == '' { name } else { relative + '/' + name } |
| 97 | full := os.join_path(base, path) |
| 98 | info := os.lstat(full)! |
| 99 | out << '${path}:${info.mode}:${info.uid}:${info.gid}' |
| 100 | match info.mode & 0o170000 { |
| 101 | 0o040000 { out << extraction_snapshot(base, path)! } |
| 102 | 0o100000 { |
| 103 | digest := compute_sha256sum(full) or { return error('cannot hash ${full}') } |
| 104 | out << '${info.size}:${info.mtime}:${info.nlink}:${digest}' |
| 105 | } |
| 106 | 0o120000 { out << '${info.mtime}:${os.readlink(full)!}' } |
| 107 | else { out << '${info.mtime}:${info.rdev}' } |
| 108 | } |
| 109 | } |
| 110 | return out |
| 111 | } |
| 112 | |
| 113 | fn extraction_native(mut handle Handle, archive string) ! { |
| 114 | mut root := handle.open_root_filesystem()! |
| 115 | defer { root.close() } |
| 116 | mut reader := open_archive(archive)! |
| 117 | defer { reader.close() or { eprintln(err) } } |
| 118 | for { |
| 119 | record := reader.next_record()! |
| 120 | if record.eof { break } |
| 121 | handle.extract_payload_record(mut root, mut reader, record, record.name)! |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn test_native_extraction_matches_source_flags_for_files_links_modes_and_times() ! { |
| 126 | base := os.join_path(os.vtmp_dir(), 'native-extraction-parity-${os.getpid()}') |
| 127 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 128 | for side in ['native', 'reference'] { |
| 129 | os.mkdir_all(os.join_path(base, side, 'existing'))! |
| 130 | os.chmod(os.join_path(base, side, 'existing'), 0o700)! |
| 131 | os.write_file(os.join_path(base, side, 'replace'), 'old')! |
| 132 | os.symlink('/outside-target', os.join_path(base, side, 'leaf-link'))! |
| 133 | os.write_file(os.join_path(base, side, 'file-to-dir'), 'old')! |
| 134 | os.symlink('existing', os.join_path(base, side, 'link-to-dir'))! |
| 135 | } |
| 136 | os.mkdir_all(os.join_path(base, 'db'))! |
| 137 | uid := u32(C.geteuid()) |
| 138 | gid := u32(C.getegid()) |
| 139 | entries := [ |
| 140 | archivefixtures.Entry{ name: 'usr/', directory: true, mode: 0o755, uid: uid, gid: gid }, |
| 141 | archivefixtures.Entry{ name: 'usr/bin/', directory: true, mode: 0o750, uid: uid, gid: gid }, |
| 142 | archivefixtures.Entry{ name: 'usr/bin/app', text: 'payload', mode: 0o4755, uid: uid, gid: gid, mtime: 123456 }, |
| 143 | archivefixtures.Entry{ name: 'usr/bin/hard', hardlink: 'usr/bin/app' }, |
| 144 | archivefixtures.Entry{ name: 'usr/bin/link', symlink: 'app', uid: uid, gid: gid, mtime: 234567 }, |
| 145 | archivefixtures.Entry{ name: 'absolute-link', symlink: '/usr/bin/app', uid: uid, gid: gid }, |
| 146 | archivefixtures.Entry{ name: 'existing/', directory: true, mode: 0o755 }, |
| 147 | archivefixtures.Entry{ name: 'file-to-dir/', directory: true, uid: uid, gid: gid }, |
| 148 | archivefixtures.Entry{ name: 'file-to-dir/child', text: 'new', uid: uid, gid: gid }, |
| 149 | archivefixtures.Entry{ name: 'link-to-dir/', directory: true, uid: uid, gid: gid }, |
| 150 | archivefixtures.Entry{ name: 'implicit/parents/file', text: 'implicit', uid: uid, gid: gid }, |
| 151 | archivefixtures.Entry{ name: 'replace', text: 'new', uid: uid, gid: gid }, |
| 152 | archivefixtures.Entry{ name: 'bad-uid', text: 'owner mismatch', mode: 0o6755, uid: 60001, gid: gid }, |
| 153 | archivefixtures.Entry{ name: 'bad-gid', text: 'group mismatch', mode: 0o6755, uid: uid, gid: 60001 }, |
| 154 | archivefixtures.Entry{ name: 'leaf-link', text: 'regular', uid: uid, gid: gid }, |
| 155 | archivefixtures.Entry{ name: 'fifo', typeflag: `6`, mode: 0o640, uid: uid, gid: gid, mtime: 345678 }, |
| 156 | ] |
| 157 | archive := os.join_path(base, 'payload.tar') |
| 158 | archivefixtures.write(archive, entries, false)! |
| 159 | mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! |
| 160 | defer { handle.release() or { eprintln(err) } } |
| 161 | cwd := os.getwd() |
| 162 | mask := C.umask(0o077) |
| 163 | defer { C.umask(mask) } |
| 164 | extraction_reference(os.join_path(base, 'reference'), archive)! |
| 165 | extraction_native(mut handle, archive)! |
| 166 | assert os.getwd() == cwd |
| 167 | assert C.umask(0o077) == 0o077 |
| 168 | native_snapshot := extraction_snapshot(os.join_path(base, 'native'), '')! |
| 169 | reference_snapshot := extraction_snapshot(os.join_path(base, 'reference'), '')! |
| 170 | assert native_snapshot == reference_snapshot, 'native: ${native_snapshot}\nreference: ${reference_snapshot}' |
| 171 | first := os.stat(os.join_path(base, 'native', 'usr/bin/app'))! |
| 172 | second := os.stat(os.join_path(base, 'native', 'usr/bin/hard'))! |
| 173 | assert first.inode == second.inode |
| 174 | assert os.stat(os.join_path(base, 'native', 'existing'))!.mode & 0o777 == 0o700 |
| 175 | } |
| 176 | |
| 177 | fn test_extraction_stays_rooted_and_replaces_shared_or_linked_leaves() ! { |
| 178 | base := os.join_path(os.vtmp_dir(), 'native-extraction-root-${os.getpid()}') |
| 179 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 180 | for path in ['root/a', 'root/b', 'db', 'outside'] { os.mkdir_all(os.join_path(base, path))! } |
| 181 | outside := os.join_path(base, 'outside', 'file') |
| 182 | os.write_file(outside, 'outside')! |
| 183 | os.link(outside, os.join_path(base, 'root', 'shared'))! |
| 184 | os.symlink('/a', os.join_path(base, 'root', 'absolute'))! |
| 185 | os.symlink('a', os.join_path(base, 'root', 'relative'))! |
| 186 | os.symlink(os.join_path(base, 'outside'), os.join_path(base, 'root', 'escape'))! |
| 187 | archive := os.join_path(base, 'payload.tar') |
| 188 | archivefixtures.write(archive, [ |
| 189 | archivefixtures.Entry{ name: 'absolute/file', text: 'inside' }, |
| 190 | archivefixtures.Entry{ name: 'shared', text: 'new inode' }, |
| 191 | archivefixtures.Entry{ name: 'escape/file', text: 'rooted' }, |
| 192 | ], false)! |
| 193 | mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! |
| 194 | defer { handle.release() or { eprintln(err) } } |
| 195 | extraction_native(mut handle, archive)! |
| 196 | assert os.read_file(outside)! == 'outside' |
| 197 | assert os.read_file(os.join_path(base, 'root', 'absolute', 'file'))! == 'inside' |
| 198 | assert !os.is_link(os.join_path(base, 'root', 'absolute')) |
| 199 | assert !os.exists(os.join_path(base, 'root', 'a', 'file')) |
| 200 | assert os.read_file(os.join_path(base, 'root', 'shared'))! == 'new inode' |
| 201 | mut root := handle.open_root_filesystem()! |
| 202 | defer { root.close() } |
| 203 | assert root.md5sum('escape/file')! != root.md5sum('absolute/file')! |
| 204 | archivefixtures.write(archive, [archivefixtures.Entry{ name: 'relative/pinned', text: 'pinned' }], false)! |
| 205 | mut reader := open_archive(archive)! |
| 206 | defer { reader.close() or { eprintln(err) } } |
| 207 | record := reader.next_record()! |
| 208 | mut destination := root.resolve('relative/pinned', false)! |
| 209 | defer { destination.close() } |
| 210 | os.rm(os.join_path(base, 'root', 'relative'))! |
| 211 | os.symlink('b', os.join_path(base, 'root', 'relative'))! |
| 212 | handle.extract_archive_record(mut root, mut reader, record, &destination, record.mode)! |
| 213 | assert os.read_file(os.join_path(base, 'root', 'a', 'pinned'))! == 'pinned' |
| 214 | assert !os.exists(os.join_path(base, 'root', 'b', 'pinned')) |
| 215 | } |
| 216 | |
| 217 | fn test_extraction_hardlink_source_symlink_policy_matches_reference() ! { |
| 218 | base := os.join_path(os.vtmp_dir(), 'native-extraction-hardlinks-${os.getpid()}') |
| 219 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 220 | for scenario in ['parent', 'leaf', 'missing', 'self'] { |
| 221 | for side in ['reference', 'native'] { |
| 222 | os.mkdir_all(os.join_path(base, scenario, side, 'target'))! |
| 223 | os.write_file(os.join_path(base, scenario, side, 'target/file'), 'original')! |
| 224 | os.symlink('target', os.join_path(base, scenario, side, 'alias'))! |
| 225 | os.symlink('target/file', os.join_path(base, scenario, side, 'leaf'))! |
| 226 | for path in ['target/file', 'alias', 'leaf'] { |
| 227 | extraction_fixture_time(os.join_path(base, scenario, side, path)) |
| 228 | } |
| 229 | } |
| 230 | os.mkdir_all(os.join_path(base, scenario, 'db'))! |
| 231 | archive := os.join_path(base, scenario, 'payload.tar') |
| 232 | source := match scenario { |
| 233 | 'parent' { 'alias/file' } |
| 234 | 'leaf' { 'leaf' } |
| 235 | 'self' { 'new' } |
| 236 | else { 'missing' } |
| 237 | } |
| 238 | archivefixtures.write(archive, [archivefixtures.Entry{ name: 'new', hardlink: source }], false)! |
| 239 | mut reference_ok := true |
| 240 | extraction_reference(os.join_path(base, scenario, 'reference'), archive) or { reference_ok = false } |
| 241 | mut handle := initialize(os.join_path(base, scenario, 'native'), os.join_path(base, scenario, 'db'))! |
| 242 | mut native_ok := true |
| 243 | extraction_native(mut handle, archive) or { native_ok = false } |
| 244 | handle.release()! |
| 245 | if scenario in ['parent', 'missing', 'self'] { |
| 246 | // extract2 downgrades header failures to WARN, which add.c accepts. |
| 247 | assert reference_ok |
| 248 | assert !native_ok |
| 249 | assert !os.exists(os.join_path(base, scenario, 'native', 'new')) |
| 250 | assert os.read_file(os.join_path(base, scenario, 'native', 'target/file'))! == 'original' |
| 251 | continue |
| 252 | } |
| 253 | assert native_ok == reference_ok, scenario |
| 254 | native_snapshot := extraction_snapshot(os.join_path(base, scenario, 'native'), '')! |
| 255 | reference_snapshot := extraction_snapshot(os.join_path(base, scenario, 'reference'), '')! |
| 256 | assert native_snapshot == reference_snapshot, '${scenario}\nnative: ${native_snapshot}\nreference: ${reference_snapshot}' |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | fn test_extraction_rejects_escaping_paths_and_database_links_before_mutation() ! { |
| 261 | base := os.join_path(os.vtmp_dir(), 'native-extraction-invalid-${os.getpid()}') |
| 262 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 263 | os.mkdir_all(os.join_path(base, 'root'))! |
| 264 | os.mkdir_all(os.join_path(base, 'db'))! |
| 265 | mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! |
| 266 | defer { handle.release() or { eprintln(err) } } |
| 267 | mut root := handle.open_root_filesystem()! |
| 268 | defer { root.close() } |
| 269 | pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\n')! |
| 270 | handle.local.prepare_local_package(&pkg)! |
| 271 | archive := os.join_path(base, 'payload.tar') |
| 272 | for name in ['../outside', '/outside', '.', 'a/../../outside'] { |
| 273 | archivefixtures.write(archive, [archivefixtures.Entry{ name: name, text: 'bad' }], false)! |
| 274 | if _ := extraction_native(mut handle, archive) { |
| 275 | assert false, name |
| 276 | } |
| 277 | assert os.ls(os.join_path(base, 'root'))!.len == 0 |
| 278 | } |
| 279 | os.write_file(os.join_path(base, 'root', 'keep'), 'keep')! |
| 280 | for target in ['../outside', '/outside'] { |
| 281 | archivefixtures.write(archive, [archivefixtures.Entry{ name: 'keep', hardlink: target }], false)! |
| 282 | if _ := extraction_native(mut handle, archive) { |
| 283 | assert false, target |
| 284 | } |
| 285 | assert os.read_file(os.join_path(base, 'root', 'keep'))! == 'keep' |
| 286 | } |
| 287 | for entry in [archivefixtures.Entry{ name: '.INSTALL', symlink: '/outside' }, |
| 288 | archivefixtures.Entry{ name: '.INSTALL', hardlink: 'keep' }, |
| 289 | archivefixtures.Entry{ name: '.INSTALL', directory: true }] { |
| 290 | archivefixtures.write(archive, [entry], false)! |
| 291 | mut reader := open_archive(archive)! |
| 292 | record := reader.next_record()! |
| 293 | if _ := handle.extract_database_record(mut root, mut reader, record, &pkg) { |
| 294 | assert false |
| 295 | } |
| 296 | assert handle.errno() == .pkg_invalid |
| 297 | reader.close()! |
| 298 | assert os.ls(os.join_path(handle.local.path, 'app-1-1'))!.len == 0 |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | fn test_database_archive_members_are_binary_regular_records_with_fixed_permissions() ! { |
| 303 | base := os.join_path(os.vtmp_dir(), 'native-extraction-database-${os.getpid()}') |
| 304 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 305 | os.mkdir_all(os.join_path(base, 'root'))! |
| 306 | os.mkdir_all(os.join_path(base, 'db'))! |
| 307 | mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! |
| 308 | defer { handle.release() or { eprintln(err) } } |
| 309 | pkg := parse_pkginfo('pkgname = app\npkgver = 1-1\n')! |
| 310 | handle.local.prepare_local_package(&pkg)! |
| 311 | archive := os.join_path(base, 'payload.tar') |
| 312 | archivefixtures.write(archive, [ |
| 313 | archivefixtures.Entry{ name: '.INSTALL', text: 'script\x00bytes', mode: 0o777 }, |
| 314 | archivefixtures.Entry{ name: '.CHANGELOG', text: 'changes', mode: 0o600 }, |
| 315 | archivefixtures.Entry{ name: '.MTREE', text: 'binary\x00mtree', mode: 0o444 }, |
| 316 | archivefixtures.Entry{ name: '.PKGINFO', text: 'ignored' }, |
| 317 | archivefixtures.Entry{ name: '.future', text: 'ignored' }, |
| 318 | ], false)! |
| 319 | mut root := handle.open_root_filesystem()! |
| 320 | defer { root.close() } |
| 321 | mut reader := open_archive(archive)! |
| 322 | defer { reader.close() or { eprintln(err) } } |
| 323 | for { |
| 324 | record := reader.next_record()! |
| 325 | if record.eof { break } |
| 326 | handle.extract_database_record(mut root, mut reader, record, &pkg)! |
| 327 | } |
| 328 | directory := os.join_path(handle.local.path, 'app-1-1') |
| 329 | mut names := os.ls(directory)! |
| 330 | names.sort() |
| 331 | assert names == ['changelog', 'install', 'mtree'] |
| 332 | assert os.read_bytes(os.join_path(directory, 'install'))! == 'script\x00bytes'.bytes() |
| 333 | assert os.read_bytes(os.join_path(directory, 'mtree'))! == 'binary\x00mtree'.bytes() |
| 334 | for name in names { |
| 335 | assert os.stat(os.join_path(directory, name))!.mode & 0o7777 == 0o644 |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | fn test_native_extraction_preserves_real_sparse_data_and_extended_attributes() ! { |
| 340 | base := os.join_path(os.vtmp_dir(), 'native-extraction-sparse-${os.getpid()}') |
| 341 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 342 | for path in ['source', 'native', 'reference', 'db'] { os.mkdir_all(os.join_path(base, path))! } |
| 343 | file := os.join_path(base, 'source', 'sparse') |
| 344 | fd := C.open(unsafe { &char(file.str) }, C.O_WRONLY | C.O_CREAT | C.O_EXCL | C.O_CLOEXEC, 0o640) |
| 345 | assert fd >= 0 |
| 346 | assert C.v_alpm_extract_write(fd, c'head', 4, 0) == 4 |
| 347 | assert C.v_alpm_extract_write(fd, c'tail', 4, 8 * 1024 * 1024) == 4 |
| 348 | assert C.close(fd) == 0 |
| 349 | assert C.setxattr(unsafe { &char(file.str) }, c'user.alpm-fixture', c'attribute', 9, 0) == 0 |
| 350 | os.chmod(file, 0o440)! |
| 351 | archive := os.join_path(base, 'sparse.tar') |
| 352 | signaturefixtures.command('tar', ['--format=pax', '--sparse', '--xattrs', '-cf', archive, '-C', |
| 353 | os.join_path(base, 'source'), 'sparse'])! |
| 354 | mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! |
| 355 | defer { handle.release() or { eprintln(err) } } |
| 356 | extraction_reference(os.join_path(base, 'reference'), archive)! |
| 357 | extraction_native(mut handle, archive)! |
| 358 | assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! |
| 359 | result := os.join_path(base, 'native', 'sparse') |
| 360 | mut attribute := []u8{len: 32} |
| 361 | count := C.getxattr(unsafe { &char(result.str) }, c'user.alpm-fixture', attribute.data, uint(attribute.len)) |
| 362 | assert count == 9 |
| 363 | assert attribute[..int(count)].bytestr() == 'attribute' |
| 364 | // the archive remains small and the sparse reader supplies hole offsets. |
| 365 | assert os.file_size(archive) < 65536 |
| 366 | mut actual := []i64{len: 5} |
| 367 | mut expected := []i64{len: 5} |
| 368 | assert C.v_alpm_extraction_probe(unsafe { &char(result.str) }, actual.data) == 0 |
| 369 | reference := os.join_path(base, 'reference', 'sparse') |
| 370 | assert C.v_alpm_extraction_probe(unsafe { &char(reference.str) }, expected.data) == 0 |
| 371 | assert actual[2..4] == expected[2..4] |
| 372 | assert actual[4] * 512 < i64(os.file_size(result)) / 10 |
| 373 | } |
| 374 | |
| 375 | fn test_extraction_source_policy_for_intermediate_directory_links() ! { |
| 376 | base := os.join_path(os.vtmp_dir(), 'native-extraction-parent-links-${os.getpid()}') |
| 377 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 378 | for side in ['reference', 'native'] { |
| 379 | os.mkdir_all(os.join_path(base, side, 'target'))! |
| 380 | os.symlink('target', os.join_path(base, side, 'alias'))! |
| 381 | os.write_file(os.join_path(base, side, 'target', 'keep'), 'keep')! |
| 382 | extraction_fixture_time(os.join_path(base, side, 'target', 'keep')) |
| 383 | } |
| 384 | os.mkdir_all(os.join_path(base, 'db'))! |
| 385 | archive := os.join_path(base, 'payload.tar') |
| 386 | archivefixtures.write(archive, [archivefixtures.Entry{ name: 'alias/file', text: 'new' }], false)! |
| 387 | extraction_reference(os.join_path(base, 'reference'), archive)! |
| 388 | assert !os.is_link(os.join_path(base, 'reference', 'alias')) |
| 389 | assert os.read_file(os.join_path(base, 'reference', 'alias', 'file'))! == 'new' |
| 390 | assert !os.exists(os.join_path(base, 'reference', 'target', 'file')) |
| 391 | mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! |
| 392 | defer { handle.release() or { eprintln(err) } } |
| 393 | extraction_native(mut handle, archive)! |
| 394 | assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! |
| 395 | } |
| 396 | |
| 397 | fn test_extraction_reports_truncated_data_and_write_failures() ! { |
| 398 | base := os.join_path(os.vtmp_dir(), 'native-extraction-failure-${os.getpid()}') |
| 399 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 400 | for path in ['root', 'db'] { os.mkdir_all(os.join_path(base, path))! } |
| 401 | mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! |
| 402 | defer { handle.release() or { eprintln(err) } } |
| 403 | archive := os.join_path(base, 'payload.tar') |
| 404 | bytes := archivefixtures.tar_bytes([archivefixtures.Entry{ name: 'file', text: 'data'.repeat(4096) }]) |
| 405 | os.write_file_array(archive, bytes[..800])! |
| 406 | mut rejected := false |
| 407 | extraction_native(mut handle, archive) or { rejected = err.code() == int(Errno.libarchive) } |
| 408 | assert rejected |
| 409 | os.write_file_array(archive, bytes)! |
| 410 | child := C.fork() |
| 411 | assert child >= 0 |
| 412 | if child == 0 { |
| 413 | if C.v_alpm_limit_database_write() != 0 { C._exit(2) } |
| 414 | extraction_native(mut handle, archive) or { |
| 415 | if err.code() != int(Errno.libarchive) { C._exit(3) } |
| 416 | C._exit(0) |
| 417 | return |
| 418 | } |
| 419 | C._exit(4) |
| 420 | } |
| 421 | mut status := i32(0) |
| 422 | for { |
| 423 | if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } |
| 424 | if C.errno != C.EINTR { return os.last_error() } |
| 425 | } |
| 426 | assert C.v_alpm_exit_status(status) == 0 |
| 427 | assert os.file_size(os.join_path(base, 'root', 'file')) <= 128 |
| 428 | } |
| 429 | |
| 430 | fn test_extraction_restores_fractional_times_and_skips_acl_xattrs() ! { |
| 431 | base := os.join_path(os.vtmp_dir(), 'native-extraction-pax-${os.getpid()}') |
| 432 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 433 | for path in ['native', 'reference', 'db'] { os.mkdir_all(os.join_path(base, path))! } |
| 434 | archive := os.join_path(base, 'payload.tar') |
| 435 | fields := extraction_pax_field('atime', '12345.123456789') + extraction_pax_field('mtime', '23456.987654321') + extraction_pax_field('SCHILY.xattr.user.alpm-fixture', 'value') + extraction_pax_field('SCHILY.xattr.system.posix_acl_access', 'ignored') |
| 436 | archivefixtures.write(archive, [ |
| 437 | archivefixtures.Entry{ name: 'PaxHeaders/file', typeflag: `x`, text: fields }, |
| 438 | archivefixtures.Entry{ name: 'file', text: 'contents', mode: 0o400, uid: u32(C.geteuid()), gid: u32(C.getegid()) }, |
| 439 | ], false)! |
| 440 | extraction_reference(os.join_path(base, 'reference'), archive)! |
| 441 | mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! |
| 442 | defer { handle.release() or { eprintln(err) } } |
| 443 | extraction_native(mut handle, archive)! |
| 444 | mut actual := []i64{len: 5} |
| 445 | mut expected := []i64{len: 5} |
| 446 | file := os.join_path(base, 'native', 'file') |
| 447 | reference := os.join_path(base, 'reference', 'file') |
| 448 | assert C.v_alpm_extraction_probe(unsafe { &char(file.str) }, actual.data) == 0 |
| 449 | assert C.v_alpm_extraction_probe(unsafe { &char(reference.str) }, expected.data) == 0 |
| 450 | assert actual[..4] == expected[..4] |
| 451 | assert actual[..4] == [i64(12345), 123456789, 23456, 987654321] |
| 452 | mut attribute := []u8{len: 32} |
| 453 | assert C.getxattr(unsafe { &char(file.str) }, c'user.alpm-fixture', attribute.data, uint(attribute.len)) == 5 |
| 454 | assert attribute[..5].bytestr() == 'value' |
| 455 | assert C.getxattr(unsafe { &char(file.str) }, c'system.posix_acl_access', attribute.data, uint(attribute.len)) < 0 |
| 456 | assert handle.diagnostics.len == 0, handle.diagnostics.str() |
| 457 | } |
| 458 | |
| 459 | fn test_extraction_device_nodes_match_reference_in_private_user_namespace() ! { |
| 460 | if C.geteuid() != 0 { return } |
| 461 | base := os.join_path(os.vtmp_dir(), 'native-extraction-nodes-${os.getpid()}') |
| 462 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 463 | for path in ['reference', 'native', 'db'] { os.mkdir_all(os.join_path(base, path))! } |
| 464 | archive := os.join_path(base, 'payload.tar') |
| 465 | archivefixtures.write(archive, [ |
| 466 | archivefixtures.Entry{ name: 'char', typeflag: `3`, mode: 0o640, mtime: 12345 }, |
| 467 | archivefixtures.Entry{ name: 'block', typeflag: `4`, mode: 0o600, mtime: 23456 }, |
| 468 | ], false)! |
| 469 | extraction_reference(os.join_path(base, 'reference'), archive)! |
| 470 | mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! |
| 471 | defer { handle.release() or { eprintln(err) } } |
| 472 | mut denied := false |
| 473 | extraction_native(mut handle, archive) or { |
| 474 | assert err.code() in [int(C.EPERM), int(C.EACCES)], err.msg() |
| 475 | denied = true |
| 476 | } |
| 477 | assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! |
| 478 | if denied { |
| 479 | // mapped root can still lack mknod under the host's capability/device policy. |
| 480 | assert !os.exists(os.join_path(base, 'native', 'block')) |
| 481 | return |
| 482 | } |
| 483 | assert os.lstat(os.join_path(base, 'native', 'char'))!.mode & 0o170000 == 0o020000 |
| 484 | assert os.lstat(os.join_path(base, 'native', 'block'))!.mode & 0o170000 == 0o060000 |
| 485 | } |
| 486 | |
| 487 | fn test_extraction_accepts_a_package_root_on_descriptor_zero() ! { |
| 488 | base := os.join_path(os.vtmp_dir(), 'native-extraction-fd-zero-${os.getpid()}') |
| 489 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 490 | for path in ['root', 'db'] { os.mkdir_all(os.join_path(base, path))! } |
| 491 | archive := os.join_path(base, 'payload.tar') |
| 492 | archivefixtures.write(archive, [archivefixtures.Entry{ name: 'file', text: 'contents' }], false)! |
| 493 | mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))! |
| 494 | defer { handle.release() or { eprintln(err) } } |
| 495 | child := C.fork() |
| 496 | assert child >= 0 |
| 497 | if child == 0 { |
| 498 | if C.close(0) != 0 { C._exit(2) } |
| 499 | mut root := handle.open_root_filesystem() or { |
| 500 | C._exit(3) |
| 501 | return |
| 502 | } |
| 503 | if root.fd != 0 { C._exit(4) } |
| 504 | mut reader := open_archive(archive) or { |
| 505 | C._exit(5) |
| 506 | return |
| 507 | } |
| 508 | record := reader.next_record() or { |
| 509 | C._exit(6) |
| 510 | return |
| 511 | } |
| 512 | handle.extract_payload_record(mut root, mut reader, record, record.name) or { C._exit(7) } |
| 513 | reader.close() or { C._exit(8) } |
| 514 | root.close() |
| 515 | C._exit(0) |
| 516 | } |
| 517 | mut status := i32(0) |
| 518 | for { |
| 519 | if C.v_alpm_wait_child(child, &status, 0) >= 0 { break } |
| 520 | if C.errno != C.EINTR { return os.last_error() } |
| 521 | } |
| 522 | assert C.v_alpm_exit_status(status) == 0 |
| 523 | assert os.read_file(os.join_path(base, 'root', 'file'))! == 'contents' |
| 524 | } |
| 525 | |
| 526 | fn test_extraction_data_bearing_cpio_hardlink_matches_reference_and_truncates() ! { |
| 527 | base := os.join_path(os.vtmp_dir(), 'native-extraction-cpio-${os.getpid()}') |
| 528 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 529 | for path in ['native', 'reference', 'db'] { os.mkdir_all(os.join_path(base, path))! } |
| 530 | mut data := extraction_cpio_entry('first', 'old-contents-that-must-disappear', 1, 2) |
| 531 | data << extraction_cpio_entry('second', 'new', 1, 2) |
| 532 | data << extraction_cpio_entry('TRAILER!!!', '', 0, 1) |
| 533 | archive := os.join_path(base, 'payload.cpio') |
| 534 | os.write_file_array(archive, data)! |
| 535 | mut reader := open_archive(archive)! |
| 536 | assert reader.next_record()!.name == 'first' |
| 537 | reader.skip_data()! |
| 538 | record := reader.next_record()! |
| 539 | assert record.name == 'second' |
| 540 | assert record.size == 3 |
| 541 | assert C.archive_entry_hardlink(record.entry) != unsafe { nil } |
| 542 | reader.close()! |
| 543 | extraction_reference(os.join_path(base, 'reference'), archive)! |
| 544 | mut handle := initialize(os.join_path(base, 'native'), os.join_path(base, 'db'))! |
| 545 | defer { handle.release() or { eprintln(err) } } |
| 546 | extraction_native(mut handle, archive)! |
| 547 | assert extraction_snapshot(os.join_path(base, 'native'), '')! == extraction_snapshot(os.join_path(base, 'reference'), '')! |
| 548 | assert os.read_file(os.join_path(base, 'native', 'first'))! == 'new' |
| 549 | assert os.read_file(os.join_path(base, 'native', 'second'))! == 'new' |
| 550 | assert os.stat(os.join_path(base, 'native', 'first'))!.inode == os.stat(os.join_path(base, 'native', 'second'))!.inode |
| 551 | } |
| 552 | |