nspkg / engine / extraction.v
195 lines · 187 sloc · 8.1 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// add.c's extraction policy, with directory-relative writes instead of process cwd.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6import time
7
8#include "@DIR/native_extract.h"
9
10fn C.archive_entry_hardlink(&C.Archive_entry) &char
11fn C.archive_entry_atime(&C.Archive_entry) i64
12fn C.archive_entry_atime_nsec(&C.Archive_entry) i64
13fn C.archive_entry_atime_is_set(&C.Archive_entry) int
14fn C.archive_entry_mtime_nsec(&C.Archive_entry) i64
15fn C.archive_entry_mtime_is_set(&C.Archive_entry) int
16fn C.archive_entry_rdevmajor(&C.Archive_entry) u64
17fn C.archive_entry_rdevminor(&C.Archive_entry) u64
18fn C.archive_entry_xattr_reset(&C.Archive_entry) int
19fn C.archive_entry_xattr_next(&C.Archive_entry, &&char, &voidptr, &uint) int
20fn C.archive_read_data_block(&C.Archive, &voidptr, &uint, &i64) int
21fn C.symlinkat(&char, i32, &char) i32
22fn C.linkat(i32, &char, i32, &char, i32) i32
23fn C.v_alpm_extract_times(i32, &char, bool, i64, i64, bool, i64, i64, i64) i32
24fn C.v_alpm_extract_node(i32, &char, u32, u64, u64) i32
25fn C.v_alpm_extract_write(i32, voidptr, uint, i64) i64
26fn C.v_alpm_extract_chown(i32, &char, u32, u32) i32
27fn C.v_alpm_extract_chmod(i32, &char, u32) i32
28fn C.v_alpm_extract_xattr(i32, &char, &char, voidptr, uint) i32
29fn C.v_alpm_extract_owner(i32, &char, &u32, &u32) i32
30
31fn (path &RootPath) descriptor_path() string {
32 return '/proc/self/fd/${path.fd}/${path.name}'
33}
34
35fn (mut handle Handle) extract_regular_data(mut reader ArchiveReader, record ArchiveRecord, fd i32) ! {
36 if record.size < 0 { return handle.fail(.pkg_invalid, 'negative archive file size') }
37 for {
38 mut data := voidptr(unsafe { nil })
39 mut size := uint(0)
40 mut offset := i64(0)
41 status := C.archive_read_data_block(reader.handle, &data, &size, &offset)
42 if status == C.ARCHIVE_EOF { break }
43 if status != C.ARCHIVE_OK {
44 return handle.fail(.libarchive, reader.archive_error('read ${record.name}').msg())
45 }
46 if offset < 0 || offset > record.size || u64(size) > u64(record.size - offset) {
47 return handle.fail(.pkg_invalid, 'archive data exceeds declared size: ${record.name}')
48 }
49 mut written := uint(0)
50 for written < size {
51 count := C.v_alpm_extract_write(fd, unsafe { &u8(data) + written }, size - written, offset + i64(written))
52 if count < 0 && C.errno == C.EINTR { continue }
53 if count <= 0 {
54 return handle.fail(.libarchive, 'write ${record.name}: ${os.last_error().msg()}')
55 }
56 written += uint(count)
57 }
58 }
59 if C.ftruncate(fd, u64(record.size)) != 0 {
60 return handle.fail(.libarchive, os.last_error().msg())
61 }
62}
63
64fn (mut handle Handle) extract_archive_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, destination &RootPath, mode u32) !ExtractionWarning {
65 started_at := time.now().unix()
66 typebits := mode & 0o170000
67 status := destination.stat()!
68 if status.present {
69 if status.info.mode & 0o170000 == 0o040000 {
70 if typebits != 0o040000 {
71 return handle.fail(.libarchive, 'cannot overwrite a directory: ${record.name}')
72 }
73 if status.info.mode & 0o7777 != mode & 0o7777 {
74 handle.diagnostics << 'directory permissions differ on ${record.name}'
75 }
76 reader.skip_data()!
77 return ExtractionWarning{}
78 }
79 if C.unlinkat(destination.fd, unsafe { &char(destination.name.str) }, 0) != 0 {
80 return os.last_error()
81 }
82 }
83 link := C.archive_entry_hardlink(record.entry)
84 if link != unsafe { nil } {
85 mut source := root.resolve_path(unsafe { cstring_to_vstring(link) }, false, .hardlink_source)!
86 defer { source.close() }
87 if record.size > 0 && source.stat()!.info.mode & 0o170000 != 0o100000 {
88 return handle.fail(.pkg_invalid, 'hardlink data requires a regular source: ${record.name}')
89 }
90 if C.linkat(source.fd, unsafe { &char(source.name.str) }, destination.fd, unsafe { &char(destination.name.str) }, 0) != 0 {
91 return os.last_error()
92 }
93 if record.size == 0 {
94 reader.skip_data()!
95 return ExtractionWarning{}
96 }
97 }
98 mut fd := i32(-1)
99 defer { handle.close_process_descriptor(fd) }
100 match typebits {
101 0o100000 {
102 if link == unsafe { nil } {
103 // keep new files private until their data and ownership have been restored.
104 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)
105 } else {
106 // pin and check the inode before opening a data-bearing hardlink for I/O.
107 pinned := C.openat(destination.fd, unsafe { &char(destination.name.str) }, C.O_PATH | C.O_NOFOLLOW | C.O_CLOEXEC)
108 if pinned < 0 { return os.last_error() }
109 defer { handle.close_process_descriptor(pinned) }
110 mut size := i64(0)
111 mut filemode := u32(0)
112 mut links := u64(0)
113 if C.v_alpm_download_stat(pinned, &size, &filemode, &links) != 0 {
114 return os.last_error()
115 }
116 if filemode & 0o170000 != 0o100000 {
117 return handle.fail(.pkg_invalid, 'hardlink data requires a regular file')
118 }
119 anchor := '/proc/self/fd/${pinned}'
120 fd = C.open(unsafe { &char(anchor.str) }, C.O_WRONLY | C.O_TRUNC | C.O_CLOEXEC)
121 }
122 if fd < 0 { return os.last_error() }
123 if link == unsafe { nil } && C.fchmod(fd, 0o600) != 0 { return os.last_error() }
124 handle.extract_regular_data(mut reader, record, fd)!
125 }
126 0o040000 {
127 if C.mkdirat(destination.fd, unsafe { &char(destination.name.str) }, 0o700) != 0 {
128 return os.last_error()
129 }
130 anchor := destination.descriptor_path()
131 if C.v_alpm_extract_chmod(-1, unsafe { &char(anchor.str) }, 0o700) != 0 {
132 return os.last_error()
133 }
134 fd = C.openat(destination.fd, unsafe { &char(destination.name.str) }, C.O_RDONLY | C.O_DIRECTORY | C.O_NOFOLLOW | C.O_CLOEXEC)
135 if fd < 0 { return os.last_error() }
136 reader.skip_data()!
137 }
138 0o120000 {
139 target := C.archive_entry_symlink(record.entry)
140 if target == unsafe { nil } {
141 return handle.fail(.pkg_invalid, 'archive symlink has no target')
142 }
143 if C.symlinkat(target, destination.fd, unsafe { &char(destination.name.str) }) != 0 {
144 return os.last_error()
145 }
146 reader.skip_data()!
147 }
148 0o010000, 0o020000, 0o060000 {
149 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 {
150 return os.last_error()
151 }
152 reader.skip_data()!
153 }
154 else { return handle.fail(.pkg_invalid, 'unsupported archive file type: ${record.name}') }
155 }
156 return handle.extract_record_metadata(record, destination, fd, mode, started_at)
157}
158
159fn (mut handle Handle) extract_payload_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, path string) !ExtractionWarning {
160 // validate link targets before creating any parent or removing the destination.
161 link := C.archive_entry_hardlink(record.entry)
162 if link != unsafe { nil } { validate_package_path(unsafe { cstring_to_vstring(link) })! }
163 if record.mode & 0o170000 !in [u32(0o100000), 0o040000, 0o120000, 0o010000, 0o020000, 0o060000] && !(link != unsafe { nil } && record.size == 0) {
164 return handle.fail(.pkg_invalid, 'unsupported archive file type: ${record.name}')
165 }
166 if record.size < 0 { return handle.fail(.pkg_invalid, 'negative archive file size') }
167 mut destination := root.resolve_path(path, false, .extract_parents)!
168 defer { destination.close() }
169 return handle.extract_archive_record(mut root, mut reader, record, &destination, record.mode)
170}
171
172fn (mut handle Handle) extract_database_record(mut root RootFilesystem, mut reader ArchiveReader, record ArchiveRecord, pkg &Package) ! {
173 name := match record.name {
174 '.INSTALL' { 'install' }
175 '.NSPKG-HOOK' { 'native_hook' }
176 '.CHANGELOG' { 'changelog' }
177 '.MTREE' { 'mtree' }
178 else {
179 reader.skip_data()!
180 return
181 }
182 }
183 if record.mode & 0o170000 != 0o100000 || C.archive_entry_hardlink(record.entry) != unsafe { nil } {
184 return handle.fail(.pkg_invalid, 'special database member is not a regular file: ${record.name}')
185 }
186 fd := handle.local.open_local_entry(pkg)!
187 mut destination := RootPath{ owner: handle, fd: fd, name: name }
188 defer { destination.close() }
189 filename := handle.local.path + local_entry_name(pkg)! + '/' + name
190 warning := handle.extract_archive_record(mut root, mut reader, record, &destination, 0o100644) or {
191 handle.record_extraction_failure(filename, err)
192 return err
193 }
194 warning.report(handle, filename)
195}
196