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