efi_variable.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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.h>
  10. #include <env_internal.h>
  11. #include <hexdump.h>
  12. #include <malloc.h>
  13. #include <rtc.h>
  14. #include <search.h>
  15. #include <uuid.h>
  16. #include <crypto/pkcs7_parser.h>
  17. #include <linux/bitops.h>
  18. #include <linux/compat.h>
  19. #include <u-boot/crc.h>
  20. enum efi_secure_mode {
  21. EFI_MODE_SETUP,
  22. EFI_MODE_USER,
  23. EFI_MODE_AUDIT,
  24. EFI_MODE_DEPLOYED,
  25. };
  26. static bool efi_secure_boot;
  27. static int efi_secure_mode;
  28. static u8 efi_vendor_keys;
  29. #define READ_ONLY BIT(31)
  30. static efi_status_t efi_get_variable_common(u16 *variable_name,
  31. const efi_guid_t *vendor,
  32. u32 *attributes,
  33. efi_uintn_t *data_size, void *data);
  34. static efi_status_t efi_set_variable_common(u16 *variable_name,
  35. const efi_guid_t *vendor,
  36. u32 attributes,
  37. efi_uintn_t data_size,
  38. const void *data,
  39. bool ro_check);
  40. /*
  41. * Mapping between EFI variables and u-boot variables:
  42. *
  43. * efi_$guid_$varname = {attributes}(type)value
  44. *
  45. * For example:
  46. *
  47. * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
  48. * "{ro,boot,run}(blob)0000000000000000"
  49. * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
  50. * "(blob)00010000"
  51. *
  52. * The attributes are a comma separated list of these possible
  53. * attributes:
  54. *
  55. * + ro - read-only
  56. * + boot - boot-services access
  57. * + run - runtime access
  58. *
  59. * NOTE: with current implementation, no variables are available after
  60. * ExitBootServices, and all are persisted (if possible).
  61. *
  62. * If not specified, the attributes default to "{boot}".
  63. *
  64. * The required type is one of:
  65. *
  66. * + utf8 - raw utf8 string
  67. * + blob - arbitrary length hex string
  68. *
  69. * Maybe a utf16 type would be useful to for a string value to be auto
  70. * converted to utf16?
  71. */
  72. #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
  73. /**
  74. * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
  75. * variable name
  76. *
  77. * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
  78. * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
  79. * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
  80. *
  81. * @native: pointer to pointer to U-Boot variable name
  82. * @variable_name: UEFI variable name
  83. * @vendor: vendor GUID
  84. * Return: status code
  85. */
  86. static efi_status_t efi_to_native(char **native, const u16 *variable_name,
  87. const efi_guid_t *vendor)
  88. {
  89. size_t len;
  90. char *pos;
  91. len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
  92. *native = malloc(len);
  93. if (!*native)
  94. return EFI_OUT_OF_RESOURCES;
  95. pos = *native;
  96. pos += sprintf(pos, "efi_%pUl_", vendor);
  97. utf16_utf8_strcpy(&pos, variable_name);
  98. return EFI_SUCCESS;
  99. }
  100. /**
  101. * prefix() - skip over prefix
  102. *
  103. * Skip over a prefix string.
  104. *
  105. * @str: string with prefix
  106. * @prefix: prefix string
  107. * Return: string without prefix, or NULL if prefix not found
  108. */
  109. static const char *prefix(const char *str, const char *prefix)
  110. {
  111. size_t n = strlen(prefix);
  112. if (!strncmp(prefix, str, n))
  113. return str + n;
  114. return NULL;
  115. }
  116. /**
  117. * parse_attr() - decode attributes part of variable value
  118. *
  119. * Convert the string encoded attributes of a UEFI variable to a bit mask.
  120. * TODO: Several attributes are not supported.
  121. *
  122. * @str: value of U-Boot variable
  123. * @attrp: pointer to UEFI attributes
  124. * @timep: pointer to time attribute
  125. * Return: pointer to remainder of U-Boot variable value
  126. */
  127. static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
  128. {
  129. u32 attr = 0;
  130. char sep = '{';
  131. if (*str != '{') {
  132. *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
  133. return str;
  134. }
  135. while (*str == sep) {
  136. const char *s;
  137. str++;
  138. if ((s = prefix(str, "ro"))) {
  139. attr |= READ_ONLY;
  140. } else if ((s = prefix(str, "nv"))) {
  141. attr |= EFI_VARIABLE_NON_VOLATILE;
  142. } else if ((s = prefix(str, "boot"))) {
  143. attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
  144. } else if ((s = prefix(str, "run"))) {
  145. attr |= EFI_VARIABLE_RUNTIME_ACCESS;
  146. } else if ((s = prefix(str, "time="))) {
  147. attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
  148. hex2bin((u8 *)timep, s, sizeof(*timep));
  149. s += sizeof(*timep) * 2;
  150. } else if (*str == '}') {
  151. break;
  152. } else {
  153. printf("invalid attribute: %s\n", str);
  154. break;
  155. }
  156. str = s;
  157. sep = ',';
  158. }
  159. str++;
  160. *attrp = attr;
  161. return str;
  162. }
  163. /**
  164. * efi_set_secure_state - modify secure boot state variables
  165. * @sec_boot: value of SecureBoot
  166. * @setup_mode: value of SetupMode
  167. * @audit_mode: value of AuditMode
  168. * @deployed_mode: value of DeployedMode
  169. *
  170. * Modify secure boot stat-related variables as indicated.
  171. *
  172. * Return: status code
  173. */
  174. static efi_status_t efi_set_secure_state(int sec_boot, int setup_mode,
  175. int audit_mode, int deployed_mode)
  176. {
  177. u32 attributes;
  178. efi_status_t ret;
  179. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  180. EFI_VARIABLE_RUNTIME_ACCESS |
  181. READ_ONLY;
  182. ret = efi_set_variable_common(L"SecureBoot", &efi_global_variable_guid,
  183. attributes, sizeof(sec_boot), &sec_boot,
  184. false);
  185. if (ret != EFI_SUCCESS)
  186. goto err;
  187. ret = efi_set_variable_common(L"SetupMode", &efi_global_variable_guid,
  188. attributes, sizeof(setup_mode),
  189. &setup_mode, false);
  190. if (ret != EFI_SUCCESS)
  191. goto err;
  192. ret = efi_set_variable_common(L"AuditMode", &efi_global_variable_guid,
  193. attributes, sizeof(audit_mode),
  194. &audit_mode, false);
  195. if (ret != EFI_SUCCESS)
  196. goto err;
  197. ret = efi_set_variable_common(L"DeployedMode",
  198. &efi_global_variable_guid, attributes,
  199. sizeof(deployed_mode), &deployed_mode,
  200. false);
  201. err:
  202. return ret;
  203. }
  204. /**
  205. * efi_transfer_secure_state - handle a secure boot state transition
  206. * @mode: new state
  207. *
  208. * Depending on @mode, secure boot related variables are updated.
  209. * Those variables are *read-only* for users, efi_set_variable_common()
  210. * is called here.
  211. *
  212. * Return: status code
  213. */
  214. static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
  215. {
  216. efi_status_t ret;
  217. debug("Switching secure state from %d to %d\n", efi_secure_mode, mode);
  218. if (mode == EFI_MODE_DEPLOYED) {
  219. ret = efi_set_secure_state(1, 0, 0, 1);
  220. if (ret != EFI_SUCCESS)
  221. goto err;
  222. efi_secure_boot = true;
  223. } else if (mode == EFI_MODE_AUDIT) {
  224. ret = efi_set_variable_common(L"PK", &efi_global_variable_guid,
  225. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  226. EFI_VARIABLE_RUNTIME_ACCESS,
  227. 0, NULL, false);
  228. if (ret != EFI_SUCCESS)
  229. goto err;
  230. ret = efi_set_secure_state(0, 1, 1, 0);
  231. if (ret != EFI_SUCCESS)
  232. goto err;
  233. efi_secure_boot = true;
  234. } else if (mode == EFI_MODE_USER) {
  235. ret = efi_set_secure_state(1, 0, 0, 0);
  236. if (ret != EFI_SUCCESS)
  237. goto err;
  238. efi_secure_boot = true;
  239. } else if (mode == EFI_MODE_SETUP) {
  240. ret = efi_set_secure_state(0, 1, 0, 0);
  241. if (ret != EFI_SUCCESS)
  242. goto err;
  243. } else {
  244. return EFI_INVALID_PARAMETER;
  245. }
  246. efi_secure_mode = mode;
  247. return EFI_SUCCESS;
  248. err:
  249. /* TODO: What action should be taken here? */
  250. printf("ERROR: Secure state transition failed\n");
  251. return ret;
  252. }
  253. /**
  254. * efi_init_secure_state - initialize secure boot state
  255. *
  256. * Return: status code
  257. */
  258. static efi_status_t efi_init_secure_state(void)
  259. {
  260. enum efi_secure_mode mode;
  261. efi_uintn_t size;
  262. efi_status_t ret;
  263. /*
  264. * TODO:
  265. * Since there is currently no "platform-specific" installation
  266. * method of Platform Key, we can't say if VendorKeys is 0 or 1
  267. * precisely.
  268. */
  269. size = 0;
  270. ret = efi_get_variable_common(L"PK", &efi_global_variable_guid,
  271. NULL, &size, NULL);
  272. if (ret == EFI_BUFFER_TOO_SMALL) {
  273. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT))
  274. mode = EFI_MODE_USER;
  275. else
  276. mode = EFI_MODE_SETUP;
  277. efi_vendor_keys = 0;
  278. } else if (ret == EFI_NOT_FOUND) {
  279. mode = EFI_MODE_SETUP;
  280. efi_vendor_keys = 1;
  281. } else {
  282. goto err;
  283. }
  284. ret = efi_transfer_secure_state(mode);
  285. if (ret == EFI_SUCCESS)
  286. ret = efi_set_variable_common(L"VendorKeys",
  287. &efi_global_variable_guid,
  288. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  289. EFI_VARIABLE_RUNTIME_ACCESS |
  290. READ_ONLY,
  291. sizeof(efi_vendor_keys),
  292. &efi_vendor_keys, false);
  293. err:
  294. return ret;
  295. }
  296. /**
  297. * efi_secure_boot_enabled - return if secure boot is enabled or not
  298. *
  299. * Return: true if enabled, false if disabled
  300. */
  301. bool efi_secure_boot_enabled(void)
  302. {
  303. return efi_secure_boot;
  304. }
  305. #ifdef CONFIG_EFI_SECURE_BOOT
  306. static u8 pkcs7_hdr[] = {
  307. /* SEQUENCE */
  308. 0x30, 0x82, 0x05, 0xc7,
  309. /* OID: pkcs7-signedData */
  310. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
  311. /* Context Structured? */
  312. 0xa0, 0x82, 0x05, 0xb8,
  313. };
  314. /**
  315. * efi_variable_parse_signature - parse a signature in variable
  316. * @buf: Pointer to variable's value
  317. * @buflen: Length of @buf
  318. *
  319. * Parse a signature embedded in variable's value and instantiate
  320. * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
  321. * pkcs7's signedData, some header needed be prepended for correctly
  322. * parsing authentication data, particularly for variable's.
  323. *
  324. * Return: Pointer to pkcs7_message structure on success, NULL on error
  325. */
  326. static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
  327. size_t buflen)
  328. {
  329. u8 *ebuf;
  330. size_t ebuflen, len;
  331. struct pkcs7_message *msg;
  332. /*
  333. * This is the best assumption to check if the binary is
  334. * already in a form of pkcs7's signedData.
  335. */
  336. if (buflen > sizeof(pkcs7_hdr) &&
  337. !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
  338. msg = pkcs7_parse_message(buf, buflen);
  339. goto out;
  340. }
  341. /*
  342. * Otherwise, we should add a dummy prefix sequence for pkcs7
  343. * message parser to be able to process.
  344. * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
  345. * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
  346. * TODO:
  347. * The header should be composed in a more refined manner.
  348. */
  349. debug("Makeshift prefix added to authentication data\n");
  350. ebuflen = sizeof(pkcs7_hdr) + buflen;
  351. if (ebuflen <= 0x7f) {
  352. debug("Data is too short\n");
  353. return NULL;
  354. }
  355. ebuf = malloc(ebuflen);
  356. if (!ebuf) {
  357. debug("Out of memory\n");
  358. return NULL;
  359. }
  360. memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
  361. memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
  362. len = ebuflen - 4;
  363. ebuf[2] = (len >> 8) & 0xff;
  364. ebuf[3] = len & 0xff;
  365. len = ebuflen - 0x13;
  366. ebuf[0x11] = (len >> 8) & 0xff;
  367. ebuf[0x12] = len & 0xff;
  368. msg = pkcs7_parse_message(ebuf, ebuflen);
  369. free(ebuf);
  370. out:
  371. if (IS_ERR(msg))
  372. return NULL;
  373. return msg;
  374. }
  375. /**
  376. * efi_variable_authenticate - authenticate a variable
  377. * @variable: Variable name in u16
  378. * @vendor: Guid of variable
  379. * @data_size: Size of @data
  380. * @data: Pointer to variable's value
  381. * @given_attr: Attributes to be given at SetVariable()
  382. * @env_attr: Attributes that an existing variable holds
  383. * @time: signed time that an existing variable holds
  384. *
  385. * Called by efi_set_variable() to verify that the input is correct.
  386. * Will replace the given data pointer with another that points to
  387. * the actual data to store in the internal memory.
  388. * On success, @data and @data_size will be replaced with variable's
  389. * actual data, excluding authentication data, and its size, and variable's
  390. * attributes and signed time will also be returned in @env_attr and @time,
  391. * respectively.
  392. *
  393. * Return: status code
  394. */
  395. static efi_status_t efi_variable_authenticate(u16 *variable,
  396. const efi_guid_t *vendor,
  397. efi_uintn_t *data_size,
  398. const void **data, u32 given_attr,
  399. u32 *env_attr, u64 *time)
  400. {
  401. const struct efi_variable_authentication_2 *auth;
  402. struct efi_signature_store *truststore, *truststore2;
  403. struct pkcs7_message *var_sig;
  404. struct efi_image_regions *regs;
  405. struct efi_time timestamp;
  406. struct rtc_time tm;
  407. u64 new_time;
  408. efi_status_t ret;
  409. var_sig = NULL;
  410. truststore = NULL;
  411. truststore2 = NULL;
  412. regs = NULL;
  413. ret = EFI_SECURITY_VIOLATION;
  414. if (*data_size < sizeof(struct efi_variable_authentication_2))
  415. goto err;
  416. /* authentication data */
  417. auth = *data;
  418. if (*data_size < (sizeof(auth->time_stamp)
  419. + auth->auth_info.hdr.dwLength))
  420. goto err;
  421. if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
  422. goto err;
  423. *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
  424. *data_size -= (sizeof(auth->time_stamp)
  425. + auth->auth_info.hdr.dwLength);
  426. memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
  427. memset(&tm, 0, sizeof(tm));
  428. tm.tm_year = timestamp.year;
  429. tm.tm_mon = timestamp.month;
  430. tm.tm_mday = timestamp.day;
  431. tm.tm_hour = timestamp.hour;
  432. tm.tm_min = timestamp.minute;
  433. tm.tm_sec = timestamp.second;
  434. new_time = rtc_mktime(&tm);
  435. if (!efi_secure_boot_enabled()) {
  436. /* finished checking */
  437. *time = new_time;
  438. return EFI_SUCCESS;
  439. }
  440. if (new_time <= *time)
  441. goto err;
  442. /* data to be digested */
  443. regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
  444. if (!regs)
  445. goto err;
  446. regs->max = 5;
  447. efi_image_region_add(regs, (uint8_t *)variable,
  448. (uint8_t *)variable
  449. + u16_strlen(variable) * sizeof(u16), 1);
  450. efi_image_region_add(regs, (uint8_t *)vendor,
  451. (uint8_t *)vendor + sizeof(*vendor), 1);
  452. efi_image_region_add(regs, (uint8_t *)&given_attr,
  453. (uint8_t *)&given_attr + sizeof(given_attr), 1);
  454. efi_image_region_add(regs, (uint8_t *)&timestamp,
  455. (uint8_t *)&timestamp + sizeof(timestamp), 1);
  456. efi_image_region_add(regs, (uint8_t *)*data,
  457. (uint8_t *)*data + *data_size, 1);
  458. /* variable's signature list */
  459. if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
  460. goto err;
  461. var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
  462. auth->auth_info.hdr.dwLength
  463. - sizeof(auth->auth_info));
  464. if (!var_sig) {
  465. debug("Parsing variable's signature failed\n");
  466. goto err;
  467. }
  468. /* signature database used for authentication */
  469. if (u16_strcmp(variable, L"PK") == 0 ||
  470. u16_strcmp(variable, L"KEK") == 0) {
  471. /* with PK */
  472. truststore = efi_sigstore_parse_sigdb(L"PK");
  473. if (!truststore)
  474. goto err;
  475. } else if (u16_strcmp(variable, L"db") == 0 ||
  476. u16_strcmp(variable, L"dbx") == 0) {
  477. /* with PK and KEK */
  478. truststore = efi_sigstore_parse_sigdb(L"KEK");
  479. truststore2 = efi_sigstore_parse_sigdb(L"PK");
  480. if (!truststore) {
  481. if (!truststore2)
  482. goto err;
  483. truststore = truststore2;
  484. truststore2 = NULL;
  485. }
  486. } else {
  487. /* TODO: support private authenticated variables */
  488. goto err;
  489. }
  490. /* verify signature */
  491. if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
  492. debug("Verified\n");
  493. } else {
  494. if (truststore2 &&
  495. efi_signature_verify_with_sigdb(regs, var_sig,
  496. truststore2, NULL)) {
  497. debug("Verified\n");
  498. } else {
  499. debug("Verifying variable's signature failed\n");
  500. goto err;
  501. }
  502. }
  503. /* finished checking */
  504. *time = rtc_mktime(&tm);
  505. ret = EFI_SUCCESS;
  506. err:
  507. efi_sigstore_free(truststore);
  508. efi_sigstore_free(truststore2);
  509. pkcs7_free_message(var_sig);
  510. free(regs);
  511. return ret;
  512. }
  513. #else
  514. static efi_status_t efi_variable_authenticate(u16 *variable,
  515. const efi_guid_t *vendor,
  516. efi_uintn_t *data_size,
  517. const void **data, u32 given_attr,
  518. u32 *env_attr, u64 *time)
  519. {
  520. return EFI_SUCCESS;
  521. }
  522. #endif /* CONFIG_EFI_SECURE_BOOT */
  523. static efi_status_t efi_get_variable_common(u16 *variable_name,
  524. const efi_guid_t *vendor,
  525. u32 *attributes,
  526. efi_uintn_t *data_size, void *data)
  527. {
  528. char *native_name;
  529. efi_status_t ret;
  530. unsigned long in_size;
  531. const char *val = NULL, *s;
  532. u64 time = 0;
  533. u32 attr;
  534. if (!variable_name || !vendor || !data_size)
  535. return EFI_EXIT(EFI_INVALID_PARAMETER);
  536. ret = efi_to_native(&native_name, variable_name, vendor);
  537. if (ret)
  538. return ret;
  539. EFI_PRINT("get '%s'\n", native_name);
  540. val = env_get(native_name);
  541. free(native_name);
  542. if (!val)
  543. return EFI_NOT_FOUND;
  544. val = parse_attr(val, &attr, &time);
  545. in_size = *data_size;
  546. if ((s = prefix(val, "(blob)"))) {
  547. size_t len = strlen(s);
  548. /* number of hexadecimal digits must be even */
  549. if (len & 1)
  550. return EFI_DEVICE_ERROR;
  551. /* two characters per byte: */
  552. len /= 2;
  553. *data_size = len;
  554. if (in_size < len) {
  555. ret = EFI_BUFFER_TOO_SMALL;
  556. goto out;
  557. }
  558. if (!data) {
  559. debug("Variable with no data shouldn't exist.\n");
  560. return EFI_INVALID_PARAMETER;
  561. }
  562. if (hex2bin(data, s, len))
  563. return EFI_DEVICE_ERROR;
  564. EFI_PRINT("got value: \"%s\"\n", s);
  565. } else if ((s = prefix(val, "(utf8)"))) {
  566. unsigned len = strlen(s) + 1;
  567. *data_size = len;
  568. if (in_size < len) {
  569. ret = EFI_BUFFER_TOO_SMALL;
  570. goto out;
  571. }
  572. if (!data) {
  573. debug("Variable with no data shouldn't exist.\n");
  574. return EFI_INVALID_PARAMETER;
  575. }
  576. memcpy(data, s, len);
  577. ((char *)data)[len] = '\0';
  578. EFI_PRINT("got value: \"%s\"\n", (char *)data);
  579. } else {
  580. EFI_PRINT("invalid value: '%s'\n", val);
  581. return EFI_DEVICE_ERROR;
  582. }
  583. out:
  584. if (attributes)
  585. *attributes = attr & EFI_VARIABLE_MASK;
  586. return ret;
  587. }
  588. /**
  589. * efi_efi_get_variable() - retrieve value of a UEFI variable
  590. *
  591. * This function implements the GetVariable runtime service.
  592. *
  593. * See the Unified Extensible Firmware Interface (UEFI) specification for
  594. * details.
  595. *
  596. * @variable_name: name of the variable
  597. * @vendor: vendor GUID
  598. * @attributes: attributes of the variable
  599. * @data_size: size of the buffer to which the variable value is copied
  600. * @data: buffer to which the variable value is copied
  601. * Return: status code
  602. */
  603. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  604. const efi_guid_t *vendor, u32 *attributes,
  605. efi_uintn_t *data_size, void *data)
  606. {
  607. efi_status_t ret;
  608. EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
  609. data_size, data);
  610. ret = efi_get_variable_common(variable_name, vendor, attributes,
  611. data_size, data);
  612. return EFI_EXIT(ret);
  613. }
  614. static char *efi_variables_list;
  615. static char *efi_cur_variable;
  616. /**
  617. * parse_uboot_variable() - parse a u-boot variable and get uefi-related
  618. * information
  619. * @variable: whole data of u-boot variable (ie. name=value)
  620. * @variable_name_size: size of variable_name buffer in byte
  621. * @variable_name: name of uefi variable in u16, null-terminated
  622. * @vendor: vendor's guid
  623. * @attributes: attributes
  624. *
  625. * A uefi variable is encoded into a u-boot variable as described above.
  626. * This function parses such a u-boot variable and retrieve uefi-related
  627. * information into respective parameters. In return, variable_name_size
  628. * is the size of variable name including NULL.
  629. *
  630. * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
  631. * the entire variable list has been returned,
  632. * otherwise non-zero status code
  633. */
  634. static efi_status_t parse_uboot_variable(char *variable,
  635. efi_uintn_t *variable_name_size,
  636. u16 *variable_name,
  637. const efi_guid_t *vendor,
  638. u32 *attributes)
  639. {
  640. char *guid, *name, *end, c;
  641. size_t name_len;
  642. efi_uintn_t old_variable_name_size;
  643. u64 time;
  644. u16 *p;
  645. guid = strchr(variable, '_');
  646. if (!guid)
  647. return EFI_INVALID_PARAMETER;
  648. guid++;
  649. name = strchr(guid, '_');
  650. if (!name)
  651. return EFI_INVALID_PARAMETER;
  652. name++;
  653. end = strchr(name, '=');
  654. if (!end)
  655. return EFI_INVALID_PARAMETER;
  656. name_len = end - name;
  657. old_variable_name_size = *variable_name_size;
  658. *variable_name_size = sizeof(u16) * (name_len + 1);
  659. if (old_variable_name_size < *variable_name_size)
  660. return EFI_BUFFER_TOO_SMALL;
  661. end++; /* point to value */
  662. /* variable name */
  663. p = variable_name;
  664. utf8_utf16_strncpy(&p, name, name_len);
  665. variable_name[name_len] = 0;
  666. /* guid */
  667. c = *(name - 1);
  668. *(name - 1) = '\0'; /* guid need be null-terminated here */
  669. if (uuid_str_to_bin(guid, (unsigned char *)vendor,
  670. UUID_STR_FORMAT_GUID))
  671. /* The only error would be EINVAL. */
  672. return EFI_INVALID_PARAMETER;
  673. *(name - 1) = c;
  674. /* attributes */
  675. parse_attr(end, attributes, &time);
  676. return EFI_SUCCESS;
  677. }
  678. /**
  679. * efi_get_next_variable_name() - enumerate the current variable names
  680. *
  681. * @variable_name_size: size of variable_name buffer in byte
  682. * @variable_name: name of uefi variable's name in u16
  683. * @vendor: vendor's guid
  684. *
  685. * This function implements the GetNextVariableName service.
  686. *
  687. * See the Unified Extensible Firmware Interface (UEFI) specification for
  688. * details.
  689. *
  690. * Return: status code
  691. */
  692. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  693. u16 *variable_name,
  694. efi_guid_t *vendor)
  695. {
  696. char *native_name, *variable;
  697. ssize_t name_len, list_len;
  698. char regex[256];
  699. char * const regexlist[] = {regex};
  700. u32 attributes;
  701. int i;
  702. efi_status_t ret;
  703. EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
  704. if (!variable_name_size || !variable_name || !vendor)
  705. return EFI_EXIT(EFI_INVALID_PARAMETER);
  706. if (variable_name[0]) {
  707. /* check null-terminated string */
  708. for (i = 0; i < *variable_name_size; i++)
  709. if (!variable_name[i])
  710. break;
  711. if (i >= *variable_name_size)
  712. return EFI_EXIT(EFI_INVALID_PARAMETER);
  713. /* search for the last-returned variable */
  714. ret = efi_to_native(&native_name, variable_name, vendor);
  715. if (ret)
  716. return EFI_EXIT(ret);
  717. name_len = strlen(native_name);
  718. for (variable = efi_variables_list; variable && *variable;) {
  719. if (!strncmp(variable, native_name, name_len) &&
  720. variable[name_len] == '=')
  721. break;
  722. variable = strchr(variable, '\n');
  723. if (variable)
  724. variable++;
  725. }
  726. free(native_name);
  727. if (!(variable && *variable))
  728. return EFI_EXIT(EFI_INVALID_PARAMETER);
  729. /* next variable */
  730. variable = strchr(variable, '\n');
  731. if (variable)
  732. variable++;
  733. if (!(variable && *variable))
  734. return EFI_EXIT(EFI_NOT_FOUND);
  735. } else {
  736. /*
  737. *new search: free a list used in the previous search
  738. */
  739. free(efi_variables_list);
  740. efi_variables_list = NULL;
  741. efi_cur_variable = NULL;
  742. snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
  743. list_len = hexport_r(&env_htab, '\n',
  744. H_MATCH_REGEX | H_MATCH_KEY,
  745. &efi_variables_list, 0, 1, regexlist);
  746. if (list_len <= 1)
  747. return EFI_EXIT(EFI_NOT_FOUND);
  748. variable = efi_variables_list;
  749. }
  750. ret = parse_uboot_variable(variable, variable_name_size, variable_name,
  751. vendor, &attributes);
  752. return EFI_EXIT(ret);
  753. }
  754. static efi_status_t efi_set_variable_common(u16 *variable_name,
  755. const efi_guid_t *vendor,
  756. u32 attributes,
  757. efi_uintn_t data_size,
  758. const void *data,
  759. bool ro_check)
  760. {
  761. char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
  762. efi_uintn_t old_size;
  763. bool append, delete;
  764. u64 time = 0;
  765. u32 attr;
  766. efi_status_t ret = EFI_SUCCESS;
  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. }