blob: aa93b750a9f32d9f6cdf8cc877187c1ea4152bb8 [file] [log] [blame]
Thomas Gleixnerb886d832019-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;
GUO Zihua29d6c692022-11-11 18:13:17 +0800125 int ret;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700126
127 perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW
128 | KEY_USR_READ | KEY_USR_SEARCH;
Nayna Jain9dc92c42018-12-09 01:56:59 +0530129
130 if (id == INTEGRITY_KEYRING_PLATFORM) {
131 restriction = NULL;
132 goto out;
133 }
134
Eric Biggers2ab5daf2018-10-03 17:15:44 -0700135 if (!IS_ENABLED(CONFIG_INTEGRITY_TRUSTED_KEYRING))
Dmitry Kasatkinf4dc3772015-10-22 21:26:10 +0300136 return 0;
137
Mat Martineau2b6aa412016-08-31 16:05:43 -0700138 restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
139 if (!restriction)
140 return -ENOMEM;
141
142 restriction->check = restrict_link_to_ima;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700143 perm |= KEY_USR_WRITE;
Mat Martineau2b6aa412016-08-31 16:05:43 -0700144
Nayna Jain9dc92c42018-12-09 01:56:59 +0530145out:
GUO Zihua29d6c692022-11-11 18:13:17 +0800146 ret = __integrity_init_keyring(id, perm, restriction);
147 if (ret)
148 kfree(restriction);
149 return ret;
Mimi Zohar7d2ce232013-08-13 08:47:43 -0400150}
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200151
Wei Yongjunf6692212021-02-10 08:01:31 +0000152static int __init integrity_add_key(const unsigned int id, const void *data,
153 off_t size, key_perm_t perm)
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200154{
155 key_ref_t key;
Nayna Jain60740ac2018-12-09 01:57:00 +0530156 int rc = 0;
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200157
158 if (!keyring[id])
159 return -EINVAL;
160
Nayna Jain60740ac2018-12-09 01:57:00 +0530161 key = key_create_or_update(make_key_ref(keyring[id], 1), "asymmetric",
Linus Torvalds028db3e2019-07-10 18:43:43 -0700162 NULL, data, size, perm,
Nayna Jain60740ac2018-12-09 01:57:00 +0530163 KEY_ALLOC_NOT_IN_QUOTA);
164 if (IS_ERR(key)) {
165 rc = PTR_ERR(key);
166 pr_err("Problem loading X.509 certificate %d\n", rc);
167 } else {
168 pr_notice("Loaded X.509 cert '%s'\n",
169 key_ref_to_ptr(key)->description);
170 key_ref_put(key);
171 }
172
173 return rc;
174
175}
176
177int __init integrity_load_x509(const unsigned int id, const char *path)
178{
Kees Cookc3074592020-10-02 10:38:13 -0700179 void *data = NULL;
Kees Cookf7a4f682020-10-02 10:38:17 -0700180 size_t size;
Nayna Jain60740ac2018-12-09 01:57:00 +0530181 int rc;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700182 key_perm_t perm;
Nayna Jain60740ac2018-12-09 01:57:00 +0530183
Kees Cook0fa8e082020-10-02 10:38:25 -0700184 rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL,
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200185 READING_X509_CERTIFICATE);
186 if (rc < 0) {
187 pr_err("Unable to open file: %s (%d)", path, rc);
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200188 return rc;
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200189 }
Kees Cookf7a4f682020-10-02 10:38:17 -0700190 size = rc;
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200191
Linus Torvalds028db3e2019-07-10 18:43:43 -0700192 perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ;
193
Nayna Jain60740ac2018-12-09 01:57:00 +0530194 pr_info("Loading X.509 certificate: %s\n", path);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700195 rc = integrity_add_key(id, (const void *)data, size, perm);
Nayna Jain60740ac2018-12-09 01:57:00 +0530196
Christoph Hellwiga7d3d032017-09-10 09:49:45 +0200197 vfree(data);
Nayna Jain60740ac2018-12-09 01:57:00 +0530198 return rc;
199}
200
201int __init integrity_load_cert(const unsigned int id, const char *source,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700202 const void *data, size_t len, key_perm_t perm)
Nayna Jain60740ac2018-12-09 01:57:00 +0530203{
204 if (!data)
205 return -EINVAL;
206
207 pr_info("Loading X.509 certificate: %s\n", source);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700208 return integrity_add_key(id, data, len, perm);
Dmitry Kasatkin65d543b2014-11-05 17:01:13 +0200209}