]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dissect-image.c
Merge pull request #15535 from fbuihuu/update-runtime-watchdog-on-reload
[thirdparty/systemd.git] / src / shared / dissect-image.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #if HAVE_VALGRIND_MEMCHECK_H
4 #include <valgrind/memcheck.h>
5 #endif
6
7 #include <linux/dm-ioctl.h>
8 #include <linux/loop.h>
9 #include <sys/mount.h>
10 #include <sys/prctl.h>
11 #include <sys/wait.h>
12
13 #include "sd-device.h"
14 #include "sd-id128.h"
15
16 #include "architecture.h"
17 #include "ask-password-api.h"
18 #include "blkid-util.h"
19 #include "blockdev-util.h"
20 #include "copy.h"
21 #include "crypt-util.h"
22 #include "def.h"
23 #include "device-nodes.h"
24 #include "device-util.h"
25 #include "dissect-image.h"
26 #include "dm-util.h"
27 #include "env-file.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "fs-util.h"
31 #include "fsck-util.h"
32 #include "gpt.h"
33 #include "hexdecoct.h"
34 #include "hostname-util.h"
35 #include "id128-util.h"
36 #include "mount-util.h"
37 #include "mountpoint-util.h"
38 #include "nulstr-util.h"
39 #include "os-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "raw-clone.h"
43 #include "signal-util.h"
44 #include "stat-util.h"
45 #include "stdio-util.h"
46 #include "string-table.h"
47 #include "string-util.h"
48 #include "strv.h"
49 #include "tmpfile-util.h"
50 #include "udev-util.h"
51 #include "user-util.h"
52 #include "xattr-util.h"
53
54 int probe_filesystem(const char *node, char **ret_fstype) {
55 /* Try to find device content type and return it in *ret_fstype. If nothing is found,
56 * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an
57 * different error otherwise. */
58
59 #if HAVE_BLKID
60 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
61 const char *fstype;
62 int r;
63
64 errno = 0;
65 b = blkid_new_probe_from_filename(node);
66 if (!b)
67 return errno_or_else(ENOMEM);
68
69 blkid_probe_enable_superblocks(b, 1);
70 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
71
72 errno = 0;
73 r = blkid_do_safeprobe(b);
74 if (r == 1) {
75 log_debug("No type detected on partition %s", node);
76 goto not_found;
77 }
78 if (r == -2)
79 return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN),
80 "Results ambiguous for partition %s", node);
81 if (r != 0)
82 return errno_or_else(EIO);
83
84 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
85
86 if (fstype) {
87 char *t;
88
89 t = strdup(fstype);
90 if (!t)
91 return -ENOMEM;
92
93 *ret_fstype = t;
94 return 1;
95 }
96
97 not_found:
98 *ret_fstype = NULL;
99 return 0;
100 #else
101 return -EOPNOTSUPP;
102 #endif
103 }
104
105 #if HAVE_BLKID
106 /* Detect RPMB and Boot partitions, which are not listed by blkid.
107 * See https://github.com/systemd/systemd/issues/5806. */
108 static bool device_is_mmc_special_partition(sd_device *d) {
109 const char *sysname;
110
111 assert(d);
112
113 if (sd_device_get_sysname(d, &sysname) < 0)
114 return false;
115
116 return startswith(sysname, "mmcblk") &&
117 (endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1"));
118 }
119
120 static bool device_is_block(sd_device *d) {
121 const char *ss;
122
123 assert(d);
124
125 if (sd_device_get_subsystem(d, &ss) < 0)
126 return false;
127
128 return streq(ss, "block");
129 }
130
131 static int enumerator_for_parent(sd_device *d, sd_device_enumerator **ret) {
132 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
133 int r;
134
135 assert(d);
136 assert(ret);
137
138 r = sd_device_enumerator_new(&e);
139 if (r < 0)
140 return r;
141
142 r = sd_device_enumerator_allow_uninitialized(e);
143 if (r < 0)
144 return r;
145
146 r = sd_device_enumerator_add_match_parent(e, d);
147 if (r < 0)
148 return r;
149
150 *ret = TAKE_PTR(e);
151 return 0;
152 }
153
154 /* how many times to wait for the device nodes to appear */
155 #define N_DEVICE_NODE_LIST_ATTEMPTS 10
156
157 static int wait_for_partitions_to_appear(
158 int fd,
159 sd_device *d,
160 unsigned num_partitions,
161 DissectImageFlags flags,
162 sd_device_enumerator **ret_enumerator) {
163
164 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
165 sd_device *q;
166 unsigned n;
167 int r;
168
169 assert(fd >= 0);
170 assert(d);
171 assert(ret_enumerator);
172
173 r = enumerator_for_parent(d, &e);
174 if (r < 0)
175 return r;
176
177 /* Count the partitions enumerated by the kernel */
178 n = 0;
179 FOREACH_DEVICE(e, q) {
180 if (sd_device_get_devnum(q, NULL) < 0)
181 continue;
182 if (!device_is_block(q))
183 continue;
184 if (device_is_mmc_special_partition(q))
185 continue;
186
187 if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) {
188 r = device_wait_for_initialization(q, "block", USEC_INFINITY, NULL);
189 if (r < 0)
190 return r;
191 }
192
193 n++;
194 }
195
196 if (n == num_partitions + 1) {
197 *ret_enumerator = TAKE_PTR(e);
198 return 0; /* success! */
199 }
200 if (n > num_partitions + 1)
201 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
202 "blkid and kernel partition lists do not match.");
203
204 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running or it
205 * got EBUSY because udev already opened the device. Let's reprobe the device, which is a synchronous
206 * call that waits until probing is complete. */
207
208 for (unsigned j = 0; ; j++) {
209 if (j++ > 20)
210 return -EBUSY;
211
212 if (ioctl(fd, BLKRRPART, 0) >= 0)
213 break;
214 r = -errno;
215 if (r == -EINVAL) {
216 struct loop_info64 info;
217
218 /* If we are running on a loop device that has partition scanning off, return
219 * an explicit recognizable error about this, so that callers can generate a
220 * proper message explaining the situation. */
221
222 if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0) {
223 #if HAVE_VALGRIND_MEMCHECK_H
224 /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
225 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
226 #endif
227
228 if ((info.lo_flags & LO_FLAGS_PARTSCAN) == 0)
229 return log_debug_errno(EPROTONOSUPPORT,
230 "Device is a loop device and partition scanning is off!");
231 }
232 }
233 if (r != -EBUSY)
234 return r;
235
236 /* If something else has the device open, such as an udev rule, the ioctl will return
237 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a bit,
238 * and try again.
239 *
240 * This is really something they should fix in the kernel! */
241 (void) usleep(50 * USEC_PER_MSEC);
242
243 }
244
245 return -EAGAIN; /* no success yet, try again */
246 }
247
248 static int loop_wait_for_partitions_to_appear(
249 int fd,
250 sd_device *d,
251 unsigned num_partitions,
252 DissectImageFlags flags,
253 sd_device_enumerator **ret_enumerator) {
254 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
255 int r;
256
257 assert(fd >= 0);
258 assert(d);
259 assert(ret_enumerator);
260
261 log_debug("Waiting for device (parent + %d partitions) to appear...", num_partitions);
262
263 if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) {
264 r = device_wait_for_initialization(d, "block", USEC_INFINITY, &device);
265 if (r < 0)
266 return r;
267 } else
268 device = sd_device_ref(d);
269
270 for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) {
271 r = wait_for_partitions_to_appear(fd, device, num_partitions, flags, ret_enumerator);
272 if (r != -EAGAIN)
273 return r;
274 }
275
276 return log_debug_errno(SYNTHETIC_ERRNO(ENXIO),
277 "Kernel partitions dit not appear within %d attempts",
278 N_DEVICE_NODE_LIST_ATTEMPTS);
279 }
280
281 static void check_partition_flags(
282 const char *node,
283 unsigned long long pflags,
284 unsigned long long supported) {
285
286 assert(node);
287
288 /* Mask away all flags supported by this partition's type and the three flags the UEFI spec defines generically */
289 pflags &= ~(supported | GPT_FLAG_REQUIRED_PARTITION | GPT_FLAG_NO_BLOCK_IO_PROTOCOL | GPT_FLAG_LEGACY_BIOS_BOOTABLE);
290
291 if (pflags == 0)
292 return;
293
294 /* If there are other bits set, then log about it, to make things discoverable */
295 for (unsigned i = 0; i < sizeof(pflags) * 8; i++) {
296 unsigned long long bit = 1ULL << i;
297 if (!FLAGS_SET(pflags, bit))
298 continue;
299
300 log_debug("Unexpected partition flag %llu set on %s!", bit, node);
301 }
302 }
303
304 #endif
305
306 int dissect_image(
307 int fd,
308 const void *root_hash,
309 size_t root_hash_size,
310 DissectImageFlags flags,
311 DissectedImage **ret) {
312
313 #if HAVE_BLKID
314 sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
315 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
316 bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
317 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
318 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
319 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
320 _cleanup_free_ char *generic_node = NULL;
321 sd_id128_t generic_uuid = SD_ID128_NULL;
322 const char *pttype = NULL;
323 blkid_partlist pl;
324 int r, generic_nr;
325 struct stat st;
326 sd_device *q;
327 unsigned i;
328
329 assert(fd >= 0);
330 assert(ret);
331 assert(root_hash || root_hash_size == 0);
332
333 /* Probes a disk image, and returns information about what it found in *ret.
334 *
335 * Returns -ENOPKG if no suitable partition table or file system could be found.
336 * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */
337
338 if (root_hash) {
339 /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
340 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
341 * 128bit. */
342
343 if (root_hash_size < sizeof(sd_id128_t))
344 return -EINVAL;
345
346 memcpy(&root_uuid, root_hash, sizeof(sd_id128_t));
347 memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t));
348
349 if (sd_id128_is_null(root_uuid))
350 return -EINVAL;
351 if (sd_id128_is_null(verity_uuid))
352 return -EINVAL;
353 }
354
355 if (fstat(fd, &st) < 0)
356 return -errno;
357
358 if (!S_ISBLK(st.st_mode))
359 return -ENOTBLK;
360
361 b = blkid_new_probe();
362 if (!b)
363 return -ENOMEM;
364
365 errno = 0;
366 r = blkid_probe_set_device(b, fd, 0, 0);
367 if (r != 0)
368 return errno_or_else(ENOMEM);
369
370 if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
371 /* Look for file system superblocks, unless we only shall look for GPT partition tables */
372 blkid_probe_enable_superblocks(b, 1);
373 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE);
374 }
375
376 blkid_probe_enable_partitions(b, 1);
377 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
378
379 errno = 0;
380 r = blkid_do_safeprobe(b);
381 if (IN_SET(r, -2, 1))
382 return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table.");
383 if (r != 0)
384 return errno_or_else(EIO);
385
386 m = new0(DissectedImage, 1);
387 if (!m)
388 return -ENOMEM;
389
390 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
391 if (r < 0)
392 return r;
393
394 if (!(flags & DISSECT_IMAGE_GPT_ONLY) &&
395 (flags & DISSECT_IMAGE_REQUIRE_ROOT)) {
396 const char *usage = NULL;
397
398 (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
399 if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
400 _cleanup_free_ char *t = NULL, *n = NULL;
401 const char *fstype = NULL;
402
403 /* OK, we have found a file system, that's our root partition then. */
404 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
405
406 if (fstype) {
407 t = strdup(fstype);
408 if (!t)
409 return -ENOMEM;
410 }
411
412 r = device_path_make_major_minor(st.st_mode, st.st_rdev, &n);
413 if (r < 0)
414 return r;
415
416 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
417 .found = true,
418 .rw = true,
419 .partno = -1,
420 .architecture = _ARCHITECTURE_INVALID,
421 .fstype = TAKE_PTR(t),
422 .node = TAKE_PTR(n),
423 };
424
425 m->encrypted = streq_ptr(fstype, "crypto_LUKS");
426
427 if (!streq(usage, "filesystem")) {
428 r = loop_wait_for_partitions_to_appear(fd, d, 0, flags, &e);
429 if (r < 0)
430 return r;
431 }
432 *ret = TAKE_PTR(m);
433
434 return 0;
435 }
436 }
437
438 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
439 if (!pttype)
440 return -ENOPKG;
441
442 is_gpt = streq_ptr(pttype, "gpt");
443 is_mbr = streq_ptr(pttype, "dos");
444
445 if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr))
446 return -ENOPKG;
447
448 errno = 0;
449 pl = blkid_probe_get_partitions(b);
450 if (!pl)
451 return errno_or_else(ENOMEM);
452
453 r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), flags, &e);
454 if (r < 0)
455 return r;
456
457 FOREACH_DEVICE(e, q) {
458 unsigned long long pflags;
459 blkid_partition pp;
460 const char *node;
461 dev_t qn;
462 int nr;
463
464 r = sd_device_get_devnum(q, &qn);
465 if (r < 0)
466 continue;
467
468 if (st.st_rdev == qn)
469 continue;
470
471 if (!device_is_block(q))
472 continue;
473
474 if (device_is_mmc_special_partition(q))
475 continue;
476
477 r = sd_device_get_devname(q, &node);
478 if (r < 0)
479 continue;
480
481 pp = blkid_partlist_devno_to_partition(pl, qn);
482 if (!pp)
483 continue;
484
485 pflags = blkid_partition_get_flags(pp);
486
487 nr = blkid_partition_get_partno(pp);
488 if (nr < 0)
489 continue;
490
491 if (is_gpt) {
492 int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID;
493 const char *stype, *sid, *fstype = NULL;
494 sd_id128_t type_id, id;
495 bool rw = true;
496
497 sid = blkid_partition_get_uuid(pp);
498 if (!sid)
499 continue;
500 if (sd_id128_from_string(sid, &id) < 0)
501 continue;
502
503 stype = blkid_partition_get_type_string(pp);
504 if (!stype)
505 continue;
506 if (sd_id128_from_string(stype, &type_id) < 0)
507 continue;
508
509 if (sd_id128_equal(type_id, GPT_HOME)) {
510
511 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
512
513 if (pflags & GPT_FLAG_NO_AUTO)
514 continue;
515
516 designator = PARTITION_HOME;
517 rw = !(pflags & GPT_FLAG_READ_ONLY);
518 } else if (sd_id128_equal(type_id, GPT_SRV)) {
519
520 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
521
522 if (pflags & GPT_FLAG_NO_AUTO)
523 continue;
524
525 designator = PARTITION_SRV;
526 rw = !(pflags & GPT_FLAG_READ_ONLY);
527 } else if (sd_id128_equal(type_id, GPT_ESP)) {
528
529 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
530 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
531 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
532
533 if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL)
534 continue;
535
536 designator = PARTITION_ESP;
537 fstype = "vfat";
538
539 } else if (sd_id128_equal(type_id, GPT_XBOOTLDR)) {
540
541 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
542
543 if (pflags & GPT_FLAG_NO_AUTO)
544 continue;
545
546 designator = PARTITION_XBOOTLDR;
547 rw = !(pflags & GPT_FLAG_READ_ONLY);
548 }
549 #ifdef GPT_ROOT_NATIVE
550 else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) {
551
552 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
553
554 if (pflags & GPT_FLAG_NO_AUTO)
555 continue;
556
557 /* If a root ID is specified, ignore everything but the root id */
558 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
559 continue;
560
561 designator = PARTITION_ROOT;
562 architecture = native_architecture();
563 rw = !(pflags & GPT_FLAG_READ_ONLY);
564 } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) {
565
566 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
567
568 if (pflags & GPT_FLAG_NO_AUTO)
569 continue;
570
571 m->can_verity = true;
572
573 /* Ignore verity unless a root hash is specified */
574 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
575 continue;
576
577 designator = PARTITION_ROOT_VERITY;
578 fstype = "DM_verity_hash";
579 architecture = native_architecture();
580 rw = false;
581 }
582 #endif
583 #ifdef GPT_ROOT_SECONDARY
584 else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) {
585
586 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
587
588 if (pflags & GPT_FLAG_NO_AUTO)
589 continue;
590
591 /* If a root ID is specified, ignore everything but the root id */
592 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
593 continue;
594
595 designator = PARTITION_ROOT_SECONDARY;
596 architecture = SECONDARY_ARCHITECTURE;
597 rw = !(pflags & GPT_FLAG_READ_ONLY);
598 } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) {
599
600 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
601
602 if (pflags & GPT_FLAG_NO_AUTO)
603 continue;
604
605 m->can_verity = true;
606
607 /* Ignore verity unless root has is specified */
608 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
609 continue;
610
611 designator = PARTITION_ROOT_SECONDARY_VERITY;
612 fstype = "DM_verity_hash";
613 architecture = SECONDARY_ARCHITECTURE;
614 rw = false;
615 }
616 #endif
617 else if (sd_id128_equal(type_id, GPT_SWAP)) {
618
619 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO);
620
621 if (pflags & GPT_FLAG_NO_AUTO)
622 continue;
623
624 designator = PARTITION_SWAP;
625 fstype = "swap";
626 } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) {
627
628 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
629
630 if (pflags & GPT_FLAG_NO_AUTO)
631 continue;
632
633 if (generic_node)
634 multiple_generic = true;
635 else {
636 generic_nr = nr;
637 generic_rw = !(pflags & GPT_FLAG_READ_ONLY);
638 generic_uuid = id;
639 generic_node = strdup(node);
640 if (!generic_node)
641 return -ENOMEM;
642 }
643
644 } else if (sd_id128_equal(type_id, GPT_TMP)) {
645
646 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
647
648 if (pflags & GPT_FLAG_NO_AUTO)
649 continue;
650
651 designator = PARTITION_TMP;
652 rw = !(pflags & GPT_FLAG_READ_ONLY);
653
654 } else if (sd_id128_equal(type_id, GPT_VAR)) {
655
656 check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY);
657
658 if (pflags & GPT_FLAG_NO_AUTO)
659 continue;
660
661 if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) {
662 sd_id128_t var_uuid;
663
664 /* For /var we insist that the uuid of the partition matches the
665 * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine
666 * ID. Why? Unlike the other partitions /var is inherently
667 * installation specific, hence we need to be careful not to mount it
668 * in the wrong installation. By hashing the partition UUID from
669 * /etc/machine-id we can securely bind the partition to the
670 * installation. */
671
672 r = sd_id128_get_machine_app_specific(GPT_VAR, &var_uuid);
673 if (r < 0)
674 return r;
675
676 if (!sd_id128_equal(var_uuid, id)) {
677 log_debug("Found a /var/ partition, but its UUID didn't match our expectations, ignoring.");
678 continue;
679 }
680 }
681
682 designator = PARTITION_VAR;
683 rw = !(pflags & GPT_FLAG_READ_ONLY);
684 }
685
686 if (designator != _PARTITION_DESIGNATOR_INVALID) {
687 _cleanup_free_ char *t = NULL, *n = NULL;
688
689 /* First one wins */
690 if (m->partitions[designator].found)
691 continue;
692
693 if (fstype) {
694 t = strdup(fstype);
695 if (!t)
696 return -ENOMEM;
697 }
698
699 n = strdup(node);
700 if (!n)
701 return -ENOMEM;
702
703 m->partitions[designator] = (DissectedPartition) {
704 .found = true,
705 .partno = nr,
706 .rw = rw,
707 .architecture = architecture,
708 .node = TAKE_PTR(n),
709 .fstype = TAKE_PTR(t),
710 .uuid = id,
711 };
712 }
713
714 } else if (is_mbr) {
715
716 switch (blkid_partition_get_type(pp)) {
717
718 case 0x83: /* Linux partition */
719
720 if (pflags != 0x80) /* Bootable flag */
721 continue;
722
723 if (generic_node)
724 multiple_generic = true;
725 else {
726 generic_nr = nr;
727 generic_rw = true;
728 generic_node = strdup(node);
729 if (!generic_node)
730 return -ENOMEM;
731 }
732
733 break;
734
735 case 0xEA: { /* Boot Loader Spec extended $BOOT partition */
736 _cleanup_free_ char *n = NULL;
737 sd_id128_t id = SD_ID128_NULL;
738 const char *sid;
739
740 /* First one wins */
741 if (m->partitions[PARTITION_XBOOTLDR].found)
742 continue;
743
744 sid = blkid_partition_get_uuid(pp);
745 if (sid)
746 (void) sd_id128_from_string(sid, &id);
747
748 n = strdup(node);
749 if (!n)
750 return -ENOMEM;
751
752 m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) {
753 .found = true,
754 .partno = nr,
755 .rw = true,
756 .architecture = _ARCHITECTURE_INVALID,
757 .node = TAKE_PTR(n),
758 .uuid = id,
759 };
760
761 break;
762 }}
763 }
764 }
765
766 if (!m->partitions[PARTITION_ROOT].found) {
767 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
768 * either, then check if there's a single generic one, and use that. */
769
770 if (m->partitions[PARTITION_ROOT_VERITY].found)
771 return -EADDRNOTAVAIL;
772
773 if (m->partitions[PARTITION_ROOT_SECONDARY].found) {
774 m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY];
775 zero(m->partitions[PARTITION_ROOT_SECONDARY]);
776
777 m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY];
778 zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
779
780 } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) {
781
782 /* If the root has was set, then we won't fallback to a generic node, because the root hash
783 * decides */
784 if (root_hash)
785 return -EADDRNOTAVAIL;
786
787 /* If we didn't find a generic node, then we can't fix this up either */
788 if (!generic_node)
789 return -ENXIO;
790
791 /* If we didn't find a properly marked root partition, but we did find a single suitable
792 * generic Linux partition, then use this as root partition, if the caller asked for it. */
793 if (multiple_generic)
794 return -ENOTUNIQ;
795
796 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
797 .found = true,
798 .rw = generic_rw,
799 .partno = generic_nr,
800 .architecture = _ARCHITECTURE_INVALID,
801 .node = TAKE_PTR(generic_node),
802 .uuid = generic_uuid,
803 };
804 }
805 }
806
807 if (root_hash) {
808 if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found)
809 return -EADDRNOTAVAIL;
810
811 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
812 * (which would be weird, after all the root hash should only be assigned to one pair of
813 * partitions... */
814 m->partitions[PARTITION_ROOT_SECONDARY].found = false;
815 m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false;
816
817 /* If we found a verity setup, then the root partition is necessarily read-only. */
818 m->partitions[PARTITION_ROOT].rw = false;
819
820 m->verity = true;
821 }
822
823 blkid_free_probe(b);
824 b = NULL;
825
826 /* Fill in file system types if we don't know them yet. */
827 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
828 DissectedPartition *p = m->partitions + i;
829
830 if (!p->found)
831 continue;
832
833 if (!p->fstype && p->node) {
834 r = probe_filesystem(p->node, &p->fstype);
835 if (r < 0 && r != -EUCLEAN)
836 return r;
837 }
838
839 if (streq_ptr(p->fstype, "crypto_LUKS"))
840 m->encrypted = true;
841
842 if (p->fstype && fstype_is_ro(p->fstype))
843 p->rw = false;
844 }
845
846 *ret = TAKE_PTR(m);
847
848 return 0;
849 #else
850 return -EOPNOTSUPP;
851 #endif
852 }
853
854 DissectedImage* dissected_image_unref(DissectedImage *m) {
855 unsigned i;
856
857 if (!m)
858 return NULL;
859
860 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
861 free(m->partitions[i].fstype);
862 free(m->partitions[i].node);
863 free(m->partitions[i].decrypted_fstype);
864 free(m->partitions[i].decrypted_node);
865 }
866
867 free(m->hostname);
868 strv_free(m->machine_info);
869 strv_free(m->os_release);
870
871 return mfree(m);
872 }
873
874 static int is_loop_device(const char *path) {
875 char s[SYS_BLOCK_PATH_MAX("/../loop/")];
876 struct stat st;
877
878 assert(path);
879
880 if (stat(path, &st) < 0)
881 return -errno;
882
883 if (!S_ISBLK(st.st_mode))
884 return -ENOTBLK;
885
886 xsprintf_sys_block_path(s, "/loop/", st.st_dev);
887 if (access(s, F_OK) < 0) {
888 if (errno != ENOENT)
889 return -errno;
890
891 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
892 xsprintf_sys_block_path(s, "/../loop/", st.st_dev);
893 if (access(s, F_OK) < 0)
894 return errno == ENOENT ? false : -errno;
895 }
896
897 return true;
898 }
899
900 static int run_fsck(const char *node, const char *fstype) {
901 int r, exit_status;
902 pid_t pid;
903
904 assert(node);
905 assert(fstype);
906
907 r = fsck_exists(fstype);
908 if (r < 0) {
909 log_debug_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", fstype);
910 return 0;
911 }
912 if (r == 0) {
913 log_debug("Not checking partition %s, as fsck for %s does not exist.", node, fstype);
914 return 0;
915 }
916
917 r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_NULL_STDIO, &pid);
918 if (r < 0)
919 return log_debug_errno(r, "Failed to fork off fsck: %m");
920 if (r == 0) {
921 /* Child */
922 execl("/sbin/fsck", "/sbin/fsck", "-aT", node, NULL);
923 log_debug_errno(errno, "Failed to execl() fsck: %m");
924 _exit(FSCK_OPERATIONAL_ERROR);
925 }
926
927 exit_status = wait_for_terminate_and_check("fsck", pid, 0);
928 if (exit_status < 0)
929 return log_debug_errno(exit_status, "Failed to fork off /sbin/fsck: %m");
930
931 if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) {
932 log_debug("fsck failed with exit status %i.", exit_status);
933
934 if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0)
935 return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), "File system is corrupted, refusing.");
936
937 log_debug("Ignoring fsck error.");
938 }
939
940 return 0;
941 }
942
943 static int mount_partition(
944 DissectedPartition *m,
945 const char *where,
946 const char *directory,
947 uid_t uid_shift,
948 DissectImageFlags flags) {
949
950 _cleanup_free_ char *chased = NULL, *options = NULL;
951 const char *p, *node, *fstype;
952 bool rw;
953 int r;
954
955 assert(m);
956 assert(where);
957
958 node = m->decrypted_node ?: m->node;
959 fstype = m->decrypted_fstype ?: m->fstype;
960
961 if (!m->found || !node || !fstype)
962 return 0;
963
964 /* Stacked encryption? Yuck */
965 if (streq_ptr(fstype, "crypto_LUKS"))
966 return -ELOOP;
967
968 rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY);
969
970 if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) {
971 r = run_fsck(node, fstype);
972 if (r < 0)
973 return r;
974 }
975
976 if (directory) {
977 r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased, NULL);
978 if (r < 0)
979 return r;
980
981 p = chased;
982 } else
983 p = where;
984
985 /* If requested, turn on discard support. */
986 if (fstype_can_discard(fstype) &&
987 ((flags & DISSECT_IMAGE_DISCARD) ||
988 ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node)))) {
989 options = strdup("discard");
990 if (!options)
991 return -ENOMEM;
992 }
993
994 if (uid_is_valid(uid_shift) && uid_shift != 0 && fstype_can_uid_gid(fstype)) {
995 _cleanup_free_ char *uid_option = NULL;
996
997 if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0)
998 return -ENOMEM;
999
1000 if (!strextend_with_separator(&options, ",", uid_option, NULL))
1001 return -ENOMEM;
1002 }
1003
1004 r = mount_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options);
1005 if (r < 0)
1006 return r;
1007
1008 return 1;
1009 }
1010
1011 int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) {
1012 int r, boot_mounted;
1013
1014 assert(m);
1015 assert(where);
1016
1017 if (!m->partitions[PARTITION_ROOT].found)
1018 return -ENXIO;
1019
1020 if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) {
1021 r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, flags);
1022 if (r < 0)
1023 return r;
1024
1025 if (flags & DISSECT_IMAGE_VALIDATE_OS) {
1026 r = path_is_os_tree(where);
1027 if (r < 0)
1028 return r;
1029 if (r == 0)
1030 return -EMEDIUMTYPE;
1031 }
1032 }
1033
1034 if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY)
1035 return 0;
1036
1037 r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags);
1038 if (r < 0)
1039 return r;
1040
1041 r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, flags);
1042 if (r < 0)
1043 return r;
1044
1045 r = mount_partition(m->partitions + PARTITION_VAR, where, "/var", uid_shift, flags);
1046 if (r < 0)
1047 return r;
1048
1049 r = mount_partition(m->partitions + PARTITION_TMP, where, "/var/tmp", uid_shift, flags);
1050 if (r < 0)
1051 return r;
1052
1053 boot_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, flags);
1054 if (boot_mounted < 0)
1055 return boot_mounted;
1056
1057 if (m->partitions[PARTITION_ESP].found) {
1058 /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it
1059 * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */
1060
1061 r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL);
1062 if (r >= 0) {
1063 r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, flags);
1064 if (r < 0)
1065 return r;
1066
1067 } else if (boot_mounted <= 0) {
1068 _cleanup_free_ char *p = NULL;
1069
1070 r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p, NULL);
1071 if (r >= 0 && dir_is_empty(p) > 0) {
1072 r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, flags);
1073 if (r < 0)
1074 return r;
1075 }
1076 }
1077 }
1078
1079 return 0;
1080 }
1081
1082 #if HAVE_LIBCRYPTSETUP
1083 typedef struct DecryptedPartition {
1084 struct crypt_device *device;
1085 char *name;
1086 bool relinquished;
1087 } DecryptedPartition;
1088
1089 struct DecryptedImage {
1090 DecryptedPartition *decrypted;
1091 size_t n_decrypted;
1092 size_t n_allocated;
1093 };
1094 #endif
1095
1096 DecryptedImage* decrypted_image_unref(DecryptedImage* d) {
1097 #if HAVE_LIBCRYPTSETUP
1098 size_t i;
1099 int r;
1100
1101 if (!d)
1102 return NULL;
1103
1104 for (i = 0; i < d->n_decrypted; i++) {
1105 DecryptedPartition *p = d->decrypted + i;
1106
1107 if (p->device && p->name && !p->relinquished) {
1108 r = crypt_deactivate(p->device, p->name);
1109 if (r < 0)
1110 log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name);
1111 }
1112
1113 if (p->device)
1114 crypt_free(p->device);
1115 free(p->name);
1116 }
1117
1118 free(d);
1119 #endif
1120 return NULL;
1121 }
1122
1123 #if HAVE_LIBCRYPTSETUP
1124
1125 static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) {
1126 _cleanup_free_ char *name = NULL, *node = NULL;
1127 const char *base;
1128
1129 assert(original_node);
1130 assert(suffix);
1131 assert(ret_name);
1132 assert(ret_node);
1133
1134 base = strrchr(original_node, '/');
1135 if (!base)
1136 return -EINVAL;
1137 base++;
1138 if (isempty(base))
1139 return -EINVAL;
1140
1141 name = strjoin(base, suffix);
1142 if (!name)
1143 return -ENOMEM;
1144 if (!filename_is_valid(name))
1145 return -EINVAL;
1146
1147 node = path_join(crypt_get_dir(), name);
1148 if (!node)
1149 return -ENOMEM;
1150
1151 *ret_name = TAKE_PTR(name);
1152 *ret_node = TAKE_PTR(node);
1153
1154 return 0;
1155 }
1156
1157 static int decrypt_partition(
1158 DissectedPartition *m,
1159 const char *passphrase,
1160 DissectImageFlags flags,
1161 DecryptedImage *d) {
1162
1163 _cleanup_free_ char *node = NULL, *name = NULL;
1164 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
1165 int r;
1166
1167 assert(m);
1168 assert(d);
1169
1170 if (!m->found || !m->node || !m->fstype)
1171 return 0;
1172
1173 if (!streq(m->fstype, "crypto_LUKS"))
1174 return 0;
1175
1176 if (!passphrase)
1177 return -ENOKEY;
1178
1179 r = make_dm_name_and_node(m->node, "-decrypted", &name, &node);
1180 if (r < 0)
1181 return r;
1182
1183 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
1184 return -ENOMEM;
1185
1186 r = crypt_init(&cd, m->node);
1187 if (r < 0)
1188 return log_debug_errno(r, "Failed to initialize dm-crypt: %m");
1189
1190 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
1191
1192 r = crypt_load(cd, CRYPT_LUKS, NULL);
1193 if (r < 0)
1194 return log_debug_errno(r, "Failed to load LUKS metadata: %m");
1195
1196 r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase),
1197 ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) |
1198 ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0));
1199 if (r < 0) {
1200 log_debug_errno(r, "Failed to activate LUKS device: %m");
1201 return r == -EPERM ? -EKEYREJECTED : r;
1202 }
1203
1204 d->decrypted[d->n_decrypted].name = TAKE_PTR(name);
1205 d->decrypted[d->n_decrypted].device = TAKE_PTR(cd);
1206 d->n_decrypted++;
1207
1208 m->decrypted_node = TAKE_PTR(node);
1209
1210 return 0;
1211 }
1212
1213 static int verity_partition(
1214 DissectedPartition *m,
1215 DissectedPartition *v,
1216 const void *root_hash,
1217 size_t root_hash_size,
1218 DissectImageFlags flags,
1219 DecryptedImage *d) {
1220
1221 _cleanup_free_ char *node = NULL, *name = NULL;
1222 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
1223 int r;
1224
1225 assert(m);
1226 assert(v);
1227
1228 if (!root_hash)
1229 return 0;
1230
1231 if (!m->found || !m->node || !m->fstype)
1232 return 0;
1233 if (!v->found || !v->node || !v->fstype)
1234 return 0;
1235
1236 if (!streq(v->fstype, "DM_verity_hash"))
1237 return 0;
1238
1239 r = make_dm_name_and_node(m->node, "-verity", &name, &node);
1240 if (r < 0)
1241 return r;
1242
1243 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
1244 return -ENOMEM;
1245
1246 r = crypt_init(&cd, v->node);
1247 if (r < 0)
1248 return r;
1249
1250 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
1251
1252 r = crypt_load(cd, CRYPT_VERITY, NULL);
1253 if (r < 0)
1254 return r;
1255
1256 r = crypt_set_data_device(cd, m->node);
1257 if (r < 0)
1258 return r;
1259
1260 r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY);
1261 if (r < 0)
1262 return r;
1263
1264 d->decrypted[d->n_decrypted].name = TAKE_PTR(name);
1265 d->decrypted[d->n_decrypted].device = TAKE_PTR(cd);
1266 d->n_decrypted++;
1267
1268 m->decrypted_node = TAKE_PTR(node);
1269
1270 return 0;
1271 }
1272 #endif
1273
1274 int dissected_image_decrypt(
1275 DissectedImage *m,
1276 const char *passphrase,
1277 const void *root_hash,
1278 size_t root_hash_size,
1279 DissectImageFlags flags,
1280 DecryptedImage **ret) {
1281
1282 #if HAVE_LIBCRYPTSETUP
1283 _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
1284 unsigned i;
1285 int r;
1286 #endif
1287
1288 assert(m);
1289 assert(root_hash || root_hash_size == 0);
1290
1291 /* Returns:
1292 *
1293 * = 0 → There was nothing to decrypt
1294 * > 0 → Decrypted successfully
1295 * -ENOKEY → There's something to decrypt but no key was supplied
1296 * -EKEYREJECTED → Passed key was not correct
1297 */
1298
1299 if (root_hash && root_hash_size < sizeof(sd_id128_t))
1300 return -EINVAL;
1301
1302 if (!m->encrypted && !m->verity) {
1303 *ret = NULL;
1304 return 0;
1305 }
1306
1307 #if HAVE_LIBCRYPTSETUP
1308 d = new0(DecryptedImage, 1);
1309 if (!d)
1310 return -ENOMEM;
1311
1312 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
1313 DissectedPartition *p = m->partitions + i;
1314 int k;
1315
1316 if (!p->found)
1317 continue;
1318
1319 r = decrypt_partition(p, passphrase, flags, d);
1320 if (r < 0)
1321 return r;
1322
1323 k = PARTITION_VERITY_OF(i);
1324 if (k >= 0) {
1325 r = verity_partition(p, m->partitions + k, root_hash, root_hash_size, flags, d);
1326 if (r < 0)
1327 return r;
1328 }
1329
1330 if (!p->decrypted_fstype && p->decrypted_node) {
1331 r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype);
1332 if (r < 0 && r != -EUCLEAN)
1333 return r;
1334 }
1335 }
1336
1337 *ret = TAKE_PTR(d);
1338
1339 return 1;
1340 #else
1341 return -EOPNOTSUPP;
1342 #endif
1343 }
1344
1345 int dissected_image_decrypt_interactively(
1346 DissectedImage *m,
1347 const char *passphrase,
1348 const void *root_hash,
1349 size_t root_hash_size,
1350 DissectImageFlags flags,
1351 DecryptedImage **ret) {
1352
1353 _cleanup_strv_free_erase_ char **z = NULL;
1354 int n = 3, r;
1355
1356 if (passphrase)
1357 n--;
1358
1359 for (;;) {
1360 r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, flags, ret);
1361 if (r >= 0)
1362 return r;
1363 if (r == -EKEYREJECTED)
1364 log_error_errno(r, "Incorrect passphrase, try again!");
1365 else if (r != -ENOKEY)
1366 return log_error_errno(r, "Failed to decrypt image: %m");
1367
1368 if (--n < 0)
1369 return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED),
1370 "Too many retries.");
1371
1372 z = strv_free(z);
1373
1374 r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z);
1375 if (r < 0)
1376 return log_error_errno(r, "Failed to query for passphrase: %m");
1377
1378 passphrase = z[0];
1379 }
1380 }
1381
1382 int decrypted_image_relinquish(DecryptedImage *d) {
1383
1384 #if HAVE_LIBCRYPTSETUP
1385 size_t i;
1386 int r;
1387 #endif
1388
1389 assert(d);
1390
1391 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1392 * that we don't clean it up ourselves either anymore */
1393
1394 #if HAVE_LIBCRYPTSETUP
1395 for (i = 0; i < d->n_decrypted; i++) {
1396 DecryptedPartition *p = d->decrypted + i;
1397
1398 if (p->relinquished)
1399 continue;
1400
1401 r = dm_deferred_remove(p->name);
1402 if (r < 0)
1403 return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name);
1404
1405 p->relinquished = true;
1406 }
1407 #endif
1408
1409 return 0;
1410 }
1411
1412 int root_hash_load(const char *image, void **ret, size_t *ret_size) {
1413 _cleanup_free_ char *text = NULL;
1414 _cleanup_free_ void *k = NULL;
1415 size_t l;
1416 int r;
1417
1418 assert(image);
1419 assert(ret);
1420 assert(ret_size);
1421
1422 if (is_device_path(image)) {
1423 /* If we are asked to load the root hash for a device node, exit early */
1424 *ret = NULL;
1425 *ret_size = 0;
1426 return 0;
1427 }
1428
1429 r = getxattr_malloc(image, "user.verity.roothash", &text, true);
1430 if (r < 0) {
1431 char *fn, *e, *n;
1432
1433 if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT))
1434 return r;
1435
1436 fn = newa(char, strlen(image) + STRLEN(".roothash") + 1);
1437 n = stpcpy(fn, image);
1438 e = endswith(fn, ".raw");
1439 if (e)
1440 n = e;
1441
1442 strcpy(n, ".roothash");
1443
1444 r = read_one_line_file(fn, &text);
1445 if (r == -ENOENT) {
1446 *ret = NULL;
1447 *ret_size = 0;
1448 return 0;
1449 }
1450 if (r < 0)
1451 return r;
1452 }
1453
1454 r = unhexmem(text, strlen(text), &k, &l);
1455 if (r < 0)
1456 return r;
1457 if (l < sizeof(sd_id128_t))
1458 return -EINVAL;
1459
1460 *ret = TAKE_PTR(k);
1461 *ret_size = l;
1462
1463 return 1;
1464 }
1465
1466 int dissected_image_acquire_metadata(DissectedImage *m) {
1467
1468 enum {
1469 META_HOSTNAME,
1470 META_MACHINE_ID,
1471 META_MACHINE_INFO,
1472 META_OS_RELEASE,
1473 _META_MAX,
1474 };
1475
1476 static const char *const paths[_META_MAX] = {
1477 [META_HOSTNAME] = "/etc/hostname\0",
1478 [META_MACHINE_ID] = "/etc/machine-id\0",
1479 [META_MACHINE_INFO] = "/etc/machine-info\0",
1480 [META_OS_RELEASE] = "/etc/os-release\0"
1481 "/usr/lib/os-release\0",
1482 };
1483
1484 _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL;
1485 _cleanup_(rmdir_and_freep) char *t = NULL;
1486 _cleanup_(sigkill_waitp) pid_t child = 0;
1487 sd_id128_t machine_id = SD_ID128_NULL;
1488 _cleanup_free_ char *hostname = NULL;
1489 unsigned n_meta_initialized = 0, k;
1490 int fds[2 * _META_MAX], r;
1491
1492 BLOCK_SIGNALS(SIGCHLD);
1493
1494 assert(m);
1495
1496 for (; n_meta_initialized < _META_MAX; n_meta_initialized ++)
1497 if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) {
1498 r = -errno;
1499 goto finish;
1500 }
1501
1502 r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t);
1503 if (r < 0)
1504 goto finish;
1505
1506 r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child);
1507 if (r < 0)
1508 goto finish;
1509 if (r == 0) {
1510 r = dissected_image_mount(m, t, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_VALIDATE_OS);
1511 if (r < 0) {
1512 log_debug_errno(r, "Failed to mount dissected image: %m");
1513 _exit(EXIT_FAILURE);
1514 }
1515
1516 for (k = 0; k < _META_MAX; k++) {
1517 _cleanup_close_ int fd = -1;
1518 const char *p;
1519
1520 fds[2*k] = safe_close(fds[2*k]);
1521
1522 NULSTR_FOREACH(p, paths[k]) {
1523 fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
1524 if (fd >= 0)
1525 break;
1526 }
1527 if (fd < 0) {
1528 log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]);
1529 continue;
1530 }
1531
1532 r = copy_bytes(fd, fds[2*k+1], (uint64_t) -1, 0);
1533 if (r < 0)
1534 _exit(EXIT_FAILURE);
1535
1536 fds[2*k+1] = safe_close(fds[2*k+1]);
1537 }
1538
1539 _exit(EXIT_SUCCESS);
1540 }
1541
1542 for (k = 0; k < _META_MAX; k++) {
1543 _cleanup_fclose_ FILE *f = NULL;
1544
1545 fds[2*k+1] = safe_close(fds[2*k+1]);
1546
1547 f = take_fdopen(&fds[2*k], "r");
1548 if (!f) {
1549 r = -errno;
1550 goto finish;
1551 }
1552
1553 switch (k) {
1554
1555 case META_HOSTNAME:
1556 r = read_etc_hostname_stream(f, &hostname);
1557 if (r < 0)
1558 log_debug_errno(r, "Failed to read /etc/hostname: %m");
1559
1560 break;
1561
1562 case META_MACHINE_ID: {
1563 _cleanup_free_ char *line = NULL;
1564
1565 r = read_line(f, LONG_LINE_MAX, &line);
1566 if (r < 0)
1567 log_debug_errno(r, "Failed to read /etc/machine-id: %m");
1568 else if (r == 33) {
1569 r = sd_id128_from_string(line, &machine_id);
1570 if (r < 0)
1571 log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line);
1572 } else if (r == 0)
1573 log_debug("/etc/machine-id file is empty.");
1574 else
1575 log_debug("/etc/machine-id has unexpected length %i.", r);
1576
1577 break;
1578 }
1579
1580 case META_MACHINE_INFO:
1581 r = load_env_file_pairs(f, "machine-info", &machine_info);
1582 if (r < 0)
1583 log_debug_errno(r, "Failed to read /etc/machine-info: %m");
1584
1585 break;
1586
1587 case META_OS_RELEASE:
1588 r = load_env_file_pairs(f, "os-release", &os_release);
1589 if (r < 0)
1590 log_debug_errno(r, "Failed to read OS release file: %m");
1591
1592 break;
1593 }
1594 }
1595
1596 r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
1597 child = 0;
1598 if (r < 0)
1599 goto finish;
1600 if (r != EXIT_SUCCESS)
1601 return -EPROTO;
1602
1603 free_and_replace(m->hostname, hostname);
1604 m->machine_id = machine_id;
1605 strv_free_and_replace(m->machine_info, machine_info);
1606 strv_free_and_replace(m->os_release, os_release);
1607
1608 finish:
1609 for (k = 0; k < n_meta_initialized; k++)
1610 safe_close_pair(fds + 2*k);
1611
1612 return r;
1613 }
1614
1615 int dissect_image_and_warn(
1616 int fd,
1617 const char *name,
1618 const void *root_hash,
1619 size_t root_hash_size,
1620 DissectImageFlags flags,
1621 DissectedImage **ret) {
1622
1623 _cleanup_free_ char *buffer = NULL;
1624 int r;
1625
1626 if (!name) {
1627 r = fd_get_path(fd, &buffer);
1628 if (r < 0)
1629 return r;
1630
1631 name = buffer;
1632 }
1633
1634 r = dissect_image(fd, root_hash, root_hash_size, flags, ret);
1635
1636 switch (r) {
1637
1638 case -EOPNOTSUPP:
1639 return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support.");
1640
1641 case -ENOPKG:
1642 return log_error_errno(r, "Couldn't identify a suitable partition table or file system in '%s'.", name);
1643
1644 case -EADDRNOTAVAIL:
1645 return log_error_errno(r, "No root partition for specified root hash found in '%s'.", name);
1646
1647 case -ENOTUNIQ:
1648 return log_error_errno(r, "Multiple suitable root partitions found in image '%s'.", name);
1649
1650 case -ENXIO:
1651 return log_error_errno(r, "No suitable root partition found in image '%s'.", name);
1652
1653 case -EPROTONOSUPPORT:
1654 return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name);
1655
1656 default:
1657 if (r < 0)
1658 return log_error_errno(r, "Failed to dissect image '%s': %m", name);
1659
1660 return r;
1661 }
1662 }
1663
1664 static const char *const partition_designator_table[] = {
1665 [PARTITION_ROOT] = "root",
1666 [PARTITION_ROOT_SECONDARY] = "root-secondary",
1667 [PARTITION_HOME] = "home",
1668 [PARTITION_SRV] = "srv",
1669 [PARTITION_ESP] = "esp",
1670 [PARTITION_XBOOTLDR] = "xbootldr",
1671 [PARTITION_SWAP] = "swap",
1672 [PARTITION_ROOT_VERITY] = "root-verity",
1673 [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity",
1674 [PARTITION_TMP] = "tmp",
1675 [PARTITION_VAR] = "var",
1676 };
1677
1678 DEFINE_STRING_TABLE_LOOKUP(partition_designator, int);