Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  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 *
  9 * 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
 15#include <linux/export.h>
 16#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];
 27static const int evmkey_len = MAX_KEY_SIZE;
 28
 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
 38static const char evm_hmac[] = "hmac(sha1)";
 39
 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;
 78	struct crypto_shash **tfm;
 79	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 {
 89		if (hash_algo >= HASH_ALGO__LAST)
 90			return ERR_PTR(-EINVAL);
 91
 92		tfm = &evm_tfm[hash_algo];
 93		algo = hash_algo_name[hash_algo];
 94	}
 95
 96	if (*tfm == NULL) {
 97		mutex_lock(&mutex);
 98		if (*tfm)
 99			goto out;
100		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
101		if (IS_ERR(*tfm)) {
102			rc = PTR_ERR(*tfm);
103			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
104			*tfm = NULL;
 
 
 
 
 
 
 
 
105			mutex_unlock(&mutex);
106			return ERR_PTR(rc);
107		}
108		if (type == EVM_XATTR_HMAC) {
109			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
110			if (rc) {
111				crypto_free_shash(*tfm);
112				*tfm = NULL;
113				mutex_unlock(&mutex);
114				return ERR_PTR(rc);
115			}
116		}
117out:
118		mutex_unlock(&mutex);
119	}
120
 
 
 
121	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
122			GFP_KERNEL);
123	if (!desc)
 
124		return ERR_PTR(-ENOMEM);
 
