mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bruno Meneguele <bmeneg@redhat.com>
To: linux-integrity@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, keyrings@vger.kernel.org,
	zohar@linux.ibm.com, roberto.sassu@huawei.com,
	dhowells@redhat.com, bauerman@linux.ibm.com,
	Bruno Meneguele <bmeneg@redhat.com>
Subject: [RFC PATCH 4/8] ima: get the modsig content digest of each signer
Date: Thu, 26 Nov 2026 23:59:07 -0300	[thread overview]
Message-ID: <20261127025912.1383750-5-bmeneg@redhat.com> (raw)
In-Reply-To: <20261127025912.1383750-1-bmeneg@redhat.com>

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


  parent reply	other threads:[~2026-11-27  2:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [RFC PATCH 3/8] pkcs7: support multiple signers in pkcs7_get_digest() Bruno Meneguele
2026-11-27  2:59 ` Bruno Meneguele [this message]
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 ` [RFC PATCH 6/8] ima: add the 'd-modsig-list' template field 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127025912.1383750-5-bmeneg@redhat.com \
    --to=bmeneg@redhat.com \
    --cc=bauerman@linux.ibm.com \
    --cc=dhowells@redhat.com \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=roberto.sassu@huawei.com \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®