system_keyring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 (dhowells@redhat.com)
  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_by_builtin_trusted - Restrict keyring addition by built-in CA
  33. * @dest_keyring: Keyring being linked to.
  34. * @type: The type of key being added.
  35. * @payload: The payload of the new key.
  36. * @restriction_key: A ring of keys that can be used to vouch for the new cert.
  37. *
  38. * Restrict the addition of keys into a keyring based on the key-to-be-added
  39. * being vouched for by a key in the built in system keyring.
  40. */
  41. int restrict_link_by_builtin_trusted(struct key *dest_keyring,
  42. const struct key_type *type,
  43. const union key_payload *payload,
  44. struct key *restriction_key)
  45. {
  46. return restrict_link_by_signature(dest_keyring, type, payload,
  47. builtin_trusted_keys);
  48. }
  49. /**
  50. * restrict_link_by_digsig_builtin - Restrict digitalSignature key additions by the built-in keyring
  51. * @dest_keyring: Keyring being linked to.
  52. * @type: The type of key being added.
  53. * @payload: The payload of the new key.
  54. * @restriction_key: A ring of keys that can be used to vouch for the new cert.
  55. *
  56. * Restrict the addition of keys into a keyring based on the key-to-be-added
  57. * being vouched for by a key in the built in system keyring. The new key
  58. * must have the digitalSignature usage field set.
  59. */
  60. int restrict_link_by_digsig_builtin(struct key *dest_keyring,
  61. const struct key_type *type,
  62. const union key_payload *payload,
  63. struct key *restriction_key)
  64. {
  65. return restrict_link_by_digsig(dest_keyring, type, payload,
  66. builtin_trusted_keys);
  67. }
  68. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  69. /**
  70. * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
  71. * addition by both built-in and secondary keyrings.
  72. * @dest_keyring: Keyring being linked to.
  73. * @type: The type of key being added.
  74. * @payload: The payload of the new key.
  75. * @restrict_key: A ring of keys that can be used to vouch for the new cert.
  76. *
  77. * Restrict the addition of keys into a keyring based on the key-to-be-added
  78. * being vouched for by a key in either the built-in or the secondary system
  79. * keyrings.
  80. */
  81. int restrict_link_by_builtin_and_secondary_trusted(
  82. struct key *dest_keyring,
  83. const struct key_type *type,
  84. const union key_payload *payload,
  85. struct key *restrict_key)
  86. {
  87. /* If we have a secondary trusted keyring, then that contains a link
  88. * through to the builtin keyring and the search will follow that link.
  89. */
  90. if (type == &key_type_keyring &&
  91. dest_keyring == secondary_trusted_keys &&
  92. payload == &builtin_trusted_keys->payload)
  93. /* Allow the builtin keyring to be added to the secondary */
  94. return 0;
  95. return restrict_link_by_signature(dest_keyring, type, payload,
  96. secondary_trusted_keys);
  97. }
  98. /**
  99. * restrict_link_by_digsig_builtin_and_secondary - Restrict by digitalSignature.
  100. * @dest_keyring: Keyring being linked to.
  101. * @type: The type of key being added.
  102. * @payload: The payload of the new key.
  103. * @restrict_key: A ring of keys that can be used to vouch for the new cert.
  104. *
  105. * Restrict the addition of keys into a keyring based on the key-to-be-added
  106. * being vouched for by a key in either the built-in or the secondary system
  107. * keyrings. The new key must have the digitalSignature usage field set.
  108. */
  109. int restrict_link_by_digsig_builtin_and_secondary(struct key *dest_keyring,
  110. const struct key_type *type,
  111. const union key_payload *payload,
  112. struct key *restrict_key)
  113. {
  114. /* If we have a secondary trusted keyring, then that contains a link
  115. * through to the builtin keyring and the search will follow that link.
  116. */
  117. if (type == &key_type_keyring &&
  118. dest_keyring == secondary_trusted_keys &&
  119. payload == &builtin_trusted_keys->payload)
  120. /* Allow the builtin keyring to be added to the secondary */
  121. return 0;
  122. return restrict_link_by_digsig(dest_keyring, type, payload,
  123. secondary_trusted_keys);
  124. }
  125. /*
  126. * Allocate a struct key_restriction for the "builtin and secondary trust"
  127. * keyring. Only for use in system_trusted_keyring_init().
  128. */
  129. static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
  130. {
  131. struct key_restriction *restriction;
  132. restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
  133. if (!restriction)
  134. panic("Can't allocate secondary trusted keyring restriction\n");
  135. if (IS_ENABLED(CONFIG_INTEGRITY_MACHINE_KEYRING))
  136. restriction->check = restrict_link_by_builtin_secondary_and_machine;
  137. else
  138. restriction->check = restrict_link_by_builtin_and_secondary_trusted;
  139. return restriction;
  140. }
  141. /**
  142. * add_to_secondary_keyring - Add to secondary keyring.
  143. * @source: Source of key
  144. * @data: The blob holding the key
  145. * @len: The length of the data blob
  146. *
  147. * Add a key to the secondary keyring. The key must be vouched for by a key in the builtin,
  148. * machine or secondary keyring itself.
  149. */
  150. void __init add_to_secondary_keyring(const char *source, const void *data, size_t len)
  151. {
  152. key_ref_t key;
  153. key_perm_t perm;
  154. perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW;
  155. key = key_create_or_update(make_key_ref(secondary_trusted_keys, 1),
  156. "asymmetric",
  157. NULL, data, len, perm,
  158. KEY_ALLOC_NOT_IN_QUOTA);
  159. if (IS_ERR(key)) {
  160. pr_err("Problem loading X.509 certificate from %s to secondary keyring %ld\n",
  161. source, PTR_ERR(key));
  162. return;
  163. }
  164. pr_notice("Loaded X.509 cert '%s'\n", key_ref_to_ptr(key)->description);
  165. key_ref_put(key);
  166. }
  167. #endif
  168. #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
  169. void __init set_machine_trusted_keys(struct key *keyring)
  170. {
  171. machine_trusted_keys = keyring;
  172. if (key_link(secondary_trusted_keys, machine_trusted_keys) < 0)
  173. panic("Can't link (machine) trusted keyrings\n");
  174. }
  175. /**
  176. * restrict_link_by_builtin_secondary_and_machine - Restrict keyring addition.
  177. * @dest_keyring: Keyring being linked to.
  178. * @type: The type of key being added.
  179. * @payload: The payload of the new key.
  180. * @restrict_key: A ring of keys that can be used to vouch for the new cert.
  181. *
  182. * Restrict the addition of keys into a keyring based on the key-to-be-added
  183. * being vouched for by a key in either the built-in, the secondary, or
  184. * the machine keyrings.
  185. */
  186. int restrict_link_by_builtin_secondary_and_machine(
  187. struct key *dest_keyring,
  188. const struct key_type *type,
  189. const union key_payload *payload,
  190. struct key *restrict_key)
  191. {
  192. if (machine_trusted_keys && type == &key_type_keyring &&
  193. dest_keyring == secondary_trusted_keys &&
  194. payload == &machine_trusted_keys->payload)
  195. /* Allow the machine keyring to be added to the secondary */
  196. return 0;
  197. return restrict_link_by_builtin_and_secondary_trusted(dest_keyring, type,
  198. payload, restrict_key);
  199. }
  200. #endif
  201. /*
  202. * Create the trusted keyrings
  203. */
  204. static __init int system_trusted_keyring_init(void)
  205. {
  206. pr_notice("Initialise system trusted keyrings\n");
  207. builtin_trusted_keys =
  208. keyring_alloc(".builtin_trusted_keys",
  209. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
  210. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  211. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
  212. KEY_ALLOC_NOT_IN_QUOTA,
  213. NULL, NULL);
  214. if (IS_ERR(builtin_trusted_keys))
  215. panic("Can't allocate builtin trusted keyring\n");
  216. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  217. secondary_trusted_keys =
  218. keyring_alloc(".secondary_trusted_keys",
  219. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
  220. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  221. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
  222. KEY_USR_WRITE),
  223. KEY_ALLOC_NOT_IN_QUOTA,
  224. get_builtin_and_secondary_restriction(),
  225. NULL);
  226. if (IS_ERR(secondary_trusted_keys))
  227. panic("Can't allocate secondary trusted keyring\n");
  228. if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
  229. panic("Can't link trusted keyrings\n");
  230. #endif
  231. return 0;
  232. }
  233. /*
  234. * Must be initialised before we try and load the keys into the keyring.
  235. */
  236. device_initcall(system_trusted_keyring_init);
  237. __init int load_module_cert(struct key *keyring)
  238. {
  239. if (!IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG))
  240. return 0;
  241. pr_notice("Loading compiled-in module X.509 certificates\n");
  242. return x509_load_certificate_list(system_certificate_list,
  243. module_cert_size, keyring);
  244. }
  245. /*
  246. * Load the compiled-in list of X.509 certificates.
  247. */
  248. static __init int load_system_certificate_list(void)
  249. {
  250. const u8 *p;
  251. unsigned long size;
  252. pr_notice("Loading compiled-in X.509 certificates\n");
  253. #ifdef CONFIG_MODULE_SIG
  254. p = system_certificate_list;
  255. size = system_certificate_list_size;
  256. #else
  257. p = system_certificate_list + module_cert_size;
  258. size = system_certificate_list_size - module_cert_size;
  259. #endif
  260. return x509_load_certificate_list(p, size, builtin_trusted_keys);
  261. }
  262. late_initcall(load_system_certificate_list);
  263. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  264. /**
  265. * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data.
  266. * @data: The data to be verified (NULL if expecting internal data).
  267. * @len: Size of @data.
  268. * @pkcs7: The PKCS#7 message that is the signature.
  269. * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
  270. * (void *)1UL for all trusted keys).
  271. * @usage: The use to which the key is being put.
  272. * @view_content: Callback to gain access to content.
  273. * @ctx: Context for callback.
  274. */
  275. int verify_pkcs7_message_sig(const void *data, size_t len,
  276. struct pkcs7_message *pkcs7,
  277. struct key *trusted_keys,
  278. enum key_being_used_for usage,
  279. int (*view_content)(void *ctx,
  280. const void *data, size_t len,
  281. size_t asn1hdrlen),
  282. void *ctx)
  283. {
  284. int ret;
  285. /* The data should be detached - so we need to supply it. */
  286. if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
  287. pr_err("PKCS#7 signature with non-detached data\n");
  288. ret = -EBADMSG;
  289. goto error;
  290. }
  291. ret = pkcs7_verify(pkcs7, usage);
  292. if (ret < 0)
  293. goto error;
  294. ret = is_key_on_revocation_list(pkcs7);
  295. if (ret != -ENOKEY) {
  296. pr_devel("PKCS#7 key is on revocation list\n");
  297. goto error;
  298. }
  299. if (!trusted_keys) {
  300. trusted_keys = builtin_trusted_keys;
  301. } else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
  302. #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
  303. trusted_keys = secondary_trusted_keys;
  304. #else
  305. trusted_keys = builtin_trusted_keys;
  306. #endif
  307. } else if (trusted_keys == VERIFY_USE_PLATFORM_KEYRING) {
  308. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  309. trusted_keys = platform_trusted_keys;
  310. #else
  311. trusted_keys = NULL;
  312. #endif
  313. if (!trusted_keys) {
  314. ret = -ENOKEY;
  315. pr_devel("PKCS#7 platform keyring is not available\n");
  316. goto error;
  317. }
  318. }
  319. ret = pkcs7_validate_trust(pkcs7, trusted_keys);
  320. if (ret < 0) {
  321. if (ret == -ENOKEY)
  322. pr_devel("PKCS#7 signature not signed with a trusted key\n");
  323. goto error;
  324. }
  325. if (view_content) {
  326. size_t asn1hdrlen;
  327. ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
  328. if (ret < 0) {
  329. if (ret == -ENODATA)
  330. pr_devel("PKCS#7 message does not contain data\n");
  331. goto error;
  332. }
  333. ret = view_content(ctx, data, len, asn1hdrlen);
  334. }
  335. error:
  336. pr_devel("<==%s() = %d\n", __func__, ret);
  337. return ret;
  338. }
  339. /**
  340. * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
  341. * @data: The data to be verified (NULL if expecting internal data).
  342. * @len: Size of @data.
  343. * @raw_pkcs7: The PKCS#7 message that is the signature.
  344. * @pkcs7_len: The size of @raw_pkcs7.
  345. * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
  346. * (void *)1UL for all trusted keys).
  347. * @usage: The use to which the key is being put.
  348. * @view_content: Callback to gain access to content.
  349. * @ctx: Context for callback.
  350. */
  351. int verify_pkcs7_signature(const void *data, size_t len,
  352. const void *raw_pkcs7, size_t pkcs7_len,
  353. struct key *trusted_keys,
  354. enum key_being_used_for usage,
  355. int (*view_content)(void *ctx,
  356. const void *data, size_t len,
  357. size_t asn1hdrlen),
  358. void *ctx)
  359. {
  360. struct pkcs7_message *pkcs7;
  361. int ret;
  362. pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
  363. if (IS_ERR(pkcs7))
  364. return PTR_ERR(pkcs7);
  365. ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage,
  366. view_content, ctx);
  367. pkcs7_free_message(pkcs7);
  368. pr_devel("<==%s() = %d\n", __func__, ret);
  369. return ret;
  370. }
  371. EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
  372. #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
  373. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  374. void __init set_platform_trusted_keys(struct key *keyring)
  375. {
  376. platform_trusted_keys = keyring;
  377. }
  378. #endif