* [RFC PATCH 1/8] pkcs7: don't cache a failed digest in pkcs7_digest()
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 2/8] pkcs7: return the content digest from pkcs7_get_digest() Bruno Meneguele
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele, stable
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()")
Cc: stable@vger.kernel.org
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
crypto/asymmetric_keys/pkcs7_verify.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c
index 474e2c1ae21b..356570b6d6aa 100644
--- a/crypto/asymmetric_keys/pkcs7_verify.c
+++ b/crypto/asymmetric_keys/pkcs7_verify.c
@@ -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;
}
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 2/8] pkcs7: return the content digest from pkcs7_get_digest()
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 1/8] pkcs7: don't cache a failed digest in pkcs7_digest() Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 3/8] pkcs7: support multiple signers in pkcs7_get_digest() Bruno Meneguele
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele, stable
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: stable@vger.kernel.org
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
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
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 3/8] pkcs7: support multiple signers in pkcs7_get_digest()
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 1/8] pkcs7: don't cache a failed digest in pkcs7_digest() Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 2/8] pkcs7: return the content digest from pkcs7_get_digest() Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 4/8] ima: get the modsig content digest of each signer Bruno Meneguele
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele
Current pkcs7_get_digest() code fails with any message that has more than
one SignerInfo, so the content digest of a multi-signed message can't be
obtained, and it also doesn't support signers whose algorithm signs the
data directly (eg. ML-DSA) without authenticated attributes, which has no
content digest to report.
Give it a signer zero-index and add pkcs7_get_signer_count(), so callers
can get the content digest of each signer of a message. Report a signer
with no content digest as a zero-length digest rather than an error, and
return the name of the signature algorithm when it signs the data directly.
This commit prepares the ground for ima-modsigv2 template, maintaining
current field templates behavior.
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
crypto/asymmetric_keys/pkcs7_verify.c | 63 ++++++++++++++++++++++-----
include/crypto/pkcs7.h | 7 ++-
security/integrity/ima/ima_modsig.c | 10 ++++-
3 files changed, 66 insertions(+), 14 deletions(-)
diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c
index db3f6d080ccf..bd55f86b9464 100644
--- a/crypto/asymmetric_keys/pkcs7_verify.c
+++ b/crypto/asymmetric_keys/pkcs7_verify.c
@@ -140,17 +140,61 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
return ret;
}
-int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
- enum hash_algo *hash_algo)
+/**
+ * pkcs7_get_signer_count - Count the signers of a PKCS#7 message
+ * @pkcs7: The preparsed PKCS#7 message to access
+ *
+ * Return: the number of SignerInfos in the message.
+ */
+unsigned int pkcs7_get_signer_count(const struct pkcs7_message *pkcs7)
+{
+ const struct pkcs7_signed_info *sinfo;
+ unsigned int count = 0;
+
+ for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next)
+ count++;
+
+ return count;
+}
+
+/**
+ * pkcs7_get_digest - Get the content digest of one signer of a PKCS#7 message
+ * @pkcs7: The PKCS#7 message
+ * @index: Zero-based index of the signer, in the message's (DER) order
+ * @buf: The content digest (NULL when there is none)
+ * @len: The content digest length (0 when there is none)
+ * @hash_algo: The digest algorithm (HASH_ALGO__LAST when there is none)
+ * @pkey_algo: The signature algorithm name if the data was signed directly
+ *
+ * The content digest is the digest of the signed content under the signer's
+ * digest algorithm. A signer whose algorithm signs the data directly has no
+ * digest when authenticated/signed attributes is absent, we return 0 in
+ * this case.
+ *
+ * Return: 0 on success, -EINVAL if there is no signer at 'index',
+ * -EKEYREJECTED if the messageDigest attribute doesn't match the content, or
+ * another negative error code.
+ */
+int pkcs7_get_digest(struct pkcs7_message *pkcs7, unsigned int index,
+ const u8 **buf, u32 *len, enum hash_algo *hash_algo,
+ const char **pkey_algo)
{
struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;
+ unsigned int n;
int i, ret;
- /*
- * This function doesn't support messages with more than one signature.
- */
- if (sinfo == NULL || sinfo->next != NULL)
- return -EBADMSG;
+ *buf = NULL;
+ *len = 0;
+ *hash_algo = HASH_ALGO__LAST;
+ *pkey_algo = NULL;
+
+ for (n = 0; sinfo && n < index; n++)
+ sinfo = sinfo->next;
+ if (!sinfo)
+ return -EINVAL;
+
+ if (sinfo->sig->algo_takes_data)
+ *pkey_algo = sinfo->sig->pkey_algo;
ret = pkcs7_digest(pkcs7, sinfo);
if (ret)
@@ -166,9 +210,8 @@ int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
*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. */
+ /* No intermediate digest and no hash_algo to match. */
+ return 0;
}
i = match_string(hash_algo_name, HASH_ALGO__LAST,
diff --git a/include/crypto/pkcs7.h b/include/crypto/pkcs7.h
index 38ec7f5f9041..b237fe01b2f4 100644
--- a/include/crypto/pkcs7.h
+++ b/include/crypto/pkcs7.h
@@ -41,7 +41,10 @@ extern int pkcs7_verify(struct pkcs7_message *pkcs7,
extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
const void *data, size_t datalen);
-extern int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf,
- u32 *len, enum hash_algo *hash_algo);
+extern unsigned int pkcs7_get_signer_count(const struct pkcs7_message *pkcs7);
+
+extern int pkcs7_get_digest(struct pkcs7_message *pkcs7, unsigned int index,
+ const u8 **buf, u32 *len, enum hash_algo *hash_algo,
+ const char **pkey_algo);
#endif /* _CRYPTO_PKCS7_H */
diff --git a/security/integrity/ima/ima_modsig.c b/security/integrity/ima/ima_modsig.c
index 632c746fd81e..15e499f7dba1 100644
--- a/security/integrity/ima/ima_modsig.c
+++ b/security/integrity/ima/ima_modsig.c
@@ -99,6 +99,7 @@ int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
*/
void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size)
{
+ const char *pkey_algo;
int rc;
/*
@@ -111,9 +112,14 @@ void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size)
if (rc)
return;
+ /* There's room for the digest of a single signer only. */
+ if (pkcs7_get_signer_count(modsig->pkcs7_msg) != 1)
+ return;
+
/* Ask the PKCS7 code to calculate the file hash. */
- rc = pkcs7_get_digest(modsig->pkcs7_msg, &modsig->digest,
- &modsig->digest_size, &modsig->hash_algo);
+ rc = pkcs7_get_digest(modsig->pkcs7_msg, 0, &modsig->digest,
+ &modsig->digest_size, &modsig->hash_algo,
+ &pkey_algo);
}
int ima_modsig_verify(struct key *keyring, const struct modsig *modsig)
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 4/8] ima: get the modsig content digest of each signer
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
` (2 preceding siblings ...)
2026-11-27 2:59 ` [RFC PATCH 3/8] pkcs7: support multiple signers in pkcs7_get_digest() Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 5/8] ima: record data-signing algorithms by name in 'd-modsig' Bruno Meneguele
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele
The current code supports only one signer through ima_collect_modsig(),
which is used for the 'd-modsig' field (from 'ima-modsig' template) and the
is_binary_blacklisted() check. This single signer digest is taken from the
PKCS#7/CMS message and kept in the modsig structure. However, in case of
multiple signatures, PKCS#7 code already keeps the digest of each signer,
thus we can use pkcs7_get_digest() to collect the digest of any of them
without storing the data anywhere.
Stop keeping a copy of content digest in struct modsig and have
ima_get_modsig_digest() get the digest lazily based on signer's index in
the DER blob. To keep backwards compatibility, 'd-modsig' and the blacklist
check still ask the digest of a single signed (index 0) modsig, so the
behavior doesn't change.
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
security/integrity/ima/ima.h | 15 +++++--
security/integrity/ima/ima_appraise.c | 8 ++--
security/integrity/ima/ima_modsig.c | 54 ++++++++++-------------
security/integrity/ima/ima_template_lib.c | 10 ++++-
4 files changed, 48 insertions(+), 39 deletions(-)
diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
index b502854f28ee..055a2a696071 100644
--- a/security/integrity/ima/ima.h
+++ b/security/integrity/ima/ima.h
@@ -560,8 +560,10 @@ static inline void __init init_ima_appraise_lsm(const struct lsm_id *lsmid)
int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
struct modsig **modsig);
void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
-int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
- const u8 **digest, u32 *digest_size);
+unsigned int ima_get_modsig_signer_count(const struct modsig *modsig);
+int ima_get_modsig_digest(const struct modsig *modsig, unsigned int index,
+ enum hash_algo *algo, const u8 **digest,
+ u32 *digest_size, const char **sig_algo);
int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
u32 *data_len);
void ima_free_modsig(struct modsig *modsig);
@@ -577,9 +579,16 @@ static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
{
}
+static inline unsigned int
+ima_get_modsig_signer_count(const struct modsig *modsig)
+{
+ return 0;
+}
+
static inline int ima_get_modsig_digest(const struct modsig *modsig,
+ unsigned int index,
enum hash_algo *algo, const u8 **digest,
- u32 *digest_size)
+ u32 *digest_size, const char **sig_algo)
{
return -EOPNOTSUPP;
}
diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index 134174d2e3f0..8aeec2778984 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -429,6 +429,7 @@ int ima_check_blacklist(struct ima_iint_cache *iint,
const struct modsig *modsig, int pcr)
{
enum hash_algo hash_algo;
+ const char *sig_algo;
const u8 *digest = NULL;
u32 digestsize = 0;
int rc = 0;
@@ -437,9 +438,10 @@ int ima_check_blacklist(struct ima_iint_cache *iint,
return 0;
if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
- ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
-
- rc = is_binary_blacklisted(digest, digestsize);
+ if (ima_get_modsig_signer_count(modsig) == 1 &&
+ !ima_get_modsig_digest(modsig, 0, &hash_algo, &digest,
+ &digestsize, &sig_algo))
+ rc = is_binary_blacklisted(digest, digestsize);
} else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash)
rc = is_binary_blacklisted(iint->ima_hash->digest, iint->ima_hash->length);
diff --git a/security/integrity/ima/ima_modsig.c b/security/integrity/ima/ima_modsig.c
index 15e499f7dba1..f69102d6b0f0 100644
--- a/security/integrity/ima/ima_modsig.c
+++ b/security/integrity/ima/ima_modsig.c
@@ -18,11 +18,8 @@
struct modsig {
struct pkcs7_message *pkcs7_msg;
- enum hash_algo hash_algo;
-
- /* This digest will go in the 'd-modsig' field of the IMA template. */
- const u8 *digest;
- u32 digest_size;
+ /* The file content was supplied datached. */
+ bool detached;
/*
* This is what will go to the measurement list if the template requires
@@ -79,47 +76,31 @@ int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
memcpy(hdr->raw_pkcs7, buf + buf_len, sig_len);
- /* We don't know the hash algorithm yet. */
- hdr->hash_algo = HASH_ALGO__LAST;
-
*modsig = hdr;
return 0;
}
/**
- * ima_collect_modsig - Calculate the file hash without the appended signature.
+ * ima_collect_modsig - Supply the file content without the appended signature.
* @modsig: parsed module signature
* @buf: data to verify the signature on
* @size: data size
*
* Since the modsig is part of the file contents, the hash used in its signature
* isn't the same one ordinarily calculated by IMA. Therefore PKCS7 code
- * calculates a separate one for signature verification.
+ * calculates a separate one for each signer when it's first needed.
*/
void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size)
{
- const char *pkey_algo;
- int rc;
-
/*
* Provide the file contents (minus the appended sig) so that the PKCS7
* code can calculate the file hash.
*/
size -= modsig->raw_pkcs7_len + strlen(MODULE_SIGNATURE_MARKER) +
sizeof(struct module_signature);
- rc = pkcs7_supply_detached_data(modsig->pkcs7_msg, buf, size);
- if (rc)
- return;
-
- /* There's room for the digest of a single signer only. */
- if (pkcs7_get_signer_count(modsig->pkcs7_msg) != 1)
- return;
-
- /* Ask the PKCS7 code to calculate the file hash. */
- rc = pkcs7_get_digest(modsig->pkcs7_msg, 0, &modsig->digest,
- &modsig->digest_size, &modsig->hash_algo,
- &pkey_algo);
+ modsig->detached = !pkcs7_supply_detached_data(modsig->pkcs7_msg,
+ buf, size);
}
int ima_modsig_verify(struct key *keyring, const struct modsig *modsig)
@@ -128,14 +109,25 @@ int ima_modsig_verify(struct key *keyring, const struct modsig *modsig)
VERIFYING_MODULE_SIGNATURE, NULL, NULL);
}
-int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
- const u8 **digest, u32 *digest_size)
+unsigned int ima_get_modsig_signer_count(const struct modsig *modsig)
{
- *algo = modsig->hash_algo;
- *digest = modsig->digest;
- *digest_size = modsig->digest_size;
+ return pkcs7_get_signer_count(modsig->pkcs7_msg);
+}
- return 0;
+/*
+ * ima_get_modsig_digest - Get the content digest of one signer of the modsig.
+ *
+ * See pkcs7_get_digest() for the results.
+ */
+int ima_get_modsig_digest(const struct modsig *modsig, unsigned int index,
+ enum hash_algo *algo, const u8 **digest,
+ u32 *digest_size, const char **sig_algo)
+{
+ if (!modsig->detached)
+ return -EBADMSG;
+
+ return pkcs7_get_digest(modsig->pkcs7_msg, index, digest, digest_size,
+ algo, sig_algo);
}
int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c
index 9bd80ca1159a..0609a8f7a127 100644
--- a/security/integrity/ima/ima_template_lib.c
+++ b/security/integrity/ima/ima_template_lib.c
@@ -471,10 +471,16 @@ int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
cur_digest = NULL;
cur_digestsize = 0;
} else {
+ const char *sig_algo;
int rc;
- rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
- &cur_digest, &cur_digestsize);
+ /* 'd-modsig' supports the digest of a single signer only. */
+ if (ima_get_modsig_signer_count(event_data->modsig) != 1)
+ return -EINVAL;
+
+ rc = ima_get_modsig_digest(event_data->modsig, 0, &hash_algo,
+ &cur_digest, &cur_digestsize,
+ &sig_algo);
if (rc)
return rc;
else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 5/8] ima: record data-signing algorithms by name in 'd-modsig'
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
` (3 preceding siblings ...)
2026-11-27 2:59 ` [RFC PATCH 4/8] ima: get the modsig content digest of each signer Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 6/8] ima: add the 'd-modsig-list' template field Bruno Meneguele
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele
A signer whose algorithm signs the data directly (eg. ML-DSA) has no
content digest unless it carries authenticated attributes. With
authenticated attributes, which sign-file uses for ML-DSA when built
against OpenSSL 3.x, 'd-modsig' holds the messageDigest attribute, while
the signature covers the attributes and not that digest.
Record such a signer by the name of its signature algorithm instead of a
hash algorithm, with an empty digest (e.g. "mldsa65:" '\0'), with or
without authenticated attributes. A verifier needs to traverse the 'modsig'
field to find out authattr's presence. Signers that sign a digest are
recorded as before.
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
Documentation/security/IMA-templates.rst | 4 +-
security/integrity/ima/ima_template_lib.c | 113 ++++++++++++++--------
2 files changed, 75 insertions(+), 42 deletions(-)
diff --git a/Documentation/security/IMA-templates.rst b/Documentation/security/IMA-templates.rst
index 15b4add314fc..4ec0440b27f1 100644
--- a/Documentation/security/IMA-templates.rst
+++ b/Documentation/security/IMA-templates.rst
@@ -69,7 +69,9 @@ descriptors by adding their identifier to the format string
algorithm (field format: <hash algo>:digest);
- 'd-ngv2': same as d-ng, but prefixed with the "ima" or "verity" digest type
(field format: <digest type>:<hash algo>:digest);
- - 'd-modsig': the digest of the event without the appended modsig;
+ - 'd-modsig': the digest of the event without the appended modsig or a signature
+ algorithm name followed by an empty digest (<signing algo>: \0) when direct data
+ algorithms are used, e.g. ML-DSA;
- 'n-ng': the name of the event, without size limitations;
- 'sig': the file signature, based on either the file's/fsverity's digest[1],
or the EVM portable signature, if 'security.ima' contains a file hash.
diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c
index 0609a8f7a127..a2aeaae83065 100644
--- a/security/integrity/ima/ima_template_lib.c
+++ b/security/integrity/ima/ima_template_lib.c
@@ -286,24 +286,27 @@ int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
return 0;
}
+/*
+ * digest formats:
+ * - DATA_FMT_DIGEST: digest
+ * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,
+ * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
+ * <digest type> + ':' + <hash algo> + ':' + '\0' + digest,
+ *
+ * where 'DATA_FMT_DIGEST' is the original digest format ('d')
+ * with a hash size limitation of 20 bytes,
+ * where <digest type> is either "ima" or "verity",
+ * where <hash algo> is the hash_algo_name[] string.
+ */
+#define IMA_MAX_DIGEST_ENTRY_SIZE (DIGEST_TYPE_NAME_LEN_MAX + \
+ CRYPTO_MAX_ALG_NAME + 2 + \
+ IMA_MAX_DIGEST_SIZE)
+
static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
u8 digest_type, u8 hash_algo,
struct ima_field_data *field_data)
{
- /*
- * digest formats:
- * - DATA_FMT_DIGEST: digest
- * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,
- * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
- * <digest type> + ':' + <hash algo> + ':' + '\0' + digest,
- *
- * where 'DATA_FMT_DIGEST' is the original digest format ('d')
- * with a hash size limitation of 20 bytes,
- * where <digest type> is either "ima" or "verity",
- * where <hash algo> is the hash_algo_name[] string.
- */
- u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 +
- IMA_MAX_DIGEST_SIZE] = { 0 };
+ u8 buffer[IMA_MAX_DIGEST_ENTRY_SIZE] = { 0 };
enum data_formats fmt = DATA_FMT_DIGEST;
u32 offset = 0;
@@ -451,6 +454,44 @@ int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
field_data);
}
+/*
+ * ima_modsig_digest_entry - encode the content digest of one modsig signer
+ * @modsig: the appended signature
+ * @index: zero-based index of the signer
+ * @entry: output buffer
+ *
+ * The entry has the DATA_FMT_DIGEST_WITH_ALGO format. A signer whose algorithm
+ * signs the data directly (eg. ML-DSA) is recorded by the name of that
+ * algorithm instead, with empty digest (eg. "mldsa65:"), regardless of
+ * authenticated attributes being present or not.
+ *
+ * Return: the entry length on success, a negative error code otherwise.
+ */
+static int ima_modsig_digest_entry(const struct modsig *modsig,
+ unsigned int index, u8 *entry)
+{
+ const char *sig_algo;
+ enum hash_algo algo;
+ const u8 *digest;
+ u32 size;
+ int len, rc;
+
+ rc = ima_get_modsig_digest(modsig, index, &algo, &digest, &size,
+ &sig_algo);
+ if (rc)
+ return rc;
+
+ if (sig_algo)
+ return sprintf(entry, "%s:", sig_algo) + 1;
+
+ if (algo >= HASH_ALGO__LAST || !size || size > IMA_MAX_DIGEST_SIZE)
+ return -EINVAL;
+
+ len = sprintf(entry, "%s:", hash_algo_name[algo]) + 1;
+ memcpy(entry + len, digest, size);
+ return len + size;
+}
+
/*
* This function writes the digest of the file which is expected to match the
* digest contained in the file's appended signature.
@@ -458,39 +499,29 @@ int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
struct ima_field_data *field_data)
{
- enum hash_algo hash_algo;
- const u8 *cur_digest;
- u32 cur_digestsize;
+ /* 'd-modsig' (DATA_FMT_DIGEST_WITH_ALGO) doesn't contain digest type */
+ u8 entry[IMA_MAX_DIGEST_ENTRY_SIZE - DIGEST_TYPE_NAME_LEN_MAX] = { 0 };
+ int len;
if (!event_data->modsig)
return 0;
- if (event_data->violation) {
+ if (event_data->violation)
/* Recording a violation. */
- hash_algo = HASH_ALGO_SHA1;
- cur_digest = NULL;
- cur_digestsize = 0;
- } else {
- const char *sig_algo;
- int rc;
-
- /* 'd-modsig' supports the digest of a single signer only. */
- if (ima_get_modsig_signer_count(event_data->modsig) != 1)
- return -EINVAL;
-
- rc = ima_get_modsig_digest(event_data->modsig, 0, &hash_algo,
- &cur_digest, &cur_digestsize,
- &sig_algo);
- if (rc)
- return rc;
- else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
- /* There was some error collecting the digest. */
- return -EINVAL;
- }
+ return ima_eventdigest_init_common(NULL, 0, DIGEST_TYPE__LAST,
+ HASH_ALGO_SHA1, field_data);
- return ima_eventdigest_init_common(cur_digest, cur_digestsize,
- DIGEST_TYPE__LAST, hash_algo,
- field_data);
+ /* 'd-modsig' supports the digest of a single signer only. */
+ if (ima_get_modsig_signer_count(event_data->modsig) != 1)
+ return -EINVAL;
+
+ len = ima_modsig_digest_entry(event_data->modsig, 0, entry);
+ if (len < 0)
+ return len;
+
+ return ima_write_template_field_data(entry, len,
+ DATA_FMT_DIGEST_WITH_ALGO,
+ field_data);
}
static int ima_eventname_init_common(struct ima_event_data *event_data,
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 6/8] ima: add the 'd-modsig-list' template field
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
` (4 preceding siblings ...)
2026-11-27 2:59 ` [RFC PATCH 5/8] ima: record data-signing algorithms by name in 'd-modsig' Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 7/8] ima: define the ima-modsigv2 template Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 8/8] ima: check the digest of every modsig signer against the blacklist Bruno Meneguele
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele
The 'd-modsig' field holds a single content digest, so it can't describe an
appended signature with more than one signer, let alone signers using
different algorithms. With that, add a 'd-modsig-list' field that records
an entry per signer as the following length-value table:
<u32 count> x (<u32 entry_len><entry>)
with the entries in the order of the signers in the 'modsig' field, so
entry i describes signer i, and following the already known d-modsig
format:
<hash algo>: <digest>
for a signer that signs an usual digest, and:
<signature algo>: '\0'
for a signer that signs data directly (eg. ML-DSA). The idea of adding an
empty entry is to keep entries aligned with their signers in 'modsig'
field, easing verifiers work.
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
Documentation/security/IMA-templates.rst | 3 +
security/integrity/ima/ima_policy.c | 3 +-
security/integrity/ima/ima_template.c | 6 +-
security/integrity/ima/ima_template_lib.c | 103 ++++++++++++++++++++++
security/integrity/ima/ima_template_lib.h | 4 +
5 files changed, 117 insertions(+), 2 deletions(-)
diff --git a/Documentation/security/IMA-templates.rst b/Documentation/security/IMA-templates.rst
index 4ec0440b27f1..161aed3da8b7 100644
--- a/Documentation/security/IMA-templates.rst
+++ b/Documentation/security/IMA-templates.rst
@@ -72,6 +72,9 @@ descriptors by adding their identifier to the format string
- 'd-modsig': the digest of the event without the appended modsig or a signature
algorithm name followed by an empty digest (<signing algo>: \0) when direct data
algorithms are used, e.g. ML-DSA;
+ - 'd-modsig-list': a list of 'd-modsig' of each signer when multiple signatures are
+ present and which may use different algorithms
+ (field format: <entry count>[pairs of <entry length><d-modsig entry>]);
- 'n-ng': the name of the event, without size limitations;
- 'sig': the file signature, based on either the file's/fsverity's digest[1],
or the EVM portable signature, if 'security.ima' contains a file hash.
diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c
index 2dc1d93aa768..bb68f78fdb0e 100644
--- a/security/integrity/ima/ima_policy.c
+++ b/security/integrity/ima/ima_policy.c
@@ -1242,7 +1242,8 @@ static void check_template_modsig(const struct ima_template_desc *template)
for (i = 0; i < template->num_fields; i++) {
if (!strcmp(template->fields[i]->field_id, "modsig"))
has_modsig = true;
- else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
+ else if (!strcmp(template->fields[i]->field_id, "d-modsig") ||
+ !strcmp(template->fields[i]->field_id, "d-modsig-list"))
has_dmodsig = true;
}
diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
index 16154078fd70..584f970e553e 100644
--- a/security/integrity/ima/ima_template.c
+++ b/security/integrity/ima/ima_template.c
@@ -50,6 +50,9 @@ static const struct ima_template_field supported_fields[] = {
.field_show = ima_show_template_buf},
{.field_id = "d-modsig", .field_init = ima_eventdigest_modsig_init,
.field_show = ima_show_template_digest_ng},
+ {.field_id = "d-modsig-list",
+ .field_init = ima_eventdigest_modsig_list_init,
+ .field_show = ima_show_template_modsig_list},
{.field_id = "modsig", .field_init = ima_eventmodsig_init,
.field_show = ima_show_template_sig},
{.field_id = "evmsig", .field_init = ima_eventevmsig_init,
@@ -95,7 +98,8 @@ bool ima_template_has_modsig(const struct ima_template_desc *ima_template)
for (i = 0; i < ima_template->num_fields; i++)
if (!strcmp(ima_template->fields[i]->field_id, "modsig") ||
- !strcmp(ima_template->fields[i]->field_id, "d-modsig"))
+ !strcmp(ima_template->fields[i]->field_id, "d-modsig") ||
+ !strcmp(ima_template->fields[i]->field_id, "d-modsig-list"))
return true;
return false;
diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c
index a2aeaae83065..eeb636155798 100644
--- a/security/integrity/ima/ima_template_lib.c
+++ b/security/integrity/ima/ima_template_lib.c
@@ -200,6 +200,40 @@ void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,
field_data);
}
+void ima_show_template_modsig_list(struct seq_file *m, enum ima_show_type show,
+ struct ima_field_data *field_data)
+{
+ struct ima_field_data entry;
+ u32 count, i;
+ int parsed;
+ void *p = field_data->data;
+ void *end = p + field_data->len;
+
+ if (show != IMA_SHOW_ASCII || field_data->len < sizeof(count)) {
+ ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
+ return;
+ }
+
+ memcpy(&count, p, sizeof(count));
+ if (ima_canonical_fmt)
+ count = le32_to_cpu((__force __le32)count);
+ p += sizeof(count);
+
+ for (i = 0; i < count; i++) {
+ ima_parse_buf(p, end, &p, 1, &entry, &parsed, NULL, 0,
+ "d-modsig-list");
+ if (parsed != 1)
+ break;
+
+ if (i)
+ seq_putc(m, ',');
+ if (entry.len)
+ ima_show_template_data_ascii(m, show,
+ DATA_FMT_DIGEST_WITH_ALGO,
+ &entry);
+ }
+}
+
void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
struct ima_field_data *field_data)
{
@@ -524,6 +558,75 @@ int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
field_data);
}
+/*
+ * ima_eventdigest_modsig_list_init - add the content digest of each modsig
+ * signer to the template data.
+ *
+ * Unlike 'd-modsig', this field describes an appended signature with any
+ * number of signers, which may use different algorithms. It is a table as
+ * follows:
+ *
+ * <u32 count> x (<u32 entry_len><entry>)
+ *
+ * with one entry per signer, in the order of the 'modsig' field, so entry i
+ * describes signer i, and with the pair <entry_len><entry> repeating <count>
+ * times.
+ *
+ * An entry has the 'd-modsig' format, see ima_modsig_digest_entry().
+ */
+int ima_eventdigest_modsig_list_init(struct ima_event_data *event_data,
+ struct ima_field_data *field_data)
+{
+ unsigned int count, i, err_count = 0;
+ int len, rc;
+ size_t buf_len;
+ u8 *buf, *p;
+ u32 val;
+
+ if (!event_data->modsig)
+ return 0;
+
+ count = ima_get_modsig_signer_count(event_data->modsig);
+ if (!count)
+ return 0;
+
+ buf_len = size_add(sizeof(u32),
+ size_mul(count, sizeof(u32) +
+ IMA_MAX_DIGEST_ENTRY_SIZE - DIGEST_TYPE_NAME_LEN_MAX));
+ buf = kmalloc(buf_len, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ p = buf;
+ val = ima_canonical_fmt ? (__force u32)cpu_to_le32(count) : count;
+ memcpy(p, &val, sizeof(val));
+ p += sizeof(u32);
+
+ for (i = 0; i < count; i++) {
+ len = ima_modsig_digest_entry(event_data->modsig, i,
+ p + sizeof(u32));
+ if (len < 0) {
+ /* Keep walking signers' list, even on failure, to
+ * maintain allignament with modsig.
+ */
+ rc = len;
+ len = 0;
+ err_count++;
+ }
+
+ val = ima_canonical_fmt ? (__force u32)cpu_to_le32(len) : len;
+ memcpy(p, &val, sizeof(val));
+ p += sizeof(u32) + len;
+ }
+
+ /* Like 'd-modsig', don't measure if the list has only bad digests */
+ if (err_count < count)
+ rc = ima_write_template_field_data(buf, p - buf, DATA_FMT_HEX,
+ field_data);
+ kfree(buf);
+ return rc;
+}
+
static int ima_eventname_init_common(struct ima_event_data *event_data,
struct ima_field_data *field_data,
bool size_limit)
diff --git a/security/integrity/ima/ima_template_lib.h b/security/integrity/ima/ima_template_lib.h
index 9f7c335f304f..cf3d453d4c37 100644
--- a/security/integrity/ima/ima_template_lib.h
+++ b/security/integrity/ima/ima_template_lib.h
@@ -23,6 +23,8 @@ void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
struct ima_field_data *field_data);
void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,
struct ima_field_data *field_data);
+void ima_show_template_modsig_list(struct seq_file *m, enum ima_show_type show,
+ struct ima_field_data *field_data);
void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
struct ima_field_data *field_data);
void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
@@ -44,6 +46,8 @@ int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
struct ima_field_data *field_data);
int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
struct ima_field_data *field_data);
+int ima_eventdigest_modsig_list_init(struct ima_event_data *event_data,
+ struct ima_field_data *field_data);
int ima_eventname_ng_init(struct ima_event_data *event_data,
struct ima_field_data *field_data);
int ima_eventsig_init(struct ima_event_data *event_data,
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 7/8] ima: define the ima-modsigv2 template
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
` (5 preceding siblings ...)
2026-11-27 2:59 ` [RFC PATCH 6/8] ima: add the 'd-modsig-list' template field Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
2026-11-27 2:59 ` [RFC PATCH 8/8] ima: check the digest of every modsig signer against the blacklist Bruno Meneguele
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele
Add the "ima-modsigv2" template with the following format:
d-ngv2|n-ng|sig|d-modsig-list|modsig
the counterpart of ima-modsig with support for multiple appended signatures
that records the content digest for every signer instead of just one. The
existing ima-modsig template and its 'd-modsig' field are left unchanged.
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
Documentation/security/IMA-templates.rst | 1 +
security/integrity/ima/ima_template.c | 2 ++
2 files changed, 3 insertions(+)
diff --git a/Documentation/security/IMA-templates.rst b/Documentation/security/IMA-templates.rst
index 161aed3da8b7..38d6d27344e2 100644
--- a/Documentation/security/IMA-templates.rst
+++ b/Documentation/security/IMA-templates.rst
@@ -99,6 +99,7 @@ Below, there is the list of defined template descriptors:
- "ima-sigv2": its format is ``d-ngv2|n-ng|sig``;
- "ima-buf": its format is ``d-ng|n-ng|buf``;
- "ima-modsig": its format is ``d-ng|n-ng|sig|d-modsig|modsig``;
+ - "ima-modsigv2": its format is ``d-ngv2|n-ng|sig|d-modsig-list|modsig``;
- "evm-sig": its format is ``d-ng|n-ng|evmsig|xattrnames|xattrlengths|xattrvalues|iuid|igid|imode``;
diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
index 584f970e553e..721af4b1828b 100644
--- a/security/integrity/ima/ima_template.c
+++ b/security/integrity/ima/ima_template.c
@@ -24,6 +24,8 @@ static struct ima_template_desc builtin_templates[] = {
{.name = "ima-sigv2", .fmt = "d-ngv2|n-ng|sig"},
{.name = "ima-buf", .fmt = "d-ng|n-ng|buf"},
{.name = "ima-modsig", .fmt = "d-ng|n-ng|sig|d-modsig|modsig"},
+ {.name = "ima-modsigv2",
+ .fmt = "d-ngv2|n-ng|sig|d-modsig-list|modsig"},
{.name = "evm-sig",
.fmt = "d-ng|n-ng|evmsig|xattrnames|xattrlengths|xattrvalues|iuid|igid|imode"},
{.name = "", .fmt = ""}, /* placeholder for a custom format */
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [RFC PATCH 8/8] ima: check the digest of every modsig signer against the blacklist
2026-11-27 2:59 [RFC PATCH 0/8] ima: measure modules with multiple appended signatures Bruno Meneguele
` (6 preceding siblings ...)
2026-11-27 2:59 ` [RFC PATCH 7/8] ima: define the ima-modsigv2 template Bruno Meneguele
@ 2026-11-27 2:59 ` Bruno Meneguele
7 siblings, 0 replies; 9+ messages in thread
From: Bruno Meneguele @ 2026-11-27 2:59 UTC (permalink / raw)
To: linux-integrity
Cc: linux-kernel, keyrings, zohar, roberto.sassu, dhowells, bauerman,
Bruno Meneguele
When checking the binary blacklist, traverse every signer content digest
appended to the file. If one digest fails (-EPERM), the content is deemed
blocked.
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
---
security/integrity/ima/ima_appraise.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index 8aeec2778984..a62dc1aa93b4 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -432,18 +432,23 @@ int ima_check_blacklist(struct ima_iint_cache *iint,
const char *sig_algo;
const u8 *digest = NULL;
u32 digestsize = 0;
+ unsigned int i, count;
int rc = 0;
if (!(iint->flags & IMA_CHECK_BLACKLIST))
return 0;
if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
- if (ima_get_modsig_signer_count(modsig) == 1 &&
- !ima_get_modsig_digest(modsig, 0, &hash_algo, &digest,
- &digestsize, &sig_algo))
- rc = is_binary_blacklisted(digest, digestsize);
- } else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash)
+ count = ima_get_modsig_signer_count(modsig);
+ for (i = 0; i < count && rc != -EPERM; i++) {
+ if (!ima_get_modsig_digest(modsig, i, &hash_algo,
+ &digest, &digestsize,
+ &sig_algo) && digestsize)
+ rc = is_binary_blacklisted(digest, digestsize);
+ }
+ } else if (iint->flags & IMA_DIGSIG_REQUIRED && iint->ima_hash) {
rc = is_binary_blacklisted(iint->ima_hash->digest, iint->ima_hash->length);
+ }
if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
process_buffer_measurement(&nop_mnt_idmap, NULL, digest, digestsize,
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread