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 
8 #include <linux/export.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/cred.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/verification.h>
15 #include <keys/asymmetric-type.h>
16 #include <keys/system_keyring.h>
17 #include <crypto/pkcs7.h>
18 
19 static struct key *builtin_trusted_keys;
20 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
21 static struct key *secondary_trusted_keys;
22 #endif
23 #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
24 static struct key *platform_trusted_keys;
25 #endif
26 
27 extern __initconst const u8 system_certificate_list[];
28 extern __initconst const unsigned long system_certificate_list_size;
29 
30 /**
31  * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
32  *
33  * Restrict the addition of keys into a keyring based on the key-to-be-added
34  * being vouched for by a key in the built in system keyring.
35  */
restrict_link_by_builtin_trusted(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * restriction_key)36 int restrict_link_by_builtin_trusted(struct key *dest_keyring,
37 				     const struct key_type *type,
38 				     const union key_payload *payload,
39 				     struct key *restriction_key)
40 {
41 	return restrict_link_by_signature(dest_keyring, type, payload,
42 					  builtin_trusted_keys);
43 }
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  */
restrict_link_by_builtin_and_secondary_trusted(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * restrict_key)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 
69 	return restrict_link_by_signature(dest_keyring, type, payload,
70 					  secondary_trusted_keys);
71 }
72 
73 /**
74  * Allocate a struct key_restriction for the "builtin and secondary trust"
75  * keyring. Only for use in system_trusted_keyring_init().
76  */
get_builtin_and_secondary_restriction(void)77 static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
78 {
79 	struct key_restriction *restriction;
80 
81 	restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
82 
83 	if (!restriction)
84 		panic("Can't allocate secondary trusted keyring restriction\n");
85 
86 	restriction->check = restrict_link_by_builtin_and_secondary_trusted;
87 
88 	return restriction;
89 }
90 #endif
91 
92 /*
93  * Create the trusted keyrings
94  */
system_trusted_keyring_init(void)95 static __init int system_trusted_keyring_init(void)
96 {
97 	pr_notice("Initialise system trusted keyrings\n");
98 
99 	builtin_trusted_keys =
100 		keyring_alloc(".builtin_trusted_keys",
101 			      KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
102 			      ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
103 			      KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
104 			      KEY_ALLOC_NOT_IN_QUOTA,
105 			      NULL, NULL);
106 	if (IS_ERR(builtin_trusted_keys))
107 		panic("Can't allocate builtin trusted keyring\n");
108 
109 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
110 	secondary_trusted_keys =
111 		keyring_alloc(".secondary_trusted_keys",
112 			      KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
113 			      ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
114 			       KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
115 			       KEY_USR_WRITE),
116 			      KEY_ALLOC_NOT_IN_QUOTA,
117 			      get_builtin_and_secondary_restriction(),
118 			      NULL);
119 	if (IS_ERR(secondary_trusted_keys))
120 		panic("Can't allocate secondary trusted keyring\n");
121 
122 	if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
123 		panic("Can't link trusted keyrings\n");
124 #endif
125 
126 	return 0;
127 }
128 
129 /*
130  * Must be initialised before we try and load the keys into the keyring.
131  */
132 device_initcall(system_trusted_keyring_init);
133 
134 /*
135  * Load the compiled-in list of X.509 certificates.
136  */
load_system_certificate_list(void)137 static __init int load_system_certificate_list(void)
138 {
139 	key_ref_t key;
140 	const u8 *p, *end;
141 	size_t plen;
142 
143 	pr_notice("Loading compiled-in X.509 certificates\n");
144 
145 	p = system_certificate_list;
146 	end = p + system_certificate_list_size;
147 	while (p < end) {
148 		/* Each cert begins with an ASN.1 SEQUENCE tag and must be more
149 		 * than 256 bytes in size.
150 		 */
151 		if (end - p < 4)
152 			goto dodgy_cert;
153 		if (p[0] != 0x30 &&
154 		    p[1] != 0x82)
155 			goto dodgy_cert;
156 		plen = (p[2] << 8) | p[3];
157 		plen += 4;
158 		if (plen > end - p)
159 			goto dodgy_cert;
160 
161 		key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
162 					   "asymmetric",
163 					   NULL,
164 					   p,
165 					   plen,
166 					   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
167 					   KEY_USR_VIEW | KEY_USR_READ),
168 					   KEY_ALLOC_NOT_IN_QUOTA |
169 					   KEY_ALLOC_BUILT_IN |
170 					   KEY_ALLOC_BYPASS_RESTRICTION);
171 		if (IS_ERR(key)) {
172 			pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
173 			       PTR_ERR(key));
174 		} else {
175 			pr_notice("Loaded X.509 cert '%s'\n",
176 				  key_ref_to_ptr(key)->description);
177 			key_ref_put(key);
178 		}
179 		p += plen;
180 	}
181 
182 	return 0;
183 
184 dodgy_cert:
185 	pr_err("Problem parsing in-kernel X.509 certificate list\n");
186 	return 0;
187 }
188 late_initcall(load_system_certificate_list);
189 
190 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
191 
192 /**
193  * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data.
194  * @data: The data to be verified (NULL if expecting internal data).
195  * @len: Size of @data.
196  * @pkcs7: The PKCS#7 message that is the signature.
197  * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
198  *					(void *)1UL for all trusted keys).
199  * @usage: The use to which the key is being put.
200  * @view_content: Callback to gain access to content.
201  * @ctx: Context for callback.
202  */
verify_pkcs7_message_sig(const void * data,size_t len,struct pkcs7_message * pkcs7,struct key * trusted_keys,enum key_being_used_for usage,int (* view_content)(void * ctx,const void * data,size_t len,size_t asn1hdrlen),void * ctx)203 int verify_pkcs7_message_sig(const void *data, size_t len,
204 			     struct pkcs7_message *pkcs7,
205 			     struct key *trusted_keys,
206 			     enum key_being_used_for usage,
207 			     int (*view_content)(void *ctx,
208 						 const void *data, size_t len,
209 						 size_t asn1hdrlen),
210 			     void *ctx)
211 {
212 	int ret;
213 
214 	/* The data should be detached - so we need to supply it. */
215 	if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
216 		pr_err("PKCS#7 signature with non-detached data\n");
217 		ret = -EBADMSG;
218 		goto error;
219 	}
220 
221 	ret = pkcs7_verify(pkcs7, usage);
222 	if (ret < 0)
223 		goto error;
224 
225 	if (!trusted_keys) {
226 		trusted_keys = builtin_trusted_keys;
227 	} else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
228 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
229 		trusted_keys = secondary_trusted_keys;
230 #else
231 		trusted_keys = builtin_trusted_keys;
232 #endif
233 	} else if (trusted_keys == VERIFY_USE_PLATFORM_KEYRING) {
234 #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
235 		trusted_keys = platform_trusted_keys;
236 #else
237 		trusted_keys = NULL;
238 #endif
239 		if (!trusted_keys) {
240 			ret = -ENOKEY;
241 			pr_devel("PKCS#7 platform keyring is not available\n");
242 			goto error;
243 		}
244 	}
245 	ret = pkcs7_validate_trust(pkcs7, trusted_keys);
246 	if (ret < 0) {
247 		if (ret == -ENOKEY)
248 			pr_devel("PKCS#7 signature not signed with a trusted key\n");
249 		goto error;
250 	}
251 
252 	if (view_content) {
253 		size_t asn1hdrlen;
254 
255 		ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
256 		if (ret < 0) {
257 			if (ret == -ENODATA)
258 				pr_devel("PKCS#7 message does not contain data\n");
259 			goto error;
260 		}
261 
262 		ret = view_content(ctx, data, len, asn1hdrlen);
263 	}
264 
265 error:
266 	pr_devel("<==%s() = %d\n", __func__, ret);
267 	return ret;
268 }
269 
270 /**
271  * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
272  * @data: The data to be verified (NULL if expecting internal data).
273  * @len: Size of @data.
274  * @raw_pkcs7: The PKCS#7 message that is the signature.
275  * @pkcs7_len: The size of @raw_pkcs7.
276  * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
277  *					(void *)1UL for all trusted keys).
278  * @usage: The use to which the key is being put.
279  * @view_content: Callback to gain access to content.
280  * @ctx: Context for callback.
281  */
verify_pkcs7_signature(const void * data,size_t len,const void * raw_pkcs7,size_t pkcs7_len,struct key * trusted_keys,enum key_being_used_for usage,int (* view_content)(void * ctx,const void * data,size_t len,size_t asn1hdrlen),void * ctx)282 int verify_pkcs7_signature(const void *data, size_t len,
283 			   const void *raw_pkcs7, size_t pkcs7_len,
284 			   struct key *trusted_keys,
285 			   enum key_being_used_for usage,
286 			   int (*view_content)(void *ctx,
287 					       const void *data, size_t len,
288 					       size_t asn1hdrlen),
289 			   void *ctx)
290 {
291 	struct pkcs7_message *pkcs7;
292 	int ret;
293 
294 	pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
295 	if (IS_ERR(pkcs7))
296 		return PTR_ERR(pkcs7);
297 
298 	ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage,
299 				       view_content, ctx);
300 
301 	pkcs7_free_message(pkcs7);
302 	pr_devel("<==%s() = %d\n", __func__, ret);
303 	return ret;
304 }
305 EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
306 
307 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
308 
309 #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
set_platform_trusted_keys(struct key * keyring)310 void __init set_platform_trusted_keys(struct key *keyring)
311 {
312 	platform_trusted_keys = keyring;
313 }
314 #endif
315