]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - security/integrity/evm/evm_crypto.c
Linux 5.11-rc3
[mirror_ubuntu-jammy-kernel.git] / security / integrity / evm / evm_crypto.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
66dbc325
MZ
2/*
3 * Copyright (C) 2005-2010 IBM Corporation
4 *
5 * Authors:
6 * Mimi Zohar <zohar@us.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
66dbc325
MZ
9 * File: evm_crypto.c
10 * Using root's kernel master key (kmk), calculate the HMAC
11 */
12
876979c9 13#include <linux/export.h>
66dbc325
MZ
14#include <linux/crypto.h>
15#include <linux/xattr.h>
76266763 16#include <linux/evm.h>
66dbc325 17#include <keys/encrypted-type.h>
d46eb369 18#include <crypto/hash.h>
5feeb611 19#include <crypto/hash_info.h>
66dbc325
MZ
20#include "evm.h"
21
22#define EVMKEY "evm-key"
23#define MAX_KEY_SIZE 128
24static unsigned char evmkey[MAX_KEY_SIZE];
b2724d58 25static const int evmkey_len = MAX_KEY_SIZE;
66dbc325 26
d46eb369 27struct crypto_shash *hmac_tfm;
5feeb611 28static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
d46eb369 29
97426f98
DK
30static DEFINE_MUTEX(mutex);
31
76266763
DK
32#define EVM_SET_KEY_BUSY 0
33
34static unsigned long evm_set_key_flags;
35
b2724d58 36static const char evm_hmac[] = "hmac(sha1)";
1a82cee3 37
76266763
DK
38/**
39 * evm_set_key() - set EVM HMAC key from the kernel
40 * @key: pointer to a buffer with the key data
41 * @size: length of the key data
42 *
43 * This function allows setting the EVM HMAC key from the kernel
44 * without using the "encrypted" key subsystem keys. It can be used
45 * by the crypto HW kernel module which has its own way of managing
46 * keys.
47 *
48 * key length should be between 32 and 128 bytes long
49 */
50int evm_set_key(void *key, size_t keylen)
51{
52 int rc;
53
54 rc = -EBUSY;
55 if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
56 goto busy;
57 rc = -EINVAL;
58 if (keylen > MAX_KEY_SIZE)
59 goto inval;
60 memcpy(evmkey, key, keylen);
61 evm_initialized |= EVM_INIT_HMAC;
62 pr_info("key initialized\n");
63 return 0;
64inval:
65 clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
66busy:
67 pr_err("key initialization failed\n");
68 return rc;
69}
70EXPORT_SYMBOL_GPL(evm_set_key);
71
5feeb611 72static struct shash_desc *init_desc(char type, uint8_t hash_algo)
66dbc325 73{
143b01d3 74 long rc;
5feeb611 75 const char *algo;
84338569 76 struct crypto_shash **tfm, *tmp_tfm;
d46eb369
DK
77 struct shash_desc *desc;
78
15647eb3 79 if (type == EVM_XATTR_HMAC) {
26ddabfe 80 if (!(evm_initialized & EVM_INIT_HMAC)) {
0485d066 81 pr_err_once("HMAC key is not set\n");
26ddabfe
DK
82 return ERR_PTR(-ENOKEY);
83 }
15647eb3
DK
84 tfm = &hmac_tfm;
85 algo = evm_hmac;
86 } else {
221be106
RS
87 if (hash_algo >= HASH_ALGO__LAST)
88 return ERR_PTR(-EINVAL);
89
5feeb611
MG
90 tfm = &evm_tfm[hash_algo];
91 algo = hash_algo_name[hash_algo];
15647eb3
DK
92 }
93
84338569
DC
94 if (*tfm)
95 goto alloc;
96 mutex_lock(&mutex);
97 if (*tfm)
98 goto unlock;
99
100 tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
101 if (IS_ERR(tmp_tfm)) {
102 pr_err("Can not allocate %s (reason: %ld)\n", algo,
103 PTR_ERR(tmp_tfm));
104 mutex_unlock(&mutex);
105 return ERR_CAST(tmp_tfm);
106 }
107 if (type == EVM_XATTR_HMAC) {
108 rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
109 if (rc) {
110 crypto_free_shash(tmp_tfm);
97426f98 111 mutex_unlock(&mutex);
d46eb369
DK
112 return ERR_PTR(rc);
113 }
66dbc325 114 }
84338569
DC
115 *tfm = tmp_tfm;
116unlock:
117 mutex_unlock(&mutex);
118alloc:
15647eb3 119 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
d46eb369
DK
120 GFP_KERNEL);
121 if (!desc)
122 return ERR_PTR(-ENOMEM);
123
15647eb3 124 desc->tfm = *tfm;
d46eb369 125
d46eb369 126 rc = crypto_shash_init(desc);
d46eb369
DK
127 if (rc) {
128 kfree(desc);
129 return ERR_PTR(rc);
130 }
131 return desc;
66dbc325
MZ
132}
133
134/* Protect against 'cutting & pasting' security.evm xattr, include inode
135 * specific info.
136 *
137 * (Additional directory/file metadata needs to be added for more complete
138 * protection.)
139 */
d46eb369 140static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
50b97748 141 char type, char *digest)
66dbc325
MZ
142{
143 struct h_misc {
144 unsigned long ino;
145 __u32 generation;
146 uid_t uid;
147 gid_t gid;
148 umode_t mode;
149 } hmac_misc;
66dbc325 150
2bb930ab 151 memset(&hmac_misc, 0, sizeof(hmac_misc));
50b97748
MG
152 /* Don't include the inode or generation number in portable
153 * signatures
154 */
155 if (type != EVM_XATTR_PORTABLE_DIGSIG) {
156 hmac_misc.ino = inode->i_ino;
157 hmac_misc.generation = inode->i_generation;
158 }
19339c25
EB
159 /* The hmac uid and gid must be encoded in the initial user
160 * namespace (not the filesystems user namespace) as encoding
161 * them in the filesystems user namespace allows an attack
162 * where first they are written in an unprivileged fuse mount
163 * of a filesystem and then the system is tricked to mount the
164 * filesystem for real on next boot and trust it because
165 * everything is signed.
166 */
167 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
168 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
66dbc325 169 hmac_misc.mode = inode->i_mode;
2bb930ab 170 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
50b97748
MG
171 if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
172 type != EVM_XATTR_PORTABLE_DIGSIG)
e7fde070 173 crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
d46eb369 174 crypto_shash_final(desc, digest);
66dbc325
MZ
175}
176
177/*
178 * Calculate the HMAC value across the set of protected security xattrs.
179 *
180 * Instead of retrieving the requested xattr, for performance, calculate
181 * the hmac using the requested xattr value. Don't alloc/free memory for
182 * each xattr, but attempt to re-use the previously allocated memory.
183 */
15647eb3 184static int evm_calc_hmac_or_hash(struct dentry *dentry,
5feeb611
MG
185 const char *req_xattr_name,
186 const char *req_xattr_value,
187 size_t req_xattr_value_len,
188 uint8_t type, struct evm_digest *data)
66dbc325 189{
c6f493d6 190 struct inode *inode = d_backing_inode(dentry);
21af7663 191 struct xattr_list *xattr;
d46eb369 192 struct shash_desc *desc;
66dbc325
MZ
193 size_t xattr_size = 0;
194 char *xattr_value = NULL;
195 int error;
196 int size;
50b97748 197 bool ima_present = false;
66dbc325 198
a3a5c966
SF
199 if (!(inode->i_opflags & IOP_XATTR) ||
200 inode->i_sb->s_user_ns != &init_user_ns)
66dbc325 201 return -EOPNOTSUPP;
5d6c3191 202
5feeb611 203 desc = init_desc(type, data->hdr.algo);
d46eb369
DK
204 if (IS_ERR(desc))
205 return PTR_ERR(desc);
66dbc325 206
5feeb611
MG
207 data->hdr.length = crypto_shash_digestsize(desc->tfm);
208
66dbc325 209 error = -ENODATA;
770f6058 210 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
50b97748
MG
211 bool is_ima = false;
212
21af7663 213 if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
50b97748
MG
214 is_ima = true;
215
66dbc325 216 if ((req_xattr_name && req_xattr_value)
21af7663 217 && !strcmp(xattr->name, req_xattr_name)) {
66dbc325 218 error = 0;
d46eb369
DK
219 crypto_shash_update(desc, (const u8 *)req_xattr_value,
220 req_xattr_value_len);
50b97748
MG
221 if (is_ima)
222 ima_present = true;
66dbc325
MZ
223 continue;
224 }
21af7663 225 size = vfs_getxattr_alloc(dentry, xattr->name,
66dbc325
MZ
226 &xattr_value, xattr_size, GFP_NOFS);
227 if (size == -ENOMEM) {
228 error = -ENOMEM;
229 goto out;
230 }
231 if (size < 0)
232 continue;
233
234 error = 0;
235 xattr_size = size;
d46eb369 236 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
50b97748
MG
237 if (is_ima)
238 ima_present = true;
66dbc325 239 }
5feeb611 240 hmac_add_misc(desc, inode, type, data->digest);
d46eb369 241
50b97748
MG
242 /* Portable EVM signatures must include an IMA hash */
243 if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
0c4395fb 244 error = -EPERM;
66dbc325 245out:
d46eb369
DK
246 kfree(xattr_value);
247 kfree(desc);
66dbc325
MZ
248 return error;
249}
250
15647eb3
DK
251int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
252 const char *req_xattr_value, size_t req_xattr_value_len,
5feeb611 253 struct evm_digest *data)
15647eb3
DK
254{
255 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
5feeb611 256 req_xattr_value_len, EVM_XATTR_HMAC, data);
15647eb3
DK
257}
258
259int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
260 const char *req_xattr_value, size_t req_xattr_value_len,
5feeb611 261 char type, struct evm_digest *data)
15647eb3
DK
262{
263 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
5feeb611 264 req_xattr_value_len, type, data);
50b97748
MG
265}
266
267static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
268{
269 const struct evm_ima_xattr_data *xattr_data = NULL;
270 struct integrity_iint_cache *iint;
271 int rc = 0;
272
273 iint = integrity_iint_find(inode);
274 if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
275 return 1;
276
277 /* Do this the hard way */
278 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
279 GFP_NOFS);
280 if (rc <= 0) {
281 if (rc == -ENODATA)
282 return 0;
283 return rc;
284 }
285 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
286 rc = 1;
287 else
288 rc = 0;
289
290 kfree(xattr_data);
291 return rc;
15647eb3
DK
292}
293
50b97748 294
66dbc325
MZ
295/*
296 * Calculate the hmac and update security.evm xattr
297 *
298 * Expects to be called with i_mutex locked.
299 */
300int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
301 const char *xattr_value, size_t xattr_value_len)
302{
c6f493d6 303 struct inode *inode = d_backing_inode(dentry);
5feeb611 304 struct evm_digest data;
66dbc325
MZ
305 int rc = 0;
306
50b97748
MG
307 /*
308 * Don't permit any transformation of the EVM xattr if the signature
309 * is of an immutable type
310 */
311 rc = evm_is_immutable(dentry, inode);
312 if (rc < 0)
313 return rc;
314 if (rc)
315 return -EPERM;
316
5feeb611 317 data.hdr.algo = HASH_ALGO_SHA1;
66dbc325 318 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
5feeb611 319 xattr_value_len, &data);
6be5cc52 320 if (rc == 0) {
5feeb611 321 data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
66dbc325 322 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
5feeb611
MG
323 &data.hdr.xattr.data[1],
324 SHA1_DIGEST_SIZE + 1, 0);
5d6c3191
AG
325 } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
326 rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
a67adb99 327 }
66dbc325
MZ
328 return rc;
329}
330
cb723180
MZ
331int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
332 char *hmac_val)
333{
d46eb369 334 struct shash_desc *desc;
cb723180 335
5feeb611 336 desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
d46eb369 337 if (IS_ERR(desc)) {
20ee451f 338 pr_info("init_desc failed\n");
d46eb369 339 return PTR_ERR(desc);
cb723180
MZ
340 }
341
d46eb369 342 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
50b97748 343 hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
d46eb369 344 kfree(desc);
cb723180
MZ
345 return 0;
346}
347
66dbc325
MZ
348/*
349 * Get the key from the TPM for the SHA1-HMAC
350 */
351int evm_init_key(void)
352{
353 struct key *evm_key;
354 struct encrypted_key_payload *ekp;
76266763 355 int rc;
66dbc325 356
028db3e2 357 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
66dbc325
MZ
358 if (IS_ERR(evm_key))
359 return -ENOENT;
360
361 down_read(&evm_key->sem);
146aa8b1 362 ekp = evm_key->payload.data[0];
76266763
DK
363
364 rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
365
66dbc325
MZ
366 /* burn the original key contents */
367 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
368 up_read(&evm_key->sem);
369 key_put(evm_key);
370 return rc;
371}