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 #include <linux/module.h>
17 #include <linux/crypto.h>
18 #include <linux/xattr.h>
19 #include <keys/encrypted-type.h>
20 #include <crypto/hash.h>
23 #define EVMKEY "evm-key"
24 #define MAX_KEY_SIZE 128
25 static unsigned char evmkey[MAX_KEY_SIZE];
26 static int evmkey_len = MAX_KEY_SIZE;
28 struct crypto_shash *hmac_tfm;
30 static DEFINE_MUTEX(mutex);
32 static struct shash_desc *init_desc(void)
35 struct shash_desc *desc;
37 if (hmac_tfm == NULL) {
41 hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
42 if (IS_ERR(hmac_tfm)) {
43 pr_err("Can not allocate %s (reason: %ld)\n",
44 evm_hmac, PTR_ERR(hmac_tfm));
45 rc = PTR_ERR(hmac_tfm);
50 rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
52 crypto_free_shash(hmac_tfm);
61 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
64 return ERR_PTR(-ENOMEM);
67 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
69 rc = crypto_shash_init(desc);
77 /* Protect against 'cutting & pasting' security.evm xattr, include inode
80 * (Additional directory/file metadata needs to be added for more complete
83 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
94 memset(&hmac_misc, 0, sizeof hmac_misc);
95 hmac_misc.ino = inode->i_ino;
96 hmac_misc.generation = inode->i_generation;
97 hmac_misc.uid = inode->i_uid;
98 hmac_misc.gid = inode->i_gid;
99 hmac_misc.mode = inode->i_mode;
100 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
101 crypto_shash_final(desc, digest);
105 * Calculate the HMAC value across the set of protected security xattrs.
107 * Instead of retrieving the requested xattr, for performance, calculate
108 * the hmac using the requested xattr value. Don't alloc/free memory for
109 * each xattr, but attempt to re-use the previously allocated memory.
111 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
112 const char *req_xattr_value, size_t req_xattr_value_len,
115 struct inode *inode = dentry->d_inode;
116 struct shash_desc *desc;
118 size_t xattr_size = 0;
119 char *xattr_value = NULL;
123 if (!inode->i_op || !inode->i_op->getxattr)
127 return PTR_ERR(desc);
130 for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
131 if ((req_xattr_name && req_xattr_value)
132 && !strcmp(*xattrname, req_xattr_name)) {
134 crypto_shash_update(desc, (const u8 *)req_xattr_value,
135 req_xattr_value_len);
138 size = vfs_getxattr_alloc(dentry, *xattrname,
139 &xattr_value, xattr_size, GFP_NOFS);
140 if (size == -ENOMEM) {
149 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
151 hmac_add_misc(desc, inode, digest);
160 * Calculate the hmac and update security.evm xattr
162 * Expects to be called with i_mutex locked.
164 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
165 const char *xattr_value, size_t xattr_value_len)
167 struct inode *inode = dentry->d_inode;
168 struct evm_ima_xattr_data xattr_data;
171 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
172 xattr_value_len, xattr_data.digest);
174 xattr_data.type = EVM_XATTR_HMAC;
175 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
177 sizeof(xattr_data), 0);
179 else if (rc == -ENODATA)
180 rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
184 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
187 struct shash_desc *desc;
191 printk(KERN_INFO "init_desc failed\n");
192 return PTR_ERR(desc);
195 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
196 hmac_add_misc(desc, inode, hmac_val);
202 * Get the key from the TPM for the SHA1-HMAC
204 int evm_init_key(void)
207 struct encrypted_key_payload *ekp;
210 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
214 down_read(&evm_key->sem);
215 ekp = evm_key->payload.data;
216 if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
220 memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
222 /* burn the original key contents */
223 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
224 up_read(&evm_key->sem);