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