1 /*
2  * Copyright (C) 2005-2010 IBM Corporation
3  *
4  * Authors:
5  * Mimi Zohar <zohar@us.ibm.com>
6  * Kylene Hall <kjhall@us.ibm.com>
7  *
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.
11  *
12  * File: evm_crypto.c
13  *	 Using root's kernel master key (kmk), calculate the HMAC
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/export.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"
26 
27 #define EVMKEY "evm-key"
28 #define MAX_KEY_SIZE 128
29 static unsigned char evmkey[MAX_KEY_SIZE];
30 static const int evmkey_len = MAX_KEY_SIZE;
31 
32 struct crypto_shash *hmac_tfm;
33 static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
34 
35 static DEFINE_MUTEX(mutex);
36 
37 #define EVM_SET_KEY_BUSY 0
38 
39 static unsigned long evm_set_key_flags;
40 
41 static const char evm_hmac[] = "hmac(sha1)";
42 
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
47  *
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.
52  *
53  * key length should be between 32 and 128 bytes long
54  */
55 int evm_set_key(void *key, size_t keylen)
56 {
57 	int rc;
58 
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;
74 }
75 EXPORT_SYMBOL_GPL(evm_set_key);
76 
77 static struct shash_desc *init_desc(char type, uint8_t hash_algo)
78 {
79 	long rc;
80 	const char *algo;
81 	struct crypto_shash **tfm;
82 	struct shash_desc *desc;
83 
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);
88 		}
89 		tfm = &hmac_tfm;
90 		algo = evm_hmac;
91 	} else {
92 		tfm = &evm_tfm[hash_algo];
93 		algo = hash_algo_name[hash_algo];
94 	}
95 
96 	if (*tfm == NULL) {
97 		mutex_lock(&mutex);
98 		if (*tfm)
99 			goto out;
100 		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
101 		if (IS_ERR(*tfm)) {
102 			rc = PTR_ERR(*tfm);
103 			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
104 			*tfm = NULL;
105 			mutex_unlock(&mutex);
106 			return ERR_PTR(rc);
107 		}
108 		if (type == EVM_XATTR_HMAC) {
109 			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
110 			if (rc) {
111 				crypto_free_shash(*tfm);
112 				*tfm = NULL;
113 				mutex_unlock(&mutex);
114 				return ERR_PTR(rc);
115 			}
116 		}
117 out:
118 		mutex_unlock(&mutex);
119 	}
120 
121 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
122 			GFP_KERNEL);
123 	if (!desc)
124 		return ERR_PTR(-ENOMEM);
125 
126 	desc->tfm = *tfm;
127 	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
128 
129 	rc = crypto_shash_init(desc);
130 	if (rc) {
131 		kfree(desc);
132 		return ERR_PTR(rc);
133 	}
134 	return desc;
135 }
136 
137 /* Protect against 'cutting & pasting' security.evm xattr, include inode
138  * specific info.
139  *
140  * (Additional directory/file metadata needs to be added for more complete
141  * protection.)
142  */
143 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
144 			  char type, char *digest)
145 {
146 	struct h_misc {
147 		unsigned long ino;
148 		__u32 generation;
149 		uid_t uid;
150 		gid_t gid;
151 		umode_t mode;
152 	} hmac_misc;
153 
154 	memset(&hmac_misc, 0, sizeof(hmac_misc));
155 	/* Don't include the inode or generation number in portable
156 	 * signatures
157 	 */
158 	if (type != EVM_XATTR_PORTABLE_DIGSIG) {
159 		hmac_misc.ino = inode->i_ino;
160 		hmac_misc.generation = inode->i_generation;
161 	}
162 	/* The hmac uid and gid must be encoded in the initial user
163 	 * namespace (not the filesystems user namespace) as encoding
164 	 * them in the filesystems user namespace allows an attack
165 	 * where first they are written in an unprivileged fuse mount
166 	 * of a filesystem and then the system is tricked to mount the
167 	 * filesystem for real on next boot and trust it because
168 	 * everything is signed.
169 	 */
170 	hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
171 	hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
172 	hmac_misc.mode = inode->i_mode;
173 	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
174 	if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
175 	    type != EVM_XATTR_PORTABLE_DIGSIG)
176 		crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
177 	crypto_shash_final(desc, digest);
178 }
179 
180 /*
181  * Calculate the HMAC value across the set of protected security xattrs.
182  *
183  * Instead of retrieving the requested xattr, for performance, calculate
184  * the hmac using the requested xattr value. Don't alloc/free memory for
185  * each xattr, but attempt to re-use the previously allocated memory.
186  */
187 static int evm_calc_hmac_or_hash(struct dentry *dentry,
188 				 const char *req_xattr_name,
189 				 const char *req_xattr_value,
190 				 size_t req_xattr_value_len,
191 				 uint8_t type, struct evm_digest *data)
192 {
193 	struct inode *inode = d_backing_inode(dentry);
194 	struct xattr_list *xattr;
195 	struct shash_desc *desc;
196 	size_t xattr_size = 0;
197 	char *xattr_value = NULL;
198 	int error;
199 	int size;
200 	bool ima_present = false;
201 
202 	if (!(inode->i_opflags & IOP_XATTR) ||
203 	    inode->i_sb->s_user_ns != &init_user_ns)
204 		return -EOPNOTSUPP;
205 
206 	desc = init_desc(type, data->hdr.algo);
207 	if (IS_ERR(desc))
208 		return PTR_ERR(desc);
209 
210 	data->hdr.length = crypto_shash_digestsize(desc->tfm);
211 
212 	error = -ENODATA;
213 	list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
214 		bool is_ima = false;
215 
216 		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
217 			is_ima = true;
218 
219 		if ((req_xattr_name && req_xattr_value)
220 		    && !strcmp(xattr->name, req_xattr_name)) {
221 			error = 0;
222 			crypto_shash_update(desc, (const u8 *)req_xattr_value,
223 					     req_xattr_value_len);
224 			if (is_ima)
225 				ima_present = true;
226 			continue;
227 		}
228 		size = vfs_getxattr_alloc(dentry, xattr->name,
229 					  &xattr_value, xattr_size, GFP_NOFS);
230 		if (size == -ENOMEM) {
231 			error = -ENOMEM;
232 			goto out;
233 		}
234 		if (size < 0)
235 			continue;
236 
237 		error = 0;
238 		xattr_size = size;
239 		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
240 		if (is_ima)
241 			ima_present = true;
242 	}
243 	hmac_add_misc(desc, inode, type, data->digest);
244 
245 	/* Portable EVM signatures must include an IMA hash */
246 	if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
247 		return -EPERM;
248 out:
249 	kfree(xattr_value);
250 	kfree(desc);
251 	return error;
252 }
253 
254 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
255 		  const char *req_xattr_value, size_t req_xattr_value_len,
256 		  struct evm_digest *data)
257 {
258 	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
259 				    req_xattr_value_len, EVM_XATTR_HMAC, data);
260 }
261 
262 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
263 		  const char *req_xattr_value, size_t req_xattr_value_len,
264 		  char type, struct evm_digest *data)
265 {
266 	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
267 				     req_xattr_value_len, type, data);
268 }
269 
270 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
271 {
272 	const struct evm_ima_xattr_data *xattr_data = NULL;
273 	struct integrity_iint_cache *iint;
274 	int rc = 0;
275 
276 	iint = integrity_iint_find(inode);
277 	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
278 		return 1;
279 
280 	/* Do this the hard way */
281 	rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
282 				GFP_NOFS);
283 	if (rc <= 0) {
284 		if (rc == -ENODATA)
285 			return 0;
286 		return rc;
287 	}
288 	if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
289 		rc = 1;
290 	else
291 		rc = 0;
292 
293 	kfree(xattr_data);
294 	return rc;
295 }
296 
297 
298 /*
299  * Calculate the hmac and update security.evm xattr
300  *
301  * Expects to be called with i_mutex locked.
302  */
303 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
304 			const char *xattr_value, size_t xattr_value_len)
305 {
306 	struct inode *inode = d_backing_inode(dentry);
307 	struct evm_digest data;
308 	int rc = 0;
309 
310 	/*
311 	 * Don't permit any transformation of the EVM xattr if the signature
312 	 * is of an immutable type
313 	 */
314 	rc = evm_is_immutable(dentry, inode);
315 	if (rc < 0)
316 		return rc;
317 	if (rc)
318 		return -EPERM;
319 
320 	data.hdr.algo = HASH_ALGO_SHA1;
321 	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
322 			   xattr_value_len, &data);
323 	if (rc == 0) {
324 		data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
325 		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
326 					   &data.hdr.xattr.data[1],
327 					   SHA1_DIGEST_SIZE + 1, 0);
328 	} else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
329 		rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
330 	}
331 	return rc;
332 }
333 
334 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
335 		  char *hmac_val)
336 {
337 	struct shash_desc *desc;
338 
339 	desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
340 	if (IS_ERR(desc)) {
341 		pr_info("init_desc failed\n");
342 		return PTR_ERR(desc);
343 	}
344 
345 	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
346 	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
347 	kfree(desc);
348 	return 0;
349 }
350 
351 /*
352  * Get the key from the TPM for the SHA1-HMAC
353  */
354 int evm_init_key(void)
355 {
356 	struct key *evm_key;
357 	struct encrypted_key_payload *ekp;
358 	int rc;
359 
360 	evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
361 	if (IS_ERR(evm_key))
362 		return -ENOENT;
363 
364 	down_read(&evm_key->sem);
365 	ekp = evm_key->payload.data[0];
366 
367 	rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
368 
369 	/* burn the original key contents */
370 	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
371 	up_read(&evm_key->sem);
372 	key_put(evm_key);
373 	return rc;
374 }
375