blob: febd12ed9b55ab4d6009176c355655c646272437 [file] [log] [blame]
Roberto Sassuadf53a72013-06-07 12:16:29 +02001/*
2 * Copyright (C) 2013 Politecnico di Torino, Italy
3 * TORSEC group -- http://security.polito.it
4 *
5 * Author: Roberto Sassu <roberto.sassu@polito.it>
6 *
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
10 * License.
11 *
12 * File: ima_template.c
13 * Helpers to manage template descriptors.
14 */
Joe Perches20ee4512014-02-24 13:59:56 -080015
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Roberto Sassuadf53a72013-06-07 12:16:29 +020018#include "ima.h"
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020019#include "ima_template_lib.h"
Roberto Sassuadf53a72013-06-07 12:16:29 +020020
21static struct ima_template_desc defined_templates[] = {
Roberto Sassu4d7aeee2013-06-07 12:16:32 +020022 {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020023 {.name = "ima-ng", .fmt = "d-ng|n-ng"},
24 {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
Roberto Sassuc2426d22014-10-13 14:08:42 +020025 {.name = "", .fmt = ""}, /* placeholder for a custom format */
Roberto Sassuadf53a72013-06-07 12:16:29 +020026};
27
28static struct ima_template_field supported_fields[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020029 {.field_id = "d", .field_init = ima_eventdigest_init,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020030 .field_show = ima_show_template_digest},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020031 {.field_id = "n", .field_init = ima_eventname_init,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020032 .field_show = ima_show_template_string},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020033 {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
Roberto Sassu4d7aeee2013-06-07 12:16:32 +020034 .field_show = ima_show_template_digest_ng},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020035 {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
Roberto Sassu4d7aeee2013-06-07 12:16:32 +020036 .field_show = ima_show_template_string},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020037 {.field_id = "sig", .field_init = ima_eventsig_init,
Mimi Zoharbcbc9b02013-07-23 11:15:00 -040038 .field_show = ima_show_template_sig},
Roberto Sassuadf53a72013-06-07 12:16:29 +020039};
40
Roberto Sassua71dc652013-06-07 12:16:33 +020041static struct ima_template_desc *ima_template;
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020042static struct ima_template_desc *lookup_template_desc(const char *name);
Roberto Sassuc2426d22014-10-13 14:08:42 +020043static int template_desc_init_fields(const char *template_fmt,
44 struct ima_template_field ***fields,
45 int *num_fields);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020046
47static int __init ima_template_setup(char *str)
48{
49 struct ima_template_desc *template_desc;
50 int template_len = strlen(str);
51
Roberto Sassuc2426d22014-10-13 14:08:42 +020052 if (ima_template)
53 return 1;
54
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020055 /*
56 * Verify that a template with the supplied name exists.
57 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
58 */
59 template_desc = lookup_template_desc(str);
Roberto Sassu71fed2e2014-10-13 14:08:38 +020060 if (!template_desc) {
61 pr_err("template %s not found, using %s\n",
62 str, CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020063 return 1;
Roberto Sassu71fed2e2014-10-13 14:08:38 +020064 }
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020065
66 /*
67 * Verify whether the current hash algorithm is supported
68 * by the 'ima' template.
69 */
70 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
71 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
Joe Perches20ee4512014-02-24 13:59:56 -080072 pr_err("template does not support hash alg\n");
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020073 return 1;
74 }
75
76 ima_template = template_desc;
77 return 1;
78}
79__setup("ima_template=", ima_template_setup);
Roberto Sassua71dc652013-06-07 12:16:33 +020080
Roberto Sassuc2426d22014-10-13 14:08:42 +020081static int __init ima_template_fmt_setup(char *str)
82{
83 int num_templates = ARRAY_SIZE(defined_templates);
84
85 if (ima_template)
86 return 1;
87
88 if (template_desc_init_fields(str, NULL, NULL) < 0) {
89 pr_err("format string '%s' not valid, using template %s\n",
90 str, CONFIG_IMA_DEFAULT_TEMPLATE);
91 return 1;
92 }
93
94 defined_templates[num_templates - 1].fmt = str;
95 ima_template = defined_templates + num_templates - 1;
96 return 1;
97}
98__setup("ima_template_fmt=", ima_template_fmt_setup);
99
Roberto Sassua71dc652013-06-07 12:16:33 +0200100static struct ima_template_desc *lookup_template_desc(const char *name)
101{
102 int i;
103
104 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
105 if (strcmp(defined_templates[i].name, name) == 0)
106 return defined_templates + i;
107 }
108
109 return NULL;
110}
111
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200112static struct ima_template_field *lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200113{
114 int i;
115
116 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
117 if (strncmp(supported_fields[i].field_id, field_id,
118 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
119 return &supported_fields[i];
120 return NULL;
121}
122
Roberto Sassudbc335d2013-11-25 20:18:52 +0100123static int template_fmt_size(const char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200124{
125 char c;
126 int template_fmt_len = strlen(template_fmt);
127 int i = 0, j = 0;
128
129 while (i < template_fmt_len) {
130 c = template_fmt[i];
131 if (c == '|')
132 j++;
133 i++;
134 }
135
136 return j + 1;
137}
138
Roberto Sassudbc335d2013-11-25 20:18:52 +0100139static int template_desc_init_fields(const char *template_fmt,
Roberto Sassuadf53a72013-06-07 12:16:29 +0200140 struct ima_template_field ***fields,
141 int *num_fields)
142{
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200143 const char *template_fmt_ptr;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200144 struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
Roberto Sassu3ce1217d2013-06-07 12:16:30 +0200145 int template_num_fields = template_fmt_size(template_fmt);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200146 int i, len;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200147
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200148 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
149 pr_err("format string '%s' contains too many fields\n",
150 template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +0200151 return -EINVAL;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200152 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200153
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200154 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
155 i++, template_fmt_ptr += len + 1) {
156 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
Roberto Sassuadf53a72013-06-07 12:16:29 +0200157
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200158 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
159 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
160 pr_err("Invalid field with length %d\n", len);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200161 return -EINVAL;
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200162 }
163
164 memcpy(tmp_field_id, template_fmt_ptr, len);
165 tmp_field_id[len] = '\0';
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200166 found_fields[i] = lookup_template_field(tmp_field_id);
167 if (!found_fields[i]) {
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200168 pr_err("field '%s' not found\n", tmp_field_id);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200169 return -ENOENT;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200170 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200171 }
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200172
Roberto Sassuc2426d22014-10-13 14:08:42 +0200173 if (fields && num_fields) {
174 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
175 if (*fields == NULL)
176 return -ENOMEM;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200177
Roberto Sassuc2426d22014-10-13 14:08:42 +0200178 memcpy(*fields, found_fields, i * sizeof(*fields));
179 *num_fields = i;
180 }
181
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200182 return 0;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200183}
184
Roberto Sassua71dc652013-06-07 12:16:33 +0200185struct ima_template_desc *ima_template_desc_current(void)
186{
187 if (!ima_template)
Mimi Zohar42865872013-06-07 12:16:34 +0200188 ima_template =
189 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassua71dc652013-06-07 12:16:33 +0200190 return ima_template;
191}
192
Dmitry Kasatkine4a9c512014-09-03 10:19:58 +0300193int __init ima_init_template(void)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200194{
Dmitry Kasatkinb4148db2014-05-08 11:23:53 +0300195 struct ima_template_desc *template = ima_template_desc_current();
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200196 int result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200197
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200198 result = template_desc_init_fields(template->fmt,
199 &(template->fields),
200 &(template->num_fields));
201 if (result < 0)
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200202 pr_err("template %s init failed, result: %d\n",
203 (strlen(template->name) ?
204 template->name : template->fmt), result);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200205
206 return result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200207}