evm_crypto.c 6.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 21
#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/xattr.h>
#include <keys/encrypted-type.h>
22
#include <crypto/hash.h>
M
Mimi Zohar 已提交
23 24 25 26 27 28 29
#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;

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

33 34
static DEFINE_MUTEX(mutex);

35
static struct shash_desc *init_desc(char type)
M
Mimi Zohar 已提交
36
{
37
	long rc;
38 39
	char *algo;
	struct crypto_shash **tfm;
40 41
	struct shash_desc *desc;

42 43 44 45 46 47 48 49 50
	if (type == EVM_XATTR_HMAC) {
		tfm = &hmac_tfm;
		algo = evm_hmac;
	} else {
		tfm = &hash_tfm;
		algo = evm_hash;
	}

	if (*tfm == NULL) {
51
		mutex_lock(&mutex);
52
		if (*tfm)
53
			goto out;
54 55 56
		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
		if (IS_ERR(*tfm)) {
			rc = PTR_ERR(*tfm);
57
			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
58
			*tfm = NULL;
59
			mutex_unlock(&mutex);
60 61
			return ERR_PTR(rc);
		}
62 63 64 65 66
		if (type == EVM_XATTR_HMAC) {
			rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
			if (rc) {
				crypto_free_shash(*tfm);
				*tfm = NULL;
67
				mutex_unlock(&mutex);
68 69
				return ERR_PTR(rc);
			}
70
		}
71 72
out:
		mutex_unlock(&mutex);
M
Mimi Zohar 已提交
73
	}
74

75
	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
76 77 78 79
			GFP_KERNEL);
	if (!desc)
		return ERR_PTR(-ENOMEM);

80
	desc->tfm = *tfm;
81 82 83 84 85 86 87 88
	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 已提交
89 90 91 92 93 94 95 96
}

/* Protect against 'cutting & pasting' security.evm xattr, include inode
 * specific info.
 *
 * (Additional directory/file metadata needs to be added for more complete
 * protection.)
 */
97
static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
M
Mimi Zohar 已提交
98 99 100 101 102 103 104 105 106 107
			  char *digest)
{
	struct h_misc {
		unsigned long ino;
		__u32 generation;
		uid_t uid;
		gid_t gid;
		umode_t mode;
	} hmac_misc;

108
	memset(&hmac_misc, 0, sizeof(hmac_misc));
M
Mimi Zohar 已提交
109 110
	hmac_misc.ino = inode->i_ino;
	hmac_misc.generation = inode->i_generation;
111 112
	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 已提交
113
	hmac_misc.mode = inode->i_mode;
114
	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
115
	if (evm_hmac_attrs & EVM_ATTR_FSUUID)
116 117
		crypto_shash_update(desc, inode->i_sb->s_uuid,
				    sizeof(inode->i_sb->s_uuid));
118
	crypto_shash_final(desc, digest);
M
Mimi Zohar 已提交
119 120 121 122 123 124 125 126 127
}

/*
 * 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.
 */
128 129 130 131 132
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 已提交
133 134
{
	struct inode *inode = dentry->d_inode;
135
	struct shash_desc *desc;
M
Mimi Zohar 已提交
136 137 138 139 140 141
	char **xattrname;
	size_t xattr_size = 0;
	char *xattr_value = NULL;
	int error;
	int size;

142
	if (!inode->i_op->getxattr)
M
Mimi Zohar 已提交
143
		return -EOPNOTSUPP;
144
	desc = init_desc(type);
145 146
	if (IS_ERR(desc))
		return PTR_ERR(desc);
M
Mimi Zohar 已提交
147 148 149 150 151 152

	error = -ENODATA;
	for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
		if ((req_xattr_name && req_xattr_value)
		    && !strcmp(*xattrname, req_xattr_name)) {
			error = 0;
153 154
			crypto_shash_update(desc, (const u8 *)req_xattr_value,
					     req_xattr_value_len);
M
Mimi Zohar 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
			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;
168
		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
M
Mimi Zohar 已提交
169
	}
170 171
	hmac_add_misc(desc, inode, digest);

M
Mimi Zohar 已提交
172
out:
173 174
	kfree(xattr_value);
	kfree(desc);
M
Mimi Zohar 已提交
175 176 177
	return error;
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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 已提交
194 195 196 197 198 199 200 201 202
/*
 * 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)
{
	struct inode *inode = dentry->d_inode;
203
	struct evm_ima_xattr_data xattr_data;
M
Mimi Zohar 已提交
204 205 206
	int rc = 0;

	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
207 208 209
			   xattr_value_len, xattr_data.digest);
	if (rc == 0) {
		xattr_data.type = EVM_XATTR_HMAC;
M
Mimi Zohar 已提交
210
		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
211 212
					   &xattr_data,
					   sizeof(xattr_data), 0);
213
	} else if (rc == -ENODATA && inode->i_op->removexattr) {
M
Mimi Zohar 已提交
214
		rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
215
	}
M
Mimi Zohar 已提交
216 217 218
	return rc;
}

219 220 221
int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
		  char *hmac_val)
{
222
	struct shash_desc *desc;
223

224
	desc = init_desc(EVM_XATTR_HMAC);
225
	if (IS_ERR(desc)) {
226
		pr_info("init_desc failed\n");
227
		return PTR_ERR(desc);
228 229
	}

230 231 232
	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
	hmac_add_misc(desc, inode, hmac_val);
	kfree(desc);
233 234 235
	return 0;
}

M
Mimi Zohar 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/*
 * Get the key from the TPM for the SHA1-HMAC
 */
int evm_init_key(void)
{
	struct key *evm_key;
	struct encrypted_key_payload *ekp;
	int rc = 0;

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

	down_read(&evm_key->sem);
	ekp = evm_key->payload.data;
	if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
		rc = -EINVAL;
		goto out;
	}
	memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
out:
	/* burn the original key contents */
	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
	up_read(&evm_key->sem);
	key_put(evm_key);
	return rc;
}