Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | f0b0f7f | 2020-03-19 17:15:18 +0000 | [diff] [blame] | 3 | * UEFI runtime variable services |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | f0b0f7f | 2020-03-19 17:15:18 +0000 | [diff] [blame] | 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 10 | #include <efi_loader.h> |
Heinrich Schuchardt | f2d2b3a | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 11 | #include <efi_variable.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
Heinrich Schuchardt | e731af4 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 14 | #include <hexdump.h> |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 15 | #include <log.h> |
Heinrich Schuchardt | e731af4 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 16 | #include <malloc.h> |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 17 | #include <rtc.h> |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 18 | #include <search.h> |
Simon Glass | ba06b3c | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 19 | #include <uuid.h> |
AKASHI Takahiro | e3f5c9c | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 20 | #include <crypto/pkcs7_parser.h> |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 21 | #include <linux/compat.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 22 | #include <u-boot/crc.h> |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 23 | #include <asm/sections.h> |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_EFI_SECURE_BOOT |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * efi_variable_authenticate - authenticate a variable |
| 29 | * @variable: Variable name in u16 |
| 30 | * @vendor: Guid of variable |
| 31 | * @data_size: Size of @data |
| 32 | * @data: Pointer to variable's value |
| 33 | * @given_attr: Attributes to be given at SetVariable() |
| 34 | * @env_attr: Attributes that an existing variable holds |
| 35 | * @time: signed time that an existing variable holds |
| 36 | * |
| 37 | * Called by efi_set_variable() to verify that the input is correct. |
| 38 | * Will replace the given data pointer with another that points to |
| 39 | * the actual data to store in the internal memory. |
| 40 | * On success, @data and @data_size will be replaced with variable's |
| 41 | * actual data, excluding authentication data, and its size, and variable's |
| 42 | * attributes and signed time will also be returned in @env_attr and @time, |
| 43 | * respectively. |
| 44 | * |
Heinrich Schuchardt | 30f92ce | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 45 | * Return: status code |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 46 | */ |
Heinrich Schuchardt | d47671c | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 47 | static efi_status_t efi_variable_authenticate(const u16 *variable, |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 48 | const efi_guid_t *vendor, |
| 49 | efi_uintn_t *data_size, |
| 50 | const void **data, u32 given_attr, |
| 51 | u32 *env_attr, u64 *time) |
| 52 | { |
| 53 | const struct efi_variable_authentication_2 *auth; |
| 54 | struct efi_signature_store *truststore, *truststore2; |
| 55 | struct pkcs7_message *var_sig; |
| 56 | struct efi_image_regions *regs; |
| 57 | struct efi_time timestamp; |
| 58 | struct rtc_time tm; |
| 59 | u64 new_time; |
AKASHI Takahiro | 0658bb2 | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 60 | u8 *ebuf; |
Heinrich Schuchardt | 99bfab8 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 61 | enum efi_auth_var_type var_type; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 62 | efi_status_t ret; |
| 63 | |
| 64 | var_sig = NULL; |
| 65 | truststore = NULL; |
| 66 | truststore2 = NULL; |
| 67 | regs = NULL; |
AKASHI Takahiro | 0658bb2 | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 68 | ebuf = NULL; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 69 | ret = EFI_SECURITY_VIOLATION; |
| 70 | |
| 71 | if (*data_size < sizeof(struct efi_variable_authentication_2)) |
| 72 | goto err; |
| 73 | |
| 74 | /* authentication data */ |
| 75 | auth = *data; |
| 76 | if (*data_size < (sizeof(auth->time_stamp) |
| 77 | + auth->auth_info.hdr.dwLength)) |
| 78 | goto err; |
| 79 | |
| 80 | if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) |
| 81 | goto err; |
| 82 | |
Heinrich Schuchardt | 33f183f | 2020-07-01 12:44:00 +0200 | [diff] [blame] | 83 | memcpy(×tamp, &auth->time_stamp, sizeof(timestamp)); |
| 84 | if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone || |
| 85 | timestamp.daylight || timestamp.pad2) |
| 86 | goto err; |
| 87 | |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 88 | *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength; |
| 89 | *data_size -= (sizeof(auth->time_stamp) |
| 90 | + auth->auth_info.hdr.dwLength); |
| 91 | |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 92 | memset(&tm, 0, sizeof(tm)); |
| 93 | tm.tm_year = timestamp.year; |
| 94 | tm.tm_mon = timestamp.month; |
| 95 | tm.tm_mday = timestamp.day; |
| 96 | tm.tm_hour = timestamp.hour; |
| 97 | tm.tm_min = timestamp.minute; |
| 98 | tm.tm_sec = timestamp.second; |
| 99 | new_time = rtc_mktime(&tm); |
| 100 | |
| 101 | if (!efi_secure_boot_enabled()) { |
| 102 | /* finished checking */ |
| 103 | *time = new_time; |
| 104 | return EFI_SUCCESS; |
| 105 | } |
| 106 | |
| 107 | if (new_time <= *time) |
| 108 | goto err; |
| 109 | |
| 110 | /* data to be digested */ |
| 111 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1); |
| 112 | if (!regs) |
| 113 | goto err; |
| 114 | regs->max = 5; |
| 115 | efi_image_region_add(regs, (uint8_t *)variable, |
| 116 | (uint8_t *)variable |
| 117 | + u16_strlen(variable) * sizeof(u16), 1); |
| 118 | efi_image_region_add(regs, (uint8_t *)vendor, |
| 119 | (uint8_t *)vendor + sizeof(*vendor), 1); |
| 120 | efi_image_region_add(regs, (uint8_t *)&given_attr, |
| 121 | (uint8_t *)&given_attr + sizeof(given_attr), 1); |
| 122 | efi_image_region_add(regs, (uint8_t *)×tamp, |
| 123 | (uint8_t *)×tamp + sizeof(timestamp), 1); |
| 124 | efi_image_region_add(regs, (uint8_t *)*data, |
| 125 | (uint8_t *)*data + *data_size, 1); |
| 126 | |
| 127 | /* variable's signature list */ |
| 128 | if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info)) |
| 129 | goto err; |
AKASHI Takahiro | 0658bb2 | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 130 | |
| 131 | /* ebuf should be kept valid during the authentication */ |
Sughosh Ganu | 201b806 | 2020-12-30 19:27:07 +0530 | [diff] [blame] | 132 | var_sig = efi_parse_pkcs7_header(auth->auth_info.cert_data, |
| 133 | auth->auth_info.hdr.dwLength |
| 134 | - sizeof(auth->auth_info), |
| 135 | &ebuf); |
Patrick Wildt | 9ad1522 | 2020-05-07 02:13:18 +0200 | [diff] [blame] | 136 | if (!var_sig) { |
AKASHI Takahiro | ce3c386 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 137 | EFI_PRINT("Parsing variable's signature failed\n"); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 138 | goto err; |
| 139 | } |
| 140 | |
| 141 | /* signature database used for authentication */ |
Heinrich Schuchardt | 99bfab8 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 142 | var_type = efi_auth_var_get_type(variable, vendor); |
| 143 | switch (var_type) { |
| 144 | case EFI_AUTH_VAR_PK: |
| 145 | case EFI_AUTH_VAR_KEK: |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 146 | /* with PK */ |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 147 | truststore = efi_sigstore_parse_sigdb(u"PK"); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 148 | if (!truststore) |
| 149 | goto err; |
Heinrich Schuchardt | 99bfab8 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 150 | break; |
| 151 | case EFI_AUTH_VAR_DB: |
| 152 | case EFI_AUTH_VAR_DBX: |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 153 | /* with PK and KEK */ |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 154 | truststore = efi_sigstore_parse_sigdb(u"KEK"); |
| 155 | truststore2 = efi_sigstore_parse_sigdb(u"PK"); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 156 | if (!truststore) { |
| 157 | if (!truststore2) |
| 158 | goto err; |
| 159 | |
| 160 | truststore = truststore2; |
| 161 | truststore2 = NULL; |
| 162 | } |
Heinrich Schuchardt | 99bfab8 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 163 | break; |
| 164 | default: |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 165 | /* TODO: support private authenticated variables */ |
Weizhao Ouyang | 0ec4279 | 2024-04-10 12:19:33 +0000 | [diff] [blame] | 166 | ret = EFI_UNSUPPORTED; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 167 | goto err; |
| 168 | } |
| 169 | |
| 170 | /* verify signature */ |
AKASHI Takahiro | 1115edd | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 171 | if (efi_signature_verify(regs, var_sig, truststore, NULL)) { |
AKASHI Takahiro | ce3c386 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 172 | EFI_PRINT("Verified\n"); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 173 | } else { |
| 174 | if (truststore2 && |
AKASHI Takahiro | 1115edd | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 175 | efi_signature_verify(regs, var_sig, truststore2, NULL)) { |
AKASHI Takahiro | ce3c386 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 176 | EFI_PRINT("Verified\n"); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 177 | } else { |
AKASHI Takahiro | ce3c386 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 178 | EFI_PRINT("Verifying variable's signature failed\n"); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 179 | goto err; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* finished checking */ |
Heinrich Schuchardt | 3a92f85 | 2020-06-30 12:02:14 +0200 | [diff] [blame] | 184 | *time = new_time; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 185 | ret = EFI_SUCCESS; |
| 186 | |
| 187 | err: |
| 188 | efi_sigstore_free(truststore); |
| 189 | efi_sigstore_free(truststore2); |
| 190 | pkcs7_free_message(var_sig); |
AKASHI Takahiro | 0658bb2 | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 191 | free(ebuf); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 192 | free(regs); |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | #else |
Heinrich Schuchardt | d47671c | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 197 | static efi_status_t efi_variable_authenticate(const u16 *variable, |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 198 | const efi_guid_t *vendor, |
| 199 | efi_uintn_t *data_size, |
| 200 | const void **data, u32 given_attr, |
| 201 | u32 *env_attr, u64 *time) |
| 202 | { |
| 203 | return EFI_SUCCESS; |
| 204 | } |
| 205 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 206 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 207 | efi_status_t __efi_runtime |
Heinrich Schuchardt | d47671c | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 208 | efi_get_variable_int(const u16 *variable_name, const efi_guid_t *vendor, |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 209 | u32 *attributes, efi_uintn_t *data_size, void *data, |
| 210 | u64 *timep) |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 211 | { |
Ilias Apalodimas | 00da8d6 | 2024-04-18 15:54:52 +0300 | [diff] [blame] | 212 | return efi_get_variable_mem(variable_name, vendor, attributes, data_size, |
| 213 | data, timep, 0); |
AKASHI Takahiro | d99a87f | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 216 | efi_status_t __efi_runtime |
| 217 | efi_get_next_variable_name_int(efi_uintn_t *variable_name_size, |
| 218 | u16 *variable_name, efi_guid_t *vendor) |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 219 | { |
Ilias Apalodimas | 00da8d6 | 2024-04-18 15:54:52 +0300 | [diff] [blame] | 220 | return efi_get_next_variable_name_mem(variable_name_size, variable_name, |
| 221 | vendor, 0); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Ilias Apalodimas | c28d32f | 2024-04-18 15:54:50 +0300 | [diff] [blame] | 224 | /** |
| 225 | * setvariable_allowed() - checks defined by the UEFI spec for setvariable |
| 226 | * |
| 227 | * @variable_name: name of the variable |
| 228 | * @vendor: vendor GUID |
| 229 | * @attributes: attributes of the variable |
| 230 | * @data_size: size of the buffer with the variable value |
| 231 | * @data: buffer with the variable value |
| 232 | * Return: status code |
| 233 | */ |
| 234 | static efi_status_t __efi_runtime |
| 235 | setvariable_allowed(const u16 *variable_name, const efi_guid_t *vendor, |
| 236 | u32 attributes, efi_uintn_t data_size, const void *data) |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 237 | { |
Masahisa Kojima | 26a3502 | 2023-02-21 11:33:17 +0900 | [diff] [blame] | 238 | if (!variable_name || !*variable_name || !vendor) |
| 239 | return EFI_INVALID_PARAMETER; |
| 240 | |
| 241 | if (data_size && !data) |
| 242 | return EFI_INVALID_PARAMETER; |
| 243 | |
Heinrich Schuchardt | e0fa2cf | 2024-04-03 17:33:35 +0200 | [diff] [blame] | 244 | /* |
| 245 | * EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated. |
| 246 | * We don't support EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS. |
| 247 | */ |
| 248 | if (attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \ |
| 249 | EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS)) |
Masahisa Kojima | 26a3502 | 2023-02-21 11:33:17 +0900 | [diff] [blame] | 250 | return EFI_UNSUPPORTED; |
| 251 | |
| 252 | /* Make sure if runtime bit is set, boot service bit is set also */ |
| 253 | if ((attributes & |
| 254 | (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == |
| 255 | EFI_VARIABLE_RUNTIME_ACCESS) |
| 256 | return EFI_INVALID_PARAMETER; |
| 257 | |
| 258 | /* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */ |
| 259 | if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE) |
| 260 | return EFI_INVALID_PARAMETER; |
| 261 | |
| 262 | /* Make sure HR is set with NV, BS and RT */ |
| 263 | if (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD && |
| 264 | (!(attributes & EFI_VARIABLE_NON_VOLATILE) || |
| 265 | !(attributes & EFI_VARIABLE_RUNTIME_ACCESS) || |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 266 | !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) |
| 267 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 268 | |
Ilias Apalodimas | c28d32f | 2024-04-18 15:54:50 +0300 | [diff] [blame] | 269 | return EFI_SUCCESS; |
| 270 | } |
| 271 | |
| 272 | efi_status_t efi_set_variable_int(const u16 *variable_name, |
| 273 | const efi_guid_t *vendor, |
| 274 | u32 attributes, efi_uintn_t data_size, |
| 275 | const void *data, bool ro_check) |
| 276 | { |
| 277 | struct efi_var_entry *var; |
| 278 | efi_uintn_t ret; |
| 279 | bool append, delete; |
| 280 | u64 time = 0; |
| 281 | enum efi_auth_var_type var_type; |
| 282 | |
| 283 | ret = setvariable_allowed(variable_name, vendor, attributes, data_size, |
| 284 | data); |
| 285 | if (ret != EFI_SUCCESS) |
| 286 | return ret; |
| 287 | |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 288 | /* check if a variable exists */ |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 289 | var = efi_var_mem_find(vendor, variable_name, NULL); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 290 | append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 291 | delete = !append && (!data_size || !attributes); |
| 292 | |
| 293 | /* check attributes */ |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 294 | var_type = efi_auth_var_get_type(variable_name, vendor); |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 295 | if (var) { |
| 296 | if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY)) |
| 297 | return EFI_WRITE_PROTECTED; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 298 | |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 299 | if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) { |
Heinrich Schuchardt | b191aa4 | 2021-08-26 04:30:24 +0200 | [diff] [blame] | 300 | if (var_type >= EFI_AUTH_VAR_PK) |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 301 | return EFI_WRITE_PROTECTED; |
| 302 | } |
| 303 | |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 304 | /* attributes won't be changed */ |
| 305 | if (!delete && |
Weizhao Ouyang | 3b7d26e | 2024-05-08 19:13:12 +0800 | [diff] [blame] | 306 | ((ro_check && var->attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE)) || |
Heinrich Schuchardt | 3b51c3a | 2024-04-03 17:33:34 +0200 | [diff] [blame] | 307 | (!ro_check && ((var->attr & ~EFI_VARIABLE_READ_ONLY) |
| 308 | != (attributes & ~EFI_VARIABLE_READ_ONLY))))) { |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 309 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 310 | } |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 311 | time = var->time; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 312 | } else { |
Masahisa Kojima | 584a2ad | 2024-04-02 18:09:50 +0900 | [diff] [blame] | 313 | /* |
| 314 | * UEFI specification does not clearly describe the expected |
| 315 | * behavior of append write with data size 0, we follow |
| 316 | * the EDK II reference implementation. |
| 317 | */ |
| 318 | if (append && !data_size) |
| 319 | return EFI_SUCCESS; |
| 320 | |
| 321 | /* |
| 322 | * EFI_VARIABLE_APPEND_WRITE to non-existent variable is accepted |
| 323 | * and new variable is created in EDK II reference implementation. |
| 324 | * We follow it and only check the deletion here. |
| 325 | */ |
| 326 | if (delete) |
| 327 | /* Trying to delete a non-existent variable. */ |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 328 | return EFI_NOT_FOUND; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Heinrich Schuchardt | b191aa4 | 2021-08-26 04:30:24 +0200 | [diff] [blame] | 331 | if (var_type >= EFI_AUTH_VAR_PK) { |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 332 | /* authentication is mandatory */ |
| 333 | if (!(attributes & |
| 334 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 335 | EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n", |
AKASHI Takahiro | ce3c386 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 336 | variable_name); |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 337 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /* authenticate a variable */ |
| 342 | if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) { |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 343 | if (attributes & |
| 344 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 345 | u32 env_attr; |
| 346 | |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 347 | ret = efi_variable_authenticate(variable_name, vendor, |
| 348 | &data_size, &data, |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 349 | attributes, &env_attr, |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 350 | &time); |
| 351 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 352 | return ret; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 353 | |
| 354 | /* last chance to check for delete */ |
| 355 | if (!data_size) |
| 356 | delete = true; |
| 357 | } |
| 358 | } else { |
| 359 | if (attributes & |
Masahisa Kojima | 26a3502 | 2023-02-21 11:33:17 +0900 | [diff] [blame] | 360 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
AKASHI Takahiro | ce3c386 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 361 | EFI_PRINT("Secure boot is not configured\n"); |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 362 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 366 | if (delete) { |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 367 | /* EFI_NOT_FOUND has been handled before */ |
Heinrich Schuchardt | ab02c3f | 2020-11-02 19:32:24 +0100 | [diff] [blame] | 368 | attributes = var->attr; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 369 | ret = EFI_SUCCESS; |
Masahisa Kojima | 584a2ad | 2024-04-02 18:09:50 +0900 | [diff] [blame] | 370 | } else if (append && var) { |
| 371 | /* |
| 372 | * data is appended if EFI_VARIABLE_APPEND_WRITE is set and |
| 373 | * variable exists. |
| 374 | */ |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 375 | u16 *old_data = var->name; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 376 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 377 | for (; *old_data; ++old_data) |
| 378 | ; |
| 379 | ++old_data; |
Weizhao Ouyang | 3b7d26e | 2024-05-08 19:13:12 +0800 | [diff] [blame] | 380 | ret = efi_var_mem_ins(variable_name, vendor, |
| 381 | attributes & ~EFI_VARIABLE_APPEND_WRITE, |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 382 | var->length, old_data, data_size, data, |
| 383 | time); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 384 | } else { |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 385 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 386 | data_size, data, 0, NULL, time); |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 387 | } |
| 388 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 389 | if (ret != EFI_SUCCESS) |
| 390 | return ret; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 391 | |
Ilias Apalodimas | 6b0f194 | 2024-04-03 18:43:53 +0300 | [diff] [blame] | 392 | efi_var_mem_del(var); |
| 393 | |
Heinrich Schuchardt | 99bfab8 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 394 | if (var_type == EFI_AUTH_VAR_PK) |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 395 | ret = efi_init_secure_state(); |
| 396 | else |
| 397 | ret = EFI_SUCCESS; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 398 | |
Heinrich Schuchardt | 90c4205 | 2023-01-19 14:49:33 +0100 | [diff] [blame] | 399 | /* |
| 400 | * Write non-volatile EFI variables to file |
| 401 | * TODO: check if a value change has occured to avoid superfluous writes |
| 402 | */ |
| 403 | if (attributes & EFI_VARIABLE_NON_VOLATILE) |
Heinrich Schuchardt | 5f7dcf0 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 404 | efi_var_to_file(); |
| 405 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 406 | return EFI_SUCCESS; |
AKASHI Takahiro | 767f6ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 407 | } |
| 408 | |
Ilias Apalodimas | 9677192 | 2024-04-25 08:18:19 +0300 | [diff] [blame] | 409 | efi_status_t __efi_runtime |
| 410 | efi_query_variable_info_int(u32 attributes, |
| 411 | u64 *maximum_variable_storage_size, |
| 412 | u64 *remaining_variable_storage_size, |
| 413 | u64 *maximum_variable_size) |
Heinrich Schuchardt | 01df8cf | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 414 | { |
Ilias Apalodimas | 9677192 | 2024-04-25 08:18:19 +0300 | [diff] [blame] | 415 | if (!maximum_variable_storage_size || |
| 416 | !remaining_variable_storage_size || |
| 417 | !maximum_variable_size || !attributes) |
Masahisa Kojima | 454a944 | 2023-02-02 22:53:35 +0900 | [diff] [blame] | 418 | return EFI_INVALID_PARAMETER; |
| 419 | |
| 420 | /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */ |
| 421 | if ((attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) || |
| 422 | ((attributes & EFI_VARIABLE_MASK) == 0)) |
| 423 | return EFI_UNSUPPORTED; |
| 424 | |
| 425 | if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE) |
| 426 | return EFI_INVALID_PARAMETER; |
| 427 | |
| 428 | /* Make sure if runtime bit is set, boot service bit is set also. */ |
| 429 | if ((attributes & |
| 430 | (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == |
| 431 | EFI_VARIABLE_RUNTIME_ACCESS) |
| 432 | return EFI_INVALID_PARAMETER; |
| 433 | |
Heinrich Schuchardt | 19327c1 | 2024-04-03 17:33:33 +0200 | [diff] [blame] | 434 | if (attributes & ~EFI_VARIABLE_MASK) |
Masahisa Kojima | 454a944 | 2023-02-02 22:53:35 +0900 | [diff] [blame] | 435 | return EFI_INVALID_PARAMETER; |
| 436 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 437 | *maximum_variable_storage_size = EFI_VAR_BUF_SIZE - |
| 438 | sizeof(struct efi_var_file); |
| 439 | *remaining_variable_storage_size = efi_var_mem_free(); |
| 440 | *maximum_variable_size = EFI_VAR_BUF_SIZE - |
| 441 | sizeof(struct efi_var_file) - |
| 442 | sizeof(struct efi_var_entry); |
| 443 | return EFI_SUCCESS; |
Heinrich Schuchardt | 01df8cf | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Heinrich Schuchardt | 77d4d39 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 446 | /** |
Heinrich Schuchardt | 01df8cf | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 447 | * efi_query_variable_info_runtime() - runtime implementation of |
| 448 | * QueryVariableInfo() |
Heinrich Schuchardt | ce43528 | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 449 | * |
| 450 | * @attributes: bitmask to select variables to be |
| 451 | * queried |
| 452 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 453 | * selected variable types |
| 454 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 455 | * selected variable types |
| 456 | * @maximum_variable_size: maximum size of a variable of the |
| 457 | * selected type |
| 458 | * Returns: status code |
| 459 | */ |
Heinrich Schuchardt | 1247257 | 2023-02-10 09:06:17 +0100 | [diff] [blame] | 460 | static efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime( |
Heinrich Schuchardt | ce43528 | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 461 | u32 attributes, |
| 462 | u64 *maximum_variable_storage_size, |
| 463 | u64 *remaining_variable_storage_size, |
| 464 | u64 *maximum_variable_size) |
| 465 | { |
Ilias Apalodimas | 9677192 | 2024-04-25 08:18:19 +0300 | [diff] [blame] | 466 | if (!(attributes & EFI_VARIABLE_RUNTIME_ACCESS)) |
| 467 | return EFI_INVALID_PARAMETER; |
| 468 | if ((attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | |
| 469 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | |
| 470 | EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS))) |
| 471 | return EFI_UNSUPPORTED; |
| 472 | |
| 473 | return efi_query_variable_info_int(attributes, |
| 474 | maximum_variable_storage_size, |
| 475 | remaining_variable_storage_size, |
| 476 | maximum_variable_size); |
Heinrich Schuchardt | ce43528 | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 477 | } |
Heinrich Schuchardt | 8819209 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 478 | |
| 479 | /** |
Heinrich Schuchardt | 29018ab | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 480 | * efi_set_variable_runtime() - runtime implementation of SetVariable() |
Heinrich Schuchardt | e5b4462 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 481 | * |
| 482 | * @variable_name: name of the variable |
| 483 | * @vendor: vendor GUID |
| 484 | * @attributes: attributes of the variable |
| 485 | * @data_size: size of the buffer with the variable value |
| 486 | * @data: buffer with the variable value |
| 487 | * Return: status code |
Heinrich Schuchardt | 29018ab | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 488 | */ |
| 489 | static efi_status_t __efi_runtime EFIAPI |
| 490 | efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 491 | u32 attributes, efi_uintn_t data_size, |
| 492 | const void *data) |
| 493 | { |
Ilias Apalodimas | c28d32f | 2024-04-18 15:54:50 +0300 | [diff] [blame] | 494 | struct efi_var_entry *var; |
| 495 | efi_uintn_t ret; |
| 496 | bool append, delete; |
| 497 | u64 time = 0; |
| 498 | |
| 499 | if (!IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) |
| 500 | return EFI_UNSUPPORTED; |
| 501 | |
| 502 | /* |
| 503 | * Authenticated variables are not supported. The EFI spec |
| 504 | * in ยง32.3.6 requires keys to be stored in non-volatile storage which |
| 505 | * is tamper and delete resistant. |
| 506 | * The rest of the checks are in setvariable_allowed() |
| 507 | */ |
| 508 | if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) |
| 509 | return EFI_INVALID_PARAMETER; |
| 510 | |
| 511 | ret = setvariable_allowed(variable_name, vendor, attributes, data_size, |
| 512 | data); |
| 513 | if (ret != EFI_SUCCESS) |
| 514 | return ret; |
| 515 | |
| 516 | /* check if a variable exists */ |
| 517 | var = efi_var_mem_find(vendor, variable_name, NULL); |
| 518 | append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); |
| 519 | attributes &= ~EFI_VARIABLE_APPEND_WRITE; |
| 520 | delete = !append && (!data_size || !attributes); |
| 521 | |
| 522 | /* BS only variables are hidden deny writing them */ |
| 523 | if (!delete && !(attributes & EFI_VARIABLE_RUNTIME_ACCESS)) |
| 524 | return EFI_INVALID_PARAMETER; |
| 525 | |
| 526 | if (var) { |
| 527 | if (var->attr & EFI_VARIABLE_READ_ONLY || |
| 528 | !(var->attr & EFI_VARIABLE_NON_VOLATILE)) |
| 529 | return EFI_WRITE_PROTECTED; |
| 530 | |
| 531 | /* attributes won't be changed */ |
| 532 | if (!delete && (((var->attr & ~EFI_VARIABLE_READ_ONLY) != |
| 533 | (attributes & ~EFI_VARIABLE_READ_ONLY)))) |
| 534 | return EFI_INVALID_PARAMETER; |
| 535 | time = var->time; |
| 536 | } else { |
| 537 | if (!(attributes & EFI_VARIABLE_NON_VOLATILE)) |
| 538 | return EFI_INVALID_PARAMETER; |
| 539 | if (append && !data_size) |
| 540 | return EFI_SUCCESS; |
| 541 | if (delete) |
| 542 | return EFI_NOT_FOUND; |
| 543 | } |
| 544 | |
| 545 | if (delete) { |
| 546 | /* EFI_NOT_FOUND has been handled before */ |
| 547 | attributes = var->attr; |
| 548 | ret = EFI_SUCCESS; |
| 549 | } else if (append && var) { |
| 550 | u16 *old_data = (void *)((uintptr_t)var->name + |
| 551 | sizeof(u16) * (u16_strlen(var->name) + 1)); |
| 552 | |
| 553 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 554 | var->length, old_data, data_size, data, |
| 555 | time); |
| 556 | } else { |
| 557 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 558 | data_size, data, 0, NULL, time); |
| 559 | } |
| 560 | |
| 561 | if (ret != EFI_SUCCESS) |
| 562 | return ret; |
| 563 | /* We are always inserting new variables, get rid of the old copy */ |
| 564 | efi_var_mem_del(var); |
| 565 | |
| 566 | return EFI_SUCCESS; |
Heinrich Schuchardt | 29018ab | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /** |
| 570 | * efi_variables_boot_exit_notify() - notify ExitBootServices() is called |
| 571 | */ |
| 572 | void efi_variables_boot_exit_notify(void) |
| 573 | { |
Heinrich Schuchardt | 5f7dcf0 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 574 | /* Switch variable services functions to runtime version */ |
Heinrich Schuchardt | 29018ab | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 575 | efi_runtime_services.get_variable = efi_get_variable_runtime; |
| 576 | efi_runtime_services.get_next_variable_name = |
| 577 | efi_get_next_variable_name_runtime; |
| 578 | efi_runtime_services.set_variable = efi_set_variable_runtime; |
Heinrich Schuchardt | 01df8cf | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 579 | efi_runtime_services.query_variable_info = |
| 580 | efi_query_variable_info_runtime; |
Heinrich Schuchardt | 29018ab | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 581 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 582 | } |
| 583 | |
| 584 | /** |
Heinrich Schuchardt | 8819209 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 585 | * efi_init_variables() - initialize variable services |
| 586 | * |
| 587 | * Return: status code |
| 588 | */ |
| 589 | efi_status_t efi_init_variables(void) |
| 590 | { |
AKASHI Takahiro | 434ffb6 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 591 | efi_status_t ret; |
| 592 | |
Heinrich Schuchardt | ab7296c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 593 | ret = efi_var_mem_init(); |
| 594 | if (ret != EFI_SUCCESS) |
| 595 | return ret; |
| 596 | |
Ilias Apalodimas | 77bb147 | 2022-12-29 10:13:22 +0200 | [diff] [blame] | 597 | ret = efi_var_from_file(); |
| 598 | if (ret != EFI_SUCCESS) |
| 599 | return ret; |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 600 | if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) { |
| 601 | ret = efi_var_restore((struct efi_var_file *) |
Heinrich Schuchardt | 9ef82e2 | 2021-08-25 19:13:24 +0200 | [diff] [blame] | 602 | __efi_var_file_begin, true); |
Heinrich Schuchardt | 7dda163 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 603 | if (ret != EFI_SUCCESS) |
| 604 | log_err("Invalid EFI variable seed\n"); |
| 605 | } |
| 606 | |
AKASHI Takahiro | f68a6d5 | 2020-08-13 17:05:29 +0900 | [diff] [blame] | 607 | return efi_init_secure_state(); |
Heinrich Schuchardt | 8819209 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 608 | } |