| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <sys/file.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "blockdev-util.h" |
| 7 |
#include "device-util.h" |
| 8 |
#include "dissect-image.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "hexdecoct.h" |
| 11 |
#include "image-policy.h" |
| 12 |
#include "initrd-util.h" |
| 13 |
#include "loop-util.h" |
| 14 |
#include "proc-cmdline.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "strv.h" |
| 17 |
#include "udev-builtin.h" |
| 18 |
|
| 19 |
static ImagePolicy *arg_image_policy = NULL; |
| 20 |
static VeritySettings arg_verity_settings = VERITY_SETTINGS_DEFAULT; |
| 21 |
static ImageFilter *arg_image_filter = NULL; |
| 22 |
|
| 23 |
static int acquire_image_policy(ImagePolicy **ret) { |
| 24 |
int r; |
| 25 |
|
| 26 |
assert(ret); |
| 27 |
|
| 28 |
_cleanup_free_ char *value = NULL; |
| 29 |
r = proc_cmdline_get_key("systemd.image_policy", /* flags= */ 0, &value); |
| 30 |
if (r < 0) |
| 31 |
return log_error_errno(r, "Failed to read systemd.image_policy= kernel command line switch: %m"); |
| 32 |
if (r == 0) { |
| 33 |
*ret = NULL; |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
r = image_policy_from_string(value, /* graceful= */ false, ret); |
| 38 |
if (r < 0) |
| 39 |
return log_error_errno(r, "Failed to parse image policy '%s': %m", value); |
| 40 |
|
| 41 |
if (DEBUG_LOGGING) { |
| 42 |
_cleanup_free_ char *s = NULL; |
| 43 |
|
| 44 |
(void) image_policy_to_string(*ret, /* simplify= */ true, &s); |
| 45 |
log_debug("Loaded image policy: %s", strna(s)); |
| 46 |
} |
| 47 |
|
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
static int acquire_verity_settings(VeritySettings *ret) { |
| 52 |
_cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
| 53 |
int r; |
| 54 |
|
| 55 |
assert(ret); |
| 56 |
|
| 57 |
_cleanup_free_ char *h = NULL; |
| 58 |
r = proc_cmdline_get_key("roothash", /* flags= */ 0, &h); |
| 59 |
if (r < 0) |
| 60 |
return log_error_errno(r, "Failed to read roothash= kernel command line switch: %m"); |
| 61 |
if (r > 0) |
| 62 |
verity.designator = PARTITION_ROOT; |
| 63 |
|
| 64 |
_cleanup_free_ char *uh = NULL; |
| 65 |
r = proc_cmdline_get_key("usrhash", /* flags= */ 0, &uh); |
| 66 |
if (r < 0) |
| 67 |
return log_error_errno(r, "Failed to read usrhash= kernel command line switch: %m"); |
| 68 |
if (r > 0) { |
| 69 |
if (h) |
| 70 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Both roothash= and usrhash= specified, refusing."); |
| 71 |
|
| 72 |
h = TAKE_PTR(uh); |
| 73 |
verity.designator = PARTITION_USR; |
| 74 |
} |
| 75 |
|
| 76 |
if (h) { |
| 77 |
r = unhexmem(h, &verity.root_hash.iov_base, &verity.root_hash.iov_len); |
| 78 |
if (r < 0) |
| 79 |
return log_error_errno(r, "Failed to parse root hash from kernel command line switch: %m"); |
| 80 |
} |
| 81 |
|
| 82 |
*ret = TAKE_GENERIC(verity, VeritySettings, VERITY_SETTINGS_DEFAULT); |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int acquire_image_filter(ImageFilter **ret) { |
| 87 |
int r; |
| 88 |
|
| 89 |
assert(ret); |
| 90 |
|
| 91 |
_cleanup_free_ char *value = NULL; |
| 92 |
r = proc_cmdline_get_key("systemd.image_filter", /* flags= */ 0, &value); |
| 93 |
if (r < 0) |
| 94 |
return log_error_errno(r, "Failed to read systemd.image_filter= kernel command line switch: %m"); |
| 95 |
if (r == 0) { |
| 96 |
*ret = NULL; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
r = image_filter_parse(value, ret); |
| 101 |
if (r < 0) |
| 102 |
return log_error_errno(r, "Failed to parse image filter '%s': %m", value); |
| 103 |
|
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
static int verb_probe(UdevEvent *event, sd_device *dev) { |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(event); |
| 111 |
assert(dev); |
| 112 |
|
| 113 |
/* This is invoked on 'main' block devices to probe the partition table. We will generate some |
| 114 |
* properties with general image information, and then a bunch of properties for each partition, with |
| 115 |
* the partition index in the variable name. These fields will be copied into partition block devices |
| 116 |
* when the dissect_image builtin is later called with the "copy" verb, i.e. in verb_copy() below. */ |
| 117 |
|
| 118 |
const char *devnode; |
| 119 |
r = sd_device_get_devname(dev, &devnode); |
| 120 |
if (r < 0) |
| 121 |
return log_device_debug_errno(dev, r, "Failed to get device node: %m"); |
| 122 |
if (block_device_is_whole_disk(dev) <= 0) { |
| 123 |
log_device_debug(dev, "Must be invoked on whole block device (was invoked in '%s), ignoring.", devnode); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
uint64_t diskseq; |
| 128 |
r = sd_device_get_diskseq(dev, &diskseq); |
| 129 |
if (r < 0) |
| 130 |
return log_device_debug_errno(dev, r, "Failed to get diskseq of '%s': %m", devnode); |
| 131 |
|
| 132 |
r = blockdev_partscan_enabled(dev); |
| 133 |
if (r < 0) |
| 134 |
return log_device_debug_errno(dev, r, "Failed to determine if block device '%s' supports partitions: %m", devnode); |
| 135 |
if (r == 0) { |
| 136 |
log_device_debug(dev, "Invoked on block device '%s' that lacks partition scanning, ignoring.", devnode); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
_cleanup_(loop_device_unrefp) LoopDevice *loop = NULL; |
| 141 |
r = loop_device_open(dev, O_RDONLY, LOCK_SH, &loop); |
| 142 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 143 |
log_device_debug(dev, "Device absent while opening block device '%s', ignoring.", devnode); |
| 144 |
return 0; |
| 145 |
} |
| 146 |
if (r < 0) |
| 147 |
return log_device_debug_errno(dev, r, "Failed to open block device '%s: %m", devnode); |
| 148 |
|
| 149 |
const ImagePolicy *image_policy = arg_image_policy ?: &image_policy_host; |
| 150 |
_cleanup_(dissected_image_unrefp) DissectedImage *image = NULL; |
| 151 |
r = dissect_loop_device( |
| 152 |
loop, |
| 153 |
&arg_verity_settings, |
| 154 |
/* mount_options= */ NULL, |
| 155 |
image_policy, |
| 156 |
arg_image_filter, |
| 157 |
DISSECT_IMAGE_READ_ONLY| |
| 158 |
DISSECT_IMAGE_GPT_ONLY| |
| 159 |
DISSECT_IMAGE_USR_NO_ROOT| |
| 160 |
DISSECT_IMAGE_ALLOW_EMPTY, |
| 161 |
&image); |
| 162 |
if (r == -ERFKILL && !in_initrd()) { |
| 163 |
/* If we transitioned into the main system and we couldn't dissect the image with the full |
| 164 |
* policy, let's see if it works if we set the policies for /usr/ and the root fs out of the |
| 165 |
* policy. After all, we already made our choices, there's no point in insisting on the |
| 166 |
* policy here. */ |
| 167 |
|
| 168 |
static const PartitionDesignator ignore_designators[] = { |
| 169 |
PARTITION_ROOT, |
| 170 |
PARTITION_ROOT_VERITY, |
| 171 |
PARTITION_ROOT_VERITY_SIG, |
| 172 |
PARTITION_USR, |
| 173 |
PARTITION_USR_VERITY, |
| 174 |
PARTITION_USR_VERITY_SIG, |
| 175 |
}; |
| 176 |
|
| 177 |
_cleanup_(image_policy_freep) ImagePolicy *image_policy_mangled = NULL; |
| 178 |
r = image_policy_ignore_designators( |
| 179 |
image_policy, |
| 180 |
ignore_designators, |
| 181 |
ELEMENTSOF(ignore_designators), |
| 182 |
&image_policy_mangled); |
| 183 |
if (r < 0) |
| 184 |
return log_device_debug_errno(dev, r, "Failed to remove root/usr partitions from image policy: %m"); |
| 185 |
|
| 186 |
if (image_policy_equal(image_policy, image_policy_mangled)) |
| 187 |
r = -ERFKILL; /* restore original error, if this didn't change anything */ |
| 188 |
else { |
| 189 |
if (DEBUG_LOGGING) { |
| 190 |
_cleanup_free_ char *a = NULL, *b = NULL; |
| 191 |
|
| 192 |
(void) image_policy_to_string(image_policy, /* simplify= */ false, &a); |
| 193 |
(void) image_policy_to_string(image_policy_mangled, /* simplify= */ false, &b); |
| 194 |
|
| 195 |
log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERFKILL), "Couldn't dissect block device with regular policy '%s', retrying with policy where root/usr are set to ignore '%s'.", a, b); |
| 196 |
} |
| 197 |
|
| 198 |
r = dissect_loop_device( |
| 199 |
loop, |
| 200 |
&arg_verity_settings, |
| 201 |
/* mount_options= */ NULL, |
| 202 |
image_policy_mangled, |
| 203 |
arg_image_filter, |
| 204 |
DISSECT_IMAGE_READ_ONLY| |
| 205 |
DISSECT_IMAGE_GPT_ONLY| |
| 206 |
DISSECT_IMAGE_USR_NO_ROOT| |
| 207 |
DISSECT_IMAGE_ALLOW_EMPTY, |
| 208 |
&image); |
| 209 |
} |
| 210 |
} |
| 211 |
if (IN_SET(r, -ENOPKG, -ENOMSG, -ENXIO, -ENOTUNIQ)) { |
| 212 |
log_device_debug_errno(dev, r, "Device does not carry a GPT disk label with suitable partitions, ignoring."); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
if (r == -ERFKILL) { |
| 216 |
log_device_debug_errno(dev, r, "Device carries GPT disk label that doesn't match our image policy, ignoring."); |
| 217 |
return 0; |
| 218 |
} |
| 219 |
if (r < 0) |
| 220 |
return log_device_debug_errno(dev, r, "Failed to dissect disk image: %m"); |
| 221 |
|
| 222 |
/* Let's try to load verity data from the image now, so that we can attach it to the device via udev |
| 223 |
* properties */ |
| 224 |
_cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
| 225 |
r = verity_settings_copy(&verity, &arg_verity_settings); |
| 226 |
if (r < 0) |
| 227 |
return r; |
| 228 |
|
| 229 |
r = dissected_image_load_verity_sig_partition(image, loop->fd, &verity); |
| 230 |
if (r < 0) |
| 231 |
return log_device_debug_errno(dev, r, "Failed to load verity signature data from image: %m"); |
| 232 |
|
| 233 |
r = dissected_image_guess_verity_roothash(image, &verity); |
| 234 |
if (r < 0) |
| 235 |
return log_device_debug_errno(dev, r, "Failed to guess root hash from image: %m"); |
| 236 |
|
| 237 |
/* Marker that we determined this to be a suitable image */ |
| 238 |
(void) udev_builtin_add_property(event, "ID_DISSECT_IMAGE", "1"); |
| 239 |
|
| 240 |
/* Output the primary architecture this image is intended for */ |
| 241 |
Architecture a = dissected_image_architecture(image); |
| 242 |
if (a >= 0) |
| 243 |
(void) udev_builtin_add_property(event, "ID_DISSECT_IMAGE_ARCHITECTURE", architecture_to_string(a)); |
| 244 |
|
| 245 |
/* And now output the intended designator and architecture (if it applies) for all partitions we |
| 246 |
* found and think belong to this system */ |
| 247 |
FOREACH_ELEMENT(p, image->partitions) { |
| 248 |
PartitionDesignator d = p - image->partitions; |
| 249 |
if (!p->found) |
| 250 |
continue; |
| 251 |
|
| 252 |
assert(p->partno > 0); |
| 253 |
|
| 254 |
/* Indicate designator for partition */ |
| 255 |
_cleanup_free_ char *df = NULL; |
| 256 |
if (asprintf(&df, "ID_DISSECT_PART%i_DESIGNATOR", p->partno) < 0) |
| 257 |
return log_oom_debug(); |
| 258 |
(void) udev_builtin_add_property(event, df, partition_designator_to_string(d)); |
| 259 |
|
| 260 |
if (p->architecture >= 0) { |
| 261 |
_cleanup_free_ char *f = NULL; |
| 262 |
if (asprintf(&f, "ID_DISSECT_PART%i_ARCHITECTURE", p->partno) < 0) |
| 263 |
return log_oom_debug(); |
| 264 |
(void) udev_builtin_add_property(event, f, architecture_to_string(p->architecture)); |
| 265 |
} |
| 266 |
|
| 267 |
/* Indicate whether this partition has verity protection */ |
| 268 |
PartitionDesignator dv = partition_verity_hash_of(d); |
| 269 |
if (dv >= 0 && image->partitions[dv].found) { |
| 270 |
/* Add one property that indicates as a boolean whether Verity is available at all for this */ |
| 271 |
_cleanup_free_ char *f = NULL; |
| 272 |
if (asprintf(&f, "ID_DISSECT_PART%i_HAS_VERITY", p->partno) < 0) |
| 273 |
return log_oom_debug(); |
| 274 |
|
| 275 |
(void) udev_builtin_add_property(event, f, "1"); |
| 276 |
|
| 277 |
/* Add a second property that indicates where the block device is found with the |
| 278 |
* Verity data. We maintain this in an independent property, since Verity data might |
| 279 |
* be available from other sources too, not just block devices, and we'd like to keep |
| 280 |
* the props somewhat open for that. */ |
| 281 |
f = mfree(f); |
| 282 |
if (asprintf(&f, "ID_DISSECT_PART%i_VERITY_DEVICE", p->partno) < 0) |
| 283 |
return log_oom_debug(); |
| 284 |
|
| 285 |
(void) udev_builtin_add_propertyf(event, f, "/dev/disk/by-diskseq/%" PRIu64 "-part%i", diskseq, image->partitions[dv].partno); |
| 286 |
} |
| 287 |
|
| 288 |
dv = partition_verity_sig_of(d); |
| 289 |
if (dv >= 0 && image->partitions[dv].found) { |
| 290 |
_cleanup_free_ char *f = NULL; |
| 291 |
if (asprintf(&f, "ID_DISSECT_PART%i_HAS_VERITY_SIG", p->partno) < 0) |
| 292 |
return log_oom_debug(); |
| 293 |
|
| 294 |
(void) udev_builtin_add_property(event, f, "1"); |
| 295 |
|
| 296 |
f = mfree(f); |
| 297 |
if (asprintf(&f, "ID_DISSECT_PART%i_VERITY_SIG_DEVICE", p->partno) < 0) |
| 298 |
return log_oom_debug(); |
| 299 |
|
| 300 |
(void) udev_builtin_add_propertyf(event, f, "/dev/disk/by-diskseq/%" PRIu64 "-part%i", diskseq, image->partitions[dv].partno); |
| 301 |
} |
| 302 |
|
| 303 |
if (d == verity.designator) { |
| 304 |
if (iovec_is_set(&verity.root_hash)) { |
| 305 |
_cleanup_free_ char *f = NULL; |
| 306 |
if (asprintf(&f, "ID_DISSECT_PART%i_ROOTHASH", p->partno) < 0) |
| 307 |
return log_oom_debug(); |
| 308 |
|
| 309 |
_cleanup_free_ char *h = hexmem(verity.root_hash.iov_base, verity.root_hash.iov_len); |
| 310 |
if (!h) |
| 311 |
return log_oom_debug(); |
| 312 |
|
| 313 |
(void) udev_builtin_add_property(event, f, h); |
| 314 |
} |
| 315 |
|
| 316 |
if (iovec_is_set(&verity.root_hash_sig)) { |
| 317 |
_cleanup_free_ char *f = NULL; |
| 318 |
if (asprintf(&f, "ID_DISSECT_PART%i_ROOTHASH_SIG", p->partno) < 0) |
| 319 |
return log_oom_debug(); |
| 320 |
|
| 321 |
_cleanup_free_ char *h = NULL; |
| 322 |
if (base64mem(verity.root_hash_sig.iov_base, verity.root_hash_sig.iov_len, &h) < 0) |
| 323 |
return log_oom_debug(); |
| 324 |
|
| 325 |
(void) udev_builtin_add_property(event, f, h); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
static int verb_copy(UdevEvent *event, sd_device *dev) { |
| 334 |
int r; |
| 335 |
|
| 336 |
assert(event); |
| 337 |
assert(dev); |
| 338 |
|
| 339 |
/* This is called for the partition block devices, and will copy the per-partition properties we |
| 340 |
* probed on the main block device into the partition device */ |
| 341 |
|
| 342 |
const char *devnode; |
| 343 |
r = sd_device_get_devname(dev, &devnode); |
| 344 |
if (r < 0) |
| 345 |
return log_device_debug_errno(dev, r, "Failed to get device node: %m"); |
| 346 |
|
| 347 |
r = device_is_subsystem_devtype(dev, "block", "partition"); |
| 348 |
if (r < 0) |
| 349 |
return log_device_debug_errno(dev, r, "Failed to check if the device '%s' is a partition, refusing: %m", devnode); |
| 350 |
if (r == 0) |
| 351 |
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invoked on non-partition device '%s', refusing.", devnode); |
| 352 |
|
| 353 |
sd_device *parent; |
| 354 |
r = sd_device_get_parent(dev, &parent); |
| 355 |
if (r < 0) |
| 356 |
return log_error_errno(r, "Failed to get parent of device '%s': %m", devnode); |
| 357 |
|
| 358 |
r = device_is_subsystem_devtype(parent, "block", "disk"); |
| 359 |
if (r < 0) |
| 360 |
return log_device_debug_errno(dev, r, "Failed to check if the parent of block device '%s' is a whole block device, refusing: %m", devnode); |
| 361 |
if (r == 0) |
| 362 |
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Parent of block device '%s' is not a whole block device, refusing.", devnode); |
| 363 |
|
| 364 |
const char *partn; |
| 365 |
r = sd_device_get_property_value(dev, "PARTN", &partn); |
| 366 |
if (r < 0) |
| 367 |
return log_device_debug_errno(dev, r, "Failed to get partition number of partition block device '%s': %m", devnode); |
| 368 |
|
| 369 |
FOREACH_STRING(f, |
| 370 |
"_DESIGNATOR", "_ARCHITECTURE", |
| 371 |
"_HAS_VERITY", "_HAS_VERITY_SIG", |
| 372 |
"_ROOTHASH", "_ROOTHASH_SIG", |
| 373 |
"_VERITY_DEVICE", "_VERITY_SIG_DEVICE") { |
| 374 |
|
| 375 |
/* The property on the parent device contains the partition number */ |
| 376 |
_cleanup_free_ char *p = strjoin("ID_DISSECT_PART", partn, f); |
| 377 |
if (!p) |
| 378 |
return log_oom_debug(); |
| 379 |
|
| 380 |
const char *v; |
| 381 |
r = sd_device_get_property_value(parent, p, &v); |
| 382 |
if (r == -ENOENT) |
| 383 |
continue; |
| 384 |
if (r < 0) |
| 385 |
return log_device_debug_errno(dev, r, "Failed to get '%s' property of parent of '%s': %m", p, devnode); |
| 386 |
|
| 387 |
/* When we copy this property to the partition we drop the partition number, so that we have |
| 388 |
* a constant field name */ |
| 389 |
_cleanup_free_ char *c = strjoin("ID_DISSECT_PART", f); |
| 390 |
if (!c) |
| 391 |
return log_oom_debug(); |
| 392 |
|
| 393 |
(void) udev_builtin_add_property(event, c, v); |
| 394 |
} |
| 395 |
|
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
static int builtin_dissect_image(UdevEvent *event, int argc, char *argv[]) { |
| 400 |
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); |
| 401 |
|
| 402 |
if (argc != 2) |
| 403 |
return log_device_warning_errno( |
| 404 |
dev, SYNTHETIC_ERRNO(EINVAL), "%s: expected single argument.", argv[0]); |
| 405 |
|
| 406 |
if (streq(argv[1], "probe")) |
| 407 |
return verb_probe(event, dev); |
| 408 |
if (streq(argv[1], "copy")) |
| 409 |
return verb_copy(event, dev); |
| 410 |
|
| 411 |
return log_device_warning_errno( |
| 412 |
dev, SYNTHETIC_ERRNO(EINVAL), "%s: unknown verb '%s'", argv[0], argv[1]); |
| 413 |
} |
| 414 |
|
| 415 |
static int builtin_dissect_image_init(void) { |
| 416 |
int r; |
| 417 |
|
| 418 |
r = acquire_image_policy(&arg_image_policy); |
| 419 |
if (r < 0) |
| 420 |
return r; |
| 421 |
|
| 422 |
r = acquire_verity_settings(&arg_verity_settings); |
| 423 |
if (r < 0) |
| 424 |
return r; |
| 425 |
|
| 426 |
r = acquire_image_filter(&arg_image_filter); |
| 427 |
if (r < 0) |
| 428 |
return r; |
| 429 |
|
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
static void builtin_dissect_image_exit(void) { |
| 434 |
arg_image_policy = image_policy_free(arg_image_policy); |
| 435 |
verity_settings_done(&arg_verity_settings); |
| 436 |
arg_image_filter = image_filter_free(arg_image_filter); |
| 437 |
} |
| 438 |
|
| 439 |
const UdevBuiltin udev_builtin_dissect_image = { |
| 440 |
.name = "dissect_image", |
| 441 |
.cmd = builtin_dissect_image, |
| 442 |
.init = builtin_dissect_image_init, |
| 443 |
.exit = builtin_dissect_image_exit, |
| 444 |
.help = "Dissect Disk Images", |
| 445 |
.run_once = true, |
| 446 |
}; |