]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8c1be37e LP |
2 | |
3 | #include <sys/mount.h> | |
3b925504 LP |
4 | #include <sys/prctl.h> |
5 | #include <sys/wait.h> | |
8c1be37e | 6 | |
dccca82b LP |
7 | #include "sd-id128.h" |
8 | ||
8c1be37e | 9 | #include "architecture.h" |
18b5886e | 10 | #include "ask-password-api.h" |
8c1be37e | 11 | #include "blkid-util.h" |
18c528e9 | 12 | #include "blockdev-util.h" |
3b925504 | 13 | #include "copy.h" |
294bd454 | 14 | #include "crypt-util.h" |
3b925504 | 15 | #include "def.h" |
553e15f2 | 16 | #include "device-nodes.h" |
8c1be37e | 17 | #include "dissect-image.h" |
18b5886e | 18 | #include "fd-util.h" |
78ebe980 | 19 | #include "fileio.h" |
2eedfd2d | 20 | #include "fs-util.h" |
8c1be37e | 21 | #include "gpt.h" |
78ebe980 | 22 | #include "hexdecoct.h" |
3b925504 LP |
23 | #include "hostname-util.h" |
24 | #include "id128-util.h" | |
dcce98a4 | 25 | #include "linux-3.13/dm-ioctl.h" |
ffc01f06 | 26 | #include "missing.h" |
8c1be37e | 27 | #include "mount-util.h" |
d58ad743 | 28 | #include "os-util.h" |
8c1be37e | 29 | #include "path-util.h" |
3b925504 LP |
30 | #include "process-util.h" |
31 | #include "raw-clone.h" | |
32 | #include "signal-util.h" | |
8c1be37e | 33 | #include "stat-util.h" |
18b5886e | 34 | #include "stdio-util.h" |
8c1be37e LP |
35 | #include "string-table.h" |
36 | #include "string-util.h" | |
2eedfd2d | 37 | #include "strv.h" |
8c1be37e | 38 | #include "udev-util.h" |
2d3a5a73 | 39 | #include "user-util.h" |
41488e1f | 40 | #include "xattr-util.h" |
8c1be37e | 41 | |
c34b75a1 | 42 | int probe_filesystem(const char *node, char **ret_fstype) { |
7cc84b2c ZJS |
43 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
44 | * 0/NULL will be returned. -EUCLEAN will be returned for ambigous results, and an | |
45 | * different error otherwise. */ | |
46 | ||
349cc4a5 | 47 | #if HAVE_BLKID |
8e766630 | 48 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
18b5886e LP |
49 | const char *fstype; |
50 | int r; | |
51 | ||
995fa2e5 | 52 | errno = 0; |
18b5886e LP |
53 | b = blkid_new_probe_from_filename(node); |
54 | if (!b) | |
995fa2e5 | 55 | return -errno ?: -ENOMEM; |
18b5886e LP |
56 | |
57 | blkid_probe_enable_superblocks(b, 1); | |
58 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
59 | ||
60 | errno = 0; | |
61 | r = blkid_do_safeprobe(b); | |
7cc84b2c ZJS |
62 | if (r == 1) { |
63 | log_debug("No type detected on partition %s", node); | |
18b5886e LP |
64 | goto not_found; |
65 | } | |
7cc84b2c ZJS |
66 | if (r == -2) { |
67 | log_debug("Results ambiguous for partition %s", node); | |
68 | return -EUCLEAN; | |
69 | } | |
b382db9f ZJS |
70 | if (r != 0) |
71 | return -errno ?: -EIO; | |
18b5886e LP |
72 | |
73 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
74 | ||
75 | if (fstype) { | |
76 | char *t; | |
77 | ||
78 | t = strdup(fstype); | |
79 | if (!t) | |
80 | return -ENOMEM; | |
81 | ||
82 | *ret_fstype = t; | |
83 | return 1; | |
84 | } | |
85 | ||
86 | not_found: | |
87 | *ret_fstype = NULL; | |
88 | return 0; | |
d1c536f5 ZJS |
89 | #else |
90 | return -EOPNOTSUPP; | |
a75e27eb | 91 | #endif |
d1c536f5 | 92 | } |
18b5886e | 93 | |
40c10d3f | 94 | #if HAVE_BLKID |
cde942f6 JPRV |
95 | /* Detect RPMB and Boot partitions, which are not listed by blkid. |
96 | * See https://github.com/systemd/systemd/issues/5806. */ | |
97 | static bool device_is_mmc_special_partition(struct udev_device *d) { | |
aae22eb3 LP |
98 | const char *sysname; |
99 | ||
100 | sysname = udev_device_get_sysname(d); | |
0cfa78dd ZJS |
101 | return sysname && startswith(sysname, "mmcblk") && |
102 | (endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1")); | |
cde942f6 JPRV |
103 | } |
104 | ||
aae22eb3 LP |
105 | static bool device_is_block(struct udev_device *d) { |
106 | const char *ss; | |
107 | ||
108 | ss = udev_device_get_subsystem(d); | |
109 | if (!ss) | |
110 | return false; | |
111 | ||
112 | return streq(ss, "block"); | |
113 | } | |
40c10d3f | 114 | #endif |
aae22eb3 | 115 | |
4526113f LP |
116 | int dissect_image( |
117 | int fd, | |
118 | const void *root_hash, | |
119 | size_t root_hash_size, | |
120 | DissectImageFlags flags, | |
121 | DissectedImage **ret) { | |
8c1be37e | 122 | |
349cc4a5 | 123 | #if HAVE_BLKID |
4623e8e6 | 124 | sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL; |
8e766630 | 125 | _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL; |
8c1be37e | 126 | bool is_gpt, is_mbr, generic_rw, multiple_generic = false; |
8e766630 | 127 | _cleanup_(udev_device_unrefp) struct udev_device *d = NULL; |
8c1be37e | 128 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
8e766630 LP |
129 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
130 | _cleanup_(udev_unrefp) struct udev *udev = NULL; | |
8c1be37e | 131 | _cleanup_free_ char *generic_node = NULL; |
be30ad41 | 132 | sd_id128_t generic_uuid = SD_ID128_NULL; |
9b6deb03 | 133 | const char *pttype = NULL; |
8c1be37e LP |
134 | struct udev_list_entry *first, *item; |
135 | blkid_partlist pl; | |
136 | int r, generic_nr; | |
137 | struct stat st; | |
138 | unsigned i; | |
139 | ||
140 | assert(fd >= 0); | |
141 | assert(ret); | |
4623e8e6 | 142 | assert(root_hash || root_hash_size == 0); |
8c1be37e LP |
143 | |
144 | /* Probes a disk image, and returns information about what it found in *ret. | |
145 | * | |
4623e8e6 LP |
146 | * Returns -ENOPKG if no suitable partition table or file system could be found. |
147 | * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */ | |
148 | ||
149 | if (root_hash) { | |
150 | /* If a root hash is supplied, then we use the root partition that has a UUID that match the first | |
151 | * 128bit of the root hash. And we use the verity partition that has a UUID that match the final | |
152 | * 128bit. */ | |
153 | ||
154 | if (root_hash_size < sizeof(sd_id128_t)) | |
155 | return -EINVAL; | |
156 | ||
157 | memcpy(&root_uuid, root_hash, sizeof(sd_id128_t)); | |
158 | memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t)); | |
159 | ||
160 | if (sd_id128_is_null(root_uuid)) | |
161 | return -EINVAL; | |
162 | if (sd_id128_is_null(verity_uuid)) | |
163 | return -EINVAL; | |
164 | } | |
8c1be37e LP |
165 | |
166 | if (fstat(fd, &st) < 0) | |
167 | return -errno; | |
168 | ||
169 | if (!S_ISBLK(st.st_mode)) | |
170 | return -ENOTBLK; | |
171 | ||
172 | b = blkid_new_probe(); | |
173 | if (!b) | |
174 | return -ENOMEM; | |
175 | ||
176 | errno = 0; | |
177 | r = blkid_probe_set_device(b, fd, 0, 0); | |
b382db9f ZJS |
178 | if (r != 0) |
179 | return -errno ?: -ENOMEM; | |
8c1be37e | 180 | |
9b6deb03 LP |
181 | if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { |
182 | /* Look for file system superblocks, unless we only shall look for GPT partition tables */ | |
183 | blkid_probe_enable_superblocks(b, 1); | |
184 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE); | |
185 | } | |
186 | ||
8c1be37e LP |
187 | blkid_probe_enable_partitions(b, 1); |
188 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
189 | ||
190 | errno = 0; | |
191 | r = blkid_do_safeprobe(b); | |
4c701096 | 192 | if (IN_SET(r, -2, 1)) { |
8c1be37e LP |
193 | log_debug("Failed to identify any partition table."); |
194 | return -ENOPKG; | |
195 | } | |
b382db9f ZJS |
196 | if (r != 0) |
197 | return -errno ?: -EIO; | |
8c1be37e LP |
198 | |
199 | m = new0(DissectedImage, 1); | |
200 | if (!m) | |
201 | return -ENOMEM; | |
202 | ||
e0f9e7bd LP |
203 | if (!(flags & DISSECT_IMAGE_GPT_ONLY) && |
204 | (flags & DISSECT_IMAGE_REQUIRE_ROOT)) { | |
9b6deb03 | 205 | const char *usage = NULL; |
8c1be37e | 206 | |
9b6deb03 LP |
207 | (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL); |
208 | if (STRPTR_IN_SET(usage, "filesystem", "crypto")) { | |
209 | _cleanup_free_ char *t = NULL, *n = NULL; | |
210 | const char *fstype = NULL; | |
8c1be37e | 211 | |
9b6deb03 LP |
212 | /* OK, we have found a file system, that's our root partition then. */ |
213 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
8c1be37e | 214 | |
9b6deb03 LP |
215 | if (fstype) { |
216 | t = strdup(fstype); | |
217 | if (!t) | |
218 | return -ENOMEM; | |
219 | } | |
220 | ||
221 | if (asprintf(&n, "/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0) | |
222 | return -ENOMEM; | |
8c1be37e | 223 | |
9b6deb03 LP |
224 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
225 | .found = true, | |
226 | .rw = true, | |
227 | .partno = -1, | |
228 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
229 | .fstype = TAKE_PTR(t), |
230 | .node = TAKE_PTR(n), | |
9b6deb03 | 231 | }; |
8c1be37e | 232 | |
9b6deb03 | 233 | m->encrypted = streq(fstype, "crypto_LUKS"); |
18b5886e | 234 | |
1cc6c93a | 235 | *ret = TAKE_PTR(m); |
8c1be37e | 236 | |
9b6deb03 LP |
237 | return 0; |
238 | } | |
8c1be37e LP |
239 | } |
240 | ||
241 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
242 | if (!pttype) | |
243 | return -ENOPKG; | |
244 | ||
245 | is_gpt = streq_ptr(pttype, "gpt"); | |
246 | is_mbr = streq_ptr(pttype, "dos"); | |
247 | ||
9b6deb03 | 248 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
249 | return -ENOPKG; |
250 | ||
251 | errno = 0; | |
252 | pl = blkid_probe_get_partitions(b); | |
b382db9f ZJS |
253 | if (!pl) |
254 | return -errno ?: -ENOMEM; | |
8c1be37e LP |
255 | |
256 | udev = udev_new(); | |
257 | if (!udev) | |
258 | return -errno; | |
259 | ||
260 | d = udev_device_new_from_devnum(udev, 'b', st.st_rdev); | |
261 | if (!d) | |
262 | return -ENOMEM; | |
263 | ||
264 | for (i = 0;; i++) { | |
265 | int n, z; | |
266 | ||
267 | if (i >= 10) { | |
268 | log_debug("Kernel partitions never appeared."); | |
269 | return -ENXIO; | |
270 | } | |
271 | ||
272 | e = udev_enumerate_new(udev); | |
273 | if (!e) | |
274 | return -errno; | |
275 | ||
276 | r = udev_enumerate_add_match_parent(e, d); | |
277 | if (r < 0) | |
278 | return r; | |
279 | ||
280 | r = udev_enumerate_scan_devices(e); | |
281 | if (r < 0) | |
282 | return r; | |
283 | ||
284 | /* Count the partitions enumerated by the kernel */ | |
285 | n = 0; | |
286 | first = udev_enumerate_get_list_entry(e); | |
cde942f6 | 287 | udev_list_entry_foreach(item, first) { |
8e766630 | 288 | _cleanup_(udev_device_unrefp) struct udev_device *q; |
aae22eb3 | 289 | dev_t qn; |
cde942f6 JPRV |
290 | |
291 | q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)); | |
292 | if (!q) | |
293 | return -errno; | |
294 | ||
aae22eb3 LP |
295 | qn = udev_device_get_devnum(q); |
296 | if (major(qn) == 0) | |
297 | continue; | |
298 | ||
299 | if (!device_is_block(q)) | |
300 | continue; | |
301 | ||
cde942f6 JPRV |
302 | if (device_is_mmc_special_partition(q)) |
303 | continue; | |
8c1be37e | 304 | n++; |
cde942f6 | 305 | } |
8c1be37e LP |
306 | |
307 | /* Count the partitions enumerated by blkid */ | |
308 | z = blkid_partlist_numof_partitions(pl); | |
309 | if (n == z + 1) | |
310 | break; | |
311 | if (n > z + 1) { | |
312 | log_debug("blkid and kernel partition list do not match."); | |
313 | return -EIO; | |
314 | } | |
315 | if (n < z + 1) { | |
759aaedc | 316 | unsigned j = 0; |
8c1be37e LP |
317 | |
318 | /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running | |
319 | * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a | |
320 | * synchronous call that waits until probing is complete. */ | |
321 | ||
759aaedc LP |
322 | for (;;) { |
323 | if (j++ > 20) | |
324 | return -EBUSY; | |
8c1be37e | 325 | |
759aaedc | 326 | if (ioctl(fd, BLKRRPART, 0) < 0) { |
8c1be37e | 327 | r = -errno; |
759aaedc LP |
328 | |
329 | if (r == -EINVAL) { | |
330 | struct loop_info64 info; | |
331 | ||
332 | /* If we are running on a loop device that has partition scanning off, | |
333 | * return an explicit recognizable error about this, so that callers | |
334 | * can generate a proper message explaining the situation. */ | |
335 | ||
336 | if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0 && (info.lo_flags & LO_FLAGS_PARTSCAN) == 0) { | |
337 | log_debug("Device is loop device and partition scanning is off!"); | |
338 | return -EPROTONOSUPPORT; | |
339 | } | |
340 | } | |
341 | if (r != -EBUSY) | |
342 | return r; | |
343 | } else | |
8c1be37e LP |
344 | break; |
345 | ||
346 | /* If something else has the device open, such as an udev rule, the ioctl will return | |
347 | * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a | |
348 | * bit, and try again. | |
349 | * | |
350 | * This is really something they should fix in the kernel! */ | |
351 | ||
759aaedc | 352 | (void) usleep(50 * USEC_PER_MSEC); |
8c1be37e | 353 | } |
8c1be37e LP |
354 | } |
355 | ||
356 | e = udev_enumerate_unref(e); | |
357 | } | |
358 | ||
359 | first = udev_enumerate_get_list_entry(e); | |
360 | udev_list_entry_foreach(item, first) { | |
8e766630 | 361 | _cleanup_(udev_device_unrefp) struct udev_device *q; |
9b6deb03 | 362 | unsigned long long pflags; |
8c1be37e | 363 | blkid_partition pp; |
cde942f6 | 364 | const char *node; |
8c1be37e LP |
365 | dev_t qn; |
366 | int nr; | |
367 | ||
368 | q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)); | |
369 | if (!q) | |
370 | return -errno; | |
371 | ||
372 | qn = udev_device_get_devnum(q); | |
373 | if (major(qn) == 0) | |
374 | continue; | |
375 | ||
376 | if (st.st_rdev == qn) | |
377 | continue; | |
378 | ||
aae22eb3 LP |
379 | if (!device_is_block(q)) |
380 | continue; | |
381 | ||
cde942f6 | 382 | if (device_is_mmc_special_partition(q)) |
7be1420f LP |
383 | continue; |
384 | ||
8c1be37e LP |
385 | node = udev_device_get_devnode(q); |
386 | if (!node) | |
387 | continue; | |
388 | ||
389 | pp = blkid_partlist_devno_to_partition(pl, qn); | |
390 | if (!pp) | |
391 | continue; | |
392 | ||
9b6deb03 | 393 | pflags = blkid_partition_get_flags(pp); |
8c1be37e LP |
394 | |
395 | nr = blkid_partition_get_partno(pp); | |
396 | if (nr < 0) | |
397 | continue; | |
398 | ||
399 | if (is_gpt) { | |
400 | int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID; | |
4623e8e6 LP |
401 | const char *stype, *sid, *fstype = NULL; |
402 | sd_id128_t type_id, id; | |
8c1be37e LP |
403 | bool rw = true; |
404 | ||
4623e8e6 LP |
405 | sid = blkid_partition_get_uuid(pp); |
406 | if (!sid) | |
407 | continue; | |
408 | if (sd_id128_from_string(sid, &id) < 0) | |
409 | continue; | |
410 | ||
8c1be37e LP |
411 | stype = blkid_partition_get_type_string(pp); |
412 | if (!stype) | |
413 | continue; | |
8c1be37e LP |
414 | if (sd_id128_from_string(stype, &type_id) < 0) |
415 | continue; | |
416 | ||
417 | if (sd_id128_equal(type_id, GPT_HOME)) { | |
a48dd347 LP |
418 | |
419 | if (pflags & GPT_FLAG_NO_AUTO) | |
420 | continue; | |
421 | ||
8c1be37e | 422 | designator = PARTITION_HOME; |
9b6deb03 | 423 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
8c1be37e | 424 | } else if (sd_id128_equal(type_id, GPT_SRV)) { |
a48dd347 LP |
425 | |
426 | if (pflags & GPT_FLAG_NO_AUTO) | |
427 | continue; | |
428 | ||
8c1be37e | 429 | designator = PARTITION_SRV; |
9b6deb03 | 430 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
8c1be37e | 431 | } else if (sd_id128_equal(type_id, GPT_ESP)) { |
a48dd347 LP |
432 | |
433 | /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined | |
434 | * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the | |
435 | * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */ | |
436 | ||
437 | if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL) | |
438 | continue; | |
439 | ||
8c1be37e LP |
440 | designator = PARTITION_ESP; |
441 | fstype = "vfat"; | |
442 | } | |
443 | #ifdef GPT_ROOT_NATIVE | |
444 | else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) { | |
4623e8e6 | 445 | |
a48dd347 LP |
446 | if (pflags & GPT_FLAG_NO_AUTO) |
447 | continue; | |
448 | ||
4623e8e6 LP |
449 | /* If a root ID is specified, ignore everything but the root id */ |
450 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
451 | continue; | |
452 | ||
8c1be37e LP |
453 | designator = PARTITION_ROOT; |
454 | architecture = native_architecture(); | |
9b6deb03 | 455 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
4f8b86e3 | 456 | } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) { |
4623e8e6 | 457 | |
a48dd347 LP |
458 | if (pflags & GPT_FLAG_NO_AUTO) |
459 | continue; | |
460 | ||
4623e8e6 LP |
461 | m->can_verity = true; |
462 | ||
463 | /* Ignore verity unless a root hash is specified */ | |
464 | if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id)) | |
465 | continue; | |
466 | ||
467 | designator = PARTITION_ROOT_VERITY; | |
468 | fstype = "DM_verity_hash"; | |
469 | architecture = native_architecture(); | |
470 | rw = false; | |
471 | } | |
472 | #endif | |
8c1be37e LP |
473 | #ifdef GPT_ROOT_SECONDARY |
474 | else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) { | |
4623e8e6 | 475 | |
a48dd347 LP |
476 | if (pflags & GPT_FLAG_NO_AUTO) |
477 | continue; | |
478 | ||
4623e8e6 LP |
479 | /* If a root ID is specified, ignore everything but the root id */ |
480 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
481 | continue; | |
482 | ||
8c1be37e LP |
483 | designator = PARTITION_ROOT_SECONDARY; |
484 | architecture = SECONDARY_ARCHITECTURE; | |
9b6deb03 | 485 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
4f8b86e3 | 486 | } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) { |
a48dd347 LP |
487 | |
488 | if (pflags & GPT_FLAG_NO_AUTO) | |
489 | continue; | |
490 | ||
4623e8e6 LP |
491 | m->can_verity = true; |
492 | ||
493 | /* Ignore verity unless root has is specified */ | |
494 | if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id)) | |
495 | continue; | |
496 | ||
497 | designator = PARTITION_ROOT_SECONDARY_VERITY; | |
498 | fstype = "DM_verity_hash"; | |
499 | architecture = SECONDARY_ARCHITECTURE; | |
500 | rw = false; | |
501 | } | |
8c1be37e LP |
502 | #endif |
503 | else if (sd_id128_equal(type_id, GPT_SWAP)) { | |
a48dd347 LP |
504 | |
505 | if (pflags & GPT_FLAG_NO_AUTO) | |
506 | continue; | |
507 | ||
8c1be37e LP |
508 | designator = PARTITION_SWAP; |
509 | fstype = "swap"; | |
510 | } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) { | |
511 | ||
a48dd347 LP |
512 | if (pflags & GPT_FLAG_NO_AUTO) |
513 | continue; | |
514 | ||
8c1be37e LP |
515 | if (generic_node) |
516 | multiple_generic = true; | |
517 | else { | |
518 | generic_nr = nr; | |
9b6deb03 | 519 | generic_rw = !(pflags & GPT_FLAG_READ_ONLY); |
be30ad41 | 520 | generic_uuid = id; |
8c1be37e LP |
521 | generic_node = strdup(node); |
522 | if (!generic_node) | |
523 | return -ENOMEM; | |
524 | } | |
525 | } | |
526 | ||
527 | if (designator != _PARTITION_DESIGNATOR_INVALID) { | |
528 | _cleanup_free_ char *t = NULL, *n = NULL; | |
529 | ||
530 | /* First one wins */ | |
531 | if (m->partitions[designator].found) | |
532 | continue; | |
533 | ||
534 | if (fstype) { | |
535 | t = strdup(fstype); | |
536 | if (!t) | |
537 | return -ENOMEM; | |
538 | } | |
539 | ||
540 | n = strdup(node); | |
541 | if (!n) | |
542 | return -ENOMEM; | |
543 | ||
544 | m->partitions[designator] = (DissectedPartition) { | |
545 | .found = true, | |
546 | .partno = nr, | |
547 | .rw = rw, | |
548 | .architecture = architecture, | |
1cc6c93a YW |
549 | .node = TAKE_PTR(n), |
550 | .fstype = TAKE_PTR(t), | |
be30ad41 | 551 | .uuid = id, |
8c1be37e | 552 | }; |
8c1be37e LP |
553 | } |
554 | ||
555 | } else if (is_mbr) { | |
556 | ||
9b6deb03 | 557 | if (pflags != 0x80) /* Bootable flag */ |
8c1be37e LP |
558 | continue; |
559 | ||
560 | if (blkid_partition_get_type(pp) != 0x83) /* Linux partition */ | |
561 | continue; | |
562 | ||
563 | if (generic_node) | |
564 | multiple_generic = true; | |
565 | else { | |
566 | generic_nr = nr; | |
567 | generic_rw = true; | |
568 | generic_node = strdup(node); | |
569 | if (!generic_node) | |
570 | return -ENOMEM; | |
571 | } | |
572 | } | |
573 | } | |
574 | ||
575 | if (!m->partitions[PARTITION_ROOT].found) { | |
576 | /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not | |
577 | * either, then check if there's a single generic one, and use that. */ | |
578 | ||
4623e8e6 | 579 | if (m->partitions[PARTITION_ROOT_VERITY].found) |
e0f9e7bd | 580 | return -EADDRNOTAVAIL; |
4623e8e6 | 581 | |
8c1be37e LP |
582 | if (m->partitions[PARTITION_ROOT_SECONDARY].found) { |
583 | m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY]; | |
584 | zero(m->partitions[PARTITION_ROOT_SECONDARY]); | |
4623e8e6 LP |
585 | |
586 | m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY]; | |
587 | zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); | |
588 | ||
e0f9e7bd LP |
589 | } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) { |
590 | ||
591 | /* If the root has was set, then we won't fallback to a generic node, because the root hash | |
592 | * decides */ | |
593 | if (root_hash) | |
594 | return -EADDRNOTAVAIL; | |
8c1be37e | 595 | |
e0f9e7bd LP |
596 | /* If we didn't find a generic node, then we can't fix this up either */ |
597 | if (!generic_node) | |
598 | return -ENXIO; | |
599 | ||
600 | /* If we didn't find a properly marked root partition, but we did find a single suitable | |
601 | * generic Linux partition, then use this as root partition, if the caller asked for it. */ | |
8c1be37e LP |
602 | if (multiple_generic) |
603 | return -ENOTUNIQ; | |
604 | ||
605 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { | |
606 | .found = true, | |
607 | .rw = generic_rw, | |
608 | .partno = generic_nr, | |
609 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a | 610 | .node = TAKE_PTR(generic_node), |
be30ad41 | 611 | .uuid = generic_uuid, |
8c1be37e | 612 | }; |
e0f9e7bd | 613 | } |
8c1be37e LP |
614 | } |
615 | ||
4623e8e6 | 616 | if (root_hash) { |
e0f9e7bd | 617 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) |
4623e8e6 LP |
618 | return -EADDRNOTAVAIL; |
619 | ||
620 | /* If we found the primary root with the hash, then we definitely want to suppress any secondary root | |
621 | * (which would be weird, after all the root hash should only be assigned to one pair of | |
622 | * partitions... */ | |
623 | m->partitions[PARTITION_ROOT_SECONDARY].found = false; | |
624 | m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false; | |
625 | ||
626 | /* If we found a verity setup, then the root partition is necessarily read-only. */ | |
627 | m->partitions[PARTITION_ROOT].rw = false; | |
628 | ||
629 | m->verity = true; | |
630 | } | |
631 | ||
18b5886e LP |
632 | blkid_free_probe(b); |
633 | b = NULL; | |
634 | ||
8c1be37e LP |
635 | /* Fill in file system types if we don't know them yet. */ |
636 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
18b5886e | 637 | DissectedPartition *p = m->partitions + i; |
8c1be37e | 638 | |
18b5886e | 639 | if (!p->found) |
8c1be37e LP |
640 | continue; |
641 | ||
18b5886e LP |
642 | if (!p->fstype && p->node) { |
643 | r = probe_filesystem(p->node, &p->fstype); | |
7cc84b2c | 644 | if (r < 0 && r != -EUCLEAN) |
18b5886e | 645 | return r; |
8c1be37e LP |
646 | } |
647 | ||
18b5886e LP |
648 | if (streq_ptr(p->fstype, "crypto_LUKS")) |
649 | m->encrypted = true; | |
896f937f LP |
650 | |
651 | if (p->fstype && fstype_is_ro(p->fstype)) | |
652 | p->rw = false; | |
8c1be37e LP |
653 | } |
654 | ||
1cc6c93a | 655 | *ret = TAKE_PTR(m); |
8c1be37e LP |
656 | |
657 | return 0; | |
658 | #else | |
659 | return -EOPNOTSUPP; | |
660 | #endif | |
661 | } | |
662 | ||
663 | DissectedImage* dissected_image_unref(DissectedImage *m) { | |
664 | unsigned i; | |
665 | ||
666 | if (!m) | |
667 | return NULL; | |
668 | ||
669 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
670 | free(m->partitions[i].fstype); | |
671 | free(m->partitions[i].node); | |
18b5886e LP |
672 | free(m->partitions[i].decrypted_fstype); |
673 | free(m->partitions[i].decrypted_node); | |
8c1be37e LP |
674 | } |
675 | ||
3b925504 LP |
676 | free(m->hostname); |
677 | strv_free(m->machine_info); | |
678 | strv_free(m->os_release); | |
679 | ||
5fecf46d | 680 | return mfree(m); |
8c1be37e LP |
681 | } |
682 | ||
18b5886e | 683 | static int is_loop_device(const char *path) { |
553e15f2 | 684 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
685 | struct stat st; |
686 | ||
687 | assert(path); | |
688 | ||
689 | if (stat(path, &st) < 0) | |
690 | return -errno; | |
691 | ||
692 | if (!S_ISBLK(st.st_mode)) | |
693 | return -ENOTBLK; | |
694 | ||
553e15f2 | 695 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
696 | if (access(s, F_OK) < 0) { |
697 | if (errno != ENOENT) | |
698 | return -errno; | |
699 | ||
700 | /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */ | |
553e15f2 | 701 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
702 | if (access(s, F_OK) < 0) |
703 | return errno == ENOENT ? false : -errno; | |
704 | } | |
705 | ||
706 | return true; | |
707 | } | |
708 | ||
709 | static int mount_partition( | |
710 | DissectedPartition *m, | |
711 | const char *where, | |
712 | const char *directory, | |
2d3a5a73 | 713 | uid_t uid_shift, |
18b5886e LP |
714 | DissectImageFlags flags) { |
715 | ||
2d3a5a73 LP |
716 | _cleanup_free_ char *chased = NULL, *options = NULL; |
717 | const char *p, *node, *fstype; | |
8c1be37e | 718 | bool rw; |
2eedfd2d | 719 | int r; |
8c1be37e LP |
720 | |
721 | assert(m); | |
722 | assert(where); | |
723 | ||
18b5886e LP |
724 | node = m->decrypted_node ?: m->node; |
725 | fstype = m->decrypted_fstype ?: m->fstype; | |
726 | ||
727 | if (!m->found || !node || !fstype) | |
8c1be37e LP |
728 | return 0; |
729 | ||
18b5886e LP |
730 | /* Stacked encryption? Yuck */ |
731 | if (streq_ptr(fstype, "crypto_LUKS")) | |
732 | return -ELOOP; | |
733 | ||
734 | rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY); | |
8c1be37e | 735 | |
2eedfd2d LP |
736 | if (directory) { |
737 | r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased); | |
738 | if (r < 0) | |
739 | return r; | |
740 | ||
741 | p = chased; | |
742 | } else | |
8c1be37e LP |
743 | p = where; |
744 | ||
18b5886e | 745 | /* If requested, turn on discard support. */ |
154d2269 | 746 | if (fstype_can_discard(fstype) && |
18b5886e | 747 | ((flags & DISSECT_IMAGE_DISCARD) || |
2d3a5a73 LP |
748 | ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node)))) { |
749 | options = strdup("discard"); | |
750 | if (!options) | |
751 | return -ENOMEM; | |
752 | } | |
753 | ||
754 | if (uid_is_valid(uid_shift) && uid_shift != 0 && fstype_can_uid_gid(fstype)) { | |
755 | _cleanup_free_ char *uid_option = NULL; | |
756 | ||
757 | if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) | |
758 | return -ENOMEM; | |
759 | ||
760 | if (!strextend_with_separator(&options, ",", uid_option, NULL)) | |
761 | return -ENOMEM; | |
762 | } | |
8c1be37e | 763 | |
18b5886e | 764 | return mount_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options); |
8c1be37e LP |
765 | } |
766 | ||
2d3a5a73 | 767 | int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) { |
8c1be37e LP |
768 | int r; |
769 | ||
770 | assert(m); | |
771 | assert(where); | |
772 | ||
773 | if (!m->partitions[PARTITION_ROOT].found) | |
774 | return -ENXIO; | |
775 | ||
2d3a5a73 LP |
776 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { |
777 | r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, flags); | |
778 | if (r < 0) | |
779 | return r; | |
03bcb6d4 LP |
780 | |
781 | if (flags & DISSECT_IMAGE_VALIDATE_OS) { | |
782 | r = path_is_os_tree(where); | |
783 | if (r < 0) | |
784 | return r; | |
785 | if (r == 0) | |
786 | return -EMEDIUMTYPE; | |
787 | } | |
2d3a5a73 LP |
788 | } |
789 | ||
790 | if ((flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY)) | |
791 | return 0; | |
8c1be37e | 792 | |
2d3a5a73 | 793 | r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags); |
8c1be37e LP |
794 | if (r < 0) |
795 | return r; | |
796 | ||
2d3a5a73 | 797 | r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, flags); |
8c1be37e LP |
798 | if (r < 0) |
799 | return r; | |
800 | ||
801 | if (m->partitions[PARTITION_ESP].found) { | |
2eedfd2d | 802 | const char *mp; |
8c1be37e LP |
803 | |
804 | /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */ | |
805 | ||
2eedfd2d LP |
806 | FOREACH_STRING(mp, "/efi", "/boot") { |
807 | _cleanup_free_ char *p = NULL; | |
808 | ||
809 | r = chase_symlinks(mp, where, CHASE_PREFIX_ROOT, &p); | |
8c1be37e | 810 | if (r < 0) |
2eedfd2d LP |
811 | continue; |
812 | ||
813 | r = dir_is_empty(p); | |
814 | if (r > 0) { | |
2d3a5a73 | 815 | r = mount_partition(m->partitions + PARTITION_ESP, where, mp, uid_shift, flags); |
2eedfd2d LP |
816 | if (r < 0) |
817 | return r; | |
818 | } | |
8c1be37e LP |
819 | } |
820 | } | |
821 | ||
822 | return 0; | |
823 | } | |
824 | ||
349cc4a5 | 825 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
826 | typedef struct DecryptedPartition { |
827 | struct crypt_device *device; | |
828 | char *name; | |
829 | bool relinquished; | |
830 | } DecryptedPartition; | |
831 | ||
832 | struct DecryptedImage { | |
833 | DecryptedPartition *decrypted; | |
834 | size_t n_decrypted; | |
835 | size_t n_allocated; | |
836 | }; | |
837 | #endif | |
838 | ||
839 | DecryptedImage* decrypted_image_unref(DecryptedImage* d) { | |
349cc4a5 | 840 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
841 | size_t i; |
842 | int r; | |
843 | ||
844 | if (!d) | |
845 | return NULL; | |
846 | ||
847 | for (i = 0; i < d->n_decrypted; i++) { | |
848 | DecryptedPartition *p = d->decrypted + i; | |
849 | ||
850 | if (p->device && p->name && !p->relinquished) { | |
851 | r = crypt_deactivate(p->device, p->name); | |
852 | if (r < 0) | |
853 | log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name); | |
854 | } | |
855 | ||
856 | if (p->device) | |
857 | crypt_free(p->device); | |
858 | free(p->name); | |
859 | } | |
860 | ||
861 | free(d); | |
862 | #endif | |
863 | return NULL; | |
864 | } | |
865 | ||
349cc4a5 | 866 | #if HAVE_LIBCRYPTSETUP |
4623e8e6 LP |
867 | |
868 | static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) { | |
869 | _cleanup_free_ char *name = NULL, *node = NULL; | |
870 | const char *base; | |
871 | ||
872 | assert(original_node); | |
873 | assert(suffix); | |
874 | assert(ret_name); | |
875 | assert(ret_node); | |
876 | ||
877 | base = strrchr(original_node, '/'); | |
878 | if (!base) | |
879 | return -EINVAL; | |
880 | base++; | |
881 | if (isempty(base)) | |
882 | return -EINVAL; | |
883 | ||
884 | name = strjoin(base, suffix); | |
885 | if (!name) | |
886 | return -ENOMEM; | |
887 | if (!filename_is_valid(name)) | |
888 | return -EINVAL; | |
889 | ||
890 | node = strjoin(crypt_get_dir(), "/", name); | |
891 | if (!node) | |
892 | return -ENOMEM; | |
893 | ||
1cc6c93a YW |
894 | *ret_name = TAKE_PTR(name); |
895 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 896 | |
4623e8e6 LP |
897 | return 0; |
898 | } | |
899 | ||
18b5886e LP |
900 | static int decrypt_partition( |
901 | DissectedPartition *m, | |
902 | const char *passphrase, | |
903 | DissectImageFlags flags, | |
904 | DecryptedImage *d) { | |
905 | ||
906 | _cleanup_free_ char *node = NULL, *name = NULL; | |
294bd454 | 907 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
18b5886e LP |
908 | int r; |
909 | ||
910 | assert(m); | |
911 | assert(d); | |
912 | ||
913 | if (!m->found || !m->node || !m->fstype) | |
914 | return 0; | |
915 | ||
916 | if (!streq(m->fstype, "crypto_LUKS")) | |
917 | return 0; | |
918 | ||
bdd73ac5 ZJS |
919 | if (!passphrase) |
920 | return -ENOKEY; | |
921 | ||
4623e8e6 LP |
922 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
923 | if (r < 0) | |
924 | return r; | |
18b5886e LP |
925 | |
926 | if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1)) | |
927 | return -ENOMEM; | |
928 | ||
929 | r = crypt_init(&cd, m->node); | |
930 | if (r < 0) | |
715cbb81 | 931 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 932 | |
dd59868b | 933 | r = crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
934 | if (r < 0) |
935 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e LP |
936 | |
937 | r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), | |
938 | ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | | |
939 | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); | |
294bd454 | 940 | if (r < 0) { |
715cbb81 | 941 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 942 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 943 | } |
18b5886e | 944 | |
1cc6c93a YW |
945 | d->decrypted[d->n_decrypted].name = TAKE_PTR(name); |
946 | d->decrypted[d->n_decrypted].device = TAKE_PTR(cd); | |
18b5886e LP |
947 | d->n_decrypted++; |
948 | ||
1cc6c93a | 949 | m->decrypted_node = TAKE_PTR(node); |
18b5886e LP |
950 | |
951 | return 0; | |
4623e8e6 LP |
952 | } |
953 | ||
954 | static int verity_partition( | |
955 | DissectedPartition *m, | |
956 | DissectedPartition *v, | |
957 | const void *root_hash, | |
958 | size_t root_hash_size, | |
959 | DissectImageFlags flags, | |
960 | DecryptedImage *d) { | |
961 | ||
962 | _cleanup_free_ char *node = NULL, *name = NULL; | |
294bd454 | 963 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
4623e8e6 LP |
964 | int r; |
965 | ||
966 | assert(m); | |
967 | assert(v); | |
968 | ||
969 | if (!root_hash) | |
970 | return 0; | |
971 | ||
972 | if (!m->found || !m->node || !m->fstype) | |
973 | return 0; | |
974 | if (!v->found || !v->node || !v->fstype) | |
975 | return 0; | |
976 | ||
977 | if (!streq(v->fstype, "DM_verity_hash")) | |
978 | return 0; | |
979 | ||
980 | r = make_dm_name_and_node(m->node, "-verity", &name, &node); | |
981 | if (r < 0) | |
982 | return r; | |
983 | ||
984 | if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1)) | |
985 | return -ENOMEM; | |
986 | ||
987 | r = crypt_init(&cd, v->node); | |
988 | if (r < 0) | |
989 | return r; | |
990 | ||
991 | r = crypt_load(cd, CRYPT_VERITY, NULL); | |
992 | if (r < 0) | |
294bd454 | 993 | return r; |
4623e8e6 LP |
994 | |
995 | r = crypt_set_data_device(cd, m->node); | |
996 | if (r < 0) | |
294bd454 | 997 | return r; |
4623e8e6 LP |
998 | |
999 | r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY); | |
1000 | if (r < 0) | |
294bd454 | 1001 | return r; |
4623e8e6 | 1002 | |
1cc6c93a YW |
1003 | d->decrypted[d->n_decrypted].name = TAKE_PTR(name); |
1004 | d->decrypted[d->n_decrypted].device = TAKE_PTR(cd); | |
4623e8e6 LP |
1005 | d->n_decrypted++; |
1006 | ||
1cc6c93a | 1007 | m->decrypted_node = TAKE_PTR(node); |
4623e8e6 LP |
1008 | |
1009 | return 0; | |
18b5886e LP |
1010 | } |
1011 | #endif | |
1012 | ||
1013 | int dissected_image_decrypt( | |
1014 | DissectedImage *m, | |
1015 | const char *passphrase, | |
4623e8e6 LP |
1016 | const void *root_hash, |
1017 | size_t root_hash_size, | |
18b5886e LP |
1018 | DissectImageFlags flags, |
1019 | DecryptedImage **ret) { | |
1020 | ||
349cc4a5 | 1021 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 1022 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
1023 | unsigned i; |
1024 | int r; | |
1025 | #endif | |
1026 | ||
1027 | assert(m); | |
4623e8e6 | 1028 | assert(root_hash || root_hash_size == 0); |
18b5886e LP |
1029 | |
1030 | /* Returns: | |
1031 | * | |
1032 | * = 0 → There was nothing to decrypt | |
1033 | * > 0 → Decrypted successfully | |
d1c536f5 | 1034 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
1035 | * -EKEYREJECTED → Passed key was not correct |
1036 | */ | |
1037 | ||
4623e8e6 LP |
1038 | if (root_hash && root_hash_size < sizeof(sd_id128_t)) |
1039 | return -EINVAL; | |
1040 | ||
1041 | if (!m->encrypted && !m->verity) { | |
18b5886e LP |
1042 | *ret = NULL; |
1043 | return 0; | |
1044 | } | |
1045 | ||
349cc4a5 | 1046 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1047 | d = new0(DecryptedImage, 1); |
1048 | if (!d) | |
1049 | return -ENOMEM; | |
1050 | ||
1051 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
1052 | DissectedPartition *p = m->partitions + i; | |
4623e8e6 | 1053 | int k; |
18b5886e LP |
1054 | |
1055 | if (!p->found) | |
1056 | continue; | |
1057 | ||
1058 | r = decrypt_partition(p, passphrase, flags, d); | |
1059 | if (r < 0) | |
1060 | return r; | |
1061 | ||
4623e8e6 LP |
1062 | k = PARTITION_VERITY_OF(i); |
1063 | if (k >= 0) { | |
1064 | r = verity_partition(p, m->partitions + k, root_hash, root_hash_size, flags, d); | |
1065 | if (r < 0) | |
1066 | return r; | |
1067 | } | |
1068 | ||
18b5886e LP |
1069 | if (!p->decrypted_fstype && p->decrypted_node) { |
1070 | r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype); | |
7cc84b2c | 1071 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
1072 | return r; |
1073 | } | |
1074 | } | |
1075 | ||
1cc6c93a | 1076 | *ret = TAKE_PTR(d); |
18b5886e LP |
1077 | |
1078 | return 1; | |
1079 | #else | |
1080 | return -EOPNOTSUPP; | |
1081 | #endif | |
1082 | } | |
1083 | ||
1084 | int dissected_image_decrypt_interactively( | |
1085 | DissectedImage *m, | |
1086 | const char *passphrase, | |
4623e8e6 LP |
1087 | const void *root_hash, |
1088 | size_t root_hash_size, | |
18b5886e LP |
1089 | DissectImageFlags flags, |
1090 | DecryptedImage **ret) { | |
1091 | ||
1092 | _cleanup_strv_free_erase_ char **z = NULL; | |
1093 | int n = 3, r; | |
1094 | ||
1095 | if (passphrase) | |
1096 | n--; | |
1097 | ||
1098 | for (;;) { | |
4623e8e6 | 1099 | r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, flags, ret); |
18b5886e LP |
1100 | if (r >= 0) |
1101 | return r; | |
1102 | if (r == -EKEYREJECTED) | |
1103 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
1104 | else if (r != -ENOKEY) { | |
1105 | log_error_errno(r, "Failed to decrypt image: %m"); | |
1106 | return r; | |
1107 | } | |
1108 | ||
1109 | if (--n < 0) { | |
1110 | log_error("Too many retries."); | |
1111 | return -EKEYREJECTED; | |
1112 | } | |
1113 | ||
1114 | z = strv_free(z); | |
1115 | ||
1116 | r = ask_password_auto("Please enter image passphrase!", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z); | |
1117 | if (r < 0) | |
1118 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
1119 | ||
1120 | passphrase = z[0]; | |
1121 | } | |
1122 | } | |
1123 | ||
349cc4a5 | 1124 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1125 | static int deferred_remove(DecryptedPartition *p) { |
1126 | ||
1127 | struct dm_ioctl dm = { | |
1128 | .version = { | |
1129 | DM_VERSION_MAJOR, | |
1130 | DM_VERSION_MINOR, | |
1131 | DM_VERSION_PATCHLEVEL | |
1132 | }, | |
1133 | .data_size = sizeof(dm), | |
1134 | .flags = DM_DEFERRED_REMOVE, | |
1135 | }; | |
1136 | ||
1137 | _cleanup_close_ int fd = -1; | |
1138 | ||
1139 | assert(p); | |
1140 | ||
1141 | /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */ | |
1142 | ||
1143 | fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); | |
1144 | if (fd < 0) | |
1145 | return -errno; | |
1146 | ||
1147 | strncpy(dm.name, p->name, sizeof(dm.name)); | |
1148 | ||
1149 | if (ioctl(fd, DM_DEV_REMOVE, &dm)) | |
1150 | return -errno; | |
1151 | ||
1152 | return 0; | |
1153 | } | |
1154 | #endif | |
1155 | ||
1156 | int decrypted_image_relinquish(DecryptedImage *d) { | |
1157 | ||
349cc4a5 | 1158 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1159 | size_t i; |
1160 | int r; | |
1161 | #endif | |
1162 | ||
1163 | assert(d); | |
1164 | ||
1165 | /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so | |
1166 | * that we don't clean it up ourselves either anymore */ | |
1167 | ||
349cc4a5 | 1168 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1169 | for (i = 0; i < d->n_decrypted; i++) { |
1170 | DecryptedPartition *p = d->decrypted + i; | |
1171 | ||
1172 | if (p->relinquished) | |
1173 | continue; | |
1174 | ||
1175 | r = deferred_remove(p); | |
1176 | if (r < 0) | |
1177 | return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); | |
1178 | ||
1179 | p->relinquished = true; | |
1180 | } | |
1181 | #endif | |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
78ebe980 LP |
1186 | int root_hash_load(const char *image, void **ret, size_t *ret_size) { |
1187 | _cleanup_free_ char *text = NULL; | |
1188 | _cleanup_free_ void *k = NULL; | |
78ebe980 LP |
1189 | size_t l; |
1190 | int r; | |
1191 | ||
1192 | assert(image); | |
1193 | assert(ret); | |
1194 | assert(ret_size); | |
1195 | ||
1196 | if (is_device_path(image)) { | |
1197 | /* If we are asked to load the root hash for a device node, exit early */ | |
1198 | *ret = NULL; | |
1199 | *ret_size = 0; | |
1200 | return 0; | |
1201 | } | |
1202 | ||
41488e1f LP |
1203 | r = getxattr_malloc(image, "user.verity.roothash", &text, true); |
1204 | if (r < 0) { | |
1205 | char *fn, *e, *n; | |
78ebe980 | 1206 | |
41488e1f LP |
1207 | if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT)) |
1208 | return r; | |
78ebe980 | 1209 | |
fbd0b64f | 1210 | fn = newa(char, strlen(image) + STRLEN(".roothash") + 1); |
41488e1f LP |
1211 | n = stpcpy(fn, image); |
1212 | e = endswith(fn, ".raw"); | |
1213 | if (e) | |
1214 | n = e; | |
1215 | ||
1216 | strcpy(n, ".roothash"); | |
1217 | ||
1218 | r = read_one_line_file(fn, &text); | |
1219 | if (r == -ENOENT) { | |
1220 | *ret = NULL; | |
1221 | *ret_size = 0; | |
1222 | return 0; | |
1223 | } | |
1224 | if (r < 0) | |
1225 | return r; | |
78ebe980 | 1226 | } |
78ebe980 LP |
1227 | |
1228 | r = unhexmem(text, strlen(text), &k, &l); | |
1229 | if (r < 0) | |
1230 | return r; | |
1231 | if (l < sizeof(sd_id128_t)) | |
1232 | return -EINVAL; | |
1233 | ||
1cc6c93a | 1234 | *ret = TAKE_PTR(k); |
78ebe980 LP |
1235 | *ret_size = l; |
1236 | ||
78ebe980 LP |
1237 | return 1; |
1238 | } | |
1239 | ||
3b925504 LP |
1240 | int dissected_image_acquire_metadata(DissectedImage *m) { |
1241 | ||
1242 | enum { | |
1243 | META_HOSTNAME, | |
1244 | META_MACHINE_ID, | |
1245 | META_MACHINE_INFO, | |
1246 | META_OS_RELEASE, | |
1247 | _META_MAX, | |
1248 | }; | |
1249 | ||
1250 | static const char *const paths[_META_MAX] = { | |
1251 | [META_HOSTNAME] = "/etc/hostname\0", | |
1252 | [META_MACHINE_ID] = "/etc/machine-id\0", | |
1253 | [META_MACHINE_INFO] = "/etc/machine-info\0", | |
1254 | [META_OS_RELEASE] = "/etc/os-release\0/usr/lib/os-release\0", | |
1255 | }; | |
1256 | ||
1257 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL; | |
1258 | _cleanup_(rmdir_and_freep) char *t = NULL; | |
1259 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
1260 | sd_id128_t machine_id = SD_ID128_NULL; | |
1261 | _cleanup_free_ char *hostname = NULL; | |
1262 | unsigned n_meta_initialized = 0, k; | |
1263 | int fds[2 * _META_MAX], r; | |
3b925504 LP |
1264 | |
1265 | BLOCK_SIGNALS(SIGCHLD); | |
1266 | ||
1267 | assert(m); | |
1268 | ||
1269 | for (; n_meta_initialized < _META_MAX; n_meta_initialized ++) | |
1270 | if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) { | |
1271 | r = -errno; | |
1272 | goto finish; | |
1273 | } | |
1274 | ||
1275 | r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); | |
1276 | if (r < 0) | |
1277 | goto finish; | |
1278 | ||
e2047ba9 | 1279 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 1280 | if (r < 0) |
3b925504 | 1281 | goto finish; |
be39f6ee | 1282 | if (r == 0) { |
03bcb6d4 | 1283 | r = dissected_image_mount(m, t, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_VALIDATE_OS); |
429d4e41 LP |
1284 | if (r < 0) { |
1285 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
3b925504 | 1286 | _exit(EXIT_FAILURE); |
429d4e41 | 1287 | } |
3b925504 LP |
1288 | |
1289 | for (k = 0; k < _META_MAX; k++) { | |
1290 | _cleanup_close_ int fd = -1; | |
1291 | const char *p; | |
1292 | ||
1293 | fds[2*k] = safe_close(fds[2*k]); | |
1294 | ||
1295 | NULSTR_FOREACH(p, paths[k]) { | |
36952d19 | 1296 | fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); |
3b925504 LP |
1297 | if (fd >= 0) |
1298 | break; | |
1299 | } | |
36952d19 LP |
1300 | if (fd < 0) { |
1301 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
3b925504 | 1302 | continue; |
36952d19 | 1303 | } |
3b925504 LP |
1304 | |
1305 | r = copy_bytes(fd, fds[2*k+1], (uint64_t) -1, 0); | |
1306 | if (r < 0) | |
1307 | _exit(EXIT_FAILURE); | |
1308 | ||
1309 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
1310 | } | |
1311 | ||
1312 | _exit(EXIT_SUCCESS); | |
1313 | } | |
1314 | ||
1315 | for (k = 0; k < _META_MAX; k++) { | |
1316 | _cleanup_fclose_ FILE *f = NULL; | |
1317 | ||
1318 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
1319 | ||
1320 | f = fdopen(fds[2*k], "re"); | |
1321 | if (!f) { | |
1322 | r = -errno; | |
1323 | goto finish; | |
1324 | } | |
1325 | ||
1326 | fds[2*k] = -1; | |
1327 | ||
1328 | switch (k) { | |
1329 | ||
1330 | case META_HOSTNAME: | |
1331 | r = read_etc_hostname_stream(f, &hostname); | |
1332 | if (r < 0) | |
1333 | log_debug_errno(r, "Failed to read /etc/hostname: %m"); | |
1334 | ||
1335 | break; | |
1336 | ||
1337 | case META_MACHINE_ID: { | |
1338 | _cleanup_free_ char *line = NULL; | |
1339 | ||
1340 | r = read_line(f, LONG_LINE_MAX, &line); | |
1341 | if (r < 0) | |
1342 | log_debug_errno(r, "Failed to read /etc/machine-id: %m"); | |
1343 | else if (r == 33) { | |
1344 | r = sd_id128_from_string(line, &machine_id); | |
1345 | if (r < 0) | |
1346 | log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line); | |
1347 | } else if (r == 0) | |
1348 | log_debug("/etc/machine-id file is empty."); | |
1349 | else | |
1350 | log_debug("/etc/machine-id has unexpected length %i.", r); | |
1351 | ||
1352 | break; | |
1353 | } | |
1354 | ||
1355 | case META_MACHINE_INFO: | |
1356 | r = load_env_file_pairs(f, "machine-info", NULL, &machine_info); | |
1357 | if (r < 0) | |
1358 | log_debug_errno(r, "Failed to read /etc/machine-info: %m"); | |
1359 | ||
1360 | break; | |
1361 | ||
1362 | case META_OS_RELEASE: | |
1363 | r = load_env_file_pairs(f, "os-release", NULL, &os_release); | |
1364 | if (r < 0) | |
1365 | log_debug_errno(r, "Failed to read OS release file: %m"); | |
1366 | ||
1367 | break; | |
1368 | } | |
1369 | } | |
1370 | ||
2e87a1fd | 1371 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 1372 | child = 0; |
2e87a1fd | 1373 | if (r < 0) |
3b925504 | 1374 | goto finish; |
2e87a1fd LP |
1375 | if (r != EXIT_SUCCESS) |
1376 | return -EPROTO; | |
3b925504 LP |
1377 | |
1378 | free_and_replace(m->hostname, hostname); | |
1379 | m->machine_id = machine_id; | |
1380 | strv_free_and_replace(m->machine_info, machine_info); | |
1381 | strv_free_and_replace(m->os_release, os_release); | |
1382 | ||
1383 | finish: | |
1384 | for (k = 0; k < n_meta_initialized; k++) | |
1385 | safe_close_pair(fds + 2*k); | |
1386 | ||
1387 | return r; | |
1388 | } | |
1389 | ||
4526113f LP |
1390 | int dissect_image_and_warn( |
1391 | int fd, | |
1392 | const char *name, | |
1393 | const void *root_hash, | |
1394 | size_t root_hash_size, | |
1395 | DissectImageFlags flags, | |
1396 | DissectedImage **ret) { | |
1397 | ||
1398 | _cleanup_free_ char *buffer = NULL; | |
1399 | int r; | |
1400 | ||
1401 | if (!name) { | |
1402 | r = fd_get_path(fd, &buffer); | |
1403 | if (r < 0) | |
1404 | return r; | |
1405 | ||
1406 | name = buffer; | |
1407 | } | |
1408 | ||
1409 | r = dissect_image(fd, root_hash, root_hash_size, flags, ret); | |
1410 | ||
1411 | switch (r) { | |
1412 | ||
1413 | case -EOPNOTSUPP: | |
1414 | return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support."); | |
1415 | ||
1416 | case -ENOPKG: | |
1417 | return log_error_errno(r, "Couldn't identify a suitable partition table or file system in '%s'.", name); | |
1418 | ||
1419 | case -EADDRNOTAVAIL: | |
1420 | return log_error_errno(r, "No root partition for specified root hash found in '%s'.", name); | |
1421 | ||
1422 | case -ENOTUNIQ: | |
1423 | return log_error_errno(r, "Multiple suitable root partitions found in image '%s'.", name); | |
1424 | ||
1425 | case -ENXIO: | |
1426 | return log_error_errno(r, "No suitable root partition found in image '%s'.", name); | |
1427 | ||
1428 | case -EPROTONOSUPPORT: | |
1429 | return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name); | |
1430 | ||
1431 | default: | |
1432 | if (r < 0) | |
1433 | return log_error_errno(r, "Failed to dissect image '%s': %m", name); | |
1434 | ||
1435 | return r; | |
1436 | } | |
1437 | } | |
1438 | ||
8c1be37e LP |
1439 | static const char *const partition_designator_table[] = { |
1440 | [PARTITION_ROOT] = "root", | |
1441 | [PARTITION_ROOT_SECONDARY] = "root-secondary", | |
1442 | [PARTITION_HOME] = "home", | |
1443 | [PARTITION_SRV] = "srv", | |
1444 | [PARTITION_ESP] = "esp", | |
1445 | [PARTITION_SWAP] = "swap", | |
4623e8e6 LP |
1446 | [PARTITION_ROOT_VERITY] = "root-verity", |
1447 | [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity", | |
8c1be37e LP |
1448 | }; |
1449 | ||
1450 | DEFINE_STRING_TABLE_LOOKUP(partition_designator, int); |