evm_crypto.c 9.3 KB
Newer Older
M
Mimi Zohar 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2005-2010 IBM Corporation
 *
 * Authors:
 * Mimi Zohar <zohar@us.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2 of the License.
 *
 * File: evm_crypto.c
 *	 Using root's kernel master key (kmk), calculate the HMAC
 */

16 17
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

18
#include <linux/export.h>
M
Mimi Zohar 已提交
19 20
#include <linux/crypto.h>
#include <linux/xattr.h>
21
#include <linux/evm.h>
M
Mimi Zohar 已提交
22
#include <keys/encrypted-type.h>
23
#include <crypto/hash.h>
24
#include <crypto/hash_info.h>
M
Mimi Zohar 已提交
25 26 27 28 29
#include "evm.h"

#define EVMKEY "evm-key"
#define MAX_KEY_SIZE 128
static unsigned char evmkey[MAX_KEY_SIZE];
30
static const int evmkey_len = MAX_KEY_SIZE;
M
Mimi Zohar 已提交
31

32
struct crypto_shash *hmac_tfm;
33
static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
34

35 36
static DEFINE_MUTEX(mutex);

37 38 39 40
#define EVM_SET_KEY_BUSY 0

static unsigned long evm_set_key_flags;

41
static const char evm_hmac[] = "hmac(sha1)";
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/**
 * evm_set_key() - set EVM HMAC key from the kernel
 * @key: pointer to a buffer with the key data
 * @size: length of the key data
 *
 * This function allows setting the EVM HMAC key from the kernel
 * without using the "encrypted" key subsystem keys. It can be used
 * by the crypto HW kernel module which has its own way of managing
 * keys.
 *
 * key length should be between 32 and 128 bytes long
 */
int evm_set_key(void *key, size_t keylen)
{
	int rc;

	rc = -EBUSY;
	if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
		goto busy;
	rc = -EINVAL;
	if (keylen > MAX_KEY_SIZE)
		goto inval;
	memcpy(evmkey, key, keylen);
	evm_initialized |= EVM_INIT_HMAC;
	pr_info("key initialized\n");
	return 0;
inval:
	clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
busy:
	pr_err("key initialization failed\n");
	return rc;
}
EXPORT_SYMBOL_GPL(evm_set_key);

77
static struct shash_desc *init_desc(char type, uint8_t hash_algo)
M
Mimi Zohar 已提交
78
{
79
	long rc;
80
	const char *algo;
81
	struct crypto_shash **tfm;
82 83
	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 88
			return ERR_PTR(-ENOKEY);
		}
89 90 91
		tfm = &hmac_tfm;
		algo = evm_hmac;
	} else {
92 93
		tfm = &evm_tfm[hash_algo];
		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 102
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm);
103
			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
104
			*tfm = NULL;
105
			mutex_unlock(&mutex);
106 107
			return ERR_PTR(rc);
		}
108 109 110 111 112
		if (type == EVM_XATTR_HMAC) {
			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
			if (rc) {
				crypto_free_shash(*tfm);
				*tfm = NULL;
113
				mutex_unlock(&mutex);
114 115
				return ERR_PTR(rc);
			}
116
		}
117 118
out:
		mutex_unlock(&mutex);
M
Mimi Zohar 已提交
119
	}
120

121
	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
122 123 124 125
			GFP_KERNEL);
	if (!desc)
		return ERR_PTR(-ENOMEM);

126
	desc->tfm = *tfm;
127 128 129 130 131 132 133

	rc = crypto_shash_init(desc);
	if (rc) {
		kfree(desc);
		return ERR_PTR(rc);
	}
	return desc;
M
Mimi Zohar 已提交
134 135 136 137 138 139 140 141
}

/* Protect against 'cutting & pasting' security.evm xattr, include inode
 * specific info.
 *
 * (Additional directory/file metadata needs to be added for more complete
 * protection.)
 */
142
static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
143
			  char type, char *digest)
M
Mimi Zohar 已提交
144 145 146 147 148 149 150 151 152
{
	struct h_misc {
		unsigned long ino;
		__u32 generation;
		uid_t uid;
		gid_t gid;
		umode_t mode;
	} hmac_misc;

153
	memset(&hmac_misc, 0, sizeof(hmac_misc));
154 155 156 157 158 159 160
	/* Don't include the inode or generation number in portable
	 * signatures
	 */
	if (type != EVM_XATTR_PORTABLE_DIGSIG) {
		hmac_misc.ino = inode->i_ino;
		hmac_misc.generation = inode->i_generation;
	}
161 162 163 164 165 166 167 168 169 170
	/* The hmac uid and gid must be encoded in the initial user
	 * namespace (not the filesystems user namespace) as encoding
	 * them in the filesystems user namespace allows an attack
	 * where first they are written in an unprivileged fuse mount
	 * of a filesystem and then the system is tricked to mount the
	 * filesystem for real on next boot and trust it because
	 * everything is signed.
	 */
	hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
	hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
M
Mimi Zohar 已提交
171
	hmac_misc.mode = inode->i_mode;
172
	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
173 174
	if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
	    type != EVM_XATTR_PORTABLE_DIGSIG)
175
		crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
176
	crypto_shash_final(desc, digest);
M
Mimi Zohar 已提交
177 178 179 180 181 182 183 184 185
}

/*
 * Calculate the HMAC value across the set of protected security xattrs.
 *
 * Instead of retrieving the requested xattr, for performance, calculate
 * the hmac using the requested xattr value. Don't alloc/free memory for
 * each xattr, but attempt to re-use the previously allocated memory.
 */
186
static int evm_calc_hmac_or_hash(struct dentry *dentry,
187 188 189 190
				 const char *req_xattr_name,
				 const char *req_xattr_value,
				 size_t req_xattr_value_len,
				 uint8_t type, struct evm_digest *data)
