]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/dissect-image.c
man: move non-target units together (#6934)
[thirdparty/systemd.git] / src / shared / dissect-image.c
CommitLineData
8c1be37e
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2016 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
18b5886e
LP
20#ifdef HAVE_LIBCRYPTSETUP
21#include <libcryptsetup.h>
22#endif
8c1be37e
LP
23#include <sys/mount.h>
24
25#include "architecture.h"
18b5886e 26#include "ask-password-api.h"
8c1be37e
LP
27#include "blkid-util.h"
28#include "dissect-image.h"
18b5886e 29#include "fd-util.h"
78ebe980 30#include "fileio.h"
2eedfd2d 31#include "fs-util.h"
8c1be37e 32#include "gpt.h"
78ebe980 33#include "hexdecoct.h"
dcce98a4 34#include "linux-3.13/dm-ioctl.h"
8c1be37e
LP
35#include "mount-util.h"
36#include "path-util.h"
37#include "stat-util.h"
18b5886e 38#include "stdio-util.h"
8c1be37e
LP
39#include "string-table.h"
40#include "string-util.h"
2eedfd2d 41#include "strv.h"
8c1be37e 42#include "udev-util.h"
41488e1f 43#include "xattr-util.h"
8c1be37e 44
d1c536f5 45_unused_ static int probe_filesystem(const char *node, char **ret_fstype) {
18b5886e
LP
46#ifdef HAVE_BLKID
47 _cleanup_blkid_free_probe_ blkid_probe b = NULL;
48 const char *fstype;
49 int r;
50
51 b = blkid_new_probe_from_filename(node);
52 if (!b)
53 return -ENOMEM;
54
55 blkid_probe_enable_superblocks(b, 1);
56 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
57
58 errno = 0;
59 r = blkid_do_safeprobe(b);
60 if (r == -2 || r == 1) {
61 log_debug("Failed to identify any partition type on partition %s", node);
62 goto not_found;
63 }
b382db9f
ZJS
64 if (r != 0)
65 return -errno ?: -EIO;
18b5886e
LP
66
67 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
68
69 if (fstype) {
70 char *t;
71
72 t = strdup(fstype);
73 if (!t)
74 return -ENOMEM;
75
76 *ret_fstype = t;
77 return 1;
78 }
79
80not_found:
81 *ret_fstype = NULL;
82 return 0;
d1c536f5
ZJS
83#else
84 return -EOPNOTSUPP;
a75e27eb 85#endif
d1c536f5 86}
18b5886e 87
9b6deb03 88int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DissectedImage **ret) {
8c1be37e
LP
89
90#ifdef HAVE_BLKID
4623e8e6 91 sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
8c1be37e
LP
92 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
93 bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
94 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
95 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
96 _cleanup_blkid_free_probe_ blkid_probe b = NULL;
97 _cleanup_udev_unref_ struct udev *udev = NULL;
98 _cleanup_free_ char *generic_node = NULL;
be30ad41 99 sd_id128_t generic_uuid = SD_ID128_NULL;
9b6deb03 100 const char *pttype = NULL;
8c1be37e
LP
101 struct udev_list_entry *first, *item;
102 blkid_partlist pl;
103 int r, generic_nr;
104 struct stat st;
105 unsigned i;
106
107 assert(fd >= 0);
108 assert(ret);
4623e8e6 109 assert(root_hash || root_hash_size == 0);
8c1be37e
LP
110
111 /* Probes a disk image, and returns information about what it found in *ret.
112 *
4623e8e6
LP
113 * Returns -ENOPKG if no suitable partition table or file system could be found.
114 * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */
115
116 if (root_hash) {
117 /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
118 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
119 * 128bit. */
120
121 if (root_hash_size < sizeof(sd_id128_t))
122 return -EINVAL;
123
124 memcpy(&root_uuid, root_hash, sizeof(sd_id128_t));
125 memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t));
126
127 if (sd_id128_is_null(root_uuid))
128 return -EINVAL;
129 if (sd_id128_is_null(verity_uuid))
130 return -EINVAL;
131 }
8c1be37e
LP
132
133 if (fstat(fd, &st) < 0)
134 return -errno;
135
136 if (!S_ISBLK(st.st_mode))
137 return -ENOTBLK;
138
139 b = blkid_new_probe();
140 if (!b)
141 return -ENOMEM;
142
143 errno = 0;
144 r = blkid_probe_set_device(b, fd, 0, 0);
b382db9f
ZJS
145 if (r != 0)
146 return -errno ?: -ENOMEM;
8c1be37e 147
9b6deb03
LP
148 if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
149 /* Look for file system superblocks, unless we only shall look for GPT partition tables */
150 blkid_probe_enable_superblocks(b, 1);
151 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE);
152 }
153
8c1be37e
LP
154 blkid_probe_enable_partitions(b, 1);
155 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
156
157 errno = 0;
158 r = blkid_do_safeprobe(b);
159 if (r == -2 || r == 1) {
160 log_debug("Failed to identify any partition table.");
161 return -ENOPKG;
162 }
b382db9f
ZJS
163 if (r != 0)
164 return -errno ?: -EIO;
8c1be37e
LP
165
166 m = new0(DissectedImage, 1);
167 if (!m)
168 return -ENOMEM;
169
e0f9e7bd
LP
170 if (!(flags & DISSECT_IMAGE_GPT_ONLY) &&
171 (flags & DISSECT_IMAGE_REQUIRE_ROOT)) {
9b6deb03 172 const char *usage = NULL;
8c1be37e 173
9b6deb03
LP
174 (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
175 if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
176 _cleanup_free_ char *t = NULL, *n = NULL;
177 const char *fstype = NULL;
8c1be37e 178
9b6deb03
LP
179 /* OK, we have found a file system, that's our root partition then. */
180 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
8c1be37e 181
9b6deb03
LP
182 if (fstype) {
183 t = strdup(fstype);
184 if (!t)
185 return -ENOMEM;
186 }
187
188 if (asprintf(&n, "/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0)
189 return -ENOMEM;
8c1be37e 190
9b6deb03
LP
191 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
192 .found = true,
193 .rw = true,
194 .partno = -1,
195 .architecture = _ARCHITECTURE_INVALID,
196 .fstype = t,
197 .node = n,
198 };
8c1be37e 199
9b6deb03 200 t = n = NULL;
8c1be37e 201
9b6deb03 202 m->encrypted = streq(fstype, "crypto_LUKS");
18b5886e 203
9b6deb03
LP
204 *ret = m;
205 m = NULL;
8c1be37e 206
9b6deb03
LP
207 return 0;
208 }
8c1be37e
LP
209 }
210
211 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
212 if (!pttype)
213 return -ENOPKG;
214
215 is_gpt = streq_ptr(pttype, "gpt");
216 is_mbr = streq_ptr(pttype, "dos");
217
9b6deb03 218 if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr))
8c1be37e
LP
219 return -ENOPKG;
220
221 errno = 0;
222 pl = blkid_probe_get_partitions(b);
b382db9f
ZJS
223 if (!pl)
224 return -errno ?: -ENOMEM;
8c1be37e
LP
225
226 udev = udev_new();
227 if (!udev)
228 return -errno;
229
230 d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
231 if (!d)
232 return -ENOMEM;
233
234 for (i = 0;; i++) {
235 int n, z;
236
237 if (i >= 10) {
238 log_debug("Kernel partitions never appeared.");
239 return -ENXIO;
240 }
241
242 e = udev_enumerate_new(udev);
243 if (!e)
244 return -errno;
245
246 r = udev_enumerate_add_match_parent(e, d);
247 if (r < 0)
248 return r;
249
250 r = udev_enumerate_scan_devices(e);
251 if (r < 0)
252 return r;
253
254 /* Count the partitions enumerated by the kernel */
255 n = 0;
256 first = udev_enumerate_get_list_entry(e);
257 udev_list_entry_foreach(item, first)
258 n++;
259
260 /* Count the partitions enumerated by blkid */
261 z = blkid_partlist_numof_partitions(pl);
262 if (n == z + 1)
263 break;
264 if (n > z + 1) {
265 log_debug("blkid and kernel partition list do not match.");
266 return -EIO;
267 }
268 if (n < z + 1) {
269 unsigned j;
270
271 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running
272 * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a
273 * synchronous call that waits until probing is complete. */
274
275 for (j = 0; j < 20; j++) {
276
277 r = ioctl(fd, BLKRRPART, 0);
278 if (r < 0)
279 r = -errno;
280 if (r >= 0 || r != -EBUSY)
281 break;
282
283 /* If something else has the device open, such as an udev rule, the ioctl will return
284 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a
285 * bit, and try again.
286 *
287 * This is really something they should fix in the kernel! */
288
289 usleep(50 * USEC_PER_MSEC);
290 }
291
292 if (r < 0)
293 return r;
294 }
295
296 e = udev_enumerate_unref(e);
297 }
298
299 first = udev_enumerate_get_list_entry(e);
300 udev_list_entry_foreach(item, first) {
301 _cleanup_udev_device_unref_ struct udev_device *q;
9b6deb03 302 unsigned long long pflags;
8c1be37e 303 blkid_partition pp;
7be1420f 304 const char *node, *sysname;
8c1be37e
LP
305 dev_t qn;
306 int nr;
307
308 q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
309 if (!q)
310 return -errno;
311
312 qn = udev_device_get_devnum(q);
313 if (major(qn) == 0)
314 continue;
315
316 if (st.st_rdev == qn)
317 continue;
318
7be1420f
LP
319 /* Filter out weird MMC RPMB partitions, which cannot reasonably be read, see
320 * https://github.com/systemd/systemd/issues/5806 */
321 sysname = udev_device_get_sysname(q);
322 if (sysname && startswith(sysname, "mmcblk") && endswith(sysname, "rpmb"))
323 continue;
324
8c1be37e
LP
325 node = udev_device_get_devnode(q);
326 if (!node)
327 continue;
328
329 pp = blkid_partlist_devno_to_partition(pl, qn);
330 if (!pp)
331 continue;
332
9b6deb03 333 pflags = blkid_partition_get_flags(pp);
8c1be37e
LP
334
335 nr = blkid_partition_get_partno(pp);
336 if (nr < 0)
337 continue;
338
339 if (is_gpt) {
340 int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID;
4623e8e6
LP
341 const char *stype, *sid, *fstype = NULL;
342 sd_id128_t type_id, id;
8c1be37e
LP
343 bool rw = true;
344
4623e8e6
LP
345 sid = blkid_partition_get_uuid(pp);
346 if (!sid)
347 continue;
348 if (sd_id128_from_string(sid, &id) < 0)
349 continue;
350
8c1be37e
LP
351 stype = blkid_partition_get_type_string(pp);
352 if (!stype)
353 continue;
8c1be37e
LP
354 if (sd_id128_from_string(stype, &type_id) < 0)
355 continue;
356
357 if (sd_id128_equal(type_id, GPT_HOME)) {
a48dd347
LP
358
359 if (pflags & GPT_FLAG_NO_AUTO)
360 continue;
361
8c1be37e 362 designator = PARTITION_HOME;
9b6deb03 363 rw = !(pflags & GPT_FLAG_READ_ONLY);
8c1be37e 364 } else if (sd_id128_equal(type_id, GPT_SRV)) {
a48dd347
LP
365
366 if (pflags & GPT_FLAG_NO_AUTO)
367 continue;
368
8c1be37e 369 designator = PARTITION_SRV;
9b6deb03 370 rw = !(pflags & GPT_FLAG_READ_ONLY);
8c1be37e 371 } else if (sd_id128_equal(type_id, GPT_ESP)) {
a48dd347
LP
372
373 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
374 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
375 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
376
377 if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL)
378 continue;
379
8c1be37e
LP
380 designator = PARTITION_ESP;
381 fstype = "vfat";
382 }
383#ifdef GPT_ROOT_NATIVE
384 else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) {
4623e8e6 385
a48dd347
LP
386 if (pflags & GPT_FLAG_NO_AUTO)
387 continue;
388
4623e8e6
LP
389 /* If a root ID is specified, ignore everything but the root id */
390 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
391 continue;
392
8c1be37e
LP
393 designator = PARTITION_ROOT;
394 architecture = native_architecture();
9b6deb03 395 rw = !(pflags & GPT_FLAG_READ_ONLY);
4f8b86e3 396 } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) {
4623e8e6 397
a48dd347
LP
398 if (pflags & GPT_FLAG_NO_AUTO)
399 continue;
400
4623e8e6
LP
401 m->can_verity = true;
402
403 /* Ignore verity unless a root hash is specified */
404 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
405 continue;
406
407 designator = PARTITION_ROOT_VERITY;
408 fstype = "DM_verity_hash";
409 architecture = native_architecture();
410 rw = false;
411 }
412#endif
8c1be37e
LP
413#ifdef GPT_ROOT_SECONDARY
414 else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) {
4623e8e6 415
a48dd347
LP
416 if (pflags & GPT_FLAG_NO_AUTO)
417 continue;
418
4623e8e6
LP
419 /* If a root ID is specified, ignore everything but the root id */
420 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
421 continue;
422
8c1be37e
LP
423 designator = PARTITION_ROOT_SECONDARY;
424 architecture = SECONDARY_ARCHITECTURE;
9b6deb03 425 rw = !(pflags & GPT_FLAG_READ_ONLY);
4f8b86e3 426 } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) {
a48dd347
LP
427
428 if (pflags & GPT_FLAG_NO_AUTO)
429 continue;
430
4623e8e6
LP
431 m->can_verity = true;
432
433 /* Ignore verity unless root has is specified */
434 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
435 continue;
436
437 designator = PARTITION_ROOT_SECONDARY_VERITY;
438 fstype = "DM_verity_hash";
439 architecture = SECONDARY_ARCHITECTURE;
440 rw = false;
441 }
8c1be37e
LP
442#endif
443 else if (sd_id128_equal(type_id, GPT_SWAP)) {
a48dd347
LP
444
445 if (pflags & GPT_FLAG_NO_AUTO)
446 continue;
447
8c1be37e
LP
448 designator = PARTITION_SWAP;
449 fstype = "swap";
450 } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) {
451
a48dd347
LP
452 if (pflags & GPT_FLAG_NO_AUTO)
453 continue;
454
8c1be37e
LP
455 if (generic_node)
456 multiple_generic = true;
457 else {
458 generic_nr = nr;
9b6deb03 459 generic_rw = !(pflags & GPT_FLAG_READ_ONLY);
be30ad41 460 generic_uuid = id;
8c1be37e
LP
461 generic_node = strdup(node);
462 if (!generic_node)
463 return -ENOMEM;
464 }
465 }
466
467 if (designator != _PARTITION_DESIGNATOR_INVALID) {
468 _cleanup_free_ char *t = NULL, *n = NULL;
469
470 /* First one wins */
471 if (m->partitions[designator].found)
472 continue;
473
474 if (fstype) {
475 t = strdup(fstype);
476 if (!t)
477 return -ENOMEM;
478 }
479
480 n = strdup(node);
481 if (!n)
482 return -ENOMEM;
483
484 m->partitions[designator] = (DissectedPartition) {
485 .found = true,
486 .partno = nr,
487 .rw = rw,
488 .architecture = architecture,
489 .node = n,
490 .fstype = t,
be30ad41 491 .uuid = id,
8c1be37e
LP
492 };
493
494 n = t = NULL;
495 }
496
497 } else if (is_mbr) {
498
9b6deb03 499 if (pflags != 0x80) /* Bootable flag */
8c1be37e
LP
500 continue;
501
502 if (blkid_partition_get_type(pp) != 0x83) /* Linux partition */
503 continue;
504
505 if (generic_node)
506 multiple_generic = true;
507 else {
508 generic_nr = nr;
509 generic_rw = true;
510 generic_node = strdup(node);
511 if (!generic_node)
512 return -ENOMEM;
513 }
514 }
515 }
516
517 if (!m->partitions[PARTITION_ROOT].found) {
518 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
519 * either, then check if there's a single generic one, and use that. */
520
4623e8e6 521 if (m->partitions[PARTITION_ROOT_VERITY].found)
e0f9e7bd 522 return -EADDRNOTAVAIL;
4623e8e6 523
8c1be37e
LP
524 if (m->partitions[PARTITION_ROOT_SECONDARY].found) {
525 m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY];
526 zero(m->partitions[PARTITION_ROOT_SECONDARY]);
4623e8e6
LP
527
528 m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY];
529 zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
530
e0f9e7bd
LP
531 } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) {
532
533 /* If the root has was set, then we won't fallback to a generic node, because the root hash
534 * decides */
535 if (root_hash)
536 return -EADDRNOTAVAIL;
8c1be37e 537
e0f9e7bd
LP
538 /* If we didn't find a generic node, then we can't fix this up either */
539 if (!generic_node)
540 return -ENXIO;
541
542 /* If we didn't find a properly marked root partition, but we did find a single suitable
543 * generic Linux partition, then use this as root partition, if the caller asked for it. */
8c1be37e
LP
544 if (multiple_generic)
545 return -ENOTUNIQ;
546
547 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
548 .found = true,
549 .rw = generic_rw,
550 .partno = generic_nr,
551 .architecture = _ARCHITECTURE_INVALID,
552 .node = generic_node,
be30ad41 553 .uuid = generic_uuid,
8c1be37e
LP
554 };
555
556 generic_node = NULL;
e0f9e7bd 557 }
8c1be37e
LP
558 }
559
4623e8e6 560 if (root_hash) {
e0f9e7bd 561 if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found)
4623e8e6
LP
562 return -EADDRNOTAVAIL;
563
564 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
565 * (which would be weird, after all the root hash should only be assigned to one pair of
566 * partitions... */
567 m->partitions[PARTITION_ROOT_SECONDARY].found = false;
568 m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false;
569
570 /* If we found a verity setup, then the root partition is necessarily read-only. */
571 m->partitions[PARTITION_ROOT].rw = false;
572
573 m->verity = true;
574 }
575
18b5886e
LP
576 blkid_free_probe(b);
577 b = NULL;
578
8c1be37e
LP
579 /* Fill in file system types if we don't know them yet. */
580 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
18b5886e 581 DissectedPartition *p = m->partitions + i;
8c1be37e 582
18b5886e 583 if (!p->found)
8c1be37e
LP
584 continue;
585
18b5886e
LP
586 if (!p->fstype && p->node) {
587 r = probe_filesystem(p->node, &p->fstype);
588 if (r < 0)
589 return r;
8c1be37e
LP
590 }
591
18b5886e
LP
592 if (streq_ptr(p->fstype, "crypto_LUKS"))
593 m->encrypted = true;
8c1be37e
LP
594 }
595
596 *ret = m;
597 m = NULL;
598
599 return 0;
600#else
601 return -EOPNOTSUPP;
602#endif
603}
604
605DissectedImage* dissected_image_unref(DissectedImage *m) {
606 unsigned i;
607
608 if (!m)
609 return NULL;
610
611 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
612 free(m->partitions[i].fstype);
613 free(m->partitions[i].node);
18b5886e
LP
614 free(m->partitions[i].decrypted_fstype);
615 free(m->partitions[i].decrypted_node);
8c1be37e
LP
616 }
617
618 free(m);
619 return NULL;
620}
621
18b5886e
LP
622static int is_loop_device(const char *path) {
623 char s[strlen("/sys/dev/block/") + DECIMAL_STR_MAX(dev_t) + 1 + DECIMAL_STR_MAX(dev_t) + strlen("/../loop/")];
624 struct stat st;
625
626 assert(path);
627
628 if (stat(path, &st) < 0)
629 return -errno;
630
631 if (!S_ISBLK(st.st_mode))
632 return -ENOTBLK;
633
634 xsprintf(s, "/sys/dev/block/%u:%u/loop/", major(st.st_rdev), minor(st.st_rdev));
635 if (access(s, F_OK) < 0) {
636 if (errno != ENOENT)
637 return -errno;
638
639 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
640 xsprintf(s, "/sys/dev/block/%u:%u/../loop/", major(st.st_rdev), minor(st.st_rdev));
641 if (access(s, F_OK) < 0)
642 return errno == ENOENT ? false : -errno;
643 }
644
645 return true;
646}
647
648static int mount_partition(
649 DissectedPartition *m,
650 const char *where,
651 const char *directory,
652 DissectImageFlags flags) {
653
654 const char *p, *options = NULL, *node, *fstype;
2eedfd2d 655 _cleanup_free_ char *chased = NULL;
8c1be37e 656 bool rw;
2eedfd2d 657 int r;
8c1be37e
LP
658
659 assert(m);
660 assert(where);
661
18b5886e
LP
662 node = m->decrypted_node ?: m->node;
663 fstype = m->decrypted_fstype ?: m->fstype;
664
665 if (!m->found || !node || !fstype)
8c1be37e
LP
666 return 0;
667
18b5886e
LP
668 /* Stacked encryption? Yuck */
669 if (streq_ptr(fstype, "crypto_LUKS"))
670 return -ELOOP;
671
672 rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY);
8c1be37e 673
2eedfd2d
LP
674 if (directory) {
675 r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased);
676 if (r < 0)
677 return r;
678
679 p = chased;
680 } else
8c1be37e
LP
681 p = where;
682
18b5886e
LP
683 /* If requested, turn on discard support. */
684 if (STR_IN_SET(fstype, "btrfs", "ext4", "vfat", "xfs") &&
685 ((flags & DISSECT_IMAGE_DISCARD) ||
686 ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node))))
687 options = "discard";
8c1be37e 688
18b5886e 689 return mount_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options);
8c1be37e
LP
690}
691
18b5886e 692int dissected_image_mount(DissectedImage *m, const char *where, DissectImageFlags flags) {
8c1be37e
LP
693 int r;
694
695 assert(m);
696 assert(where);
697
698 if (!m->partitions[PARTITION_ROOT].found)
699 return -ENXIO;
700
701 r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, flags);
702 if (r < 0)
703 return r;
704
705 r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", flags);
706 if (r < 0)
707 return r;
708
709 r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", flags);
710 if (r < 0)
711 return r;
712
713 if (m->partitions[PARTITION_ESP].found) {
2eedfd2d 714 const char *mp;
8c1be37e
LP
715
716 /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */
717
2eedfd2d
LP
718 FOREACH_STRING(mp, "/efi", "/boot") {
719 _cleanup_free_ char *p = NULL;
720
721 r = chase_symlinks(mp, where, CHASE_PREFIX_ROOT, &p);
8c1be37e 722 if (r < 0)
2eedfd2d
LP
723 continue;
724
725 r = dir_is_empty(p);
726 if (r > 0) {
727 r = mount_partition(m->partitions + PARTITION_ESP, where, mp, flags);
728 if (r < 0)
729 return r;
730 }
8c1be37e
LP
731 }
732 }
733
734 return 0;
735}
736
18b5886e
LP
737#ifdef HAVE_LIBCRYPTSETUP
738typedef struct DecryptedPartition {
739 struct crypt_device *device;
740 char *name;
741 bool relinquished;
742} DecryptedPartition;
743
744struct DecryptedImage {
745 DecryptedPartition *decrypted;
746 size_t n_decrypted;
747 size_t n_allocated;
748};
749#endif
750
751DecryptedImage* decrypted_image_unref(DecryptedImage* d) {
752#ifdef HAVE_LIBCRYPTSETUP
753 size_t i;
754 int r;
755
756 if (!d)
757 return NULL;
758
759 for (i = 0; i < d->n_decrypted; i++) {
760 DecryptedPartition *p = d->decrypted + i;
761
762 if (p->device && p->name && !p->relinquished) {
763 r = crypt_deactivate(p->device, p->name);
764 if (r < 0)
765 log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name);
766 }
767
768 if (p->device)
769 crypt_free(p->device);
770 free(p->name);
771 }
772
773 free(d);
774#endif
775 return NULL;
776}
777
778#ifdef HAVE_LIBCRYPTSETUP
4623e8e6
LP
779
780static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) {
781 _cleanup_free_ char *name = NULL, *node = NULL;
782 const char *base;
783
784 assert(original_node);
785 assert(suffix);
786 assert(ret_name);
787 assert(ret_node);
788
789 base = strrchr(original_node, '/');
790 if (!base)
791 return -EINVAL;
792 base++;
793 if (isempty(base))
794 return -EINVAL;
795
796 name = strjoin(base, suffix);
797 if (!name)
798 return -ENOMEM;
799 if (!filename_is_valid(name))
800 return -EINVAL;
801
802 node = strjoin(crypt_get_dir(), "/", name);
803 if (!node)
804 return -ENOMEM;
805
806 *ret_name = name;
807 *ret_node = node;
808
809 name = node = NULL;
810 return 0;
811}
812
18b5886e
LP
813static int decrypt_partition(
814 DissectedPartition *m,
815 const char *passphrase,
816 DissectImageFlags flags,
817 DecryptedImage *d) {
818
819 _cleanup_free_ char *node = NULL, *name = NULL;
820 struct crypt_device *cd;
18b5886e
LP
821 int r;
822
823 assert(m);
824 assert(d);
825
826 if (!m->found || !m->node || !m->fstype)
827 return 0;
828
829 if (!streq(m->fstype, "crypto_LUKS"))
830 return 0;
831
4623e8e6
LP
832 r = make_dm_name_and_node(m->node, "-decrypted", &name, &node);
833 if (r < 0)
834 return r;
18b5886e
LP
835
836 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
837 return -ENOMEM;
838
839 r = crypt_init(&cd, m->node);
840 if (r < 0)
715cbb81 841 return log_debug_errno(r, "Failed to initialize dm-crypt: %m");
18b5886e
LP
842
843 r = crypt_load(cd, CRYPT_LUKS1, NULL);
715cbb81
LP
844 if (r < 0) {
845 log_debug_errno(r, "Failed to load LUKS metadata: %m");
18b5886e 846 goto fail;
715cbb81 847 }
18b5886e
LP
848
849 r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase),
850 ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) |
851 ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0));
715cbb81
LP
852 if (r < 0)
853 log_debug_errno(r, "Failed to activate LUKS device: %m");
18b5886e
LP
854 if (r == -EPERM) {
855 r = -EKEYREJECTED;
856 goto fail;
857 }
858 if (r < 0)
859 goto fail;
860
861 d->decrypted[d->n_decrypted].name = name;
862 name = NULL;
863
864 d->decrypted[d->n_decrypted].device = cd;
865 d->n_decrypted++;
866
867 m->decrypted_node = node;
868 node = NULL;
869
870 return 0;
871
4623e8e6
LP
872fail:
873 crypt_free(cd);
874 return r;
875}
876
877static int verity_partition(
878 DissectedPartition *m,
879 DissectedPartition *v,
880 const void *root_hash,
881 size_t root_hash_size,
882 DissectImageFlags flags,
883 DecryptedImage *d) {
884
885 _cleanup_free_ char *node = NULL, *name = NULL;
886 struct crypt_device *cd;
887 int r;
888
889 assert(m);
890 assert(v);
891
892 if (!root_hash)
893 return 0;
894
895 if (!m->found || !m->node || !m->fstype)
896 return 0;
897 if (!v->found || !v->node || !v->fstype)
898 return 0;
899
900 if (!streq(v->fstype, "DM_verity_hash"))
901 return 0;
902
903 r = make_dm_name_and_node(m->node, "-verity", &name, &node);
904 if (r < 0)
905 return r;
906
907 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
908 return -ENOMEM;
909
910 r = crypt_init(&cd, v->node);
911 if (r < 0)
912 return r;
913
914 r = crypt_load(cd, CRYPT_VERITY, NULL);
915 if (r < 0)
916 goto fail;
917
918 r = crypt_set_data_device(cd, m->node);
919 if (r < 0)
920 goto fail;
921
922 r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY);
923 if (r < 0)
924 goto fail;
925
926 d->decrypted[d->n_decrypted].name = name;
927 name = NULL;
928
929 d->decrypted[d->n_decrypted].device = cd;
930 d->n_decrypted++;
931
932 m->decrypted_node = node;
933 node = NULL;
934
935 return 0;
936
18b5886e
LP
937fail:
938 crypt_free(cd);
939 return r;
940}
941#endif
942
943int dissected_image_decrypt(
944 DissectedImage *m,
945 const char *passphrase,
4623e8e6
LP
946 const void *root_hash,
947 size_t root_hash_size,
18b5886e
LP
948 DissectImageFlags flags,
949 DecryptedImage **ret) {
950
951 _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
952#ifdef HAVE_LIBCRYPTSETUP
953 unsigned i;
954 int r;
955#endif
956
957 assert(m);
4623e8e6 958 assert(root_hash || root_hash_size == 0);
18b5886e
LP
959
960 /* Returns:
961 *
962 * = 0 → There was nothing to decrypt
963 * > 0 → Decrypted successfully
d1c536f5 964 * -ENOKEY → There's something to decrypt but no key was supplied
18b5886e
LP
965 * -EKEYREJECTED → Passed key was not correct
966 */
967
4623e8e6
LP
968 if (root_hash && root_hash_size < sizeof(sd_id128_t))
969 return -EINVAL;
970
971 if (!m->encrypted && !m->verity) {
18b5886e
LP
972 *ret = NULL;
973 return 0;
974 }
975
976#ifdef HAVE_LIBCRYPTSETUP
4623e8e6 977 if (m->encrypted && !passphrase)
18b5886e
LP
978 return -ENOKEY;
979
980 d = new0(DecryptedImage, 1);
981 if (!d)
982 return -ENOMEM;
983
984 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
985 DissectedPartition *p = m->partitions + i;
4623e8e6 986 int k;
18b5886e
LP
987
988 if (!p->found)
989 continue;
990
991 r = decrypt_partition(p, passphrase, flags, d);
992 if (r < 0)
993 return r;
994
4623e8e6
LP
995 k = PARTITION_VERITY_OF(i);
996 if (k >= 0) {
997 r = verity_partition(p, m->partitions + k, root_hash, root_hash_size, flags, d);
998 if (r < 0)
999 return r;
1000 }
1001
18b5886e
LP
1002 if (!p->decrypted_fstype && p->decrypted_node) {
1003 r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype);
1004 if (r < 0)
1005 return r;
1006 }
1007 }
1008
1009 *ret = d;
1010 d = NULL;
1011
1012 return 1;
1013#else
1014 return -EOPNOTSUPP;
1015#endif
1016}
1017
1018int dissected_image_decrypt_interactively(
1019 DissectedImage *m,
1020 const char *passphrase,
4623e8e6
LP
1021 const void *root_hash,
1022 size_t root_hash_size,
18b5886e
LP
1023 DissectImageFlags flags,
1024 DecryptedImage **ret) {
1025
1026 _cleanup_strv_free_erase_ char **z = NULL;
1027 int n = 3, r;
1028
1029 if (passphrase)
1030 n--;
1031
1032 for (;;) {
4623e8e6 1033 r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, flags, ret);
18b5886e
LP
1034 if (r >= 0)
1035 return r;
1036 if (r == -EKEYREJECTED)
1037 log_error_errno(r, "Incorrect passphrase, try again!");
1038 else if (r != -ENOKEY) {
1039 log_error_errno(r, "Failed to decrypt image: %m");
1040 return r;
1041 }
1042
1043 if (--n < 0) {
1044 log_error("Too many retries.");
1045 return -EKEYREJECTED;
1046 }
1047
1048 z = strv_free(z);
1049
1050 r = ask_password_auto("Please enter image passphrase!", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z);
1051 if (r < 0)
1052 return log_error_errno(r, "Failed to query for passphrase: %m");
1053
1054 passphrase = z[0];
1055 }
1056}
1057
1058#ifdef HAVE_LIBCRYPTSETUP
1059static int deferred_remove(DecryptedPartition *p) {
1060
1061 struct dm_ioctl dm = {
1062 .version = {
1063 DM_VERSION_MAJOR,
1064 DM_VERSION_MINOR,
1065 DM_VERSION_PATCHLEVEL
1066 },
1067 .data_size = sizeof(dm),
1068 .flags = DM_DEFERRED_REMOVE,
1069 };
1070
1071 _cleanup_close_ int fd = -1;
1072
1073 assert(p);
1074
1075 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1076
1077 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
1078 if (fd < 0)
1079 return -errno;
1080
1081 strncpy(dm.name, p->name, sizeof(dm.name));
1082
1083 if (ioctl(fd, DM_DEV_REMOVE, &dm))
1084 return -errno;
1085
1086 return 0;
1087}
1088#endif
1089
1090int decrypted_image_relinquish(DecryptedImage *d) {
1091
1092#ifdef HAVE_LIBCRYPTSETUP
1093 size_t i;
1094 int r;
1095#endif
1096
1097 assert(d);
1098
1099 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1100 * that we don't clean it up ourselves either anymore */
1101
1102#ifdef HAVE_LIBCRYPTSETUP
1103 for (i = 0; i < d->n_decrypted; i++) {
1104 DecryptedPartition *p = d->decrypted + i;
1105
1106 if (p->relinquished)
1107 continue;
1108
1109 r = deferred_remove(p);
1110 if (r < 0)
1111 return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name);
1112
1113 p->relinquished = true;
1114 }
1115#endif
1116
1117 return 0;
1118}
1119
78ebe980
LP
1120int root_hash_load(const char *image, void **ret, size_t *ret_size) {
1121 _cleanup_free_ char *text = NULL;
1122 _cleanup_free_ void *k = NULL;
78ebe980
LP
1123 size_t l;
1124 int r;
1125
1126 assert(image);
1127 assert(ret);
1128 assert(ret_size);
1129
1130 if (is_device_path(image)) {
1131 /* If we are asked to load the root hash for a device node, exit early */
1132 *ret = NULL;
1133 *ret_size = 0;
1134 return 0;
1135 }
1136
41488e1f
LP
1137 r = getxattr_malloc(image, "user.verity.roothash", &text, true);
1138 if (r < 0) {
1139 char *fn, *e, *n;
78ebe980 1140
41488e1f
LP
1141 if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT))
1142 return r;
78ebe980 1143
41488e1f
LP
1144 fn = newa(char, strlen(image) + strlen(".roothash") + 1);
1145 n = stpcpy(fn, image);
1146 e = endswith(fn, ".raw");
1147 if (e)
1148 n = e;
1149
1150 strcpy(n, ".roothash");
1151
1152 r = read_one_line_file(fn, &text);
1153 if (r == -ENOENT) {
1154 *ret = NULL;
1155 *ret_size = 0;
1156 return 0;
1157 }
1158 if (r < 0)
1159 return r;
78ebe980 1160 }
78ebe980
LP
1161
1162 r = unhexmem(text, strlen(text), &k, &l);
1163 if (r < 0)
1164 return r;
1165 if (l < sizeof(sd_id128_t))
1166 return -EINVAL;
1167
1168 *ret = k;
1169 *ret_size = l;
1170
1171 k = NULL;
1172
1173 return 1;
1174}
1175
8c1be37e
LP
1176static const char *const partition_designator_table[] = {
1177 [PARTITION_ROOT] = "root",
1178 [PARTITION_ROOT_SECONDARY] = "root-secondary",
1179 [PARTITION_HOME] = "home",
1180 [PARTITION_SRV] = "srv",
1181 [PARTITION_ESP] = "esp",
1182 [PARTITION_SWAP] = "swap",
4623e8e6
LP
1183 [PARTITION_ROOT_VERITY] = "root-verity",
1184 [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity",
8c1be37e
LP
1185};
1186
1187DEFINE_STRING_TABLE_LOOKUP(partition_designator, int);