]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8c1be37e | 2 | |
10c1b188 LP |
3 | #if HAVE_VALGRIND_MEMCHECK_H |
4 | #include <valgrind/memcheck.h> | |
5 | #endif | |
6 | ||
01234e1f YW |
7 | #include <linux/dm-ioctl.h> |
8 | #include <linux/loop.h> | |
19df770f | 9 | #include <sys/file.h> |
8c1be37e | 10 | #include <sys/mount.h> |
3b925504 LP |
11 | #include <sys/prctl.h> |
12 | #include <sys/wait.h> | |
f5ea63a5 | 13 | #include <sysexits.h> |
8c1be37e | 14 | |
c2fa92e7 LP |
15 | #if HAVE_OPENSSL |
16 | #include <openssl/err.h> | |
17 | #include <openssl/pem.h> | |
18 | #include <openssl/x509.h> | |
19 | #endif | |
20 | ||
3c1f2cee | 21 | #include "sd-device.h" |
dccca82b LP |
22 | #include "sd-id128.h" |
23 | ||
8c1be37e | 24 | #include "architecture.h" |
18b5886e | 25 | #include "ask-password-api.h" |
8c1be37e | 26 | #include "blkid-util.h" |
18c528e9 | 27 | #include "blockdev-util.h" |
f4351959 | 28 | #include "chase-symlinks.h" |
c2fa92e7 | 29 | #include "conf-files.h" |
3b925504 | 30 | #include "copy.h" |
1e2f3230 | 31 | #include "cryptsetup-util.h" |
3b925504 | 32 | #include "def.h" |
553e15f2 | 33 | #include "device-nodes.h" |
8437c059 | 34 | #include "device-util.h" |
7718ac97 | 35 | #include "discover-image.h" |
8c1be37e | 36 | #include "dissect-image.h" |
a709a315 | 37 | #include "dm-util.h" |
686d13b9 | 38 | #include "env-file.h" |
88b3300f | 39 | #include "env-util.h" |
93f59701 | 40 | #include "extension-release.h" |
18b5886e | 41 | #include "fd-util.h" |
78ebe980 | 42 | #include "fileio.h" |
2eedfd2d | 43 | #include "fs-util.h" |
cf32c486 | 44 | #include "fsck-util.h" |
8c1be37e | 45 | #include "gpt.h" |
78ebe980 | 46 | #include "hexdecoct.h" |
e2054217 | 47 | #include "hostname-setup.h" |
3b925504 | 48 | #include "id128-util.h" |
593fe6c0 | 49 | #include "import-util.h" |
a4e0d617 | 50 | #include "io-util.h" |
35cd0ba5 | 51 | #include "mkdir-label.h" |
8c1be37e | 52 | #include "mount-util.h" |
e4de7287 | 53 | #include "mountpoint-util.h" |
6aa05ebd | 54 | #include "namespace-util.h" |
d8b4d14d | 55 | #include "nulstr-util.h" |
c2fa92e7 | 56 | #include "openssl-util.h" |
d58ad743 | 57 | #include "os-util.h" |
8c1be37e | 58 | #include "path-util.h" |
3b925504 LP |
59 | #include "process-util.h" |
60 | #include "raw-clone.h" | |
81939d9d | 61 | #include "resize-fs.h" |
3b925504 | 62 | #include "signal-util.h" |
8c1be37e | 63 | #include "stat-util.h" |
18b5886e | 64 | #include "stdio-util.h" |
8c1be37e LP |
65 | #include "string-table.h" |
66 | #include "string-util.h" | |
2eedfd2d | 67 | #include "strv.h" |
e4de7287 | 68 | #include "tmpfile-util.h" |
a8040b6d | 69 | #include "udev-util.h" |
2d3a5a73 | 70 | #include "user-util.h" |
41488e1f | 71 | #include "xattr-util.h" |
8c1be37e | 72 | |
28e2641a FF |
73 | /* how many times to wait for the device nodes to appear */ |
74 | #define N_DEVICE_NODE_LIST_ATTEMPTS 10 | |
75 | ||
c34b75a1 | 76 | int probe_filesystem(const char *node, char **ret_fstype) { |
7cc84b2c | 77 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
5238e957 | 78 | * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an |
7cc84b2c ZJS |
79 | * different error otherwise. */ |
80 | ||
349cc4a5 | 81 | #if HAVE_BLKID |
8e766630 | 82 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
18b5886e LP |
83 | const char *fstype; |
84 | int r; | |
85 | ||
995fa2e5 | 86 | errno = 0; |
18b5886e LP |
87 | b = blkid_new_probe_from_filename(node); |
88 | if (!b) | |
66855de7 | 89 | return errno_or_else(ENOMEM); |
18b5886e LP |
90 | |
91 | blkid_probe_enable_superblocks(b, 1); | |
92 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
93 | ||
94 | errno = 0; | |
95 | r = blkid_do_safeprobe(b); | |
1cefb9a6 | 96 | if (r == 1) |
18b5886e | 97 | goto not_found; |
58dfbfbd LP |
98 | if (r == -2) |
99 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
100 | "Results ambiguous for partition %s", node); | |
b382db9f | 101 | if (r != 0) |
1cefb9a6 | 102 | return log_debug_errno(errno_or_else(EIO), "Failed to probe partition %s: %m", node); |
18b5886e LP |
103 | |
104 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
105 | ||
106 | if (fstype) { | |
107 | char *t; | |
108 | ||
1cefb9a6 LP |
109 | log_debug("Probed fstype '%s' on partition %s.", fstype, node); |
110 | ||
18b5886e LP |
111 | t = strdup(fstype); |
112 | if (!t) | |
113 | return -ENOMEM; | |
114 | ||
115 | *ret_fstype = t; | |
116 | return 1; | |
117 | } | |
118 | ||
119 | not_found: | |
1cefb9a6 | 120 | log_debug("No type detected on partition %s", node); |
18b5886e LP |
121 | *ret_fstype = NULL; |
122 | return 0; | |
d1c536f5 ZJS |
123 | #else |
124 | return -EOPNOTSUPP; | |
a75e27eb | 125 | #endif |
d1c536f5 | 126 | } |
18b5886e | 127 | |
40c10d3f | 128 | #if HAVE_BLKID |
0f7c9a3d LP |
129 | static void check_partition_flags( |
130 | const char *node, | |
131 | unsigned long long pflags, | |
132 | unsigned long long supported) { | |
133 | ||
134 | assert(node); | |
135 | ||
136 | /* Mask away all flags supported by this partition's type and the three flags the UEFI spec defines generically */ | |
137 | pflags &= ~(supported | GPT_FLAG_REQUIRED_PARTITION | GPT_FLAG_NO_BLOCK_IO_PROTOCOL | GPT_FLAG_LEGACY_BIOS_BOOTABLE); | |
138 | ||
139 | if (pflags == 0) | |
140 | return; | |
141 | ||
142 | /* If there are other bits set, then log about it, to make things discoverable */ | |
143 | for (unsigned i = 0; i < sizeof(pflags) * 8; i++) { | |
144 | unsigned long long bit = 1ULL << i; | |
145 | if (!FLAGS_SET(pflags, bit)) | |
146 | continue; | |
147 | ||
148 | log_debug("Unexpected partition flag %llu set on %s!", bit, node); | |
149 | } | |
150 | } | |
1b010ae7 | 151 | #endif |
0f7c9a3d | 152 | |
234c2e16 | 153 | static void dissected_partition_done(DissectedPartition *p) { |
1b010ae7 | 154 | assert(p); |
786e3a52 | 155 | |
1b010ae7 LP |
156 | free(p->fstype); |
157 | free(p->node); | |
158 | free(p->label); | |
159 | free(p->decrypted_fstype); | |
160 | free(p->decrypted_node); | |
161 | free(p->mount_options); | |
786e3a52 | 162 | |
1b010ae7 LP |
163 | *p = (DissectedPartition) { |
164 | .partno = -1, | |
165 | .architecture = _ARCHITECTURE_INVALID, | |
166 | }; | |
167 | } | |
786e3a52 | 168 | |
1b010ae7 | 169 | #if HAVE_BLKID |
1b010ae7 LP |
170 | static int make_partition_devname( |
171 | const char *whole_devname, | |
172 | int nr, | |
173 | char **ret) { | |
786e3a52 | 174 | |
1b010ae7 | 175 | bool need_p; |
786e3a52 | 176 | |
1b010ae7 LP |
177 | assert(whole_devname); |
178 | assert(nr > 0); | |
aae22eb3 | 179 | |
1b010ae7 LP |
180 | /* Given a whole block device node name (e.g. /dev/sda or /dev/loop7) generate a partition device |
181 | * name (e.g. /dev/sda7 or /dev/loop7p5). The rule the kernel uses is simple: if whole block device | |
182 | * node name ends in a digit, then suffix a 'p', followed by the partition number. Otherwise, just | |
183 | * suffix the partition number without any 'p'. */ | |
4ba86848 | 184 | |
1b010ae7 LP |
185 | if (isempty(whole_devname)) /* Make sure there *is* a last char */ |
186 | return -EINVAL; | |
08fe0a53 | 187 | |
ff25d338 | 188 | need_p = ascii_isdigit(whole_devname[strlen(whole_devname)-1]); /* Last char a digit? */ |
08fe0a53 | 189 | |
1b010ae7 | 190 | return asprintf(ret, "%s%s%i", whole_devname, need_p ? "p" : "", nr); |
08fe0a53 | 191 | } |
1b010ae7 | 192 | #endif |
08fe0a53 | 193 | |
4526113f LP |
194 | int dissect_image( |
195 | int fd, | |
0b214aa0 | 196 | const char *devname, |
369de26f | 197 | const char *image_path, |
89e62e0b | 198 | const VeritySettings *verity, |
18d73705 | 199 | const MountOptions *mount_options, |
4526113f LP |
200 | DissectImageFlags flags, |
201 | DissectedImage **ret) { | |
8c1be37e | 202 | |
349cc4a5 | 203 | #if HAVE_BLKID |
62ea0ed0 | 204 | sd_id128_t root_uuid = SD_ID128_NULL, root_verity_uuid = SD_ID128_NULL; |
62ea0ed0 | 205 | sd_id128_t usr_uuid = SD_ID128_NULL, usr_verity_uuid = SD_ID128_NULL; |
1f8fb21c | 206 | bool is_gpt, is_mbr, multiple_generic = false, |
de98f631 LP |
207 | generic_rw = false, /* initialize to appease gcc */ |
208 | generic_growfs = false; | |
8c1be37e | 209 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
8e766630 | 210 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
8c1be37e | 211 | _cleanup_free_ char *generic_node = NULL; |
be30ad41 | 212 | sd_id128_t generic_uuid = SD_ID128_NULL; |
0b214aa0 | 213 | const char *pttype = NULL; |
8c1be37e | 214 | blkid_partlist pl; |
1f8fb21c | 215 | int r, generic_nr = -1, n_partitions; |
8c1be37e LP |
216 | |
217 | assert(fd >= 0); | |
0b214aa0 | 218 | assert(devname); |
8c1be37e | 219 | assert(ret); |
a0bff7ea | 220 | assert(!verity || verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
89e62e0b | 221 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
a0bff7ea LP |
222 | assert(!verity || verity->root_hash_sig || verity->root_hash_sig_size == 0); |
223 | assert(!verity || (verity->root_hash || !verity->root_hash_sig)); | |
e7cbe5cb | 224 | assert(!((flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_NO_PARTITION_TABLE))); |
8c1be37e LP |
225 | |
226 | /* Probes a disk image, and returns information about what it found in *ret. | |
227 | * | |
4623e8e6 | 228 | * Returns -ENOPKG if no suitable partition table or file system could be found. |
2679f407 LP |
229 | * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. |
230 | * Returns -ENXIO if we couldn't find any partition suitable as root or /usr partition | |
231 | * Returns -ENOTUNIQ if we only found multiple generic partitions and thus don't know what to do with that */ | |
4623e8e6 | 232 | |
89e62e0b | 233 | if (verity && verity->root_hash) { |
aee36b4e LP |
234 | sd_id128_t fsuuid, vuuid; |
235 | ||
236 | /* If a root hash is supplied, then we use the root partition that has a UUID that match the | |
237 | * first 128bit of the root hash. And we use the verity partition that has a UUID that match | |
238 | * the final 128bit. */ | |
4623e8e6 | 239 | |
89e62e0b | 240 | if (verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
241 | return -EINVAL; |
242 | ||
aee36b4e LP |
243 | memcpy(&fsuuid, verity->root_hash, sizeof(sd_id128_t)); |
244 | memcpy(&vuuid, (const uint8_t*) verity->root_hash + verity->root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t)); | |
4623e8e6 | 245 | |
aee36b4e | 246 | if (sd_id128_is_null(fsuuid)) |
4623e8e6 | 247 | return -EINVAL; |
aee36b4e | 248 | if (sd_id128_is_null(vuuid)) |
4623e8e6 | 249 | return -EINVAL; |
aee36b4e LP |
250 | |
251 | /* If the verity data declares it's for the /usr partition, then search for that, in all | |
252 | * other cases assume it's for the root partition. */ | |
253 | if (verity->designator == PARTITION_USR) { | |
254 | usr_uuid = fsuuid; | |
255 | usr_verity_uuid = vuuid; | |
256 | } else { | |
257 | root_uuid = fsuuid; | |
258 | root_verity_uuid = vuuid; | |
259 | } | |
4623e8e6 | 260 | } |
8c1be37e | 261 | |
8c1be37e LP |
262 | b = blkid_new_probe(); |
263 | if (!b) | |
264 | return -ENOMEM; | |
265 | ||
266 | errno = 0; | |
267 | r = blkid_probe_set_device(b, fd, 0, 0); | |
b382db9f | 268 | if (r != 0) |
66855de7 | 269 | return errno_or_else(ENOMEM); |
8c1be37e | 270 | |
9b6deb03 LP |
271 | if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { |
272 | /* Look for file system superblocks, unless we only shall look for GPT partition tables */ | |
273 | blkid_probe_enable_superblocks(b, 1); | |
274 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE); | |
275 | } | |
276 | ||
8c1be37e LP |
277 | blkid_probe_enable_partitions(b, 1); |
278 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
279 | ||
280 | errno = 0; | |
281 | r = blkid_do_safeprobe(b); | |
59ba6d0c LP |
282 | if (IN_SET(r, -2, 1)) |
283 | return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); | |
b382db9f | 284 | if (r != 0) |
66855de7 | 285 | return errno_or_else(EIO); |
8c1be37e | 286 | |
a4e0d617 | 287 | m = new(DissectedImage, 1); |
8c1be37e LP |
288 | if (!m) |
289 | return -ENOMEM; | |
290 | ||
a4e0d617 LP |
291 | *m = (DissectedImage) { |
292 | .has_init_system = -1, | |
293 | }; | |
294 | ||
369de26f YW |
295 | if (image_path) { |
296 | _cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL; | |
297 | ||
298 | r = path_extract_filename(image_path, &extracted_filename); | |
299 | if (r < 0) | |
300 | return r; | |
301 | ||
302 | r = raw_strip_suffixes(extracted_filename, &name_stripped); | |
593fe6c0 LB |
303 | if (r < 0) |
304 | return r; | |
369de26f YW |
305 | |
306 | if (!image_name_is_valid(name_stripped)) | |
307 | log_debug("Image name %s is not valid, ignoring.", strna(name_stripped)); | |
308 | else | |
309 | m->image_name = TAKE_PTR(name_stripped); | |
593fe6c0 | 310 | } |
369de26f | 311 | |
e7cbe5cb | 312 | if ((!(flags & DISSECT_IMAGE_GPT_ONLY) && |
4b5de5dd | 313 | (flags & DISSECT_IMAGE_GENERIC_ROOT)) || |
e7cbe5cb | 314 | (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)) { |
9b6deb03 | 315 | const char *usage = NULL; |
8c1be37e | 316 | |
aee36b4e LP |
317 | /* If flags permit this, also allow using non-partitioned single-filesystem images */ |
318 | ||
9b6deb03 LP |
319 | (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL); |
320 | if (STRPTR_IN_SET(usage, "filesystem", "crypto")) { | |
18d73705 | 321 | _cleanup_free_ char *t = NULL, *n = NULL, *o = NULL; |
1b010ae7 | 322 | const char *fstype = NULL, *options = NULL; |
8c1be37e | 323 | |
9b6deb03 LP |
324 | /* OK, we have found a file system, that's our root partition then. */ |
325 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
8c1be37e | 326 | |
9b6deb03 LP |
327 | if (fstype) { |
328 | t = strdup(fstype); | |
329 | if (!t) | |
330 | return -ENOMEM; | |
331 | } | |
332 | ||
6c544d14 LP |
333 | n = strdup(devname); |
334 | if (!n) | |
335 | return -ENOMEM; | |
336 | ||
e7cbe5cb | 337 | m->single_file_system = true; |
c3c88d67 LP |
338 | m->encrypted = streq_ptr(fstype, "crypto_LUKS"); |
339 | ||
340 | m->has_verity = verity && verity->data_path; | |
341 | m->verity_ready = m->has_verity && | |
342 | verity->root_hash && | |
343 | (verity->designator < 0 || verity->designator == PARTITION_ROOT); | |
e7cbe5cb | 344 | |
8ee9615e LP |
345 | m->has_verity_sig = false; /* signature not embedded, must be specified */ |
346 | m->verity_sig_ready = m->verity_ready && | |
347 | verity->root_hash_sig; | |
348 | ||
f5215bc8 | 349 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
350 | if (options) { |
351 | o = strdup(options); | |
352 | if (!o) | |
353 | return -ENOMEM; | |
354 | } | |
355 | ||
9b6deb03 LP |
356 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
357 | .found = true, | |
e0d53d52 | 358 | .rw = !m->verity_ready && !fstype_is_ro(fstype), |
9b6deb03 LP |
359 | .partno = -1, |
360 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
361 | .fstype = TAKE_PTR(t), |
362 | .node = TAKE_PTR(n), | |
18d73705 | 363 | .mount_options = TAKE_PTR(o), |
88b3300f LP |
364 | .offset = 0, |
365 | .size = UINT64_MAX, | |
9b6deb03 | 366 | }; |
8c1be37e | 367 | |
1cc6c93a | 368 | *ret = TAKE_PTR(m); |
9b6deb03 LP |
369 | return 0; |
370 | } | |
8c1be37e LP |
371 | } |
372 | ||
373 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
374 | if (!pttype) | |
375 | return -ENOPKG; | |
376 | ||
377 | is_gpt = streq_ptr(pttype, "gpt"); | |
378 | is_mbr = streq_ptr(pttype, "dos"); | |
379 | ||
9b6deb03 | 380 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
381 | return -ENOPKG; |
382 | ||
0903fd26 LP |
383 | /* We support external verity data partitions only if the image has no partition table */ |
384 | if (verity && verity->data_path) | |
385 | return -EBADR; | |
386 | ||
4ba86848 LP |
387 | /* Safety check: refuse block devices that carry a partition table but for which the kernel doesn't |
388 | * do partition scanning. */ | |
389 | r = blockdev_partscan_enabled(fd); | |
390 | if (r < 0) | |
391 | return r; | |
392 | if (r == 0) | |
393 | return -EPROTONOSUPPORT; | |
394 | ||
8c1be37e LP |
395 | errno = 0; |
396 | pl = blkid_probe_get_partitions(b); | |
b382db9f | 397 | if (!pl) |
66855de7 | 398 | return errno_or_else(ENOMEM); |
8c1be37e | 399 | |
4ba86848 LP |
400 | errno = 0; |
401 | n_partitions = blkid_partlist_numof_partitions(pl); | |
402 | if (n_partitions < 0) | |
403 | return errno_or_else(EIO); | |
8c1be37e | 404 | |
4ba86848 | 405 | for (int i = 0; i < n_partitions; i++) { |
1b010ae7 | 406 | _cleanup_free_ char *node = NULL; |
9b6deb03 | 407 | unsigned long long pflags; |
88b3300f | 408 | blkid_loff_t start, size; |
8c1be37e | 409 | blkid_partition pp; |
8c1be37e LP |
410 | int nr; |
411 | ||
4ba86848 LP |
412 | errno = 0; |
413 | pp = blkid_partlist_get_partition(pl, i); | |
414 | if (!pp) | |
415 | return errno_or_else(EIO); | |
aae22eb3 | 416 | |
9b6deb03 | 417 | pflags = blkid_partition_get_flags(pp); |
8c1be37e | 418 | |
4ba86848 | 419 | errno = 0; |
8c1be37e LP |
420 | nr = blkid_partition_get_partno(pp); |
421 | if (nr < 0) | |
4ba86848 | 422 | return errno_or_else(EIO); |
8c1be37e | 423 | |
88b3300f LP |
424 | errno = 0; |
425 | start = blkid_partition_get_start(pp); | |
426 | if (start < 0) | |
427 | return errno_or_else(EIO); | |
428 | ||
429 | assert((uint64_t) start < UINT64_MAX/512); | |
430 | ||
431 | errno = 0; | |
432 | size = blkid_partition_get_size(pp); | |
433 | if (size < 0) | |
434 | return errno_or_else(EIO); | |
435 | ||
436 | assert((uint64_t) size < UINT64_MAX/512); | |
437 | ||
1b010ae7 LP |
438 | r = make_partition_devname(devname, nr, &node); |
439 | if (r < 0) | |
440 | return r; | |
441 | ||
442 | /* So here's the thing: after the main ("whole") block device popped up it might take a while | |
443 | * before the kernel fully probed the partition table. Waiting for that to finish is icky in | |
444 | * userspace. So here's what we do instead. We issue the BLKPG_ADD_PARTITION ioctl to add the | |
445 | * partition ourselves, racing against the kernel. Good thing is: if this call fails with | |
446 | * EBUSY then the kernel was quicker than us, and that's totally OK, the outcome is good for | |
447 | * us: the device node will exist. If OTOH our call was successful we won the race. Which is | |
448 | * also good as the outcome is the same: the partition block device exists, and we can use | |
449 | * it. | |
450 | * | |
451 | * Kernel returns EBUSY if there's already a partition by that number or an overlapping | |
452 | * partition already existent. */ | |
453 | ||
d25697f5 | 454 | r = block_device_add_partition(fd, node, nr, (uint64_t) start * 512, (uint64_t) size * 512); |
1b010ae7 LP |
455 | if (r < 0) { |
456 | if (r != -EBUSY) | |
457 | return log_debug_errno(r, "BLKPG_ADD_PARTITION failed: %m"); | |
458 | ||
459 | log_debug_errno(r, "Kernel was quicker than us in adding partition %i.", nr); | |
460 | } else | |
461 | log_debug("We were quicker than kernel in adding partition %i.", nr); | |
462 | ||
8c1be37e | 463 | if (is_gpt) { |
569a0e42 | 464 | PartitionDesignator designator = _PARTITION_DESIGNATOR_INVALID; |
6b41a7b2 | 465 | Architecture architecture = _ARCHITECTURE_INVALID; |
08fe0a53 | 466 | const char *stype, *sid, *fstype = NULL, *label; |
4623e8e6 | 467 | sd_id128_t type_id, id; |
de98f631 | 468 | bool rw = true, growfs = false; |
8c1be37e | 469 | |
4623e8e6 LP |
470 | sid = blkid_partition_get_uuid(pp); |
471 | if (!sid) | |
472 | continue; | |
473 | if (sd_id128_from_string(sid, &id) < 0) | |
474 | continue; | |
475 | ||
8c1be37e LP |
476 | stype = blkid_partition_get_type_string(pp); |
477 | if (!stype) | |
478 | continue; | |
8c1be37e LP |
479 | if (sd_id128_from_string(stype, &type_id) < 0) |
480 | continue; | |
481 | ||
08fe0a53 LP |
482 | label = blkid_partition_get_name(pp); /* libblkid returns NULL here if empty */ |
483 | ||
8c1be37e | 484 | if (sd_id128_equal(type_id, GPT_HOME)) { |
a48dd347 | 485 | |
de98f631 | 486 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 487 | |
a48dd347 LP |
488 | if (pflags & GPT_FLAG_NO_AUTO) |
489 | continue; | |
490 | ||
8c1be37e | 491 | designator = PARTITION_HOME; |
9b6deb03 | 492 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
de98f631 | 493 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
aee36b4e | 494 | |
8c1be37e | 495 | } else if (sd_id128_equal(type_id, GPT_SRV)) { |
a48dd347 | 496 | |
de98f631 | 497 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 498 | |
a48dd347 LP |
499 | if (pflags & GPT_FLAG_NO_AUTO) |
500 | continue; | |
501 | ||
8c1be37e | 502 | designator = PARTITION_SRV; |
9b6deb03 | 503 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
de98f631 | 504 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
aee36b4e | 505 | |
8c1be37e | 506 | } else if (sd_id128_equal(type_id, GPT_ESP)) { |
a48dd347 | 507 | |
aee36b4e LP |
508 | /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is |
509 | * not defined there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as | |
510 | * recommended by the UEFI spec (See "12.3.3 Number and Location of System | |
511 | * Partitions"). */ | |
a48dd347 LP |
512 | |
513 | if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL) | |
514 | continue; | |
515 | ||
8c1be37e LP |
516 | designator = PARTITION_ESP; |
517 | fstype = "vfat"; | |
a8c47660 LP |
518 | |
519 | } else if (sd_id128_equal(type_id, GPT_XBOOTLDR)) { | |
520 | ||
de98f631 | 521 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 522 | |
a8c47660 LP |
523 | if (pflags & GPT_FLAG_NO_AUTO) |
524 | continue; | |
525 | ||
526 | designator = PARTITION_XBOOTLDR; | |
527 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
de98f631 | 528 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
49ae9d91 DDM |
529 | |
530 | } else if (gpt_partition_type_is_root(type_id)) { | |
4623e8e6 | 531 | |
de98f631 | 532 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 533 | |
a48dd347 LP |
534 | if (pflags & GPT_FLAG_NO_AUTO) |
535 | continue; | |
536 | ||
4623e8e6 LP |
537 | /* If a root ID is specified, ignore everything but the root id */ |
538 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
539 | continue; | |
540 | ||
49ae9d91 DDM |
541 | assert_se((architecture = gpt_partition_type_uuid_to_arch(type_id)) >= 0); |
542 | designator = PARTITION_ROOT_OF_ARCH(architecture); | |
9b6deb03 | 543 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
de98f631 | 544 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
aee36b4e | 545 | |
49ae9d91 | 546 | } else if (gpt_partition_type_is_root_verity(type_id)) { |
4623e8e6 | 547 | |
0f7c9a3d LP |
548 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); |
549 | ||
a48dd347 LP |
550 | if (pflags & GPT_FLAG_NO_AUTO) |
551 | continue; | |
552 | ||
c3c88d67 | 553 | m->has_verity = true; |
4623e8e6 | 554 | |
8ee9615e LP |
555 | /* If no verity configuration is specified, then don't do verity */ |
556 | if (!verity) | |
557 | continue; | |
558 | if (verity->designator >= 0 && verity->designator != PARTITION_ROOT) | |
559 | continue; | |
560 | ||
561 | /* If root hash is specified, then ignore everything but the root id */ | |
562 | if (!sd_id128_is_null(root_verity_uuid) && !sd_id128_equal(root_verity_uuid, id)) | |
4623e8e6 LP |
563 | continue; |
564 | ||
49ae9d91 DDM |
565 | assert_se((architecture = gpt_partition_type_uuid_to_arch(type_id)) >= 0); |
566 | designator = PARTITION_VERITY_OF(PARTITION_ROOT_OF_ARCH(architecture)); | |
4623e8e6 | 567 | fstype = "DM_verity_hash"; |
4623e8e6 | 568 | rw = false; |
8ee9615e | 569 | |
49ae9d91 | 570 | } else if (gpt_partition_type_is_root_verity_sig(type_id)) { |
8ee9615e LP |
571 | |
572 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
573 | ||
574 | if (pflags & GPT_FLAG_NO_AUTO) | |
575 | continue; | |
576 | ||
577 | m->has_verity_sig = true; | |
578 | ||
579 | /* If root hash is specified explicitly, then ignore any embedded signature */ | |
580 | if (!verity) | |
581 | continue; | |
582 | if (verity->designator >= 0 && verity->designator != PARTITION_ROOT) | |
583 | continue; | |
584 | if (verity->root_hash) | |
585 | continue; | |
586 | ||
49ae9d91 DDM |
587 | assert_se((architecture = gpt_partition_type_uuid_to_arch(type_id)) >= 0); |
588 | designator = PARTITION_VERITY_SIG_OF(PARTITION_ROOT_OF_ARCH(architecture)); | |
8ee9615e | 589 | fstype = "verity_hash_signature"; |
4623e8e6 | 590 | rw = false; |
8ee9615e | 591 | |
49ae9d91 | 592 | } else if (gpt_partition_type_is_usr(type_id)) { |
aee36b4e | 593 | |
de98f631 | 594 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
aee36b4e LP |
595 | |
596 | if (pflags & GPT_FLAG_NO_AUTO) | |
597 | continue; | |
598 | ||
599 | /* If a usr ID is specified, ignore everything but the usr id */ | |
600 | if (!sd_id128_is_null(usr_uuid) && !sd_id128_equal(usr_uuid, id)) | |
601 | continue; | |
602 | ||
49ae9d91 DDM |
603 | assert_se((architecture = gpt_partition_type_uuid_to_arch(type_id)) >= 0); |
604 | designator = PARTITION_USR_OF_ARCH(architecture); | |
aee36b4e | 605 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
de98f631 | 606 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
aee36b4e | 607 | |
49ae9d91 | 608 | } else if (gpt_partition_type_is_usr_verity(type_id)) { |
aee36b4e LP |
609 | |
610 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
611 | ||
612 | if (pflags & GPT_FLAG_NO_AUTO) | |
613 | continue; | |
614 | ||
c3c88d67 | 615 | m->has_verity = true; |
aee36b4e | 616 | |
8ee9615e LP |
617 | if (!verity) |
618 | continue; | |
619 | if (verity->designator >= 0 && verity->designator != PARTITION_USR) | |
620 | continue; | |
621 | ||
622 | /* If usr hash is specified, then ignore everything but the usr id */ | |
623 | if (!sd_id128_is_null(usr_verity_uuid) && !sd_id128_equal(usr_verity_uuid, id)) | |
aee36b4e LP |
624 | continue; |
625 | ||
49ae9d91 DDM |
626 | assert_se((architecture = gpt_partition_type_uuid_to_arch(type_id)) >= 0); |
627 | designator = PARTITION_VERITY_OF(PARTITION_USR_OF_ARCH(architecture)); | |
aee36b4e | 628 | fstype = "DM_verity_hash"; |
aee36b4e | 629 | rw = false; |
8ee9615e | 630 | |
49ae9d91 | 631 | } else if (gpt_partition_type_is_usr_verity_sig(type_id)) { |
8ee9615e LP |
632 | |
633 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY); | |
634 | ||
635 | if (pflags & GPT_FLAG_NO_AUTO) | |
636 | continue; | |
637 | ||
638 | m->has_verity_sig = true; | |
639 | ||
640 | /* If usr hash is specified explicitly, then ignore any embedded signature */ | |
641 | if (!verity) | |
642 | continue; | |
643 | if (verity->designator >= 0 && verity->designator != PARTITION_USR) | |
644 | continue; | |
645 | if (verity->root_hash) | |
646 | continue; | |
647 | ||
49ae9d91 DDM |
648 | assert_se((architecture = gpt_partition_type_uuid_to_arch(type_id)) >= 0); |
649 | designator = PARTITION_VERITY_SIG_OF(PARTITION_USR_OF_ARCH(architecture)); | |
8ee9615e | 650 | fstype = "verity_hash_signature"; |
aee36b4e | 651 | rw = false; |
8ee9615e | 652 | |
49ae9d91 | 653 | } else if (sd_id128_equal(type_id, GPT_SWAP)) { |
a48dd347 | 654 | |
0f7c9a3d LP |
655 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO); |
656 | ||
a48dd347 LP |
657 | if (pflags & GPT_FLAG_NO_AUTO) |
658 | continue; | |
659 | ||
8c1be37e | 660 | designator = PARTITION_SWAP; |
aee36b4e | 661 | |
8c1be37e LP |
662 | } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) { |
663 | ||
de98f631 | 664 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 665 | |
a48dd347 LP |
666 | if (pflags & GPT_FLAG_NO_AUTO) |
667 | continue; | |
668 | ||
8c1be37e LP |
669 | if (generic_node) |
670 | multiple_generic = true; | |
671 | else { | |
672 | generic_nr = nr; | |
9b6deb03 | 673 | generic_rw = !(pflags & GPT_FLAG_READ_ONLY); |
de98f631 | 674 | generic_growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
be30ad41 | 675 | generic_uuid = id; |
8c1be37e LP |
676 | generic_node = strdup(node); |
677 | if (!generic_node) | |
678 | return -ENOMEM; | |
679 | } | |
d4dffb85 LP |
680 | |
681 | } else if (sd_id128_equal(type_id, GPT_TMP)) { | |
682 | ||
de98f631 | 683 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 684 | |
d4dffb85 LP |
685 | if (pflags & GPT_FLAG_NO_AUTO) |
686 | continue; | |
687 | ||
688 | designator = PARTITION_TMP; | |
689 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
de98f631 | 690 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
d4dffb85 LP |
691 | |
692 | } else if (sd_id128_equal(type_id, GPT_VAR)) { | |
693 | ||
de98f631 | 694 | check_partition_flags(node, pflags, GPT_FLAG_NO_AUTO|GPT_FLAG_READ_ONLY|GPT_FLAG_GROWFS); |
0f7c9a3d | 695 | |
d4dffb85 LP |
696 | if (pflags & GPT_FLAG_NO_AUTO) |
697 | continue; | |
698 | ||
699 | if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) { | |
700 | sd_id128_t var_uuid; | |
701 | ||
702 | /* For /var we insist that the uuid of the partition matches the | |
703 | * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine | |
704 | * ID. Why? Unlike the other partitions /var is inherently | |
705 | * installation specific, hence we need to be careful not to mount it | |
706 | * in the wrong installation. By hashing the partition UUID from | |
707 | * /etc/machine-id we can securely bind the partition to the | |
708 | * installation. */ | |
709 | ||
710 | r = sd_id128_get_machine_app_specific(GPT_VAR, &var_uuid); | |
711 | if (r < 0) | |
712 | return r; | |
713 | ||
714 | if (!sd_id128_equal(var_uuid, id)) { | |
715 | log_debug("Found a /var/ partition, but its UUID didn't match our expectations, ignoring."); | |
716 | continue; | |
717 | } | |
718 | } | |
719 | ||
720 | designator = PARTITION_VAR; | |
721 | rw = !(pflags & GPT_FLAG_READ_ONLY); | |
de98f631 | 722 | growfs = FLAGS_SET(pflags, GPT_FLAG_GROWFS); |
8c1be37e LP |
723 | } |
724 | ||
725 | if (designator != _PARTITION_DESIGNATOR_INVALID) { | |
0fb5036f | 726 | _cleanup_free_ char *t = NULL, *o = NULL, *l = NULL; |
18d73705 | 727 | const char *options = NULL; |
8c1be37e | 728 | |
08fe0a53 LP |
729 | if (m->partitions[designator].found) { |
730 | /* For most partition types the first one we see wins. Except for the | |
731 | * rootfs and /usr, where we do a version compare of the label, and | |
732 | * let the newest version win. This permits a simple A/B versioning | |
733 | * scheme in OS images. */ | |
734 | ||
735 | if (!PARTITION_DESIGNATOR_VERSIONED(designator) || | |
234c2e16 | 736 | strverscmp_improved(m->partitions[designator].label, label) >= 0) |
08fe0a53 LP |
737 | continue; |
738 | ||
234c2e16 | 739 | dissected_partition_done(m->partitions + designator); |
08fe0a53 | 740 | } |
8c1be37e LP |
741 | |
742 | if (fstype) { | |
743 | t = strdup(fstype); | |
744 | if (!t) | |
745 | return -ENOMEM; | |
746 | } | |
747 | ||
08fe0a53 LP |
748 | if (label) { |
749 | l = strdup(label); | |
750 | if (!l) | |
751 | return -ENOMEM; | |
752 | } | |
753 | ||
f5215bc8 | 754 | options = mount_options_from_designator(mount_options, designator); |
18d73705 LB |
755 | if (options) { |
756 | o = strdup(options); | |
757 | if (!o) | |
758 | return -ENOMEM; | |
759 | } | |
760 | ||
8c1be37e LP |
761 | m->partitions[designator] = (DissectedPartition) { |
762 | .found = true, | |
763 | .partno = nr, | |
764 | .rw = rw, | |
de98f631 | 765 | .growfs = growfs, |
8c1be37e | 766 | .architecture = architecture, |
0fb5036f | 767 | .node = TAKE_PTR(node), |
1cc6c93a | 768 | .fstype = TAKE_PTR(t), |
08fe0a53 | 769 | .label = TAKE_PTR(l), |
be30ad41 | 770 | .uuid = id, |
18d73705 | 771 | .mount_options = TAKE_PTR(o), |
88b3300f LP |
772 | .offset = (uint64_t) start * 512, |
773 | .size = (uint64_t) size * 512, | |
8c1be37e | 774 | }; |
8c1be37e LP |
775 | } |
776 | ||
777 | } else if (is_mbr) { | |
778 | ||
a8c47660 | 779 | switch (blkid_partition_get_type(pp)) { |
8c1be37e | 780 | |
a8c47660 LP |
781 | case 0x83: /* Linux partition */ |
782 | ||
783 | if (pflags != 0x80) /* Bootable flag */ | |
784 | continue; | |
8c1be37e | 785 | |
a8c47660 LP |
786 | if (generic_node) |
787 | multiple_generic = true; | |
788 | else { | |
789 | generic_nr = nr; | |
790 | generic_rw = true; | |
de98f631 | 791 | generic_growfs = false; |
a8c47660 LP |
792 | generic_node = strdup(node); |
793 | if (!generic_node) | |
794 | return -ENOMEM; | |
795 | } | |
796 | ||
797 | break; | |
798 | ||
799 | case 0xEA: { /* Boot Loader Spec extended $BOOT partition */ | |
0fb5036f | 800 | _cleanup_free_ char *o = NULL; |
a8c47660 | 801 | sd_id128_t id = SD_ID128_NULL; |
18d73705 | 802 | const char *sid, *options = NULL; |
a8c47660 LP |
803 | |
804 | /* First one wins */ | |
234c2e16 | 805 | if (m->partitions[PARTITION_XBOOTLDR].found) |
a8c47660 LP |
806 | continue; |
807 | ||
808 | sid = blkid_partition_get_uuid(pp); | |
809 | if (sid) | |
810 | (void) sd_id128_from_string(sid, &id); | |
811 | ||
f5215bc8 | 812 | options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR); |
18d73705 LB |
813 | if (options) { |
814 | o = strdup(options); | |
815 | if (!o) | |
816 | return -ENOMEM; | |
817 | } | |
818 | ||
a8c47660 LP |
819 | m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) { |
820 | .found = true, | |
821 | .partno = nr, | |
822 | .rw = true, | |
de98f631 | 823 | .growfs = false, |
a8c47660 | 824 | .architecture = _ARCHITECTURE_INVALID, |
0fb5036f | 825 | .node = TAKE_PTR(node), |
a8c47660 | 826 | .uuid = id, |
18d73705 | 827 | .mount_options = TAKE_PTR(o), |
88b3300f LP |
828 | .offset = (uint64_t) start * 512, |
829 | .size = (uint64_t) size * 512, | |
a8c47660 LP |
830 | }; |
831 | ||
832 | break; | |
833 | }} | |
8c1be37e LP |
834 | } |
835 | } | |
836 | ||
74cb2db9 | 837 | if (m->partitions[PARTITION_ROOT].found) { |
49ae9d91 DDM |
838 | /* If we found the primary arch, then invalidate the secondary and other arch to avoid any |
839 | * ambiguities, since we never want to mount the secondary or other arch in this case. */ | |
74cb2db9 LP |
840 | m->partitions[PARTITION_ROOT_SECONDARY].found = false; |
841 | m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false; | |
8ee9615e | 842 | m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG].found = false; |
aee36b4e LP |
843 | m->partitions[PARTITION_USR_SECONDARY].found = false; |
844 | m->partitions[PARTITION_USR_SECONDARY_VERITY].found = false; | |
8ee9615e | 845 | m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG].found = false; |
8c1be37e | 846 | |
49ae9d91 DDM |
847 | m->partitions[PARTITION_ROOT_OTHER].found = false; |
848 | m->partitions[PARTITION_ROOT_OTHER_VERITY].found = false; | |
849 | m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG].found = false; | |
850 | m->partitions[PARTITION_USR_OTHER].found = false; | |
851 | m->partitions[PARTITION_USR_OTHER_VERITY].found = false; | |
852 | m->partitions[PARTITION_USR_OTHER_VERITY_SIG].found = false; | |
853 | ||
8ee9615e LP |
854 | } else if (m->partitions[PARTITION_ROOT_VERITY].found || |
855 | m->partitions[PARTITION_ROOT_VERITY_SIG].found) | |
7cf66030 | 856 | return -EADDRNOTAVAIL; /* Verity found but no matching rootfs? Something is off, refuse. */ |
4623e8e6 | 857 | |
7cf66030 | 858 | else if (m->partitions[PARTITION_ROOT_SECONDARY].found) { |
aee36b4e | 859 | |
7cf66030 | 860 | /* No root partition found but there's one for the secondary architecture? Then upgrade |
49ae9d91 DDM |
861 | * secondary arch to first and invalidate the other arch. */ |
862 | ||
863 | log_debug("No root partition found of the native architecture, falling back to a root " | |
864 | "partition of the secondary architecture."); | |
4623e8e6 | 865 | |
7cf66030 LP |
866 | m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY]; |
867 | zero(m->partitions[PARTITION_ROOT_SECONDARY]); | |
868 | m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY]; | |
869 | zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); | |
8ee9615e LP |
870 | m->partitions[PARTITION_ROOT_VERITY_SIG] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]; |
871 | zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG]); | |
aee36b4e | 872 | |
7cf66030 LP |
873 | m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY]; |
874 | zero(m->partitions[PARTITION_USR_SECONDARY]); | |
875 | m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY]; | |
876 | zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]); | |
8ee9615e LP |
877 | m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]; |
878 | zero(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]); | |
e0f9e7bd | 879 | |
49ae9d91 DDM |
880 | m->partitions[PARTITION_ROOT_OTHER].found = false; |
881 | m->partitions[PARTITION_ROOT_OTHER_VERITY].found = false; | |
882 | m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG].found = false; | |
883 | m->partitions[PARTITION_USR_OTHER].found = false; | |
884 | m->partitions[PARTITION_USR_OTHER_VERITY].found = false; | |
885 | m->partitions[PARTITION_USR_OTHER_VERITY_SIG].found = false; | |
886 | ||
8ee9615e LP |
887 | } else if (m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found || |
888 | m->partitions[PARTITION_ROOT_SECONDARY_VERITY_SIG].found) | |
7cf66030 | 889 | return -EADDRNOTAVAIL; /* as above */ |
18d73705 | 890 | |
49ae9d91 DDM |
891 | else if (m->partitions[PARTITION_ROOT_OTHER].found) { |
892 | ||
893 | /* No root or secondary partition found but there's one for another architecture? Then | |
894 | * upgrade the other architecture to first. */ | |
895 | ||
896 | log_debug("No root partition found of the native architecture or the secondary architecture, " | |
897 | "falling back to a root partition of a non-native architecture (%s).", | |
898 | architecture_to_string(m->partitions[PARTITION_ROOT_OTHER].architecture)); | |
899 | ||
900 | m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_OTHER]; | |
901 | zero(m->partitions[PARTITION_ROOT_OTHER]); | |
902 | m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_OTHER_VERITY]; | |
903 | zero(m->partitions[PARTITION_ROOT_OTHER_VERITY]); | |
904 | m->partitions[PARTITION_ROOT_VERITY_SIG] = m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]; | |
905 | zero(m->partitions[PARTITION_ROOT_OTHER_VERITY_SIG]); | |
906 | ||
907 | m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_OTHER]; | |
908 | zero(m->partitions[PARTITION_USR_OTHER]); | |
909 | m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_OTHER_VERITY]; | |
910 | zero(m->partitions[PARTITION_USR_OTHER_VERITY]); | |
911 | m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_OTHER_VERITY_SIG]; | |
912 | zero(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]); | |
913 | } | |
914 | ||
8ee9615e LP |
915 | /* Hmm, we found a signature partition but no Verity data? Something is off. */ |
916 | if (m->partitions[PARTITION_ROOT_VERITY_SIG].found && !m->partitions[PARTITION_ROOT_VERITY].found) | |
917 | return -EADDRNOTAVAIL; | |
7cf66030 | 918 | |
8ee9615e | 919 | if (m->partitions[PARTITION_USR].found) { |
49ae9d91 | 920 | /* Invalidate secondary and other arch /usr/ if we found the primary arch */ |
7cf66030 LP |
921 | m->partitions[PARTITION_USR_SECONDARY].found = false; |
922 | m->partitions[PARTITION_USR_SECONDARY_VERITY].found = false; | |
8ee9615e | 923 | m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG].found = false; |
7cf66030 | 924 | |
49ae9d91 DDM |
925 | m->partitions[PARTITION_USR_OTHER].found = false; |
926 | m->partitions[PARTITION_USR_OTHER_VERITY].found = false; | |
927 | m->partitions[PARTITION_USR_OTHER_VERITY_SIG].found = false; | |
928 | ||
8ee9615e LP |
929 | } else if (m->partitions[PARTITION_USR_VERITY].found || |
930 | m->partitions[PARTITION_USR_VERITY_SIG].found) | |
7cf66030 | 931 | return -EADDRNOTAVAIL; /* as above */ |
8c1be37e | 932 | |
7cf66030 | 933 | else if (m->partitions[PARTITION_USR_SECONDARY].found) { |
e0f9e7bd | 934 | |
49ae9d91 DDM |
935 | log_debug("No usr partition found of the native architecture, falling back to a usr " |
936 | "partition of the secondary architecture."); | |
937 | ||
7cf66030 LP |
938 | /* Upgrade secondary arch to primary */ |
939 | m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_SECONDARY]; | |
940 | zero(m->partitions[PARTITION_USR_SECONDARY]); | |
941 | m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_SECONDARY_VERITY]; | |
942 | zero(m->partitions[PARTITION_USR_SECONDARY_VERITY]); | |
8ee9615e LP |
943 | m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]; |
944 | zero(m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG]); | |
7cf66030 | 945 | |
49ae9d91 DDM |
946 | m->partitions[PARTITION_USR_OTHER].found = false; |
947 | m->partitions[PARTITION_USR_OTHER_VERITY].found = false; | |
948 | m->partitions[PARTITION_USR_OTHER_VERITY_SIG].found = false; | |
949 | ||
8ee9615e LP |
950 | } else if (m->partitions[PARTITION_USR_SECONDARY_VERITY].found || |
951 | m->partitions[PARTITION_USR_SECONDARY_VERITY_SIG].found) | |
7cf66030 LP |
952 | return -EADDRNOTAVAIL; /* as above */ |
953 | ||
49ae9d91 DDM |
954 | else if (m->partitions[PARTITION_USR_OTHER].found) { |
955 | ||
956 | log_debug("No usr partition found of the native architecture or the secondary architecture, " | |
957 | "falling back to a usr partition of a non-native architecture (%s).", | |
958 | architecture_to_string(m->partitions[PARTITION_ROOT_OTHER].architecture)); | |
959 | ||
960 | /* Upgrade other arch to primary */ | |
961 | m->partitions[PARTITION_USR] = m->partitions[PARTITION_USR_OTHER]; | |
962 | zero(m->partitions[PARTITION_USR_OTHER]); | |
963 | m->partitions[PARTITION_USR_VERITY] = m->partitions[PARTITION_USR_OTHER_VERITY]; | |
964 | zero(m->partitions[PARTITION_USR_OTHER_VERITY]); | |
965 | m->partitions[PARTITION_USR_VERITY_SIG] = m->partitions[PARTITION_USR_OTHER_VERITY_SIG]; | |
966 | zero(m->partitions[PARTITION_USR_OTHER_VERITY_SIG]); | |
967 | } | |
968 | ||
8ee9615e LP |
969 | /* Hmm, we found a signature partition but no Verity data? Something is off. */ |
970 | if (m->partitions[PARTITION_USR_VERITY_SIG].found && !m->partitions[PARTITION_USR_VERITY].found) | |
971 | return -EADDRNOTAVAIL; | |
972 | ||
cb241a69 LP |
973 | /* If root and /usr are combined then insist that the architecture matches */ |
974 | if (m->partitions[PARTITION_ROOT].found && | |
975 | m->partitions[PARTITION_USR].found && | |
976 | (m->partitions[PARTITION_ROOT].architecture >= 0 && | |
977 | m->partitions[PARTITION_USR].architecture >= 0 && | |
978 | m->partitions[PARTITION_ROOT].architecture != m->partitions[PARTITION_USR].architecture)) | |
979 | return -EADDRNOTAVAIL; | |
980 | ||
4ab51780 LP |
981 | if (!m->partitions[PARTITION_ROOT].found && |
982 | !m->partitions[PARTITION_USR].found && | |
983 | (flags & DISSECT_IMAGE_GENERIC_ROOT) && | |
00a8b34f | 984 | (!verity || !verity->root_hash || verity->designator != PARTITION_USR)) { |
7cf66030 | 985 | |
1b010ae7 | 986 | /* OK, we found nothing usable, then check if there's a single generic partition, and use |
4b5de5dd LP |
987 | * that. If the root hash was set however, then we won't fall back to a generic node, because |
988 | * the root hash decides. */ | |
7cf66030 LP |
989 | |
990 | /* If we didn't find a properly marked root partition, but we did find a single suitable | |
991 | * generic Linux partition, then use this as root partition, if the caller asked for it. */ | |
992 | if (multiple_generic) | |
993 | return -ENOTUNIQ; | |
994 | ||
4b5de5dd LP |
995 | /* If we didn't find a generic node, then we can't fix this up either */ |
996 | if (generic_node) { | |
997 | _cleanup_free_ char *o = NULL; | |
998 | const char *options; | |
8c1be37e | 999 | |
f5215bc8 | 1000 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
1001 | if (options) { |
1002 | o = strdup(options); | |
1003 | if (!o) | |
1004 | return -ENOMEM; | |
1005 | } | |
1006 | ||
1f8fb21c | 1007 | assert(generic_nr >= 0); |
8c1be37e LP |
1008 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
1009 | .found = true, | |
1010 | .rw = generic_rw, | |
de98f631 | 1011 | .growfs = generic_growfs, |
8c1be37e LP |
1012 | .partno = generic_nr, |
1013 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a | 1014 | .node = TAKE_PTR(generic_node), |
be30ad41 | 1015 | .uuid = generic_uuid, |
18d73705 | 1016 | .mount_options = TAKE_PTR(o), |
88b3300f LP |
1017 | .offset = UINT64_MAX, |
1018 | .size = UINT64_MAX, | |
8c1be37e | 1019 | }; |
e0f9e7bd | 1020 | } |
8c1be37e LP |
1021 | } |
1022 | ||
4b5de5dd LP |
1023 | /* Check if we have a root fs if we are told to do check. /usr alone is fine too, but only if appropriate flag for that is set too */ |
1024 | if (FLAGS_SET(flags, DISSECT_IMAGE_REQUIRE_ROOT) && | |
1025 | !(m->partitions[PARTITION_ROOT].found || (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT)))) | |
1026 | return -ENXIO; | |
1027 | ||
7b32164f LP |
1028 | if (m->partitions[PARTITION_ROOT_VERITY].found) { |
1029 | /* We only support one verity partition per image, i.e. can't do for both /usr and root fs */ | |
1030 | if (m->partitions[PARTITION_USR_VERITY].found) | |
1031 | return -ENOTUNIQ; | |
1032 | ||
1033 | /* We don't support verity enabled root with a split out /usr. Neither with nor without | |
1034 | * verity there. (Note that we do support verity-less root with verity-full /usr, though.) */ | |
1035 | if (m->partitions[PARTITION_USR].found) | |
1036 | return -EADDRNOTAVAIL; | |
1037 | } | |
aee36b4e | 1038 | |
1903defc LP |
1039 | if (verity) { |
1040 | /* If a verity designator is specified, then insist that the matching partition exists */ | |
1041 | if (verity->designator >= 0 && !m->partitions[verity->designator].found) | |
1042 | return -EADDRNOTAVAIL; | |
aee36b4e | 1043 | |
1903defc | 1044 | if (verity->root_hash) { |
8ee9615e LP |
1045 | /* If we have an explicit root hash and found the partitions for it, then we are ready to use |
1046 | * Verity, set things up for it */ | |
1047 | ||
1903defc LP |
1048 | if (verity->designator < 0 || verity->designator == PARTITION_ROOT) { |
1049 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) | |
1050 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1051 | |
1903defc LP |
1052 | /* If we found a verity setup, then the root partition is necessarily read-only. */ |
1053 | m->partitions[PARTITION_ROOT].rw = false; | |
1054 | m->verity_ready = true; | |
1903defc | 1055 | |
f9e0bb61 LP |
1056 | } else { |
1057 | assert(verity->designator == PARTITION_USR); | |
1058 | ||
1903defc LP |
1059 | if (!m->partitions[PARTITION_USR_VERITY].found || !m->partitions[PARTITION_USR].found) |
1060 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1061 | |
1903defc LP |
1062 | m->partitions[PARTITION_USR].rw = false; |
1063 | m->verity_ready = true; | |
1064 | } | |
8ee9615e LP |
1065 | |
1066 | if (m->verity_ready) | |
b98416e1 | 1067 | m->verity_sig_ready = verity->root_hash_sig; |
8ee9615e LP |
1068 | |
1069 | } else if (m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR_VERITY_SIG : PARTITION_ROOT_VERITY_SIG].found) { | |
1070 | ||
1071 | /* If we found an embedded signature partition, we are ready, too. */ | |
1072 | ||
1073 | m->verity_ready = m->verity_sig_ready = true; | |
1074 | m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR : PARTITION_ROOT].rw = false; | |
aee36b4e | 1075 | } |
4623e8e6 LP |
1076 | } |
1077 | ||
18b5886e LP |
1078 | blkid_free_probe(b); |
1079 | b = NULL; | |
1080 | ||
8c1be37e | 1081 | /* Fill in file system types if we don't know them yet. */ |
569a0e42 | 1082 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
18b5886e | 1083 | DissectedPartition *p = m->partitions + i; |
8c1be37e | 1084 | |
18b5886e | 1085 | if (!p->found) |
8c1be37e LP |
1086 | continue; |
1087 | ||
18b5886e LP |
1088 | if (!p->fstype && p->node) { |
1089 | r = probe_filesystem(p->node, &p->fstype); | |
7cc84b2c | 1090 | if (r < 0 && r != -EUCLEAN) |
18b5886e | 1091 | return r; |
8c1be37e LP |
1092 | } |
1093 | ||
18b5886e LP |
1094 | if (streq_ptr(p->fstype, "crypto_LUKS")) |
1095 | m->encrypted = true; | |
896f937f LP |
1096 | |
1097 | if (p->fstype && fstype_is_ro(p->fstype)) | |
1098 | p->rw = false; | |
de98f631 LP |
1099 | |
1100 | if (!p->rw) | |
1101 | p->growfs = false; | |
8c1be37e LP |
1102 | } |
1103 | ||
1cc6c93a | 1104 | *ret = TAKE_PTR(m); |
8c1be37e LP |
1105 | return 0; |
1106 | #else | |
1107 | return -EOPNOTSUPP; | |
1108 | #endif | |
1109 | } | |
1110 | ||
1111 | DissectedImage* dissected_image_unref(DissectedImage *m) { | |
8c1be37e LP |
1112 | if (!m) |
1113 | return NULL; | |
1114 | ||
08fe0a53 | 1115 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) |
234c2e16 | 1116 | dissected_partition_done(m->partitions + i); |
8c1be37e | 1117 | |
593fe6c0 | 1118 | free(m->image_name); |
3b925504 LP |
1119 | free(m->hostname); |
1120 | strv_free(m->machine_info); | |
1121 | strv_free(m->os_release); | |
7718ac97 | 1122 | strv_free(m->extension_release); |
3b925504 | 1123 | |
5fecf46d | 1124 | return mfree(m); |
8c1be37e LP |
1125 | } |
1126 | ||
18b5886e | 1127 | static int is_loop_device(const char *path) { |
553e15f2 | 1128 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
1129 | struct stat st; |
1130 | ||
1131 | assert(path); | |
1132 | ||
1133 | if (stat(path, &st) < 0) | |
1134 | return -errno; | |
1135 | ||
1136 | if (!S_ISBLK(st.st_mode)) | |
1137 | return -ENOTBLK; | |
1138 | ||
553e15f2 | 1139 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
1140 | if (access(s, F_OK) < 0) { |
1141 | if (errno != ENOENT) | |
1142 | return -errno; | |
1143 | ||
1144 | /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */ | |
553e15f2 | 1145 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
1146 | if (access(s, F_OK) < 0) |
1147 | return errno == ENOENT ? false : -errno; | |
1148 | } | |
1149 | ||
1150 | return true; | |
1151 | } | |
1152 | ||
cf32c486 LP |
1153 | static int run_fsck(const char *node, const char *fstype) { |
1154 | int r, exit_status; | |
1155 | pid_t pid; | |
1156 | ||
1157 | assert(node); | |
1158 | assert(fstype); | |
1159 | ||
1160 | r = fsck_exists(fstype); | |
1161 | if (r < 0) { | |
1162 | log_debug_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", fstype); | |
1163 | return 0; | |
1164 | } | |
1165 | if (r == 0) { | |
1166 | log_debug("Not checking partition %s, as fsck for %s does not exist.", node, fstype); | |
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_NULL_STDIO, &pid); | |
1171 | if (r < 0) | |
1172 | return log_debug_errno(r, "Failed to fork off fsck: %m"); | |
1173 | if (r == 0) { | |
1174 | /* Child */ | |
1175 | execl("/sbin/fsck", "/sbin/fsck", "-aT", node, NULL); | |
7e0ed2e9 | 1176 | log_open(); |
cf32c486 LP |
1177 | log_debug_errno(errno, "Failed to execl() fsck: %m"); |
1178 | _exit(FSCK_OPERATIONAL_ERROR); | |
1179 | } | |
1180 | ||
1181 | exit_status = wait_for_terminate_and_check("fsck", pid, 0); | |
1182 | if (exit_status < 0) | |
1183 | return log_debug_errno(exit_status, "Failed to fork off /sbin/fsck: %m"); | |
1184 | ||
1185 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { | |
1186 | log_debug("fsck failed with exit status %i.", exit_status); | |
1187 | ||
1188 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
1189 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), "File system is corrupted, refusing."); | |
1190 | ||
1191 | log_debug("Ignoring fsck error."); | |
1192 | } | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | ||
81939d9d LP |
1197 | static int fs_grow(const char *node_path, const char *mount_path) { |
1198 | _cleanup_close_ int mount_fd = -1, node_fd = -1; | |
81939d9d LP |
1199 | uint64_t size, newsize; |
1200 | int r; | |
1201 | ||
1202 | node_fd = open(node_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
1203 | if (node_fd < 0) | |
1204 | return log_debug_errno(errno, "Failed to open node device %s: %m", node_path); | |
1205 | ||
1206 | if (ioctl(node_fd, BLKGETSIZE64, &size) != 0) | |
1207 | return log_debug_errno(errno, "Failed to get block device size of %s: %m", node_path); | |
1208 | ||
1209 | mount_fd = open(mount_path, O_RDONLY|O_DIRECTORY|O_CLOEXEC); | |
1210 | if (mount_fd < 0) | |
1211 | return log_debug_errno(errno, "Failed to open mountd file system %s: %m", mount_path); | |
1212 | ||
1213 | log_debug("Resizing \"%s\" to %"PRIu64" bytes...", mount_path, size); | |
1214 | r = resize_fs(mount_fd, size, &newsize); | |
1215 | if (r < 0) | |
1216 | return log_debug_errno(r, "Failed to resize \"%s\" to %"PRIu64" bytes: %m", mount_path, size); | |
1217 | ||
1218 | if (newsize == size) | |
1219 | log_debug("Successfully resized \"%s\" to %s bytes.", | |
2b59bf51 | 1220 | mount_path, FORMAT_BYTES(newsize)); |
81939d9d LP |
1221 | else { |
1222 | assert(newsize < size); | |
1223 | log_debug("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).", | |
2b59bf51 | 1224 | mount_path, FORMAT_BYTES(newsize), size - newsize); |
81939d9d LP |
1225 | } |
1226 | ||
1227 | return 0; | |
1228 | } | |
1229 | ||
18b5886e LP |
1230 | static int mount_partition( |
1231 | DissectedPartition *m, | |
1232 | const char *where, | |
1233 | const char *directory, | |
2d3a5a73 | 1234 | uid_t uid_shift, |
21b61b1d | 1235 | uid_t uid_range, |
18b5886e LP |
1236 | DissectImageFlags flags) { |
1237 | ||
2d3a5a73 LP |
1238 | _cleanup_free_ char *chased = NULL, *options = NULL; |
1239 | const char *p, *node, *fstype; | |
21b61b1d | 1240 | bool rw, remap_uid_gid = false; |
2eedfd2d | 1241 | int r; |
8c1be37e LP |
1242 | |
1243 | assert(m); | |
1244 | assert(where); | |
1245 | ||
4dc28665 | 1246 | /* Use decrypted node and matching fstype if available, otherwise use the original device */ |
18b5886e | 1247 | node = m->decrypted_node ?: m->node; |
4dc28665 | 1248 | fstype = m->decrypted_node ? m->decrypted_fstype: m->fstype; |
18b5886e | 1249 | |
4dc28665 | 1250 | if (!m->found || !node) |
8c1be37e | 1251 | return 0; |
4dc28665 LP |
1252 | if (!fstype) |
1253 | return -EAFNOSUPPORT; | |
8c1be37e | 1254 | |
68ac5118 ZJS |
1255 | /* We are looking at an encrypted partition? This either means stacked encryption, or the caller |
1256 | * didn't call dissected_image_decrypt() beforehand. Let's return a recognizable error for this | |
1257 | * case. */ | |
4dc28665 | 1258 | if (streq(fstype, "crypto_LUKS")) |
fa45d12c | 1259 | return -EUNATCH; |
18b5886e | 1260 | |
ef9c184d | 1261 | rw = m->rw && !(flags & DISSECT_IMAGE_MOUNT_READ_ONLY); |
8c1be37e | 1262 | |
cf32c486 LP |
1263 | if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) { |
1264 | r = run_fsck(node, fstype); | |
1265 | if (r < 0) | |
1266 | return r; | |
1267 | } | |
1268 | ||
2eedfd2d | 1269 | if (directory) { |
334eb5b0 LP |
1270 | /* Automatically create missing mount points inside the image, if necessary. */ |
1271 | r = mkdir_p_root(where, directory, uid_shift, (gid_t) uid_shift, 0755); | |
1272 | if (r < 0 && r != -EROFS) | |
1273 | return r; | |
1f0f82f1 | 1274 | |
a5648b80 | 1275 | r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased, NULL); |
2eedfd2d LP |
1276 | if (r < 0) |
1277 | return r; | |
1278 | ||
1279 | p = chased; | |
9842905e LP |
1280 | } else { |
1281 | /* Create top-level mount if missing – but only if this is asked for. This won't modify the | |
1282 | * image (as the branch above does) but the host hierarchy, and the created directory might | |
1283 | * survive our mount in the host hierarchy hence. */ | |
1284 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { | |
1285 | r = mkdir_p(where, 0755); | |
1286 | if (r < 0) | |
1287 | return r; | |
1288 | } | |
1289 | ||
8c1be37e | 1290 | p = where; |
9842905e | 1291 | } |
8c1be37e | 1292 | |
18b5886e | 1293 | /* If requested, turn on discard support. */ |
154d2269 | 1294 | if (fstype_can_discard(fstype) && |
18b5886e | 1295 | ((flags & DISSECT_IMAGE_DISCARD) || |
3afda7c7 | 1296 | ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node) > 0))) { |
2d3a5a73 LP |
1297 | options = strdup("discard"); |
1298 | if (!options) | |
1299 | return -ENOMEM; | |
1300 | } | |
1301 | ||
21b61b1d | 1302 | if (uid_is_valid(uid_shift) && uid_shift != 0) { |
2d3a5a73 | 1303 | |
21b61b1d LP |
1304 | if (fstype_can_uid_gid(fstype)) { |
1305 | _cleanup_free_ char *uid_option = NULL; | |
2d3a5a73 | 1306 | |
21b61b1d LP |
1307 | if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) |
1308 | return -ENOMEM; | |
1309 | ||
1310 | if (!strextend_with_separator(&options, ",", uid_option)) | |
1311 | return -ENOMEM; | |
1312 | } else if (FLAGS_SET(flags, DISSECT_IMAGE_MOUNT_IDMAPPED)) | |
1313 | remap_uid_gid = true; | |
2d3a5a73 | 1314 | } |
8c1be37e | 1315 | |
18d73705 | 1316 | if (!isempty(m->mount_options)) |
c2bc710b | 1317 | if (!strextend_with_separator(&options, ",", m->mount_options)) |
18d73705 LB |
1318 | return -ENOMEM; |
1319 | ||
b620bf33 LP |
1320 | /* So, when you request MS_RDONLY from ext4, then this means nothing. It happily still writes to the |
1321 | * backing storage. What's worse, the BLKRO[GS]ET flag and (in case of loopback devices) | |
1322 | * LO_FLAGS_READ_ONLY don't mean anything, they affect userspace accesses only, and write accesses | |
1323 | * from the upper file system still get propagated through to the underlying file system, | |
1324 | * unrestricted. To actually get ext4/xfs/btrfs to stop writing to the device we need to specify | |
1325 | * "norecovery" as mount option, in addition to MS_RDONLY. Yes, this sucks, since it means we need to | |
1326 | * carry a per file system table here. | |
1327 | * | |
1328 | * Note that this means that we might not be able to mount corrupted file systems as read-only | |
1329 | * anymore (since in some cases the kernel implementations will refuse mounting when corrupted, | |
1330 | * read-only and "norecovery" is specified). But I think for the case of automatically determined | |
1331 | * mount options for loopback devices this is the right choice, since otherwise using the same | |
1332 | * loopback file twice even in read-only mode, is going to fail badly sooner or later. The usecase of | |
1333 | * making reuse of the immutable images "just work" is more relevant to us than having read-only | |
1334 | * access that actually modifies stuff work on such image files. Or to say this differently: if | |
1335 | * people want their file systems to be fixed up they should just open them in writable mode, where | |
1336 | * all these problems don't exist. */ | |
1337 | if (!rw && STRPTR_IN_SET(fstype, "ext3", "ext4", "xfs", "btrfs")) | |
1338 | if (!strextend_with_separator(&options, ",", "norecovery")) | |
1339 | return -ENOMEM; | |
1340 | ||
511a8cfe | 1341 | r = mount_nofollow_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options); |
d9223c07 LP |
1342 | if (r < 0) |
1343 | return r; | |
1344 | ||
81939d9d LP |
1345 | if (rw && m->growfs && FLAGS_SET(flags, DISSECT_IMAGE_GROWFS)) |
1346 | (void) fs_grow(node, p); | |
1347 | ||
21b61b1d | 1348 | if (remap_uid_gid) { |
2b2777ed | 1349 | r = remount_idmap(p, uid_shift, uid_range, UID_INVALID, REMOUNT_IDMAPPING_HOST_ROOT); |
21b61b1d LP |
1350 | if (r < 0) |
1351 | return r; | |
1352 | } | |
1353 | ||
d9223c07 | 1354 | return 1; |
8c1be37e LP |
1355 | } |
1356 | ||
7cf66030 LP |
1357 | static int mount_root_tmpfs(const char *where, uid_t uid_shift, DissectImageFlags flags) { |
1358 | _cleanup_free_ char *options = NULL; | |
1359 | int r; | |
1360 | ||
1361 | assert(where); | |
1362 | ||
1363 | /* For images that contain /usr/ but no rootfs, let's mount rootfs as tmpfs */ | |
1364 | ||
1365 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { | |
1366 | r = mkdir_p(where, 0755); | |
1367 | if (r < 0) | |
1368 | return r; | |
1369 | } | |
1370 | ||
1371 | if (uid_is_valid(uid_shift)) { | |
1372 | if (asprintf(&options, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) | |
1373 | return -ENOMEM; | |
1374 | } | |
1375 | ||
1376 | r = mount_nofollow_verbose(LOG_DEBUG, "rootfs", where, "tmpfs", MS_NODEV, options); | |
1377 | if (r < 0) | |
1378 | return r; | |
1379 | ||
1380 | return 1; | |
1381 | } | |
1382 | ||
21b61b1d LP |
1383 | int dissected_image_mount( |
1384 | DissectedImage *m, | |
1385 | const char *where, | |
1386 | uid_t uid_shift, | |
1387 | uid_t uid_range, | |
1388 | DissectImageFlags flags) { | |
1389 | ||
1f0f82f1 | 1390 | int r, xbootldr_mounted; |
8c1be37e LP |
1391 | |
1392 | assert(m); | |
1393 | assert(where); | |
1394 | ||
fa45d12c LP |
1395 | /* Returns: |
1396 | * | |
1397 | * -ENXIO → No root partition found | |
7718ac97 | 1398 | * -EMEDIUMTYPE → DISSECT_IMAGE_VALIDATE_OS set but no os-release/extension-release file found |
fa45d12c LP |
1399 | * -EUNATCH → Encrypted partition found for which no dm-crypt was set up yet |
1400 | * -EUCLEAN → fsck for file system failed | |
1401 | * -EBUSY → File system already mounted/used elsewhere (kernel) | |
4dc28665 | 1402 | * -EAFNOSUPPORT → File system type not supported or not known |
fa45d12c LP |
1403 | */ |
1404 | ||
7cf66030 LP |
1405 | if (!(m->partitions[PARTITION_ROOT].found || |
1406 | (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT)))) | |
1407 | return -ENXIO; /* Require a root fs or at least a /usr/ fs (the latter is subject to a flag of its own) */ | |
8c1be37e | 1408 | |
2d3a5a73 | 1409 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { |
7cf66030 LP |
1410 | |
1411 | /* First mount the root fs. If there's none we use a tmpfs. */ | |
1412 | if (m->partitions[PARTITION_ROOT].found) | |
21b61b1d | 1413 | r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, uid_range, flags); |
7cf66030 LP |
1414 | else |
1415 | r = mount_root_tmpfs(where, uid_shift, flags); | |
2d3a5a73 LP |
1416 | if (r < 0) |
1417 | return r; | |
aee36b4e | 1418 | |
aee36b4e | 1419 | /* For us mounting root always means mounting /usr as well */ |
21b61b1d | 1420 | r = mount_partition(m->partitions + PARTITION_USR, where, "/usr", uid_shift, uid_range, flags); |
aee36b4e LP |
1421 | if (r < 0) |
1422 | return r; | |
03bcb6d4 | 1423 | |
9ccb531a LB |
1424 | if ((flags & (DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_VALIDATE_OS_EXT)) != 0) { |
1425 | /* If either one of the validation flags are set, ensure that the image qualifies | |
1426 | * as one or the other (or both). */ | |
1427 | bool ok = false; | |
1428 | ||
1429 | if (FLAGS_SET(flags, DISSECT_IMAGE_VALIDATE_OS)) { | |
1430 | r = path_is_os_tree(where); | |
1431 | if (r < 0) | |
1432 | return r; | |
1433 | if (r > 0) | |
1434 | ok = true; | |
1435 | } | |
1436 | if (!ok && FLAGS_SET(flags, DISSECT_IMAGE_VALIDATE_OS_EXT)) { | |
7718ac97 LB |
1437 | r = path_is_extension_tree(where, m->image_name); |
1438 | if (r < 0) | |
1439 | return r; | |
9ccb531a LB |
1440 | if (r > 0) |
1441 | ok = true; | |
7718ac97 | 1442 | } |
9ccb531a LB |
1443 | |
1444 | if (!ok) | |
1445 | return -ENOMEDIUM; | |
03bcb6d4 | 1446 | } |
2d3a5a73 LP |
1447 | } |
1448 | ||
705727fd | 1449 | if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY) |
2d3a5a73 | 1450 | return 0; |
8c1be37e | 1451 | |
21b61b1d | 1452 | r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, uid_range, flags); |
8c1be37e LP |
1453 | if (r < 0) |
1454 | return r; | |
1455 | ||
21b61b1d | 1456 | r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, uid_range, flags); |
8c1be37e LP |
1457 | if (r < 0) |
1458 | return r; | |
1459 | ||
21b61b1d | 1460 | r = mount_partition(m->partitions + PARTITION_VAR, where, "/var", uid_shift, uid_range, flags); |
d4dffb85 LP |
1461 | if (r < 0) |
1462 | return r; | |
1463 | ||
21b61b1d | 1464 | r = mount_partition(m->partitions + PARTITION_TMP, where, "/var/tmp", uid_shift, uid_range, flags); |
d4dffb85 LP |
1465 | if (r < 0) |
1466 | return r; | |
1467 | ||
21b61b1d | 1468 | xbootldr_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, uid_range, flags); |
1f0f82f1 LP |
1469 | if (xbootldr_mounted < 0) |
1470 | return xbootldr_mounted; | |
d9223c07 | 1471 | |
8c1be37e | 1472 | if (m->partitions[PARTITION_ESP].found) { |
1f0f82f1 LP |
1473 | int esp_done = false; |
1474 | ||
d9223c07 LP |
1475 | /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it |
1476 | * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */ | |
8c1be37e | 1477 | |
a5648b80 | 1478 | r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL); |
1f0f82f1 LP |
1479 | if (r < 0) { |
1480 | if (r != -ENOENT) | |
d9223c07 | 1481 | return r; |
8c1be37e | 1482 | |
1f0f82f1 LP |
1483 | /* /efi doesn't exist. Let's see if /boot is suitable then */ |
1484 | ||
1485 | if (!xbootldr_mounted) { | |
1486 | _cleanup_free_ char *p = NULL; | |
2eedfd2d | 1487 | |
1f0f82f1 LP |
1488 | r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p, NULL); |
1489 | if (r < 0) { | |
1490 | if (r != -ENOENT) | |
1491 | return r; | |
db55bbf2 | 1492 | } else if (dir_is_empty(p, /* ignore_hidden_or_backup= */ false) > 0) { |
1f0f82f1 | 1493 | /* It exists and is an empty directory. Let's mount the ESP there. */ |
21b61b1d | 1494 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, uid_range, flags); |
1f0f82f1 LP |
1495 | if (r < 0) |
1496 | return r; | |
1497 | ||
1498 | esp_done = true; | |
1499 | } | |
2eedfd2d | 1500 | } |
8c1be37e | 1501 | } |
1f0f82f1 LP |
1502 | |
1503 | if (!esp_done) { | |
1504 | /* OK, let's mount the ESP now to /efi (possibly creating the dir if missing) */ | |
1505 | ||
21b61b1d | 1506 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, uid_range, flags); |
1f0f82f1 LP |
1507 | if (r < 0) |
1508 | return r; | |
1509 | } | |
8c1be37e LP |
1510 | } |
1511 | ||
1512 | return 0; | |
1513 | } | |
1514 | ||
21b61b1d LP |
1515 | int dissected_image_mount_and_warn( |
1516 | DissectedImage *m, | |
1517 | const char *where, | |
1518 | uid_t uid_shift, | |
1519 | uid_t uid_range, | |
1520 | DissectImageFlags flags) { | |
1521 | ||
af187ab2 LP |
1522 | int r; |
1523 | ||
1524 | assert(m); | |
1525 | assert(where); | |
1526 | ||
21b61b1d | 1527 | r = dissected_image_mount(m, where, uid_shift, uid_range, flags); |
af187ab2 LP |
1528 | if (r == -ENXIO) |
1529 | return log_error_errno(r, "Not root file system found in image."); | |
1530 | if (r == -EMEDIUMTYPE) | |
7718ac97 | 1531 | return log_error_errno(r, "No suitable os-release/extension-release file in image found."); |
af187ab2 LP |
1532 | if (r == -EUNATCH) |
1533 | return log_error_errno(r, "Encrypted file system discovered, but decryption not requested."); | |
1534 | if (r == -EUCLEAN) | |
1535 | return log_error_errno(r, "File system check on image failed."); | |
1536 | if (r == -EBUSY) | |
1537 | return log_error_errno(r, "File system already mounted elsewhere."); | |
4dc28665 LP |
1538 | if (r == -EAFNOSUPPORT) |
1539 | return log_error_errno(r, "File system type not supported or not known."); | |
af187ab2 LP |
1540 | if (r < 0) |
1541 | return log_error_errno(r, "Failed to mount image: %m"); | |
1542 | ||
1543 | return r; | |
1544 | } | |
1545 | ||
349cc4a5 | 1546 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1547 | typedef struct DecryptedPartition { |
1548 | struct crypt_device *device; | |
1549 | char *name; | |
1550 | bool relinquished; | |
1551 | } DecryptedPartition; | |
1552 | ||
1553 | struct DecryptedImage { | |
1554 | DecryptedPartition *decrypted; | |
1555 | size_t n_decrypted; | |
18b5886e LP |
1556 | }; |
1557 | #endif | |
1558 | ||
1559 | DecryptedImage* decrypted_image_unref(DecryptedImage* d) { | |
349cc4a5 | 1560 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1561 | int r; |
1562 | ||
1563 | if (!d) | |
1564 | return NULL; | |
1565 | ||
67f63ee5 | 1566 | for (size_t i = 0; i < d->n_decrypted; i++) { |
18b5886e LP |
1567 | DecryptedPartition *p = d->decrypted + i; |
1568 | ||
1569 | if (p->device && p->name && !p->relinquished) { | |
0d12936d | 1570 | r = sym_crypt_deactivate_by_name(p->device, p->name, 0); |
18b5886e LP |
1571 | if (r < 0) |
1572 | log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name); | |
1573 | } | |
1574 | ||
1575 | if (p->device) | |
0d12936d | 1576 | sym_crypt_free(p->device); |
18b5886e LP |
1577 | free(p->name); |
1578 | } | |
1579 | ||
f91861e4 | 1580 | free(d->decrypted); |
18b5886e LP |
1581 | free(d); |
1582 | #endif | |
1583 | return NULL; | |
1584 | } | |
1585 | ||
349cc4a5 | 1586 | #if HAVE_LIBCRYPTSETUP |
4623e8e6 LP |
1587 | |
1588 | static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) { | |
1589 | _cleanup_free_ char *name = NULL, *node = NULL; | |
1590 | const char *base; | |
1591 | ||
1592 | assert(original_node); | |
1593 | assert(suffix); | |
1594 | assert(ret_name); | |
1595 | assert(ret_node); | |
1596 | ||
1597 | base = strrchr(original_node, '/'); | |
1598 | if (!base) | |
ac1f3ad0 LB |
1599 | base = original_node; |
1600 | else | |
1601 | base++; | |
4623e8e6 LP |
1602 | if (isempty(base)) |
1603 | return -EINVAL; | |
1604 | ||
1605 | name = strjoin(base, suffix); | |
1606 | if (!name) | |
1607 | return -ENOMEM; | |
1608 | if (!filename_is_valid(name)) | |
1609 | return -EINVAL; | |
1610 | ||
0d12936d | 1611 | node = path_join(sym_crypt_get_dir(), name); |
4623e8e6 LP |
1612 | if (!node) |
1613 | return -ENOMEM; | |
1614 | ||
1cc6c93a YW |
1615 | *ret_name = TAKE_PTR(name); |
1616 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 1617 | |
4623e8e6 LP |
1618 | return 0; |
1619 | } | |
1620 | ||
18b5886e LP |
1621 | static int decrypt_partition( |
1622 | DissectedPartition *m, | |
1623 | const char *passphrase, | |
1624 | DissectImageFlags flags, | |
1625 | DecryptedImage *d) { | |
1626 | ||
1627 | _cleanup_free_ char *node = NULL, *name = NULL; | |
0d12936d | 1628 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
18b5886e LP |
1629 | int r; |
1630 | ||
1631 | assert(m); | |
1632 | assert(d); | |
1633 | ||
1634 | if (!m->found || !m->node || !m->fstype) | |
1635 | return 0; | |
1636 | ||
1637 | if (!streq(m->fstype, "crypto_LUKS")) | |
1638 | return 0; | |
1639 | ||
bdd73ac5 ZJS |
1640 | if (!passphrase) |
1641 | return -ENOKEY; | |
1642 | ||
0d12936d LP |
1643 | r = dlopen_cryptsetup(); |
1644 | if (r < 0) | |
1645 | return r; | |
1646 | ||
4623e8e6 LP |
1647 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
1648 | if (r < 0) | |
1649 | return r; | |
18b5886e | 1650 | |
319a4f4b | 1651 | if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1)) |
18b5886e LP |
1652 | return -ENOMEM; |
1653 | ||
0d12936d | 1654 | r = sym_crypt_init(&cd, m->node); |
18b5886e | 1655 | if (r < 0) |
715cbb81 | 1656 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 1657 | |
efc3b12f | 1658 | cryptsetup_enable_logging(cd); |
1887032f | 1659 | |
0d12936d | 1660 | r = sym_crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
1661 | if (r < 0) |
1662 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e | 1663 | |
0d12936d | 1664 | r = sym_crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), |
ef9c184d | 1665 | ((flags & DISSECT_IMAGE_DEVICE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | |
0d12936d | 1666 | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); |
294bd454 | 1667 | if (r < 0) { |
715cbb81 | 1668 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 1669 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 1670 | } |
18b5886e | 1671 | |
94344385 LP |
1672 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
1673 | .name = TAKE_PTR(name), | |
1674 | .device = TAKE_PTR(cd), | |
1675 | }; | |
18b5886e | 1676 | |
1cc6c93a | 1677 | m->decrypted_node = TAKE_PTR(node); |
18b5886e LP |
1678 | |
1679 | return 0; | |
4623e8e6 LP |
1680 | } |
1681 | ||
89e62e0b LP |
1682 | static int verity_can_reuse( |
1683 | const VeritySettings *verity, | |
1684 | const char *name, | |
1685 | struct crypt_device **ret_cd) { | |
1686 | ||
ac1f3ad0 LB |
1687 | /* If the same volume was already open, check that the root hashes match, and reuse it if they do */ |
1688 | _cleanup_free_ char *root_hash_existing = NULL; | |
0d12936d | 1689 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 1690 | struct crypt_params_verity crypt_params = {}; |
89e62e0b | 1691 | size_t root_hash_existing_size; |
ac1f3ad0 LB |
1692 | int r; |
1693 | ||
89e62e0b LP |
1694 | assert(verity); |
1695 | assert(name); | |
ac1f3ad0 LB |
1696 | assert(ret_cd); |
1697 | ||
0d12936d | 1698 | r = sym_crypt_init_by_name(&cd, name); |
ac1f3ad0 LB |
1699 | if (r < 0) |
1700 | return log_debug_errno(r, "Error opening verity device, crypt_init_by_name failed: %m"); | |
0d12936d | 1701 | |
c719805e LP |
1702 | cryptsetup_enable_logging(cd); |
1703 | ||
0d12936d | 1704 | r = sym_crypt_get_verity_info(cd, &crypt_params); |
ac1f3ad0 LB |
1705 | if (r < 0) |
1706 | return log_debug_errno(r, "Error opening verity device, crypt_get_verity_info failed: %m"); | |
0d12936d | 1707 | |
89e62e0b LP |
1708 | root_hash_existing_size = verity->root_hash_size; |
1709 | root_hash_existing = malloc0(root_hash_existing_size); | |
ac1f3ad0 LB |
1710 | if (!root_hash_existing) |
1711 | return -ENOMEM; | |
0d12936d LP |
1712 | |
1713 | r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash_existing, &root_hash_existing_size, NULL, 0); | |
ac1f3ad0 LB |
1714 | if (r < 0) |
1715 | return log_debug_errno(r, "Error opening verity device, crypt_volume_key_get failed: %m"); | |
89e62e0b LP |
1716 | if (verity->root_hash_size != root_hash_existing_size || |
1717 | memcmp(root_hash_existing, verity->root_hash, verity->root_hash_size) != 0) | |
ac1f3ad0 | 1718 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but root hashes are different."); |
89e62e0b | 1719 | |
ac1f3ad0 | 1720 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY |
89e62e0b LP |
1721 | /* Ensure that, if signatures are supported, we only reuse the device if the previous mount used the |
1722 | * same settings, so that a previous unsigned mount will not be reused if the user asks to use | |
28423d9a | 1723 | * signing for the new one, and vice versa. */ |
89e62e0b | 1724 | if (!!verity->root_hash_sig != !!(crypt_params.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE)) |
ac1f3ad0 LB |
1725 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but signature settings are not the same."); |
1726 | #endif | |
1727 | ||
1728 | *ret_cd = TAKE_PTR(cd); | |
1729 | return 0; | |
1730 | } | |
1731 | ||
75db809a | 1732 | static inline char* dm_deferred_remove_clean(char *name) { |
ac1f3ad0 | 1733 | if (!name) |
75db809a | 1734 | return NULL; |
0d12936d LP |
1735 | |
1736 | (void) sym_crypt_deactivate_by_name(NULL, name, CRYPT_DEACTIVATE_DEFERRED); | |
75db809a | 1737 | return mfree(name); |
ac1f3ad0 LB |
1738 | } |
1739 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, dm_deferred_remove_clean); | |
1740 | ||
c2fa92e7 LP |
1741 | static int validate_signature_userspace(const VeritySettings *verity) { |
1742 | #if HAVE_OPENSSL | |
1743 | _cleanup_(sk_X509_free_allp) STACK_OF(X509) *sk = NULL; | |
1744 | _cleanup_strv_free_ char **certs = NULL; | |
1745 | _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL; | |
1746 | _cleanup_free_ char *s = NULL; | |
1747 | _cleanup_(BIO_freep) BIO *bio = NULL; /* 'bio' must be freed first, 's' second, hence keep this order | |
1748 | * of declaration in place, please */ | |
1749 | const unsigned char *d; | |
c2fa92e7 LP |
1750 | int r; |
1751 | ||
1752 | assert(verity); | |
1753 | assert(verity->root_hash); | |
1754 | assert(verity->root_hash_sig); | |
1755 | ||
1756 | /* Because installing a signature certificate into the kernel chain is so messy, let's optionally do | |
1757 | * userspace validation. */ | |
1758 | ||
1759 | r = conf_files_list_nulstr(&certs, ".crt", NULL, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, CONF_PATHS_NULSTR("verity.d")); | |
1760 | if (r < 0) | |
1761 | return log_debug_errno(r, "Failed to enumerate certificates: %m"); | |
1762 | if (strv_isempty(certs)) { | |
1763 | log_debug("No userspace dm-verity certificates found."); | |
1764 | return 0; | |
1765 | } | |
1766 | ||
1767 | d = verity->root_hash_sig; | |
1768 | p7 = d2i_PKCS7(NULL, &d, (long) verity->root_hash_sig_size); | |
1769 | if (!p7) | |
1770 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse PKCS7 DER signature data."); | |
1771 | ||
1772 | s = hexmem(verity->root_hash, verity->root_hash_size); | |
1773 | if (!s) | |
1774 | return log_oom_debug(); | |
1775 | ||
1776 | bio = BIO_new_mem_buf(s, strlen(s)); | |
1777 | if (!bio) | |
1778 | return log_oom_debug(); | |
1779 | ||
1780 | sk = sk_X509_new_null(); | |
1781 | if (!sk) | |
1782 | return log_oom_debug(); | |
1783 | ||
1784 | STRV_FOREACH(i, certs) { | |
1785 | _cleanup_(X509_freep) X509 *c = NULL; | |
1786 | _cleanup_fclose_ FILE *f = NULL; | |
1787 | ||
1788 | f = fopen(*i, "re"); | |
1789 | if (!f) { | |
1790 | log_debug_errno(errno, "Failed to open '%s', ignoring: %m", *i); | |
1791 | continue; | |
1792 | } | |
1793 | ||
1794 | c = PEM_read_X509(f, NULL, NULL, NULL); | |
1795 | if (!c) { | |
1796 | log_debug("Failed to load X509 certificate '%s', ignoring.", *i); | |
1797 | continue; | |
1798 | } | |
1799 | ||
1800 | if (sk_X509_push(sk, c) == 0) | |
1801 | return log_oom_debug(); | |
1802 | ||
1803 | TAKE_PTR(c); | |
1804 | } | |
1805 | ||
1806 | r = PKCS7_verify(p7, sk, NULL, bio, NULL, PKCS7_NOINTERN|PKCS7_NOVERIFY); | |
1807 | if (r) | |
1808 | log_debug("Userspace PKCS#7 validation succeeded."); | |
1809 | else | |
1810 | log_debug("Userspace PKCS#7 validation failed: %s", ERR_error_string(ERR_get_error(), NULL)); | |
1811 | ||
1812 | return r; | |
1813 | #else | |
1814 | log_debug("Not doing client-side validation of dm-verity root hash signatures, OpenSSL support disabled."); | |
1815 | return 0; | |
1816 | #endif | |
1817 | } | |
1818 | ||
1819 | static int do_crypt_activate_verity( | |
1820 | struct crypt_device *cd, | |
1821 | const char *name, | |
1822 | const VeritySettings *verity) { | |
1823 | ||
1824 | bool check_signature; | |
1825 | int r; | |
1826 | ||
1827 | assert(cd); | |
1828 | assert(name); | |
1829 | assert(verity); | |
1830 | ||
1831 | if (verity->root_hash_sig) { | |
1832 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_SIGNATURE"); | |
1833 | if (r < 0 && r != -ENXIO) | |
1834 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_SIGNATURE"); | |
1835 | ||
1836 | check_signature = r != 0; | |
1837 | } else | |
1838 | check_signature = false; | |
1839 | ||
1840 | if (check_signature) { | |
1841 | ||
1842 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY | |
1843 | /* First, if we have support for signed keys in the kernel, then try that first. */ | |
1844 | r = sym_crypt_activate_by_signed_key( | |
1845 | cd, | |
1846 | name, | |
1847 | verity->root_hash, | |
1848 | verity->root_hash_size, | |
1849 | verity->root_hash_sig, | |
1850 | verity->root_hash_sig_size, | |
1851 | CRYPT_ACTIVATE_READONLY); | |
1852 | if (r >= 0) | |
1853 | return r; | |
1854 | ||
1855 | log_debug("Validation of dm-verity signature failed via the kernel, trying userspace validation instead."); | |
1856 | #else | |
1857 | log_debug("Activation of verity device with signature requested, but not supported via the kernel by %s due to missing crypt_activate_by_signed_key(), trying userspace validation instead.", | |
1858 | program_invocation_short_name); | |
1859 | #endif | |
1860 | ||
1861 | /* So this didn't work via the kernel, then let's try userspace validation instead. If that | |
1862 | * works we'll try to activate without telling the kernel the signature. */ | |
1863 | ||
1864 | r = validate_signature_userspace(verity); | |
1865 | if (r < 0) | |
1866 | return r; | |
1867 | if (r == 0) | |
1868 | return log_debug_errno(SYNTHETIC_ERRNO(ENOKEY), | |
1869 | "Activation of signed Verity volume worked neither via the kernel nor in userspace, can't activate."); | |
1870 | } | |
1871 | ||
1872 | return sym_crypt_activate_by_volume_key( | |
1873 | cd, | |
1874 | name, | |
1875 | verity->root_hash, | |
1876 | verity->root_hash_size, | |
1877 | CRYPT_ACTIVATE_READONLY); | |
1878 | } | |
1879 | ||
4623e8e6 | 1880 | static int verity_partition( |
aee36b4e | 1881 | PartitionDesignator designator, |
4623e8e6 LP |
1882 | DissectedPartition *m, |
1883 | DissectedPartition *v, | |
89e62e0b | 1884 | const VeritySettings *verity, |
4623e8e6 LP |
1885 | DissectImageFlags flags, |
1886 | DecryptedImage *d) { | |
1887 | ||
0d12936d | 1888 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 1889 | _cleanup_(dm_deferred_remove_cleanp) char *restore_deferred_remove = NULL; |
89e62e0b | 1890 | _cleanup_free_ char *node = NULL, *name = NULL; |
4623e8e6 LP |
1891 | int r; |
1892 | ||
1893 | assert(m); | |
89e62e0b | 1894 | assert(v || (verity && verity->data_path)); |
4623e8e6 | 1895 | |
89e62e0b | 1896 | if (!verity || !verity->root_hash) |
4623e8e6 | 1897 | return 0; |
aee36b4e LP |
1898 | if (!((verity->designator < 0 && designator == PARTITION_ROOT) || |
1899 | (verity->designator == designator))) | |
1900 | return 0; | |
4623e8e6 LP |
1901 | |
1902 | if (!m->found || !m->node || !m->fstype) | |
1903 | return 0; | |
89e62e0b | 1904 | if (!verity->data_path) { |
e7cbe5cb LB |
1905 | if (!v->found || !v->node || !v->fstype) |
1906 | return 0; | |
4623e8e6 | 1907 | |
e7cbe5cb LB |
1908 | if (!streq(v->fstype, "DM_verity_hash")) |
1909 | return 0; | |
1910 | } | |
4623e8e6 | 1911 | |
0d12936d LP |
1912 | r = dlopen_cryptsetup(); |
1913 | if (r < 0) | |
1914 | return r; | |
1915 | ||
ac1f3ad0 LB |
1916 | if (FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) { |
1917 | /* Use the roothash, which is unique per volume, as the device node name, so that it can be reused */ | |
1918 | _cleanup_free_ char *root_hash_encoded = NULL; | |
0d12936d | 1919 | |
89e62e0b | 1920 | root_hash_encoded = hexmem(verity->root_hash, verity->root_hash_size); |
ac1f3ad0 LB |
1921 | if (!root_hash_encoded) |
1922 | return -ENOMEM; | |
aee36b4e | 1923 | |
ac1f3ad0 LB |
1924 | r = make_dm_name_and_node(root_hash_encoded, "-verity", &name, &node); |
1925 | } else | |
1926 | r = make_dm_name_and_node(m->node, "-verity", &name, &node); | |
4623e8e6 LP |
1927 | if (r < 0) |
1928 | return r; | |
1929 | ||
89e62e0b | 1930 | r = sym_crypt_init(&cd, verity->data_path ?: v->node); |
4623e8e6 LP |
1931 | if (r < 0) |
1932 | return r; | |
1933 | ||
efc3b12f | 1934 | cryptsetup_enable_logging(cd); |
1887032f | 1935 | |
0d12936d | 1936 | r = sym_crypt_load(cd, CRYPT_VERITY, NULL); |
4623e8e6 | 1937 | if (r < 0) |
294bd454 | 1938 | return r; |
4623e8e6 | 1939 | |
0d12936d | 1940 | r = sym_crypt_set_data_device(cd, m->node); |
4623e8e6 | 1941 | if (r < 0) |
294bd454 | 1942 | return r; |
4623e8e6 | 1943 | |
319a4f4b | 1944 | if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1)) |
ac1f3ad0 LB |
1945 | return -ENOMEM; |
1946 | ||
1947 | /* If activating fails because the device already exists, check the metadata and reuse it if it matches. | |
1948 | * In case of ENODEV/ENOENT, which can happen if another process is activating at the exact same time, | |
1949 | * retry a few times before giving up. */ | |
1950 | for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { | |
c2fa92e7 LP |
1951 | |
1952 | r = do_crypt_activate_verity(cd, name, verity); | |
ac1f3ad0 LB |
1953 | /* libdevmapper can return EINVAL when the device is already in the activation stage. |
1954 | * There's no way to distinguish this situation from a genuine error due to invalid | |
2aed63f4 | 1955 | * parameters, so immediately fall back to activating the device with a unique name. |
89e62e0b LP |
1956 | * Improvements in libcrypsetup can ensure this never happens: |
1957 | * https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/96 */ | |
ac1f3ad0 | 1958 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) |
aee36b4e | 1959 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
5bf5013f YW |
1960 | if (r < 0 && !IN_SET(r, |
1961 | -EEXIST, /* Volume is already open and ready to be used */ | |
1962 | -EBUSY, /* Volume is being opened but not ready, crypt_init_by_name can fetch details */ | |
1963 | -ENODEV /* Volume is being opened but not ready, crypt_init_by_name would fail, try to open again */)) | |
ac1f3ad0 | 1964 | return r; |
9ecb5c10 | 1965 | if (IN_SET(r, -EEXIST, -EBUSY)) { |
041e2eda | 1966 | _cleanup_(sym_crypt_freep) struct crypt_device *existing_cd = NULL; |
c2923fdc | 1967 | |
ac1f3ad0 LB |
1968 | if (!restore_deferred_remove){ |
1969 | /* To avoid races, disable automatic removal on umount while setting up the new device. Restore it on failure. */ | |
1970 | r = dm_deferred_remove_cancel(name); | |
9ecb5c10 LB |
1971 | /* If activation returns EBUSY there might be no deferred removal to cancel, that's fine */ |
1972 | if (r < 0 && r != -ENXIO) | |
ac1f3ad0 | 1973 | return log_debug_errno(r, "Disabling automated deferred removal for verity device %s failed: %m", node); |
5bf5013f | 1974 | if (r >= 0) { |
9ecb5c10 LB |
1975 | restore_deferred_remove = strdup(name); |
1976 | if (!restore_deferred_remove) | |
1977 | return -ENOMEM; | |
1978 | } | |
ac1f3ad0 | 1979 | } |
c2923fdc | 1980 | |
89e62e0b | 1981 | r = verity_can_reuse(verity, name, &existing_cd); |
ac1f3ad0 LB |
1982 | /* Same as above, -EINVAL can randomly happen when it actually means -EEXIST */ |
1983 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
aee36b4e | 1984 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
5bf5013f | 1985 | if (r < 0 && !IN_SET(r, -ENODEV, -ENOENT, -EBUSY)) |
ac1f3ad0 | 1986 | return log_debug_errno(r, "Checking whether existing verity device %s can be reused failed: %m", node); |
5bf5013f | 1987 | if (r >= 0) { |
2b660510 YW |
1988 | usec_t timeout_usec = 100 * USEC_PER_MSEC; |
1989 | const char *e; | |
1990 | ||
1991 | /* On slower machines, like non-KVM vm, setting up device may take a long time. | |
1992 | * Let's make the timeout configurable. */ | |
1993 | e = getenv("SYSTEMD_DISSECT_VERITY_TIMEOUT_SEC"); | |
1994 | if (e) { | |
1995 | usec_t t; | |
1996 | ||
1997 | r = parse_sec(e, &t); | |
1998 | if (r < 0) | |
1999 | log_debug_errno(r, | |
2000 | "Failed to parse timeout specified in $SYSTEMD_DISSECT_VERITY_TIMEOUT_SEC, " | |
2001 | "using the default timeout (%s).", | |
2002 | FORMAT_TIMESPAN(timeout_usec, USEC_PER_MSEC)); | |
2003 | else | |
2004 | timeout_usec = t; | |
2005 | } | |
2006 | ||
c419b6f0 LB |
2007 | /* devmapper might say that the device exists, but the devlink might not yet have been |
2008 | * created. Check and wait for the udev event in that case. */ | |
2b660510 | 2009 | r = device_wait_for_devlink(node, "block", timeout_usec, NULL); |
c419b6f0 LB |
2010 | /* Fallback to activation with a unique device if it's taking too long */ |
2011 | if (r == -ETIMEDOUT) | |
2012 | break; | |
2013 | if (r < 0) | |
2014 | return r; | |
2015 | ||
041e2eda YW |
2016 | sym_crypt_free(cd); |
2017 | cd = TAKE_PTR(existing_cd); | |
ac1f3ad0 | 2018 | } |
c2923fdc | 2019 | } |
5bf5013f | 2020 | if (r >= 0) |
ac1f3ad0 | 2021 | break; |
ecab4c47 LB |
2022 | |
2023 | /* Device is being opened by another process, but it has not finished yet, yield for 2ms */ | |
2024 | (void) usleep(2 * USEC_PER_MSEC); | |
ac1f3ad0 LB |
2025 | } |
2026 | ||
ac1f3ad0 LB |
2027 | /* An existing verity device was reported by libcryptsetup/libdevmapper, but we can't use it at this time. |
2028 | * Fall back to activating it with a unique device name. */ | |
5bf5013f | 2029 | if (r < 0 && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) |
aee36b4e | 2030 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
ac1f3ad0 LB |
2031 | |
2032 | /* Everything looks good and we'll be able to mount the device, so deferred remove will be re-enabled at that point. */ | |
2033 | restore_deferred_remove = mfree(restore_deferred_remove); | |
4623e8e6 | 2034 | |
94344385 LP |
2035 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
2036 | .name = TAKE_PTR(name), | |
2037 | .device = TAKE_PTR(cd), | |
2038 | }; | |
4623e8e6 | 2039 | |
1cc6c93a | 2040 | m->decrypted_node = TAKE_PTR(node); |
4623e8e6 LP |
2041 | |
2042 | return 0; | |
18b5886e LP |
2043 | } |
2044 | #endif | |
2045 | ||
2046 | int dissected_image_decrypt( | |
2047 | DissectedImage *m, | |
2048 | const char *passphrase, | |
89e62e0b | 2049 | const VeritySettings *verity, |
18b5886e LP |
2050 | DissectImageFlags flags, |
2051 | DecryptedImage **ret) { | |
2052 | ||
349cc4a5 | 2053 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 2054 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
2055 | int r; |
2056 | #endif | |
2057 | ||
2058 | assert(m); | |
89e62e0b | 2059 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
18b5886e LP |
2060 | |
2061 | /* Returns: | |
2062 | * | |
2063 | * = 0 → There was nothing to decrypt | |
2064 | * > 0 → Decrypted successfully | |
d1c536f5 | 2065 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
2066 | * -EKEYREJECTED → Passed key was not correct |
2067 | */ | |
2068 | ||
89e62e0b | 2069 | if (verity && verity->root_hash && verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
2070 | return -EINVAL; |
2071 | ||
c3c88d67 | 2072 | if (!m->encrypted && !m->verity_ready) { |
18b5886e LP |
2073 | *ret = NULL; |
2074 | return 0; | |
2075 | } | |
2076 | ||
349cc4a5 | 2077 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
2078 | d = new0(DecryptedImage, 1); |
2079 | if (!d) | |
2080 | return -ENOMEM; | |
2081 | ||
569a0e42 | 2082 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
18b5886e | 2083 | DissectedPartition *p = m->partitions + i; |
22043172 | 2084 | PartitionDesignator k; |
18b5886e LP |
2085 | |
2086 | if (!p->found) | |
2087 | continue; | |
2088 | ||
2089 | r = decrypt_partition(p, passphrase, flags, d); | |
2090 | if (r < 0) | |
2091 | return r; | |
2092 | ||
4623e8e6 LP |
2093 | k = PARTITION_VERITY_OF(i); |
2094 | if (k >= 0) { | |
aee36b4e | 2095 | r = verity_partition(i, p, m->partitions + k, verity, flags | DISSECT_IMAGE_VERITY_SHARE, d); |
4623e8e6 LP |
2096 | if (r < 0) |
2097 | return r; | |
2098 | } | |
2099 | ||
18b5886e LP |
2100 | if (!p->decrypted_fstype && p->decrypted_node) { |
2101 | r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype); | |
7cc84b2c | 2102 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
2103 | return r; |
2104 | } | |
2105 | } | |
2106 | ||
1cc6c93a | 2107 | *ret = TAKE_PTR(d); |
18b5886e LP |
2108 | |
2109 | return 1; | |
2110 | #else | |
2111 | return -EOPNOTSUPP; | |
2112 | #endif | |
2113 | } | |
2114 | ||
2115 | int dissected_image_decrypt_interactively( | |
2116 | DissectedImage *m, | |
2117 | const char *passphrase, | |
89e62e0b | 2118 | const VeritySettings *verity, |
18b5886e LP |
2119 | DissectImageFlags flags, |
2120 | DecryptedImage **ret) { | |
2121 | ||
2122 | _cleanup_strv_free_erase_ char **z = NULL; | |
2123 | int n = 3, r; | |
2124 | ||
2125 | if (passphrase) | |
2126 | n--; | |
2127 | ||
2128 | for (;;) { | |
89e62e0b | 2129 | r = dissected_image_decrypt(m, passphrase, verity, flags, ret); |
18b5886e LP |
2130 | if (r >= 0) |
2131 | return r; | |
2132 | if (r == -EKEYREJECTED) | |
2133 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
fc95c359 YW |
2134 | else if (r != -ENOKEY) |
2135 | return log_error_errno(r, "Failed to decrypt image: %m"); | |
18b5886e | 2136 | |
baaa35ad ZJS |
2137 | if (--n < 0) |
2138 | return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED), | |
2139 | "Too many retries."); | |
18b5886e LP |
2140 | |
2141 | z = strv_free(z); | |
2142 | ||
8806bb4b | 2143 | r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", "dissect.passphrase", USEC_INFINITY, 0, &z); |
18b5886e LP |
2144 | if (r < 0) |
2145 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
2146 | ||
2147 | passphrase = z[0]; | |
2148 | } | |
2149 | } | |
2150 | ||
18b5886e | 2151 | int decrypted_image_relinquish(DecryptedImage *d) { |
18b5886e LP |
2152 | assert(d); |
2153 | ||
67f63ee5 ZJS |
2154 | /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a |
2155 | * boolean so that we don't clean it up ourselves either anymore */ | |
18b5886e | 2156 | |
349cc4a5 | 2157 | #if HAVE_LIBCRYPTSETUP |
67f63ee5 ZJS |
2158 | int r; |
2159 | ||
2160 | for (size_t i = 0; i < d->n_decrypted; i++) { | |
18b5886e LP |
2161 | DecryptedPartition *p = d->decrypted + i; |
2162 | ||
2163 | if (p->relinquished) | |
2164 | continue; | |
2165 | ||
0d12936d | 2166 | r = sym_crypt_deactivate_by_name(NULL, p->name, CRYPT_DEACTIVATE_DEFERRED); |
18b5886e LP |
2167 | if (r < 0) |
2168 | return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); | |
2169 | ||
2170 | p->relinquished = true; | |
2171 | } | |
2172 | #endif | |
2173 | ||
2174 | return 0; | |
2175 | } | |
2176 | ||
89e62e0b LP |
2177 | static char *build_auxiliary_path(const char *image, const char *suffix) { |
2178 | const char *e; | |
2179 | char *n; | |
2180 | ||
2181 | assert(image); | |
2182 | assert(suffix); | |
2183 | ||
2184 | e = endswith(image, ".raw"); | |
2185 | if (!e) | |
2186 | return strjoin(e, suffix); | |
2187 | ||
2188 | n = new(char, e - image + strlen(suffix) + 1); | |
2189 | if (!n) | |
2190 | return NULL; | |
2191 | ||
2192 | strcpy(mempcpy(n, image, e - image), suffix); | |
2193 | return n; | |
2194 | } | |
2195 | ||
2196 | void verity_settings_done(VeritySettings *v) { | |
2197 | assert(v); | |
2198 | ||
2199 | v->root_hash = mfree(v->root_hash); | |
2200 | v->root_hash_size = 0; | |
2201 | ||
2202 | v->root_hash_sig = mfree(v->root_hash_sig); | |
2203 | v->root_hash_sig_size = 0; | |
2204 | ||
2205 | v->data_path = mfree(v->data_path); | |
2206 | } | |
2207 | ||
2208 | int verity_settings_load( | |
2209 | VeritySettings *verity, | |
f5ea63a5 LP |
2210 | const char *image, |
2211 | const char *root_hash_path, | |
89e62e0b LP |
2212 | const char *root_hash_sig_path) { |
2213 | ||
2214 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
2215 | size_t root_hash_size = 0, root_hash_sig_size = 0; | |
2216 | _cleanup_free_ char *verity_data_path = NULL; | |
aee36b4e | 2217 | PartitionDesignator designator; |
78ebe980 LP |
2218 | int r; |
2219 | ||
89e62e0b | 2220 | assert(verity); |
78ebe980 | 2221 | assert(image); |
aee36b4e | 2222 | assert(verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
78ebe980 | 2223 | |
89e62e0b LP |
2224 | /* If we are asked to load the root hash for a device node, exit early */ |
2225 | if (is_device_path(image)) | |
78ebe980 | 2226 | return 0; |
78ebe980 | 2227 | |
d5fcc5b0 LP |
2228 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_SIDECAR"); |
2229 | if (r < 0 && r != -ENXIO) | |
2230 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_SIDECAR, ignoring: %m"); | |
2231 | if (r == 0) | |
2232 | return 0; | |
2233 | ||
aee36b4e LP |
2234 | designator = verity->designator; |
2235 | ||
89e62e0b | 2236 | /* We only fill in what isn't already filled in */ |
c2923fdc | 2237 | |
89e62e0b | 2238 | if (!verity->root_hash) { |
e7cbe5cb | 2239 | _cleanup_free_ char *text = NULL; |
e7cbe5cb | 2240 | |
0389f4fa | 2241 | if (root_hash_path) { |
aee36b4e | 2242 | /* If explicitly specified it takes precedence */ |
0389f4fa LB |
2243 | r = read_one_line_file(root_hash_path, &text); |
2244 | if (r < 0) | |
e7cbe5cb | 2245 | return r; |
aee36b4e LP |
2246 | |
2247 | if (designator < 0) | |
2248 | designator = PARTITION_ROOT; | |
0389f4fa | 2249 | } else { |
aee36b4e LP |
2250 | /* Otherwise look for xattr and separate file, and first for the data for root and if |
2251 | * that doesn't exist for /usr */ | |
0389f4fa | 2252 | |
aee36b4e | 2253 | if (designator < 0 || designator == PARTITION_ROOT) { |
c53e07e2 | 2254 | r = getxattr_malloc(image, "user.verity.roothash", &text); |
aee36b4e LP |
2255 | if (r < 0) { |
2256 | _cleanup_free_ char *p = NULL; | |
78ebe980 | 2257 | |
aee36b4e LP |
2258 | if (!IN_SET(r, -ENODATA, -ENOENT) && !ERRNO_IS_NOT_SUPPORTED(r)) |
2259 | return r; | |
e7cbe5cb | 2260 | |
aee36b4e LP |
2261 | p = build_auxiliary_path(image, ".roothash"); |
2262 | if (!p) | |
2263 | return -ENOMEM; | |
2264 | ||
2265 | r = read_one_line_file(p, &text); | |
2266 | if (r < 0 && r != -ENOENT) | |
2267 | return r; | |
2268 | } | |
2269 | ||
2270 | if (text) | |
2271 | designator = PARTITION_ROOT; | |
2272 | } | |
2273 | ||
2274 | if (!text && (designator < 0 || designator == PARTITION_USR)) { | |
2275 | /* So in the "roothash" xattr/file name above the "root" of course primarily | |
2276 | * refers to the root of the Verity Merkle tree. But coincidentally it also | |
2277 | * is the hash for the *root* file system, i.e. the "root" neatly refers to | |
2278 | * two distinct concepts called "root". Taking benefit of this happy | |
2279 | * coincidence we call the file with the root hash for the /usr/ file system | |
2280 | * `usrhash`, because `usrroothash` or `rootusrhash` would just be too | |
2281 | * confusing. We thus drop the reference to the root of the Merkle tree, and | |
2282 | * just indicate which file system it's about. */ | |
c53e07e2 | 2283 | r = getxattr_malloc(image, "user.verity.usrhash", &text); |
aee36b4e LP |
2284 | if (r < 0) { |
2285 | _cleanup_free_ char *p = NULL; | |
2286 | ||
2287 | if (!IN_SET(r, -ENODATA, -ENOENT) && !ERRNO_IS_NOT_SUPPORTED(r)) | |
2288 | return r; | |
2289 | ||
2290 | p = build_auxiliary_path(image, ".usrhash"); | |
2291 | if (!p) | |
2292 | return -ENOMEM; | |
2293 | ||
2294 | r = read_one_line_file(p, &text); | |
2295 | if (r < 0 && r != -ENOENT) | |
2296 | return r; | |
2297 | } | |
2298 | ||
2299 | if (text) | |
2300 | designator = PARTITION_USR; | |
0389f4fa | 2301 | } |
e7cbe5cb LB |
2302 | } |
2303 | ||
2304 | if (text) { | |
89e62e0b | 2305 | r = unhexmem(text, strlen(text), &root_hash, &root_hash_size); |
e7cbe5cb LB |
2306 | if (r < 0) |
2307 | return r; | |
89e62e0b | 2308 | if (root_hash_size < sizeof(sd_id128_t)) |
e7cbe5cb LB |
2309 | return -EINVAL; |
2310 | } | |
2311 | } | |
2312 | ||
90f98986 | 2313 | if ((root_hash || verity->root_hash) && !verity->root_hash_sig) { |
aee36b4e | 2314 | if (root_hash_sig_path) { |
ae9cf30b | 2315 | r = read_full_file(root_hash_sig_path, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2316 | if (r < 0 && r != -ENOENT) |
2317 | return r; | |
2318 | ||
2319 | if (designator < 0) | |
2320 | designator = PARTITION_ROOT; | |
2321 | } else { | |
2322 | if (designator < 0 || designator == PARTITION_ROOT) { | |
2323 | _cleanup_free_ char *p = NULL; | |
2324 | ||
2325 | /* Follow naming convention recommended by the relevant RFC: | |
2326 | * https://tools.ietf.org/html/rfc5751#section-3.2.1 */ | |
2327 | p = build_auxiliary_path(image, ".roothash.p7s"); | |
2328 | if (!p) | |
2329 | return -ENOMEM; | |
89e62e0b | 2330 | |
ae9cf30b | 2331 | r = read_full_file(p, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2332 | if (r < 0 && r != -ENOENT) |
2333 | return r; | |
2334 | if (r >= 0) | |
2335 | designator = PARTITION_ROOT; | |
2336 | } | |
2337 | ||
2338 | if (!root_hash_sig && (designator < 0 || designator == PARTITION_USR)) { | |
2339 | _cleanup_free_ char *p = NULL; | |
2340 | ||
2341 | p = build_auxiliary_path(image, ".usrhash.p7s"); | |
2342 | if (!p) | |
2343 | return -ENOMEM; | |
89e62e0b | 2344 | |
ae9cf30b | 2345 | r = read_full_file(p, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
2346 | if (r < 0 && r != -ENOENT) |
2347 | return r; | |
2348 | if (r >= 0) | |
2349 | designator = PARTITION_USR; | |
2350 | } | |
89e62e0b LP |
2351 | } |
2352 | ||
aee36b4e | 2353 | if (root_hash_sig && root_hash_sig_size == 0) /* refuse empty size signatures */ |
89e62e0b LP |
2354 | return -EINVAL; |
2355 | } | |
2356 | ||
2357 | if (!verity->data_path) { | |
2358 | _cleanup_free_ char *p = NULL; | |
2359 | ||
2360 | p = build_auxiliary_path(image, ".verity"); | |
2361 | if (!p) | |
2362 | return -ENOMEM; | |
2363 | ||
2364 | if (access(p, F_OK) < 0) { | |
2365 | if (errno != ENOENT) | |
2366 | return -errno; | |
2367 | } else | |
2368 | verity_data_path = TAKE_PTR(p); | |
2369 | } | |
2370 | ||
2371 | if (root_hash) { | |
2372 | verity->root_hash = TAKE_PTR(root_hash); | |
2373 | verity->root_hash_size = root_hash_size; | |
2374 | } | |
2375 | ||
2376 | if (root_hash_sig) { | |
2377 | verity->root_hash_sig = TAKE_PTR(root_hash_sig); | |
2378 | verity->root_hash_sig_size = root_hash_sig_size; | |
e7cbe5cb | 2379 | } |
89e62e0b LP |
2380 | |
2381 | if (verity_data_path) | |
2382 | verity->data_path = TAKE_PTR(verity_data_path); | |
78ebe980 | 2383 | |
aee36b4e LP |
2384 | if (verity->designator < 0) |
2385 | verity->designator = designator; | |
2386 | ||
78ebe980 LP |
2387 | return 1; |
2388 | } | |
2389 | ||
88b3300f LP |
2390 | int dissected_image_load_verity_sig_partition( |
2391 | DissectedImage *m, | |
2392 | int fd, | |
2393 | VeritySettings *verity) { | |
2394 | ||
2395 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
2396 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
2397 | size_t root_hash_size, root_hash_sig_size; | |
2398 | _cleanup_free_ char *buf = NULL; | |
2399 | PartitionDesignator d; | |
2400 | DissectedPartition *p; | |
2401 | JsonVariant *rh, *sig; | |
2402 | ssize_t n; | |
2403 | char *e; | |
2404 | int r; | |
2405 | ||
2406 | assert(m); | |
2407 | assert(fd >= 0); | |
2408 | assert(verity); | |
2409 | ||
2410 | if (verity->root_hash && verity->root_hash_sig) /* Already loaded? */ | |
2411 | return 0; | |
2412 | ||
2413 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_EMBEDDED"); | |
2414 | if (r < 0 && r != -ENXIO) | |
2415 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_EMBEDDED, ignoring: %m"); | |
2416 | if (r == 0) | |
2417 | return 0; | |
2418 | ||
2419 | d = PARTITION_VERITY_SIG_OF(verity->designator < 0 ? PARTITION_ROOT : verity->designator); | |
2420 | assert(d >= 0); | |
2421 | ||
2422 | p = m->partitions + d; | |
2423 | if (!p->found) | |
2424 | return 0; | |
2425 | if (p->offset == UINT64_MAX || p->size == UINT64_MAX) | |
2426 | return -EINVAL; | |
2427 | ||
2428 | if (p->size > 4*1024*1024) /* Signature data cannot possible be larger than 4M, refuse that */ | |
2429 | return -EFBIG; | |
2430 | ||
2431 | buf = new(char, p->size+1); | |
2432 | if (!buf) | |
2433 | return -ENOMEM; | |
2434 | ||
2435 | n = pread(fd, buf, p->size, p->offset); | |
2436 | if (n < 0) | |
2437 | return -ENOMEM; | |
2438 | if ((uint64_t) n != p->size) | |
2439 | return -EIO; | |
2440 | ||
2441 | e = memchr(buf, 0, p->size); | |
2442 | if (e) { | |
2443 | /* If we found a NUL byte then the rest of the data must be NUL too */ | |
2444 | if (!memeqzero(e, p->size - (e - buf))) | |
2445 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature data contains embedded NUL byte."); | |
2446 | } else | |
2447 | buf[p->size] = 0; | |
2448 | ||
2449 | r = json_parse(buf, 0, &v, NULL, NULL); | |
2450 | if (r < 0) | |
2451 | return log_debug_errno(r, "Failed to parse signature JSON data: %m"); | |
2452 | ||
2453 | rh = json_variant_by_key(v, "rootHash"); | |
2454 | if (!rh) | |
2455 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'rootHash' field."); | |
2456 | if (!json_variant_is_string(rh)) | |
2457 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'rootHash' field of signature JSON object is not a string."); | |
2458 | ||
2459 | r = unhexmem(json_variant_string(rh), SIZE_MAX, &root_hash, &root_hash_size); | |
2460 | if (r < 0) | |
2461 | return log_debug_errno(r, "Failed to parse root hash field: %m"); | |
2462 | ||
2463 | /* Check if specified root hash matches if it is specified */ | |
2464 | if (verity->root_hash && | |
2465 | memcmp_nn(verity->root_hash, verity->root_hash_size, root_hash, root_hash_size) != 0) { | |
2466 | _cleanup_free_ char *a = NULL, *b = NULL; | |
2467 | ||
2468 | a = hexmem(root_hash, root_hash_size); | |
2469 | b = hexmem(verity->root_hash, verity->root_hash_size); | |
2470 | ||
2471 | return log_debug_errno(r, "Root hash in signature JSON data (%s) doesn't match configured hash (%s).", strna(a), strna(b)); | |
2472 | } | |
2473 | ||
2474 | sig = json_variant_by_key(v, "signature"); | |
2475 | if (!sig) | |
2476 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'signature' field."); | |
2477 | if (!json_variant_is_string(sig)) | |
2478 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'signature' field of signature JSON object is not a string."); | |
2479 | ||
2480 | r = unbase64mem(json_variant_string(sig), SIZE_MAX, &root_hash_sig, &root_hash_sig_size); | |
2481 | if (r < 0) | |
2482 | return log_debug_errno(r, "Failed to parse signature field: %m"); | |
2483 | ||
2484 | free_and_replace(verity->root_hash, root_hash); | |
2485 | verity->root_hash_size = root_hash_size; | |
2486 | ||
2487 | free_and_replace(verity->root_hash_sig, root_hash_sig); | |
2488 | verity->root_hash_sig_size = root_hash_sig_size; | |
2489 | ||
2490 | return 1; | |
2491 | } | |
2492 | ||
22847508 | 2493 | int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_flags) { |
3b925504 LP |
2494 | |
2495 | enum { | |
2496 | META_HOSTNAME, | |
2497 | META_MACHINE_ID, | |
2498 | META_MACHINE_INFO, | |
2499 | META_OS_RELEASE, | |
7718ac97 | 2500 | META_EXTENSION_RELEASE, |
a4e0d617 | 2501 | META_HAS_INIT_SYSTEM, |
3b925504 LP |
2502 | _META_MAX, |
2503 | }; | |
2504 | ||
9a4b883b | 2505 | static const char *const paths[_META_MAX] = { |
7718ac97 LB |
2506 | [META_HOSTNAME] = "/etc/hostname\0", |
2507 | [META_MACHINE_ID] = "/etc/machine-id\0", | |
2508 | [META_MACHINE_INFO] = "/etc/machine-info\0", | |
9a4b883b | 2509 | [META_OS_RELEASE] = ("/etc/os-release\0" |
22847508 | 2510 | "/usr/lib/os-release\0"), |
a4e0d617 LP |
2511 | [META_EXTENSION_RELEASE] = "extension-release\0", /* Used only for logging. */ |
2512 | [META_HAS_INIT_SYSTEM] = "has-init-system\0", /* ditto */ | |
3b925504 LP |
2513 | }; |
2514 | ||
7718ac97 | 2515 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL, **extension_release = NULL; |
af8219d5 | 2516 | _cleanup_close_pair_ int error_pipe[2] = { -1, -1 }; |
3b925504 LP |
2517 | _cleanup_(rmdir_and_freep) char *t = NULL; |
2518 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
2519 | sd_id128_t machine_id = SD_ID128_NULL; | |
2520 | _cleanup_free_ char *hostname = NULL; | |
67f63ee5 | 2521 | unsigned n_meta_initialized = 0; |
af8219d5 | 2522 | int fds[2 * _META_MAX], r, v; |
a4e0d617 | 2523 | int has_init_system = -1; |
af8219d5 | 2524 | ssize_t n; |
3b925504 LP |
2525 | |
2526 | BLOCK_SIGNALS(SIGCHLD); | |
2527 | ||
2528 | assert(m); | |
2529 | ||
7718ac97 | 2530 | for (; n_meta_initialized < _META_MAX; n_meta_initialized ++) { |
d9119c00 LP |
2531 | if (!paths[n_meta_initialized]) { |
2532 | fds[2*n_meta_initialized] = fds[2*n_meta_initialized+1] = -1; | |
7718ac97 | 2533 | continue; |
d9119c00 LP |
2534 | } |
2535 | ||
3b925504 LP |
2536 | if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) { |
2537 | r = -errno; | |
2538 | goto finish; | |
2539 | } | |
7718ac97 | 2540 | } |
3b925504 LP |
2541 | |
2542 | r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); | |
2543 | if (r < 0) | |
2544 | goto finish; | |
2545 | ||
af8219d5 LP |
2546 | if (pipe2(error_pipe, O_CLOEXEC) < 0) { |
2547 | r = -errno; | |
2548 | goto finish; | |
2549 | } | |
2550 | ||
e2047ba9 | 2551 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 2552 | if (r < 0) |
3b925504 | 2553 | goto finish; |
be39f6ee | 2554 | if (r == 0) { |
a4e0d617 | 2555 | /* Child in a new mount namespace */ |
af8219d5 LP |
2556 | error_pipe[0] = safe_close(error_pipe[0]); |
2557 | ||
7cf66030 LP |
2558 | r = dissected_image_mount( |
2559 | m, | |
2560 | t, | |
2561 | UID_INVALID, | |
21b61b1d | 2562 | UID_INVALID, |
22847508 ZJS |
2563 | extra_flags | |
2564 | DISSECT_IMAGE_READ_ONLY | | |
2565 | DISSECT_IMAGE_MOUNT_ROOT_ONLY | | |
7cf66030 | 2566 | DISSECT_IMAGE_USR_NO_ROOT); |
429d4e41 LP |
2567 | if (r < 0) { |
2568 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
03ae68f4 | 2569 | goto inner_fail; |
429d4e41 | 2570 | } |
3b925504 | 2571 | |
67f63ee5 | 2572 | for (unsigned k = 0; k < _META_MAX; k++) { |
37e44c3f | 2573 | _cleanup_close_ int fd = -ENOENT; |
3b925504 LP |
2574 | const char *p; |
2575 | ||
7718ac97 LB |
2576 | if (!paths[k]) |
2577 | continue; | |
2578 | ||
3b925504 LP |
2579 | fds[2*k] = safe_close(fds[2*k]); |
2580 | ||
a4e0d617 LP |
2581 | switch (k) { |
2582 | ||
2583 | case META_EXTENSION_RELEASE: | |
9a4b883b LB |
2584 | /* As per the os-release spec, if the image is an extension it will have a file |
2585 | * named after the image name in extension-release.d/ - we use the image name | |
2586 | * and try to resolve it with the extension-release helpers, as sometimes | |
2587 | * the image names are mangled on deployment and do not match anymore. | |
2588 | * Unlike other paths this is not fixed, and the image name | |
2589 | * can be mangled on deployment, so by calling into the helper | |
2590 | * we allow a fallback that matches on the first extension-release | |
2591 | * file found in the directory, if one named after the image cannot | |
2592 | * be found first. */ | |
2593 | r = open_extension_release(t, m->image_name, NULL, &fd); | |
2594 | if (r < 0) | |
2595 | fd = r; /* Propagate the error. */ | |
a4e0d617 LP |
2596 | break; |
2597 | ||
2598 | case META_HAS_INIT_SYSTEM: { | |
2599 | bool found = false; | |
a4e0d617 LP |
2600 | |
2601 | FOREACH_STRING(init, | |
2602 | "/usr/lib/systemd/systemd", /* systemd on /usr merged system */ | |
2603 | "/lib/systemd/systemd", /* systemd on /usr non-merged systems */ | |
2604 | "/sbin/init") { /* traditional path the Linux kernel invokes */ | |
2605 | ||
2606 | r = chase_symlinks(init, t, CHASE_PREFIX_ROOT, NULL, NULL); | |
2607 | if (r < 0) { | |
2608 | if (r != -ENOENT) | |
2609 | log_debug_errno(r, "Failed to resolve %s, ignoring: %m", init); | |
2610 | } else { | |
2611 | found = true; | |
2612 | break; | |
2613 | } | |
2614 | } | |
2615 | ||
2616 | r = loop_write(fds[2*k+1], &found, sizeof(found), false); | |
2617 | if (r < 0) | |
2618 | goto inner_fail; | |
2619 | ||
2620 | continue; | |
2621 | } | |
2622 | ||
2623 | default: | |
9a4b883b LB |
2624 | NULSTR_FOREACH(p, paths[k]) { |
2625 | fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); | |
2626 | if (fd >= 0) | |
2627 | break; | |
2628 | } | |
a4e0d617 LP |
2629 | } |
2630 | ||
36952d19 LP |
2631 | if (fd < 0) { |
2632 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
37e44c3f | 2633 | fds[2*k+1] = safe_close(fds[2*k+1]); |
3b925504 | 2634 | continue; |
36952d19 | 2635 | } |
3b925504 | 2636 | |
f5fbe71d | 2637 | r = copy_bytes(fd, fds[2*k+1], UINT64_MAX, 0); |
a4e0d617 LP |
2638 | if (r < 0) |
2639 | goto inner_fail; | |
3b925504 LP |
2640 | |
2641 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
2642 | } | |
2643 | ||
2644 | _exit(EXIT_SUCCESS); | |
a4e0d617 LP |
2645 | |
2646 | inner_fail: | |
03ae68f4 | 2647 | /* Let parent know the error */ |
a4e0d617 LP |
2648 | (void) write(error_pipe[1], &r, sizeof(r)); |
2649 | _exit(EXIT_FAILURE); | |
3b925504 LP |
2650 | } |
2651 | ||
af8219d5 LP |
2652 | error_pipe[1] = safe_close(error_pipe[1]); |
2653 | ||
67f63ee5 | 2654 | for (unsigned k = 0; k < _META_MAX; k++) { |
3b925504 LP |
2655 | _cleanup_fclose_ FILE *f = NULL; |
2656 | ||
7718ac97 LB |
2657 | if (!paths[k]) |
2658 | continue; | |
2659 | ||
3b925504 LP |
2660 | fds[2*k+1] = safe_close(fds[2*k+1]); |
2661 | ||
4fa744a3 | 2662 | f = take_fdopen(&fds[2*k], "r"); |
3b925504 LP |
2663 | if (!f) { |
2664 | r = -errno; | |
2665 | goto finish; | |
2666 | } | |
2667 | ||
3b925504 LP |
2668 | switch (k) { |
2669 | ||
2670 | case META_HOSTNAME: | |
2671 | r = read_etc_hostname_stream(f, &hostname); | |
2672 | if (r < 0) | |
f6048e5e | 2673 | log_debug_errno(r, "Failed to read /etc/hostname of image: %m"); |
3b925504 LP |
2674 | |
2675 | break; | |
2676 | ||
2677 | case META_MACHINE_ID: { | |
2678 | _cleanup_free_ char *line = NULL; | |
2679 | ||
2680 | r = read_line(f, LONG_LINE_MAX, &line); | |
2681 | if (r < 0) | |
f6048e5e | 2682 | log_debug_errno(r, "Failed to read /etc/machine-id of image: %m"); |
3b925504 LP |
2683 | else if (r == 33) { |
2684 | r = sd_id128_from_string(line, &machine_id); | |
2685 | if (r < 0) | |
2686 | log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line); | |
2687 | } else if (r == 0) | |
f6048e5e | 2688 | log_debug("/etc/machine-id file of image is empty."); |
ab763cb2 | 2689 | else if (streq(line, "uninitialized")) |
f6048e5e | 2690 | log_debug("/etc/machine-id file of image is uninitialized (likely aborted first boot)."); |
3b925504 | 2691 | else |
f6048e5e | 2692 | log_debug("/etc/machine-id file of image has unexpected length %i.", r); |
3b925504 LP |
2693 | |
2694 | break; | |
2695 | } | |
2696 | ||
2697 | case META_MACHINE_INFO: | |
aa8fbc74 | 2698 | r = load_env_file_pairs(f, "machine-info", &machine_info); |
3b925504 | 2699 | if (r < 0) |
f6048e5e | 2700 | log_debug_errno(r, "Failed to read /etc/machine-info of image: %m"); |
3b925504 LP |
2701 | |
2702 | break; | |
2703 | ||
2704 | case META_OS_RELEASE: | |
aa8fbc74 | 2705 | r = load_env_file_pairs(f, "os-release", &os_release); |
3b925504 | 2706 | if (r < 0) |
f6048e5e | 2707 | log_debug_errno(r, "Failed to read OS release file of image: %m"); |
3b925504 LP |
2708 | |
2709 | break; | |
7718ac97 LB |
2710 | |
2711 | case META_EXTENSION_RELEASE: | |
2712 | r = load_env_file_pairs(f, "extension-release", &extension_release); | |
2713 | if (r < 0) | |
f6048e5e | 2714 | log_debug_errno(r, "Failed to read extension release file of image: %m"); |
7718ac97 LB |
2715 | |
2716 | break; | |
a4e0d617 LP |
2717 | |
2718 | case META_HAS_INIT_SYSTEM: { | |
2719 | bool b = false; | |
2720 | size_t nr; | |
2721 | ||
2722 | errno = 0; | |
2723 | nr = fread(&b, 1, sizeof(b), f); | |
2724 | if (nr != sizeof(b)) | |
2725 | log_debug_errno(errno_or_else(EIO), "Failed to read has-init-system boolean: %m"); | |
2726 | else | |
2727 | has_init_system = b; | |
2728 | ||
2729 | break; | |
2730 | }} | |
3b925504 LP |
2731 | } |
2732 | ||
2e87a1fd | 2733 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 2734 | child = 0; |
2e87a1fd | 2735 | if (r < 0) |
af8219d5 LP |
2736 | return r; |
2737 | ||
2738 | n = read(error_pipe[0], &v, sizeof(v)); | |
2739 | if (n < 0) | |
2740 | return -errno; | |
2741 | if (n == sizeof(v)) | |
2742 | return v; /* propagate error sent to us from child */ | |
2743 | if (n != 0) | |
2744 | return -EIO; | |
2745 | ||
2e87a1fd LP |
2746 | if (r != EXIT_SUCCESS) |
2747 | return -EPROTO; | |
3b925504 LP |
2748 | |
2749 | free_and_replace(m->hostname, hostname); | |
2750 | m->machine_id = machine_id; | |
2751 | strv_free_and_replace(m->machine_info, machine_info); | |
2752 | strv_free_and_replace(m->os_release, os_release); | |
7718ac97 | 2753 | strv_free_and_replace(m->extension_release, extension_release); |
a4e0d617 | 2754 | m->has_init_system = has_init_system; |
3b925504 LP |
2755 | |
2756 | finish: | |
67f63ee5 | 2757 | for (unsigned k = 0; k < n_meta_initialized; k++) |
3b925504 LP |
2758 | safe_close_pair(fds + 2*k); |
2759 | ||
2760 | return r; | |
2761 | } | |
2762 | ||
bad31660 | 2763 | int dissect_loop_device_and_warn( |
bad31660 | 2764 | const LoopDevice *loop, |
89e62e0b | 2765 | const VeritySettings *verity, |
18d73705 | 2766 | const MountOptions *mount_options, |
4526113f LP |
2767 | DissectImageFlags flags, |
2768 | DissectedImage **ret) { | |
2769 | ||
64dd3a24 | 2770 | const char *name; |
4526113f LP |
2771 | int r; |
2772 | ||
bad31660 YW |
2773 | assert(loop); |
2774 | assert(loop->fd >= 0); | |
4526113f | 2775 | |
64dd3a24 | 2776 | name = ASSERT_PTR(loop->backing_file ?: loop->node); |
4526113f | 2777 | |
369de26f | 2778 | r = dissect_loop_device(loop, verity, mount_options, flags, ret); |
4526113f LP |
2779 | switch (r) { |
2780 | ||
2781 | case -EOPNOTSUPP: | |
2782 | return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support."); | |
2783 | ||
2784 | case -ENOPKG: | |
48084df6 ZJS |
2785 | return log_error_errno(r, "%s: Couldn't identify a suitable partition table or file system.", name); |
2786 | ||
2787 | case -ENOMEDIUM: | |
2788 | return log_error_errno(r, "%s: The image does not pass validation.", name); | |
4526113f LP |
2789 | |
2790 | case -EADDRNOTAVAIL: | |
48084df6 | 2791 | return log_error_errno(r, "%s: No root partition for specified root hash found.", name); |
4526113f LP |
2792 | |
2793 | case -ENOTUNIQ: | |
48084df6 | 2794 | return log_error_errno(r, "%s: Multiple suitable root partitions found in image.", name); |
4526113f LP |
2795 | |
2796 | case -ENXIO: | |
48084df6 | 2797 | return log_error_errno(r, "%s: No suitable root partition found in image.", name); |
4526113f LP |
2798 | |
2799 | case -EPROTONOSUPPORT: | |
2800 | return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name); | |
2801 | ||
48084df6 ZJS |
2802 | case -ENOTBLK: |
2803 | return log_error_errno(r, "%s: Image is not a block device.", name); | |
2804 | ||
a94aa2b9 LP |
2805 | case -EBADR: |
2806 | return log_error_errno(r, | |
2807 | "Combining partitioned images (such as '%s') with external Verity data (such as '%s') not supported. " | |
2808 | "(Consider setting $SYSTEMD_DISSECT_VERITY_SIDECAR=0 to disable automatic discovery of external Verity data.)", | |
2809 | name, strna(verity ? verity->data_path : NULL)); | |
2810 | ||
4526113f LP |
2811 | default: |
2812 | if (r < 0) | |
2813 | return log_error_errno(r, "Failed to dissect image '%s': %m", name); | |
2814 | ||
2815 | return r; | |
2816 | } | |
2817 | } | |
2818 | ||
49536766 LP |
2819 | bool dissected_image_verity_candidate(const DissectedImage *image, PartitionDesignator partition_designator) { |
2820 | assert(image); | |
2821 | ||
2822 | /* Checks if this partition could theoretically do Verity. For non-partitioned images this only works | |
2823 | * if there's an external verity file supplied, for which we can consult .has_verity. For partitioned | |
2824 | * images we only check the partition type. | |
2825 | * | |
2826 | * This call is used to decide whether to suppress or show a verity column in tabular output of the | |
2827 | * image. */ | |
2828 | ||
e7cbe5cb | 2829 | if (image->single_file_system) |
c3c88d67 | 2830 | return partition_designator == PARTITION_ROOT && image->has_verity; |
e7cbe5cb LB |
2831 | |
2832 | return PARTITION_VERITY_OF(partition_designator) >= 0; | |
2833 | } | |
2834 | ||
49536766 LP |
2835 | bool dissected_image_verity_ready(const DissectedImage *image, PartitionDesignator partition_designator) { |
2836 | PartitionDesignator k; | |
2837 | ||
2838 | assert(image); | |
2839 | ||
2840 | /* Checks if this partition has verity data available that we can activate. For non-partitioned this | |
2841 | * works for the root partition, for others only if the associated verity partition was found. */ | |
2842 | ||
2843 | if (!image->verity_ready) | |
2844 | return false; | |
e7cbe5cb LB |
2845 | |
2846 | if (image->single_file_system) | |
49536766 | 2847 | return partition_designator == PARTITION_ROOT; |
e7cbe5cb LB |
2848 | |
2849 | k = PARTITION_VERITY_OF(partition_designator); | |
2850 | return k >= 0 && image->partitions[k].found; | |
2851 | } | |
2852 | ||
8ee9615e LP |
2853 | bool dissected_image_verity_sig_ready(const DissectedImage *image, PartitionDesignator partition_designator) { |
2854 | PartitionDesignator k; | |
2855 | ||
2856 | assert(image); | |
2857 | ||
2858 | /* Checks if this partition has verity signature data available that we can use. */ | |
2859 | ||
2860 | if (!image->verity_sig_ready) | |
2861 | return false; | |
2862 | ||
2863 | if (image->single_file_system) | |
2864 | return partition_designator == PARTITION_ROOT; | |
2865 | ||
2866 | k = PARTITION_VERITY_SIG_OF(partition_designator); | |
2867 | return k >= 0 && image->partitions[k].found; | |
2868 | } | |
2869 | ||
18d73705 LB |
2870 | MountOptions* mount_options_free_all(MountOptions *options) { |
2871 | MountOptions *m; | |
2872 | ||
2873 | while ((m = options)) { | |
2874 | LIST_REMOVE(mount_options, options, m); | |
2875 | free(m->options); | |
2876 | free(m); | |
2877 | } | |
2878 | ||
2879 | return NULL; | |
2880 | } | |
2881 | ||
569a0e42 | 2882 | const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator) { |
f5215bc8 | 2883 | LIST_FOREACH(mount_options, m, options) |
9ece6444 | 2884 | if (designator == m->partition_designator && !isempty(m->options)) |
18d73705 | 2885 | return m->options; |
6aa05ebd | 2886 | |
18d73705 LB |
2887 | return NULL; |
2888 | } | |
2889 | ||
6aa05ebd LP |
2890 | int mount_image_privately_interactively( |
2891 | const char *image, | |
2892 | DissectImageFlags flags, | |
2893 | char **ret_directory, | |
2894 | LoopDevice **ret_loop_device, | |
2895 | DecryptedImage **ret_decrypted_image) { | |
2896 | ||
27ec815e | 2897 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
6aa05ebd LP |
2898 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
2899 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
2900 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; | |
2901 | _cleanup_(rmdir_and_freep) char *created_dir = NULL; | |
2902 | _cleanup_free_ char *temp = NULL; | |
2903 | int r; | |
2904 | ||
2905 | /* Mounts an OS image at a temporary place, inside a newly created mount namespace of our own. This | |
2906 | * is used by tools such as systemd-tmpfiles or systemd-firstboot to operate on some disk image | |
2907 | * easily. */ | |
2908 | ||
2909 | assert(image); | |
2910 | assert(ret_directory); | |
2911 | assert(ret_loop_device); | |
2912 | assert(ret_decrypted_image); | |
2913 | ||
27ec815e LP |
2914 | r = verity_settings_load(&verity, image, NULL, NULL); |
2915 | if (r < 0) | |
2916 | return log_error_errno(r, "Failed to load root hash data: %m"); | |
2917 | ||
6aa05ebd LP |
2918 | r = tempfn_random_child(NULL, program_invocation_short_name, &temp); |
2919 | if (r < 0) | |
2920 | return log_error_errno(r, "Failed to generate temporary mount directory: %m"); | |
2921 | ||
2922 | r = loop_device_make_by_path( | |
2923 | image, | |
ef9c184d | 2924 | FLAGS_SET(flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : O_RDWR, |
6aa05ebd | 2925 | FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, |
7f52206a | 2926 | LOCK_SH, |
6aa05ebd LP |
2927 | &d); |
2928 | if (r < 0) | |
7b87fe4c | 2929 | return log_error_errno(r, "Failed to set up loopback device for %s: %m", image); |
6aa05ebd | 2930 | |
64dd3a24 | 2931 | r = dissect_loop_device_and_warn(d, &verity, NULL, flags, &dissected_image); |
6aa05ebd LP |
2932 | if (r < 0) |
2933 | return r; | |
2934 | ||
88b3300f LP |
2935 | r = dissected_image_load_verity_sig_partition(dissected_image, d->fd, &verity); |
2936 | if (r < 0) | |
2937 | return r; | |
2938 | ||
27ec815e | 2939 | r = dissected_image_decrypt_interactively(dissected_image, NULL, &verity, flags, &decrypted_image); |
6aa05ebd LP |
2940 | if (r < 0) |
2941 | return r; | |
2942 | ||
2943 | r = detach_mount_namespace(); | |
2944 | if (r < 0) | |
2945 | return log_error_errno(r, "Failed to detach mount namespace: %m"); | |
2946 | ||
2947 | r = mkdir_p(temp, 0700); | |
2948 | if (r < 0) | |
2949 | return log_error_errno(r, "Failed to create mount point: %m"); | |
2950 | ||
2951 | created_dir = TAKE_PTR(temp); | |
2952 | ||
21b61b1d | 2953 | r = dissected_image_mount_and_warn(dissected_image, created_dir, UID_INVALID, UID_INVALID, flags); |
6aa05ebd | 2954 | if (r < 0) |
af187ab2 | 2955 | return r; |
6aa05ebd | 2956 | |
41bc4849 LP |
2957 | r = loop_device_flock(d, LOCK_UN); |
2958 | if (r < 0) | |
2959 | return r; | |
2960 | ||
6aa05ebd LP |
2961 | if (decrypted_image) { |
2962 | r = decrypted_image_relinquish(decrypted_image); | |
2963 | if (r < 0) | |
2964 | return log_error_errno(r, "Failed to relinquish DM devices: %m"); | |
2965 | } | |
2966 | ||
2967 | loop_device_relinquish(d); | |
2968 | ||
2969 | *ret_directory = TAKE_PTR(created_dir); | |
2970 | *ret_loop_device = TAKE_PTR(d); | |
2971 | *ret_decrypted_image = TAKE_PTR(decrypted_image); | |
2972 | ||
2973 | return 0; | |
2974 | } | |
2975 | ||
8c1be37e | 2976 | static const char *const partition_designator_table[] = { |
68ac5118 ZJS |
2977 | [PARTITION_ROOT] = "root", |
2978 | [PARTITION_ROOT_SECONDARY] = "root-secondary", | |
2979 | [PARTITION_ROOT_OTHER] = "root-other", | |
2980 | [PARTITION_USR] = "usr", | |
2981 | [PARTITION_USR_SECONDARY] = "usr-secondary", | |
2982 | [PARTITION_USR_OTHER] = "usr-other", | |
2983 | [PARTITION_HOME] = "home", | |
2984 | [PARTITION_SRV] = "srv", | |
2985 | [PARTITION_ESP] = "esp", | |
2986 | [PARTITION_XBOOTLDR] = "xbootldr", | |
2987 | [PARTITION_SWAP] = "swap", | |
2988 | [PARTITION_ROOT_VERITY] = "root-verity", | |
2989 | [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity", | |
2990 | [PARTITION_ROOT_OTHER_VERITY] = "root-other-verity", | |
2991 | [PARTITION_USR_VERITY] = "usr-verity", | |
2992 | [PARTITION_USR_SECONDARY_VERITY] = "usr-secondary-verity", | |
2993 | [PARTITION_USR_OTHER_VERITY] = "usr-other-verity", | |
2994 | [PARTITION_ROOT_VERITY_SIG] = "root-verity-sig", | |
8ee9615e | 2995 | [PARTITION_ROOT_SECONDARY_VERITY_SIG] = "root-secondary-verity-sig", |
68ac5118 ZJS |
2996 | [PARTITION_ROOT_OTHER_VERITY_SIG] = "root-other-verity-sig", |
2997 | [PARTITION_USR_VERITY_SIG] = "usr-verity-sig", | |
2998 | [PARTITION_USR_SECONDARY_VERITY_SIG] = "usr-secondary-verity-sig", | |
2999 | [PARTITION_USR_OTHER_VERITY_SIG] = "usr-other-verity-sig", | |
3000 | [PARTITION_TMP] = "tmp", | |
3001 | [PARTITION_VAR] = "var", | |
8c1be37e LP |
3002 | }; |
3003 | ||
93f59701 | 3004 | int verity_dissect_and_mount( |
cedf5b1a | 3005 | int src_fd, |
93f59701 LB |
3006 | const char *src, |
3007 | const char *dest, | |
3008 | const MountOptions *options, | |
3009 | const char *required_host_os_release_id, | |
3010 | const char *required_host_os_release_version_id, | |
60c5f700 LP |
3011 | const char *required_host_os_release_sysext_level, |
3012 | const char *required_sysext_scope) { | |
93f59701 | 3013 | |
4beda316 LB |
3014 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
3015 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
3016 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; | |
3017 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; | |
3018 | DissectImageFlags dissect_image_flags; | |
3019 | int r; | |
3020 | ||
3021 | assert(src); | |
3022 | assert(dest); | |
3023 | ||
cedf5b1a | 3024 | /* We might get an FD for the image, but we use the original path to look for the dm-verity files */ |
4beda316 LB |
3025 | r = verity_settings_load(&verity, src, NULL, NULL); |
3026 | if (r < 0) | |
3027 | return log_debug_errno(r, "Failed to load root hash: %m"); | |
3028 | ||
3029 | dissect_image_flags = verity.data_path ? DISSECT_IMAGE_NO_PARTITION_TABLE : 0; | |
3030 | ||
cedf5b1a LB |
3031 | /* Note that we don't use loop_device_make here, as the FD is most likely O_PATH which would not be |
3032 | * accepted by LOOP_CONFIGURE, so just let loop_device_make_by_path reopen it as a regular FD. */ | |
4beda316 | 3033 | r = loop_device_make_by_path( |
cedf5b1a | 3034 | src_fd >= 0 ? FORMAT_PROC_FD_PATH(src_fd) : src, |
4beda316 LB |
3035 | -1, |
3036 | verity.data_path ? 0 : LO_FLAGS_PARTSCAN, | |
7f52206a | 3037 | LOCK_SH, |
4beda316 LB |
3038 | &loop_device); |
3039 | if (r < 0) | |
3040 | return log_debug_errno(r, "Failed to create loop device for image: %m"); | |
3041 | ||
bad31660 YW |
3042 | r = dissect_loop_device( |
3043 | loop_device, | |
4beda316 LB |
3044 | &verity, |
3045 | options, | |
3046 | dissect_image_flags, | |
3047 | &dissected_image); | |
3048 | /* No partition table? Might be a single-filesystem image, try again */ | |
3049 | if (!verity.data_path && r == -ENOPKG) | |
bad31660 YW |
3050 | r = dissect_loop_device( |
3051 | loop_device, | |
4beda316 LB |
3052 | &verity, |
3053 | options, | |
75dc190d | 3054 | dissect_image_flags | DISSECT_IMAGE_NO_PARTITION_TABLE, |
4beda316 LB |
3055 | &dissected_image); |
3056 | if (r < 0) | |
3057 | return log_debug_errno(r, "Failed to dissect image: %m"); | |
3058 | ||
88b3300f LP |
3059 | r = dissected_image_load_verity_sig_partition(dissected_image, loop_device->fd, &verity); |
3060 | if (r < 0) | |
3061 | return r; | |
3062 | ||
4beda316 LB |
3063 | r = dissected_image_decrypt( |
3064 | dissected_image, | |
3065 | NULL, | |
3066 | &verity, | |
3067 | dissect_image_flags, | |
3068 | &decrypted_image); | |
3069 | if (r < 0) | |
3070 | return log_debug_errno(r, "Failed to decrypt dissected image: %m"); | |
3071 | ||
3072 | r = mkdir_p_label(dest, 0755); | |
3073 | if (r < 0) | |
3074 | return log_debug_errno(r, "Failed to create destination directory %s: %m", dest); | |
3075 | r = umount_recursive(dest, 0); | |
3076 | if (r < 0) | |
3077 | return log_debug_errno(r, "Failed to umount under destination directory %s: %m", dest); | |
3078 | ||
21b61b1d | 3079 | r = dissected_image_mount(dissected_image, dest, UID_INVALID, UID_INVALID, dissect_image_flags); |
4beda316 LB |
3080 | if (r < 0) |
3081 | return log_debug_errno(r, "Failed to mount image: %m"); | |
3082 | ||
41bc4849 LP |
3083 | r = loop_device_flock(loop_device, LOCK_UN); |
3084 | if (r < 0) | |
3085 | return log_debug_errno(r, "Failed to unlock loopback device: %m"); | |
3086 | ||
93f59701 LB |
3087 | /* If we got os-release values from the caller, then we need to match them with the image's |
3088 | * extension-release.d/ content. Return -EINVAL if there's any mismatch. | |
3089 | * First, check the distro ID. If that matches, then check the new SYSEXT_LEVEL value if | |
37361f46 LB |
3090 | * available, or else fallback to VERSION_ID. If neither is present (eg: rolling release), |
3091 | * then a simple match on the ID will be performed. */ | |
8b2dcbbd | 3092 | if (required_host_os_release_id) { |
93f59701 LB |
3093 | _cleanup_strv_free_ char **extension_release = NULL; |
3094 | ||
d30d86b7 YW |
3095 | assert(!isempty(required_host_os_release_id)); |
3096 | ||
93f59701 LB |
3097 | r = load_extension_release_pairs(dest, dissected_image->image_name, &extension_release); |
3098 | if (r < 0) | |
3099 | return log_debug_errno(r, "Failed to parse image %s extension-release metadata: %m", dissected_image->image_name); | |
3100 | ||
3101 | r = extension_release_validate( | |
60c5f700 LP |
3102 | dissected_image->image_name, |
3103 | required_host_os_release_id, | |
3104 | required_host_os_release_version_id, | |
3105 | required_host_os_release_sysext_level, | |
3106 | required_sysext_scope, | |
3107 | extension_release); | |
93f59701 LB |
3108 | if (r == 0) |
3109 | return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Image %s extension-release metadata does not match the root's", dissected_image->image_name); | |
3110 | if (r < 0) | |
3111 | return log_debug_errno(r, "Failed to compare image %s extension-release metadata with the root's os-release: %m", dissected_image->image_name); | |
3112 | } | |
3113 | ||
4beda316 LB |
3114 | if (decrypted_image) { |
3115 | r = decrypted_image_relinquish(decrypted_image); | |
3116 | if (r < 0) | |
3117 | return log_debug_errno(r, "Failed to relinquish decrypted image: %m"); | |
3118 | } | |
3119 | ||
3120 | loop_device_relinquish(loop_device); | |
3121 | ||
3122 | return 0; | |
3123 | } | |
3124 | ||
569a0e42 | 3125 | DEFINE_STRING_TABLE_LOOKUP(partition_designator, PartitionDesignator); |