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")
Cc: [email protected]
Signed-off-by: Bruno Meneguele <[email protected]>
---
 crypto/asymmetric_keys/pkcs7_verify.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/crypto/asymmetric_keys/pkcs7_verify.c 
b/crypto/asymmetric_keys/pkcs7_verify.c
index 356570b6d6aa..db3f6d080ccf 100644
--- a/crypto/asymmetric_keys/pkcs7_verify.c
+++ b/crypto/asymmetric_keys/pkcs7_verify.c
@@ -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)
-- 
2.55.0


Reply via email to