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
;
29 struct crypto_shash
*hash_tfm
;
31 static struct shash_desc
*init_desc(const char type
)
35 struct crypto_shash
**tfm
;
36 struct shash_desc
*desc
;
38 if (type
== EVM_XATTR_HMAC
) {
47 *tfm
= crypto_alloc_shash(algo
, 0, CRYPTO_ALG_ASYNC
);
49 pr_err("Can not allocate %s (reason: %ld)\n",
57 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(*tfm
),
60 return ERR_PTR(-ENOMEM
);
63 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
65 if (type
== EVM_XATTR_HMAC
) {
66 rc
= crypto_shash_setkey(*tfm
, evmkey
, evmkey_len
);
71 rc
= crypto_shash_init(desc
);
80 /* Protect against 'cutting & pasting' security.evm xattr, include inode
83 * (Additional directory/file metadata needs to be added for more complete
86 static void hmac_add_misc(struct shash_desc
*desc
, struct inode
*inode
,
97 memset(&hmac_misc
, 0, sizeof hmac_misc
);
98 hmac_misc
.ino
= inode
->i_ino
;
99 hmac_misc
.generation
= inode
->i_generation
;
100 hmac_misc
.uid
= inode
->i_uid
;
101 hmac_misc
.gid
= inode
->i_gid
;
102 hmac_misc
.mode
= inode
->i_mode
;
103 crypto_shash_update(desc
, (const u8
*)&hmac_misc
, sizeof hmac_misc
);
104 crypto_shash_final(desc
, digest
);
108 * Calculate the HMAC value across the set of protected security xattrs.
110 * Instead of retrieving the requested xattr, for performance, calculate
111 * the hmac using the requested xattr value. Don't alloc/free memory for
112 * each xattr, but attempt to re-use the previously allocated memory.
114 static int evm_calc_hmac_or_hash(struct dentry
*dentry
,
115 const char *req_xattr_name
,
116 const char *req_xattr_value
,
117 size_t req_xattr_value_len
,
118 char type
, char *digest
)
120 struct inode
*inode
= dentry
->d_inode
;
121 struct shash_desc
*desc
;
123 size_t xattr_size
= 0;
124 char *xattr_value
= NULL
;
128 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
130 desc
= init_desc(type
);
132 return PTR_ERR(desc
);
135 for (xattrname
= evm_config_xattrnames
; *xattrname
!= NULL
; xattrname
++) {
136 if ((req_xattr_name
&& req_xattr_value
)
137 && !strcmp(*xattrname
, req_xattr_name
)) {
139 crypto_shash_update(desc
, (const u8
*)req_xattr_value
,
140 req_xattr_value_len
);
143 size
= vfs_getxattr_alloc(dentry
, *xattrname
,
144 &xattr_value
, xattr_size
, GFP_NOFS
);
145 if (size
== -ENOMEM
) {
154 crypto_shash_update(desc
, (const u8
*)xattr_value
, xattr_size
);
156 hmac_add_misc(desc
, inode
, digest
);
164 int evm_calc_hmac(struct dentry
*dentry
, const char *req_xattr_name
,
165 const char *req_xattr_value
, size_t req_xattr_value_len
,
168 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
169 req_xattr_value_len
, EVM_XATTR_HMAC
, digest
);
172 int evm_calc_hash(struct dentry
*dentry
, const char *req_xattr_name
,
173 const char *req_xattr_value
, size_t req_xattr_value_len
,
176 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
177 req_xattr_value_len
, IMA_XATTR_DIGEST
, digest
);
181 * Calculate the hmac and update security.evm xattr
183 * Expects to be called with i_mutex locked.
185 int evm_update_evmxattr(struct dentry
*dentry
, const char *xattr_name
,
186 const char *xattr_value
, size_t xattr_value_len
)
188 struct inode
*inode
= dentry
->d_inode
;
189 struct evm_ima_xattr_data xattr_data
;
192 rc
= evm_calc_hmac(dentry
, xattr_name
, xattr_value
,
193 xattr_value_len
, xattr_data
.digest
);
195 xattr_data
.type
= EVM_XATTR_HMAC
;
196 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_EVM
,
198 sizeof(xattr_data
), 0);
200 else if (rc
== -ENODATA
)
201 rc
= inode
->i_op
->removexattr(dentry
, XATTR_NAME_EVM
);
205 int evm_init_hmac(struct inode
*inode
, const struct xattr
*lsm_xattr
,
208 struct shash_desc
*desc
;
210 desc
= init_desc(EVM_XATTR_HMAC
);
212 printk(KERN_INFO
"init_desc failed\n");
213 return PTR_ERR(desc
);
216 crypto_shash_update(desc
, lsm_xattr
->value
, lsm_xattr
->value_len
);
217 hmac_add_misc(desc
, inode
, hmac_val
);
223 * Get the key from the TPM for the SHA1-HMAC
225 int evm_init_key(void)
228 struct encrypted_key_payload
*ekp
;
231 evm_key
= request_key(&key_type_encrypted
, EVMKEY
, NULL
);
235 down_read(&evm_key
->sem
);
236 ekp
= evm_key
->payload
.data
;
237 if (ekp
->decrypted_datalen
> MAX_KEY_SIZE
) {
241 memcpy(evmkey
, ekp
->decrypted_data
, ekp
->decrypted_datalen
);
243 /* burn the original key contents */
244 memset(ekp
->decrypted_data
, 0, ekp
->decrypted_datalen
);
245 up_read(&evm_key
->sem
);