Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * UEFI runtime variable services |
| 4 | * |
| 5 | * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de> |
AKASHI Takahiro | 200647a | 2020-07-30 16:33:59 +0900 | [diff] [blame] | 6 | * Copyright (c) 2020 Linaro Limited, Author: AKASHI Takahiro |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <efi_loader.h> |
| 11 | #include <efi_variable.h> |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 12 | #include <stdlib.h> |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 13 | |
Heinrich Schuchardt | 0f2c333 | 2020-07-14 08:04:49 +0200 | [diff] [blame] | 14 | enum efi_secure_mode { |
| 15 | EFI_MODE_SETUP, |
| 16 | EFI_MODE_USER, |
| 17 | EFI_MODE_AUDIT, |
| 18 | EFI_MODE_DEPLOYED, |
| 19 | }; |
| 20 | |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 21 | struct efi_auth_var_name_type { |
| 22 | const u16 *name; |
| 23 | const efi_guid_t *guid; |
| 24 | const enum efi_auth_var_type type; |
| 25 | }; |
| 26 | |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 27 | const efi_guid_t efi_guid_image_security_database = |
| 28 | EFI_IMAGE_SECURITY_DATABASE_GUID; |
| 29 | |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 30 | static const struct efi_auth_var_name_type name_type[] = { |
| 31 | {u"PK", &efi_global_variable_guid, EFI_AUTH_VAR_PK}, |
| 32 | {u"KEK", &efi_global_variable_guid, EFI_AUTH_VAR_KEK}, |
| 33 | {u"db", &efi_guid_image_security_database, EFI_AUTH_VAR_DB}, |
| 34 | {u"dbx", &efi_guid_image_security_database, EFI_AUTH_VAR_DBX}, |
| 35 | /* not used yet |
| 36 | {u"dbt", &efi_guid_image_security_database, EFI_AUTH_VAR_DBT}, |
| 37 | {u"dbr", &efi_guid_image_security_database, EFI_AUTH_VAR_DBR}, |
| 38 | */ |
| 39 | }; |
| 40 | |
Heinrich Schuchardt | 0f2c333 | 2020-07-14 08:04:49 +0200 | [diff] [blame] | 41 | static bool efi_secure_boot; |
| 42 | static enum efi_secure_mode efi_secure_mode; |
| 43 | |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 44 | /** |
| 45 | * efi_efi_get_variable() - retrieve value of a UEFI variable |
| 46 | * |
| 47 | * This function implements the GetVariable runtime service. |
| 48 | * |
| 49 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 50 | * details. |
| 51 | * |
| 52 | * @variable_name: name of the variable |
| 53 | * @vendor: vendor GUID |
| 54 | * @attributes: attributes of the variable |
| 55 | * @data_size: size of the buffer to which the variable value is copied |
| 56 | * @data: buffer to which the variable value is copied |
| 57 | * Return: status code |
| 58 | */ |
| 59 | efi_status_t EFIAPI efi_get_variable(u16 *variable_name, |
| 60 | const efi_guid_t *vendor, u32 *attributes, |
| 61 | efi_uintn_t *data_size, void *data) |
| 62 | { |
| 63 | efi_status_t ret; |
| 64 | |
| 65 | EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes, |
| 66 | data_size, data); |
| 67 | |
| 68 | ret = efi_get_variable_int(variable_name, vendor, attributes, |
| 69 | data_size, data, NULL); |
| 70 | |
| 71 | /* Remove EFI_VARIABLE_READ_ONLY flag */ |
| 72 | if (attributes) |
| 73 | *attributes &= EFI_VARIABLE_MASK; |
| 74 | |
| 75 | return EFI_EXIT(ret); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * efi_set_variable() - set value of a UEFI variable |
| 80 | * |
| 81 | * This function implements the SetVariable runtime service. |
| 82 | * |
| 83 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 84 | * details. |
| 85 | * |
| 86 | * @variable_name: name of the variable |
| 87 | * @vendor: vendor GUID |
| 88 | * @attributes: attributes of the variable |
| 89 | * @data_size: size of the buffer with the variable value |
| 90 | * @data: buffer with the variable value |
| 91 | * Return: status code |
| 92 | */ |
| 93 | efi_status_t EFIAPI efi_set_variable(u16 *variable_name, |
| 94 | const efi_guid_t *vendor, u32 attributes, |
| 95 | efi_uintn_t data_size, const void *data) |
| 96 | { |
| 97 | efi_status_t ret; |
| 98 | |
| 99 | EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes, |
| 100 | data_size, data); |
| 101 | |
| 102 | /* Make sure that the EFI_VARIABLE_READ_ONLY flag is not set */ |
| 103 | if (attributes & ~(u32)EFI_VARIABLE_MASK) |
| 104 | ret = EFI_INVALID_PARAMETER; |
| 105 | else |
| 106 | ret = efi_set_variable_int(variable_name, vendor, attributes, |
| 107 | data_size, data, true); |
| 108 | |
| 109 | return EFI_EXIT(ret); |
| 110 | } |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * efi_get_next_variable_name() - enumerate the current variable names |
| 114 | * |
| 115 | * @variable_name_size: size of variable_name buffer in byte |
| 116 | * @variable_name: name of uefi variable's name in u16 |
| 117 | * @vendor: vendor's guid |
| 118 | * |
| 119 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 120 | * details. |
| 121 | * |
| 122 | * Return: status code |
| 123 | */ |
| 124 | efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, |
| 125 | u16 *variable_name, |
| 126 | efi_guid_t *vendor) |
| 127 | { |
| 128 | efi_status_t ret; |
| 129 | |
| 130 | EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor); |
| 131 | |
| 132 | ret = efi_get_next_variable_name_int(variable_name_size, variable_name, |
| 133 | vendor); |
| 134 | |
| 135 | return EFI_EXIT(ret); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * efi_query_variable_info() - get information about EFI variables |
| 140 | * |
| 141 | * This function implements the QueryVariableInfo() runtime service. |
| 142 | * |
| 143 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 144 | * details. |
| 145 | * |
| 146 | * @attributes: bitmask to select variables to be |
| 147 | * queried |
| 148 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 149 | * selected variable types |
| 150 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 151 | * selected variable types |
| 152 | * @maximum_variable_size: maximum size of a variable of the |
| 153 | * selected type |
| 154 | * Returns: status code |
| 155 | */ |
| 156 | efi_status_t EFIAPI efi_query_variable_info( |
| 157 | u32 attributes, u64 *maximum_variable_storage_size, |
| 158 | u64 *remaining_variable_storage_size, |
| 159 | u64 *maximum_variable_size) |
| 160 | { |
| 161 | efi_status_t ret; |
| 162 | |
| 163 | EFI_ENTRY("%x %p %p %p", attributes, maximum_variable_storage_size, |
| 164 | remaining_variable_storage_size, maximum_variable_size); |
| 165 | |
Masami Hiramatsu | aee5330 | 2021-07-01 00:49:48 +0900 | [diff] [blame] | 166 | if (!maximum_variable_storage_size || |
| 167 | !remaining_variable_storage_size || |
| 168 | !maximum_variable_size || |
| 169 | !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)) |
| 170 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 171 | |
| 172 | if ((attributes & ~(u32)EFI_VARIABLE_MASK) || |
| 173 | (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) || |
| 174 | (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) || |
| 175 | (!IS_ENABLED(CONFIG_EFI_SECURE_BOOT) && |
| 176 | (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS))) |
| 177 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 178 | |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 179 | ret = efi_query_variable_info_int(attributes, |
| 180 | maximum_variable_storage_size, |
| 181 | remaining_variable_storage_size, |
| 182 | maximum_variable_size); |
| 183 | |
| 184 | return EFI_EXIT(ret); |
| 185 | } |
Heinrich Schuchardt | 0f2c333 | 2020-07-14 08:04:49 +0200 | [diff] [blame] | 186 | |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 187 | efi_status_t __efi_runtime EFIAPI |
| 188 | efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid, |
| 189 | u32 *attributes, efi_uintn_t *data_size, void *data) |
| 190 | { |
| 191 | efi_status_t ret; |
| 192 | |
| 193 | ret = efi_get_variable_mem(variable_name, guid, attributes, data_size, data, NULL); |
| 194 | |
| 195 | /* Remove EFI_VARIABLE_READ_ONLY flag */ |
| 196 | if (attributes) |
| 197 | *attributes &= EFI_VARIABLE_MASK; |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | efi_status_t __efi_runtime EFIAPI |
| 203 | efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size, |
| 204 | u16 *variable_name, efi_guid_t *guid) |
| 205 | { |
| 206 | return efi_get_next_variable_name_mem(variable_name_size, variable_name, guid); |
| 207 | } |
| 208 | |
Heinrich Schuchardt | 0f2c333 | 2020-07-14 08:04:49 +0200 | [diff] [blame] | 209 | /** |
| 210 | * efi_set_secure_state - modify secure boot state variables |
| 211 | * @secure_boot: value of SecureBoot |
| 212 | * @setup_mode: value of SetupMode |
| 213 | * @audit_mode: value of AuditMode |
| 214 | * @deployed_mode: value of DeployedMode |
| 215 | * |
| 216 | * Modify secure boot status related variables as indicated. |
| 217 | * |
| 218 | * Return: status code |
| 219 | */ |
| 220 | static efi_status_t efi_set_secure_state(u8 secure_boot, u8 setup_mode, |
| 221 | u8 audit_mode, u8 deployed_mode) |
| 222 | { |
| 223 | efi_status_t ret; |
| 224 | const u32 attributes_ro = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 225 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 226 | EFI_VARIABLE_READ_ONLY; |
| 227 | const u32 attributes_rw = EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 228 | EFI_VARIABLE_RUNTIME_ACCESS; |
| 229 | |
| 230 | efi_secure_boot = secure_boot; |
| 231 | |
| 232 | ret = efi_set_variable_int(L"SecureBoot", &efi_global_variable_guid, |
| 233 | attributes_ro, sizeof(secure_boot), |
| 234 | &secure_boot, false); |
| 235 | if (ret != EFI_SUCCESS) |
| 236 | goto err; |
| 237 | |
| 238 | ret = efi_set_variable_int(L"SetupMode", &efi_global_variable_guid, |
| 239 | attributes_ro, sizeof(setup_mode), |
| 240 | &setup_mode, false); |
| 241 | if (ret != EFI_SUCCESS) |
| 242 | goto err; |
| 243 | |
| 244 | ret = efi_set_variable_int(L"AuditMode", &efi_global_variable_guid, |
| 245 | audit_mode || setup_mode ? |
| 246 | attributes_ro : attributes_rw, |
| 247 | sizeof(audit_mode), &audit_mode, false); |
| 248 | if (ret != EFI_SUCCESS) |
| 249 | goto err; |
| 250 | |
| 251 | ret = efi_set_variable_int(L"DeployedMode", |
| 252 | &efi_global_variable_guid, |
| 253 | audit_mode || deployed_mode || setup_mode ? |
| 254 | attributes_ro : attributes_rw, |
| 255 | sizeof(deployed_mode), &deployed_mode, |
| 256 | false); |
| 257 | err: |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * efi_transfer_secure_state - handle a secure boot state transition |
| 263 | * @mode: new state |
| 264 | * |
| 265 | * Depending on @mode, secure boot related variables are updated. |
| 266 | * Those variables are *read-only* for users, efi_set_variable_int() |
| 267 | * is called here. |
| 268 | * |
| 269 | * Return: status code |
| 270 | */ |
| 271 | static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode) |
| 272 | { |
| 273 | efi_status_t ret; |
| 274 | |
| 275 | EFI_PRINT("Switching secure state from %d to %d\n", efi_secure_mode, |
| 276 | mode); |
| 277 | |
| 278 | if (mode == EFI_MODE_DEPLOYED) { |
| 279 | ret = efi_set_secure_state(1, 0, 0, 1); |
| 280 | if (ret != EFI_SUCCESS) |
| 281 | goto err; |
| 282 | } else if (mode == EFI_MODE_AUDIT) { |
| 283 | ret = efi_set_variable_int(L"PK", &efi_global_variable_guid, |
| 284 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 285 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 286 | 0, NULL, false); |
| 287 | if (ret != EFI_SUCCESS) |
| 288 | goto err; |
| 289 | |
| 290 | ret = efi_set_secure_state(0, 1, 1, 0); |
| 291 | if (ret != EFI_SUCCESS) |
| 292 | goto err; |
| 293 | } else if (mode == EFI_MODE_USER) { |
| 294 | ret = efi_set_secure_state(1, 0, 0, 0); |
| 295 | if (ret != EFI_SUCCESS) |
| 296 | goto err; |
| 297 | } else if (mode == EFI_MODE_SETUP) { |
| 298 | ret = efi_set_secure_state(0, 1, 0, 0); |
| 299 | if (ret != EFI_SUCCESS) |
| 300 | goto err; |
| 301 | } else { |
| 302 | return EFI_INVALID_PARAMETER; |
| 303 | } |
| 304 | |
| 305 | efi_secure_mode = mode; |
| 306 | |
| 307 | return EFI_SUCCESS; |
| 308 | |
| 309 | err: |
| 310 | /* TODO: What action should be taken here? */ |
| 311 | printf("ERROR: Secure state transition failed\n"); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | efi_status_t efi_init_secure_state(void) |
| 316 | { |
| 317 | enum efi_secure_mode mode = EFI_MODE_SETUP; |
| 318 | u8 efi_vendor_keys = 0; |
| 319 | efi_uintn_t size = 0; |
| 320 | efi_status_t ret; |
| 321 | |
| 322 | ret = efi_get_variable_int(L"PK", &efi_global_variable_guid, |
| 323 | NULL, &size, NULL, NULL); |
| 324 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 325 | if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) |
| 326 | mode = EFI_MODE_USER; |
| 327 | } |
| 328 | |
| 329 | ret = efi_transfer_secure_state(mode); |
| 330 | if (ret != EFI_SUCCESS) |
| 331 | return ret; |
| 332 | |
| 333 | /* As we do not provide vendor keys this variable is always 0. */ |
| 334 | ret = efi_set_variable_int(L"VendorKeys", |
| 335 | &efi_global_variable_guid, |
| 336 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 337 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 338 | EFI_VARIABLE_READ_ONLY, |
| 339 | sizeof(efi_vendor_keys), |
| 340 | &efi_vendor_keys, false); |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * efi_secure_boot_enabled - return if secure boot is enabled or not |
| 346 | * |
| 347 | * Return: true if enabled, false if disabled |
| 348 | */ |
| 349 | bool efi_secure_boot_enabled(void) |
| 350 | { |
| 351 | return efi_secure_boot; |
| 352 | } |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 353 | |
| 354 | enum efi_auth_var_type efi_auth_var_get_type(u16 *name, const efi_guid_t *guid) |
| 355 | { |
| 356 | for (size_t i = 0; i < ARRAY_SIZE(name_type); ++i) { |
| 357 | if (!u16_strcmp(name, name_type[i].name) && |
| 358 | !guidcmp(guid, name_type[i].guid)) |
| 359 | return name_type[i].type; |
| 360 | } |
| 361 | return EFI_AUTH_VAR_NONE; |
| 362 | } |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * efi_get_var() - read value of an EFI variable |
| 366 | * |
| 367 | * @name: variable name |
| 368 | * @start: vendor GUID |
| 369 | * @size: size of allocated buffer |
| 370 | * |
| 371 | * Return: buffer with variable data or NULL |
| 372 | */ |
| 373 | void *efi_get_var(u16 *name, const efi_guid_t *vendor, efi_uintn_t *size) |
| 374 | { |
| 375 | efi_status_t ret; |
| 376 | void *buf = NULL; |
| 377 | |
| 378 | *size = 0; |
| 379 | ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL); |
| 380 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 381 | buf = malloc(*size); |
| 382 | if (!buf) |
| 383 | return NULL; |
| 384 | ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL); |
| 385 | } |
| 386 | |
| 387 | if (ret != EFI_SUCCESS) { |
| 388 | free(buf); |
| 389 | *size = 0; |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | return buf; |
| 394 | } |