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