blacklist.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 ([email protected])
  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_or_update(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. pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
  174. return PTR_ERR(key);
  175. }
  176. return 0;
  177. }
  178. int mark_hash_blacklisted(const u8 *hash, size_t hash_len,
  179. enum blacklist_hash_type hash_type)
  180. {
  181. const char *buffer;
  182. int err;
  183. buffer = get_raw_hash(hash, hash_len, hash_type);
  184. if (IS_ERR(buffer))
  185. return PTR_ERR(buffer);
  186. err = mark_raw_hash_blacklisted(buffer);
  187. kfree(buffer);
  188. return err;
  189. }
  190. /**
  191. * is_hash_blacklisted - Determine if a hash is blacklisted
  192. * @hash: The hash to be checked as a binary blob
  193. * @hash_len: The length of the binary hash
  194. * @hash_type: Type of hash
  195. */
  196. int is_hash_blacklisted(const u8 *hash, size_t hash_len,
  197. enum blacklist_hash_type hash_type)
  198. {
  199. key_ref_t kref;
  200. const char *buffer;
  201. int ret = 0;
  202. buffer = get_raw_hash(hash, hash_len, hash_type);
  203. if (IS_ERR(buffer))
  204. return PTR_ERR(buffer);
  205. kref = keyring_search(make_key_ref(blacklist_keyring, true),
  206. &key_type_blacklist, buffer, false);
  207. if (!IS_ERR(kref)) {
  208. key_ref_put(kref);
  209. ret = -EKEYREJECTED;
  210. }
  211. kfree(buffer);
  212. return ret;
  213. }
  214. EXPORT_SYMBOL_GPL(is_hash_blacklisted);
  215. int is_binary_blacklisted(const u8 *hash, size_t hash_len)
  216. {
  217. if (is_hash_blacklisted(hash, hash_len, BLACKLIST_HASH_BINARY) ==
  218. -EKEYREJECTED)
  219. return -EPERM;
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(is_binary_blacklisted);
  223. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  224. /**
  225. * add_key_to_revocation_list - Add a revocation certificate to the blacklist
  226. * @data: The data blob containing the certificate
  227. * @size: The size of data blob
  228. */
  229. int add_key_to_revocation_list(const char *data, size_t size)
  230. {
  231. key_ref_t key;
  232. key = key_create_or_update(make_key_ref(blacklist_keyring, true),
  233. "asymmetric",
  234. NULL,
  235. data,
  236. size,
  237. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH
  238. | KEY_USR_VIEW,
  239. KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN
  240. | KEY_ALLOC_BYPASS_RESTRICTION);
  241. if (IS_ERR(key)) {
  242. pr_err("Problem with revocation key (%ld)\n", PTR_ERR(key));
  243. return PTR_ERR(key);
  244. }
  245. return 0;
  246. }
  247. /**
  248. * is_key_on_revocation_list - Determine if the key for a PKCS#7 message is revoked
  249. * @pkcs7: The PKCS#7 message to check
  250. */
  251. int is_key_on_revocation_list(struct pkcs7_message *pkcs7)
  252. {
  253. int ret;
  254. ret = pkcs7_validate_trust(pkcs7, blacklist_keyring);
  255. if (ret == 0)
  256. return -EKEYREJECTED;
  257. return -ENOKEY;
  258. }
  259. #endif
  260. static int restrict_link_for_blacklist(struct key *dest_keyring,
  261. const struct key_type *type, const union key_payload *payload,
  262. struct key *restrict_key)
  263. {
  264. if (type == &key_type_blacklist)
  265. return 0;
  266. return -EOPNOTSUPP;
  267. }
  268. /*
  269. * Initialise the blacklist
  270. *
  271. * The blacklist_init() function is registered as an initcall via
  272. * device_initcall(). As a result if the blacklist_init() function fails for
  273. * any reason the kernel continues to execute. While cleanly returning -ENODEV
  274. * could be acceptable for some non-critical kernel parts, if the blacklist
  275. * keyring fails to load it defeats the certificate/key based deny list for
  276. * signed modules. If a critical piece of security functionality that users
  277. * expect to be present fails to initialize, panic()ing is likely the right
  278. * thing to do.
  279. */
  280. static int __init blacklist_init(void)
  281. {
  282. const char *const *bl;
  283. struct key_restriction *restriction;
  284. if (register_key_type(&key_type_blacklist) < 0)
  285. panic("Can't allocate system blacklist key type\n");
  286. restriction = kzalloc(sizeof(*restriction), GFP_KERNEL);
  287. if (!restriction)
  288. panic("Can't allocate blacklist keyring restriction\n");
  289. restriction->check = restrict_link_for_blacklist;
  290. blacklist_keyring =
  291. keyring_alloc(".blacklist",
  292. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
  293. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
  294. KEY_POS_WRITE |
  295. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH
  296. #ifdef CONFIG_SYSTEM_BLACKLIST_AUTH_UPDATE
  297. | KEY_USR_WRITE
  298. #endif
  299. , KEY_ALLOC_NOT_IN_QUOTA |
  300. KEY_ALLOC_SET_KEEP,
  301. restriction, NULL);
  302. if (IS_ERR(blacklist_keyring))
  303. panic("Can't allocate system blacklist keyring\n");
  304. for (bl = blacklist_hashes; *bl; bl++)
  305. if (mark_raw_hash_blacklisted(*bl) < 0)
  306. pr_err("- blacklisting failed\n");
  307. return 0;
  308. }
  309. /*
  310. * Must be initialised before we try and load the keys into the keyring.
  311. */
  312. device_initcall(blacklist_init);
  313. #ifdef CONFIG_SYSTEM_REVOCATION_LIST
  314. /*
  315. * Load the compiled-in list of revocation X.509 certificates.
  316. */
  317. static __init int load_revocation_certificate_list(void)
  318. {
  319. if (revocation_certificate_list_size)
  320. pr_notice("Loading compiled-in revocation X.509 certificates\n");
  321. return x509_load_certificate_list(revocation_certificate_list,
  322. revocation_certificate_list_size,
  323. blacklist_keyring);
  324. }
  325. late_initcall(load_revocation_certificate_list);
  326. #endif