1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2010 IBM Corporation
10 * Using root's kernel master key (kmk), calculate the HMAC
13 #define pr_fmt(fmt) "EVM: "fmt
15 #include <linux/export.h>
16 #include <linux/crypto.h>
17 #include <linux/xattr.h>
18 #include <linux/evm.h>
19 #include <keys/encrypted-type.h>
20 #include <crypto/hash.h>
21 #include <crypto/hash_info.h>
24 #define EVMKEY "evm-key"
25 #define MAX_KEY_SIZE 128
26 static unsigned char evmkey[MAX_KEY_SIZE];
27 static const int evmkey_len = MAX_KEY_SIZE;
29 struct crypto_shash *hmac_tfm;
30 static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
32 static DEFINE_MUTEX(mutex);
34 #define EVM_SET_KEY_BUSY 0
36 static unsigned long evm_set_key_flags;
38 static const char evm_hmac[] = "hmac(sha1)";
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, uint8_t hash_algo)
78 struct crypto_shash **tfm, *tmp_tfm = NULL;
79 struct shash_desc *desc;
81 if (type == EVM_XATTR_HMAC) {
82 if (!(evm_initialized & EVM_INIT_HMAC)) {
83 pr_err_once("HMAC key is not set\n");
84 return ERR_PTR(-ENOKEY);
89 if (hash_algo >= HASH_ALGO__LAST)
90 return ERR_PTR(-EINVAL);
92 tfm = &evm_tfm[hash_algo];
93 algo = hash_algo_name[hash_algo];
102 tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
103 if (IS_ERR(tmp_tfm)) {
104 pr_err("Can not allocate %s (reason: %ld)\n", algo,
106 mutex_unlock(&mutex);
107 return ERR_CAST(tmp_tfm);
109 if (type == EVM_XATTR_HMAC) {
110 rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
112 crypto_free_shash(tmp_tfm);
113 mutex_unlock(&mutex);
119 mutex_unlock(&mutex);
121 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
124 crypto_free_shash(tmp_tfm);
125 return ERR_PTR(-ENOMEM);
130 rc = crypto_shash_init(desc);
132 crypto_free_shash(tmp_tfm);
139 /* Protect against 'cutting & pasting' security.evm xattr, include inode
142 * (Additional directory/file metadata needs to be added for more complete
145 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
146 char type, char *digest)
156 memset(&hmac_misc, 0, sizeof(hmac_misc));
157 /* Don't include the inode or generation number in portable
160 if (type != EVM_XATTR_PORTABLE_DIGSIG) {
161 hmac_misc.ino = inode->i_ino;
162 hmac_misc.generation = inode->i_generation;
164 /* The hmac uid and gid must be encoded in the initial user
165 * namespace (not the filesystems user namespace) as encoding
166 * them in the filesystems user namespace allows an attack
167 * where first they are written in an unprivileged fuse mount
168 * of a filesystem and then the system is tricked to mount the
169 * filesystem for real on next boot and trust it because
170 * everything is signed.
172 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
173 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
174 hmac_misc.mode = inode->i_mode;
175 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
176 if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
177 type != EVM_XATTR_PORTABLE_DIGSIG)
178 crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
179 crypto_shash_final(desc, digest);
181 pr_debug("hmac_misc: (%zu) [%*phN]\n", sizeof(struct h_misc),
182 (int)sizeof(struct h_misc), &hmac_misc);
186 * Dump large security xattr values as a continuous ascii hexademical string.
187 * (pr_debug is limited to 64 bytes.)
189 static void dump_security_xattr(const char *prefix, const void *src,
192 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
195 p = asciihex = kmalloc(count * 2 + 1, GFP_KERNEL);
199 p = bin2hex(p, src, count);
201 pr_debug("%s: (%zu) %.*s\n", prefix, count, (int)count * 2, asciihex);
207 * Calculate the HMAC value across the set of protected security xattrs.
209 * Instead of retrieving the requested xattr, for performance, calculate
210 * the hmac using the requested xattr value. Don't alloc/free memory for
211 * each xattr, but attempt to re-use the previously allocated memory.
213 static int evm_calc_hmac_or_hash(struct dentry *dentry,
214 const char *req_xattr_name,
215 const char *req_xattr_value,
216 size_t req_xattr_value_len,
217 uint8_t type, struct evm_digest *data)
219 struct inode *inode = d_backing_inode(dentry);
220 struct xattr_list *xattr;
221 struct shash_desc *desc;
222 size_t xattr_size = 0;
223 char *xattr_value = NULL;
225 int size, user_space_size;
226 bool ima_present = false;
228 if (!(inode->i_opflags & IOP_XATTR) ||
229 inode->i_sb->s_user_ns != &init_user_ns)
232 desc = init_desc(type, data->hdr.algo);
234 return PTR_ERR(desc);
236 data->hdr.length = crypto_shash_digestsize(desc->tfm);
239 list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
242 if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
246 * Skip non-enabled xattrs for locally calculated
249 if (type != EVM_XATTR_PORTABLE_DIGSIG && !xattr->enabled)
252 if ((req_xattr_name && req_xattr_value)
253 && !strcmp(xattr->name, req_xattr_name)) {
255 crypto_shash_update(desc, (const u8 *)req_xattr_value,
256 req_xattr_value_len);
260 if (req_xattr_value_len < 64)
261 pr_debug("%s: (%zu) [%*phN]\n", req_xattr_name,
263 (int)req_xattr_value_len,
266 dump_security_xattr(req_xattr_name,
268 req_xattr_value_len);
271 size = vfs_getxattr_alloc(&init_user_ns, dentry, xattr->name,
272 &xattr_value, xattr_size, GFP_NOFS);
273 if (size == -ENOMEM) {
280 user_space_size = vfs_getxattr(&init_user_ns, dentry,
281 xattr->name, NULL, 0);
282 if (user_space_size != size)
283 pr_debug("file %s: xattr %s size mismatch (kernel: %d, user: %d)\n",
284 dentry->d_name.name, xattr->name, size,
288 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
293 pr_debug("%s: (%zu) [%*phN]", xattr->name, xattr_size,
294 (int)xattr_size, xattr_value);
296 dump_security_xattr(xattr->name, xattr_value,
299 hmac_add_misc(desc, inode, type, data->digest);
301 /* Portable EVM signatures must include an IMA hash */
302 if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
310 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
311 const char *req_xattr_value, size_t req_xattr_value_len,
312 struct evm_digest *data)
314 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
315 req_xattr_value_len, EVM_XATTR_HMAC, data);
318 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
319 const char *req_xattr_value, size_t req_xattr_value_len,
320 char type, struct evm_digest *data)
322 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
323 req_xattr_value_len, type, data);
326 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
328 const struct evm_ima_xattr_data *xattr_data = NULL;
329 struct integrity_iint_cache *iint;
332 iint = integrity_iint_find(inode);
333 if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
336 /* Do this the hard way */
337 rc = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_EVM,
338 (char **)&xattr_data, 0, GFP_NOFS);
344 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
355 * Calculate the hmac and update security.evm xattr
357 * Expects to be called with i_mutex locked.
359 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
360 const char *xattr_value, size_t xattr_value_len)
362 struct inode *inode = d_backing_inode(dentry);
363 struct evm_digest data;
367 * Don't permit any transformation of the EVM xattr if the signature
368 * is of an immutable type
370 rc = evm_is_immutable(dentry, inode);
376 data.hdr.algo = HASH_ALGO_SHA1;
377 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
378 xattr_value_len, &data);
380 data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
381 rc = __vfs_setxattr_noperm(&init_user_ns, dentry,
383 &data.hdr.xattr.data[1],
384 SHA1_DIGEST_SIZE + 1, 0);
385 } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
386 rc = __vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_EVM);
391 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
394 struct shash_desc *desc;
396 desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
398 pr_info("init_desc failed\n");
399 return PTR_ERR(desc);
402 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
403 hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
409 * Get the key from the TPM for the SHA1-HMAC
411 int evm_init_key(void)
414 struct encrypted_key_payload *ekp;
417 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
421 down_read(&evm_key->sem);
422 ekp = evm_key->payload.data[0];
424 rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
426 /* burn the original key contents */
427 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
428 up_read(&evm_key->sem);