crypto: pkcs7: Return no-digest for data-taking algorithms
Subsystems (3)
PATCH 1/1 crypto: pkcs7: Return no-digest for data-taking algorithms
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.
--- crypto/asymmetric_keys/pkcs7_verify.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
crypto/asymmetric_keys/pkcs7_verify.c +6 -5
@@ -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\n" , + __func__); +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
Ignat Korchagin
maintainerQuoted 18 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->mpoints at the raw data andm_freeis false, so the existing check rejects it. But when the signature does have authenticated attributes,sig->mis an allocated buffer containing a modified copy of the AuthAttrs andm_freeis true. The existing check therefore returns that buffer to the caller as if it were a digest.Test for
algo_takes_datadirectly and report that no digest is available, before attempting to callpkcs7_digest(). This covers both ML-DSA cases consistently and removes the now-deadm_freecheck.
Quoted 8 lines
The only in-tree caller,
ima_collect_modsig(), does not check the return value ofpkcs7_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-EOPNOTSUPPreturn.Signed-off-by Muhammad Mahdi Amirpour <[email protected]>
Would be nice to include "Fixes: " and "Cc: stable.."
Quoted 9 lines
--- crypto/asymmetric_keys/pkcs7_verify.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)crypto/asymmetric_keys/pkcs7_verify.c +0
-0
crypto/asymmetric_keys/pkcs7_verify.c int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len, · line 151
Quoted 5 lines
crypto/asymmetric_keys/pkcs7_verify.c +2
-0@@ -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\n",
We should actually include what the algorithm is here probably for easier debugging
Quoted 16 lines
crypto/asymmetric_keys/pkcs7_verify.c +4
-6+ __func__); +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; --
Quoted 3 lines
2.55.0
Ignat