Files
OrangePi_CM5/security/integrity/evm/evm_crypto.c
T

257 lines
6.1 KiB
C
Raw Normal View History

2011-03-15 16:12:09 -04:00
/*
* 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>
2011-03-09 15:07:36 -05:00
#include <crypto/hash.h>
2011-03-15 16:12:09 -04:00
#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;
2011-03-09 15:07:36 -05:00
struct crypto_shash *hmac_tfm;
2011-09-01 14:41:40 +03:00
struct crypto_shash *hash_tfm;
2011-03-09 15:07:36 -05:00
2011-12-05 13:17:42 +02:00
static DEFINE_MUTEX(mutex);
2012-01-09 12:16:48 +11:00
static struct shash_desc *init_desc(char type)
2011-03-15 16:12:09 -04:00
{
2011-12-05 13:17:42 +02:00
long rc;
2011-09-01 14:41:40 +03:00
char *algo;
struct crypto_shash **tfm;
2011-03-09 15:07:36 -05:00
struct shash_desc *desc;
2011-09-01 14:41:40 +03:00
if (type == EVM_XATTR_HMAC) {
tfm = &hmac_tfm;
algo = evm_hmac;
} else {
tfm = &hash_tfm;
algo = evm_hash;
}
if (*tfm == NULL) {
2011-12-05 13:17:42 +02:00
mutex_lock(&mutex);
2011-12-05 13:17:42 +02:00
if (*tfm)
2011-12-05 13:17:42 +02:00
goto out;
2011-09-01 14:41:40 +03:00
*tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(*tfm)) {
rc = PTR_ERR(*tfm);
2011-12-05 13:17:42 +02:00
pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
2011-09-01 14:41:40 +03:00
*tfm = NULL;
2011-12-05 13:17:42 +02:00
mutex_unlock(&mutex);
2011-03-09 15:07:36 -05:00
return ERR_PTR(rc);
}
if (type == EVM_XATTR_HMAC) {
rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
if (rc) {
crypto_free_shash(*tfm);
*tfm = NULL;
2011-12-05 13:17:42 +02:00
mutex_unlock(&mutex);
return ERR_PTR(rc);
}
}
2011-12-05 13:17:42 +02:00
out:
mutex_unlock(&mutex);
2011-03-15 16:12:09 -04:00
}
2011-03-09 15:07:36 -05:00
2011-09-01 14:41:40 +03:00
desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
2011-03-09 15:07:36 -05:00
GFP_KERNEL);
if (!desc)
return ERR_PTR(-ENOMEM);
2011-09-01 14:41:40 +03:00
desc->tfm = *tfm;
2011-03-09 15:07:36 -05:00
desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
rc = crypto_shash_init(desc);
if (rc) {
kfree(desc);
return ERR_PTR(rc);
}
return desc;
2011-03-15 16:12:09 -04:00
}
/* Protect against 'cutting & pasting' security.evm xattr, include inode
* specific info.
*
* (Additional directory/file metadata needs to be added for more complete
* protection.)
*/
2011-03-09 15:07:36 -05:00
static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
2011-03-15 16:12:09 -04:00
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;
2011-03-09 15:07:36 -05:00
crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
crypto_shash_final(desc, digest);
2011-03-15 16:12:09 -04:00
}
/*
* 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.
*/
2011-09-01 14:41:40 +03:00
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)
2011-03-15 16:12:09 -04:00
{
struct inode *inode = dentry->d_inode;
2011-03-09 15:07:36 -05:00
struct shash_desc *desc;
2011-03-15 16:12:09 -04:00
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;
2011-09-01 14:41:40 +03:00
desc = init_desc(type);
2011-03-09 15:07:36 -05:00
if (IS_ERR(desc))
return PTR_ERR(desc);
2011-03-15 16:12:09 -04:00
error = -ENODATA;
for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
if ((req_xattr_name && req_xattr_value)
&& !strcmp(*xattrname, req_xattr_name)) {
error = 0;
2011-03-09 15:07:36 -05:00
crypto_shash_update(desc, (const u8 *)req_xattr_value,
req_xattr_value_len);
2011-03-15 16:12:09 -04:00
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;
2011-03-09 15:07:36 -05:00
crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
2011-03-15 16:12:09 -04:00
}
2011-03-09 15:07:36 -05:00
hmac_add_misc(desc, inode, digest);
2011-03-15 16:12:09 -04:00
out:
2011-03-09 15:07:36 -05:00
kfree(xattr_value);
kfree(desc);
2011-03-15 16:12:09 -04:00
return error;
}
2011-09-01 14:41:40 +03:00
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);
}
2011-03-15 16:12:09 -04:00
/*
* 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;
struct evm_ima_xattr_data xattr_data;
2011-03-15 16:12:09 -04:00
int rc = 0;
rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
xattr_value_len, xattr_data.digest);
if (rc == 0) {
xattr_data.type = EVM_XATTR_HMAC;
2011-03-15 16:12:09 -04:00
rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
&xattr_data,
sizeof(xattr_data), 0);
}
2011-03-15 16:12:09 -04:00
else if (rc == -ENODATA)
rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
return rc;
}
int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
char *hmac_val)
{
2011-03-09 15:07:36 -05:00
struct shash_desc *desc;
2011-09-01 14:41:40 +03:00
desc = init_desc(EVM_XATTR_HMAC);
2011-03-09 15:07:36 -05:00
if (IS_ERR(desc)) {
printk(KERN_INFO "init_desc failed\n");
2011-03-09 15:07:36 -05:00
return PTR_ERR(desc);
}
2011-03-09 15:07:36 -05:00
crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
hmac_add_misc(desc, inode, hmac_val);
kfree(desc);
return 0;
}
2011-03-15 16:12:09 -04:00
/*
* 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;
}