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 5/8] ima: record data-signing algorithms by name in 'd-modsig'
Date: Thu, 26 Nov 2026 23:59:08 -0300	[thread overview]
Message-ID: <20261127025912.1383750-6-bmeneg@redhat.com> (raw)
In-Reply-To: <20261127025912.1383750-1-bmeneg@redhat.com>

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


  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 ` [RFC PATCH 4/8] ima: get the modsig content digest of each signer Bruno Meneguele
2026-11-27  2:59 ` Bruno Meneguele [this message]
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-6-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®