evm_crypto.c 7.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

M
Mimi Zohar 已提交
18 19 20
#include <linux/module.h>
#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>
M
Mimi Zohar 已提交
24 25 26 27 28 29 30
#include "evm.h"

#define EVMKEY "evm-key"
#define MAX_KEY_SIZE 128
static unsigned char evmkey[MAX_KEY_SIZE];
static int evmkey_len = MAX_KEY_SIZE;

31
struct crypto_shash *hmac_tfm;
32
struct crypto_shash *hash_tfm;
33

34 35
static DEFINE_MUTEX(mutex);

36 37 38 39 40 41 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
#define EVM_SET_KEY_BUSY 0

static unsigned long evm_set_key_flags;

/**
 * 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);

74
static struct shash_desc *init_desc(char type)
M
Mimi Zohar 已提交
75
{
76
	long rc;
77 78
	char *algo;
	struct crypto_shash **tfm;
79 80
	struct shash_desc *desc;

81
	if (type == EVM_XATTR_HMAC) {
82 83 84 85
		if (!(evm_initialized & EVM_INIT_HMAC)) {
			pr_err("HMAC key is not set\n");
			return ERR_PTR(-ENOKEY);
		}
86 87 88 89 90 91 92 93
		tfm = &hmac_tfm;
		algo = evm_hmac;
	} else {
		tfm = &hash_tfm;
		algo = evm_hash;
	}

	if (*tfm == NULL) {
94
		mutex_lock(&mutex);
95
		if (*tfm)
96
			goto out;
97 98 99
		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm);
100
			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
101
			*tfm = NULL;
102
			mutex_unlock(&mutex);
103 104
			return ERR_PTR(rc);
		}
105 106 107 108 109
		if (type == EVM_XATTR_HMAC) {
			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
			if (rc) {
				crypto_free_shash(*tfm);
				*tfm = NULL;
110
				mutex_unlock(&mutex);
111 112
				return ERR_PTR(rc);
			}
113
		}
114 115
out:
		mutex_unlock(&mutex);
M
Mimi Zohar 已提交
116
	}
117

118
	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
119 120 121 122
			GFP_KERNEL);
	if (!desc)
		return ERR_PTR(-ENOMEM);

123
	desc->tfm = *tfm;
124 125 126 127 128 129 130 131
	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;

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

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

151
	memset(&hmac_misc, 0, sizeof(hmac_misc));
M
Mimi Zohar 已提交
152 153
	hmac_misc.ino = inode->i_ino;
	hmac_misc.generation = inode->i_generation;
154 155
	hmac_misc.uid = from_kuid(inode->i_sb->s_user_ns, inode->i_uid);
	hmac_misc.gid = from_kgid(inode->i_sb->s_user_ns, inode->i_gid);
M
Mimi Zohar 已提交
156
	hmac_misc.mode = inode->i_mode;
157
	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
158
	if (evm_hmac_attrs & EVM_ATTR_FSUUID)
159 160
		crypto_shash_update(desc, inode->i_sb->s_uuid,
				    sizeof(inode->i_sb->s_uuid));
161
	crypto_shash_final(desc, digest);
M
Mimi Zohar 已提交
162 163 164 165 166 167 168 169 170
}

/*
 * 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.
 */
171 172 173 174 175
static int evm_calc_hmac_or_hash(struct dentry *dentry,
				const char *req_xattr_name,
				const char *req_xattr_value,
				size_t req_xattr_value_len,
				char type, char *digest)
M
Mimi Zohar 已提交
176
{
177
	struct inode *inode = d_backing_inode(dentry);
178
	struct shash_desc *desc;
M
Mimi Zohar 已提交
179 180 181 182 183 184
	char **xattrname;
	size_t xattr_size = 0;
	char *xattr_value = NULL;
	int error;
	int size;

185
	if (!inode->i_op->getxattr)
M
Mimi Zohar 已提交
186
		return -EOPNOTSUPP;
187
	desc = init_desc(type);
188 189
	if (IS_ERR(desc))
		return PTR_ERR(desc);
M
Mimi Zohar 已提交
190 191 192 193 194 195

	error = -ENODATA;
	for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
		if ((req_xattr_name && req_xattr_value)
		    && !strcmp(*xattrname, req_xattr_name)) {
			error = 0;
196 197
			crypto_shash_update(desc, (const u8 *)req_xattr_value,
					     req_xattr_value_len);
M
Mimi Zohar 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210
			continue;
		}
		size = vfs_getxattr_alloc(dentry, *xattrname,
					  &xattr_value, xattr_size, GFP_NOFS);
		if (size == -ENOMEM) {
			error = -ENOMEM;
			goto out;
		}
		if (size < 0)
			continue;

		error = 0;
		xattr_size = size;
211
		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
M
Mimi Zohar 已提交
212
	}
213 214
	hmac_add_misc(desc, inode, digest);

M
Mimi Zohar 已提交
215
out:
216 217
	kfree(xattr_value);
	kfree(desc);
M
Mimi Zohar 已提交
218 219 220
	return error;
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
		  const char *req_xattr_value, size_t req_xattr_value_len,
		  char *digest)
{
	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
				req_xattr_value_len, EVM_XATTR_HMAC, digest);
}

int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
		  const char *req_xattr_value, size_t req_xattr_value_len,
		  char *digest)
{
	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
				req_xattr_value_len, IMA_XATTR_DIGEST, digest);
}

M
Mimi Zohar 已提交
237 238 239 240 241 242 243 244
/*
 * 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)
{
245
	struct inode *inode = d_backing_inode(dentry);
246
	struct evm_ima_xattr_data xattr_data;
M
Mimi Zohar 已提交
247 248 249
	int rc = 0;

	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
250 251 252
			   xattr_value_len, xattr_data.digest);
	if (rc == 0) {
		xattr_data.type = EVM_XATTR_HMAC;
M
Mimi Zohar 已提交
253
		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
254 255
					   &xattr_data,
					   sizeof(xattr_data), 0);
256
	} else if (rc == -ENODATA && inode->i_op->removexattr) {
M
Mimi Zohar 已提交
257
		rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
258
	}
M
Mimi Zohar 已提交
259 260 261
	return rc;
}

262 263 264
int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
		  char *hmac_val)
{
265
	struct shash_desc *desc;
266

267
	desc = init_desc(EVM_XATTR_HMAC);
268
	if (IS_ERR(desc)) {
269
		pr_info("init_desc failed\n");
270
		return PTR_ERR(desc);
271 272
	}

273 274 275
	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
	hmac_add_misc(desc, inode, hmac_val);
	kfree(desc);
276 277 278
	return 0;
}

M
Mimi Zohar 已提交
279 280 281 282 283 284 285
/*
 * Get the key from the TPM for the SHA1-HMAC
 */
int evm_init_key(void)
{
	struct key *evm_key;
	struct encrypted_key_payload *ekp;
286
	int rc;
M
Mimi Zohar 已提交
287 288 289 290 291 292

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

	down_read(&evm_key->sem);
293
	ekp = evm_key->payload.data[0];
294 295 296

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

M
Mimi Zohar 已提交
297 298 299 300 301 302
	/* burn the original key contents */
	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
	up_read(&evm_key->sem);
	key_put(evm_key);
	return rc;
}