2 * Copyright (C) 2005-2010 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
6 * Kylene Hall <kjhall@us.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2 of the License.
13 * Using root's kernel master key (kmk), calculate the HMAC
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/crypto.h>
20 #include <linux/xattr.h>
21 #include <linux/evm.h>
22 #include <keys/encrypted-type.h>
23 #include <crypto/hash.h>
26 #define EVMKEY "evm-key"
27 #define MAX_KEY_SIZE 128
28 static unsigned char evmkey[MAX_KEY_SIZE];
29 static int evmkey_len = MAX_KEY_SIZE;
31 struct crypto_shash *hmac_tfm;
32 struct crypto_shash *hash_tfm;
34 static DEFINE_MUTEX(mutex);
36 #define EVM_SET_KEY_BUSY 0
38 static unsigned long evm_set_key_flags;
40 static char * const evm_hmac = "hmac(sha1)";
41 static char * const evm_hash = "sha1";
44 * evm_set_key() - set EVM HMAC key from the kernel
45 * @key: pointer to a buffer with the key data
46 * @size: length of the key data
48 * This function allows setting the EVM HMAC key from the kernel
49 * without using the "encrypted" key subsystem keys. It can be used
50 * by the crypto HW kernel module which has its own way of managing
53 * key length should be between 32 and 128 bytes long
55 int evm_set_key(void *key, size_t keylen)
60 if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
63 if (keylen > MAX_KEY_SIZE)
65 memcpy(evmkey, key, keylen);
66 evm_initialized |= EVM_INIT_HMAC;
67 pr_info("key initialized\n");
70 clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
72 pr_err("key initialization failed\n");
75 EXPORT_SYMBOL_GPL(evm_set_key);
77 static struct shash_desc *init_desc(char type)
81 struct crypto_shash **tfm;
82 struct shash_desc *desc;
84 if (type == EVM_XATTR_HMAC) {
85 if (!(evm_initialized & EVM_INIT_HMAC)) {
86 pr_err_once("HMAC key is not set\n");
87 return ERR_PTR(-ENOKEY);
100 *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
103 pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
105 mutex_unlock(&mutex);
108 if (type == EVM_XATTR_HMAC) {
109 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
111 crypto_free_shash(*tfm);
113 mutex_unlock(&mutex);
118 mutex_unlock(&mutex);
121 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
124 return ERR_PTR(-ENOMEM);
127 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
129 rc = crypto_shash_init(desc);
137 /* Protect against 'cutting & pasting' security.evm xattr, include inode
140 * (Additional directory/file metadata needs to be added for more complete
143 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
144 char type, char *digest)
154 memset(&hmac_misc, 0, sizeof(hmac_misc));
155 /* Don't include the inode or generation number in portable
158 if (type != EVM_XATTR_PORTABLE_DIGSIG) {
159 hmac_misc.ino = inode->i_ino;
160 hmac_misc.generation = inode->i_generation;
162 /* The hmac uid and gid must be encoded in the initial user
163 * namespace (not the filesystems user namespace) as encoding
164 * them in the filesystems user namespace allows an attack
165 * where first they are written in an unprivileged fuse mount
166 * of a filesystem and then the system is tricked to mount the
167 * filesystem for real on next boot and trust it because
168 * everything is signed.
170 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
171 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
172 hmac_misc.mode = inode->i_mode;
173 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
174 if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
175 type != EVM_XATTR_PORTABLE_DIGSIG)
176 crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0],
177 sizeof(inode->i_sb->s_uuid));
178 crypto_shash_final(desc, digest);
182 * Calculate the HMAC value across the set of protected security xattrs.
184 * Instead of retrieving the requested xattr, for performance, calculate
185 * the hmac using the requested xattr value. Don't alloc/free memory for
186 * each xattr, but attempt to re-use the previously allocated memory.
188 static int evm_calc_hmac_or_hash(struct dentry *dentry,
189 const char *req_xattr_name,
190 const char *req_xattr_value,
191 size_t req_xattr_value_len,
192 char type, char *digest)
194 struct inode *inode = d_backing_inode(dentry);
195 struct xattr_list *xattr;
196 struct shash_desc *desc;
197 size_t xattr_size = 0;
198 char *xattr_value = NULL;
201 bool ima_present = false;
203 if (!(inode->i_opflags & IOP_XATTR) ||
204 inode->i_sb->s_user_ns != &init_user_ns)
207 desc = init_desc(type);
209 return PTR_ERR(desc);
212 list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
215 if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
218 if ((req_xattr_name && req_xattr_value)
219 && !strcmp(xattr->name, req_xattr_name)) {
221 crypto_shash_update(desc, (const u8 *)req_xattr_value,
222 req_xattr_value_len);
227 size = vfs_getxattr_alloc(dentry, xattr->name,
228 &xattr_value, xattr_size, GFP_NOFS);
229 if (size == -ENOMEM) {
238 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
242 hmac_add_misc(desc, inode, type, digest);
244 /* Portable EVM signatures must include an IMA hash */
245 if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
253 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
254 const char *req_xattr_value, size_t req_xattr_value_len,
257 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
258 req_xattr_value_len, EVM_XATTR_HMAC, digest);
261 int 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, char *digest)
265 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
266 req_xattr_value_len, type, digest);
269 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
271 const struct evm_ima_xattr_data *xattr_data = NULL;
272 struct integrity_iint_cache *iint;
275 iint = integrity_iint_find(inode);
276 if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
279 /* Do this the hard way */
280 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
287 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
298 * Calculate the hmac and update security.evm xattr
300 * Expects to be called with i_mutex locked.
302 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
303 const char *xattr_value, size_t xattr_value_len)
305 struct inode *inode = d_backing_inode(dentry);
306 struct evm_ima_xattr_data xattr_data;
310 * Don't permit any transformation of the EVM xattr if the signature
311 * is of an immutable type
313 rc = evm_is_immutable(dentry, inode);
319 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
320 xattr_value_len, xattr_data.digest);
322 xattr_data.type = EVM_XATTR_HMAC;
323 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
325 sizeof(xattr_data), 0);
326 } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
327 rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
332 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
335 struct shash_desc *desc;
337 desc = init_desc(EVM_XATTR_HMAC);
339 pr_info("init_desc failed\n");
340 return PTR_ERR(desc);
343 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
344 hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
350 * Get the key from the TPM for the SHA1-HMAC
352 int evm_init_key(void)
355 struct encrypted_key_payload *ekp;
358 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
362 down_read(&evm_key->sem);
363 ekp = evm_key->payload.data[0];
365 rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
367 /* burn the original key contents */
368 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
369 up_read(&evm_key->sem);