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