[RFC PATCH 4/8] ima: get the modsig content digest of each signer
From: Bruno Meneguele
Date: Thu Nov 26 2026 - 22:00:11 EST
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@xxxxxxxxxx>
---
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