]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8c1be37e | 2 | |
10c1b188 LP |
3 | #if HAVE_VALGRIND_MEMCHECK_H |
4 | #include <valgrind/memcheck.h> | |
5 | #endif | |
6 | ||
01234e1f YW |
7 | #include <linux/dm-ioctl.h> |
8 | #include <linux/loop.h> | |
19df770f | 9 | #include <sys/file.h> |
8c1be37e | 10 | #include <sys/mount.h> |
3b925504 LP |
11 | #include <sys/prctl.h> |
12 | #include <sys/wait.h> | |
f5ea63a5 | 13 | #include <sysexits.h> |
8c1be37e | 14 | |
c2fa92e7 LP |
15 | #if HAVE_OPENSSL |
16 | #include <openssl/err.h> | |
17 | #include <openssl/pem.h> | |
18 | #include <openssl/x509.h> | |
19 | #endif | |
20 | ||
3c1f2cee | 21 | #include "sd-device.h" |
dccca82b LP |
22 | #include "sd-id128.h" |
23 | ||
8c1be37e | 24 | #include "architecture.h" |
18b5886e | 25 | #include "ask-password-api.h" |
8c1be37e | 26 | #include "blkid-util.h" |
18c528e9 | 27 | #include "blockdev-util.h" |
f4351959 | 28 | #include "chase-symlinks.h" |
c2fa92e7 | 29 | #include "conf-files.h" |
28db6fbf | 30 | #include "constants.h" |
3b925504 | 31 | #include "copy.h" |
1e2f3230 | 32 | #include "cryptsetup-util.h" |
553e15f2 | 33 | #include "device-nodes.h" |
8437c059 | 34 | #include "device-util.h" |
f7725647 | 35 | #include "devnum-util.h" |
7718ac97 | 36 | #include "discover-image.h" |
8c1be37e | 37 | #include "dissect-image.h" |
a709a315 | 38 | #include "dm-util.h" |
686d13b9 | 39 | #include "env-file.h" |
88b3300f | 40 | #include "env-util.h" |
93f59701 | 41 | #include "extension-release.h" |
18b5886e | 42 | #include "fd-util.h" |
78ebe980 | 43 | #include "fileio.h" |
2eedfd2d | 44 | #include "fs-util.h" |
cf32c486 | 45 | #include "fsck-util.h" |
8c1be37e | 46 | #include "gpt.h" |
78ebe980 | 47 | #include "hexdecoct.h" |
e2054217 | 48 | #include "hostname-setup.h" |
3b925504 | 49 | #include "id128-util.h" |
593fe6c0 | 50 | #include "import-util.h" |
a4e0d617 | 51 | #include "io-util.h" |
35cd0ba5 | 52 | #include "mkdir-label.h" |
8c1be37e | 53 | #include "mount-util.h" |
e4de7287 | 54 | #include "mountpoint-util.h" |
6aa05ebd | 55 | #include "namespace-util.h" |
d8b4d14d | 56 | #include "nulstr-util.h" |
c2fa92e7 | 57 | #include "openssl-util.h" |
d58ad743 | 58 | #include "os-util.h" |
8c1be37e | 59 | #include "path-util.h" |
3b925504 LP |
60 | #include "process-util.h" |
61 | #include "raw-clone.h" | |
81939d9d | 62 | #include "resize-fs.h" |
3b925504 | 63 | #include "signal-util.h" |
8c1be37e | 64 | #include "stat-util.h" |
18b5886e | 65 | #include "stdio-util.h" |
8c1be37e LP |
66 | #include "string-table.h" |
67 | #include "string-util.h" | |
2eedfd2d | 68 | #include "strv.h" |
e4de7287 | 69 | #include "tmpfile-util.h" |
a8040b6d | 70 | #include "udev-util.h" |
2d3a5a73 | 71 | #include "user-util.h" |
41488e1f | 72 | #include "xattr-util.h" |
8c1be37e | 73 | |
28e2641a FF |
74 | /* how many times to wait for the device nodes to appear */ |
75 | #define N_DEVICE_NODE_LIST_ATTEMPTS 10 | |
76 | ||
d2c6e79d | 77 | int probe_filesystem_full(int fd, const char *path, char **ret_fstype) { |
7cc84b2c | 78 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
5238e957 | 79 | * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an |
7cc84b2c ZJS |
80 | * different error otherwise. */ |
81 | ||
349cc4a5 | 82 | #if HAVE_BLKID |
8e766630 | 83 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
d2c6e79d YW |
84 | _cleanup_free_ char *path_by_fd = NULL; |
85 | _cleanup_close_ int fd_close = -1; | |
18b5886e LP |
86 | const char *fstype; |
87 | int r; | |
88 | ||
d2c6e79d YW |
89 | assert(fd >= 0 || path); |
90 | assert(ret_fstype); | |
91 | ||
92 | if (fd < 0) { | |
93 | fd_close = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
94 | if (fd_close < 0) | |
95 | return -errno; | |
96 | ||
97 | fd = fd_close; | |
98 | } | |
99 | ||
100 | if (!path) { | |
101 | r = fd_get_path(fd, &path_by_fd); | |
102 | if (r < 0) | |
103 | return r; | |
104 | ||
105 | path = path_by_fd; | |
106 | } | |
107 | ||
108 | b = blkid_new_probe(); | |
18b5886e | 109 | if (!b) |
d2c6e79d YW |
110 | return -ENOMEM; |
111 | ||
112 | errno = 0; | |
113 | r = blkid_probe_set_device(b, fd, 0, 0); | |
114 | if (r != 0) | |
66855de7 | 115 | return errno_or_else(ENOMEM); |
18b5886e LP |
116 | |
117 | blkid_probe_enable_superblocks(b, 1); | |
118 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
119 | ||
120 | errno = 0; | |
121 | r = blkid_do_safeprobe(b); | |
1cefb9a6 | 122 | if (r == 1) |
18b5886e | 123 | goto not_found; |
58dfbfbd LP |
124 | if (r == -2) |
125 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
d2c6e79d | 126 | "Results ambiguous for partition %s", path); |
b382db9f | 127 | if (r != 0) |
d2c6e79d | 128 | return log_debug_errno(errno_or_else(EIO), "Failed to probe partition %s: %m", path); |
18b5886e LP |
129 | |
130 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
131 | ||
132 | if (fstype) { | |
133 | char *t; | |
134 | ||
d2c6e79d | 135 | log_debug("Probed fstype '%s' on partition %s.", fstype, path); |
1cefb9a6 | 136 | |
18b5886e LP |
137 | t = strdup(fstype); |
138 | if (!t) | |
139 | return -ENOMEM; | |
140 | ||
141 | *ret_fstype = t; | |
142 | return 1; | |
143 | } | |
144 | ||
145 | not_found: | |
d2c6e79d | 146 | log_debug("No type detected on partition %s", path); |
18b5886e LP |
147 | *ret_fstype = NULL; |
148 | return 0; | |
d1c536f5 ZJS |
149 | #else |
150 | return -EOPNOTSUPP; | |
a75e27eb | 151 | #endif |
d1c536f5 | 152 | } |
18b5886e | 153 | |
40c10d3f | 154 | #if HAVE_BLKID |
698bb074 YW |
155 | static int dissected_image_probe_filesystem(DissectedImage *m) { |
156 | int r; | |
157 | ||
158 | assert(m); | |
159 | ||
160 | /* Fill in file system types if we don't know them yet. */ | |
161 | ||
162 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
163 | DissectedPartition *p = m->partitions + i; | |
164 | ||
165 | if (!p->found) | |
166 | continue; | |
167 | ||
d2c6e79d YW |
168 | if (!p->fstype && p->mount_node_fd >= 0 && !p->decrypted_node) { |
169 | r = probe_filesystem_full(p->mount_node_fd, p->node, &p->fstype); | |
698bb074 YW |
170 | if (r < 0 && r != -EUCLEAN) |
171 | return r; | |
172 | } | |
173 | ||
174 | if (streq_ptr(p->fstype, "crypto_LUKS")) | |
175 | m->encrypted = true; | |
176 | ||
177 | if (p->fstype && fstype_is_ro(p->fstype)) | |
178 | p->rw = false; | |
179 | ||
180 | if (!p->rw) | |
181 | p->growfs = false; | |
182 | } | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
0f7c9a3d LP |
187 | static void check_partition_flags( |
188 | const char *node, | |
189 | unsigned long long pflags, | |
190 | unsigned long long supported) { | |
191 | ||
192 | assert(node); | |
193 | ||
194 | /* Mask away all flags supported by this partition's type and the three flags the UEFI spec defines generically */ | |
92e72028 ZJS |
195 | pflags &= ~(supported | |
196 | SD_GPT_FLAG_REQUIRED_PARTITION | | |
197 | SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL | | |
198 | SD_GPT_FLAG_LEGACY_BIOS_BOOTABLE); | |
0f7c9a3d LP |
199 | |
200 | if (pflags == 0) | |
201 | return; | |
202 | ||
203 | /* If there are other bits set, then log about it, to make things discoverable */ | |
204 | for (unsigned i = 0; i < sizeof(pflags) * 8; i++) { | |
205 | unsigned long long bit = 1ULL << i; | |
206 | if (!FLAGS_SET(pflags, bit)) | |
207 | continue; | |
208 | ||
209 | log_debug("Unexpected partition flag %llu set on %s!", bit, node); | |
210 | } | |
211 | } | |
1b010ae7 | 212 | #endif |
0f7c9a3d | 213 | |
00e29505 YW |
214 | #if HAVE_BLKID |
215 | static int dissected_image_new(const char *path, DissectedImage **ret) { | |
216 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
217 | _cleanup_free_ char *name = NULL; | |
218 | int r; | |
219 | ||
220 | assert(ret); | |
221 | ||
222 | if (path) { | |
223 | _cleanup_free_ char *filename = NULL; | |
224 | ||
225 | r = path_extract_filename(path, &filename); | |
226 | if (r < 0) | |
227 | return r; | |
228 | ||
229 | r = raw_strip_suffixes(filename, &name); | |
230 | if (r < 0) | |
231 | return r; | |
232 | ||
233 | if (!image_name_is_valid(name)) { | |
234 | log_debug("Image name %s is not valid, ignoring.", strna(name)); | |
235 | name = mfree(name); | |
236 | } | |
237 | } | |
238 | ||
239 | m = new(DissectedImage, 1); | |
240 | if (!m) | |
241 | return -ENOMEM; | |
242 | ||
243 | *m = (DissectedImage) { | |
244 | .has_init_system = -1, | |
245 | .image_name = TAKE_PTR(name), | |
246 | }; | |
247 | ||
babd5b08 YW |
248 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) |
249 | m->partitions[i] = DISSECTED_PARTITION_NULL; | |
250 | ||
00e29505 YW |
251 | *ret = TAKE_PTR(m); |
252 | return 0; | |
253 | } | |
254 | #endif | |
255 | ||
234c2e16 | 256 | static void dissected_partition_done(DissectedPartition *p) { |
1b010ae7 | 257 | assert(p); |
786e3a52 | 258 | |
1b010ae7 LP |
259 | free(p->fstype); |
260 | free(p->node); | |
261 | free(p->label); | |
262 | free(p->decrypted_fstype); | |
263 | free(p->decrypted_node); | |
264 | free(p->mount_options); | |
f7725647 | 265 | safe_close(p->mount_node_fd); |
786e3a52 | 266 | |
babd5b08 | 267 | *p = DISSECTED_PARTITION_NULL; |
1b010ae7 | 268 | } |
786e3a52 | 269 | |
1b010ae7 | 270 | #if HAVE_BLKID |
1b010ae7 LP |
271 | static int make_partition_devname( |
272 | const char *whole_devname, | |
273 | int nr, | |
274 | char **ret) { | |
786e3a52 | 275 | |
1b010ae7 | 276 | bool need_p; |
786e3a52 | 277 | |
1b010ae7 LP |
278 | assert(whole_devname); |
279 | assert(nr > 0); | |
aae22eb3 | 280 | |
1b010ae7 LP |
281 | /* Given a whole block device node name (e.g. /dev/sda or /dev/loop7) generate a partition device |
282 | * name (e.g. /dev/sda7 or /dev/loop7p5). The rule the kernel uses is simple: if whole block device | |
283 | * node name ends in a digit, then suffix a 'p', followed by the partition number. Otherwise, just | |
284 | * suffix the partition number without any 'p'. */ | |
4ba86848 | 285 | |
1b010ae7 LP |
286 | if (isempty(whole_devname)) /* Make sure there *is* a last char */ |
287 | return -EINVAL; | |
08fe0a53 | 288 | |
ff25d338 | 289 | need_p = ascii_isdigit(whole_devname[strlen(whole_devname)-1]); /* Last char a digit? */ |
08fe0a53 | 290 | |
1b010ae7 | 291 | return asprintf(ret, "%s%s%i", whole_devname, need_p ? "p" : "", nr); |
08fe0a53 LP |
292 | } |
293 | ||
f7725647 YW |
294 | static int open_partition(const char *node, bool is_partition, const LoopDevice *loop) { |
295 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; | |
296 | _cleanup_close_ int fd = -1; | |
297 | dev_t devnum; | |
298 | int r; | |
299 | ||
300 | assert(node); | |
301 | assert(loop); | |
302 | ||
303 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
304 | if (fd < 0) | |
305 | return -errno; | |
306 | ||
307 | /* Check if the block device is a child of (or equivalent to) the originally provided one. */ | |
308 | r = block_device_new_from_fd(fd, is_partition ? BLOCK_DEVICE_LOOKUP_WHOLE_DISK : 0, &dev); | |
309 | if (r < 0) | |
310 | return r; | |
311 | ||
312 | r = sd_device_get_devnum(dev, &devnum); | |
313 | if (r < 0) | |
314 | return r; | |
315 | ||
316 | if (loop->devno != devnum) | |
317 | return -ENXIO; | |
318 | ||
319 | /* Also check diskseq. */ | |
320 | if (loop->diskseq > 0) { | |
321 | uint64_t diskseq; | |
322 | ||
323 | r = fd_get_diskseq(fd, &diskseq); | |
324 | if (r < 0) | |
325 | return r; | |
326 | ||
327 | if (loop->diskseq != diskseq) | |
328 | return -ENXIO; | |
329 | } | |
330 | ||
331 | log_debug("Opened %s (fd=%i, whole_block_devnum=" DEVNUM_FORMAT_STR ", diskseq=%" PRIu64 ").", | |
332 | node, fd, DEVNUM_FORMAT_VAL(loop->devno), loop->diskseq); | |
333 | return TAKE_FD(fd); | |
334 | } | |
335 | ||
22e932f4 DDM |
336 | static int compare_arch(Architecture a, Architecture b) { |
337 | if (a == b) | |
338 | return 0; | |
339 | ||
340 | if (a == native_architecture()) | |
341 | return 1; | |
342 | ||
343 | if (b == native_architecture()) | |
344 | return -1; | |
345 | ||
346 | #ifdef ARCHITECTURE_SECONDARY | |
347 | if (a == ARCHITECTURE_SECONDARY) | |
348 | return 1; | |
349 | ||
350 | if (b == ARCHITECTURE_SECONDARY) | |
351 | return -1; | |
352 | #endif | |
353 | ||
354 | return 0; | |
355 | } | |
356 | ||
08f14be4 YW |
357 | static int dissect_image( |
358 | DissectedImage *m, | |
4526113f | 359 | int fd, |
0b214aa0 | 360 | const char *devname, |
89e62e0b | 361 | const VeritySettings *verity, |
18d73705 | 362 | const MountOptions *mount_options, |
08f14be4 | 363 | DissectImageFlags flags) { |
8c1be37e | 364 | |
62ea0ed0 | 365 | sd_id128_t root_uuid = SD_ID128_NULL, root_verity_uuid = SD_ID128_NULL; |
62ea0ed0 | 366 | sd_id128_t usr_uuid = SD_ID128_NULL, usr_verity_uuid = SD_ID128_NULL; |
1f8fb21c | 367 | bool is_gpt, is_mbr, multiple_generic = false, |
de98f631 LP |
368 | generic_rw = false, /* initialize to appease gcc */ |
369 | generic_growfs = false; | |
8e766630 | 370 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
8c1be37e | 371 | _cleanup_free_ char *generic_node = NULL; |
be30ad41 | 372 | sd_id128_t generic_uuid = SD_ID128_NULL; |
b387778c | 373 | const char *pttype = NULL, *sptuuid = NULL; |
8c1be37e | 374 | blkid_partlist pl; |
1f8fb21c | 375 | int r, generic_nr = -1, n_partitions; |
8c1be37e | 376 | |
08f14be4 | 377 | assert(m); |
8c1be37e | 378 | assert(fd >= 0); |
0b214aa0 | 379 | assert(devname); |
a0bff7ea | 380 | assert(!verity || verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
89e62e0b | 381 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
a0bff7ea LP |
382 | assert(!verity || verity->root_hash_sig || verity->root_hash_sig_size == 0); |
383 | assert(!verity || (verity->root_hash || !verity->root_hash_sig)); | |
e7cbe5cb | 384 | assert(!((flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_NO_PARTITION_TABLE))); |
8c1be37e LP |
385 | |
386 | /* Probes a disk image, and returns information about what it found in *ret. | |
387 | * | |
4623e8e6 | 388 | * Returns -ENOPKG if no suitable partition table or file system could be found. |
2679f407 LP |
389 | * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. |
390 | * Returns -ENXIO if we couldn't find any partition suitable as root or /usr partition | |
391 | * Returns -ENOTUNIQ if we only found multiple generic partitions and thus don't know what to do with that */ | |
4623e8e6 | 392 | |
89e62e0b | 393 | if (verity && verity->root_hash) { |
aee36b4e LP |
394 | sd_id128_t fsuuid, vuuid; |
395 | ||
396 | /* If a root hash is supplied, then we use the root partition that has a UUID that match the | |
397 | * first 128bit of the root hash. And we use the verity partition that has a UUID that match | |
398 | * the final 128bit. */ | |
4623e8e6 | 399 | |
89e62e0b | 400 | if (verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
401 | return -EINVAL; |
402 | ||
aee36b4e LP |
403 | memcpy(&fsuuid, verity->root_hash, sizeof(sd_id128_t)); |
404 | memcpy(&vuuid, (const uint8_t*) verity->root_hash + verity->root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t)); | |
4623e8e6 | 405 | |
aee36b4e | 406 | if (sd_id128_is_null(fsuuid)) |
4623e8e6 | 407 | return -EINVAL; |
aee36b4e | 408 | if (sd_id128_is_null(vuuid)) |
4623e8e6 | 409 | return -EINVAL; |
aee36b4e LP |
410 | |
411 | /* If the verity data declares it's for the /usr partition, then search for that, in all | |
412 | * other cases assume it's for the root partition. */ | |
413 | if (verity->designator == PARTITION_USR) { | |
414 | usr_uuid = fsuuid; | |
415 | usr_verity_uuid = vuuid; | |
416 | } else { | |
417 | root_uuid = fsuuid; | |
418 | root_verity_uuid = vuuid; | |
419 | } | |
4623e8e6 | 420 | } |
8c1be37e | 421 | |
8c1be37e LP |
422 | b = blkid_new_probe(); |
423 | if (!b) | |
424 | return -ENOMEM; | |
425 | ||
426 | errno = 0; | |
427 | r = blkid_probe_set_device(b, fd, 0, 0); | |
b382db9f | 428 | if (r != 0) |
66855de7 | 429 | return errno_or_else(ENOMEM); |
8c1be37e | 430 | |
9b6deb03 LP |
431 | if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { |
432 | /* Look for file system superblocks, unless we only shall look for GPT partition tables */ | |
433 | blkid_probe_enable_superblocks(b, 1); | |
b387778c | 434 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE|BLKID_SUBLKS_UUID); |
9b6deb03 LP |
435 | } |
436 | ||
8c1be37e LP |
437 | blkid_probe_enable_partitions(b, 1); |
438 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
439 | ||
440 | errno = 0; | |
441 | r = blkid_do_safeprobe(b); | |
59ba6d0c LP |
442 | if (IN_SET(r, -2, 1)) |
443 | return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); | |
b382db9f | 444 | if (r != 0) |
66855de7 | 445 | return errno_or_else(EIO); |
8c1be37e | 446 | |
e7cbe5cb | 447 | if ((!(flags & DISSECT_IMAGE_GPT_ONLY) && |
4b5de5dd | 448 | (flags & DISSECT_IMAGE_GENERIC_ROOT)) || |
e7cbe5cb | 449 | (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)) { |
9b6deb03 | 450 | const char *usage = NULL; |
8c1be37e | 451 | |
aee36b4e LP |
452 | /* If flags permit this, also allow using non-partitioned single-filesystem images */ |
453 | ||
9b6deb03 LP |
454 | (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL); |
455 | if (STRPTR_IN_SET(usage, "filesystem", "crypto")) { | |
18d73705 | 456 | _cleanup_free_ char *t = NULL, *n = NULL, *o = NULL; |
b387778c | 457 | const char *fstype = NULL, *options = NULL, *suuid = NULL; |
f7725647 | 458 | _cleanup_close_ int mount_node_fd = -1; |
b387778c | 459 | sd_id128_t uuid = SD_ID128_NULL; |
f7725647 | 460 | |
73d88b80 | 461 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) { |
f7725647 YW |
462 | mount_node_fd = open_partition(devname, /* is_partition = */ false, m->loop); |
463 | if (mount_node_fd < 0) | |
464 | return mount_node_fd; | |
465 | } | |
8c1be37e | 466 | |
9b6deb03 LP |
467 | /* OK, we have found a file system, that's our root partition then. */ |
468 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
b387778c | 469 | (void) blkid_probe_lookup_value(b, "UUID", &suuid, NULL); |
8c1be37e | 470 | |
9b6deb03 LP |
471 | if (fstype) { |
472 | t = strdup(fstype); | |
473 | if (!t) | |
474 | return -ENOMEM; | |
475 | } | |
476 | ||
b387778c LP |
477 | if (suuid) { |
478 | /* blkid will return FAT's serial number as UUID, hence it is quite possible | |
479 | * that parsing this will fail. We'll ignore the ID, since it's just too | |
480 | * short to be useful as tru identifier. */ | |
481 | r = sd_id128_from_string(suuid, &uuid); | |
482 | if (r < 0) | |
483 | log_debug_errno(r, "Failed to parse file system UUID '%s', ignoring: %m", suuid); | |
484 | } | |
485 | ||
6c544d14 LP |
486 | n = strdup(devname); |
487 | if (!n) | |
488 | return -ENOMEM; | |
489 | ||
e7cbe5cb | 490 | m->single_file_system = true; |
c3c88d67 LP |
491 | m->encrypted = streq_ptr(fstype, "crypto_LUKS"); |
492 | ||
493 | m->has_verity = verity && verity->data_path; | |
494 | m->verity_ready = m->has_verity && | |
495 | verity->root_hash && | |
496 | (verity->designator < 0 || verity->designator == PARTITION_ROOT); | |
e7cbe5cb | 497 | |
8ee9615e LP |
498 | m->has_verity_sig = false; /* signature not embedded, must be specified */ |
499 | m->verity_sig_ready = m->verity_ready && | |
500 | verity->root_hash_sig; | |
501 | ||
b387778c LP |
502 | m->image_uuid = uuid; |
503 | ||
f5215bc8 | 504 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
505 | if (options) { |
506 | o = strdup(options); | |
507 | if (!o) | |
508 | return -ENOMEM; | |
509 | } | |
510 | ||
9b6deb03 LP |
511 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
512 | .found = true, | |
e0d53d52 | 513 | .rw = !m->verity_ready && !fstype_is_ro(fstype), |
9b6deb03 LP |
514 | .partno = -1, |
515 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
516 | .fstype = TAKE_PTR(t), |
517 | .node = TAKE_PTR(n), | |
18d73705 | 518 | .mount_options = TAKE_PTR(o), |
f7725647 | 519 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
520 | .offset = 0, |
521 | .size = UINT64_MAX, | |
9b6deb03 | 522 | }; |
8c1be37e | 523 | |
9b6deb03 LP |
524 | return 0; |
525 | } | |
8c1be37e LP |
526 | } |
527 | ||
528 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
529 | if (!pttype) | |
530 | return -ENOPKG; | |
531 | ||
532 | is_gpt = streq_ptr(pttype, "gpt"); | |
533 | is_mbr = streq_ptr(pttype, "dos"); | |
534 | ||
9b6deb03 | 535 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
536 | return -ENOPKG; |
537 | ||
0903fd26 LP |
538 | /* We support external verity data partitions only if the image has no partition table */ |
539 | if (verity && verity->data_path) | |
540 | return -EBADR; | |
541 | ||
73d88b80 | 542 | if (FLAGS_SET(flags, DISSECT_IMAGE_ADD_PARTITION_DEVICES)) { |
08f14be4 YW |
543 | /* Safety check: refuse block devices that carry a partition table but for which the kernel doesn't |
544 | * do partition scanning. */ | |
545 | r = blockdev_partscan_enabled(fd); | |
546 | if (r < 0) | |
547 | return r; | |
548 | if (r == 0) | |
549 | return -EPROTONOSUPPORT; | |
550 | } | |
4ba86848 | 551 | |
b387778c LP |
552 | (void) blkid_probe_lookup_value(b, "PTUUID", &sptuuid, NULL); |
553 | if (sptuuid) { | |
554 | r = sd_id128_from_string(sptuuid, &m->image_uuid); | |
555 | if (r < 0) | |
556 | log_debug_errno(r, "Failed to parse partition table UUID '%s', ignoring: %m", sptuuid); | |
557 | } | |
558 | ||
8c1be37e LP |
559 | errno = 0; |
560 | pl = blkid_probe_get_partitions(b); | |
b382db9f | 561 | if (!pl) |
66855de7 | 562 | return errno_or_else(ENOMEM); |
8c1be37e | 563 | |
4ba86848 LP |
564 | errno = 0; |
565 | n_partitions = blkid_partlist_numof_partitions(pl); | |
566 | if (n_partitions < 0) | |
567 | return errno_or_else(EIO); | |
8c1be37e | 568 | |
4ba86848 | 569 | for (int i = 0; i < n_partitions; i++) { |
1b010ae7 | 570 | _cleanup_free_ char *node = NULL; |
9b6deb03 | 571 | unsigned long long pflags; |
88b3300f | 572 | blkid_loff_t start, size; |
8c1be37e | 573 | blkid_partition pp; |
8c1be37e LP |
574 | int nr; |
575 | ||
4ba86848 LP |
576 | errno = 0; |
577 | pp = blkid_partlist_get_partition(pl, i); | |
578 | if (!pp) | |
579 | return errno_or_else(EIO); | |
aae22eb3 | 580 | |
9b6deb03 | 581 | pflags = blkid_partition_get_flags(pp); |
8c1be37e | 582 | |
4ba86848 | 583 | errno = 0; |
8c1be37e LP |
584 | nr = blkid_partition_get_partno(pp); |
585 | if (nr < 0) | |
4ba86848 | 586 | return errno_or_else(EIO); |
8c1be37e | 587 | |
88b3300f LP |
588 | errno = 0; |
589 | start = blkid_partition_get_start(pp); | |
590 | if (start < 0) | |
591 | return errno_or_else(EIO); | |
592 | ||
593 | assert((uint64_t) start < UINT64_MAX/512); | |
594 | ||
595 | errno = 0; | |
596 | size = blkid_partition_get_size(pp); | |
597 | if (size < 0) | |
598 | return errno_or_else(EIO); | |
599 | ||
600 | assert((uint64_t) size < UINT64_MAX/512); | |
601 | ||
1b010ae7 LP |
602 | r = make_partition_devname(devname, nr, &node); |
603 | if (r < 0) | |
604 | return r; | |
605 | ||
606 | /* So here's the thing: after the main ("whole") block device popped up it might take a while | |
607 | * before the kernel fully probed the partition table. Waiting for that to finish is icky in | |
608 | * userspace. So here's what we do instead. We issue the BLKPG_ADD_PARTITION ioctl to add the | |
609 | * partition ourselves, racing against the kernel. Good thing is: if this call fails with | |
610 | * EBUSY then the kernel was quicker than us, and that's totally OK, the outcome is good for | |
611 | * us: the device node will exist. If OTOH our call was successful we won the race. Which is | |
612 | * also good as the outcome is the same: the partition block device exists, and we can use | |
613 | * it. | |
614 | * | |
615 | * Kernel returns EBUSY if there's already a partition by that number or an overlapping | |
616 | * partition already existent. */ | |
617 | ||
73d88b80 | 618 | if (FLAGS_SET(flags, DISSECT_IMAGE_ADD_PARTITION_DEVICES)) { |
08f14be4 YW |
619 | r = block_device_add_partition(fd, node, nr, (uint64_t) start * 512, (uint64_t) size * 512); |
620 | if (r < 0) { | |
621 | if (r != -EBUSY) | |
622 | return log_debug_errno(r, "BLKPG_ADD_PARTITION failed: %m"); | |
1b010ae7 | 623 | |
08f14be4 YW |
624 | log_debug_errno(r, "Kernel was quicker than us in adding partition %i.", nr); |
625 | } else | |
626 | log_debug("We were quicker than kernel in adding partition %i.", nr); | |
627 | } | |
1b010ae7 | 628 | |
8c1be37e | 629 | if (is_gpt) { |
e3b9a5ff | 630 | const char *fstype = NULL, *label; |
4623e8e6 | 631 | sd_id128_t type_id, id; |
22e932f4 | 632 | GptPartitionType type; |
de98f631 | 633 | bool rw = true, growfs = false; |
8c1be37e | 634 | |
e3b9a5ff LP |
635 | r = blkid_partition_get_uuid_id128(pp, &id); |
636 | if (r < 0) { | |
637 | log_debug_errno(r, "Failed to read partition UUID, ignoring: %m"); | |
4623e8e6 | 638 | continue; |
e3b9a5ff | 639 | } |
4623e8e6 | 640 | |
e3b9a5ff LP |
641 | r = blkid_partition_get_type_id128(pp, &type_id); |
642 | if (r < 0) { | |
643 | log_debug_errno(r, "Failed to read partition type UUID, ignoring: %m"); | |
8c1be37e | 644 | continue; |
e3b9a5ff | 645 | } |
8c1be37e | 646 | |
22e932f4 DDM |
647 | type = gpt_partition_type_from_uuid(type_id); |
648 | ||
08fe0a53 LP |
649 | label = blkid_partition_get_name(pp); /* libblkid returns NULL here if empty */ |
650 | ||
fb3921b8 LP |
651 | if (IN_SET(type.designator, |
652 | PARTITION_HOME, | |
653 | PARTITION_SRV, | |
654 | PARTITION_XBOOTLDR, | |
655 | PARTITION_TMP)) { | |
a48dd347 | 656 | |
92e72028 ZJS |
657 | check_partition_flags(node, pflags, |
658 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 659 | |
92e72028 | 660 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
661 | continue; |
662 | ||
92e72028 ZJS |
663 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
664 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
aee36b4e | 665 | |
22e932f4 | 666 | } else if (type.designator == PARTITION_ESP) { |
a48dd347 | 667 | |
92e72028 ZJS |
668 | /* Note that we don't check the SD_GPT_FLAG_NO_AUTO flag for the ESP, as it is |
669 | * not defined there. We instead check the SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as | |
aee36b4e LP |
670 | * recommended by the UEFI spec (See "12.3.3 Number and Location of System |
671 | * Partitions"). */ | |
a48dd347 | 672 | |
92e72028 | 673 | if (pflags & SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL) |
a48dd347 LP |
674 | continue; |
675 | ||
8c1be37e | 676 | fstype = "vfat"; |
a8c47660 | 677 | |
22e932f4 | 678 | } else if (type.designator == PARTITION_ROOT) { |
4623e8e6 | 679 | |
92e72028 ZJS |
680 | check_partition_flags(node, pflags, |
681 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 682 | |
92e72028 | 683 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
684 | continue; |
685 | ||
4623e8e6 LP |
686 | /* If a root ID is specified, ignore everything but the root id */ |
687 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
688 | continue; | |
689 | ||
92e72028 ZJS |
690 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
691 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
aee36b4e | 692 | |
22e932f4 | 693 | } else if (type.designator == PARTITION_ROOT_VERITY) { |
4623e8e6 | 694 | |
92e72028 ZJS |
695 | check_partition_flags(node, pflags, |
696 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
0f7c9a3d | 697 | |
92e72028 | 698 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
699 | continue; |
700 | ||
c3c88d67 | 701 | m->has_verity = true; |
4623e8e6 | 702 | |
8ee9615e LP |
703 | /* If no verity configuration is specified, then don't do verity */ |
704 | if (!verity) | |
705 | continue; | |
706 | if (verity->designator >= 0 && verity->designator != PARTITION_ROOT) | |
707 | continue; | |
708 | ||
709 | /* If root hash is specified, then ignore everything but the root id */ | |
710 | if (!sd_id128_is_null(root_verity_uuid) && !sd_id128_equal(root_verity_uuid, id)) | |
4623e8e6 LP |
711 | continue; |
712 | ||
4623e8e6 | 713 | fstype = "DM_verity_hash"; |
4623e8e6 | 714 | rw = false; |
8ee9615e | 715 | |
22e932f4 | 716 | } else if (type.designator == PARTITION_ROOT_VERITY_SIG) { |
8ee9615e | 717 | |
92e72028 ZJS |
718 | check_partition_flags(node, pflags, |
719 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
8ee9615e | 720 | |
92e72028 | 721 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
8ee9615e LP |
722 | continue; |
723 | ||
724 | m->has_verity_sig = true; | |
725 | ||
8ee9615e LP |
726 | if (!verity) |
727 | continue; | |
728 | if (verity->designator >= 0 && verity->designator != PARTITION_ROOT) | |
729 | continue; | |
8ee9615e | 730 | |
8ee9615e | 731 | fstype = "verity_hash_signature"; |
4623e8e6 | 732 | rw = false; |
8ee9615e | 733 | |
22e932f4 | 734 | } else if (type.designator == PARTITION_USR) { |
aee36b4e | 735 | |
92e72028 ZJS |
736 | check_partition_flags(node, pflags, |
737 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
aee36b4e | 738 | |
92e72028 | 739 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
aee36b4e LP |
740 | continue; |
741 | ||
742 | /* If a usr ID is specified, ignore everything but the usr id */ | |
743 | if (!sd_id128_is_null(usr_uuid) && !sd_id128_equal(usr_uuid, id)) | |
744 | continue; | |
745 | ||
92e72028 ZJS |
746 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
747 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
aee36b4e | 748 | |
22e932f4 | 749 | } else if (type.designator == PARTITION_USR_VERITY) { |
aee36b4e | 750 | |
92e72028 ZJS |
751 | check_partition_flags(node, pflags, |
752 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
aee36b4e | 753 | |
92e72028 | 754 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
aee36b4e LP |
755 | continue; |
756 | ||
c3c88d67 | 757 | m->has_verity = true; |
aee36b4e | 758 | |
8ee9615e LP |
759 | if (!verity) |
760 | continue; | |
761 | if (verity->designator >= 0 && verity->designator != PARTITION_USR) | |
762 | continue; | |
763 | ||
764 | /* If usr hash is specified, then ignore everything but the usr id */ | |
765 | if (!sd_id128_is_null(usr_verity_uuid) && !sd_id128_equal(usr_verity_uuid, id)) | |
aee36b4e LP |
766 | continue; |
767 | ||
aee36b4e | 768 | fstype = "DM_verity_hash"; |
aee36b4e | 769 | rw = false; |
8ee9615e | 770 | |
22e932f4 | 771 | } else if (type.designator == PARTITION_USR_VERITY_SIG) { |
8ee9615e | 772 | |
92e72028 ZJS |
773 | check_partition_flags(node, pflags, |
774 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
8ee9615e | 775 | |
92e72028 | 776 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
8ee9615e LP |
777 | continue; |
778 | ||
779 | m->has_verity_sig = true; | |
780 | ||
8ee9615e LP |
781 | if (!verity) |
782 | continue; | |
783 | if (verity->designator >= 0 && verity->designator != PARTITION_USR) | |
784 | continue; | |
8ee9615e | 785 | |
8ee9615e | 786 | fstype = "verity_hash_signature"; |
aee36b4e | 787 | rw = false; |
8ee9615e | 788 | |
22e932f4 | 789 | } else if (type.designator == PARTITION_SWAP) { |
a48dd347 | 790 | |
92e72028 | 791 | check_partition_flags(node, pflags, SD_GPT_FLAG_NO_AUTO); |
0f7c9a3d | 792 | |
92e72028 | 793 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
794 | continue; |
795 | ||
df4524cb LP |
796 | fstype = "swap"; |
797 | ||
df655bf3 DDM |
798 | /* We don't have a designator for SD_GPT_LINUX_GENERIC so check the UUID instead. */ |
799 | } else if (sd_id128_equal(type.uuid, SD_GPT_LINUX_GENERIC)) { | |
8c1be37e | 800 | |
92e72028 ZJS |
801 | check_partition_flags(node, pflags, |
802 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 803 | |
92e72028 | 804 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
805 | continue; |
806 | ||
8c1be37e LP |
807 | if (generic_node) |
808 | multiple_generic = true; | |
809 | else { | |
810 | generic_nr = nr; | |
92e72028 ZJS |
811 | generic_rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
812 | generic_growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
be30ad41 | 813 | generic_uuid = id; |
8c1be37e LP |
814 | generic_node = strdup(node); |
815 | if (!generic_node) | |
816 | return -ENOMEM; | |
817 | } | |
d4dffb85 | 818 | |
22e932f4 | 819 | } else if (type.designator == PARTITION_VAR) { |
d4dffb85 | 820 | |
92e72028 ZJS |
821 | check_partition_flags(node, pflags, |
822 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 823 | |
92e72028 | 824 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
d4dffb85 LP |
825 | continue; |
826 | ||
827 | if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) { | |
828 | sd_id128_t var_uuid; | |
829 | ||
830 | /* For /var we insist that the uuid of the partition matches the | |
831 | * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine | |
832 | * ID. Why? Unlike the other partitions /var is inherently | |
833 | * installation specific, hence we need to be careful not to mount it | |
834 | * in the wrong installation. By hashing the partition UUID from | |
835 | * /etc/machine-id we can securely bind the partition to the | |
836 | * installation. */ | |
837 | ||
92e72028 | 838 | r = sd_id128_get_machine_app_specific(SD_GPT_VAR, &var_uuid); |
d4dffb85 LP |
839 | if (r < 0) |
840 | return r; | |
841 | ||
842 | if (!sd_id128_equal(var_uuid, id)) { | |
843 | log_debug("Found a /var/ partition, but its UUID didn't match our expectations, ignoring."); | |
844 | continue; | |
845 | } | |
846 | } | |
847 | ||
92e72028 ZJS |
848 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
849 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
8c1be37e LP |
850 | } |
851 | ||
22e932f4 | 852 | if (type.designator != _PARTITION_DESIGNATOR_INVALID) { |
0fb5036f | 853 | _cleanup_free_ char *t = NULL, *o = NULL, *l = NULL; |
f7725647 | 854 | _cleanup_close_ int mount_node_fd = -1; |
18d73705 | 855 | const char *options = NULL; |
8c1be37e | 856 | |
22e932f4 | 857 | if (m->partitions[type.designator].found) { |
08fe0a53 LP |
858 | /* For most partition types the first one we see wins. Except for the |
859 | * rootfs and /usr, where we do a version compare of the label, and | |
860 | * let the newest version win. This permits a simple A/B versioning | |
861 | * scheme in OS images. */ | |
862 | ||
22e932f4 DDM |
863 | if (compare_arch(type.arch, m->partitions[type.designator].architecture) <= 0) |
864 | continue; | |
865 | ||
866 | if (!partition_designator_is_versioned(type.designator) || | |
867 | strverscmp_improved(m->partitions[type.designator].label, label) >= 0) | |
08fe0a53 LP |
868 | continue; |
869 | ||
22e932f4 | 870 | dissected_partition_done(m->partitions + type.designator); |
08fe0a53 | 871 | } |
8c1be37e | 872 | |
73d88b80 | 873 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES) && |
f52261a0 | 874 | type.designator != PARTITION_SWAP) { |
f7725647 YW |
875 | mount_node_fd = open_partition(node, /* is_partition = */ true, m->loop); |
876 | if (mount_node_fd < 0) | |
877 | return mount_node_fd; | |
878 | } | |
879 | ||
8c1be37e LP |
880 | if (fstype) { |
881 | t = strdup(fstype); | |
882 | if (!t) | |
883 | return -ENOMEM; | |
884 | } | |
885 | ||
08fe0a53 LP |
886 | if (label) { |
887 | l = strdup(label); | |
888 | if (!l) | |
889 | return -ENOMEM; | |
890 | } | |
891 | ||
22e932f4 | 892 | options = mount_options_from_designator(mount_options, type.designator); |
18d73705 LB |
893 | if (options) { |
894 | o = strdup(options); | |
895 | if (!o) | |
896 | return -ENOMEM; | |
897 | } | |
898 | ||
22e932f4 | 899 | m->partitions[type.designator] = (DissectedPartition) { |
8c1be37e LP |
900 | .found = true, |
901 | .partno = nr, | |
902 | .rw = rw, | |
de98f631 | 903 | .growfs = growfs, |
22e932f4 | 904 | .architecture = type.arch, |
0fb5036f | 905 | .node = TAKE_PTR(node), |
1cc6c93a | 906 | .fstype = TAKE_PTR(t), |
08fe0a53 | 907 | .label = TAKE_PTR(l), |
be30ad41 | 908 | .uuid = id, |
18d73705 | 909 | .mount_options = TAKE_PTR(o), |
f7725647 | 910 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
911 | .offset = (uint64_t) start * 512, |
912 | .size = (uint64_t) size * 512, | |
8c1be37e | 913 | }; |
8c1be37e LP |
914 | } |
915 | ||
916 | } else if (is_mbr) { | |
917 | ||
a8c47660 | 918 | switch (blkid_partition_get_type(pp)) { |
8c1be37e | 919 | |
a8c47660 LP |
920 | case 0x83: /* Linux partition */ |
921 | ||
922 | if (pflags != 0x80) /* Bootable flag */ | |
923 | continue; | |
8c1be37e | 924 | |
a8c47660 LP |
925 | if (generic_node) |
926 | multiple_generic = true; | |
927 | else { | |
928 | generic_nr = nr; | |
929 | generic_rw = true; | |
de98f631 | 930 | generic_growfs = false; |
a8c47660 LP |
931 | generic_node = strdup(node); |
932 | if (!generic_node) | |
933 | return -ENOMEM; | |
934 | } | |
935 | ||
936 | break; | |
937 | ||
938 | case 0xEA: { /* Boot Loader Spec extended $BOOT partition */ | |
f7725647 | 939 | _cleanup_close_ int mount_node_fd = -1; |
0fb5036f | 940 | _cleanup_free_ char *o = NULL; |
a8c47660 | 941 | sd_id128_t id = SD_ID128_NULL; |
e3b9a5ff | 942 | const char *options = NULL; |
a8c47660 LP |
943 | |
944 | /* First one wins */ | |
234c2e16 | 945 | if (m->partitions[PARTITION_XBOOTLDR].found) |
a8c47660 LP |
946 | continue; |
947 | ||
73d88b80 | 948 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) { |
f7725647 YW |
949 | mount_node_fd = open_partition(node, /* is_partition = */ true, m->loop); |
950 | if (mount_node_fd < 0) | |
951 | return mount_node_fd; | |
952 | } | |
953 | ||
e3b9a5ff | 954 | (void) blkid_partition_get_uuid_id128(pp, &id); |
a8c47660 | 955 | |
f5215bc8 | 956 | options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR); |
18d73705 LB |
957 | if (options) { |
958 | o = strdup(options); | |
959 | if (!o) | |
960 | return -ENOMEM; | |
961 | } | |
962 | ||
a8c47660 LP |
963 | m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) { |
964 | .found = true, | |
965 | .partno = nr, | |
966 | .rw = true, | |
de98f631 | 967 | .growfs = false, |
a8c47660 | 968 | .architecture = _ARCHITECTURE_INVALID, |
0fb5036f | 969 | .node = TAKE_PTR(node), |
a8c47660 | 970 | .uuid = id, |
18d73705 | 971 | .mount_options = TAKE_PTR(o), |
f7725647 | 972 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
973 | .offset = (uint64_t) start * 512, |
974 | .size = (uint64_t) size * 512, | |
a8c47660 LP |
975 | }; |
976 | ||
977 | break; | |
978 | }} | |
8c1be37e LP |
979 | } |
980 | } | |
981 | ||
22e932f4 DDM |
982 | if (!m->partitions[PARTITION_ROOT].found && |
983 | (m->partitions[PARTITION_ROOT_VERITY].found || | |
984 | m->partitions[PARTITION_ROOT_VERITY_SIG].found)) | |
985 | return -EADDRNOTAVAIL; /* Verity found but no matching rootfs? Something is off, refuse. */ | |
49ae9d91 | 986 | |
8ee9615e LP |
987 | /* Hmm, we found a signature partition but no Verity data? Something is off. */ |
988 | if (m->partitions[PARTITION_ROOT_VERITY_SIG].found && !m->partitions[PARTITION_ROOT_VERITY].found) | |
989 | return -EADDRNOTAVAIL; | |
7cf66030 | 990 | |
22e932f4 DDM |
991 | if (!m->partitions[PARTITION_USR].found && |
992 | (m->partitions[PARTITION_USR_VERITY].found || | |
993 | m->partitions[PARTITION_USR_VERITY_SIG].found)) | |
994 | return -EADDRNOTAVAIL; /* as above */ | |
7cf66030 | 995 | |
22e932f4 | 996 | /* as above */ |
8ee9615e LP |
997 | if (m->partitions[PARTITION_USR_VERITY_SIG].found && !m->partitions[PARTITION_USR_VERITY].found) |
998 | return -EADDRNOTAVAIL; | |
999 | ||
cb241a69 LP |
1000 | /* If root and /usr are combined then insist that the architecture matches */ |
1001 | if (m->partitions[PARTITION_ROOT].found && | |
1002 | m->partitions[PARTITION_USR].found && | |
1003 | (m->partitions[PARTITION_ROOT].architecture >= 0 && | |
1004 | m->partitions[PARTITION_USR].architecture >= 0 && | |
1005 | m->partitions[PARTITION_ROOT].architecture != m->partitions[PARTITION_USR].architecture)) | |
1006 | return -EADDRNOTAVAIL; | |
1007 | ||
4ab51780 LP |
1008 | if (!m->partitions[PARTITION_ROOT].found && |
1009 | !m->partitions[PARTITION_USR].found && | |
1010 | (flags & DISSECT_IMAGE_GENERIC_ROOT) && | |
00a8b34f | 1011 | (!verity || !verity->root_hash || verity->designator != PARTITION_USR)) { |
7cf66030 | 1012 | |
1b010ae7 | 1013 | /* OK, we found nothing usable, then check if there's a single generic partition, and use |
4b5de5dd LP |
1014 | * that. If the root hash was set however, then we won't fall back to a generic node, because |
1015 | * the root hash decides. */ | |
7cf66030 LP |
1016 | |
1017 | /* If we didn't find a properly marked root partition, but we did find a single suitable | |
1018 | * generic Linux partition, then use this as root partition, if the caller asked for it. */ | |
1019 | if (multiple_generic) | |
1020 | return -ENOTUNIQ; | |
1021 | ||
4b5de5dd LP |
1022 | /* If we didn't find a generic node, then we can't fix this up either */ |
1023 | if (generic_node) { | |
f7725647 | 1024 | _cleanup_close_ int mount_node_fd = -1; |
4b5de5dd LP |
1025 | _cleanup_free_ char *o = NULL; |
1026 | const char *options; | |
8c1be37e | 1027 | |
73d88b80 | 1028 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) { |
f7725647 YW |
1029 | mount_node_fd = open_partition(generic_node, /* is_partition = */ true, m->loop); |
1030 | if (mount_node_fd < 0) | |
1031 | return mount_node_fd; | |
1032 | } | |
1033 | ||
f5215bc8 | 1034 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
1035 | if (options) { |
1036 | o = strdup(options); | |
1037 | if (!o) | |
1038 | return -ENOMEM; | |
1039 | } | |
1040 | ||
1f8fb21c | 1041 | assert(generic_nr >= 0); |
8c1be37e LP |
1042 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
1043 | .found = true, | |
1044 | .rw = generic_rw, | |
de98f631 | 1045 | .growfs = generic_growfs, |
8c1be37e LP |
1046 | .partno = generic_nr, |
1047 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a | 1048 | .node = TAKE_PTR(generic_node), |
be30ad41 | 1049 | .uuid = generic_uuid, |
18d73705 | 1050 | .mount_options = TAKE_PTR(o), |
f7725647 | 1051 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
1052 | .offset = UINT64_MAX, |
1053 | .size = UINT64_MAX, | |
8c1be37e | 1054 | }; |
e0f9e7bd | 1055 | } |
8c1be37e LP |
1056 | } |
1057 | ||
4b5de5dd LP |
1058 | /* Check if we have a root fs if we are told to do check. /usr alone is fine too, but only if appropriate flag for that is set too */ |
1059 | if (FLAGS_SET(flags, DISSECT_IMAGE_REQUIRE_ROOT) && | |
1060 | !(m->partitions[PARTITION_ROOT].found || (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT)))) | |
1061 | return -ENXIO; | |
1062 | ||
7b32164f LP |
1063 | if (m->partitions[PARTITION_ROOT_VERITY].found) { |
1064 | /* We only support one verity partition per image, i.e. can't do for both /usr and root fs */ | |
1065 | if (m->partitions[PARTITION_USR_VERITY].found) | |
1066 | return -ENOTUNIQ; | |
1067 | ||
1068 | /* We don't support verity enabled root with a split out /usr. Neither with nor without | |
1069 | * verity there. (Note that we do support verity-less root with verity-full /usr, though.) */ | |
1070 | if (m->partitions[PARTITION_USR].found) | |
1071 | return -EADDRNOTAVAIL; | |
1072 | } | |
aee36b4e | 1073 | |
1903defc LP |
1074 | if (verity) { |
1075 | /* If a verity designator is specified, then insist that the matching partition exists */ | |
1076 | if (verity->designator >= 0 && !m->partitions[verity->designator].found) | |
1077 | return -EADDRNOTAVAIL; | |
aee36b4e | 1078 | |
3dd73ea7 DDM |
1079 | bool have_verity_sig_partition = |
1080 | m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR_VERITY_SIG : PARTITION_ROOT_VERITY_SIG].found; | |
1081 | ||
1903defc | 1082 | if (verity->root_hash) { |
8ee9615e LP |
1083 | /* If we have an explicit root hash and found the partitions for it, then we are ready to use |
1084 | * Verity, set things up for it */ | |
1085 | ||
1903defc LP |
1086 | if (verity->designator < 0 || verity->designator == PARTITION_ROOT) { |
1087 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) | |
1088 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1089 | |
1903defc LP |
1090 | /* If we found a verity setup, then the root partition is necessarily read-only. */ |
1091 | m->partitions[PARTITION_ROOT].rw = false; | |
1092 | m->verity_ready = true; | |
1903defc | 1093 | |
f9e0bb61 LP |
1094 | } else { |
1095 | assert(verity->designator == PARTITION_USR); | |
1096 | ||
1903defc LP |
1097 | if (!m->partitions[PARTITION_USR_VERITY].found || !m->partitions[PARTITION_USR].found) |
1098 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1099 | |
1903defc LP |
1100 | m->partitions[PARTITION_USR].rw = false; |
1101 | m->verity_ready = true; | |
1102 | } | |
8ee9615e LP |
1103 | |
1104 | if (m->verity_ready) | |
3dd73ea7 | 1105 | m->verity_sig_ready = verity->root_hash_sig || have_verity_sig_partition; |
8ee9615e | 1106 | |
3dd73ea7 | 1107 | } else if (have_verity_sig_partition) { |
8ee9615e LP |
1108 | |
1109 | /* If we found an embedded signature partition, we are ready, too. */ | |
1110 | ||
1111 | m->verity_ready = m->verity_sig_ready = true; | |
1112 | m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR : PARTITION_ROOT].rw = false; | |
aee36b4e | 1113 | } |
4623e8e6 LP |
1114 | } |
1115 | ||
08f14be4 YW |
1116 | return 0; |
1117 | } | |
1118 | #endif | |
18b5886e | 1119 | |
08f14be4 YW |
1120 | int dissect_image_file( |
1121 | const char *path, | |
1122 | const VeritySettings *verity, | |
1123 | const MountOptions *mount_options, | |
1124 | DissectImageFlags flags, | |
1125 | DissectedImage **ret) { | |
8c1be37e | 1126 | |
08f14be4 YW |
1127 | #if HAVE_BLKID |
1128 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
1129 | _cleanup_close_ int fd = -1; | |
1130 | int r; | |
8c1be37e | 1131 | |
08f14be4 | 1132 | assert(path); |
08f14be4 | 1133 | assert(ret); |
8c1be37e | 1134 | |
08f14be4 YW |
1135 | fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
1136 | if (fd < 0) | |
1137 | return -errno; | |
896f937f | 1138 | |
08f14be4 YW |
1139 | r = fd_verify_regular(fd); |
1140 | if (r < 0) | |
1141 | return r; | |
de98f631 | 1142 | |
08f14be4 YW |
1143 | r = dissected_image_new(path, &m); |
1144 | if (r < 0) | |
1145 | return r; | |
1146 | ||
1147 | r = dissect_image(m, fd, path, verity, mount_options, flags); | |
698bb074 YW |
1148 | if (r < 0) |
1149 | return r; | |
8c1be37e | 1150 | |
1cc6c93a | 1151 | *ret = TAKE_PTR(m); |
8c1be37e LP |
1152 | return 0; |
1153 | #else | |
1154 | return -EOPNOTSUPP; | |
1155 | #endif | |
1156 | } | |
1157 | ||
1158 | DissectedImage* dissected_image_unref(DissectedImage *m) { | |
8c1be37e LP |
1159 | if (!m) |
1160 | return NULL; | |
1161 | ||
ac1e1b5f | 1162 | /* First, clear dissected partitions. */ |
08fe0a53 | 1163 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) |
234c2e16 | 1164 | dissected_partition_done(m->partitions + i); |
8c1be37e | 1165 | |
ac1e1b5f YW |
1166 | /* Second, free decrypted images. This must be after dissected_partition_done(), as freeing |
1167 | * DecryptedImage may try to deactivate partitions. */ | |
1168 | decrypted_image_unref(m->decrypted_image); | |
1169 | ||
1e63dc4f YW |
1170 | /* Third, unref LoopDevice. This must be called after the above two, as freeing LoopDevice may try to |
1171 | * remove existing partitions on the loopback block device. */ | |
1172 | loop_device_unref(m->loop); | |
1173 | ||
593fe6c0 | 1174 | free(m->image_name); |
3b925504 LP |
1175 | free(m->hostname); |
1176 | strv_free(m->machine_info); | |
1177 | strv_free(m->os_release); | |
fab22946 | 1178 | strv_free(m->initrd_release); |
7718ac97 | 1179 | strv_free(m->extension_release); |
3b925504 | 1180 | |
5fecf46d | 1181 | return mfree(m); |
8c1be37e LP |
1182 | } |
1183 | ||
18b5886e | 1184 | static int is_loop_device(const char *path) { |
553e15f2 | 1185 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
1186 | struct stat st; |
1187 | ||
1188 | assert(path); | |
1189 | ||
1190 | if (stat(path, &st) < 0) | |
1191 | return -errno; | |
1192 | ||
1193 | if (!S_ISBLK(st.st_mode)) | |
1194 | return -ENOTBLK; | |
1195 | ||
553e15f2 | 1196 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
1197 | if (access(s, F_OK) < 0) { |
1198 | if (errno != ENOENT) | |
1199 | return -errno; | |
1200 | ||
1201 | /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */ | |
553e15f2 | 1202 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
1203 | if (access(s, F_OK) < 0) |
1204 | return errno == ENOENT ? false : -errno; | |
1205 | } | |
1206 | ||
1207 | return true; | |
1208 | } | |
1209 | ||
f8ab7812 | 1210 | static int run_fsck(int node_fd, const char *fstype) { |
cf32c486 LP |
1211 | int r, exit_status; |
1212 | pid_t pid; | |
1213 | ||
f8ab7812 | 1214 | assert(node_fd >= 0); |
cf32c486 LP |
1215 | assert(fstype); |
1216 | ||
13556724 | 1217 | r = fsck_exists_for_fstype(fstype); |
cf32c486 LP |
1218 | if (r < 0) { |
1219 | log_debug_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", fstype); | |
1220 | return 0; | |
1221 | } | |
1222 | if (r == 0) { | |
f8ab7812 | 1223 | log_debug("Not checking partition %s, as fsck for %s does not exist.", FORMAT_PROC_FD_PATH(node_fd), fstype); |
cf32c486 LP |
1224 | return 0; |
1225 | } | |
1226 | ||
f8ab7812 LP |
1227 | r = safe_fork_full( |
1228 | "(fsck)", | |
1229 | &node_fd, 1, /* Leave the node fd open */ | |
1230 | FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_CLOEXEC_OFF, | |
1231 | &pid); | |
cf32c486 LP |
1232 | if (r < 0) |
1233 | return log_debug_errno(r, "Failed to fork off fsck: %m"); | |
1234 | if (r == 0) { | |
1235 | /* Child */ | |
f8ab7812 | 1236 | execl("/sbin/fsck", "/sbin/fsck", "-aT", FORMAT_PROC_FD_PATH(node_fd), NULL); |
7e0ed2e9 | 1237 | log_open(); |
cf32c486 LP |
1238 | log_debug_errno(errno, "Failed to execl() fsck: %m"); |
1239 | _exit(FSCK_OPERATIONAL_ERROR); | |
1240 | } | |
1241 | ||
1242 | exit_status = wait_for_terminate_and_check("fsck", pid, 0); | |
1243 | if (exit_status < 0) | |
1244 | return log_debug_errno(exit_status, "Failed to fork off /sbin/fsck: %m"); | |
1245 | ||
1246 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { | |
1247 | log_debug("fsck failed with exit status %i.", exit_status); | |
1248 | ||
1249 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
1250 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), "File system is corrupted, refusing."); | |
1251 | ||
1252 | log_debug("Ignoring fsck error."); | |
1253 | } | |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
81939d9d LP |
1258 | static int fs_grow(const char *node_path, const char *mount_path) { |
1259 | _cleanup_close_ int mount_fd = -1, node_fd = -1; | |
81939d9d LP |
1260 | uint64_t size, newsize; |
1261 | int r; | |
1262 | ||
1263 | node_fd = open(node_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
1264 | if (node_fd < 0) | |
1265 | return log_debug_errno(errno, "Failed to open node device %s: %m", node_path); | |
1266 | ||
1267 | if (ioctl(node_fd, BLKGETSIZE64, &size) != 0) | |
1268 | return log_debug_errno(errno, "Failed to get block device size of %s: %m", node_path); | |
1269 | ||
1270 | mount_fd = open(mount_path, O_RDONLY|O_DIRECTORY|O_CLOEXEC); | |
1271 | if (mount_fd < 0) | |
1272 | return log_debug_errno(errno, "Failed to open mountd file system %s: %m", mount_path); | |
1273 | ||
1274 | log_debug("Resizing \"%s\" to %"PRIu64" bytes...", mount_path, size); | |
1275 | r = resize_fs(mount_fd, size, &newsize); | |
1276 | if (r < 0) | |
1277 | return log_debug_errno(r, "Failed to resize \"%s\" to %"PRIu64" bytes: %m", mount_path, size); | |
1278 | ||
1279 | if (newsize == size) | |
1280 | log_debug("Successfully resized \"%s\" to %s bytes.", | |
2b59bf51 | 1281 | mount_path, FORMAT_BYTES(newsize)); |
81939d9d LP |
1282 | else { |
1283 | assert(newsize < size); | |
1284 | log_debug("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).", | |
2b59bf51 | 1285 | mount_path, FORMAT_BYTES(newsize), size - newsize); |
81939d9d LP |
1286 | } |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
18b5886e LP |
1291 | static int mount_partition( |
1292 | DissectedPartition *m, | |
1293 | const char *where, | |
1294 | const char *directory, | |
2d3a5a73 | 1295 | uid_t uid_shift, |
21b61b1d | 1296 | uid_t uid_range, |
18b5886e LP |
1297 | DissectImageFlags flags) { |
1298 | ||
2d3a5a73 LP |
1299 | _cleanup_free_ char *chased = NULL, *options = NULL; |
1300 | const char *p, *node, *fstype; | |
21b61b1d | 1301 | bool rw, remap_uid_gid = false; |
2eedfd2d | 1302 | int r; |
8c1be37e LP |
1303 | |
1304 | assert(m); | |
1305 | assert(where); | |
1306 | ||
f7725647 YW |
1307 | if (m->mount_node_fd < 0) |
1308 | return 0; | |
1309 | ||
4dc28665 | 1310 | /* Use decrypted node and matching fstype if available, otherwise use the original device */ |
f7725647 | 1311 | node = FORMAT_PROC_FD_PATH(m->mount_node_fd); |
4dc28665 | 1312 | fstype = m->decrypted_node ? m->decrypted_fstype: m->fstype; |
18b5886e | 1313 | |
4dc28665 LP |
1314 | if (!fstype) |
1315 | return -EAFNOSUPPORT; | |
8c1be37e | 1316 | |
68ac5118 ZJS |
1317 | /* We are looking at an encrypted partition? This either means stacked encryption, or the caller |
1318 | * didn't call dissected_image_decrypt() beforehand. Let's return a recognizable error for this | |
1319 | * case. */ | |
4dc28665 | 1320 | if (streq(fstype, "crypto_LUKS")) |
fa45d12c | 1321 | return -EUNATCH; |
18b5886e | 1322 | |
ef9c184d | 1323 | rw = m->rw && !(flags & DISSECT_IMAGE_MOUNT_READ_ONLY); |
8c1be37e | 1324 | |
cf32c486 | 1325 | if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) { |
f8ab7812 | 1326 | r = run_fsck(m->mount_node_fd, fstype); |
cf32c486 LP |
1327 | if (r < 0) |
1328 | return r; | |
1329 | } | |
1330 | ||
2eedfd2d | 1331 | if (directory) { |
334eb5b0 LP |
1332 | /* Automatically create missing mount points inside the image, if necessary. */ |
1333 | r = mkdir_p_root(where, directory, uid_shift, (gid_t) uid_shift, 0755); | |
1334 | if (r < 0 && r != -EROFS) | |
1335 | return r; | |
1f0f82f1 | 1336 | |
a5648b80 | 1337 | r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased, NULL); |
2eedfd2d LP |
1338 | if (r < 0) |
1339 | return r; | |
1340 | ||
1341 | p = chased; | |
9842905e LP |
1342 | } else { |
1343 | /* Create top-level mount if missing – but only if this is asked for. This won't modify the | |
1344 | * image (as the branch above does) but the host hierarchy, and the created directory might | |
1345 | * survive our mount in the host hierarchy hence. */ | |
1346 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { | |
1347 | r = mkdir_p(where, 0755); | |
1348 | if (r < 0) | |
1349 | return r; | |
1350 | } | |
1351 | ||
8c1be37e | 1352 | p = where; |
9842905e | 1353 | } |
8c1be37e | 1354 | |
18b5886e | 1355 | /* If requested, turn on discard support. */ |
154d2269 | 1356 | if (fstype_can_discard(fstype) && |
18b5886e | 1357 | ((flags & DISSECT_IMAGE_DISCARD) || |
3afda7c7 | 1358 | ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node) > 0))) { |
2d3a5a73 LP |
1359 | options = strdup("discard"); |
1360 | if (!options) | |
1361 | return -ENOMEM; | |
1362 | } | |
1363 | ||
21b61b1d | 1364 | if (uid_is_valid(uid_shift) && uid_shift != 0) { |
2d3a5a73 | 1365 | |
21b61b1d LP |
1366 | if (fstype_can_uid_gid(fstype)) { |
1367 | _cleanup_free_ char *uid_option = NULL; | |
2d3a5a73 | 1368 | |
21b61b1d LP |
1369 | if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) |
1370 | return -ENOMEM; | |
1371 | ||
1372 | if (!strextend_with_separator(&options, ",", uid_option)) | |
1373 | return -ENOMEM; | |
1374 | } else if (FLAGS_SET(flags, DISSECT_IMAGE_MOUNT_IDMAPPED)) | |
1375 | remap_uid_gid = true; | |
2d3a5a73 | 1376 | } |
8c1be37e | 1377 | |
18d73705 | 1378 | if (!isempty(m->mount_options)) |
c2bc710b | 1379 | if (!strextend_with_separator(&options, ",", m->mount_options)) |
18d73705 LB |
1380 | return -ENOMEM; |
1381 | ||
b620bf33 LP |
1382 | /* So, when you request MS_RDONLY from ext4, then this means nothing. It happily still writes to the |
1383 | * backing storage. What's worse, the BLKRO[GS]ET flag and (in case of loopback devices) | |
1384 | * LO_FLAGS_READ_ONLY don't mean anything, they affect userspace accesses only, and write accesses | |
1385 | * from the upper file system still get propagated through to the underlying file system, | |
1386 | * unrestricted. To actually get ext4/xfs/btrfs to stop writing to the device we need to specify | |
1387 | * "norecovery" as mount option, in addition to MS_RDONLY. Yes, this sucks, since it means we need to | |
1388 | * carry a per file system table here. | |
1389 | * | |
1390 | * Note that this means that we might not be able to mount corrupted file systems as read-only | |
1391 | * anymore (since in some cases the kernel implementations will refuse mounting when corrupted, | |
1392 | * read-only and "norecovery" is specified). But I think for the case of automatically determined | |
1393 | * mount options for loopback devices this is the right choice, since otherwise using the same | |
1394 | * loopback file twice even in read-only mode, is going to fail badly sooner or later. The usecase of | |
1395 | * making reuse of the immutable images "just work" is more relevant to us than having read-only | |
1396 | * access that actually modifies stuff work on such image files. Or to say this differently: if | |
1397 | * people want their file systems to be fixed up they should just open them in writable mode, where | |
1398 | * all these problems don't exist. */ | |
1399 | if (!rw && STRPTR_IN_SET(fstype, "ext3", "ext4", "xfs", "btrfs")) | |
1400 | if (!strextend_with_separator(&options, ",", "norecovery")) | |
1401 | return -ENOMEM; | |
1402 | ||
511a8cfe | 1403 | r = mount_nofollow_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options); |
d9223c07 LP |
1404 | if (r < 0) |
1405 | return r; | |
1406 | ||
81939d9d LP |
1407 | if (rw && m->growfs && FLAGS_SET(flags, DISSECT_IMAGE_GROWFS)) |
1408 | (void) fs_grow(node, p); | |
1409 | ||
21b61b1d | 1410 | if (remap_uid_gid) { |
2b2777ed | 1411 | r = remount_idmap(p, uid_shift, uid_range, UID_INVALID, REMOUNT_IDMAPPING_HOST_ROOT); |
21b61b1d LP |
1412 | if (r < 0) |
1413 | return r; | |
1414 | } | |
1415 | ||
d9223c07 | 1416 | return 1; |
8c1be37e LP |
1417 | } |
1418 | ||
7cf66030 LP |
1419 | static int mount_root_tmpfs(const char *where, uid_t uid_shift, DissectImageFlags flags) { |
1420 | _cleanup_free_ char *options = NULL; | |
1421 | int r; | |
1422 | ||
1423 | assert(where); | |
1424 | ||
1425 | /* For images that contain /usr/ but no rootfs, let's mount rootfs as tmpfs */ | |
1426 | ||
1427 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { | |
1428 | r = mkdir_p(where, 0755); | |
1429 | if (r < 0) | |
1430 | return r; | |
1431 | } | |
1432 | ||
1433 | if (uid_is_valid(uid_shift)) { | |
1434 | if (asprintf(&options, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) | |
1435 | return -ENOMEM; | |
1436 | } | |
1437 | ||
1438 | r = mount_nofollow_verbose(LOG_DEBUG, "rootfs", where, "tmpfs", MS_NODEV, options); | |
1439 | if (r < 0) | |
1440 | return r; | |
1441 | ||
1442 | return 1; | |
1443 | } | |
1444 | ||
21b61b1d LP |
1445 | int dissected_image_mount( |
1446 | DissectedImage *m, | |
1447 | const char *where, | |
1448 | uid_t uid_shift, | |
1449 | uid_t uid_range, | |
1450 | DissectImageFlags flags) { | |
1451 | ||
1f0f82f1 | 1452 | int r, xbootldr_mounted; |
8c1be37e LP |
1453 | |
1454 | assert(m); | |
1455 | assert(where); | |
1456 | ||
fa45d12c LP |
1457 | /* Returns: |
1458 | * | |
1459 | * -ENXIO → No root partition found | |
7718ac97 | 1460 | * -EMEDIUMTYPE → DISSECT_IMAGE_VALIDATE_OS set but no os-release/extension-release file found |
fa45d12c LP |
1461 | * -EUNATCH → Encrypted partition found for which no dm-crypt was set up yet |
1462 | * -EUCLEAN → fsck for file system failed | |
1463 | * -EBUSY → File system already mounted/used elsewhere (kernel) | |
4dc28665 | 1464 | * -EAFNOSUPPORT → File system type not supported or not known |
fa45d12c LP |
1465 | */ |
1466 | ||
7cf66030 LP |
1467 | if (!(m->partitions[PARTITION_ROOT].found || |
1468 | (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT)))) | |
1469 | return -ENXIO; /* Require a root fs or at least a /usr/ fs (the latter is subject to a flag of its own) */ | |
8c1be37e | 1470 | |
2d3a5a73 | 1471 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { |
7cf66030 LP |
1472 | |
1473 | /* First mount the root fs. If there's none we use a tmpfs. */ | |
1474 | if (m->partitions[PARTITION_ROOT].found) | |
21b61b1d | 1475 | r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, uid_range, flags); |
7cf66030 LP |
1476 | else |
1477 | r = mount_root_tmpfs(where, uid_shift, flags); | |
2d3a5a73 LP |
1478 | if (r < 0) |
1479 | return r; | |
aee36b4e | 1480 | |
aee36b4e | 1481 | /* For us mounting root always means mounting /usr as well */ |
21b61b1d | 1482 | r = mount_partition(m->partitions + PARTITION_USR, where, "/usr", uid_shift, uid_range, flags); |
aee36b4e LP |
1483 | if (r < 0) |
1484 | return r; | |
03bcb6d4 | 1485 | |
9ccb531a LB |
1486 | if ((flags & (DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_VALIDATE_OS_EXT)) != 0) { |
1487 | /* If either one of the validation flags are set, ensure that the image qualifies | |
1488 | * as one or the other (or both). */ | |
1489 | bool ok = false; | |
1490 | ||
1491 | if (FLAGS_SET(flags, DISSECT_IMAGE_VALIDATE_OS)) { | |
1492 | r = path_is_os_tree(where); | |
1493 | if (r < 0) | |
1494 | return r; | |
1495 | if (r > 0) | |
1496 | ok = true; | |
1497 | } | |
1498 | if (!ok && FLAGS_SET(flags, DISSECT_IMAGE_VALIDATE_OS_EXT)) { | |
06768b90 | 1499 | r = path_is_extension_tree(where, m->image_name, FLAGS_SET(flags, DISSECT_IMAGE_RELAX_SYSEXT_CHECK)); |
7718ac97 LB |
1500 | if (r < 0) |
1501 | return r; | |
9ccb531a LB |
1502 | if (r > 0) |
1503 | ok = true; | |
7718ac97 | 1504 | } |
9ccb531a LB |
1505 | |
1506 | if (!ok) | |
1507 | return -ENOMEDIUM; | |
03bcb6d4 | 1508 | } |
2d3a5a73 LP |
1509 | } |
1510 | ||
705727fd | 1511 | if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY) |
2d3a5a73 | 1512 | return 0; |
8c1be37e | 1513 | |
21b61b1d | 1514 | r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, uid_range, flags); |
8c1be37e LP |
1515 | if (r < 0) |
1516 | return r; | |
1517 | ||
21b61b1d | 1518 | r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, uid_range, flags); |
8c1be37e LP |
1519 | if (r < 0) |
1520 | return r; | |
1521 | ||
21b61b1d | 1522 | r = mount_partition(m->partitions + PARTITION_VAR, where, "/var", uid_shift, uid_range, flags); |
d4dffb85 LP |
1523 | if (r < 0) |
1524 | return r; | |
1525 | ||
21b61b1d | 1526 | r = mount_partition(m->partitions + PARTITION_TMP, where, "/var/tmp", uid_shift, uid_range, flags); |
d4dffb85 LP |
1527 | if (r < 0) |
1528 | return r; | |
1529 | ||
21b61b1d | 1530 | xbootldr_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, uid_range, flags); |
1f0f82f1 LP |
1531 | if (xbootldr_mounted < 0) |
1532 | return xbootldr_mounted; | |
d9223c07 | 1533 | |
8c1be37e | 1534 | if (m->partitions[PARTITION_ESP].found) { |
1f0f82f1 LP |
1535 | int esp_done = false; |
1536 | ||
d9223c07 LP |
1537 | /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it |
1538 | * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */ | |
8c1be37e | 1539 | |
a5648b80 | 1540 | r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL); |
1f0f82f1 LP |
1541 | if (r < 0) { |
1542 | if (r != -ENOENT) | |
d9223c07 | 1543 | return r; |
8c1be37e | 1544 | |
1f0f82f1 LP |
1545 | /* /efi doesn't exist. Let's see if /boot is suitable then */ |
1546 | ||
1547 | if (!xbootldr_mounted) { | |
1548 | _cleanup_free_ char *p = NULL; | |
2eedfd2d | 1549 | |
1f0f82f1 LP |
1550 | r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p, NULL); |
1551 | if (r < 0) { | |
1552 | if (r != -ENOENT) | |
1553 | return r; | |
db55bbf2 | 1554 | } else if (dir_is_empty(p, /* ignore_hidden_or_backup= */ false) > 0) { |
1f0f82f1 | 1555 | /* It exists and is an empty directory. Let's mount the ESP there. */ |
21b61b1d | 1556 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, uid_range, flags); |
1f0f82f1 LP |
1557 | if (r < 0) |
1558 | return r; | |
1559 | ||
1560 | esp_done = true; | |
1561 | } | |
2eedfd2d | 1562 | } |
8c1be37e | 1563 | } |
1f0f82f1 LP |
1564 | |
1565 | if (!esp_done) { | |
1566 | /* OK, let's mount the ESP now to /efi (possibly creating the dir if missing) */ | |
1567 | ||
21b61b1d | 1568 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, uid_range, flags); |
1f0f82f1 LP |
1569 | if (r < 0) |
1570 | return r; | |
1571 | } | |
8c1be37e LP |
1572 | } |
1573 | ||
1574 | return 0; | |
1575 | } | |
1576 | ||
21b61b1d LP |
1577 | int dissected_image_mount_and_warn( |
1578 | DissectedImage *m, | |
1579 | const char *where, | |
1580 | uid_t uid_shift, | |
1581 | uid_t uid_range, | |
1582 | DissectImageFlags flags) { | |
1583 | ||
af187ab2 LP |
1584 | int r; |
1585 | ||
1586 | assert(m); | |
1587 | assert(where); | |
1588 | ||
21b61b1d | 1589 | r = dissected_image_mount(m, where, uid_shift, uid_range, flags); |
af187ab2 LP |
1590 | if (r == -ENXIO) |
1591 | return log_error_errno(r, "Not root file system found in image."); | |
1592 | if (r == -EMEDIUMTYPE) | |
7718ac97 | 1593 | return log_error_errno(r, "No suitable os-release/extension-release file in image found."); |
af187ab2 LP |
1594 | if (r == -EUNATCH) |
1595 | return log_error_errno(r, "Encrypted file system discovered, but decryption not requested."); | |
1596 | if (r == -EUCLEAN) | |
1597 | return log_error_errno(r, "File system check on image failed."); | |
1598 | if (r == -EBUSY) | |
1599 | return log_error_errno(r, "File system already mounted elsewhere."); | |
4dc28665 LP |
1600 | if (r == -EAFNOSUPPORT) |
1601 | return log_error_errno(r, "File system type not supported or not known."); | |
af187ab2 LP |
1602 | if (r < 0) |
1603 | return log_error_errno(r, "Failed to mount image: %m"); | |
1604 | ||
1605 | return r; | |
1606 | } | |
1607 | ||
349cc4a5 | 1608 | #if HAVE_LIBCRYPTSETUP |
9321ad51 | 1609 | struct DecryptedPartition { |
18b5886e LP |
1610 | struct crypt_device *device; |
1611 | char *name; | |
1612 | bool relinquished; | |
9321ad51 YW |
1613 | }; |
1614 | #endif | |
1615 | ||
1616 | typedef struct DecryptedPartition DecryptedPartition; | |
18b5886e LP |
1617 | |
1618 | struct DecryptedImage { | |
9321ad51 | 1619 | unsigned n_ref; |
18b5886e LP |
1620 | DecryptedPartition *decrypted; |
1621 | size_t n_decrypted; | |
18b5886e | 1622 | }; |
18b5886e | 1623 | |
9321ad51 | 1624 | static DecryptedImage* decrypted_image_free(DecryptedImage *d) { |
349cc4a5 | 1625 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1626 | int r; |
1627 | ||
1628 | if (!d) | |
1629 | return NULL; | |
1630 | ||
67f63ee5 | 1631 | for (size_t i = 0; i < d->n_decrypted; i++) { |
18b5886e LP |
1632 | DecryptedPartition *p = d->decrypted + i; |
1633 | ||
1634 | if (p->device && p->name && !p->relinquished) { | |
ea16d7f4 YW |
1635 | /* Let's deactivate lazily, as the dm volume may be already/still used by other processes. */ |
1636 | r = sym_crypt_deactivate_by_name(p->device, p->name, CRYPT_DEACTIVATE_DEFERRED); | |
18b5886e LP |
1637 | if (r < 0) |
1638 | log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name); | |
1639 | } | |
1640 | ||
1641 | if (p->device) | |
0d12936d | 1642 | sym_crypt_free(p->device); |
18b5886e LP |
1643 | free(p->name); |
1644 | } | |
1645 | ||
f91861e4 | 1646 | free(d->decrypted); |
18b5886e LP |
1647 | free(d); |
1648 | #endif | |
1649 | return NULL; | |
1650 | } | |
1651 | ||
9321ad51 YW |
1652 | DEFINE_TRIVIAL_REF_UNREF_FUNC(DecryptedImage, decrypted_image, decrypted_image_free); |
1653 | ||
349cc4a5 | 1654 | #if HAVE_LIBCRYPTSETUP |
9321ad51 YW |
1655 | static int decrypted_image_new(DecryptedImage **ret) { |
1656 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; | |
1657 | ||
1658 | assert(ret); | |
1659 | ||
1660 | d = new(DecryptedImage, 1); | |
1661 | if (!d) | |
1662 | return -ENOMEM; | |
1663 | ||
1664 | *d = (DecryptedImage) { | |
1665 | .n_ref = 1, | |
1666 | }; | |
1667 | ||
1668 | *ret = TAKE_PTR(d); | |
1669 | return 0; | |
1670 | } | |
4623e8e6 LP |
1671 | |
1672 | static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) { | |
1673 | _cleanup_free_ char *name = NULL, *node = NULL; | |
1674 | const char *base; | |
1675 | ||
1676 | assert(original_node); | |
1677 | assert(suffix); | |
1678 | assert(ret_name); | |
1679 | assert(ret_node); | |
1680 | ||
1681 | base = strrchr(original_node, '/'); | |
1682 | if (!base) | |
ac1f3ad0 LB |
1683 | base = original_node; |
1684 | else | |
1685 | base++; | |
4623e8e6 LP |
1686 | if (isempty(base)) |
1687 | return -EINVAL; | |
1688 | ||
1689 | name = strjoin(base, suffix); | |
1690 | if (!name) | |
1691 | return -ENOMEM; | |
1692 | if (!filename_is_valid(name)) | |
1693 | return -EINVAL; | |
1694 | ||
0d12936d | 1695 | node = path_join(sym_crypt_get_dir(), name); |
4623e8e6 LP |
1696 | if (!node) |
1697 | return -ENOMEM; | |
1698 | ||
1cc6c93a YW |
1699 | *ret_name = TAKE_PTR(name); |
1700 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 1701 | |
4623e8e6 LP |
1702 | return 0; |
1703 | } | |
1704 | ||
18b5886e LP |
1705 | static int decrypt_partition( |
1706 | DissectedPartition *m, | |
1707 | const char *passphrase, | |
1708 | DissectImageFlags flags, | |
1709 | DecryptedImage *d) { | |
1710 | ||
1711 | _cleanup_free_ char *node = NULL, *name = NULL; | |
0d12936d | 1712 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
f7725647 | 1713 | _cleanup_close_ int fd = -1; |
18b5886e LP |
1714 | int r; |
1715 | ||
1716 | assert(m); | |
1717 | assert(d); | |
1718 | ||
1719 | if (!m->found || !m->node || !m->fstype) | |
1720 | return 0; | |
1721 | ||
1722 | if (!streq(m->fstype, "crypto_LUKS")) | |
1723 | return 0; | |
1724 | ||
bdd73ac5 ZJS |
1725 | if (!passphrase) |
1726 | return -ENOKEY; | |
1727 | ||
0d12936d LP |
1728 | r = dlopen_cryptsetup(); |
1729 | if (r < 0) | |
1730 | return r; | |
1731 | ||
4623e8e6 LP |
1732 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
1733 | if (r < 0) | |
1734 | return r; | |
18b5886e | 1735 | |
319a4f4b | 1736 | if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1)) |
18b5886e LP |
1737 | return -ENOMEM; |
1738 | ||
0d12936d | 1739 | r = sym_crypt_init(&cd, m->node); |
18b5886e | 1740 | if (r < 0) |
715cbb81 | 1741 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 1742 | |
efc3b12f | 1743 | cryptsetup_enable_logging(cd); |
1887032f | 1744 | |
0d12936d | 1745 | r = sym_crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
1746 | if (r < 0) |
1747 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e | 1748 | |
0d12936d | 1749 | r = sym_crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), |
ef9c184d | 1750 | ((flags & DISSECT_IMAGE_DEVICE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | |
0d12936d | 1751 | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); |
294bd454 | 1752 | if (r < 0) { |
715cbb81 | 1753 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 1754 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 1755 | } |
18b5886e | 1756 | |
f7725647 YW |
1757 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); |
1758 | if (fd < 0) | |
1759 | return log_debug_errno(errno, "Failed to open %s: %m", node); | |
1760 | ||
94344385 LP |
1761 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
1762 | .name = TAKE_PTR(name), | |
1763 | .device = TAKE_PTR(cd), | |
1764 | }; | |
18b5886e | 1765 | |
1cc6c93a | 1766 | m->decrypted_node = TAKE_PTR(node); |
f7725647 | 1767 | close_and_replace(m->mount_node_fd, fd); |
18b5886e LP |
1768 | |
1769 | return 0; | |
4623e8e6 LP |
1770 | } |
1771 | ||
89e62e0b LP |
1772 | static int verity_can_reuse( |
1773 | const VeritySettings *verity, | |
1774 | const char *name, | |
1775 | struct crypt_device **ret_cd) { | |
1776 | ||
ac1f3ad0 LB |
1777 | /* If the same volume was already open, check that the root hashes match, and reuse it if they do */ |
1778 | _cleanup_free_ char *root_hash_existing = NULL; | |
0d12936d | 1779 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 1780 | struct crypt_params_verity crypt_params = {}; |
89e62e0b | 1781 | size_t root_hash_existing_size; |
ac1f3ad0 LB |
1782 | int r; |
1783 | ||
89e62e0b LP |
1784 | assert(verity); |
1785 | assert(name); | |
ac1f3ad0 LB |
1786 | assert(ret_cd); |
1787 | ||
0d12936d | 1788 | r = sym_crypt_init_by_name(&cd, name); |
ac1f3ad0 LB |
1789 | if (r < 0) |
1790 | return log_debug_errno(r, "Error opening verity device, crypt_init_by_name failed: %m"); | |
0d12936d | 1791 | |
c719805e LP |
1792 | cryptsetup_enable_logging(cd); |
1793 | ||
0d12936d | 1794 | r = sym_crypt_get_verity_info(cd, &crypt_params); |
ac1f3ad0 LB |
1795 | if (r < 0) |
1796 | return log_debug_errno(r, "Error opening verity device, crypt_get_verity_info failed: %m"); | |
0d12936d | 1797 | |
89e62e0b LP |
1798 | root_hash_existing_size = verity->root_hash_size; |
1799 | root_hash_existing = malloc0(root_hash_existing_size); | |
ac1f3ad0 LB |
1800 | if (!root_hash_existing) |
1801 | return -ENOMEM; | |
0d12936d LP |
1802 | |
1803 | r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash_existing, &root_hash_existing_size, NULL, 0); | |
ac1f3ad0 LB |
1804 | if (r < 0) |
1805 | return log_debug_errno(r, "Error opening verity device, crypt_volume_key_get failed: %m"); | |
89e62e0b LP |
1806 | if (verity->root_hash_size != root_hash_existing_size || |
1807 | memcmp(root_hash_existing, verity->root_hash, verity->root_hash_size) != 0) | |
ac1f3ad0 | 1808 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but root hashes are different."); |
89e62e0b | 1809 | |
ac1f3ad0 | 1810 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY |
89e62e0b LP |
1811 | /* Ensure that, if signatures are supported, we only reuse the device if the previous mount used the |
1812 | * same settings, so that a previous unsigned mount will not be reused if the user asks to use | |
28423d9a | 1813 | * signing for the new one, and vice versa. */ |
89e62e0b | 1814 | if (!!verity->root_hash_sig != !!(crypt_params.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE)) |
ac1f3ad0 LB |
1815 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but signature settings are not the same."); |
1816 | #endif | |
1817 | ||
1818 | *ret_cd = TAKE_PTR(cd); | |
1819 | return 0; | |
1820 | } | |
1821 | ||
75db809a | 1822 | static inline char* dm_deferred_remove_clean(char *name) { |
ac1f3ad0 | 1823 | if (!name) |
75db809a | 1824 | return NULL; |
0d12936d LP |
1825 | |
1826 | (void) sym_crypt_deactivate_by_name(NULL, name, CRYPT_DEACTIVATE_DEFERRED); | |
75db809a | 1827 | return mfree(name); |
ac1f3ad0 LB |
1828 | } |
1829 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, dm_deferred_remove_clean); | |
1830 | ||
c2fa92e7 LP |
1831 | static int validate_signature_userspace(const VeritySettings *verity) { |
1832 | #if HAVE_OPENSSL | |
1833 | _cleanup_(sk_X509_free_allp) STACK_OF(X509) *sk = NULL; | |
1834 | _cleanup_strv_free_ char **certs = NULL; | |
1835 | _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL; | |
1836 | _cleanup_free_ char *s = NULL; | |
1837 | _cleanup_(BIO_freep) BIO *bio = NULL; /* 'bio' must be freed first, 's' second, hence keep this order | |
1838 | * of declaration in place, please */ | |
1839 | const unsigned char *d; | |
c2fa92e7 LP |
1840 | int r; |
1841 | ||
1842 | assert(verity); | |
1843 | assert(verity->root_hash); | |
1844 | assert(verity->root_hash_sig); | |
1845 | ||
1846 | /* Because installing a signature certificate into the kernel chain is so messy, let's optionally do | |
1847 | * userspace validation. */ | |
1848 | ||
1849 | r = conf_files_list_nulstr(&certs, ".crt", NULL, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, CONF_PATHS_NULSTR("verity.d")); | |
1850 | if (r < 0) | |
1851 | return log_debug_errno(r, "Failed to enumerate certificates: %m"); | |
1852 | if (strv_isempty(certs)) { | |
1853 | log_debug("No userspace dm-verity certificates found."); | |
1854 | return 0; | |
1855 | } | |
1856 | ||
1857 | d = verity->root_hash_sig; | |
1858 | p7 = d2i_PKCS7(NULL, &d, (long) verity->root_hash_sig_size); | |
1859 | if (!p7) | |
1860 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse PKCS7 DER signature data."); | |
1861 | ||
1862 | s = hexmem(verity->root_hash, verity->root_hash_size); | |
1863 | if (!s) | |
1864 | return log_oom_debug(); | |
1865 | ||
1866 | bio = BIO_new_mem_buf(s, strlen(s)); | |
1867 | if (!bio) | |
1868 | return log_oom_debug(); | |
1869 | ||
1870 | sk = sk_X509_new_null(); | |
1871 | if (!sk) | |
1872 | return log_oom_debug(); | |
1873 | ||
1874 | STRV_FOREACH(i, certs) { | |
1875 | _cleanup_(X509_freep) X509 *c = NULL; | |
1876 | _cleanup_fclose_ FILE *f = NULL; | |
1877 | ||
1878 | f = fopen(*i, "re"); | |
1879 | if (!f) { | |
1880 | log_debug_errno(errno, "Failed to open '%s', ignoring: %m", *i); | |
1881 | continue; | |
1882 | } | |
1883 | ||
1884 | c = PEM_read_X509(f, NULL, NULL, NULL); | |
1885 | if (!c) { | |
1886 | log_debug("Failed to load X509 certificate '%s', ignoring.", *i); | |
1887 | continue; | |
1888 | } | |
1889 | ||
1890 | if (sk_X509_push(sk, c) == 0) | |
1891 | return log_oom_debug(); | |
1892 | ||
1893 | TAKE_PTR(c); | |
1894 | } | |
1895 | ||
1896 | r = PKCS7_verify(p7, sk, NULL, bio, NULL, PKCS7_NOINTERN|PKCS7_NOVERIFY); | |
1897 | if (r) | |
1898 | log_debug("Userspace PKCS#7 validation succeeded."); | |
1899 | else | |
1900 | log_debug("Userspace PKCS#7 validation failed: %s", ERR_error_string(ERR_get_error(), NULL)); | |
1901 | ||
1902 | return r; | |
1903 | #else | |
1904 | log_debug("Not doing client-side validation of dm-verity root hash signatures, OpenSSL support disabled."); | |
1905 | return 0; | |
1906 | #endif | |
1907 | } | |
1908 | ||
1909 | static int do_crypt_activate_verity( | |
1910 | struct crypt_device *cd, | |
1911 | const char *name, | |
1912 | const VeritySettings *verity) { | |
1913 | ||
1914 | bool check_signature; | |
1915 | int r; | |
1916 | ||
1917 | assert(cd); | |
1918 | assert(name); | |
1919 | assert(verity); | |
1920 | ||
1921 | if (verity->root_hash_sig) { | |
1922 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_SIGNATURE"); | |
1923 | if (r < 0 && r != -ENXIO) | |
1924 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_SIGNATURE"); | |
1925 | ||
1926 | check_signature = r != 0; | |
1927 | } else | |
1928 | check_signature = false; | |
1929 | ||
1930 | if (check_signature) { | |
1931 | ||
1932 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY | |
1933 | /* First, if we have support for signed keys in the kernel, then try that first. */ | |
1934 | r = sym_crypt_activate_by_signed_key( | |
1935 | cd, | |
1936 | name, | |
1937 | verity->root_hash, | |
1938 | verity->root_hash_size, | |
1939 | verity->root_hash_sig, | |
1940 | verity->root_hash_sig_size, | |
1941 | CRYPT_ACTIVATE_READONLY); | |
1942 | if (r >= 0) | |
1943 | return r; | |
1944 | ||
1945 | log_debug("Validation of dm-verity signature failed via the kernel, trying userspace validation instead."); | |
1946 | #else | |
1947 | log_debug("Activation of verity device with signature requested, but not supported via the kernel by %s due to missing crypt_activate_by_signed_key(), trying userspace validation instead.", | |
1948 | program_invocation_short_name); | |
1949 | #endif | |
1950 | ||
1951 | /* So this didn't work via the kernel, then let's try userspace validation instead. If that | |
1952 | * works we'll try to activate without telling the kernel the signature. */ | |
1953 | ||
1954 | r = validate_signature_userspace(verity); | |
1955 | if (r < 0) | |
1956 | return r; | |
1957 | if (r == 0) | |
1958 | return log_debug_errno(SYNTHETIC_ERRNO(ENOKEY), | |
1959 | "Activation of signed Verity volume worked neither via the kernel nor in userspace, can't activate."); | |
1960 | } | |
1961 | ||
1962 | return sym_crypt_activate_by_volume_key( | |
1963 | cd, | |
1964 | name, | |
1965 | verity->root_hash, | |
1966 | verity->root_hash_size, | |
1967 | CRYPT_ACTIVATE_READONLY); | |
1968 | } | |
1969 | ||
ad361a50 YW |
1970 | static usec_t verity_timeout(void) { |
1971 | usec_t t = 100 * USEC_PER_MSEC; | |
1972 | const char *e; | |
1973 | int r; | |
1974 | ||
1975 | /* On slower machines, like non-KVM vm, setting up device may take a long time. | |
1976 | * Let's make the timeout configurable. */ | |
1977 | ||
1978 | e = getenv("SYSTEMD_DISSECT_VERITY_TIMEOUT_SEC"); | |
1979 | if (!e) | |
1980 | return t; | |
1981 | ||
1982 | r = parse_sec(e, &t); | |
1983 | if (r < 0) | |
1984 | log_debug_errno(r, | |
1985 | "Failed to parse timeout specified in $SYSTEMD_DISSECT_VERITY_TIMEOUT_SEC, " | |
1986 | "using the default timeout (%s).", | |
1987 | FORMAT_TIMESPAN(t, USEC_PER_MSEC)); | |
1988 | ||
1989 | return t; | |
1990 | } | |
1991 | ||
4623e8e6 | 1992 | static int verity_partition( |
aee36b4e | 1993 | PartitionDesignator designator, |
4623e8e6 LP |
1994 | DissectedPartition *m, |
1995 | DissectedPartition *v, | |
89e62e0b | 1996 | const VeritySettings *verity, |
4623e8e6 LP |
1997 | DissectImageFlags flags, |
1998 | DecryptedImage *d) { | |
1999 | ||
0d12936d | 2000 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 2001 | _cleanup_(dm_deferred_remove_cleanp) char *restore_deferred_remove = NULL; |
89e62e0b | 2002 | _cleanup_free_ char *node = NULL, *name = NULL; |
f7725647 | 2003 | _cleanup_close_ int mount_node_fd = -1; |
4623e8e6 LP |
2004 | int r; |
2005 | ||
2006 | assert(m); | |
89e62e0b | 2007 | assert(v || (verity && verity->data_path)); |
4623e8e6 | 2008 | |
89e62e0b | 2009 | if (!verity || !verity->root_hash) |
4623e8e6 | 2010 | return 0; |
aee36b4e LP |
2011 | if (!((verity->designator < 0 && designator == PARTITION_ROOT) || |
2012 | (verity->designator == designator))) | |
2013 | return 0; | |
4623e8e6 LP |
2014 | |
2015 | if (!m->found || !m->node || !m->fstype) | |
2016 | return 0; | |
89e62e0b | 2017 | if (!verity->data_path) { |
e7cbe5cb LB |
2018 | if (!v->found || !v->node || !v->fstype) |
2019 | return 0; | |
4623e8e6 | 2020 | |
e7cbe5cb LB |
2021 | if (!streq(v->fstype, "DM_verity_hash")) |
2022 | return 0; | |
2023 | } | |
4623e8e6 | 2024 | |
0d12936d LP |
2025 | r = dlopen_cryptsetup(); |
2026 | if (r < 0) | |
2027 | return r; | |
2028 | ||
ac1f3ad0 LB |
2029 | if (FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) { |
2030 | /* Use the roothash, which is unique per volume, as the device node name, so that it can be reused */ | |
2031 | _cleanup_free_ char *root_hash_encoded = NULL; | |
0d12936d | 2032 | |
89e62e0b | 2033 | root_hash_encoded = hexmem(verity->root_hash, verity->root_hash_size); |
ac1f3ad0 LB |
2034 | if (!root_hash_encoded) |
2035 | return -ENOMEM; | |
aee36b4e | 2036 | |
ac1f3ad0 LB |
2037 | r = make_dm_name_and_node(root_hash_encoded, "-verity", &name, &node); |
2038 | } else | |
2039 | r = make_dm_name_and_node(m->node, "-verity", &name, &node); | |
4623e8e6 LP |
2040 | if (r < 0) |
2041 | return r; | |
2042 | ||
89e62e0b | 2043 | r = sym_crypt_init(&cd, verity->data_path ?: v->node); |
4623e8e6 LP |
2044 | if (r < 0) |
2045 | return r; | |
2046 | ||
efc3b12f | 2047 | cryptsetup_enable_logging(cd); |
1887032f | 2048 | |
0d12936d | 2049 | r = sym_crypt_load(cd, CRYPT_VERITY, NULL); |
4623e8e6 | 2050 | if (r < 0) |
294bd454 | 2051 | return r; |
4623e8e6 | 2052 | |
0d12936d | 2053 | r = sym_crypt_set_data_device(cd, m->node); |
4623e8e6 | 2054 | if (r < 0) |
294bd454 | 2055 | return r; |
4623e8e6 | 2056 | |
319a4f4b | 2057 | if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1)) |
ac1f3ad0 LB |
2058 | return -ENOMEM; |
2059 | ||
2060 | /* If activating fails because the device already exists, check the metadata and reuse it if it matches. | |
2061 | * In case of ENODEV/ENOENT, which can happen if another process is activating at the exact same time, | |
2062 | * retry a few times before giving up. */ | |
2063 | for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { | |
1d369d78 | 2064 | _cleanup_(sym_crypt_freep) struct crypt_device *existing_cd = NULL; |
f80015ff | 2065 | _cleanup_close_ int fd = -1; |
c2fa92e7 | 2066 | |
f80015ff YW |
2067 | /* First, check if the device already exists. */ |
2068 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
2069 | if (fd < 0 && !ERRNO_IS_DEVICE_ABSENT(errno)) | |
2070 | return log_debug_errno(errno, "Failed to open verity device %s: %m", node); | |
2071 | if (fd >= 0) | |
2072 | goto check; /* The device already exists. Let's check it. */ | |
2073 | ||
2074 | /* The symlink to the device node does not exist yet. Assume not activated, and let's activate it. */ | |
c2fa92e7 | 2075 | r = do_crypt_activate_verity(cd, name, verity); |
1d369d78 | 2076 | if (r >= 0) |
f7725647 | 2077 | goto try_open; /* The device is activated. Let's open it. */ |
ac1f3ad0 LB |
2078 | /* libdevmapper can return EINVAL when the device is already in the activation stage. |
2079 | * There's no way to distinguish this situation from a genuine error due to invalid | |
2aed63f4 | 2080 | * parameters, so immediately fall back to activating the device with a unique name. |
89e62e0b LP |
2081 | * Improvements in libcrypsetup can ensure this never happens: |
2082 | * https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/96 */ | |
ac1f3ad0 | 2083 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) |
0f75b0c5 | 2084 | break; |
1d369d78 YW |
2085 | if (r == -ENODEV) /* Volume is being opened but not ready, crypt_init_by_name would fail, try to open again */ |
2086 | goto try_again; | |
2087 | if (!IN_SET(r, | |
2088 | -EEXIST, /* Volume has already been opened and ready to be used. */ | |
2089 | -EBUSY /* Volume is being opened but not ready, crypt_init_by_name() can fetch details. */)) | |
2090 | return log_debug_errno(r, "Failed to activate verity device %s: %m", node); | |
2091 | ||
f80015ff | 2092 | check: |
1d369d78 YW |
2093 | if (!restore_deferred_remove){ |
2094 | /* To avoid races, disable automatic removal on umount while setting up the new device. Restore it on failure. */ | |
2095 | r = dm_deferred_remove_cancel(name); | |
12f5fbdf YW |
2096 | /* -EBUSY and -ENXIO: the device has already been removed or being removed. We cannot |
2097 | * use the device, try to open again. See target_message() in drivers/md/dm-ioctl.c | |
2098 | * and dm_cancel_deferred_remove() in drivers/md/dm.c */ | |
2099 | if (IN_SET(r, -EBUSY, -ENXIO)) | |
2100 | goto try_again; | |
2101 | if (r < 0) | |
1d369d78 | 2102 | return log_debug_errno(r, "Failed to disable automated deferred removal for verity device %s: %m", node); |
12f5fbdf YW |
2103 | |
2104 | restore_deferred_remove = strdup(name); | |
2105 | if (!restore_deferred_remove) | |
2106 | return log_oom_debug(); | |
1d369d78 | 2107 | } |
c2923fdc | 2108 | |
1d369d78 YW |
2109 | r = verity_can_reuse(verity, name, &existing_cd); |
2110 | /* Same as above, -EINVAL can randomly happen when it actually means -EEXIST */ | |
2111 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
2112 | break; | |
2113 | if (IN_SET(r, | |
2114 | -ENOENT, /* Removed?? */ | |
2115 | -EBUSY, /* Volume is being opened but not ready, crypt_init_by_name() can fetch details. */ | |
2116 | -ENODEV /* Volume is being opened but not ready, crypt_init_by_name() would fail, try to open again. */ )) | |
2117 | goto try_again; | |
2118 | if (r < 0) | |
2119 | return log_debug_errno(r, "Failed to check if existing verity device %s can be reused: %m", node); | |
2120 | ||
f80015ff | 2121 | if (fd < 0) { |
1d369d78 YW |
2122 | /* devmapper might say that the device exists, but the devlink might not yet have been |
2123 | * created. Check and wait for the udev event in that case. */ | |
2124 | r = device_wait_for_devlink(node, "block", verity_timeout(), NULL); | |
2125 | /* Fallback to activation with a unique device if it's taking too long */ | |
2126 | if (r == -ETIMEDOUT && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
0f75b0c5 | 2127 | break; |
1d369d78 YW |
2128 | if (r < 0) |
2129 | return log_debug_errno(r, "Failed to wait device node symlink %s: %m", node); | |
c2923fdc | 2130 | } |
ecab4c47 | 2131 | |
f7725647 YW |
2132 | try_open: |
2133 | if (fd < 0) { | |
2134 | /* Now, the device is activated and devlink is created. Let's open it. */ | |
2135 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
2136 | if (fd < 0) { | |
2137 | if (!ERRNO_IS_DEVICE_ABSENT(errno)) | |
2138 | return log_debug_errno(errno, "Failed to open verity device %s: %m", node); | |
2139 | ||
2140 | /* The device has already been removed?? */ | |
2141 | goto try_again; | |
2142 | } | |
2143 | } | |
2144 | ||
2145 | mount_node_fd = TAKE_FD(fd); | |
1d369d78 YW |
2146 | if (existing_cd) |
2147 | crypt_free_and_replace(cd, existing_cd); | |
2148 | ||
2149 | goto success; | |
2150 | ||
2151 | try_again: | |
2152 | /* Device is being removed by another process. Let's wait for a while. */ | |
ecab4c47 | 2153 | (void) usleep(2 * USEC_PER_MSEC); |
ac1f3ad0 LB |
2154 | } |
2155 | ||
0f75b0c5 YW |
2156 | /* All trials failed or a conflicting verity device exists. Let's try to activate with a unique name. */ |
2157 | if (FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) { | |
2158 | /* Before trying to activate with unique name, we need to free crypt_device object. | |
2159 | * Otherwise, we get error from libcryptsetup like the following: | |
2160 | * ------ | |
2161 | * systemd[1234]: Cannot use device /dev/loop5 which is in use (already mapped or mounted). | |
2162 | * ------ | |
2163 | */ | |
2164 | sym_crypt_free(cd); | |
2165 | cd = NULL; | |
aee36b4e | 2166 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
0f75b0c5 | 2167 | } |
ac1f3ad0 | 2168 | |
9972e6d6 YW |
2169 | return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "All attempts to activate verity device %s failed.", name); |
2170 | ||
2171 | success: | |
ac1f3ad0 LB |
2172 | /* Everything looks good and we'll be able to mount the device, so deferred remove will be re-enabled at that point. */ |
2173 | restore_deferred_remove = mfree(restore_deferred_remove); | |
4623e8e6 | 2174 | |
94344385 LP |
2175 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
2176 | .name = TAKE_PTR(name), | |
2177 | .device = TAKE_PTR(cd), | |
2178 | }; | |
4623e8e6 | 2179 | |
1cc6c93a | 2180 | m->decrypted_node = TAKE_PTR(node); |
f7725647 | 2181 | close_and_replace(m->mount_node_fd, mount_node_fd); |
4623e8e6 LP |
2182 | |
2183 | return 0; | |
18b5886e LP |
2184 | } |
2185 | #endif | |
2186 | ||
2187 | int dissected_image_decrypt( | |
2188 | DissectedImage *m, | |
2189 | const char *passphrase, | |
89e62e0b | 2190 | const VeritySettings *verity, |
e330f97a | 2191 | DissectImageFlags flags) { |
18b5886e | 2192 | |
349cc4a5 | 2193 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 2194 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
2195 | int r; |
2196 | #endif | |
2197 | ||
2198 | assert(m); | |
89e62e0b | 2199 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
18b5886e LP |
2200 | |
2201 | /* Returns: | |
2202 | * | |
2203 | * = 0 → There was nothing to decrypt | |
2204 | * > 0 → Decrypted successfully | |
d1c536f5 | 2205 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
2206 | * -EKEYREJECTED → Passed key was not correct |
2207 | */ | |
2208 | ||
89e62e0b | 2209 | if (verity && verity->root_hash && verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
2210 | return -EINVAL; |
2211 | ||
e330f97a | 2212 | if (!m->encrypted && !m->verity_ready) |
18b5886e | 2213 | return 0; |
18b5886e | 2214 | |
349cc4a5 | 2215 | #if HAVE_LIBCRYPTSETUP |
9321ad51 YW |
2216 | r = decrypted_image_new(&d); |
2217 | if (r < 0) | |
2218 | return r; | |
18b5886e | 2219 | |
569a0e42 | 2220 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
18b5886e | 2221 | DissectedPartition *p = m->partitions + i; |
22043172 | 2222 | PartitionDesignator k; |
18b5886e LP |
2223 | |
2224 | if (!p->found) | |
2225 | continue; | |
2226 | ||
2227 | r = decrypt_partition(p, passphrase, flags, d); | |
2228 | if (r < 0) | |
2229 | return r; | |
2230 | ||
dd894023 | 2231 | k = partition_verity_of(i); |
4623e8e6 | 2232 | if (k >= 0) { |
aee36b4e | 2233 | r = verity_partition(i, p, m->partitions + k, verity, flags | DISSECT_IMAGE_VERITY_SHARE, d); |
4623e8e6 LP |
2234 | if (r < 0) |
2235 | return r; | |
2236 | } | |
2237 | ||
d2c6e79d YW |
2238 | if (!p->decrypted_fstype && p->mount_node_fd >= 0 && p->decrypted_node) { |
2239 | r = probe_filesystem_full(p->mount_node_fd, p->decrypted_node, &p->decrypted_fstype); | |
7cc84b2c | 2240 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
2241 | return r; |
2242 | } | |
2243 | } | |
2244 | ||
ac1e1b5f | 2245 | m->decrypted_image = TAKE_PTR(d); |
18b5886e LP |
2246 | |
2247 | return 1; | |
2248 | #else | |
2249 | return -EOPNOTSUPP; | |
2250 | #endif | |
2251 | } | |
2252 | ||
2253 | int dissected_image_decrypt_interactively( | |
2254 | DissectedImage *m, | |
2255 | const char *passphrase, | |
89e62e0b | 2256 | const VeritySettings *verity, |
e330f97a | 2257 | DissectImageFlags flags) { |
18b5886e LP |
2258 | |
2259 | _cleanup_strv_free_erase_ char **z = NULL; | |
2260 | int n = 3, r; | |
2261 | ||
2262 | if (passphrase) | |
2263 | n--; | |
2264 | ||
2265 | for (;;) { | |
e330f97a | 2266 | r = dissected_image_decrypt(m, passphrase, verity, flags); |
18b5886e LP |
2267 | if (r >= 0) |
2268 | return r; | |
2269 | if (r == -EKEYREJECTED) | |
2270 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
fc95c359 YW |
2271 | else if (r != -ENOKEY) |
2272 | return log_error_errno(r, "Failed to decrypt image: %m"); | |
18b5886e | 2273 | |
baaa35ad ZJS |
2274 | if (--n < 0) |
2275 | return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED), | |
2276 | "Too many retries."); | |
18b5886e LP |
2277 | |
2278 | z = strv_free(z); | |
2279 | ||
8806bb4b | 2280 | r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", "dissect.passphrase", USEC_INFINITY, 0, &z); |
18b5886e LP |
2281 | if (r < 0) |
2282 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
2283 | ||
2284 | passphrase = z[0]; | |
2285 | } | |
2286 | } | |
2287 | ||
e330f97a | 2288 | static int decrypted_image_relinquish(DecryptedImage *d) { |
18b5886e LP |
2289 | assert(d); |
2290 | ||
67f63ee5 ZJS |
2291 | /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a |
2292 | * boolean so that we don't clean it up ourselves either anymore */ | |
18b5886e | 2293 | |
349cc4a5 | 2294 | #if HAVE_LIBCRYPTSETUP |
67f63ee5 ZJS |
2295 | int r; |
2296 | ||
2297 | for (size_t i = 0; i < d->n_decrypted; i++) { | |
18b5886e LP |
2298 | DecryptedPartition *p = d->decrypted + i; |
2299 | ||
2300 | if (p->relinquished) | |
2301 | continue; | |
2302 | ||
0d12936d | 2303 | r = sym_crypt_deactivate_by_name(NULL, p->name, CRYPT_DEACTIVATE_DEFERRED); |
18b5886e LP |
2304 | if (r < 0) |
2305 | return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); | |
2306 | ||
2307 | p->relinquished = true; | |
2308 | } | |
2309 | #endif | |
2310 | ||
2311 | return 0; | |
2312 | } | |
2313 | ||
f906075a YW |
2314 | int dissected_image_relinquish(DissectedImage *m) { |
2315 | int r; | |
2316 | ||
2317 | assert(m); | |
2318 | ||
2319 | if (m->decrypted_image) { | |
2320 | r = decrypted_image_relinquish(m->decrypted_image); | |
2321 | if (r < 0) | |
2322 | return r; | |
2323 | } | |
2324 | ||
2325 | if (m->loop) | |
2326 | loop_device_relinquish(m->loop); | |
2327 | ||
2328 | return 0; | |
2329 | } | |
2330 | ||
89e62e0b LP |
2331 | static char *build_auxiliary_path(const char *image, const char *suffix) { |
2332 | const char *e; | |
2333 | char *n; | |
2334 | ||
2335 | assert(image); | |
2336 | assert(suffix); | |
2337 | ||
2338 | e = endswith(image, ".raw"); | |
2339 | if (!e) | |
2340 | return strjoin(e, suffix); | |
2341 | ||
2342 | n = new(char, e - image + strlen(suffix) + 1); | |
2343 | if (!n) | |
2344 | return NULL; | |
2345 | ||
2346 | strcpy(mempcpy(n, image, e - image), suffix); | |
2347 | return n; | |
2348 | } | |
2349 | ||
2350 | void verity_settings_done(VeritySettings *v) { | |
2351 | assert(v); | |
2352 | ||
2353 | v->root_hash = mfree(v->root_hash); | |
2354 | v->root_hash_size = 0; | |
2355 | ||
2356 | v->root_hash_sig = mfree(v->root_hash_sig); | |
2357 | v->root_hash_sig_size = 0; | |
2358 | ||
2359 | v->data_path = mfree(v->data_path); | |
2360 | } | |
2361 | ||
2362 | int verity_settings_load( | |
2363 | VeritySettings *verity, | |
f5ea63a5 LP |
2364 | const char *image, |
2365 | const char *root_hash_path, | |
89e62e0b LP |
2366 | const char *root_hash_sig_path) { |
2367 | ||
2368 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
2369 | size_t root_hash_size = 0, root_hash_sig_size = 0; | |
2370 | _cleanup_free_ char *verity_data_path = NULL; | |
aee36b4e | 2371 | PartitionDesignator designator; |
78ebe980 LP |
2372 | int r; |
2373 | ||
89e62e0b | 2374 | assert(verity); |
78ebe980 | 2375 | assert(image); |
aee36b4e | 2376 | assert(verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
78ebe980 | 2377 | |
89e62e0b LP |
2378 | /* If we are asked to load the root hash for a device node, exit early */ |
2379 | if (is_device_path(image)) | |
78ebe980 | 2380 | return 0; |
78ebe980 | 2381 | |
d5fcc5b0 LP |
2382 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_SIDECAR"); |
2383 | if (r < 0 && r != -ENXIO) | |
2384 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_SIDECAR, ignoring: %m"); | |
2385 | if (r == 0) | |
2386 | return 0; | |
2387 | ||
aee36b4e LP |
2388 | designator = verity->designator; |
2389 | ||
89e62e0b | 2390 | /* We only fill in what isn't already filled in */ |
c2923fdc | 2391 | |
89e62e0b | 2392 | if (!verity->root_hash) { |
e7cbe5cb | 2393 | _cleanup_free_ char *text = NULL; |
e7cbe5cb | 2394 | |
0389f4fa | 2395 | if (root_hash_path) { |
aee36b4e | 2396 | /* If explicitly specified it takes precedence */ |
0389f4fa LB |
2397 | r = read_one_line_file(root_hash_path, &text); |
2398 | if (r < 0) | |
e7cbe5cb | 2399 | return r; |
aee36b4e LP |
2400 | |
2401 | if (designator < 0) | |
2402 | designator = PARTITION_ROOT; | |
0389f4fa | 2403 | } else { |
aee36b4e LP |
2404 | /* Otherwise look for xattr and separate file, and first for the data for root and if |
2405 | * that doesn't exist for /usr */ | |
0389f4fa | 2406 | |
aee36b4e | 2407 | if (designator < 0 || designator == PARTITION_ROOT) { |
c53e07e2 | 2408 | r = getxattr_malloc(image, "user.verity.roothash", &text); |
aee36b4e LP |
2409 | if (r < 0) { |
2410 | _cleanup_free_ char *p = NULL; | |
78ebe980 | 2411 | |
00675c36 | 2412 | if (r != -ENOENT && !ERRNO_IS_XATTR_ABSENT(r)) |
aee36b4e | 2413 | return r; |
e7cbe5cb | 2414 | |
aee36b4e LP |
2415 | p = build_auxiliary_path(image, ".roothash"); |
2416 | if (!p) | |
2417 | return -ENOMEM; | |
2418 | ||
2419 | r = read_one_line_file(p, &text); | |
2420 | if (r < 0 && r != -ENOENT) | |
2421 | return r; | |
2422 | } | |
2423 | ||
2424 | if (text) | |
2425 | designator = PARTITION_ROOT; | |
2426 | } | |
2427 | ||
2428 | if (!text && (designator < 0 || designator == PARTITION_USR)) { | |
2429 | /* So in the "roothash" xattr/file name above the "root" of course primarily | |
2430 | * refers to the root of the Verity Merkle tree. But coincidentally it also | |
2431 | * is the hash for the *root* file system, i.e. the "root" neatly refers to | |
2432 | * two distinct concepts called "root". Taking benefit of this happy | |
2433 | * coincidence we call the file with the root hash for the /usr/ file system | |
2434 | * `usrhash`, because `usrroothash` or `rootusrhash` would just be too | |
2435 | * confusing. We thus drop the reference to the root of the Merkle tree, and | |
2436 | * just indicate which file system it's about. */ | |
c53e07e2 | 2437 | r = getxattr_malloc(image, "user.verity.usrhash", &text); |
aee36b4e LP |
2438 | if (r < 0) { |
2439 | _cleanup_free_ char *p = NULL; | |
2440 | ||
00675c36 | 2441 | if (r != -ENOENT && !ERRNO_IS_XATTR_ABSENT(r)) |
aee36b4e LP |
2442 | return r; |
2443 | ||
2444 | p = build_auxiliary_path(image, ".usrhash"); | |
2445 | if (!p) | |
2446 | return -ENOMEM; | |
2447 | ||
2448 | r = read_one_line_file(p, &text); | |
2449 | if (r < 0 && r != -ENOENT) | |
2450 | return r; | |
2451 | } | |
2452 | ||
2453 | if (text) | |
2454 | designator = PARTITION_USR; | |
0389f4fa | 2455 | } |
e7cbe5cb LB |
2456 | } |
2457 | ||
2458 | if (text) { | |
89e62e0b | 2459 | r = unhexmem(text, strlen(text), &root_hash, &root_hash_size); |
e7cbe5cb LB |
2460 | if (r < 0) |
2461 | return r; | |
89e62e0b | 2462 | if (root_hash_size < sizeof(sd_id128_t)) |
e7cbe5cb LB |
2463 | return -EINVAL; |
2464 | } | |
2465 | } | |
2466 | ||
90f98986 | 2467 | if ((root_hash || verity->root_hash) && !verity->root_hash_sig) { |
aee36b4e | 2468 | if (root_hash_sig_path) { |
ae9cf30b | 2469 | r = read_full_file(root_hash_sig_path, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2470 | if (r < 0 && r != -ENOENT) |
2471 | return r; | |
2472 | ||
2473 | if (designator < 0) | |
2474 | designator = PARTITION_ROOT; | |
2475 | } else { | |
2476 | if (designator < 0 || designator == PARTITION_ROOT) { | |
2477 | _cleanup_free_ char *p = NULL; | |
2478 | ||
2479 | /* Follow naming convention recommended by the relevant RFC: | |
2480 | * https://tools.ietf.org/html/rfc5751#section-3.2.1 */ | |
2481 | p = build_auxiliary_path(image, ".roothash.p7s"); | |
2482 | if (!p) | |
2483 | return -ENOMEM; | |
89e62e0b | 2484 | |
ae9cf30b | 2485 | r = read_full_file(p, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2486 | if (r < 0 && r != -ENOENT) |
2487 | return r; | |
2488 | if (r >= 0) | |
2489 | designator = PARTITION_ROOT; | |
2490 | } | |
2491 | ||
2492 | if (!root_hash_sig && (designator < 0 || designator == PARTITION_USR)) { | |
2493 | _cleanup_free_ char *p = NULL; | |
2494 | ||
2495 | p = build_auxiliary_path(image, ".usrhash.p7s"); | |
2496 | if (!p) | |
2497 | return -ENOMEM; | |
89e62e0b | 2498 | |
ae9cf30b | 2499 | r = read_full_file(p, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2500 | if (r < 0 && r != -ENOENT) |
2501 | return r; | |
2502 | if (r >= 0) | |
2503 | designator = PARTITION_USR; | |
2504 | } | |
89e62e0b LP |
2505 | } |
2506 | ||
aee36b4e | 2507 | if (root_hash_sig && root_hash_sig_size == 0) /* refuse empty size signatures */ |
89e62e0b LP |
2508 | return -EINVAL; |
2509 | } | |
2510 | ||
2511 | if (!verity->data_path) { | |
2512 | _cleanup_free_ char *p = NULL; | |
2513 | ||
2514 | p = build_auxiliary_path(image, ".verity"); | |
2515 | if (!p) | |
2516 | return -ENOMEM; | |
2517 | ||
2518 | if (access(p, F_OK) < 0) { | |
2519 | if (errno != ENOENT) | |
2520 | return -errno; | |
2521 | } else | |
2522 | verity_data_path = TAKE_PTR(p); | |
2523 | } | |
2524 | ||
2525 | if (root_hash) { | |
2526 | verity->root_hash = TAKE_PTR(root_hash); | |
2527 | verity->root_hash_size = root_hash_size; | |
2528 | } | |
2529 | ||
2530 | if (root_hash_sig) { | |
2531 | verity->root_hash_sig = TAKE_PTR(root_hash_sig); | |
2532 | verity->root_hash_sig_size = root_hash_sig_size; | |
e7cbe5cb | 2533 | } |
89e62e0b LP |
2534 | |
2535 | if (verity_data_path) | |
2536 | verity->data_path = TAKE_PTR(verity_data_path); | |
78ebe980 | 2537 | |
aee36b4e LP |
2538 | if (verity->designator < 0) |
2539 | verity->designator = designator; | |
2540 | ||
78ebe980 LP |
2541 | return 1; |
2542 | } | |
2543 | ||
88b3300f LP |
2544 | int dissected_image_load_verity_sig_partition( |
2545 | DissectedImage *m, | |
2546 | int fd, | |
2547 | VeritySettings *verity) { | |
2548 | ||
2549 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
2550 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
2551 | size_t root_hash_size, root_hash_sig_size; | |
2552 | _cleanup_free_ char *buf = NULL; | |
2553 | PartitionDesignator d; | |
2554 | DissectedPartition *p; | |
2555 | JsonVariant *rh, *sig; | |
2556 | ssize_t n; | |
2557 | char *e; | |
2558 | int r; | |
2559 | ||
2560 | assert(m); | |
2561 | assert(fd >= 0); | |
2562 | assert(verity); | |
2563 | ||
2564 | if (verity->root_hash && verity->root_hash_sig) /* Already loaded? */ | |
2565 | return 0; | |
2566 | ||
2567 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_EMBEDDED"); | |
2568 | if (r < 0 && r != -ENXIO) | |
2569 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_EMBEDDED, ignoring: %m"); | |
2570 | if (r == 0) | |
2571 | return 0; | |
2572 | ||
dd894023 | 2573 | d = partition_verity_sig_of(verity->designator < 0 ? PARTITION_ROOT : verity->designator); |
88b3300f LP |
2574 | assert(d >= 0); |
2575 | ||
2576 | p = m->partitions + d; | |
2577 | if (!p->found) | |
2578 | return 0; | |
2579 | if (p->offset == UINT64_MAX || p->size == UINT64_MAX) | |
2580 | return -EINVAL; | |
2581 | ||
2582 | if (p->size > 4*1024*1024) /* Signature data cannot possible be larger than 4M, refuse that */ | |
2583 | return -EFBIG; | |
2584 | ||
2585 | buf = new(char, p->size+1); | |
2586 | if (!buf) | |
2587 | return -ENOMEM; | |
2588 | ||
2589 | n = pread(fd, buf, p->size, p->offset); | |
2590 | if (n < 0) | |
2591 | return -ENOMEM; | |
2592 | if ((uint64_t) n != p->size) | |
2593 | return -EIO; | |
2594 | ||
2595 | e = memchr(buf, 0, p->size); | |
2596 | if (e) { | |
2597 | /* If we found a NUL byte then the rest of the data must be NUL too */ | |
2598 | if (!memeqzero(e, p->size - (e - buf))) | |
2599 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature data contains embedded NUL byte."); | |
2600 | } else | |
2601 | buf[p->size] = 0; | |
2602 | ||
2603 | r = json_parse(buf, 0, &v, NULL, NULL); | |
2604 | if (r < 0) | |
2605 | return log_debug_errno(r, "Failed to parse signature JSON data: %m"); | |
2606 | ||
2607 | rh = json_variant_by_key(v, "rootHash"); | |
2608 | if (!rh) | |
2609 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'rootHash' field."); | |
2610 | if (!json_variant_is_string(rh)) | |
2611 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'rootHash' field of signature JSON object is not a string."); | |
2612 | ||
2613 | r = unhexmem(json_variant_string(rh), SIZE_MAX, &root_hash, &root_hash_size); | |
2614 | if (r < 0) | |
2615 | return log_debug_errno(r, "Failed to parse root hash field: %m"); | |
2616 | ||
2617 | /* Check if specified root hash matches if it is specified */ | |
2618 | if (verity->root_hash && | |
2619 | memcmp_nn(verity->root_hash, verity->root_hash_size, root_hash, root_hash_size) != 0) { | |
2620 | _cleanup_free_ char *a = NULL, *b = NULL; | |
2621 | ||
2622 | a = hexmem(root_hash, root_hash_size); | |
2623 | b = hexmem(verity->root_hash, verity->root_hash_size); | |
2624 | ||
2625 | return log_debug_errno(r, "Root hash in signature JSON data (%s) doesn't match configured hash (%s).", strna(a), strna(b)); | |
2626 | } | |
2627 | ||
2628 | sig = json_variant_by_key(v, "signature"); | |
2629 | if (!sig) | |
2630 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'signature' field."); | |
2631 | if (!json_variant_is_string(sig)) | |
2632 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'signature' field of signature JSON object is not a string."); | |
2633 | ||
2634 | r = unbase64mem(json_variant_string(sig), SIZE_MAX, &root_hash_sig, &root_hash_sig_size); | |
2635 | if (r < 0) | |
2636 | return log_debug_errno(r, "Failed to parse signature field: %m"); | |
2637 | ||
2638 | free_and_replace(verity->root_hash, root_hash); | |
2639 | verity->root_hash_size = root_hash_size; | |
2640 | ||
2641 | free_and_replace(verity->root_hash_sig, root_hash_sig); | |
2642 | verity->root_hash_sig_size = root_hash_sig_size; | |
2643 | ||
2644 | return 1; | |
2645 | } | |
2646 | ||
22847508 | 2647 | int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_flags) { |
3b925504 LP |
2648 | |
2649 | enum { | |
2650 | META_HOSTNAME, | |
2651 | META_MACHINE_ID, | |
2652 | META_MACHINE_INFO, | |
2653 | META_OS_RELEASE, | |
fab22946 | 2654 | META_INITRD_RELEASE, |
7718ac97 | 2655 | META_EXTENSION_RELEASE, |
a4e0d617 | 2656 | META_HAS_INIT_SYSTEM, |
3b925504 LP |
2657 | _META_MAX, |
2658 | }; | |
2659 | ||
9a4b883b | 2660 | static const char *const paths[_META_MAX] = { |
7718ac97 LB |
2661 | [META_HOSTNAME] = "/etc/hostname\0", |
2662 | [META_MACHINE_ID] = "/etc/machine-id\0", | |
2663 | [META_MACHINE_INFO] = "/etc/machine-info\0", | |
9a4b883b | 2664 | [META_OS_RELEASE] = ("/etc/os-release\0" |
22847508 | 2665 | "/usr/lib/os-release\0"), |
fab22946 LP |
2666 | [META_INITRD_RELEASE] = ("/etc/initrd-release\0" |
2667 | "/usr/lib/initrd-release\0"), | |
a4e0d617 LP |
2668 | [META_EXTENSION_RELEASE] = "extension-release\0", /* Used only for logging. */ |
2669 | [META_HAS_INIT_SYSTEM] = "has-init-system\0", /* ditto */ | |
3b925504 LP |
2670 | }; |
2671 | ||
fab22946 | 2672 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL, **initrd_release = NULL, **extension_release = NULL; |
af8219d5 | 2673 | _cleanup_close_pair_ int error_pipe[2] = { -1, -1 }; |
3b925504 LP |
2674 | _cleanup_(rmdir_and_freep) char *t = NULL; |
2675 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
2676 | sd_id128_t machine_id = SD_ID128_NULL; | |
2677 | _cleanup_free_ char *hostname = NULL; | |
67f63ee5 | 2678 | unsigned n_meta_initialized = 0; |
af8219d5 | 2679 | int fds[2 * _META_MAX], r, v; |
a4e0d617 | 2680 | int has_init_system = -1; |
af8219d5 | 2681 | ssize_t n; |
3b925504 LP |
2682 | |
2683 | BLOCK_SIGNALS(SIGCHLD); | |
2684 | ||
2685 | assert(m); | |
2686 | ||
7718ac97 | 2687 | for (; n_meta_initialized < _META_MAX; n_meta_initialized ++) { |
d9119c00 LP |
2688 | if (!paths[n_meta_initialized]) { |
2689 | fds[2*n_meta_initialized] = fds[2*n_meta_initialized+1] = -1; | |
7718ac97 | 2690 | continue; |
d9119c00 LP |
2691 | } |
2692 | ||
3b925504 LP |
2693 | if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) { |
2694 | r = -errno; | |
2695 | goto finish; | |
2696 | } | |
7718ac97 | 2697 | } |
3b925504 LP |
2698 | |
2699 | r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); | |
2700 | if (r < 0) | |
2701 | goto finish; | |
2702 | ||
af8219d5 LP |
2703 | if (pipe2(error_pipe, O_CLOEXEC) < 0) { |
2704 | r = -errno; | |
2705 | goto finish; | |
2706 | } | |
2707 | ||
e2047ba9 | 2708 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 2709 | if (r < 0) |
3b925504 | 2710 | goto finish; |
be39f6ee | 2711 | if (r == 0) { |
a4e0d617 | 2712 | /* Child in a new mount namespace */ |
af8219d5 LP |
2713 | error_pipe[0] = safe_close(error_pipe[0]); |
2714 | ||
7cf66030 LP |
2715 | r = dissected_image_mount( |
2716 | m, | |
2717 | t, | |
2718 | UID_INVALID, | |
21b61b1d | 2719 | UID_INVALID, |
22847508 ZJS |
2720 | extra_flags | |
2721 | DISSECT_IMAGE_READ_ONLY | | |
2722 | DISSECT_IMAGE_MOUNT_ROOT_ONLY | | |
7cf66030 | 2723 | DISSECT_IMAGE_USR_NO_ROOT); |
429d4e41 LP |
2724 | if (r < 0) { |
2725 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
03ae68f4 | 2726 | goto inner_fail; |
429d4e41 | 2727 | } |
3b925504 | 2728 | |
67f63ee5 | 2729 | for (unsigned k = 0; k < _META_MAX; k++) { |
37e44c3f | 2730 | _cleanup_close_ int fd = -ENOENT; |
3b925504 | 2731 | |
7718ac97 LB |
2732 | if (!paths[k]) |
2733 | continue; | |
2734 | ||
3b925504 LP |
2735 | fds[2*k] = safe_close(fds[2*k]); |
2736 | ||
a4e0d617 LP |
2737 | switch (k) { |
2738 | ||
2739 | case META_EXTENSION_RELEASE: | |
9a4b883b LB |
2740 | /* As per the os-release spec, if the image is an extension it will have a file |
2741 | * named after the image name in extension-release.d/ - we use the image name | |
2742 | * and try to resolve it with the extension-release helpers, as sometimes | |
2743 | * the image names are mangled on deployment and do not match anymore. | |
2744 | * Unlike other paths this is not fixed, and the image name | |
2745 | * can be mangled on deployment, so by calling into the helper | |
2746 | * we allow a fallback that matches on the first extension-release | |
2747 | * file found in the directory, if one named after the image cannot | |
2748 | * be found first. */ | |
06768b90 | 2749 | r = open_extension_release(t, m->image_name, /* relax_extension_release_check= */ false, NULL, &fd); |
9a4b883b LB |
2750 | if (r < 0) |
2751 | fd = r; /* Propagate the error. */ | |
a4e0d617 LP |
2752 | break; |
2753 | ||
2754 | case META_HAS_INIT_SYSTEM: { | |
2755 | bool found = false; | |
a4e0d617 LP |
2756 | |
2757 | FOREACH_STRING(init, | |
2758 | "/usr/lib/systemd/systemd", /* systemd on /usr merged system */ | |
2759 | "/lib/systemd/systemd", /* systemd on /usr non-merged systems */ | |
2760 | "/sbin/init") { /* traditional path the Linux kernel invokes */ | |
2761 | ||
2762 | r = chase_symlinks(init, t, CHASE_PREFIX_ROOT, NULL, NULL); | |
2763 | if (r < 0) { | |
2764 | if (r != -ENOENT) | |
2765 | log_debug_errno(r, "Failed to resolve %s, ignoring: %m", init); | |
2766 | } else { | |
2767 | found = true; | |
2768 | break; | |
2769 | } | |
2770 | } | |
2771 | ||
2772 | r = loop_write(fds[2*k+1], &found, sizeof(found), false); | |
2773 | if (r < 0) | |
2774 | goto inner_fail; | |
2775 | ||
2776 | continue; | |
2777 | } | |
2778 | ||
2779 | default: | |
9a4b883b LB |
2780 | NULSTR_FOREACH(p, paths[k]) { |
2781 | fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); | |
2782 | if (fd >= 0) | |
2783 | break; | |
2784 | } | |
a4e0d617 LP |
2785 | } |
2786 | ||
36952d19 LP |
2787 | if (fd < 0) { |
2788 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
37e44c3f | 2789 | fds[2*k+1] = safe_close(fds[2*k+1]); |
3b925504 | 2790 | continue; |
36952d19 | 2791 | } |
3b925504 | 2792 | |
f5fbe71d | 2793 | r = copy_bytes(fd, fds[2*k+1], UINT64_MAX, 0); |
a4e0d617 LP |
2794 | if (r < 0) |
2795 | goto inner_fail; | |
3b925504 LP |
2796 | |
2797 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
2798 | } | |
2799 | ||
2800 | _exit(EXIT_SUCCESS); | |
a4e0d617 LP |
2801 | |
2802 | inner_fail: | |
03ae68f4 | 2803 | /* Let parent know the error */ |
a4e0d617 LP |
2804 | (void) write(error_pipe[1], &r, sizeof(r)); |
2805 | _exit(EXIT_FAILURE); | |
3b925504 LP |
2806 | } |
2807 | ||
af8219d5 LP |
2808 | error_pipe[1] = safe_close(error_pipe[1]); |
2809 | ||
67f63ee5 | 2810 | for (unsigned k = 0; k < _META_MAX; k++) { |
3b925504 LP |
2811 | _cleanup_fclose_ FILE *f = NULL; |
2812 | ||
7718ac97 LB |
2813 | if (!paths[k]) |
2814 | continue; | |
2815 | ||
3b925504 LP |
2816 | fds[2*k+1] = safe_close(fds[2*k+1]); |
2817 | ||
4fa744a3 | 2818 | f = take_fdopen(&fds[2*k], "r"); |
3b925504 LP |
2819 | if (!f) { |
2820 | r = -errno; | |
2821 | goto finish; | |
2822 | } | |
2823 | ||
3b925504 LP |
2824 | switch (k) { |
2825 | ||
2826 | case META_HOSTNAME: | |
2827 | r = read_etc_hostname_stream(f, &hostname); | |
2828 | if (r < 0) | |
f6048e5e | 2829 | log_debug_errno(r, "Failed to read /etc/hostname of image: %m"); |
3b925504 LP |
2830 | |
2831 | break; | |
2832 | ||
2833 | case META_MACHINE_ID: { | |
2834 | _cleanup_free_ char *line = NULL; | |
2835 | ||
2836 | r = read_line(f, LONG_LINE_MAX, &line); | |
2837 | if (r < 0) | |
f6048e5e | 2838 | log_debug_errno(r, "Failed to read /etc/machine-id of image: %m"); |
3b925504 LP |
2839 | else if (r == 33) { |
2840 | r = sd_id128_from_string(line, &machine_id); | |
2841 | if (r < 0) | |
2842 | log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line); | |
2843 | } else if (r == 0) | |
f6048e5e | 2844 | log_debug("/etc/machine-id file of image is empty."); |
ab763cb2 | 2845 | else if (streq(line, "uninitialized")) |
f6048e5e | 2846 | log_debug("/etc/machine-id file of image is uninitialized (likely aborted first boot)."); |
3b925504 | 2847 | else |
f6048e5e | 2848 | log_debug("/etc/machine-id file of image has unexpected length %i.", r); |
3b925504 LP |
2849 | |
2850 | break; | |
2851 | } | |
2852 | ||
2853 | case META_MACHINE_INFO: | |
aa8fbc74 | 2854 | r = load_env_file_pairs(f, "machine-info", &machine_info); |
3b925504 | 2855 | if (r < 0) |
f6048e5e | 2856 | log_debug_errno(r, "Failed to read /etc/machine-info of image: %m"); |
3b925504 LP |
2857 | |
2858 | break; | |
2859 | ||
2860 | case META_OS_RELEASE: | |
aa8fbc74 | 2861 | r = load_env_file_pairs(f, "os-release", &os_release); |
3b925504 | 2862 | if (r < 0) |
f6048e5e | 2863 | log_debug_errno(r, "Failed to read OS release file of image: %m"); |
3b925504 LP |
2864 | |
2865 | break; | |
7718ac97 | 2866 | |
fab22946 LP |
2867 | case META_INITRD_RELEASE: |
2868 | r = load_env_file_pairs(f, "initrd-release", &initrd_release); | |
2869 | if (r < 0) | |
2870 | log_debug_errno(r, "Failed to read initrd release file of image: %m"); | |
2871 | ||
2872 | break; | |
2873 | ||
7718ac97 LB |
2874 | case META_EXTENSION_RELEASE: |
2875 | r = load_env_file_pairs(f, "extension-release", &extension_release); | |
2876 | if (r < 0) | |
f6048e5e | 2877 | log_debug_errno(r, "Failed to read extension release file of image: %m"); |
7718ac97 LB |
2878 | |
2879 | break; | |
a4e0d617 LP |
2880 | |
2881 | case META_HAS_INIT_SYSTEM: { | |
2882 | bool b = false; | |
2883 | size_t nr; | |
2884 | ||
2885 | errno = 0; | |
2886 | nr = fread(&b, 1, sizeof(b), f); | |
2887 | if (nr != sizeof(b)) | |
2888 | log_debug_errno(errno_or_else(EIO), "Failed to read has-init-system boolean: %m"); | |
2889 | else | |
2890 | has_init_system = b; | |
2891 | ||
2892 | break; | |
2893 | }} | |
3b925504 LP |
2894 | } |
2895 | ||
2e87a1fd | 2896 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 2897 | child = 0; |
2e87a1fd | 2898 | if (r < 0) |
af8219d5 LP |
2899 | return r; |
2900 | ||
2901 | n = read(error_pipe[0], &v, sizeof(v)); | |
2902 | if (n < 0) | |
2903 | return -errno; | |
2904 | if (n == sizeof(v)) | |
2905 | return v; /* propagate error sent to us from child */ | |
2906 | if (n != 0) | |
2907 | return -EIO; | |
2908 | ||
2e87a1fd LP |
2909 | if (r != EXIT_SUCCESS) |
2910 | return -EPROTO; | |
3b925504 LP |
2911 | |
2912 | free_and_replace(m->hostname, hostname); | |
2913 | m->machine_id = machine_id; | |
2914 | strv_free_and_replace(m->machine_info, machine_info); | |
2915 | strv_free_and_replace(m->os_release, os_release); | |
fab22946 | 2916 | strv_free_and_replace(m->initrd_release, initrd_release); |
7718ac97 | 2917 | strv_free_and_replace(m->extension_release, extension_release); |
a4e0d617 | 2918 | m->has_init_system = has_init_system; |
3b925504 LP |
2919 | |
2920 | finish: | |
67f63ee5 | 2921 | for (unsigned k = 0; k < n_meta_initialized; k++) |
3b925504 LP |
2922 | safe_close_pair(fds + 2*k); |
2923 | ||
2924 | return r; | |
2925 | } | |
2926 | ||
1e63dc4f YW |
2927 | int dissect_loop_device( |
2928 | LoopDevice *loop, | |
2929 | const VeritySettings *verity, | |
2930 | const MountOptions *mount_options, | |
2931 | DissectImageFlags flags, | |
2932 | DissectedImage **ret) { | |
2933 | ||
08f14be4 | 2934 | #if HAVE_BLKID |
1e63dc4f YW |
2935 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
2936 | int r; | |
2937 | ||
2938 | assert(loop); | |
2939 | assert(ret); | |
2940 | ||
08f14be4 | 2941 | r = dissected_image_new(loop->backing_file ?: loop->node, &m); |
1e63dc4f YW |
2942 | if (r < 0) |
2943 | return r; | |
2944 | ||
2945 | m->loop = loop_device_ref(loop); | |
2946 | ||
73d88b80 | 2947 | r = dissect_image(m, loop->fd, loop->node, verity, mount_options, flags); |
08f14be4 YW |
2948 | if (r < 0) |
2949 | return r; | |
2950 | ||
2951 | r = dissected_image_probe_filesystem(m); | |
2952 | if (r < 0) | |
2953 | return r; | |
2954 | ||
1e63dc4f YW |
2955 | *ret = TAKE_PTR(m); |
2956 | return 0; | |
08f14be4 YW |
2957 | #else |
2958 | return -EOPNOTSUPP; | |
2959 | #endif | |
1e63dc4f YW |
2960 | } |
2961 | ||
bad31660 | 2962 | int dissect_loop_device_and_warn( |
1e63dc4f | 2963 | LoopDevice *loop, |
89e62e0b | 2964 | const VeritySettings *verity, |
18d73705 | 2965 | const MountOptions *mount_options, |
4526113f LP |
2966 | DissectImageFlags flags, |
2967 | DissectedImage **ret) { | |
2968 | ||
64dd3a24 | 2969 | const char *name; |
4526113f LP |
2970 | int r; |
2971 | ||
bad31660 YW |
2972 | assert(loop); |
2973 | assert(loop->fd >= 0); | |
4526113f | 2974 | |
64dd3a24 | 2975 | name = ASSERT_PTR(loop->backing_file ?: loop->node); |
4526113f | 2976 | |
369de26f | 2977 | r = dissect_loop_device(loop, verity, mount_options, flags, ret); |
4526113f LP |
2978 | switch (r) { |
2979 | ||
2980 | case -EOPNOTSUPP: | |
2981 | return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support."); | |
2982 | ||
2983 | case -ENOPKG: | |
48084df6 ZJS |
2984 | return log_error_errno(r, "%s: Couldn't identify a suitable partition table or file system.", name); |
2985 | ||
2986 | case -ENOMEDIUM: | |
2987 | return log_error_errno(r, "%s: The image does not pass validation.", name); | |
4526113f LP |
2988 | |
2989 | case -EADDRNOTAVAIL: | |
48084df6 | 2990 | return log_error_errno(r, "%s: No root partition for specified root hash found.", name); |
4526113f LP |
2991 | |
2992 | case -ENOTUNIQ: | |
48084df6 | 2993 | return log_error_errno(r, "%s: Multiple suitable root partitions found in image.", name); |
4526113f LP |
2994 | |
2995 | case -ENXIO: | |
48084df6 | 2996 | return log_error_errno(r, "%s: No suitable root partition found in image.", name); |
4526113f LP |
2997 | |
2998 | case -EPROTONOSUPPORT: | |
2999 | return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name); | |
3000 | ||
48084df6 ZJS |
3001 | case -ENOTBLK: |
3002 | return log_error_errno(r, "%s: Image is not a block device.", name); | |
3003 | ||
a94aa2b9 LP |
3004 | case -EBADR: |
3005 | return log_error_errno(r, | |
3006 | "Combining partitioned images (such as '%s') with external Verity data (such as '%s') not supported. " | |
3007 | "(Consider setting $SYSTEMD_DISSECT_VERITY_SIDECAR=0 to disable automatic discovery of external Verity data.)", | |
3008 | name, strna(verity ? verity->data_path : NULL)); | |
3009 | ||
4526113f LP |
3010 | default: |
3011 | if (r < 0) | |
3012 | return log_error_errno(r, "Failed to dissect image '%s': %m", name); | |
3013 | ||
3014 | return r; | |
3015 | } | |
3016 | } | |
3017 | ||
49536766 LP |
3018 | bool dissected_image_verity_candidate(const DissectedImage *image, PartitionDesignator partition_designator) { |
3019 | assert(image); | |
3020 | ||
3021 | /* Checks if this partition could theoretically do Verity. For non-partitioned images this only works | |
3022 | * if there's an external verity file supplied, for which we can consult .has_verity. For partitioned | |
3023 | * images we only check the partition type. | |
3024 | * | |
3025 | * This call is used to decide whether to suppress or show a verity column in tabular output of the | |
3026 | * image. */ | |
3027 | ||
e7cbe5cb | 3028 | if (image->single_file_system) |
c3c88d67 | 3029 | return partition_designator == PARTITION_ROOT && image->has_verity; |
e7cbe5cb | 3030 | |
dd894023 | 3031 | return partition_verity_of(partition_designator) >= 0; |
e7cbe5cb LB |
3032 | } |
3033 | ||
49536766 LP |
3034 | bool dissected_image_verity_ready(const DissectedImage *image, PartitionDesignator partition_designator) { |
3035 | PartitionDesignator k; | |
3036 | ||
3037 | assert(image); | |
3038 | ||
3039 | /* Checks if this partition has verity data available that we can activate. For non-partitioned this | |
3040 | * works for the root partition, for others only if the associated verity partition was found. */ | |
3041 | ||
3042 | if (!image->verity_ready) | |
3043 | return false; | |
e7cbe5cb LB |
3044 | |
3045 | if (image->single_file_system) | |
49536766 | 3046 | return partition_designator == PARTITION_ROOT; |
e7cbe5cb | 3047 | |
dd894023 | 3048 | k = partition_verity_of(partition_designator); |
e7cbe5cb LB |
3049 | return k >= 0 && image->partitions[k].found; |
3050 | } | |
3051 | ||
8ee9615e LP |
3052 | bool dissected_image_verity_sig_ready(const DissectedImage *image, PartitionDesignator partition_designator) { |
3053 | PartitionDesignator k; | |
3054 | ||
3055 | assert(image); | |
3056 | ||
3057 | /* Checks if this partition has verity signature data available that we can use. */ | |
3058 | ||
3059 | if (!image->verity_sig_ready) | |
3060 | return false; | |
3061 | ||
3062 | if (image->single_file_system) | |
3063 | return partition_designator == PARTITION_ROOT; | |
3064 | ||
dd894023 | 3065 | k = partition_verity_sig_of(partition_designator); |
8ee9615e LP |
3066 | return k >= 0 && image->partitions[k].found; |
3067 | } | |
3068 | ||
18d73705 LB |
3069 | MountOptions* mount_options_free_all(MountOptions *options) { |
3070 | MountOptions *m; | |
3071 | ||
3072 | while ((m = options)) { | |
3073 | LIST_REMOVE(mount_options, options, m); | |
3074 | free(m->options); | |
3075 | free(m); | |
3076 | } | |
3077 | ||
3078 | return NULL; | |
3079 | } | |
3080 | ||
569a0e42 | 3081 | const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator) { |
f5215bc8 | 3082 | LIST_FOREACH(mount_options, m, options) |
9ece6444 | 3083 | if (designator == m->partition_designator && !isempty(m->options)) |
18d73705 | 3084 | return m->options; |
6aa05ebd | 3085 | |
18d73705 LB |
3086 | return NULL; |
3087 | } | |
3088 | ||
6aa05ebd LP |
3089 | int mount_image_privately_interactively( |
3090 | const char *image, | |
3091 | DissectImageFlags flags, | |
3092 | char **ret_directory, | |
e330f97a | 3093 | LoopDevice **ret_loop_device) { |
6aa05ebd | 3094 | |
27ec815e | 3095 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
6aa05ebd | 3096 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
6aa05ebd LP |
3097 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; |
3098 | _cleanup_(rmdir_and_freep) char *created_dir = NULL; | |
3099 | _cleanup_free_ char *temp = NULL; | |
3100 | int r; | |
3101 | ||
3102 | /* Mounts an OS image at a temporary place, inside a newly created mount namespace of our own. This | |
3103 | * is used by tools such as systemd-tmpfiles or systemd-firstboot to operate on some disk image | |
3104 | * easily. */ | |
3105 | ||
3106 | assert(image); | |
3107 | assert(ret_directory); | |
3108 | assert(ret_loop_device); | |
6aa05ebd | 3109 | |
73d88b80 LP |
3110 | /* We intend to mount this right-away, hence add the partitions if needed and pin them*/ |
3111 | flags |= DISSECT_IMAGE_ADD_PARTITION_DEVICES | | |
3112 | DISSECT_IMAGE_PIN_PARTITION_DEVICES; | |
3113 | ||
27ec815e LP |
3114 | r = verity_settings_load(&verity, image, NULL, NULL); |
3115 | if (r < 0) | |
3116 | return log_error_errno(r, "Failed to load root hash data: %m"); | |
3117 | ||
6aa05ebd LP |
3118 | r = tempfn_random_child(NULL, program_invocation_short_name, &temp); |
3119 | if (r < 0) | |
3120 | return log_error_errno(r, "Failed to generate temporary mount directory: %m"); | |
3121 | ||
3122 | r = loop_device_make_by_path( | |
3123 | image, | |
ef9c184d | 3124 | FLAGS_SET(flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : O_RDWR, |
6aa05ebd | 3125 | FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, |
7f52206a | 3126 | LOCK_SH, |
6aa05ebd LP |
3127 | &d); |
3128 | if (r < 0) | |
7b87fe4c | 3129 | return log_error_errno(r, "Failed to set up loopback device for %s: %m", image); |
6aa05ebd | 3130 | |
64dd3a24 | 3131 | r = dissect_loop_device_and_warn(d, &verity, NULL, flags, &dissected_image); |
6aa05ebd LP |
3132 | if (r < 0) |
3133 | return r; | |
3134 | ||
88b3300f LP |
3135 | r = dissected_image_load_verity_sig_partition(dissected_image, d->fd, &verity); |
3136 | if (r < 0) | |
3137 | return r; | |
3138 | ||
e330f97a | 3139 | r = dissected_image_decrypt_interactively(dissected_image, NULL, &verity, flags); |
6aa05ebd LP |
3140 | if (r < 0) |
3141 | return r; | |
3142 | ||
3143 | r = detach_mount_namespace(); | |
3144 | if (r < 0) | |
3145 | return log_error_errno(r, "Failed to detach mount namespace: %m"); | |
3146 | ||
3147 | r = mkdir_p(temp, 0700); | |
3148 | if (r < 0) | |
3149 | return log_error_errno(r, "Failed to create mount point: %m"); | |
3150 | ||
3151 | created_dir = TAKE_PTR(temp); | |
3152 | ||
21b61b1d | 3153 | r = dissected_image_mount_and_warn(dissected_image, created_dir, UID_INVALID, UID_INVALID, flags); |
6aa05ebd | 3154 | if (r < 0) |
af187ab2 | 3155 | return r; |
6aa05ebd | 3156 | |
41bc4849 LP |
3157 | r = loop_device_flock(d, LOCK_UN); |
3158 | if (r < 0) | |
3159 | return r; | |
3160 | ||
3044d343 YW |
3161 | r = dissected_image_relinquish(dissected_image); |
3162 | if (r < 0) | |
3163 | return log_error_errno(r, "Failed to relinquish DM and loopback block devices: %m"); | |
6aa05ebd LP |
3164 | |
3165 | *ret_directory = TAKE_PTR(created_dir); | |
3166 | *ret_loop_device = TAKE_PTR(d); | |
6aa05ebd LP |
3167 | |
3168 | return 0; | |
3169 | } | |
3170 | ||
06768b90 LB |
3171 | static bool mount_options_relax_extension_release_checks(const MountOptions *options) { |
3172 | if (!options) | |
3173 | return false; | |
3174 | ||
3175 | return string_contains_word(mount_options_from_designator(options, PARTITION_ROOT), ",", "x-systemd.relax-extension-release-check") || | |
3176 | string_contains_word(mount_options_from_designator(options, PARTITION_USR), ",", "x-systemd.relax-extension-release-check") || | |
3177 | string_contains_word(options->options, ",", "x-systemd.relax-extension-release-check"); | |
3178 | } | |
3179 | ||
93f59701 | 3180 | int verity_dissect_and_mount( |
cedf5b1a | 3181 | int src_fd, |
93f59701 LB |
3182 | const char *src, |
3183 | const char *dest, | |
3184 | const MountOptions *options, | |
3185 | const char *required_host_os_release_id, | |
3186 | const char *required_host_os_release_version_id, | |
60c5f700 LP |
3187 | const char *required_host_os_release_sysext_level, |
3188 | const char *required_sysext_scope) { | |
93f59701 | 3189 | |
4beda316 | 3190 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
4beda316 LB |
3191 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; |
3192 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; | |
3193 | DissectImageFlags dissect_image_flags; | |
06768b90 | 3194 | bool relax_extension_release_check; |
4beda316 LB |
3195 | int r; |
3196 | ||
3197 | assert(src); | |
3198 | assert(dest); | |
3199 | ||
06768b90 LB |
3200 | relax_extension_release_check = mount_options_relax_extension_release_checks(options); |
3201 | ||
cedf5b1a | 3202 | /* We might get an FD for the image, but we use the original path to look for the dm-verity files */ |
4beda316 LB |
3203 | r = verity_settings_load(&verity, src, NULL, NULL); |
3204 | if (r < 0) | |
3205 | return log_debug_errno(r, "Failed to load root hash: %m"); | |
3206 | ||
06768b90 | 3207 | dissect_image_flags = (verity.data_path ? DISSECT_IMAGE_NO_PARTITION_TABLE : 0) | |
73d88b80 LP |
3208 | (relax_extension_release_check ? DISSECT_IMAGE_RELAX_SYSEXT_CHECK : 0) | |
3209 | DISSECT_IMAGE_ADD_PARTITION_DEVICES | | |
3210 | DISSECT_IMAGE_PIN_PARTITION_DEVICES; | |
4beda316 | 3211 | |
cedf5b1a LB |
3212 | /* Note that we don't use loop_device_make here, as the FD is most likely O_PATH which would not be |
3213 | * accepted by LOOP_CONFIGURE, so just let loop_device_make_by_path reopen it as a regular FD. */ | |
4beda316 | 3214 | r = loop_device_make_by_path( |
cedf5b1a | 3215 | src_fd >= 0 ? FORMAT_PROC_FD_PATH(src_fd) : src, |
4beda316 LB |
3216 | -1, |
3217 | verity.data_path ? 0 : LO_FLAGS_PARTSCAN, | |
7f52206a | 3218 | LOCK_SH, |
4beda316 LB |
3219 | &loop_device); |
3220 | if (r < 0) | |
3221 | return log_debug_errno(r, "Failed to create loop device for image: %m"); | |
3222 | ||
bad31660 YW |
3223 | r = dissect_loop_device( |
3224 | loop_device, | |
4beda316 LB |
3225 | &verity, |
3226 | options, | |
3227 | dissect_image_flags, | |
3228 | &dissected_image); | |
3229 | /* No partition table? Might be a single-filesystem image, try again */ | |
3230 | if (!verity.data_path && r == -ENOPKG) | |
bad31660 YW |
3231 | r = dissect_loop_device( |
3232 | loop_device, | |
4beda316 LB |
3233 | &verity, |
3234 | options, | |
75dc190d | 3235 | dissect_image_flags | DISSECT_IMAGE_NO_PARTITION_TABLE, |
4beda316 LB |
3236 | &dissected_image); |
3237 | if (r < 0) | |
3238 | return log_debug_errno(r, "Failed to dissect image: %m"); | |
3239 | ||
88b3300f LP |
3240 | r = dissected_image_load_verity_sig_partition(dissected_image, loop_device->fd, &verity); |
3241 | if (r < 0) | |
3242 | return r; | |
3243 | ||
4beda316 LB |
3244 | r = dissected_image_decrypt( |
3245 | dissected_image, | |
3246 | NULL, | |
3247 | &verity, | |
e330f97a | 3248 | dissect_image_flags); |
4beda316 LB |
3249 | if (r < 0) |
3250 | return log_debug_errno(r, "Failed to decrypt dissected image: %m"); | |
3251 | ||
3252 | r = mkdir_p_label(dest, 0755); | |
3253 | if (r < 0) | |
3254 | return log_debug_errno(r, "Failed to create destination directory %s: %m", dest); | |
3255 | r = umount_recursive(dest, 0); | |
3256 | if (r < 0) | |
3257 | return log_debug_errno(r, "Failed to umount under destination directory %s: %m", dest); | |
3258 | ||
21b61b1d | 3259 | r = dissected_image_mount(dissected_image, dest, UID_INVALID, UID_INVALID, dissect_image_flags); |
4beda316 LB |
3260 | if (r < 0) |
3261 | return log_debug_errno(r, "Failed to mount image: %m"); | |
3262 | ||
41bc4849 LP |
3263 | r = loop_device_flock(loop_device, LOCK_UN); |
3264 | if (r < 0) | |
3265 | return log_debug_errno(r, "Failed to unlock loopback device: %m"); | |
3266 | ||
93f59701 LB |
3267 | /* If we got os-release values from the caller, then we need to match them with the image's |
3268 | * extension-release.d/ content. Return -EINVAL if there's any mismatch. | |
3269 | * First, check the distro ID. If that matches, then check the new SYSEXT_LEVEL value if | |
37361f46 LB |
3270 | * available, or else fallback to VERSION_ID. If neither is present (eg: rolling release), |
3271 | * then a simple match on the ID will be performed. */ | |
8b2dcbbd | 3272 | if (required_host_os_release_id) { |
93f59701 LB |
3273 | _cleanup_strv_free_ char **extension_release = NULL; |
3274 | ||
d30d86b7 YW |
3275 | assert(!isempty(required_host_os_release_id)); |
3276 | ||
06768b90 | 3277 | r = load_extension_release_pairs(dest, dissected_image->image_name, relax_extension_release_check, &extension_release); |
93f59701 LB |
3278 | if (r < 0) |
3279 | return log_debug_errno(r, "Failed to parse image %s extension-release metadata: %m", dissected_image->image_name); | |
3280 | ||
3281 | r = extension_release_validate( | |
60c5f700 LP |
3282 | dissected_image->image_name, |
3283 | required_host_os_release_id, | |
3284 | required_host_os_release_version_id, | |
3285 | required_host_os_release_sysext_level, | |
3286 | required_sysext_scope, | |
3287 | extension_release); | |
93f59701 LB |
3288 | if (r == 0) |
3289 | return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Image %s extension-release metadata does not match the root's", dissected_image->image_name); | |
3290 | if (r < 0) | |
3291 | return log_debug_errno(r, "Failed to compare image %s extension-release metadata with the root's os-release: %m", dissected_image->image_name); | |
3292 | } | |
3293 | ||
3044d343 YW |
3294 | r = dissected_image_relinquish(dissected_image); |
3295 | if (r < 0) | |
3296 | return log_debug_errno(r, "Failed to relinquish dissected image: %m"); | |
4beda316 LB |
3297 | |
3298 | return 0; | |
3299 | } |