125
126	desc->tfm = *tfm;
127
128	rc = crypto_shash_init(desc);
129	if (rc) {
 
130		kfree(desc);
131		return ERR_PTR(rc);
132	}
133	return desc;
134}
135
136/* Protect against 'cutting & pasting' security.evm xattr, include inode
137 * specific info.
138 *
139 * (Additional directory/file metadata needs to be added for more complete
140 * protection.)
141 */
142static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
143			  char type, char *digest)
144{
145	struct h_misc {
146		unsigned long ino;
147		__u32 generation;
148		uid_t uid;
149		gid_t gid;
150		umode_t mode;
151	} hmac_misc;
152
153	memset(&hmac_misc, 0, sizeof(hmac_misc));
154	/* Don't include the inode or generation number in portable
155	 * signatures
156	 */
157	if (type != EVM_XATTR_PORTABLE_DIGSIG) {
158		hmac_misc.ino = inode->i_ino;
159		hmac_misc.generation = inode->i_generation;
160	}
161	/* The hmac uid and gid must be encoded in the initial user
162	 * namespace (not the filesystems user namespace) as encoding
163	 * them in the filesystems user namespace allows an attack
164	 * where first they are written in an unprivileged fuse mount
165	 * of a filesystem and then the system is tricked to mount the
166	 * filesystem for real on next boot and trust it because
167	 * everything is signed.
168	 */
169	hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
170	hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
171	hmac_misc.mode = inode->i_mode;
172	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
173	if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
174	    type != EVM_XATTR_PORTABLE_DIGSIG)
175		crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
176	crypto_shash_final(desc, digest);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177}
178
179/*
180 * Calculate the HMAC value across the set of protected security xattrs.
181 *
182 * Instead of retrieving the requested xattr, for performance, calculate
183 * the hmac using the requested xattr value. Don't alloc/free memory for
184 * each xattr, but attempt to re-use the previously allocated memory.
185 */
186static int evm_calc_hmac_or_hash(struct dentry *dentry,
187				 const char *req_xattr_name,
188				 const char *req_xattr_value,
189				 size_t req_xattr_value_len,
190				 uint8_t type, struct evm_digest *data)
191{
192	struct inode *inode = d_backing_inode(dentry);
193	struct xattr_list *xattr;
194	struct shash_desc *desc;
195	size_t xattr_size = 0;
196	char *xattr_value = NULL;
197	int error;
198	int size;
199	bool ima_present = false;
200
201	if (!(inode->i_opflags & IOP_XATTR) ||
202	    inode->i_sb->s_user_ns != &init_user_ns)
203		return -EOPNOTSUPP;
204
205	desc = init_desc(type, data->hdr.algo);
206	if (IS_ERR(desc))
207		return PTR_ERR(desc);
208
209	data->hdr.length = crypto_shash_digestsize(desc->tfm);
210
211	error = -ENODATA;
212	list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
213		bool is_ima = false;
214
215		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
216			is_ima = true;
217
 
 
 
 
 
 
 
218		if ((req_xattr_name && req_xattr_value)
219		    && !strcmp(xattr->name, req_xattr_name)) {
220			error = 0;
221			crypto_shash_update(desc, (const u8 *)req_xattr_value,
222					     req_xattr_value_len);
223			if (is_ima)
224				ima_present = true;
 
 
 
 
 
 
 
 
 
 
225			continue;
226		}
227		size = vfs_getxattr_alloc(dentry, xattr->name,
228					  &xattr_value, xattr_size, GFP_NOFS);
229		if (size == -ENOMEM) {
230			error = -ENOMEM;
231			goto out;
232		}
233		if (size < 0)
234			continue;
235
 
 
 
 
 
 
236		error = 0;
237		xattr_size = size;
238		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
239		if (is_ima)
240			ima_present = true;
 
 
 
 
 
 
 
241	}
242	hmac_add_misc(desc, inode, type, data->digest);
243
244	/* Portable EVM signatures must include an IMA hash */
245	if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
246		return -EPERM;
247out:
248	kfree(xattr_value);
249	kfree(desc);
250	return error;
251}
252
253int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
254		  const char *req_xattr_value, size_t req_xattr_value_len,
255		  struct evm_digest *data)
256{
257	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
258				    req_xattr_value_len, EVM_XATTR_HMAC, data);
259}
260
261int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
262		  const char *req_xattr_value, size_t req_xattr_value_len,
263		  char type, struct evm_digest *data)
264{
265	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
266				     req_xattr_value_len, type, data);
267}
268
269static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
270{
271	const struct evm_ima_xattr_data *xattr_data = NULL;
272	struct integrity_iint_cache *iint;
273	int rc = 0;
274
275	iint = integrity_iint_find(inode);
276	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
277		return 1;
278
279	/* Do this the hard way */
280	rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
281				GFP_NOFS);
282	if (rc <= 0) {
283		if (rc == -ENODATA)
284			return 0;
285		return rc;
286	}
287	if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
288		rc = 1;
289	else
290		rc = 0;
291
292	kfree(xattr_data);
293	return rc;
294}
295
296
297/*
298 * Calculate the hmac and update security.evm xattr
299 *
300 * Expects to be called with i_mutex locked.
301 */
302int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
303			const char *xattr_value, size_t xattr_value_len)
304{
305	struct inode *inode = d_backing_inode(dentry);
306	struct evm_digest data;
307	int rc = 0;
308
309	/*
310	 * Don't permit any transformation of the EVM xattr if the signature
311	 * is of an immutable type
312	 */
313	rc = evm_is_immutable(dentry, inode);
314	if (rc < 0)
315		return rc;
316	if (rc)
317		return -EPERM;
318
319	data.hdr.algo = HASH_ALGO_SHA1;
320	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
321			   xattr_value_len, &data);
322	if (rc == 0) {
323		data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
324		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
 
325					   &data.hdr.xattr.data[1],
326					   SHA1_DIGEST_SIZE + 1, 0);
327	} else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
328		rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
329	}
330	return rc;
331}
332
333int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
334		  char *hmac_val)
335{
336	struct shash_desc *desc;
337
338	desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
339	if (IS_ERR(desc)) {
340		pr_info("init_desc failed\n");
341		return PTR_ERR(desc);
342	}
343
344	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
345	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
346	kfree(desc);
347	return 0;
348}
349
350/*
351 * Get the key from the TPM for the SHA1-HMAC
352 */
353int evm_init_key(void)
354{
355	struct key *evm_key;
356	struct encrypted_key_payload *ekp;
357	int rc;
358
359	evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
360	if (IS_ERR(evm_key))
361		return -ENOENT;
362
363	down_read(&evm_key->sem);
364	ekp = evm_key->payload.data[0];
365
366	rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
367
368	/* burn the original key contents */
369	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
370	up_read(&evm_key->sem);
371	key_put(evm_key);
372	return rc;
373}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  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 *
  9 * File: evm_crypto.c
 10 *	 Using root's kernel master key (kmk), calculate the HMAC
 11 */
 12
 13#define pr_fmt(fmt) "EVM: "fmt
 14
 15#include <linux/export.h>
 16#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];
 27static const int evmkey_len = MAX_KEY_SIZE;
 28
 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
 38static const char evm_hmac[] = "hmac(sha1)";
 39
 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;
 78	struct crypto_shash **tfm, *tmp_tfm = NULL;
 79	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 {
 89		if (hash_algo >= HASH_ALGO__LAST)
 90			return ERR_PTR(-EINVAL);
 91
 92		tfm = &evm_tfm[hash_algo];
 93		algo = hash_algo_name[hash_algo];
 94	}
 95
 96	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);
