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
;
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
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
50 * key length should be between 32 and 128 bytes long
52 int evm_set_key(void *key
, size_t keylen
)
57 if (test_and_set_bit(EVM_SET_KEY_BUSY
, &evm_set_key_flags
))
60 if (keylen
> MAX_KEY_SIZE
)
62 memcpy(evmkey
, key
, keylen
);
63 evm_initialized
|= EVM_INIT_HMAC
;
64 pr_info("key initialized\n");
67 clear_bit(EVM_SET_KEY_BUSY
, &evm_set_key_flags
);
69 pr_err("key initialization failed\n");
72 EXPORT_SYMBOL_GPL(evm_set_key
);
74 static struct shash_desc
*init_desc(char type
)
78 struct crypto_shash
**tfm
;
79 struct shash_desc
*desc
;
81 if (type
== EVM_XATTR_HMAC
) {
82 if (!(evm_initialized
& EVM_INIT_HMAC
)) {
83 pr_err("HMAC key is not set\n");
84 return ERR_PTR(-ENOKEY
);
97 *tfm
= crypto_alloc_shash(algo
, 0,
98 CRYPTO_ALG_ASYNC
| CRYPTO_NOLOAD
);
101 pr_err("Can not allocate %s (reason: %ld)\n", algo
, rc
);
103 mutex_unlock(&mutex
);
106 if (type
== EVM_XATTR_HMAC
) {
107 rc
= crypto_shash_setkey(*tfm
, evmkey
, evmkey_len
);
109 crypto_free_shash(*tfm
);
111 mutex_unlock(&mutex
);
116 mutex_unlock(&mutex
);
119 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(*tfm
),
122 return ERR_PTR(-ENOMEM
);
125 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
127 rc
= crypto_shash_init(desc
);
135 /* Protect against 'cutting & pasting' security.evm xattr, include inode
138 * (Additional directory/file metadata needs to be added for more complete
141 static void hmac_add_misc(struct shash_desc
*desc
, struct inode
*inode
,
142 char type
, char *digest
)
152 memset(&hmac_misc
, 0, sizeof(hmac_misc
));
153 /* Don't include the inode or generation number in portable
156 if (type
!= EVM_XATTR_PORTABLE_DIGSIG
) {
157 hmac_misc
.ino
= inode
->i_ino
;
158 hmac_misc
.generation
= inode
->i_generation
;
160 /* The hmac uid and gid must be encoded in the initial user
161 * namespace (not the filesystems user namespace) as encoding
162 * them in the filesystems user namespace allows an attack
163 * where first they are written in an unprivileged fuse mount
164 * of a filesystem and then the system is tricked to mount the
165 * filesystem for real on next boot and trust it because
166 * everything is signed.
168 hmac_misc
.uid
= from_kuid(&init_user_ns
, inode
->i_uid
);
169 hmac_misc
.gid
= from_kgid(&init_user_ns
, inode
->i_gid
);
170 hmac_misc
.mode
= inode
->i_mode
;
171 crypto_shash_update(desc
, (const u8
*)&hmac_misc
, sizeof(hmac_misc
));
172 if ((evm_hmac_attrs
& EVM_ATTR_FSUUID
) &&
173 type
!= EVM_XATTR_PORTABLE_DIGSIG
)
174 crypto_shash_update(desc
, inode
->i_sb
->s_uuid
,
175 sizeof(inode
->i_sb
->s_uuid
));
176 crypto_shash_final(desc
, digest
);
180 * Calculate the HMAC value across the set of protected security xattrs.
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.
186 static 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 char type
, char *digest
)
192 struct inode
*inode
= d_backing_inode(dentry
);
193 struct shash_desc
*desc
;
195 size_t xattr_size
= 0;
196 char *xattr_value
= NULL
;
199 bool ima_present
= false;
201 if (!(inode
->i_opflags
& IOP_XATTR
))
204 desc
= init_desc(type
);
206 return PTR_ERR(desc
);
209 for (xattrname
= evm_config_xattrnames
; *xattrname
!= NULL
; xattrname
++) {
212 if (strcmp(*xattrname
, XATTR_NAME_IMA
) == 0)
215 if ((req_xattr_name
&& req_xattr_value
)
216 && !strcmp(*xattrname
, req_xattr_name
)) {
218 crypto_shash_update(desc
, (const u8
*)req_xattr_value
,
219 req_xattr_value_len
);
224 size
= vfs_getxattr_alloc(dentry
, *xattrname
,
225 &xattr_value
, xattr_size
, GFP_NOFS
);
226 if (size
== -ENOMEM
) {
235 crypto_shash_update(desc
, (const u8
*)xattr_value
, xattr_size
);
239 hmac_add_misc(desc
, inode
, type
, digest
);
241 /* Portable EVM signatures must include an IMA hash */
242 if (type
== EVM_XATTR_PORTABLE_DIGSIG
&& !ima_present
)
250 int evm_calc_hmac(struct dentry
*dentry
, const char *req_xattr_name
,
251 const char *req_xattr_value
, size_t req_xattr_value_len
,
254 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
255 req_xattr_value_len
, EVM_XATTR_HMAC
, digest
);
258 int evm_calc_hash(struct dentry
*dentry
, const char *req_xattr_name
,
259 const char *req_xattr_value
, size_t req_xattr_value_len
,
260 char type
, char *digest
)
262 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
263 req_xattr_value_len
, type
, digest
);
266 static int evm_is_immutable(struct dentry
*dentry
, struct inode
*inode
)
268 const struct evm_ima_xattr_data
*xattr_data
= NULL
;
269 struct integrity_iint_cache
*iint
;
272 iint
= integrity_iint_find(inode
);
273 if (iint
&& (iint
->flags
& EVM_IMMUTABLE_DIGSIG
))
276 /* Do this the hard way */
277 rc
= vfs_getxattr_alloc(dentry
, XATTR_NAME_EVM
, (char **)&xattr_data
, 0,
284 if (xattr_data
->type
== EVM_XATTR_PORTABLE_DIGSIG
)
295 * Calculate the hmac and update security.evm xattr
297 * Expects to be called with i_mutex locked.
299 int evm_update_evmxattr(struct dentry
*dentry
, const char *xattr_name
,
300 const char *xattr_value
, size_t xattr_value_len
)
302 struct inode
*inode
= d_backing_inode(dentry
);
303 struct evm_ima_xattr_data xattr_data
;
307 * Don't permit any transformation of the EVM xattr if the signature
308 * is of an immutable type
310 rc
= evm_is_immutable(dentry
, inode
);
316 rc
= evm_calc_hmac(dentry
, xattr_name
, xattr_value
,
317 xattr_value_len
, xattr_data
.digest
);
319 xattr_data
.type
= EVM_XATTR_HMAC
;
320 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_EVM
,
322 sizeof(xattr_data
), 0);
323 } else if (rc
== -ENODATA
&& (inode
->i_opflags
& IOP_XATTR
)) {
324 rc
= __vfs_removexattr(dentry
, XATTR_NAME_EVM
);
329 int evm_init_hmac(struct inode
*inode
, const struct xattr
*lsm_xattr
,
332 struct shash_desc
*desc
;
334 desc
= init_desc(EVM_XATTR_HMAC
);
336 pr_info("init_desc failed\n");
337 return PTR_ERR(desc
);
340 crypto_shash_update(desc
, lsm_xattr
->value
, lsm_xattr
->value_len
);
341 hmac_add_misc(desc
, inode
, EVM_XATTR_HMAC
, hmac_val
);
347 * Get the key from the TPM for the SHA1-HMAC
349 int evm_init_key(void)
352 struct encrypted_key_payload
*ekp
;
355 evm_key
= request_key(&key_type_encrypted
, EVMKEY
, NULL
);
359 down_read(&evm_key
->sem
);
360 ekp
= evm_key
->payload
.data
[0];
362 rc
= evm_set_key(ekp
->decrypted_data
, ekp
->decrypted_datalen
);
364 /* burn the original key contents */
365 memset(ekp
->decrypted_data
, 0, ekp
->decrypted_datalen
);
366 up_read(&evm_key
->sem
);