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