2 * Copyright (C) 2013 Politecnico di Torino, Italy
3 * TORSEC group -- http://security.polito.it
5 * Author: Roberto Sassu <roberto.sassu@polito.it>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2 of the
12 * File: ima_template.c
13 * Helpers to manage template descriptors.
15 #include <crypto/hash_info.h>
18 #include "ima_template_lib.h"
20 static struct ima_template_desc defined_templates[] = {
21 {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
22 {.name = "ima-ng",.fmt = "d-ng|n-ng"},
23 {.name = "ima-sig",.fmt = "d-ng|n-ng|sig"},
26 static struct ima_template_field supported_fields[] = {
27 {.field_id = "d",.field_init = ima_eventdigest_init,
28 .field_show = ima_show_template_digest},
29 {.field_id = "n",.field_init = ima_eventname_init,
30 .field_show = ima_show_template_string},
31 {.field_id = "d-ng",.field_init = ima_eventdigest_ng_init,
32 .field_show = ima_show_template_digest_ng},
33 {.field_id = "n-ng",.field_init = ima_eventname_ng_init,
34 .field_show = ima_show_template_string},
35 {.field_id = "sig",.field_init = ima_eventsig_init,
36 .field_show = ima_show_template_sig},
39 static struct ima_template_desc *ima_template;
40 static struct ima_template_desc *lookup_template_desc(const char *name);
42 static int __init ima_template_setup(char *str)
44 struct ima_template_desc *template_desc;
45 int template_len = strlen(str);
48 * Verify that a template with the supplied name exists.
49 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
51 template_desc = lookup_template_desc(str);
56 * Verify whether the current hash algorithm is supported
57 * by the 'ima' template.
59 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
60 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
61 pr_err("IMA: template does not support hash alg\n");
65 ima_template = template_desc;
68 __setup("ima_template=", ima_template_setup);
70 static struct ima_template_desc *lookup_template_desc(const char *name)
74 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
75 if (strcmp(defined_templates[i].name, name) == 0)
76 return defined_templates + i;
82 static struct ima_template_field *lookup_template_field(const char *field_id)
86 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
87 if (strncmp(supported_fields[i].field_id, field_id,
88 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
89 return &supported_fields[i];
93 static int template_fmt_size(char *template_fmt)
96 int template_fmt_len = strlen(template_fmt);
99 while (i < template_fmt_len) {
109 static int template_desc_init_fields(char *template_fmt,
110 struct ima_template_field ***fields,
113 char *c, *template_fmt_ptr = template_fmt;
114 int template_num_fields = template_fmt_size(template_fmt);
117 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX)
120 *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
121 if (*fields == NULL) {
125 for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
126 i < template_num_fields; i++) {
127 struct ima_template_field *f = lookup_template_field(c);
143 static int init_defined_templates(void)
148 /* Init defined templates. */
149 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
150 struct ima_template_desc *template = &defined_templates[i];
152 result = template_desc_init_fields(template->fmt,
154 &(template->num_fields));
161 struct ima_template_desc *ima_template_desc_current(void)
165 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
169 int ima_init_template(void)
173 result = init_defined_templates();