blacklist.c 9.5 KB

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