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 <[email protected]>
---
 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


Reply via email to