blob: 3b06a01bd0fdd5ae59f86ff74735befd5737de77 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Dmitry Kasatkin8607c502011-10-05 11:54:46 +03002/*
3 * Copyright (C) 2011 Intel Corporation
4 *
5 * Author:
6 * Dmitry Kasatkin <dmitry.kasatkin@intel.com>
Dmitry Kasatkin8607c502011-10-05 11:54:46 +03007 */
8
Dmitry Kasatkin8607c502011-10-05 11:54:46 +03009#include <linux/err.h>
Mimi Zohar7d2ce232013-08-13 08:47:43 -040010#include <linux/sched.h>
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +020011#include <linux/slab.h>
Mimi Zohar7d2ce232013-08-13 08:47:43 -040012#include <linux/cred.h>
Scott Brandenb89999d02020-10-02 10:38:15 -070013#include <linux/kernel_read_file.h>
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030014#include <linux/key-type.h>
15#include <linux/digsig.h>
Randy Dunlap120f3b12018-02-12 17:26:20 -080016#include <linux/vmalloc.h>
David Howellsa511e1a2016-04-06 16:14:26 +010017#include <crypto/public_key.h>
18#include <keys/system_keyring.h>
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030019
20#include "integrity.h"
21
22static struct key *keyring[INTEGRITY_KEYRING_MAX];
23
Eric Biggersb2724d52018-09-07 13:22:23 -070024static const char * const keyring_name[INTEGRITY_KEYRING_MAX] = {
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +030025#ifndef CONFIG_INTEGRITY_TRUSTED_KEYRING
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030026 "_evm",
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030027 "_ima",
Mimi Zohar7d2ce232013-08-13 08:47:43 -040028#else
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +030029 ".evm",
Mimi Zohar7d2ce232013-08-13 08:47:43 -040030 ".ima",
31#endif
Nayna Jain9dc92c42018-12-09 01:56:59 +053032 ".platform",
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030033};
34
David Howells56104cf2016-04-07 09:45:23 +010035#ifdef CONFIG_IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY
36#define restrict_link_to_ima restrict_link_by_builtin_and_secondary_trusted
David Howellsa511e1a2016-04-06 16:14:26 +010037#else
David Howells56104cf2016-04-07 09:45:23 +010038#define restrict_link_to_ima restrict_link_by_builtin_trusted
David Howellsa511e1a2016-04-06 16:14:26 +010039#endif
40
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030041static struct key *integrity_keyring_from_id(const unsigned int id)
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030042{
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030043 if (id >= INTEGRITY_KEYRING_MAX)
44 return ERR_PTR(-EINVAL);
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030045
46 if (!keyring[id]) {
47 keyring[id] =
Linus Torvalds028db3e2019-07-10 18:43:43 -070048 request_key(&key_type_keyring, keyring_name[id], NULL);
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030049 if (IS_ERR(keyring[id])) {
50 int err = PTR_ERR(keyring[id]);
51 pr_err("no %s keyring: %d\n", keyring_name[id], err);
52 keyring[id] = NULL;
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030053 return ERR_PTR(err);
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030054 }
55 }
56
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030057 return keyring[id];
58}
59
60int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
61 const char *digest, int digestlen)
62{
63 struct key *keyring;
64
65 if (siglen < 2)
66 return -EINVAL;
67
68 keyring = integrity_keyring_from_id(id);
69 if (IS_ERR(keyring))
70 return PTR_ERR(keyring);
71
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +090072 switch (sig[1]) {
Dmitry Kasatkine0751252013-02-07 00:12:08 +020073 case 1:
Dmitry Kasatkinb1aaab22013-10-10 16:12:03 +090074 /* v1 API expect signature without xattr type */
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030075 return digsig_verify(keyring, sig + 1, siglen - 1, digest,
76 digestlen);
Dmitry Kasatkine0751252013-02-07 00:12:08 +020077 case 2:
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030078 return asymmetric_verify(keyring, sig, siglen, digest,
79 digestlen);
Dmitry Kasatkine0751252013-02-07 00:12:08 +020080 }
81
82 return -EOPNOTSUPP;
Dmitry Kasatkin8607c502011-10-05 11:54:46 +030083}
Mimi Zohar7d2ce232013-08-13 08:47:43 -040084
Thiago Jung Bauermann39b07092019-06-27 23:19:30 -030085int integrity_modsig_verify(const unsigned int id, const struct modsig *modsig)
86{
87 struct key *keyring;
88
89 keyring = integrity_keyring_from_id(id);
90 if (IS_ERR(keyring))
91 return PTR_ERR(keyring);
92
93 return ima_modsig_verify(keyring, modsig);
94}
95
Geert Uytterhoeven8c655782019-06-17 09:44:52 +020096static int __init __integrity_init_keyring(const unsigned int id,
Linus Torvalds028db3e2019-07-10 18:43:43 -070097 key_perm_t perm,
Geert Uytterhoeven8c655782019-06-17 09:44:52 +020098 struct key_restriction *restriction)
Mimi Zohar7d2ce232013-08-13 08:47:43 -040099{
100 const struct cred *cred = current_cred();
101 int err = 0;
102
Nayna Jain9dc92c42018-12-09 01:56:59 +0530103 keyring[id] = keyring_alloc(keyring_name[id], KUIDT_INIT(0),
Linus Torvalds028db3e2019-07-10 18:43:43 -0700104 KGIDT_INIT(0), cred, perm,
Nayna Jain60740ac2018-12-09 01:57:00 +0530105 KEY_ALLOC_NOT_IN_QUOTA, restriction, NULL);
Nayna Jain9dc92c42018-12-09 01:56:59 +0530106 if (IS_ERR(keyring[id])) {
107 err = PTR_ERR(keyring[id]);
108 pr_info("Can't allocate %s keyring (%d)\n",
109 keyring_name[id], err);
110 keyring[id] = NULL;
Kairui Song219a3e82019-01-21 17:59:28 +0800111 } else {
112 if (id == INTEGRITY_KEYRING_PLATFORM)
113 set_platform_trusted_keys(keyring[id]);
Nayna Jain6cbdfb32021-04-09 10:35:07 -0400114 if (id == INTEGRITY_KEYRING_IMA)
115 load_module_cert(keyring[id]);
Nayna Jain9dc92c42018-12-09 01:56:59 +0530116 }
117
118 return err;
119}
120
121int __init integrity_init_keyring(const unsigned int id)
122{
123 struct key_restriction *restriction;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700124 key_perm_t perm;
125
126 perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW
127 | KEY_USR_READ | KEY_USR_SEARCH;
Nayna Jain9dc92c42018-12-09 01:56:59 +0530128
129 if (id == INTEGRITY_KEYRING_PLATFORM) {
130 restriction = NULL;
131 goto out;
132 }
133
Eric Biggers2ab5daf2018-10-03 17:15:44 -0700134 if (!IS_ENABLED(CONFIG_INTEGRITY_TRUSTED_KEYRING))
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300135 return 0;
136
Mat Martineau2b6aa412016-08-31 16:05:43 -0700137 restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
138 if (!restriction)
139 return -ENOMEM;
140
141 restriction->check = restrict_link_to_ima;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700142 perm |= KEY_USR_WRITE;
Mat Martineau2b6aa412016-08-31 16:05:43 -0700143
Nayna Jain9dc92c42018-12-09 01:56:59 +0530144out:
Linus Torvalds028db3e2019-07-10 18:43:43 -0700145 return __integrity_init_keyring(id, perm, restriction);
Mimi Zohar7d2ce232013-08-13 08:47:43 -0400146}
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200147
Wei Yongjunf6692212021-02-10 08:01:31 +0000148static int __init integrity_add_key(const unsigned int id, const void *data,
149 off_t size, key_perm_t perm)
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200150{
151 key_ref_t key;
Nayna Jain60740ac2018-12-09 01:57:00 +0530152 int rc = 0;
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200153
154 if (!keyring[id])
155 return -EINVAL;
156
Nayna Jain60740ac2018-12-09 01:57:00 +0530157 key = key_create_or_update(make_key_ref(keyring[id], 1), "asymmetric",
Linus Torvalds028db3e2019-07-10 18:43:43 -0700158 NULL, data, size, perm,
Nayna Jain60740ac2018-12-09 01:57:00 +0530159 KEY_ALLOC_NOT_IN_QUOTA);
160 if (IS_ERR(key)) {
161 rc = PTR_ERR(key);
162 pr_err("Problem loading X.509 certificate %d\n", rc);
163 } else {
164 pr_notice("Loaded X.509 cert '%s'\n",
165 key_ref_to_ptr(key)->description);
166 key_ref_put(key);
167 }
168
169 return rc;
170
171}
172
173int __init integrity_load_x509(const unsigned int id, const char *path)
174{
Kees Cookc3074592020-10-02 10:38:13 -0700175 void *data = NULL;
Kees Cookf7a4f682020-10-02 10:38:17 -0700176 size_t size;
Nayna Jain60740ac2018-12-09 01:57:00 +0530177 int rc;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700178 key_perm_t perm;
Nayna Jain60740ac2018-12-09 01:57:00 +0530179
Kees Cook0fa8e082020-10-02 10:38:25 -0700180 rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL,
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200181 READING_X509_CERTIFICATE);
182 if (rc < 0) {
183 pr_err("Unable to open file: %s (%d)", path, rc);
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200184 return rc;
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200185 }
Kees Cookf7a4f682020-10-02 10:38:17 -0700186 size = rc;
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200187
Linus Torvalds028db3e2019-07-10 18:43:43 -0700188 perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ;
189
Nayna Jain60740ac2018-12-09 01:57:00 +0530190 pr_info("Loading X.509 certificate: %s\n", path);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700191 rc = integrity_add_key(id, (const void *)data, size, perm);
Nayna Jain60740ac2018-12-09 01:57:00 +0530192
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200193 vfree(data);
Nayna Jain60740ac2018-12-09 01:57:00 +0530194 return rc;
195}
196
197int __init integrity_load_cert(const unsigned int id, const char *source,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700198 const void *data, size_t len, key_perm_t perm)
Nayna Jain60740ac2018-12-09 01:57:00 +0530199{
200 if (!data)
201 return -EINVAL;
202
203 pr_info("Loading X.509 certificate: %s\n", source);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700204 return integrity_add_key(id, data, len, perm);
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200205}