| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "efi-api.h" |
| 5 |
#include "efi-loader.h" |
| 6 |
#include "efivars.h" |
| 7 |
#include "env-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "parse-util.h" |
| 10 |
#include "path-util.h" |
| 11 |
#include "proc-cmdline.h" |
| 12 |
#include "stat-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "time-util.h" |
| 16 |
#include "tpm2-pcr.h" |
| 17 |
#include "utf8.h" |
| 18 |
|
| 19 |
#if ENABLE_EFI |
| 20 |
static int read_usec(const char *variable, usec_t *ret) { |
| 21 |
_cleanup_free_ char *j = NULL; |
| 22 |
uint64_t x = 0; |
| 23 |
int r; |
| 24 |
|
| 25 |
assert(variable); |
| 26 |
assert(ret); |
| 27 |
|
| 28 |
r = efi_get_variable_string(variable, &j); |
| 29 |
if (r < 0) |
| 30 |
return r; |
| 31 |
|
| 32 |
r = safe_atou64(j, &x); |
| 33 |
if (r < 0) |
| 34 |
return r; |
| 35 |
|
| 36 |
*ret = x; |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static int get_device_part_uuid(const char *variable, sd_id128_t *ret) { |
| 41 |
if (!is_efi_boot()) |
| 42 |
return -EOPNOTSUPP; |
| 43 |
|
| 44 |
return efi_get_variable_id128(variable, ret); |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
int efi_loader_get_boot_usec(usec_t *ret_firmware, usec_t *ret_loader) { |
| 49 |
#if ENABLE_EFI |
| 50 |
uint64_t x, y; |
| 51 |
int r; |
| 52 |
|
| 53 |
assert(ret_firmware); |
| 54 |
assert(ret_loader); |
| 55 |
|
| 56 |
if (!is_efi_boot()) |
| 57 |
return -EOPNOTSUPP; |
| 58 |
|
| 59 |
r = read_usec(EFI_LOADER_VARIABLE_STR("LoaderTimeInitUSec"), &x); |
| 60 |
if (r < 0) |
| 61 |
return log_debug_errno(r, "Failed to read LoaderTimeInitUSec: %m"); |
| 62 |
|
| 63 |
r = read_usec(EFI_LOADER_VARIABLE_STR("LoaderTimeExecUSec"), &y); |
| 64 |
if (r < 0) |
| 65 |
return log_debug_errno(r, "Failed to read LoaderTimeExecUSec: %m"); |
| 66 |
|
| 67 |
if (y == 0 || y < x || y - x > USEC_PER_HOUR) |
| 68 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 69 |
"Bad LoaderTimeInitUSec=%"PRIu64", LoaderTimeExecUSec=%" PRIu64"; refusing.", |
| 70 |
x, y); |
| 71 |
|
| 72 |
*ret_firmware = x; |
| 73 |
*ret_loader = y; |
| 74 |
return 0; |
| 75 |
#else |
| 76 |
return -EOPNOTSUPP; |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
int efi_loader_get_device_part_uuid(sd_id128_t *ret) { |
| 81 |
#if ENABLE_EFI |
| 82 |
return get_device_part_uuid(EFI_LOADER_VARIABLE_STR("LoaderDevicePartUUID"), ret); |
| 83 |
#else |
| 84 |
return -EOPNOTSUPP; |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
int efi_stub_get_device_part_uuid(sd_id128_t *ret) { |
| 89 |
#if ENABLE_EFI |
| 90 |
return get_device_part_uuid(EFI_LOADER_VARIABLE_STR("StubDevicePartUUID"), ret); |
| 91 |
#else |
| 92 |
return -EOPNOTSUPP; |
| 93 |
#endif |
| 94 |
} |
| 95 |
|
| 96 |
int efi_loader_get_entries(char ***ret) { |
| 97 |
#if ENABLE_EFI |
| 98 |
_cleanup_free_ char16_t *entries = NULL; |
| 99 |
_cleanup_strv_free_ char **l = NULL; |
| 100 |
size_t size; |
| 101 |
int r; |
| 102 |
|
| 103 |
assert(ret); |
| 104 |
|
| 105 |
if (!is_efi_boot()) |
| 106 |
return -EOPNOTSUPP; |
| 107 |
|
| 108 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderEntries"), NULL, (void**) &entries, &size); |
| 109 |
if (r < 0) |
| 110 |
return r; |
| 111 |
|
| 112 |
/* The variable contains a series of individually NUL terminated UTF-16 strings. We gracefully |
| 113 |
* consider the final NUL byte optional (i.e. the last string may or may not end in a NUL byte). */ |
| 114 |
|
| 115 |
for (size_t i = 0, start = 0;; i++) { |
| 116 |
_cleanup_free_ char *decoded = NULL; |
| 117 |
bool end; |
| 118 |
|
| 119 |
/* Is this the end of the variable's data? */ |
| 120 |
end = i * sizeof(char16_t) >= size; |
| 121 |
|
| 122 |
/* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If |
| 123 |
* so, let's go to the next entry. */ |
| 124 |
if (!end && entries[i] != 0) |
| 125 |
continue; |
| 126 |
|
| 127 |
/* Empty string at the end of variable? That's the trailer, we are done (i.e. we have a final |
| 128 |
* NUL terminator). */ |
| 129 |
if (end && start == i) |
| 130 |
break; |
| 131 |
|
| 132 |
/* We reached the end of a string, let's decode it into UTF-8 */ |
| 133 |
decoded = utf16_to_utf8(entries + start, (i - start) * sizeof(char16_t)); |
| 134 |
if (!decoded) |
| 135 |
return -ENOMEM; |
| 136 |
|
| 137 |
if (efi_loader_entry_name_valid(decoded)) { |
| 138 |
r = strv_consume(&l, TAKE_PTR(decoded)); |
| 139 |
if (r < 0) |
| 140 |
return r; |
| 141 |
} else |
| 142 |
log_debug("Ignoring invalid loader entry '%s'.", decoded); |
| 143 |
|
| 144 |
/* Exit the loop if we reached the end of the variable (i.e. we do not have a final NUL |
| 145 |
* terminator) */ |
| 146 |
if (end) |
| 147 |
break; |
| 148 |
|
| 149 |
/* Continue after the NUL byte */ |
| 150 |
start = i + 1; |
| 151 |
} |
| 152 |
|
| 153 |
*ret = TAKE_PTR(l); |
| 154 |
return 0; |
| 155 |
#else |
| 156 |
return -EOPNOTSUPP; |
| 157 |
#endif |
| 158 |
} |
| 159 |
|
| 160 |
int efi_loader_get_features(uint64_t *ret) { |
| 161 |
#if ENABLE_EFI |
| 162 |
_cleanup_free_ void *v = NULL; |
| 163 |
size_t s; |
| 164 |
int r; |
| 165 |
|
| 166 |
assert(ret); |
| 167 |
|
| 168 |
if (!is_efi_boot()) { |
| 169 |
*ret = 0; |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderFeatures"), NULL, &v, &s); |
| 174 |
if (r == -ENOENT) { |
| 175 |
_cleanup_free_ char *info = NULL; |
| 176 |
|
| 177 |
/* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */ |
| 178 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderInfo"), &info); |
| 179 |
if (r < 0) { |
| 180 |
if (r != -ENOENT) |
| 181 |
return r; |
| 182 |
|
| 183 |
/* Variable not set, definitely means not systemd-boot */ |
| 184 |
|
| 185 |
} else if (first_word(info, "systemd-boot")) { |
| 186 |
|
| 187 |
/* An older systemd-boot version. Let's hardcode the feature set, since it was pretty |
| 188 |
* static in all its versions. */ |
| 189 |
|
| 190 |
*ret = EFI_LOADER_FEATURE_CONFIG_TIMEOUT | |
| 191 |
EFI_LOADER_FEATURE_ENTRY_DEFAULT | |
| 192 |
EFI_LOADER_FEATURE_ENTRY_ONESHOT; |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
/* No features supported */ |
| 198 |
*ret = 0; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
if (r < 0) |
| 202 |
return r; |
| 203 |
|
| 204 |
if (s != sizeof(uint64_t)) |
| 205 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 206 |
"LoaderFeatures EFI variable doesn't have the right size."); |
| 207 |
|
| 208 |
memcpy(ret, v, sizeof(uint64_t)); |
| 209 |
return 0; |
| 210 |
#else |
| 211 |
return -EOPNOTSUPP; |
| 212 |
#endif |
| 213 |
} |
| 214 |
|
| 215 |
int efi_stub_get_features(uint64_t *ret) { |
| 216 |
#if ENABLE_EFI |
| 217 |
_cleanup_free_ void *v = NULL; |
| 218 |
size_t s; |
| 219 |
int r; |
| 220 |
|
| 221 |
assert(ret); |
| 222 |
|
| 223 |
if (!is_efi_boot()) { |
| 224 |
*ret = 0; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("StubFeatures"), NULL, &v, &s); |
| 229 |
if (r == -ENOENT) { |
| 230 |
_cleanup_free_ char *info = NULL; |
| 231 |
|
| 232 |
/* The new (v252+) StubFeatures variable is not supported, let's see if it's systemd-stub at all */ |
| 233 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("StubInfo"), &info); |
| 234 |
if (r < 0) { |
| 235 |
if (r != -ENOENT) |
| 236 |
return r; |
| 237 |
|
| 238 |
/* Variable not set, definitely means not systemd-stub */ |
| 239 |
|
| 240 |
} else if (first_word(info, "systemd-stub")) { |
| 241 |
|
| 242 |
/* An older systemd-stub version. Let's hardcode the feature set, since it was pretty |
| 243 |
* static in all its versions. */ |
| 244 |
|
| 245 |
*ret = EFI_STUB_FEATURE_REPORT_BOOT_PARTITION; |
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
/* No features supported */ |
| 250 |
*ret = 0; |
| 251 |
return 0; |
| 252 |
} |
| 253 |
if (r < 0) |
| 254 |
return r; |
| 255 |
|
| 256 |
if (s != sizeof(uint64_t)) |
| 257 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 258 |
"StubFeatures EFI variable doesn't have the right size."); |
| 259 |
|
| 260 |
memcpy(ret, v, sizeof(uint64_t)); |
| 261 |
return 0; |
| 262 |
#else |
| 263 |
return -EOPNOTSUPP; |
| 264 |
#endif |
| 265 |
} |
| 266 |
|
| 267 |
int efi_measured_uki(int log_level) { |
| 268 |
#if ENABLE_EFI |
| 269 |
_cleanup_free_ char *pcr_string = NULL; |
| 270 |
static int cached = -1; |
| 271 |
unsigned pcr_nr; |
| 272 |
int r; |
| 273 |
|
| 274 |
if (cached >= 0) |
| 275 |
return cached; |
| 276 |
|
| 277 |
/* Checks if we are booted on a kernel with sd-stub which measured the kernel into PCR 11 on a TPM2 |
| 278 |
* chip. Or in other words, if we are running on a TPM enabled UKI. (TPM 1.2 situations are ignored.) |
| 279 |
* |
| 280 |
* Returns == 0 and > 0 depending on the result of the test. Returns -EREMOTE if we detected a stub |
| 281 |
* being used, but it measured things into a different PCR than we are configured for in |
| 282 |
* userspace. (i.e. we expect PCR 11 being used for this by both sd-stub and us) */ |
| 283 |
|
| 284 |
r = secure_getenv_bool("SYSTEMD_FORCE_MEASURE"); /* Give user a chance to override the variable test, |
| 285 |
* for debugging purposes */ |
| 286 |
if (r >= 0) |
| 287 |
return (cached = r); |
| 288 |
if (r != -ENXIO) |
| 289 |
log_debug_errno(r, "Failed to parse $SYSTEMD_FORCE_MEASURE, ignoring: %m"); |
| 290 |
|
| 291 |
if (!efi_has_tpm2()) |
| 292 |
return (cached = 0); |
| 293 |
|
| 294 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("StubPcrKernelImage"), &pcr_string); |
| 295 |
if (r == -ENOENT) |
| 296 |
return (cached = 0); |
| 297 |
if (r < 0) |
| 298 |
return log_full_errno(log_level, r, |
| 299 |
"Failed to get StubPcrKernelImage EFI variable: %m"); |
| 300 |
|
| 301 |
r = safe_atou(pcr_string, &pcr_nr); |
| 302 |
if (r < 0) |
| 303 |
return log_full_errno(log_level, r, |
| 304 |
"Failed to parse StubPcrKernelImage EFI variable: %s", pcr_string); |
| 305 |
if (pcr_nr != TPM2_PCR_KERNEL_BOOT) |
| 306 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EREMOTE), |
| 307 |
"Kernel stub measured kernel image into PCR %u, which is different than expected %i.", |
| 308 |
pcr_nr, TPM2_PCR_KERNEL_BOOT); |
| 309 |
|
| 310 |
return (cached = 1); |
| 311 |
#else |
| 312 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), "Compiled without support for EFI"); |
| 313 |
#endif |
| 314 |
} |
| 315 |
|
| 316 |
int efi_measured_os(int log_level) { |
| 317 |
#if ENABLE_EFI |
| 318 |
static int cached = -1; |
| 319 |
int r; |
| 320 |
|
| 321 |
/* Returns if we shall enable our measurement machinery */ |
| 322 |
|
| 323 |
if (cached >= 0) |
| 324 |
return cached; |
| 325 |
|
| 326 |
bool b; |
| 327 |
r = proc_cmdline_get_bool("systemd.tpm2_measured_os", /* flags= */ 0, &b); |
| 328 |
if (r < 0) |
| 329 |
log_debug_errno(r, "Failed to parse systemd.tpm2_measured_os= kernel command line argument, ignoring: %m"); |
| 330 |
else if (r > 0) |
| 331 |
return (cached = b); |
| 332 |
|
| 333 |
/* If nothing is explicitly configured, just assume that if we booted with a measured UKI we also want a measured OS */ |
| 334 |
return (cached = efi_measured_uki(log_level)); |
| 335 |
#else |
| 336 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), "Compiled without support for EFI"); |
| 337 |
#endif |
| 338 |
} |
| 339 |
|
| 340 |
int efi_loader_get_config_timeout_one_shot(usec_t *ret) { |
| 341 |
#if ENABLE_EFI |
| 342 |
_cleanup_free_ char *v = NULL; |
| 343 |
static struct stat cache_stat = {}; |
| 344 |
struct stat new_stat; |
| 345 |
static usec_t cache; |
| 346 |
uint64_t sec; |
| 347 |
int r; |
| 348 |
|
| 349 |
assert(ret); |
| 350 |
|
| 351 |
/* stat() the EFI variable, to see if the mtime changed. If it did, we need to cache again. */ |
| 352 |
if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot")), &new_stat) < 0) |
| 353 |
return -errno; |
| 354 |
|
| 355 |
if (stat_inode_unmodified(&new_stat, &cache_stat)) { |
| 356 |
*ret = cache; |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), &v); |
| 361 |
if (r < 0) |
| 362 |
return r; |
| 363 |
|
| 364 |
r = safe_atou64(v, &sec); |
| 365 |
if (r < 0) |
| 366 |
return r; |
| 367 |
if (sec > USEC_INFINITY / USEC_PER_SEC) |
| 368 |
return -ERANGE; |
| 369 |
|
| 370 |
cache_stat = new_stat; |
| 371 |
*ret = cache = sec * USEC_PER_SEC; /* return in μs */ |
| 372 |
return 0; |
| 373 |
#else |
| 374 |
return -EOPNOTSUPP; |
| 375 |
#endif |
| 376 |
} |
| 377 |
|
| 378 |
int efi_loader_update_entry_one_shot_cache(char **cache, struct stat *cache_stat) { |
| 379 |
#if ENABLE_EFI |
| 380 |
_cleanup_free_ char *v = NULL; |
| 381 |
struct stat new_stat; |
| 382 |
int r; |
| 383 |
|
| 384 |
assert(cache); |
| 385 |
assert(cache_stat); |
| 386 |
|
| 387 |
/* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */ |
| 388 |
if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot")), &new_stat) < 0) |
| 389 |
return -errno; |
| 390 |
|
| 391 |
if (stat_inode_unmodified(&new_stat, cache_stat)) |
| 392 |
return 0; |
| 393 |
|
| 394 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), &v); |
| 395 |
if (r < 0) |
| 396 |
return r; |
| 397 |
|
| 398 |
if (!efi_loader_entry_name_valid(v)) |
| 399 |
return -EINVAL; |
| 400 |
|
| 401 |
*cache_stat = new_stat; |
| 402 |
free_and_replace(*cache, v); |
| 403 |
|
| 404 |
return 0; |
| 405 |
#else |
| 406 |
return -EOPNOTSUPP; |
| 407 |
#endif |
| 408 |
} |
| 409 |
|
| 410 |
int efi_get_variable_id128(const char *variable, sd_id128_t *ret) { |
| 411 |
#if ENABLE_EFI |
| 412 |
int r; |
| 413 |
|
| 414 |
assert(variable); |
| 415 |
|
| 416 |
/* This is placed here (rather than in basic/efivars.c) because code in basic/ is not allowed to link |
| 417 |
* against libsystemd.so */ |
| 418 |
|
| 419 |
_cleanup_free_ char *p = NULL; |
| 420 |
r = efi_get_variable_string(variable, &p); |
| 421 |
if (r < 0) |
| 422 |
return r; |
| 423 |
|
| 424 |
return sd_id128_from_string(p, ret); |
| 425 |
#else |
| 426 |
return -EOPNOTSUPP; |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
bool efi_loader_entry_name_valid(const char *s) { |
| 431 |
if (!filename_is_valid(s)) /* Make sure entry names fit in filenames */ |
| 432 |
return false; |
| 433 |
|
| 434 |
return in_charset(s, ALPHANUMERICAL "+-_.@"); |
| 435 |
} |
| 436 |
|
| 437 |
bool efi_loader_entry_title_valid(const char *s) { |
| 438 |
return string_is_safe(s, /* flags= */ 0); |
| 439 |
} |
| 440 |
|
| 441 |
bool efi_loader_entry_resource_filename_valid(const char *s) { |
| 442 |
/* Validates file names so that they are safe for their inclusion in boot loader type #1 |
| 443 |
* entries. i.e. may not contain CCs, and should be ASCII */ |
| 444 |
|
| 445 |
return string_is_safe(s, STRING_ASCII|STRING_FILENAME); |
| 446 |
} |