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