ima: extend the measurement list to include the file signature
authorMimi Zohar <zohar@linux.vnet.ibm.com>
Tue, 23 Jul 2013 15:15:00 +0000 (11:15 -0400)
committerMimi Zohar <zohar@linux.vnet.ibm.com>
Fri, 1 Nov 2013 00:19:35 +0000 (20:19 -0400)
This patch defines a new template called 'ima-sig', which includes
the file signature in the template data, in addition to the file's
digest and pathname.

A template is composed of a set of fields.  Associated with each
field is an initialization and display function.  This patch defines
a new template field called 'sig', the initialization function
ima_eventsig_init(), and the display function ima_show_template_sig().

This patch modifies the .field_init() function definition to include
the 'security.ima' extended attribute and length.

Changelog:
- remove unused code (Dmitry Kasatkin)
- avoid calling ima_write_template_field_data() unnecesarily (Roberto Sassu)
- rename DATA_FMT_SIG to DATA_FMT_HEX
- cleanup ima_eventsig_init() based on Roberto's comments

Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Signed-off-by: Roberto Sassu <roberto.sassu@polito.it>
security/integrity/ima/Kconfig
security/integrity/ima/ima.h
security/integrity/ima/ima_api.c
security/integrity/ima/ima_init.c
security/integrity/ima/ima_main.c
security/integrity/ima/ima_template.c
security/integrity/ima/ima_template_lib.c
security/integrity/ima/ima_template_lib.h

index 351a58ed56ab3ca6206d78b0d4966c7e9371c56b..81a27971d884215bc20e71d756f6778893ae1d67 100644 (file)
@@ -63,6 +63,8 @@ choice
                bool "ima"
        config IMA_NG_TEMPLATE
                bool "ima-ng (default)"
+       config IMA_SIG_TEMPLATE
+               bool "ima-sig"
 endchoice
 
 config IMA_DEFAULT_TEMPLATE
@@ -70,6 +72,7 @@ config IMA_DEFAULT_TEMPLATE
        depends on IMA
        default "ima" if IMA_TEMPLATE
        default "ima-ng" if IMA_NG_TEMPLATE
+       default "ima-sig" if IMA_SIG_TEMPLATE
 
 choice
        prompt "Default integrity hash algorithm"
index 72d013e190b14615f8dcf5db0594f76b4295d883..bf03c6a16cc83ace2f47a3ea7571ef79a63ed1d9 100644 (file)
@@ -59,7 +59,8 @@ struct ima_template_field {
        const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
        int (*field_init) (struct integrity_iint_cache *iint, struct file *file,
                           const unsigned char *filename,
-                          struct ima_field_data *field_data);
+                          struct evm_ima_xattr_data *xattr_value,
+                          int xattr_len, struct ima_field_data *field_data);
        void (*field_show) (struct seq_file *m, enum ima_show_type show,
                            struct ima_field_data *field_data);
 };
@@ -134,12 +135,15 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
                            struct evm_ima_xattr_data **xattr_value,
                            int *xattr_len);
 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
-                          const unsigned char *filename);
+                          const unsigned char *filename,
+                          struct evm_ima_xattr_data *xattr_value,
+                          int xattr_len);
 void ima_audit_measurement(struct integrity_iint_cache *iint,
                           const unsigned char *filename);
 int ima_alloc_init_template(struct integrity_iint_cache *iint,
                            struct file *file, const unsigned char *filename,
-                           struct ima_template_entry **entry);
+                           struct evm_ima_xattr_data *xattr_value,
+                           int xattr_len, struct ima_template_entry **entry);
 int ima_store_template(struct ima_template_entry *entry, int violation,
                       struct inode *inode, const unsigned char *filename);
 const char *ima_d_path(struct path *path, char **pathbuf);
