drm/i915: fix port checks for MST support on gen >= 11
[linux/fpc-iii.git] / security / integrity / evm / evm_crypto.c
blobf0878d81dcef2fd61cc2f5c8685953436053764b
1 /*
2 * Copyright (C) 2005-2010 IBM Corporation
4 * Authors:
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.
12 * File: evm_crypto.c
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>
24 #include <crypto/hash_info.h>
25 #include "evm.h"
27 #define EVMKEY "evm-key"
28 #define MAX_KEY_SIZE 128
29 static unsigned char evmkey[MAX_KEY_SIZE];
30 static int evmkey_len = MAX_KEY_SIZE;
32 struct crypto_shash *hmac_tfm;
33 static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
35 static DEFINE_MUTEX(mutex);
37 #define EVM_SET_KEY_BUSY 0
39 static unsigned long evm_set_key_flags;
41 static char * const evm_hmac = "hmac(sha1)";
43 /**
44 * evm_set_key() - set EVM HMAC key from the kernel
45 * @key: pointer to a buffer with the key data
46 * @size: length of the key data
48 * This function allows setting the EVM HMAC key from the kernel
49 * without using the "encrypted" key subsystem keys. It can be used
50 * by the crypto HW kernel module which has its own way of managing
51 * keys.
53 * key length should be between 32 and 128 bytes long
55 int evm_set_key(void *key, size_t keylen)
57 int rc;
59 rc = -EBUSY;
60 if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
61 goto busy;
62 rc = -EINVAL;
63 if (keylen > MAX_KEY_SIZE)
64 goto inval;
65 memcpy(evmkey, key, keylen);
66 evm_initialized |= EVM_INIT_HMAC;
67 pr_info("key initialized\n");
68 return 0;
69 inval:
70 clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
71 busy:
72 pr_err("key initialization failed\n");
73 return rc;
75 EXPORT_SYMBOL_GPL(evm_set_key);
77 static struct shash_desc *init_desc(char type, uint8_t hash_algo)
79 long rc;
80 const char *algo;
81 struct crypto_shash **tfm;
82 struct shash_desc *desc;
84 if (type == EVM_XATTR_HMAC) {
85 if (!(evm_initialized & EVM_INIT_HMAC)) {
86 pr_err_once("HMAC key is not set\n");
87 return ERR_PTR(-ENOKEY);
89 tfm = &hmac_tfm;
90 algo = evm_hmac;
91 } else {
92 if (hash_algo >= HASH_ALGO__LAST)
93 return ERR_PTR(-EINVAL);
95 tfm = &evm_tfm[hash_algo];
96 algo = hash_algo_name[hash_algo];
99 if (IS_ERR_OR_NULL(*tfm)) {
100 mutex_lock(&mutex);
101 if (*tfm)
102 goto out;
103 *tfm = crypto_alloc_shash(algo, 0,
104 CRYPTO_ALG_ASYNC | CRYPTO_NOLOAD);
105 if (IS_ERR(*tfm)) {
106 rc = PTR_ERR(*tfm);
107 pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
108 *tfm = NULL;
109 mutex_unlock(&mutex);
110 return ERR_PTR(rc);
112 if (type == EVM_XATTR_HMAC) {
113 rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
114 if (rc) {
115 crypto_free_shash(*tfm);
116 *tfm = NULL;
117 mutex_unlock(&mutex);
118 return ERR_PTR(rc);
121 out:
122 mutex_unlock(&mutex);
125 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
126 GFP_KERNEL);
127 if (!desc)
128 return ERR_PTR(-ENOMEM);
130 desc->tfm = *tfm;
131 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
133 rc = crypto_shash_init(desc);
134 if (rc) {
135 kfree(desc);
136 return ERR_PTR(rc);
138 return desc;
141 /* Protect against 'cutting & pasting' security.evm xattr, include inode
142 * specific info.
144 * (Additional directory/file metadata needs to be added for more complete
145 * protection.)
147 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
148 char type, char *digest)
150 struct h_misc {
151 unsigned long ino;
152 __u32 generation;
153 uid_t uid;
154 gid_t gid;
155 umode_t mode;
156 } hmac_misc;
158 memset(&hmac_misc, 0, sizeof(hmac_misc));
159 /* Don't include the inode or generation number in portable
160 * signatures
162 if (type != EVM_XATTR_PORTABLE_DIGSIG) {
163 hmac_misc.ino = inode->i_ino;
164 hmac_misc.generation = inode->i_generation;
166 /* The hmac uid and gid must be encoded in the initial user
167 * namespace (not the filesystems user namespace) as encoding
168 * them in the filesystems user namespace allows an attack
169 * where first they are written in an unprivileged fuse mount
170 * of a filesystem and then the system is tricked to mount the
171 * filesystem for real on next boot and trust it because
172 * everything is signed.
174 hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
175 hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
176 hmac_misc.mode = inode->i_mode;
177 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
178 if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
179 type != EVM_XATTR_PORTABLE_DIGSIG)
180 crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0],
181 sizeof(inode->i_sb->s_uuid));
182 crypto_shash_final(desc, digest);
186 * Calculate the HMAC value across the set of protected security xattrs.
188 * Instead of retrieving the requested xattr, for performance, calculate
189 * the hmac using the requested xattr value. Don't alloc/free memory for
190 * each xattr, but attempt to re-use the previously allocated memory.
192 static int evm_calc_hmac_or_hash(struct dentry *dentry,
193 const char *req_xattr_name,
194 const char *req_xattr_value,
195 size_t req_xattr_value_len,
196 uint8_t type, struct evm_digest *data)
198 struct inode *inode = d_backing_inode(dentry);
199 struct xattr_list *xattr;
200 struct shash_desc *desc;
201 size_t xattr_size = 0;
202 char *xattr_value = NULL;
203 int error;
204 int size;
205 bool ima_present = false;
207 if (!(inode->i_opflags & IOP_XATTR) ||
208 inode->i_sb->s_user_ns != &init_user_ns)
209 return -EOPNOTSUPP;
211 desc = init_desc(type, data->hdr.algo);
212 if (IS_ERR(desc))
213 return PTR_ERR(desc);
215 data->hdr.length = crypto_shash_digestsize(desc->tfm);
217 error = -ENODATA;
218 list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
219 bool is_ima = false;
221 if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
222 is_ima = true;
224 if ((req_xattr_name && req_xattr_value)
225 && !strcmp(xattr->name, req_xattr_name)) {
226 error = 0;
227 crypto_shash_update(desc, (const u8 *)req_xattr_value,
228 req_xattr_value_len);
229 if (is_ima)
230 ima_present = true;
231 continue;
233 size = vfs_getxattr_alloc(dentry, xattr->name,
234 &xattr_value, xattr_size, GFP_NOFS);
235 if (size == -ENOMEM) {
236 error = -ENOMEM;
237 goto out;
239 if (size < 0)
240 continue;
242 error = 0;
243 xattr_size = size;
244 crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
245 if (is_ima)
246 ima_present = true;
248 hmac_add_misc(desc, inode, type, data->digest);
250 /* Portable EVM signatures must include an IMA hash */
251 if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
252 return -EPERM;
253 out:
254 kfree(xattr_value);
255 kfree(desc);
256 return error;
259 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
260 const char *req_xattr_value, size_t req_xattr_value_len,
261 struct evm_digest *data)
263 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
264 req_xattr_value_len, EVM_XATTR_HMAC, data);
267 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
268 const char *req_xattr_value, size_t req_xattr_value_len,
269 char type, struct evm_digest *data)
271 return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
272 req_xattr_value_len, type, data);
275 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
277 const struct evm_ima_xattr_data *xattr_data = NULL;
278 struct integrity_iint_cache *iint;
279 int rc = 0;
281 iint = integrity_iint_find(inode);
282 if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
283 return 1;
285 /* Do this the hard way */
286 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
287 GFP_NOFS);
288 if (rc <= 0) {
289 if (rc == -ENODATA)
290 return 0;
291 return rc;
293 if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
294 rc = 1;
295 else
296 rc = 0;
298 kfree(xattr_data);
299 return rc;
304 * Calculate the hmac and update security.evm xattr
306 * Expects to be called with i_mutex locked.
308 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
309 const char *xattr_value, size_t xattr_value_len)
311 struct inode *inode = d_backing_inode(dentry);
312 struct evm_digest data;
313 int rc = 0;
316 * Don't permit any transformation of the EVM xattr if the signature
317 * is of an immutable type
319 rc = evm_is_immutable(dentry, inode);
320 if (rc < 0)
321 return rc;
322 if (rc)
323 return -EPERM;
325 data.hdr.algo = HASH_ALGO_SHA1;
326 rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
327 xattr_value_len, &data);
328 if (rc == 0) {
329 data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
330 rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
331 &data.hdr.xattr.data[1],
332 SHA1_DIGEST_SIZE + 1, 0);
333 } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
334 rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
336 return rc;
339 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
340 char *hmac_val)
342 struct shash_desc *desc;
344 desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
345 if (IS_ERR(desc)) {
346 pr_info("init_desc failed\n");
347 return PTR_ERR(desc);
350 crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
351 hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
352 kfree(desc);
353 return 0;
357 * Get the key from the TPM for the SHA1-HMAC
359 int evm_init_key(void)
361 struct key *evm_key;
362 struct encrypted_key_payload *ekp;
363 int rc;
365 evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
366 if (IS_ERR(evm_key))
367 return -ENOENT;
369 down_read(&evm_key->sem);
370 ekp = evm_key->payload.data[0];
372 rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
374 /* burn the original key contents */
375 memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
376 up_read(&evm_key->sem);
377 key_put(evm_key);
378 return rc;