113			mutex_unlock(&mutex);
114			return ERR_PTR(rc);
115		}
 
 
 
 
 
 
 
 
 
 
 
116	}
117	*tfm = tmp_tfm;
118unlock:
119	mutex_unlock(&mutex);
120alloc:
121	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
122			GFP_KERNEL);
123	if (!desc) {
124		crypto_free_shash(tmp_tfm);
125		return ERR_PTR(-ENOMEM);
126	}
127
128	desc->tfm = *tfm;
129
130	rc = crypto_shash_init(desc);
131	if (rc) {
132		crypto_free_shash(tmp_tfm);
133		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)
178		crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
179	crypto_shash_final(desc, digest);
180
181	pr_debug("hmac_misc: (%zu) [%*phN]\n", sizeof(struct h_misc),
182		 (int)sizeof(struct h_misc), &hmac_misc);
183}
184
185/*
186 * Dump large security xattr values as a continuous ascii hexademical string.
187 * (pr_debug is limited to 64 bytes.)
188 */
189static void dump_security_xattr(const char *prefix, const void *src,
190				size_t count)
191{
192#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
193	char *asciihex, *p;
194
195	p = asciihex = kmalloc(count * 2 + 1, GFP_KERNEL);
196	if (!asciihex)
197		return;
198
199	p = bin2hex(p, src, count);
200	*p = 0;
201	pr_debug("%s: (%zu) %.*s\n", prefix, count, (int)count * 2, asciihex);
202	kfree(asciihex);
203#endif
204}
205
206/*
207 * Calculate the HMAC value across the set of protected security xattrs.
208 *
209 * Instead of retrieving the requested xattr, for performance, calculate
210 * the hmac using the requested xattr value. Don't alloc/free memory for
211 * each xattr, but attempt to re-use the previously allocated memory.
212 */
213static int evm_calc_hmac_or_hash(struct dentry *dentry,
214				 const char *req_xattr_name,
215				 const char *req_xattr_value,
216				 size_t req_xattr_value_len,
217				 uint8_t type, struct evm_digest *data)
218{
219	struct inode *inode = d_backing_inode(dentry);
220	struct xattr_list *xattr;
221	struct shash_desc *desc;
222	size_t xattr_size = 0;
223	char *xattr_value = NULL;
224	int error;
225	int size, user_space_size;
226	bool ima_present = false;
227
228	if (!(inode->i_opflags & IOP_XATTR) ||
229	    inode->i_sb->s_user_ns != &init_user_ns)
230		return -EOPNOTSUPP;
231
232	desc = init_desc(type, data->hdr.algo);
233	if (IS_ERR(desc))
234		return PTR_ERR(desc);
235
236	data->hdr.length = crypto_shash_digestsize(desc->tfm);
237
238	error = -ENODATA;
239	list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
240		bool is_ima = false;
241
242		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
243			is_ima = true;
244
245		/*
246		 * Skip non-enabled xattrs for locally calculated
247		 * signatures/HMACs.
248		 */
249		if (type != EVM_XATTR_PORTABLE_DIGSIG && !xattr->enabled)
250			continue;
251
252		if ((req_xattr_name && req_xattr_value)
253		    && !strcmp(xattr->name, req_xattr_name)) {
254			error = 0;
255			crypto_shash_update(desc, (const u8 *)req_xattr_value,
256					     req_xattr_value_len);
257			if (is_ima)
258				ima_present = true;
259
260			if (req_xattr_value_len < 64)
261				pr_debug("%s: (%zu) [%*phN]\n", req_xattr_name,
262					 req_xattr_value_len,
263					 (int)req_xattr_value_len,
264					 req_xattr_value);
265			else
266				dump_security_xattr(req_xattr_name,
267						    req_xattr_value,
268						    req_xattr_value_len);
269			continue;
270		}
271		size = vfs_getxattr_alloc(&init_user_ns, dentry, xattr->name,
272					  &xattr_value, xattr_size, GFP_NOFS);
273		if (size == -ENOMEM) {
274			error = -ENOMEM;
275			goto out;
276		}
277		if (size < 0)
278			continue;
279
280		user_space_size = vfs_getxattr(&init_user_ns, dentry,
281					       xattr->name, NULL, 0);
282		if (user_space_size != size)
283			pr_debug("file %s: xattr %s size mismatch (kernel: %d, user: %d)\n",
284				 dentry->d_name.name, xattr->name, size,
285				 user_space_size);
286		error = 0;
287		xattr_size = size;
288		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
289		if (is_ima)
290			ima_present = true;
291
292		if (xattr_size < 64)
293			pr_debug("%s: (%zu) [%*phN]", xattr->name, xattr_size,
294				 (int)xattr_size, xattr_value);
295		else
296			dump_security_xattr(xattr->name, xattr_value,
297					    xattr_size);
298	}
299	hmac_add_misc(desc, inode, type, data->digest);
300
301	/* Portable EVM signatures must include an IMA hash */
302	if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
303		error = -EPERM;
304out:
305	kfree(xattr_value);
306	kfree(desc);
307	return error;
308}
309
310int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
311		  const char *req_xattr_value, size_t req_xattr_value_len,
312		  struct evm_digest *data)
313{
314	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
315				    req_xattr_value_len, EVM_XATTR_HMAC, data);
316}
317
318int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
319		  const char *req_xattr_value, size_t req_xattr_value_len,
320		  char type, struct evm_digest *data)
321{
322	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
323				     req_xattr_value_len, type, data);
324}
325
326static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
327{
328	const struct evm_ima_xattr_data *xattr_data = NULL;
329	struct integrity_iint_cache *iint;
330	int rc = 0;
331
332	iint = integrity_iint_find(inode);
333	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
334		return 1;
335
336	/* Do this the hard way */
337	rc = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_EVM,
338				(char **)&xattr_data, 0, GFP_NOFS);
339	if (rc <= 0) {
340		if (rc == -ENODATA)
341			return 0;
342		return rc;
343	}
344	if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
345		rc = 1;
346	else
347		rc = 0;
348
349	kfree(xattr_data);
350	return rc;
351}
352
353
354/*
355 * Calculate the hmac and update security.evm xattr
356 *
357 * Expects to be called with i_mutex locked.
358 */
359int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
360			const char *xattr_value, size_t xattr_value_len)
361{
362	struct inode *inode = d_backing_inode(dentry);
363	struct evm_digest data;
364	int rc = 0;
365
366	/*
367	 * Don't permit any transformation of the EVM xattr if the signature
368	 * is of an immutable type
369	 */
370	rc = evm_is_immutable(dentry, inode);
371	if (rc < 0)
372		return rc;
373	if (rc)
374		return -EPERM;
375
376	data.hdr.algo = HASH_ALGO_SHA1;
377	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
378			   xattr_value_len, &data);
379	if (rc == 0) {
380		data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
381		rc = __vfs_setxattr_noperm(&init_user_ns, dentry,
382					   XATTR_NAME_EVM,
383					   &data.hdr.xattr.data[1],
384					   SHA1_DIGEST_SIZE + 1, 0);
385	} else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
386		rc = __vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_EVM);
387	}
388	return rc;
389}
390
391int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
392		  char *hmac_val)
393{
394	struct shash_desc *desc;
395
396	desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
397	if (IS_ERR(desc)) {
398		pr_info("init_desc failed\n");
399		return PTR_ERR(desc);
400	}
401
402	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
403	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
404	kfree(desc);
405	return 0;
406}
407
408/*
409 * Get the key from the TPM for the SHA1-HMAC
410 */
411int evm_init_key(void)
412{
413	struct key *evm_key;
414	struct encrypted_key_payload *ekp;
415	int rc;
416
417	evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
418	if (IS_ERR(evm_key))
419		return -ENOENT;
420
421	down_read(&evm_key->sem);
422	ekp = evm_key->payload.data[0];
423
424	rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
425
426	/* burn the original key contents */
427	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
428	up_read(&evm_key->sem);
429	key_put(evm_key);
430	return rc;
431}