efi_variable.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * UEFI runtime variable services
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #define LOG_CATEGORY LOGC_EFI
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <efi_variable.h>
  11. #include <env.h>
  12. #include <env_internal.h>
  13. #include <hexdump.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <rtc.h>
  17. #include <search.h>
  18. #include <uuid.h>
  19. #include <crypto/pkcs7_parser.h>
  20. #include <linux/compat.h>
  21. #include <u-boot/crc.h>
  22. #include <asm/sections.h>
  23. #ifdef CONFIG_EFI_SECURE_BOOT
  24. /**
  25. * efi_variable_authenticate - authenticate a variable
  26. * @variable: Variable name in u16
  27. * @vendor: Guid of variable
  28. * @data_size: Size of @data
  29. * @data: Pointer to variable's value
  30. * @given_attr: Attributes to be given at SetVariable()
  31. * @env_attr: Attributes that an existing variable holds
  32. * @time: signed time that an existing variable holds
  33. *
  34. * Called by efi_set_variable() to verify that the input is correct.
  35. * Will replace the given data pointer with another that points to
  36. * the actual data to store in the internal memory.
  37. * On success, @data and @data_size will be replaced with variable's
  38. * actual data, excluding authentication data, and its size, and variable's
  39. * attributes and signed time will also be returned in @env_attr and @time,
  40. * respectively.
  41. *
  42. * Return: status code
  43. */
  44. static efi_status_t efi_variable_authenticate(const u16 *variable,
  45. const efi_guid_t *vendor,
  46. efi_uintn_t *data_size,
  47. const void **data, u32 given_attr,
  48. u32 *env_attr, u64 *time)
  49. {
  50. const struct efi_variable_authentication_2 *auth;
  51. struct efi_signature_store *truststore, *truststore2;
  52. struct pkcs7_message *var_sig;
  53. struct efi_image_regions *regs;
  54. struct efi_time timestamp;
  55. struct rtc_time tm;
  56. u64 new_time;
  57. u8 *ebuf;
  58. enum efi_auth_var_type var_type;
  59. efi_status_t ret;
  60. var_sig = NULL;
  61. truststore = NULL;
  62. truststore2 = NULL;
  63. regs = NULL;
  64. ebuf = NULL;
  65. ret = EFI_SECURITY_VIOLATION;
  66. if (*data_size < sizeof(struct efi_variable_authentication_2))
  67. goto err;
  68. /* authentication data */
  69. auth = *data;
  70. if (*data_size < (sizeof(auth->time_stamp)
  71. + auth->auth_info.hdr.dwLength))
  72. goto err;
  73. if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
  74. goto err;
  75. memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
  76. if (timestamp.pad1 || timestamp.nanosecond || timestamp.timezone ||
  77. timestamp.daylight || timestamp.pad2)
  78. goto err;
  79. *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
  80. *data_size -= (sizeof(auth->time_stamp)
  81. + auth->auth_info.hdr.dwLength);
  82. memset(&tm, 0, sizeof(tm));
  83. tm.tm_year = timestamp.year;
  84. tm.tm_mon = timestamp.month;
  85. tm.tm_mday = timestamp.day;
  86. tm.tm_hour = timestamp.hour;
  87. tm.tm_min = timestamp.minute;
  88. tm.tm_sec = timestamp.second;
  89. new_time = rtc_mktime(&tm);
  90. if (!efi_secure_boot_enabled()) {
  91. /* finished checking */
  92. *time = new_time;
  93. return EFI_SUCCESS;
  94. }
  95. if (new_time <= *time)
  96. goto err;
  97. /* data to be digested */
  98. regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
  99. if (!regs)
  100. goto err;
  101. regs->max = 5;
  102. efi_image_region_add(regs, (uint8_t *)variable,
  103. (uint8_t *)variable
  104. + u16_strlen(variable) * sizeof(u16), 1);
  105. efi_image_region_add(regs, (uint8_t *)vendor,
  106. (uint8_t *)vendor + sizeof(*vendor), 1);
  107. efi_image_region_add(regs, (uint8_t *)&given_attr,
  108. (uint8_t *)&given_attr + sizeof(given_attr), 1);
  109. efi_image_region_add(regs, (uint8_t *)&timestamp,
  110. (uint8_t *)&timestamp + sizeof(timestamp), 1);
  111. efi_image_region_add(regs, (uint8_t *)*data,
  112. (uint8_t *)*data + *data_size, 1);
  113. /* variable's signature list */
  114. if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
  115. goto err;
  116. /* ebuf should be kept valid during the authentication */
  117. var_sig = efi_parse_pkcs7_header(auth->auth_info.cert_data,
  118. auth->auth_info.hdr.dwLength
  119. - sizeof(auth->auth_info),
  120. &ebuf);
  121. if (!var_sig) {
  122. EFI_PRINT("Parsing variable's signature failed\n");
  123. goto err;
  124. }
  125. /* signature database used for authentication */
  126. var_type = efi_auth_var_get_type(variable, vendor);
  127. switch (var_type) {
  128. case EFI_AUTH_VAR_PK:
  129. case EFI_AUTH_VAR_KEK:
  130. /* with PK */
  131. truststore = efi_sigstore_parse_sigdb(u"PK");
  132. if (!truststore)
  133. goto err;
  134. break;
  135. case EFI_AUTH_VAR_DB:
  136. case EFI_AUTH_VAR_DBX:
  137. /* with PK and KEK */
  138. truststore = efi_sigstore_parse_sigdb(u"KEK");
  139. truststore2 = efi_sigstore_parse_sigdb(u"PK");
  140. if (!truststore) {
  141. if (!truststore2)
  142. goto err;
  143. truststore = truststore2;
  144. truststore2 = NULL;
  145. }
  146. break;
  147. default:
  148. /* TODO: support private authenticated variables */
  149. goto err;
  150. }
  151. /* verify signature */
  152. if (efi_signature_verify(regs, var_sig, truststore, NULL)) {
  153. EFI_PRINT("Verified\n");
  154. } else {
  155. if (truststore2 &&
  156. efi_signature_verify(regs, var_sig, truststore2, NULL)) {
  157. EFI_PRINT("Verified\n");
  158. } else {
  159. EFI_PRINT("Verifying variable's signature failed\n");
  160. goto err;
  161. }
  162. }
  163. /* finished checking */
  164. *time = new_time;
  165. ret = EFI_SUCCESS;
  166. err:
  167. efi_sigstore_free(truststore);
  168. efi_sigstore_free(truststore2);
  169. pkcs7_free_message(var_sig);
  170. free(ebuf);
  171. free(regs);
  172. return ret;
  173. }
  174. #else
  175. static efi_status_t efi_variable_authenticate(const u16 *variable,
  176. const efi_guid_t *vendor,
  177. efi_uintn_t *data_size,
  178. const void **data, u32 given_attr,
  179. u32 *env_attr, u64 *time)
  180. {
  181. return EFI_SUCCESS;
  182. }
  183. #endif /* CONFIG_EFI_SECURE_BOOT */
  184. efi_status_t __efi_runtime
  185. efi_get_variable_int(const u16 *variable_name, const efi_guid_t *vendor,
  186. u32 *attributes, efi_uintn_t *data_size, void *data,
  187. u64 *timep)
  188. {
  189. return efi_get_variable_mem(variable_name, vendor, attributes, data_size, data, timep);
  190. }
  191. efi_status_t __efi_runtime
  192. efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
  193. u16 *variable_name, efi_guid_t *vendor)
  194. {
  195. return efi_get_next_variable_name_mem(variable_name_size, variable_name, vendor);
  196. }
  197. efi_status_t efi_set_variable_int(const u16 *variable_name,
  198. const efi_guid_t *vendor,
  199. u32 attributes, efi_uintn_t data_size,
  200. const void *data, bool ro_check)
  201. {
  202. struct efi_var_entry *var;
  203. efi_uintn_t ret;
  204. bool append, delete;
  205. u64 time = 0;
  206. enum efi_auth_var_type var_type;
  207. if (!variable_name || !*variable_name || !vendor)
  208. return EFI_INVALID_PARAMETER;
  209. if (data_size && !data)
  210. return EFI_INVALID_PARAMETER;
  211. /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
  212. if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
  213. return EFI_UNSUPPORTED;
  214. /* Make sure if runtime bit is set, boot service bit is set also */
  215. if ((attributes &
  216. (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
  217. EFI_VARIABLE_RUNTIME_ACCESS)
  218. return EFI_INVALID_PARAMETER;
  219. /* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */
  220. if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
  221. return EFI_INVALID_PARAMETER;
  222. /* Make sure HR is set with NV, BS and RT */
  223. if (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD &&
  224. (!(attributes & EFI_VARIABLE_NON_VOLATILE) ||
  225. !(attributes & EFI_VARIABLE_RUNTIME_ACCESS) ||
  226. !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
  227. return EFI_INVALID_PARAMETER;
  228. /* check if a variable exists */
  229. var = efi_var_mem_find(vendor, variable_name, NULL);
  230. append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
  231. attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
  232. delete = !append && (!data_size || !attributes);
  233. /* check attributes */
  234. var_type = efi_auth_var_get_type(variable_name, vendor);
  235. if (var) {
  236. if (ro_check && (var->attr & EFI_VARIABLE_READ_ONLY))
  237. return EFI_WRITE_PROTECTED;
  238. if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
  239. if (var_type >= EFI_AUTH_VAR_PK)
  240. return EFI_WRITE_PROTECTED;
  241. }
  242. /* attributes won't be changed */
  243. if (!delete &&
  244. ((ro_check && var->attr != attributes) ||
  245. (!ro_check && ((var->attr & ~(u32)EFI_VARIABLE_READ_ONLY)
  246. != (attributes & ~(u32)EFI_VARIABLE_READ_ONLY))))) {
  247. return EFI_INVALID_PARAMETER;
  248. }
  249. time = var->time;
  250. } else {
  251. if (delete || append)
  252. /*
  253. * Trying to delete or to update a non-existent
  254. * variable.
  255. */
  256. return EFI_NOT_FOUND;
  257. }
  258. if (var_type >= EFI_AUTH_VAR_PK) {
  259. /* authentication is mandatory */
  260. if (!(attributes &
  261. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
  262. EFI_PRINT("%ls: TIME_BASED_AUTHENTICATED_WRITE_ACCESS required\n",
  263. variable_name);
  264. return EFI_INVALID_PARAMETER;
  265. }
  266. }
  267. /* authenticate a variable */
  268. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
  269. if (attributes &
  270. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  271. u32 env_attr;
  272. ret = efi_variable_authenticate(variable_name, vendor,
  273. &data_size, &data,
  274. attributes, &env_attr,
  275. &time);
  276. if (ret != EFI_SUCCESS)
  277. return ret;
  278. /* last chance to check for delete */
  279. if (!data_size)
  280. delete = true;
  281. }
  282. } else {
  283. if (attributes &
  284. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  285. EFI_PRINT("Secure boot is not configured\n");
  286. return EFI_INVALID_PARAMETER;
  287. }
  288. }
  289. if (delete) {
  290. /* EFI_NOT_FOUND has been handled before */
  291. attributes = var->attr;
  292. ret = EFI_SUCCESS;
  293. } else if (append) {
  294. u16 *old_data = var->name;
  295. for (; *old_data; ++old_data)
  296. ;
  297. ++old_data;
  298. ret = efi_var_mem_ins(variable_name, vendor, attributes,
  299. var->length, old_data, data_size, data,
  300. time);
  301. } else {
  302. ret = efi_var_mem_ins(variable_name, vendor, attributes,
  303. data_size, data, 0, NULL, time);
  304. }
  305. efi_var_mem_del(var);
  306. if (ret != EFI_SUCCESS)
  307. return ret;
  308. if (var_type == EFI_AUTH_VAR_PK)
  309. ret = efi_init_secure_state();
  310. else
  311. ret = EFI_SUCCESS;
  312. /*
  313. * Write non-volatile EFI variables to file
  314. * TODO: check if a value change has occured to avoid superfluous writes
  315. */
  316. if (attributes & EFI_VARIABLE_NON_VOLATILE)
  317. efi_var_to_file();
  318. return EFI_SUCCESS;
  319. }
  320. efi_status_t efi_query_variable_info_int(u32 attributes,
  321. u64 *maximum_variable_storage_size,
  322. u64 *remaining_variable_storage_size,
  323. u64 *maximum_variable_size)
  324. {
  325. if (attributes == 0)
  326. return EFI_INVALID_PARAMETER;
  327. /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
  328. if ((attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) ||
  329. ((attributes & EFI_VARIABLE_MASK) == 0))
  330. return EFI_UNSUPPORTED;
  331. if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
  332. return EFI_INVALID_PARAMETER;
  333. /* Make sure if runtime bit is set, boot service bit is set also. */
  334. if ((attributes &
  335. (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
  336. EFI_VARIABLE_RUNTIME_ACCESS)
  337. return EFI_INVALID_PARAMETER;
  338. if (attributes & ~(u32)EFI_VARIABLE_MASK)
  339. return EFI_INVALID_PARAMETER;
  340. *maximum_variable_storage_size = EFI_VAR_BUF_SIZE -
  341. sizeof(struct efi_var_file);
  342. *remaining_variable_storage_size = efi_var_mem_free();
  343. *maximum_variable_size = EFI_VAR_BUF_SIZE -
  344. sizeof(struct efi_var_file) -
  345. sizeof(struct efi_var_entry);
  346. return EFI_SUCCESS;
  347. }
  348. /**
  349. * efi_query_variable_info_runtime() - runtime implementation of
  350. * QueryVariableInfo()
  351. *
  352. * @attributes: bitmask to select variables to be
  353. * queried
  354. * @maximum_variable_storage_size: maximum size of storage area for the
  355. * selected variable types
  356. * @remaining_variable_storage_size: remaining size of storage are for the
  357. * selected variable types
  358. * @maximum_variable_size: maximum size of a variable of the
  359. * selected type
  360. * Returns: status code
  361. */
  362. static efi_status_t __efi_runtime EFIAPI efi_query_variable_info_runtime(
  363. u32 attributes,
  364. u64 *maximum_variable_storage_size,
  365. u64 *remaining_variable_storage_size,
  366. u64 *maximum_variable_size)
  367. {
  368. return EFI_UNSUPPORTED;
  369. }
  370. /**
  371. * efi_set_variable_runtime() - runtime implementation of SetVariable()
  372. *
  373. * @variable_name: name of the variable
  374. * @vendor: vendor GUID
  375. * @attributes: attributes of the variable
  376. * @data_size: size of the buffer with the variable value
  377. * @data: buffer with the variable value
  378. * Return: status code
  379. */
  380. static efi_status_t __efi_runtime EFIAPI
  381. efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
  382. u32 attributes, efi_uintn_t data_size,
  383. const void *data)
  384. {
  385. return EFI_UNSUPPORTED;
  386. }
  387. /**
  388. * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
  389. */
  390. void efi_variables_boot_exit_notify(void)
  391. {
  392. /* Switch variable services functions to runtime version */
  393. efi_runtime_services.get_variable = efi_get_variable_runtime;
  394. efi_runtime_services.get_next_variable_name =
  395. efi_get_next_variable_name_runtime;
  396. efi_runtime_services.set_variable = efi_set_variable_runtime;
  397. efi_runtime_services.query_variable_info =
  398. efi_query_variable_info_runtime;
  399. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  400. }
  401. /**
  402. * efi_init_variables() - initialize variable services
  403. *
  404. * Return: status code
  405. */
  406. efi_status_t efi_init_variables(void)
  407. {
  408. efi_status_t ret;
  409. ret = efi_var_mem_init();
  410. if (ret != EFI_SUCCESS)
  411. return ret;
  412. ret = efi_var_from_file();
  413. if (ret != EFI_SUCCESS)
  414. return ret;
  415. if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
  416. ret = efi_var_restore((struct efi_var_file *)
  417. __efi_var_file_begin, true);
  418. if (ret != EFI_SUCCESS)
  419. log_err("Invalid EFI variable seed\n");
  420. }
  421. return efi_init_secure_state();
  422. }