]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8c1be37e | 2 | |
10c1b188 LP |
3 | #if HAVE_VALGRIND_MEMCHECK_H |
4 | #include <valgrind/memcheck.h> | |
5 | #endif | |
6 | ||
01234e1f YW |
7 | #include <linux/dm-ioctl.h> |
8 | #include <linux/loop.h> | |
19df770f | 9 | #include <sys/file.h> |
8c1be37e | 10 | #include <sys/mount.h> |
3b925504 LP |
11 | #include <sys/prctl.h> |
12 | #include <sys/wait.h> | |
f5ea63a5 | 13 | #include <sysexits.h> |
8c1be37e | 14 | |
c2fa92e7 LP |
15 | #if HAVE_OPENSSL |
16 | #include <openssl/err.h> | |
17 | #include <openssl/pem.h> | |
18 | #include <openssl/x509.h> | |
19 | #endif | |
20 | ||
3c1f2cee | 21 | #include "sd-device.h" |
dccca82b LP |
22 | #include "sd-id128.h" |
23 | ||
8c1be37e | 24 | #include "architecture.h" |
18b5886e | 25 | #include "ask-password-api.h" |
8c1be37e | 26 | #include "blkid-util.h" |
18c528e9 | 27 | #include "blockdev-util.h" |
5228c58e | 28 | #include "btrfs-util.h" |
f461a28d | 29 | #include "chase.h" |
c2fa92e7 | 30 | #include "conf-files.h" |
28db6fbf | 31 | #include "constants.h" |
3b925504 | 32 | #include "copy.h" |
1e2f3230 | 33 | #include "cryptsetup-util.h" |
553e15f2 | 34 | #include "device-nodes.h" |
8437c059 | 35 | #include "device-util.h" |
f7725647 | 36 | #include "devnum-util.h" |
7718ac97 | 37 | #include "discover-image.h" |
8c1be37e | 38 | #include "dissect-image.h" |
a709a315 | 39 | #include "dm-util.h" |
686d13b9 | 40 | #include "env-file.h" |
88b3300f | 41 | #include "env-util.h" |
d51f8eb3 | 42 | #include "extension-util.h" |
18b5886e | 43 | #include "fd-util.h" |
78ebe980 | 44 | #include "fileio.h" |
2eedfd2d | 45 | #include "fs-util.h" |
cf32c486 | 46 | #include "fsck-util.h" |
8c1be37e | 47 | #include "gpt.h" |
78ebe980 | 48 | #include "hexdecoct.h" |
e2054217 | 49 | #include "hostname-setup.h" |
3b925504 | 50 | #include "id128-util.h" |
593fe6c0 | 51 | #include "import-util.h" |
a4e0d617 | 52 | #include "io-util.h" |
254e392e | 53 | #include "missing_mount.h" |
35cd0ba5 | 54 | #include "mkdir-label.h" |
8c1be37e | 55 | #include "mount-util.h" |
e4de7287 | 56 | #include "mountpoint-util.h" |
6aa05ebd | 57 | #include "namespace-util.h" |
d8b4d14d | 58 | #include "nulstr-util.h" |
c2fa92e7 | 59 | #include "openssl-util.h" |
d58ad743 | 60 | #include "os-util.h" |
8c1be37e | 61 | #include "path-util.h" |
3b925504 LP |
62 | #include "process-util.h" |
63 | #include "raw-clone.h" | |
81939d9d | 64 | #include "resize-fs.h" |
3b925504 | 65 | #include "signal-util.h" |
05c4c59f | 66 | #include "sparse-endian.h" |
8c1be37e | 67 | #include "stat-util.h" |
18b5886e | 68 | #include "stdio-util.h" |
8c1be37e LP |
69 | #include "string-table.h" |
70 | #include "string-util.h" | |
2eedfd2d | 71 | #include "strv.h" |
e4de7287 | 72 | #include "tmpfile-util.h" |
a8040b6d | 73 | #include "udev-util.h" |
2d3a5a73 | 74 | #include "user-util.h" |
41488e1f | 75 | #include "xattr-util.h" |
8c1be37e | 76 | |
28e2641a FF |
77 | /* how many times to wait for the device nodes to appear */ |
78 | #define N_DEVICE_NODE_LIST_ATTEMPTS 10 | |
79 | ||
80ce8580 LP |
80 | int dissect_fstype_ok(const char *fstype) { |
81 | const char *e; | |
82 | bool b; | |
83 | ||
84 | /* When we automatically mount file systems, be a bit conservative by default what we are willing to | |
85 | * mount, just as an extra safety net to not mount with badly maintained legacy file system | |
86 | * drivers. */ | |
87 | ||
88 | e = secure_getenv("SYSTEMD_DISSECT_FILE_SYSTEMS"); | |
89 | if (e) { | |
90 | _cleanup_strv_free_ char **l = NULL; | |
91 | ||
92 | l = strv_split(e, ":"); | |
93 | if (!l) | |
94 | return -ENOMEM; | |
95 | ||
96 | b = strv_contains(l, fstype); | |
97 | } else | |
98 | b = STR_IN_SET(fstype, | |
99 | "btrfs", | |
100 | "erofs", | |
101 | "ext4", | |
102 | "squashfs", | |
103 | "vfat", | |
104 | "xfs"); | |
105 | if (b) | |
106 | return true; | |
107 | ||
108 | log_debug("File system type '%s' is not allowed to be mounted as result of automatic dissection.", fstype); | |
109 | return false; | |
110 | } | |
111 | ||
05c4c59f LP |
112 | int probe_sector_size(int fd, uint32_t *ret) { |
113 | ||
114 | struct gpt_header { | |
115 | char signature[8]; | |
116 | le32_t revision; | |
117 | le32_t header_size; | |
118 | le32_t crc32; | |
119 | le32_t reserved; | |
120 | le64_t my_lba; | |
121 | le64_t alternate_lba; | |
122 | le64_t first_usable_lba; | |
123 | le64_t last_usable_lba; | |
124 | sd_id128_t disk_guid; | |
125 | le64_t partition_entry_lba; | |
126 | le32_t number_of_partition_entries; | |
127 | le32_t size_of_partition_entry; | |
128 | le32_t partition_entry_array_crc32; | |
129 | } _packed_; | |
130 | ||
131 | /* Disk images might be for 512B or for 4096 sector sizes, let's try to auto-detect that by searching | |
132 | * for the GPT headers at the relevant byte offsets */ | |
133 | ||
134 | assert_cc(sizeof(struct gpt_header) == 92); | |
135 | ||
136 | /* We expect a sector size in the range 512…4096. The GPT header is located in the second | |
137 | * sector. Hence it could be at byte 512 at the earliest, and at byte 4096 at the latest. And we must | |
138 | * read with granularity of the largest sector size we care about. Which means 8K. */ | |
139 | uint8_t sectors[2 * 4096]; | |
140 | uint32_t found = 0; | |
141 | ssize_t n; | |
142 | ||
143 | assert(fd >= 0); | |
144 | assert(ret); | |
145 | ||
146 | n = pread(fd, sectors, sizeof(sectors), 0); | |
147 | if (n < 0) | |
148 | return -errno; | |
149 | if (n != sizeof(sectors)) /* too short? */ | |
150 | goto not_found; | |
151 | ||
152 | /* Let's see if we find the GPT partition header with various expected sector sizes */ | |
153 | for (uint32_t sz = 512; sz <= 4096; sz <<= 1) { | |
154 | struct gpt_header *p; | |
155 | ||
156 | assert(sizeof(sectors) >= sz * 2); | |
157 | p = (struct gpt_header*) (sectors + sz); | |
158 | ||
159 | if (memcmp(p->signature, (const char[8]) { 'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T' }, 8) != 0) | |
160 | continue; | |
161 | ||
162 | if (le32toh(p->revision) != UINT32_C(0x00010000)) /* the only known revision of the spec: 1.0 */ | |
163 | continue; | |
164 | ||
165 | if (le32toh(p->header_size) < sizeof(struct gpt_header)) | |
166 | continue; | |
167 | ||
168 | if (le32toh(p->header_size) > 4096) /* larger than a sector? something is off… */ | |
169 | continue; | |
170 | ||
171 | if (le64toh(p->my_lba) != 1) /* this sector must claim to be at sector offset 1 */ | |
172 | continue; | |
173 | ||
174 | if (found != 0) | |
175 | return log_debug_errno(SYNTHETIC_ERRNO(ENOTUNIQ), | |
176 | "Detected valid partition table at offsets matching multiple sector sizes, refusing."); | |
177 | ||
178 | found = sz; | |
179 | } | |
180 | ||
181 | if (found != 0) { | |
182 | log_debug("Determined sector size %" PRIu32 " based on discovered partition table.", found); | |
183 | *ret = found; | |
184 | return 1; /* indicate we *did* find it */ | |
185 | } | |
186 | ||
187 | not_found: | |
188 | log_debug("Couldn't find any partition table to derive sector size of."); | |
189 | *ret = 512; /* pick the traditional default */ | |
190 | return 0; /* indicate we didn't find it */ | |
191 | } | |
192 | ||
81dde3d8 LP |
193 | int probe_sector_size_prefer_ioctl(int fd, uint32_t *ret) { |
194 | struct stat st; | |
195 | ||
196 | assert(fd >= 0); | |
197 | assert(ret); | |
198 | ||
199 | /* Just like probe_sector_size(), but if we are looking at a block device, will use the already | |
200 | * configured sector size rather than probing by contents */ | |
201 | ||
202 | if (fstat(fd, &st) < 0) | |
203 | return -errno; | |
204 | ||
205 | if (S_ISBLK(st.st_mode)) | |
206 | return blockdev_get_sector_size(fd, ret); | |
207 | ||
208 | return probe_sector_size(fd, ret); | |
209 | } | |
210 | ||
c80c9079 LP |
211 | int probe_filesystem_full( |
212 | int fd, | |
213 | const char *path, | |
214 | uint64_t offset, | |
215 | uint64_t size, | |
216 | char **ret_fstype) { | |
217 | ||
7cc84b2c | 218 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
c80c9079 | 219 | * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and a |
7cc84b2c ZJS |
220 | * different error otherwise. */ |
221 | ||
349cc4a5 | 222 | #if HAVE_BLKID |
8e766630 | 223 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
d2c6e79d | 224 | _cleanup_free_ char *path_by_fd = NULL; |
254d1313 | 225 | _cleanup_close_ int fd_close = -EBADF; |
18b5886e LP |
226 | const char *fstype; |
227 | int r; | |
228 | ||
d2c6e79d YW |
229 | assert(fd >= 0 || path); |
230 | assert(ret_fstype); | |
231 | ||
232 | if (fd < 0) { | |
233 | fd_close = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
234 | if (fd_close < 0) | |
235 | return -errno; | |
236 | ||
237 | fd = fd_close; | |
238 | } | |
239 | ||
240 | if (!path) { | |
241 | r = fd_get_path(fd, &path_by_fd); | |
242 | if (r < 0) | |
243 | return r; | |
244 | ||
245 | path = path_by_fd; | |
246 | } | |
247 | ||
c80c9079 LP |
248 | if (size == 0) /* empty size? nothing found! */ |
249 | goto not_found; | |
250 | ||
d2c6e79d | 251 | b = blkid_new_probe(); |
18b5886e | 252 | if (!b) |
d2c6e79d YW |
253 | return -ENOMEM; |
254 | ||
255 | errno = 0; | |
c80c9079 LP |
256 | r = blkid_probe_set_device( |
257 | b, | |
258 | fd, | |
259 | offset, | |
260 | size == UINT64_MAX ? 0 : size); /* when blkid sees size=0 it understands "everything". We prefer using UINT64_MAX for that */ | |
d2c6e79d | 261 | if (r != 0) |
66855de7 | 262 | return errno_or_else(ENOMEM); |
18b5886e LP |
263 | |
264 | blkid_probe_enable_superblocks(b, 1); | |
265 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
266 | ||
267 | errno = 0; | |
268 | r = blkid_do_safeprobe(b); | |
2e3944b8 | 269 | if (r == _BLKID_SAFEPROBE_NOT_FOUND) |
18b5886e | 270 | goto not_found; |
2e3944b8 | 271 | if (r == _BLKID_SAFEPROBE_AMBIGUOUS) |
58dfbfbd | 272 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), |
d2c6e79d | 273 | "Results ambiguous for partition %s", path); |
2e3944b8 | 274 | if (r == _BLKID_SAFEPROBE_ERROR) |
d2c6e79d | 275 | return log_debug_errno(errno_or_else(EIO), "Failed to probe partition %s: %m", path); |
18b5886e | 276 | |
2e3944b8 LP |
277 | assert(r == _BLKID_SAFEPROBE_FOUND); |
278 | ||
18b5886e LP |
279 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); |
280 | ||
281 | if (fstype) { | |
282 | char *t; | |
283 | ||
d2c6e79d | 284 | log_debug("Probed fstype '%s' on partition %s.", fstype, path); |
1cefb9a6 | 285 | |
18b5886e LP |
286 | t = strdup(fstype); |
287 | if (!t) | |
288 | return -ENOMEM; | |
289 | ||
290 | *ret_fstype = t; | |
291 | return 1; | |
292 | } | |
293 | ||
294 | not_found: | |
d2c6e79d | 295 | log_debug("No type detected on partition %s", path); |
18b5886e LP |
296 | *ret_fstype = NULL; |
297 | return 0; | |
d1c536f5 ZJS |
298 | #else |
299 | return -EOPNOTSUPP; | |
a75e27eb | 300 | #endif |
d1c536f5 | 301 | } |
18b5886e | 302 | |
40c10d3f | 303 | #if HAVE_BLKID |
cd22d856 LP |
304 | static int image_policy_may_use( |
305 | const ImagePolicy *policy, | |
306 | PartitionDesignator designator) { | |
307 | ||
308 | PartitionPolicyFlags f; | |
309 | ||
310 | /* For each partition we find in the partition table do a first check if it may exist at all given | |
311 | * the policy, or if it shall be ignored. */ | |
312 | ||
313 | f = image_policy_get_exhaustively(policy, designator); | |
314 | if (f < 0) | |
315 | return f; | |
316 | ||
317 | if ((f & _PARTITION_POLICY_USE_MASK) == PARTITION_POLICY_ABSENT) | |
318 | /* only flag set in policy is "absent"? then this partition may not exist at all */ | |
319 | return log_debug_errno( | |
320 | SYNTHETIC_ERRNO(ERFKILL), | |
321 | "Partition of designator '%s' exists, but not allowed by policy, refusing.", | |
322 | partition_designator_to_string(designator)); | |
323 | if ((f & _PARTITION_POLICY_USE_MASK & ~PARTITION_POLICY_ABSENT) == PARTITION_POLICY_UNUSED) { | |
324 | /* only "unused" or "unused" + "absent" are set? then don't use it */ | |
325 | log_debug("Partition of designator '%s' exists, and policy dictates to ignore it, doing so.", | |
326 | partition_designator_to_string(designator)); | |
327 | return false; /* ignore! */ | |
328 | } | |
329 | ||
330 | return true; /* use! */ | |
331 | } | |
332 | ||
333 | static int image_policy_check_protection( | |
334 | const ImagePolicy *policy, | |
335 | PartitionDesignator designator, | |
336 | PartitionPolicyFlags found_flags) { | |
337 | ||
338 | PartitionPolicyFlags policy_flags; | |
339 | ||
340 | /* Checks if the flags in the policy for the designated partition overlap the flags of what we found */ | |
341 | ||
342 | if (found_flags < 0) | |
343 | return found_flags; | |
344 | ||
345 | policy_flags = image_policy_get_exhaustively(policy, designator); | |
346 | if (policy_flags < 0) | |
347 | return policy_flags; | |
348 | ||
349 | if ((found_flags & policy_flags) == 0) { | |
350 | _cleanup_free_ char *found_flags_string = NULL, *policy_flags_string = NULL; | |
351 | ||
352 | (void) partition_policy_flags_to_string(found_flags, /* simplify= */ true, &found_flags_string); | |
353 | (void) partition_policy_flags_to_string(policy_flags, /* simplify= */ true, &policy_flags_string); | |
354 | ||
355 | return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL), "Partition %s discovered with policy '%s' but '%s' was required, refusing.", | |
356 | partition_designator_to_string(designator), | |
357 | strnull(found_flags_string), strnull(policy_flags_string)); | |
358 | } | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | static int image_policy_check_partition_flags( | |
364 | const ImagePolicy *policy, | |
365 | PartitionDesignator designator, | |
366 | uint64_t gpt_flags) { | |
367 | ||
368 | PartitionPolicyFlags policy_flags; | |
369 | bool b; | |
370 | ||
371 | /* Checks if the partition flags in the policy match reality */ | |
372 | ||
373 | policy_flags = image_policy_get_exhaustively(policy, designator); | |
374 | if (policy_flags < 0) | |
375 | return policy_flags; | |
376 | ||
377 | b = FLAGS_SET(gpt_flags, SD_GPT_FLAG_READ_ONLY); | |
378 | if ((policy_flags & _PARTITION_POLICY_READ_ONLY_MASK) == (b ? PARTITION_POLICY_READ_ONLY_OFF : PARTITION_POLICY_READ_ONLY_ON)) | |
379 | return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL), "Partition %s has 'read-only' flag incorrectly set (must be %s, is %s), refusing.", | |
380 | partition_designator_to_string(designator), | |
381 | one_zero(!b), one_zero(b)); | |
382 | ||
383 | b = FLAGS_SET(gpt_flags, SD_GPT_FLAG_GROWFS); | |
384 | if ((policy_flags & _PARTITION_POLICY_GROWFS_MASK) == (b ? PARTITION_POLICY_GROWFS_OFF : PARTITION_POLICY_GROWFS_ON)) | |
385 | return log_debug_errno(SYNTHETIC_ERRNO(ERFKILL), "Partition %s has 'growfs' flag incorrectly set (must be %s, is %s), refusing.", | |
386 | partition_designator_to_string(designator), | |
387 | one_zero(!b), one_zero(b)); | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static int dissected_image_probe_filesystems( | |
393 | DissectedImage *m, | |
394 | int fd, | |
395 | const ImagePolicy *policy) { | |
396 | ||
698bb074 YW |
397 | int r; |
398 | ||
399 | assert(m); | |
400 | ||
401 | /* Fill in file system types if we don't know them yet. */ | |
402 | ||
403 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
404 | DissectedPartition *p = m->partitions + i; | |
cd22d856 | 405 | PartitionPolicyFlags found_flags; |
698bb074 YW |
406 | |
407 | if (!p->found) | |
408 | continue; | |
409 | ||
c80c9079 LP |
410 | if (!p->fstype) { |
411 | /* If we have an fd referring to the partition block device, use that. Otherwise go | |
412 | * via the whole block device or backing regular file, and read via offset. */ | |
413 | if (p->mount_node_fd >= 0) | |
414 | r = probe_filesystem_full(p->mount_node_fd, p->node, 0, UINT64_MAX, &p->fstype); | |
415 | else | |
416 | r = probe_filesystem_full(fd, p->node, p->offset, p->size, &p->fstype); | |
417 | if (r < 0) | |
698bb074 YW |
418 | return r; |
419 | } | |
420 | ||
cd22d856 | 421 | if (streq_ptr(p->fstype, "crypto_LUKS")) { |
698bb074 | 422 | m->encrypted = true; |
cd22d856 LP |
423 | found_flags = PARTITION_POLICY_ENCRYPTED; /* found this one, and its definitely encrypted */ |
424 | } else | |
425 | /* found it, but it's definitely not encrypted, hence mask the encrypted flag, but | |
426 | * set all other ways that indicate "present". */ | |
427 | found_flags = PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED; | |
698bb074 YW |
428 | |
429 | if (p->fstype && fstype_is_ro(p->fstype)) | |
430 | p->rw = false; | |
431 | ||
432 | if (!p->rw) | |
433 | p->growfs = false; | |
cd22d856 LP |
434 | |
435 | /* We might have learnt more about the file system now (i.e. whether it is encrypted or not), | |
436 | * hence we need to validate this against policy again, to see if the policy still matches | |
437 | * with this new information. Note that image_policy_check_protection() will check for | |
438 | * overlap between what's allowed in the policy and what we pass as 'found_policy' here. In | |
439 | * the unencrypted case we thus might pass an overly unspecific mask here (i.e. unprotected | |
440 | * OR verity OR signed), but that's fine since the earlier policy check already checked more | |
441 | * specific which of those three cases where OK. Keep in mind that this function here only | |
442 | * looks at specific partitions (and thus can only deduce encryption or not) but not the | |
443 | * overall partition table (and thus cannot deduce verity or not). The earlier dissection | |
444 | * checks already did the relevant checks that look at the whole partition table, and | |
445 | * enforced policy there as needed. */ | |
446 | r = image_policy_check_protection(policy, i, found_flags); | |
447 | if (r < 0) | |
448 | return r; | |
698bb074 YW |
449 | } |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
0f7c9a3d LP |
454 | static void check_partition_flags( |
455 | const char *node, | |
456 | unsigned long long pflags, | |
457 | unsigned long long supported) { | |
458 | ||
459 | assert(node); | |
460 | ||
461 | /* Mask away all flags supported by this partition's type and the three flags the UEFI spec defines generically */ | |
92e72028 ZJS |
462 | pflags &= ~(supported | |
463 | SD_GPT_FLAG_REQUIRED_PARTITION | | |
464 | SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL | | |
465 | SD_GPT_FLAG_LEGACY_BIOS_BOOTABLE); | |
0f7c9a3d LP |
466 | |
467 | if (pflags == 0) | |
468 | return; | |
469 | ||
470 | /* If there are other bits set, then log about it, to make things discoverable */ | |
471 | for (unsigned i = 0; i < sizeof(pflags) * 8; i++) { | |
472 | unsigned long long bit = 1ULL << i; | |
473 | if (!FLAGS_SET(pflags, bit)) | |
474 | continue; | |
475 | ||
476 | log_debug("Unexpected partition flag %llu set on %s!", bit, node); | |
477 | } | |
478 | } | |
479 | ||
00e29505 YW |
480 | static int dissected_image_new(const char *path, DissectedImage **ret) { |
481 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
482 | _cleanup_free_ char *name = NULL; | |
483 | int r; | |
484 | ||
485 | assert(ret); | |
486 | ||
487 | if (path) { | |
488 | _cleanup_free_ char *filename = NULL; | |
489 | ||
490 | r = path_extract_filename(path, &filename); | |
491 | if (r < 0) | |
492 | return r; | |
493 | ||
494 | r = raw_strip_suffixes(filename, &name); | |
495 | if (r < 0) | |
496 | return r; | |
497 | ||
498 | if (!image_name_is_valid(name)) { | |
499 | log_debug("Image name %s is not valid, ignoring.", strna(name)); | |
500 | name = mfree(name); | |
501 | } | |
502 | } | |
503 | ||
504 | m = new(DissectedImage, 1); | |
505 | if (!m) | |
506 | return -ENOMEM; | |
507 | ||
508 | *m = (DissectedImage) { | |
509 | .has_init_system = -1, | |
510 | .image_name = TAKE_PTR(name), | |
511 | }; | |
512 | ||
babd5b08 YW |
513 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) |
514 | m->partitions[i] = DISSECTED_PARTITION_NULL; | |
515 | ||
00e29505 YW |
516 | *ret = TAKE_PTR(m); |
517 | return 0; | |
518 | } | |
519 | #endif | |
520 | ||
234c2e16 | 521 | static void dissected_partition_done(DissectedPartition *p) { |
1b010ae7 | 522 | assert(p); |
786e3a52 | 523 | |
1b010ae7 LP |
524 | free(p->fstype); |
525 | free(p->node); | |
526 | free(p->label); | |
527 | free(p->decrypted_fstype); | |
528 | free(p->decrypted_node); | |
529 | free(p->mount_options); | |
f7725647 | 530 | safe_close(p->mount_node_fd); |
786e3a52 | 531 | |
babd5b08 | 532 | *p = DISSECTED_PARTITION_NULL; |
1b010ae7 | 533 | } |
786e3a52 | 534 | |
1b010ae7 | 535 | #if HAVE_BLKID |
1b010ae7 LP |
536 | static int make_partition_devname( |
537 | const char *whole_devname, | |
1a81ddef | 538 | uint64_t diskseq, |
1b010ae7 | 539 | int nr, |
1a81ddef | 540 | DissectImageFlags flags, |
1b010ae7 | 541 | char **ret) { |
786e3a52 | 542 | |
1a81ddef LP |
543 | _cleanup_free_ char *s = NULL; |
544 | int r; | |
786e3a52 | 545 | |
1b010ae7 | 546 | assert(whole_devname); |
1a81ddef LP |
547 | assert(nr != 0); /* zero is not a valid partition nr */ |
548 | assert(ret); | |
aae22eb3 | 549 | |
c12a0d6d | 550 | if (!FLAGS_SET(flags, DISSECT_IMAGE_DISKSEQ_DEVNODE) || diskseq == 0) { |
4ba86848 | 551 | |
1a81ddef LP |
552 | /* Given a whole block device node name (e.g. /dev/sda or /dev/loop7) generate a partition |
553 | * device name (e.g. /dev/sda7 or /dev/loop7p5). The rule the kernel uses is simple: if whole | |
554 | * block device node name ends in a digit, then suffix a 'p', followed by the partition | |
555 | * number. Otherwise, just suffix the partition number without any 'p'. */ | |
08fe0a53 | 556 | |
1a81ddef LP |
557 | if (nr < 0) { /* whole disk? */ |
558 | s = strdup(whole_devname); | |
559 | if (!s) | |
560 | return -ENOMEM; | |
561 | } else { | |
562 | size_t l = strlen(whole_devname); | |
563 | if (l < 1) /* underflow check for the subtraction below */ | |
564 | return -EINVAL; | |
08fe0a53 | 565 | |
1a81ddef LP |
566 | bool need_p = ascii_isdigit(whole_devname[l-1]); /* Last char a digit? */ |
567 | ||
568 | if (asprintf(&s, "%s%s%i", whole_devname, need_p ? "p" : "", nr) < 0) | |
569 | return -ENOMEM; | |
570 | } | |
571 | } else { | |
572 | if (nr < 0) /* whole disk? */ | |
573 | r = asprintf(&s, "/dev/disk/by-diskseq/%" PRIu64, diskseq); | |
574 | else | |
575 | r = asprintf(&s, "/dev/disk/by-diskseq/%" PRIu64 "-part%i", diskseq, nr); | |
576 | if (r < 0) | |
577 | return -ENOMEM; | |
578 | } | |
579 | ||
580 | *ret = TAKE_PTR(s); | |
581 | return 0; | |
08fe0a53 LP |
582 | } |
583 | ||
1a81ddef LP |
584 | static int open_partition( |
585 | const char *node, | |
586 | bool is_partition, | |
587 | const LoopDevice *loop) { | |
588 | ||
f7725647 | 589 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
254d1313 | 590 | _cleanup_close_ int fd = -EBADF; |
f7725647 YW |
591 | dev_t devnum; |
592 | int r; | |
593 | ||
594 | assert(node); | |
595 | assert(loop); | |
596 | ||
597 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
598 | if (fd < 0) | |
599 | return -errno; | |
600 | ||
601 | /* Check if the block device is a child of (or equivalent to) the originally provided one. */ | |
602 | r = block_device_new_from_fd(fd, is_partition ? BLOCK_DEVICE_LOOKUP_WHOLE_DISK : 0, &dev); | |
603 | if (r < 0) | |
604 | return r; | |
605 | ||
606 | r = sd_device_get_devnum(dev, &devnum); | |
607 | if (r < 0) | |
608 | return r; | |
609 | ||
610 | if (loop->devno != devnum) | |
611 | return -ENXIO; | |
612 | ||
613 | /* Also check diskseq. */ | |
c12a0d6d | 614 | if (loop->diskseq != 0) { |
f7725647 YW |
615 | uint64_t diskseq; |
616 | ||
617 | r = fd_get_diskseq(fd, &diskseq); | |
618 | if (r < 0) | |
619 | return r; | |
620 | ||
621 | if (loop->diskseq != diskseq) | |
622 | return -ENXIO; | |
623 | } | |
624 | ||
625 | log_debug("Opened %s (fd=%i, whole_block_devnum=" DEVNUM_FORMAT_STR ", diskseq=%" PRIu64 ").", | |
626 | node, fd, DEVNUM_FORMAT_VAL(loop->devno), loop->diskseq); | |
627 | return TAKE_FD(fd); | |
628 | } | |
629 | ||
22e932f4 DDM |
630 | static int compare_arch(Architecture a, Architecture b) { |
631 | if (a == b) | |
632 | return 0; | |
633 | ||
634 | if (a == native_architecture()) | |
635 | return 1; | |
636 | ||
637 | if (b == native_architecture()) | |
638 | return -1; | |
639 | ||
640 | #ifdef ARCHITECTURE_SECONDARY | |
641 | if (a == ARCHITECTURE_SECONDARY) | |
642 | return 1; | |
643 | ||
644 | if (b == ARCHITECTURE_SECONDARY) | |
645 | return -1; | |
646 | #endif | |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
08f14be4 YW |
651 | static int dissect_image( |
652 | DissectedImage *m, | |
4526113f | 653 | int fd, |
0b214aa0 | 654 | const char *devname, |
89e62e0b | 655 | const VeritySettings *verity, |
18d73705 | 656 | const MountOptions *mount_options, |
84be0c71 | 657 | const ImagePolicy *policy, |
08f14be4 | 658 | DissectImageFlags flags) { |
8c1be37e | 659 | |
62ea0ed0 | 660 | sd_id128_t root_uuid = SD_ID128_NULL, root_verity_uuid = SD_ID128_NULL; |
62ea0ed0 | 661 | sd_id128_t usr_uuid = SD_ID128_NULL, usr_verity_uuid = SD_ID128_NULL; |
1f8fb21c | 662 | bool is_gpt, is_mbr, multiple_generic = false, |
de98f631 LP |
663 | generic_rw = false, /* initialize to appease gcc */ |
664 | generic_growfs = false; | |
8e766630 | 665 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
8c1be37e | 666 | _cleanup_free_ char *generic_node = NULL; |
be30ad41 | 667 | sd_id128_t generic_uuid = SD_ID128_NULL; |
b387778c | 668 | const char *pttype = NULL, *sptuuid = NULL; |
8c1be37e | 669 | blkid_partlist pl; |
1f8fb21c | 670 | int r, generic_nr = -1, n_partitions; |
8c1be37e | 671 | |
08f14be4 | 672 | assert(m); |
8c1be37e | 673 | assert(fd >= 0); |
0b214aa0 | 674 | assert(devname); |
a0bff7ea | 675 | assert(!verity || verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
89e62e0b | 676 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
a0bff7ea LP |
677 | assert(!verity || verity->root_hash_sig || verity->root_hash_sig_size == 0); |
678 | assert(!verity || (verity->root_hash || !verity->root_hash_sig)); | |
e7cbe5cb | 679 | assert(!((flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_NO_PARTITION_TABLE))); |
22ee78a8 | 680 | assert(m->sector_size > 0); |
8c1be37e LP |
681 | |
682 | /* Probes a disk image, and returns information about what it found in *ret. | |
683 | * | |
4623e8e6 | 684 | * Returns -ENOPKG if no suitable partition table or file system could be found. |
2679f407 LP |
685 | * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. |
686 | * Returns -ENXIO if we couldn't find any partition suitable as root or /usr partition | |
97ce55e3 LP |
687 | * Returns -ENOTUNIQ if we only found multiple generic partitions and thus don't know what to do with that |
688 | * Returns -ERFKILL if image doesn't match image policy | |
689 | * Returns -EBADR if verity data was provided externally for an image that has a GPT partition table (i.e. is not just a naked fs) | |
690 | * Returns -EPROTONOSUPPORT if DISSECT_IMAGE_ADD_PARTITION_DEVICES is set but the block device does not have partition logic enabled | |
691 | * Returns -ENOMSG if we didn't find a single usable partition (and DISSECT_IMAGE_REFUSE_EMPTY is set) */ | |
4623e8e6 | 692 | |
c12a0d6d | 693 | uint64_t diskseq = m->loop ? m->loop->diskseq : 0; |
1a81ddef | 694 | |
89e62e0b | 695 | if (verity && verity->root_hash) { |
aee36b4e LP |
696 | sd_id128_t fsuuid, vuuid; |
697 | ||
698 | /* If a root hash is supplied, then we use the root partition that has a UUID that match the | |
699 | * first 128bit of the root hash. And we use the verity partition that has a UUID that match | |
700 | * the final 128bit. */ | |
4623e8e6 | 701 | |
89e62e0b | 702 | if (verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
703 | return -EINVAL; |
704 | ||
aee36b4e LP |
705 | memcpy(&fsuuid, verity->root_hash, sizeof(sd_id128_t)); |
706 | memcpy(&vuuid, (const uint8_t*) verity->root_hash + verity->root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t)); | |
4623e8e6 | 707 | |
aee36b4e | 708 | if (sd_id128_is_null(fsuuid)) |
4623e8e6 | 709 | return -EINVAL; |
aee36b4e | 710 | if (sd_id128_is_null(vuuid)) |
4623e8e6 | 711 | return -EINVAL; |
aee36b4e LP |
712 | |
713 | /* If the verity data declares it's for the /usr partition, then search for that, in all | |
714 | * other cases assume it's for the root partition. */ | |
715 | if (verity->designator == PARTITION_USR) { | |
716 | usr_uuid = fsuuid; | |
717 | usr_verity_uuid = vuuid; | |
718 | } else { | |
719 | root_uuid = fsuuid; | |
720 | root_verity_uuid = vuuid; | |
721 | } | |
4623e8e6 | 722 | } |
8c1be37e | 723 | |
8c1be37e LP |
724 | b = blkid_new_probe(); |
725 | if (!b) | |
726 | return -ENOMEM; | |
727 | ||
728 | errno = 0; | |
729 | r = blkid_probe_set_device(b, fd, 0, 0); | |
b382db9f | 730 | if (r != 0) |
66855de7 | 731 | return errno_or_else(ENOMEM); |
8c1be37e | 732 | |
22ee78a8 LP |
733 | errno = 0; |
734 | r = blkid_probe_set_sectorsize(b, m->sector_size); | |
735 | if (r != 0) | |
736 | return errno_or_else(EIO); | |
737 | ||
9b6deb03 LP |
738 | if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) { |
739 | /* Look for file system superblocks, unless we only shall look for GPT partition tables */ | |
740 | blkid_probe_enable_superblocks(b, 1); | |
b387778c | 741 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE|BLKID_SUBLKS_UUID); |
9b6deb03 LP |
742 | } |
743 | ||
8c1be37e LP |
744 | blkid_probe_enable_partitions(b, 1); |
745 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
746 | ||
747 | errno = 0; | |
748 | r = blkid_do_safeprobe(b); | |
2e3944b8 | 749 | if (r == _BLKID_SAFEPROBE_ERROR) |
66855de7 | 750 | return errno_or_else(EIO); |
2e3944b8 LP |
751 | if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND)) |
752 | return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); | |
753 | ||
754 | assert(r == _BLKID_SAFEPROBE_FOUND); | |
8c1be37e | 755 | |
e7cbe5cb | 756 | if ((!(flags & DISSECT_IMAGE_GPT_ONLY) && |
4b5de5dd | 757 | (flags & DISSECT_IMAGE_GENERIC_ROOT)) || |
e7cbe5cb | 758 | (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)) { |
9b6deb03 | 759 | const char *usage = NULL; |
8c1be37e | 760 | |
aee36b4e LP |
761 | /* If flags permit this, also allow using non-partitioned single-filesystem images */ |
762 | ||
9b6deb03 LP |
763 | (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL); |
764 | if (STRPTR_IN_SET(usage, "filesystem", "crypto")) { | |
18d73705 | 765 | _cleanup_free_ char *t = NULL, *n = NULL, *o = NULL; |
b387778c | 766 | const char *fstype = NULL, *options = NULL, *suuid = NULL; |
254d1313 | 767 | _cleanup_close_ int mount_node_fd = -EBADF; |
b387778c | 768 | sd_id128_t uuid = SD_ID128_NULL; |
cd22d856 LP |
769 | PartitionPolicyFlags found_flags; |
770 | bool encrypted; | |
771 | ||
772 | /* OK, we have found a file system, that's our root partition then. */ | |
773 | ||
774 | r = image_policy_may_use(policy, PARTITION_ROOT); | |
775 | if (r < 0) | |
776 | return r; | |
777 | if (r == 0) /* policy says ignore this, so we ignore it */ | |
778 | return -ENOPKG; | |
779 | ||
780 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
781 | (void) blkid_probe_lookup_value(b, "UUID", &suuid, NULL); | |
782 | ||
783 | encrypted = streq_ptr(fstype, "crypto_LUKS"); | |
784 | ||
785 | if (verity_settings_data_covers(verity, PARTITION_ROOT)) | |
786 | found_flags = verity->root_hash_sig ? PARTITION_POLICY_SIGNED : PARTITION_POLICY_VERITY; | |
787 | else | |
788 | found_flags = encrypted ? PARTITION_POLICY_ENCRYPTED : PARTITION_POLICY_UNPROTECTED; | |
789 | ||
790 | r = image_policy_check_protection(policy, PARTITION_ROOT, found_flags); | |
791 | if (r < 0) | |
792 | return r; | |
793 | ||
794 | r = image_policy_check_partition_flags(policy, PARTITION_ROOT, 0); /* we have no gpt partition flags, hence check against all bits off */ | |
795 | if (r < 0) | |
796 | return r; | |
f7725647 | 797 | |
73d88b80 | 798 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) { |
f7725647 YW |
799 | mount_node_fd = open_partition(devname, /* is_partition = */ false, m->loop); |
800 | if (mount_node_fd < 0) | |
801 | return mount_node_fd; | |
802 | } | |
8c1be37e | 803 | |
9b6deb03 LP |
804 | if (fstype) { |
805 | t = strdup(fstype); | |
806 | if (!t) | |
807 | return -ENOMEM; | |
808 | } | |
809 | ||
b387778c LP |
810 | if (suuid) { |
811 | /* blkid will return FAT's serial number as UUID, hence it is quite possible | |
812 | * that parsing this will fail. We'll ignore the ID, since it's just too | |
813 | * short to be useful as tru identifier. */ | |
814 | r = sd_id128_from_string(suuid, &uuid); | |
815 | if (r < 0) | |
816 | log_debug_errno(r, "Failed to parse file system UUID '%s', ignoring: %m", suuid); | |
817 | } | |
818 | ||
1a81ddef LP |
819 | r = make_partition_devname(devname, diskseq, -1, flags, &n); |
820 | if (r < 0) | |
821 | return r; | |
6c544d14 | 822 | |
e7cbe5cb | 823 | m->single_file_system = true; |
cd22d856 | 824 | m->encrypted = encrypted; |
c3c88d67 LP |
825 | |
826 | m->has_verity = verity && verity->data_path; | |
c2534821 | 827 | m->verity_ready = verity_settings_data_covers(verity, PARTITION_ROOT); |
e7cbe5cb | 828 | |
8ee9615e | 829 | m->has_verity_sig = false; /* signature not embedded, must be specified */ |
c2534821 | 830 | m->verity_sig_ready = m->verity_ready && verity->root_hash_sig; |
8ee9615e | 831 | |
b387778c LP |
832 | m->image_uuid = uuid; |
833 | ||
f5215bc8 | 834 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); |
18d73705 LB |
835 | if (options) { |
836 | o = strdup(options); | |
837 | if (!o) | |
838 | return -ENOMEM; | |
839 | } | |
840 | ||
9b6deb03 LP |
841 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
842 | .found = true, | |
e0d53d52 | 843 | .rw = !m->verity_ready && !fstype_is_ro(fstype), |
9b6deb03 LP |
844 | .partno = -1, |
845 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
846 | .fstype = TAKE_PTR(t), |
847 | .node = TAKE_PTR(n), | |
18d73705 | 848 | .mount_options = TAKE_PTR(o), |
f7725647 | 849 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
850 | .offset = 0, |
851 | .size = UINT64_MAX, | |
9b6deb03 | 852 | }; |
8c1be37e | 853 | |
9b6deb03 LP |
854 | return 0; |
855 | } | |
8c1be37e LP |
856 | } |
857 | ||
858 | (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); | |
859 | if (!pttype) | |
860 | return -ENOPKG; | |
861 | ||
862 | is_gpt = streq_ptr(pttype, "gpt"); | |
863 | is_mbr = streq_ptr(pttype, "dos"); | |
864 | ||
9b6deb03 | 865 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
866 | return -ENOPKG; |
867 | ||
0903fd26 LP |
868 | /* We support external verity data partitions only if the image has no partition table */ |
869 | if (verity && verity->data_path) | |
870 | return -EBADR; | |
871 | ||
73d88b80 | 872 | if (FLAGS_SET(flags, DISSECT_IMAGE_ADD_PARTITION_DEVICES)) { |
08f14be4 YW |
873 | /* Safety check: refuse block devices that carry a partition table but for which the kernel doesn't |
874 | * do partition scanning. */ | |
875 | r = blockdev_partscan_enabled(fd); | |
876 | if (r < 0) | |
877 | return r; | |
878 | if (r == 0) | |
879 | return -EPROTONOSUPPORT; | |
880 | } | |
4ba86848 | 881 | |
b387778c LP |
882 | (void) blkid_probe_lookup_value(b, "PTUUID", &sptuuid, NULL); |
883 | if (sptuuid) { | |
884 | r = sd_id128_from_string(sptuuid, &m->image_uuid); | |
885 | if (r < 0) | |
886 | log_debug_errno(r, "Failed to parse partition table UUID '%s', ignoring: %m", sptuuid); | |
887 | } | |
888 | ||
8c1be37e LP |
889 | errno = 0; |
890 | pl = blkid_probe_get_partitions(b); | |
b382db9f | 891 | if (!pl) |
66855de7 | 892 | return errno_or_else(ENOMEM); |
8c1be37e | 893 | |
4ba86848 LP |
894 | errno = 0; |
895 | n_partitions = blkid_partlist_numof_partitions(pl); | |
896 | if (n_partitions < 0) | |
897 | return errno_or_else(EIO); | |
8c1be37e | 898 | |
4ba86848 | 899 | for (int i = 0; i < n_partitions; i++) { |
1b010ae7 | 900 | _cleanup_free_ char *node = NULL; |
9b6deb03 | 901 | unsigned long long pflags; |
88b3300f | 902 | blkid_loff_t start, size; |
8c1be37e | 903 | blkid_partition pp; |
8c1be37e LP |
904 | int nr; |
905 | ||
4ba86848 LP |
906 | errno = 0; |
907 | pp = blkid_partlist_get_partition(pl, i); | |
908 | if (!pp) | |
909 | return errno_or_else(EIO); | |
aae22eb3 | 910 | |
9b6deb03 | 911 | pflags = blkid_partition_get_flags(pp); |
8c1be37e | 912 | |
4ba86848 | 913 | errno = 0; |
8c1be37e LP |
914 | nr = blkid_partition_get_partno(pp); |
915 | if (nr < 0) | |
4ba86848 | 916 | return errno_or_else(EIO); |
8c1be37e | 917 | |
88b3300f LP |
918 | errno = 0; |
919 | start = blkid_partition_get_start(pp); | |
920 | if (start < 0) | |
921 | return errno_or_else(EIO); | |
922 | ||
923 | assert((uint64_t) start < UINT64_MAX/512); | |
924 | ||
925 | errno = 0; | |
926 | size = blkid_partition_get_size(pp); | |
927 | if (size < 0) | |
928 | return errno_or_else(EIO); | |
929 | ||
930 | assert((uint64_t) size < UINT64_MAX/512); | |
931 | ||
1a81ddef LP |
932 | /* While probing we need the non-diskseq device node name to access the thing, hence mask off |
933 | * DISSECT_IMAGE_DISKSEQ_DEVNODE. */ | |
934 | r = make_partition_devname(devname, diskseq, nr, flags & ~DISSECT_IMAGE_DISKSEQ_DEVNODE, &node); | |
1b010ae7 LP |
935 | if (r < 0) |
936 | return r; | |
937 | ||
938 | /* So here's the thing: after the main ("whole") block device popped up it might take a while | |
939 | * before the kernel fully probed the partition table. Waiting for that to finish is icky in | |
940 | * userspace. So here's what we do instead. We issue the BLKPG_ADD_PARTITION ioctl to add the | |
941 | * partition ourselves, racing against the kernel. Good thing is: if this call fails with | |
942 | * EBUSY then the kernel was quicker than us, and that's totally OK, the outcome is good for | |
943 | * us: the device node will exist. If OTOH our call was successful we won the race. Which is | |
944 | * also good as the outcome is the same: the partition block device exists, and we can use | |
945 | * it. | |
946 | * | |
947 | * Kernel returns EBUSY if there's already a partition by that number or an overlapping | |
948 | * partition already existent. */ | |
949 | ||
73d88b80 | 950 | if (FLAGS_SET(flags, DISSECT_IMAGE_ADD_PARTITION_DEVICES)) { |
08f14be4 YW |
951 | r = block_device_add_partition(fd, node, nr, (uint64_t) start * 512, (uint64_t) size * 512); |
952 | if (r < 0) { | |
953 | if (r != -EBUSY) | |
954 | return log_debug_errno(r, "BLKPG_ADD_PARTITION failed: %m"); | |
1b010ae7 | 955 | |
08f14be4 YW |
956 | log_debug_errno(r, "Kernel was quicker than us in adding partition %i.", nr); |
957 | } else | |
958 | log_debug("We were quicker than kernel in adding partition %i.", nr); | |
959 | } | |
1b010ae7 | 960 | |
8c1be37e | 961 | if (is_gpt) { |
e3b9a5ff | 962 | const char *fstype = NULL, *label; |
4623e8e6 | 963 | sd_id128_t type_id, id; |
22e932f4 | 964 | GptPartitionType type; |
de98f631 | 965 | bool rw = true, growfs = false; |
8c1be37e | 966 | |
e3b9a5ff LP |
967 | r = blkid_partition_get_uuid_id128(pp, &id); |
968 | if (r < 0) { | |
969 | log_debug_errno(r, "Failed to read partition UUID, ignoring: %m"); | |
4623e8e6 | 970 | continue; |
e3b9a5ff | 971 | } |
4623e8e6 | 972 | |
e3b9a5ff LP |
973 | r = blkid_partition_get_type_id128(pp, &type_id); |
974 | if (r < 0) { | |
975 | log_debug_errno(r, "Failed to read partition type UUID, ignoring: %m"); | |
8c1be37e | 976 | continue; |
e3b9a5ff | 977 | } |
8c1be37e | 978 | |
22e932f4 DDM |
979 | type = gpt_partition_type_from_uuid(type_id); |
980 | ||
08fe0a53 LP |
981 | label = blkid_partition_get_name(pp); /* libblkid returns NULL here if empty */ |
982 | ||
fb3921b8 LP |
983 | if (IN_SET(type.designator, |
984 | PARTITION_HOME, | |
985 | PARTITION_SRV, | |
986 | PARTITION_XBOOTLDR, | |
987 | PARTITION_TMP)) { | |
a48dd347 | 988 | |
92e72028 ZJS |
989 | check_partition_flags(node, pflags, |
990 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 991 | |
92e72028 | 992 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
993 | continue; |
994 | ||
92e72028 ZJS |
995 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
996 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
aee36b4e | 997 | |
22e932f4 | 998 | } else if (type.designator == PARTITION_ESP) { |
a48dd347 | 999 | |
92e72028 ZJS |
1000 | /* Note that we don't check the SD_GPT_FLAG_NO_AUTO flag for the ESP, as it is |
1001 | * not defined there. We instead check the SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as | |
aee36b4e LP |
1002 | * recommended by the UEFI spec (See "12.3.3 Number and Location of System |
1003 | * Partitions"). */ | |
a48dd347 | 1004 | |
92e72028 | 1005 | if (pflags & SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL) |
a48dd347 LP |
1006 | continue; |
1007 | ||
8c1be37e | 1008 | fstype = "vfat"; |
a8c47660 | 1009 | |
22e932f4 | 1010 | } else if (type.designator == PARTITION_ROOT) { |
4623e8e6 | 1011 | |
92e72028 ZJS |
1012 | check_partition_flags(node, pflags, |
1013 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 1014 | |
92e72028 | 1015 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
1016 | continue; |
1017 | ||
4623e8e6 LP |
1018 | /* If a root ID is specified, ignore everything but the root id */ |
1019 | if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) | |
1020 | continue; | |
1021 | ||
92e72028 ZJS |
1022 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
1023 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
aee36b4e | 1024 | |
22e932f4 | 1025 | } else if (type.designator == PARTITION_ROOT_VERITY) { |
4623e8e6 | 1026 | |
92e72028 ZJS |
1027 | check_partition_flags(node, pflags, |
1028 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
0f7c9a3d | 1029 | |
92e72028 | 1030 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
1031 | continue; |
1032 | ||
c3c88d67 | 1033 | m->has_verity = true; |
4623e8e6 | 1034 | |
8ee9615e LP |
1035 | /* If no verity configuration is specified, then don't do verity */ |
1036 | if (!verity) | |
1037 | continue; | |
1038 | if (verity->designator >= 0 && verity->designator != PARTITION_ROOT) | |
1039 | continue; | |
1040 | ||
1041 | /* If root hash is specified, then ignore everything but the root id */ | |
1042 | if (!sd_id128_is_null(root_verity_uuid) && !sd_id128_equal(root_verity_uuid, id)) | |
4623e8e6 LP |
1043 | continue; |
1044 | ||
4623e8e6 | 1045 | fstype = "DM_verity_hash"; |
4623e8e6 | 1046 | rw = false; |
8ee9615e | 1047 | |
22e932f4 | 1048 | } else if (type.designator == PARTITION_ROOT_VERITY_SIG) { |
8ee9615e | 1049 | |
92e72028 ZJS |
1050 | check_partition_flags(node, pflags, |
1051 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
8ee9615e | 1052 | |
92e72028 | 1053 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
8ee9615e LP |
1054 | continue; |
1055 | ||
1056 | m->has_verity_sig = true; | |
1057 | ||
8ee9615e LP |
1058 | if (!verity) |
1059 | continue; | |
1060 | if (verity->designator >= 0 && verity->designator != PARTITION_ROOT) | |
1061 | continue; | |
8ee9615e | 1062 | |
8ee9615e | 1063 | fstype = "verity_hash_signature"; |
4623e8e6 | 1064 | rw = false; |
8ee9615e | 1065 | |
22e932f4 | 1066 | } else if (type.designator == PARTITION_USR) { |
aee36b4e | 1067 | |
92e72028 ZJS |
1068 | check_partition_flags(node, pflags, |
1069 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
aee36b4e | 1070 | |
92e72028 | 1071 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
aee36b4e LP |
1072 | continue; |
1073 | ||
1074 | /* If a usr ID is specified, ignore everything but the usr id */ | |
1075 | if (!sd_id128_is_null(usr_uuid) && !sd_id128_equal(usr_uuid, id)) | |
1076 | continue; | |
1077 | ||
92e72028 ZJS |
1078 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
1079 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
aee36b4e | 1080 | |
22e932f4 | 1081 | } else if (type.designator == PARTITION_USR_VERITY) { |
aee36b4e | 1082 | |
92e72028 ZJS |
1083 | check_partition_flags(node, pflags, |
1084 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
aee36b4e | 1085 | |
92e72028 | 1086 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
aee36b4e LP |
1087 | continue; |
1088 | ||
c3c88d67 | 1089 | m->has_verity = true; |
aee36b4e | 1090 | |
8ee9615e LP |
1091 | if (!verity) |
1092 | continue; | |
1093 | if (verity->designator >= 0 && verity->designator != PARTITION_USR) | |
1094 | continue; | |
1095 | ||
1096 | /* If usr hash is specified, then ignore everything but the usr id */ | |
1097 | if (!sd_id128_is_null(usr_verity_uuid) && !sd_id128_equal(usr_verity_uuid, id)) | |
aee36b4e LP |
1098 | continue; |
1099 | ||
aee36b4e | 1100 | fstype = "DM_verity_hash"; |
aee36b4e | 1101 | rw = false; |
8ee9615e | 1102 | |
22e932f4 | 1103 | } else if (type.designator == PARTITION_USR_VERITY_SIG) { |
8ee9615e | 1104 | |
92e72028 ZJS |
1105 | check_partition_flags(node, pflags, |
1106 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY); | |
8ee9615e | 1107 | |
92e72028 | 1108 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
8ee9615e LP |
1109 | continue; |
1110 | ||
1111 | m->has_verity_sig = true; | |
1112 | ||
8ee9615e LP |
1113 | if (!verity) |
1114 | continue; | |
1115 | if (verity->designator >= 0 && verity->designator != PARTITION_USR) | |
1116 | continue; | |
8ee9615e | 1117 | |
8ee9615e | 1118 | fstype = "verity_hash_signature"; |
aee36b4e | 1119 | rw = false; |
8ee9615e | 1120 | |
22e932f4 | 1121 | } else if (type.designator == PARTITION_SWAP) { |
a48dd347 | 1122 | |
92e72028 | 1123 | check_partition_flags(node, pflags, SD_GPT_FLAG_NO_AUTO); |
0f7c9a3d | 1124 | |
92e72028 | 1125 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
1126 | continue; |
1127 | ||
41aca66b LP |
1128 | /* Note: we don't set fstype = "swap" here, because we still need to probe if |
1129 | * it might be encrypted (i.e. fstype "crypt_LUKS") or unencrypted | |
1130 | * (i.e. fstype "swap"), and the only way to figure that out is via fstype | |
1131 | * probing. */ | |
df4524cb | 1132 | |
df655bf3 DDM |
1133 | /* We don't have a designator for SD_GPT_LINUX_GENERIC so check the UUID instead. */ |
1134 | } else if (sd_id128_equal(type.uuid, SD_GPT_LINUX_GENERIC)) { | |
8c1be37e | 1135 | |
92e72028 ZJS |
1136 | check_partition_flags(node, pflags, |
1137 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 1138 | |
92e72028 | 1139 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
a48dd347 LP |
1140 | continue; |
1141 | ||
8c1be37e LP |
1142 | if (generic_node) |
1143 | multiple_generic = true; | |
1144 | else { | |
1145 | generic_nr = nr; | |
92e72028 ZJS |
1146 | generic_rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
1147 | generic_growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
be30ad41 | 1148 | generic_uuid = id; |
a5590886 | 1149 | generic_node = TAKE_PTR(node); |
8c1be37e | 1150 | } |
d4dffb85 | 1151 | |
22e932f4 | 1152 | } else if (type.designator == PARTITION_VAR) { |
d4dffb85 | 1153 | |
92e72028 ZJS |
1154 | check_partition_flags(node, pflags, |
1155 | SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS); | |
0f7c9a3d | 1156 | |
92e72028 | 1157 | if (pflags & SD_GPT_FLAG_NO_AUTO) |
d4dffb85 LP |
1158 | continue; |
1159 | ||
1160 | if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) { | |
1161 | sd_id128_t var_uuid; | |
1162 | ||
1163 | /* For /var we insist that the uuid of the partition matches the | |
1164 | * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine | |
1165 | * ID. Why? Unlike the other partitions /var is inherently | |
1166 | * installation specific, hence we need to be careful not to mount it | |
1167 | * in the wrong installation. By hashing the partition UUID from | |
1168 | * /etc/machine-id we can securely bind the partition to the | |
1169 | * installation. */ | |
1170 | ||
92e72028 | 1171 | r = sd_id128_get_machine_app_specific(SD_GPT_VAR, &var_uuid); |
d4dffb85 LP |
1172 | if (r < 0) |
1173 | return r; | |
1174 | ||
1175 | if (!sd_id128_equal(var_uuid, id)) { | |
a52efa81 YW |
1176 | log_debug("Found a /var/ partition, but its UUID didn't match our expectations " |
1177 | "(found: " SD_ID128_UUID_FORMAT_STR ", expected: " SD_ID128_UUID_FORMAT_STR "), ignoring.", | |
1178 | SD_ID128_FORMAT_VAL(id), SD_ID128_FORMAT_VAL(var_uuid)); | |
d4dffb85 LP |
1179 | continue; |
1180 | } | |
1181 | } | |
1182 | ||
92e72028 ZJS |
1183 | rw = !(pflags & SD_GPT_FLAG_READ_ONLY); |
1184 | growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS); | |
8c1be37e LP |
1185 | } |
1186 | ||
22e932f4 | 1187 | if (type.designator != _PARTITION_DESIGNATOR_INVALID) { |
1a81ddef | 1188 | _cleanup_free_ char *t = NULL, *o = NULL, *l = NULL, *n = NULL; |
254d1313 | 1189 | _cleanup_close_ int mount_node_fd = -EBADF; |
18d73705 | 1190 | const char *options = NULL; |
8c1be37e | 1191 | |
cd22d856 LP |
1192 | r = image_policy_may_use(policy, type.designator); |
1193 | if (r < 0) | |
1194 | return r; | |
1195 | if (r == 0) { | |
1196 | /* Policy says: ignore; Remember this fact, so that we later can distinguish between "found but ignored" and "not found at all" */ | |
1197 | ||
1198 | if (!m->partitions[type.designator].found) | |
1199 | m->partitions[type.designator].ignored = true; | |
1200 | ||
1201 | continue; | |
1202 | } | |
1203 | ||
22e932f4 | 1204 | if (m->partitions[type.designator].found) { |
08fe0a53 LP |
1205 | /* For most partition types the first one we see wins. Except for the |
1206 | * rootfs and /usr, where we do a version compare of the label, and | |
1207 | * let the newest version win. This permits a simple A/B versioning | |
1208 | * scheme in OS images. */ | |
1209 | ||
22e932f4 DDM |
1210 | if (compare_arch(type.arch, m->partitions[type.designator].architecture) <= 0) |
1211 | continue; | |
1212 | ||
1213 | if (!partition_designator_is_versioned(type.designator) || | |
1214 | strverscmp_improved(m->partitions[type.designator].label, label) >= 0) | |
08fe0a53 LP |
1215 | continue; |
1216 | ||
22e932f4 | 1217 | dissected_partition_done(m->partitions + type.designator); |
08fe0a53 | 1218 | } |
8c1be37e | 1219 | |
73d88b80 | 1220 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES) && |
f52261a0 | 1221 | type.designator != PARTITION_SWAP) { |
f7725647 YW |
1222 | mount_node_fd = open_partition(node, /* is_partition = */ true, m->loop); |
1223 | if (mount_node_fd < 0) | |
1224 | return mount_node_fd; | |
1225 | } | |
1226 | ||
1a81ddef LP |
1227 | r = make_partition_devname(devname, diskseq, nr, flags, &n); |
1228 | if (r < 0) | |
1229 | return r; | |
1230 | ||
8c1be37e LP |
1231 | if (fstype) { |
1232 | t = strdup(fstype); | |
1233 | if (!t) | |
1234 | return -ENOMEM; | |
1235 | } | |
1236 | ||
08fe0a53 LP |
1237 | if (label) { |
1238 | l = strdup(label); | |
1239 | if (!l) | |
1240 | return -ENOMEM; | |
1241 | } | |
1242 | ||
22e932f4 | 1243 | options = mount_options_from_designator(mount_options, type.designator); |
18d73705 LB |
1244 | if (options) { |
1245 | o = strdup(options); | |
1246 | if (!o) | |
1247 | return -ENOMEM; | |
1248 | } | |
1249 | ||
22e932f4 | 1250 | m->partitions[type.designator] = (DissectedPartition) { |
8c1be37e LP |
1251 | .found = true, |
1252 | .partno = nr, | |
1253 | .rw = rw, | |
de98f631 | 1254 | .growfs = growfs, |
22e932f4 | 1255 | .architecture = type.arch, |
1a81ddef | 1256 | .node = TAKE_PTR(n), |
1cc6c93a | 1257 | .fstype = TAKE_PTR(t), |
08fe0a53 | 1258 | .label = TAKE_PTR(l), |
be30ad41 | 1259 | .uuid = id, |
18d73705 | 1260 | .mount_options = TAKE_PTR(o), |
f7725647 | 1261 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
1262 | .offset = (uint64_t) start * 512, |
1263 | .size = (uint64_t) size * 512, | |
d90b03f8 | 1264 | .gpt_flags = pflags, |
8c1be37e | 1265 | }; |
8c1be37e LP |
1266 | } |
1267 | ||
1268 | } else if (is_mbr) { | |
1269 | ||
a8c47660 | 1270 | switch (blkid_partition_get_type(pp)) { |
8c1be37e | 1271 | |
a8c47660 LP |
1272 | case 0x83: /* Linux partition */ |
1273 | ||
1274 | if (pflags != 0x80) /* Bootable flag */ | |
1275 | continue; | |
8c1be37e | 1276 | |
a8c47660 LP |
1277 | if (generic_node) |
1278 | multiple_generic = true; | |
1279 | else { | |
1280 | generic_nr = nr; | |
1281 | generic_rw = true; | |
de98f631 | 1282 | generic_growfs = false; |
a5590886 | 1283 | generic_node = TAKE_PTR(node); |
a8c47660 LP |
1284 | } |
1285 | ||
1286 | break; | |
1287 | ||
1288 | case 0xEA: { /* Boot Loader Spec extended $BOOT partition */ | |
254d1313 | 1289 | _cleanup_close_ int mount_node_fd = -EBADF; |
1a81ddef | 1290 | _cleanup_free_ char *o = NULL, *n = NULL; |
a8c47660 | 1291 | sd_id128_t id = SD_ID128_NULL; |
e3b9a5ff | 1292 | const char *options = NULL; |
a8c47660 | 1293 | |
cd22d856 LP |
1294 | r = image_policy_may_use(policy, PARTITION_XBOOTLDR); |
1295 | if (r < 0) | |
1296 | return r; | |
1297 | if (r == 0) { /* policy says: ignore */ | |
1298 | if (!m->partitions[PARTITION_XBOOTLDR].found) | |
1299 | m->partitions[PARTITION_XBOOTLDR].ignored = true; | |
1300 | ||
1301 | continue; | |
1302 | } | |
1303 | ||
a8c47660 | 1304 | /* First one wins */ |
234c2e16 | 1305 | if (m->partitions[PARTITION_XBOOTLDR].found) |
a8c47660 LP |
1306 | continue; |
1307 | ||
73d88b80 | 1308 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) { |
f7725647 YW |
1309 | mount_node_fd = open_partition(node, /* is_partition = */ true, m->loop); |
1310 | if (mount_node_fd < 0) | |
1311 | return mount_node_fd; | |
1312 | } | |
1313 | ||
e3b9a5ff | 1314 | (void) blkid_partition_get_uuid_id128(pp, &id); |
a8c47660 | 1315 | |
1a81ddef LP |
1316 | r = make_partition_devname(devname, diskseq, nr, flags, &n); |
1317 | if (r < 0) | |
1318 | return r; | |
1319 | ||
f5215bc8 | 1320 | options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR); |
18d73705 LB |
1321 | if (options) { |
1322 | o = strdup(options); | |
1323 | if (!o) | |
1324 | return -ENOMEM; | |
1325 | } | |
1326 | ||
a8c47660 LP |
1327 | m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) { |
1328 | .found = true, | |
1329 | .partno = nr, | |
1330 | .rw = true, | |
de98f631 | 1331 | .growfs = false, |
a8c47660 | 1332 | .architecture = _ARCHITECTURE_INVALID, |
1a81ddef | 1333 | .node = TAKE_PTR(n), |
a8c47660 | 1334 | .uuid = id, |
18d73705 | 1335 | .mount_options = TAKE_PTR(o), |
f7725647 | 1336 | .mount_node_fd = TAKE_FD(mount_node_fd), |
88b3300f LP |
1337 | .offset = (uint64_t) start * 512, |
1338 | .size = (uint64_t) size * 512, | |
a8c47660 LP |
1339 | }; |
1340 | ||
1341 | break; | |
1342 | }} | |
8c1be37e LP |
1343 | } |
1344 | } | |
1345 | ||
22e932f4 DDM |
1346 | if (!m->partitions[PARTITION_ROOT].found && |
1347 | (m->partitions[PARTITION_ROOT_VERITY].found || | |
1348 | m->partitions[PARTITION_ROOT_VERITY_SIG].found)) | |
1349 | return -EADDRNOTAVAIL; /* Verity found but no matching rootfs? Something is off, refuse. */ | |
49ae9d91 | 1350 | |
8ee9615e LP |
1351 | /* Hmm, we found a signature partition but no Verity data? Something is off. */ |
1352 | if (m->partitions[PARTITION_ROOT_VERITY_SIG].found && !m->partitions[PARTITION_ROOT_VERITY].found) | |
1353 | return -EADDRNOTAVAIL; | |
7cf66030 | 1354 | |
22e932f4 DDM |
1355 | if (!m->partitions[PARTITION_USR].found && |
1356 | (m->partitions[PARTITION_USR_VERITY].found || | |
1357 | m->partitions[PARTITION_USR_VERITY_SIG].found)) | |
1358 | return -EADDRNOTAVAIL; /* as above */ | |
7cf66030 | 1359 | |
22e932f4 | 1360 | /* as above */ |
8ee9615e LP |
1361 | if (m->partitions[PARTITION_USR_VERITY_SIG].found && !m->partitions[PARTITION_USR_VERITY].found) |
1362 | return -EADDRNOTAVAIL; | |
1363 | ||
cb241a69 LP |
1364 | /* If root and /usr are combined then insist that the architecture matches */ |
1365 | if (m->partitions[PARTITION_ROOT].found && | |
1366 | m->partitions[PARTITION_USR].found && | |
1367 | (m->partitions[PARTITION_ROOT].architecture >= 0 && | |
1368 | m->partitions[PARTITION_USR].architecture >= 0 && | |
1369 | m->partitions[PARTITION_ROOT].architecture != m->partitions[PARTITION_USR].architecture)) | |
1370 | return -EADDRNOTAVAIL; | |
1371 | ||
4ab51780 LP |
1372 | if (!m->partitions[PARTITION_ROOT].found && |
1373 | !m->partitions[PARTITION_USR].found && | |
1374 | (flags & DISSECT_IMAGE_GENERIC_ROOT) && | |
00a8b34f | 1375 | (!verity || !verity->root_hash || verity->designator != PARTITION_USR)) { |
7cf66030 | 1376 | |
1b010ae7 | 1377 | /* OK, we found nothing usable, then check if there's a single generic partition, and use |
4b5de5dd LP |
1378 | * that. If the root hash was set however, then we won't fall back to a generic node, because |
1379 | * the root hash decides. */ | |
7cf66030 LP |
1380 | |
1381 | /* If we didn't find a properly marked root partition, but we did find a single suitable | |
1382 | * generic Linux partition, then use this as root partition, if the caller asked for it. */ | |
1383 | if (multiple_generic) | |
1384 | return -ENOTUNIQ; | |
1385 | ||
4b5de5dd LP |
1386 | /* If we didn't find a generic node, then we can't fix this up either */ |
1387 | if (generic_node) { | |
cd22d856 | 1388 | r = image_policy_may_use(policy, PARTITION_ROOT); |
1a81ddef LP |
1389 | if (r < 0) |
1390 | return r; | |
cd22d856 LP |
1391 | if (r == 0) |
1392 | /* Policy says: ignore; remember that we did */ | |
1393 | m->partitions[PARTITION_ROOT].ignored = true; | |
1394 | else { | |
1395 | _cleanup_close_ int mount_node_fd = -EBADF; | |
1396 | _cleanup_free_ char *o = NULL, *n = NULL; | |
1397 | const char *options; | |
1a81ddef | 1398 | |
cd22d856 LP |
1399 | if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) { |
1400 | mount_node_fd = open_partition(generic_node, /* is_partition = */ true, m->loop); | |
1401 | if (mount_node_fd < 0) | |
1402 | return mount_node_fd; | |
1403 | } | |
18d73705 | 1404 | |
cd22d856 LP |
1405 | r = make_partition_devname(devname, diskseq, generic_nr, flags, &n); |
1406 | if (r < 0) | |
1407 | return r; | |
1408 | ||
1409 | options = mount_options_from_designator(mount_options, PARTITION_ROOT); | |
1410 | if (options) { | |
1411 | o = strdup(options); | |
1412 | if (!o) | |
1413 | return -ENOMEM; | |
1414 | } | |
1415 | ||
1416 | assert(generic_nr >= 0); | |
1417 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { | |
1418 | .found = true, | |
1419 | .rw = generic_rw, | |
1420 | .growfs = generic_growfs, | |
1421 | .partno = generic_nr, | |
1422 | .architecture = _ARCHITECTURE_INVALID, | |
1423 | .node = TAKE_PTR(n), | |
1424 | .uuid = generic_uuid, | |
1425 | .mount_options = TAKE_PTR(o), | |
1426 | .mount_node_fd = TAKE_FD(mount_node_fd), | |
1427 | .offset = UINT64_MAX, | |
1428 | .size = UINT64_MAX, | |
1429 | }; | |
1430 | } | |
e0f9e7bd | 1431 | } |
8c1be37e LP |
1432 | } |
1433 | ||
4b5de5dd LP |
1434 | /* 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 */ |
1435 | if (FLAGS_SET(flags, DISSECT_IMAGE_REQUIRE_ROOT) && | |
1436 | !(m->partitions[PARTITION_ROOT].found || (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT)))) | |
1437 | return -ENXIO; | |
1438 | ||
7b32164f LP |
1439 | if (m->partitions[PARTITION_ROOT_VERITY].found) { |
1440 | /* We only support one verity partition per image, i.e. can't do for both /usr and root fs */ | |
1441 | if (m->partitions[PARTITION_USR_VERITY].found) | |
1442 | return -ENOTUNIQ; | |
1443 | ||
1444 | /* We don't support verity enabled root with a split out /usr. Neither with nor without | |
1445 | * verity there. (Note that we do support verity-less root with verity-full /usr, though.) */ | |
1446 | if (m->partitions[PARTITION_USR].found) | |
1447 | return -EADDRNOTAVAIL; | |
1448 | } | |
aee36b4e | 1449 | |
1903defc LP |
1450 | if (verity) { |
1451 | /* If a verity designator is specified, then insist that the matching partition exists */ | |
1452 | if (verity->designator >= 0 && !m->partitions[verity->designator].found) | |
1453 | return -EADDRNOTAVAIL; | |
aee36b4e | 1454 | |
3dd73ea7 DDM |
1455 | bool have_verity_sig_partition = |
1456 | m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR_VERITY_SIG : PARTITION_ROOT_VERITY_SIG].found; | |
1457 | ||
1903defc | 1458 | if (verity->root_hash) { |
8ee9615e LP |
1459 | /* If we have an explicit root hash and found the partitions for it, then we are ready to use |
1460 | * Verity, set things up for it */ | |
1461 | ||
1903defc LP |
1462 | if (verity->designator < 0 || verity->designator == PARTITION_ROOT) { |
1463 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) | |
1464 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1465 | |
1903defc LP |
1466 | /* If we found a verity setup, then the root partition is necessarily read-only. */ |
1467 | m->partitions[PARTITION_ROOT].rw = false; | |
1468 | m->verity_ready = true; | |
1903defc | 1469 | |
f9e0bb61 LP |
1470 | } else { |
1471 | assert(verity->designator == PARTITION_USR); | |
1472 | ||
1903defc LP |
1473 | if (!m->partitions[PARTITION_USR_VERITY].found || !m->partitions[PARTITION_USR].found) |
1474 | return -EADDRNOTAVAIL; | |
4623e8e6 | 1475 | |
1903defc LP |
1476 | m->partitions[PARTITION_USR].rw = false; |
1477 | m->verity_ready = true; | |
1478 | } | |
8ee9615e LP |
1479 | |
1480 | if (m->verity_ready) | |
3dd73ea7 | 1481 | m->verity_sig_ready = verity->root_hash_sig || have_verity_sig_partition; |
8ee9615e | 1482 | |
3dd73ea7 | 1483 | } else if (have_verity_sig_partition) { |
8ee9615e LP |
1484 | |
1485 | /* If we found an embedded signature partition, we are ready, too. */ | |
1486 | ||
1487 | m->verity_ready = m->verity_sig_ready = true; | |
1488 | m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR : PARTITION_ROOT].rw = false; | |
aee36b4e | 1489 | } |
4623e8e6 LP |
1490 | } |
1491 | ||
598fd4da LP |
1492 | bool any = false; |
1493 | ||
cd22d856 LP |
1494 | /* After we discovered all partitions let's see if the verity requirements match the policy. (Note: |
1495 | * we don't check encryption requirements here, because we haven't probed the file system yet, hence | |
1496 | * don't know if this is encrypted or not) */ | |
1497 | for (PartitionDesignator di = 0; di < _PARTITION_DESIGNATOR_MAX; di++) { | |
1498 | PartitionDesignator vi, si; | |
1499 | PartitionPolicyFlags found_flags; | |
1500 | ||
598fd4da LP |
1501 | any = any || m->partitions[di].found; |
1502 | ||
cd22d856 LP |
1503 | vi = partition_verity_of(di); |
1504 | si = partition_verity_sig_of(di); | |
1505 | ||
1506 | /* Determine the verity protection level for this partition. */ | |
1507 | found_flags = m->partitions[di].found ? | |
1508 | (vi >= 0 && m->partitions[vi].found ? | |
1509 | (si >= 0 && m->partitions[si].found ? PARTITION_POLICY_SIGNED : PARTITION_POLICY_VERITY) : | |
1510 | PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_UNPROTECTED) : | |
1511 | (m->partitions[di].ignored ? PARTITION_POLICY_UNUSED : PARTITION_POLICY_ABSENT); | |
1512 | ||
1513 | r = image_policy_check_protection(policy, di, found_flags); | |
1514 | if (r < 0) | |
1515 | return r; | |
1516 | ||
1517 | if (m->partitions[di].found) { | |
1518 | r = image_policy_check_partition_flags(policy, di, m->partitions[di].gpt_flags); | |
1519 | if (r < 0) | |
1520 | return r; | |
1521 | } | |
1522 | } | |
1523 | ||
598fd4da LP |
1524 | if (!any && !FLAGS_SET(flags, DISSECT_IMAGE_ALLOW_EMPTY)) |
1525 | return -ENOMSG; | |
1526 | ||
cd22d856 | 1527 | r = dissected_image_probe_filesystems(m, fd, policy); |
c80c9079 LP |
1528 | if (r < 0) |
1529 | return r; | |
1530 | ||
08f14be4 YW |
1531 | return 0; |
1532 | } | |
1533 | #endif | |
18b5886e | 1534 | |
08f14be4 YW |
1535 | int dissect_image_file( |
1536 | const char *path, | |
1537 | const VeritySettings *verity, | |
1538 | const MountOptions *mount_options, | |
84be0c71 | 1539 | const ImagePolicy *image_policy, |
08f14be4 YW |
1540 | DissectImageFlags flags, |
1541 | DissectedImage **ret) { | |
8c1be37e | 1542 | |
08f14be4 YW |
1543 | #if HAVE_BLKID |
1544 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
254d1313 | 1545 | _cleanup_close_ int fd = -EBADF; |
08f14be4 | 1546 | int r; |
8c1be37e | 1547 | |
08f14be4 | 1548 | assert(path); |
8c1be37e | 1549 | |
08f14be4 YW |
1550 | fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
1551 | if (fd < 0) | |
1552 | return -errno; | |
896f937f | 1553 | |
08f14be4 YW |
1554 | r = fd_verify_regular(fd); |
1555 | if (r < 0) | |
1556 | return r; | |
de98f631 | 1557 | |
08f14be4 YW |
1558 | r = dissected_image_new(path, &m); |
1559 | if (r < 0) | |
1560 | return r; | |
1561 | ||
22ee78a8 LP |
1562 | r = probe_sector_size(fd, &m->sector_size); |
1563 | if (r < 0) | |
1564 | return r; | |
1565 | ||
84be0c71 | 1566 | r = dissect_image(m, fd, path, verity, mount_options, image_policy, flags); |
698bb074 YW |
1567 | if (r < 0) |
1568 | return r; | |
8c1be37e | 1569 | |
93a8a85b LP |
1570 | if (ret) |
1571 | *ret = TAKE_PTR(m); | |
8c1be37e LP |
1572 | return 0; |
1573 | #else | |
1574 | return -EOPNOTSUPP; | |
1575 | #endif | |
1576 | } | |
1577 | ||
4953e39c ZJS |
1578 | int dissect_log_error(int log_level, int r, const char *name, const VeritySettings *verity) { |
1579 | assert(log_level >= 0 && log_level <= LOG_DEBUG); | |
7cd7a195 LP |
1580 | assert(name); |
1581 | ||
1582 | switch (r) { | |
1583 | ||
1584 | case 0 ... INT_MAX: /* success! */ | |
1585 | return r; | |
1586 | ||
1587 | case -EOPNOTSUPP: | |
1588 | return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support."); | |
1589 | ||
1590 | case -ENOPKG: | |
1591 | return log_error_errno(r, "%s: Couldn't identify a suitable partition table or file system.", name); | |
1592 | ||
1593 | case -ENOMEDIUM: | |
1594 | return log_error_errno(r, "%s: The image does not pass os-release/extension-release validation.", name); | |
1595 | ||
1596 | case -EADDRNOTAVAIL: | |
1597 | return log_error_errno(r, "%s: No root partition for specified root hash found.", name); | |
1598 | ||
1599 | case -ENOTUNIQ: | |
1600 | return log_error_errno(r, "%s: Multiple suitable root partitions found in image.", name); | |
1601 | ||
1602 | case -ENXIO: | |
1603 | return log_error_errno(r, "%s: No suitable root partition found in image.", name); | |
1604 | ||
1605 | case -EPROTONOSUPPORT: | |
1606 | return log_error_errno(r, "Device '%s' is a loopback block device with partition scanning turned off, please turn it on.", name); | |
1607 | ||
1608 | case -ENOTBLK: | |
1609 | return log_error_errno(r, "%s: Image is not a block device.", name); | |
1610 | ||
1611 | case -EBADR: | |
1612 | return log_error_errno(r, | |
1613 | "Combining partitioned images (such as '%s') with external Verity data (such as '%s') not supported. " | |
1614 | "(Consider setting $SYSTEMD_DISSECT_VERITY_SIDECAR=0 to disable automatic discovery of external Verity data.)", | |
1615 | name, strna(verity ? verity->data_path : NULL)); | |
1616 | ||
1617 | case -ERFKILL: | |
1618 | return log_error_errno(r, "%s: image does not match image policy.", name); | |
1619 | ||
598fd4da LP |
1620 | case -ENOMSG: |
1621 | return log_error_errno(r, "%s: no suitable partitions found.", name); | |
1622 | ||
7cd7a195 | 1623 | default: |
4953e39c | 1624 | return log_error_errno(r, "%s: cannot dissect image: %m", name); |
7cd7a195 LP |
1625 | } |
1626 | } | |
1627 | ||
1628 | int dissect_image_file_and_warn( | |
1629 | const char *path, | |
1630 | const VeritySettings *verity, | |
1631 | const MountOptions *mount_options, | |
1632 | const ImagePolicy *image_policy, | |
1633 | DissectImageFlags flags, | |
1634 | DissectedImage **ret) { | |
1635 | ||
1636 | return dissect_log_error( | |
4953e39c | 1637 | LOG_ERR, |
7cd7a195 LP |
1638 | dissect_image_file(path, verity, mount_options, image_policy, flags, ret), |
1639 | path, | |
1640 | verity); | |
1641 | } | |
1642 | ||
8c1be37e | 1643 | DissectedImage* dissected_image_unref(DissectedImage *m) { |
8c1be37e LP |
1644 | if (!m) |
1645 | return NULL; | |
1646 | ||
ac1e1b5f | 1647 | /* First, clear dissected partitions. */ |
08fe0a53 | 1648 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) |
234c2e16 | 1649 | dissected_partition_done(m->partitions + i); |
8c1be37e | 1650 | |
ac1e1b5f YW |
1651 | /* Second, free decrypted images. This must be after dissected_partition_done(), as freeing |
1652 | * DecryptedImage may try to deactivate partitions. */ | |
1653 | decrypted_image_unref(m->decrypted_image); | |
1654 | ||
1e63dc4f YW |
1655 | /* Third, unref LoopDevice. This must be called after the above two, as freeing LoopDevice may try to |
1656 | * remove existing partitions on the loopback block device. */ | |
1657 | loop_device_unref(m->loop); | |
1658 | ||
593fe6c0 | 1659 | free(m->image_name); |
3b925504 LP |
1660 | free(m->hostname); |
1661 | strv_free(m->machine_info); | |
1662 | strv_free(m->os_release); | |
fab22946 | 1663 | strv_free(m->initrd_release); |
7718ac97 | 1664 | strv_free(m->extension_release); |
3b925504 | 1665 | |
5fecf46d | 1666 | return mfree(m); |
8c1be37e LP |
1667 | } |
1668 | ||
18b5886e | 1669 | static int is_loop_device(const char *path) { |
553e15f2 | 1670 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
1671 | struct stat st; |
1672 | ||
1673 | assert(path); | |
1674 | ||
1675 | if (stat(path, &st) < 0) | |
1676 | return -errno; | |
1677 | ||
1678 | if (!S_ISBLK(st.st_mode)) | |
1679 | return -ENOTBLK; | |
1680 | ||
553e15f2 | 1681 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
1682 | if (access(s, F_OK) < 0) { |
1683 | if (errno != ENOENT) | |
1684 | return -errno; | |
1685 | ||
1686 | /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */ | |
553e15f2 | 1687 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
1688 | if (access(s, F_OK) < 0) |
1689 | return errno == ENOENT ? false : -errno; | |
1690 | } | |
1691 | ||
1692 | return true; | |
1693 | } | |
1694 | ||
f8ab7812 | 1695 | static int run_fsck(int node_fd, const char *fstype) { |
cf32c486 LP |
1696 | int r, exit_status; |
1697 | pid_t pid; | |
1698 | ||
f8ab7812 | 1699 | assert(node_fd >= 0); |
cf32c486 LP |
1700 | assert(fstype); |
1701 | ||
13556724 | 1702 | r = fsck_exists_for_fstype(fstype); |
cf32c486 LP |
1703 | if (r < 0) { |
1704 | log_debug_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", fstype); | |
1705 | return 0; | |
1706 | } | |
1707 | if (r == 0) { | |
f8ab7812 | 1708 | log_debug("Not checking partition %s, as fsck for %s does not exist.", FORMAT_PROC_FD_PATH(node_fd), fstype); |
cf32c486 LP |
1709 | return 0; |
1710 | } | |
1711 | ||
f8ab7812 LP |
1712 | r = safe_fork_full( |
1713 | "(fsck)", | |
911f8f01 | 1714 | NULL, |
f8ab7812 | 1715 | &node_fd, 1, /* Leave the node fd open */ |
911f8f01 | 1716 | FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_REARRANGE_STDIO|FORK_CLOEXEC_OFF, |
f8ab7812 | 1717 | &pid); |
cf32c486 LP |
1718 | if (r < 0) |
1719 | return log_debug_errno(r, "Failed to fork off fsck: %m"); | |
1720 | if (r == 0) { | |
1721 | /* Child */ | |
360c9cdc | 1722 | execlp("fsck", "fsck", "-aT", FORMAT_PROC_FD_PATH(node_fd), NULL); |
7e0ed2e9 | 1723 | log_open(); |
cf32c486 LP |
1724 | log_debug_errno(errno, "Failed to execl() fsck: %m"); |
1725 | _exit(FSCK_OPERATIONAL_ERROR); | |
1726 | } | |
1727 | ||
1728 | exit_status = wait_for_terminate_and_check("fsck", pid, 0); | |
1729 | if (exit_status < 0) | |
360c9cdc | 1730 | return log_debug_errno(exit_status, "Failed to fork off fsck: %m"); |
cf32c486 LP |
1731 | |
1732 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { | |
1733 | log_debug("fsck failed with exit status %i.", exit_status); | |
1734 | ||
1735 | if ((exit_status & (FSCK_SYSTEM_SHOULD_REBOOT|FSCK_ERRORS_LEFT_UNCORRECTED)) != 0) | |
1736 | return log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN), "File system is corrupted, refusing."); | |
1737 | ||
1738 | log_debug("Ignoring fsck error."); | |
1739 | } | |
1740 | ||
1741 | return 0; | |
1742 | } | |
1743 | ||
81939d9d | 1744 | static int fs_grow(const char *node_path, const char *mount_path) { |
254d1313 | 1745 | _cleanup_close_ int mount_fd = -EBADF, node_fd = -EBADF; |
81939d9d LP |
1746 | uint64_t size, newsize; |
1747 | int r; | |
1748 | ||
1749 | node_fd = open(node_path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
1750 | if (node_fd < 0) | |
1751 | return log_debug_errno(errno, "Failed to open node device %s: %m", node_path); | |
1752 | ||
1753 | if (ioctl(node_fd, BLKGETSIZE64, &size) != 0) | |
1754 | return log_debug_errno(errno, "Failed to get block device size of %s: %m", node_path); | |
1755 | ||
1756 | mount_fd = open(mount_path, O_RDONLY|O_DIRECTORY|O_CLOEXEC); | |
1757 | if (mount_fd < 0) | |
1758 | return log_debug_errno(errno, "Failed to open mountd file system %s: %m", mount_path); | |
1759 | ||
1760 | log_debug("Resizing \"%s\" to %"PRIu64" bytes...", mount_path, size); | |
1761 | r = resize_fs(mount_fd, size, &newsize); | |
1762 | if (r < 0) | |
1763 | return log_debug_errno(r, "Failed to resize \"%s\" to %"PRIu64" bytes: %m", mount_path, size); | |
1764 | ||
1765 | if (newsize == size) | |
1766 | log_debug("Successfully resized \"%s\" to %s bytes.", | |
2b59bf51 | 1767 | mount_path, FORMAT_BYTES(newsize)); |
81939d9d LP |
1768 | else { |
1769 | assert(newsize < size); | |
1770 | log_debug("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).", | |
2b59bf51 | 1771 | mount_path, FORMAT_BYTES(newsize), size - newsize); |
81939d9d LP |
1772 | } |
1773 | ||
1774 | return 0; | |
1775 | } | |
1776 | ||
254e392e LP |
1777 | int partition_pick_mount_options( |
1778 | PartitionDesignator d, | |
1779 | const char *fstype, | |
1780 | bool rw, | |
1781 | bool discard, | |
1782 | char **ret_options, | |
1783 | unsigned long *ret_ms_flags) { | |
1784 | ||
1785 | _cleanup_free_ char *options = NULL; | |
1786 | ||
1787 | assert(ret_options); | |
1788 | ||
1789 | /* Selects a baseline of bind mount flags, that should always apply. | |
1790 | * | |
1791 | * Firstly, we set MS_NODEV universally on all mounts, since we don't want to allow device nodes outside of /dev/. | |
1792 | * | |
1793 | * On /var/tmp/ we'll also set MS_NOSUID, same as we set for /tmp/ on the host. | |
1794 | * | |
1795 | * On the ESP and XBOOTLDR partitions we'll also disable symlinks, and execution. These file systems | |
1796 | * are generally untrusted (i.e. not encrypted or authenticated), and typically VFAT hence we should | |
1797 | * be as restrictive as possible, and this shouldn't hurt, since the functionality is not available | |
1798 | * there anyway. */ | |
1799 | ||
1800 | unsigned long flags = MS_NODEV; | |
1801 | ||
1802 | if (!rw) | |
1803 | flags |= MS_RDONLY; | |
1804 | ||
1805 | switch (d) { | |
1806 | ||
1807 | case PARTITION_ESP: | |
1808 | case PARTITION_XBOOTLDR: | |
1809 | flags |= MS_NOSUID|MS_NOEXEC|ms_nosymfollow_supported(); | |
1810 | ||
6eda6f7e LP |
1811 | /* The ESP might contain a pre-boot random seed. Let's make this unaccessible to regular |
1812 | * userspace. ESP/XBOOTLDR is almost certainly VFAT, hence if we don't know assume it is. */ | |
1813 | if (!fstype || fstype_can_umask(fstype)) | |
254e392e LP |
1814 | if (!strextend_with_separator(&options, ",", "umask=0077")) |
1815 | return -ENOMEM; | |
1816 | break; | |
1817 | ||
1818 | case PARTITION_TMP: | |
1819 | flags |= MS_NOSUID; | |
1820 | break; | |
1821 | ||
1822 | default: | |
1823 | break; | |
1824 | } | |
1825 | ||
1826 | /* So, when you request MS_RDONLY from ext4, then this means nothing. It happily still writes to the | |
1827 | * backing storage. What's worse, the BLKRO[GS]ET flag and (in case of loopback devices) | |
1828 | * LO_FLAGS_READ_ONLY don't mean anything, they affect userspace accesses only, and write accesses | |
1829 | * from the upper file system still get propagated through to the underlying file system, | |
1830 | * unrestricted. To actually get ext4/xfs/btrfs to stop writing to the device we need to specify | |
1831 | * "norecovery" as mount option, in addition to MS_RDONLY. Yes, this sucks, since it means we need to | |
1832 | * carry a per file system table here. | |
1833 | * | |
1834 | * Note that this means that we might not be able to mount corrupted file systems as read-only | |
1835 | * anymore (since in some cases the kernel implementations will refuse mounting when corrupted, | |
1836 | * read-only and "norecovery" is specified). But I think for the case of automatically determined | |
1837 | * mount options for loopback devices this is the right choice, since otherwise using the same | |
1838 | * loopback file twice even in read-only mode, is going to fail badly sooner or later. The usecase of | |
1839 | * making reuse of the immutable images "just work" is more relevant to us than having read-only | |
1840 | * access that actually modifies stuff work on such image files. Or to say this differently: if | |
1841 | * people want their file systems to be fixed up they should just open them in writable mode, where | |
1842 | * all these problems don't exist. */ | |
034ebc47 | 1843 | if (!rw && fstype && fstype_can_norecovery(fstype)) |
254e392e LP |
1844 | if (!strextend_with_separator(&options, ",", "norecovery")) |
1845 | return -ENOMEM; | |
1846 | ||
1847 | if (discard && fstype && fstype_can_discard(fstype)) | |
1848 | if (!strextend_with_separator(&options, ",", "discard")) | |
1849 | return -ENOMEM; | |
1850 | ||
1851 | if (!ret_ms_flags) /* Fold flags into option string if ret_flags specified as NULL */ | |
1852 | if (!strextend_with_separator(&options, ",", | |
1853 | FLAGS_SET(flags, MS_RDONLY) ? "ro" : "rw", | |
1854 | FLAGS_SET(flags, MS_NODEV) ? "nodev" : "dev", | |
1855 | FLAGS_SET(flags, MS_NOSUID) ? "nosuid" : "suid", | |
1856 | FLAGS_SET(flags, MS_NOEXEC) ? "noexec" : "exec", | |
1857 | FLAGS_SET(flags, MS_NOSYMFOLLOW) ? "nosymfollow" : NULL)) | |
1858 | /* NB: we suppress 'symfollow' here, since it's the default, and old /bin/mount might not know it */ | |
1859 | return -ENOMEM; | |
1860 | ||
1861 | if (ret_ms_flags) | |
1862 | *ret_ms_flags = flags; | |
1863 | ||
1864 | *ret_options = TAKE_PTR(options); | |
1865 | return 0; | |
1866 | } | |
1867 | ||
18b5886e | 1868 | static int mount_partition( |
254e392e | 1869 | PartitionDesignator d, |
18b5886e LP |
1870 | DissectedPartition *m, |
1871 | const char *where, | |
1872 | const char *directory, | |
2d3a5a73 | 1873 | uid_t uid_shift, |
21b61b1d | 1874 | uid_t uid_range, |
18b5886e LP |
1875 | DissectImageFlags flags) { |
1876 | ||
2d3a5a73 | 1877 | _cleanup_free_ char *chased = NULL, *options = NULL; |
254e392e | 1878 | bool rw, discard, remap_uid_gid = false; |
2d3a5a73 | 1879 | const char *p, *node, *fstype; |
254e392e | 1880 | unsigned long ms_flags; |
2eedfd2d | 1881 | int r; |
8c1be37e LP |
1882 | |
1883 | assert(m); | |
1884 | assert(where); | |
1885 | ||
f7725647 YW |
1886 | if (m->mount_node_fd < 0) |
1887 | return 0; | |
1888 | ||
4dc28665 | 1889 | /* Use decrypted node and matching fstype if available, otherwise use the original device */ |
f7725647 | 1890 | node = FORMAT_PROC_FD_PATH(m->mount_node_fd); |
c571dee7 | 1891 | fstype = dissected_partition_fstype(m); |
18b5886e | 1892 | |
4dc28665 LP |
1893 | if (!fstype) |
1894 | return -EAFNOSUPPORT; | |
80ce8580 LP |
1895 | r = dissect_fstype_ok(fstype); |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | if (!r) | |
1899 | return -EIDRM; /* Recognizable error */ | |
8c1be37e | 1900 | |
68ac5118 ZJS |
1901 | /* We are looking at an encrypted partition? This either means stacked encryption, or the caller |
1902 | * didn't call dissected_image_decrypt() beforehand. Let's return a recognizable error for this | |
1903 | * case. */ | |
4dc28665 | 1904 | if (streq(fstype, "crypto_LUKS")) |
fa45d12c | 1905 | return -EUNATCH; |
18b5886e | 1906 | |
ef9c184d | 1907 | rw = m->rw && !(flags & DISSECT_IMAGE_MOUNT_READ_ONLY); |
8c1be37e | 1908 | |
254e392e LP |
1909 | discard = ((flags & DISSECT_IMAGE_DISCARD) || |
1910 | ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node) > 0)); | |
1911 | ||
cf32c486 | 1912 | if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) { |
f8ab7812 | 1913 | r = run_fsck(m->mount_node_fd, fstype); |
cf32c486 LP |
1914 | if (r < 0) |
1915 | return r; | |
1916 | } | |
1917 | ||
2eedfd2d | 1918 | if (directory) { |
334eb5b0 LP |
1919 | /* Automatically create missing mount points inside the image, if necessary. */ |
1920 | r = mkdir_p_root(where, directory, uid_shift, (gid_t) uid_shift, 0755); | |
1921 | if (r < 0 && r != -EROFS) | |
1922 | return r; | |
1f0f82f1 | 1923 | |
f461a28d | 1924 | r = chase(directory, where, CHASE_PREFIX_ROOT, &chased, NULL); |
2eedfd2d LP |
1925 | if (r < 0) |
1926 | return r; | |
1927 | ||
1928 | p = chased; | |
9842905e LP |
1929 | } else { |
1930 | /* Create top-level mount if missing – but only if this is asked for. This won't modify the | |
1931 | * image (as the branch above does) but the host hierarchy, and the created directory might | |
1932 | * survive our mount in the host hierarchy hence. */ | |
1933 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { | |
1934 | r = mkdir_p(where, 0755); | |
1935 | if (r < 0) | |
1936 | return r; | |
1937 | } | |
1938 | ||
8c1be37e | 1939 | p = where; |
9842905e | 1940 | } |
8c1be37e | 1941 | |
254e392e LP |
1942 | r = partition_pick_mount_options(d, dissected_partition_fstype(m), rw, discard, &options, &ms_flags); |
1943 | if (r < 0) | |
1944 | return r; | |
2d3a5a73 | 1945 | |
21b61b1d | 1946 | if (uid_is_valid(uid_shift) && uid_shift != 0) { |
2d3a5a73 | 1947 | |
21b61b1d LP |
1948 | if (fstype_can_uid_gid(fstype)) { |
1949 | _cleanup_free_ char *uid_option = NULL; | |
2d3a5a73 | 1950 | |
21b61b1d LP |
1951 | if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) |
1952 | return -ENOMEM; | |
1953 | ||
1954 | if (!strextend_with_separator(&options, ",", uid_option)) | |
1955 | return -ENOMEM; | |
1956 | } else if (FLAGS_SET(flags, DISSECT_IMAGE_MOUNT_IDMAPPED)) | |
1957 | remap_uid_gid = true; | |
2d3a5a73 | 1958 | } |
8c1be37e | 1959 | |
18d73705 | 1960 | if (!isempty(m->mount_options)) |
c2bc710b | 1961 | if (!strextend_with_separator(&options, ",", m->mount_options)) |
18d73705 LB |
1962 | return -ENOMEM; |
1963 | ||
254e392e | 1964 | r = mount_nofollow_verbose(LOG_DEBUG, node, p, fstype, ms_flags, options); |
d9223c07 LP |
1965 | if (r < 0) |
1966 | return r; | |
1967 | ||
81939d9d LP |
1968 | if (rw && m->growfs && FLAGS_SET(flags, DISSECT_IMAGE_GROWFS)) |
1969 | (void) fs_grow(node, p); | |
1970 | ||
21b61b1d | 1971 | if (remap_uid_gid) { |
2b2777ed | 1972 | r = remount_idmap(p, uid_shift, uid_range, UID_INVALID, REMOUNT_IDMAPPING_HOST_ROOT); |
21b61b1d LP |
1973 | if (r < 0) |
1974 | return r; | |
1975 | } | |
1976 | ||
d9223c07 | 1977 | return 1; |
8c1be37e LP |
1978 | } |
1979 | ||
7cf66030 LP |
1980 | static int mount_root_tmpfs(const char *where, uid_t uid_shift, DissectImageFlags flags) { |
1981 | _cleanup_free_ char *options = NULL; | |
1982 | int r; | |
1983 | ||
1984 | assert(where); | |
1985 | ||
1986 | /* For images that contain /usr/ but no rootfs, let's mount rootfs as tmpfs */ | |
1987 | ||
1988 | if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { | |
1989 | r = mkdir_p(where, 0755); | |
1990 | if (r < 0) | |
1991 | return r; | |
1992 | } | |
1993 | ||
1994 | if (uid_is_valid(uid_shift)) { | |
1995 | if (asprintf(&options, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0) | |
1996 | return -ENOMEM; | |
1997 | } | |
1998 | ||
1999 | r = mount_nofollow_verbose(LOG_DEBUG, "rootfs", where, "tmpfs", MS_NODEV, options); | |
2000 | if (r < 0) | |
2001 | return r; | |
2002 | ||
2003 | return 1; | |
2004 | } | |
2005 | ||
21b61b1d LP |
2006 | int dissected_image_mount( |
2007 | DissectedImage *m, | |
2008 | const char *where, | |
2009 | uid_t uid_shift, | |
2010 | uid_t uid_range, | |
2011 | DissectImageFlags flags) { | |
2012 | ||
1f0f82f1 | 2013 | int r, xbootldr_mounted; |
8c1be37e LP |
2014 | |
2015 | assert(m); | |
2016 | assert(where); | |
2017 | ||
fa45d12c LP |
2018 | /* Returns: |
2019 | * | |
2020 | * -ENXIO → No root partition found | |
7718ac97 | 2021 | * -EMEDIUMTYPE → DISSECT_IMAGE_VALIDATE_OS set but no os-release/extension-release file found |
fa45d12c LP |
2022 | * -EUNATCH → Encrypted partition found for which no dm-crypt was set up yet |
2023 | * -EUCLEAN → fsck for file system failed | |
2024 | * -EBUSY → File system already mounted/used elsewhere (kernel) | |
4dc28665 | 2025 | * -EAFNOSUPPORT → File system type not supported or not known |
80ce8580 | 2026 | * -EIDRM → File system is not among allowlisted "common" file systems |
fa45d12c LP |
2027 | */ |
2028 | ||
7cf66030 LP |
2029 | if (!(m->partitions[PARTITION_ROOT].found || |
2030 | (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT)))) | |
2031 | return -ENXIO; /* Require a root fs or at least a /usr/ fs (the latter is subject to a flag of its own) */ | |
8c1be37e | 2032 | |
2d3a5a73 | 2033 | if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) { |
7cf66030 LP |
2034 | |
2035 | /* First mount the root fs. If there's none we use a tmpfs. */ | |
2036 | if (m->partitions[PARTITION_ROOT].found) | |
254e392e | 2037 | r = mount_partition(PARTITION_ROOT, m->partitions + PARTITION_ROOT, where, NULL, uid_shift, uid_range, flags); |
7cf66030 LP |
2038 | else |
2039 | r = mount_root_tmpfs(where, uid_shift, flags); | |
2d3a5a73 LP |
2040 | if (r < 0) |
2041 | return r; | |
aee36b4e | 2042 | |
aee36b4e | 2043 | /* For us mounting root always means mounting /usr as well */ |
254e392e | 2044 | r = mount_partition(PARTITION_USR, m->partitions + PARTITION_USR, where, "/usr", uid_shift, uid_range, flags); |
aee36b4e LP |
2045 | if (r < 0) |
2046 | return r; | |
03bcb6d4 | 2047 | |
9ccb531a LB |
2048 | if ((flags & (DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_VALIDATE_OS_EXT)) != 0) { |
2049 | /* If either one of the validation flags are set, ensure that the image qualifies | |
2050 | * as one or the other (or both). */ | |
2051 | bool ok = false; | |
2052 | ||
2053 | if (FLAGS_SET(flags, DISSECT_IMAGE_VALIDATE_OS)) { | |
2054 | r = path_is_os_tree(where); | |
2055 | if (r < 0) | |
2056 | return r; | |
2057 | if (r > 0) | |
2058 | ok = true; | |
2059 | } | |
2060 | if (!ok && FLAGS_SET(flags, DISSECT_IMAGE_VALIDATE_OS_EXT)) { | |
6afa5d86 | 2061 | r = extension_has_forbidden_content(where); |
7718ac97 LB |
2062 | if (r < 0) |
2063 | return r; | |
6afa5d86 | 2064 | if (r == 0) { |
b60e0f57 | 2065 | r = path_is_extension_tree(IMAGE_SYSEXT, where, m->image_name, FLAGS_SET(flags, DISSECT_IMAGE_RELAX_SYSEXT_CHECK)); |
6afa5d86 LB |
2066 | if (r < 0) |
2067 | return r; | |
2068 | if (r > 0) | |
2069 | ok = true; | |
2070 | } | |
7718ac97 | 2071 | } |
9ccb531a LB |
2072 | |
2073 | if (!ok) | |
2074 | return -ENOMEDIUM; | |
03bcb6d4 | 2075 | } |
2d3a5a73 LP |
2076 | } |
2077 | ||
705727fd | 2078 | if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY) |
2d3a5a73 | 2079 | return 0; |
8c1be37e | 2080 | |
254e392e | 2081 | r = mount_partition(PARTITION_HOME, m->partitions + PARTITION_HOME, where, "/home", uid_shift, uid_range, flags); |
8c1be37e LP |
2082 | if (r < 0) |
2083 | return r; | |
2084 | ||
254e392e | 2085 | r = mount_partition(PARTITION_SRV, m->partitions + PARTITION_SRV, where, "/srv", uid_shift, uid_range, flags); |
8c1be37e LP |
2086 | if (r < 0) |
2087 | return r; | |
2088 | ||
254e392e | 2089 | r = mount_partition(PARTITION_VAR, m->partitions + PARTITION_VAR, where, "/var", uid_shift, uid_range, flags); |
d4dffb85 LP |
2090 | if (r < 0) |
2091 | return r; | |
2092 | ||
254e392e | 2093 | r = mount_partition(PARTITION_TMP, m->partitions + PARTITION_TMP, where, "/var/tmp", uid_shift, uid_range, flags); |
d4dffb85 LP |
2094 | if (r < 0) |
2095 | return r; | |
2096 | ||
254e392e | 2097 | xbootldr_mounted = mount_partition(PARTITION_XBOOTLDR, m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, uid_range, flags); |
1f0f82f1 LP |
2098 | if (xbootldr_mounted < 0) |
2099 | return xbootldr_mounted; | |
d9223c07 | 2100 | |
8c1be37e | 2101 | if (m->partitions[PARTITION_ESP].found) { |
1f0f82f1 LP |
2102 | int esp_done = false; |
2103 | ||
d9223c07 LP |
2104 | /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it |
2105 | * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */ | |
8c1be37e | 2106 | |
f461a28d | 2107 | r = chase("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL); |
1f0f82f1 LP |
2108 | if (r < 0) { |
2109 | if (r != -ENOENT) | |
d9223c07 | 2110 | return r; |
8c1be37e | 2111 | |
1f0f82f1 LP |
2112 | /* /efi doesn't exist. Let's see if /boot is suitable then */ |
2113 | ||
2114 | if (!xbootldr_mounted) { | |
2115 | _cleanup_free_ char *p = NULL; | |
2eedfd2d | 2116 | |
f461a28d | 2117 | r = chase("/boot", where, CHASE_PREFIX_ROOT, &p, NULL); |
1f0f82f1 LP |
2118 | if (r < 0) { |
2119 | if (r != -ENOENT) | |
2120 | return r; | |
db55bbf2 | 2121 | } else if (dir_is_empty(p, /* ignore_hidden_or_backup= */ false) > 0) { |
1f0f82f1 | 2122 | /* It exists and is an empty directory. Let's mount the ESP there. */ |
254e392e | 2123 | r = mount_partition(PARTITION_ESP, m->partitions + PARTITION_ESP, where, "/boot", uid_shift, uid_range, flags); |
1f0f82f1 LP |
2124 | if (r < 0) |
2125 | return r; | |
2126 | ||
2127 | esp_done = true; | |
2128 | } | |
2eedfd2d | 2129 | } |
8c1be37e | 2130 | } |
1f0f82f1 LP |
2131 | |
2132 | if (!esp_done) { | |
2133 | /* OK, let's mount the ESP now to /efi (possibly creating the dir if missing) */ | |
2134 | ||
254e392e | 2135 | r = mount_partition(PARTITION_ESP, m->partitions + PARTITION_ESP, where, "/efi", uid_shift, uid_range, flags); |
1f0f82f1 LP |
2136 | if (r < 0) |
2137 | return r; | |
2138 | } | |
8c1be37e LP |
2139 | } |
2140 | ||
2141 | return 0; | |
2142 | } | |
2143 | ||
21b61b1d LP |
2144 | int dissected_image_mount_and_warn( |
2145 | DissectedImage *m, | |
2146 | const char *where, | |
2147 | uid_t uid_shift, | |
2148 | uid_t uid_range, | |
2149 | DissectImageFlags flags) { | |
2150 | ||
af187ab2 LP |
2151 | int r; |
2152 | ||
2153 | assert(m); | |
2154 | assert(where); | |
2155 | ||
21b61b1d | 2156 | r = dissected_image_mount(m, where, uid_shift, uid_range, flags); |
af187ab2 LP |
2157 | if (r == -ENXIO) |
2158 | return log_error_errno(r, "Not root file system found in image."); | |
2159 | if (r == -EMEDIUMTYPE) | |
7718ac97 | 2160 | return log_error_errno(r, "No suitable os-release/extension-release file in image found."); |
af187ab2 LP |
2161 | if (r == -EUNATCH) |
2162 | return log_error_errno(r, "Encrypted file system discovered, but decryption not requested."); | |
2163 | if (r == -EUCLEAN) | |
2164 | return log_error_errno(r, "File system check on image failed."); | |
2165 | if (r == -EBUSY) | |
2166 | return log_error_errno(r, "File system already mounted elsewhere."); | |
4dc28665 LP |
2167 | if (r == -EAFNOSUPPORT) |
2168 | return log_error_errno(r, "File system type not supported or not known."); | |
80ce8580 LP |
2169 | if (r == -EIDRM) |
2170 | return log_error_errno(r, "File system is too uncommon, refused."); | |
af187ab2 LP |
2171 | if (r < 0) |
2172 | return log_error_errno(r, "Failed to mount image: %m"); | |
2173 | ||
2174 | return r; | |
2175 | } | |
2176 | ||
349cc4a5 | 2177 | #if HAVE_LIBCRYPTSETUP |
9321ad51 | 2178 | struct DecryptedPartition { |
18b5886e LP |
2179 | struct crypt_device *device; |
2180 | char *name; | |
2181 | bool relinquished; | |
9321ad51 YW |
2182 | }; |
2183 | #endif | |
2184 | ||
2185 | typedef struct DecryptedPartition DecryptedPartition; | |
18b5886e LP |
2186 | |
2187 | struct DecryptedImage { | |
9321ad51 | 2188 | unsigned n_ref; |
18b5886e LP |
2189 | DecryptedPartition *decrypted; |
2190 | size_t n_decrypted; | |
18b5886e | 2191 | }; |
18b5886e | 2192 | |
9321ad51 | 2193 | static DecryptedImage* decrypted_image_free(DecryptedImage *d) { |
349cc4a5 | 2194 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
2195 | int r; |
2196 | ||
2197 | if (!d) | |
2198 | return NULL; | |
2199 | ||
67f63ee5 | 2200 | for (size_t i = 0; i < d->n_decrypted; i++) { |
18b5886e LP |
2201 | DecryptedPartition *p = d->decrypted + i; |
2202 | ||
2203 | if (p->device && p->name && !p->relinquished) { | |
5228c58e DDM |
2204 | _cleanup_free_ char *node = NULL; |
2205 | ||
2206 | node = path_join("/dev/mapper", p->name); | |
2207 | if (node) { | |
2208 | r = btrfs_forget_device(node); | |
2209 | if (r < 0 && r != -ENOENT) | |
2210 | log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", node); | |
2211 | } else | |
2212 | log_oom_debug(); | |
2213 | ||
ea16d7f4 YW |
2214 | /* Let's deactivate lazily, as the dm volume may be already/still used by other processes. */ |
2215 | r = sym_crypt_deactivate_by_name(p->device, p->name, CRYPT_DEACTIVATE_DEFERRED); | |
18b5886e LP |
2216 | if (r < 0) |
2217 | log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name); | |
2218 | } | |
2219 | ||
2220 | if (p->device) | |
0d12936d | 2221 | sym_crypt_free(p->device); |
18b5886e LP |
2222 | free(p->name); |
2223 | } | |
2224 | ||
f91861e4 | 2225 | free(d->decrypted); |
18b5886e LP |
2226 | free(d); |
2227 | #endif | |
2228 | return NULL; | |
2229 | } | |
2230 | ||
9321ad51 YW |
2231 | DEFINE_TRIVIAL_REF_UNREF_FUNC(DecryptedImage, decrypted_image, decrypted_image_free); |
2232 | ||
349cc4a5 | 2233 | #if HAVE_LIBCRYPTSETUP |
9321ad51 YW |
2234 | static int decrypted_image_new(DecryptedImage **ret) { |
2235 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; | |
2236 | ||
2237 | assert(ret); | |
2238 | ||
2239 | d = new(DecryptedImage, 1); | |
2240 | if (!d) | |
2241 | return -ENOMEM; | |
2242 | ||
2243 | *d = (DecryptedImage) { | |
2244 | .n_ref = 1, | |
2245 | }; | |
2246 | ||
2247 | *ret = TAKE_PTR(d); | |
2248 | return 0; | |
2249 | } | |
4623e8e6 LP |
2250 | |
2251 | static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) { | |
2252 | _cleanup_free_ char *name = NULL, *node = NULL; | |
2253 | const char *base; | |
2254 | ||
2255 | assert(original_node); | |
2256 | assert(suffix); | |
2257 | assert(ret_name); | |
2258 | assert(ret_node); | |
2259 | ||
2260 | base = strrchr(original_node, '/'); | |
2261 | if (!base) | |
ac1f3ad0 LB |
2262 | base = original_node; |
2263 | else | |
2264 | base++; | |
4623e8e6 LP |
2265 | if (isempty(base)) |
2266 | return -EINVAL; | |
2267 | ||
2268 | name = strjoin(base, suffix); | |
2269 | if (!name) | |
2270 | return -ENOMEM; | |
2271 | if (!filename_is_valid(name)) | |
2272 | return -EINVAL; | |
2273 | ||
0d12936d | 2274 | node = path_join(sym_crypt_get_dir(), name); |
4623e8e6 LP |
2275 | if (!node) |
2276 | return -ENOMEM; | |
2277 | ||
1cc6c93a YW |
2278 | *ret_name = TAKE_PTR(name); |
2279 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 2280 | |
4623e8e6 LP |
2281 | return 0; |
2282 | } | |
2283 | ||
18b5886e LP |
2284 | static int decrypt_partition( |
2285 | DissectedPartition *m, | |
2286 | const char *passphrase, | |
2287 | DissectImageFlags flags, | |
2288 | DecryptedImage *d) { | |
2289 | ||
2290 | _cleanup_free_ char *node = NULL, *name = NULL; | |
0d12936d | 2291 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
254d1313 | 2292 | _cleanup_close_ int fd = -EBADF; |
18b5886e LP |
2293 | int r; |
2294 | ||
2295 | assert(m); | |
2296 | assert(d); | |
2297 | ||
2298 | if (!m->found || !m->node || !m->fstype) | |
2299 | return 0; | |
2300 | ||
2301 | if (!streq(m->fstype, "crypto_LUKS")) | |
2302 | return 0; | |
2303 | ||
bdd73ac5 ZJS |
2304 | if (!passphrase) |
2305 | return -ENOKEY; | |
2306 | ||
0d12936d LP |
2307 | r = dlopen_cryptsetup(); |
2308 | if (r < 0) | |
2309 | return r; | |
2310 | ||
4623e8e6 LP |
2311 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
2312 | if (r < 0) | |
2313 | return r; | |
18b5886e | 2314 | |
319a4f4b | 2315 | if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1)) |
18b5886e LP |
2316 | return -ENOMEM; |
2317 | ||
0d12936d | 2318 | r = sym_crypt_init(&cd, m->node); |
18b5886e | 2319 | if (r < 0) |
715cbb81 | 2320 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 2321 | |
efc3b12f | 2322 | cryptsetup_enable_logging(cd); |
1887032f | 2323 | |
0d12936d | 2324 | r = sym_crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
2325 | if (r < 0) |
2326 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e | 2327 | |
0d12936d | 2328 | r = sym_crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), |
ef9c184d | 2329 | ((flags & DISSECT_IMAGE_DEVICE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | |
0d12936d | 2330 | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); |
294bd454 | 2331 | if (r < 0) { |
715cbb81 | 2332 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 2333 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 2334 | } |
18b5886e | 2335 | |
f7725647 YW |
2336 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); |
2337 | if (fd < 0) | |
2338 | return log_debug_errno(errno, "Failed to open %s: %m", node); | |
2339 | ||
94344385 LP |
2340 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
2341 | .name = TAKE_PTR(name), | |
2342 | .device = TAKE_PTR(cd), | |
2343 | }; | |
18b5886e | 2344 | |
1cc6c93a | 2345 | m->decrypted_node = TAKE_PTR(node); |
f7725647 | 2346 | close_and_replace(m->mount_node_fd, fd); |
18b5886e LP |
2347 | |
2348 | return 0; | |
4623e8e6 LP |
2349 | } |
2350 | ||
89e62e0b LP |
2351 | static int verity_can_reuse( |
2352 | const VeritySettings *verity, | |
2353 | const char *name, | |
2354 | struct crypt_device **ret_cd) { | |
2355 | ||
ac1f3ad0 LB |
2356 | /* If the same volume was already open, check that the root hashes match, and reuse it if they do */ |
2357 | _cleanup_free_ char *root_hash_existing = NULL; | |
0d12936d | 2358 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 2359 | struct crypt_params_verity crypt_params = {}; |
89e62e0b | 2360 | size_t root_hash_existing_size; |
ac1f3ad0 LB |
2361 | int r; |
2362 | ||
89e62e0b LP |
2363 | assert(verity); |
2364 | assert(name); | |
ac1f3ad0 LB |
2365 | assert(ret_cd); |
2366 | ||
0d12936d | 2367 | r = sym_crypt_init_by_name(&cd, name); |
ac1f3ad0 LB |
2368 | if (r < 0) |
2369 | return log_debug_errno(r, "Error opening verity device, crypt_init_by_name failed: %m"); | |
0d12936d | 2370 | |
c719805e LP |
2371 | cryptsetup_enable_logging(cd); |
2372 | ||
0d12936d | 2373 | r = sym_crypt_get_verity_info(cd, &crypt_params); |
ac1f3ad0 LB |
2374 | if (r < 0) |
2375 | return log_debug_errno(r, "Error opening verity device, crypt_get_verity_info failed: %m"); | |
0d12936d | 2376 | |
89e62e0b LP |
2377 | root_hash_existing_size = verity->root_hash_size; |
2378 | root_hash_existing = malloc0(root_hash_existing_size); | |
ac1f3ad0 LB |
2379 | if (!root_hash_existing) |
2380 | return -ENOMEM; | |
0d12936d LP |
2381 | |
2382 | r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash_existing, &root_hash_existing_size, NULL, 0); | |
ac1f3ad0 LB |
2383 | if (r < 0) |
2384 | return log_debug_errno(r, "Error opening verity device, crypt_volume_key_get failed: %m"); | |
89e62e0b LP |
2385 | if (verity->root_hash_size != root_hash_existing_size || |
2386 | memcmp(root_hash_existing, verity->root_hash, verity->root_hash_size) != 0) | |
ac1f3ad0 | 2387 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but root hashes are different."); |
89e62e0b | 2388 | |
ac1f3ad0 | 2389 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY |
89e62e0b LP |
2390 | /* Ensure that, if signatures are supported, we only reuse the device if the previous mount used the |
2391 | * same settings, so that a previous unsigned mount will not be reused if the user asks to use | |
28423d9a | 2392 | * signing for the new one, and vice versa. */ |
89e62e0b | 2393 | if (!!verity->root_hash_sig != !!(crypt_params.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE)) |
ac1f3ad0 LB |
2394 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Error opening verity device, it already exists but signature settings are not the same."); |
2395 | #endif | |
2396 | ||
2397 | *ret_cd = TAKE_PTR(cd); | |
2398 | return 0; | |
2399 | } | |
2400 | ||
75db809a | 2401 | static inline char* dm_deferred_remove_clean(char *name) { |
ac1f3ad0 | 2402 | if (!name) |
75db809a | 2403 | return NULL; |
0d12936d LP |
2404 | |
2405 | (void) sym_crypt_deactivate_by_name(NULL, name, CRYPT_DEACTIVATE_DEFERRED); | |
75db809a | 2406 | return mfree(name); |
ac1f3ad0 LB |
2407 | } |
2408 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, dm_deferred_remove_clean); | |
2409 | ||
c2fa92e7 LP |
2410 | static int validate_signature_userspace(const VeritySettings *verity) { |
2411 | #if HAVE_OPENSSL | |
2412 | _cleanup_(sk_X509_free_allp) STACK_OF(X509) *sk = NULL; | |
2413 | _cleanup_strv_free_ char **certs = NULL; | |
2414 | _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL; | |
2415 | _cleanup_free_ char *s = NULL; | |
2416 | _cleanup_(BIO_freep) BIO *bio = NULL; /* 'bio' must be freed first, 's' second, hence keep this order | |
2417 | * of declaration in place, please */ | |
2418 | const unsigned char *d; | |
c2fa92e7 LP |
2419 | int r; |
2420 | ||
2421 | assert(verity); | |
2422 | assert(verity->root_hash); | |
2423 | assert(verity->root_hash_sig); | |
2424 | ||
2425 | /* Because installing a signature certificate into the kernel chain is so messy, let's optionally do | |
2426 | * userspace validation. */ | |
2427 | ||
2428 | r = conf_files_list_nulstr(&certs, ".crt", NULL, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, CONF_PATHS_NULSTR("verity.d")); | |
2429 | if (r < 0) | |
2430 | return log_debug_errno(r, "Failed to enumerate certificates: %m"); | |
2431 | if (strv_isempty(certs)) { | |
2432 | log_debug("No userspace dm-verity certificates found."); | |
2433 | return 0; | |
2434 | } | |
2435 | ||
2436 | d = verity->root_hash_sig; | |
2437 | p7 = d2i_PKCS7(NULL, &d, (long) verity->root_hash_sig_size); | |
2438 | if (!p7) | |
2439 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse PKCS7 DER signature data."); | |
2440 | ||
2441 | s = hexmem(verity->root_hash, verity->root_hash_size); | |
2442 | if (!s) | |
2443 | return log_oom_debug(); | |
2444 | ||
2445 | bio = BIO_new_mem_buf(s, strlen(s)); | |
2446 | if (!bio) | |
2447 | return log_oom_debug(); | |
2448 | ||
2449 | sk = sk_X509_new_null(); | |
2450 | if (!sk) | |
2451 | return log_oom_debug(); | |
2452 | ||
2453 | STRV_FOREACH(i, certs) { | |
2454 | _cleanup_(X509_freep) X509 *c = NULL; | |
2455 | _cleanup_fclose_ FILE *f = NULL; | |
2456 | ||
2457 | f = fopen(*i, "re"); | |
2458 | if (!f) { | |
2459 | log_debug_errno(errno, "Failed to open '%s', ignoring: %m", *i); | |
2460 | continue; | |
2461 | } | |
2462 | ||
2463 | c = PEM_read_X509(f, NULL, NULL, NULL); | |
2464 | if (!c) { | |
2465 | log_debug("Failed to load X509 certificate '%s', ignoring.", *i); | |
2466 | continue; | |
2467 | } | |
2468 | ||
2469 | if (sk_X509_push(sk, c) == 0) | |
2470 | return log_oom_debug(); | |
2471 | ||
2472 | TAKE_PTR(c); | |
2473 | } | |
2474 | ||
2475 | r = PKCS7_verify(p7, sk, NULL, bio, NULL, PKCS7_NOINTERN|PKCS7_NOVERIFY); | |
2476 | if (r) | |
2477 | log_debug("Userspace PKCS#7 validation succeeded."); | |
2478 | else | |
2479 | log_debug("Userspace PKCS#7 validation failed: %s", ERR_error_string(ERR_get_error(), NULL)); | |
2480 | ||
2481 | return r; | |
2482 | #else | |
2483 | log_debug("Not doing client-side validation of dm-verity root hash signatures, OpenSSL support disabled."); | |
2484 | return 0; | |
2485 | #endif | |
2486 | } | |
2487 | ||
2488 | static int do_crypt_activate_verity( | |
2489 | struct crypt_device *cd, | |
2490 | const char *name, | |
2491 | const VeritySettings *verity) { | |
2492 | ||
2493 | bool check_signature; | |
2494 | int r; | |
2495 | ||
2496 | assert(cd); | |
2497 | assert(name); | |
2498 | assert(verity); | |
2499 | ||
2500 | if (verity->root_hash_sig) { | |
2501 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_SIGNATURE"); | |
2502 | if (r < 0 && r != -ENXIO) | |
2503 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_SIGNATURE"); | |
2504 | ||
2505 | check_signature = r != 0; | |
2506 | } else | |
2507 | check_signature = false; | |
2508 | ||
2509 | if (check_signature) { | |
2510 | ||
2511 | #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY | |
2512 | /* First, if we have support for signed keys in the kernel, then try that first. */ | |
2513 | r = sym_crypt_activate_by_signed_key( | |
2514 | cd, | |
2515 | name, | |
2516 | verity->root_hash, | |
2517 | verity->root_hash_size, | |
2518 | verity->root_hash_sig, | |
2519 | verity->root_hash_sig_size, | |
2520 | CRYPT_ACTIVATE_READONLY); | |
2521 | if (r >= 0) | |
2522 | return r; | |
2523 | ||
2524 | log_debug("Validation of dm-verity signature failed via the kernel, trying userspace validation instead."); | |
2525 | #else | |
2526 | 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.", | |
2527 | program_invocation_short_name); | |
2528 | #endif | |
2529 | ||
2530 | /* So this didn't work via the kernel, then let's try userspace validation instead. If that | |
2531 | * works we'll try to activate without telling the kernel the signature. */ | |
2532 | ||
2533 | r = validate_signature_userspace(verity); | |
2534 | if (r < 0) | |
2535 | return r; | |
2536 | if (r == 0) | |
2537 | return log_debug_errno(SYNTHETIC_ERRNO(ENOKEY), | |
2538 | "Activation of signed Verity volume worked neither via the kernel nor in userspace, can't activate."); | |
2539 | } | |
2540 | ||
2541 | return sym_crypt_activate_by_volume_key( | |
2542 | cd, | |
2543 | name, | |
2544 | verity->root_hash, | |
2545 | verity->root_hash_size, | |
2546 | CRYPT_ACTIVATE_READONLY); | |
2547 | } | |
2548 | ||
ad361a50 YW |
2549 | static usec_t verity_timeout(void) { |
2550 | usec_t t = 100 * USEC_PER_MSEC; | |
2551 | const char *e; | |
2552 | int r; | |
2553 | ||
2554 | /* On slower machines, like non-KVM vm, setting up device may take a long time. | |
2555 | * Let's make the timeout configurable. */ | |
2556 | ||
2557 | e = getenv("SYSTEMD_DISSECT_VERITY_TIMEOUT_SEC"); | |
2558 | if (!e) | |
2559 | return t; | |
2560 | ||
2561 | r = parse_sec(e, &t); | |
2562 | if (r < 0) | |
2563 | log_debug_errno(r, | |
2564 | "Failed to parse timeout specified in $SYSTEMD_DISSECT_VERITY_TIMEOUT_SEC, " | |
2565 | "using the default timeout (%s).", | |
2566 | FORMAT_TIMESPAN(t, USEC_PER_MSEC)); | |
2567 | ||
2568 | return t; | |
2569 | } | |
2570 | ||
4623e8e6 | 2571 | static int verity_partition( |
aee36b4e | 2572 | PartitionDesignator designator, |
4623e8e6 LP |
2573 | DissectedPartition *m, |
2574 | DissectedPartition *v, | |
89e62e0b | 2575 | const VeritySettings *verity, |
4623e8e6 LP |
2576 | DissectImageFlags flags, |
2577 | DecryptedImage *d) { | |
2578 | ||
0d12936d | 2579 | _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; |
ac1f3ad0 | 2580 | _cleanup_(dm_deferred_remove_cleanp) char *restore_deferred_remove = NULL; |
89e62e0b | 2581 | _cleanup_free_ char *node = NULL, *name = NULL; |
254d1313 | 2582 | _cleanup_close_ int mount_node_fd = -EBADF; |
4623e8e6 LP |
2583 | int r; |
2584 | ||
2585 | assert(m); | |
89e62e0b | 2586 | assert(v || (verity && verity->data_path)); |
4623e8e6 | 2587 | |
89e62e0b | 2588 | if (!verity || !verity->root_hash) |
4623e8e6 | 2589 | return 0; |
aee36b4e LP |
2590 | if (!((verity->designator < 0 && designator == PARTITION_ROOT) || |
2591 | (verity->designator == designator))) | |
2592 | return 0; | |
4623e8e6 LP |
2593 | |
2594 | if (!m->found || !m->node || !m->fstype) | |
2595 | return 0; | |
89e62e0b | 2596 | if (!verity->data_path) { |
e7cbe5cb LB |
2597 | if (!v->found || !v->node || !v->fstype) |
2598 | return 0; | |
4623e8e6 | 2599 | |
e7cbe5cb LB |
2600 | if (!streq(v->fstype, "DM_verity_hash")) |
2601 | return 0; | |
2602 | } | |
4623e8e6 | 2603 | |
0d12936d LP |
2604 | r = dlopen_cryptsetup(); |
2605 | if (r < 0) | |
2606 | return r; | |
2607 | ||
ac1f3ad0 LB |
2608 | if (FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) { |
2609 | /* Use the roothash, which is unique per volume, as the device node name, so that it can be reused */ | |
2610 | _cleanup_free_ char *root_hash_encoded = NULL; | |
0d12936d | 2611 | |
89e62e0b | 2612 | root_hash_encoded = hexmem(verity->root_hash, verity->root_hash_size); |
ac1f3ad0 LB |
2613 | if (!root_hash_encoded) |
2614 | return -ENOMEM; | |
aee36b4e | 2615 | |
ac1f3ad0 LB |
2616 | r = make_dm_name_and_node(root_hash_encoded, "-verity", &name, &node); |
2617 | } else | |
2618 | r = make_dm_name_and_node(m->node, "-verity", &name, &node); | |
4623e8e6 LP |
2619 | if (r < 0) |
2620 | return r; | |
2621 | ||
89e62e0b | 2622 | r = sym_crypt_init(&cd, verity->data_path ?: v->node); |
4623e8e6 LP |
2623 | if (r < 0) |
2624 | return r; | |
2625 | ||
efc3b12f | 2626 | cryptsetup_enable_logging(cd); |
1887032f | 2627 | |
0d12936d | 2628 | r = sym_crypt_load(cd, CRYPT_VERITY, NULL); |
4623e8e6 | 2629 | if (r < 0) |
294bd454 | 2630 | return r; |
4623e8e6 | 2631 | |
0d12936d | 2632 | r = sym_crypt_set_data_device(cd, m->node); |
4623e8e6 | 2633 | if (r < 0) |
294bd454 | 2634 | return r; |
4623e8e6 | 2635 | |
319a4f4b | 2636 | if (!GREEDY_REALLOC0(d->decrypted, d->n_decrypted + 1)) |
ac1f3ad0 LB |
2637 | return -ENOMEM; |
2638 | ||
2639 | /* If activating fails because the device already exists, check the metadata and reuse it if it matches. | |
2640 | * In case of ENODEV/ENOENT, which can happen if another process is activating at the exact same time, | |
2641 | * retry a few times before giving up. */ | |
2642 | for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { | |
1d369d78 | 2643 | _cleanup_(sym_crypt_freep) struct crypt_device *existing_cd = NULL; |
254d1313 | 2644 | _cleanup_close_ int fd = -EBADF; |
c2fa92e7 | 2645 | |
f80015ff YW |
2646 | /* First, check if the device already exists. */ |
2647 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
2648 | if (fd < 0 && !ERRNO_IS_DEVICE_ABSENT(errno)) | |
2649 | return log_debug_errno(errno, "Failed to open verity device %s: %m", node); | |
2650 | if (fd >= 0) | |
2651 | goto check; /* The device already exists. Let's check it. */ | |
2652 | ||
2653 | /* The symlink to the device node does not exist yet. Assume not activated, and let's activate it. */ | |
c2fa92e7 | 2654 | r = do_crypt_activate_verity(cd, name, verity); |
1d369d78 | 2655 | if (r >= 0) |
f7725647 | 2656 | goto try_open; /* The device is activated. Let's open it. */ |
ac1f3ad0 LB |
2657 | /* libdevmapper can return EINVAL when the device is already in the activation stage. |
2658 | * There's no way to distinguish this situation from a genuine error due to invalid | |
2aed63f4 | 2659 | * parameters, so immediately fall back to activating the device with a unique name. |
89e62e0b LP |
2660 | * Improvements in libcrypsetup can ensure this never happens: |
2661 | * https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/96 */ | |
ac1f3ad0 | 2662 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) |
0f75b0c5 | 2663 | break; |
1d369d78 YW |
2664 | if (r == -ENODEV) /* Volume is being opened but not ready, crypt_init_by_name would fail, try to open again */ |
2665 | goto try_again; | |
2666 | if (!IN_SET(r, | |
2667 | -EEXIST, /* Volume has already been opened and ready to be used. */ | |
2668 | -EBUSY /* Volume is being opened but not ready, crypt_init_by_name() can fetch details. */)) | |
2669 | return log_debug_errno(r, "Failed to activate verity device %s: %m", node); | |
2670 | ||
f80015ff | 2671 | check: |
1d369d78 YW |
2672 | if (!restore_deferred_remove){ |
2673 | /* To avoid races, disable automatic removal on umount while setting up the new device. Restore it on failure. */ | |
2674 | r = dm_deferred_remove_cancel(name); | |
12f5fbdf YW |
2675 | /* -EBUSY and -ENXIO: the device has already been removed or being removed. We cannot |
2676 | * use the device, try to open again. See target_message() in drivers/md/dm-ioctl.c | |
2677 | * and dm_cancel_deferred_remove() in drivers/md/dm.c */ | |
2678 | if (IN_SET(r, -EBUSY, -ENXIO)) | |
2679 | goto try_again; | |
2680 | if (r < 0) | |
1d369d78 | 2681 | return log_debug_errno(r, "Failed to disable automated deferred removal for verity device %s: %m", node); |
12f5fbdf YW |
2682 | |
2683 | restore_deferred_remove = strdup(name); | |
2684 | if (!restore_deferred_remove) | |
2685 | return log_oom_debug(); | |
1d369d78 | 2686 | } |
c2923fdc | 2687 | |
1d369d78 YW |
2688 | r = verity_can_reuse(verity, name, &existing_cd); |
2689 | /* Same as above, -EINVAL can randomly happen when it actually means -EEXIST */ | |
2690 | if (r == -EINVAL && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
2691 | break; | |
2692 | if (IN_SET(r, | |
2693 | -ENOENT, /* Removed?? */ | |
2694 | -EBUSY, /* Volume is being opened but not ready, crypt_init_by_name() can fetch details. */ | |
2695 | -ENODEV /* Volume is being opened but not ready, crypt_init_by_name() would fail, try to open again. */ )) | |
2696 | goto try_again; | |
2697 | if (r < 0) | |
2698 | return log_debug_errno(r, "Failed to check if existing verity device %s can be reused: %m", node); | |
2699 | ||
f80015ff | 2700 | if (fd < 0) { |
1d369d78 YW |
2701 | /* devmapper might say that the device exists, but the devlink might not yet have been |
2702 | * created. Check and wait for the udev event in that case. */ | |
2703 | r = device_wait_for_devlink(node, "block", verity_timeout(), NULL); | |
2704 | /* Fallback to activation with a unique device if it's taking too long */ | |
2705 | if (r == -ETIMEDOUT && FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) | |
0f75b0c5 | 2706 | break; |
1d369d78 YW |
2707 | if (r < 0) |
2708 | return log_debug_errno(r, "Failed to wait device node symlink %s: %m", node); | |
c2923fdc | 2709 | } |
ecab4c47 | 2710 | |
f7725647 YW |
2711 | try_open: |
2712 | if (fd < 0) { | |
2713 | /* Now, the device is activated and devlink is created. Let's open it. */ | |
2714 | fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
2715 | if (fd < 0) { | |
2716 | if (!ERRNO_IS_DEVICE_ABSENT(errno)) | |
2717 | return log_debug_errno(errno, "Failed to open verity device %s: %m", node); | |
2718 | ||
2719 | /* The device has already been removed?? */ | |
2720 | goto try_again; | |
2721 | } | |
2722 | } | |
2723 | ||
2724 | mount_node_fd = TAKE_FD(fd); | |
1d369d78 YW |
2725 | if (existing_cd) |
2726 | crypt_free_and_replace(cd, existing_cd); | |
2727 | ||
2728 | goto success; | |
2729 | ||
2730 | try_again: | |
2731 | /* Device is being removed by another process. Let's wait for a while. */ | |
ecab4c47 | 2732 | (void) usleep(2 * USEC_PER_MSEC); |
ac1f3ad0 LB |
2733 | } |
2734 | ||
0f75b0c5 YW |
2735 | /* All trials failed or a conflicting verity device exists. Let's try to activate with a unique name. */ |
2736 | if (FLAGS_SET(flags, DISSECT_IMAGE_VERITY_SHARE)) { | |
2737 | /* Before trying to activate with unique name, we need to free crypt_device object. | |
2738 | * Otherwise, we get error from libcryptsetup like the following: | |
2739 | * ------ | |
2740 | * systemd[1234]: Cannot use device /dev/loop5 which is in use (already mapped or mounted). | |
2741 | * ------ | |
2742 | */ | |
2743 | sym_crypt_free(cd); | |
2744 | cd = NULL; | |
aee36b4e | 2745 | return verity_partition(designator, m, v, verity, flags & ~DISSECT_IMAGE_VERITY_SHARE, d); |
0f75b0c5 | 2746 | } |
ac1f3ad0 | 2747 | |
9972e6d6 YW |
2748 | return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "All attempts to activate verity device %s failed.", name); |
2749 | ||
2750 | success: | |
ac1f3ad0 LB |
2751 | /* Everything looks good and we'll be able to mount the device, so deferred remove will be re-enabled at that point. */ |
2752 | restore_deferred_remove = mfree(restore_deferred_remove); | |
4623e8e6 | 2753 | |
94344385 LP |
2754 | d->decrypted[d->n_decrypted++] = (DecryptedPartition) { |
2755 | .name = TAKE_PTR(name), | |
2756 | .device = TAKE_PTR(cd), | |
2757 | }; | |
4623e8e6 | 2758 | |
1cc6c93a | 2759 | m->decrypted_node = TAKE_PTR(node); |
f7725647 | 2760 | close_and_replace(m->mount_node_fd, mount_node_fd); |
4623e8e6 LP |
2761 | |
2762 | return 0; | |
18b5886e LP |
2763 | } |
2764 | #endif | |
2765 | ||
2766 | int dissected_image_decrypt( | |
2767 | DissectedImage *m, | |
2768 | const char *passphrase, | |
89e62e0b | 2769 | const VeritySettings *verity, |
e330f97a | 2770 | DissectImageFlags flags) { |
18b5886e | 2771 | |
349cc4a5 | 2772 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 2773 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
2774 | int r; |
2775 | #endif | |
2776 | ||
2777 | assert(m); | |
89e62e0b | 2778 | assert(!verity || verity->root_hash || verity->root_hash_size == 0); |
18b5886e LP |
2779 | |
2780 | /* Returns: | |
2781 | * | |
2782 | * = 0 → There was nothing to decrypt | |
2783 | * > 0 → Decrypted successfully | |
d1c536f5 | 2784 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
2785 | * -EKEYREJECTED → Passed key was not correct |
2786 | */ | |
2787 | ||
89e62e0b | 2788 | if (verity && verity->root_hash && verity->root_hash_size < sizeof(sd_id128_t)) |
4623e8e6 LP |
2789 | return -EINVAL; |
2790 | ||
e330f97a | 2791 | if (!m->encrypted && !m->verity_ready) |
18b5886e | 2792 | return 0; |
18b5886e | 2793 | |
349cc4a5 | 2794 | #if HAVE_LIBCRYPTSETUP |
9321ad51 YW |
2795 | r = decrypted_image_new(&d); |
2796 | if (r < 0) | |
2797 | return r; | |
18b5886e | 2798 | |
569a0e42 | 2799 | for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { |
18b5886e | 2800 | DissectedPartition *p = m->partitions + i; |
22043172 | 2801 | PartitionDesignator k; |
18b5886e LP |
2802 | |
2803 | if (!p->found) | |
2804 | continue; | |
2805 | ||
2806 | r = decrypt_partition(p, passphrase, flags, d); | |
2807 | if (r < 0) | |
2808 | return r; | |
2809 | ||
dd894023 | 2810 | k = partition_verity_of(i); |
4623e8e6 | 2811 | if (k >= 0) { |
aee36b4e | 2812 | r = verity_partition(i, p, m->partitions + k, verity, flags | DISSECT_IMAGE_VERITY_SHARE, d); |
4623e8e6 LP |
2813 | if (r < 0) |
2814 | return r; | |
2815 | } | |
2816 | ||
d2c6e79d | 2817 | if (!p->decrypted_fstype && p->mount_node_fd >= 0 && p->decrypted_node) { |
c80c9079 | 2818 | r = probe_filesystem_full(p->mount_node_fd, p->decrypted_node, 0, UINT64_MAX, &p->decrypted_fstype); |
7cc84b2c | 2819 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
2820 | return r; |
2821 | } | |
2822 | } | |
2823 | ||
ac1e1b5f | 2824 | m->decrypted_image = TAKE_PTR(d); |
18b5886e LP |
2825 | |
2826 | return 1; | |
2827 | #else | |
2828 | return -EOPNOTSUPP; | |
2829 | #endif | |
2830 | } | |
2831 | ||
2832 | int dissected_image_decrypt_interactively( | |
2833 | DissectedImage *m, | |
2834 | const char *passphrase, | |
89e62e0b | 2835 | const VeritySettings *verity, |
e330f97a | 2836 | DissectImageFlags flags) { |
18b5886e LP |
2837 | |
2838 | _cleanup_strv_free_erase_ char **z = NULL; | |
2839 | int n = 3, r; | |
2840 | ||
2841 | if (passphrase) | |
2842 | n--; | |
2843 | ||
2844 | for (;;) { | |
e330f97a | 2845 | r = dissected_image_decrypt(m, passphrase, verity, flags); |
18b5886e LP |
2846 | if (r >= 0) |
2847 | return r; | |
2848 | if (r == -EKEYREJECTED) | |
2849 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
fc95c359 YW |
2850 | else if (r != -ENOKEY) |
2851 | return log_error_errno(r, "Failed to decrypt image: %m"); | |
18b5886e | 2852 | |
baaa35ad ZJS |
2853 | if (--n < 0) |
2854 | return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED), | |
2855 | "Too many retries."); | |
18b5886e LP |
2856 | |
2857 | z = strv_free(z); | |
2858 | ||
8806bb4b | 2859 | r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", "dissect.passphrase", USEC_INFINITY, 0, &z); |
18b5886e LP |
2860 | if (r < 0) |
2861 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
2862 | ||
2863 | passphrase = z[0]; | |
2864 | } | |
2865 | } | |
2866 | ||
e330f97a | 2867 | static int decrypted_image_relinquish(DecryptedImage *d) { |
18b5886e LP |
2868 | assert(d); |
2869 | ||
67f63ee5 ZJS |
2870 | /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a |
2871 | * boolean so that we don't clean it up ourselves either anymore */ | |
18b5886e | 2872 | |
349cc4a5 | 2873 | #if HAVE_LIBCRYPTSETUP |
67f63ee5 ZJS |
2874 | int r; |
2875 | ||
2876 | for (size_t i = 0; i < d->n_decrypted; i++) { | |
18b5886e LP |
2877 | DecryptedPartition *p = d->decrypted + i; |
2878 | ||
2879 | if (p->relinquished) | |
2880 | continue; | |
2881 | ||
0d12936d | 2882 | r = sym_crypt_deactivate_by_name(NULL, p->name, CRYPT_DEACTIVATE_DEFERRED); |
18b5886e LP |
2883 | if (r < 0) |
2884 | return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name); | |
2885 | ||
2886 | p->relinquished = true; | |
2887 | } | |
2888 | #endif | |
2889 | ||
2890 | return 0; | |
2891 | } | |
2892 | ||
f906075a YW |
2893 | int dissected_image_relinquish(DissectedImage *m) { |
2894 | int r; | |
2895 | ||
2896 | assert(m); | |
2897 | ||
2898 | if (m->decrypted_image) { | |
2899 | r = decrypted_image_relinquish(m->decrypted_image); | |
2900 | if (r < 0) | |
2901 | return r; | |
2902 | } | |
2903 | ||
2904 | if (m->loop) | |
2905 | loop_device_relinquish(m->loop); | |
2906 | ||
2907 | return 0; | |
2908 | } | |
2909 | ||
89e62e0b LP |
2910 | static char *build_auxiliary_path(const char *image, const char *suffix) { |
2911 | const char *e; | |
2912 | char *n; | |
2913 | ||
2914 | assert(image); | |
2915 | assert(suffix); | |
2916 | ||
2917 | e = endswith(image, ".raw"); | |
2918 | if (!e) | |
2919 | return strjoin(e, suffix); | |
2920 | ||
2921 | n = new(char, e - image + strlen(suffix) + 1); | |
2922 | if (!n) | |
2923 | return NULL; | |
2924 | ||
2925 | strcpy(mempcpy(n, image, e - image), suffix); | |
2926 | return n; | |
2927 | } | |
2928 | ||
2929 | void verity_settings_done(VeritySettings *v) { | |
2930 | assert(v); | |
2931 | ||
2932 | v->root_hash = mfree(v->root_hash); | |
2933 | v->root_hash_size = 0; | |
2934 | ||
2935 | v->root_hash_sig = mfree(v->root_hash_sig); | |
2936 | v->root_hash_sig_size = 0; | |
2937 | ||
2938 | v->data_path = mfree(v->data_path); | |
2939 | } | |
2940 | ||
2941 | int verity_settings_load( | |
2942 | VeritySettings *verity, | |
f5ea63a5 LP |
2943 | const char *image, |
2944 | const char *root_hash_path, | |
89e62e0b LP |
2945 | const char *root_hash_sig_path) { |
2946 | ||
2947 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
2948 | size_t root_hash_size = 0, root_hash_sig_size = 0; | |
2949 | _cleanup_free_ char *verity_data_path = NULL; | |
aee36b4e | 2950 | PartitionDesignator designator; |
78ebe980 LP |
2951 | int r; |
2952 | ||
89e62e0b | 2953 | assert(verity); |
78ebe980 | 2954 | assert(image); |
aee36b4e | 2955 | assert(verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR)); |
78ebe980 | 2956 | |
89e62e0b LP |
2957 | /* If we are asked to load the root hash for a device node, exit early */ |
2958 | if (is_device_path(image)) | |
78ebe980 | 2959 | return 0; |
78ebe980 | 2960 | |
d5fcc5b0 LP |
2961 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_SIDECAR"); |
2962 | if (r < 0 && r != -ENXIO) | |
2963 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_SIDECAR, ignoring: %m"); | |
2964 | if (r == 0) | |
2965 | return 0; | |
2966 | ||
aee36b4e LP |
2967 | designator = verity->designator; |
2968 | ||
89e62e0b | 2969 | /* We only fill in what isn't already filled in */ |
c2923fdc | 2970 | |
89e62e0b | 2971 | if (!verity->root_hash) { |
e7cbe5cb | 2972 | _cleanup_free_ char *text = NULL; |
e7cbe5cb | 2973 | |
0389f4fa | 2974 | if (root_hash_path) { |
aee36b4e | 2975 | /* If explicitly specified it takes precedence */ |
0389f4fa LB |
2976 | r = read_one_line_file(root_hash_path, &text); |
2977 | if (r < 0) | |
e7cbe5cb | 2978 | return r; |
aee36b4e LP |
2979 | |
2980 | if (designator < 0) | |
2981 | designator = PARTITION_ROOT; | |
0389f4fa | 2982 | } else { |
aee36b4e LP |
2983 | /* Otherwise look for xattr and separate file, and first for the data for root and if |
2984 | * that doesn't exist for /usr */ | |
0389f4fa | 2985 | |
aee36b4e | 2986 | if (designator < 0 || designator == PARTITION_ROOT) { |
c53e07e2 | 2987 | r = getxattr_malloc(image, "user.verity.roothash", &text); |
aee36b4e LP |
2988 | if (r < 0) { |
2989 | _cleanup_free_ char *p = NULL; | |
78ebe980 | 2990 | |
00675c36 | 2991 | if (r != -ENOENT && !ERRNO_IS_XATTR_ABSENT(r)) |
aee36b4e | 2992 | return r; |
e7cbe5cb | 2993 | |
aee36b4e LP |
2994 | p = build_auxiliary_path(image, ".roothash"); |
2995 | if (!p) | |
2996 | return -ENOMEM; | |
2997 | ||
2998 | r = read_one_line_file(p, &text); | |
2999 | if (r < 0 && r != -ENOENT) | |
3000 | return r; | |
3001 | } | |
3002 | ||
3003 | if (text) | |
3004 | designator = PARTITION_ROOT; | |
3005 | } | |
3006 | ||
3007 | if (!text && (designator < 0 || designator == PARTITION_USR)) { | |
3008 | /* So in the "roothash" xattr/file name above the "root" of course primarily | |
3009 | * refers to the root of the Verity Merkle tree. But coincidentally it also | |
3010 | * is the hash for the *root* file system, i.e. the "root" neatly refers to | |
3011 | * two distinct concepts called "root". Taking benefit of this happy | |
3012 | * coincidence we call the file with the root hash for the /usr/ file system | |
3013 | * `usrhash`, because `usrroothash` or `rootusrhash` would just be too | |
3014 | * confusing. We thus drop the reference to the root of the Merkle tree, and | |
3015 | * just indicate which file system it's about. */ | |
c53e07e2 | 3016 | r = getxattr_malloc(image, "user.verity.usrhash", &text); |
aee36b4e LP |
3017 | if (r < 0) { |
3018 | _cleanup_free_ char *p = NULL; | |
3019 | ||
00675c36 | 3020 | if (r != -ENOENT && !ERRNO_IS_XATTR_ABSENT(r)) |
aee36b4e LP |
3021 | return r; |
3022 | ||
3023 | p = build_auxiliary_path(image, ".usrhash"); | |
3024 | if (!p) | |
3025 | return -ENOMEM; | |
3026 | ||
3027 | r = read_one_line_file(p, &text); | |
3028 | if (r < 0 && r != -ENOENT) | |
3029 | return r; | |
3030 | } | |
3031 | ||
3032 | if (text) | |
3033 | designator = PARTITION_USR; | |
0389f4fa | 3034 | } |
e7cbe5cb LB |
3035 | } |
3036 | ||
3037 | if (text) { | |
89e62e0b | 3038 | r = unhexmem(text, strlen(text), &root_hash, &root_hash_size); |
e7cbe5cb LB |
3039 | if (r < 0) |
3040 | return r; | |
89e62e0b | 3041 | if (root_hash_size < sizeof(sd_id128_t)) |
e7cbe5cb LB |
3042 | return -EINVAL; |
3043 | } | |
3044 | } | |
3045 | ||
90f98986 | 3046 | if ((root_hash || verity->root_hash) && !verity->root_hash_sig) { |
aee36b4e | 3047 | if (root_hash_sig_path) { |
ae9cf30b | 3048 | r = read_full_file(root_hash_sig_path, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
3049 | if (r < 0 && r != -ENOENT) |
3050 | return r; | |
3051 | ||
3052 | if (designator < 0) | |
3053 | designator = PARTITION_ROOT; | |
3054 | } else { | |
3055 | if (designator < 0 || designator == PARTITION_ROOT) { | |
3056 | _cleanup_free_ char *p = NULL; | |
3057 | ||
3058 | /* Follow naming convention recommended by the relevant RFC: | |
3059 | * https://tools.ietf.org/html/rfc5751#section-3.2.1 */ | |
3060 | p = build_auxiliary_path(image, ".roothash.p7s"); | |
3061 | if (!p) | |
3062 | return -ENOMEM; | |
89e62e0b | 3063 | |
ae9cf30b | 3064 | r = read_full_file(p, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
3065 | if (r < 0 && r != -ENOENT) |
3066 | return r; | |
3067 | if (r >= 0) | |
3068 | designator = PARTITION_ROOT; | |
3069 | } | |
3070 | ||
3071 | if (!root_hash_sig && (designator < 0 || designator == PARTITION_USR)) { | |
3072 | _cleanup_free_ char *p = NULL; | |
3073 | ||
3074 | p = build_auxiliary_path(image, ".usrhash.p7s"); | |
3075 | if (!p) | |
3076 | return -ENOMEM; | |
89e62e0b | 3077 | |
ae9cf30b | 3078 | r = read_full_file(p, (char**) &root_hash_sig, &root_hash_sig_size); |
aee36b4e LP |
3079 | if (r < 0 && r != -ENOENT) |
3080 | return r; | |
3081 | if (r >= 0) | |
3082 | designator = PARTITION_USR; | |
3083 | } | |
89e62e0b LP |
3084 | } |
3085 | ||
aee36b4e | 3086 | if (root_hash_sig && root_hash_sig_size == 0) /* refuse empty size signatures */ |
89e62e0b LP |
3087 | return -EINVAL; |
3088 | } | |
3089 | ||
3090 | if (!verity->data_path) { | |
3091 | _cleanup_free_ char *p = NULL; | |
3092 | ||
3093 | p = build_auxiliary_path(image, ".verity"); | |
3094 | if (!p) | |
3095 | return -ENOMEM; | |
3096 | ||
3097 | if (access(p, F_OK) < 0) { | |
3098 | if (errno != ENOENT) | |
3099 | return -errno; | |
3100 | } else | |
3101 | verity_data_path = TAKE_PTR(p); | |
3102 | } | |
3103 | ||
3104 | if (root_hash) { | |
3105 | verity->root_hash = TAKE_PTR(root_hash); | |
3106 | verity->root_hash_size = root_hash_size; | |
3107 | } | |
3108 | ||
3109 | if (root_hash_sig) { | |
3110 | verity->root_hash_sig = TAKE_PTR(root_hash_sig); | |
3111 | verity->root_hash_sig_size = root_hash_sig_size; | |
e7cbe5cb | 3112 | } |
89e62e0b LP |
3113 | |
3114 | if (verity_data_path) | |
3115 | verity->data_path = TAKE_PTR(verity_data_path); | |
78ebe980 | 3116 | |
aee36b4e LP |
3117 | if (verity->designator < 0) |
3118 | verity->designator = designator; | |
3119 | ||
78ebe980 LP |
3120 | return 1; |
3121 | } | |
3122 | ||
88b3300f LP |
3123 | int dissected_image_load_verity_sig_partition( |
3124 | DissectedImage *m, | |
3125 | int fd, | |
3126 | VeritySettings *verity) { | |
3127 | ||
3128 | _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; | |
3129 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
3130 | size_t root_hash_size, root_hash_sig_size; | |
3131 | _cleanup_free_ char *buf = NULL; | |
3132 | PartitionDesignator d; | |
3133 | DissectedPartition *p; | |
3134 | JsonVariant *rh, *sig; | |
3135 | ssize_t n; | |
3136 | char *e; | |
3137 | int r; | |
3138 | ||
3139 | assert(m); | |
3140 | assert(fd >= 0); | |
3141 | assert(verity); | |
3142 | ||
3143 | if (verity->root_hash && verity->root_hash_sig) /* Already loaded? */ | |
3144 | return 0; | |
3145 | ||
3146 | r = getenv_bool_secure("SYSTEMD_DISSECT_VERITY_EMBEDDED"); | |
3147 | if (r < 0 && r != -ENXIO) | |
3148 | log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_EMBEDDED, ignoring: %m"); | |
3149 | if (r == 0) | |
3150 | return 0; | |
3151 | ||
dd894023 | 3152 | d = partition_verity_sig_of(verity->designator < 0 ? PARTITION_ROOT : verity->designator); |
88b3300f LP |
3153 | assert(d >= 0); |
3154 | ||
3155 | p = m->partitions + d; | |
3156 | if (!p->found) | |
3157 | return 0; | |
3158 | if (p->offset == UINT64_MAX || p->size == UINT64_MAX) | |
3159 | return -EINVAL; | |
3160 | ||
3161 | if (p->size > 4*1024*1024) /* Signature data cannot possible be larger than 4M, refuse that */ | |
3162 | return -EFBIG; | |
3163 | ||
3164 | buf = new(char, p->size+1); | |
3165 | if (!buf) | |
3166 | return -ENOMEM; | |
3167 | ||
3168 | n = pread(fd, buf, p->size, p->offset); | |
3169 | if (n < 0) | |
3170 | return -ENOMEM; | |
3171 | if ((uint64_t) n != p->size) | |
3172 | return -EIO; | |
3173 | ||
3174 | e = memchr(buf, 0, p->size); | |
3175 | if (e) { | |
3176 | /* If we found a NUL byte then the rest of the data must be NUL too */ | |
3177 | if (!memeqzero(e, p->size - (e - buf))) | |
3178 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature data contains embedded NUL byte."); | |
3179 | } else | |
3180 | buf[p->size] = 0; | |
3181 | ||
3182 | r = json_parse(buf, 0, &v, NULL, NULL); | |
3183 | if (r < 0) | |
3184 | return log_debug_errno(r, "Failed to parse signature JSON data: %m"); | |
3185 | ||
3186 | rh = json_variant_by_key(v, "rootHash"); | |
3187 | if (!rh) | |
3188 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'rootHash' field."); | |
3189 | if (!json_variant_is_string(rh)) | |
3190 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'rootHash' field of signature JSON object is not a string."); | |
3191 | ||
3192 | r = unhexmem(json_variant_string(rh), SIZE_MAX, &root_hash, &root_hash_size); | |
3193 | if (r < 0) | |
3194 | return log_debug_errno(r, "Failed to parse root hash field: %m"); | |
3195 | ||
3196 | /* Check if specified root hash matches if it is specified */ | |
3197 | if (verity->root_hash && | |
3198 | memcmp_nn(verity->root_hash, verity->root_hash_size, root_hash, root_hash_size) != 0) { | |
3199 | _cleanup_free_ char *a = NULL, *b = NULL; | |
3200 | ||
3201 | a = hexmem(root_hash, root_hash_size); | |
3202 | b = hexmem(verity->root_hash, verity->root_hash_size); | |
3203 | ||
3204 | return log_debug_errno(r, "Root hash in signature JSON data (%s) doesn't match configured hash (%s).", strna(a), strna(b)); | |
3205 | } | |
3206 | ||
3207 | sig = json_variant_by_key(v, "signature"); | |
3208 | if (!sig) | |
3209 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'signature' field."); | |
3210 | if (!json_variant_is_string(sig)) | |
3211 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'signature' field of signature JSON object is not a string."); | |
3212 | ||
3213 | r = unbase64mem(json_variant_string(sig), SIZE_MAX, &root_hash_sig, &root_hash_sig_size); | |
3214 | if (r < 0) | |
3215 | return log_debug_errno(r, "Failed to parse signature field: %m"); | |
3216 | ||
3217 | free_and_replace(verity->root_hash, root_hash); | |
3218 | verity->root_hash_size = root_hash_size; | |
3219 | ||
3220 | free_and_replace(verity->root_hash_sig, root_hash_sig); | |
3221 | verity->root_hash_sig_size = root_hash_sig_size; | |
3222 | ||
3223 | return 1; | |
3224 | } | |
3225 | ||
22847508 | 3226 | int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_flags) { |
3b925504 LP |
3227 | |
3228 | enum { | |
3229 | META_HOSTNAME, | |
3230 | META_MACHINE_ID, | |
3231 | META_MACHINE_INFO, | |
3232 | META_OS_RELEASE, | |
fab22946 | 3233 | META_INITRD_RELEASE, |
7718ac97 | 3234 | META_EXTENSION_RELEASE, |
a4e0d617 | 3235 | META_HAS_INIT_SYSTEM, |
3b925504 LP |
3236 | _META_MAX, |
3237 | }; | |
3238 | ||
9a4b883b | 3239 | static const char *const paths[_META_MAX] = { |
7718ac97 LB |
3240 | [META_HOSTNAME] = "/etc/hostname\0", |
3241 | [META_MACHINE_ID] = "/etc/machine-id\0", | |
3242 | [META_MACHINE_INFO] = "/etc/machine-info\0", | |
9a4b883b | 3243 | [META_OS_RELEASE] = ("/etc/os-release\0" |
22847508 | 3244 | "/usr/lib/os-release\0"), |
fab22946 LP |
3245 | [META_INITRD_RELEASE] = ("/etc/initrd-release\0" |
3246 | "/usr/lib/initrd-release\0"), | |
a4e0d617 LP |
3247 | [META_EXTENSION_RELEASE] = "extension-release\0", /* Used only for logging. */ |
3248 | [META_HAS_INIT_SYSTEM] = "has-init-system\0", /* ditto */ | |
3b925504 LP |
3249 | }; |
3250 | ||
fab22946 | 3251 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL, **initrd_release = NULL, **extension_release = NULL; |
19ee48a6 | 3252 | _cleanup_close_pair_ int error_pipe[2] = PIPE_EBADF; |
3b925504 LP |
3253 | _cleanup_(rmdir_and_freep) char *t = NULL; |
3254 | _cleanup_(sigkill_waitp) pid_t child = 0; | |
3255 | sd_id128_t machine_id = SD_ID128_NULL; | |
3256 | _cleanup_free_ char *hostname = NULL; | |
67f63ee5 | 3257 | unsigned n_meta_initialized = 0; |
af8219d5 | 3258 | int fds[2 * _META_MAX], r, v; |
a4e0d617 | 3259 | int has_init_system = -1; |
af8219d5 | 3260 | ssize_t n; |
3b925504 LP |
3261 | |
3262 | BLOCK_SIGNALS(SIGCHLD); | |
3263 | ||
3264 | assert(m); | |
3265 | ||
7718ac97 | 3266 | for (; n_meta_initialized < _META_MAX; n_meta_initialized ++) { |
d9119c00 | 3267 | if (!paths[n_meta_initialized]) { |
254d1313 | 3268 | fds[2*n_meta_initialized] = fds[2*n_meta_initialized+1] = -EBADF; |
7718ac97 | 3269 | continue; |
d9119c00 LP |
3270 | } |
3271 | ||
3b925504 LP |
3272 | if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) { |
3273 | r = -errno; | |
3274 | goto finish; | |
3275 | } | |
7718ac97 | 3276 | } |
3b925504 LP |
3277 | |
3278 | r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); | |
3279 | if (r < 0) | |
3280 | goto finish; | |
3281 | ||
af8219d5 LP |
3282 | if (pipe2(error_pipe, O_CLOEXEC) < 0) { |
3283 | r = -errno; | |
3284 | goto finish; | |
3285 | } | |
3286 | ||
e2047ba9 | 3287 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 3288 | if (r < 0) |
3b925504 | 3289 | goto finish; |
be39f6ee | 3290 | if (r == 0) { |
a4e0d617 | 3291 | /* Child in a new mount namespace */ |
af8219d5 LP |
3292 | error_pipe[0] = safe_close(error_pipe[0]); |
3293 | ||
7cf66030 LP |
3294 | r = dissected_image_mount( |
3295 | m, | |
3296 | t, | |
3297 | UID_INVALID, | |
21b61b1d | 3298 | UID_INVALID, |
22847508 ZJS |
3299 | extra_flags | |
3300 | DISSECT_IMAGE_READ_ONLY | | |
3301 | DISSECT_IMAGE_MOUNT_ROOT_ONLY | | |
7cf66030 | 3302 | DISSECT_IMAGE_USR_NO_ROOT); |
429d4e41 LP |
3303 | if (r < 0) { |
3304 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
03ae68f4 | 3305 | goto inner_fail; |
429d4e41 | 3306 | } |
3b925504 | 3307 | |
67f63ee5 | 3308 | for (unsigned k = 0; k < _META_MAX; k++) { |
37e44c3f | 3309 | _cleanup_close_ int fd = -ENOENT; |
3b925504 | 3310 | |
7718ac97 LB |
3311 | if (!paths[k]) |
3312 | continue; | |
3313 | ||
3b925504 LP |
3314 | fds[2*k] = safe_close(fds[2*k]); |
3315 | ||
a4e0d617 LP |
3316 | switch (k) { |
3317 | ||
3318 | case META_EXTENSION_RELEASE: | |
9a4b883b LB |
3319 | /* As per the os-release spec, if the image is an extension it will have a file |
3320 | * named after the image name in extension-release.d/ - we use the image name | |
3321 | * and try to resolve it with the extension-release helpers, as sometimes | |
3322 | * the image names are mangled on deployment and do not match anymore. | |
3323 | * Unlike other paths this is not fixed, and the image name | |
3324 | * can be mangled on deployment, so by calling into the helper | |
3325 | * we allow a fallback that matches on the first extension-release | |
3326 | * file found in the directory, if one named after the image cannot | |
3327 | * be found first. */ | |
b60e0f57 | 3328 | r = open_extension_release(t, IMAGE_SYSEXT, m->image_name, /* relax_extension_release_check= */ false, NULL, &fd); |
9a4b883b LB |
3329 | if (r < 0) |
3330 | fd = r; /* Propagate the error. */ | |
a4e0d617 LP |
3331 | break; |
3332 | ||
3333 | case META_HAS_INIT_SYSTEM: { | |
3334 | bool found = false; | |
a4e0d617 LP |
3335 | |
3336 | FOREACH_STRING(init, | |
3337 | "/usr/lib/systemd/systemd", /* systemd on /usr merged system */ | |
3338 | "/lib/systemd/systemd", /* systemd on /usr non-merged systems */ | |
3339 | "/sbin/init") { /* traditional path the Linux kernel invokes */ | |
3340 | ||
f461a28d | 3341 | r = chase(init, t, CHASE_PREFIX_ROOT, NULL, NULL); |
a4e0d617 LP |
3342 | if (r < 0) { |
3343 | if (r != -ENOENT) | |
3344 | log_debug_errno(r, "Failed to resolve %s, ignoring: %m", init); | |
3345 | } else { | |
3346 | found = true; | |
3347 | break; | |
3348 | } | |
3349 | } | |
3350 | ||
3351 | r = loop_write(fds[2*k+1], &found, sizeof(found), false); | |
3352 | if (r < 0) | |
3353 | goto inner_fail; | |
3354 | ||
3355 | continue; | |
3356 | } | |
3357 | ||
3358 | default: | |
9a4b883b | 3359 | NULSTR_FOREACH(p, paths[k]) { |
f461a28d | 3360 | fd = chase_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); |
9a4b883b LB |
3361 | if (fd >= 0) |
3362 | break; | |
3363 | } | |
a4e0d617 LP |
3364 | } |
3365 | ||
36952d19 LP |
3366 | if (fd < 0) { |
3367 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
37e44c3f | 3368 | fds[2*k+1] = safe_close(fds[2*k+1]); |
3b925504 | 3369 | continue; |
36952d19 | 3370 | } |
3b925504 | 3371 | |
f5fbe71d | 3372 | r = copy_bytes(fd, fds[2*k+1], UINT64_MAX, 0); |
a4e0d617 LP |
3373 | if (r < 0) |
3374 | goto inner_fail; | |
3b925504 LP |
3375 | |
3376 | fds[2*k+1] = safe_close(fds[2*k+1]); | |
3377 | } | |
3378 | ||
3379 | _exit(EXIT_SUCCESS); | |
a4e0d617 LP |
3380 | |
3381 | inner_fail: | |
03ae68f4 | 3382 | /* Let parent know the error */ |
a4e0d617 LP |
3383 | (void) write(error_pipe[1], &r, sizeof(r)); |
3384 | _exit(EXIT_FAILURE); | |
3b925504 LP |
3385 | } |
3386 | ||
af8219d5 LP |
3387 | error_pipe[1] = safe_close(error_pipe[1]); |
3388 | ||
67f63ee5 | 3389 | for (unsigned k = 0; k < _META_MAX; k++) { |
3b925504 LP |
3390 | _cleanup_fclose_ FILE *f = NULL; |
3391 | ||
7718ac97 LB |
3392 | if (!paths[k]) |
3393 | continue; | |
3394 | ||
3b925504 LP |
3395 | fds[2*k+1] = safe_close(fds[2*k+1]); |
3396 | ||
4fa744a3 | 3397 | f = take_fdopen(&fds[2*k], "r"); |
3b925504 LP |
3398 | if (!f) { |
3399 | r = -errno; | |
3400 | goto finish; | |
3401 | } | |
3402 | ||
3b925504 LP |
3403 | switch (k) { |
3404 | ||
3405 | case META_HOSTNAME: | |
3406 | r = read_etc_hostname_stream(f, &hostname); | |
3407 | if (r < 0) | |
f6048e5e | 3408 | log_debug_errno(r, "Failed to read /etc/hostname of image: %m"); |
3b925504 LP |
3409 | |
3410 | break; | |
3411 | ||
3412 | case META_MACHINE_ID: { | |
3413 | _cleanup_free_ char *line = NULL; | |
3414 | ||
3415 | r = read_line(f, LONG_LINE_MAX, &line); | |
3416 | if (r < 0) | |
f6048e5e | 3417 | log_debug_errno(r, "Failed to read /etc/machine-id of image: %m"); |
3b925504 LP |
3418 | else if (r == 33) { |
3419 | r = sd_id128_from_string(line, &machine_id); | |
3420 | if (r < 0) | |
3421 | log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line); | |
3422 | } else if (r == 0) | |
f6048e5e | 3423 | log_debug("/etc/machine-id file of image is empty."); |
ab763cb2 | 3424 | else if (streq(line, "uninitialized")) |
f6048e5e | 3425 | log_debug("/etc/machine-id file of image is uninitialized (likely aborted first boot)."); |
3b925504 | 3426 | else |
f6048e5e | 3427 | log_debug("/etc/machine-id file of image has unexpected length %i.", r); |
3b925504 LP |
3428 | |
3429 | break; | |
3430 | } | |
3431 | ||
3432 | case META_MACHINE_INFO: | |
aa8fbc74 | 3433 | r = load_env_file_pairs(f, "machine-info", &machine_info); |
3b925504 | 3434 | if (r < 0) |
f6048e5e | 3435 | log_debug_errno(r, "Failed to read /etc/machine-info of image: %m"); |
3b925504 LP |
3436 | |
3437 | break; | |
3438 | ||
3439 | case META_OS_RELEASE: | |
aa8fbc74 | 3440 | r = load_env_file_pairs(f, "os-release", &os_release); |
3b925504 | 3441 | if (r < 0) |
f6048e5e | 3442 | log_debug_errno(r, "Failed to read OS release file of image: %m"); |
3b925504 LP |
3443 | |
3444 | break; | |
7718ac97 | 3445 | |
fab22946 LP |
3446 | case META_INITRD_RELEASE: |
3447 | r = load_env_file_pairs(f, "initrd-release", &initrd_release); | |
3448 | if (r < 0) | |
3449 | log_debug_errno(r, "Failed to read initrd release file of image: %m"); | |
3450 | ||
3451 | break; | |
3452 | ||
7718ac97 LB |
3453 | case META_EXTENSION_RELEASE: |
3454 | r = load_env_file_pairs(f, "extension-release", &extension_release); | |
3455 | if (r < 0) | |
f6048e5e | 3456 | log_debug_errno(r, "Failed to read extension release file of image: %m"); |
7718ac97 LB |
3457 | |
3458 | break; | |
a4e0d617 LP |
3459 | |
3460 | case META_HAS_INIT_SYSTEM: { | |
3461 | bool b = false; | |
3462 | size_t nr; | |
3463 | ||
3464 | errno = 0; | |
3465 | nr = fread(&b, 1, sizeof(b), f); | |
3466 | if (nr != sizeof(b)) | |
3467 | log_debug_errno(errno_or_else(EIO), "Failed to read has-init-system boolean: %m"); | |
3468 | else | |
3469 | has_init_system = b; | |
3470 | ||
3471 | break; | |
3472 | }} | |
3b925504 LP |
3473 | } |
3474 | ||
2e87a1fd | 3475 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 3476 | child = 0; |
2e87a1fd | 3477 | if (r < 0) |
af8219d5 LP |
3478 | return r; |
3479 | ||
3480 | n = read(error_pipe[0], &v, sizeof(v)); | |
3481 | if (n < 0) | |
3482 | return -errno; | |
3483 | if (n == sizeof(v)) | |
3484 | return v; /* propagate error sent to us from child */ | |
3485 | if (n != 0) | |
3486 | return -EIO; | |
3487 | ||
2e87a1fd LP |
3488 | if (r != EXIT_SUCCESS) |
3489 | return -EPROTO; | |
3b925504 LP |
3490 | |
3491 | free_and_replace(m->hostname, hostname); | |
3492 | m->machine_id = machine_id; | |
3493 | strv_free_and_replace(m->machine_info, machine_info); | |
3494 | strv_free_and_replace(m->os_release, os_release); | |
fab22946 | 3495 | strv_free_and_replace(m->initrd_release, initrd_release); |
7718ac97 | 3496 | strv_free_and_replace(m->extension_release, extension_release); |
a4e0d617 | 3497 | m->has_init_system = has_init_system; |
3b925504 LP |
3498 | |
3499 | finish: | |
67f63ee5 | 3500 | for (unsigned k = 0; k < n_meta_initialized; k++) |
3b925504 LP |
3501 | safe_close_pair(fds + 2*k); |
3502 | ||
3503 | return r; | |
3504 | } | |
3505 | ||
2348043f LP |
3506 | Architecture dissected_image_architecture(DissectedImage *img) { |
3507 | assert(img); | |
3508 | ||
3509 | if (img->partitions[PARTITION_ROOT].found && | |
3510 | img->partitions[PARTITION_ROOT].architecture >= 0) | |
3511 | return img->partitions[PARTITION_ROOT].architecture; | |
3512 | ||
3513 | if (img->partitions[PARTITION_USR].found && | |
3514 | img->partitions[PARTITION_USR].architecture >= 0) | |
3515 | return img->partitions[PARTITION_USR].architecture; | |
3516 | ||
3517 | return _ARCHITECTURE_INVALID; | |
3518 | } | |
3519 | ||
1e63dc4f YW |
3520 | int dissect_loop_device( |
3521 | LoopDevice *loop, | |
3522 | const VeritySettings *verity, | |
3523 | const MountOptions *mount_options, | |
84be0c71 | 3524 | const ImagePolicy *image_policy, |
1e63dc4f YW |
3525 | DissectImageFlags flags, |
3526 | DissectedImage **ret) { | |
3527 | ||
08f14be4 | 3528 | #if HAVE_BLKID |
1e63dc4f YW |
3529 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
3530 | int r; | |
3531 | ||
3532 | assert(loop); | |
1e63dc4f | 3533 | |
08f14be4 | 3534 | r = dissected_image_new(loop->backing_file ?: loop->node, &m); |
1e63dc4f YW |
3535 | if (r < 0) |
3536 | return r; | |
3537 | ||
3538 | m->loop = loop_device_ref(loop); | |
22ee78a8 | 3539 | m->sector_size = m->loop->sector_size; |
1e63dc4f | 3540 | |
84be0c71 | 3541 | r = dissect_image(m, loop->fd, loop->node, verity, mount_options, image_policy, flags); |
08f14be4 | 3542 | if (r < 0) |
08f14be4 YW |
3543 | return r; |
3544 | ||
93a8a85b LP |
3545 | if (ret) |
3546 | *ret = TAKE_PTR(m); | |
3547 | ||
1e63dc4f | 3548 | return 0; |
08f14be4 YW |
3549 | #else |
3550 | return -EOPNOTSUPP; | |
3551 | #endif | |
1e63dc4f YW |
3552 | } |
3553 | ||
bad31660 | 3554 | int dissect_loop_device_and_warn( |
1e63dc4f | 3555 | LoopDevice *loop, |
89e62e0b | 3556 | const VeritySettings *verity, |
18d73705 | 3557 | const MountOptions *mount_options, |
84be0c71 | 3558 | const ImagePolicy *image_policy, |
4526113f LP |
3559 | DissectImageFlags flags, |
3560 | DissectedImage **ret) { | |
3561 | ||
bad31660 | 3562 | assert(loop); |
4526113f | 3563 | |
7cd7a195 | 3564 | return dissect_log_error( |
4953e39c | 3565 | LOG_ERR, |
7cd7a195 LP |
3566 | dissect_loop_device(loop, verity, mount_options, image_policy, flags, ret), |
3567 | loop->backing_file ?: loop->node, | |
3568 | verity); | |
4526113f | 3569 | |
4526113f LP |
3570 | } |
3571 | ||
49536766 LP |
3572 | bool dissected_image_verity_candidate(const DissectedImage *image, PartitionDesignator partition_designator) { |
3573 | assert(image); | |
3574 | ||
3575 | /* Checks if this partition could theoretically do Verity. For non-partitioned images this only works | |
3576 | * if there's an external verity file supplied, for which we can consult .has_verity. For partitioned | |
3577 | * images we only check the partition type. | |
3578 | * | |
3579 | * This call is used to decide whether to suppress or show a verity column in tabular output of the | |
3580 | * image. */ | |
3581 | ||
e7cbe5cb | 3582 | if (image->single_file_system) |
c3c88d67 | 3583 | return partition_designator == PARTITION_ROOT && image->has_verity; |
e7cbe5cb | 3584 | |
dd894023 | 3585 | return partition_verity_of(partition_designator) >= 0; |
e7cbe5cb LB |
3586 | } |
3587 | ||
49536766 LP |
3588 | bool dissected_image_verity_ready(const DissectedImage *image, PartitionDesignator partition_designator) { |
3589 | PartitionDesignator k; | |
3590 | ||
3591 | assert(image); | |
3592 | ||
3593 | /* Checks if this partition has verity data available that we can activate. For non-partitioned this | |
3594 | * works for the root partition, for others only if the associated verity partition was found. */ | |
3595 | ||
3596 | if (!image->verity_ready) | |
3597 | return false; | |
e7cbe5cb LB |
3598 | |
3599 | if (image->single_file_system) | |
49536766 | 3600 | return partition_designator == PARTITION_ROOT; |
e7cbe5cb | 3601 | |
dd894023 | 3602 | k = partition_verity_of(partition_designator); |
e7cbe5cb LB |
3603 | return k >= 0 && image->partitions[k].found; |
3604 | } | |
3605 | ||
8ee9615e LP |
3606 | bool dissected_image_verity_sig_ready(const DissectedImage *image, PartitionDesignator partition_designator) { |
3607 | PartitionDesignator k; | |
3608 | ||
3609 | assert(image); | |
3610 | ||
3611 | /* Checks if this partition has verity signature data available that we can use. */ | |
3612 | ||
3613 | if (!image->verity_sig_ready) | |
3614 | return false; | |
3615 | ||
3616 | if (image->single_file_system) | |
3617 | return partition_designator == PARTITION_ROOT; | |
3618 | ||
dd894023 | 3619 | k = partition_verity_sig_of(partition_designator); |
8ee9615e LP |
3620 | return k >= 0 && image->partitions[k].found; |
3621 | } | |
3622 | ||
18d73705 LB |
3623 | MountOptions* mount_options_free_all(MountOptions *options) { |
3624 | MountOptions *m; | |
3625 | ||
3626 | while ((m = options)) { | |
3627 | LIST_REMOVE(mount_options, options, m); | |
3628 | free(m->options); | |
3629 | free(m); | |
3630 | } | |
3631 | ||
3632 | return NULL; | |
3633 | } | |
3634 | ||
569a0e42 | 3635 | const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator) { |
f5215bc8 | 3636 | LIST_FOREACH(mount_options, m, options) |
9ece6444 | 3637 | if (designator == m->partition_designator && !isempty(m->options)) |
18d73705 | 3638 | return m->options; |
6aa05ebd | 3639 | |
18d73705 LB |
3640 | return NULL; |
3641 | } | |
3642 | ||
6aa05ebd LP |
3643 | int mount_image_privately_interactively( |
3644 | const char *image, | |
84be0c71 | 3645 | const ImagePolicy *image_policy, |
6aa05ebd LP |
3646 | DissectImageFlags flags, |
3647 | char **ret_directory, | |
a133d2c3 | 3648 | int *ret_dir_fd, |
e330f97a | 3649 | LoopDevice **ret_loop_device) { |
6aa05ebd | 3650 | |
27ec815e | 3651 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
6aa05ebd | 3652 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
6aa05ebd LP |
3653 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; |
3654 | _cleanup_(rmdir_and_freep) char *created_dir = NULL; | |
3655 | _cleanup_free_ char *temp = NULL; | |
3656 | int r; | |
3657 | ||
3658 | /* Mounts an OS image at a temporary place, inside a newly created mount namespace of our own. This | |
3659 | * is used by tools such as systemd-tmpfiles or systemd-firstboot to operate on some disk image | |
3660 | * easily. */ | |
3661 | ||
3662 | assert(image); | |
3663 | assert(ret_directory); | |
3664 | assert(ret_loop_device); | |
6aa05ebd | 3665 | |
0b75493d | 3666 | /* We intend to mount this right-away, hence add the partitions if needed and pin them. */ |
73d88b80 LP |
3667 | flags |= DISSECT_IMAGE_ADD_PARTITION_DEVICES | |
3668 | DISSECT_IMAGE_PIN_PARTITION_DEVICES; | |
3669 | ||
27ec815e LP |
3670 | r = verity_settings_load(&verity, image, NULL, NULL); |
3671 | if (r < 0) | |
3672 | return log_error_errno(r, "Failed to load root hash data: %m"); | |
3673 | ||
6aa05ebd LP |
3674 | r = tempfn_random_child(NULL, program_invocation_short_name, &temp); |
3675 | if (r < 0) | |
3676 | return log_error_errno(r, "Failed to generate temporary mount directory: %m"); | |
3677 | ||
3678 | r = loop_device_make_by_path( | |
3679 | image, | |
ef9c184d | 3680 | FLAGS_SET(flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : O_RDWR, |
22ee78a8 | 3681 | /* sector_size= */ UINT32_MAX, |
6aa05ebd | 3682 | FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, |
7f52206a | 3683 | LOCK_SH, |
6aa05ebd LP |
3684 | &d); |
3685 | if (r < 0) | |
7b87fe4c | 3686 | return log_error_errno(r, "Failed to set up loopback device for %s: %m", image); |
6aa05ebd | 3687 | |
84be0c71 LP |
3688 | r = dissect_loop_device_and_warn( |
3689 | d, | |
3690 | &verity, | |
3691 | /* mount_options= */ NULL, | |
3692 | image_policy, | |
3693 | flags, | |
3694 | &dissected_image); | |
6aa05ebd LP |
3695 | if (r < 0) |
3696 | return r; | |
3697 | ||
88b3300f LP |
3698 | r = dissected_image_load_verity_sig_partition(dissected_image, d->fd, &verity); |
3699 | if (r < 0) | |
3700 | return r; | |
3701 | ||
e330f97a | 3702 | r = dissected_image_decrypt_interactively(dissected_image, NULL, &verity, flags); |
6aa05ebd LP |
3703 | if (r < 0) |
3704 | return r; | |
3705 | ||
3706 | r = detach_mount_namespace(); | |
3707 | if (r < 0) | |
3708 | return log_error_errno(r, "Failed to detach mount namespace: %m"); | |
3709 | ||
3710 | r = mkdir_p(temp, 0700); | |
3711 | if (r < 0) | |
3712 | return log_error_errno(r, "Failed to create mount point: %m"); | |
3713 | ||
3714 | created_dir = TAKE_PTR(temp); | |
3715 | ||
21b61b1d | 3716 | r = dissected_image_mount_and_warn(dissected_image, created_dir, UID_INVALID, UID_INVALID, flags); |
6aa05ebd | 3717 | if (r < 0) |
af187ab2 | 3718 | return r; |
6aa05ebd | 3719 | |
41bc4849 LP |
3720 | r = loop_device_flock(d, LOCK_UN); |
3721 | if (r < 0) | |
3722 | return r; | |
3723 | ||
3044d343 YW |
3724 | r = dissected_image_relinquish(dissected_image); |
3725 | if (r < 0) | |
3726 | return log_error_errno(r, "Failed to relinquish DM and loopback block devices: %m"); | |
6aa05ebd | 3727 | |
a133d2c3 DDM |
3728 | if (ret_dir_fd) { |
3729 | _cleanup_close_ int dir_fd = -EBADF; | |
3730 | ||
3731 | dir_fd = open(created_dir, O_CLOEXEC|O_DIRECTORY); | |
3732 | if (dir_fd < 0) | |
3733 | return log_error_errno(errno, "Failed to open mount point directory: %m"); | |
3734 | ||
3735 | *ret_dir_fd = TAKE_FD(dir_fd); | |
3736 | } | |
3737 | ||
6aa05ebd LP |
3738 | *ret_directory = TAKE_PTR(created_dir); |
3739 | *ret_loop_device = TAKE_PTR(d); | |
6aa05ebd LP |
3740 | |
3741 | return 0; | |
3742 | } | |
3743 | ||
06768b90 LB |
3744 | static bool mount_options_relax_extension_release_checks(const MountOptions *options) { |
3745 | if (!options) | |
3746 | return false; | |
3747 | ||
3748 | return string_contains_word(mount_options_from_designator(options, PARTITION_ROOT), ",", "x-systemd.relax-extension-release-check") || | |
3749 | string_contains_word(mount_options_from_designator(options, PARTITION_USR), ",", "x-systemd.relax-extension-release-check") || | |
3750 | string_contains_word(options->options, ",", "x-systemd.relax-extension-release-check"); | |
3751 | } | |
3752 | ||
93f59701 | 3753 | int verity_dissect_and_mount( |
cedf5b1a | 3754 | int src_fd, |
93f59701 LB |
3755 | const char *src, |
3756 | const char *dest, | |
3757 | const MountOptions *options, | |
84be0c71 | 3758 | const ImagePolicy *image_policy, |
93f59701 LB |
3759 | const char *required_host_os_release_id, |
3760 | const char *required_host_os_release_version_id, | |
60c5f700 LP |
3761 | const char *required_host_os_release_sysext_level, |
3762 | const char *required_sysext_scope) { | |
93f59701 | 3763 | |
4beda316 | 3764 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
4beda316 LB |
3765 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; |
3766 | _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; | |
3767 | DissectImageFlags dissect_image_flags; | |
06768b90 | 3768 | bool relax_extension_release_check; |
4beda316 LB |
3769 | int r; |
3770 | ||
3771 | assert(src); | |
3772 | assert(dest); | |
3773 | ||
06768b90 LB |
3774 | relax_extension_release_check = mount_options_relax_extension_release_checks(options); |
3775 | ||
cedf5b1a | 3776 | /* We might get an FD for the image, but we use the original path to look for the dm-verity files */ |
4beda316 LB |
3777 | r = verity_settings_load(&verity, src, NULL, NULL); |
3778 | if (r < 0) | |
3779 | return log_debug_errno(r, "Failed to load root hash: %m"); | |
3780 | ||
06768b90 | 3781 | dissect_image_flags = (verity.data_path ? DISSECT_IMAGE_NO_PARTITION_TABLE : 0) | |
73d88b80 LP |
3782 | (relax_extension_release_check ? DISSECT_IMAGE_RELAX_SYSEXT_CHECK : 0) | |
3783 | DISSECT_IMAGE_ADD_PARTITION_DEVICES | | |
3784 | DISSECT_IMAGE_PIN_PARTITION_DEVICES; | |
4beda316 | 3785 | |
cedf5b1a LB |
3786 | /* Note that we don't use loop_device_make here, as the FD is most likely O_PATH which would not be |
3787 | * accepted by LOOP_CONFIGURE, so just let loop_device_make_by_path reopen it as a regular FD. */ | |
4beda316 | 3788 | r = loop_device_make_by_path( |
cedf5b1a | 3789 | src_fd >= 0 ? FORMAT_PROC_FD_PATH(src_fd) : src, |
22ee78a8 LP |
3790 | /* open_flags= */ -1, |
3791 | /* sector_size= */ UINT32_MAX, | |
4beda316 | 3792 | verity.data_path ? 0 : LO_FLAGS_PARTSCAN, |
7f52206a | 3793 | LOCK_SH, |
4beda316 LB |
3794 | &loop_device); |
3795 | if (r < 0) | |
3796 | return log_debug_errno(r, "Failed to create loop device for image: %m"); | |
3797 | ||
bad31660 YW |
3798 | r = dissect_loop_device( |
3799 | loop_device, | |
4beda316 LB |
3800 | &verity, |
3801 | options, | |
84be0c71 | 3802 | image_policy, |
4beda316 LB |
3803 | dissect_image_flags, |
3804 | &dissected_image); | |
3805 | /* No partition table? Might be a single-filesystem image, try again */ | |
3806 | if (!verity.data_path && r == -ENOPKG) | |
bad31660 YW |
3807 | r = dissect_loop_device( |
3808 | loop_device, | |
4beda316 LB |
3809 | &verity, |
3810 | options, | |
84be0c71 | 3811 | image_policy, |
75dc190d | 3812 | dissect_image_flags | DISSECT_IMAGE_NO_PARTITION_TABLE, |
4beda316 LB |
3813 | &dissected_image); |
3814 | if (r < 0) | |
3815 | return log_debug_errno(r, "Failed to dissect image: %m"); | |
3816 | ||
88b3300f LP |
3817 | r = dissected_image_load_verity_sig_partition(dissected_image, loop_device->fd, &verity); |
3818 | if (r < 0) | |
3819 | return r; | |
3820 | ||
4beda316 LB |
3821 | r = dissected_image_decrypt( |
3822 | dissected_image, | |
3823 | NULL, | |
3824 | &verity, | |
e330f97a | 3825 | dissect_image_flags); |
4beda316 LB |
3826 | if (r < 0) |
3827 | return log_debug_errno(r, "Failed to decrypt dissected image: %m"); | |
3828 | ||
3829 | r = mkdir_p_label(dest, 0755); | |
3830 | if (r < 0) | |
3831 | return log_debug_errno(r, "Failed to create destination directory %s: %m", dest); | |
3832 | r = umount_recursive(dest, 0); | |
3833 | if (r < 0) | |
3834 | return log_debug_errno(r, "Failed to umount under destination directory %s: %m", dest); | |
3835 | ||
21b61b1d | 3836 | r = dissected_image_mount(dissected_image, dest, UID_INVALID, UID_INVALID, dissect_image_flags); |
4beda316 LB |
3837 | if (r < 0) |
3838 | return log_debug_errno(r, "Failed to mount image: %m"); | |
3839 | ||
41bc4849 LP |
3840 | r = loop_device_flock(loop_device, LOCK_UN); |
3841 | if (r < 0) | |
3842 | return log_debug_errno(r, "Failed to unlock loopback device: %m"); | |
3843 | ||
93f59701 LB |
3844 | /* If we got os-release values from the caller, then we need to match them with the image's |
3845 | * extension-release.d/ content. Return -EINVAL if there's any mismatch. | |
3846 | * First, check the distro ID. If that matches, then check the new SYSEXT_LEVEL value if | |
37361f46 LB |
3847 | * available, or else fallback to VERSION_ID. If neither is present (eg: rolling release), |
3848 | * then a simple match on the ID will be performed. */ | |
8b2dcbbd | 3849 | if (required_host_os_release_id) { |
93f59701 LB |
3850 | _cleanup_strv_free_ char **extension_release = NULL; |
3851 | ||
d30d86b7 YW |
3852 | assert(!isempty(required_host_os_release_id)); |
3853 | ||
b60e0f57 | 3854 | r = load_extension_release_pairs(dest, IMAGE_SYSEXT, dissected_image->image_name, relax_extension_release_check, &extension_release); |
93f59701 LB |
3855 | if (r < 0) |
3856 | return log_debug_errno(r, "Failed to parse image %s extension-release metadata: %m", dissected_image->image_name); | |
3857 | ||
3858 | r = extension_release_validate( | |
60c5f700 LP |
3859 | dissected_image->image_name, |
3860 | required_host_os_release_id, | |
3861 | required_host_os_release_version_id, | |
3862 | required_host_os_release_sysext_level, | |
3863 | required_sysext_scope, | |
30dfe035 | 3864 | extension_release, |
3865 | IMAGE_SYSEXT); | |
93f59701 LB |
3866 | if (r == 0) |
3867 | return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Image %s extension-release metadata does not match the root's", dissected_image->image_name); | |
3868 | if (r < 0) | |
3869 | return log_debug_errno(r, "Failed to compare image %s extension-release metadata with the root's os-release: %m", dissected_image->image_name); | |
3870 | } | |
3871 | ||
3044d343 YW |
3872 | r = dissected_image_relinquish(dissected_image); |
3873 | if (r < 0) | |
3874 | return log_debug_errno(r, "Failed to relinquish dissected image: %m"); | |
4beda316 LB |
3875 | |
3876 | return 0; | |
3877 | } |