]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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> | |
8c1be37e | 9 | #include <sys/mount.h> |
3b925504 LP |
10 | #include <sys/prctl.h> |
11 | #include <sys/wait.h> | |
f5ea63a5 | 12 | #include <sysexits.h> |
8c1be37e | 13 | |
3c1f2cee | 14 | #include "sd-device.h" |
dccca82b LP |
15 | #include "sd-id128.h" |
16 | ||
8c1be37e | 17 | #include "architecture.h" |
18b5886e | 18 | #include "ask-password-api.h" |
8c1be37e | 19 | #include "blkid-util.h" |
18c528e9 | 20 | #include "blockdev-util.h" |
3b925504 | 21 | #include "copy.h" |
1e2f3230 | 22 | #include "cryptsetup-util.h" |
3b925504 | 23 | #include "def.h" |
553e15f2 | 24 | #include "device-nodes.h" |
8437c059 | 25 | #include "device-util.h" |
8c1be37e | 26 | #include "dissect-image.h" |
a709a315 | 27 | #include "dm-util.h" |
686d13b9 | 28 | #include "env-file.h" |
18b5886e | 29 | #include "fd-util.h" |
78ebe980 | 30 | #include "fileio.h" |
2eedfd2d | 31 | #include "fs-util.h" |
cf32c486 | 32 | #include "fsck-util.h" |
8c1be37e | 33 | #include "gpt.h" |
78ebe980 | 34 | #include "hexdecoct.h" |
3b925504 LP |
35 | #include "hostname-util.h" |
36 | #include "id128-util.h" | |
6aa05ebd | 37 | #include "mkdir.h" |
8c1be37e | 38 | #include "mount-util.h" |
e4de7287 | 39 | #include "mountpoint-util.h" |
6aa05ebd | 40 | #include "namespace-util.h" |
d8b4d14d | 41 | #include "nulstr-util.h" |
d58ad743 | 42 | #include "os-util.h" |
8c1be37e | 43 | #include "path-util.h" |
3b925504 LP |
44 | #include "process-util.h" |
45 | #include "raw-clone.h" | |
46 | #include "signal-util.h" | |
8c1be37e | 47 | #include "stat-util.h" |
18b5886e | 48 | #include "stdio-util.h" |
8c1be37e LP |
49 | #include "string-table.h" |
50 | #include "string-util.h" | |
2eedfd2d | 51 | #include "strv.h" |
e4de7287 | 52 | #include "tmpfile-util.h" |
a8040b6d | 53 | #include "udev-util.h" |
2d3a5a73 | 54 | #include "user-util.h" |
41488e1f | 55 | #include "xattr-util.h" |
8c1be37e | 56 | |
28e2641a FF |
57 | /* how many times to wait for the device nodes to appear */ |
58 | #define N_DEVICE_NODE_LIST_ATTEMPTS 10 | |
59 | ||
c34b75a1 | 60 | int probe_filesystem(const char *node, char **ret_fstype) { |
7cc84b2c | 61 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
5238e957 | 62 | * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an |
7cc84b2c ZJS |
63 | * different error otherwise. */ |
64 | ||
349cc4a5 | 65 | #if HAVE_BLKID |
8e766630 | 66 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
18b5886e LP |
67 | const char *fstype; |
68 | int r; | |
69 | ||
995fa2e5 | 70 | errno = 0; |
18b5886e LP |
71 | b = blkid_new_probe_from_filename(node); |
72 | if (!b) | |
66855de7 | 73 | return errno_or_else(ENOMEM); |
18b5886e LP |
74 | |
75 | blkid_probe_enable_superblocks(b, 1); | |
76 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
77 | ||
78 | errno = 0; | |
79 | r = blkid_do_safeprobe(b); | |
7cc84b2c ZJS |
80 | if (r == 1) { |
81 | log_debug("No type detected on partition %s", node); | |
18b5886e LP |
82 | goto not_found; |
83 | } | |
58dfbfbd LP |
84 | if (r == -2) |
85 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
86 | "Results ambiguous for partition %s", node); | |
b382db9f | 87 | if (r != 0) |
66855de7 | 88 | return errno_or_else(EIO); |
18b5886e LP |
89 | |
90 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
91 | ||
92 | if (fstype) { | |
93 | char *t; | |
94 | ||
95 | t = strdup(fstype); | |
96 | if (!t) | |
97 | return -ENOMEM; | |
98 | ||
99 | *ret_fstype = t; | |
100 | return 1; | |
101 | } | |
102 | ||
103 | not_found: | |
104 | *ret_fstype = NULL; | |
105 | return 0; | |
d1c536f5 ZJS |
106 | #else |
107 | return -EOPNOTSUPP; | |
a75e27eb | 108 | #endif |
d1c536f5 | 109 | } |
18b5886e | 110 | |
40c10d3f | 111 | #if HAVE_BLKID |
4ba86848 LP |
112 | static int enumerator_for_parent(sd_device *d, sd_device_enumerator **ret) { |
113 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
114 | int r; | |
aae22eb3 | 115 | |
f70e7f70 | 116 | assert(d); |
4ba86848 | 117 | assert(ret); |
f70e7f70 | 118 | |
4ba86848 LP |
119 | r = sd_device_enumerator_new(&e); |
120 | if (r < 0) | |
121 | return r; | |
3c1f2cee | 122 | |
4ba86848 LP |
123 | r = sd_device_enumerator_allow_uninitialized(e); |
124 | if (r < 0) | |
125 | return r; | |
126 | ||
127 | r = sd_device_enumerator_add_match_parent(e, d); | |
128 | if (r < 0) | |
129 | return r; | |
130 | ||
131 | *ret = TAKE_PTR(e); | |
132 | return 0; | |
cde942f6 JPRV |
133 | } |
134 | ||
4ba86848 LP |
135 | static int device_is_partition(sd_device *d, blkid_partition pp) { |
136 | blkid_loff_t bsize, bstart; | |
137 | uint64_t size, start; | |
138 | int partno, bpartno, r; | |
139 | const char *ss, *v; | |
aae22eb3 | 140 | |
f70e7f70 | 141 | assert(d); |
4ba86848 | 142 | assert(pp); |
f70e7f70 | 143 | |
4ba86848 LP |
144 | r = sd_device_get_subsystem(d, &ss); |
145 | if (r < 0) | |
146 | return r; | |
147 | if (!streq(ss, "block")) | |
aae22eb3 LP |
148 | return false; |
149 | ||
4ba86848 LP |
150 | r = sd_device_get_sysattr_value(d, "partition", &v); |
151 | if (r == -ENOENT) /* Not a partition device */ | |
152 | return false; | |
153 | if (r < 0) | |
154 | return r; | |
155 | r = safe_atoi(v, &partno); | |
156 | if (r < 0) | |
157 | return r; | |
ea887be0 | 158 | |
4ba86848 LP |
159 | errno = 0; |
160 | bpartno = blkid_partition_get_partno(pp); | |
161 | if (bpartno < 0) | |
162 | return errno_or_else(EIO); | |
ea887be0 | 163 | |
4ba86848 LP |
164 | if (partno != bpartno) |
165 | return false; | |
f70e7f70 | 166 | |
4ba86848 | 167 | r = sd_device_get_sysattr_value(d, "start", &v); |
ea887be0 ZJS |
168 | if (r < 0) |
169 | return r; | |
4ba86848 | 170 | r = safe_atou64(v, &start); |
ea887be0 ZJS |
171 | if (r < 0) |
172 | return r; | |
173 | ||
4ba86848 LP |
174 | errno = 0; |
175 | bstart = blkid_partition_get_start(pp); | |
176 | if (bstart < 0) | |
177 | return errno_or_else(EIO); | |
178 | ||
179 | if (start != (uint64_t) bstart) | |
180 | return false; | |
181 | ||
182 | r = sd_device_get_sysattr_value(d, "size", &v); | |
183 | if (r < 0) | |
184 | return r; | |
185 | r = safe_atou64(v, &size); | |
ea887be0 ZJS |
186 | if (r < 0) |
187 | return r; | |
188 | ||
4ba86848 LP |
189 | errno = 0; |
190 | bsize = blkid_partition_get_size(pp); | |
191 | if (bsize < 0) | |
192 | return errno_or_else(EIO); | |
193 | ||
194 | if (size != (uint64_t) bsize) | |
195 | return false; | |
196 | ||
197 | return true; | |
ea887be0 ZJS |
198 | } |
199 | ||
4ba86848 LP |
200 | static int find_partition( |
201 | sd_device *parent, | |
202 | blkid_partition pp, | |
203 | sd_device **ret) { | |
ea887be0 ZJS |
204 | |
205 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
206 | sd_device *q; | |
ea887be0 ZJS |
207 | int r; |
208 | ||
4ba86848 LP |
209 | assert(parent); |
210 | assert(pp); | |
211 | assert(ret); | |
f70e7f70 | 212 | |
4ba86848 | 213 | r = enumerator_for_parent(parent, &e); |
ea887be0 ZJS |
214 | if (r < 0) |
215 | return r; | |
216 | ||
ea887be0 | 217 | FOREACH_DEVICE(e, q) { |
4ba86848 LP |
218 | r = device_is_partition(q, pp); |
219 | if (r < 0) | |
220 | return r; | |
221 | if (r > 0) { | |
222 | *ret = sd_device_ref(q); | |
223 | return 0; | |
052eaf5c | 224 | } |
ea887be0 ZJS |
225 | } |
226 | ||
4ba86848 LP |
227 | return -ENXIO; |
228 | } | |
10c1b188 | 229 | |
4ba86848 LP |
230 | struct wait_data { |
231 | sd_device *parent_device; | |
232 | blkid_partition blkidp; | |
233 | sd_device *found; | |
234 | }; | |
ea887be0 | 235 | |
4ba86848 LP |
236 | static inline void wait_data_done(struct wait_data *d) { |
237 | sd_device_unref(d->found); | |
238 | } | |
ea887be0 | 239 | |
4ba86848 LP |
240 | static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
241 | const char *parent1_path, *parent2_path; | |
242 | struct wait_data *w = userdata; | |
243 | sd_device *pp; | |
244 | int r; | |
245 | ||
246 | assert(w); | |
247 | ||
248 | if (device_for_action(device, DEVICE_ACTION_REMOVE)) | |
249 | return 0; | |
250 | ||
251 | r = sd_device_get_parent(device, &pp); | |
252 | if (r < 0) | |
253 | return 0; /* Doesn't have a parent? No relevant to us */ | |
254 | ||
255 | r = sd_device_get_syspath(pp, &parent1_path); /* Check parent of device of this action */ | |
256 | if (r < 0) | |
257 | goto finish; | |
ea887be0 | 258 | |
4ba86848 LP |
259 | r = sd_device_get_syspath(w->parent_device, &parent2_path); /* Check parent of device we are looking for */ |
260 | if (r < 0) | |
261 | goto finish; | |
262 | ||
263 | if (!path_equal(parent1_path, parent2_path)) | |
264 | return 0; /* Has a different parent than what we need, not interesting to us */ | |
265 | ||
266 | r = device_is_partition(device, w->blkidp); | |
267 | if (r < 0) | |
268 | goto finish; | |
269 | if (r == 0) /* Not the one we need */ | |
270 | return 0; | |
271 | ||
272 | /* It's the one we need! Yay! */ | |
273 | assert(!w->found); | |
274 | w->found = sd_device_ref(device); | |
275 | r = 0; | |
276 | ||
277 | finish: | |
278 | return sd_event_exit(sd_device_monitor_get_event(monitor), r); | |
ea887be0 ZJS |
279 | } |
280 | ||
4ba86848 LP |
281 | static int wait_for_partition_device( |
282 | sd_device *parent, | |
283 | blkid_partition pp, | |
284 | usec_t deadline, | |
285 | sd_device **ret) { | |
286 | ||
287 | _cleanup_(sd_event_source_unrefp) sd_event_source *timeout_source = NULL; | |
288 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
289 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
ea887be0 ZJS |
290 | int r; |
291 | ||
4ba86848 LP |
292 | assert(parent); |
293 | assert(pp); | |
294 | assert(ret); | |
295 | ||
296 | r = find_partition(parent, pp, ret); | |
297 | if (r != -ENXIO) | |
298 | return r; | |
299 | ||
300 | r = sd_event_new(&event); | |
301 | if (r < 0) | |
302 | return r; | |
303 | ||
304 | r = sd_device_monitor_new(&monitor); | |
305 | if (r < 0) | |
306 | return r; | |
307 | ||
308 | r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "block", "partition"); | |
309 | if (r < 0) | |
310 | return r; | |
311 | ||
312 | r = sd_device_monitor_attach_event(monitor, event); | |
313 | if (r < 0) | |
314 | return r; | |
315 | ||
316 | _cleanup_(wait_data_done) struct wait_data w = { | |
317 | .parent_device = parent, | |
318 | .blkidp = pp, | |
319 | }; | |
f70e7f70 | 320 | |
4ba86848 LP |
321 | r = sd_device_monitor_start(monitor, device_monitor_handler, &w); |
322 | if (r < 0) | |
323 | return r; | |
a8040b6d | 324 | |
4ba86848 LP |
325 | /* Check again, the partition might have appeared in the meantime */ |
326 | r = find_partition(parent, pp, ret); | |
327 | if (r != -ENXIO) | |
328 | return r; | |
329 | ||
330 | if (deadline != USEC_INFINITY) { | |
331 | r = sd_event_add_time( | |
332 | event, &timeout_source, | |
333 | CLOCK_MONOTONIC, deadline, 0, | |
334 | NULL, INT_TO_PTR(-ETIMEDOUT)); | |
335 | if (r < 0) | |
ea887be0 ZJS |
336 | return r; |
337 | } | |
338 | ||
4ba86848 LP |
339 | r = sd_event_loop(event); |
340 | if (r < 0) | |
341 | return r; | |
342 | ||
343 | assert(w.found); | |
344 | *ret = TAKE_PTR(w.found); | |
345 | return 0; | |
ea887be0 ZJS |
346 | } |
347 | ||
0f7c9a3d LP |
348 | static void check_partition_flags( |
349 | const char *node, | |
350 | unsigned long long pflags, | |
351 | unsigned long long supported) { | |
352 | ||
353 | assert(node); | |
354 | ||
355 | /* Mask away all flags supported by this partition's type and the three flags the UEFI spec defines generically */ | |
356 | pflags &= ~(supported | GPT_FLAG_REQUIRED_PARTITION | GPT_FLAG_NO_BLOCK_IO_PROTOCOL | GPT_FLAG_LEGACY_BIOS_BOOTABLE); | |
357 | ||
358 | if (pflags == 0) | |
359 | return; | |
360 | ||
361 | /* If there are other bits set, then log about it, to make things discoverable */ | |
362 | for (unsigned i = 0; i < sizeof(pflags) * 8; i++) { | |
363 | unsigned long long bit = 1ULL << i; | |
364 | if (!FLAGS_SET(pflags, bit)) | |
365 | continue; | |
366 | ||
367 | log_debug("Unexpected partition flag %llu set on %s!", bit, node); | |
368 | } | |
369 | } | |
370 | ||
40c10d3f | 371 | #endif |
aae22eb3 | 372 | |
4ba86848 LP |
373 | #define DEVICE_TIMEOUT_USEC (45 * USEC_PER_SEC) |
374 | ||
4526113f LP |
375 | int dissect_image( |
376 | int fd, | |
89e62e0b | 377 | const VeritySettings *verity, |
18d73705 | 378 | const MountOptions *mount_options, |
4526113f LP |
379 | DissectImageFlags flags, |
380 | DissectedImage **ret) { | |
8c1be37e | 381 | |
349cc4a5 | 382 | #if HAVE_BLKID |
aee36b4e LP |
383 | sd_id128_t root_uuid = SD_ID128_NULL, root_verity_uuid = SD_ID128_NULL, |
384 | usr_uuid = SD_ID128_NULL, usr_verity_uuid = SD_ID128_NULL; | |
8c1be37e | 385 | bool is_gpt, is_mbr, generic_rw, multiple_generic = false; |
3c1f2cee | 386 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
8c1be37e | 387 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
8e766630 | 388 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
8c1be37e | 389 | _cleanup_free_ char *generic_node = NULL; |
be30ad41 | 390 | sd_id128_t generic_uuid = SD_ID128_NULL; |
9b6deb03 | 391 | const char *pttype = NULL; |
8c1be37e | 392 | blkid_partlist pl; |
4ba86848 | 393 | int r, generic_nr, n_partitions; |
8c1be37e | 394 | struct stat st; |
4ba86848 | 395 | usec_t deadline; |
8c1be37e LP |
396 | |
397 | assert(fd >= 0); | |
398 | assert(ret); | |
89e62e0b | 399 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
e7cbe5cb | 400 | assert(!((flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_NO_PARTITION_TABLE))); |
8c1be37e LP |
401 | |
402 | /* Probes a disk image, and returns information about what it found in *ret. | |
403 | * | |
4623e8e6 LP |
404 | * Returns -ENOPKG if no suitable partition table or file system could be found. |
405 | * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */ | |
406 | ||
89e62e0b | 407 | if (verity && verity->root_hash) { |
aee36b4e LP |
408 | sd_id128_t fsuuid, vuuid; |
409 | ||
410 | /* If a root hash is supplied, then we use the root partition that has a UUID that match the | |
411 | * first 128bit of the root hash. And we use the verity partition that has a UUID that match | |
412 | * the final 128bit. */ | |
4623e8e6 | 413 | |
89e62e0b | 414 | if (verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
415 | return -EINVAL; |
416 | ||
aee36b4e LP |
417 | memcpy(&fsuuid, verity->root_hash, sizeof(sd_id128_t)); |
418 | memcpy(&vuuid, (const uint8_t*) verity->root_hash + verity->root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t)); | |
4623e8e6 | 419 | |
aee36b4e | 420 | if (sd_id128_is_null(fsuuid)) |
4623e8e6 | 421 | return -EINVAL; |
aee36b4e | 422 | if (sd_id128_is_null(vuuid)) |
4623e8e6 | 423 | return -EINVAL; |
aee36b4e LP |
424 | |
425 | /* If the verity data declares it's for the /usr partition, then search for that, in all | |
426 | * other cases assume it's for the root partition. */ | |
427 | if (verity->designator == PARTITION_USR) { | |
428 | usr_uuid = fsuuid; | |
429 | usr_verity_uuid = vuuid; | |
430 | } else { | |
431 | root_uuid = fsuuid; | |
432 | root_verity_uuid = vuuid; | |
433 | } | |
4623e8e6 | 434 | } |
8c1be37e LP |
435 | |
436 | if (fstat(fd, &st) < 0) | |
437 | return -errno; | |
438 | ||
439 | if (!S_ISBLK(st.st_mode)) | |
440 | return -ENOTBLK; | |
441 | ||
6c544d14 LP |
442 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
443 | if (r < 0) | |
444 | return r; | |
445 | ||
446 | if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) { | |
447 | _cleanup_(sd_device_unrefp) sd_device *initialized = NULL; | |
448 | ||
449 | /* If udev support is enabled, then let's wait for the device to be initialized before we doing anything. */ | |
450 | ||
4ba86848 | 451 | r = device_wait_for_initialization(d, "block", DEVICE_TIMEOUT_USEC, &initialized); |
6c544d14 LP |
452 | if (r < 0) |
453 | return r; | |
454 | ||
455 | sd_device_unref(d); | |
456 | d = TAKE_PTR(initialized); | |
457 | } | |
458 | ||
8c1be37e LP |
459 | b = blkid_new_probe(); |
460 | if (!b) | |
461 | return -ENOMEM; | |
462 | ||
463 | errno = 0; | |
464 | r = blkid_probe_set_device(b, fd, 0, 0); | |
b382db9f | 465 | if (r != 0) |
66855de7 | 466 | return errno_or_else(ENOMEM); |
8c1be37e | 467 | |
9b6deb03 LP |
468 | if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { |
469 | /* Look for file system superblocks, unless we only shall look for GPT partition tables */ | |
470 | blkid_probe_enable_superblocks(b, 1); | |
471 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE); | |
472 | } | |
473 | ||
8c1be37e LP |
474 | blkid_probe_enable_partitions(b, 1); |
475 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
476 | ||
477 | errno = 0; | |
478 | r = blkid_do_safeprobe(b); | |
59ba6d0c LP |
479 | if (IN_SET(r, -2, 1)) |
480 | return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); | |
b382db9f | 481 | if (r != 0) |
66855de7 | 482 | return errno_or_else(EIO); |
8c1be37e LP |
483 | |
484 | m = new0(DissectedImage, 1); | |
485 | if (!m) | |
486 | return -ENOMEM; | |
487 | ||
e7cbe5cb LB |
488 | if ((!(flags & DISSECT_IMAGE_GPT_ONLY) && |
489 | (flags & DISSECT_IMAGE_REQUIRE_ROOT)) || | |
490 | (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)) { | |
9b6deb03 | 491 | const char *usage = NULL; |
8c1be37e | 492 | |
aee36b4e LP |
493 | /* If flags permit this, also allow using non-partitioned single-filesystem images */ |
494 | ||
9b6deb03 LP |
495 | (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL); |
496 | if (STRPTR_IN_SET(usage, "filesystem", "crypto")) { | |
6c544d14 | 497 | const char *fstype = NULL, *options = NULL, *devname = NULL; |
18d73705 | 498 | _cleanup_free_ char *t = NULL, *n = NULL, *o = NULL; |
8c1be37e | 499 | |
9b6deb03 LP |
500 | /* OK, we have found a file system, that's our root partition then. */ |
501 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
8c1be37e | 502 | |
9b6deb03 LP |
503 | if (fstype) { |
504 | t = strdup(fstype); | |
505 | if (!t) | |
506 | return -ENOMEM; | |
507 | } | |
508 | ||
6c544d14 | 509 | r = sd_device_get_devname(d, &devname); |
54b22b26 LP |
510 | if (r < 0) |
511 | return r; | |
8c1be37e | 512 | |
6c544d14 LP |
513 | n = strdup(devname); |
514 | if (!n) | |
515 | return -ENOMEM; | |
516 | ||
e7cbe5cb | 517 | m->single_file_system = true; |
aee36b4e | 518 | m->verity = verity && verity->root_hash && verity->data_path && (verity->designator < 0 || verity->designator == PARTITION_ROOT); |
89e62e0b | 519 | m->can_verity = verity && verity->data_path; |
e7cbe5cb | 520 | |
f5215bc8 | 521 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
522 | if (options) { |
523 | o = strdup(options); | |
524 | if (!o) | |
525 | return -ENOMEM; | |
526 | } | |
527 | ||
9b6deb03 LP |
528 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
529 | .found = true, | |
e7cbe5cb | 530 | .rw = !m->verity, |
9b6deb03 LP |
531 | .partno = -1, |
532 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
533 | .fstype = TAKE_PTR(t), |
534 | .node = TAKE_PTR(n), | |
18d73705 | 535 | .mount_options = TAKE_PTR(o), |
9b6deb03 | 536 | }; |
8c1be37e | 537 | |
4db1879a | 538 | m->encrypted = streq_ptr(fstype, "crypto_LUKS"); |
18b5886e | 539 | |
1cc6c93a | 540 | *ret = TAKE_PTR(m); |
9b6deb03 LP |
541 | return 0; |
542 | } | |
8c1be37e LP |
543 | } |
544 | ||
545 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
546 | if (!pttype) | |
547 | return -ENOPKG; | |
548 | ||
549 | is_gpt = streq_ptr(pttype, "gpt"); | |
550 | is_mbr = streq_ptr(pttype, "dos"); | |
551 | ||
9b6deb03 | 552 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
553 | return -ENOPKG; |
554 | ||
4ba86848 LP |
555 | /* Safety check: refuse block devices that carry a partition table but for which the kernel doesn't |
556 | * do partition scanning. */ | |
557 | r = blockdev_partscan_enabled(fd); | |
558 | if (r < 0) | |
559 | return r; | |
560 | if (r == 0) | |
561 | return -EPROTONOSUPPORT; | |
562 | ||
8c1be37e LP |
563 | errno = 0; |
564 | pl = blkid_probe_get_partitions(b); | |
b382db9f | 565 | if (!pl) |
66855de7 | 566 | return errno_or_else(ENOMEM); |
8c1be37e | 567 | |
4ba86848 LP |
568 | errno = 0; |
569 | n_partitions = blkid_partlist_numof_partitions(pl); | |
570 | if (n_partitions < 0) | |
571 | return errno_or_else(EIO); | |
8c1be37e | 572 | |
4ba86848 LP |
573 | deadline = usec_add(now(CLOCK_MONOTONIC), DEVICE_TIMEOUT_USEC); |
574 | for (int i = 0; i < n_partitions; i++) { | |
575 | _cleanup_(sd_device_unrefp) sd_device *q = NULL; | |
9b6deb03 | 576 | unsigned long long pflags; |
8c1be37e | 577 | blkid_partition pp; |
cde942f6 | 578 | const char *node; |
8c1be37e LP |
579 | int nr; |
580 | ||
4ba86848 LP |
581 | errno = 0; |
582 | pp = blkid_partlist_get_partition(pl, i); | |
583 | if (!pp) | |
584 | return errno_or_else(EIO); | |
aae22eb3 | 585 | |
4ba86848 LP |
586 | r = wait_for_partition_device(d, pp, deadline, &q); |
587 | if (r < 0) | |
588 | return r; | |
7be1420f | 589 | |
3c1f2cee YW |
590 | r = sd_device_get_devname(q, &node); |
591 | if (r < 0) | |
4ba86848 | 592 | return r; |
8c1be37e | 593 | |
9b6deb03 | 594 | pflags = blkid_partition_get_flags(pp); |
8c1be37e | 595 | |
4ba86848 | 596 | errno = 0; |
8c1be37e LP |
597 | nr = blkid_partition_get_partno(pp); |
598 | if (nr < 0) | |
4ba86848 | 599 | return errno_or_else(EIO); |
8c1be37e LP |
600 | |
601 | if (is_gpt) { | |
569a0e42 LP |
602 | PartitionDesignator designator = _PARTITION_DESIGNATOR_INVALID; |
603 | int architecture = _ARCHITECTURE_INVALID; | |
4623e8e6 LP |
604 | const char *stype, *sid, *fstype = NULL; |
605 | sd_id128_t type_id, id; | |
8c1be37e LP |
606 | bool rw = true; |
607 | ||
4623e8e6 LP |
608 | sid = blkid_partition_get_uuid(pp); |
609 | if (!sid) | |
610 | continue; | |
611 | if (sd_id128_from_string(sid, &id) < 0) | |
612 | continue; | |
613 | ||
8c1be37e LP |
614 | stype = blkid_partition_get_type_string(pp); |
615 | if (!stype) | |
616 | continue; | |
8c1be37e LP |
617 | if (sd_id128_from_string(stype, &type_id) < 0) |
618 | continue; | |
619 | ||
620 | if (sd_id128_equal(type_id, GPT_HOME)) { | |
a48dd347 | 621 | |
0f7c9a3d LP |
622 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
623 | ||
a48dd347 LP |
624 | if (pflags & GPT_FLAG_NO_AUTO) |
625 | continue; | |
626 | ||
8c1be37e | 627 | designator = PARTITION_HOME; |
9b6deb03 | 628 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
aee36b4e | 629 | |
8c1be37e | 630 | } else if (sd_id128_equal(type_id, GPT_SRV)) { |
a48dd347 | 631 | |
0f7c9a3d LP |
632 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
633 | ||
a48dd347 LP |
634 | if (pflags & GPT_FLAG_NO_AUTO) |
635 | continue; | |
636 | ||
8c1be37e | 637 | designator = PARTITION_SRV; |
9b6deb03 | 638 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
aee36b4e | 639 | |
8c1be37e | 640 | } else if (sd_id128_equal(type_id, GPT_ESP)) { |
a48dd347 | 641 | |
aee36b4e LP |
642 | /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is |
643 | * not defined there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as | |
644 | * recommended by the UEFI spec (See "12.3.3 Number and Location of System | |
645 | * Partitions"). */ | |
a48dd347 LP |
646 | |
647 | if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL) | |
648 | continue; | |
649 | ||
8c1be37e LP |
650 | designator = PARTITION_ESP; |
651 | fstype = "vfat"; | |
a8c47660 LP |
652 | |
653 | } else if (sd_id128_equal(type_id, GPT_XBOOTLDR)) { | |
654 | ||
0f7c9a3d LP |
655 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
656 | ||
a8c47660 LP |
657 | if (pflags & GPT_FLAG_NO_AUTO) |
658 | continue; | |
659 | ||
660 | designator = PARTITION_XBOOTLDR; | |
661 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
8c1be37e LP |
662 | } |
663 | #ifdef GPT_ROOT_NATIVE | |
664 | else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) { | |
4623e8e6 | 665 | |
0f7c9a3d LP |
666 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
667 | ||
a48dd347 LP |
668 | if (pflags & GPT_FLAG_NO_AUTO) |
669 | continue; | |
670 | ||
4623e8e6 LP |
671 | /* If a root ID is specified, ignore everything but the root id */ |
672 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
673 | continue; | |
674 | ||
8c1be37e LP |
675 | designator = PARTITION_ROOT; |
676 | architecture = native_architecture(); | |
9b6deb03 | 677 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
aee36b4e | 678 | |
4f8b86e3 | 679 | } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) { |
4623e8e6 | 680 | |
0f7c9a3d LP |
681 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
682 | ||
a48dd347 LP |
683 | if (pflags & GPT_FLAG_NO_AUTO) |
684 | continue; | |
685 | ||
4623e8e6 LP |
686 | m->can_verity = true; |
687 | ||
688 | /* Ignore verity unless a root hash is specified */ | |
aee36b4e | 689 | if (sd_id128_is_null(root_verity_uuid) || !sd_id128_equal(root_verity_uuid, id)) |
4623e8e6 LP |
690 | continue; |
691 | ||
692 | designator = PARTITION_ROOT_VERITY; | |
693 | fstype = "DM_verity_hash"; | |
694 | architecture = native_architecture(); | |
695 | rw = false; | |
696 | } | |
697 | #endif | |
8c1be37e LP |
698 | #ifdef GPT_ROOT_SECONDARY |
699 | else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) { | |
4623e8e6 | 700 | |
0f7c9a3d LP |
701 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
702 | ||
a48dd347 LP |
703 | if (pflags & GPT_FLAG_NO_AUTO) |
704 | continue; | |
705 | ||
4623e8e6 LP |
706 | /* If a root ID is specified, ignore everything but the root id */ |
707 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
708 | continue; | |
709 | ||
8c1be37e LP |
710 | designator = PARTITION_ROOT_SECONDARY; |
711 | architecture = SECONDARY_ARCHITECTURE; | |
9b6deb03 | 712 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
aee36b4e | 713 | |
4f8b86e3 | 714 | } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) { |
a48dd347 | 715 | |
0f7c9a3d LP |
716 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
717 | ||
a48dd347 LP |
718 | if (pflags & GPT_FLAG_NO_AUTO) |
719 | continue; | |
720 | ||
4623e8e6 LP |
721 | m->can_verity = true; |
722 | ||
723 | /* Ignore verity unless root has is specified */ | |
aee36b4e | 724 | if (sd_id128_is_null(root_verity_uuid) || !sd_id128_equal(root_verity_uuid, id)) |
4623e8e6 LP |
725 | continue; |
726 | ||
727 | designator = PARTITION_ROOT_SECONDARY_VERITY; | |
728 | fstype = "DM_verity_hash"; | |
729 | architecture = SECONDARY_ARCHITECTURE; | |
730 | rw = false; | |
731 | } | |
aee36b4e LP |
732 | #endif |
733 | #ifdef GPT_USR_NATIVE | |
734 | else if (sd_id128_equal(type_id, GPT_USR_NATIVE)) { | |
735 | ||
736 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
737 | ||
738 | if (pflags & GPT_FLAG_NO_AUTO) | |
739 | continue; | |
740 | ||
741 | /* If a usr ID is specified, ignore everything but the usr id */ | |
742 | if (!sd_id128_is_null(usr_uuid) && !sd_id128_equal(usr_uuid, id)) | |
743 | continue; | |
744 | ||
745 | designator = PARTITION_USR; | |
746 | architecture = native_architecture(); | |
747 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
748 | ||
749 | } else if (sd_id128_equal(type_id, GPT_USR_NATIVE_VERITY)) { | |
750 | ||
751 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
752 | ||
753 | if (pflags & GPT_FLAG_NO_AUTO) | |
754 | continue; | |
755 | ||
756 | m->can_verity = true; | |
757 | ||
758 | /* Ignore verity unless a usr hash is specified */ | |
759 | if (sd_id128_is_null(usr_verity_uuid) || !sd_id128_equal(usr_verity_uuid, id)) | |
760 | continue; | |
761 | ||
762 | designator = PARTITION_USR_VERITY; | |
763 | fstype = "DM_verity_hash"; | |
764 | architecture = native_architecture(); | |
765 | rw = false; | |
766 | } | |
767 | #endif | |
768 | #ifdef GPT_USR_SECONDARY | |
769 | else if (sd_id128_equal(type_id, GPT_USR_SECONDARY)) { | |
770 | ||
771 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
772 | ||
773 | if (pflags & GPT_FLAG_NO_AUTO) | |
774 | continue; | |
775 | ||
776 | /* If a usr ID is specified, ignore everything but the usr id */ | |
777 | if (!sd_id128_is_null(usr_uuid) && !sd_id128_equal(usr_uuid, id)) | |
778 | continue; | |
779 | ||
780 | designator = PARTITION_USR_SECONDARY; | |
781 | architecture = SECONDARY_ARCHITECTURE; | |
782 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
783 | ||
784 | } else if (sd_id128_equal(type_id, GPT_USR_SECONDARY_VERITY)) { | |
785 | ||
786 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
787 | ||
788 | if (pflags & GPT_FLAG_NO_AUTO) | |
789 | continue; | |
790 | ||
791 | m->can_verity = true; | |
792 | ||
793 | /* Ignore verity unless usr has is specified */ | |
794 | if (sd_id128_is_null(usr_verity_uuid) || !sd_id128_equal(usr_verity_uuid, id)) | |
795 | continue; | |
796 | ||
797 | designator = PARTITION_USR_SECONDARY_VERITY; | |
798 | fstype = "DM_verity_hash"; | |
799 | architecture = SECONDARY_ARCHITECTURE; | |
800 | rw = false; | |
801 | } | |
8c1be37e LP |
802 | #endif |
803 | else if (sd_id128_equal(type_id, GPT_SWAP)) { | |
a48dd347 | 804 | |
0f7c9a3d LP |
805 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO); |
806 | ||
a48dd347 LP |
807 | if (pflags & GPT_FLAG_NO_AUTO) |
808 | continue; | |
809 | ||
8c1be37e LP |
810 | designator = PARTITION_SWAP; |
811 | fstype = "swap"; | |
aee36b4e | 812 | |
8c1be37e LP |
813 | } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) { |
814 | ||
0f7c9a3d LP |
815 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
816 | ||
a48dd347 LP |
817 | if (pflags & GPT_FLAG_NO_AUTO) |
818 | continue; | |
819 | ||
8c1be37e LP |
820 | if (generic_node) |
821 | multiple_generic = true; | |
822 | else { | |
823 | generic_nr = nr; | |
9b6deb03 | 824 | generic_rw = !(pflags & GPT_FLAG_READ_ONLY); |
be30ad41 | 825 | generic_uuid = id; |
8c1be37e LP |
826 | generic_node = strdup(node); |
827 | if (!generic_node) | |
828 | return -ENOMEM; | |
829 | } | |
d4dffb85 LP |
830 | |
831 | } else if (sd_id128_equal(type_id, GPT_TMP)) { | |
832 | ||
0f7c9a3d LP |
833 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
834 | ||
d4dffb85 LP |
835 | if (pflags & GPT_FLAG_NO_AUTO) |
836 | continue; | |
837 | ||
838 | designator = PARTITION_TMP; | |
839 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
840 | ||
841 | } else if (sd_id128_equal(type_id, GPT_VAR)) { | |
842 | ||
0f7c9a3d LP |
843 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
844 | ||
d4dffb85 LP |
845 | if (pflags & GPT_FLAG_NO_AUTO) |
846 | continue; | |
847 | ||
848 | if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) { | |
849 | sd_id128_t var_uuid; | |
850 | ||
851 | /* For /var we insist that the uuid of the partition matches the | |
852 | * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine | |
853 | * ID. Why? Unlike the other partitions /var is inherently | |
854 | * installation specific, hence we need to be careful not to mount it | |
855 | * in the wrong installation. By hashing the partition UUID from | |
856 | * /etc/machine-id we can securely bind the partition to the | |
857 | * installation. */ | |
858 | ||
859 | r = sd_id128_get_machine_app_specific(GPT_VAR, &var_uuid); | |
860 | if (r < 0) | |
861 | return r; | |
862 | ||
863 | if (!sd_id128_equal(var_uuid, id)) { | |
864 | log_debug("Found a /var/ partition, but its UUID didn't match our expectations, ignoring."); | |
865 | continue; | |
866 | } | |
867 | } | |
868 | ||
869 | designator = PARTITION_VAR; | |
870 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
8c1be37e LP |
871 | } |
872 | ||
873 | if (designator != _PARTITION_DESIGNATOR_INVALID) { | |
18d73705 LB |
874 | _cleanup_free_ char *t = NULL, *n = NULL, *o = NULL; |
875 | const char *options = NULL; | |
8c1be37e LP |
876 | |
877 | /* First one wins */ | |
878 | if (m->partitions[designator].found) | |
879 | continue; | |
880 | ||
881 | if (fstype) { | |
882 | t = strdup(fstype); | |
883 | if (!t) | |
884 | return -ENOMEM; | |
885 | } | |
886 | ||
887 | n = strdup(node); | |
888 | if (!n) | |
889 | return -ENOMEM; | |
890 | ||
f5215bc8 | 891 | options = mount_options_from_designator(mount_options, designator); |
18d73705 LB |
892 | if (options) { |
893 | o = strdup(options); | |
894 | if (!o) | |
895 | return -ENOMEM; | |
896 | } | |
897 | ||
8c1be37e LP |
898 | m->partitions[designator] = (DissectedPartition) { |
899 | .found = true, | |
900 | .partno = nr, | |
901 | .rw = rw, | |
902 | .architecture = architecture, | |
1cc6c93a YW |
903 | .node = TAKE_PTR(n), |
904 | .fstype = TAKE_PTR(t), | |
be30ad41 | 905 | .uuid = id, |
18d73705 | 906 | .mount_options = TAKE_PTR(o), |
8c1be37e | 907 | }; |
8c1be37e LP |
908 | } |
909 | ||
910 | } else if (is_mbr) { | |
911 | ||
a8c47660 | 912 | switch (blkid_partition_get_type(pp)) { |
8c1be37e | 913 | |
a8c47660 LP |
914 | case 0x83: /* Linux partition */ |
915 | ||
916 | if (pflags != 0x80) /* Bootable flag */ | |
917 | continue; | |
8c1be37e | 918 | |
a8c47660 LP |
919 | if (generic_node) |
920 | multiple_generic = true; | |
921 | else { | |
922 | generic_nr = nr; | |
923 | generic_rw = true; | |
924 | generic_node = strdup(node); | |
925 | if (!generic_node) | |
926 | return -ENOMEM; | |
927 | } | |
928 | ||
929 | break; | |
930 | ||
931 | case 0xEA: { /* Boot Loader Spec extended $BOOT partition */ | |
18d73705 | 932 | _cleanup_free_ char *n = NULL, *o = NULL; |
a8c47660 | 933 | sd_id128_t id = SD_ID128_NULL; |
18d73705 | 934 | const char *sid, *options = NULL; |
a8c47660 LP |
935 | |
936 | /* First one wins */ | |
937 | if (m->partitions[PARTITION_XBOOTLDR].found) | |
938 | continue; | |
939 | ||
940 | sid = blkid_partition_get_uuid(pp); | |
941 | if (sid) | |
942 | (void) sd_id128_from_string(sid, &id); | |
943 | ||
944 | n = strdup(node); | |
945 | if (!n) | |
8c1be37e | 946 | return -ENOMEM; |
a8c47660 | 947 | |
f5215bc8 | 948 | options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR); |
18d73705 LB |
949 | if (options) { |
950 | o = strdup(options); | |
951 | if (!o) | |
952 | return -ENOMEM; | |
953 | } | |
954 | ||
a8c47660 LP |
955 | m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) { |
956 | .found = true, | |
957 | .partno = nr, | |
958 | .rw = true, | |
959 | .architecture = _ARCHITECTURE_INVALID, | |
960 | .node = TAKE_PTR(n), | |
961 | .uuid = id, | |
18d73705 | 962 | .mount_options = TAKE_PTR(o), |
a8c47660 LP |
963 | }; |
964 | ||
965 | break; | |
966 | }} | |
8c1be37e LP |
967 | } |
968 | } | |
969 | ||
74cb2db9 LP |
970 | if (m->partitions[PARTITION_ROOT].found) { |
971 | /* If we found the primary arch, then invalidate the secondary arch to avoid any ambiguities, | |
972 | * since we never want to mount the secondary arch in this case. */ | |
973 | m->partitions[PARTITION_ROOT_SECONDARY].found = false; | |
974 | m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false; | |
aee36b4e LP |
975 | m->partitions[PARTITION_USR_SECONDARY].found = false; |
976 | m->partitions[PARTITION_USR_SECONDARY_VERITY].found = false; | |
74cb2db9 | 977 | } else { |
8c1be37e LP |
978 | /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not |
979 | * either, then check if there's a single generic one, and use that. */ | |
980 | ||
4623e8e6 | 981 | if (m->partitions[PARTITION_ROOT_VERITY].found) |
e0f9e7bd | 982 | return -EADDRNOTAVAIL; |
4623e8e6 | 983 | |
aee36b4e LP |
984 | /* We didn't find a primary architecture root, but we found a primary architecture /usr? Refuse that for now. */ |
985 | if (m->partitions[PARTITION_USR].found || m->partitions[PARTITION_USR_VERITY].found) | |
986 | return -EADDRNOTAVAIL; | |
987 | ||
8c1be37e | 988 | if (m->partitions[PARTITION_ROOT_SECONDARY].found) { |
aee36b4e | 989 | /* Upgrade secondary arch to first */ |
8c1be37e LP |
990 | m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY]; |
991 | zero(m->partitions[PARTITION_ROOT_SECONDARY]); | |
4623e8e6 LP |
992 | m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY]; |
993 | zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); | |
994 | ||
aee36b4e LP |
995 | m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY]; |
996 | zero(m->partitions[PARTITION_USR_SECONDARY]); | |
997 | m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY]; | |
998 | zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]); | |
999 | ||
e0f9e7bd | 1000 | } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) { |
18d73705 LB |
1001 | _cleanup_free_ char *o = NULL; |
1002 | const char *options = NULL; | |
e0f9e7bd | 1003 | |
2aed63f4 ZJS |
1004 | /* If the root hash was set, then we won't fall back to a generic node, because the |
1005 | * root hash decides. */ | |
89e62e0b | 1006 | if (verity && verity->root_hash) |
e0f9e7bd | 1007 | return -EADDRNOTAVAIL; |
8c1be37e | 1008 | |
e0f9e7bd LP |
1009 | /* If we didn't find a generic node, then we can't fix this up either */ |
1010 | if (!generic_node) | |
1011 | return -ENXIO; | |
1012 | ||
1013 | /* If we didn't find a properly marked root partition, but we did find a single suitable | |
1014 | * generic Linux partition, then use this as root partition, if the caller asked for it. */ | |
8c1be37e LP |
1015 | if (multiple_generic) |
1016 | return -ENOTUNIQ; | |
1017 | ||
f5215bc8 | 1018 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
1019 | if (options) { |
1020 | o = strdup(options); | |
1021 | if (!o) | |
1022 | return -ENOMEM; | |
1023 | } | |
1024 | ||
8c1be37e LP |
1025 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
1026 | .found = true, | |
1027 | .rw = generic_rw, | |
1028 | .partno = generic_nr, | |
1029 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a | 1030 | .node = TAKE_PTR(generic_node), |
be30ad41 | 1031 | .uuid = generic_uuid, |
18d73705 | 1032 | .mount_options = TAKE_PTR(o), |
8c1be37e | 1033 | }; |
e0f9e7bd | 1034 | } |
8c1be37e LP |
1035 | } |
1036 | ||
aee36b4e LP |
1037 | /* Refuse if we found a verity partition for /usr but no matching file system partition */ |
1038 | if (!m->partitions[PARTITION_USR].found && m->partitions[PARTITION_USR_VERITY].found) | |
1039 | return -EADDRNOTAVAIL; | |
1040 | ||
1041 | /* Combinations of verity /usr with verity-less root is OK, but the reverse is not */ | |
c848516f | 1042 | if (m->partitions[PARTITION_ROOT_VERITY].found && m->partitions[PARTITION_USR].found && !m->partitions[PARTITION_USR_VERITY].found) |
aee36b4e LP |
1043 | return -EADDRNOTAVAIL; |
1044 | ||
89e62e0b | 1045 | if (verity && verity->root_hash) { |
aee36b4e LP |
1046 | if (verity->designator < 0 || verity->designator == PARTITION_ROOT) { |
1047 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) | |
1048 | return -EADDRNOTAVAIL; | |
1049 | ||
1050 | /* If we found a verity setup, then the root partition is necessarily read-only. */ | |
1051 | m->partitions[PARTITION_ROOT].rw = false; | |
1052 | m->verity = true; | |
1053 | } | |
4623e8e6 | 1054 | |
aee36b4e LP |
1055 | if (verity->designator == PARTITION_USR) { |
1056 | if (!m->partitions[PARTITION_USR_VERITY].found || !m->partitions[PARTITION_USR].found) | |
1057 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1058 | |
aee36b4e LP |
1059 | m->partitions[PARTITION_USR].rw = false; |
1060 | m->verity = true; | |
1061 | } | |
4623e8e6 LP |
1062 | } |
1063 | ||
18b5886e LP |
1064 | blkid_free_probe(b); |
1065 | b = NULL; | |
1066 | ||
8c1be37e | 1067 | /* Fill in file system types if we don't know them yet. */ |
569a0e42 | 1068 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
18b5886e | 1069 | DissectedPartition *p = m->partitions + i; |
8c1be37e | 1070 | |
18b5886e | 1071 | if (!p->found) |
8c1be37e LP |
1072 | continue; |
1073 | ||
18b5886e LP |
1074 | if (!p->fstype && p->node) { |
1075 | r = probe_filesystem(p->node, &p->fstype); | |
7cc84b2c | 1076 | if (r < 0 && r != -EUCLEAN) |
18b5886e | 1077 | return r; |
8c1be37e LP |
1078 | } |
1079 | ||
18b5886e LP |
1080 | if (streq_ptr(p->fstype, "crypto_LUKS")) |
1081 | m->encrypted = true; | |
896f937f LP |
1082 | |
1083 | if (p->fstype && fstype_is_ro(p->fstype)) | |
1084 | p->rw = false; | |
8c1be37e LP |
1085 | } |
1086 | ||
1cc6c93a | 1087 | *ret = TAKE_PTR(m); |
8c1be37e LP |
1088 | return 0; |
1089 | #else | |
1090 | return -EOPNOTSUPP; | |
1091 | #endif | |
1092 | } | |
1093 | ||
1094 | DissectedImage* dissected_image_unref(DissectedImage *m) { | |
8c1be37e LP |
1095 | if (!m) |
1096 | return NULL; | |
1097 | ||
569a0e42 | 1098 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
8c1be37e LP |
1099 | free(m->partitions[i].fstype); |
1100 | free(m->partitions[i].node); | |
18b5886e LP |
1101 | free(m->partitions[i].decrypted_fstype); |
1102 | free(m->partitions[i].decrypted_node); | |
18d73705 | 1103 | free(m->partitions[i].mount_options); |
8c1be37e LP |
1104 | } |
1105 | ||
3b925504 LP |
1106 | free(m->hostname); |
1107 | strv_free(m->machine_info); | |
1108 | strv_free(m->os_release); | |
1109 | ||
5fecf46d | 1110 | return mfree(m); |
8c1be37e LP |
1111 | } |
1112 | ||
18b5886e | 1113 | static int is_loop_device(const char *path) { |
553e15f2 | 1114 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
1115 | struct stat st; |
1116 | ||
1117 | assert(path); | |
1118 | ||
1119 | if (stat(path, &st) < 0) | |
1120 | return -errno; | |
1121 | ||
1122 | if (!S_ISBLK(st.st_mode)) | |
1123 | return -ENOTBLK; | |
1124 | ||
553e15f2 | 1125 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
1126 | if (access(s, F_OK) < 0) { |
1127 | if (errno != ENOENT) | |
1128 | return -errno; | |
1129 | ||
1130 | /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */ | |
553e15f2 | 1131 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
1132 | if (access(s, F_OK) < 0) |
1133 | return errno == ENOENT ? false : -errno; | |
1134 | } | |
1135 | ||
1136 | return true; | |
1137 | } | |
1138 | ||
cf32c486 LP |
1139 | static int run_fsck(const char *node, const char *fstype) { |
1140 | int r, exit_status; | |
1141 | pid_t pid; | |
1142 | ||
1143 | assert(node); | |
1144 | assert(fstype); | |
1145 | ||
1146 | r = fsck_exists(fstype); | |
1147 | if (r < 0) { | |
1148 | log_debug_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", fstype); | |
1149 | return 0; | |
1150 | } | |
1151 | if (r == 0) { | |
1152 | log_debug("Not checking partition %s, as fsck for %s does not exist.", node, fstype); | |
1153 | return 0; | |
1154 | } | |
1155 | ||
1156 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_NULL_STDIO, &pid); | |
1157 | if (r < 0) | |
1158 | return log_debug_errno(r, "Failed to fork off fsck: %m"); | |
1159 | if (r == 0) { | |
1160 | /* Child */ | |
1161 | execl("/sbin/fsck", "/sbin/fsck", "-aT", node, NULL); | |
1162 | log_debug_errno(errno, "Failed to execl() fsck: %m"); | |
1163 | _exit(FSCK_OPERATIONAL_ERROR); | |
1164 | } | |
1165 | ||
1166 | exit_status = wait_for_terminate_and_check("fsck", pid, 0); | |
1167 | if (exit_status < 0) | |
1168 | return log_debug_errno(exit_status, "Failed to fork off /sbin/fsck: %m"); | |
1169 | ||
1170 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { | |
1171 | log_debug("fsck failed with exit status %i.", exit_status); | |
1172 | ||
1173 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
1174 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), "File system is corrupted, refusing."); | |
1175 | ||
1176 | log_debug("Ignoring fsck error."); | |
1177 | } | |
1178 | ||
1179 | return 0; | |
1180 | } | |
1181 | ||
18b5886e LP |
1182 | static int mount_partition( |
1183 | DissectedPartition *m, | |
1184 | const char *where, | |
1185 | const char *directory, | |
2d3a5a73 | 1186 | uid_t uid_shift, |
18b5886e LP |
1187 | DissectImageFlags flags) { |
1188 | ||
2d3a5a73 LP |
1189 | _cleanup_free_ char *chased = NULL, *options = NULL; |
1190 | const char *p, *node, *fstype; | |
8c1be37e | 1191 | bool rw; |
2eedfd2d | 1192 | int r; |
8c1be37e LP |
1193 | |
1194 | assert(m); | |
1195 | assert(where); | |
1196 | ||
4dc28665 | 1197 | /* Use decrypted node and matching fstype if available, otherwise use the original device */ |
18b5886e | 1198 | node = m->decrypted_node ?: m->node; |
4dc28665 | 1199 | fstype = m->decrypted_node ? m->decrypted_fstype: m->fstype; |
18b5886e | 1200 | |
4dc28665 | 1201 | if (!m->found || !node) |
8c1be37e | 1202 | return 0; |
4dc28665 LP |
1203 | if (!fstype) |
1204 | return -EAFNOSUPPORT; | |
8c1be37e | 1205 | |
fa45d12c | 1206 | /* We are looking at an encrypted partition? This either means stacked encryption, or the caller didn't call dissected_image_decrypt() beforehand. Let's return a recognizable error for this case. */ |
4dc28665 | 1207 | if (streq(fstype, "crypto_LUKS")) |
fa45d12c | 1208 | return -EUNATCH; |
18b5886e LP |
1209 | |
1210 | rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY); | |
8c1be37e | 1211 | |
cf32c486 LP |
1212 | if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) { |
1213 | r = run_fsck(node, fstype); | |
1214 | if (r < 0) | |
1215 | return r; | |
1216 | } | |
1217 | ||
2eedfd2d | 1218 | if (directory) { |
1f0f82f1 LP |
1219 | if (!FLAGS_SET(flags, DISSECT_IMAGE_READ_ONLY)) { |
1220 | /* Automatically create missing mount points, if necessary. */ | |
1221 | r = mkdir_p_root(where, directory, uid_shift, (gid_t) uid_shift, 0755); | |
1222 | if (r < 0) | |
1223 | return r; | |
1224 | } | |
1225 | ||
a5648b80 | 1226 | r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased, NULL); |
2eedfd2d LP |
1227 | if (r < 0) |
1228 | return r; | |
1229 | ||
1230 | p = chased; | |
1231 | } else | |
8c1be37e LP |
1232 | p = where; |
1233 | ||
18b5886e | 1234 | /* If requested, turn on discard support. */ |
154d2269 | 1235 | if (fstype_can_discard(fstype) && |
18b5886e | 1236 | ((flags & DISSECT_IMAGE_DISCARD) || |
3afda7c7 | 1237 | ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node) > 0))) { |
2d3a5a73 LP |
1238 | options = strdup("discard"); |
1239 | if (!options) | |
1240 | return -ENOMEM; | |
1241 | } | |
1242 | ||
1243 | if (uid_is_valid(uid_shift) && uid_shift != 0 && fstype_can_uid_gid(fstype)) { | |
1244 | _cleanup_free_ char *uid_option = NULL; | |
1245 | ||
1246 | if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) | |
1247 | return -ENOMEM; | |
1248 | ||
1249 | if (!strextend_with_separator(&options, ",", uid_option, NULL)) | |
1250 | return -ENOMEM; | |
1251 | } | |
8c1be37e | 1252 | |
18d73705 LB |
1253 | if (!isempty(m->mount_options)) |
1254 | if (!strextend_with_separator(&options, ",", m->mount_options, NULL)) | |
1255 | return -ENOMEM; | |
1256 | ||
5c05f062 LP |
1257 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { |
1258 | r = mkdir_p(p, 0755); | |
1259 | if (r < 0) | |
1260 | return r; | |
1261 | } | |
1262 | ||
511a8cfe | 1263 | r = mount_nofollow_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options); |
d9223c07 LP |
1264 | if (r < 0) |
1265 | return r; | |
1266 | ||
1267 | return 1; | |
8c1be37e LP |
1268 | } |
1269 | ||
2d3a5a73 | 1270 | int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) { |
1f0f82f1 | 1271 | int r, xbootldr_mounted; |
8c1be37e LP |
1272 | |
1273 | assert(m); | |
1274 | assert(where); | |
1275 | ||
fa45d12c LP |
1276 | /* Returns: |
1277 | * | |
1278 | * -ENXIO → No root partition found | |
1279 | * -EMEDIUMTYPE → DISSECT_IMAGE_VALIDATE_OS set but no os-release file found | |
1280 | * -EUNATCH → Encrypted partition found for which no dm-crypt was set up yet | |
1281 | * -EUCLEAN → fsck for file system failed | |
1282 | * -EBUSY → File system already mounted/used elsewhere (kernel) | |
4dc28665 | 1283 | * -EAFNOSUPPORT → File system type not supported or not known |
fa45d12c LP |
1284 | */ |
1285 | ||
8c1be37e LP |
1286 | if (!m->partitions[PARTITION_ROOT].found) |
1287 | return -ENXIO; | |
1288 | ||
2d3a5a73 LP |
1289 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { |
1290 | r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, flags); | |
1291 | if (r < 0) | |
1292 | return r; | |
aee36b4e LP |
1293 | } |
1294 | ||
1295 | /* Mask DISSECT_IMAGE_MKDIR for all subdirs: the idea is that only the top-level mount point is | |
1296 | * created if needed, but the image itself not modified. */ | |
1297 | flags &= ~DISSECT_IMAGE_MKDIR; | |
1298 | ||
1299 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { | |
1300 | /* For us mounting root always means mounting /usr as well */ | |
1301 | r = mount_partition(m->partitions + PARTITION_USR, where, "/usr", uid_shift, flags); | |
1302 | if (r < 0) | |
1303 | return r; | |
03bcb6d4 LP |
1304 | |
1305 | if (flags & DISSECT_IMAGE_VALIDATE_OS) { | |
1306 | r = path_is_os_tree(where); | |
1307 | if (r < 0) | |
1308 | return r; | |
1309 | if (r == 0) | |
1310 | return -EMEDIUMTYPE; | |
1311 | } | |
2d3a5a73 LP |
1312 | } |
1313 | ||
705727fd | 1314 | if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY) |
2d3a5a73 | 1315 | return 0; |
8c1be37e | 1316 | |
2d3a5a73 | 1317 | r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags); |
8c1be37e LP |
1318 | if (r < 0) |
1319 | return r; | |
1320 | ||
2d3a5a73 | 1321 | r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, flags); |
8c1be37e LP |
1322 | if (r < 0) |
1323 | return r; | |
1324 | ||
d4dffb85 LP |
1325 | r = mount_partition(m->partitions + PARTITION_VAR, where, "/var", uid_shift, flags); |
1326 | if (r < 0) | |
1327 | return r; | |
1328 | ||
1329 | r = mount_partition(m->partitions + PARTITION_TMP, where, "/var/tmp", uid_shift, flags); | |
1330 | if (r < 0) | |
1331 | return r; | |
1332 | ||
1f0f82f1 LP |
1333 | xbootldr_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, flags); |
1334 | if (xbootldr_mounted < 0) | |
1335 | return xbootldr_mounted; | |
d9223c07 | 1336 | |
8c1be37e | 1337 | if (m->partitions[PARTITION_ESP].found) { |
1f0f82f1 LP |
1338 | int esp_done = false; |
1339 | ||
d9223c07 LP |
1340 | /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it |
1341 | * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */ | |
8c1be37e | 1342 | |
a5648b80 | 1343 | r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL); |
1f0f82f1 LP |
1344 | if (r < 0) { |
1345 | if (r != -ENOENT) | |
d9223c07 | 1346 | return r; |
8c1be37e | 1347 | |
1f0f82f1 LP |
1348 | /* /efi doesn't exist. Let's see if /boot is suitable then */ |
1349 | ||
1350 | if (!xbootldr_mounted) { | |
1351 | _cleanup_free_ char *p = NULL; | |
2eedfd2d | 1352 | |
1f0f82f1 LP |
1353 | r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p, NULL); |
1354 | if (r < 0) { | |
1355 | if (r != -ENOENT) | |
1356 | return r; | |
1357 | } else if (dir_is_empty(p) > 0) { | |
1358 | /* It exists and is an empty directory. Let's mount the ESP there. */ | |
1359 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, flags); | |
1360 | if (r < 0) | |
1361 | return r; | |
1362 | ||
1363 | esp_done = true; | |
1364 | } | |
2eedfd2d | 1365 | } |
8c1be37e | 1366 | } |
1f0f82f1 LP |
1367 | |
1368 | if (!esp_done) { | |
1369 | /* OK, let's mount the ESP now to /efi (possibly creating the dir if missing) */ | |
1370 | ||
1371 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, flags); | |
1372 | if (r < 0) | |
1373 | return r; | |
1374 | } | |
8c1be37e LP |
1375 | } |
1376 | ||
1377 | return 0; | |
1378 | } | |
1379 | ||
af187ab2 LP |
1380 | int dissected_image_mount_and_warn(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) { |
1381 | int r; | |
1382 | ||
1383 | assert(m); | |
1384 | assert(where); | |
1385 | ||
1386 | r = dissected_image_mount(m, where, uid_shift, flags); | |
1387 | if (r == -ENXIO) | |
1388 | return log_error_errno(r, "Not root file system found in image."); | |
1389 | if (r == -EMEDIUMTYPE) | |
1390 | return log_error_errno(r, "No suitable os-release file in image found."); | |
1391 | if (r == -EUNATCH) | |
1392 | return log_error_errno(r, "Encrypted file system discovered, but decryption not requested."); | |
1393 | if (r == -EUCLEAN) | |
1394 | return log_error_errno(r, "File system check on image failed."); | |
1395 | if (r == -EBUSY) | |
1396 | return log_error_errno(r, "File system already mounted elsewhere."); | |
4dc28665 LP |
1397 | if (r == -EAFNOSUPPORT) |
1398 | return log_error_errno(r, "File system type not supported or not known."); | |
af187ab2 LP |
1399 | if (r < 0) |
1400 | return log_error_errno(r, "Failed to mount image: %m"); | |
1401 | ||
1402 | return r; | |
1403 | } | |
1404 | ||
349cc4a5 | 1405 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1406 | typedef struct DecryptedPartition { |
1407 | struct crypt_device *device; | |
1408 | char *name; | |
1409 | bool relinquished; | |
1410 | } DecryptedPartition; | |
1411 | ||
1412 | struct DecryptedImage { | |
1413 | DecryptedPartition *decrypted; | |
1414 | size_t n_decrypted; | |
1415 | size_t n_allocated; | |
1416 | }; | |
1417 | #endif | |
1418 | ||
1419 | DecryptedImage* decrypted_image_unref(DecryptedImage* d) { | |
349cc4a5 | 1420 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1421 | size_t i; |
1422 | int r; | |
1423 | ||
1424 | if (!d) | |
1425 | return NULL; | |
1426 | ||
1427 | for (i = 0; i < d->n_decrypted; i++) { | |
1428 | DecryptedPartition *p = d->decrypted + i; | |
1429 | ||
1430 | if (p->device && p->name && !p->relinquished) { | |
0d12936d | 1431 | r = sym_crypt_deactivate_by_name(p->device, p->name, 0); |
18b5886e LP |
1432 | if (r < 0) |
1433 | log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name); | |
1434 | } | |
1435 | ||
1436 | if (p->device) | |
0d12936d | 1437 | sym_crypt_free(p->device); |
18b5886e LP |
1438 | free(p->name); |
1439 | } | |
1440 | ||
1441 | free(d); | |
1442 | #endif | |
1443 | return NULL; | |
1444 | } | |
1445 | ||
349cc4a5 | 1446 | #if HAVE_LIBCRYPTSETUP |
4623e8e6 LP |
1447 | |
1448 | static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) { | |
1449 | _cleanup_free_ char *name = NULL, *node = NULL; | |
1450 | const char *base; | |
1451 | ||
1452 | assert(original_node); | |
1453 | assert(suffix); | |
1454 | assert(ret_name); | |
1455 | assert(ret_node); | |
1456 | ||
1457 | base = strrchr(original_node, '/'); | |
1458 | if (!base) | |
ac1f3ad0 LB |
1459 | base = original_node; |
1460 | else | |
1461 | base++; | |
4623e8e6 LP |
1462 | if (isempty(base)) |
1463 | return -EINVAL; | |
1464 | ||
1465 | name = strjoin(base, suffix); | |
1466 | if (!name) | |
1467 | return -ENOMEM; | |
1468 | if (!filename_is_valid(name)) | |
1469 | return -EINVAL; | |
1470 | ||
0d12936d | 1471 | node = path_join(sym_crypt_get_dir(), name); |
4623e8e6 LP |
1472 | if (!node) |
1473 | return -ENOMEM; | |
1474 | ||
1cc6c93a YW |
1475 | *ret_name = TAKE_PTR(name); |
1476 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 1477 | |
4623e8e6 LP |
1478 | return 0; |
1479 | } | |
1480 | ||
18b5886e LP |
1481 | static int decrypt_partition( |
1482 | DissectedPartition *m, | |
1483 | const char *passphrase, | |
1484 | DissectImageFlags flags, | |
1485 | DecryptedImage *d) { | |
1486 | ||
1487 | _cleanup_free_ char *node = NULL, *name = NULL; | |
0d12936d | 1488 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
18b5886e LP |
1489 | int r; |
1490 | ||
1491 | assert(m); | |
1492 | assert(d); | |
1493 | ||
1494 | if (!m->found || !m->node || !m->fstype) | |
1495 | return 0; | |
1496 | ||
1497 | if (!streq(m->fstype, "crypto_LUKS")) | |
1498 | return 0; | |
1499 | ||
bdd73ac5 ZJS |
1500 | if (!passphrase) |
1501 | return -ENOKEY; | |
1502 | ||
0d12936d LP |
1503 | r = dlopen_cryptsetup(); |
1504 | if (r < 0) | |
1505 | return r; | |
1506 | ||
4623e8e6 LP |
1507 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
1508 | if (r < 0) | |
1509 | return r; | |
18b5886e LP |
1510 | |
1511 | if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1)) | |
1512 | return -ENOMEM; | |
1513 | ||
0d12936d | 1514 | r = sym_crypt_init(&cd, m->node); |
18b5886e | 1515 | if (r < 0) |
715cbb81 | 1516 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 1517 | |
efc3b12f | 1518 | cryptsetup_enable_logging(cd); |
1887032f | 1519 | |
0d12936d | 1520 | r = sym_crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
1521 | if (r < 0) |
1522 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e | 1523 | |
0d12936d LP |
1524 | r = sym_crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), |
1525 | ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | | |
1526 | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); | |
294bd454 | 1527 | if (r < 0) { |
715cbb81 | 1528 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 1529 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 1530 | } |
18b5886e | 1531 | |
94344385 LP |
1532 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
1533 | .name = TAKE_PTR(name), | |
1534 | .device = TAKE_PTR(cd), | |
1535 | }; | |
18b5886e | 1536 | |
1cc6c93a | 1537 | m->decrypted_node = TAKE_PTR(node); |
18b5886e LP |
1538 | |
1539 | return 0; | |
4623e8e6 LP |
1540 | } |
1541 | ||
89e62e0b LP |
1542 | static int verity_can_reuse( |
1543 | const VeritySettings *verity, | |
1544 | const char *name, | |
1545 | struct crypt_device **ret_cd) { | |
1546 | ||
ac1f3ad0 LB |
1547 | /* If the same volume was already open, check that the root hashes match, and reuse it if they do */ |
1548 | _cleanup_free_ char *root_hash_existing = NULL; | |
0d12936d | 1549 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 1550 | struct crypt_params_verity crypt_params = {}; |
89e62e0b | 1551 | size_t root_hash_existing_size; |
ac1f3ad0 LB |
1552 | int r; |
1553 | ||
89e62e0b LP |
1554 | assert(verity); |
1555 | assert(name); | |
ac1f3ad0 LB |
1556 | assert(ret_cd); |
1557 | ||
0d12936d | 1558 | r = sym_crypt_init_by_name(&cd, name); |
ac1f3ad0 LB |
1559 | if (r < 0) |
1560 | return log_debug_errno(r, "Error opening verity device, crypt_init_by_name failed: %m"); | |
0d12936d LP |
1561 | |
1562 | r = sym_crypt_get_verity_info(cd, &crypt_params); | |
ac1f3ad0 LB |
1563 | if (r < 0) |
1564 | return log_debug_errno(r, "Error opening verity device, crypt_get_verity_info failed: %m"); | |
0d12936d | 1565 | |
89e62e0b LP |
1566 | root_hash_existing_size = verity->root_hash_size; |
1567 | root_hash_existing = malloc0(root_hash_existing_size); | |
ac1f3ad0 LB |
1568 | if (!root_hash_existing) |
1569 | return -ENOMEM; | |
0d12936d LP |
1570 | |
1571 | r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash_existing, &root_hash_existing_size, NULL, 0); | |
ac1f3ad0 LB |
1572 | if (r < 0) |
1573 | return log_debug_errno(r, "Error opening verity device, crypt_volume_key_get failed: %m"); | |
89e62e0b LP |
1574 | if (verity->root_hash_size != root_hash_existing_size || |
1575 | memcmp(root_hash_existing, verity->root_hash, verity->root_hash_size) != 0) | |
ac1f3ad0 | 1576 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but root hashes are different."); |
89e62e0b | 1577 | |
ac1f3ad0 | 1578 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY |
89e62e0b LP |
1579 | /* Ensure that, if signatures are supported, we only reuse the device if the previous mount used the |
1580 | * same settings, so that a previous unsigned mount will not be reused if the user asks to use | |
1581 | * signing for the new one, and viceversa. */ | |
1582 | if (!!verity->root_hash_sig != !!(crypt_params.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE)) | |
ac1f3ad0 LB |
1583 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but signature settings are not the same."); |
1584 | #endif | |
1585 | ||
1586 | *ret_cd = TAKE_PTR(cd); | |
1587 | return 0; | |
1588 | } | |
1589 | ||
1590 | static inline void dm_deferred_remove_clean(char *name) { | |
1591 | if (!name) | |
1592 | return; | |
0d12936d LP |
1593 | |
1594 | (void) sym_crypt_deactivate_by_name(NULL, name, CRYPT_DEACTIVATE_DEFERRED); | |
ac1f3ad0 LB |
1595 | free(name); |
1596 | } | |
1597 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, dm_deferred_remove_clean); | |
1598 | ||
4623e8e6 | 1599 | static int verity_partition( |
aee36b4e | 1600 | PartitionDesignator designator, |
4623e8e6 LP |
1601 | DissectedPartition *m, |
1602 | DissectedPartition *v, | |
89e62e0b | 1603 | const VeritySettings *verity, |
4623e8e6 LP |
1604 | DissectImageFlags flags, |
1605 | DecryptedImage *d) { | |
1606 | ||
0d12936d | 1607 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 1608 | _cleanup_(dm_deferred_remove_cleanp) char *restore_deferred_remove = NULL; |
89e62e0b | 1609 | _cleanup_free_ char *node = NULL, *name = NULL; |
4623e8e6 LP |
1610 | int r; |
1611 | ||
1612 | assert(m); | |
89e62e0b | 1613 | assert(v || (verity && verity->data_path)); |
4623e8e6 | 1614 | |
89e62e0b | 1615 | if (!verity || !verity->root_hash) |
4623e8e6 | 1616 | return 0; |
aee36b4e LP |
1617 | if (!((verity->designator < 0 && designator == PARTITION_ROOT) || |
1618 | (verity->designator == designator))) | |
1619 | return 0; | |
4623e8e6 LP |
1620 | |
1621 | if (!m->found || !m->node || !m->fstype) | |
1622 | return 0; | |
89e62e0b | 1623 | if (!verity->data_path) { |
e7cbe5cb LB |
1624 | if (!v->found || !v->node || !v->fstype) |
1625 | return 0; | |
4623e8e6 | 1626 | |
e7cbe5cb LB |
1627 | if (!streq(v->fstype, "DM_verity_hash")) |
1628 | return 0; | |
1629 | } | |
4623e8e6 | 1630 | |
0d12936d LP |
1631 | r = dlopen_cryptsetup(); |
1632 | if (r < 0) | |
1633 | return r; | |
1634 | ||
ac1f3ad0 LB |
1635 | if (FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) { |
1636 | /* Use the roothash, which is unique per volume, as the device node name, so that it can be reused */ | |
1637 | _cleanup_free_ char *root_hash_encoded = NULL; | |
0d12936d | 1638 | |
89e62e0b | 1639 | root_hash_encoded = hexmem(verity->root_hash, verity->root_hash_size); |
ac1f3ad0 LB |
1640 | if (!root_hash_encoded) |
1641 | return -ENOMEM; | |
aee36b4e | 1642 | |
ac1f3ad0 LB |
1643 | r = make_dm_name_and_node(root_hash_encoded, "-verity", &name, &node); |
1644 | } else | |
1645 | r = make_dm_name_and_node(m->node, "-verity", &name, &node); | |
4623e8e6 LP |
1646 | if (r < 0) |
1647 | return r; | |
1648 | ||
89e62e0b | 1649 | r = sym_crypt_init(&cd, verity->data_path ?: v->node); |
4623e8e6 LP |
1650 | if (r < 0) |
1651 | return r; | |
1652 | ||
efc3b12f | 1653 | cryptsetup_enable_logging(cd); |
1887032f | 1654 | |
0d12936d | 1655 | r = sym_crypt_load(cd, CRYPT_VERITY, NULL); |
4623e8e6 | 1656 | if (r < 0) |
294bd454 | 1657 | return r; |
4623e8e6 | 1658 | |
0d12936d | 1659 | r = sym_crypt_set_data_device(cd, m->node); |
4623e8e6 | 1660 | if (r < 0) |
294bd454 | 1661 | return r; |
4623e8e6 | 1662 | |
ac1f3ad0 LB |
1663 | if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1)) |
1664 | return -ENOMEM; | |
1665 | ||
1666 | /* If activating fails because the device already exists, check the metadata and reuse it if it matches. | |
1667 | * In case of ENODEV/ENOENT, which can happen if another process is activating at the exact same time, | |
1668 | * retry a few times before giving up. */ | |
1669 | for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { | |
89e62e0b | 1670 | if (verity->root_hash_sig) { |
c2923fdc | 1671 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY |
89e62e0b LP |
1672 | r = sym_crypt_activate_by_signed_key( |
1673 | cd, | |
1674 | name, | |
1675 | verity->root_hash, | |
1676 | verity->root_hash_size, | |
1677 | verity->root_hash_sig, | |
1678 | verity->root_hash_sig_size, | |
1679 | CRYPT_ACTIVATE_READONLY); | |
ac1f3ad0 | 1680 | #else |
22043172 LP |
1681 | r = log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
1682 | "Activation of verity device with signature requested, but not supported by %s due to missing crypt_activate_by_signed_key().", program_invocation_short_name); | |
ac1f3ad0 LB |
1683 | #endif |
1684 | } else | |
89e62e0b LP |
1685 | r = sym_crypt_activate_by_volume_key( |
1686 | cd, | |
1687 | name, | |
1688 | verity->root_hash, | |
1689 | verity->root_hash_size, | |
1690 | CRYPT_ACTIVATE_READONLY); | |
ac1f3ad0 LB |
1691 | /* libdevmapper can return EINVAL when the device is already in the activation stage. |
1692 | * There's no way to distinguish this situation from a genuine error due to invalid | |
2aed63f4 | 1693 | * parameters, so immediately fall back to activating the device with a unique name. |
89e62e0b LP |
1694 | * Improvements in libcrypsetup can ensure this never happens: |
1695 | * https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/96 */ | |
ac1f3ad0 | 1696 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) |
aee36b4e | 1697 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
9ecb5c10 | 1698 | if (!IN_SET(r, |
22043172 | 1699 | 0, /* Success */ |
9ecb5c10 | 1700 | -EEXIST, /* Volume is already open and ready to be used */ |
22043172 LP |
1701 | -EBUSY, /* Volume is being opened but not ready, crypt_init_by_name can fetch details */ |
1702 | -ENODEV /* Volume is being opened but not ready, crypt_init_by_name would fail, try to open again */)) | |
ac1f3ad0 | 1703 | return r; |
9ecb5c10 | 1704 | if (IN_SET(r, -EEXIST, -EBUSY)) { |
ac1f3ad0 | 1705 | struct crypt_device *existing_cd = NULL; |
c2923fdc | 1706 | |
ac1f3ad0 LB |
1707 | if (!restore_deferred_remove){ |
1708 | /* To avoid races, disable automatic removal on umount while setting up the new device. Restore it on failure. */ | |
1709 | r = dm_deferred_remove_cancel(name); | |
9ecb5c10 LB |
1710 | /* If activation returns EBUSY there might be no deferred removal to cancel, that's fine */ |
1711 | if (r < 0 && r != -ENXIO) | |
ac1f3ad0 | 1712 | return log_debug_errno(r, "Disabling automated deferred removal for verity device %s failed: %m", node); |
9ecb5c10 LB |
1713 | if (r == 0) { |
1714 | restore_deferred_remove = strdup(name); | |
1715 | if (!restore_deferred_remove) | |
1716 | return -ENOMEM; | |
1717 | } | |
ac1f3ad0 | 1718 | } |
c2923fdc | 1719 | |
89e62e0b | 1720 | r = verity_can_reuse(verity, name, &existing_cd); |
ac1f3ad0 LB |
1721 | /* Same as above, -EINVAL can randomly happen when it actually means -EEXIST */ |
1722 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
aee36b4e | 1723 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
9ecb5c10 | 1724 | if (!IN_SET(r, 0, -ENODEV, -ENOENT, -EBUSY)) |
ac1f3ad0 LB |
1725 | return log_debug_errno(r, "Checking whether existing verity device %s can be reused failed: %m", node); |
1726 | if (r == 0) { | |
c419b6f0 LB |
1727 | /* devmapper might say that the device exists, but the devlink might not yet have been |
1728 | * created. Check and wait for the udev event in that case. */ | |
9e3d9067 | 1729 | r = device_wait_for_devlink(node, "block", usec_add(now(CLOCK_MONOTONIC), 100 * USEC_PER_MSEC), NULL); |
c419b6f0 LB |
1730 | /* Fallback to activation with a unique device if it's taking too long */ |
1731 | if (r == -ETIMEDOUT) | |
1732 | break; | |
1733 | if (r < 0) | |
1734 | return r; | |
1735 | ||
ac1f3ad0 | 1736 | if (cd) |
0d12936d | 1737 | sym_crypt_free(cd); |
ac1f3ad0 LB |
1738 | cd = existing_cd; |
1739 | } | |
c2923fdc | 1740 | } |
ac1f3ad0 LB |
1741 | if (r == 0) |
1742 | break; | |
ecab4c47 LB |
1743 | |
1744 | /* Device is being opened by another process, but it has not finished yet, yield for 2ms */ | |
1745 | (void) usleep(2 * USEC_PER_MSEC); | |
ac1f3ad0 LB |
1746 | } |
1747 | ||
ac1f3ad0 LB |
1748 | /* An existing verity device was reported by libcryptsetup/libdevmapper, but we can't use it at this time. |
1749 | * Fall back to activating it with a unique device name. */ | |
1750 | if (r != 0 && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
aee36b4e | 1751 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
ac1f3ad0 LB |
1752 | |
1753 | /* Everything looks good and we'll be able to mount the device, so deferred remove will be re-enabled at that point. */ | |
1754 | restore_deferred_remove = mfree(restore_deferred_remove); | |
4623e8e6 | 1755 | |
94344385 LP |
1756 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
1757 | .name = TAKE_PTR(name), | |
1758 | .device = TAKE_PTR(cd), | |
1759 | }; | |
4623e8e6 | 1760 | |
1cc6c93a | 1761 | m->decrypted_node = TAKE_PTR(node); |
4623e8e6 LP |
1762 | |
1763 | return 0; | |
18b5886e LP |
1764 | } |
1765 | #endif | |
1766 | ||
1767 | int dissected_image_decrypt( | |
1768 | DissectedImage *m, | |
1769 | const char *passphrase, | |
89e62e0b | 1770 | const VeritySettings *verity, |
18b5886e LP |
1771 | DissectImageFlags flags, |
1772 | DecryptedImage **ret) { | |
1773 | ||
349cc4a5 | 1774 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 1775 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
1776 | int r; |
1777 | #endif | |
1778 | ||
1779 | assert(m); | |
89e62e0b | 1780 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
18b5886e LP |
1781 | |
1782 | /* Returns: | |
1783 | * | |
1784 | * = 0 → There was nothing to decrypt | |
1785 | * > 0 → Decrypted successfully | |
d1c536f5 | 1786 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
1787 | * -EKEYREJECTED → Passed key was not correct |
1788 | */ | |
1789 | ||
89e62e0b | 1790 | if (verity && verity->root_hash && verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
1791 | return -EINVAL; |
1792 | ||
1793 | if (!m->encrypted && !m->verity) { | |
18b5886e LP |
1794 | *ret = NULL; |
1795 | return 0; | |
1796 | } | |
1797 | ||
349cc4a5 | 1798 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1799 | d = new0(DecryptedImage, 1); |
1800 | if (!d) | |
1801 | return -ENOMEM; | |
1802 | ||
569a0e42 | 1803 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
18b5886e | 1804 | DissectedPartition *p = m->partitions + i; |
22043172 | 1805 | PartitionDesignator k; |
18b5886e LP |
1806 | |
1807 | if (!p->found) | |
1808 | continue; | |
1809 | ||
1810 | r = decrypt_partition(p, passphrase, flags, d); | |
1811 | if (r < 0) | |
1812 | return r; | |
1813 | ||
4623e8e6 LP |
1814 | k = PARTITION_VERITY_OF(i); |
1815 | if (k >= 0) { | |
aee36b4e | 1816 | r = verity_partition(i, p, m->partitions + k, verity, flags | DISSECT_IMAGE_VERITY_SHARE, d); |
4623e8e6 LP |
1817 | if (r < 0) |
1818 | return r; | |
1819 | } | |
1820 | ||
18b5886e LP |
1821 | if (!p->decrypted_fstype && p->decrypted_node) { |
1822 | r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype); | |
7cc84b2c | 1823 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
1824 | return r; |
1825 | } | |
1826 | } | |
1827 | ||
1cc6c93a | 1828 | *ret = TAKE_PTR(d); |
18b5886e LP |
1829 | |
1830 | return 1; | |
1831 | #else | |
1832 | return -EOPNOTSUPP; | |
1833 | #endif | |
1834 | } | |
1835 | ||
1836 | int dissected_image_decrypt_interactively( | |
1837 | DissectedImage *m, | |
1838 | const char *passphrase, | |
89e62e0b | 1839 | const VeritySettings *verity, |
18b5886e LP |
1840 | DissectImageFlags flags, |
1841 | DecryptedImage **ret) { | |
1842 | ||
1843 | _cleanup_strv_free_erase_ char **z = NULL; | |
1844 | int n = 3, r; | |
1845 | ||
1846 | if (passphrase) | |
1847 | n--; | |
1848 | ||
1849 | for (;;) { | |
89e62e0b | 1850 | r = dissected_image_decrypt(m, passphrase, verity, flags, ret); |
18b5886e LP |
1851 | if (r >= 0) |
1852 | return r; | |
1853 | if (r == -EKEYREJECTED) | |
1854 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
fc95c359 YW |
1855 | else if (r != -ENOKEY) |
1856 | return log_error_errno(r, "Failed to decrypt image: %m"); | |
18b5886e | 1857 | |
baaa35ad ZJS |
1858 | if (--n < 0) |
1859 | return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED), | |
1860 | "Too many retries."); | |
18b5886e LP |
1861 | |
1862 | z = strv_free(z); | |
1863 | ||
a1c111c2 | 1864 | r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z); |
18b5886e LP |
1865 | if (r < 0) |
1866 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
1867 | ||
1868 | passphrase = z[0]; | |
1869 | } | |
1870 | } | |
1871 | ||
18b5886e LP |
1872 | int decrypted_image_relinquish(DecryptedImage *d) { |
1873 | ||
349cc4a5 | 1874 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1875 | size_t i; |
1876 | int r; | |
1877 | #endif | |
1878 | ||
1879 | assert(d); | |
1880 | ||
1881 | /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so | |
1882 | * that we don't clean it up ourselves either anymore */ | |
1883 | ||
349cc4a5 | 1884 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1885 | for (i = 0; i < d->n_decrypted; i++) { |
1886 | DecryptedPartition *p = d->decrypted + i; | |
1887 | ||
1888 | if (p->relinquished) | |
1889 | continue; | |
1890 | ||
0d12936d | 1891 | r = sym_crypt_deactivate_by_name(NULL, p->name, CRYPT_DEACTIVATE_DEFERRED); |
18b5886e LP |
1892 | if (r < 0) |
1893 | return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); | |
1894 | ||
1895 | p->relinquished = true; | |
1896 | } | |
1897 | #endif | |
1898 | ||
1899 | return 0; | |
1900 | } | |
1901 | ||
89e62e0b LP |
1902 | static char *build_auxiliary_path(const char *image, const char *suffix) { |
1903 | const char *e; | |
1904 | char *n; | |
1905 | ||
1906 | assert(image); | |
1907 | assert(suffix); | |
1908 | ||
1909 | e = endswith(image, ".raw"); | |
1910 | if (!e) | |
1911 | return strjoin(e, suffix); | |
1912 | ||
1913 | n = new(char, e - image + strlen(suffix) + 1); | |
1914 | if (!n) | |
1915 | return NULL; | |
1916 | ||
1917 | strcpy(mempcpy(n, image, e - image), suffix); | |
1918 | return n; | |
1919 | } | |
1920 | ||
1921 | void verity_settings_done(VeritySettings *v) { | |
1922 | assert(v); | |
1923 | ||
1924 | v->root_hash = mfree(v->root_hash); | |
1925 | v->root_hash_size = 0; | |
1926 | ||
1927 | v->root_hash_sig = mfree(v->root_hash_sig); | |
1928 | v->root_hash_sig_size = 0; | |
1929 | ||
1930 | v->data_path = mfree(v->data_path); | |
1931 | } | |
1932 | ||
1933 | int verity_settings_load( | |
1934 | VeritySettings *verity, | |
f5ea63a5 LP |
1935 | const char *image, |
1936 | const char *root_hash_path, | |
89e62e0b LP |
1937 | const char *root_hash_sig_path) { |
1938 | ||
1939 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
1940 | size_t root_hash_size = 0, root_hash_sig_size = 0; | |
1941 | _cleanup_free_ char *verity_data_path = NULL; | |
aee36b4e | 1942 | PartitionDesignator designator; |
78ebe980 LP |
1943 | int r; |
1944 | ||
89e62e0b | 1945 | assert(verity); |
78ebe980 | 1946 | assert(image); |
aee36b4e | 1947 | assert(verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
78ebe980 | 1948 | |
89e62e0b LP |
1949 | /* If we are asked to load the root hash for a device node, exit early */ |
1950 | if (is_device_path(image)) | |
78ebe980 | 1951 | return 0; |
78ebe980 | 1952 | |
aee36b4e LP |
1953 | designator = verity->designator; |
1954 | ||
89e62e0b | 1955 | /* We only fill in what isn't already filled in */ |
c2923fdc | 1956 | |
89e62e0b | 1957 | if (!verity->root_hash) { |
e7cbe5cb | 1958 | _cleanup_free_ char *text = NULL; |
e7cbe5cb | 1959 | |
0389f4fa | 1960 | if (root_hash_path) { |
aee36b4e | 1961 | /* If explicitly specified it takes precedence */ |
0389f4fa LB |
1962 | r = read_one_line_file(root_hash_path, &text); |
1963 | if (r < 0) | |
e7cbe5cb | 1964 | return r; |
aee36b4e LP |
1965 | |
1966 | if (designator < 0) | |
1967 | designator = PARTITION_ROOT; | |
0389f4fa | 1968 | } else { |
aee36b4e LP |
1969 | /* Otherwise look for xattr and separate file, and first for the data for root and if |
1970 | * that doesn't exist for /usr */ | |
0389f4fa | 1971 | |
aee36b4e LP |
1972 | if (designator < 0 || designator == PARTITION_ROOT) { |
1973 | r = getxattr_malloc(image, "user.verity.roothash", &text, true); | |
1974 | if (r < 0) { | |
1975 | _cleanup_free_ char *p = NULL; | |
78ebe980 | 1976 | |
aee36b4e LP |
1977 | if (!IN_SET(r, -ENODATA, -ENOENT) && !ERRNO_IS_NOT_SUPPORTED(r)) |
1978 | return r; | |
e7cbe5cb | 1979 | |
aee36b4e LP |
1980 | p = build_auxiliary_path(image, ".roothash"); |
1981 | if (!p) | |
1982 | return -ENOMEM; | |
1983 | ||
1984 | r = read_one_line_file(p, &text); | |
1985 | if (r < 0 && r != -ENOENT) | |
1986 | return r; | |
1987 | } | |
1988 | ||
1989 | if (text) | |
1990 | designator = PARTITION_ROOT; | |
1991 | } | |
1992 | ||
1993 | if (!text && (designator < 0 || designator == PARTITION_USR)) { | |
1994 | /* So in the "roothash" xattr/file name above the "root" of course primarily | |
1995 | * refers to the root of the Verity Merkle tree. But coincidentally it also | |
1996 | * is the hash for the *root* file system, i.e. the "root" neatly refers to | |
1997 | * two distinct concepts called "root". Taking benefit of this happy | |
1998 | * coincidence we call the file with the root hash for the /usr/ file system | |
1999 | * `usrhash`, because `usrroothash` or `rootusrhash` would just be too | |
2000 | * confusing. We thus drop the reference to the root of the Merkle tree, and | |
2001 | * just indicate which file system it's about. */ | |
2002 | r = getxattr_malloc(image, "user.verity.usrhash", &text, true); | |
2003 | if (r < 0) { | |
2004 | _cleanup_free_ char *p = NULL; | |
2005 | ||
2006 | if (!IN_SET(r, -ENODATA, -ENOENT) && !ERRNO_IS_NOT_SUPPORTED(r)) | |
2007 | return r; | |
2008 | ||
2009 | p = build_auxiliary_path(image, ".usrhash"); | |
2010 | if (!p) | |
2011 | return -ENOMEM; | |
2012 | ||
2013 | r = read_one_line_file(p, &text); | |
2014 | if (r < 0 && r != -ENOENT) | |
2015 | return r; | |
2016 | } | |
2017 | ||
2018 | if (text) | |
2019 | designator = PARTITION_USR; | |
0389f4fa | 2020 | } |
e7cbe5cb LB |
2021 | } |
2022 | ||
2023 | if (text) { | |
89e62e0b | 2024 | r = unhexmem(text, strlen(text), &root_hash, &root_hash_size); |
e7cbe5cb LB |
2025 | if (r < 0) |
2026 | return r; | |
89e62e0b | 2027 | if (root_hash_size < sizeof(sd_id128_t)) |
e7cbe5cb LB |
2028 | return -EINVAL; |
2029 | } | |
2030 | } | |
2031 | ||
aee36b4e LP |
2032 | if (verity->root_hash && !verity->root_hash_sig) { |
2033 | if (root_hash_sig_path) { | |
2034 | r = read_full_file_full(AT_FDCWD, root_hash_sig_path, 0, (char**) &root_hash_sig, &root_hash_sig_size); | |
2035 | if (r < 0 && r != -ENOENT) | |
2036 | return r; | |
2037 | ||
2038 | if (designator < 0) | |
2039 | designator = PARTITION_ROOT; | |
2040 | } else { | |
2041 | if (designator < 0 || designator == PARTITION_ROOT) { | |
2042 | _cleanup_free_ char *p = NULL; | |
2043 | ||
2044 | /* Follow naming convention recommended by the relevant RFC: | |
2045 | * https://tools.ietf.org/html/rfc5751#section-3.2.1 */ | |
2046 | p = build_auxiliary_path(image, ".roothash.p7s"); | |
2047 | if (!p) | |
2048 | return -ENOMEM; | |
89e62e0b | 2049 | |
7025fa8b | 2050 | r = read_full_file_full(AT_FDCWD, p, 0, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2051 | if (r < 0 && r != -ENOENT) |
2052 | return r; | |
2053 | if (r >= 0) | |
2054 | designator = PARTITION_ROOT; | |
2055 | } | |
2056 | ||
2057 | if (!root_hash_sig && (designator < 0 || designator == PARTITION_USR)) { | |
2058 | _cleanup_free_ char *p = NULL; | |
2059 | ||
2060 | p = build_auxiliary_path(image, ".usrhash.p7s"); | |
2061 | if (!p) | |
2062 | return -ENOMEM; | |
89e62e0b | 2063 | |
7025fa8b | 2064 | r = read_full_file_full(AT_FDCWD, p, 0, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2065 | if (r < 0 && r != -ENOENT) |
2066 | return r; | |
2067 | if (r >= 0) | |
2068 | designator = PARTITION_USR; | |
2069 | } | |
89e62e0b LP |
2070 | } |
2071 | ||
aee36b4e | 2072 | if (root_hash_sig && root_hash_sig_size == 0) /* refuse empty size signatures */ |
89e62e0b LP |
2073 | return -EINVAL; |
2074 | } | |
2075 | ||
2076 | if (!verity->data_path) { | |
2077 | _cleanup_free_ char *p = NULL; | |
2078 | ||
2079 | p = build_auxiliary_path(image, ".verity"); | |
2080 | if (!p) | |
2081 | return -ENOMEM; | |
2082 | ||
2083 | if (access(p, F_OK) < 0) { | |
2084 | if (errno != ENOENT) | |
2085 | return -errno; | |
2086 | } else | |
2087 | verity_data_path = TAKE_PTR(p); | |
2088 | } | |
2089 | ||
2090 | if (root_hash) { | |
2091 | verity->root_hash = TAKE_PTR(root_hash); | |
2092 | verity->root_hash_size = root_hash_size; | |
2093 | } | |
2094 | ||
2095 | if (root_hash_sig) { | |
2096 | verity->root_hash_sig = TAKE_PTR(root_hash_sig); | |
2097 | verity->root_hash_sig_size = root_hash_sig_size; | |
e7cbe5cb | 2098 | } |
89e62e0b LP |
2099 | |
2100 | if (verity_data_path) | |
2101 | verity->data_path = TAKE_PTR(verity_data_path); | |
78ebe980 | 2102 | |
aee36b4e LP |
2103 | if (verity->designator < 0) |
2104 | verity->designator = designator; | |
2105 | ||
78ebe980 LP |
2106 | return 1; |
2107 | } | |
2108 | ||
3b925504 LP |
2109 | int dissected_image_acquire_metadata(DissectedImage *m) { |
2110 | ||
2111 | enum { | |
2112 | META_HOSTNAME, | |
2113 | META_MACHINE_ID, | |
2114 | META_MACHINE_INFO, | |
2115 | META_OS_RELEASE, | |
2116 | _META_MAX, | |
2117 | }; | |
2118 | ||
2119 | static const char *const paths[_META_MAX] = { | |
2120 | [META_HOSTNAME] = "/etc/hostname\0", | |
2121 | [META_MACHINE_ID] = "/etc/machine-id\0", | |
2122 | [META_MACHINE_INFO] = "/etc/machine-info\0", | |
d4dffb85 LP |
2123 | [META_OS_RELEASE] = "/etc/os-release\0" |
2124 | "/usr/lib/os-release\0", | |
3b925504 LP |
2125 | }; |
2126 | ||
2127 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL; | |
af8219d5 | 2128 | _cleanup_close_pair_ int error_pipe[2] = { -1, -1 }; |
3b925504 LP |
2129 | _cleanup_(rmdir_and_freep) char *t = NULL; |
2130 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
2131 | sd_id128_t machine_id = SD_ID128_NULL; | |
2132 | _cleanup_free_ char *hostname = NULL; | |
2133 | unsigned n_meta_initialized = 0, k; | |
af8219d5 LP |
2134 | int fds[2 * _META_MAX], r, v; |
2135 | ssize_t n; | |
3b925504 LP |
2136 | |
2137 | BLOCK_SIGNALS(SIGCHLD); | |
2138 | ||
2139 | assert(m); | |
2140 | ||
2141 | for (; n_meta_initialized < _META_MAX; n_meta_initialized ++) | |
2142 | if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) { | |
2143 | r = -errno; | |
2144 | goto finish; | |
2145 | } | |
2146 | ||
2147 | r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); | |
2148 | if (r < 0) | |
2149 | goto finish; | |
2150 | ||
af8219d5 LP |
2151 | if (pipe2(error_pipe, O_CLOEXEC) < 0) { |
2152 | r = -errno; | |
2153 | goto finish; | |
2154 | } | |
2155 | ||
e2047ba9 | 2156 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 2157 | if (r < 0) |
3b925504 | 2158 | goto finish; |
be39f6ee | 2159 | if (r == 0) { |
af8219d5 LP |
2160 | error_pipe[0] = safe_close(error_pipe[0]); |
2161 | ||
03bcb6d4 | 2162 | r = dissected_image_mount(m, t, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_VALIDATE_OS); |
429d4e41 | 2163 | if (r < 0) { |
af8219d5 LP |
2164 | /* Let parent know the error */ |
2165 | (void) write(error_pipe[1], &r, sizeof(r)); | |
2166 | ||
429d4e41 | 2167 | log_debug_errno(r, "Failed to mount dissected image: %m"); |
3b925504 | 2168 | _exit(EXIT_FAILURE); |
429d4e41 | 2169 | } |
3b925504 LP |
2170 | |
2171 | for (k = 0; k < _META_MAX; k++) { | |
37e44c3f | 2172 | _cleanup_close_ int fd = -ENOENT; |
3b925504 LP |
2173 | const char *p; |
2174 | ||
2175 | fds[2*k] = safe_close(fds[2*k]); | |
2176 | ||
2177 | NULSTR_FOREACH(p, paths[k]) { | |
36952d19 | 2178 | fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); |
3b925504 LP |
2179 | if (fd >= 0) |
2180 | break; | |
2181 | } | |
36952d19 LP |
2182 | if (fd < 0) { |
2183 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
37e44c3f | 2184 | fds[2*k+1] = safe_close(fds[2*k+1]); |
3b925504 | 2185 | continue; |
36952d19 | 2186 | } |
3b925504 LP |
2187 | |
2188 | r = copy_bytes(fd, fds[2*k+1], (uint64_t) -1, 0); | |
af8219d5 LP |
2189 | if (r < 0) { |
2190 | (void) write(error_pipe[1], &r, sizeof(r)); | |
3b925504 | 2191 | _exit(EXIT_FAILURE); |
af8219d5 | 2192 | } |
3b925504 LP |
2193 | |
2194 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
2195 | } | |
2196 | ||
2197 | _exit(EXIT_SUCCESS); | |
2198 | } | |
2199 | ||
af8219d5 LP |
2200 | error_pipe[1] = safe_close(error_pipe[1]); |
2201 | ||
3b925504 LP |
2202 | for (k = 0; k < _META_MAX; k++) { |
2203 | _cleanup_fclose_ FILE *f = NULL; | |
2204 | ||
2205 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
2206 | ||
4fa744a3 | 2207 | f = take_fdopen(&fds[2*k], "r"); |
3b925504 LP |
2208 | if (!f) { |
2209 | r = -errno; | |
2210 | goto finish; | |
2211 | } | |
2212 | ||
3b925504 LP |
2213 | switch (k) { |
2214 | ||
2215 | case META_HOSTNAME: | |
2216 | r = read_etc_hostname_stream(f, &hostname); | |
2217 | if (r < 0) | |
2218 | log_debug_errno(r, "Failed to read /etc/hostname: %m"); | |
2219 | ||
2220 | break; | |
2221 | ||
2222 | case META_MACHINE_ID: { | |
2223 | _cleanup_free_ char *line = NULL; | |
2224 | ||
2225 | r = read_line(f, LONG_LINE_MAX, &line); | |
2226 | if (r < 0) | |
2227 | log_debug_errno(r, "Failed to read /etc/machine-id: %m"); | |
2228 | else if (r == 33) { | |
2229 | r = sd_id128_from_string(line, &machine_id); | |
2230 | if (r < 0) | |
2231 | log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line); | |
2232 | } else if (r == 0) | |
2233 | log_debug("/etc/machine-id file is empty."); | |
ab763cb2 HS |
2234 | else if (streq(line, "uninitialized")) |
2235 | log_debug("/etc/machine-id file is uninitialized (likely aborted first boot)."); | |
3b925504 LP |
2236 | else |
2237 | log_debug("/etc/machine-id has unexpected length %i.", r); | |
2238 | ||
2239 | break; | |
2240 | } | |
2241 | ||
2242 | case META_MACHINE_INFO: | |
aa8fbc74 | 2243 | r = load_env_file_pairs(f, "machine-info", &machine_info); |
3b925504 LP |
2244 | if (r < 0) |
2245 | log_debug_errno(r, "Failed to read /etc/machine-info: %m"); | |
2246 | ||
2247 | break; | |
2248 | ||
2249 | case META_OS_RELEASE: | |
aa8fbc74 | 2250 | r = load_env_file_pairs(f, "os-release", &os_release); |
3b925504 LP |
2251 | if (r < 0) |
2252 | log_debug_errno(r, "Failed to read OS release file: %m"); | |
2253 | ||
2254 | break; | |
2255 | } | |
2256 | } | |
2257 | ||
2e87a1fd | 2258 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 2259 | child = 0; |
2e87a1fd | 2260 | if (r < 0) |
af8219d5 LP |
2261 | return r; |
2262 | ||
2263 | n = read(error_pipe[0], &v, sizeof(v)); | |
2264 | if (n < 0) | |
2265 | return -errno; | |
2266 | if (n == sizeof(v)) | |
2267 | return v; /* propagate error sent to us from child */ | |
2268 | if (n != 0) | |
2269 | return -EIO; | |
2270 | ||
2e87a1fd LP |
2271 | if (r != EXIT_SUCCESS) |
2272 | return -EPROTO; | |
3b925504 LP |
2273 | |
2274 | free_and_replace(m->hostname, hostname); | |
2275 | m->machine_id = machine_id; | |
2276 | strv_free_and_replace(m->machine_info, machine_info); | |
2277 | strv_free_and_replace(m->os_release, os_release); | |
2278 | ||
2279 | finish: | |
2280 | for (k = 0; k < n_meta_initialized; k++) | |
2281 | safe_close_pair(fds + 2*k); | |
2282 | ||
2283 | return r; | |
2284 | } | |
2285 | ||
4526113f LP |
2286 | int dissect_image_and_warn( |
2287 | int fd, | |
2288 | const char *name, | |
89e62e0b | 2289 | const VeritySettings *verity, |
18d73705 | 2290 | const MountOptions *mount_options, |
4526113f LP |
2291 | DissectImageFlags flags, |
2292 | DissectedImage **ret) { | |
2293 | ||
2294 | _cleanup_free_ char *buffer = NULL; | |
2295 | int r; | |
2296 | ||
2297 | if (!name) { | |
2298 | r = fd_get_path(fd, &buffer); | |
2299 | if (r < 0) | |
2300 | return r; | |
2301 | ||
2302 | name = buffer; | |
2303 | } | |
2304 | ||
89e62e0b | 2305 | r = dissect_image(fd, verity, mount_options, flags, ret); |
4526113f LP |
2306 | switch (r) { |
2307 | ||
2308 | case -EOPNOTSUPP: | |
2309 | return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support."); | |
2310 | ||
2311 | case -ENOPKG: | |
2312 | return log_error_errno(r, "Couldn't identify a suitable partition table or file system in '%s'.", name); | |
2313 | ||
2314 | case -EADDRNOTAVAIL: | |
2315 | return log_error_errno(r, "No root partition for specified root hash found in '%s'.", name); | |
2316 | ||
2317 | case -ENOTUNIQ: | |
2318 | return log_error_errno(r, "Multiple suitable root partitions found in image '%s'.", name); | |
2319 | ||
2320 | case -ENXIO: | |
2321 | return log_error_errno(r, "No suitable root partition found in image '%s'.", name); | |
2322 | ||
2323 | case -EPROTONOSUPPORT: | |
2324 | return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name); | |
2325 | ||
2326 | default: | |
2327 | if (r < 0) | |
2328 | return log_error_errno(r, "Failed to dissect image '%s': %m", name); | |
2329 | ||
2330 | return r; | |
2331 | } | |
2332 | } | |
2333 | ||
569a0e42 | 2334 | bool dissected_image_can_do_verity(const DissectedImage *image, PartitionDesignator partition_designator) { |
e7cbe5cb LB |
2335 | if (image->single_file_system) |
2336 | return partition_designator == PARTITION_ROOT && image->can_verity; | |
2337 | ||
2338 | return PARTITION_VERITY_OF(partition_designator) >= 0; | |
2339 | } | |
2340 | ||
569a0e42 | 2341 | bool dissected_image_has_verity(const DissectedImage *image, PartitionDesignator partition_designator) { |
e7cbe5cb LB |
2342 | int k; |
2343 | ||
2344 | if (image->single_file_system) | |
2345 | return partition_designator == PARTITION_ROOT && image->verity; | |
2346 | ||
2347 | k = PARTITION_VERITY_OF(partition_designator); | |
2348 | return k >= 0 && image->partitions[k].found; | |
2349 | } | |
2350 | ||
18d73705 LB |
2351 | MountOptions* mount_options_free_all(MountOptions *options) { |
2352 | MountOptions *m; | |
2353 | ||
2354 | while ((m = options)) { | |
2355 | LIST_REMOVE(mount_options, options, m); | |
2356 | free(m->options); | |
2357 | free(m); | |
2358 | } | |
2359 | ||
2360 | return NULL; | |
2361 | } | |
2362 | ||
569a0e42 | 2363 | const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator) { |
f5215bc8 | 2364 | const MountOptions *m; |
18d73705 | 2365 | |
f5215bc8 | 2366 | LIST_FOREACH(mount_options, m, options) |
9ece6444 | 2367 | if (designator == m->partition_designator && !isempty(m->options)) |
18d73705 | 2368 | return m->options; |
6aa05ebd | 2369 | |
18d73705 LB |
2370 | return NULL; |
2371 | } | |
2372 | ||
6aa05ebd LP |
2373 | int mount_image_privately_interactively( |
2374 | const char *image, | |
2375 | DissectImageFlags flags, | |
2376 | char **ret_directory, | |
2377 | LoopDevice **ret_loop_device, | |
2378 | DecryptedImage **ret_decrypted_image) { | |
2379 | ||
2380 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; | |
2381 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
2382 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; | |
2383 | _cleanup_(rmdir_and_freep) char *created_dir = NULL; | |
2384 | _cleanup_free_ char *temp = NULL; | |
2385 | int r; | |
2386 | ||
2387 | /* Mounts an OS image at a temporary place, inside a newly created mount namespace of our own. This | |
2388 | * is used by tools such as systemd-tmpfiles or systemd-firstboot to operate on some disk image | |
2389 | * easily. */ | |
2390 | ||
2391 | assert(image); | |
2392 | assert(ret_directory); | |
2393 | assert(ret_loop_device); | |
2394 | assert(ret_decrypted_image); | |
2395 | ||
2396 | r = tempfn_random_child(NULL, program_invocation_short_name, &temp); | |
2397 | if (r < 0) | |
2398 | return log_error_errno(r, "Failed to generate temporary mount directory: %m"); | |
2399 | ||
2400 | r = loop_device_make_by_path( | |
2401 | image, | |
2402 | FLAGS_SET(flags, DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, | |
2403 | FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, | |
2404 | &d); | |
2405 | if (r < 0) | |
2406 | return log_error_errno(r, "Failed to set up loopback device: %m"); | |
2407 | ||
89e62e0b | 2408 | r = dissect_image_and_warn(d->fd, image, NULL, NULL, flags, &dissected_image); |
6aa05ebd LP |
2409 | if (r < 0) |
2410 | return r; | |
2411 | ||
89e62e0b | 2412 | r = dissected_image_decrypt_interactively(dissected_image, NULL, NULL, flags, &decrypted_image); |
6aa05ebd LP |
2413 | if (r < 0) |
2414 | return r; | |
2415 | ||
2416 | r = detach_mount_namespace(); | |
2417 | if (r < 0) | |
2418 | return log_error_errno(r, "Failed to detach mount namespace: %m"); | |
2419 | ||
2420 | r = mkdir_p(temp, 0700); | |
2421 | if (r < 0) | |
2422 | return log_error_errno(r, "Failed to create mount point: %m"); | |
2423 | ||
2424 | created_dir = TAKE_PTR(temp); | |
2425 | ||
af187ab2 | 2426 | r = dissected_image_mount_and_warn(dissected_image, created_dir, UID_INVALID, flags); |
6aa05ebd | 2427 | if (r < 0) |
af187ab2 | 2428 | return r; |
6aa05ebd LP |
2429 | |
2430 | if (decrypted_image) { | |
2431 | r = decrypted_image_relinquish(decrypted_image); | |
2432 | if (r < 0) | |
2433 | return log_error_errno(r, "Failed to relinquish DM devices: %m"); | |
2434 | } | |
2435 | ||
2436 | loop_device_relinquish(d); | |
2437 | ||
2438 | *ret_directory = TAKE_PTR(created_dir); | |
2439 | *ret_loop_device = TAKE_PTR(d); | |
2440 | *ret_decrypted_image = TAKE_PTR(decrypted_image); | |
2441 | ||
2442 | return 0; | |
2443 | } | |
2444 | ||
8c1be37e LP |
2445 | static const char *const partition_designator_table[] = { |
2446 | [PARTITION_ROOT] = "root", | |
2447 | [PARTITION_ROOT_SECONDARY] = "root-secondary", | |
aee36b4e LP |
2448 | [PARTITION_USR] = "usr", |
2449 | [PARTITION_USR_SECONDARY] = "usr-secondary", | |
8c1be37e LP |
2450 | [PARTITION_HOME] = "home", |
2451 | [PARTITION_SRV] = "srv", | |
2452 | [PARTITION_ESP] = "esp", | |
a8c47660 | 2453 | [PARTITION_XBOOTLDR] = "xbootldr", |
8c1be37e | 2454 | [PARTITION_SWAP] = "swap", |
4623e8e6 LP |
2455 | [PARTITION_ROOT_VERITY] = "root-verity", |
2456 | [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity", | |
aee36b4e LP |
2457 | [PARTITION_USR_VERITY] = "usr-verity", |
2458 | [PARTITION_USR_SECONDARY_VERITY] = "usr-secondary-verity", | |
d4dffb85 LP |
2459 | [PARTITION_TMP] = "tmp", |
2460 | [PARTITION_VAR] = "var", | |
8c1be37e LP |
2461 | }; |
2462 | ||
569a0e42 | 2463 | DEFINE_STRING_TABLE_LOOKUP(partition_designator, PartitionDesignator); |