vars.c 16 KB

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