]>
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" | |
8c1be37e | 36 | #include "mount-util.h" |
e4de7287 | 37 | #include "mountpoint-util.h" |
d8b4d14d | 38 | #include "nulstr-util.h" |
d58ad743 | 39 | #include "os-util.h" |
8c1be37e | 40 | #include "path-util.h" |
3b925504 LP |
41 | #include "process-util.h" |
42 | #include "raw-clone.h" | |
43 | #include "signal-util.h" | |
8c1be37e | 44 | #include "stat-util.h" |
18b5886e | 45 | #include "stdio-util.h" |
8c1be37e LP |
46 | #include "string-table.h" |
47 | #include "string-util.h" | |
2eedfd2d | 48 | #include "strv.h" |
e4de7287 | 49 | #include "tmpfile-util.h" |
a8040b6d | 50 | #include "udev-util.h" |
2d3a5a73 | 51 | #include "user-util.h" |
41488e1f | 52 | #include "xattr-util.h" |
8c1be37e | 53 | |
c34b75a1 | 54 | int probe_filesystem(const char *node, char **ret_fstype) { |
7cc84b2c | 55 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
5238e957 | 56 | * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an |
7cc84b2c ZJS |
57 | * different error otherwise. */ |
58 | ||
349cc4a5 | 59 | #if HAVE_BLKID |
8e766630 | 60 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
18b5886e LP |
61 | const char *fstype; |
62 | int r; | |
63 | ||
995fa2e5 | 64 | errno = 0; |
18b5886e LP |
65 | b = blkid_new_probe_from_filename(node); |
66 | if (!b) | |
66855de7 | 67 | return errno_or_else(ENOMEM); |
18b5886e LP |
68 | |
69 | blkid_probe_enable_superblocks(b, 1); | |
70 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
71 | ||
72 | errno = 0; | |
73 | r = blkid_do_safeprobe(b); | |
7cc84b2c ZJS |
74 | if (r == 1) { |
75 | log_debug("No type detected on partition %s", node); | |
18b5886e LP |
76 | goto not_found; |
77 | } | |
58dfbfbd LP |
78 | if (r == -2) |
79 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
80 | "Results ambiguous for partition %s", node); | |
b382db9f | 81 | if (r != 0) |
66855de7 | 82 | return errno_or_else(EIO); |
18b5886e LP |
83 | |
84 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
85 | ||
86 | if (fstype) { | |
87 | char *t; | |
88 | ||
89 | t = strdup(fstype); | |
90 | if (!t) | |
91 | return -ENOMEM; | |
92 | ||
93 | *ret_fstype = t; | |
94 | return 1; | |
95 | } | |
96 | ||
97 | not_found: | |
98 | *ret_fstype = NULL; | |
99 | return 0; | |
d1c536f5 ZJS |
100 | #else |
101 | return -EOPNOTSUPP; | |
a75e27eb | 102 | #endif |
d1c536f5 | 103 | } |
18b5886e | 104 | |
40c10d3f | 105 | #if HAVE_BLKID |
cde942f6 JPRV |
106 | /* Detect RPMB and Boot partitions, which are not listed by blkid. |
107 | * See https://github.com/systemd/systemd/issues/5806. */ | |
3c1f2cee | 108 | static bool device_is_mmc_special_partition(sd_device *d) { |
aae22eb3 LP |
109 | const char *sysname; |
110 | ||
f70e7f70 LP |
111 | assert(d); |
112 | ||
3c1f2cee YW |
113 | if (sd_device_get_sysname(d, &sysname) < 0) |
114 | return false; | |
115 | ||
116 | return startswith(sysname, "mmcblk") && | |
0cfa78dd | 117 | (endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1")); |
cde942f6 JPRV |
118 | } |
119 | ||
3c1f2cee | 120 | static bool device_is_block(sd_device *d) { |
aae22eb3 LP |
121 | const char *ss; |
122 | ||
f70e7f70 LP |
123 | assert(d); |
124 | ||
3c1f2cee | 125 | if (sd_device_get_subsystem(d, &ss) < 0) |
aae22eb3 LP |
126 | return false; |
127 | ||
128 | return streq(ss, "block"); | |
129 | } | |
ea887be0 ZJS |
130 | |
131 | static int enumerator_for_parent(sd_device *d, sd_device_enumerator **ret) { | |
132 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
133 | int r; | |
134 | ||
f70e7f70 LP |
135 | assert(d); |
136 | assert(ret); | |
137 | ||
ea887be0 ZJS |
138 | r = sd_device_enumerator_new(&e); |
139 | if (r < 0) | |
140 | return r; | |
141 | ||
142 | r = sd_device_enumerator_allow_uninitialized(e); | |
143 | if (r < 0) | |
144 | return r; | |
145 | ||
146 | r = sd_device_enumerator_add_match_parent(e, d); | |
147 | if (r < 0) | |
148 | return r; | |
149 | ||
150 | *ret = TAKE_PTR(e); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* how many times to wait for the device nodes to appear */ | |
155 | #define N_DEVICE_NODE_LIST_ATTEMPTS 10 | |
156 | ||
157 | static int wait_for_partitions_to_appear( | |
158 | int fd, | |
159 | sd_device *d, | |
160 | unsigned num_partitions, | |
052eaf5c | 161 | DissectImageFlags flags, |
ea887be0 ZJS |
162 | sd_device_enumerator **ret_enumerator) { |
163 | ||
164 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
165 | sd_device *q; | |
166 | unsigned n; | |
167 | int r; | |
168 | ||
f70e7f70 LP |
169 | assert(fd >= 0); |
170 | assert(d); | |
171 | assert(ret_enumerator); | |
172 | ||
ea887be0 ZJS |
173 | r = enumerator_for_parent(d, &e); |
174 | if (r < 0) | |
175 | return r; | |
176 | ||
177 | /* Count the partitions enumerated by the kernel */ | |
178 | n = 0; | |
179 | FOREACH_DEVICE(e, q) { | |
180 | if (sd_device_get_devnum(q, NULL) < 0) | |
181 | continue; | |
182 | if (!device_is_block(q)) | |
183 | continue; | |
184 | if (device_is_mmc_special_partition(q)) | |
185 | continue; | |
186 | ||
052eaf5c | 187 | if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) { |
1b47436e | 188 | r = device_wait_for_initialization(q, "block", USEC_INFINITY, NULL); |
052eaf5c LP |
189 | if (r < 0) |
190 | return r; | |
191 | } | |
a8040b6d | 192 | |
ea887be0 ZJS |
193 | n++; |
194 | } | |
195 | ||
196 | if (n == num_partitions + 1) { | |
197 | *ret_enumerator = TAKE_PTR(e); | |
198 | return 0; /* success! */ | |
199 | } | |
200 | if (n > num_partitions + 1) | |
201 | return log_debug_errno(SYNTHETIC_ERRNO(EIO), | |
202 | "blkid and kernel partition lists do not match."); | |
203 | ||
204 | /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running or it | |
205 | * got EBUSY because udev already opened the device. Let's reprobe the device, which is a synchronous | |
206 | * call that waits until probing is complete. */ | |
207 | ||
208 | for (unsigned j = 0; ; j++) { | |
209 | if (j++ > 20) | |
210 | return -EBUSY; | |
211 | ||
212 | if (ioctl(fd, BLKRRPART, 0) >= 0) | |
213 | break; | |
214 | r = -errno; | |
215 | if (r == -EINVAL) { | |
216 | struct loop_info64 info; | |
217 | ||
218 | /* If we are running on a loop device that has partition scanning off, return | |
219 | * an explicit recognizable error about this, so that callers can generate a | |
220 | * proper message explaining the situation. */ | |
221 | ||
10c1b188 LP |
222 | if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0) { |
223 | #if HAVE_VALGRIND_MEMCHECK_H | |
224 | /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */ | |
225 | VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); | |
226 | #endif | |
227 | ||
228 | if ((info.lo_flags & LO_FLAGS_PARTSCAN) == 0) | |
229 | return log_debug_errno(EPROTONOSUPPORT, | |
230 | "Device is a loop device and partition scanning is off!"); | |
ea887be0 ZJS |
231 | } |
232 | } | |
233 | if (r != -EBUSY) | |
234 | return r; | |
235 | ||
236 | /* If something else has the device open, such as an udev rule, the ioctl will return | |
237 | * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a bit, | |
238 | * and try again. | |
239 | * | |
240 | * This is really something they should fix in the kernel! */ | |
241 | (void) usleep(50 * USEC_PER_MSEC); | |
242 | ||
243 | } | |
244 | ||
245 | return -EAGAIN; /* no success yet, try again */ | |
246 | } | |
247 | ||
248 | static int loop_wait_for_partitions_to_appear( | |
249 | int fd, | |
250 | sd_device *d, | |
251 | unsigned num_partitions, | |
052eaf5c | 252 | DissectImageFlags flags, |
ea887be0 | 253 | sd_device_enumerator **ret_enumerator) { |
a8040b6d | 254 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
ea887be0 ZJS |
255 | int r; |
256 | ||
f70e7f70 LP |
257 | assert(fd >= 0); |
258 | assert(d); | |
259 | assert(ret_enumerator); | |
260 | ||
b887c8b8 ZJS |
261 | log_debug("Waiting for device (parent + %d partitions) to appear...", num_partitions); |
262 | ||
052eaf5c | 263 | if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) { |
1b47436e | 264 | r = device_wait_for_initialization(d, "block", USEC_INFINITY, &device); |
052eaf5c LP |
265 | if (r < 0) |
266 | return r; | |
267 | } else | |
268 | device = sd_device_ref(d); | |
a8040b6d | 269 | |
ea887be0 | 270 | for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { |
052eaf5c | 271 | r = wait_for_partitions_to_appear(fd, device, num_partitions, flags, ret_enumerator); |
ea887be0 ZJS |
272 | if (r != -EAGAIN) |
273 | return r; | |
274 | } | |
275 | ||
276 | return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), | |
277 | "Kernel partitions dit not appear within %d attempts", | |
278 | N_DEVICE_NODE_LIST_ATTEMPTS); | |
279 | } | |
280 | ||
0f7c9a3d LP |
281 | static void check_partition_flags( |
282 | const char *node, | |
283 | unsigned long long pflags, | |
284 | unsigned long long supported) { | |
285 | ||
286 | assert(node); | |
287 | ||
288 | /* Mask away all flags supported by this partition's type and the three flags the UEFI spec defines generically */ | |
289 | pflags &= ~(supported | GPT_FLAG_REQUIRED_PARTITION | GPT_FLAG_NO_BLOCK_IO_PROTOCOL | GPT_FLAG_LEGACY_BIOS_BOOTABLE); | |
290 | ||
291 | if (pflags == 0) | |
292 | return; | |
293 | ||
294 | /* If there are other bits set, then log about it, to make things discoverable */ | |
295 | for (unsigned i = 0; i < sizeof(pflags) * 8; i++) { | |
296 | unsigned long long bit = 1ULL << i; | |
297 | if (!FLAGS_SET(pflags, bit)) | |
298 | continue; | |
299 | ||
300 | log_debug("Unexpected partition flag %llu set on %s!", bit, node); | |
301 | } | |
302 | } | |
303 | ||
40c10d3f | 304 | #endif |
aae22eb3 | 305 | |
4526113f LP |
306 | int dissect_image( |
307 | int fd, | |
308 | const void *root_hash, | |
309 | size_t root_hash_size, | |
e7cbe5cb | 310 | const char *verity_data, |
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")) { | |
403 | _cleanup_free_ char *t = NULL, *n = NULL; | |
404 | const char *fstype = 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 | ||
9b6deb03 LP |
423 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
424 | .found = true, | |
e7cbe5cb | 425 | .rw = !m->verity, |
9b6deb03 LP |
426 | .partno = -1, |
427 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
428 | .fstype = TAKE_PTR(t), |
429 | .node = TAKE_PTR(n), | |
9b6deb03 | 430 | }; |
8c1be37e | 431 | |
4db1879a | 432 | m->encrypted = streq_ptr(fstype, "crypto_LUKS"); |
18b5886e | 433 | |
b1806441 LB |
434 | /* Even on a single partition we need to wait for udev to create the |
435 | * /dev/block/X:Y symlink to /dev/loopZ */ | |
436 | r = loop_wait_for_partitions_to_appear(fd, d, 0, flags, &e); | |
437 | if (r < 0) | |
438 | return r; | |
1cc6c93a | 439 | *ret = TAKE_PTR(m); |
8c1be37e | 440 | |
9b6deb03 LP |
441 | return 0; |
442 | } | |
8c1be37e LP |
443 | } |
444 | ||
445 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
446 | if (!pttype) | |
447 | return -ENOPKG; | |
448 | ||
449 | is_gpt = streq_ptr(pttype, "gpt"); | |
450 | is_mbr = streq_ptr(pttype, "dos"); | |
451 | ||
9b6deb03 | 452 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
453 | return -ENOPKG; |
454 | ||
455 | errno = 0; | |
456 | pl = blkid_probe_get_partitions(b); | |
b382db9f | 457 | if (!pl) |
66855de7 | 458 | return errno_or_else(ENOMEM); |
8c1be37e | 459 | |
052eaf5c | 460 | r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), flags, &e); |
ea887be0 ZJS |
461 | if (r < 0) |
462 | return r; | |
8c1be37e | 463 | |
8437c059 | 464 | FOREACH_DEVICE(e, q) { |
9b6deb03 | 465 | unsigned long long pflags; |
8c1be37e | 466 | blkid_partition pp; |
cde942f6 | 467 | const char *node; |
8c1be37e LP |
468 | dev_t qn; |
469 | int nr; | |
470 | ||
3c1f2cee YW |
471 | r = sd_device_get_devnum(q, &qn); |
472 | if (r < 0) | |
8c1be37e LP |
473 | continue; |
474 | ||
475 | if (st.st_rdev == qn) | |
476 | continue; | |
477 | ||
aae22eb3 LP |
478 | if (!device_is_block(q)) |
479 | continue; | |
480 | ||
cde942f6 | 481 | if (device_is_mmc_special_partition(q)) |
7be1420f LP |
482 | continue; |
483 | ||
3c1f2cee YW |
484 | r = sd_device_get_devname(q, &node); |
485 | if (r < 0) | |
8c1be37e LP |
486 | continue; |
487 | ||
488 | pp = blkid_partlist_devno_to_partition(pl, qn); | |
489 | if (!pp) | |
490 | continue; | |
491 | ||
9b6deb03 | 492 | pflags = blkid_partition_get_flags(pp); |
8c1be37e LP |
493 | |
494 | nr = blkid_partition_get_partno(pp); | |
495 | if (nr < 0) | |
496 | continue; | |
497 | ||
498 | if (is_gpt) { | |
499 | int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID; | |
4623e8e6 LP |
500 | const char *stype, *sid, *fstype = NULL; |
501 | sd_id128_t type_id, id; | |
8c1be37e LP |
502 | bool rw = true; |
503 | ||
4623e8e6 LP |
504 | sid = blkid_partition_get_uuid(pp); |
505 | if (!sid) | |
506 | continue; | |
507 | if (sd_id128_from_string(sid, &id) < 0) | |
508 | continue; | |
509 | ||
8c1be37e LP |
510 | stype = blkid_partition_get_type_string(pp); |
511 | if (!stype) | |
512 | continue; | |
8c1be37e LP |
513 | if (sd_id128_from_string(stype, &type_id) < 0) |
514 | continue; | |
515 | ||
516 | if (sd_id128_equal(type_id, GPT_HOME)) { | |
a48dd347 | 517 | |
0f7c9a3d LP |
518 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
519 | ||
a48dd347 LP |
520 | if (pflags & GPT_FLAG_NO_AUTO) |
521 | continue; | |
522 | ||
8c1be37e | 523 | designator = PARTITION_HOME; |
9b6deb03 | 524 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
8c1be37e | 525 | } else if (sd_id128_equal(type_id, GPT_SRV)) { |
a48dd347 | 526 | |
0f7c9a3d LP |
527 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
528 | ||
a48dd347 LP |
529 | if (pflags & GPT_FLAG_NO_AUTO) |
530 | continue; | |
531 | ||
8c1be37e | 532 | designator = PARTITION_SRV; |
9b6deb03 | 533 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
8c1be37e | 534 | } else if (sd_id128_equal(type_id, GPT_ESP)) { |
a48dd347 LP |
535 | |
536 | /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined | |
537 | * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the | |
538 | * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */ | |
539 | ||
540 | if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL) | |
541 | continue; | |
542 | ||
8c1be37e LP |
543 | designator = PARTITION_ESP; |
544 | fstype = "vfat"; | |
a8c47660 LP |
545 | |
546 | } else if (sd_id128_equal(type_id, GPT_XBOOTLDR)) { | |
547 | ||
0f7c9a3d LP |
548 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
549 | ||
a8c47660 LP |
550 | if (pflags & GPT_FLAG_NO_AUTO) |
551 | continue; | |
552 | ||
553 | designator = PARTITION_XBOOTLDR; | |
554 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
8c1be37e LP |
555 | } |
556 | #ifdef GPT_ROOT_NATIVE | |
557 | else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) { | |
4623e8e6 | 558 | |
0f7c9a3d LP |
559 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
560 | ||
a48dd347 LP |
561 | if (pflags & GPT_FLAG_NO_AUTO) |
562 | continue; | |
563 | ||
4623e8e6 LP |
564 | /* If a root ID is specified, ignore everything but the root id */ |
565 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
566 | continue; | |
567 | ||
8c1be37e LP |
568 | designator = PARTITION_ROOT; |
569 | architecture = native_architecture(); | |
9b6deb03 | 570 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
4f8b86e3 | 571 | } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) { |
4623e8e6 | 572 | |
0f7c9a3d LP |
573 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
574 | ||
a48dd347 LP |
575 | if (pflags & GPT_FLAG_NO_AUTO) |
576 | continue; | |
577 | ||
4623e8e6 LP |
578 | m->can_verity = true; |
579 | ||
580 | /* Ignore verity unless a root hash is specified */ | |
581 | if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id)) | |
582 | continue; | |
583 | ||
584 | designator = PARTITION_ROOT_VERITY; | |
585 | fstype = "DM_verity_hash"; | |
586 | architecture = native_architecture(); | |
587 | rw = false; | |
588 | } | |
589 | #endif | |
8c1be37e LP |
590 | #ifdef GPT_ROOT_SECONDARY |
591 | else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) { | |
4623e8e6 | 592 | |
0f7c9a3d LP |
593 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
594 | ||
a48dd347 LP |
595 | if (pflags & GPT_FLAG_NO_AUTO) |
596 | continue; | |
597 | ||
4623e8e6 LP |
598 | /* If a root ID is specified, ignore everything but the root id */ |
599 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
600 | continue; | |
601 | ||
8c1be37e LP |
602 | designator = PARTITION_ROOT_SECONDARY; |
603 | architecture = SECONDARY_ARCHITECTURE; | |
9b6deb03 | 604 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
4f8b86e3 | 605 | } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) { |
a48dd347 | 606 | |
0f7c9a3d LP |
607 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
608 | ||
a48dd347 LP |
609 | if (pflags & GPT_FLAG_NO_AUTO) |
610 | continue; | |
611 | ||
4623e8e6 LP |
612 | m->can_verity = true; |
613 | ||
614 | /* Ignore verity unless root has is specified */ | |
615 | if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id)) | |
616 | continue; | |
617 | ||
618 | designator = PARTITION_ROOT_SECONDARY_VERITY; | |
619 | fstype = "DM_verity_hash"; | |
620 | architecture = SECONDARY_ARCHITECTURE; | |
621 | rw = false; | |
622 | } | |
8c1be37e LP |
623 | #endif |
624 | else if (sd_id128_equal(type_id, GPT_SWAP)) { | |
a48dd347 | 625 | |
0f7c9a3d LP |
626 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO); |
627 | ||
a48dd347 LP |
628 | if (pflags & GPT_FLAG_NO_AUTO) |
629 | continue; | |
630 | ||
8c1be37e LP |
631 | designator = PARTITION_SWAP; |
632 | fstype = "swap"; | |
633 | } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) { | |
634 | ||
0f7c9a3d LP |
635 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
636 | ||
a48dd347 LP |
637 | if (pflags & GPT_FLAG_NO_AUTO) |
638 | continue; | |
639 | ||
8c1be37e LP |
640 | if (generic_node) |
641 | multiple_generic = true; | |
642 | else { | |
643 | generic_nr = nr; | |
9b6deb03 | 644 | generic_rw = !(pflags & GPT_FLAG_READ_ONLY); |
be30ad41 | 645 | generic_uuid = id; |
8c1be37e LP |
646 | generic_node = strdup(node); |
647 | if (!generic_node) | |
648 | return -ENOMEM; | |
649 | } | |
d4dffb85 LP |
650 | |
651 | } else if (sd_id128_equal(type_id, GPT_TMP)) { | |
652 | ||
0f7c9a3d LP |
653 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
654 | ||
d4dffb85 LP |
655 | if (pflags & GPT_FLAG_NO_AUTO) |
656 | continue; | |
657 | ||
658 | designator = PARTITION_TMP; | |
659 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
660 | ||
661 | } else if (sd_id128_equal(type_id, GPT_VAR)) { | |
662 | ||
0f7c9a3d LP |
663 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
664 | ||
d4dffb85 LP |
665 | if (pflags & GPT_FLAG_NO_AUTO) |
666 | continue; | |
667 | ||
668 | if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) { | |
669 | sd_id128_t var_uuid; | |
670 | ||
671 | /* For /var we insist that the uuid of the partition matches the | |
672 | * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine | |
673 | * ID. Why? Unlike the other partitions /var is inherently | |
674 | * installation specific, hence we need to be careful not to mount it | |
675 | * in the wrong installation. By hashing the partition UUID from | |
676 | * /etc/machine-id we can securely bind the partition to the | |
677 | * installation. */ | |
678 | ||
679 | r = sd_id128_get_machine_app_specific(GPT_VAR, &var_uuid); | |
680 | if (r < 0) | |
681 | return r; | |
682 | ||
683 | if (!sd_id128_equal(var_uuid, id)) { | |
684 | log_debug("Found a /var/ partition, but its UUID didn't match our expectations, ignoring."); | |
685 | continue; | |
686 | } | |
687 | } | |
688 | ||
689 | designator = PARTITION_VAR; | |
690 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
8c1be37e LP |
691 | } |
692 | ||
693 | if (designator != _PARTITION_DESIGNATOR_INVALID) { | |
694 | _cleanup_free_ char *t = NULL, *n = NULL; | |
695 | ||
696 | /* First one wins */ | |
697 | if (m->partitions[designator].found) | |
698 | continue; | |
699 | ||
700 | if (fstype) { | |
701 | t = strdup(fstype); | |
702 | if (!t) | |
703 | return -ENOMEM; | |
704 | } | |
705 | ||
706 | n = strdup(node); | |
707 | if (!n) | |
708 | return -ENOMEM; | |
709 | ||
710 | m->partitions[designator] = (DissectedPartition) { | |
711 | .found = true, | |
712 | .partno = nr, | |
713 | .rw = rw, | |
714 | .architecture = architecture, | |
1cc6c93a YW |
715 | .node = TAKE_PTR(n), |
716 | .fstype = TAKE_PTR(t), | |
be30ad41 | 717 | .uuid = id, |
8c1be37e | 718 | }; |
8c1be37e LP |
719 | } |
720 | ||
721 | } else if (is_mbr) { | |
722 | ||
a8c47660 | 723 | switch (blkid_partition_get_type(pp)) { |
8c1be37e | 724 | |
a8c47660 LP |
725 | case 0x83: /* Linux partition */ |
726 | ||
727 | if (pflags != 0x80) /* Bootable flag */ | |
728 | continue; | |
8c1be37e | 729 | |
a8c47660 LP |
730 | if (generic_node) |
731 | multiple_generic = true; | |
732 | else { | |
733 | generic_nr = nr; | |
734 | generic_rw = true; | |
735 | generic_node = strdup(node); | |
736 | if (!generic_node) | |
737 | return -ENOMEM; | |
738 | } | |
739 | ||
740 | break; | |
741 | ||
742 | case 0xEA: { /* Boot Loader Spec extended $BOOT partition */ | |
743 | _cleanup_free_ char *n = NULL; | |
744 | sd_id128_t id = SD_ID128_NULL; | |
745 | const char *sid; | |
746 | ||
747 | /* First one wins */ | |
748 | if (m->partitions[PARTITION_XBOOTLDR].found) | |
749 | continue; | |
750 | ||
751 | sid = blkid_partition_get_uuid(pp); | |
752 | if (sid) | |
753 | (void) sd_id128_from_string(sid, &id); | |
754 | ||
755 | n = strdup(node); | |
756 | if (!n) | |
8c1be37e | 757 | return -ENOMEM; |
a8c47660 LP |
758 | |
759 | m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) { | |
760 | .found = true, | |
761 | .partno = nr, | |
762 | .rw = true, | |
763 | .architecture = _ARCHITECTURE_INVALID, | |
764 | .node = TAKE_PTR(n), | |
765 | .uuid = id, | |
766 | }; | |
767 | ||
768 | break; | |
769 | }} | |
8c1be37e LP |
770 | } |
771 | } | |
772 | ||
773 | if (!m->partitions[PARTITION_ROOT].found) { | |
774 | /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not | |
775 | * either, then check if there's a single generic one, and use that. */ | |
776 | ||
4623e8e6 | 777 | if (m->partitions[PARTITION_ROOT_VERITY].found) |
e0f9e7bd | 778 | return -EADDRNOTAVAIL; |
4623e8e6 | 779 | |
8c1be37e LP |
780 | if (m->partitions[PARTITION_ROOT_SECONDARY].found) { |
781 | m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY]; | |
782 | zero(m->partitions[PARTITION_ROOT_SECONDARY]); | |
4623e8e6 LP |
783 | |
784 | m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY]; | |
785 | zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); | |
786 | ||
e0f9e7bd LP |
787 | } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) { |
788 | ||
789 | /* If the root has was set, then we won't fallback to a generic node, because the root hash | |
790 | * decides */ | |
791 | if (root_hash) | |
792 | return -EADDRNOTAVAIL; | |
8c1be37e | 793 | |
e0f9e7bd LP |
794 | /* If we didn't find a generic node, then we can't fix this up either */ |
795 | if (!generic_node) | |
796 | return -ENXIO; | |
797 | ||
798 | /* If we didn't find a properly marked root partition, but we did find a single suitable | |
799 | * generic Linux partition, then use this as root partition, if the caller asked for it. */ | |
8c1be37e LP |
800 | if (multiple_generic) |
801 | return -ENOTUNIQ; | |
802 | ||
803 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { | |
804 | .found = true, | |
805 | .rw = generic_rw, | |
806 | .partno = generic_nr, | |
807 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a | 808 | .node = TAKE_PTR(generic_node), |
be30ad41 | 809 | .uuid = generic_uuid, |
8c1be37e | 810 | }; |
e0f9e7bd | 811 | } |
8c1be37e LP |
812 | } |
813 | ||
4623e8e6 | 814 | if (root_hash) { |
e0f9e7bd | 815 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) |
4623e8e6 LP |
816 | return -EADDRNOTAVAIL; |
817 | ||
818 | /* If we found the primary root with the hash, then we definitely want to suppress any secondary root | |
819 | * (which would be weird, after all the root hash should only be assigned to one pair of | |
820 | * partitions... */ | |
821 | m->partitions[PARTITION_ROOT_SECONDARY].found = false; | |
822 | m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false; | |
823 | ||
824 | /* If we found a verity setup, then the root partition is necessarily read-only. */ | |
825 | m->partitions[PARTITION_ROOT].rw = false; | |
826 | ||
827 | m->verity = true; | |
828 | } | |
829 | ||
18b5886e LP |
830 | blkid_free_probe(b); |
831 | b = NULL; | |
832 | ||
8c1be37e LP |
833 | /* Fill in file system types if we don't know them yet. */ |
834 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
18b5886e | 835 | DissectedPartition *p = m->partitions + i; |
8c1be37e | 836 | |
18b5886e | 837 | if (!p->found) |
8c1be37e LP |
838 | continue; |
839 | ||
18b5886e LP |
840 | if (!p->fstype && p->node) { |
841 | r = probe_filesystem(p->node, &p->fstype); | |
7cc84b2c | 842 | if (r < 0 && r != -EUCLEAN) |
18b5886e | 843 | return r; |
8c1be37e LP |
844 | } |
845 | ||
18b5886e LP |
846 | if (streq_ptr(p->fstype, "crypto_LUKS")) |
847 | m->encrypted = true; | |
896f937f LP |
848 | |
849 | if (p->fstype && fstype_is_ro(p->fstype)) | |
850 | p->rw = false; | |
8c1be37e LP |
851 | } |
852 | ||
1cc6c93a | 853 | *ret = TAKE_PTR(m); |
8c1be37e LP |
854 | |
855 | return 0; | |
856 | #else | |
857 | return -EOPNOTSUPP; | |
858 | #endif | |
859 | } | |
860 | ||
861 | DissectedImage* dissected_image_unref(DissectedImage *m) { | |
862 | unsigned i; | |
863 | ||
864 | if (!m) | |
865 | return NULL; | |
866 | ||
867 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
868 | free(m->partitions[i].fstype); | |
869 | free(m->partitions[i].node); | |
18b5886e LP |
870 | free(m->partitions[i].decrypted_fstype); |
871 | free(m->partitions[i].decrypted_node); | |
8c1be37e LP |
872 | } |
873 | ||
3b925504 LP |
874 | free(m->hostname); |
875 | strv_free(m->machine_info); | |
876 | strv_free(m->os_release); | |
877 | ||
5fecf46d | 878 | return mfree(m); |
8c1be37e LP |
879 | } |
880 | ||
18b5886e | 881 | static int is_loop_device(const char *path) { |
553e15f2 | 882 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
883 | struct stat st; |
884 | ||
885 | assert(path); | |
886 | ||
887 | if (stat(path, &st) < 0) | |
888 | return -errno; | |
889 | ||
890 | if (!S_ISBLK(st.st_mode)) | |
891 | return -ENOTBLK; | |
892 | ||
553e15f2 | 893 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
894 | if (access(s, F_OK) < 0) { |
895 | if (errno != ENOENT) | |
896 | return -errno; | |
897 | ||
898 | /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */ | |
553e15f2 | 899 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
900 | if (access(s, F_OK) < 0) |
901 | return errno == ENOENT ? false : -errno; | |
902 | } | |
903 | ||
904 | return true; | |
905 | } | |
906 | ||
cf32c486 LP |
907 | static int run_fsck(const char *node, const char *fstype) { |
908 | int r, exit_status; | |
909 | pid_t pid; | |
910 | ||
911 | assert(node); | |
912 | assert(fstype); | |
913 | ||
914 | r = fsck_exists(fstype); | |
915 | if (r < 0) { | |
916 | log_debug_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", fstype); | |
917 | return 0; | |
918 | } | |
919 | if (r == 0) { | |
920 | log_debug("Not checking partition %s, as fsck for %s does not exist.", node, fstype); | |
921 | return 0; | |
922 | } | |
923 | ||
924 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_NULL_STDIO, &pid); | |
925 | if (r < 0) | |
926 | return log_debug_errno(r, "Failed to fork off fsck: %m"); | |
927 | if (r == 0) { | |
928 | /* Child */ | |
929 | execl("/sbin/fsck", "/sbin/fsck", "-aT", node, NULL); | |
930 | log_debug_errno(errno, "Failed to execl() fsck: %m"); | |
931 | _exit(FSCK_OPERATIONAL_ERROR); | |
932 | } | |
933 | ||
934 | exit_status = wait_for_terminate_and_check("fsck", pid, 0); | |
935 | if (exit_status < 0) | |
936 | return log_debug_errno(exit_status, "Failed to fork off /sbin/fsck: %m"); | |
937 | ||
938 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { | |
939 | log_debug("fsck failed with exit status %i.", exit_status); | |
940 | ||
941 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
942 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), "File system is corrupted, refusing."); | |
943 | ||
944 | log_debug("Ignoring fsck error."); | |
945 | } | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
18b5886e LP |
950 | static int mount_partition( |
951 | DissectedPartition *m, | |
952 | const char *where, | |
953 | const char *directory, | |
2d3a5a73 | 954 | uid_t uid_shift, |
18b5886e LP |
955 | DissectImageFlags flags) { |
956 | ||
2d3a5a73 LP |
957 | _cleanup_free_ char *chased = NULL, *options = NULL; |
958 | const char *p, *node, *fstype; | |
8c1be37e | 959 | bool rw; |
2eedfd2d | 960 | int r; |
8c1be37e LP |
961 | |
962 | assert(m); | |
963 | assert(where); | |
964 | ||
18b5886e LP |
965 | node = m->decrypted_node ?: m->node; |
966 | fstype = m->decrypted_fstype ?: m->fstype; | |
967 | ||
968 | if (!m->found || !node || !fstype) | |
8c1be37e LP |
969 | return 0; |
970 | ||
18b5886e LP |
971 | /* Stacked encryption? Yuck */ |
972 | if (streq_ptr(fstype, "crypto_LUKS")) | |
973 | return -ELOOP; | |
974 | ||
975 | rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY); | |
8c1be37e | 976 | |
cf32c486 LP |
977 | if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) { |
978 | r = run_fsck(node, fstype); | |
979 | if (r < 0) | |
980 | return r; | |
981 | } | |
982 | ||
2eedfd2d | 983 | if (directory) { |
a5648b80 | 984 | r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased, NULL); |
2eedfd2d LP |
985 | if (r < 0) |
986 | return r; | |
987 | ||
988 | p = chased; | |
989 | } else | |
8c1be37e LP |
990 | p = where; |
991 | ||
18b5886e | 992 | /* If requested, turn on discard support. */ |
154d2269 | 993 | if (fstype_can_discard(fstype) && |
18b5886e | 994 | ((flags & DISSECT_IMAGE_DISCARD) || |
2d3a5a73 LP |
995 | ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node)))) { |
996 | options = strdup("discard"); | |
997 | if (!options) | |
998 | return -ENOMEM; | |
999 | } | |
1000 | ||
1001 | if (uid_is_valid(uid_shift) && uid_shift != 0 && fstype_can_uid_gid(fstype)) { | |
1002 | _cleanup_free_ char *uid_option = NULL; | |
1003 | ||
1004 | if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) | |
1005 | return -ENOMEM; | |
1006 | ||
1007 | if (!strextend_with_separator(&options, ",", uid_option, NULL)) | |
1008 | return -ENOMEM; | |
1009 | } | |
8c1be37e | 1010 | |
d9223c07 LP |
1011 | r = mount_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options); |
1012 | if (r < 0) | |
1013 | return r; | |
1014 | ||
1015 | return 1; | |
8c1be37e LP |
1016 | } |
1017 | ||
2d3a5a73 | 1018 | int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) { |
d9223c07 | 1019 | int r, boot_mounted; |
8c1be37e LP |
1020 | |
1021 | assert(m); | |
1022 | assert(where); | |
1023 | ||
1024 | if (!m->partitions[PARTITION_ROOT].found) | |
1025 | return -ENXIO; | |
1026 | ||
2d3a5a73 LP |
1027 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { |
1028 | r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, flags); | |
1029 | if (r < 0) | |
1030 | return r; | |
03bcb6d4 LP |
1031 | |
1032 | if (flags & DISSECT_IMAGE_VALIDATE_OS) { | |
1033 | r = path_is_os_tree(where); | |
1034 | if (r < 0) | |
1035 | return r; | |
1036 | if (r == 0) | |
1037 | return -EMEDIUMTYPE; | |
1038 | } | |
2d3a5a73 LP |
1039 | } |
1040 | ||
705727fd | 1041 | if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY) |
2d3a5a73 | 1042 | return 0; |
8c1be37e | 1043 | |
2d3a5a73 | 1044 | r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags); |
8c1be37e LP |
1045 | if (r < 0) |
1046 | return r; | |
1047 | ||
2d3a5a73 | 1048 | r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, flags); |
8c1be37e LP |
1049 | if (r < 0) |
1050 | return r; | |
1051 | ||
d4dffb85 LP |
1052 | r = mount_partition(m->partitions + PARTITION_VAR, where, "/var", uid_shift, flags); |
1053 | if (r < 0) | |
1054 | return r; | |
1055 | ||
1056 | r = mount_partition(m->partitions + PARTITION_TMP, where, "/var/tmp", uid_shift, flags); | |
1057 | if (r < 0) | |
1058 | return r; | |
1059 | ||
d9223c07 LP |
1060 | boot_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, flags); |
1061 | if (boot_mounted < 0) | |
1062 | return boot_mounted; | |
1063 | ||
8c1be37e | 1064 | if (m->partitions[PARTITION_ESP].found) { |
d9223c07 LP |
1065 | /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it |
1066 | * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */ | |
8c1be37e | 1067 | |
a5648b80 | 1068 | r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL); |
d9223c07 LP |
1069 | if (r >= 0) { |
1070 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, flags); | |
1071 | if (r < 0) | |
1072 | return r; | |
8c1be37e | 1073 | |
d9223c07 | 1074 | } else if (boot_mounted <= 0) { |
2eedfd2d LP |
1075 | _cleanup_free_ char *p = NULL; |
1076 | ||
a5648b80 | 1077 | r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p, NULL); |
d9223c07 LP |
1078 | if (r >= 0 && dir_is_empty(p) > 0) { |
1079 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, flags); | |
2eedfd2d LP |
1080 | if (r < 0) |
1081 | return r; | |
1082 | } | |
8c1be37e LP |
1083 | } |
1084 | } | |
1085 | ||
1086 | return 0; | |
1087 | } | |
1088 | ||
349cc4a5 | 1089 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1090 | typedef struct DecryptedPartition { |
1091 | struct crypt_device *device; | |
1092 | char *name; | |
1093 | bool relinquished; | |
1094 | } DecryptedPartition; | |
1095 | ||
1096 | struct DecryptedImage { | |
1097 | DecryptedPartition *decrypted; | |
1098 | size_t n_decrypted; | |
1099 | size_t n_allocated; | |
1100 | }; | |
1101 | #endif | |
1102 | ||
1103 | DecryptedImage* decrypted_image_unref(DecryptedImage* d) { | |
349cc4a5 | 1104 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1105 | size_t i; |
1106 | int r; | |
1107 | ||
1108 | if (!d) | |
1109 | return NULL; | |
1110 | ||
1111 | for (i = 0; i < d->n_decrypted; i++) { | |
1112 | DecryptedPartition *p = d->decrypted + i; | |
1113 | ||
1114 | if (p->device && p->name && !p->relinquished) { | |
1115 | r = crypt_deactivate(p->device, p->name); | |
1116 | if (r < 0) | |
1117 | log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name); | |
1118 | } | |
1119 | ||
1120 | if (p->device) | |
1121 | crypt_free(p->device); | |
1122 | free(p->name); | |
1123 | } | |
1124 | ||
1125 | free(d); | |
1126 | #endif | |
1127 | return NULL; | |
1128 | } | |
1129 | ||
349cc4a5 | 1130 | #if HAVE_LIBCRYPTSETUP |
4623e8e6 LP |
1131 | |
1132 | static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) { | |
1133 | _cleanup_free_ char *name = NULL, *node = NULL; | |
1134 | const char *base; | |
1135 | ||
1136 | assert(original_node); | |
1137 | assert(suffix); | |
1138 | assert(ret_name); | |
1139 | assert(ret_node); | |
1140 | ||
1141 | base = strrchr(original_node, '/'); | |
1142 | if (!base) | |
1143 | return -EINVAL; | |
1144 | base++; | |
1145 | if (isempty(base)) | |
1146 | return -EINVAL; | |
1147 | ||
1148 | name = strjoin(base, suffix); | |
1149 | if (!name) | |
1150 | return -ENOMEM; | |
1151 | if (!filename_is_valid(name)) | |
1152 | return -EINVAL; | |
1153 | ||
657ee2d8 | 1154 | node = path_join(crypt_get_dir(), name); |
4623e8e6 LP |
1155 | if (!node) |
1156 | return -ENOMEM; | |
1157 | ||
1cc6c93a YW |
1158 | *ret_name = TAKE_PTR(name); |
1159 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 1160 | |
4623e8e6 LP |
1161 | return 0; |
1162 | } | |
1163 | ||
18b5886e LP |
1164 | static int decrypt_partition( |
1165 | DissectedPartition *m, | |
1166 | const char *passphrase, | |
1167 | DissectImageFlags flags, | |
1168 | DecryptedImage *d) { | |
1169 | ||
1170 | _cleanup_free_ char *node = NULL, *name = NULL; | |
294bd454 | 1171 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
18b5886e LP |
1172 | int r; |
1173 | ||
1174 | assert(m); | |
1175 | assert(d); | |
1176 | ||
1177 | if (!m->found || !m->node || !m->fstype) | |
1178 | return 0; | |
1179 | ||
1180 | if (!streq(m->fstype, "crypto_LUKS")) | |
1181 | return 0; | |
1182 | ||
bdd73ac5 ZJS |
1183 | if (!passphrase) |
1184 | return -ENOKEY; | |
1185 | ||
4623e8e6 LP |
1186 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
1187 | if (r < 0) | |
1188 | return r; | |
18b5886e LP |
1189 | |
1190 | if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1)) | |
1191 | return -ENOMEM; | |
1192 | ||
1193 | r = crypt_init(&cd, m->node); | |
1194 | if (r < 0) | |
715cbb81 | 1195 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 1196 | |
1887032f TM |
1197 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); |
1198 | ||
dd59868b | 1199 | r = crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
1200 | if (r < 0) |
1201 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e LP |
1202 | |
1203 | r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), | |
1204 | ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | | |
1205 | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); | |
294bd454 | 1206 | if (r < 0) { |
715cbb81 | 1207 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 1208 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 1209 | } |
18b5886e | 1210 | |
1cc6c93a YW |
1211 | d->decrypted[d->n_decrypted].name = TAKE_PTR(name); |
1212 | d->decrypted[d->n_decrypted].device = TAKE_PTR(cd); | |
18b5886e LP |
1213 | d->n_decrypted++; |
1214 | ||
1cc6c93a | 1215 | m->decrypted_node = TAKE_PTR(node); |
18b5886e LP |
1216 | |
1217 | return 0; | |
4623e8e6 LP |
1218 | } |
1219 | ||
1220 | static int verity_partition( | |
1221 | DissectedPartition *m, | |
1222 | DissectedPartition *v, | |
1223 | const void *root_hash, | |
1224 | size_t root_hash_size, | |
e7cbe5cb | 1225 | const char *verity_data, |
4623e8e6 LP |
1226 | DissectImageFlags flags, |
1227 | DecryptedImage *d) { | |
1228 | ||
1229 | _cleanup_free_ char *node = NULL, *name = NULL; | |
294bd454 | 1230 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
4623e8e6 LP |
1231 | int r; |
1232 | ||
1233 | assert(m); | |
e7cbe5cb | 1234 | assert(v || verity_data); |
4623e8e6 LP |
1235 | |
1236 | if (!root_hash) | |
1237 | return 0; | |
1238 | ||
1239 | if (!m->found || !m->node || !m->fstype) | |
1240 | return 0; | |
e7cbe5cb LB |
1241 | if (!verity_data) { |
1242 | if (!v->found || !v->node || !v->fstype) | |
1243 | return 0; | |
4623e8e6 | 1244 | |
e7cbe5cb LB |
1245 | if (!streq(v->fstype, "DM_verity_hash")) |
1246 | return 0; | |
1247 | } | |
4623e8e6 LP |
1248 | |
1249 | r = make_dm_name_and_node(m->node, "-verity", &name, &node); | |
1250 | if (r < 0) | |
1251 | return r; | |
1252 | ||
1253 | if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1)) | |
1254 | return -ENOMEM; | |
1255 | ||
e7cbe5cb | 1256 | r = crypt_init(&cd, verity_data ?: v->node); |
4623e8e6 LP |
1257 | if (r < 0) |
1258 | return r; | |
1259 | ||
1887032f TM |
1260 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); |
1261 | ||
4623e8e6 LP |
1262 | r = crypt_load(cd, CRYPT_VERITY, NULL); |
1263 | if (r < 0) | |
294bd454 | 1264 | return r; |
4623e8e6 LP |
1265 | |
1266 | r = crypt_set_data_device(cd, m->node); | |
1267 | if (r < 0) | |
294bd454 | 1268 | return r; |
4623e8e6 LP |
1269 | |
1270 | r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY); | |
1271 | if (r < 0) | |
294bd454 | 1272 | return r; |
4623e8e6 | 1273 | |
1cc6c93a YW |
1274 | d->decrypted[d->n_decrypted].name = TAKE_PTR(name); |
1275 | d->decrypted[d->n_decrypted].device = TAKE_PTR(cd); | |
4623e8e6 LP |
1276 | d->n_decrypted++; |
1277 | ||
1cc6c93a | 1278 | m->decrypted_node = TAKE_PTR(node); |
4623e8e6 LP |
1279 | |
1280 | return 0; | |
18b5886e LP |
1281 | } |
1282 | #endif | |
1283 | ||
1284 | int dissected_image_decrypt( | |
1285 | DissectedImage *m, | |
1286 | const char *passphrase, | |
4623e8e6 LP |
1287 | const void *root_hash, |
1288 | size_t root_hash_size, | |
e7cbe5cb | 1289 | const char *verity_data, |
18b5886e LP |
1290 | DissectImageFlags flags, |
1291 | DecryptedImage **ret) { | |
1292 | ||
349cc4a5 | 1293 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 1294 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
1295 | unsigned i; |
1296 | int r; | |
1297 | #endif | |
1298 | ||
1299 | assert(m); | |
4623e8e6 | 1300 | assert(root_hash || root_hash_size == 0); |
18b5886e LP |
1301 | |
1302 | /* Returns: | |
1303 | * | |
1304 | * = 0 → There was nothing to decrypt | |
1305 | * > 0 → Decrypted successfully | |
d1c536f5 | 1306 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
1307 | * -EKEYREJECTED → Passed key was not correct |
1308 | */ | |
1309 | ||
4623e8e6 LP |
1310 | if (root_hash && root_hash_size < sizeof(sd_id128_t)) |
1311 | return -EINVAL; | |
1312 | ||
1313 | if (!m->encrypted && !m->verity) { | |
18b5886e LP |
1314 | *ret = NULL; |
1315 | return 0; | |
1316 | } | |
1317 | ||
349cc4a5 | 1318 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1319 | d = new0(DecryptedImage, 1); |
1320 | if (!d) | |
1321 | return -ENOMEM; | |
1322 | ||
1323 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
1324 | DissectedPartition *p = m->partitions + i; | |
4623e8e6 | 1325 | int k; |
18b5886e LP |
1326 | |
1327 | if (!p->found) | |
1328 | continue; | |
1329 | ||
1330 | r = decrypt_partition(p, passphrase, flags, d); | |
1331 | if (r < 0) | |
1332 | return r; | |
1333 | ||
4623e8e6 LP |
1334 | k = PARTITION_VERITY_OF(i); |
1335 | if (k >= 0) { | |
e7cbe5cb | 1336 | r = verity_partition(p, m->partitions + k, root_hash, root_hash_size, verity_data, flags, d); |
4623e8e6 LP |
1337 | if (r < 0) |
1338 | return r; | |
1339 | } | |
1340 | ||
18b5886e LP |
1341 | if (!p->decrypted_fstype && p->decrypted_node) { |
1342 | r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype); | |
7cc84b2c | 1343 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
1344 | return r; |
1345 | } | |
1346 | } | |
1347 | ||
1cc6c93a | 1348 | *ret = TAKE_PTR(d); |
18b5886e LP |
1349 | |
1350 | return 1; | |
1351 | #else | |
1352 | return -EOPNOTSUPP; | |
1353 | #endif | |
1354 | } | |
1355 | ||
1356 | int dissected_image_decrypt_interactively( | |
1357 | DissectedImage *m, | |
1358 | const char *passphrase, | |
4623e8e6 LP |
1359 | const void *root_hash, |
1360 | size_t root_hash_size, | |
e7cbe5cb | 1361 | const char *verity_data, |
18b5886e LP |
1362 | DissectImageFlags flags, |
1363 | DecryptedImage **ret) { | |
1364 | ||
1365 | _cleanup_strv_free_erase_ char **z = NULL; | |
1366 | int n = 3, r; | |
1367 | ||
1368 | if (passphrase) | |
1369 | n--; | |
1370 | ||
1371 | for (;;) { | |
e7cbe5cb | 1372 | r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, verity_data, flags, ret); |
18b5886e LP |
1373 | if (r >= 0) |
1374 | return r; | |
1375 | if (r == -EKEYREJECTED) | |
1376 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
fc95c359 YW |
1377 | else if (r != -ENOKEY) |
1378 | return log_error_errno(r, "Failed to decrypt image: %m"); | |
18b5886e | 1379 | |
baaa35ad ZJS |
1380 | if (--n < 0) |
1381 | return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED), | |
1382 | "Too many retries."); | |
18b5886e LP |
1383 | |
1384 | z = strv_free(z); | |
1385 | ||
a1c111c2 | 1386 | r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z); |
18b5886e LP |
1387 | if (r < 0) |
1388 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
1389 | ||
1390 | passphrase = z[0]; | |
1391 | } | |
1392 | } | |
1393 | ||
18b5886e LP |
1394 | int decrypted_image_relinquish(DecryptedImage *d) { |
1395 | ||
349cc4a5 | 1396 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1397 | size_t i; |
1398 | int r; | |
1399 | #endif | |
1400 | ||
1401 | assert(d); | |
1402 | ||
1403 | /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so | |
1404 | * that we don't clean it up ourselves either anymore */ | |
1405 | ||
349cc4a5 | 1406 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1407 | for (i = 0; i < d->n_decrypted; i++) { |
1408 | DecryptedPartition *p = d->decrypted + i; | |
1409 | ||
1410 | if (p->relinquished) | |
1411 | continue; | |
1412 | ||
a709a315 | 1413 | r = dm_deferred_remove(p->name); |
18b5886e LP |
1414 | if (r < 0) |
1415 | return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); | |
1416 | ||
1417 | p->relinquished = true; | |
1418 | } | |
1419 | #endif | |
1420 | ||
1421 | return 0; | |
1422 | } | |
1423 | ||
0389f4fa | 1424 | int verity_metadata_load(const char *image, const char *root_hash_path, void **ret_roothash, size_t *ret_roothash_size, char **ret_verity_data) { |
e7cbe5cb LB |
1425 | _cleanup_free_ char *verity_filename = NULL; |
1426 | _cleanup_free_ void *roothash_decoded = NULL; | |
1427 | size_t roothash_decoded_size = 0; | |
78ebe980 LP |
1428 | int r; |
1429 | ||
1430 | assert(image); | |
78ebe980 LP |
1431 | |
1432 | if (is_device_path(image)) { | |
1433 | /* If we are asked to load the root hash for a device node, exit early */ | |
e7cbe5cb LB |
1434 | if (ret_roothash) |
1435 | *ret_roothash = NULL; | |
1436 | if (ret_roothash_size) | |
1437 | *ret_roothash_size = 0; | |
1438 | if (ret_verity_data) | |
1439 | *ret_verity_data = NULL; | |
78ebe980 LP |
1440 | return 0; |
1441 | } | |
1442 | ||
e7cbe5cb LB |
1443 | if (ret_verity_data) { |
1444 | char *e; | |
78ebe980 | 1445 | |
e7cbe5cb LB |
1446 | verity_filename = new(char, strlen(image) + STRLEN(".verity") + 1); |
1447 | if (!verity_filename) | |
1448 | return -ENOMEM; | |
1449 | strcpy(verity_filename, image); | |
1450 | e = endswith(verity_filename, ".raw"); | |
41488e1f | 1451 | if (e) |
e7cbe5cb LB |
1452 | strcpy(e, ".verity"); |
1453 | else | |
1454 | strcat(verity_filename, ".verity"); | |
41488e1f | 1455 | |
e7cbe5cb LB |
1456 | r = access(verity_filename, F_OK); |
1457 | if (r < 0) { | |
1458 | if (errno != ENOENT) | |
1459 | return -errno; | |
1460 | verity_filename = mfree(verity_filename); | |
41488e1f | 1461 | } |
78ebe980 | 1462 | } |
78ebe980 | 1463 | |
e7cbe5cb LB |
1464 | if (ret_roothash) { |
1465 | _cleanup_free_ char *text = NULL; | |
1466 | assert(ret_roothash_size); | |
1467 | ||
0389f4fa LB |
1468 | if (root_hash_path) { |
1469 | /* We have the path to a roothash to load and decode, eg: RootHash=/foo/bar.roothash */ | |
1470 | r = read_one_line_file(root_hash_path, &text); | |
1471 | if (r < 0) | |
e7cbe5cb | 1472 | return r; |
0389f4fa LB |
1473 | } else { |
1474 | r = getxattr_malloc(image, "user.verity.roothash", &text, true); | |
1475 | if (r < 0) { | |
1476 | char *fn, *e, *n; | |
1477 | ||
1478 | if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT)) | |
1479 | return r; | |
78ebe980 | 1480 | |
0389f4fa LB |
1481 | fn = newa(char, strlen(image) + STRLEN(".roothash") + 1); |
1482 | n = stpcpy(fn, image); | |
1483 | e = endswith(fn, ".raw"); | |
1484 | if (e) | |
1485 | n = e; | |
e7cbe5cb | 1486 | |
0389f4fa | 1487 | strcpy(n, ".roothash"); |
e7cbe5cb | 1488 | |
0389f4fa LB |
1489 | r = read_one_line_file(fn, &text); |
1490 | if (r < 0 && r != -ENOENT) | |
1491 | return r; | |
1492 | } | |
e7cbe5cb LB |
1493 | } |
1494 | ||
1495 | if (text) { | |
1496 | r = unhexmem(text, strlen(text), &roothash_decoded, &roothash_decoded_size); | |
1497 | if (r < 0) | |
1498 | return r; | |
1499 | if (roothash_decoded_size < sizeof(sd_id128_t)) | |
1500 | return -EINVAL; | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | if (ret_roothash) { | |
1505 | *ret_roothash = TAKE_PTR(roothash_decoded); | |
1506 | *ret_roothash_size = roothash_decoded_size; | |
1507 | } | |
1508 | if (ret_verity_data) | |
1509 | *ret_verity_data = TAKE_PTR(verity_filename); | |
78ebe980 | 1510 | |
78ebe980 LP |
1511 | return 1; |
1512 | } | |
1513 | ||
3b925504 LP |
1514 | int dissected_image_acquire_metadata(DissectedImage *m) { |
1515 | ||
1516 | enum { | |
1517 | META_HOSTNAME, | |
1518 | META_MACHINE_ID, | |
1519 | META_MACHINE_INFO, | |
1520 | META_OS_RELEASE, | |
1521 | _META_MAX, | |
1522 | }; | |
1523 | ||
1524 | static const char *const paths[_META_MAX] = { | |
1525 | [META_HOSTNAME] = "/etc/hostname\0", | |
1526 | [META_MACHINE_ID] = "/etc/machine-id\0", | |
1527 | [META_MACHINE_INFO] = "/etc/machine-info\0", | |
d4dffb85 LP |
1528 | [META_OS_RELEASE] = "/etc/os-release\0" |
1529 | "/usr/lib/os-release\0", | |
3b925504 LP |
1530 | }; |
1531 | ||
1532 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL; | |
1533 | _cleanup_(rmdir_and_freep) char *t = NULL; | |
1534 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
1535 | sd_id128_t machine_id = SD_ID128_NULL; | |
1536 | _cleanup_free_ char *hostname = NULL; | |
1537 | unsigned n_meta_initialized = 0, k; | |
1538 | int fds[2 * _META_MAX], r; | |
3b925504 LP |
1539 | |
1540 | BLOCK_SIGNALS(SIGCHLD); | |
1541 | ||
1542 | assert(m); | |
1543 | ||
1544 | for (; n_meta_initialized < _META_MAX; n_meta_initialized ++) | |
1545 | if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) { | |
1546 | r = -errno; | |
1547 | goto finish; | |
1548 | } | |
1549 | ||
1550 | r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); | |
1551 | if (r < 0) | |
1552 | goto finish; | |
1553 | ||
e2047ba9 | 1554 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 1555 | if (r < 0) |
3b925504 | 1556 | goto finish; |
be39f6ee | 1557 | if (r == 0) { |
03bcb6d4 | 1558 | r = dissected_image_mount(m, t, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_VALIDATE_OS); |
429d4e41 LP |
1559 | if (r < 0) { |
1560 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
3b925504 | 1561 | _exit(EXIT_FAILURE); |
429d4e41 | 1562 | } |
3b925504 LP |
1563 | |
1564 | for (k = 0; k < _META_MAX; k++) { | |
1565 | _cleanup_close_ int fd = -1; | |
1566 | const char *p; | |
1567 | ||
1568 | fds[2*k] = safe_close(fds[2*k]); | |
1569 | ||
1570 | NULSTR_FOREACH(p, paths[k]) { | |
36952d19 | 1571 | fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); |
3b925504 LP |
1572 | if (fd >= 0) |
1573 | break; | |
1574 | } | |
36952d19 LP |
1575 | if (fd < 0) { |
1576 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
3b925504 | 1577 | continue; |
36952d19 | 1578 | } |
3b925504 LP |
1579 | |
1580 | r = copy_bytes(fd, fds[2*k+1], (uint64_t) -1, 0); | |
1581 | if (r < 0) | |
1582 | _exit(EXIT_FAILURE); | |
1583 | ||
1584 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
1585 | } | |
1586 | ||
1587 | _exit(EXIT_SUCCESS); | |
1588 | } | |
1589 | ||
1590 | for (k = 0; k < _META_MAX; k++) { | |
1591 | _cleanup_fclose_ FILE *f = NULL; | |
1592 | ||
1593 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
1594 | ||
4fa744a3 | 1595 | f = take_fdopen(&fds[2*k], "r"); |
3b925504 LP |
1596 | if (!f) { |
1597 | r = -errno; | |
1598 | goto finish; | |
1599 | } | |
1600 | ||
3b925504 LP |
1601 | switch (k) { |
1602 | ||
1603 | case META_HOSTNAME: | |
1604 | r = read_etc_hostname_stream(f, &hostname); | |
1605 | if (r < 0) | |
1606 | log_debug_errno(r, "Failed to read /etc/hostname: %m"); | |
1607 | ||
1608 | break; | |
1609 | ||
1610 | case META_MACHINE_ID: { | |
1611 | _cleanup_free_ char *line = NULL; | |
1612 | ||
1613 | r = read_line(f, LONG_LINE_MAX, &line); | |
1614 | if (r < 0) | |
1615 | log_debug_errno(r, "Failed to read /etc/machine-id: %m"); | |
1616 | else if (r == 33) { | |
1617 | r = sd_id128_from_string(line, &machine_id); | |
1618 | if (r < 0) | |
1619 | log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line); | |
1620 | } else if (r == 0) | |
1621 | log_debug("/etc/machine-id file is empty."); | |
1622 | else | |
1623 | log_debug("/etc/machine-id has unexpected length %i.", r); | |
1624 | ||
1625 | break; | |
1626 | } | |
1627 | ||
1628 | case META_MACHINE_INFO: | |
aa8fbc74 | 1629 | r = load_env_file_pairs(f, "machine-info", &machine_info); |
3b925504 LP |
1630 | if (r < 0) |
1631 | log_debug_errno(r, "Failed to read /etc/machine-info: %m"); | |
1632 | ||
1633 | break; | |
1634 | ||
1635 | case META_OS_RELEASE: | |
aa8fbc74 | 1636 | r = load_env_file_pairs(f, "os-release", &os_release); |
3b925504 LP |
1637 | if (r < 0) |
1638 | log_debug_errno(r, "Failed to read OS release file: %m"); | |
1639 | ||
1640 | break; | |
1641 | } | |
1642 | } | |
1643 | ||
2e87a1fd | 1644 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 1645 | child = 0; |
2e87a1fd | 1646 | if (r < 0) |
3b925504 | 1647 | goto finish; |
2e87a1fd LP |
1648 | if (r != EXIT_SUCCESS) |
1649 | return -EPROTO; | |
3b925504 LP |
1650 | |
1651 | free_and_replace(m->hostname, hostname); | |
1652 | m->machine_id = machine_id; | |
1653 | strv_free_and_replace(m->machine_info, machine_info); | |
1654 | strv_free_and_replace(m->os_release, os_release); | |
1655 | ||
1656 | finish: | |
1657 | for (k = 0; k < n_meta_initialized; k++) | |
1658 | safe_close_pair(fds + 2*k); | |
1659 | ||
1660 | return r; | |
1661 | } | |
1662 | ||
4526113f LP |
1663 | int dissect_image_and_warn( |
1664 | int fd, | |
1665 | const char *name, | |
1666 | const void *root_hash, | |
1667 | size_t root_hash_size, | |
e7cbe5cb | 1668 | const char *verity_data, |
4526113f LP |
1669 | DissectImageFlags flags, |
1670 | DissectedImage **ret) { | |
1671 | ||
1672 | _cleanup_free_ char *buffer = NULL; | |
1673 | int r; | |
1674 | ||
1675 | if (!name) { | |
1676 | r = fd_get_path(fd, &buffer); | |
1677 | if (r < 0) | |
1678 | return r; | |
1679 | ||
1680 | name = buffer; | |
1681 | } | |
1682 | ||
e7cbe5cb | 1683 | r = dissect_image(fd, root_hash, root_hash_size, verity_data, flags, ret); |
4526113f LP |
1684 | |
1685 | switch (r) { | |
1686 | ||
1687 | case -EOPNOTSUPP: | |
1688 | return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support."); | |
1689 | ||
1690 | case -ENOPKG: | |
1691 | return log_error_errno(r, "Couldn't identify a suitable partition table or file system in '%s'.", name); | |
1692 | ||
1693 | case -EADDRNOTAVAIL: | |
1694 | return log_error_errno(r, "No root partition for specified root hash found in '%s'.", name); | |
1695 | ||
1696 | case -ENOTUNIQ: | |
1697 | return log_error_errno(r, "Multiple suitable root partitions found in image '%s'.", name); | |
1698 | ||
1699 | case -ENXIO: | |
1700 | return log_error_errno(r, "No suitable root partition found in image '%s'.", name); | |
1701 | ||
1702 | case -EPROTONOSUPPORT: | |
1703 | return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name); | |
1704 | ||
1705 | default: | |
1706 | if (r < 0) | |
1707 | return log_error_errno(r, "Failed to dissect image '%s': %m", name); | |
1708 | ||
1709 | return r; | |
1710 | } | |
1711 | } | |
1712 | ||
e7cbe5cb LB |
1713 | bool dissected_image_can_do_verity(const DissectedImage *image, unsigned partition_designator) { |
1714 | if (image->single_file_system) | |
1715 | return partition_designator == PARTITION_ROOT && image->can_verity; | |
1716 | ||
1717 | return PARTITION_VERITY_OF(partition_designator) >= 0; | |
1718 | } | |
1719 | ||
1720 | bool dissected_image_has_verity(const DissectedImage *image, unsigned partition_designator) { | |
1721 | int k; | |
1722 | ||
1723 | if (image->single_file_system) | |
1724 | return partition_designator == PARTITION_ROOT && image->verity; | |
1725 | ||
1726 | k = PARTITION_VERITY_OF(partition_designator); | |
1727 | return k >= 0 && image->partitions[k].found; | |
1728 | } | |
1729 | ||
8c1be37e LP |
1730 | static const char *const partition_designator_table[] = { |
1731 | [PARTITION_ROOT] = "root", | |
1732 | [PARTITION_ROOT_SECONDARY] = "root-secondary", | |
1733 | [PARTITION_HOME] = "home", | |
1734 | [PARTITION_SRV] = "srv", | |
1735 | [PARTITION_ESP] = "esp", | |
a8c47660 | 1736 | [PARTITION_XBOOTLDR] = "xbootldr", |
8c1be37e | 1737 | [PARTITION_SWAP] = "swap", |
4623e8e6 LP |
1738 | [PARTITION_ROOT_VERITY] = "root-verity", |
1739 | [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity", | |
d4dffb85 LP |
1740 | [PARTITION_TMP] = "tmp", |
1741 | [PARTITION_VAR] = "var", | |
8c1be37e LP |
1742 | }; |
1743 | ||
1744 | DEFINE_STRING_TABLE_LOOKUP(partition_designator, int); |