blacklist.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* System hash blacklist.
  2. *
  3. * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "blacklist: "fmt
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/key.h>
  15. #include <linux/key-type.h>
  16. #include <linux/sched.h>
  17. #include <linux/ctype.h>
  18. #include <linux/err.h>
  19. #include <linux/seq_file.h>
  20. #include <keys/system_keyring.h>
  21. #include "blacklist.h"
  22. static struct key *blacklist_keyring;
  23. /*
  24. * The description must be a type prefix, a colon and then an even number of
  25. * hex digits. The hash is kept in the description.
  26. */
  27. static int blacklist_vet_description(const char *desc)
  28. {
  29. int n = 0;
  30. if (*desc == ':')
  31. return -EINVAL;
  32. for (; *desc; desc++)
  33. if (*desc == ':')
  34. goto found_colon;
  35. return -EINVAL;
  36. found_colon:
  37. desc++;
  38. for (; *desc; desc++) {
  39. if (!isxdigit(*desc))
  40. return -EINVAL;
  41. n++;
  42. }
  43. if (n == 0 || n & 1)
  44. return -EINVAL;
  45. return 0;
  46. }
  47. /*
  48. * The hash to be blacklisted is expected to be in the description. There will
  49. * be no payload.
  50. */
  51. static int blacklist_preparse(struct key_preparsed_payload *prep)
  52. {
  53. if (prep->datalen > 0)
  54. return -EINVAL;
  55. return 0;
  56. }
  57. static void blacklist_free_preparse(struct key_preparsed_payload *prep)
  58. {
  59. }
  60. static void blacklist_describe(const struct key *key, struct seq_file *m)
  61. {
  62. seq_puts(m, key->description);
  63. }
  64. static struct key_type key_type_blacklist = {
  65. .name = "blacklist",
  66. .vet_description = blacklist_vet_description,
  67. .preparse = blacklist_preparse,
  68. .free_preparse = blacklist_free_preparse,
  69. .instantiate = generic_key_instantiate,
  70. .describe = blacklist_describe,
  71. };
  72. /**
  73. * mark_hash_blacklisted - Add a hash to the system blacklist
  74. * @hash - The hash as a hex string with a type prefix (eg. "tbs:23aa429783")
  75. */
  76. int mark_hash_blacklisted(const char *hash)
  77. {
  78. key_ref_t key;
  79. key = key_create_or_update(make_key_ref(blacklist_keyring, true),
  80. "blacklist",
  81. hash,
  82. NULL,
  83. 0,
  84. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  85. KEY_USR_VIEW),
  86. KEY_ALLOC_NOT_IN_QUOTA |
  87. KEY_ALLOC_BUILT_IN);
  88. if (IS_ERR(key)) {
  89. pr_err("Problem blacklisting hash (%ld)\n", PTR_ERR(key));
  90. return PTR_ERR(key);
  91. }
  92. return 0;
  93. }
  94. /**
  95. * is_hash_blacklisted - Determine if a hash is blacklisted
  96. * @hash: The hash to be checked as a binary blob
  97. * @hash_len: The length of the binary hash
  98. * @type: Type of hash
  99. */
  100. int is_hash_blacklisted(const u8 *hash, size_t hash_len, const char *type)
  101. {
  102. key_ref_t kref;
  103. size_t type_len = strlen(type);
  104. char *buffer, *p;
  105. int ret = 0;
  106. buffer = kmalloc(type_len + 1 + hash_len * 2 + 1, GFP_KERNEL);
  107. if (!buffer)
  108. return -ENOMEM;
  109. p = memcpy(buffer, type, type_len);
  110. p += type_len;
  111. *p++ = ':';
  112. bin2hex(p, hash, hash_len);
  113. p += hash_len * 2;
  114. *p = 0;
  115. kref = keyring_search(make_key_ref(blacklist_keyring, true),
  116. &key_type_blacklist, buffer);
  117. if (!IS_ERR(kref)) {
  118. key_ref_put(kref);
  119. ret = -EKEYREJECTED;
  120. }
  121. kfree(buffer);
  122. return ret;
  123. }
  124. EXPORT_SYMBOL_GPL(is_hash_blacklisted);
  125. /*
  126. * Initialise the blacklist
  127. */
  128. static int __init blacklist_init(void)
  129. {
  130. const char *const *bl;
  131. if (register_key_type(&key_type_blacklist) < 0)
  132. panic("Can't allocate system blacklist key type\n");
  133. blacklist_keyring =
  134. keyring_alloc(".blacklist",
  135. KUIDT_INIT(0), KGIDT_INIT(0),
  136. current_cred(),
  137. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  138. KEY_USR_VIEW | KEY_USR_READ |
  139. KEY_USR_SEARCH,
  140. KEY_ALLOC_NOT_IN_QUOTA |
  141. KEY_ALLOC_SET_KEEP,
  142. NULL, NULL);
  143. if (IS_ERR(blacklist_keyring))
  144. panic("Can't allocate system blacklist keyring\n");
  145. for (bl = blacklist_hashes; *bl; bl++)
  146. if (mark_hash_blacklisted(*bl) < 0)
  147. pr_err("- blacklisting failed\n");
  148. return 0;
  149. }
  150. /*
  151. * Must be initialised before we try and load the keys into the keyring.
  152. */
  153. device_initcall(blacklist_init);