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