blob: 7e86783ccf51ed7e9b9a8fae79c7ba8716bef4a3 [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 */
15#include "ima.h"
16
17static struct ima_template_desc defined_templates[] = {
18};
19
20static struct ima_template_field supported_fields[] = {
21};
22
23static struct ima_template_field *ima_lookup_template_field(
24 const char *field_id)
25{
26 int i;
27
28 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
29 if (strncmp(supported_fields[i].field_id, field_id,
30 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
31 return &supported_fields[i];
32 return NULL;
33}
34
35static int ima_template_fmt_size(char *template_fmt)
36{
37 char c;
38 int template_fmt_len = strlen(template_fmt);
39 int i = 0, j = 0;
40
41 while (i < template_fmt_len) {
42 c = template_fmt[i];
43 if (c == '|')
44 j++;
45 i++;
46 }
47
48 return j + 1;
49}
50
51static int template_desc_init_fields(char *template_fmt,
52 struct ima_template_field ***fields,
53 int *num_fields)
54{
55 char *c, *template_fmt_ptr = template_fmt;
56 int template_num_fields = ima_template_fmt_size(template_fmt);
57 int i, result = 0;
58
59 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX)
60 return -EINVAL;
61
62 *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
63 if (*fields == NULL) {
64 result = -ENOMEM;
65 goto out;
66 }
67 for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
68 i < template_num_fields; i++) {
69 struct ima_template_field *f = ima_lookup_template_field(c);
70
71 if (!f) {
72 result = -ENOENT;
73 goto out;
74 }
75 (*fields)[i] = f;
76 }
77 *num_fields = i;
78 return 0;
79out:
80 kfree(*fields);
81 *fields = NULL;
82 return result;
83}
84
85static int init_defined_templates(void)
86{
87 int i = 0;
88 int result = 0;
89
90 /* Init defined templates. */
91 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
92 struct ima_template_desc *template = &defined_templates[i];
93
94 result = template_desc_init_fields(template->fmt,
95 &(template->fields),
96 &(template->num_fields));
97 if (result < 0)
98 return result;
99 }
100 return result;
101}
102
103int ima_init_template(void)
104{
105 int result;
106
107 result = init_defined_templates();
108 if (result < 0)
109 return result;
110
111 return 0;
112}