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