Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2005-2010 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Mimi Zohar <zohar@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * File: evm_crypto.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	 Using root's kernel master key (kmk), calculate the HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/evm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <keys/encrypted-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/hash_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "evm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define EVMKEY "evm-key"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define MAX_KEY_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static unsigned char evmkey[MAX_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static const int evmkey_len = MAX_KEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct crypto_shash *hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DEFINE_MUTEX(mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define EVM_SET_KEY_BUSY 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static unsigned long evm_set_key_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static const char evm_hmac[] = "hmac(sha1)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * evm_set_key() - set EVM HMAC key from the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * @key: pointer to a buffer with the key data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * @size: length of the key data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * This function allows setting the EVM HMAC key from the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * without using the "encrypted" key subsystem keys. It can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * by the crypto HW kernel module which has its own way of managing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * key length should be between 32 and 128 bytes long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) int evm_set_key(void *key, size_t keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		goto busy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (keylen > MAX_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		goto inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	memcpy(evmkey, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	evm_initialized |= EVM_INIT_HMAC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	pr_info("key initialized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) inval:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) busy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	pr_err("key initialization failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) EXPORT_SYMBOL_GPL(evm_set_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static struct shash_desc *init_desc(char type, uint8_t hash_algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	long rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	const char *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct crypto_shash **tfm, *tmp_tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct shash_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (type == EVM_XATTR_HMAC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (!(evm_initialized & EVM_INIT_HMAC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			pr_err_once("HMAC key is not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			return ERR_PTR(-ENOKEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		tfm = &hmac_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		algo = evm_hmac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (hash_algo >= HASH_ALGO__LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		tfm = &evm_tfm[hash_algo];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		algo = hash_algo_name[hash_algo];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (*tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		goto alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	mutex_lock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (*tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (IS_ERR(tmp_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		pr_err("Can not allocate %s (reason: %ld)\n", algo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		       PTR_ERR(tmp_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		mutex_unlock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return ERR_CAST(tmp_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (type == EVM_XATTR_HMAC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			crypto_free_shash(tmp_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			mutex_unlock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	*tfm = tmp_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	mutex_unlock(&mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		crypto_free_shash(tmp_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	desc->tfm = *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	rc = crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		crypto_free_shash(tmp_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Protect against 'cutting & pasting' security.evm xattr, include inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * specific info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * (Additional directory/file metadata needs to be added for more complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * protection.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			  char type, char *digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct h_misc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		unsigned long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		__u32 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		uid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		gid_t gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	} hmac_misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	memset(&hmac_misc, 0, sizeof(hmac_misc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* Don't include the inode or generation number in portable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * signatures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (type != EVM_XATTR_PORTABLE_DIGSIG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		hmac_misc.ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		hmac_misc.generation = inode->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	/* The hmac uid and gid must be encoded in the initial user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	 * namespace (not the filesystems user namespace) as encoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	 * them in the filesystems user namespace allows an attack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * where first they are written in an unprivileged fuse mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * of a filesystem and then the system is tricked to mount the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * filesystem for real on next boot and trust it because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * everything is signed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	hmac_misc.mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	    type != EVM_XATTR_PORTABLE_DIGSIG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	crypto_shash_final(desc, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * Calculate the HMAC value across the set of protected security xattrs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * Instead of retrieving the requested xattr, for performance, calculate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * the hmac using the requested xattr value. Don't alloc/free memory for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * each xattr, but attempt to re-use the previously allocated memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int evm_calc_hmac_or_hash(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				 const char *req_xattr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 				 const char *req_xattr_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				 size_t req_xattr_value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				 uint8_t type, struct evm_digest *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct inode *inode = d_backing_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct xattr_list *xattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct shash_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	size_t xattr_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	char *xattr_value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	bool ima_present = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (!(inode->i_opflags & IOP_XATTR) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	    inode->i_sb->s_user_ns != &init_user_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	desc = init_desc(type, data->hdr.algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (IS_ERR(desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	data->hdr.length = crypto_shash_digestsize(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	error = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		bool is_ima = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			is_ima = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if ((req_xattr_name && req_xattr_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		    && !strcmp(xattr->name, req_xattr_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			crypto_shash_update(desc, (const u8 *)req_xattr_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 					     req_xattr_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			if (is_ima)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				ima_present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		size = vfs_getxattr_alloc(dentry, xattr->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 					  &xattr_value, xattr_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		if (size == -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		xattr_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (is_ima)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			ima_present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	hmac_add_misc(desc, inode, type, data->digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	/* Portable EVM signatures must include an IMA hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	kfree(xattr_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		  const char *req_xattr_value, size_t req_xattr_value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		  struct evm_digest *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				    req_xattr_value_len, EVM_XATTR_HMAC, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		  const char *req_xattr_value, size_t req_xattr_value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		  char type, struct evm_digest *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				     req_xattr_value_len, type, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	const struct evm_ima_xattr_data *xattr_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct integrity_iint_cache *iint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	iint = integrity_iint_find(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/* Do this the hard way */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (rc <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (rc == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	kfree(xattr_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  * Calculate the hmac and update security.evm xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * Expects to be called with i_mutex locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			const char *xattr_value, size_t xattr_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct inode *inode = d_backing_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct evm_digest data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 * Don't permit any transformation of the EVM xattr if the signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 * is of an immutable type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	rc = evm_is_immutable(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	data.hdr.algo = HASH_ALGO_SHA1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			   xattr_value_len, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 					   &data.hdr.xattr.data[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 					   SHA1_DIGEST_SIZE + 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	} else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		  char *hmac_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	struct shash_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		pr_info("init_desc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * Get the key from the TPM for the SHA1-HMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int evm_init_key(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct key *evm_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct encrypted_key_payload *ekp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (IS_ERR(evm_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	down_read(&evm_key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	ekp = evm_key->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	/* burn the original key contents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	up_read(&evm_key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	key_put(evm_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }