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