Skip to contentSkip to conversation

evm: Don't deadlock if a crypto algorithm is unavailable

PATCH Superseded Matthew Garrett 2 participants 4 messages Jun 1, 2018 – Jun 4, 2018

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 evm: Don't deadlock if a crypto algorithm is unavailable

Trying to instantiate a non-existent crypto algorithm will cause the kernel to trigger a module load. If EVM appraisal is enabled, this will in turn trigger appraisal of the module, which will fail because the crypto algorithm isn't available. Add a CRYPTO_NOLOAD flag and skip module loading if it's set, and add that flag in the EVM case.

---
 crypto/api.c                        | 2 +-
 include/linux/crypto.h              | 5 +++++
 security/integrity/evm/evm_crypto.c | 3 ++-
 3 files changed, 8 insertions(+), 2 deletions(-)
crypto/api.c +1 -1
index 0ee632bba064..7aca9f86c5f3 100644
@@ -229,7 +229,7 @@ static struct crypto_alg *crypto_larval_lookup(const char *name, u32 type,
 	mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
 
 	alg = crypto_alg_lookup(name, type, mask);
-	if (!alg) {
+	if (!alg && !(mask & CRYPTO_NOLOAD)) {
 		request_module("crypto-%s", name);
 
 		if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
include/linux/crypto.h +5 -0
index 6eb06101089f..e8839d3a7559 100644
@@ -112,6 +112,11 @@
  */
 #define CRYPTO_ALG_OPTIONAL_KEY		0x00004000
 
+/*
+ * Don't trigger module loading
+ */
+#define CRYPTO_NOLOAD			0x00008000
+
 /*
  * Transform masks and values (for crt_flags).
  */
security/integrity/evm/evm_crypto.c +2 -1
index a46fba322340..ccafc9468611 100644
@@ -97,7 +97,8 @@ static struct shash_desc *init_desc(char type)
 		mutex_lock(&mutex);
 		if (*tfm)
 			goto out;
-		*tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
+		*tfm = crypto_alloc_shash(algo, 0,
+					  CRYPTO_ALG_ASYNC | CRYPTO_NOLOAD);
 		if (IS_ERR(*tfm)) {
 			rc = PTR_ERR(*tfm);
 			pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
Signature · 1 line
2.17.1.1185.g55be947832-goog
#1 lore raw

PATCH 2/2 evm: Allow non-SHA1 digital signatures

SHA1 is reasonable in HMAC constructs, but it's desirable to be able to use stronger hashes in digital signatures. Modify the EVM crypto code so the hash type is imported from the digital signature and passed down to the hash calculation code, and return the digest size to higher layers for validation.

---
 security/integrity/evm/evm.h        | 10 ++++--
 security/integrity/evm/evm_crypto.c | 47 +++++++++++++++--------------
 security/integrity/evm/evm_main.c   | 19 +++++++-----
 3 files changed, 45 insertions(+), 31 deletions(-)
security/integrity/evm/evm.h +8 -2
index 45c4a89c02ff..a8289fbf2f0c 100644
@@ -42,6 +42,11 @@ extern struct crypto_shash *hash_tfm;
 /* List of EVM protected security xattrs */
 extern char *evm_config_xattrnames[];
 
+struct evm_digest {
+	struct ima_digest_data hdr;
+	char digest[IMA_MAX_DIGEST_SIZE];
+} __packed;
+
 int evm_init_key(void);
 int evm_update_evmxattr(struct dentry *dentry,
 			const char *req_xattr_name,
@@ -49,10 +54,11 @@ int evm_update_evmxattr(struct dentry *dentry,
 			size_t req_xattr_value_len);
 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);
+		  size_t req_xattr_value_len, struct evm_digest *data);
 int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
 		  const char *req_xattr_value,
-		  size_t req_xattr_value_len, char type, char *digest);
+		  size_t req_xattr_value_len, char type,
+		  struct evm_digest *data);
 int evm_init_hmac(struct inode *inode, const struct xattr *xattr,
 		  char *hmac_val);
 int evm_init_secfs(void);
security/integrity/evm/evm_crypto.c +25 -22

135 lines not on this page · raw message

security/integrity/evm/evm_main.c +12 -7
index 9ea9c19a545c..88ea9c1962d6 100644
@@ -25,6 +25,7 @@
 #include <linux/magic.h>
 
 #include <crypto/hash.h>
+#include <crypto/hash_info.h>
 #include <crypto/algapi.h>
 #include "evm.h"
 
@@ -122,8 +123,9 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 					     struct integrity_iint_cache *iint)
 {
 	struct evm_ima_xattr_data *xattr_data = NULL;
-	struct evm_ima_xattr_data calc;
+	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
+	struct evm_digest digest;
 	struct inode *inode;
 	int rc, xattr_len;
 
@@ -159,25 +161,28 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 			evm_status = INTEGRITY_FAIL;
 			goto out;
 		}
+
+		digest.hdr.algo = HASH_ALGO_SHA1;
 		rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
-				   xattr_value_len, calc.digest);
+				   xattr_value_len, &digest);
 		if (rc)
 			break;
-		rc = crypto_memneq(xattr_data->digest, calc.digest,
-			    sizeof(calc.digest));
+		rc = crypto_memneq(xattr_data->digest, digest.digest,
+				   SHA1_DIGEST_SIZE);
 		if (rc)
 			rc = -EINVAL;
 		break;
 	case EVM_IMA_XATTR_DIGSIG:
 	case EVM_XATTR_PORTABLE_DIGSIG:
+		hdr = (struct signature_v2_hdr *)xattr_data;
+		digest.hdr.algo = hdr->hash_algo;
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type,
-				   calc.digest);
+				   xattr_value_len, xattr_data->type, &digest);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					calc.digest, sizeof(calc.digest));
+					digest.digest, digest.hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Signature · 1 line
2.17.1.1185.g55be947832-goog
maintainer
#2 lore raw
In reply to Matthew Garrett
Quoted 7 lines · Matthew Garrett

Trying to instantiate a non-existent crypto algorithm will cause the kernel to trigger a module load. If EVM appraisal is enabled, this will in turn trigger appraisal of the module, which will fail because the crypto algorithm isn't available. Add a CRYPTO_NOLOAD flag and skip module loading if it's set, and add that flag in the EVM case.

I don't get it. Without your patch it will fail because the EVM appraisal fails. With your patch it will fail because there is no algorithm registered. So what's the difference?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 8 lines · Herbert Xu
Quoted 6 lines · Matthew Garrett

Trying to instantiate a non-existent crypto algorithm will cause the kernel to trigger a module load. If EVM appraisal is enabled, this will in turn trigger appraisal of the module, which will fail because the crypto algorithm isn't available. Add a CRYPTO_NOLOAD flag and skip module loading if it's set, and add that flag in the EVM case.

Quoted 5 lines
Quoted 1 line

I don't get it. Without your patch it will fail because the EVM appraisal fails. With your patch it will fail because there is no algorithm registered. So what's the difference?

Without my patch it will deadlock as it recursively calls back into EVM to perform the module appraisal. Sorry, the description was unclear on that point.