1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2010 IBM Corporation
6 * Mimi Zohar <zohar@us.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
10 * Using root's kernel master key (kmk), calculate the HMAC
13 #include <linux/export.h>
14 #include <linux/crypto.h>
15 #include <linux/xattr.h>
16 #include <linux/evm.h>
17 #include <keys/encrypted-type.h>
18 #include <crypto/hash.h>
19 #include <crypto/hash_info.h>
22 #define EVMKEY "evm-key"
23 #define MAX_KEY_SIZE 128
24 static unsigned char evmkey
[MAX_KEY_SIZE
];
25 static const int evmkey_len
= MAX_KEY_SIZE
;
27 struct crypto_shash
*hmac_tfm
;
28 static struct crypto_shash
*evm_tfm
[HASH_ALGO__LAST
];
30 static DEFINE_MUTEX(mutex
);
32 #define EVM_SET_KEY_BUSY 0
34 static unsigned long evm_set_key_flags
;
36 static const char evm_hmac
[] = "hmac(sha1)";
39 * evm_set_key() - set EVM HMAC key from the kernel
40 * @key: pointer to a buffer with the key data
41 * @size: length of the key data
43 * This function allows setting the EVM HMAC key from the kernel
44 * without using the "encrypted" key subsystem keys. It can be used
45 * by the crypto HW kernel module which has its own way of managing
48 * key length should be between 32 and 128 bytes long
50 int evm_set_key(void *key
, size_t keylen
)
55 if (test_and_set_bit(EVM_SET_KEY_BUSY
, &evm_set_key_flags
))
58 if (keylen
> MAX_KEY_SIZE
)
60 memcpy(evmkey
, key
, keylen
);
61 evm_initialized
|= EVM_INIT_HMAC
;
62 pr_info("key initialized\n");
65 clear_bit(EVM_SET_KEY_BUSY
, &evm_set_key_flags
);
67 pr_err("key initialization failed\n");
70 EXPORT_SYMBOL_GPL(evm_set_key
);
72 static struct shash_desc
*init_desc(char type
, uint8_t hash_algo
)
76 struct crypto_shash
**tfm
, *tmp_tfm
;
77 struct shash_desc
*desc
;
79 if (type
== EVM_XATTR_HMAC
) {
80 if (!(evm_initialized
& EVM_INIT_HMAC
)) {
81 pr_err_once("HMAC key is not set\n");
82 return ERR_PTR(-ENOKEY
);
87 if (hash_algo
>= HASH_ALGO__LAST
)
88 return ERR_PTR(-EINVAL
);
90 tfm
= &evm_tfm
[hash_algo
];
91 algo
= hash_algo_name
[hash_algo
];
100 tmp_tfm
= crypto_alloc_shash(algo
, 0, CRYPTO_NOLOAD
);
101 if (IS_ERR(tmp_tfm
)) {
102 pr_err("Can not allocate %s (reason: %ld)\n", algo
,
104 mutex_unlock(&mutex
);
105 return ERR_CAST(tmp_tfm
);
107 if (type
== EVM_XATTR_HMAC
) {
108 rc
= crypto_shash_setkey(tmp_tfm
, evmkey
, evmkey_len
);
110 crypto_free_shash(tmp_tfm
);
111 mutex_unlock(&mutex
);
117 mutex_unlock(&mutex
);
119 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(*tfm
),
122 return ERR_PTR(-ENOMEM
);
126 rc
= crypto_shash_init(desc
);
134 /* Protect against 'cutting & pasting' security.evm xattr, include inode
137 * (Additional directory/file metadata needs to be added for more complete
140 static void hmac_add_misc(struct shash_desc
*desc
, struct inode
*inode
,
141 char type
, char *digest
)
151 memset(&hmac_misc
, 0, sizeof(hmac_misc
));
152 /* Don't include the inode or generation number in portable
155 if (type
!= EVM_XATTR_PORTABLE_DIGSIG
) {
156 hmac_misc
.ino
= inode
->i_ino
;
157 hmac_misc
.generation
= inode
->i_generation
;
159 /* The hmac uid and gid must be encoded in the initial user
160 * namespace (not the filesystems user namespace) as encoding
161 * them in the filesystems user namespace allows an attack
162 * where first they are written in an unprivileged fuse mount
163 * of a filesystem and then the system is tricked to mount the
164 * filesystem for real on next boot and trust it because
165 * everything is signed.
167 hmac_misc
.uid
= from_kuid(&init_user_ns
, inode
->i_uid
);
168 hmac_misc
.gid
= from_kgid(&init_user_ns
, inode
->i_gid
);
169 hmac_misc
.mode
= inode
->i_mode
;
170 crypto_shash_update(desc
, (const u8
*)&hmac_misc
, sizeof(hmac_misc
));
171 if ((evm_hmac_attrs
& EVM_ATTR_FSUUID
) &&
172 type
!= EVM_XATTR_PORTABLE_DIGSIG
)
173 crypto_shash_update(desc
, (u8
*)&inode
->i_sb
->s_uuid
, UUID_SIZE
);
174 crypto_shash_final(desc
, digest
);
178 * Calculate the HMAC value across the set of protected security xattrs.
180 * Instead of retrieving the requested xattr, for performance, calculate
181 * the hmac using the requested xattr value. Don't alloc/free memory for
182 * each xattr, but attempt to re-use the previously allocated memory.
184 static int evm_calc_hmac_or_hash(struct dentry
*dentry
,
185 const char *req_xattr_name
,
186 const char *req_xattr_value
,
187 size_t req_xattr_value_len
,
188 uint8_t type
, struct evm_digest
*data
)
190 struct inode
*inode
= d_backing_inode(dentry
);
191 struct xattr_list
*xattr
;
192 struct shash_desc
*desc
;
193 size_t xattr_size
= 0;
194 char *xattr_value
= NULL
;
197 bool ima_present
= false;
199 if (!(inode
->i_opflags
& IOP_XATTR
) ||
200 inode
->i_sb
->s_user_ns
!= &init_user_ns
)
203 desc
= init_desc(type
, data
->hdr
.algo
);
205 return PTR_ERR(desc
);
207 data
->hdr
.length
= crypto_shash_digestsize(desc
->tfm
);
210 list_for_each_entry_lockless(xattr
, &evm_config_xattrnames
, list
) {
213 if (strcmp(xattr
->name
, XATTR_NAME_IMA
) == 0)
216 if ((req_xattr_name
&& req_xattr_value
)
217 && !strcmp(xattr
->name
, req_xattr_name
)) {
219 crypto_shash_update(desc
, (const u8
*)req_xattr_value
,
220 req_xattr_value_len
);
225 size
= vfs_getxattr_alloc(dentry
, xattr
->name
,
226 &xattr_value
, xattr_size
, GFP_NOFS
);
227 if (size
== -ENOMEM
) {
236 crypto_shash_update(desc
, (const u8
*)xattr_value
, xattr_size
);
240 hmac_add_misc(desc
, inode
, type
, data
->digest
);
242 /* Portable EVM signatures must include an IMA hash */
243 if (type
== EVM_XATTR_PORTABLE_DIGSIG
&& !ima_present
)
251 int evm_calc_hmac(struct dentry
*dentry
, const char *req_xattr_name
,
252 const char *req_xattr_value
, size_t req_xattr_value_len
,
253 struct evm_digest
*data
)
255 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
256 req_xattr_value_len
, EVM_XATTR_HMAC
, data
);
259 int evm_calc_hash(struct dentry
*dentry
, const char *req_xattr_name
,
260 const char *req_xattr_value
, size_t req_xattr_value_len
,
261 char type
, struct evm_digest
*data
)
263 return evm_calc_hmac_or_hash(dentry
, req_xattr_name
, req_xattr_value
,
264 req_xattr_value_len
, type
, data
);
267 static int evm_is_immutable(struct dentry
*dentry
, struct inode
*inode
)
269 const struct evm_ima_xattr_data
*xattr_data
= NULL
;
270 struct integrity_iint_cache
*iint
;
273 iint
= integrity_iint_find(inode
);
274 if (iint
&& (iint
->flags
& EVM_IMMUTABLE_DIGSIG
))
277 /* Do this the hard way */
278 rc
= vfs_getxattr_alloc(dentry
, XATTR_NAME_EVM
, (char **)&xattr_data
, 0,
285 if (xattr_data
->type
== EVM_XATTR_PORTABLE_DIGSIG
)
296 * Calculate the hmac and update security.evm xattr
298 * Expects to be called with i_mutex locked.
300 int evm_update_evmxattr(struct dentry
*dentry
, const char *xattr_name
,
301 const char *xattr_value
, size_t xattr_value_len
)
303 struct inode
*inode
= d_backing_inode(dentry
);
304 struct evm_digest data
;
308 * Don't permit any transformation of the EVM xattr if the signature
309 * is of an immutable type
311 rc
= evm_is_immutable(dentry
, inode
);
317 data
.hdr
.algo
= HASH_ALGO_SHA1
;
318 rc
= evm_calc_hmac(dentry
, xattr_name
, xattr_value
,
319 xattr_value_len
, &data
);
321 data
.hdr
.xattr
.sha1
.type
= EVM_XATTR_HMAC
;
322 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_EVM
,
323 &data
.hdr
.xattr
.data
[1],
324 SHA1_DIGEST_SIZE
+ 1, 0);
325 } else if (rc
== -ENODATA
&& (inode
->i_opflags
& IOP_XATTR
)) {
326 rc
= __vfs_removexattr(dentry
, XATTR_NAME_EVM
);
331 int evm_init_hmac(struct inode
*inode
, const struct xattr
*lsm_xattr
,
334 struct shash_desc
*desc
;
336 desc
= init_desc(EVM_XATTR_HMAC
, HASH_ALGO_SHA1
);
338 pr_info("init_desc failed\n");
339 return PTR_ERR(desc
);
342 crypto_shash_update(desc
, lsm_xattr
->value
, lsm_xattr
->value_len
);
343 hmac_add_misc(desc
, inode
, EVM_XATTR_HMAC
, hmac_val
);
349 * Get the key from the TPM for the SHA1-HMAC
351 int evm_init_key(void)
354 struct encrypted_key_payload
*ekp
;
357 evm_key
= request_key(&key_type_encrypted
, EVMKEY
, NULL
);
361 down_read(&evm_key
->sem
);
362 ekp
= evm_key
->payload
.data
[0];
364 rc
= evm_set_key(ekp
->decrypted_data
, ekp
->decrypted_datalen
);
366 /* burn the original key contents */
367 memset(ekp
->decrypted_data
, 0, ekp
->decrypted_datalen
);
368 up_read(&evm_key
->sem
);