ima: switch to new template management mechanism
[deliverable/linux.git] / security / integrity / ima / ima_template.c
1 /*
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 */
15 #include "ima.h"
16 #include "ima_template_lib.h"
17
18 static struct ima_template_desc defined_templates[] = {
19 {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
20 {.name = "ima-ng",.fmt = "d-ng|n-ng"},
21 };
22
23 static struct ima_template_field supported_fields[] = {
24 {.field_id = "d",.field_init = ima_eventdigest_init,
25 .field_show = ima_show_template_digest},
26 {.field_id = "n",.field_init = ima_eventname_init,
27 .field_show = ima_show_template_string},
28 {.field_id = "d-ng",.field_init = ima_eventdigest_ng_init,
29 .field_show = ima_show_template_digest_ng},
30 {.field_id = "n-ng",.field_init = ima_eventname_ng_init,
31 .field_show = ima_show_template_string},
32 };
33
34 static struct ima_template_desc *ima_template;
35
36 static struct ima_template_desc *lookup_template_desc(const char *name)
37 {
38 int i;
39
40 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
41 if (strcmp(defined_templates[i].name, name) == 0)
42 return defined_templates + i;
43 }
44
45 return NULL;
46 }
47
48 static struct ima_template_field *lookup_template_field(const char *field_id)
49 {
50 int i;
51
52 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
53 if (strncmp(supported_fields[i].field_id, field_id,
54 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
55 return &supported_fields[i];
56 return NULL;
57 }
58
59 static int template_fmt_size(char *template_fmt)
60 {
61 char c;
62 int template_fmt_len = strlen(template_fmt);
63 int i = 0, j = 0;
64
65 while (i < template_fmt_len) {
66 c = template_fmt[i];
67 if (c == '|')
68 j++;
69 i++;
70 }
71
72 return j + 1;
73 }
74
75 static int template_desc_init_fields(char *template_fmt,
76 struct ima_template_field ***fields,
77 int *num_fields)
78 {
79 char *c, *template_fmt_ptr = template_fmt;
80 int template_num_fields = template_fmt_size(template_fmt);
81 int i, result = 0;
82
83 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX)
84 return -EINVAL;
85
86 *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
87 if (*fields == NULL) {
88 result = -ENOMEM;
89 goto out;
90 }
91 for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
92 i < template_num_fields; i++) {
93 struct ima_template_field *f = lookup_template_field(c);
94
95 if (!f) {
96 result = -ENOENT;
97 goto out;
98 }
99 (*fields)[i] = f;
100 }
101 *num_fields = i;
102 return 0;
103 out:
104 kfree(*fields);
105 *fields = NULL;
106 return result;
107 }
108
109 static int init_defined_templates(void)
110 {
111 int i = 0;
112 int result = 0;
113
114 /* Init defined templates. */
115 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
116 struct ima_template_desc *template = &defined_templates[i];
117
118 result = template_desc_init_fields(template->fmt,
119 &(template->fields),
120 &(template->num_fields));
121 if (result < 0)
122 return result;
123 }
124 return result;
125 }
126
127 struct ima_template_desc *ima_template_desc_current(void)
128 {
129 if (!ima_template)
130 ima_template = lookup_template_desc(IMA_TEMPLATE_IMA_NAME);
131
132 return ima_template;
133 }
134
135 int ima_init_template(void)
136 {
137 int result;
138
139 result = init_defined_templates();
140 if (result < 0)
141 return result;
142
143 return 0;
144 }
This page took 0.033501 seconds and 5 git commands to generate.