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