1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Originally from efivars.c 4 * 5 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> 6 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> 7 */ 8 9 #include <linux/capability.h> 10 #include <linux/types.h> 11 #include <linux/errno.h> 12 #include <linux/init.h> 13 #include <linux/mm.h> 14 #include <linux/module.h> 15 #include <linux/string.h> 16 #include <linux/smp.h> 17 #include <linux/efi.h> 18 #include <linux/device.h> 19 #include <linux/slab.h> 20 #include <linux/ctype.h> 21 #include <linux/ucs2_string.h> 22 23 #include "internal.h" 24 25 MODULE_IMPORT_NS("EFIVAR"); 26 27 static bool 28 validate_device_path(efi_char16_t *var_name, int match, u8 *buffer, 29 unsigned long len) 30 { 31 struct efi_generic_dev_path *node; 32 int offset = 0; 33 34 node = (struct efi_generic_dev_path *)buffer; 35 36 if (len < sizeof(*node)) 37 return false; 38 39 while (offset <= len - sizeof(*node) && 40 node->length >= sizeof(*node) && 41 node->length <= len - offset) { 42 offset += node->length; 43 44 if ((node->type == EFI_DEV_END_PATH || 45 node->type == EFI_DEV_END_PATH2) && 46 node->sub_type == EFI_DEV_END_ENTIRE) 47 return true; 48 49 node = (struct efi_generic_dev_path *)(buffer + offset); 50 } 51 52 /* 53 * If we're here then either node->length pointed past the end 54 * of the buffer or we reached the end of the buffer without 55 * finding a device path end node. 56 */ 57 return false; 58 } 59 60 static bool 61 validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer, 62 unsigned long len) 63 { 64 /* An array of 16-bit integers */ 65 if ((len % 2) != 0) 66 return false; 67 68 return true; 69 } 70 71 static bool 72 validate_load_option(efi_char16_t *var_name, int match, u8 *buffer, 73 unsigned long len) 74 { 75 u16 filepathlength; 76 int i, desclength = 0, namelen; 77 78 namelen = ucs2_strnlen(var_name, EFI_VAR_NAME_LEN); 79 80 /* Either "Boot" or "Driver" followed by four digits of hex */ 81 for (i = match; i < match+4; i++) { 82 if (var_name[i] > 127 || 83 hex_to_bin(var_name[i] & 0xff) < 0) 84 return true; 85 } 86 87 /* Reject it if there's 4 digits of hex and then further content */ 88 if (namelen > match + 4) 89 return false; 90 91 /* A valid entry must be at least 8 bytes */ 92 if (len < 8) 93 return false; 94 95 filepathlength = buffer[4] | buffer[5] << 8; 96 97 /* 98 * There's no stored length for the description, so it has to be 99 * found by hand 100 */ 101 desclength = ucs2_strsize((efi_char16_t *)(buffer + 6), len - 6) + 2; 102 103 /* Each boot entry must have a descriptor */ 104 if (!desclength) 105 return false; 106 107 /* 108 * If the sum of the length of the description, the claimed filepath 109 * length and the original header are greater than the length of the 110 * variable, it's malformed 111 */ 112 if ((desclength + filepathlength + 6) > len) 113 return false; 114 115 /* 116 * And, finally, check the filepath 117 */ 118 return validate_device_path(var_name, match, buffer + desclength + 6, 119 filepathlength); 120 } 121 122 static bool 123 validate_uint16(efi_char16_t *var_name, int match, u8 *buffer, 124 unsigned long len) 125 { 126 /* A single 16-bit integer */ 127 if (len != 2) 128 return false; 129 130 return true; 131 } 132 133 static bool 134 validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer, 135 unsigned long len) 136 { 137 int i; 138 139 for (i = 0; i < len; i++) { 140 if (buffer[i] > 127) 141 return false; 142 143 if (buffer[i] == 0) 144 return true; 145 } 146 147 return false; 148 } 149 150 struct variable_validate { 151 efi_guid_t vendor; 152 char *name; 153 bool (*validate)(efi_char16_t *var_name, int match, u8 *data, 154 unsigned long len); 155 }; 156 157 /* 158 * This is the list of variables we need to validate, as well as the 159 * whitelist for what we think is safe not to default to immutable. 160 * 161 * If it has a validate() method that's not NULL, it'll go into the 162 * validation routine. If not, it is assumed valid, but still used for 163 * whitelisting. 164 * 165 * Note that it's sorted by {vendor,name}, but globbed names must come after 166 * any other name with the same prefix. 167 */ 168 static const struct variable_validate variable_validate[] = { 169 { EFI_GLOBAL_VARIABLE_GUID, "BootNext", validate_uint16 }, 170 { EFI_GLOBAL_VARIABLE_GUID, "BootOrder", validate_boot_order }, 171 { EFI_GLOBAL_VARIABLE_GUID, "Boot*", validate_load_option }, 172 { EFI_GLOBAL_VARIABLE_GUID, "DriverOrder", validate_boot_order }, 173 { EFI_GLOBAL_VARIABLE_GUID, "Driver*", validate_load_option }, 174 { EFI_GLOBAL_VARIABLE_GUID, "ConIn", validate_device_path }, 175 { EFI_GLOBAL_VARIABLE_GUID, "ConInDev", validate_device_path }, 176 { EFI_GLOBAL_VARIABLE_GUID, "ConOut", validate_device_path }, 177 { EFI_GLOBAL_VARIABLE_GUID, "ConOutDev", validate_device_path }, 178 { EFI_GLOBAL_VARIABLE_GUID, "ErrOut", validate_device_path }, 179 { EFI_GLOBAL_VARIABLE_GUID, "ErrOutDev", validate_device_path }, 180 { EFI_GLOBAL_VARIABLE_GUID, "Lang", validate_ascii_string }, 181 { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL }, 182 { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string }, 183 { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 }, 184 { LINUX_EFI_CRASH_GUID, "*", NULL }, 185 { NULL_GUID, "", NULL }, 186 }; 187 188 /* 189 * Check if @var_name matches the pattern given in @match_name. 190 * 191 * @var_name: an array of @len non-NUL characters. 192 * @match_name: a NUL-terminated pattern string, optionally ending in "*". A 193 * final "*" character matches any trailing characters @var_name, 194 * including the case when there are none left in @var_name. 195 * @match: on output, the number of non-wildcard characters in @match_name 196 * that @var_name matches, regardless of the return value. 197 * @return: whether @var_name fully matches @match_name. 198 */ 199 static bool 200 variable_matches(const char *var_name, size_t len, const char *match_name, 201 int *match) 202 { 203 for (*match = 0; ; (*match)++) { 204 char c = match_name[*match]; 205 206 switch (c) { 207 case '*': 208 /* Wildcard in @match_name means we've matched. */ 209 return true; 210 211 case '\0': 212 /* @match_name has ended. Has @var_name too? */ 213 return (*match == len); 214 215 default: 216 /* 217 * We've reached a non-wildcard char in @match_name. 218 * Continue only if there's an identical character in 219 * @var_name. 220 */ 221 if (*match < len && c == var_name[*match]) 222 continue; 223 return false; 224 } 225 } 226 } 227 228 char * 229 efivar_get_utf8name(const efi_char16_t *name16, efi_guid_t *vendor) 230 { 231 int len = ucs2_utf8size(name16); 232 char *name; 233 234 /* name, plus '-', plus GUID, plus NUL*/ 235 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL); 236 if (!name) 237 return NULL; 238 239 ucs2_as_utf8(name, name16, len); 240 241 name[len] = '-'; 242 243 efi_guid_to_str(vendor, name + len + 1); 244 245 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0'; 246 247 /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */ 248 strreplace(name, '/', '!'); 249 250 return name; 251 } 252 253 bool 254 efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data, 255 unsigned long data_size) 256 { 257 int i; 258 unsigned long utf8_size; 259 u8 *utf8_name; 260 261 utf8_size = ucs2_utf8size(var_name); 262 utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL); 263 if (!utf8_name) 264 return false; 265 266 ucs2_as_utf8(utf8_name, var_name, utf8_size); 267 utf8_name[utf8_size] = '\0'; 268 269 for (i = 0; variable_validate[i].name[0] != '\0'; i++) { 270 const char *name = variable_validate[i].name; 271 int match = 0; 272 273 if (efi_guidcmp(vendor, variable_validate[i].vendor)) 274 continue; 275 276 if (variable_matches(utf8_name, utf8_size+1, name, &match)) { 277 if (variable_validate[i].validate == NULL) 278 break; 279 kfree(utf8_name); 280 return variable_validate[i].validate(var_name, match, 281 data, data_size); 282 } 283 } 284 kfree(utf8_name); 285 return true; 286 } 287 288 bool 289 efivar_variable_is_removable(efi_guid_t vendor, const char *var_name, 290 size_t len) 291 { 292 int i; 293 bool found = false; 294 int match = 0; 295 296 /* 297 * Check if our variable is in the validated variables list 298 */ 299 for (i = 0; variable_validate[i].name[0] != '\0'; i++) { 300 if (efi_guidcmp(variable_validate[i].vendor, vendor)) 301 continue; 302 303 if (variable_matches(var_name, len, 304 variable_validate[i].name, &match)) { 305 found = true; 306 break; 307 } 308 } 309 310 /* 311 * If it's in our list, it is removable. 312 */ 313 return found; 314 } 315 316 /* 317 * Returns the size of variable_name, in bytes, including the 318 * terminating NULL character, or variable_name_size if no NULL 319 * character is found among the first variable_name_size bytes. 320 */ 321 static unsigned long var_name_strnsize(efi_char16_t *variable_name, 322 unsigned long variable_name_size) 323 { 324 unsigned long len; 325 efi_char16_t c; 326 327 /* 328 * The variable name is, by definition, a NULL-terminated 329 * string, so make absolutely sure that variable_name_size is 330 * the value we expect it to be. If not, return the real size. 331 */ 332 for (len = 2; len <= variable_name_size; len += sizeof(c)) { 333 c = variable_name[(len / sizeof(c)) - 1]; 334 if (!c) 335 break; 336 } 337 338 return min(len, variable_name_size); 339 } 340 341 /* 342 * Print a warning when duplicate EFI variables are encountered and 343 * disable the sysfs workqueue since the firmware is buggy. 344 */ 345 static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid, 346 unsigned long len16) 347 { 348 size_t i, len8 = len16 / sizeof(efi_char16_t); 349 char *str8; 350 351 str8 = kzalloc(len8, GFP_KERNEL); 352 if (!str8) 353 return; 354 355 for (i = 0; i < len8; i++) 356 str8[i] = str16[i]; 357 358 printk(KERN_WARNING "efivars: duplicate variable: %s-%pUl\n", 359 str8, vendor_guid); 360 kfree(str8); 361 } 362 363 /** 364 * efivar_init - build the initial list of EFI variables 365 * @func: callback function to invoke for every variable 366 * @data: function-specific data to pass to @func 367 * 368 * Get every EFI variable from the firmware and invoke @func. @func 369 * should populate the initial dentry and inode tree. 370 * 371 * Returns 0 on success, or a kernel error code on failure. 372 */ 373 int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), 374 void *data) 375 { 376 unsigned long variable_name_size = 512; 377 efi_char16_t *variable_name; 378 efi_status_t status; 379 efi_guid_t vendor_guid; 380 int err = 0; 381 382 variable_name = kzalloc(variable_name_size, GFP_KERNEL); 383 if (!variable_name) { 384 printk(KERN_ERR "efivars: Memory allocation failed.\n"); 385 return -ENOMEM; 386 } 387 388 err = efivar_lock(); 389 if (err) 390 goto free; 391 392 /* 393 * A small set of old UEFI implementations reject sizes 394 * above a certain threshold, the lowest seen in the wild 395 * is 512. 396 */ 397 398 do { 399 variable_name_size = 512; 400 BUILD_BUG_ON(EFI_VAR_NAME_LEN < 512); 401 402 status = efivar_get_next_variable(&variable_name_size, 403 variable_name, 404 &vendor_guid); 405 switch (status) { 406 case EFI_SUCCESS: 407 variable_name_size = var_name_strnsize(variable_name, 408 variable_name_size); 409 410 /* 411 * Some firmware implementations return the 412 * same variable name on multiple calls to 413 * get_next_variable(). Terminate the loop 414 * immediately as there is no guarantee that 415 * we'll ever see a different variable name, 416 * and may end up looping here forever. 417 */ 418 if (efivarfs_variable_is_present(variable_name, 419 &vendor_guid, data)) { 420 dup_variable_bug(variable_name, &vendor_guid, 421 variable_name_size); 422 status = EFI_NOT_FOUND; 423 } else { 424 err = func(variable_name, vendor_guid, 425 variable_name_size, data); 426 if (err) 427 status = EFI_NOT_FOUND; 428 } 429 break; 430 case EFI_UNSUPPORTED: 431 err = -EOPNOTSUPP; 432 status = EFI_NOT_FOUND; 433 break; 434 case EFI_NOT_FOUND: 435 break; 436 case EFI_BUFFER_TOO_SMALL: 437 pr_warn("efivars: Variable name size exceeds maximum (%lu > 512)\n", 438 variable_name_size); 439 status = EFI_NOT_FOUND; 440 break; 441 default: 442 pr_warn("efivars: get_next_variable: status=%lx\n", status); 443 status = EFI_NOT_FOUND; 444 break; 445 } 446 447 } while (status != EFI_NOT_FOUND); 448 449 efivar_unlock(); 450 free: 451 kfree(variable_name); 452 453 return err; 454 } 455 456 /** 457 * efivar_entry_delete - delete variable 458 * @entry: entry containing variable to delete 459 * 460 * Delete the variable from the firmware. It is the caller's 461 * responsibility to free @entry (by deleting the dentry/inode) once 462 * we return. 463 * 464 * Returns 0 on success, -EINTR if we can't grab the semaphore, 465 * converted EFI status code if set_variable() fails. 466 */ 467 int efivar_entry_delete(struct efivar_entry *entry) 468 { 469 efi_status_t status; 470 int err; 471 472 err = efivar_lock(); 473 if (err) 474 return err; 475 476 status = efivar_set_variable_locked(entry->var.VariableName, 477 &entry->var.VendorGuid, 478 0, 0, NULL, false); 479 efivar_unlock(); 480 if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) 481 return efi_status_to_err(status); 482 483 return 0; 484 } 485 486 /** 487 * efivar_entry_size - obtain the size of a variable 488 * @entry: entry for this variable 489 * @size: location to store the variable's size 490 */ 491 int efivar_entry_size(struct efivar_entry *entry, unsigned long *size) 492 { 493 efi_status_t status; 494 int err; 495 496 *size = 0; 497 498 err = efivar_lock(); 499 if (err) 500 return err; 501 502 status = efivar_get_variable(entry->var.VariableName, 503 &entry->var.VendorGuid, NULL, size, NULL); 504 efivar_unlock(); 505 506 if (status != EFI_BUFFER_TOO_SMALL) 507 return efi_status_to_err(status); 508 509 return 0; 510 } 511 512 /** 513 * __efivar_entry_get - call get_variable() 514 * @entry: read data for this variable 515 * @attributes: variable attributes 516 * @size: size of @data buffer 517 * @data: buffer to store variable data 518 * 519 * The caller MUST call efivar_entry_iter_begin() and 520 * efivar_entry_iter_end() before and after the invocation of this 521 * function, respectively. 522 */ 523 int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes, 524 unsigned long *size, void *data) 525 { 526 efi_status_t status; 527 528 status = efivar_get_variable(entry->var.VariableName, 529 &entry->var.VendorGuid, 530 attributes, size, data); 531 532 return efi_status_to_err(status); 533 } 534 535 /** 536 * efivar_entry_get - call get_variable() 537 * @entry: read data for this variable 538 * @attributes: variable attributes 539 * @size: size of @data buffer 540 * @data: buffer to store variable data 541 */ 542 int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, 543 unsigned long *size, void *data) 544 { 545 int err; 546 547 err = efivar_lock(); 548 if (err) 549 return err; 550 err = __efivar_entry_get(entry, attributes, size, data); 551 efivar_unlock(); 552 553 return 0; 554 } 555 556 /** 557 * efivar_entry_set_get_size - call set_variable() and get new size (atomic) 558 * @entry: entry containing variable to set and get 559 * @attributes: attributes of variable to be written 560 * @size: size of data buffer 561 * @data: buffer containing data to write 562 * @set: did the set_variable() call succeed? 563 * 564 * This is a pretty special (complex) function. See efivarfs_file_write(). 565 * 566 * Atomically call set_variable() for @entry and if the call is 567 * successful, return the new size of the variable from get_variable() 568 * in @size. The success of set_variable() is indicated by @set. 569 * 570 * Returns 0 on success, -EINVAL if the variable data is invalid, 571 * -ENOSPC if the firmware does not have enough available space, or a 572 * converted EFI status code if either of set_variable() or 573 * get_variable() fail. 574 * 575 * If the EFI variable does not exist when calling set_variable() 576 * (EFI_NOT_FOUND). 577 */ 578 int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, 579 unsigned long *size, void *data, bool *set) 580 { 581 efi_char16_t *name = entry->var.VariableName; 582 efi_guid_t *vendor = &entry->var.VendorGuid; 583 efi_status_t status; 584 int err; 585 586 *set = false; 587 588 if (efivar_validate(*vendor, name, data, *size) == false) 589 return -EINVAL; 590 591 /* 592 * The lock here protects the get_variable call and the 593 * conditional set_variable call 594 */ 595 err = efivar_lock(); 596 if (err) 597 return err; 598 599 status = efivar_set_variable_locked(name, vendor, attributes, *size, 600 data, false); 601 if (status != EFI_SUCCESS) { 602 err = efi_status_to_err(status); 603 goto out; 604 } 605 606 *set = true; 607 608 /* 609 * Writing to the variable may have caused a change in size (which 610 * could either be an append or an overwrite), or the variable to be 611 * deleted. Perform a GetVariable() so we can tell what actually 612 * happened. 613 */ 614 *size = 0; 615 status = efivar_get_variable(entry->var.VariableName, 616 &entry->var.VendorGuid, 617 NULL, size, NULL); 618 619 efivar_unlock(); 620 621 if (status && status != EFI_BUFFER_TOO_SMALL) 622 return efi_status_to_err(status); 623 624 return 0; 625 626 out: 627 efivar_unlock(); 628 return err; 629 630 } 631