system_keyring.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* System trusted keyring for trusted public keys
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/cred.h>
  11. #include <linux/err.h>
  12. #include <linux/slab.h>
  13. #include <linux/uidgid.h>
  14. #include <linux/verification.h>
  15. #include <keys/asymmetric-type.h>
  16. #include <keys/system_keyring.h>
  17. #include <crypto/pkcs7.h>
  18. static struct key *builtin_trusted_keys;
  19. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  20. static struct key *secondary_trusted_keys;
  21. #endif
  22. #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
  23. static struct key *machine_trusted_keys;
  24. #endif
  25. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  26. static struct key *platform_trusted_keys;
  27. #endif
  28. extern __initconst const u8 system_certificate_list[];
  29. extern __initconst const unsigned long system_certificate_list_size;
  30. extern __initconst const unsigned long module_cert_size;
  31. /**
  32. * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
  33. *
  34. * Restrict the addition of keys into a keyring based on the key-to-be-added
  35. * being vouched for by a key in the built in system keyring.
  36. */
  37. int restrict_link_by_builtin_trusted(struct key *dest_keyring,
  38. const struct key_type *type,
  39. const union key_payload *payload,
  40. struct key *restriction_key)
  41. {
  42. return restrict_link_by_signature(dest_keyring, type, payload,
  43. builtin_trusted_keys);
  44. }
  45. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  46. /**
  47. * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
  48. * addition by both builtin and secondary keyrings
  49. *
  50. * Restrict the addition of keys into a keyring based on the key-to-be-added
  51. * being vouched for by a key in either the built-in or the secondary system
  52. * keyrings.
  53. */
  54. int restrict_link_by_builtin_and_secondary_trusted(
  55. struct key *dest_keyring,
  56. const struct key_type *type,
  57. const union key_payload *payload,
  58. struct key *restrict_key)
  59. {
  60. /* If we have a secondary trusted keyring, then that contains a link
  61. * through to the builtin keyring and the search will follow that link.
  62. */
  63. if (type == &key_type_keyring &&
  64. dest_keyring == secondary_trusted_keys &&
  65. payload == &builtin_trusted_keys->payload)
  66. /* Allow the builtin keyring to be added to the secondary */
  67. return 0;
  68. return restrict_link_by_signature(dest_keyring, type, payload,
  69. secondary_trusted_keys);
  70. }
  71. /**
  72. * Allocate a struct key_restriction for the "builtin and secondary trust"
  73. * keyring. Only for use in system_trusted_keyring_init().
  74. */
  75. static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
  76. {
  77. struct key_restriction *restriction;
  78. restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
  79. if (!restriction)
  80. panic("Can't allocate secondary trusted keyring restriction\n");
  81. if (IS_ENABLED(CONFIG_INTEGRITY_MACHINE_KEYRING))
  82. restriction->check = restrict_link_by_builtin_secondary_and_machine;
  83. else
  84. restriction->check = restrict_link_by_builtin_and_secondary_trusted;
  85. return restriction;
  86. }
  87. #endif
  88. #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
  89. void __init set_machine_trusted_keys(struct key *keyring)
  90. {
  91. machine_trusted_keys = keyring;
  92. if (key_link(secondary_trusted_keys, machine_trusted_keys) < 0)
  93. panic("Can't link (machine) trusted keyrings\n");
  94. }
  95. /**
  96. * restrict_link_by_builtin_secondary_and_machine - Restrict keyring addition.
  97. * @dest_keyring: Keyring being linked to.
  98. * @type: The type of key being added.
  99. * @payload: The payload of the new key.
  100. * @restrict_key: A ring of keys that can be used to vouch for the new cert.
  101. *
  102. * Restrict the addition of keys into a keyring based on the key-to-be-added
  103. * being vouched for by a key in either the built-in, the secondary, or
  104. * the machine keyrings.
  105. */
  106. int restrict_link_by_builtin_secondary_and_machine(
  107. struct key *dest_keyring,
  108. const struct key_type *type,
  109. const union key_payload *payload,
  110. struct key *restrict_key)
  111. {
  112. if (machine_trusted_keys && type == &key_type_keyring &&
  113. dest_keyring == secondary_trusted_keys &&
  114. payload == &machine_trusted_keys->payload)
  115. /* Allow the machine keyring to be added to the secondary */
  116. return 0;
  117. return restrict_link_by_builtin_and_secondary_trusted(dest_keyring, type,
  118. payload, restrict_key);
  119. }
  120. #endif
  121. /*
  122. * Create the trusted keyrings
  123. */
  124. static __init int system_trusted_keyring_init(void)
  125. {
  126. pr_notice("Initialise system trusted keyrings\n");
  127. builtin_trusted_keys =
  128. keyring_alloc(".builtin_trusted_keys",
  129. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
  130. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  131. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
  132. KEY_ALLOC_NOT_IN_QUOTA,
  133. NULL, NULL);
  134. if (IS_ERR(builtin_trusted_keys))
  135. panic("Can't allocate builtin trusted keyring\n");
  136. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  137. secondary_trusted_keys =
  138. keyring_alloc(".secondary_trusted_keys",
  139. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
  140. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  141. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
  142. KEY_USR_WRITE),
  143. KEY_ALLOC_NOT_IN_QUOTA,
  144. get_builtin_and_secondary_restriction(),
  145. NULL);
  146. if (IS_ERR(secondary_trusted_keys))
  147. panic("Can't allocate secondary trusted keyring\n");
  148. if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
  149. panic("Can't link trusted keyrings\n");
  150. #endif
  151. return 0;
  152. }
  153. /*
  154. * Must be initialised before we try and load the keys into the keyring.
  155. */
  156. device_initcall(system_trusted_keyring_init);
  157. __init int load_module_cert(struct key *keyring)
  158. {
  159. if (!IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG))
  160. return 0;
  161. pr_notice("Loading compiled-in module X.509 certificates\n");
  162. return x509_load_certificate_list(system_certificate_list,
  163. module_cert_size, keyring);
  164. }
  165. /*
  166. * Load the compiled-in list of X.509 certificates.
  167. */
  168. static __init int load_system_certificate_list(void)
  169. {
  170. const u8 *p;
  171. unsigned long size;
  172. pr_notice("Loading compiled-in X.509 certificates\n");
  173. #ifdef CONFIG_MODULE_SIG
  174. p = system_certificate_list;
  175. size = system_certificate_list_size;
  176. #else
  177. p = system_certificate_list + module_cert_size;
  178. size = system_certificate_list_size - module_cert_size;
  179. #endif
  180. return x509_load_certificate_list(p, size, builtin_trusted_keys);
  181. }
  182. late_initcall(load_system_certificate_list);
  183. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  184. /**
  185. * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data.
  186. * @data: The data to be verified (NULL if expecting internal data).
  187. * @len: Size of @data.
  188. * @pkcs7: The PKCS#7 message that is the signature.
  189. * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
  190. * (void *)1UL for all trusted keys).
  191. * @usage: The use to which the key is being put.
  192. * @view_content: Callback to gain access to content.
  193. * @ctx: Context for callback.
  194. */
  195. int verify_pkcs7_message_sig(const void *data, size_t len,
  196. struct pkcs7_message *pkcs7,
  197. struct key *trusted_keys,
  198. enum key_being_used_for usage,
  199. int (*view_content)(void *ctx,
  200. const void *data, size_t len,
  201. size_t asn1hdrlen),
  202. void *ctx)
  203. {
  204. int ret;
  205. /* The data should be detached - so we need to supply it. */
  206. if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
  207. pr_err("PKCS#7 signature with non-detached data\n");
  208. ret = -EBADMSG;
  209. goto error;
  210. }
  211. ret = pkcs7_verify(pkcs7, usage);
  212. if (ret < 0)
  213. goto error;
  214. if (!trusted_keys) {
  215. trusted_keys = builtin_trusted_keys;
  216. } else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
  217. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  218. trusted_keys = secondary_trusted_keys;
  219. #else
  220. trusted_keys = builtin_trusted_keys;
  221. #endif
  222. } else if (trusted_keys == VERIFY_USE_PLATFORM_KEYRING) {
  223. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  224. trusted_keys = platform_trusted_keys;
  225. #else
  226. trusted_keys = NULL;
  227. #endif
  228. if (!trusted_keys) {
  229. ret = -ENOKEY;
  230. pr_devel("PKCS#7 platform keyring is not available\n");
  231. goto error;
  232. }
  233. ret = is_key_on_revocation_list(pkcs7);
  234. if (ret != -ENOKEY) {
  235. pr_devel("PKCS#7 platform key is on revocation list\n");
  236. goto error;
  237. }
  238. }
  239. ret = pkcs7_validate_trust(pkcs7, trusted_keys);
  240. if (ret < 0) {
  241. if (ret == -ENOKEY)
  242. pr_devel("PKCS#7 signature not signed with a trusted key\n");
  243. goto error;
  244. }
  245. if (view_content) {
  246. size_t asn1hdrlen;
  247. ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
  248. if (ret < 0) {
  249. if (ret == -ENODATA)
  250. pr_devel("PKCS#7 message does not contain data\n");
  251. goto error;
  252. }
  253. ret = view_content(ctx, data, len, asn1hdrlen);
  254. }
  255. error:
  256. pr_devel("<==%s() = %d\n", __func__, ret);
  257. return ret;
  258. }
  259. /**
  260. * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
  261. * @data: The data to be verified (NULL if expecting internal data).
  262. * @len: Size of @data.
  263. * @raw_pkcs7: The PKCS#7 message that is the signature.
  264. * @pkcs7_len: The size of @raw_pkcs7.
  265. * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
  266. * (void *)1UL for all trusted keys).
  267. * @usage: The use to which the key is being put.
  268. * @view_content: Callback to gain access to content.
  269. * @ctx: Context for callback.
  270. */
  271. int verify_pkcs7_signature(const void *data, size_t len,
  272. const void *raw_pkcs7, size_t pkcs7_len,
  273. struct key *trusted_keys,
  274. enum key_being_used_for usage,
  275. int (*view_content)(void *ctx,
  276. const void *data, size_t len,
  277. size_t asn1hdrlen),
  278. void *ctx)
  279. {
  280. struct pkcs7_message *pkcs7;
  281. int ret;
  282. pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
  283. if (IS_ERR(pkcs7))
  284. return PTR_ERR(pkcs7);
  285. ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage,
  286. view_content, ctx);
  287. pkcs7_free_message(pkcs7);
  288. pr_devel("<==%s() = %d\n", __func__, ret);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
  292. #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
  293. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  294. void __init set_platform_trusted_keys(struct key *keyring)
  295. {
  296. platform_trusted_keys = keyring;
  297. }
  298. #endif