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