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