Skip to contentSkip to conversation

pkcs7: Return -EOPNOTSUPP for data-taking algorithms in pkcs7_get_digest()

PATCH In review Muhammad Mahdi Amirpour 1 participant 2 messages Nov 6 – Nov 7

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pkcs7: Return -EOPNOTSUPP for data-taking algorithms in pkcs7_get_digest()

pkcs7_get_digest() is documented to return a digest for IMA to use when verifying a modsig. For signature algorithms where the public key algorithm consumes the raw data rather than a precomputed hash (algo_takes_data, currently ML-DSA), pkcs7_digest() does not produce an intermediate digest.

When such a signature has no authenticated attributes, sig->m points at the raw data and m_free is false, so the existing check rejects it. But when the signature does have authenticated attributes, sig->m is an allocated buffer containing a modified copy of the AuthAttrs and m_free is true. The existing check therefore returns that buffer to the caller as if it were a digest.

Test for algo_takes_data directly and report that no digest is available, before attempting to call pkcs7_digest(). This covers both ML-DSA cases consistently and removes the now-dead m_free check.

The only in-tree caller, ima_collect_modsig(), does not check the return value of pkcs7_get_digest(); for a data-taking algorithm it will now leave the digest fields unset and fall through with no digest, which is the correct outcome for an algorithm that does not produce an intermediate digest. Callers that do care can detect the new -EOPNOTSUPP return.

Fixes f3eccecd782d ("pkcs7: Allow the signing algo to do whatever digestion it wants itself")

---
v2:
- Add algorithm name to the pr_notice_once() message (Ignat)
- Add Fixes: and Cc: stable tags (Ignat)

 crypto/asymmetric_keys/pkcs7_verify.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
crypto/asymmetric_keys/pkcs7_verify.c +6 -5
index 474e2c1ae21b..c2e0eda502e2 100644
@@ -147,14 +147,15 @@ int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
 	if (sinfo == NULL || sinfo->next != NULL)
 		return -EBADMSG;
 
+	if (sinfo->sig->algo_takes_data) {
+		pr_notice_once("%s: No digest for data-taking algorithm (%s)\n",
+			       __func__, sinfo->sig->pkey_algo);
+		return -EOPNOTSUPP;
+	}
+
 	ret = pkcs7_digest(pkcs7, sinfo);
 	if (ret)
 		return ret;
-	if (!sinfo->sig->m_free) {
-		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;
Signature · 1 line
2.55.0
#1 lore raw

Note: [email protected] bounced; the correct address per MAINTAINERS is [email protected]. The patch is otherwise unchanged and available at:

https://lore.kernel.org/all/[email protected]/

Thanks,
Mahdi

Quoted 64 lines · Muhammad Mahdi Amirpour

pkcs7_get_digest() is documented to return a digest for IMA to use when verifying a modsig. For signature algorithms where the public key algorithm consumes the raw data rather than a precomputed hash (algo_takes_data, currently ML-DSA), pkcs7_digest() does not produce an intermediate digest.

When such a signature has no authenticated attributes, sig->m points at the raw data and m_free is false, so the existing check rejects it. But when the signature does have authenticated attributes, sig->m is an allocated buffer containing a modified copy of the AuthAttrs and m_free is true. The existing check therefore returns that buffer to the caller as if it were a digest.

Test for algo_takes_data directly and report that no digest is available, before attempting to call pkcs7_digest(). This covers both ML-DSA cases consistently and removes the now-dead m_free check.

The only in-tree caller, ima_collect_modsig(), does not check the return value of pkcs7_get_digest(); for a data-taking algorithm it will now leave the digest fields unset and fall through with no digest, which is the correct outcome for an algorithm that does not produce an intermediate digest. Callers that do care can detect the new -EOPNOTSUPP return.

Fixes f3eccecd782d ("pkcs7: Allow the signing algo to do whatever digestion it wants itself")

---
v2:
- Add algorithm name to the pr_notice_once() message (Ignat)
- Add Fixes: and Cc: stable tags (Ignat)

 crypto/asymmetric_keys/pkcs7_verify.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
crypto/asymmetric_keys/pkcs7_verify.c +6 -6
index 474e2c1ae21b..c2e0eda502e2 100644
@@ -147,14 +147,15 @@ int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
        if (sinfo == NULL || sinfo->next != NULL)
                return -EBADMSG;
 
+       if (sinfo->sig->algo_takes_data) {
+               pr_notice_once("%s: No digest for data-taking algorithm (%s)\n",
+                              __func__, sinfo->sig->pkey_algo);
+               return -EOPNOTSUPP;
+       }
+
        ret = pkcs7_digest(pkcs7, sinfo);
        if (ret)
                return ret;
-       if (!sinfo->sig->m_free) {
-               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;
--

2.55.0