blacklist.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* System hash blacklist.
  3. *
  4. * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) "blacklist: "fmt
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/key.h>
  11. #include <linux/key-type.h>
  12. #include <linux/sched.h>
  13. #include <linux/ctype.h>
  14. #include <linux/err.h>
  15. #include <linux/seq_file.h>
  16. #include <keys/system_keyring.h>
  17. #include "blacklist.h"
  18. #include "common.h"
  19. static struct key *blacklist_keyring;
  20. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  21. extern __initconst const u8 revocation_certificate_list[];
  22. extern __initconst const unsigned long revocation_certificate_list_size;
  23. #endif
  24. /*
  25. * The description must be a type prefix, a colon and then an even number of
  26. * hex digits. The hash is kept in the description.
  27. */
  28. static int blacklist_vet_description(const char *desc)
  29. {
  30. int n = 0;
  31. if (*desc == ':')
  32. return -EINVAL;
  33. for (; *desc; desc++)
  34. if (*desc == ':')
  35. goto found_colon;
  36. return -EINVAL;
  37. found_colon:
  38. desc++;
  39. for (; *desc; desc++) {
  40. if (!isxdigit(*desc))
  41. return -EINVAL;
  42. n++;
  43. }
  44. if (n == 0 || n & 1)
  45. return -EINVAL;
  46. return 0;
  47. }
  48. /*
  49. * The hash to be blacklisted is expected to be in the description. There will
  50. * be no payload.
  51. */
  52. static int blacklist_preparse(struct key_preparsed_payload *prep)
  53. {
  54. if (prep->datalen > 0)
  55. return -EINVAL;
  56. return 0;
  57. }
  58. static void blacklist_free_preparse(struct key_preparsed_payload *prep)
  59. {
  60. }
  61. static void blacklist_describe(const struct key *key, struct seq_file *m)
  62. {
  63. seq_puts(m, key->description);
  64. }
  65. static struct key_type key_type_blacklist = {
  66. .name = "blacklist",
  67. .vet_description = blacklist_vet_description,
  68. .preparse = blacklist_preparse,
  69. .free_preparse = blacklist_free_preparse,
  70. .instantiate = generic_key_instantiate,
  71. .describe = blacklist_describe,
  72. };
  73. /**
  74. * mark_hash_blacklisted - Add a hash to the system blacklist
  75. * @hash - The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
  76. */
  77. int mark_hash_blacklisted(const char *hash)
  78. {
  79. key_ref_t key;
  80. key = key_create_or_update(make_key_ref(blacklist_keyring, true),
  81. "blacklist",
  82. hash,
  83. NULL,
  84. 0,
  85. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  86. KEY_USR_VIEW),
  87. KEY_ALLOC_NOT_IN_QUOTA |
  88. KEY_ALLOC_BUILT_IN);
  89. if (IS_ERR(key)) {
  90. pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
  91. return PTR_ERR(key);
  92. }
  93. return 0;
  94. }
  95. /**
  96. * is_hash_blacklisted - Determine if a hash is blacklisted
  97. * @hash: The hash to be checked as a binary blob
  98. * @hash_len: The length of the binary hash
  99. * @type: Type of hash
  100. */
  101. int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
  102. {
  103. key_ref_t kref;
  104. size_t type_len = strlen(type);
  105. char *buffer, *p;
  106. int ret = 0;
  107. buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
  108. if (!buffer)
  109. return -ENOMEM;
  110. p = memcpy(buffer, type, type_len);
  111. p += type_len;
  112. *p++ = ':';
  113. bin2hex(p, hash, hash_len);
  114. p += hash_len * 2;
  115. *p = 0;
  116. kref = keyring_search(make_key_ref(blacklist_keyring, true),
  117. &key_type_blacklist, buffer, false);
  118. if (!IS_ERR(kref)) {
  119. key_ref_put(kref);
  120. ret = -EKEYREJECTED;
  121. }
  122. kfree(buffer);
  123. return ret;
  124. }
  125. EXPORT_SYMBOL_GPL(is_hash_blacklisted);
  126. int is_binary_blacklisted(const u8 *hash, size_t hash_len)
  127. {
  128. if (is_hash_blacklisted(hash, hash_len, "bin") == -EKEYREJECTED)
  129. return -EPERM;
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(is_binary_blacklisted);
  133. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  134. /**
  135. * add_key_to_revocation_list - Add a revocation certificate to the blacklist
  136. * @data: The data blob containing the certificate
  137. * @size: The size of data blob
  138. */
  139. int add_key_to_revocation_list(const char *data, size_t size)
  140. {
  141. key_ref_t key;
  142. key = key_create_or_update(make_key_ref(blacklist_keyring, true),
  143. "asymmetric",
  144. NULL,
  145. data,
  146. size,
  147. ((KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW),
  148. KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN);
  149. if (IS_ERR(key)) {
  150. pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
  151. return PTR_ERR(key);
  152. }
  153. return 0;
  154. }
  155. /**
  156. * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
  157. * @pkcs7: The PKCS#7 message to check
  158. */
  159. int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
  160. {
  161. int ret;
  162. ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
  163. if (ret == 0)
  164. return -EKEYREJECTED;
  165. return -ENOKEY;
  166. }
  167. #endif
  168. /*
  169. * Initialise the blacklist
  170. */
  171. static int __init blacklist_init(void)
  172. {
  173. const char *const *bl;
  174. if (register_key_type(&key_type_blacklist) < 0)
  175. panic("Can't allocate system blacklist key type\n");
  176. blacklist_keyring =
  177. keyring_alloc(".blacklist",
  178. KUIDT_INIT(0), KGIDT_INIT(0),
  179. current_cred(),
  180. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  181. KEY_USR_VIEW | KEY_USR_READ |
  182. KEY_USR_SEARCH,
  183. KEY_ALLOC_NOT_IN_QUOTA |
  184. KEY_ALLOC_SET_KEEP,
  185. NULL, NULL);
  186. if (IS_ERR(blacklist_keyring))
  187. panic("Can't allocate system blacklist keyring\n");
  188. for (bl = blacklist_hashes; *bl; bl++)
  189. if (mark_hash_blacklisted(*bl) < 0)
  190. pr_err("- blacklisting failed\n");
  191. return 0;
  192. }
  193. /*
  194. * Must be initialised before we try and load the keys into the keyring.
  195. */
  196. device_initcall(blacklist_init);
  197. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  198. /*
  199. * Load the compiled-in list of revocation X.509 certificates.
  200. */
  201. static __init int load_revocation_certificate_list(void)
  202. {
  203. if (revocation_certificate_list_size)
  204. pr_notice("Loading compiled-in revocation X.509 certificates\n");
  205. return load_certificate_list(revocation_certificate_list, revocation_certificate_list_size,
  206. blacklist_keyring);
  207. }
  208. late_initcall(load_revocation_certificate_list);
  209. #endif