M
Mimi Zohar 已提交
191
{
192
	struct inode *inode = d_backing_inode(dentry);
193
	struct xattr_list *xattr;
194
	struct shash_desc *desc;
M
Mimi Zohar 已提交
195 196 197 198
	size_t xattr_size = 0;
	char *xattr_value = NULL;
	int error;
	int size;
199
	bool ima_present = false;
M
Mimi Zohar 已提交
200

201 202
	if (!(inode->i_opflags & IOP_XATTR) ||
	    inode->i_sb->s_user_ns != &init_user_ns)
M
Mimi Zohar 已提交
203
		return -EOPNOTSUPP;
204

205
	desc = init_desc(type, data->hdr.algo);
206 207
	if (IS_ERR(desc))
		return PTR_ERR(desc);
M
Mimi Zohar 已提交
208

209 210
	data->hdr.length = crypto_shash_digestsize(desc->tfm);

M
Mimi Zohar 已提交
211
	error = -ENODATA;
212
	list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
213 214
		bool is_ima = false;

215
		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
216 217
			is_ima = true;

M
Mimi Zohar 已提交
218
		if ((req_xattr_name && req_xattr_value)
219
		    && !strcmp(xattr->name, req_xattr_name)) {
M
Mimi Zohar 已提交
220
			error = 0;
221 222
			crypto_shash_update(desc, (const u8 *)req_xattr_value,
					     req_xattr_value_len);
223 224
			if (is_ima)
				ima_present = true;
M
Mimi Zohar 已提交
225 226
			continue;
		}
227
		size = vfs_getxattr_alloc(dentry, xattr->name,
M
Mimi Zohar 已提交
228 229 230 231 232 233 234 235 236 237
					  &xattr_value, xattr_size, GFP_NOFS);
		if (size == -ENOMEM) {
			error = -ENOMEM;
			goto out;
		}
		if (size < 0)
			continue;

		error = 0;
		xattr_size = size;
238
		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
239 240
		if (is_ima)
			ima_present = true;
M
Mimi Zohar 已提交
241
	}
242
	hmac_add_misc(desc, inode, type, data->digest);
243

244 245 246
	/* Portable EVM signatures must include an IMA hash */
	if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
		return -EPERM;
M
Mimi Zohar 已提交
247
out:
248 249
	kfree(xattr_value);
	kfree(desc);
M
Mimi Zohar 已提交
250 251 252
	return error;
}

253 254
int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
		  const char *req_xattr_value, size_t req_xattr_value_len,
255
		  struct evm_digest *data)
256 257
{
	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
258
				    req_xattr_value_len, EVM_XATTR_HMAC, data);
259 260 261 262
}

int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
		  const char *req_xattr_value, size_t req_xattr_value_len,
263
		  char type, struct evm_digest *data)
264 265
{
	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
266
				     req_xattr_value_len, type, data);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
{
	const struct evm_ima_xattr_data *xattr_data = NULL;
	struct integrity_iint_cache *iint;
	int rc = 0;

	iint = integrity_iint_find(inode);
	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
		return 1;

	/* Do this the hard way */
	rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
				GFP_NOFS);
	if (rc <= 0) {
		if (rc == -ENODATA)
			return 0;
		return rc;
	}
	if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
		rc = 1;
	else
		rc = 0;

	kfree(xattr_data);
	return rc;
294 295
}

296

M
Mimi Zohar 已提交
297 298 299 300 301 302 303 304
/*
 * Calculate the hmac and update security.evm xattr
 *
 * Expects to be called with i_mutex locked.
 */
int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
			const char *xattr_value, size_t xattr_value_len)
{
305
	struct inode *inode = d_backing_inode(dentry);
306
	struct evm_digest data;
M
Mimi Zohar 已提交
307 308
	int rc = 0;

309 310 311 312 313 314 315 316 317 318
	/*
	 * Don't permit any transformation of the EVM xattr if the signature
	 * is of an immutable type
	 */
	rc = evm_is_immutable(dentry, inode);
	if (rc < 0)
		return rc;
	if (rc)
		return -EPERM;

319
	data.hdr.algo = HASH_ALGO_SHA1;
M
Mimi Zohar 已提交
320
	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
321
			   xattr_value_len, &data);
322
	if (rc == 0) {
323
		data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
M
Mimi Zohar 已提交
324
		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
325 326
					   &data.hdr.xattr.data[1],
					   SHA1_DIGEST_SIZE + 1, 0);
327 328
	} else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
		rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
329
	}
M
Mimi Zohar 已提交
330 331 332
	return rc;
}

333 334 335
int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
		  char *hmac_val)
{
336
	struct shash_desc *desc;
337

338
	desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
339
	if (IS_ERR(desc)) {
340
		pr_info("init_desc failed\n");
341
		return PTR_ERR(desc);
342 343
	}

344
	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
345
	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
346
	kfree(desc);
347 348 349
	return 0;
}

M
Mimi Zohar 已提交
350 351 352 353 354 355 356
/*
 * Get the key from the TPM for the SHA1-HMAC
 */
int evm_init_key(void)
{
	struct key *evm_key;
	struct encrypted_key_payload *ekp;
357
	int rc;
M
Mimi Zohar 已提交
358 359 360 361 362 363

	evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
	if (IS_ERR(evm_key))
		return -ENOENT;

	down_read(&evm_key->sem);
364
	ekp = evm_key->payload.data[0];
365 366 367

	rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);

M
Mimi Zohar 已提交
368 369 370 371 372 373
	/* burn the original key contents */
	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
	up_read(&evm_key->sem);
	key_put(evm_key);
	return rc;
}