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