index 5fcc80695d8776c69164251dfd31caa703a2c461..0e7540863fc299687877ae4591961b13b60fcaa9 100644 (file)
@@ -26,7 +26,8 @@
  */
 int ima_alloc_init_template(struct integrity_iint_cache *iint,
                            struct file *file, const unsigned char *filename,
-                           struct ima_template_entry **entry)
+                           struct evm_ima_xattr_data *xattr_value,
+                           int xattr_len, struct ima_template_entry **entry)
 {
        struct ima_template_desc *template_desc = ima_template_desc_current();
        int i, result = 0;
@@ -41,6 +42,7 @@ int ima_alloc_init_template(struct integrity_iint_cache *iint,
                u32 len;
 
                result = field->field_init(iint, file, filename,
+                                          xattr_value, xattr_len,
                                           &((*entry)->template_data[i]));
                if (result != 0)
                        goto out;
@@ -123,7 +125,8 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
        /* can overflow, only indicator */
        atomic_long_inc(&ima_htable.violations);
 
-       result = ima_alloc_init_template(NULL, file, filename, &entry);
+       result = ima_alloc_init_template(NULL, file, filename,
+                                        NULL, 0, &entry);
        if (result < 0) {
                result = -ENOMEM;
                goto err_out;
@@ -239,7 +242,9 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
  * Must be called with iint->mutex held.
  */
 void ima_store_measurement(struct integrity_iint_cache *iint,
-                          struct file *file, const unsigned char *filename)
+                          struct file *file, const unsigned char *filename,
+                          struct evm_ima_xattr_data *xattr_value,
+                          int xattr_len)
 {
        const char *op = "add_template_measure";
        const char *audit_cause = "ENOMEM";
@@ -251,7 +256,8 @@ void ima_store_measurement(struct integrity_iint_cache *iint,
        if (iint->flags & IMA_MEASURED)
                return;
 
-       result = ima_alloc_init_template(iint, file, filename, &entry);
+       result = ima_alloc_init_template(iint, file, filename,
+                                        xattr_value, xattr_len, &entry);
        if (result < 0) {
                integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
                                    op, audit_cause, result, 0);
index f84aec5412f37e0f7d1ed203452ab3534b13f75a..15f34bd40abed1530216be53c59658b6245d280b 100644 (file)
@@ -69,7 +69,7 @@ static void __init ima_add_boot_aggregate(void)
        }
 
        result = ima_alloc_init_template(iint, NULL, boot_aggregate_name,
-                                        &entry);
+                                        NULL, 0, &entry);
        if (result < 0)
                return;
 
index 14d4cb557894413c45f043b4523d58c328335ce2..149ee1119f87ba37c7673efdd7fd4d0e50cae809 100644 (file)
@@ -225,7 +225,8 @@ static int process_measurement(struct file *file, const char *filename,
                pathname = (const char *)file->f_dentry->d_name.name;
 
        if (action & IMA_MEASURE)
-               ima_store_measurement(iint, file, pathname);
+               ima_store_measurement(iint, file, pathname,
+                                     xattr_value, xattr_len);
        if (action & IMA_APPRAISE_SUBMASK)
                rc = ima_appraise_measurement(_func, iint, file, pathname,
                                              xattr_value, xattr_len);
index 000221419f6c95d0efcec7bdc69c1be4e2269186..4e5da990630beca2c179894db96fd9f20d31bc17 100644 (file)
@@ -20,6 +20,7 @@
 static struct ima_template_desc defined_templates[] = {
        {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
        {.name = "ima-ng",.fmt = "d-ng|n-ng"},
+       {.name = "ima-sig",.fmt = "d-ng|n-ng|sig"},
 };
 
 static struct ima_template_field supported_fields[] = {
@@ -31,6 +32,8 @@ static struct ima_template_field supported_fields[] = {
         .field_show = ima_show_template_digest_ng},
        {.field_id = "n-ng",.field_init = ima_eventname_ng_init,
         .field_show = ima_show_template_string},
+       {.field_id = "sig",.field_init = ima_eventsig_init,
+        .field_show = ima_show_template_sig},
 };
 
 static struct ima_template_desc *ima_template;
index 7d841448f2462ee976047a64a251896576ac8309..6d66ad6ed265f8f6422c0d9d7f7a64aa732f0779 100644 (file)
@@ -28,7 +28,8 @@ enum data_formats {
        DATA_FMT_DIGEST = 0,
        DATA_FMT_DIGEST_WITH_ALGO,
        DATA_FMT_EVENT_NAME,
-       DATA_FMT_STRING
+       DATA_FMT_STRING,
+       DATA_FMT_HEX
 };
 
 static int ima_write_template_field_data(const void *data, const u32 datalen,
@@ -90,6 +91,9 @@ static void ima_show_template_data_ascii(struct seq_file *m,
                buf_ptr += 2;
                buflen -= buf_ptr - field_data->data;
        case DATA_FMT_DIGEST:
+       case DATA_FMT_HEX:
+               if (!buflen)
+                       break;
                ima_print_digest(m, buf_ptr, buflen);
                break;
        case DATA_FMT_STRING:
@@ -147,6 +151,12 @@ void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
        ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
 }
 
+void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
+                          struct ima_field_data *field_data)
+{
+       ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
+}
+
 static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
                                       struct ima_field_data *field_data,
                                       bool size_limit)
@@ -190,6 +200,7 @@ static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
  */
 int ima_eventdigest_init(struct integrity_iint_cache *iint, struct file *file,
                         const unsigned char *filename,
+                        struct evm_ima_xattr_data *xattr_value, int xattr_len,
                         struct ima_field_data *field_data)
 {
        struct {
@@ -237,7 +248,8 @@ out:
  */
 int ima_eventdigest_ng_init(struct integrity_iint_cache *iint,
                            struct file *file, const unsigned char *filename,
-                           struct ima_field_data *field_data)
+                           struct evm_ima_xattr_data *xattr_value,
+                           int xattr_len, struct ima_field_data *field_data)
 {
        u8 *cur_digest = NULL, hash_algo = HASH_ALGO__LAST;
        u32 cur_digestsize = 0;
@@ -295,6 +307,7 @@ out:
  */
 int ima_eventname_init(struct integrity_iint_cache *iint, struct file *file,
                       const unsigned char *filename,
+                      struct evm_ima_xattr_data *xattr_value, int xattr_len,
                       struct ima_field_data *field_data)
 {
        return ima_eventname_init_common(iint, file, filename,
@@ -306,8 +319,29 @@ int ima_eventname_init(struct integrity_iint_cache *iint, struct file *file,
  */
 int ima_eventname_ng_init(struct integrity_iint_cache *iint, struct file *file,
                          const unsigned char *filename,
+                         struct evm_ima_xattr_data *xattr_value, int xattr_len,
                          struct ima_field_data *field_data)
 {
        return ima_eventname_init_common(iint, file, filename,
                                         field_data, false);
 }
+
+/*
+ *  ima_eventsig_init - include the file signature as part of the template data
+ */
+int ima_eventsig_init(struct integrity_iint_cache *iint, struct file *file,
+                     const unsigned char *filename,
+                     struct evm_ima_xattr_data *xattr_value, int xattr_len,
+                     struct ima_field_data *field_data)
+{
+       enum data_formats fmt = DATA_FMT_HEX;
+       int rc = 0;
+
+       if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
+               goto out;
+
+       rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
+                                          field_data);
+out:
+       return rc;
+}
index 16c5e7810234d6e0f0b82cb4623a417e9f9cc22f..63f6b52cb1c26810acaa32f37c5fbb296fac852b 100644 (file)
@@ -24,16 +24,26 @@ 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_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,
+                          struct ima_field_data *field_data);
 int ima_eventdigest_init(struct integrity_iint_cache *iint, struct file *file,
                         const unsigned char *filename,
+                        struct evm_ima_xattr_data *xattr_value, int xattr_len,
                         struct ima_field_data *field_data);
 int ima_eventname_init(struct integrity_iint_cache *iint, struct file *file,
                       const unsigned char *filename,
+                      struct evm_ima_xattr_data *xattr_value, int xattr_len,
                       struct ima_field_data *field_data);
 int ima_eventdigest_ng_init(struct integrity_iint_cache *iint,
                            struct file *file, const unsigned char *filename,
-                           struct ima_field_data *field_data);
+                           struct evm_ima_xattr_data *xattr_value,
+                           int xattr_len, struct ima_field_data *field_data);
 int ima_eventname_ng_init(struct integrity_iint_cache *iint, struct file *file,
                          const unsigned char *filename,
+                         struct evm_ima_xattr_data *xattr_value, int xattr_len,
                          struct ima_field_data *field_data);
+int ima_eventsig_init(struct integrity_iint_cache *iint, struct file *file,
+                     const unsigned char *filename,
+                     struct evm_ima_xattr_data *xattr_value, int xattr_len,
+                     struct ima_field_data *field_data);
 #endif /* __LINUX_IMA_TEMPLATE_LIB_H */
This page took 0.047592 seconds and 5 git commands to generate.