efi_var_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * UEFI runtime variable services
  4. *
  5. * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. * Copyright (c) 2020 Linaro Limited, Author: AKASHI Takahiro
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <efi_variable.h>
  11. #include <stdlib.h>
  12. enum efi_secure_mode {
  13. EFI_MODE_SETUP,
  14. EFI_MODE_USER,
  15. EFI_MODE_AUDIT,
  16. EFI_MODE_DEPLOYED,
  17. };
  18. struct efi_auth_var_name_type {
  19. const u16 *name;
  20. const efi_guid_t *guid;
  21. const enum efi_auth_var_type type;
  22. };
  23. const efi_guid_t efi_guid_image_security_database =
  24. EFI_IMAGE_SECURITY_DATABASE_GUID;
  25. static const struct efi_auth_var_name_type name_type[] = {
  26. {u"PK", &efi_global_variable_guid, EFI_AUTH_VAR_PK},
  27. {u"KEK", &efi_global_variable_guid, EFI_AUTH_VAR_KEK},
  28. {u"db", &efi_guid_image_security_database, EFI_AUTH_VAR_DB},
  29. {u"dbx", &efi_guid_image_security_database, EFI_AUTH_VAR_DBX},
  30. {u"dbt", &efi_guid_image_security_database, EFI_AUTH_VAR_DBT},
  31. {u"dbr", &efi_guid_image_security_database, EFI_AUTH_VAR_DBR},
  32. {u"AuditMode", &efi_global_variable_guid, EFI_AUTH_MODE},
  33. {u"DeployedMode", &efi_global_variable_guid, EFI_AUTH_MODE},
  34. };
  35. static bool efi_secure_boot;
  36. static enum efi_secure_mode efi_secure_mode;
  37. /**
  38. * efi_efi_get_variable() - retrieve value of a UEFI variable
  39. *
  40. * This function implements the GetVariable runtime service.
  41. *
  42. * See the Unified Extensible Firmware Interface (UEFI) specification for
  43. * details.
  44. *
  45. * @variable_name: name of the variable
  46. * @vendor: vendor GUID
  47. * @attributes: attributes of the variable
  48. * @data_size: size of the buffer to which the variable value is copied
  49. * @data: buffer to which the variable value is copied
  50. * Return: status code
  51. */
  52. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  53. const efi_guid_t *vendor, u32 *attributes,
  54. efi_uintn_t *data_size, void *data)
  55. {
  56. efi_status_t ret;
  57. EFI_ENTRY("\"%ls\" %pUs %p %p %p", variable_name, vendor, attributes,
  58. data_size, data);
  59. ret = efi_get_variable_int(variable_name, vendor, attributes,
  60. data_size, data, NULL);
  61. /* Remove EFI_VARIABLE_READ_ONLY flag */
  62. if (attributes)
  63. *attributes &= EFI_VARIABLE_MASK;
  64. return EFI_EXIT(ret);
  65. }
  66. /**
  67. * efi_set_variable() - set value of a UEFI variable
  68. *
  69. * This function implements the SetVariable runtime service.
  70. *
  71. * See the Unified Extensible Firmware Interface (UEFI) specification for
  72. * details.
  73. *
  74. * @variable_name: name of the variable
  75. * @vendor: vendor GUID
  76. * @attributes: attributes of the variable
  77. * @data_size: size of the buffer with the variable value
  78. * @data: buffer with the variable value
  79. * Return: status code
  80. */
  81. efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
  82. const efi_guid_t *vendor, u32 attributes,
  83. efi_uintn_t data_size, const void *data)
  84. {
  85. efi_status_t ret;
  86. EFI_ENTRY("\"%ls\" %pUs %x %zu %p", variable_name, vendor, attributes,
  87. data_size, data);
  88. /* Make sure that the EFI_VARIABLE_READ_ONLY flag is not set */
  89. if (attributes & ~(u32)EFI_VARIABLE_MASK)
  90. ret = EFI_INVALID_PARAMETER;
  91. else
  92. ret = efi_set_variable_int(variable_name, vendor, attributes,
  93. data_size, data, true);
  94. return EFI_EXIT(ret);
  95. }
  96. /**
  97. * efi_get_next_variable_name() - enumerate the current variable names
  98. *
  99. * @variable_name_size: size of variable_name buffer in byte
  100. * @variable_name: name of uefi variable's name in u16
  101. * @vendor: vendor's guid
  102. *
  103. * See the Unified Extensible Firmware Interface (UEFI) specification for
  104. * details.
  105. *
  106. * Return: status code
  107. */
  108. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  109. u16 *variable_name,
  110. efi_guid_t *vendor)
  111. {
  112. efi_status_t ret;
  113. EFI_ENTRY("%p \"%ls\" %pUs", variable_name_size, variable_name, vendor);
  114. ret = efi_get_next_variable_name_int(variable_name_size, variable_name,
  115. vendor);
  116. return EFI_EXIT(ret);
  117. }
  118. /**
  119. * efi_query_variable_info() - get information about EFI variables
  120. *
  121. * This function implements the QueryVariableInfo() runtime service.
  122. *
  123. * See the Unified Extensible Firmware Interface (UEFI) specification for
  124. * details.
  125. *
  126. * @attributes: bitmask to select variables to be
  127. * queried
  128. * @maximum_variable_storage_size: maximum size of storage area for the
  129. * selected variable types
  130. * @remaining_variable_storage_size: remaining size of storage are for the
  131. * selected variable types
  132. * @maximum_variable_size: maximum size of a variable of the
  133. * selected type
  134. * Returns: status code
  135. */
  136. efi_status_t EFIAPI efi_query_variable_info(
  137. u32 attributes, u64 *maximum_variable_storage_size,
  138. u64 *remaining_variable_storage_size,
  139. u64 *maximum_variable_size)
  140. {
  141. efi_status_t ret;
  142. EFI_ENTRY("%x %p %p %p", attributes, maximum_variable_storage_size,
  143. remaining_variable_storage_size, maximum_variable_size);
  144. if (!maximum_variable_storage_size ||
  145. !remaining_variable_storage_size ||
  146. !maximum_variable_size ||
  147. !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))
  148. return EFI_EXIT(EFI_INVALID_PARAMETER);
  149. if ((attributes & ~(u32)EFI_VARIABLE_MASK) ||
  150. (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) ||
  151. (attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) ||
  152. (!IS_ENABLED(CONFIG_EFI_SECURE_BOOT) &&
  153. (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)))
  154. return EFI_EXIT(EFI_UNSUPPORTED);
  155. ret = efi_query_variable_info_int(attributes,
  156. maximum_variable_storage_size,
  157. remaining_variable_storage_size,
  158. maximum_variable_size);
  159. return EFI_EXIT(ret);
  160. }
  161. efi_status_t __efi_runtime EFIAPI
  162. efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
  163. u32 *attributes, efi_uintn_t *data_size, void *data)
  164. {
  165. efi_status_t ret;
  166. ret = efi_get_variable_mem(variable_name, guid, attributes, data_size, data, NULL);
  167. /* Remove EFI_VARIABLE_READ_ONLY flag */
  168. if (attributes)
  169. *attributes &= EFI_VARIABLE_MASK;
  170. return ret;
  171. }
  172. efi_status_t __efi_runtime EFIAPI
  173. efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
  174. u16 *variable_name, efi_guid_t *guid)
  175. {
  176. return efi_get_next_variable_name_mem(variable_name_size, variable_name, guid);
  177. }
  178. /**
  179. * efi_set_secure_state - modify secure boot state variables
  180. * @secure_boot: value of SecureBoot
  181. * @setup_mode: value of SetupMode
  182. * @audit_mode: value of AuditMode
  183. * @deployed_mode: value of DeployedMode
  184. *
  185. * Modify secure boot status related variables as indicated.
  186. *
  187. * Return: status code
  188. */
  189. static efi_status_t efi_set_secure_state(u8 secure_boot, u8 setup_mode,
  190. u8 audit_mode, u8 deployed_mode)
  191. {
  192. efi_status_t ret;
  193. const u32 attributes_ro = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  194. EFI_VARIABLE_RUNTIME_ACCESS |
  195. EFI_VARIABLE_READ_ONLY;
  196. const u32 attributes_rw = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  197. EFI_VARIABLE_RUNTIME_ACCESS;
  198. efi_secure_boot = secure_boot;
  199. ret = efi_set_variable_int(L"SecureBoot", &efi_global_variable_guid,
  200. attributes_ro, sizeof(secure_boot),
  201. &secure_boot, false);
  202. if (ret != EFI_SUCCESS)
  203. goto err;
  204. ret = efi_set_variable_int(L"SetupMode", &efi_global_variable_guid,
  205. attributes_ro, sizeof(setup_mode),
  206. &setup_mode, false);
  207. if (ret != EFI_SUCCESS)
  208. goto err;
  209. ret = efi_set_variable_int(L"AuditMode", &efi_global_variable_guid,
  210. audit_mode || setup_mode ?
  211. attributes_ro : attributes_rw,
  212. sizeof(audit_mode), &audit_mode, false);
  213. if (ret != EFI_SUCCESS)
  214. goto err;
  215. ret = efi_set_variable_int(L"DeployedMode",
  216. &efi_global_variable_guid,
  217. audit_mode || deployed_mode || setup_mode ?
  218. attributes_ro : attributes_rw,
  219. sizeof(deployed_mode), &deployed_mode,
  220. false);
  221. err:
  222. return ret;
  223. }
  224. /**
  225. * efi_transfer_secure_state - handle a secure boot state transition
  226. * @mode: new state
  227. *
  228. * Depending on @mode, secure boot related variables are updated.
  229. * Those variables are *read-only* for users, efi_set_variable_int()
  230. * is called here.
  231. *
  232. * Return: status code
  233. */
  234. static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
  235. {
  236. efi_status_t ret;
  237. EFI_PRINT("Switching secure state from %d to %d\n", efi_secure_mode,
  238. mode);
  239. if (mode == EFI_MODE_DEPLOYED) {
  240. ret = efi_set_secure_state(1, 0, 0, 1);
  241. if (ret != EFI_SUCCESS)
  242. goto err;
  243. } else if (mode == EFI_MODE_AUDIT) {
  244. ret = efi_set_variable_int(L"PK", &efi_global_variable_guid,
  245. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  246. EFI_VARIABLE_RUNTIME_ACCESS,
  247. 0, NULL, false);
  248. if (ret != EFI_SUCCESS)
  249. goto err;
  250. ret = efi_set_secure_state(0, 1, 1, 0);
  251. if (ret != EFI_SUCCESS)
  252. goto err;
  253. } else if (mode == EFI_MODE_USER) {
  254. ret = efi_set_secure_state(1, 0, 0, 0);
  255. if (ret != EFI_SUCCESS)
  256. goto err;
  257. } else if (mode == EFI_MODE_SETUP) {
  258. ret = efi_set_secure_state(0, 1, 0, 0);
  259. if (ret != EFI_SUCCESS)
  260. goto err;
  261. } else {
  262. return EFI_INVALID_PARAMETER;
  263. }
  264. efi_secure_mode = mode;
  265. return EFI_SUCCESS;
  266. err:
  267. /* TODO: What action should be taken here? */
  268. printf("ERROR: Secure state transition failed\n");
  269. return ret;
  270. }
  271. efi_status_t efi_init_secure_state(void)
  272. {
  273. enum efi_secure_mode mode;
  274. u8 efi_vendor_keys = 0;
  275. efi_uintn_t size;
  276. efi_status_t ret;
  277. u8 deployed_mode = 0;
  278. u8 audit_mode = 0;
  279. u8 setup_mode = 1;
  280. if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
  281. size = sizeof(deployed_mode);
  282. ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
  283. NULL, &size, &deployed_mode, NULL);
  284. size = sizeof(audit_mode);
  285. ret = efi_get_variable_int(u"AuditMode", &efi_global_variable_guid,
  286. NULL, &size, &audit_mode, NULL);
  287. size = 0;
  288. ret = efi_get_variable_int(u"PK", &efi_global_variable_guid,
  289. NULL, &size, NULL, NULL);
  290. if (ret == EFI_BUFFER_TOO_SMALL) {
  291. setup_mode = 0;
  292. audit_mode = 0;
  293. } else {
  294. setup_mode = 1;
  295. deployed_mode = 0;
  296. }
  297. }
  298. if (deployed_mode)
  299. mode = EFI_MODE_DEPLOYED;
  300. else if (audit_mode)
  301. mode = EFI_MODE_AUDIT;
  302. else if (setup_mode)
  303. mode = EFI_MODE_SETUP;
  304. else
  305. mode = EFI_MODE_USER;
  306. ret = efi_transfer_secure_state(mode);
  307. if (ret != EFI_SUCCESS)
  308. return ret;
  309. /* As we do not provide vendor keys this variable is always 0. */
  310. ret = efi_set_variable_int(L"VendorKeys",
  311. &efi_global_variable_guid,
  312. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  313. EFI_VARIABLE_RUNTIME_ACCESS |
  314. EFI_VARIABLE_READ_ONLY,
  315. sizeof(efi_vendor_keys),
  316. &efi_vendor_keys, false);
  317. return ret;
  318. }
  319. /**
  320. * efi_secure_boot_enabled - return if secure boot is enabled or not
  321. *
  322. * Return: true if enabled, false if disabled
  323. */
  324. bool efi_secure_boot_enabled(void)
  325. {
  326. return efi_secure_boot;
  327. }
  328. enum efi_auth_var_type efi_auth_var_get_type(const u16 *name,
  329. const efi_guid_t *guid)
  330. {
  331. for (size_t i = 0; i < ARRAY_SIZE(name_type); ++i) {
  332. if (!u16_strcmp(name, name_type[i].name) &&
  333. !guidcmp(guid, name_type[i].guid))
  334. return name_type[i].type;
  335. }
  336. return EFI_AUTH_VAR_NONE;
  337. }
  338. const efi_guid_t *efi_auth_var_get_guid(const u16 *name)
  339. {
  340. for (size_t i = 0; i < ARRAY_SIZE(name_type); ++i) {
  341. if (!u16_strcmp(name, name_type[i].name))
  342. return name_type[i].guid;
  343. }
  344. return &efi_global_variable_guid;
  345. }
  346. /**
  347. * efi_get_var() - read value of an EFI variable
  348. *
  349. * @name: variable name
  350. * @start: vendor GUID
  351. * @size: size of allocated buffer
  352. *
  353. * Return: buffer with variable data or NULL
  354. */
  355. void *efi_get_var(const u16 *name, const efi_guid_t *vendor, efi_uintn_t *size)
  356. {
  357. efi_status_t ret;
  358. void *buf = NULL;
  359. *size = 0;
  360. ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
  361. if (ret == EFI_BUFFER_TOO_SMALL) {
  362. buf = malloc(*size);
  363. if (!buf)
  364. return NULL;
  365. ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
  366. }
  367. if (ret != EFI_SUCCESS) {
  368. free(buf);
  369. *size = 0;
  370. return NULL;
  371. }
  372. return buf;
  373. }