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 <keys/encrypted-type.h>
22 #include <crypto/hash.h>
25 #define EVMKEY "evm-key"
26 #define MAX_KEY_SIZE 128
27 static unsigned char evmkey
[MAX_KEY_SIZE
];
28 static int evmkey_len
= MAX_KEY_SIZE
;
30 struct crypto_shash
*hmac_tfm
;
31 struct crypto_shash
*hash_tfm
;
33 static DEFINE_MUTEX(mutex
);
35 static struct shash_desc
*init_desc(char type
)
39 struct crypto_shash
**tfm
;
40 struct shash_desc
*desc
;
42 if (type
== EVM_XATTR_HMAC
) {
54 *tfm
= crypto_alloc_shash(algo
, 0, CRYPTO_ALG_ASYNC
);
57 pr_err("Can not allocate %s (reason: %ld)\n", algo
, rc
);
62 if (type
== EVM_XATTR_HMAC
) {
63 rc
= crypto_shash_setkey(*tfm
, evmkey
, evmkey_len
);
65 crypto_free_shash(*tfm
);
75 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(*tfm
),
78 return ERR_PTR(-ENOMEM
);
81 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
83 rc
= crypto_shash_init(desc
);
91 /* Protect against 'cutting & pasting' security.evm xattr, include inode
94 * (Additional directory/file metadata needs to be added for more complete
97 static void hmac_add_misc(struct shash_desc
*desc
, struct inode
*inode
,
108 memset(&hmac_misc
, 0, sizeof(hmac_misc
));
109 hmac_misc
.ino
= inode
->i_ino
;
110 hmac_misc
.generation
= inode
->i_generation
;
111 hmac_misc
.uid
= from_kuid(&init_user_ns
, inode
->i_uid
);
112 hmac_misc
.gid
= from_kgid(&init_user_ns
, inode
->i_gid
);
113 hmac_misc
.mode
= inode
->i_mode
;
114 crypto_shash_update(desc
, (const u8
*)&hmac_misc
, sizeof(hmac_misc
));
115 if (evm_hmac_attrs
& EVM_ATTR_FSUUID
)
116 crypto_shash_update(desc
, inode
->i_sb
->s_uuid
,
117 sizeof(inode
->i_sb
->s_uuid
));
118 crypto_shash_final(desc
, digest
);
122 * Calculate the HMAC value across the set of protected security xattrs.
124 * Instead of retrieving the requested xattr, for performance, calculate
125 * the hmac using the requested xattr value. Don't alloc/free memory for
126 * each xattr, but attempt to re-use the previously allocated memory.
128 static int evm_calc_hmac_or_hash(struct dentry
*dentry
,
129 const char *req_xattr_name
,
130 const char *req_xattr_value
,
131 size_t req_xattr_value_len
,
132 char type
, char *digest
)
134 struct inode
*inode
= dentry
->d_inode
;
135 struct shash_desc
*desc
;
137 size_t xattr_size
= 0;
138 char *xattr_value
= NULL
;
142 if (!inode
->i_op
->getxattr
)
144 desc
= init_desc(type
);
146 return PTR_ERR(desc
);
149 for (xattrname
= evm_config_xattrnames
; *xattrname
!= NULL
; xattrname
++) {
150 if ((req_xattr_name
&& req_xattr_value
)
151 && !strcmp(*xattrname
, req_xattr_name
)) {
153 crypto_shash_update(desc
, (const u8
*)req_xattr_value
,
154 req_xattr_value_len
);
157 size
= vfs_getxattr_alloc(dentry
, *xattrname
,
158 &xattr_value
, xattr_size
, GFP_NOFS
);
159 if (size
== -ENOMEM
) {
168 crypto_shash_update(desc
, (const u8
*)xattr_value
, xattr_size
);
170 hmac_add_misc(desc
, inode
, digest
);
178 int evm_calc_hmac(struct dentry
*dentry
, const char *req_xattr_name
,
179 const char *req_xattr_value
, size_t req_xattr_value_len
,
182 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
183 req_xattr_value_len
, EVM_XATTR_HMAC
, digest
);
186 int evm_calc_hash(struct dentry
*dentry
, const char *req_xattr_name
,
187 const char *req_xattr_value
, size_t req_xattr_value_len
,
190 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
191 req_xattr_value_len
, IMA_XATTR_DIGEST
, digest
);
195 * Calculate the hmac and update security.evm xattr
197 * Expects to be called with i_mutex locked.
199 int evm_update_evmxattr(struct dentry
*dentry
, const char *xattr_name
,
200 const char *xattr_value
, size_t xattr_value_len
)
202 struct inode
*inode
= dentry
->d_inode
;
203 struct evm_ima_xattr_data xattr_data
;
206 rc
= evm_calc_hmac(dentry
, xattr_name
, xattr_value
,
207 xattr_value_len
, xattr_data
.digest
);
209 xattr_data
.type
= EVM_XATTR_HMAC
;
210 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_EVM
,
212 sizeof(xattr_data
), 0);
213 } else if (rc
== -ENODATA
&& inode
->i_op
->removexattr
) {
214 rc
= inode
->i_op
->removexattr(dentry
, XATTR_NAME_EVM
);
219 int evm_init_hmac(struct inode
*inode
, const struct xattr
*lsm_xattr
,
222 struct shash_desc
*desc
;
224 desc
= init_desc(EVM_XATTR_HMAC
);
226 pr_info("init_desc failed\n");
227 return PTR_ERR(desc
);
230 crypto_shash_update(desc
, lsm_xattr
->value
, lsm_xattr
->value_len
);
231 hmac_add_misc(desc
, inode
, hmac_val
);
237 * Get the key from the TPM for the SHA1-HMAC
239 int evm_init_key(void)
242 struct encrypted_key_payload
*ekp
;
245 evm_key
= request_key(&key_type_encrypted
, EVMKEY
, NULL
);
249 down_read(&evm_key
->sem
);
250 ekp
= evm_key
->payload
.data
;
251 if (ekp
->decrypted_datalen
> MAX_KEY_SIZE
) {
255 memcpy(evmkey
, ekp
->decrypted_data
, ekp
->decrypted_datalen
);
257 /* burn the original key contents */
258 memset(ekp
->decrypted_data
, 0, ekp
->decrypted_datalen
);
259 up_read(&evm_key
->sem
);