Re: [RFC v2.0 3/3] evm: digital signature support

From: Kasatkin, Dmitry
Date: Tue Sep 06 2011 - 09:16:03 EST


Please ignore this patch. It was sent by mistake...
Check:
evm: digital signature verification support

- Dmitry

On Tue, Sep 6, 2011 at 4:11 PM, Dmitry Kasatkin
<dmitry.kasatkin@xxxxxxxxx> wrote:
> When building an image, which has to be flashed to different devices,
> an HMAC cannot be used to sign file metadata, as the HMAC key is different
> on every device. File metadata can be protected using digital signature.
> This patch enables RSA signature based integrity verification.
>
> Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@xxxxxxxxx>
> Acked-by: Mimi Zohar <zohar@xxxxxxxxxx>
> ---
> Âsecurity/integrity/evm/Kconfig   Â|  14 ++++
> Âsecurity/integrity/evm/evm.h    Â|  12 ++++
> Âsecurity/integrity/evm/evm_crypto.c | Â 66 ++++++++++++++-----
> Âsecurity/integrity/evm/evm_main.c  | Â125 +++++++++++++++++++++++++++++++----
> Â4 files changed, 187 insertions(+), 30 deletions(-)
>
> diff --git a/security/integrity/evm/Kconfig b/security/integrity/evm/Kconfig
> index 884617d..84eea75 100644
> --- a/security/integrity/evm/Kconfig
> +++ b/security/integrity/evm/Kconfig
> @@ -12,3 +12,17 @@ config EVM
> Â Â Â Â Âintegrity attacks.
>
> Â Â Â Â ÂIf you are unsure how to answer this question, answer N.
> +
> +config EVM_DIGSIG
> + Â Â Â boolean "EVM Digital Signature support"
> + Â Â Â depends on EVM
> + Â Â Â default n
> + Â Â Â select CRYPTO_KSIGN_RSA
> + Â Â Â help
> + Â Â Â Â When building an image, which has to be flashed to different
> + Â Â Â Â devices, an HMAC cannot be used to sign file metadata, as
> + Â Â Â Â the HMAC key is different on every device.
> + Â Â Â Â File metadata can be protected using digital signature.
> + Â Â Â Â This option enables RSA signature based integrity verification.
> +
> + Â Â Â Â If unsure, say N.
> diff --git a/security/integrity/evm/evm.h b/security/integrity/evm/evm.h
> index d320f51..c885247 100644
> --- a/security/integrity/evm/evm.h
> +++ b/security/integrity/evm/evm.h
> @@ -12,14 +12,21 @@
> Â* File: evm.h
> Â*
> Â*/
> +
> +#ifndef __INTEGRITY_EVM_H
> +#define __INTEGRITY_EVM_H
> +
> Â#include <linux/xattr.h>
> Â#include <linux/security.h>
> +
> Â#include "../integrity.h"
>
> Âextern int evm_initialized;
> Âextern char *evm_hmac;
> +extern char *evm_hash;
>
> Âextern struct crypto_shash *hmac_tfm;
> +extern struct crypto_shash *hash_tfm;
>
> Â/* List of EVM protected security xattrs */
> Âextern char *evm_config_xattrnames[];
> @@ -32,7 +39,12 @@ extern int evm_update_evmxattr(struct dentry *dentry,
> Âextern 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);
> +extern 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);
> Âextern int evm_init_hmac(struct inode *inode, const struct xattr *xattr,
> Â Â Â Â Â Â Â Â Â Â Â Â char *hmac_val);
> Âextern int evm_init_secfs(void);
> Âextern void evm_cleanup_secfs(void);
> +
> +#endif
> diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
> index 5dd5b140..847a2d7 100644
> --- a/security/integrity/evm/evm_crypto.c
> +++ b/security/integrity/evm/evm_crypto.c
> @@ -26,34 +26,48 @@ 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 struct shash_desc *init_desc(void)
> +static struct shash_desc *init_desc(const char type)
> Â{
> Â Â Â Âint rc;
> + Â Â Â char *algo;
> + Â Â Â struct crypto_shash **tfm;
> Â Â Â Âstruct shash_desc *desc;
>
> - Â Â Â if (hmac_tfm == NULL) {
> - Â Â Â Â Â Â Â hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
> - Â Â Â Â Â Â Â if (IS_ERR(hmac_tfm)) {
> + Â Â Â if (type == EVM_XATTR_HMAC) {
> + Â Â Â Â Â Â Â tfm = &hmac_tfm;
> + Â Â Â Â Â Â Â algo = evm_hmac;
> + Â Â Â } else {
> + Â Â Â Â Â Â Â tfm = &hash_tfm;
> + Â Â Â Â Â Â Â algo = evm_hash;
> + Â Â Â }
> +
> + Â Â Â if (*tfm == NULL) {
> + Â Â Â Â Â Â Â *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
> + Â Â Â Â Â Â Â if (IS_ERR(*tfm)) {
> Â Â Â Â Â Â Â Â Â Â Â Âpr_err("Can not allocate %s (reason: %ld)\n",
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âevm_hmac, PTR_ERR(hmac_tfm));
> - Â Â Â Â Â Â Â Â Â Â Â rc = PTR_ERR(hmac_tfm);
> - Â Â Â Â Â Â Â Â Â Â Â hmac_tfm = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âalgo, PTR_ERR(*tfm));
> + Â Â Â Â Â Â Â Â Â Â Â rc = PTR_ERR(*tfm);
> + Â Â Â Â Â Â Â Â Â Â Â *tfm = NULL;
> Â Â Â Â Â Â Â Â Â Â Â Âreturn ERR_PTR(rc);
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
>
> - Â Â Â desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
> + Â Â Â desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> Â Â Â Â Â Â Â Â Â Â Â ÂGFP_KERNEL);
> Â Â Â Âif (!desc)
> Â Â Â Â Â Â Â Âreturn ERR_PTR(-ENOMEM);
>
> - Â Â Â desc->tfm = hmac_tfm;
> + Â Â Â desc->tfm = *tfm;
> Â Â Â Âdesc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
>
> - Â Â Â rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
> - Â Â Â if (rc)
> - Â Â Â Â Â Â Â goto out;
> + Â Â Â if (type == EVM_XATTR_HMAC) {
> + Â Â Â Â Â Â Â rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
> + Â Â Â Â Â Â Â if (rc)
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> Â Â Â Ârc = crypto_shash_init(desc);
> Âout:
> Â Â Â Âif (rc) {
> @@ -97,9 +111,11 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
> Â* 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)
> +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 = dentry->d_inode;
> Â Â Â Âstruct shash_desc *desc;
> @@ -111,7 +127,7 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
>
> Â Â Â Âif (!inode->i_op || !inode->i_op->getxattr)
> Â Â Â Â Â Â Â Âreturn -EOPNOTSUPP;
> - Â Â Â desc = init_desc();
> + Â Â Â desc = init_desc(type);
> Â Â Â Âif (IS_ERR(desc))
> Â Â Â Â Â Â Â Âreturn PTR_ERR(desc);
>
> @@ -145,6 +161,22 @@ out:
> Â Â Â Âreturn error;
> Â}
>
> +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);
> +}
> +
> Â/*
> Â* Calculate the hmac and update security.evm xattr
> Â*
> @@ -175,7 +207,7 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
> Â{
> Â Â Â Âstruct shash_desc *desc;
>
> - Â Â Â desc = init_desc();
> + Â Â Â desc = init_desc(EVM_XATTR_HMAC);
> Â Â Â Âif (IS_ERR(desc)) {
> Â Â Â Â Â Â Â Âprintk(KERN_INFO "init_desc failed\n");
> Â Â Â Â Â Â Â Âreturn PTR_ERR(desc);
> diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c
> index 8b2eff9..d8ac109 100644
> --- a/security/integrity/evm/evm_main.c
> +++ b/security/integrity/evm/evm_main.c
> @@ -18,6 +18,8 @@
> Â#include <linux/crypto.h>
> Â#include <linux/xattr.h>
> Â#include <linux/integrity.h>
> +#include <linux/key-type.h>
> +#include <linux/crypto/ksign.h>
> Â#include <linux/evm.h>
> Â#include <crypto/hash.h>
> Â#include "evm.h"
> @@ -25,6 +27,7 @@
> Âint evm_initialized;
>
> Âchar *evm_hmac = "hmac(sha1)";
> +char *evm_hash = "sha1";
>
> Âchar *evm_config_xattrnames[] = {
> Â#ifdef CONFIG_SECURITY_SELINUX
> @@ -40,6 +43,8 @@ char *evm_config_xattrnames[] = {
> Â Â Â ÂNULL
> Â};
>
> +static struct key *evm_keyring;
> +
> Âstatic int evm_fixmode;
> Âstatic int __init evm_set_fixmode(char *str)
> Â{
> @@ -49,6 +54,56 @@ static int __init evm_set_fixmode(char *str)
> Â}
> Â__setup("evm=", evm_set_fixmode);
>
> +static int evm_find_protected_xattrs(struct dentry *dentry)
> +{
> + Â Â Â struct inode *inode = dentry->d_inode;
> + Â Â Â char **xattr;
> + Â Â Â int error;
> + Â Â Â int count = 0;
> +
> + Â Â Â if (!inode->i_op || !inode->i_op->getxattr)
> + Â Â Â Â Â Â Â return -EOPNOTSUPP;
> +
> + Â Â Â for (xattr = evm_config_xattrnames; *xattr != NULL; xattr++) {
> + Â Â Â Â Â Â Â error = inode->i_op->getxattr(dentry, *xattr, NULL, 0);
> + Â Â Â Â Â Â Â if (error < 0) {
> + Â Â Â Â Â Â Â Â Â Â Â if (error == -ENODATA)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â continue;
> + Â Â Â Â Â Â Â Â Â Â Â return error;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â count++;
> + Â Â Â }
> +
> + Â Â Â return count;
> +}
> +
> +#ifdef CONFIG_EVM_DIGSIG
> +
> +int evm_sign_verify(const char *sig, int siglen,
> + Â Â Â Â Â Â Â Â Â Â Âconst char *digest, int digestlen)
> +{
> + Â Â Â if (!evm_keyring) {
> + Â Â Â Â Â Â Â evm_keyring = request_key(&key_type_keyring, "_evm", NULL);
> + Â Â Â Â Â Â Â if (IS_ERR(evm_keyring)) {
> + Â Â Â Â Â Â Â Â Â Â Â pr_err("no evm keyring: %ld\n", PTR_ERR(evm_keyring));
> + Â Â Â Â Â Â Â Â Â Â Â evm_keyring = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â return PTR_ERR(evm_keyring);
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â return ksign_verify(evm_keyring, sig, siglen, digest, digestlen);
> +}
> +
> +#else
> +
> +static inline int evm_sign_verify(const char *sig, int siglen,
> + Â Â Â Â Â Â Â Â Â Â Âconst char *digest, int digestlen)
> +{
> + Â Â Â return -EOPNOTSUPP;
> +}
> +
> +#endif /* CONFIG_EVM_DIGSIG */
> +
> Â/*
> Â* evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
> Â*
> @@ -68,32 +123,71 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â size_t xattr_value_len,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct integrity_iint_cache *iint)
> Â{
> - Â Â Â struct evm_ima_xattr_data xattr_data;
> + Â Â Â struct evm_ima_xattr_data *xattr_data = NULL;
> + Â Â Â struct evm_ima_xattr_data calc;
> Â Â Â Âenum integrity_status evm_status = INTEGRITY_PASS;
> - Â Â Â int rc;
> + Â Â Â int rc, xattr_len;
>
> Â Â Â Âif (iint && iint->evm_status == INTEGRITY_PASS)
> Â Â Â Â Â Â Â Âreturn iint->evm_status;
>
> Â Â Â Â/* if status is not PASS, try to check again - against -ENOMEM */
>
> - Â Â Â rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
> - Â Â Â Â Â Â Â Â Â Â Â Â Âxattr_value_len, xattr_data.digest);
> - Â Â Â if (rc < 0) {
> - Â Â Â Â Â Â Â evm_status = (rc == -ENODATA)
> - Â Â Â Â Â Â Â Â Â ? INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
> + Â Â Â /* first need to know the sig type */
> + Â Â Â rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â GFP_NOFS);
> + Â Â Â if (rc <= 0) {
> + Â Â Â Â Â Â Â if (rc == 0)
> + Â Â Â Â Â Â Â Â Â Â Â evm_status = INTEGRITY_FAIL; /* empty */
> + Â Â Â Â Â Â Â else if (rc == -ENODATA) {
> + Â Â Â Â Â Â Â Â Â Â Â rc = evm_find_protected_xattrs(dentry);
> + Â Â Â Â Â Â Â Â Â Â Â if (rc > 0)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â evm_status = INTEGRITY_NOLABEL;
> + Â Â Â Â Â Â Â Â Â Â Â else if (rc == 0)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â evm_status = INTEGRITY_NOXATTRS; /* new file */
> + Â Â Â Â Â Â Â }
> Â Â Â Â Â Â Â Âgoto out;
> Â Â Â Â}
>
> - Â Â Â xattr_data.type = EVM_XATTR_HMAC;
> - Â Â Â rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data,
> - Â Â Â Â Â Â Â Â Â Â Â Â Âsizeof xattr_data, GFP_NOFS);
> - Â Â Â if (rc < 0)
> - Â Â Â Â Â Â Â evm_status = (rc == -ENODATA)
> - Â Â Â Â Â Â Â Â Â ? INTEGRITY_NOLABEL : INTEGRITY_FAIL;
> + Â Â Â xattr_len = rc - 1;
> +
> + Â Â Â /* check value type */
> + Â Â Â switch (xattr_data->type) {
> + Â Â Â case EVM_XATTR_HMAC:
> + Â Â Â Â Â Â Â rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âxattr_value_len, calc.digest);
> + Â Â Â Â Â Â Â if (rc)
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â rc = memcmp(xattr_data->digest, calc.digest,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â sizeof(calc.digest));
> + Â Â Â Â Â Â Â if (rc)
> + Â Â Â Â Â Â Â Â Â Â Â rc = -EINVAL;
> + Â Â Â Â Â Â Â break;
> + Â Â Â case EVM_IMA_XATTR_DIGSIG:
> + Â Â Â Â Â Â Â rc = evm_calc_hash(dentry, xattr_name, xattr_value,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â xattr_value_len, calc.digest);
> + Â Â Â Â Â Â Â if (rc)
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â rc = evm_sign_verify(xattr_data->digest, xattr_len,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âcalc.digest, sizeof(calc.digest));
> + Â Â Â Â Â Â Â if (!rc) {
> + Â Â Â Â Â Â Â Â Â Â Â /* we probably want to replace rsa with hmac here */
> + Â Â Â Â Â Â Â Â Â Â Â evm_update_evmxattr(dentry, xattr_name, xattr_value,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âxattr_value_len);
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â break;
> + Â Â Â default:
> + Â Â Â Â Â Â Â rc = -EINVAL;
> + Â Â Â Â Â Â Â break;
> + Â Â Â }
> +
> + Â Â Â if (rc)
> + Â Â Â Â Â Â Â evm_status = (rc == -ENODATA) ?
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
> Âout:
> Â Â Â Âif (iint)
> Â Â Â Â Â Â Â Âiint->evm_status = evm_status;
> + Â Â Â kfree(xattr_data);
> Â Â Â Âreturn evm_status;
> Â}
>
> @@ -357,6 +451,8 @@ static int __init init_evm(void)
> Â Â Â Â Â Â Â Âprintk(KERN_INFO "EVM: Error registering secfs\n");
> Â Â Â Â Â Â Â Âgoto err;
> Â Â Â Â}
> +
> + Â Â Â return 0;
> Âerr:
> Â Â Â Âreturn error;
> Â}
> @@ -364,8 +460,11 @@ err:
> Âstatic void __exit cleanup_evm(void)
> Â{
> Â Â Â Âevm_cleanup_secfs();
> + Â Â Â key_put(evm_keyring);
> Â Â Â Âif (hmac_tfm)
> Â Â Â Â Â Â Â Âcrypto_free_shash(hmac_tfm);
> + Â Â Â if (hash_tfm)
> + Â Â Â Â Â Â Â crypto_free_shash(hash_tfm);
> Â}
>
> Â/*
> --
> 1.7.4.1
>
>
N‹§²æìr¸›yúèšØb²X¬¶ÇvØ^–)Þ{.nÇ+‰·¥Š{±‘êçzX§¶›¡Ü}©ž²ÆzÚ&j:+v‰¨¾«‘êçzZ+€Ê+zf£¢·hšˆ§~†­†Ûiÿûàz¹®w¥¢¸?™¨è­Ú&¢)ßf”ù^jÇy§m…á@A«a¶Úÿ 0¶ìh®å’i