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