efi_variable.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * UEFI runtime variable services
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <env_internal.h>
  10. #include <hexdump.h>
  11. #include <malloc.h>
  12. #include <rtc.h>
  13. #include <search.h>
  14. #include <uuid.h>
  15. #include <crypto/pkcs7_parser.h>
  16. #include <linux/compat.h>
  17. #include <u-boot/crc.h>
  18. enum efi_secure_mode {
  19. EFI_MODE_SETUP,
  20. EFI_MODE_USER,
  21. EFI_MODE_AUDIT,
  22. EFI_MODE_DEPLOYED,
  23. };
  24. const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
  25. static bool efi_secure_boot;
  26. static int efi_secure_mode;
  27. static u8 efi_vendor_keys;
  28. #define READ_ONLY BIT(31)
  29. static efi_status_t efi_get_variable_common(u16 *variable_name,
  30. const efi_guid_t *vendor,
  31. u32 *attributes,
  32. efi_uintn_t *data_size, void *data);
  33. static efi_status_t efi_set_variable_common(u16 *variable_name,
  34. const efi_guid_t *vendor,
  35. u32 attributes,
  36. efi_uintn_t data_size,
  37. const void *data,
  38. bool ro_check);
  39. /*
  40. * Mapping between EFI variables and u-boot variables:
  41. *
  42. * efi_$guid_$varname = {attributes}(type)value
  43. *
  44. * For example:
  45. *
  46. * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
  47. * "{ro,boot,run}(blob)0000000000000000"
  48. * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
  49. * "(blob)00010000"
  50. *
  51. * The attributes are a comma separated list of these possible
  52. * attributes:
  53. *
  54. * + ro - read-only
  55. * + boot - boot-services access
  56. * + run - runtime access
  57. *
  58. * NOTE: with current implementation, no variables are available after
  59. * ExitBootServices, and all are persisted (if possible).
  60. *
  61. * If not specified, the attributes default to "{boot}".
  62. *
  63. * The required type is one of:
  64. *
  65. * + utf8 - raw utf8 string
  66. * + blob - arbitrary length hex string
  67. *
  68. * Maybe a utf16 type would be useful to for a string value to be auto
  69. * converted to utf16?
  70. */
  71. #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
  72. /**
  73. * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
  74. * variable name
  75. *
  76. * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
  77. * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
  78. * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
  79. *
  80. * @native: pointer to pointer to U-Boot variable name
  81. * @variable_name: UEFI variable name
  82. * @vendor: vendor GUID
  83. * Return: status code
  84. */
  85. static efi_status_t efi_to_native(char **native, const u16 *variable_name,
  86. const efi_guid_t *vendor)
  87. {
  88. size_t len;
  89. char *pos;
  90. len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
  91. *native = malloc(len);
  92. if (!*native)
  93. return EFI_OUT_OF_RESOURCES;
  94. pos = *native;
  95. pos += sprintf(pos, "efi_%pUl_", vendor);
  96. utf16_utf8_strcpy(&pos, variable_name);
  97. return EFI_SUCCESS;
  98. }
  99. /**
  100. * prefix() - skip over prefix
  101. *
  102. * Skip over a prefix string.
  103. *
  104. * @str: string with prefix
  105. * @prefix: prefix string
  106. * Return: string without prefix, or NULL if prefix not found
  107. */
  108. static const char *prefix(const char *str, const char *prefix)
  109. {
  110. size_t n = strlen(prefix);
  111. if (!strncmp(prefix, str, n))
  112. return str + n;
  113. return NULL;
  114. }
  115. /**
  116. * parse_attr() - decode attributes part of variable value
  117. *
  118. * Convert the string encoded attributes of a UEFI variable to a bit mask.
  119. * TODO: Several attributes are not supported.
  120. *
  121. * @str: value of U-Boot variable
  122. * @attrp: pointer to UEFI attributes
  123. * @timep: pointer to time attribute
  124. * Return: pointer to remainder of U-Boot variable value
  125. */
  126. static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
  127. {
  128. u32 attr = 0;
  129. char sep = '{';
  130. if (*str != '{') {
  131. *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
  132. return str;
  133. }
  134. while (*str == sep) {
  135. const char *s;
  136. str++;
  137. if ((s = prefix(str, "ro"))) {
  138. attr |= READ_ONLY;
  139. } else if ((s = prefix(str, "nv"))) {
  140. attr |= EFI_VARIABLE_NON_VOLATILE;
  141. } else if ((s = prefix(str, "boot"))) {
  142. attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  143. } else if ((s = prefix(str, "run"))) {
  144. attr |= EFI_VARIABLE_RUNTIME_ACCESS;
  145. } else if ((s = prefix(str, "time="))) {
  146. attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  147. hex2bin((u8 *)timep, s, sizeof(*timep));
  148. s += sizeof(*timep) * 2;
  149. } else if (*str == '}') {
  150. break;
  151. } else {
  152. printf("invalid attribute: %s\n", str);
  153. break;
  154. }
  155. str = s;
  156. sep = ',';
  157. }
  158. str++;
  159. *attrp = attr;
  160. return str;
  161. }
  162. /**
  163. * efi_set_secure_state - modify secure boot state variables
  164. * @sec_boot: value of SecureBoot
  165. * @setup_mode: value of SetupMode
  166. * @audit_mode: value of AuditMode
  167. * @deployed_mode: value of DeployedMode
  168. *
  169. * Modify secure boot stat-related variables as indicated.
  170. *
  171. * Return: status code
  172. */
  173. static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
  174. int audit_mode, int deployed_mode)
  175. {
  176. u32 attributes;
  177. efi_status_t ret;
  178. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  179. EFI_VARIABLE_RUNTIME_ACCESS |
  180. READ_ONLY;
  181. ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
  182. attributes, sizeof(sec_boot), &sec_boot,
  183. false);
  184. if (ret != EFI_SUCCESS)
  185. goto err;
  186. ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
  187. attributes, sizeof(setup_mode),
  188. &setup_mode, false);
  189. if (ret != EFI_SUCCESS)
  190. goto err;
  191. ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
  192. attributes, sizeof(audit_mode),
  193. &audit_mode, false);
  194. if (ret != EFI_SUCCESS)
  195. goto err;
  196. ret = efi_set_variable_common(L"DeployedMode",
  197. &efi_global_variable_guid, attributes,
  198. sizeof(deployed_mode), &deployed_mode,
  199. false);
  200. err:
  201. return ret;
  202. }
  203. /**
  204. * efi_transfer_secure_state - handle a secure boot state transition
  205. * @mode: new state
  206. *
  207. * Depending on @mode, secure boot related variables are updated.
  208. * Those variables are *read-only* for users, efi_set_variable_common()
  209. * is called here.
  210. *
  211. * Return: status code
  212. */
  213. static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
  214. {
  215. efi_status_t ret;
  216. debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
  217. if (mode == EFI_MODE_DEPLOYED) {
  218. ret = efi_set_secure_state(1, 0, 0, 1);
  219. if (ret != EFI_SUCCESS)
  220. goto err;
  221. efi_secure_boot = true;
  222. } else if (mode == EFI_MODE_AUDIT) {
  223. ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
  224. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  225. EFI_VARIABLE_RUNTIME_ACCESS,
  226. 0, NULL, false);
  227. if (ret != EFI_SUCCESS)
  228. goto err;
  229. ret = efi_set_secure_state(0, 1, 1, 0);
  230. if (ret != EFI_SUCCESS)
  231. goto err;
  232. efi_secure_boot = true;
  233. } else if (mode == EFI_MODE_USER) {
  234. ret = efi_set_secure_state(1, 0, 0, 0);
  235. if (ret != EFI_SUCCESS)
  236. goto err;
  237. efi_secure_boot = true;
  238. } else if (mode == EFI_MODE_SETUP) {
  239. ret = efi_set_secure_state(0, 1, 0, 0);
  240. if (ret != EFI_SUCCESS)
  241. goto err;
  242. } else {
  243. return EFI_INVALID_PARAMETER;
  244. }
  245. efi_secure_mode = mode;
  246. return EFI_SUCCESS;
  247. err:
  248. /* TODO: What action should be taken here? */
  249. printf("ERROR: Secure state transition failed\n");
  250. return ret;
  251. }
  252. /**
  253. * efi_init_secure_state - initialize secure boot state
  254. *
  255. * Return: status code
  256. */
  257. static efi_status_t efi_init_secure_state(void)
  258. {
  259. enum efi_secure_mode mode;
  260. efi_uintn_t size;
  261. efi_status_t ret;
  262. /*
  263. * TODO:
  264. * Since there is currently no "platform-specific" installation
  265. * method of Platform Key, we can't say if VendorKeys is 0 or 1
  266. * precisely.
  267. */
  268. size = 0;
  269. ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
  270. NULL, &size, NULL);
  271. if (ret == EFI_BUFFER_TOO_SMALL) {
  272. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
  273. mode = EFI_MODE_USER;
  274. else
  275. mode = EFI_MODE_SETUP;
  276. efi_vendor_keys = 0;
  277. } else if (ret == EFI_NOT_FOUND) {
  278. mode = EFI_MODE_SETUP;
  279. efi_vendor_keys = 1;
  280. } else {
  281. goto err;
  282. }
  283. ret = efi_transfer_secure_state(mode);
  284. if (ret == EFI_SUCCESS)
  285. ret = efi_set_variable_common(L"VendorKeys",
  286. &efi_global_variable_guid,
  287. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  288. EFI_VARIABLE_RUNTIME_ACCESS |
  289. READ_ONLY,
  290. sizeof(efi_vendor_keys),
  291. &efi_vendor_keys, false);
  292. err:
  293. return ret;
  294. }
  295. /**
  296. * efi_secure_boot_enabled - return if secure boot is enabled or not
  297. *
  298. * Return: true if enabled, false if disabled
  299. */
  300. bool efi_secure_boot_enabled(void)
  301. {
  302. return efi_secure_boot;
  303. }
  304. #ifdef CONFIG_EFI_SECURE_BOOT
  305. static u8 pkcs7_hdr[] = {
  306. /* SEQUENCE */
  307. 0x30, 0x82, 0x05, 0xc7,
  308. /* OID: pkcs7-signedData */
  309. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
  310. /* Context Structured? */
  311. 0xa0, 0x82, 0x05, 0xb8,
  312. };
  313. /**
  314. * efi_variable_parse_signature - parse a signature in variable
  315. * @buf: Pointer to variable's value
  316. * @buflen: Length of @buf
  317. *
  318. * Parse a signature embedded in variable's value and instantiate
  319. * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
  320. * pkcs7's signedData, some header needed be prepended for correctly
  321. * parsing authentication data, particularly for variable's.
  322. *
  323. * Return: Pointer to pkcs7_message structure on success, NULL on error
  324. */
  325. static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
  326. size_t buflen)
  327. {
  328. u8 *ebuf;
  329. size_t ebuflen, len;
  330. struct pkcs7_message *msg;
  331. /*
  332. * This is the best assumption to check if the binary is
  333. * already in a form of pkcs7's signedData.
  334. */
  335. if (buflen > sizeof(pkcs7_hdr) &&
  336. !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
  337. msg = pkcs7_parse_message(buf, buflen);
  338. goto out;
  339. }
  340. /*
  341. * Otherwise, we should add a dummy prefix sequence for pkcs7
  342. * message parser to be able to process.
  343. * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
  344. * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
  345. * TODO:
  346. * The header should be composed in a more refined manner.
  347. */
  348. debug("Makeshift prefix added to authentication data\n");
  349. ebuflen = sizeof(pkcs7_hdr) + buflen;
  350. if (ebuflen <= 0x7f) {
  351. debug("Data is too short\n");
  352. return NULL;
  353. }
  354. ebuf = malloc(ebuflen);
  355. if (!ebuf) {
  356. debug("Out of memory\n");
  357. return NULL;
  358. }
  359. memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
  360. memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
  361. len = ebuflen - 4;
  362. ebuf[2] = (len >> 8) & 0xff;
  363. ebuf[3] = len & 0xff;
  364. len = ebuflen - 0x13;
  365. ebuf[0x11] = (len >> 8) & 0xff;
  366. ebuf[0x12] = len & 0xff;
  367. msg = pkcs7_parse_message(ebuf, ebuflen);
  368. free(ebuf);
  369. out:
  370. if (IS_ERR(msg))
  371. return NULL;
  372. return msg;
  373. }
  374. /**
  375. * efi_variable_authenticate - authenticate a variable
  376. * @variable: Variable name in u16
  377. * @vendor: Guid of variable
  378. * @data_size: Size of @data
  379. * @data: Pointer to variable's value
  380. * @given_attr: Attributes to be given at SetVariable()
  381. * @env_attr: Attributes that an existing variable holds
  382. * @time: signed time that an existing variable holds
  383. *
  384. * Called by efi_set_variable() to verify that the input is correct.
  385. * Will replace the given data pointer with another that points to
  386. * the actual data to store in the internal memory.
  387. * On success, @data and @data_size will be replaced with variable's
  388. * actual data, excluding authentication data, and its size, and variable's
  389. * attributes and signed time will also be returned in @env_attr and @time,
  390. * respectively.
  391. *
  392. * Return: status code
  393. */
  394. static efi_status_t efi_variable_authenticate(u16 *variable,
  395. const efi_guid_t *vendor,
  396. efi_uintn_t *data_size,
  397. const void **data, u32 given_attr,
  398. u32 *env_attr, u64 *time)
  399. {
  400. const struct efi_variable_authentication_2 *auth;
  401. struct efi_signature_store *truststore, *truststore2;
  402. struct pkcs7_message *var_sig;
  403. struct efi_image_regions *regs;
  404. struct efi_time timestamp;
  405. struct rtc_time tm;
  406. u64 new_time;
  407. efi_status_t ret;
  408. var_sig = NULL;
  409. truststore = NULL;
  410. truststore2 = NULL;
  411. regs = NULL;
  412. ret = EFI_SECURITY_VIOLATION;
  413. if (*data_size < sizeof(struct efi_variable_authentication_2))
  414. goto err;
  415. /* authentication data */
  416. auth = *data;
  417. if (*data_size < (sizeof(auth->time_stamp)
  418. + auth->auth_info.hdr.dwLength))
  419. goto err;
  420. if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
  421. goto err;
  422. *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
  423. *data_size -= (sizeof(auth->time_stamp)
  424. + auth->auth_info.hdr.dwLength);
  425. memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
  426. memset(&tm, 0, sizeof(tm));
  427. tm.tm_year = timestamp.year;
  428. tm.tm_mon = timestamp.month;
  429. tm.tm_mday = timestamp.day;
  430. tm.tm_hour = timestamp.hour;
  431. tm.tm_min = timestamp.minute;
  432. tm.tm_sec = timestamp.second;
  433. new_time = rtc_mktime(&tm);
  434. if (!efi_secure_boot_enabled()) {
  435. /* finished checking */
  436. *time = new_time;
  437. return EFI_SUCCESS;
  438. }
  439. if (new_time <= *time)
  440. goto err;
  441. /* data to be digested */
  442. regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
  443. if (!regs)
  444. goto err;
  445. regs->max = 5;
  446. efi_image_region_add(regs, (uint8_t *)variable,
  447. (uint8_t *)variable
  448. + u16_strlen(variable) * sizeof(u16), 1);
  449. efi_image_region_add(regs, (uint8_t *)vendor,
  450. (uint8_t *)vendor + sizeof(*vendor), 1);
  451. efi_image_region_add(regs, (uint8_t *)&given_attr,
  452. (uint8_t *)&given_attr + sizeof(given_attr), 1);
  453. efi_image_region_add(regs, (uint8_t *)&timestamp,
  454. (uint8_t *)&timestamp + sizeof(timestamp), 1);
  455. efi_image_region_add(regs, (uint8_t *)*data,
  456. (uint8_t *)*data + *data_size, 1);
  457. /* variable's signature list */
  458. if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
  459. goto err;
  460. var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
  461. auth->auth_info.hdr.dwLength
  462. - sizeof(auth->auth_info));
  463. if (!var_sig) {
  464. debug("Parsing variable's signature failed\n");
  465. goto err;
  466. }
  467. /* signature database used for authentication */
  468. if (u16_strcmp(variable, L"PK") == 0 ||
  469. u16_strcmp(variable, L"KEK") == 0) {
  470. /* with PK */
  471. truststore = efi_sigstore_parse_sigdb(L"PK");
  472. if (!truststore)
  473. goto err;
  474. } else if (u16_strcmp(variable, L"db") == 0 ||
  475. u16_strcmp(variable, L"dbx") == 0) {
  476. /* with PK and KEK */
  477. truststore = efi_sigstore_parse_sigdb(L"KEK");
  478. truststore2 = efi_sigstore_parse_sigdb(L"PK");
  479. if (!truststore) {
  480. if (!truststore2)
  481. goto err;
  482. truststore = truststore2;
  483. truststore2 = NULL;
  484. }
  485. } else {
  486. /* TODO: support private authenticated variables */
  487. goto err;
  488. }
  489. /* verify signature */
  490. if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
  491. debug("Verified\n");
  492. } else {
  493. if (truststore2 &&
  494. efi_signature_verify_with_sigdb(regs, var_sig,
  495. truststore2, NULL)) {
  496. debug("Verified\n");
  497. } else {
  498. debug("Verifying variable's signature failed\n");
  499. goto err;
  500. }
  501. }
  502. /* finished checking */
  503. *time = rtc_mktime(&tm);
  504. ret = EFI_SUCCESS;
  505. err:
  506. efi_sigstore_free(truststore);
  507. efi_sigstore_free(truststore2);
  508. pkcs7_free_message(var_sig);
  509. free(regs);
  510. return ret;
  511. }
  512. #else
  513. static efi_status_t efi_variable_authenticate(u16 *variable,
  514. const efi_guid_t *vendor,
  515. efi_uintn_t *data_size,
  516. const void **data, u32 given_attr,
  517. u32 *env_attr, u64 *time)
  518. {
  519. return EFI_SUCCESS;
  520. }
  521. #endif /* CONFIG_EFI_SECURE_BOOT */
  522. static efi_status_t efi_get_variable_common(u16 *variable_name,
  523. const efi_guid_t *vendor,
  524. u32 *attributes,
  525. efi_uintn_t *data_size, void *data)
  526. {
  527. char *native_name;
  528. efi_status_t ret;
  529. unsigned long in_size;
  530. const char *val = NULL, *s;
  531. u64 time = 0;
  532. u32 attr;
  533. if (!variable_name || !vendor || !data_size)
  534. return EFI_EXIT(EFI_INVALID_PARAMETER);
  535. ret = efi_to_native(&native_name, variable_name, vendor);
  536. if (ret)
  537. return ret;
  538. EFI_PRINT("get '%s'\n", native_name);
  539. val = env_get(native_name);
  540. free(native_name);
  541. if (!val)
  542. return EFI_NOT_FOUND;
  543. val = parse_attr(val, &attr, &time);
  544. in_size = *data_size;
  545. if ((s = prefix(val, "(blob)"))) {
  546. size_t len = strlen(s);
  547. /* number of hexadecimal digits must be even */
  548. if (len & 1)
  549. return EFI_DEVICE_ERROR;
  550. /* two characters per byte: */
  551. len /= 2;
  552. *data_size = len;
  553. if (in_size < len) {
  554. ret = EFI_BUFFER_TOO_SMALL;
  555. goto out;
  556. }
  557. if (!data) {
  558. debug("Variable with no data shouldn't exist.\n");
  559. return EFI_INVALID_PARAMETER;
  560. }
  561. if (hex2bin(data, s, len))
  562. return EFI_DEVICE_ERROR;
  563. EFI_PRINT("got value: \"%s\"\n", s);
  564. } else if ((s = prefix(val, "(utf8)"))) {
  565. unsigned len = strlen(s) + 1;
  566. *data_size = len;
  567. if (in_size < len) {
  568. ret = EFI_BUFFER_TOO_SMALL;
  569. goto out;
  570. }
  571. if (!data) {
  572. debug("Variable with no data shouldn't exist.\n");
  573. return EFI_INVALID_PARAMETER;
  574. }
  575. memcpy(data, s, len);
  576. ((char *)data)[len] = '\0';
  577. EFI_PRINT("got value: \"%s\"\n", (char *)data);
  578. } else {
  579. EFI_PRINT("invalid value: '%s'\n", val);
  580. return EFI_DEVICE_ERROR;
  581. }
  582. out:
  583. if (attributes)
  584. *attributes = attr & EFI_VARIABLE_MASK;
  585. return ret;
  586. }
  587. /**
  588. * efi_efi_get_variable() - retrieve value of a UEFI variable
  589. *
  590. * This function implements the GetVariable runtime service.
  591. *
  592. * See the Unified Extensible Firmware Interface (UEFI) specification for
  593. * details.
  594. *
  595. * @variable_name: name of the variable
  596. * @vendor: vendor GUID
  597. * @attributes: attributes of the variable
  598. * @data_size: size of the buffer to which the variable value is copied
  599. * @data: buffer to which the variable value is copied
  600. * Return: status code
  601. */
  602. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  603. const efi_guid_t *vendor, u32 *attributes,
  604. efi_uintn_t *data_size, void *data)
  605. {
  606. efi_status_t ret;
  607. EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
  608. data_size, data);
  609. ret = efi_get_variable_common(variable_name, vendor, attributes,
  610. data_size, data);
  611. return EFI_EXIT(ret);
  612. }
  613. static char *efi_variables_list;
  614. static char *efi_cur_variable;
  615. /**
  616. * parse_uboot_variable() - parse a u-boot variable and get uefi-related
  617. * information
  618. * @variable: whole data of u-boot variable (ie. name=value)
  619. * @variable_name_size: size of variable_name buffer in byte
  620. * @variable_name: name of uefi variable in u16, null-terminated
  621. * @vendor: vendor's guid
  622. * @attributes: attributes
  623. *
  624. * A uefi variable is encoded into a u-boot variable as described above.
  625. * This function parses such a u-boot variable and retrieve uefi-related
  626. * information into respective parameters. In return, variable_name_size
  627. * is the size of variable name including NULL.
  628. *
  629. * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
  630. * the entire variable list has been returned,
  631. * otherwise non-zero status code
  632. */
  633. static efi_status_t parse_uboot_variable(char *variable,
  634. efi_uintn_t *variable_name_size,
  635. u16 *variable_name,
  636. const efi_guid_t *vendor,
  637. u32 *attributes)
  638. {
  639. char *guid, *name, *end, c;
  640. size_t name_len;
  641. efi_uintn_t old_variable_name_size;
  642. u64 time;
  643. u16 *p;
  644. guid = strchr(variable, '_');
  645. if (!guid)
  646. return EFI_INVALID_PARAMETER;
  647. guid++;
  648. name = strchr(guid, '_');
  649. if (!name)
  650. return EFI_INVALID_PARAMETER;
  651. name++;
  652. end = strchr(name, '=');
  653. if (!end)
  654. return EFI_INVALID_PARAMETER;
  655. name_len = end - name;
  656. old_variable_name_size = *variable_name_size;
  657. *variable_name_size = sizeof(u16) * (name_len + 1);
  658. if (old_variable_name_size < *variable_name_size)
  659. return EFI_BUFFER_TOO_SMALL;
  660. end++; /* point to value */
  661. /* variable name */
  662. p = variable_name;
  663. utf8_utf16_strncpy(&p, name, name_len);
  664. variable_name[name_len] = 0;
  665. /* guid */
  666. c = *(name - 1);
  667. *(name - 1) = '\0'; /* guid need be null-terminated here */
  668. if (uuid_str_to_bin(guid, (unsigned char *)vendor,
  669. UUID_STR_FORMAT_GUID))
  670. /* The only error would be EINVAL. */
  671. return EFI_INVALID_PARAMETER;
  672. *(name - 1) = c;
  673. /* attributes */
  674. parse_attr(end, attributes, &time);
  675. return EFI_SUCCESS;
  676. }
  677. /**
  678. * efi_get_next_variable_name() - enumerate the current variable names
  679. *
  680. * @variable_name_size: size of variable_name buffer in byte
  681. * @variable_name: name of uefi variable's name in u16
  682. * @vendor: vendor's guid
  683. *
  684. * This function implements the GetNextVariableName service.
  685. *
  686. * See the Unified Extensible Firmware Interface (UEFI) specification for
  687. * details.
  688. *
  689. * Return: status code
  690. */
  691. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  692. u16 *variable_name,
  693. efi_guid_t *vendor)
  694. {
  695. char *native_name, *variable;
  696. ssize_t name_len, list_len;
  697. char regex[256];
  698. char * const regexlist[] = {regex};
  699. u32 attributes;
  700. int i;
  701. efi_status_t ret;
  702. EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
  703. if (!variable_name_size || !variable_name || !vendor)
  704. return EFI_EXIT(EFI_INVALID_PARAMETER);
  705. if (variable_name[0]) {
  706. /* check null-terminated string */
  707. for (i = 0; i < *variable_name_size; i++)
  708. if (!variable_name[i])
  709. break;
  710. if (i >= *variable_name_size)
  711. return EFI_EXIT(EFI_INVALID_PARAMETER);
  712. /* search for the last-returned variable */
  713. ret = efi_to_native(&native_name, variable_name, vendor);
  714. if (ret)
  715. return EFI_EXIT(ret);
  716. name_len = strlen(native_name);
  717. for (variable = efi_variables_list; variable && *variable;) {
  718. if (!strncmp(variable, native_name, name_len) &&
  719. variable[name_len] == '=')
  720. break;
  721. variable = strchr(variable, '\n');
  722. if (variable)
  723. variable++;
  724. }
  725. free(native_name);
  726. if (!(variable && *variable))
  727. return EFI_EXIT(EFI_INVALID_PARAMETER);
  728. /* next variable */
  729. variable = strchr(variable, '\n');
  730. if (variable)
  731. variable++;
  732. if (!(variable && *variable))
  733. return EFI_EXIT(EFI_NOT_FOUND);
  734. } else {
  735. /*
  736. *new search: free a list used in the previous search
  737. */
  738. free(efi_variables_list);
  739. efi_variables_list = NULL;
  740. efi_cur_variable = NULL;
  741. snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
  742. list_len = hexport_r(&env_htab, '\n',
  743. H_MATCH_REGEX | H_MATCH_KEY,
  744. &efi_variables_list, 0, 1, regexlist);
  745. if (list_len <= 1)
  746. return EFI_EXIT(EFI_NOT_FOUND);
  747. variable = efi_variables_list;
  748. }
  749. ret = parse_uboot_variable(variable, variable_name_size, variable_name,
  750. vendor, &attributes);
  751. return EFI_EXIT(ret);
  752. }
  753. static efi_status_t efi_set_variable_common(u16 *variable_name,
  754. const efi_guid_t *vendor,
  755. u32 attributes,
  756. efi_uintn_t data_size,
  757. const void *data,
  758. bool ro_check)
  759. {
  760. char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
  761. efi_uintn_t old_size;
  762. bool append, delete;
  763. u64 time = 0;
  764. u32 attr;
  765. efi_status_t ret = EFI_SUCCESS;
  766. debug("%s: set '%s'\n", __func__, native_name);
  767. if (!variable_name || !*variable_name || !vendor ||
  768. ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
  769. !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
  770. ret = EFI_INVALID_PARAMETER;
  771. goto err;
  772. }
  773. ret = efi_to_native(&native_name, variable_name, vendor);
  774. if (ret)
  775. goto err;
  776. /* check if a variable exists */
  777. old_size = 0;
  778. attr = 0;
  779. ret = efi_get_variable_common(variable_name, vendor, &attr,
  780. &old_size, NULL);
  781. append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
  782. attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
  783. delete = !append && (!data_size || !attributes);
  784. /* check attributes */
  785. if (old_size) {
  786. if (ro_check && (attr & READ_ONLY)) {
  787. ret = EFI_WRITE_PROTECTED;
  788. goto err;
  789. }
  790. /* attributes won't be changed */
  791. if (!delete &&
  792. ((ro_check && attr != attributes) ||
  793. (!ro_check && ((attr & ~(u32)READ_ONLY)
  794. != (attributes & ~(u32)READ_ONLY))))) {
  795. ret = EFI_INVALID_PARAMETER;
  796. goto err;
  797. }
  798. } else {
  799. if (delete || append) {
  800. /*
  801. * Trying to delete or to update a non-existent
  802. * variable.
  803. */
  804. ret = EFI_NOT_FOUND;
  805. goto err;
  806. }
  807. }
  808. if (((!u16_strcmp(variable_name, L"PK") ||
  809. !u16_strcmp(variable_name, L"KEK")) &&
  810. !guidcmp(vendor, &efi_global_variable_guid)) ||
  811. ((!u16_strcmp(variable_name, L"db") ||
  812. !u16_strcmp(variable_name, L"dbx")) &&
  813. !guidcmp(vendor, &efi_guid_image_security_database))) {
  814. /* authentication is mandatory */
  815. if (!(attributes &
  816. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
  817. debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
  818. variable_name);
  819. ret = EFI_INVALID_PARAMETER;
  820. goto err;
  821. }
  822. }
  823. /* authenticate a variable */
  824. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
  825. if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
  826. ret = EFI_INVALID_PARAMETER;
  827. goto err;
  828. }
  829. if (attributes &
  830. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  831. ret = efi_variable_authenticate(variable_name, vendor,
  832. &data_size, &data,
  833. attributes, &attr,
  834. &time);
  835. if (ret != EFI_SUCCESS)
  836. goto err;
  837. /* last chance to check for delete */
  838. if (!data_size)
  839. delete = true;
  840. }
  841. } else {
  842. if (attributes &
  843. (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
  844. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
  845. debug("Secure boot is not configured\n");
  846. ret = EFI_INVALID_PARAMETER;
  847. goto err;
  848. }
  849. }
  850. /* delete a variable */
  851. if (delete) {
  852. /* !old_size case has been handled before */
  853. val = NULL;
  854. ret = EFI_SUCCESS;
  855. goto out;
  856. }
  857. if (append) {
  858. old_data = malloc(old_size);
  859. if (!old_data) {
  860. ret = EFI_OUT_OF_RESOURCES;
  861. goto err;
  862. }
  863. ret = efi_get_variable_common(variable_name, vendor,
  864. &attr, &old_size, old_data);
  865. if (ret != EFI_SUCCESS)
  866. goto err;
  867. } else {
  868. old_size = 0;
  869. }
  870. val = malloc(2 * old_size + 2 * data_size
  871. + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
  872. + 1);
  873. if (!val) {
  874. ret = EFI_OUT_OF_RESOURCES;
  875. goto err;
  876. }
  877. s = val;
  878. /*
  879. * store attributes
  880. */
  881. attributes &= (READ_ONLY |
  882. EFI_VARIABLE_NON_VOLATILE |
  883. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  884. EFI_VARIABLE_RUNTIME_ACCESS |
  885. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
  886. s += sprintf(s, "{");
  887. while (attributes) {
  888. attr = 1 << (ffs(attributes) - 1);
  889. if (attr == READ_ONLY) {
  890. s += sprintf(s, "ro");
  891. } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
  892. s += sprintf(s, "nv");
  893. } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
  894. s += sprintf(s, "boot");
  895. } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
  896. s += sprintf(s, "run");
  897. } else if (attr ==
  898. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  899. s += sprintf(s, "time=");
  900. s = bin2hex(s, (u8 *)&time, sizeof(time));
  901. }
  902. attributes &= ~attr;
  903. if (attributes)
  904. s += sprintf(s, ",");
  905. }
  906. s += sprintf(s, "}");
  907. s += sprintf(s, "(blob)");
  908. /* store payload: */
  909. if (append)
  910. s = bin2hex(s, old_data, old_size);
  911. s = bin2hex(s, data, data_size);
  912. *s = '\0';
  913. EFI_PRINT("setting: %s=%s\n", native_name, val);
  914. out:
  915. if (env_set(native_name, val)) {
  916. ret = EFI_DEVICE_ERROR;
  917. } else {
  918. bool vendor_keys_modified = false;
  919. if ((u16_strcmp(variable_name, L"PK") == 0 &&
  920. guidcmp(vendor, &efi_global_variable_guid) == 0)) {
  921. ret = efi_transfer_secure_state(
  922. (delete ? EFI_MODE_SETUP :
  923. EFI_MODE_USER));
  924. if (ret != EFI_SUCCESS)
  925. goto err;
  926. if (efi_secure_mode != EFI_MODE_SETUP)
  927. vendor_keys_modified = true;
  928. } else if ((u16_strcmp(variable_name, L"KEK") == 0 &&
  929. guidcmp(vendor, &efi_global_variable_guid) == 0)) {
  930. if (efi_secure_mode != EFI_MODE_SETUP)
  931. vendor_keys_modified = true;
  932. }
  933. /* update VendorKeys */
  934. if (vendor_keys_modified & efi_vendor_keys) {
  935. efi_vendor_keys = 0;
  936. ret = efi_set_variable_common(
  937. L"VendorKeys",
  938. &efi_global_variable_guid,
  939. EFI_VARIABLE_BOOTSERVICE_ACCESS
  940. | EFI_VARIABLE_RUNTIME_ACCESS
  941. | READ_ONLY,
  942. sizeof(efi_vendor_keys),
  943. &efi_vendor_keys,
  944. false);
  945. } else {
  946. ret = EFI_SUCCESS;
  947. }
  948. }
  949. err:
  950. free(native_name);
  951. free(old_data);
  952. free(val);
  953. return ret;
  954. }
  955. /**
  956. * efi_set_variable() - set value of a UEFI variable
  957. *
  958. * This function implements the SetVariable runtime service.
  959. *
  960. * See the Unified Extensible Firmware Interface (UEFI) specification for
  961. * details.
  962. *
  963. * @variable_name: name of the variable
  964. * @vendor: vendor GUID
  965. * @attributes: attributes of the variable
  966. * @data_size: size of the buffer with the variable value
  967. * @data: buffer with the variable value
  968. * Return: status code
  969. */
  970. efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
  971. const efi_guid_t *vendor, u32 attributes,
  972. efi_uintn_t data_size, const void *data)
  973. {
  974. EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
  975. data_size, data);
  976. /* READ_ONLY bit is not part of API */
  977. attributes &= ~(u32)READ_ONLY;
  978. return EFI_EXIT(efi_set_variable_common(variable_name, vendor,
  979. attributes, data_size, data,
  980. true));
  981. }
  982. /**
  983. * efi_query_variable_info() - get information about EFI variables
  984. *
  985. * This function implements the QueryVariableInfo() runtime service.
  986. *
  987. * See the Unified Extensible Firmware Interface (UEFI) specification for
  988. * details.
  989. *
  990. * @attributes: bitmask to select variables to be
  991. * queried
  992. * @maximum_variable_storage_size: maximum size of storage area for the
  993. * selected variable types
  994. * @remaining_variable_storage_size: remaining size of storage are for the
  995. * selected variable types
  996. * @maximum_variable_size: maximum size of a variable of the
  997. * selected type
  998. * Returns: status code
  999. */
  1000. efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
  1001. u32 attributes,
  1002. u64 *maximum_variable_storage_size,
  1003. u64 *remaining_variable_storage_size,
  1004. u64 *maximum_variable_size)
  1005. {
  1006. return EFI_UNSUPPORTED;
  1007. }
  1008. /**
  1009. * efi_get_variable_runtime() - runtime implementation of GetVariable()
  1010. *
  1011. * @variable_name: name of the variable
  1012. * @vendor: vendor GUID
  1013. * @attributes: attributes of the variable
  1014. * @data_size: size of the buffer to which the variable value is copied
  1015. * @data: buffer to which the variable value is copied
  1016. * Return: status code
  1017. */
  1018. static efi_status_t __efi_runtime EFIAPI
  1019. efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
  1020. u32 *attributes, efi_uintn_t *data_size, void *data)
  1021. {
  1022. return EFI_UNSUPPORTED;
  1023. }
  1024. /**
  1025. * efi_get_next_variable_name_runtime() - runtime implementation of
  1026. * GetNextVariable()
  1027. *
  1028. * @variable_name_size: size of variable_name buffer in byte
  1029. * @variable_name: name of uefi variable's name in u16
  1030. * @vendor: vendor's guid
  1031. * Return: status code
  1032. */
  1033. static efi_status_t __efi_runtime EFIAPI
  1034. efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
  1035. u16 *variable_name, efi_guid_t *vendor)
  1036. {
  1037. return EFI_UNSUPPORTED;
  1038. }
  1039. /**
  1040. * efi_set_variable_runtime() - runtime implementation of SetVariable()
  1041. *
  1042. * @variable_name: name of the variable
  1043. * @vendor: vendor GUID
  1044. * @attributes: attributes of the variable
  1045. * @data_size: size of the buffer with the variable value
  1046. * @data: buffer with the variable value
  1047. * Return: status code
  1048. */
  1049. static efi_status_t __efi_runtime EFIAPI
  1050. efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
  1051. u32 attributes, efi_uintn_t data_size,
  1052. const void *data)
  1053. {
  1054. return EFI_UNSUPPORTED;
  1055. }
  1056. /**
  1057. * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
  1058. */
  1059. void efi_variables_boot_exit_notify(void)
  1060. {
  1061. efi_runtime_services.get_variable = efi_get_variable_runtime;
  1062. efi_runtime_services.get_next_variable_name =
  1063. efi_get_next_variable_name_runtime;
  1064. efi_runtime_services.set_variable = efi_set_variable_runtime;
  1065. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  1066. }
  1067. /**
  1068. * efi_init_variables() - initialize variable services
  1069. *
  1070. * Return: status code
  1071. */
  1072. efi_status_t efi_init_variables(void)
  1073. {
  1074. efi_status_t ret;
  1075. ret = efi_init_secure_state();
  1076. return ret;
  1077. }