Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | c69addd | 2020-03-19 17:15:18 +0000 | [diff] [blame] | 3 | * UEFI runtime variable services |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | c69addd | 2020-03-19 17:15:18 +0000 | [diff] [blame] | 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Heinrich Schuchardt | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 10 | #include <common.h> |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 11 | #include <efi_loader.h> |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 12 | #include <efi_variable.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 14 | #include <env_internal.h> |
Heinrich Schuchardt | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 15 | #include <hexdump.h> |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 16 | #include <log.h> |
Heinrich Schuchardt | 147d5d3 | 2019-10-26 23:53:48 +0200 | [diff] [blame] | 17 | #include <malloc.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 18 | #include <rtc.h> |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 19 | #include <search.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 20 | #include <uuid.h> |
AKASHI Takahiro | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 21 | #include <crypto/pkcs7_parser.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 22 | #include <linux/compat.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 23 | #include <u-boot/crc.h> |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 24 | #include <asm/sections.h> |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 25 | |
| 26 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 27 | static u8 pkcs7_hdr[] = { |
| 28 | /* SEQUENCE */ |
| 29 | 0x30, 0x82, 0x05, 0xc7, |
| 30 | /* OID: pkcs7-signedData */ |
| 31 | 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, |
| 32 | /* Context Structured? */ |
| 33 | 0xa0, 0x82, 0x05, 0xb8, |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * efi_variable_parse_signature - parse a signature in variable |
| 38 | * @buf: Pointer to variable's value |
| 39 | * @buflen: Length of @buf |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 40 | * @tmpbuf: Pointer to temporary buffer |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 41 | * |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 42 | * Parse a signature embedded in variable's value and instantiate |
| 43 | * a pkcs7_message structure. Since pkcs7_parse_message() accepts only |
| 44 | * pkcs7's signedData, some header needed be prepended for correctly |
| 45 | * parsing authentication data, particularly for variable's. |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 46 | * A temporary buffer will be allocated if needed, and it should be |
| 47 | * kept valid during the authentication because some data in the buffer |
| 48 | * will be referenced by efi_signature_verify(). |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 49 | * |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 50 | * Return: Pointer to pkcs7_message structure on success, NULL on error |
Heinrich Schuchardt | 0ffda47 | 2019-01-18 19:52:05 +0100 | [diff] [blame] | 51 | */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 52 | static struct pkcs7_message *efi_variable_parse_signature(const void *buf, |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 53 | size_t buflen, |
| 54 | u8 **tmpbuf) |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 55 | { |
| 56 | u8 *ebuf; |
| 57 | size_t ebuflen, len; |
| 58 | struct pkcs7_message *msg; |
| 59 | |
| 60 | /* |
| 61 | * This is the best assumption to check if the binary is |
| 62 | * already in a form of pkcs7's signedData. |
| 63 | */ |
| 64 | if (buflen > sizeof(pkcs7_hdr) && |
| 65 | !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) { |
| 66 | msg = pkcs7_parse_message(buf, buflen); |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 67 | if (IS_ERR(msg)) |
| 68 | return NULL; |
| 69 | return msg; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Otherwise, we should add a dummy prefix sequence for pkcs7 |
| 74 | * message parser to be able to process. |
| 75 | * NOTE: EDK2 also uses similar hack in WrapPkcs7Data() |
| 76 | * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c |
| 77 | * TODO: |
| 78 | * The header should be composed in a more refined manner. |
| 79 | */ |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 80 | EFI_PRINT("Makeshift prefix added to authentication data\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 81 | ebuflen = sizeof(pkcs7_hdr) + buflen; |
| 82 | if (ebuflen <= 0x7f) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 83 | EFI_PRINT("Data is too short\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | ebuf = malloc(ebuflen); |
| 88 | if (!ebuf) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 89 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr)); |
| 94 | memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen); |
| 95 | len = ebuflen - 4; |
| 96 | ebuf[2] = (len >> 8) & 0xff; |
| 97 | ebuf[3] = len & 0xff; |
| 98 | len = ebuflen - 0x13; |
| 99 | ebuf[0x11] = (len >> 8) & 0xff; |
| 100 | ebuf[0x12] = len & 0xff; |
| 101 | |
| 102 | msg = pkcs7_parse_message(ebuf, ebuflen); |
| 103 | |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 104 | if (IS_ERR(msg)) { |
| 105 | free(ebuf); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 106 | return NULL; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 107 | } |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 108 | |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 109 | *tmpbuf = ebuf; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 110 | return msg; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * efi_variable_authenticate - authenticate a variable |
| 115 | * @variable: Variable name in u16 |
| 116 | * @vendor: Guid of variable |
| 117 | * @data_size: Size of @data |
| 118 | * @data: Pointer to variable's value |
| 119 | * @given_attr: Attributes to be given at SetVariable() |
| 120 | * @env_attr: Attributes that an existing variable holds |
| 121 | * @time: signed time that an existing variable holds |
| 122 | * |
| 123 | * Called by efi_set_variable() to verify that the input is correct. |
| 124 | * Will replace the given data pointer with another that points to |
| 125 | * the actual data to store in the internal memory. |
| 126 | * On success, @data and @data_size will be replaced with variable's |
| 127 | * actual data, excluding authentication data, and its size, and variable's |
| 128 | * attributes and signed time will also be returned in @env_attr and @time, |
| 129 | * respectively. |
| 130 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 131 | * Return: status code |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 132 | */ |
| 133 | static efi_status_t efi_variable_authenticate(u16 *variable, |
| 134 | const efi_guid_t *vendor, |
| 135 | efi_uintn_t *data_size, |
| 136 | const void **data, u32 given_attr, |
| 137 | u32 *env_attr, u64 *time) |
| 138 | { |
| 139 | const struct efi_variable_authentication_2 *auth; |
| 140 | struct efi_signature_store *truststore, *truststore2; |
| 141 | struct pkcs7_message *var_sig; |
| 142 | struct efi_image_regions *regs; |
| 143 | struct efi_time timestamp; |
| 144 | struct rtc_time tm; |
| 145 | u64 new_time; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 146 | u8 *ebuf; |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 147 | enum efi_auth_var_type var_type; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 148 | efi_status_t ret; |
| 149 | |
| 150 | var_sig = NULL; |
| 151 | truststore = NULL; |
| 152 | truststore2 = NULL; |
| 153 | regs = NULL; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 154 | ebuf = NULL; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 155 | ret = EFI_SECURITY_VIOLATION; |
| 156 | |
| 157 | if (*data_size < sizeof(struct efi_variable_authentication_2)) |
| 158 | goto err; |
| 159 | |
| 160 | /* authentication data */ |
| 161 | auth = *data; |
| 162 | if (*data_size < (sizeof(auth->time_stamp) |
| 163 | + auth->auth_info.hdr.dwLength)) |
| 164 | goto err; |
| 165 | |
| 166 | if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) |
| 167 | goto err; |
| 168 | |
Heinrich Schuchardt | 7a76a3f | 2020-07-01 12:44:00 +0200 | [diff] [blame] | 169 | memcpy(×tamp, &auth->time_stamp, sizeof(timestamp)); |
| 170 | if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone || |
| 171 | timestamp.daylight || timestamp.pad2) |
| 172 | goto err; |
| 173 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 174 | *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength; |
| 175 | *data_size -= (sizeof(auth->time_stamp) |
| 176 | + auth->auth_info.hdr.dwLength); |
| 177 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 178 | memset(&tm, 0, sizeof(tm)); |
| 179 | tm.tm_year = timestamp.year; |
| 180 | tm.tm_mon = timestamp.month; |
| 181 | tm.tm_mday = timestamp.day; |
| 182 | tm.tm_hour = timestamp.hour; |
| 183 | tm.tm_min = timestamp.minute; |
| 184 | tm.tm_sec = timestamp.second; |
| 185 | new_time = rtc_mktime(&tm); |
| 186 | |
| 187 | if (!efi_secure_boot_enabled()) { |
| 188 | /* finished checking */ |
| 189 | *time = new_time; |
| 190 | return EFI_SUCCESS; |
| 191 | } |
| 192 | |
| 193 | if (new_time <= *time) |
| 194 | goto err; |
| 195 | |
| 196 | /* data to be digested */ |
| 197 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1); |
| 198 | if (!regs) |
| 199 | goto err; |
| 200 | regs->max = 5; |
| 201 | efi_image_region_add(regs, (uint8_t *)variable, |
| 202 | (uint8_t *)variable |
| 203 | + u16_strlen(variable) * sizeof(u16), 1); |
| 204 | efi_image_region_add(regs, (uint8_t *)vendor, |
| 205 | (uint8_t *)vendor + sizeof(*vendor), 1); |
| 206 | efi_image_region_add(regs, (uint8_t *)&given_attr, |
| 207 | (uint8_t *)&given_attr + sizeof(given_attr), 1); |
| 208 | efi_image_region_add(regs, (uint8_t *)×tamp, |
| 209 | (uint8_t *)×tamp + sizeof(timestamp), 1); |
| 210 | efi_image_region_add(regs, (uint8_t *)*data, |
| 211 | (uint8_t *)*data + *data_size, 1); |
| 212 | |
| 213 | /* variable's signature list */ |
| 214 | if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info)) |
| 215 | goto err; |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 216 | |
| 217 | /* ebuf should be kept valid during the authentication */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 218 | var_sig = efi_variable_parse_signature(auth->auth_info.cert_data, |
| 219 | auth->auth_info.hdr.dwLength |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 220 | - sizeof(auth->auth_info), |
| 221 | &ebuf); |
Patrick Wildt | cd8a55b | 2020-05-07 02:13:18 +0200 | [diff] [blame] | 222 | if (!var_sig) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 223 | EFI_PRINT("Parsing variable's signature failed\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 224 | goto err; |
| 225 | } |
| 226 | |
| 227 | /* signature database used for authentication */ |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 228 | var_type = efi_auth_var_get_type(variable, vendor); |
| 229 | switch (var_type) { |
| 230 | case EFI_AUTH_VAR_PK: |
| 231 | case EFI_AUTH_VAR_KEK: |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 232 | /* with PK */ |
| 233 | truststore = efi_sigstore_parse_sigdb(L"PK"); |
| 234 | if (!truststore) |
| 235 | goto err; |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 236 | break; |
| 237 | case EFI_AUTH_VAR_DB: |
| 238 | case EFI_AUTH_VAR_DBX: |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 239 | /* with PK and KEK */ |
| 240 | truststore = efi_sigstore_parse_sigdb(L"KEK"); |
| 241 | truststore2 = efi_sigstore_parse_sigdb(L"PK"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 242 | if (!truststore) { |
| 243 | if (!truststore2) |
| 244 | goto err; |
| 245 | |
| 246 | truststore = truststore2; |
| 247 | truststore2 = NULL; |
| 248 | } |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 249 | break; |
| 250 | default: |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 251 | /* TODO: support private authenticated variables */ |
| 252 | goto err; |
| 253 | } |
| 254 | |
| 255 | /* verify signature */ |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 256 | if (efi_signature_verify(regs, var_sig, truststore, NULL)) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 257 | EFI_PRINT("Verified\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 258 | } else { |
| 259 | if (truststore2 && |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 260 | efi_signature_verify(regs, var_sig, truststore2, NULL)) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 261 | EFI_PRINT("Verified\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 262 | } else { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 263 | EFI_PRINT("Verifying variable's signature failed\n"); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 264 | goto err; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* finished checking */ |
Heinrich Schuchardt | 5106dc7 | 2020-06-30 12:02:14 +0200 | [diff] [blame] | 269 | *time = new_time; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 270 | ret = EFI_SUCCESS; |
| 271 | |
| 272 | err: |
| 273 | efi_sigstore_free(truststore); |
| 274 | efi_sigstore_free(truststore2); |
| 275 | pkcs7_free_message(var_sig); |
AKASHI Takahiro | 1bb6edf | 2020-08-12 09:37:50 +0900 | [diff] [blame] | 276 | free(ebuf); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 277 | free(regs); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | #else |
| 282 | static efi_status_t efi_variable_authenticate(u16 *variable, |
| 283 | const efi_guid_t *vendor, |
| 284 | efi_uintn_t *data_size, |
| 285 | const void **data, u32 given_attr, |
| 286 | u32 *env_attr, u64 *time) |
| 287 | { |
| 288 | return EFI_SUCCESS; |
| 289 | } |
| 290 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 291 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 292 | efi_status_t __efi_runtime |
| 293 | efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor, |
| 294 | u32 *attributes, efi_uintn_t *data_size, void *data, |
| 295 | u64 *timep) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 296 | { |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 297 | return efi_get_variable_mem(variable_name, vendor, attributes, data_size, data, timep); |
AKASHI Takahiro | ad3f36c | 2019-01-21 12:43:13 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 300 | efi_status_t __efi_runtime |
| 301 | efi_get_next_variable_name_int(efi_uintn_t *variable_name_size, |
| 302 | u16 *variable_name, efi_guid_t *vendor) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 303 | { |
Ilias Apalodimas | a4d1b1b | 2020-07-23 15:49:49 +0300 | [diff] [blame] | 304 | return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 307 | efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor, |
| 308 | u32 attributes, efi_uintn_t data_size, |
| 309 | const void *data, bool ro_check) |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 310 | { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 311 | struct efi_var_entry *var; |
| 312 | efi_uintn_t ret; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 313 | bool append, delete; |
| 314 | u64 time = 0; |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 315 | enum efi_auth_var_type var_type; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 316 | |
Heinrich Schuchardt | adba396 | 2019-06-12 23:28:42 +0200 | [diff] [blame] | 317 | if (!variable_name || !*variable_name || !vendor || |
| 318 | ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) && |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 319 | !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) |
| 320 | return EFI_INVALID_PARAMETER; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 321 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 322 | /* check if a variable exists */ |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 323 | var = efi_var_mem_find(vendor, variable_name, NULL); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 324 | append = !!(attributes & EFI_VARIABLE_APPEND_WRITE); |
| 325 | attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE; |
| 326 | delete = !append && (!data_size || !attributes); |
| 327 | |
| 328 | /* check attributes */ |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 329 | var_type = efi_auth_var_get_type(variable_name, vendor); |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 330 | if (var) { |
| 331 | if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY)) |
| 332 | return EFI_WRITE_PROTECTED; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 333 | |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 334 | if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) { |
| 335 | if (var_type != EFI_AUTH_VAR_NONE) |
| 336 | return EFI_WRITE_PROTECTED; |
| 337 | } |
| 338 | |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 339 | /* attributes won't be changed */ |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 340 | if (!delete && |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 341 | ((ro_check && var->attr != attributes) || |
| 342 | (!ro_check && ((var->attr & ~(u32)EFI_VARIABLE_READ_ONLY) |
Heinrich Schuchardt | 9827e84 | 2020-06-22 18:10:27 +0200 | [diff] [blame] | 343 | != (attributes & ~(u32)EFI_VARIABLE_READ_ONLY))))) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 344 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 345 | } |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 346 | time = var->time; |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 347 | } else { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 348 | if (delete || append) |
Heinrich Schuchardt | 8021bbe | 2019-09-26 21:40:18 +0200 | [diff] [blame] | 349 | /* |
| 350 | * Trying to delete or to update a non-existent |
| 351 | * variable. |
| 352 | */ |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 353 | return EFI_NOT_FOUND; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 354 | } |
AKASHI Takahiro | e5023b7 | 2019-09-06 15:09:52 +0900 | [diff] [blame] | 355 | |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 356 | if (var_type != EFI_AUTH_VAR_NONE) { |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 357 | /* authentication is mandatory */ |
| 358 | if (!(attributes & |
| 359 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 360 | EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n", |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 361 | variable_name); |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 362 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /* authenticate a variable */ |
| 367 | if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 368 | if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) |
| 369 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 370 | if (attributes & |
| 371 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 372 | u32 env_attr; |
| 373 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 374 | ret = efi_variable_authenticate(variable_name, vendor, |
| 375 | &data_size, &data, |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 376 | attributes, &env_attr, |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 377 | &time); |
| 378 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 379 | return ret; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 380 | |
| 381 | /* last chance to check for delete */ |
| 382 | if (!data_size) |
| 383 | delete = true; |
| 384 | } |
| 385 | } else { |
| 386 | if (attributes & |
| 387 | (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | |
| 388 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) { |
AKASHI Takahiro | 3712373 | 2020-06-09 14:09:34 +0900 | [diff] [blame] | 389 | EFI_PRINT("Secure boot is not configured\n"); |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 390 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 394 | if (delete) { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 395 | /* EFI_NOT_FOUND has been handled before */ |
Heinrich Schuchardt | 0f7a680 | 2020-11-02 19:32:24 +0100 | [diff] [blame^] | 396 | attributes = var->attr; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 397 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 398 | } else if (append) { |
| 399 | u16 *old_data = var->name; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 400 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 401 | for (; *old_data; ++old_data) |
| 402 | ; |
| 403 | ++old_data; |
| 404 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 405 | var->length, old_data, data_size, data, |
| 406 | time); |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 407 | } else { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 408 | ret = efi_var_mem_ins(variable_name, vendor, attributes, |
| 409 | data_size, data, 0, NULL, time); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 410 | } |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 411 | efi_var_mem_del(var); |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 412 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 413 | if (ret != EFI_SUCCESS) |
| 414 | return ret; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 415 | |
Heinrich Schuchardt | 3a28033 | 2020-07-15 12:40:35 +0200 | [diff] [blame] | 416 | if (var_type == EFI_AUTH_VAR_PK) |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 417 | ret = efi_init_secure_state(); |
| 418 | else |
| 419 | ret = EFI_SUCCESS; |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 420 | |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 421 | /* Write non-volatile EFI variables to file */ |
| 422 | if (attributes & EFI_VARIABLE_NON_VOLATILE && |
| 423 | ret == EFI_SUCCESS && efi_obj_list_initialized == EFI_SUCCESS) |
| 424 | efi_var_to_file(); |
| 425 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 426 | return EFI_SUCCESS; |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 427 | } |
| 428 | |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 429 | efi_status_t efi_query_variable_info_int(u32 attributes, |
| 430 | u64 *maximum_variable_storage_size, |
| 431 | u64 *remaining_variable_storage_size, |
| 432 | u64 *maximum_variable_size) |
| 433 | { |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 434 | *maximum_variable_storage_size = EFI_VAR_BUF_SIZE - |
| 435 | sizeof(struct efi_var_file); |
| 436 | *remaining_variable_storage_size = efi_var_mem_free(); |
| 437 | *maximum_variable_size = EFI_VAR_BUF_SIZE - |
| 438 | sizeof(struct efi_var_file) - |
| 439 | sizeof(struct efi_var_entry); |
| 440 | return EFI_SUCCESS; |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 441 | } |
| 442 | |
AKASHI Takahiro | b0f49ee | 2020-04-14 11:51:41 +0900 | [diff] [blame] | 443 | /** |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 444 | * efi_query_variable_info_runtime() - runtime implementation of |
| 445 | * QueryVariableInfo() |
Heinrich Schuchardt | 9b19dae | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 446 | * |
| 447 | * @attributes: bitmask to select variables to be |
| 448 | * queried |
| 449 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 450 | * selected variable types |
| 451 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 452 | * selected variable types |
| 453 | * @maximum_variable_size: maximum size of a variable of the |
| 454 | * selected type |
| 455 | * Returns: status code |
| 456 | */ |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 457 | efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime( |
Heinrich Schuchardt | 9b19dae | 2019-06-20 12:13:05 +0200 | [diff] [blame] | 458 | u32 attributes, |
| 459 | u64 *maximum_variable_storage_size, |
| 460 | u64 *remaining_variable_storage_size, |
| 461 | u64 *maximum_variable_size) |
| 462 | { |
| 463 | return EFI_UNSUPPORTED; |
| 464 | } |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 465 | |
| 466 | /** |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 467 | * efi_set_variable_runtime() - runtime implementation of SetVariable() |
Heinrich Schuchardt | ee1f0e4 | 2019-07-14 12:11:16 +0200 | [diff] [blame] | 468 | * |
| 469 | * @variable_name: name of the variable |
| 470 | * @vendor: vendor GUID |
| 471 | * @attributes: attributes of the variable |
| 472 | * @data_size: size of the buffer with the variable value |
| 473 | * @data: buffer with the variable value |
| 474 | * Return: status code |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 475 | */ |
| 476 | static efi_status_t __efi_runtime EFIAPI |
| 477 | efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor, |
| 478 | u32 attributes, efi_uintn_t data_size, |
| 479 | const void *data) |
| 480 | { |
| 481 | return EFI_UNSUPPORTED; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * efi_variables_boot_exit_notify() - notify ExitBootServices() is called |
| 486 | */ |
| 487 | void efi_variables_boot_exit_notify(void) |
| 488 | { |
Heinrich Schuchardt | 09a8d50 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 489 | /* Switch variable services functions to runtime version */ |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 490 | efi_runtime_services.get_variable = efi_get_variable_runtime; |
| 491 | efi_runtime_services.get_next_variable_name = |
| 492 | efi_get_next_variable_name_runtime; |
| 493 | efi_runtime_services.set_variable = efi_set_variable_runtime; |
Heinrich Schuchardt | 276c61d | 2020-06-26 17:57:48 +0200 | [diff] [blame] | 494 | efi_runtime_services.query_variable_info = |
| 495 | efi_query_variable_info_runtime; |
Heinrich Schuchardt | 2ac6258 | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 496 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 497 | } |
| 498 | |
| 499 | /** |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 500 | * efi_init_variables() - initialize variable services |
| 501 | * |
| 502 | * Return: status code |
| 503 | */ |
| 504 | efi_status_t efi_init_variables(void) |
| 505 | { |
AKASHI Takahiro | c78dc19 | 2020-04-14 11:51:42 +0900 | [diff] [blame] | 506 | efi_status_t ret; |
| 507 | |
Heinrich Schuchardt | 047276c | 2020-07-04 18:34:15 +0200 | [diff] [blame] | 508 | ret = efi_var_mem_init(); |
| 509 | if (ret != EFI_SUCCESS) |
| 510 | return ret; |
| 511 | |
Heinrich Schuchardt | 4b7d5c1 | 2020-07-14 21:25:28 +0200 | [diff] [blame] | 512 | if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) { |
| 513 | ret = efi_var_restore((struct efi_var_file *) |
| 514 | __efi_var_file_begin); |
| 515 | if (ret != EFI_SUCCESS) |
| 516 | log_err("Invalid EFI variable seed\n"); |
| 517 | } |
| 518 | |
AKASHI Takahiro | 2e0d94a | 2020-08-13 17:05:29 +0900 | [diff] [blame] | 519 | ret = efi_var_from_file(); |
| 520 | if (ret != EFI_SUCCESS) |
| 521 | return ret; |
| 522 | |
| 523 | return efi_init_secure_state(); |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 524 | } |