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