// add.c's extraction policy, with directory-relative writes instead of process cwd. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os import time #include "@DIR/native_extract.h" fn C.archive_entry_hardlink(&C.Archive_entry) &char fn C.archive_entry_atime(&C.Archive_entry) i64 fn C.archive_entry_atime_nsec(&C.Archive_entry) i64 fn C.archive_entry_atime_is_set(&C.Archive_entry) int fn C.archive_entry_mtime_nsec(&C.Archive_entry) i64 fn C.archive_entry_mtime_is_set(&C.Archive_entry) int fn C.archive_entry_rdevmajor(&C.Archive_entry) u64 fn C.archive_entry_rdevminor(&C.Archive_entry) u64 fn C.archive_entry_xattr_reset(&C.Archive_entry) int fn C.archive_entry_xattr_next(&C.Archive_entry, &&char, &voidptr, &uint) int fn C.archive_read_data_block(&C.Archive, &voidptr, &uint, &i64) int fn C.symlinkat(&char, i32, &char) i32 fn C.linkat(i32, &char, i32, &char, i32) i32 fn C.v_alpm_extract_times(i32, &char, bool, i64, i64, bool, i64, i64, i64) i32 fn C.v_alpm_extract_node(i32, &char, u32, u64, u64) i32 fn C.v_alpm_extract_write(i32, voidptr, uint, i64) i64 fn C.v_alpm_extract_chown(i32, &char, u32, u32) i32 fn C.v_alpm_extract_chmod(i32, &char, u32) i32 fn C.v_alpm_extract_xattr(i32, &char, &char, voidptr, uint) i32 fn C.v_alpm_extract_owner(i32, &char, &u32, &u32) i32 fn (path &RootPath) descriptor_path() string { return '/proc/self/fd/${path.fd}/${path.name}' } fn (mut handle Handle) extract_regular_data(mut reader ArchiveReader, record ArchiveRecord, fd i32) ! { if record.size < 0 { return handle.fail(.pkg_invalid, 'negative archive file size') } for { mut data := voidptr(unsafe { nil }) mut size := uint(0) mut offset := i64(0) status := C.archive_read_data_block(reader.handle, &data, &size, &offset) if status == C.ARCHIVE_EOF { break } if status != C.ARCHIVE_OK { return handle.fail(.libarchive, reader.archive_error('read ${record.name}').msg()) } if offset < 0 || offset > record.size || u64(size) > u64(record.size - offset) { return handle.fail(.pkg_invalid, 'archive data exceeds declared size: ${record.name}') } mut written := uint(0) for written < size { count := C.v_alpm_extract_write(fd, unsafe { &u8(data) + written }, size - written, offset + i64(written)) if count < 0 && C.errno == C.EINTR { continue } if count <= 0 { return handle.fail(.libarchive, 'write ${record.name}: ${os.last_error().msg()}') } written += uint(count) } } if C.ftruncate(fd, u64(record.size)) != 0 { return handle.fail(.libarchive, os.last_error().msg()) } } fn (mut handle Handle) extract_archive_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, destination &RootPath, mode u32) !ExtractionWarning { started_at := time.now().unix() typebits := mode & 0o170000 status := destination.stat()! if status.present { if status.info.mode & 0o170000 == 0o040000 { if typebits != 0o040000 { return handle.fail(.libarchive, 'cannot overwrite a directory: ${record.name}') } if status.info.mode & 0o7777 != mode & 0o7777 { handle.diagnostics << 'directory permissions differ on ${record.name}' } reader.skip_data()! return ExtractionWarning{} } if C.unlinkat(destination.fd, unsafe { &char(destination.name.str) }, 0) != 0 { return os.last_error() } } link := C.archive_entry_hardlink(record.entry) if link != unsafe { nil } { mut source := root.resolve_path(unsafe { cstring_to_vstring(link) }, false, .hardlink_source)! defer { source.close() } if record.size > 0 && source.stat()!.info.mode & 0o170000 != 0o100000 { return handle.fail(.pkg_invalid, 'hardlink data requires a regular source: ${record.name}') } if C.linkat(source.fd, unsafe { &char(source.name.str) }, destination.fd, unsafe { &char(destination.name.str) }, 0) != 0 { return os.last_error() } if record.size == 0 { reader.skip_data()! return ExtractionWarning{} } } mut fd := i32(-1) defer { handle.close_process_descriptor(fd) } match typebits { 0o100000 { if link == unsafe { nil } { // keep new files private until their data and ownership have been restored. fd = C.openat(destination.fd, unsafe { &char(destination.name.str) }, C.O_WRONLY | C.O_CLOEXEC | C.O_NOFOLLOW | C.O_CREAT | C.O_EXCL, 0o600) } else { // pin and check the inode before opening a data-bearing hardlink for I/O. pinned := C.openat(destination.fd, unsafe { &char(destination.name.str) }, C.O_PATH | C.O_NOFOLLOW | C.O_CLOEXEC) if pinned < 0 { return os.last_error() } defer { handle.close_process_descriptor(pinned) } mut size := i64(0) mut filemode := u32(0) mut links := u64(0) if C.v_alpm_download_stat(pinned, &size, &filemode, &links) != 0 { return os.last_error() } if filemode & 0o170000 != 0o100000 { return handle.fail(.pkg_invalid, 'hardlink data requires a regular file') } anchor := '/proc/self/fd/${pinned}' fd = C.open(unsafe { &char(anchor.str) }, C.O_WRONLY | C.O_TRUNC | C.O_CLOEXEC) } if fd < 0 { return os.last_error() } if link == unsafe { nil } && C.fchmod(fd, 0o600) != 0 { return os.last_error() } handle.extract_regular_data(mut reader, record, fd)! } 0o040000 { if C.mkdirat(destination.fd, unsafe { &char(destination.name.str) }, 0o700) != 0 { return os.last_error() } anchor := destination.descriptor_path() if C.v_alpm_extract_chmod(-1, unsafe { &char(anchor.str) }, 0o700) != 0 { return os.last_error() } fd = C.openat(destination.fd, unsafe { &char(destination.name.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_NOFOLLOW | C.O_CLOEXEC) if fd < 0 { return os.last_error() } reader.skip_data()! } 0o120000 { target := C.archive_entry_symlink(record.entry) if target == unsafe { nil } { return handle.fail(.pkg_invalid, 'archive symlink has no target') } if C.symlinkat(target, destination.fd, unsafe { &char(destination.name.str) }) != 0 { return os.last_error() } reader.skip_data()! } 0o010000, 0o020000, 0o060000 { if C.v_alpm_extract_node(destination.fd, unsafe { &char(destination.name.str) }, mode, C.archive_entry_rdevmajor(record.entry), C.archive_entry_rdevminor(record.entry)) != 0 { return os.last_error() } reader.skip_data()! } else { return handle.fail(.pkg_invalid, 'unsupported archive file type: ${record.name}') } } return handle.extract_record_metadata(record, destination, fd, mode, started_at) } fn (mut handle Handle) extract_payload_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, path string) !ExtractionWarning { // validate link targets before creating any parent or removing the destination. link := C.archive_entry_hardlink(record.entry) if link != unsafe { nil } { validate_package_path(unsafe { cstring_to_vstring(link) })! } if record.mode & 0o170000 !in [u32(0o100000), 0o040000, 0o120000, 0o010000, 0o020000, 0o060000] && !(link != unsafe { nil } && record.size == 0) { return handle.fail(.pkg_invalid, 'unsupported archive file type: ${record.name}') } if record.size < 0 { return handle.fail(.pkg_invalid, 'negative archive file size') } mut destination := root.resolve_path(path, false, .extract_parents)! defer { destination.close() } return handle.extract_archive_record(mut root, mut reader, record, &destination, record.mode) } fn (mut handle Handle) extract_database_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, pkg &Package) ! { name := match record.name { '.INSTALL' { 'install' } '.NSPKG-HOOK' { 'native_hook' } '.CHANGELOG' { 'changelog' } '.MTREE' { 'mtree' } else { reader.skip_data()! return } } if record.mode & 0o170000 != 0o100000 || C.archive_entry_hardlink(record.entry) != unsafe { nil } { return handle.fail(.pkg_invalid, 'special database member is not a regular file: ${record.name}') } fd := handle.local.open_local_entry(pkg)! mut destination := RootPath{ owner: handle, fd: fd, name: name } defer { destination.close() } filename := handle.local.path + local_entry_name(pkg)! + '/' + name warning := handle.extract_archive_record(mut root, mut reader, record, &destination, 0o100644) or { handle.record_extraction_failure(filename, err) return err } warning.report(handle, filename) }