| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "boot-secret.h" |
| 4 |
#include "console.h" |
| 5 |
#include "cpio.h" |
| 6 |
#include "device-path-util.h" |
| 7 |
#include "devicetree.h" |
| 8 |
#include "efi-efivars.h" |
| 9 |
#include "efi-log.h" |
| 10 |
#include "efi-string.h" |
| 11 |
#include "export-vars.h" |
| 12 |
#include "graphics.h" |
| 13 |
#include "initrd.h" |
| 14 |
#include "iovec-util.h" |
| 15 |
#include "linux.h" |
| 16 |
#include "measure.h" |
| 17 |
#include "measure-smbios.h" |
| 18 |
#include "memory-util.h" |
| 19 |
#include "part-discovery.h" |
| 20 |
#include "pe.h" |
| 21 |
#include "proto/shell-parameters.h" |
| 22 |
#include "random-seed.h" |
| 23 |
#include "sbat.h" |
| 24 |
#include "secure-boot.h" |
| 25 |
#include "shim.h" |
| 26 |
#include "smbios.h" |
| 27 |
#include "splash.h" |
| 28 |
#include "tpm2-pcr.h" |
| 29 |
#include "uki.h" |
| 30 |
#include "url-discovery.h" |
| 31 |
#include "util.h" |
| 32 |
#include "version.h" |
| 33 |
#include "vmm.h" |
| 34 |
|
| 35 |
/* The list of initrds we combine into one, in the order we want to merge them */ |
| 36 |
enum { |
| 37 |
INITRD_UCODE, /* Part of the PE binary */ |
| 38 |
INITRD_PREVIOUS, /* initrd already configured via the EFI protocol before we were invoked */ |
| 39 |
INITRD_BASE, /* Part of the PE binary */ |
| 40 |
INITRD_CREDENTIAL, |
| 41 |
INITRD_GLOBAL_CREDENTIAL, |
| 42 |
INITRD_SYSEXT, |
| 43 |
INITRD_GLOBAL_SYSEXT, |
| 44 |
INITRD_CONFEXT, |
| 45 |
INITRD_GLOBAL_CONFEXT, |
| 46 |
INITRD_PCRSIG, |
| 47 |
INITRD_PCRPKEY, |
| 48 |
INITRD_OSREL, |
| 49 |
INITRD_PROFILE, |
| 50 |
INITRD_BOOT_SECRET, |
| 51 |
_INITRD_MAX, |
| 52 |
}; |
| 53 |
|
| 54 |
#define INITRD_IS_STATIC(idx) IN_SET(idx, INITRD_UCODE, INITRD_BASE) |
| 55 |
|
| 56 |
/* magic string to find in the binary image */ |
| 57 |
DECLARE_NOALLOC_SECTION(".sdmagic", "#### LoaderInfo: systemd-stub " GIT_VERSION " ####"); |
| 58 |
|
| 59 |
DECLARE_SBAT_PADDED(SBAT_STUB_SECTION_TEXT); |
| 60 |
|
| 61 |
static char16_t* pe_section_to_str16( |
| 62 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 63 |
const PeSectionVector *section) { |
| 64 |
|
| 65 |
assert(loaded_image); |
| 66 |
assert(section); |
| 67 |
|
| 68 |
if (!PE_SECTION_VECTOR_IS_SET(section)) |
| 69 |
return NULL; |
| 70 |
|
| 71 |
return xstrn8_to_16((const char *) loaded_image->ImageBase + section->memory_offset, section->memory_size); |
| 72 |
} |
| 73 |
|
| 74 |
static char *pe_section_to_str8( |
| 75 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 76 |
const PeSectionVector *section) { |
| 77 |
|
| 78 |
assert(loaded_image); |
| 79 |
assert(section); |
| 80 |
|
| 81 |
if (!PE_SECTION_VECTOR_IS_SET(section)) |
| 82 |
return NULL; |
| 83 |
|
| 84 |
return xstrndup8((const char *)loaded_image->ImageBase + section->memory_offset, section->memory_size); |
| 85 |
} |
| 86 |
|
| 87 |
static void combine_measured_flag(int *value, int measured) { |
| 88 |
assert(value); |
| 89 |
|
| 90 |
/* Combine the "measured" flag in a sensible way: if we haven't measured anything yet, the first |
| 91 |
* write is taken as is. Later writes can only turn off the flag, never on again. Or in other words, |
| 92 |
* we eventually want to return true iff we really measured *everything* there was to measure. |
| 93 |
* |
| 94 |
* Reminder how the "measured" flag actually works: |
| 95 |
* > 0 → something was measured |
| 96 |
* == 0 → there was something to measure but we didn't (because no TPM or so) |
| 97 |
* < 0 → nothing has been submitted for measurement so far |
| 98 |
*/ |
| 99 |
|
| 100 |
if (measured < 0) |
| 101 |
return; |
| 102 |
|
| 103 |
*value = *value < 0 ? measured : *value && measured; |
| 104 |
} |
| 105 |
|
| 106 |
static void export_stub_variables(EFI_LOADED_IMAGE_PROTOCOL *loaded_image, unsigned profile) { |
| 107 |
static const uint64_t stub_features = |
| 108 |
EFI_STUB_FEATURE_REPORT_BOOT_PARTITION | /* We set LoaderDevicePartUUID */ |
| 109 |
EFI_STUB_FEATURE_PICK_UP_CREDENTIALS | /* We pick up credentials from the boot partition */ |
| 110 |
EFI_STUB_FEATURE_PICK_UP_SYSEXTS | /* We pick up system extensions from the boot partition */ |
| 111 |
EFI_STUB_FEATURE_PICK_UP_CONFEXTS | /* We pick up configuration extensions from the boot partition */ |
| 112 |
EFI_STUB_FEATURE_THREE_PCRS | /* We can measure kernel image, parameters and sysext */ |
| 113 |
EFI_STUB_FEATURE_RANDOM_SEED | /* We pass a random seed to the kernel */ |
| 114 |
EFI_STUB_FEATURE_CMDLINE_ADDONS | /* We pick up .cmdline addons */ |
| 115 |
EFI_STUB_FEATURE_CMDLINE_SMBIOS | /* We support extending kernel cmdline from SMBIOS Type #11 */ |
| 116 |
EFI_STUB_FEATURE_DEVICETREE_ADDONS | /* We pick up .dtb addons */ |
| 117 |
EFI_STUB_FEATURE_MULTI_PROFILE_UKI | /* We grok the "@1" profile command line argument */ |
| 118 |
EFI_STUB_FEATURE_REPORT_STUB_PARTITION | /* We set StubDevicePartUUID + StubImageIdentifier */ |
| 119 |
EFI_STUB_FEATURE_REPORT_URL | /* We set StubDeviceURL + LoaderDeviceURL */ |
| 120 |
EFI_STUB_FEATURE_SMBIOS_MEASURED | /* We measure SMBIOS data into PCR 1 */ |
| 121 |
0; |
| 122 |
|
| 123 |
assert(loaded_image); |
| 124 |
|
| 125 |
/* add StubInfo (this is one is owned by the stub, hence we unconditionally override this with our |
| 126 |
* own data) */ |
| 127 |
(void) efivar_set_str16(MAKE_GUID_PTR(LOADER), u"StubInfo", u"systemd-stub " GIT_VERSION, 0); |
| 128 |
|
| 129 |
(void) efivar_set_uint64_le(MAKE_GUID_PTR(LOADER), u"StubFeatures", stub_features, 0); |
| 130 |
|
| 131 |
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubProfile", profile, 0); |
| 132 |
|
| 133 |
if (loaded_image->DeviceHandle) { |
| 134 |
_cleanup_free_ char16_t *uuid = disk_get_part_uuid(loaded_image->DeviceHandle); |
| 135 |
if (uuid) |
| 136 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"StubDevicePartUUID", uuid, 0); |
| 137 |
|
| 138 |
_cleanup_free_ char16_t *url = disk_get_url(loaded_image->DeviceHandle); |
| 139 |
if (url) |
| 140 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"StubDeviceURL", url, 0); |
| 141 |
} |
| 142 |
|
| 143 |
if (loaded_image->FilePath) { |
| 144 |
_cleanup_free_ char16_t *s = NULL; |
| 145 |
if (device_path_to_str(loaded_image->FilePath, &s) == EFI_SUCCESS) |
| 146 |
efivar_set_str16(MAKE_GUID_PTR(LOADER), u"StubImageIdentifier", s, 0); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
static bool parse_profile_from_cmdline(char16_t **cmdline, unsigned *ret_profile) { |
| 151 |
assert(cmdline); |
| 152 |
assert(*cmdline); |
| 153 |
assert(ret_profile); |
| 154 |
|
| 155 |
const char16_t *p = *cmdline; |
| 156 |
if (p[0] != '@') |
| 157 |
goto nothing; |
| 158 |
|
| 159 |
uint64_t u; |
| 160 |
const char16_t *tail; |
| 161 |
if (!parse_number16(p + 1, &u, &tail)) |
| 162 |
goto nothing; |
| 163 |
if (u > UINT_MAX) |
| 164 |
goto nothing; |
| 165 |
/* Remove exactly one separating space. No further mangling, in order to not disturb measurements – |
| 166 |
* and thus making prediction harder –, after all we want that people can safely prefix their command |
| 167 |
* lines with a profile without having to be bothered with additional whitespace the command line |
| 168 |
* might already contain. */ |
| 169 |
if (tail[0] == u' ') |
| 170 |
tail++; |
| 171 |
else if (tail[0] != 0) /* If this is neither a space nor the end of the string, it must be something else */ |
| 172 |
goto nothing; |
| 173 |
|
| 174 |
/* Drop prefix */ |
| 175 |
free_and_xstrdup16(cmdline, tail); |
| 176 |
*ret_profile = u; |
| 177 |
return true; |
| 178 |
|
| 179 |
nothing: |
| 180 |
*ret_profile = 0; |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
static bool parse_profile_from_argument(const char16_t *arg, unsigned *ret_profile) { |
| 185 |
assert(arg); |
| 186 |
assert(ret_profile); |
| 187 |
|
| 188 |
if (arg[0] != '@') |
| 189 |
goto nothing; |
| 190 |
|
| 191 |
uint64_t u; |
| 192 |
if (!parse_number16(arg + 1, &u, /* ret_tail= */ NULL)) |
| 193 |
goto nothing; |
| 194 |
|
| 195 |
if (u > UINT_MAX) |
| 196 |
goto nothing; |
| 197 |
|
| 198 |
*ret_profile = u; |
| 199 |
return true; |
| 200 |
|
| 201 |
nothing: |
| 202 |
*ret_profile = 0; |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
static void process_arguments( |
| 207 |
EFI_HANDLE stub_image, |
| 208 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 209 |
unsigned *ret_profile, |
| 210 |
char16_t **ret_cmdline) { |
| 211 |
|
| 212 |
assert(stub_image); |
| 213 |
assert(loaded_image); |
| 214 |
assert(ret_profile); |
| 215 |
assert(ret_cmdline); |
| 216 |
|
| 217 |
/* The UEFI shell registers EFI_SHELL_PARAMETERS_PROTOCOL onto images it runs. This lets us know that |
| 218 |
* LoadOptions starts with the stub binary path which we want to strip off. */ |
| 219 |
EFI_SHELL_PARAMETERS_PROTOCOL *shell; |
| 220 |
if (BS->HandleProtocol(stub_image, MAKE_GUID_PTR(EFI_SHELL_PARAMETERS_PROTOCOL), (void **) &shell) != EFI_SUCCESS) { |
| 221 |
|
| 222 |
if (loaded_image->LoadOptionsSize < sizeof(char16_t)) |
| 223 |
goto nothing; |
| 224 |
|
| 225 |
/* Superficial check to ensure the load options data looks like it might be a printable |
| 226 |
* string. Some Dell and other systems fill in binary data in UEFI entries that are generated |
| 227 |
* by the firmware. The UEFI specification allows this. See |
| 228 |
* https://uefi.org/specs/UEFI/2.10/03_Boot_Manager.html#load-options */ |
| 229 |
for (size_t i = 0; i < loaded_image->LoadOptionsSize / sizeof(char16_t); i++) { |
| 230 |
char16_t c = ((const char16_t *) loaded_image->LoadOptions)[i]; |
| 231 |
if (c == L'\0') |
| 232 |
break; |
| 233 |
if (c <= 0x1F) |
| 234 |
goto nothing; |
| 235 |
} |
| 236 |
|
| 237 |
/* Not running from EFI shell, use entire LoadOptions. Note that LoadOptions is a void*, so |
| 238 |
* it could actually be anything! */ |
| 239 |
char16_t *c = xstrndup16(loaded_image->LoadOptions, loaded_image->LoadOptionsSize / sizeof(char16_t)); |
| 240 |
parse_profile_from_cmdline(&c, ret_profile); |
| 241 |
*ret_cmdline = mangle_stub_cmdline(c); |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
if (shell->Argc <= 1) /* No arguments were provided? Then we fall back to built-in cmdline. */ |
| 246 |
goto nothing; |
| 247 |
|
| 248 |
size_t i = 1; |
| 249 |
|
| 250 |
/* The first argument is possibly an "@5" style profile specifier */ |
| 251 |
i += parse_profile_from_argument(shell->Argv[i], ret_profile); |
| 252 |
|
| 253 |
if (i < shell->Argc) { |
| 254 |
/* Assemble the command line ourselves without our stub path. */ |
| 255 |
*ret_cmdline = xstrdup16(shell->Argv[i++]); |
| 256 |
for (; i < shell->Argc; i++) { |
| 257 |
_cleanup_free_ char16_t *old = *ret_cmdline; |
| 258 |
*ret_cmdline = xasprintf("%ls %ls", old, shell->Argv[i]); |
| 259 |
} |
| 260 |
} else |
| 261 |
*ret_cmdline = NULL; |
| 262 |
|
| 263 |
return; |
| 264 |
|
| 265 |
nothing: |
| 266 |
*ret_profile = 0; |
| 267 |
*ret_cmdline = NULL; |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
static EFI_STATUS load_addons_from_dir( |
| 272 |
EFI_FILE *root, |
| 273 |
const char16_t *prefix, |
| 274 |
char16_t ***items, |
| 275 |
size_t *n_items, |
| 276 |
size_t *n_allocated) { |
| 277 |
|
| 278 |
_cleanup_file_close_ EFI_FILE *extra_dir = NULL; |
| 279 |
_cleanup_free_ EFI_FILE_INFO *dirent = NULL; |
| 280 |
size_t dirent_size = 0; |
| 281 |
EFI_STATUS err; |
| 282 |
|
| 283 |
assert(root); |
| 284 |
assert(prefix); |
| 285 |
assert(items); |
| 286 |
assert(n_items); |
| 287 |
assert(n_allocated); |
| 288 |
|
| 289 |
err = open_directory(root, prefix, &extra_dir); |
| 290 |
if (err == EFI_NOT_FOUND) |
| 291 |
/* No extra subdir, that's totally OK */ |
| 292 |
return EFI_SUCCESS; |
| 293 |
if (err != EFI_SUCCESS) |
| 294 |
return log_error_status(err, "Failed to open addons directory '%ls': %m", prefix); |
| 295 |
|
| 296 |
for (;;) { |
| 297 |
_cleanup_free_ char16_t *d = NULL; |
| 298 |
|
| 299 |
err = readdir(extra_dir, &dirent, &dirent_size); |
| 300 |
if (err != EFI_SUCCESS) |
| 301 |
return log_error_status(err, "Failed to read addons directory of loaded image: %m"); |
| 302 |
if (!dirent) /* End of directory */ |
| 303 |
break; |
| 304 |
|
| 305 |
if (dirent->FileName[0] == '.') |
| 306 |
continue; |
| 307 |
if (FLAGS_SET(dirent->Attribute, EFI_FILE_DIRECTORY)) |
| 308 |
continue; |
| 309 |
if (!is_ascii(dirent->FileName)) |
| 310 |
continue; |
| 311 |
if (strlen16(dirent->FileName) > 255) /* Max filename size on Linux */ |
| 312 |
continue; |
| 313 |
if (!endswith_no_case(dirent->FileName, u".addon.efi")) |
| 314 |
continue; |
| 315 |
|
| 316 |
d = xstrdup16(dirent->FileName); |
| 317 |
|
| 318 |
if (*n_items + 2 > *n_allocated) { |
| 319 |
/* We allocate 16 entries at a time, as a matter of optimization */ |
| 320 |
if (*n_items > (SIZE_MAX / sizeof(uint16_t)) - 16) /* Overflow check, just in case */ |
| 321 |
return log_oom(); |
| 322 |
|
| 323 |
size_t m = *n_items + 16; |
| 324 |
*items = xrealloc(*items, *n_allocated * sizeof(uint16_t *), m * sizeof(uint16_t *)); |
| 325 |
*n_allocated = m; |
| 326 |
} |
| 327 |
|
| 328 |
(*items)[(*n_items)++] = TAKE_PTR(d); |
| 329 |
(*items)[*n_items] = NULL; /* Let's always NUL terminate, to make freeing via strv_free() easy */ |
| 330 |
} |
| 331 |
|
| 332 |
return EFI_SUCCESS; |
| 333 |
} |
| 334 |
|
| 335 |
static void cmdline_append_and_measure_addons( |
| 336 |
char16_t *cmdline_addon, |
| 337 |
char16_t **cmdline_append, |
| 338 |
int *parameters_measured) { |
| 339 |
|
| 340 |
assert(cmdline_append); |
| 341 |
assert(parameters_measured); |
| 342 |
|
| 343 |
if (isempty(cmdline_addon)) |
| 344 |
return; |
| 345 |
|
| 346 |
_cleanup_free_ char16_t *copy = mangle_stub_cmdline(xstrdup16(cmdline_addon)); |
| 347 |
if (isempty(copy)) |
| 348 |
return; |
| 349 |
|
| 350 |
bool m = false; |
| 351 |
(void) tpm_log_load_options(copy, &m); |
| 352 |
combine_measured_flag(parameters_measured, m); |
| 353 |
|
| 354 |
_cleanup_free_ char16_t *tmp = TAKE_PTR(*cmdline_append); |
| 355 |
if (isempty(tmp)) |
| 356 |
*cmdline_append = TAKE_PTR(copy); |
| 357 |
else |
| 358 |
*cmdline_append = xasprintf("%ls %ls", tmp, copy); |
| 359 |
} |
| 360 |
|
| 361 |
typedef struct NamedAddon { |
| 362 |
char16_t *filename; |
| 363 |
struct iovec blob; |
| 364 |
} NamedAddon; |
| 365 |
|
| 366 |
static void named_addon_done(NamedAddon *a) { |
| 367 |
assert(a); |
| 368 |
|
| 369 |
a->filename = mfree(a->filename); |
| 370 |
iovec_done(&a->blob); |
| 371 |
} |
| 372 |
|
| 373 |
static DEFINE_ARRAY_FREE_FUNC(named_addon_free_array, NamedAddon, named_addon_done); |
| 374 |
|
| 375 |
static void install_addon_devicetrees( |
| 376 |
struct devicetree_state *dt_state, |
| 377 |
const NamedAddon *addons, |
| 378 |
size_t n_addons, |
| 379 |
int *parameters_measured) { |
| 380 |
|
| 381 |
EFI_STATUS err; |
| 382 |
|
| 383 |
assert(dt_state); |
| 384 |
assert(addons || n_addons == 0); |
| 385 |
assert(parameters_measured); |
| 386 |
|
| 387 |
FOREACH_ARRAY(a, addons, n_addons) { |
| 388 |
err = devicetree_install_from_memory(dt_state, a->blob.iov_base, a->blob.iov_len); |
| 389 |
if (err != EFI_SUCCESS) { |
| 390 |
log_error_status(err, "Error loading addon devicetree, ignoring: %m"); |
| 391 |
continue; |
| 392 |
} |
| 393 |
|
| 394 |
bool m = false; |
| 395 |
err = tpm_log_tagged_event( |
| 396 |
TPM2_PCR_KERNEL_CONFIG, |
| 397 |
POINTER_TO_PHYSICAL_ADDRESS(a->blob.iov_base), |
| 398 |
a->blob.iov_len, |
| 399 |
DEVICETREE_ADDON_EVENT_TAG_ID, |
| 400 |
a->filename, |
| 401 |
&m); |
| 402 |
if (err != EFI_SUCCESS) |
| 403 |
return (void) log_error_status( |
| 404 |
err, |
| 405 |
"Unable to extend PCR %i with DTB addon '%ls': %m", |
| 406 |
TPM2_PCR_KERNEL_CONFIG, |
| 407 |
a->filename); |
| 408 |
|
| 409 |
combine_measured_flag(parameters_measured, m); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
static inline void iovec_array_extend(struct iovec **arr, size_t *n_arr, struct iovec elem) { |
| 414 |
assert(arr); |
| 415 |
assert(n_arr); |
| 416 |
|
| 417 |
if (!iovec_is_set(&elem)) |
| 418 |
return; |
| 419 |
|
| 420 |
*arr = xrealloc(*arr, *n_arr * sizeof(struct iovec), (*n_arr + 1) * sizeof(struct iovec)); |
| 421 |
(*arr)[(*n_arr)++] = elem; |
| 422 |
} |
| 423 |
|
| 424 |
static void measure_and_append_initrd_addons( |
| 425 |
struct iovec **all_initrds, |
| 426 |
size_t *n_all_initrds, |
| 427 |
const NamedAddon *initrd_addons, |
| 428 |
size_t n_initrd_addons, |
| 429 |
int *sections_measured) { |
| 430 |
|
| 431 |
EFI_STATUS err; |
| 432 |
|
| 433 |
assert(all_initrds); |
| 434 |
assert(n_all_initrds); |
| 435 |
assert(initrd_addons || n_initrd_addons == 0); |
| 436 |
assert(sections_measured); |
| 437 |
|
| 438 |
FOREACH_ARRAY(i, initrd_addons, n_initrd_addons) { |
| 439 |
bool m = false; |
| 440 |
err = tpm_log_tagged_event( |
| 441 |
TPM2_PCR_KERNEL_CONFIG, |
| 442 |
POINTER_TO_PHYSICAL_ADDRESS(i->blob.iov_base), |
| 443 |
i->blob.iov_len, |
| 444 |
INITRD_ADDON_EVENT_TAG_ID, |
| 445 |
i->filename, |
| 446 |
&m); |
| 447 |
if (err != EFI_SUCCESS) |
| 448 |
return (void) log_error_status( |
| 449 |
err, |
| 450 |
"Unable to extend PCR %i with INITRD addon '%ls': %m", |
| 451 |
TPM2_PCR_KERNEL_CONFIG, |
| 452 |
i->filename); |
| 453 |
|
| 454 |
combine_measured_flag(sections_measured, m); |
| 455 |
|
| 456 |
iovec_array_extend(all_initrds, n_all_initrds, i->blob); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
static void measure_and_append_ucode_addons( |
| 461 |
struct iovec **all_initrds, |
| 462 |
size_t *n_all_initrds, |
| 463 |
const NamedAddon *ucode_addons, |
| 464 |
size_t n_ucode_addons, |
| 465 |
int *sections_measured) { |
| 466 |
|
| 467 |
EFI_STATUS err; |
| 468 |
|
| 469 |
assert(all_initrds); |
| 470 |
assert(n_all_initrds); |
| 471 |
assert(ucode_addons || n_ucode_addons == 0); |
| 472 |
assert(sections_measured); |
| 473 |
|
| 474 |
/* Ucode addons need to be measured and copied into all_initrds in reverse order, |
| 475 |
* the kernel takes the first one it finds. */ |
| 476 |
for (ssize_t i = n_ucode_addons - 1; i >= 0; i--) { |
| 477 |
bool m = false; |
| 478 |
err = tpm_log_tagged_event( |
| 479 |
TPM2_PCR_KERNEL_CONFIG, |
| 480 |
POINTER_TO_PHYSICAL_ADDRESS(ucode_addons[i].blob.iov_base), |
| 481 |
ucode_addons[i].blob.iov_len, |
| 482 |
UCODE_ADDON_EVENT_TAG_ID, |
| 483 |
ucode_addons[i].filename, |
| 484 |
&m); |
| 485 |
if (err != EFI_SUCCESS) |
| 486 |
return (void) log_error_status( |
| 487 |
err, |
| 488 |
"Unable to extend PCR %i with UCODE addon '%ls': %m", |
| 489 |
TPM2_PCR_KERNEL_CONFIG, |
| 490 |
ucode_addons[i].filename); |
| 491 |
|
| 492 |
combine_measured_flag(sections_measured, m); |
| 493 |
|
| 494 |
iovec_array_extend(all_initrds, n_all_initrds, ucode_addons[i].blob); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
static void extend_initrds( |
| 499 |
const struct iovec initrds[static _INITRD_MAX], |
| 500 |
struct iovec **all_initrds, |
| 501 |
size_t *n_all_initrds) { |
| 502 |
|
| 503 |
assert(initrds); |
| 504 |
assert(all_initrds); |
| 505 |
assert(n_all_initrds); |
| 506 |
|
| 507 |
FOREACH_ARRAY(i, initrds, _INITRD_MAX) |
| 508 |
iovec_array_extend(all_initrds, n_all_initrds, *i); |
| 509 |
} |
| 510 |
|
| 511 |
static void acquire_previous_initrd(struct iovec initrds[static _INITRD_MAX]) { |
| 512 |
EFI_STATUS err; |
| 513 |
|
| 514 |
/* NB: the assumption here is that any previously installed initrd are measured by whatever |
| 515 |
* registered them, and we just pass them on here. */ |
| 516 |
|
| 517 |
err = initrd_read_previous(initrds + INITRD_PREVIOUS); |
| 518 |
if (err == EFI_NOT_FOUND) |
| 519 |
log_debug_status(err, "No previous initrd registered."); |
| 520 |
else if (err != EFI_SUCCESS) |
| 521 |
log_warning_status(err, "Failed to read previously registered initrd, ignoring."); |
| 522 |
else |
| 523 |
log_debug("Successfully loaded previously registered initrd (%zu bytes).", initrds[INITRD_PREVIOUS].iov_len); |
| 524 |
} |
| 525 |
|
| 526 |
static EFI_STATUS load_addons( |
| 527 |
EFI_HANDLE stub_image, |
| 528 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 529 |
const char16_t *prefix, |
| 530 |
const char *uname, |
| 531 |
char16_t **cmdline, /* Both input+output, extended with new addons we find */ |
| 532 |
NamedAddon **devicetree_addons, /* Ditto */ |
| 533 |
size_t *n_devicetree_addons, |
| 534 |
NamedAddon **initrd_addons, /* Ditto */ |
| 535 |
size_t *n_initrd_addons, |
| 536 |
NamedAddon **ucode_addons, /* Ditto */ |
| 537 |
size_t *n_ucode_addons) { |
| 538 |
|
| 539 |
_cleanup_strv_free_ char16_t **items = NULL; |
| 540 |
_cleanup_file_close_ EFI_FILE *root = NULL; |
| 541 |
size_t n_items = 0, n_allocated = 0; |
| 542 |
EFI_STATUS err; |
| 543 |
|
| 544 |
assert(stub_image); |
| 545 |
assert(loaded_image); |
| 546 |
assert(prefix); |
| 547 |
|
| 548 |
if (!loaded_image->DeviceHandle) |
| 549 |
return EFI_SUCCESS; |
| 550 |
|
| 551 |
err = open_volume(loaded_image->DeviceHandle, &root); |
| 552 |
if (err == EFI_UNSUPPORTED) |
| 553 |
/* Error will be unsupported if the bootloader doesn't implement the file system protocol on |
| 554 |
* its file handles. */ |
| 555 |
return EFI_SUCCESS; |
| 556 |
if (err != EFI_SUCCESS) |
| 557 |
return log_error_status(err, "Unable to open root directory: %m"); |
| 558 |
|
| 559 |
err = load_addons_from_dir(root, prefix, &items, &n_items, &n_allocated); |
| 560 |
if (err != EFI_SUCCESS) |
| 561 |
return err; |
| 562 |
|
| 563 |
if (n_items == 0) |
| 564 |
return EFI_SUCCESS; /* Empty directory */ |
| 565 |
|
| 566 |
/* Now, sort the files we found, to make this uniform and stable (and to ensure the TPM measurements |
| 567 |
* are not dependent on read order) */ |
| 568 |
sort_pointer_array((void**) items, n_items, (compare_pointer_func_t) strcmp16); |
| 569 |
|
| 570 |
for (size_t i = 0; i < n_items; i++) { |
| 571 |
PeSectionVector sections[ELEMENTSOF(unified_sections)] = {}; |
| 572 |
_cleanup_free_ EFI_DEVICE_PATH *addon_path = NULL; |
| 573 |
_cleanup_(unload_imagep) EFI_HANDLE addon = NULL; |
| 574 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_addon = NULL; |
| 575 |
_cleanup_free_ char16_t *addon_spath = NULL; |
| 576 |
|
| 577 |
addon_spath = xasprintf("%ls\\%ls", prefix, items[i]); |
| 578 |
err = make_file_device_path(loaded_image->DeviceHandle, addon_spath, &addon_path); |
| 579 |
if (err != EFI_SUCCESS) |
| 580 |
return log_error_status(err, "Error making device path for %ls: %m", addon_spath); |
| 581 |
|
| 582 |
/* By using shim_load_image, we cover both the case where the PE files are signed with MoK |
| 583 |
* and with DB, and running with or without shim. */ |
| 584 |
err = shim_load_image(stub_image, addon_path, /* boot_policy= */ false, &addon); |
| 585 |
if (err != EFI_SUCCESS) { |
| 586 |
log_error_status(err, |
| 587 |
"Failed to read '%ls' from '%ls', ignoring: %m", |
| 588 |
items[i], |
| 589 |
addon_spath); |
| 590 |
continue; |
| 591 |
} |
| 592 |
|
| 593 |
err = BS->HandleProtocol(addon, |
| 594 |
MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), |
| 595 |
(void **) &loaded_addon); |
| 596 |
if (err != EFI_SUCCESS) |
| 597 |
return log_error_status(err, "Failed to find protocol in %ls: %m", items[i]); |
| 598 |
|
| 599 |
err = pe_memory_locate_sections(loaded_addon->ImageBase, unified_sections, sections); |
| 600 |
if (err != EFI_SUCCESS) { |
| 601 |
log_error_status(err, |
| 602 |
"Unable to locate embedded .cmdline/.dtb/.dtbauto/.initrd/.ucode sections in %ls, ignoring: %m", |
| 603 |
items[i]); |
| 604 |
continue; |
| 605 |
} |
| 606 |
|
| 607 |
if (!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_CMDLINE) && |
| 608 |
!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTB) && |
| 609 |
!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTBAUTO) && |
| 610 |
!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_INITRD) && |
| 611 |
!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_UCODE)) { |
| 612 |
log_debug("No applicable .cmdline/.dtb/.dtbauto/.initrd/.ucode sections found in %ls, ignoring.", |
| 613 |
items[i]); |
| 614 |
continue; |
| 615 |
} |
| 616 |
|
| 617 |
/* We want to enforce that addons are not UKIs, i.e.: they must not embed a kernel. */ |
| 618 |
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_LINUX)) { |
| 619 |
log_error("%ls is a UKI, not an addon, ignoring.", items[i]); |
| 620 |
continue; |
| 621 |
} |
| 622 |
|
| 623 |
/* Also enforce that, in case it is specified, .uname matches as a quick way to allow |
| 624 |
* enforcing compatibility with a specific UKI only */ |
| 625 |
if (uname && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_UNAME) && |
| 626 |
!strneq8(uname, |
| 627 |
(const char *)loaded_addon->ImageBase + sections[UNIFIED_SECTION_UNAME].memory_offset, |
| 628 |
sections[UNIFIED_SECTION_UNAME].memory_size)) { |
| 629 |
log_error(".uname mismatch between %ls and UKI, ignoring", items[i]); |
| 630 |
continue; |
| 631 |
} |
| 632 |
|
| 633 |
if (cmdline && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_CMDLINE)) { |
| 634 |
_cleanup_free_ char16_t *tmp = TAKE_PTR(*cmdline), |
| 635 |
*extra16 = mangle_stub_cmdline(pe_section_to_str16(loaded_addon, sections + UNIFIED_SECTION_CMDLINE)); |
| 636 |
|
| 637 |
*cmdline = xasprintf("%ls%ls%ls", strempty(tmp), isempty(tmp) ? u"" : u" ", extra16); |
| 638 |
} |
| 639 |
|
| 640 |
// FIXME: do we want to do something else here? |
| 641 |
// This should behave exactly as .dtb/.dtbauto in the main UKI |
| 642 |
if (devicetree_addons && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTBAUTO)) { |
| 643 |
*devicetree_addons = xrealloc(*devicetree_addons, |
| 644 |
*n_devicetree_addons * sizeof(NamedAddon), |
| 645 |
(*n_devicetree_addons + 1) * sizeof(NamedAddon)); |
| 646 |
|
| 647 |
(*devicetree_addons)[(*n_devicetree_addons)++] = (NamedAddon) { |
| 648 |
.blob = { |
| 649 |
.iov_base = xmemdup((const uint8_t*) loaded_addon->ImageBase + sections[UNIFIED_SECTION_DTBAUTO].memory_offset, sections[UNIFIED_SECTION_DTBAUTO].memory_size), |
| 650 |
.iov_len = sections[UNIFIED_SECTION_DTBAUTO].memory_size, |
| 651 |
}, |
| 652 |
.filename = xstrdup16(items[i]), |
| 653 |
}; |
| 654 |
} else if (devicetree_addons && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTB)) { |
| 655 |
*devicetree_addons = xrealloc(*devicetree_addons, |
| 656 |
*n_devicetree_addons * sizeof(NamedAddon), |
| 657 |
(*n_devicetree_addons + 1) * sizeof(NamedAddon)); |
| 658 |
|
| 659 |
(*devicetree_addons)[(*n_devicetree_addons)++] = (NamedAddon) { |
| 660 |
.blob = { |
| 661 |
.iov_base = xmemdup((const uint8_t*) loaded_addon->ImageBase + sections[UNIFIED_SECTION_DTB].memory_offset, sections[UNIFIED_SECTION_DTB].memory_size), |
| 662 |
.iov_len = sections[UNIFIED_SECTION_DTB].memory_size, |
| 663 |
}, |
| 664 |
.filename = xstrdup16(items[i]), |
| 665 |
}; |
| 666 |
} |
| 667 |
|
| 668 |
if (initrd_addons && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_INITRD)) { |
| 669 |
*initrd_addons = xrealloc(*initrd_addons, |
| 670 |
*n_initrd_addons * sizeof(NamedAddon), |
| 671 |
(*n_initrd_addons + 1) * sizeof(NamedAddon)); |
| 672 |
(*initrd_addons)[(*n_initrd_addons)++] = (NamedAddon) { |
| 673 |
.blob = { |
| 674 |
.iov_base = xmemdup((const uint8_t*) loaded_addon->ImageBase + sections[UNIFIED_SECTION_INITRD].memory_offset, sections[UNIFIED_SECTION_INITRD].memory_size), |
| 675 |
.iov_len = sections[UNIFIED_SECTION_INITRD].memory_size, |
| 676 |
}, |
| 677 |
.filename = xstrdup16(items[i]), |
| 678 |
}; |
| 679 |
} |
| 680 |
|
| 681 |
if (ucode_addons && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_UCODE)) { |
| 682 |
*ucode_addons = xrealloc(*ucode_addons, |
| 683 |
*n_ucode_addons * sizeof(NamedAddon), |
| 684 |
(*n_ucode_addons + 1) * sizeof(NamedAddon)); |
| 685 |
(*ucode_addons)[(*n_ucode_addons)++] = (NamedAddon) { |
| 686 |
.blob = { |
| 687 |
.iov_base = xmemdup((const uint8_t*) loaded_addon->ImageBase + sections[UNIFIED_SECTION_UCODE].memory_offset, sections[UNIFIED_SECTION_UCODE].memory_size), |
| 688 |
.iov_len = sections[UNIFIED_SECTION_UCODE].memory_size, |
| 689 |
}, |
| 690 |
.filename = xstrdup16(items[i]), |
| 691 |
}; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
return EFI_SUCCESS; |
| 696 |
} |
| 697 |
|
| 698 |
static void refresh_random_seed(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) { |
| 699 |
EFI_STATUS err; |
| 700 |
|
| 701 |
assert(loaded_image); |
| 702 |
|
| 703 |
/* Handle case, where bootloader doesn't support DeviceHandle. */ |
| 704 |
if (!loaded_image->DeviceHandle) |
| 705 |
return; |
| 706 |
|
| 707 |
/* Don't measure again, if sd-boot already initialized the random seed */ |
| 708 |
uint64_t loader_features = 0; |
| 709 |
(void) efivar_get_uint64_le(MAKE_GUID_PTR(LOADER), u"LoaderFeatures", &loader_features); |
| 710 |
if (FLAGS_SET(loader_features, EFI_LOADER_FEATURE_RANDOM_SEED)) |
| 711 |
return; |
| 712 |
|
| 713 |
_cleanup_file_close_ EFI_FILE *esp_dir = NULL; |
| 714 |
err = partition_open(MAKE_GUID_PTR(ESP), loaded_image->DeviceHandle, NULL, &esp_dir); |
| 715 |
if (err != EFI_SUCCESS) /* Non-fatal on failure, so that we still boot without it. */ |
| 716 |
return; |
| 717 |
|
| 718 |
(void) process_random_seed(esp_dir); |
| 719 |
} |
| 720 |
|
| 721 |
static void measure_sections( |
| 722 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 723 |
const PeSectionVector sections[static _UNIFIED_SECTION_MAX], |
| 724 |
int *sections_measured) { |
| 725 |
|
| 726 |
assert(loaded_image); |
| 727 |
assert(sections); |
| 728 |
assert(sections_measured); |
| 729 |
|
| 730 |
/* Measure all "payload" of this PE image into a separate PCR (i.e. where nothing else is written |
| 731 |
* into so far), so that we have one PCR that we can nicely write policies against because it |
| 732 |
* contains all static data of this image, and thus can be easily be pre-calculated. */ |
| 733 |
for (UnifiedSection section = 0; section < _UNIFIED_SECTION_MAX; section++) { |
| 734 |
|
| 735 |
if (!unified_section_measure(section)) /* shall not measure? */ |
| 736 |
continue; |
| 737 |
|
| 738 |
if (!PE_SECTION_VECTOR_IS_SET(sections + section)) /* not found */ |
| 739 |
continue; |
| 740 |
|
| 741 |
/* First measure the name of the section */ |
| 742 |
bool m = false; |
| 743 |
(void) tpm_log_ipl_event_ascii( |
| 744 |
TPM2_PCR_KERNEL_BOOT, |
| 745 |
POINTER_TO_PHYSICAL_ADDRESS(unified_sections[section]), |
| 746 |
strsize8(unified_sections[section]), /* including NUL byte */ |
| 747 |
unified_sections[section], |
| 748 |
&m); |
| 749 |
combine_measured_flag(sections_measured, m); |
| 750 |
|
| 751 |
/* Then measure the data of the section */ |
| 752 |
m = false; |
| 753 |
(void) tpm_log_ipl_event_ascii( |
| 754 |
TPM2_PCR_KERNEL_BOOT, |
| 755 |
POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + sections[section].memory_offset, |
| 756 |
sections[section].memory_size, |
| 757 |
unified_sections[section], |
| 758 |
&m); |
| 759 |
combine_measured_flag(sections_measured, m); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
static void cmdline_append_and_measure_smbios(char16_t **cmdline, int *parameters_measured) { |
| 764 |
assert(cmdline); |
| 765 |
assert(parameters_measured); |
| 766 |
|
| 767 |
/* SMBIOS OEM Strings data is controlled by the host admin and not covered by the VM attestation, so |
| 768 |
* MUST NOT be trusted when in a confidential VM */ |
| 769 |
if (is_confidential_vm()) |
| 770 |
return; |
| 771 |
|
| 772 |
const char *extra = smbios_find_oem_string("io.systemd.stub.kernel-cmdline-extra=", /* after= */ NULL); |
| 773 |
if (!extra) |
| 774 |
return; |
| 775 |
|
| 776 |
_cleanup_free_ char16_t *extra16 = mangle_stub_cmdline(xstr8_to_16(extra)); |
| 777 |
if (isempty(extra16)) |
| 778 |
return; |
| 779 |
|
| 780 |
/* SMBIOS strings are measured in PCR1, but we also want to measure them in our specific PCR12, as |
| 781 |
* firmware-owned PCRs are very difficult to use as they'll contain unpredictable measurements that |
| 782 |
* are not under control of the machine owner. */ |
| 783 |
bool m = false; |
| 784 |
(void) tpm_log_load_options(extra16, &m); |
| 785 |
combine_measured_flag(parameters_measured, m); |
| 786 |
|
| 787 |
_cleanup_free_ char16_t *tmp = TAKE_PTR(*cmdline); |
| 788 |
if (isempty(tmp)) |
| 789 |
*cmdline = TAKE_PTR(extra16); |
| 790 |
else |
| 791 |
*cmdline = xasprintf("%ls %ls", tmp, extra16); |
| 792 |
} |
| 793 |
|
| 794 |
static void initrds_free(struct iovec (*initrds)[_INITRD_MAX]) { |
| 795 |
assert(initrds); |
| 796 |
|
| 797 |
/* Free the non-static initrds, but leave the static (i.e. PE embedded) ones around */ |
| 798 |
|
| 799 |
for (size_t i = 0; i < _INITRD_MAX; i++) |
| 800 |
if (!INITRD_IS_STATIC(i)) |
| 801 |
iovec_done((*initrds) + i); |
| 802 |
} |
| 803 |
|
| 804 |
static void generate_sidecar_initrds( |
| 805 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 806 |
struct iovec initrds[static _INITRD_MAX], |
| 807 |
int *parameters_measured, |
| 808 |
int *sysext_measured, |
| 809 |
int *confext_measured) { |
| 810 |
|
| 811 |
bool m; |
| 812 |
|
| 813 |
assert(loaded_image); |
| 814 |
assert(initrds); |
| 815 |
assert(parameters_measured); |
| 816 |
assert(sysext_measured); |
| 817 |
assert(confext_measured); |
| 818 |
|
| 819 |
if (pack_cpio(loaded_image, |
| 820 |
/* dropin_dir= */ NULL, |
| 821 |
u".cred", |
| 822 |
/* exclude_suffix= */ NULL, |
| 823 |
&cpio_target_credentials, |
| 824 |
u"Credentials initrd", |
| 825 |
initrds + INITRD_CREDENTIAL, |
| 826 |
&m) == EFI_SUCCESS) |
| 827 |
combine_measured_flag(parameters_measured, m); |
| 828 |
|
| 829 |
if (pack_cpio(loaded_image, |
| 830 |
u"\\loader\\credentials", |
| 831 |
u".cred", |
| 832 |
/* exclude_suffix= */ NULL, |
| 833 |
&cpio_target_global_credentials, |
| 834 |
u"Global credentials initrd", |
| 835 |
initrds + INITRD_GLOBAL_CREDENTIAL, |
| 836 |
&m) == EFI_SUCCESS) |
| 837 |
combine_measured_flag(parameters_measured, m); |
| 838 |
|
| 839 |
if (pack_cpio(loaded_image, |
| 840 |
/* dropin_dir= */ NULL, |
| 841 |
u".raw", /* ideally we'd pick up only *.sysext.raw here, but for compat we pick up *.raw instead … */ |
| 842 |
u".confext.raw", /* … but then exclude *.confext.raw again */ |
| 843 |
&cpio_target_sysext, |
| 844 |
u"System extension initrd", |
| 845 |
initrds + INITRD_SYSEXT, |
| 846 |
&m) == EFI_SUCCESS) |
| 847 |
combine_measured_flag(sysext_measured, m); |
| 848 |
|
| 849 |
if (pack_cpio(loaded_image, |
| 850 |
u"\\loader\\extensions", |
| 851 |
u".raw", /* as above */ |
| 852 |
u".confext.raw", |
| 853 |
&cpio_target_global_sysext, |
| 854 |
u"Global system extension initrd", |
| 855 |
initrds + INITRD_GLOBAL_SYSEXT, |
| 856 |
&m) == EFI_SUCCESS) |
| 857 |
combine_measured_flag(sysext_measured, m); |
| 858 |
|
| 859 |
if (pack_cpio(loaded_image, |
| 860 |
/* dropin_dir= */ NULL, |
| 861 |
u".confext.raw", |
| 862 |
/* exclude_suffix= */ NULL, |
| 863 |
&cpio_target_confext, |
| 864 |
u"Configuration extension initrd", |
| 865 |
initrds + INITRD_CONFEXT, |
| 866 |
&m) == EFI_SUCCESS) |
| 867 |
combine_measured_flag(confext_measured, m); |
| 868 |
|
| 869 |
if (pack_cpio(loaded_image, |
| 870 |
u"\\loader\\extensions", |
| 871 |
u".confext.raw", |
| 872 |
/* exclude_suffix= */ NULL, |
| 873 |
&cpio_target_global_confext, |
| 874 |
u"Global configuration extension initrd", |
| 875 |
initrds + INITRD_GLOBAL_CONFEXT, |
| 876 |
&m) == EFI_SUCCESS) |
| 877 |
combine_measured_flag(confext_measured, m); |
| 878 |
} |
| 879 |
|
| 880 |
static void generate_embedded_initrds( |
| 881 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 882 |
const PeSectionVector sections[static _UNIFIED_SECTION_MAX], |
| 883 |
struct iovec initrds[static _INITRD_MAX]) { |
| 884 |
|
| 885 |
static const struct { |
| 886 |
UnifiedSection section; |
| 887 |
size_t initrd_index; |
| 888 |
const char16_t *filename; |
| 889 |
} table[] = { |
| 890 |
/* If the PCR signature was embedded in the PE image, then let's wrap it in a cpio and also pass it |
| 891 |
* to the kernel, so that it can be read from /.extra/tpm2-pcr-signature.json. Note that this section |
| 892 |
* is not measured, neither as raw section (see above), nor as cpio (here), because it is the |
| 893 |
* signature of expected PCR values, i.e. its input are PCR measurements, and hence it shouldn't |
| 894 |
* itself be input for PCR measurements. */ |
| 895 |
{ UNIFIED_SECTION_PCRSIG, INITRD_PCRSIG, u"tpm2-pcr-signature.json" }, |
| 896 |
|
| 897 |
/* If the public key used for the PCR signatures was embedded in the PE image, then let's |
| 898 |
* wrap it in a cpio and also pass it to the kernel, so that it can be read from |
| 899 |
* /.extra/tpm2-pcr-public-key.pem. This section is already measured above, hence we won't |
| 900 |
* measure the cpio. */ |
| 901 |
{ UNIFIED_SECTION_PCRPKEY, INITRD_PCRPKEY, u"tpm2-pcr-public-key.pem" }, |
| 902 |
|
| 903 |
/* If we boot a specific profile, let's place the chosen profile in a file that userspace can |
| 904 |
* make use of this information reasonably. */ |
| 905 |
{ UNIFIED_SECTION_PROFILE, INITRD_PROFILE, u"profile" }, |
| 906 |
|
| 907 |
/* Similar, pass the .osrel section too. Userspace should have this information anyway, but |
| 908 |
* it's so nicely symmetric to the .profile section which we pass around, and who knows, |
| 909 |
* maybe this is useful to some. */ |
| 910 |
{ UNIFIED_SECTION_OSREL, INITRD_OSREL, u"os-release" }, |
| 911 |
}; |
| 912 |
|
| 913 |
assert(loaded_image); |
| 914 |
assert(initrds); |
| 915 |
|
| 916 |
FOREACH_ELEMENT(t, table) { |
| 917 |
if (!PE_SECTION_VECTOR_IS_SET(sections + t->section)) |
| 918 |
continue; |
| 919 |
|
| 920 |
(void) pack_cpio_literal( |
| 921 |
(const uint8_t*) loaded_image->ImageBase + sections[t->section].memory_offset, |
| 922 |
sections[t->section].memory_size, |
| 923 |
&cpio_target_meta, |
| 924 |
t->filename, |
| 925 |
/* tpm_description= */ NULL, |
| 926 |
initrds + t->initrd_index, |
| 927 |
/* ret_measured= */ NULL); |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
static void generate_boot_secret_initrd( |
| 932 |
const uint8_t boot_secret[static BOOT_SECRET_SIZE], |
| 933 |
struct iovec initrds[static _INITRD_MAX]) { |
| 934 |
|
| 935 |
assert(initrds); |
| 936 |
|
| 937 |
/* All zero means: no boot secret acquired */ |
| 938 |
if (memeqzero(boot_secret, BOOT_SECRET_SIZE)) |
| 939 |
return; |
| 940 |
|
| 941 |
(void) pack_cpio_literal( |
| 942 |
boot_secret, |
| 943 |
BOOT_SECRET_SIZE, |
| 944 |
&cpio_target_meta_secret, |
| 945 |
u"boot-secret", |
| 946 |
/* tpm_description= */ NULL, |
| 947 |
initrds + INITRD_BOOT_SECRET, |
| 948 |
/* ret_measured= */ NULL); |
| 949 |
} |
| 950 |
|
| 951 |
static void lookup_embedded_initrds( |
| 952 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 953 |
const PeSectionVector sections[static _UNIFIED_SECTION_MAX], |
| 954 |
struct iovec initrds[static _INITRD_MAX]) { |
| 955 |
|
| 956 |
assert(loaded_image); |
| 957 |
assert(sections); |
| 958 |
assert(initrds); |
| 959 |
|
| 960 |
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_INITRD)) |
| 961 |
initrds[INITRD_BASE] = IOVEC_MAKE( |
| 962 |
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_INITRD].memory_offset, |
| 963 |
sections[UNIFIED_SECTION_INITRD].memory_size); |
| 964 |
|
| 965 |
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_UCODE)) |
| 966 |
initrds[INITRD_UCODE] = IOVEC_MAKE( |
| 967 |
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_UCODE].memory_offset, |
| 968 |
sections[UNIFIED_SECTION_UCODE].memory_size); |
| 969 |
} |
| 970 |
|
| 971 |
static void export_pcr_variables( |
| 972 |
int sections_measured, |
| 973 |
int parameters_measured, |
| 974 |
int sysext_measured, |
| 975 |
int confext_measured) { |
| 976 |
|
| 977 |
/* After we are done with measuring, set an EFI variable that tells userspace this was done |
| 978 |
* successfully, and encode in it which PCR was used. */ |
| 979 |
|
| 980 |
if (sections_measured > 0) |
| 981 |
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrKernelImage", TPM2_PCR_KERNEL_BOOT, 0); |
| 982 |
if (parameters_measured > 0) |
| 983 |
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrKernelParameters", TPM2_PCR_KERNEL_CONFIG, 0); |
| 984 |
if (sysext_measured > 0) |
| 985 |
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrInitRDSysExts", TPM2_PCR_SYSEXTS, 0); |
| 986 |
if (confext_measured > 0) |
| 987 |
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrInitRDConfExts", TPM2_PCR_KERNEL_CONFIG, 0); |
| 988 |
} |
| 989 |
|
| 990 |
static void install_embedded_devicetree( |
| 991 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 992 |
const PeSectionVector sections[static _UNIFIED_SECTION_MAX], |
| 993 |
struct devicetree_state *dt_state) { |
| 994 |
|
| 995 |
EFI_STATUS err; |
| 996 |
|
| 997 |
assert(loaded_image); |
| 998 |
assert(sections); |
| 999 |
assert(dt_state); |
| 1000 |
|
| 1001 |
UnifiedSection section = _UNIFIED_SECTION_MAX; |
| 1002 |
|
| 1003 |
/* Use automatically selected DT if available, otherwise go for "normal" one */ |
| 1004 |
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTBAUTO)) |
| 1005 |
section = UNIFIED_SECTION_DTBAUTO; |
| 1006 |
else if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTB)) |
| 1007 |
section = UNIFIED_SECTION_DTB; |
| 1008 |
else |
| 1009 |
return; |
| 1010 |
|
| 1011 |
err = devicetree_install_from_memory( |
| 1012 |
dt_state, |
| 1013 |
(const uint8_t*) loaded_image->ImageBase + sections[section].memory_offset, |
| 1014 |
sections[section].memory_size); |
| 1015 |
if (err != EFI_SUCCESS) |
| 1016 |
log_error_status(err, "Error loading embedded devicetree, ignoring: %m"); |
| 1017 |
} |
| 1018 |
|
| 1019 |
static void load_all_addons( |
| 1020 |
EFI_HANDLE image, |
| 1021 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 1022 |
const char *uname, |
| 1023 |
char16_t **cmdline_addons, |
| 1024 |
NamedAddon **dt_addons, |
| 1025 |
size_t *n_dt_addons, |
| 1026 |
NamedAddon **initrd_addons, |
| 1027 |
size_t *n_initrd_addons, |
| 1028 |
NamedAddon **ucode_addons, |
| 1029 |
size_t *n_ucode_addons) { |
| 1030 |
|
| 1031 |
EFI_STATUS err; |
| 1032 |
|
| 1033 |
assert(loaded_image); |
| 1034 |
assert(cmdline_addons); |
| 1035 |
assert(dt_addons); |
| 1036 |
assert(n_dt_addons); |
| 1037 |
assert(initrd_addons); |
| 1038 |
assert(n_initrd_addons); |
| 1039 |
assert(ucode_addons); |
| 1040 |
assert(n_ucode_addons); |
| 1041 |
|
| 1042 |
err = load_addons( |
| 1043 |
image, |
| 1044 |
loaded_image, |
| 1045 |
u"\\loader\\addons", |
| 1046 |
uname, |
| 1047 |
cmdline_addons, |
| 1048 |
dt_addons, |
| 1049 |
n_dt_addons, |
| 1050 |
initrd_addons, |
| 1051 |
n_initrd_addons, |
| 1052 |
ucode_addons, |
| 1053 |
n_ucode_addons); |
| 1054 |
if (err != EFI_SUCCESS) |
| 1055 |
log_error_status(err, "Error loading global addons, ignoring: %m"); |
| 1056 |
|
| 1057 |
/* Some bootloaders always pass NULL in FilePath, so we need to check for it here. */ |
| 1058 |
_cleanup_free_ char16_t *dropin_dir = get_extra_dir(loaded_image->FilePath); |
| 1059 |
if (!dropin_dir) |
| 1060 |
return; |
| 1061 |
|
| 1062 |
err = load_addons( |
| 1063 |
image, |
| 1064 |
loaded_image, |
| 1065 |
dropin_dir, |
| 1066 |
uname, |
| 1067 |
cmdline_addons, |
| 1068 |
dt_addons, |
| 1069 |
n_dt_addons, |
| 1070 |
initrd_addons, |
| 1071 |
n_initrd_addons, |
| 1072 |
ucode_addons, |
| 1073 |
n_ucode_addons); |
| 1074 |
if (err != EFI_SUCCESS) |
| 1075 |
log_error_status(err, "Error loading UKI-specific addons, ignoring: %m"); |
| 1076 |
} |
| 1077 |
|
| 1078 |
static void display_splash( |
| 1079 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 1080 |
const PeSectionVector sections[static _UNIFIED_SECTION_MAX]) { |
| 1081 |
|
| 1082 |
assert(loaded_image); |
| 1083 |
assert(sections); |
| 1084 |
|
| 1085 |
if (!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_SPLASH)) |
| 1086 |
return; |
| 1087 |
|
| 1088 |
graphics_splash((const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_SPLASH].memory_offset, sections[UNIFIED_SECTION_SPLASH].memory_size); |
| 1089 |
} |
| 1090 |
|
| 1091 |
static EFI_STATUS find_sections( |
| 1092 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 1093 |
unsigned profile, |
| 1094 |
PeSectionVector sections[static _UNIFIED_SECTION_MAX]) { |
| 1095 |
|
| 1096 |
EFI_STATUS err; |
| 1097 |
|
| 1098 |
assert(loaded_image); |
| 1099 |
assert(sections); |
| 1100 |
|
| 1101 |
const PeSectionHeader *section_table; |
| 1102 |
size_t n_section_table; |
| 1103 |
err = pe_section_table_from_base(loaded_image->ImageBase, §ion_table, &n_section_table); |
| 1104 |
if (err != EFI_SUCCESS) |
| 1105 |
return log_error_status(err, "Unable to locate PE section table: %m"); |
| 1106 |
|
| 1107 |
/* Get the base sections */ |
| 1108 |
err = pe_locate_profile_sections( |
| 1109 |
section_table, |
| 1110 |
n_section_table, |
| 1111 |
unified_sections, |
| 1112 |
/* profile= */ UINT_MAX, |
| 1113 |
/* validate_base= */ PTR_TO_SIZE(loaded_image->ImageBase), |
| 1114 |
sections); |
| 1115 |
if (err != EFI_SUCCESS) |
| 1116 |
return log_error_status(err, "Unable to locate embedded base PE sections: %m"); |
| 1117 |
|
| 1118 |
if (profile != UINT_MAX) { |
| 1119 |
/* And then override them with the per-profile sections of the selected profile */ |
| 1120 |
err = pe_locate_profile_sections( |
| 1121 |
section_table, |
| 1122 |
n_section_table, |
| 1123 |
unified_sections, |
| 1124 |
profile, |
| 1125 |
/* validate_base= */ PTR_TO_SIZE(loaded_image->ImageBase), |
| 1126 |
sections); |
| 1127 |
if (err != EFI_SUCCESS && !(err == EFI_NOT_FOUND && profile == 0)) /* the first profile is implied if it doesn't exist */ |
| 1128 |
return log_error_status(err, "Unable to locate embedded per-profile PE sections: %m"); |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_LINUX)) |
| 1132 |
return log_error_status(EFI_NOT_FOUND, "Image lacks .linux section."); |
| 1133 |
|
| 1134 |
return EFI_SUCCESS; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static void settle_command_line( |
| 1138 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 1139 |
const PeSectionVector sections[static _UNIFIED_SECTION_MAX], |
| 1140 |
char16_t **cmdline, |
| 1141 |
int *parameters_measured) { |
| 1142 |
|
| 1143 |
assert(loaded_image); |
| 1144 |
assert(sections); |
| 1145 |
assert(cmdline); |
| 1146 |
|
| 1147 |
/* This determines which command line to use. On input *cmdline contains the custom passed in cmdline |
| 1148 |
* if there is any. |
| 1149 |
* |
| 1150 |
* We'll suppress the custom cmdline if we are in Secure Boot mode, and if either there is already |
| 1151 |
* a cmdline baked into the UKI or we are in confidential VM mode. */ |
| 1152 |
|
| 1153 |
if (!isempty(*cmdline)) { |
| 1154 |
if (secure_boot_enabled() && (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_CMDLINE) || is_confidential_vm())) |
| 1155 |
/* Drop the custom cmdline */ |
| 1156 |
*cmdline = mfree(*cmdline); |
| 1157 |
else { |
| 1158 |
/* Let's measure the passed kernel command line into the TPM. Note that this possibly |
| 1159 |
* duplicates what we already did in the boot menu, if that was already |
| 1160 |
* used. However, since we want the boot menu to support an EFI binary, and want to |
| 1161 |
* this stub to be usable from any boot menu, let's measure things anyway. */ |
| 1162 |
bool m = false; |
| 1163 |
(void) tpm_log_load_options(*cmdline, &m); |
| 1164 |
combine_measured_flag(parameters_measured, m); |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
/* No cmdline specified? Or suppressed? Then let's take the one from the UKI, if there is any. */ |
| 1169 |
if (isempty(*cmdline)) |
| 1170 |
*cmdline = mangle_stub_cmdline(pe_section_to_str16(loaded_image, sections + UNIFIED_SECTION_CMDLINE)); |
| 1171 |
} |
| 1172 |
|
| 1173 |
static void measure_profile(unsigned profile, int *parameters_measured) { |
| 1174 |
if (profile == 0) /* don't measure anything about the default profile */ |
| 1175 |
return; |
| 1176 |
|
| 1177 |
_cleanup_free_ char16_t *s = xasprintf("%u", profile); |
| 1178 |
|
| 1179 |
bool m = false; |
| 1180 |
(void) tpm_log_tagged_event( |
| 1181 |
TPM2_PCR_KERNEL_CONFIG, |
| 1182 |
POINTER_TO_PHYSICAL_ADDRESS(s), |
| 1183 |
strsize16(s), |
| 1184 |
UKI_PROFILE_EVENT_TAG_ID, |
| 1185 |
s, |
| 1186 |
&m); |
| 1187 |
combine_measured_flag(parameters_measured, m); |
| 1188 |
} |
| 1189 |
|
| 1190 |
static EFI_STATUS run(EFI_HANDLE image) { |
| 1191 |
int sections_measured = -1, parameters_measured = -1, sysext_measured = -1, confext_measured = -1; |
| 1192 |
_cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {}; |
| 1193 |
_cleanup_free_ char16_t *cmdline = NULL, *cmdline_addons = NULL; |
| 1194 |
_cleanup_(initrds_free) struct iovec initrds[_INITRD_MAX] = {}; |
| 1195 |
PeSectionVector sections[ELEMENTSOF(unified_sections)] = {}; |
| 1196 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image; |
| 1197 |
_cleanup_free_ char *uname = NULL; |
| 1198 |
NamedAddon *dt_addons = NULL, *initrd_addons = NULL, *ucode_addons = NULL; |
| 1199 |
size_t n_dt_addons = 0, n_initrd_addons = 0, n_ucode_addons = 0; |
| 1200 |
_cleanup_free_ struct iovec *all_initrds = NULL; |
| 1201 |
size_t n_all_initrds = 0; |
| 1202 |
unsigned profile = 0; |
| 1203 |
EFI_STATUS err; |
| 1204 |
|
| 1205 |
log_set_max_level_from_smbios(); |
| 1206 |
|
| 1207 |
err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image); |
| 1208 |
if (err != EFI_SUCCESS) |
| 1209 |
return log_error_status(err, "Error getting a LoadedImageProtocol handle: %m"); |
| 1210 |
|
| 1211 |
/* Pick up the arguments passed to us, split out the prefixing profile parameter, and return the rest |
| 1212 |
* as potential command line to use. */ |
| 1213 |
process_arguments(image, loaded_image, &profile, &cmdline); |
| 1214 |
|
| 1215 |
/* Find the sections we want to operate on, both the basic ones, and the one appropriate for the |
| 1216 |
* selected profile. */ |
| 1217 |
err = find_sections(loaded_image, profile, sections); |
| 1218 |
if (err != EFI_SUCCESS) |
| 1219 |
return err; |
| 1220 |
|
| 1221 |
measure_profile(profile, ¶meters_measured); |
| 1222 |
measure_sections(loaded_image, sections, §ions_measured); |
| 1223 |
|
| 1224 |
/* Show splash screen as early as possible, but after measuring it */ |
| 1225 |
display_splash(loaded_image, sections); |
| 1226 |
|
| 1227 |
refresh_random_seed(loaded_image); |
| 1228 |
|
| 1229 |
uint8_t boot_secret[BOOT_SECRET_SIZE] = {}; /* all zeroes means: not acquired */ |
| 1230 |
CLEANUP_ERASE(boot_secret); |
| 1231 |
(void) prepare_boot_secret(loaded_image, sections + UNIFIED_SECTION_OSREL, boot_secret); |
| 1232 |
|
| 1233 |
uname = pe_section_to_str8(loaded_image, sections + UNIFIED_SECTION_UNAME); |
| 1234 |
|
| 1235 |
/* Let's now check if we actually want to use the command line, measure it if it was passed in. */ |
| 1236 |
settle_command_line(loaded_image, sections, &cmdline, ¶meters_measured); |
| 1237 |
|
| 1238 |
/* Now that we have the UKI sections loaded, also load global first and then local (per-UKI) |
| 1239 |
* addons. The data is loaded at once, and then used later. */ |
| 1240 |
CLEANUP_ARRAY(dt_addons, n_dt_addons, named_addon_free_array); |
| 1241 |
CLEANUP_ARRAY(initrd_addons, n_initrd_addons, named_addon_free_array); |
| 1242 |
CLEANUP_ARRAY(ucode_addons, n_ucode_addons, named_addon_free_array); |
| 1243 |
load_all_addons(image, loaded_image, uname, &cmdline_addons, &dt_addons, &n_dt_addons, &initrd_addons, &n_initrd_addons, &ucode_addons, &n_ucode_addons); |
| 1244 |
|
| 1245 |
/* If we have any extra command line to add via PE addons, load them now and append, and measure the |
| 1246 |
* additions together, after the embedded options, but before the smbios ones, so that the order is |
| 1247 |
* reversed from "most hardcoded" to "most dynamic". The global addons are loaded first, and the |
| 1248 |
* image-specific ones later, for the same reason. */ |
| 1249 |
cmdline_append_and_measure_addons(cmdline_addons, &cmdline, ¶meters_measured); |
| 1250 |
cmdline_append_and_measure_smbios(&cmdline, ¶meters_measured); |
| 1251 |
|
| 1252 |
cmdline_append_console(&cmdline); |
| 1253 |
|
| 1254 |
export_common_variables(loaded_image); |
| 1255 |
export_stub_variables(loaded_image, profile); |
| 1256 |
|
| 1257 |
/* Measure SMBIOS data into PCR 1, unless sd-boot already did so in the same boot (tracked via |
| 1258 |
* the LoaderPcrSMBIOS EFI variable). */ |
| 1259 |
measure_smbios(); |
| 1260 |
|
| 1261 |
/* First load the base device tree, then fix it up using addons - global first, then per-UKI. */ |
| 1262 |
install_embedded_devicetree(loaded_image, sections, &dt_state); |
| 1263 |
install_addon_devicetrees(&dt_state, dt_addons, n_dt_addons, ¶meters_measured); |
| 1264 |
|
| 1265 |
/* Generate & find all initrds */ |
| 1266 |
acquire_previous_initrd(initrds); |
| 1267 |
generate_sidecar_initrds(loaded_image, initrds, ¶meters_measured, &sysext_measured, &confext_measured); |
| 1268 |
generate_embedded_initrds(loaded_image, sections, initrds); |
| 1269 |
generate_boot_secret_initrd(boot_secret, initrds); |
| 1270 |
lookup_embedded_initrds(loaded_image, sections, initrds); |
| 1271 |
|
| 1272 |
/* Add initrds in the right order. Generally, later initrds can overwrite files in earlier ones, |
| 1273 |
* except for ucode, where the kernel uses the first matching embedded filename. |
| 1274 |
* We want addons to take precedence over the base initrds, so the order is: |
| 1275 |
* 1. Ucode addons |
| 1276 |
* 2. UKI ucode |
| 1277 |
* 3. Previous initrds |
| 1278 |
* 4. UKI initrd |
| 1279 |
* 5. Generated initrds |
| 1280 |
* 6. initrd addons */ |
| 1281 |
measure_and_append_ucode_addons(&all_initrds, &n_all_initrds, ucode_addons, n_ucode_addons, ¶meters_measured); |
| 1282 |
extend_initrds(initrds, &all_initrds, &n_all_initrds); |
| 1283 |
measure_and_append_initrd_addons(&all_initrds, &n_all_initrds, initrd_addons, n_initrd_addons, ¶meters_measured); |
| 1284 |
|
| 1285 |
/* Export variables indicating what we measured */ |
| 1286 |
export_pcr_variables(sections_measured, parameters_measured, sysext_measured, confext_measured); |
| 1287 |
|
| 1288 |
/* Combine the initrds into one */ |
| 1289 |
_cleanup_pages_ Pages initrd_pages = {}; |
| 1290 |
struct iovec final_initrd = {}; |
| 1291 |
if (n_all_initrds > 1) { |
| 1292 |
/* If there is more then 1 initrd we need to combine them */ |
| 1293 |
err = combine_initrds(all_initrds, n_all_initrds, &initrd_pages, &final_initrd.iov_len); |
| 1294 |
if (err != EFI_SUCCESS) |
| 1295 |
return err; |
| 1296 |
|
| 1297 |
final_initrd.iov_base = PHYSICAL_ADDRESS_TO_POINTER(initrd_pages.addr); |
| 1298 |
|
| 1299 |
/* Given these might be large let's free them explicitly before we pass control to Linux */ |
| 1300 |
initrds_free(&initrds); |
| 1301 |
} else if (n_all_initrds == 1) |
| 1302 |
final_initrd = all_initrds[0]; |
| 1303 |
|
| 1304 |
struct iovec kernel = IOVEC_MAKE( |
| 1305 |
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_LINUX].memory_offset, |
| 1306 |
sections[UNIFIED_SECTION_LINUX].memory_size); |
| 1307 |
|
| 1308 |
err = linux_exec(image, cmdline, &kernel, &final_initrd); |
| 1309 |
graphics_mode(false); |
| 1310 |
return err; |
| 1311 |
} |
| 1312 |
|
| 1313 |
// NOLINTNEXTLINE(misc-use-internal-linkage) |
| 1314 |
DEFINE_EFI_MAIN_FUNCTION(run, "systemd-stub", /* wait_for_debugger= */ false); |