load_uefi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <linux/kernel.h>
  2. #include <linux/sched.h>
  3. #include <linux/cred.h>
  4. #include <linux/err.h>
  5. #include <linux/efi.h>
  6. #include <linux/slab.h>
  7. #include <keys/asymmetric-type.h>
  8. #include <keys/system_keyring.h>
  9. #include "internal.h"
  10. static __initdata efi_guid_t efi_cert_x509_guid = EFI_CERT_X509_GUID;
  11. static __initdata efi_guid_t efi_cert_x509_sha256_guid = EFI_CERT_X509_SHA256_GUID;
  12. static __initdata efi_guid_t efi_cert_sha256_guid = EFI_CERT_SHA256_GUID;
  13. /*
  14. * Look to see if a UEFI variable called MokIgnoreDB exists and return true if
  15. * it does.
  16. *
  17. * This UEFI variable is set by the shim if a user tells the shim to not use
  18. * the certs/hashes in the UEFI db variable for verification purposes. If it
  19. * is set, we should ignore the db variable also and the true return indicates
  20. * this.
  21. */
  22. static __init bool uefi_check_ignore_db(void)
  23. {
  24. efi_status_t status;
  25. unsigned int db = 0;
  26. unsigned long size = sizeof(db);
  27. efi_guid_t guid = EFI_SHIM_LOCK_GUID;
  28. status = efi.get_variable(L"MokIgnoreDB", &guid, NULL, &size, &db);
  29. return status == EFI_SUCCESS;
  30. }
  31. /*
  32. * Get a certificate list blob from the named EFI variable.
  33. */
  34. static __init int get_cert_list(efi_char16_t *name, efi_guid_t *guid,
  35. unsigned long *size, void **cert_list,
  36. u32 pos_attr, u32 neg_attr)
  37. {
  38. efi_status_t status;
  39. unsigned long lsize = 4;
  40. unsigned long tmpdb[4];
  41. void *db;
  42. u32 attr = 0;
  43. status = efi.get_variable(name, guid, NULL, &lsize, &tmpdb);
  44. if (status == EFI_NOT_FOUND) {
  45. *size = 0;
  46. *cert_list = NULL;
  47. return 0;
  48. }
  49. if (status != EFI_BUFFER_TOO_SMALL) {
  50. pr_err("Couldn't get size: 0x%lx\n", status);
  51. return efi_status_to_err(status);
  52. }
  53. db = kmalloc(lsize, GFP_KERNEL);
  54. if (!db) {
  55. pr_err("Couldn't allocate memory for uefi cert list\n");
  56. return -ENOMEM;
  57. }
  58. status = efi.get_variable(name, guid, &attr, &lsize, db);
  59. if (status != EFI_SUCCESS) {
  60. kfree(db);
  61. pr_err("Error reading db var: 0x%lx\n", status);
  62. return efi_status_to_err(status);
  63. }
  64. /* must have positive attributes and no negative attributes */
  65. if ((pos_attr && !(attr & pos_attr)) ||
  66. (neg_attr && (attr & neg_attr))) {
  67. kfree(db);
  68. pr_err("Error reading db var attributes: 0x%016x\n", attr);
  69. return -1;
  70. }
  71. *size = lsize;
  72. *cert_list = db;
  73. return 0;
  74. }
  75. /*
  76. * Blacklist an X509 TBS hash.
  77. */
  78. static __init void uefi_blacklist_x509_tbs(const char *source,
  79. const void *data, size_t len)
  80. {
  81. char *hash, *p;
  82. hash = kmalloc(4 + len * 2 + 1, GFP_KERNEL);
  83. if (!hash)
  84. return;
  85. p = memcpy(hash, "tbs:", 4);
  86. p += 4;
  87. bin2hex(p, data, len);
  88. p += len * 2;
  89. *p = 0;
  90. mark_hash_blacklisted(hash);
  91. kfree(hash);
  92. }
  93. /*
  94. * Blacklist the hash of an executable.
  95. */
  96. static __init void uefi_blacklist_binary(const char *source,
  97. const void *data, size_t len)
  98. {
  99. char *hash, *p;
  100. hash = kmalloc(4 + len * 2 + 1, GFP_KERNEL);
  101. if (!hash)
  102. return;
  103. p = memcpy(hash, "bin:", 4);
  104. p += 4;
  105. bin2hex(p, data, len);
  106. p += len * 2;
  107. *p = 0;
  108. mark_hash_blacklisted(hash);
  109. kfree(hash);
  110. }
  111. /*
  112. * Return the appropriate handler for particular signature list types found in
  113. * the UEFI db and MokListRT tables.
  114. */
  115. static __init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
  116. {
  117. if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
  118. return add_trusted_secondary_key;
  119. return 0;
  120. }
  121. /*
  122. * Return the appropriate handler for particular signature list types found in
  123. * the UEFI dbx and MokListXRT tables.
  124. */
  125. static __init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
  126. {
  127. if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
  128. return uefi_blacklist_x509_tbs;
  129. if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
  130. return uefi_blacklist_binary;
  131. return 0;
  132. }
  133. /*
  134. * Load the certs contained in the UEFI databases into the secondary trusted
  135. * keyring and the UEFI blacklisted X.509 cert SHA256 hashes into the blacklist
  136. * keyring.
  137. */
  138. static int __init load_uefi_certs(void)
  139. {
  140. efi_guid_t secure_var = EFI_IMAGE_SECURITY_DATABASE_GUID;
  141. efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
  142. void *db = NULL, *dbx = NULL, *mok = NULL, *mokx = NULL;
  143. unsigned long dbsize = 0, dbxsize = 0, moksize = 0, mokxsize = 0;
  144. int rc = 0;
  145. if (!efi.get_variable)
  146. return false;
  147. /* Get db, MokListRT, and dbx. They might not exist, so it isn't
  148. * an error if we can't get them.
  149. */
  150. if (!uefi_check_ignore_db()) {
  151. rc = get_cert_list(L"db", &secure_var, &dbsize, &db,
  152. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, 0);
  153. if (rc < 0) {
  154. pr_err("MODSIGN: Couldn't get UEFI db list\n");
  155. } else if (dbsize != 0) {
  156. rc = parse_efi_signature_list("UEFI:db",
  157. db, dbsize, get_handler_for_db);
  158. if (rc)
  159. pr_err("Couldn't parse db signatures: %d\n", rc);
  160. kfree(db);
  161. }
  162. }
  163. rc = get_cert_list(L"dbx", &secure_var, &dbxsize, &dbx,
  164. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, 0);
  165. if (rc < 0) {
  166. pr_info("MODSIGN: Couldn't get UEFI dbx list\n");
  167. } else if (dbxsize != 0) {
  168. rc = parse_efi_signature_list("UEFI:dbx",
  169. dbx, dbxsize,
  170. get_handler_for_dbx);
  171. if (rc)
  172. pr_err("Couldn't parse dbx signatures: %d\n", rc);
  173. kfree(dbx);
  174. }
  175. /* the MOK and MOKx can not be trusted when secure boot is disabled */
  176. if (!efi_enabled(EFI_SECURE_BOOT))
  177. return 0;
  178. rc = get_cert_list(L"MokListRT", &mok_var, &moksize, &mok,
  179. 0, EFI_VARIABLE_NON_VOLATILE);
  180. if (rc < 0) {
  181. pr_info("MODSIGN: Couldn't get UEFI MokListRT\n");
  182. } else if (moksize != 0) {
  183. rc = parse_efi_signature_list("UEFI:MokListRT",
  184. mok, moksize, get_handler_for_db);
  185. if (rc)
  186. pr_err("Couldn't parse MokListRT signatures: %d\n", rc);
  187. kfree(mok);
  188. }
  189. rc = get_cert_list(L"MokListXRT", &mok_var, &mokxsize, &mokx,
  190. 0, EFI_VARIABLE_NON_VOLATILE);
  191. if (rc < 0) {
  192. pr_info("MODSIGN: Couldn't get UEFI MokListXRT\n");
  193. } else if (mokxsize != 0) {
  194. rc = parse_efi_signature_list("UEFI:mokx",
  195. mokx, mokxsize,
  196. get_handler_for_dbx);
  197. if (rc)
  198. pr_err("Couldn't parse MokListXRT signatures: %d\n", rc);
  199. kfree(mokx);
  200. }
  201. return rc;
  202. }
  203. late_initcall(load_uefi_certs);