Skip to contentSkip to conversation

pkcs7: don't cache a failed digest in pkcs7_digest()

RFC PATCH New Bruno Meneguele 1 participant 2 messages Nov 27 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

RFC PATCH 1/8 pkcs7: don't cache a failed digest in pkcs7_digest()

The pkcs7_digest() function error path leaves sig->m allocated when messageDigest attribute doesn't match the content or the digest calculation fails. Considering it can be called more than once for the same signer, a second call after the first failed would have sig->m incorrectly set and would skip any further check as a successful calculation.

One example of user is IMA, which hits it when measuring first and then appraising a file module signature. With that, free and clear sig->m on failure, so every call repeats the checks.

Fixes e201af16d1ec ("PKCS#7: Introduce pkcs7_get_digest()")
---
 crypto/asymmetric_keys/pkcs7_verify.c | 5 +++++
 1 file changed, 5 insertions(+)
crypto/asymmetric_keys/pkcs7_verify.c +5 -0
index 474e2c1ae21b..356570b6d6aa 100644
@@ -131,6 +131,11 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
 	kfree(desc);
 error_no_desc:
 	crypto_free_shash(tfm);
+	if (ret && sig->m_free) {
+		kfree(sig->m);
+		sig->m = NULL;
+		sig->m_free = false;
+	}
 	kleave(" = %d", ret);
 	return ret;
 }
Signature · 1 line
2.55.0
#2 lore raw

RFC PATCH 2/8 pkcs7: return the content digest from pkcs7_get_digest()

In reply to a message not in the archive

When generating an appended module signature with authenticated attributes, allowed only for ML-DSA with OpenSSL v3.x, pkcs7_digest() stores the file content digest in sig->m and then check it against the messageDigest attribute, later on it overwrites sig->m with a copy of the whole attributes set, setting sig->m_size to its length and returning it to the caller.

As consequence, a caller like IMA always fails on its binary blacklist check because it's, possibly, comparing a file digest (blacklisted digest) against the authenticated attributes data (computed and stored at sig->m). On such case, return the messageDigest attribute instead.

Fixes f3eccecd782d ("pkcs7: Allow the signing algo to do whatever digestion it wants itself")
---
 crypto/asymmetric_keys/pkcs7_verify.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
crypto/asymmetric_keys/pkcs7_verify.c +11 -4
index 356570b6d6aa..db3f6d080ccf 100644
@@ -155,15 +155,22 @@ int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
 	ret = pkcs7_digest(pkcs7, sinfo);
 	if (ret)
 		return ret;
-	if (!sinfo->sig->m_free) {
+
+	/* 'm' holds the digest or a copy of authattr, if the algorithm signs
+	 * the data directly.
+	 */
+	if (sinfo->authattrs) {
+		*buf = sinfo->msgdigest;
+		*len = sinfo->msgdigest_len;
+	} else if (sinfo->sig->m_free) {
+		*buf = sinfo->sig->m;
+		*len = sinfo->sig->m_size;
+	} else {
 		pr_notice_once("%s: No digest available\n", __func__);
 		return -EINVAL; /* TODO: MLDSA doesn't necessarily calculate an
 				 * intermediate digest. */
 	}
 
-	*buf = sinfo->sig->m;
-	*len = sinfo->sig->m_size;
-
 	i = match_string(hash_algo_name, HASH_ALGO__LAST,
 			 sinfo->sig->hash_algo);
 	if (i >= 0)
Signature · 1 line
2.55.0