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