blob: 8100422643962ec3a1273711b1b722cfdb6ce123 [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"
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020016#include "ima_template_lib.h"
Roberto Sassuadf53a72013-06-07 12:16:29 +020017
18static struct ima_template_desc defined_templates[] = {
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020019 {.name = IMA_TEMPLATE_IMA_NAME,.fmt = IMA_TEMPLATE_IMA_FMT},
Roberto Sassuadf53a72013-06-07 12:16:29 +020020};
21
22static struct ima_template_field supported_fields[] = {
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020023 {.field_id = "d",.field_init = ima_eventdigest_init,
24 .field_show = ima_show_template_digest},
25 {.field_id = "n",.field_init = ima_eventname_init,
26 .field_show = ima_show_template_string},
Roberto Sassuadf53a72013-06-07 12:16:29 +020027};
28
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020029static struct ima_template_field *lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +020030{
31 int i;
32
33 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
34 if (strncmp(supported_fields[i].field_id, field_id,
35 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
36 return &supported_fields[i];
37 return NULL;
38}
39
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020040static int template_fmt_size(char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +020041{
42 char c;
43 int template_fmt_len = strlen(template_fmt);
44 int i = 0, j = 0;
45
46 while (i < template_fmt_len) {
47 c = template_fmt[i];
48 if (c == '|')
49 j++;
50 i++;
51 }
52
53 return j + 1;
54}
55
56static int template_desc_init_fields(char *template_fmt,
57 struct ima_template_field ***fields,
58 int *num_fields)
59{
60 char *c, *template_fmt_ptr = template_fmt;
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020061 int template_num_fields = template_fmt_size(template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +020062 int i, result = 0;
63
64 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX)
65 return -EINVAL;
66
67 *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
68 if (*fields == NULL) {
69 result = -ENOMEM;
70 goto out;
71 }
72 for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
73 i < template_num_fields; i++) {
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020074 struct ima_template_field *f = lookup_template_field(c);
Roberto Sassuadf53a72013-06-07 12:16:29 +020075
76 if (!f) {
77 result = -ENOENT;
78 goto out;
79 }
80 (*fields)[i] = f;
81 }
82 *num_fields = i;
83 return 0;
84out:
85 kfree(*fields);
86 *fields = NULL;
87 return result;
88}
89
90static int init_defined_templates(void)
91{
92 int i = 0;
93 int result = 0;
94
95 /* Init defined templates. */
96 for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
97 struct ima_template_desc *template = &defined_templates[i];
98
99 result = template_desc_init_fields(template->fmt,
100 &(template->fields),
101 &(template->num_fields));
102 if (result < 0)
103 return result;
104 }
105 return result;
106}
107
108int ima_init_template(void)
109{
110 int result;
111
112 result = init_defined_templates();
113 if (result < 0)
114 return result;
115
116 return 0;
117}