| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "console.h" |
| 4 |
#include "efi-efivars.h" |
| 5 |
#include "efi-log.h" |
| 6 |
#include "efi-string.h" |
| 7 |
#include "efi-string-table.h" |
| 8 |
#include "proto/security-arch.h" |
| 9 |
#include "secure-boot.h" |
| 10 |
#include "util.h" |
| 11 |
#include "vmm.h" |
| 12 |
|
| 13 |
bool secure_boot_enabled(void) { |
| 14 |
bool secure = false; /* avoid false maybe-uninitialized warning */ |
| 15 |
EFI_STATUS err; |
| 16 |
|
| 17 |
err = efivar_get_boolean_u8(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"SecureBoot", &secure); |
| 18 |
|
| 19 |
return err == EFI_SUCCESS && secure; |
| 20 |
} |
| 21 |
|
| 22 |
SecureBootMode secure_boot_mode(void) { |
| 23 |
bool secure, audit = false, deployed = false, setup = false, moksb = false; |
| 24 |
EFI_STATUS err; |
| 25 |
|
| 26 |
err = efivar_get_boolean_u8(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"SecureBoot", &secure); |
| 27 |
if (err != EFI_SUCCESS) |
| 28 |
return SECURE_BOOT_UNSUPPORTED; |
| 29 |
|
| 30 |
/* We can assume false for all these if they are abscent (AuditMode and |
| 31 |
* DeployedMode may not exist on older firmware). */ |
| 32 |
(void) efivar_get_boolean_u8(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"AuditMode", &audit); |
| 33 |
(void) efivar_get_boolean_u8(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"DeployedMode", &deployed); |
| 34 |
(void) efivar_get_boolean_u8(MAKE_GUID_PTR(EFI_GLOBAL_VARIABLE), u"SetupMode", &setup); |
| 35 |
(void) efivar_get_boolean_u8(MAKE_GUID_PTR(SHIM_LOCK), u"MokSBStateRT", &moksb); |
| 36 |
|
| 37 |
return decode_secure_boot_mode(secure, audit, deployed, setup, moksb); |
| 38 |
} |
| 39 |
|
| 40 |
/* |
| 41 |
* Custom mode allows the secure boot certificate databases db, dbx, KEK, and PK to be changed without the variable |
| 42 |
* updates being signed. When enrolling certificates to an unconfigured system (no PK present yet) writing |
| 43 |
* db, dbx and KEK updates without signature works fine even in standard mode. Writing PK updates without |
| 44 |
* signature requires custom mode in any case. |
| 45 |
* |
| 46 |
* Enabling custom mode works only if a user is physically present. Note that OVMF has a dummy |
| 47 |
* implementation for the user presence check (there is no useful way to implement a presence check for a |
| 48 |
* virtual machine). |
| 49 |
* |
| 50 |
* FYI: Your firmware setup utility might offers the option to enroll certificates from *.crt files |
| 51 |
* (DER-encoded x509 certificates) on the ESP; that uses custom mode too. Your firmware setup might also |
| 52 |
* offer the option to switch the system into custom mode for the next boot. |
| 53 |
*/ |
| 54 |
static bool custom_mode_enabled(void) { |
| 55 |
bool enabled = false; |
| 56 |
|
| 57 |
(void) efivar_get_boolean_u8(MAKE_GUID_PTR(EFI_CUSTOM_MODE_ENABLE), |
| 58 |
u"CustomMode", &enabled); |
| 59 |
return enabled; |
| 60 |
} |
| 61 |
|
| 62 |
static EFI_STATUS set_custom_mode(bool enable) { |
| 63 |
static char16_t name[] = u"CustomMode"; |
| 64 |
static uint32_t attr = |
| 65 |
EFI_VARIABLE_NON_VOLATILE | |
| 66 |
EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 67 |
uint8_t mode = enable |
| 68 |
? 1 /* CUSTOM_SECURE_BOOT_MODE */ |
| 69 |
: 0; /* STANDARD_SECURE_BOOT_MODE */ |
| 70 |
|
| 71 |
return RT->SetVariable(name, MAKE_GUID_PTR(EFI_CUSTOM_MODE_ENABLE), |
| 72 |
attr, sizeof(mode), &mode); |
| 73 |
} |
| 74 |
|
| 75 |
EFI_STATUS secure_boot_enroll_at(EFI_FILE *root_dir, const char16_t *path, bool force, |
| 76 |
secure_boot_enroll_action action, uint64_t timeout_sec) { |
| 77 |
assert(root_dir); |
| 78 |
assert(path); |
| 79 |
|
| 80 |
bool need_custom_mode = false; |
| 81 |
EFI_STATUS err; |
| 82 |
|
| 83 |
clear_screen(COLOR_NORMAL); |
| 84 |
|
| 85 |
/* Enrolling secure boot keys is safe to do in virtualized environments as there is nothing |
| 86 |
* we can brick there. */ |
| 87 |
bool is_safe = in_hypervisor(); |
| 88 |
|
| 89 |
if (!is_safe && !force) |
| 90 |
return EFI_SUCCESS; |
| 91 |
|
| 92 |
printf("Enrolling secure boot keys from directory: %ls\n", path); |
| 93 |
|
| 94 |
if (!is_safe && timeout_sec != ENROLL_TIMEOUT_HIDDEN) { |
| 95 |
printf("Warning: Enrolling custom Secure Boot keys might soft-brick your machine!\n"); |
| 96 |
|
| 97 |
for (;;) { |
| 98 |
printf("\rEnrolling in %"PRIu64"s, press any key to abort.", timeout_sec); |
| 99 |
|
| 100 |
err = console_key_read(/* ret_key= */ NULL, /* timeout_usec= */ 1000 * 1000); |
| 101 |
if (err == EFI_NOT_READY) |
| 102 |
continue; |
| 103 |
if (err == EFI_TIMEOUT) { |
| 104 |
if (timeout_sec == 0) /* continue enrolling keys */ |
| 105 |
break; |
| 106 |
timeout_sec--; |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
printf("\n"); |
| 111 |
|
| 112 |
if (err != EFI_SUCCESS) |
| 113 |
return log_error_status( |
| 114 |
err, |
| 115 |
"Error waiting for user input to enroll Secure Boot keys: %m"); |
| 116 |
|
| 117 |
/* user aborted, returning EFI_SUCCESS here allows the user to go back to the menu */ |
| 118 |
return EFI_SUCCESS; |
| 119 |
} |
| 120 |
|
| 121 |
printf("\n"); |
| 122 |
} |
| 123 |
|
| 124 |
_cleanup_file_close_ EFI_FILE *dir = NULL; |
| 125 |
|
| 126 |
err = open_directory(root_dir, path, &dir); |
| 127 |
if (err != EFI_SUCCESS) |
| 128 |
return log_error_status(err, "Failed to open keys directory %ls: %m", path); |
| 129 |
|
| 130 |
struct { |
| 131 |
const char16_t *name; |
| 132 |
const char16_t *filename; |
| 133 |
const EFI_GUID vendor; |
| 134 |
bool required; |
| 135 |
char *buffer; |
| 136 |
size_t size; |
| 137 |
} sb_vars[] = { |
| 138 |
{ u"db", u"db.auth", EFI_IMAGE_SECURITY_DATABASE_GUID, true }, |
| 139 |
{ u"dbx", u"dbx.auth", EFI_IMAGE_SECURITY_DATABASE_GUID, false }, |
| 140 |
{ u"KEK", u"KEK.auth", EFI_GLOBAL_VARIABLE, true }, |
| 141 |
{ u"PK", u"PK.auth", EFI_GLOBAL_VARIABLE, true }, |
| 142 |
}; |
| 143 |
|
| 144 |
/* Make sure all keys files exist before we start enrolling them by loading them from the disk first. */ |
| 145 |
FOREACH_ELEMENT(sb_var, sb_vars) { |
| 146 |
err = file_read(dir, sb_var->filename, 0, 0, &sb_var->buffer, &sb_var->size); |
| 147 |
if (err != EFI_SUCCESS && sb_var->required) { |
| 148 |
log_error_status(err, "Failed to read file %ls\\%ls: %m", path, sb_var->filename); |
| 149 |
goto out_deallocate; |
| 150 |
} |
| 151 |
if (streq16(sb_var->name, u"PK") && sb_var->size > 20) { |
| 152 |
assert(sb_var->buffer); |
| 153 |
/* |
| 154 |
* The buffer should be EFI_TIME (16 bytes), followed by |
| 155 |
* EFI_VARIABLE_AUTHENTICATION_2 header. First header field is the size. If the |
| 156 |
* size covers only the header itself (8 bytes) plus the signature type guid (16 |
| 157 |
* bytes), leaving no space for an actual signature, we can conclude that no |
| 158 |
* signature is present. |
| 159 |
*/ |
| 160 |
uint32_t *sigsize = (uint32_t*)(sb_var->buffer + 16); |
| 161 |
if (*sigsize <= 24) { |
| 162 |
printf("PK is not signed (need custom mode).\n"); |
| 163 |
need_custom_mode = true; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if (need_custom_mode && !custom_mode_enabled()) { |
| 169 |
err = set_custom_mode(/* enable= */ true); |
| 170 |
if (err != EFI_SUCCESS) { |
| 171 |
log_error_status(err, "Failed to enable custom mode: %m"); |
| 172 |
goto out_deallocate; |
| 173 |
} |
| 174 |
printf("Custom mode enabled.\n"); |
| 175 |
} |
| 176 |
|
| 177 |
FOREACH_ELEMENT(sb_var, sb_vars) { |
| 178 |
uint32_t sb_vars_opts = |
| 179 |
EFI_VARIABLE_NON_VOLATILE | |
| 180 |
EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 181 |
EFI_VARIABLE_RUNTIME_ACCESS | |
| 182 |
EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
| 183 |
|
| 184 |
if (!sb_var->buffer) |
| 185 |
continue; |
| 186 |
|
| 187 |
err = efivar_set_raw(&sb_var->vendor, sb_var->name, sb_var->buffer, sb_var->size, sb_vars_opts); |
| 188 |
if (err != EFI_SUCCESS) { |
| 189 |
log_error_status(err, "Failed to write %ls secure boot variable: %m", sb_var->name); |
| 190 |
goto out_deallocate; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/* The system should be in secure boot mode now and we could continue a regular boot. But at least |
| 195 |
* TPM PCR7 measurements should change on next boot. Reboot/poweroff now so that any OS we load |
| 196 |
* does not end up relying on the old PCR state. |
| 197 |
*/ |
| 198 |
|
| 199 |
if (action == ENROLL_ACTION_SHUTDOWN) { |
| 200 |
printf("Custom Secure Boot keys successfully enrolled, powering off the system now!\n"); |
| 201 |
console_key_read(/* ret_key= */ NULL, /* timeout_usec= */ 2 * 1000 * 1000); /* wait a bit so user can see the message */ |
| 202 |
RT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL); |
| 203 |
} else { |
| 204 |
assert(action == ENROLL_ACTION_REBOOT); |
| 205 |
printf("Custom Secure Boot keys successfully enrolled, rebooting the system now!\n"); |
| 206 |
console_key_read(/* ret_key= */ NULL, /* timeout_usec= */ 2 * 1000 * 1000); /* wait a bit so user can see the message */ |
| 207 |
RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); |
| 208 |
} |
| 209 |
assert_not_reached(); |
| 210 |
|
| 211 |
out_deallocate: |
| 212 |
FOREACH_ELEMENT(sb_var, sb_vars) |
| 213 |
free(sb_var->buffer); |
| 214 |
|
| 215 |
return err; |
| 216 |
} |
| 217 |
|
| 218 |
static struct SecurityOverride { |
| 219 |
EFI_SECURITY_ARCH_PROTOCOL *security; |
| 220 |
EFI_SECURITY2_ARCH_PROTOCOL *security2; |
| 221 |
EFI_SECURITY_FILE_AUTHENTICATION_STATE original_hook; |
| 222 |
EFI_SECURITY2_FILE_AUTHENTICATION original_hook2; |
| 223 |
|
| 224 |
security_validator_t validator; |
| 225 |
const void *validator_ctx; |
| 226 |
} security_override; |
| 227 |
|
| 228 |
static EFIAPI EFI_STATUS security_hook( |
| 229 |
const EFI_SECURITY_ARCH_PROTOCOL *this, |
| 230 |
uint32_t authentication_status, |
| 231 |
const EFI_DEVICE_PATH *file) { |
| 232 |
|
| 233 |
assert(security_override.validator); |
| 234 |
assert(security_override.security); |
| 235 |
assert(security_override.original_hook); |
| 236 |
|
| 237 |
if (security_override.validator(security_override.validator_ctx, file, NULL, 0)) |
| 238 |
return EFI_SUCCESS; |
| 239 |
|
| 240 |
return security_override.original_hook(security_override.security, authentication_status, file); |
| 241 |
} |
| 242 |
|
| 243 |
static EFIAPI EFI_STATUS security2_hook( |
| 244 |
const EFI_SECURITY2_ARCH_PROTOCOL *this, |
| 245 |
const EFI_DEVICE_PATH *device_path, |
| 246 |
void *file_buffer, |
| 247 |
size_t file_size, |
| 248 |
bool boot_policy) { |
| 249 |
|
| 250 |
assert(security_override.validator); |
| 251 |
assert(security_override.security2); |
| 252 |
assert(security_override.original_hook2); |
| 253 |
|
| 254 |
if (security_override.validator(security_override.validator_ctx, device_path, file_buffer, file_size)) |
| 255 |
return EFI_SUCCESS; |
| 256 |
|
| 257 |
return security_override.original_hook2( |
| 258 |
security_override.security2, device_path, file_buffer, file_size, boot_policy); |
| 259 |
} |
| 260 |
|
| 261 |
/* This replaces the platform provided security arch protocols hooks (defined in the UEFI Platform |
| 262 |
* Initialization Specification) with our own that uses the given validator to decide if a image is to be |
| 263 |
* trusted. If not running in secure boot or the protocols are not available nothing happens. The override |
| 264 |
* must be removed with uninstall_security_override() after LoadImage() has been called. |
| 265 |
* |
| 266 |
* This is a hack as we do not own the security protocol instances and modifying them is not an official part |
| 267 |
* of their spec. But there is little else we can do to circumvent secure boot short of implementing our own |
| 268 |
* PE loader. We could replace the firmware instances with our own instance using |
| 269 |
* ReinstallProtocolInterface(), but some firmware will still use the old ones. */ |
| 270 |
// TODO: now that there is a custom PE loader, this can be dropped once shim < v16 is no longer supported. |
| 271 |
void install_security_override(security_validator_t validator, const void *validator_ctx) { |
| 272 |
EFI_STATUS err; |
| 273 |
|
| 274 |
assert(validator); |
| 275 |
|
| 276 |
if (!secure_boot_enabled()) |
| 277 |
return; |
| 278 |
|
| 279 |
security_override = (struct SecurityOverride) { |
| 280 |
.validator = validator, |
| 281 |
.validator_ctx = validator_ctx, |
| 282 |
}; |
| 283 |
|
| 284 |
EFI_SECURITY_ARCH_PROTOCOL *security = NULL; |
| 285 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_SECURITY_ARCH_PROTOCOL), NULL, (void **) &security); |
| 286 |
if (err == EFI_SUCCESS) { |
| 287 |
security_override.security = security; |
| 288 |
security_override.original_hook = security->FileAuthenticationState; |
| 289 |
security->FileAuthenticationState = security_hook; |
| 290 |
} |
| 291 |
|
| 292 |
EFI_SECURITY2_ARCH_PROTOCOL *security2 = NULL; |
| 293 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_SECURITY2_ARCH_PROTOCOL), NULL, (void **) &security2); |
| 294 |
if (err == EFI_SUCCESS) { |
| 295 |
security_override.security2 = security2; |
| 296 |
security_override.original_hook2 = security2->FileAuthentication; |
| 297 |
security2->FileAuthentication = security2_hook; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
bool security_override_available(void) { |
| 302 |
EFI_SECURITY_ARCH_PROTOCOL *security; |
| 303 |
EFI_SECURITY2_ARCH_PROTOCOL *security2; |
| 304 |
|
| 305 |
return BS->LocateProtocol(MAKE_GUID_PTR(EFI_SECURITY_ARCH_PROTOCOL), NULL, (void **) &security) == EFI_SUCCESS && |
| 306 |
BS->LocateProtocol(MAKE_GUID_PTR(EFI_SECURITY2_ARCH_PROTOCOL), NULL, (void **) &security2) == EFI_SUCCESS; |
| 307 |
} |
| 308 |
|
| 309 |
void uninstall_security_override(void) { |
| 310 |
if (security_override.original_hook) |
| 311 |
security_override.security->FileAuthenticationState = security_override.original_hook; |
| 312 |
if (security_override.original_hook2) |
| 313 |
security_override.security2->FileAuthentication = security_override.original_hook2; |
| 314 |
} |
| 315 |
|
| 316 |
static const char *secure_boot_enroll_table[_SECURE_BOOT_ENROLL_MAX] = { |
| 317 |
[ENROLL_OFF] = "off", |
| 318 |
[ENROLL_MANUAL] = "manual", |
| 319 |
[ENROLL_IF_SAFE] = "if-safe", |
| 320 |
[ENROLL_FORCE] = "force" |
| 321 |
}; |
| 322 |
|
| 323 |
static const char *secure_boot_enroll_action_table[_SECURE_BOOT_ENROLL_ACTION_MAX] = { |
| 324 |
[ENROLL_ACTION_REBOOT] = "reboot", |
| 325 |
[ENROLL_ACTION_SHUTDOWN] = "shutdown" |
| 326 |
}; |
| 327 |
|
| 328 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(secure_boot_enroll, secure_boot_enroll); |
| 329 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(secure_boot_enroll_action, secure_boot_enroll_action); |