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/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 "evm.h"
25 
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;
30 
31 struct crypto_shash *hmac_tfm;
32 struct crypto_shash *hash_tfm;
33 
34 static DEFINE_MUTEX(mutex);
35 
36 #define EVM_SET_KEY_BUSY 0
37 
38 static unsigned long evm_set_key_flags;
39 
40 /**
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
44  *
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
48  * keys.
49  *
50  * key length should be between 32 and 128 bytes long
51  */
52 int evm_set_key(void *key, size_t keylen)
53 {
54 	int rc;
55 
56 	rc = -EBUSY;
57 	if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
58 		goto busy;
59 	rc = -EINVAL;
60 	if (keylen > MAX_KEY_SIZE)
61 		goto inval;
62 	memcpy(evmkey, key, keylen);
63 	evm_initialized |= EVM_INIT_HMAC;
64 	pr_info("key initialized\n");
65 	return 0;
66 inval:
67 	clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
68 busy:
69 	pr_err("key initialization failed\n");
70 	return rc;
71 }
72 EXPORT_SYMBOL_GPL(evm_set_key);
73 
74 static struct shash_desc *init_desc(char type)
75 {
76 	long rc;
77 	char *algo;
78 	struct crypto_shash **tfm;
79 	struct shash_desc *desc;
80 
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);
85 		}
86 		tfm = &hmac_tfm;
87 		algo = evm_hmac;
88 	} else {
89 		tfm = &hash_tfm;
90 		algo = evm_hash;
91 	}
92 
93 	if (*tfm == NULL) {
94 		mutex_lock(&mutex);
95 		if (*tfm)
96 			goto out;
97 		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
98 		if (IS_ERR(*tfm)) {
99 			rc = PTR_ERR(*tfm);
100 			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
101 			*tfm = NULL;
102 			mutex_unlock(&mutex);
103 			return ERR_PTR(rc);
104 		}
105 		if (type == EVM_XATTR_HMAC) {
106 			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
107 			if (rc) {
108 				crypto_free_shash(*tfm);
109 				*tfm = NULL;
110 				mutex_unlock(&mutex);
111 				return ERR_PTR(rc);
112 			}
113 		}
114 out:
115 		mutex_unlock(&mutex);
116 	}
117 
118 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
119 			GFP_KERNEL);
120 	if (!desc)
121 		return ERR_PTR(-ENOMEM);
122 
123 	desc->tfm = *tfm;
124 	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
125 
126 	rc = crypto_shash_init(desc);
127 	if (rc) {
128 		kfree(desc);
129 		return ERR_PTR(rc);
130 	}
131 	return desc;
132 }
133 
134 /* Protect against 'cutting & pasting' security.evm xattr, include inode
135  * specific info.
136  *
137  * (Additional directory/file metadata needs to be added for more complete
138  * protection.)
139  */
140 static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
141 			  char type, char *digest)
142 {
143 	struct h_misc {
144 		unsigned long ino;
145 		__u32 generation;
146 		uid_t uid;
147 		gid_t gid;
148 		umode_t mode;
149 	} hmac_misc;
150 
151 	memset(&hmac_misc, 0, sizeof(hmac_misc));
152 	/* Don't include the inode or generation number in portable
153 	 * signatures
154 	 */
155 	if (type != EVM_XATTR_PORTABLE_DIGSIG) {
156 		hmac_misc.ino = inode->i_ino;
157 		hmac_misc.generation = inode->i_generation;
158 	}
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.
166 	 */
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, &inode->i_sb->s_uuid.b[0],
174 				    sizeof(inode->i_sb->s_uuid));
175 	crypto_shash_final(desc, digest);
176 }
177 
178 /*
179  * Calculate the HMAC value across the set of protected security xattrs.
180  *
181  * Instead of retrieving the requested xattr, for performance, calculate
182  * the hmac using the requested xattr value. Don't alloc/free memory for
183  * each xattr, but attempt to re-use the previously allocated memory.
184  */
185 static int evm_calc_hmac_or_hash(struct dentry *dentry,
186 				const char *req_xattr_name,
187 				const char *req_xattr_value,
188 				size_t req_xattr_value_len,
189 				char type, char *digest)
190 {
191 	struct inode *inode = d_backing_inode(dentry);
192 	struct shash_desc *desc;
193 	char **xattrname;
194 	size_t xattr_size = 0;
195 	char *xattr_value = NULL;
196 	int error;
197 	int size;
198 	bool ima_present = false;
199 
200 	if (!(inode->i_opflags & IOP_XATTR))
201 		return -EOPNOTSUPP;
202 
203 	desc = init_desc(type);
204 	if (IS_ERR(desc))
205 		return PTR_ERR(desc);
206 
207 	error = -ENODATA;
208 	for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
209 		bool is_ima = false;
210 
211 		if (strcmp(*xattrname, XATTR_NAME_IMA) == 0)
212 			is_ima = true;
213 
214 		if ((req_xattr_name && req_xattr_value)
215 		    && !strcmp(*xattrname, req_xattr_name)) {
216 			error = 0;
217 			crypto_shash_update(desc, (const u8 *)req_xattr_value,
218 					     req_xattr_value_len);
219 			if (is_ima)
220 				ima_present = true;
221 			continue;
222 		}
223 		size = vfs_getxattr_alloc(dentry, *xattrname,
224 					  &xattr_value, xattr_size, GFP_NOFS);
225 		if (size == -ENOMEM) {
226 			error = -ENOMEM;
227 			goto out;
228 		}
229 		if (size < 0)
230 			continue;
231 
232 		error = 0;
233 		xattr_size = size;
234 		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
235 		if (is_ima)
236 			ima_present = true;
237 	}
238 	hmac_add_misc(desc, inode, type, digest);
239 
240 	/* Portable EVM signatures must include an IMA hash */
241 	if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
242 		return -EPERM;
243 out:
244 	kfree(xattr_value);
245 	kfree(desc);
246 	return error;
247 }
248 
249 int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
250 		  const char *req_xattr_value, size_t req_xattr_value_len,
251 		  char *digest)
252 {
253 	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
254 			       req_xattr_value_len, EVM_XATTR_HMAC, digest);
255 }
256 
257 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
258 		  const char *req_xattr_value, size_t req_xattr_value_len,
259 		  char type, char *digest)
260 {
261 	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
262 				     req_xattr_value_len, type, digest);
263 }
264 
265 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
266 {
267 	const struct evm_ima_xattr_data *xattr_data = NULL;
268 	struct integrity_iint_cache *iint;
269 	int rc = 0;
270 
271 	iint = integrity_iint_find(inode);
272 	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
273 		return 1;
274 
275 	/* Do this the hard way */
276 	rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
277 				GFP_NOFS);
278 	if (rc <= 0) {
279 		if (rc == -ENODATA)
280 			return 0;
281 		return rc;
282 	}
283 	if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
284 		rc = 1;
285 	else
286 		rc = 0;
287 
288 	kfree(xattr_data);
289 	return rc;
290 }
291 
292 
293 /*
294  * Calculate the hmac and update security.evm xattr
295  *
296  * Expects to be called with i_mutex locked.
297  */
298 int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
299 			const char *xattr_value, size_t xattr_value_len)
300 {
301 	struct inode *inode = d_backing_inode(dentry);
302 	struct evm_ima_xattr_data xattr_data;
303 	int rc = 0;
304 
305 	/*
306 	 * Don't permit any transformation of the EVM xattr if the signature
307 	 * is of an immutable type
308 	 */
309 	rc = evm_is_immutable(dentry, inode);
310 	if (rc < 0)
311 		return rc;
312 	if (rc)
313 		return -EPERM;
314 
315 	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
316 			   xattr_value_len, xattr_data.digest);
317 	if (rc == 0) {
318 		xattr_data.type = EVM_XATTR_HMAC;
319 		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
320 					   &xattr_data,
321 					   sizeof(xattr_data), 0);
322 	} else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
323 		rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
324 	}
325 	return rc;
326 }
327 
328 int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
329 		  char *hmac_val)
330 {
331 	struct shash_desc *desc;
332 
333 	desc = init_desc(EVM_XATTR_HMAC);
334 	if (IS_ERR(desc)) {
335 		pr_info("init_desc failed\n");
336 		return PTR_ERR(desc);
337 	}
338 
339 	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
340 	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
341 	kfree(desc);
342 	return 0;
343 }
344 
345 /*
346  * Get the key from the TPM for the SHA1-HMAC
347  */
348 int evm_init_key(void)
349 {
350 	struct key *evm_key;
351 	struct encrypted_key_payload *ekp;
352 	int rc;
353 
354 	evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
355 	if (IS_ERR(evm_key))
356 		return -ENOENT;
357 
358 	down_read(&evm_key->sem);
359 	ekp = evm_key->payload.data[0];
360 
361 	rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
362 
363 	/* burn the original key contents */
364 	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
365 	up_read(&evm_key->sem);
366 	key_put(evm_key);
367 	return rc;
368 }
369