blacklist.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/hex.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/uidgid.h>
  18. #include <keys/asymmetric-type.h>
  19. #include <keys/system_keyring.h>
  20. #include "blacklist.h"
  21. /*
  22. * According to crypto/asymmetric_keys/x509_cert_parser.c:x509_note_pkey_algo(),
  23. * the size of the currently longest supported hash algorithm is 512 bits,
  24. * which translates into 128 hex characters.
  25. */
  26. #define MAX_HASH_LEN 128
  27. #define BLACKLIST_KEY_PERM (KEY_POS_SEARCH | KEY_POS_VIEW | \
  28. KEY_USR_SEARCH | KEY_USR_VIEW)
  29. static const char tbs_prefix[] = "tbs";
  30. static const char bin_prefix[] = "bin";
  31. static struct key *blacklist_keyring;
  32. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  33. extern __initconst const u8 revocation_certificate_list[];
  34. extern __initconst const unsigned long revocation_certificate_list_size;
  35. #endif
  36. /*
  37. * The description must be a type prefix, a colon and then an even number of
  38. * hex digits. The hash is kept in the description.
  39. */
  40. static int blacklist_vet_description(const char *desc)
  41. {
  42. int i, prefix_len, tbs_step = 0, bin_step = 0;
  43. /* The following algorithm only works if prefix lengths match. */
  44. BUILD_BUG_ON(sizeof(tbs_prefix) != sizeof(bin_prefix));
  45. prefix_len = sizeof(tbs_prefix) - 1;
  46. for (i = 0; *desc; desc++, i++) {
  47. if (*desc == ':') {
  48. if (tbs_step == prefix_len)
  49. goto found_colon;
  50. if (bin_step == prefix_len)
  51. goto found_colon;
  52. return -EINVAL;
  53. }
  54. if (i >= prefix_len)
  55. return -EINVAL;
  56. if (*desc == tbs_prefix[i])
  57. tbs_step++;
  58. if (*desc == bin_prefix[i])
  59. bin_step++;
  60. }
  61. return -EINVAL;
  62. found_colon:
  63. desc++;
  64. for (i = 0; *desc && i < MAX_HASH_LEN; desc++, i++) {
  65. if (!isxdigit(*desc) || isupper(*desc))
  66. return -EINVAL;
  67. }
  68. if (*desc)
  69. /* The hash is greater than MAX_HASH_LEN. */
  70. return -ENOPKG;
  71. /* Checks for an even number of hexadecimal characters. */
  72. if (i == 0 || i & 1)
  73. return -EINVAL;
  74. return 0;
  75. }
  76. static int blacklist_key_instantiate(struct key *key,
  77. struct key_preparsed_payload *prep)
  78. {
  79. #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
  80. int err;
  81. #endif
  82. /* Sets safe default permissions for keys loaded by user space. */
  83. key->perm = BLACKLIST_KEY_PERM;
  84. /*
  85. * Skips the authentication step for builtin hashes, they are not
  86. * signed but still trusted.
  87. */
  88. if (key->flags & (1 << KEY_FLAG_BUILTIN))
  89. goto out;
  90. #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
  91. /*
  92. * Verifies the description's PKCS#7 signature against the builtin
  93. * trusted keyring.
  94. */
  95. err = verify_pkcs7_signature(key->description,
  96. strlen(key->description), prep->data, prep->datalen,
  97. NULL, VERIFYING_UNSPECIFIED_SIGNATURE, NULL, NULL);
  98. if (err)
  99. return err;
  100. #else
  101. /*
  102. * It should not be possible to come here because the keyring doesn't
  103. * have KEY_USR_WRITE and the only other way to call this function is
  104. * for builtin hashes.
  105. */
  106. WARN_ON_ONCE(1);
  107. return -EPERM;
  108. #endif
  109. out:
  110. return generic_key_instantiate(key, prep);
  111. }
  112. static int blacklist_key_update(struct key *key,
  113. struct key_preparsed_payload *prep)
  114. {
  115. return -EPERM;
  116. }
  117. static void blacklist_describe(const struct key *key, struct seq_file *m)
  118. {
  119. seq_puts(m, key->description);
  120. }
  121. static struct key_type key_type_blacklist = {
  122. .name = "blacklist",
  123. .vet_description = blacklist_vet_description,
  124. .instantiate = blacklist_key_instantiate,
  125. .update = blacklist_key_update,
  126. .describe = blacklist_describe,
  127. };
  128. static char *get_raw_hash(const u8 *hash, size_t hash_len,
  129. enum blacklist_hash_type hash_type)
  130. {
  131. size_t type_len;
  132. const char *type_prefix;
  133. char *buffer, *p;
  134. switch (hash_type) {
  135. case BLACKLIST_HASH_X509_TBS:
  136. type_len = sizeof(tbs_prefix) - 1;
  137. type_prefix = tbs_prefix;
  138. break;
  139. case BLACKLIST_HASH_BINARY:
  140. type_len = sizeof(bin_prefix) - 1;
  141. type_prefix = bin_prefix;
  142. break;
  143. default:
  144. WARN_ON_ONCE(1);
  145. return ERR_PTR(-EINVAL);
  146. }
  147. buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
  148. if (!buffer)
  149. return ERR_PTR(-ENOMEM);
  150. p = memcpy(buffer, type_prefix, type_len);
  151. p += type_len;
  152. *p++ = ':';
  153. bin2hex(p, hash, hash_len);
  154. p += hash_len * 2;
  155. *p = '\0';
  156. return buffer;
  157. }
  158. /**
  159. * mark_raw_hash_blacklisted - Add a hash to the system blacklist
  160. * @hash: The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
  161. */
  162. static int mark_raw_hash_blacklisted(const char *hash)
  163. {
  164. key_ref_t key;
  165. key = key_create(make_key_ref(blacklist_keyring, true),
  166. "blacklist",
  167. hash,
  168. NULL,
  169. 0,
  170. BLACKLIST_KEY_PERM,
  171. KEY_ALLOC_NOT_IN_QUOTA |
  172. KEY_ALLOC_BUILT_IN);
  173. if (IS_ERR(key)) {
  174. if (PTR_ERR(key) == -EEXIST)
  175. pr_warn("Duplicate blacklisted hash %s\n", hash);
  176. else
  177. pr_err("Problem blacklisting hash %s: %pe\n", hash, key);
  178. return PTR_ERR(key);
  179. }
  180. return 0;
  181. }
  182. int mark_hash_blacklisted(const u8 *hash, size_t hash_len,
  183. enum blacklist_hash_type hash_type)
  184. {
  185. const char *buffer;
  186. int err;
  187. buffer = get_raw_hash(hash, hash_len, hash_type);
  188. if (IS_ERR(buffer))
  189. return PTR_ERR(buffer);
  190. err = mark_raw_hash_blacklisted(buffer);
  191. kfree(buffer);
  192. return err;
  193. }
  194. /**
  195. * is_hash_blacklisted - Determine if a hash is blacklisted
  196. * @hash: The hash to be checked as a binary blob
  197. * @hash_len: The length of the binary hash
  198. * @hash_type: Type of hash
  199. */
  200. int is_hash_blacklisted(const u8 *hash, size_t hash_len,
  201. enum blacklist_hash_type hash_type)
  202. {
  203. key_ref_t kref;
  204. const char *buffer;
  205. int ret = 0;
  206. buffer = get_raw_hash(hash, hash_len, hash_type);
  207. if (IS_ERR(buffer))
  208. return PTR_ERR(buffer);
  209. kref = keyring_search(make_key_ref(blacklist_keyring, true),
  210. &key_type_blacklist, buffer, false);
  211. if (!IS_ERR(kref)) {
  212. key_ref_put(kref);
  213. ret = -EKEYREJECTED;
  214. }
  215. kfree(buffer);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL_GPL(is_hash_blacklisted);
  219. int is_binary_blacklisted(const u8 *hash, size_t hash_len)
  220. {
  221. if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) ==
  222. -EKEYREJECTED)
  223. return -EPERM;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(is_binary_blacklisted);
  227. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  228. /**
  229. * add_key_to_revocation_list - Add a revocation certificate to the blacklist
  230. * @data: The data blob containing the certificate
  231. * @size: The size of data blob
  232. */
  233. int add_key_to_revocation_list(const char *data, size_t size)
  234. {
  235. key_ref_t key;
  236. key = key_create_or_update(make_key_ref(blacklist_keyring, true),
  237. "asymmetric",
  238. NULL,
  239. data,
  240. size,
  241. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
  242. | KEY_USR_VIEW,
  243. KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN
  244. | KEY_ALLOC_BYPASS_RESTRICTION);
  245. if (IS_ERR(key)) {
  246. pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
  247. return PTR_ERR(key);
  248. }
  249. return 0;
  250. }
  251. /**
  252. * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
  253. * @pkcs7: The PKCS#7 message to check
  254. */
  255. int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
  256. {
  257. int ret;
  258. ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
  259. if (ret == 0)
  260. return -EKEYREJECTED;
  261. return -ENOKEY;
  262. }
  263. #endif
  264. static int restrict_link_for_blacklist(struct key *dest_keyring,
  265. const struct key_type *type, const union key_payload *payload,
  266. struct key *restrict_key)
  267. {
  268. if (type == &key_type_blacklist)
  269. return 0;
  270. return -EOPNOTSUPP;
  271. }
  272. /*
  273. * Initialise the blacklist
  274. *
  275. * The blacklist_init() function is registered as an initcall via
  276. * device_initcall(). As a result if the blacklist_init() function fails for
  277. * any reason the kernel continues to execute. While cleanly returning -ENODEV
  278. * could be acceptable for some non-critical kernel parts, if the blacklist
  279. * keyring fails to load it defeats the certificate/key based deny list for
  280. * signed modules. If a critical piece of security functionality that users
  281. * expect to be present fails to initialize, panic()ing is likely the right
  282. * thing to do.
  283. */
  284. static int __init blacklist_init(void)
  285. {
  286. const char *const *bl;
  287. struct key_restriction *restriction;
  288. if (register_key_type(&key_type_blacklist) < 0)
  289. panic("Can't allocate system blacklist key type\n");
  290. restriction = kzalloc_obj(*restriction);
  291. if (!restriction)
  292. panic("Can't allocate blacklist keyring restriction\n");
  293. restriction->check = restrict_link_for_blacklist;
  294. blacklist_keyring =
  295. keyring_alloc(".blacklist",
  296. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
  297. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
  298. KEY_POS_WRITE |
  299. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH
  300. #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
  301. | KEY_USR_WRITE
  302. #endif
  303. , KEY_ALLOC_NOT_IN_QUOTA |
  304. KEY_ALLOC_SET_KEEP,
  305. restriction, NULL);
  306. if (IS_ERR(blacklist_keyring))
  307. panic("Can't allocate system blacklist keyring\n");
  308. for (bl = blacklist_hashes; *bl; bl++)
  309. if (mark_raw_hash_blacklisted(*bl) < 0)
  310. pr_err("- blacklisting failed\n");
  311. return 0;
  312. }
  313. /*
  314. * Must be initialised before we try and load the keys into the keyring.
  315. */
  316. device_initcall(blacklist_init);
  317. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  318. /*
  319. * Load the compiled-in list of revocation X.509 certificates.
  320. */
  321. static __init int load_revocation_certificate_list(void)
  322. {
  323. if (revocation_certificate_list_size)
  324. pr_notice("Loading compiled-in revocation X.509 certificates\n");
  325. return x509_load_certificate_list(revocation_certificate_list,
  326. revocation_certificate_list_size,
  327. blacklist_keyring);
  328. }
  329. late_initcall(load_revocation_certificate_list);
  330. #endif