cregit-Linux how code gets into the kernel

Release 4.7 security/integrity/evm/evm_crypto.c

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


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/xattr.h>
#include <linux/evm.h>
#include <keys/encrypted-type.h>
#include <crypto/hash.h>
#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;


struct crypto_shash *hmac_tfm;

struct crypto_shash *hash_tfm;

static DEFINE_MUTEX(mutex);


#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; }

Contributors

PersonTokensPropCommitsCommitProp
dmitry kasatkindmitry kasatkin88100.00%1100.00%
Total88100.00%1100.00%

EXPORT_SYMBOL_GPL(evm_set_key);
static struct shash_desc *init_desc(char type) { long rc; char *algo; struct crypto_shash **tfm; struct shash_desc *desc; if (type == EVM_XATTR_HMAC) { if (!(evm_initialized & EVM_INIT_HMAC)) { pr_err("HMAC key is not set\n"); return ERR_PTR(-ENOKEY); } tfm = &hmac_tfm; algo = evm_hmac; } else { tfm = &hash_tfm; algo = evm_hash; } if (*tfm == NULL) { mutex_lock(&mutex); if (*tfm) goto out; *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC); if (IS_ERR(*tfm)) { rc = PTR_ERR(*tfm); pr_err("Can not allocate %s (reason: %ld)\n", algo, rc); *tfm = NULL; mutex_unlock(&mutex); 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; mutex_unlock(&mutex); return ERR_PTR(rc); } } out: mutex_unlock(&mutex); } desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm), GFP_KERNEL); if (!desc) return ERR_PTR(-ENOMEM); desc->tfm = *tfm; desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; rc = crypto_shash_init(desc); if (rc) { kfree(desc); return ERR_PTR(rc); } return desc; }

Contributors

PersonTokensPropCommitsCommitProp
dmitry kasatkindmitry kasatkin22778.82%787.50%
mimi zoharmimi zohar6121.18%112.50%
Total288100.00%8100.00%

/* Protect against 'cutting & pasting' security.evm xattr, include inode * specific info. * * (Additional directory/file metadata needs to be added for more complete * protection.) */
static void hmac_add_misc(struct shash_desc *desc, struct inode *inode, 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 = from_kuid(&init_user_ns, inode->i_uid); hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid); hmac_misc.mode = inode->i_mode; crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc)); if (evm_hmac_attrs & EVM_ATTR_FSUUID) crypto_shash_update(desc, inode->i_sb->s_uuid, sizeof(inode->i_sb->s_uuid)); crypto_shash_final(desc, digest); }

Contributors

PersonTokensPropCommitsCommitProp
mimi zoharmimi zohar10767.72%116.67%
dmitry kasatkindmitry kasatkin3924.68%466.67%
eric w. biedermaneric w. biederman127.59%116.67%
Total158100.00%6100.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. */
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) { struct inode *inode = d_backing_inode(dentry); struct shash_desc *desc; char **xattrname; size_t xattr_size = 0; char *xattr_value = NULL; int error; int size; if (!inode->i_op->getxattr) return -EOPNOTSUPP; desc = init_desc(type); if (IS_ERR(desc)) return PTR_ERR(desc); error = -ENODATA; for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { if ((req_xattr_name && req_xattr_value) && !strcmp(*xattrname, req_xattr_name)) { error = 0; crypto_shash_update(desc, (const u8 *)req_xattr_value, req_xattr_value_len); 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; crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size); } hmac_add_misc(desc, inode, digest); out: kfree(xattr_value); kfree(desc); return error; }

Contributors

PersonTokensPropCommitsCommitProp
mimi zoharmimi zohar20884.21%125.00%
dmitry kasatkindmitry kasatkin3614.57%250.00%
david howellsdavid howells31.21%125.00%
Total247100.00%4100.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); }

Contributors

PersonTokensPropCommitsCommitProp
dmitry kasatkindmitry kasatkin43100.00%1100.00%
Total43100.00%1100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
dmitry kasatkindmitry kasatkin43100.00%1100.00%
Total43100.00%1100.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 = d_backing_inode(dentry); struct evm_ima_xattr_data xattr_data; 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; rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM, &xattr_data, sizeof(xattr_data), 0); } else if (rc == -ENODATA && inode->i_op->removexattr) { rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM); } return rc; }

Contributors

PersonTokensPropCommitsCommitProp
mimi zoharmimi zohar9375.00%125.00%
dmitry kasatkindmitry kasatkin2822.58%250.00%
david howellsdavid howells32.42%125.00%
Total124100.00%4100.00%


int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr, char *hmac_val) { struct shash_desc *desc; desc = init_desc(EVM_XATTR_HMAC); if (IS_ERR(desc)) { pr_info("init_desc failed\n"); return PTR_ERR(desc); } crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len); hmac_add_misc(desc, inode, hmac_val); kfree(desc); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
mimi zoharmimi zohar6478.05%125.00%
dmitry kasatkindmitry kasatkin1720.73%250.00%
joe perchesjoe perches11.22%125.00%
Total82100.00%4100.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; 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[0]; rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen); /* burn the original key contents */ memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); up_read(&evm_key->sem); key_put(evm_key); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
mimi zoharmimi zohar10196.19%133.33%
david howellsdavid howells32.86%133.33%
dmitry kasatkindmitry kasatkin10.95%133.33%
Total105100.00%3100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
mimi zoharmimi zohar67753.56%210.53%
dmitry kasatkindmitry kasatkin55844.15%1368.42%
eric w. biedermaneric w. biederman120.95%15.26%
david howellsdavid howells90.71%210.53%
joe perchesjoe perches80.63%15.26%
Total1264100.00%19100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}