evm_crypto.c 5.2 KB
Newer Older
M
Mimi Zohar 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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
 */

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

28 29 30
struct crypto_shash *hmac_tfm;

static struct shash_desc *init_desc(void)
M
Mimi Zohar 已提交
31 32
{
	int rc;
33 34 35 36 37 38 39 40 41 42 43
	struct shash_desc *desc;

	if (hmac_tfm == NULL) {
		hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
		if (IS_ERR(hmac_tfm)) {
			pr_err("Can not allocate %s (reason: %ld)\n",
			       evm_hmac, PTR_ERR(hmac_tfm));
			rc = PTR_ERR(hmac_tfm);
			hmac_tfm = NULL;
			return ERR_PTR(rc);
		}
44 45 46 47 48 49
		rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
		if (rc) {
			crypto_free_shash(hmac_tfm);
			hmac_tfm = NULL;
			return ERR_PTR(rc);
		}
M
Mimi Zohar 已提交
50
	}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
			GFP_KERNEL);
	if (!desc)
		return ERR_PTR(-ENOMEM);

	desc->tfm = hmac_tfm;
	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 已提交
66 67 68 69 70 71 72 73
}

/* Protect against 'cutting & pasting' security.evm xattr, include inode
 * specific info.
 *
 * (Additional directory/file metadata needs to be added for more complete
 * protection.)
 */
74
static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
M
Mimi Zohar 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
			  char *digest)
{
	struct h_misc {
		unsigned long ino;
		__u32 generation;
		uid_t uid;
		gid_t gid;
		umode_t mode;
	} hmac_misc;

	memset(&hmac_misc, 0, sizeof hmac_misc);
	hmac_misc.ino = inode->i_ino;
	hmac_misc.generation = inode->i_generation;
	hmac_misc.uid = inode->i_uid;
	hmac_misc.gid = inode->i_gid;
	hmac_misc.mode = inode->i_mode;
91 92
	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
	crypto_shash_final(desc, digest);
M
Mimi Zohar 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

/*
 * 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.
 */
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)
{
	struct inode *inode = dentry->d_inode;
107
	struct shash_desc *desc;
M
Mimi Zohar 已提交
108 109 110 111 112 113 114 115
	char **xattrname;
	size_t xattr_size = 0;
	char *xattr_value = NULL;
	int error;
	int size;

	if (!inode->i_op || !inode->i_op->getxattr)
		return -EOPNOTSUPP;
116 117 118
	desc = init_desc();
	if (IS_ERR(desc))
		return PTR_ERR(desc);
M
Mimi Zohar 已提交
119 120 121 122 123 124

	error = -ENODATA;
	for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
		if ((req_xattr_name && req_xattr_value)
		    && !strcmp(*xattrname, req_xattr_name)) {
			error = 0;
125 126
			crypto_shash_update(desc, (const u8 *)req_xattr_value,
					     req_xattr_value_len);
M
Mimi Zohar 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
			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;
140
		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
M
Mimi Zohar 已提交
141
	}
142 143
	hmac_add_misc(desc, inode, digest);

M
Mimi Zohar 已提交
144
out:
145 146
	kfree(xattr_value);
	kfree(desc);
M
Mimi Zohar 已提交
147 148 149 150 151 152 153 154 155 156 157 158
	return error;
}

/*
 * 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;
159
	struct evm_ima_xattr_data xattr_data;
M
Mimi Zohar 已提交
160 161 162
	int rc = 0;

	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
163 164 165
			   xattr_value_len, xattr_data.digest);
	if (rc == 0) {
		xattr_data.type = EVM_XATTR_HMAC;
M
Mimi Zohar 已提交
166
		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
167 168 169
					   &xattr_data,
					   sizeof(xattr_data), 0);
	}
M
Mimi Zohar 已提交
170 171 172 173 174
	else if (rc == -ENODATA)
		rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
	return rc;
}

175 176 177
int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
		  char *hmac_val)
{
178
	struct shash_desc *desc;
179

180 181
	desc = init_desc();
	if (IS_ERR(desc)) {
182
		printk(KERN_INFO "init_desc failed\n");
183
		return PTR_ERR(desc);
184 185
	}

186 187 188
	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
	hmac_add_misc(desc, inode, hmac_val);
	kfree(desc);
189 190 191
	return 0;
}

M
Mimi Zohar 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/*
 * 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;
}