| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "boot-secret.h" |
| 4 |
#include "efi-efivars.h" |
| 5 |
#include "efi-log.h" |
| 6 |
#include "random-seed.h" |
| 7 |
#include "sha256.h" |
| 8 |
#include "util.h" |
| 9 |
|
| 10 |
#define BOOT_SECRET_MIXIN_PATH u"\\loader\\boot-secret-mixin" |
| 11 |
|
| 12 |
/* This maintains a per-system secret that is stored in an EFI variable that is only accessible during EFI |
| 13 |
* boot, and becomes inaccessible afterwards, once ExitBootServices() is called. The variable is |
| 14 |
* automatically initialized if missing. A secret derived by hashing from this EFI variable secret is then |
| 15 |
* passed to the OS, in an initrd file inaccessible to unprivileged userspace. To make things a bit more |
| 16 |
* robust while hashing two more pieces of information are mixed in: a random "mixin" that is stored in the |
| 17 |
* ESP and is supposed to ensure that the passed boot secrets are distinct for each disk used on the system; |
| 18 |
* moreover an OS identifier derived from the UKI's .osrel field (ideally IMAGE_ID=, but if not defined ID= |
| 19 |
* will do, with a final fallback to "linux"). Note that these two additions are not supposed to enhance the |
| 20 |
* cryptographic quality of the secret, they are just supposed to make things more robust on systems with |
| 21 |
* multiple disks and OSes. |
| 22 |
* |
| 23 |
* The boot secret passed to the OS can be used to protect resources during OS runtime, from earliest boot |
| 24 |
* phases on, as a fallback for the usual TPM based protections. |
| 25 |
* |
| 26 |
* Note that this secret comes with much weaker protection than TPM backed secrets: there's no physical |
| 27 |
* isolation, there are no cryptographic access policies, there's just the hope the firmware reasonably |
| 28 |
* correctly implements boot-time-only EFI variable mechanism. (But then again, this is what mok/shim's |
| 29 |
* security also relies on, and hence this all is not too bad?) */ |
| 30 |
|
| 31 |
static EFI_STATUS random_seed_find_table(struct linux_efi_random_seed **ret) { |
| 32 |
assert(ret); |
| 33 |
|
| 34 |
/* We use the Linux random seed EFI table as our source of randomness, since there's reason to |
| 35 |
* believe it is as good as it possibly would get. Note that we ourselves might be the ones |
| 36 |
* initializing it, based on EFI RNG APIs, the monotonic boot counter, a random seed file on disk and |
| 37 |
* the clock. */ |
| 38 |
|
| 39 |
struct linux_efi_random_seed *seed_table = |
| 40 |
find_configuration_table(MAKE_GUID_PTR(LINUX_EFI_RANDOM_SEED_TABLE)); |
| 41 |
if (!seed_table) |
| 42 |
return log_debug_status(EFI_NOT_FOUND, "No random seed available, not creating a boot secret."); |
| 43 |
if (seed_table->size < BOOT_SECRET_SIZE) |
| 44 |
return log_debug_status(EFI_NOT_FOUND, "Random seed is available, but too short."); |
| 45 |
|
| 46 |
*ret = seed_table; |
| 47 |
return EFI_SUCCESS; |
| 48 |
} |
| 49 |
|
| 50 |
static void random_seed_evolve(struct linux_efi_random_seed *seed_table) { |
| 51 |
static const char label[] = "systemd-stub random seed evolve label v1"; |
| 52 |
|
| 53 |
assert(seed_table); |
| 54 |
|
| 55 |
/* Whenever we derived something from the Linux random seed EFI table we evolve the secret in it, so |
| 56 |
* that the seed is never reused. */ |
| 57 |
|
| 58 |
struct sha256_ctx hash; |
| 59 |
CLEANUP_ERASE(hash); |
| 60 |
sha256_init_ctx(&hash); |
| 61 |
sha256_process_bytes(label, sizeof(label) - 1, &hash); |
| 62 |
sha256_process_bytes(&seed_table->size, sizeof(seed_table->size), &hash); |
| 63 |
sha256_process_bytes(seed_table->seed, seed_table->size, &hash); |
| 64 |
assert(seed_table->size >= SHA256_DIGEST_SIZE); |
| 65 |
sha256_finish_ctx(&hash, seed_table->seed); |
| 66 |
} |
| 67 |
|
| 68 |
static void random_seed_make_secret( |
| 69 |
struct linux_efi_random_seed *seed_table, |
| 70 |
uint8_t ret_secret[static BOOT_SECRET_SIZE]) { |
| 71 |
|
| 72 |
static const char label[] = "systemd-stub random seed make secret label v1"; |
| 73 |
|
| 74 |
assert(seed_table); |
| 75 |
assert(ret_secret); |
| 76 |
|
| 77 |
/* Derive a new secret from the Linux random seed EFI table data */ |
| 78 |
|
| 79 |
struct sha256_ctx hash; |
| 80 |
CLEANUP_ERASE(hash); |
| 81 |
sha256_init_ctx(&hash); |
| 82 |
sha256_process_bytes(label, sizeof(label) - 1, &hash); |
| 83 |
sha256_process_bytes(&seed_table->size, sizeof(seed_table->size), &hash); |
| 84 |
sha256_process_bytes(seed_table->seed, seed_table->size, &hash); |
| 85 |
sha256_finish_ctx(&hash, ret_secret); |
| 86 |
|
| 87 |
random_seed_evolve(seed_table); /* ← ensure the same seed is not reused */ |
| 88 |
} |
| 89 |
|
| 90 |
static EFI_STATUS read_efivar_secret(uint8_t ret_secret[static BOOT_SECRET_SIZE]) { |
| 91 |
EFI_STATUS err; |
| 92 |
|
| 93 |
assert(ret_secret); |
| 94 |
|
| 95 |
/* Reads the boot secret from the EFI variable, ensuring it's properly protected from the OS, as per |
| 96 |
* the attribute flags */ |
| 97 |
|
| 98 |
_cleanup_free_ void* data = NULL; |
| 99 |
uint32_t attributes; |
| 100 |
size_t size = 0; |
| 101 |
err = efivar_get_raw_full(MAKE_GUID_PTR(LOADER), u"LoaderBootSecret", &attributes, &data, &size); |
| 102 |
if (err != EFI_SUCCESS) |
| 103 |
return log_debug_status(err, "Failed to read LoaderBootSecret EFI variable: %m"); |
| 104 |
|
| 105 |
if (size != BOOT_SECRET_SIZE) { |
| 106 |
err = log_debug_status(EFI_PROTOCOL_ERROR, "Unexpected size of BootSecret EFI variable, ignoring."); |
| 107 |
goto finish; |
| 108 |
} |
| 109 |
|
| 110 |
if ((attributes & (EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS)) != |
| 111 |
(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS)) { |
| 112 |
err = log_debug_status(EFI_PROTOCOL_ERROR, "Unexpected attributes of BootSecret EFI variable, ignoring."); |
| 113 |
goto finish; |
| 114 |
} |
| 115 |
|
| 116 |
memcpy(ret_secret, data, size); |
| 117 |
err = EFI_SUCCESS; |
| 118 |
finish: |
| 119 |
explicit_bzero_safe(data, size); |
| 120 |
return err; |
| 121 |
} |
| 122 |
|
| 123 |
static EFI_STATUS setup_efivar_secret( |
| 124 |
struct linux_efi_random_seed *seed_table, |
| 125 |
uint8_t ret_secret[static BOOT_SECRET_SIZE]) { |
| 126 |
|
| 127 |
EFI_STATUS err; |
| 128 |
|
| 129 |
assert(seed_table); |
| 130 |
assert(ret_secret); |
| 131 |
|
| 132 |
/* Generates a new EFI variable secret, and stores it in an EFI variable. */ |
| 133 |
|
| 134 |
uint8_t secret[BOOT_SECRET_SIZE]; |
| 135 |
CLEANUP_ERASE(secret); |
| 136 |
random_seed_make_secret(seed_table, secret); |
| 137 |
|
| 138 |
/* Set the variable with the EFI_VARIABLE_RUNTIME_ACCESS flag off (!), so that it's invisible after |
| 139 |
* ExitBootServices()! */ |
| 140 |
err = RT->SetVariable( |
| 141 |
(char16_t*) u"LoaderBootSecret", |
| 142 |
MAKE_GUID_PTR(LOADER), |
| 143 |
EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, /* ← No EFI_VARIABLE_RUNTIME_ACCESS here */ |
| 144 |
sizeof(secret), |
| 145 |
secret); |
| 146 |
if (err != EFI_SUCCESS) |
| 147 |
return log_debug_status(err, "Failed to set boot secret EFI variable: %m"); |
| 148 |
|
| 149 |
memcpy(ret_secret, secret, sizeof(secret)); |
| 150 |
return EFI_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
static EFI_STATUS acquire_efivar_secret( |
| 154 |
struct linux_efi_random_seed *seed_table, |
| 155 |
uint8_t ret_secret[static BOOT_SECRET_SIZE]) { |
| 156 |
|
| 157 |
EFI_STATUS err; |
| 158 |
|
| 159 |
assert(seed_table); |
| 160 |
assert(ret_secret); |
| 161 |
|
| 162 |
/* Try to read the boot secret EFI variable, but if it doesn't exist create a new one */ |
| 163 |
|
| 164 |
err = read_efivar_secret(ret_secret); |
| 165 |
if (err != EFI_NOT_FOUND) |
| 166 |
return err; |
| 167 |
|
| 168 |
return setup_efivar_secret(seed_table, ret_secret); |
| 169 |
} |
| 170 |
|
| 171 |
static EFI_STATUS setup_secret_mixin( |
| 172 |
EFI_FILE *handle, |
| 173 |
struct linux_efi_random_seed *seed_table, |
| 174 |
uint8_t ret_mixin[static BOOT_SECRET_SIZE]) { |
| 175 |
|
| 176 |
EFI_STATUS err; |
| 177 |
|
| 178 |
assert(handle); |
| 179 |
assert(seed_table); |
| 180 |
assert(ret_mixin); |
| 181 |
|
| 182 |
/* This writes a new 'mixin' to the ESP, in case the ESP so far had none */ |
| 183 |
|
| 184 |
uint8_t mixin[BOOT_SECRET_SIZE]; |
| 185 |
random_seed_make_secret(seed_table, mixin); |
| 186 |
|
| 187 |
size_t wsize = sizeof(mixin); |
| 188 |
err = handle->Write(handle, &wsize, mixin); |
| 189 |
if (err != EFI_SUCCESS) |
| 190 |
return log_debug_status(err, "Failed to write secret mixin file: %m"); |
| 191 |
if (wsize != sizeof(mixin)) |
| 192 |
return log_debug_status(EFI_LOAD_ERROR, "Short write while writing secret mixin file: %m"); |
| 193 |
|
| 194 |
err = handle->Flush(handle); |
| 195 |
if (err != EFI_SUCCESS) |
| 196 |
return log_debug_status(err, "Failed to flush secret mixin file: %m"); |
| 197 |
|
| 198 |
memcpy(ret_mixin, mixin, sizeof(mixin)); |
| 199 |
return EFI_SUCCESS; |
| 200 |
} |
| 201 |
|
| 202 |
static EFI_STATUS acquire_secret_mixin( |
| 203 |
EFI_FILE *root_dir, |
| 204 |
struct linux_efi_random_seed *seed_table, |
| 205 |
uint8_t ret_mixin[static BOOT_SECRET_SIZE]) { |
| 206 |
|
| 207 |
EFI_STATUS err; |
| 208 |
|
| 209 |
assert(seed_table); |
| 210 |
assert(ret_mixin); |
| 211 |
|
| 212 |
if (!root_dir) |
| 213 |
return EFI_NOT_FOUND; |
| 214 |
|
| 215 |
/* Acquires the mixin for the boot secret stored in the ESP. If it already exists we'll read it. If |
| 216 |
* it doesn't we'll initialize it */ |
| 217 |
|
| 218 |
bool writable; |
| 219 |
_cleanup_file_close_ EFI_FILE *handle = NULL; |
| 220 |
err = root_dir->Open( |
| 221 |
root_dir, |
| 222 |
&handle, |
| 223 |
(char16_t *) BOOT_SECRET_MIXIN_PATH, |
| 224 |
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, |
| 225 |
/* Attributes= */ 0); |
| 226 |
if (err == EFI_WRITE_PROTECTED) { |
| 227 |
err = root_dir->Open( |
| 228 |
root_dir, |
| 229 |
&handle, |
| 230 |
(char16_t *) BOOT_SECRET_MIXIN_PATH, |
| 231 |
EFI_FILE_MODE_READ, |
| 232 |
/* Attributes= */ 0); |
| 233 |
if (err != EFI_SUCCESS) |
| 234 |
return log_debug_status(err, "Failed to read the boot secret mixin file '%ls': %m", BOOT_SECRET_MIXIN_PATH); |
| 235 |
|
| 236 |
writable = false; |
| 237 |
} else if (err != EFI_SUCCESS) |
| 238 |
return log_debug_status(err, "Failed to access the boot secret mixin file '%ls': %m", BOOT_SECRET_MIXIN_PATH); |
| 239 |
else |
| 240 |
writable = true; |
| 241 |
|
| 242 |
_cleanup_free_ EFI_FILE_INFO *info = NULL; |
| 243 |
err = get_file_info(handle, &info, /* ret_size= */ NULL); |
| 244 |
if (err != EFI_SUCCESS) |
| 245 |
return log_debug_status(err, "Failed to get boot secret mixin file '%ls' info: %m", BOOT_SECRET_MIXIN_PATH); |
| 246 |
if (info->FileSize == 0 && writable) /* New file? Fill it. */ |
| 247 |
return setup_secret_mixin(handle, seed_table, ret_mixin); |
| 248 |
|
| 249 |
/* If the mixin file is too small we won't overwrite it (in order to not destroy some potentially |
| 250 |
* load bearing key), but we won't use it either. */ |
| 251 |
if (info->FileSize < BOOT_SECRET_SIZE) |
| 252 |
return log_debug_status(EFI_PROTOCOL_ERROR, "Boot secret mixin file '%ls' is too short %" PRIu64 " < %u", BOOT_SECRET_MIXIN_PATH, info->FileSize, BOOT_SECRET_SIZE); |
| 253 |
|
| 254 |
uint8_t mixin[BOOT_SECRET_SIZE]; |
| 255 |
size_t rsize = sizeof(mixin); |
| 256 |
err = handle->Read(handle, &rsize, mixin); |
| 257 |
if (err != EFI_SUCCESS) |
| 258 |
return log_debug_status(err, "Failed to read boot secret mixin file '%ls': %m", BOOT_SECRET_MIXIN_PATH); |
| 259 |
if (rsize != BOOT_SECRET_SIZE) |
| 260 |
return log_debug_status(EFI_PROTOCOL_ERROR, "Unexpected size from Read(): %zu != %zu", rsize, sizeof(mixin)); |
| 261 |
|
| 262 |
memcpy(ret_mixin, mixin, BOOT_SECRET_SIZE); |
| 263 |
return EFI_SUCCESS; |
| 264 |
} |
| 265 |
|
| 266 |
static char* pick_id(const char *_osrel, size_t osrel_size) { |
| 267 |
assert(_osrel || osrel_size == 0); |
| 268 |
|
| 269 |
/* Make a NUL terminated copy we can chop into pieces */ |
| 270 |
_cleanup_free_ char *osrel = NULL; |
| 271 |
osrel = xmalloc(osrel_size + 1); |
| 272 |
if (osrel_size > 0) |
| 273 |
memcpy(osrel, _osrel, osrel_size); |
| 274 |
osrel[osrel_size] = 0; |
| 275 |
|
| 276 |
/* Find an OS ID. Preferably the IMAGE_ID. */ |
| 277 |
_cleanup_free_ char *os_id = NULL; |
| 278 |
char *line, *key, *value; |
| 279 |
size_t pos = 0; |
| 280 |
while ((line = line_get_key_value(osrel, "=", &pos, &key, &value))) { |
| 281 |
if (streq8(key, "IMAGE_ID")) |
| 282 |
return xstrdup8(value); |
| 283 |
|
| 284 |
if (streq8(key, "ID")) { |
| 285 |
free(os_id); |
| 286 |
os_id = xstrdup8(value); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* If the IMAGE_ID= wasn't set, use the OS ID=. If that one isn't set either fall back to "linux". */ |
| 291 |
return TAKE_PTR(os_id) ?: xstrdup8("linux"); |
| 292 |
} |
| 293 |
|
| 294 |
static void derive_secret( |
| 295 |
uint8_t efivar_secret[static BOOT_SECRET_SIZE], |
| 296 |
uint8_t secret_mixin[static BOOT_SECRET_SIZE], |
| 297 |
const char *id, |
| 298 |
uint8_t ret[static BOOT_SECRET_SIZE]) { |
| 299 |
|
| 300 |
static const char hash_label[] = "systemd-stub derive secret label v1"; |
| 301 |
|
| 302 |
assert(efivar_secret); |
| 303 |
assert(secret_mixin); |
| 304 |
assert(id); |
| 305 |
assert(ret); |
| 306 |
|
| 307 |
/* Now combine the EFI variable secret, the mixin from the ESP and the OS id to generate the secret |
| 308 |
* to pass to the OS */ |
| 309 |
|
| 310 |
struct sha256_ctx hash; |
| 311 |
CLEANUP_ERASE(hash); |
| 312 |
sha256_init_ctx(&hash); |
| 313 |
sha256_process_bytes(hash_label, sizeof(hash_label) - 1, &hash); |
| 314 |
sha256_process_bytes(efivar_secret, BOOT_SECRET_SIZE, &hash); |
| 315 |
sha256_process_bytes(secret_mixin, BOOT_SECRET_SIZE, &hash); |
| 316 |
|
| 317 |
/* Include an OS id in the hash, so that every OS gets a different derived secret */ |
| 318 |
size_t size = strlen8(id); |
| 319 |
sha256_process_bytes(&size, sizeof(size), &hash); |
| 320 |
sha256_process_bytes(id, size, &hash); |
| 321 |
|
| 322 |
assert_cc(SHA256_DIGEST_SIZE == BOOT_SECRET_SIZE); |
| 323 |
sha256_finish_ctx(&hash, ret); |
| 324 |
} |
| 325 |
|
| 326 |
EFI_STATUS prepare_boot_secret( |
| 327 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 328 |
const PeSectionVector *osrel_section, |
| 329 |
uint8_t ret[static BOOT_SECRET_SIZE]) { |
| 330 |
|
| 331 |
EFI_STATUS err; |
| 332 |
|
| 333 |
assert(loaded_image); |
| 334 |
assert(ret); |
| 335 |
|
| 336 |
/* Prepares the boot secret to pass to the OS */ |
| 337 |
|
| 338 |
if (!loaded_image->DeviceHandle) |
| 339 |
return EFI_SUCCESS; |
| 340 |
|
| 341 |
_cleanup_file_close_ EFI_FILE *root = NULL; |
| 342 |
err = open_volume(loaded_image->DeviceHandle, &root); |
| 343 |
if (err != EFI_SUCCESS) |
| 344 |
return err; |
| 345 |
|
| 346 |
/* We need the Linux random seed EFI table, so that we can initialize the EFI variable secret and |
| 347 |
* generate the secret mixin. */ |
| 348 |
struct linux_efi_random_seed *seed_table = NULL; |
| 349 |
err = random_seed_find_table(&seed_table); |
| 350 |
if (err != EFI_SUCCESS) |
| 351 |
return err; |
| 352 |
|
| 353 |
uint8_t efivar_secret[BOOT_SECRET_SIZE]; |
| 354 |
CLEANUP_ERASE(efivar_secret); |
| 355 |
err = acquire_efivar_secret(seed_table, efivar_secret); |
| 356 |
if (err != EFI_SUCCESS) |
| 357 |
return err; |
| 358 |
|
| 359 |
uint8_t secret_mixin[BOOT_SECRET_SIZE]; |
| 360 |
err = acquire_secret_mixin(root, seed_table, secret_mixin); |
| 361 |
if (err != EFI_SUCCESS) |
| 362 |
return err; |
| 363 |
|
| 364 |
const char *osrel = NULL; |
| 365 |
size_t osrel_size = 0; |
| 366 |
if (PE_SECTION_VECTOR_IS_SET(osrel_section)) { |
| 367 |
osrel = (const char*) loaded_image->ImageBase + osrel_section->memory_offset; |
| 368 |
osrel_size = osrel_section->memory_size; |
| 369 |
} |
| 370 |
_cleanup_free_ char *id = pick_id(osrel, osrel_size); |
| 371 |
|
| 372 |
derive_secret(efivar_secret, secret_mixin, id, ret); |
| 373 |
return EFI_SUCCESS; |
| 374 |
} |