Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 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 | */ |
Roberto Sassu | 9b9d4ce | 2013-06-07 12:16:35 +0200 | [diff] [blame] | 15 | #include <crypto/hash_info.h> |
| 16 | |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 17 | #include "ima.h" |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 18 | #include "ima_template_lib.h" |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 19 | |
| 20 | static struct ima_template_desc defined_templates[] = { |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 21 | {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT}, |
| 22 | {.name = "ima-ng",.fmt = "d-ng|n-ng"}, |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 23 | {.name = "ima-sig",.fmt = "d-ng|n-ng|sig"}, |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static struct ima_template_field supported_fields[] = { |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 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}, |
Roberto Sassu | 4d7aeee7 | 2013-06-07 12:16:32 +0200 | [diff] [blame] | 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}, |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 35 | {.field_id = "sig",.field_init = ima_eventsig_init, |
| 36 | .field_show = ima_show_template_sig}, |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 37 | }; |
| 38 | |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 39 | static struct ima_template_desc *ima_template; |
Roberto Sassu | 9b9d4ce | 2013-06-07 12:16:35 +0200 | [diff] [blame] | 40 | static struct ima_template_desc *lookup_template_desc(const char *name); |
| 41 | |
| 42 | static int __init ima_template_setup(char *str) |
| 43 | { |
| 44 | struct ima_template_desc *template_desc; |
| 45 | int template_len = strlen(str); |
| 46 | |
| 47 | /* |
| 48 | * Verify that a template with the supplied name exists. |
| 49 | * If not, use CONFIG_IMA_DEFAULT_TEMPLATE. |
| 50 | */ |
| 51 | template_desc = lookup_template_desc(str); |
| 52 | if (!template_desc) |
| 53 | return 1; |
| 54 | |
| 55 | /* |
| 56 | * Verify whether the current hash algorithm is supported |
| 57 | * by the 'ima' template. |
| 58 | */ |
| 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"); |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | ima_template = template_desc; |
| 66 | return 1; |
| 67 | } |
| 68 | __setup("ima_template=", ima_template_setup); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 69 | |
| 70 | static struct ima_template_desc *lookup_template_desc(const char *name) |
| 71 | { |
| 72 | int i; |
| 73 | |
| 74 | for (i = 0; i < ARRAY_SIZE(defined_templates); i++) { |
| 75 | if (strcmp(defined_templates[i].name, name) == 0) |
| 76 | return defined_templates + i; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 82 | static struct ima_template_field *lookup_template_field(const char *field_id) |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | |
| 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]; |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Roberto Sassu | dbc335d | 2013-11-25 20:18:52 +0100 | [diff] [blame] | 93 | static int template_fmt_size(const char *template_fmt) |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 94 | { |
| 95 | char c; |
| 96 | int template_fmt_len = strlen(template_fmt); |
| 97 | int i = 0, j = 0; |
| 98 | |
| 99 | while (i < template_fmt_len) { |
| 100 | c = template_fmt[i]; |
| 101 | if (c == '|') |
| 102 | j++; |
| 103 | i++; |
| 104 | } |
| 105 | |
| 106 | return j + 1; |
| 107 | } |
| 108 | |
Roberto Sassu | dbc335d | 2013-11-25 20:18:52 +0100 | [diff] [blame] | 109 | static int template_desc_init_fields(const char *template_fmt, |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 110 | struct ima_template_field ***fields, |
| 111 | int *num_fields) |
| 112 | { |
Roberto Sassu | dbc335d | 2013-11-25 20:18:52 +0100 | [diff] [blame] | 113 | char *c, *template_fmt_copy; |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 114 | int template_num_fields = template_fmt_size(template_fmt); |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 115 | int i, result = 0; |
| 116 | |
| 117 | if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) |
| 118 | return -EINVAL; |
| 119 | |
Roberto Sassu | dbc335d | 2013-11-25 20:18:52 +0100 | [diff] [blame] | 120 | /* copying is needed as strsep() modifies the original buffer */ |
| 121 | template_fmt_copy = kstrdup(template_fmt, GFP_KERNEL); |
| 122 | if (template_fmt_copy == NULL) |
| 123 | return -ENOMEM; |
| 124 | |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 125 | *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL); |
| 126 | if (*fields == NULL) { |
| 127 | result = -ENOMEM; |
| 128 | goto out; |
| 129 | } |
Roberto Sassu | dbc335d | 2013-11-25 20:18:52 +0100 | [diff] [blame] | 130 | for (i = 0; (c = strsep(&template_fmt_copy, "|")) != NULL && |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 131 | i < template_num_fields; i++) { |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame] | 132 | struct ima_template_field *f = lookup_template_field(c); |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 133 | |
| 134 | if (!f) { |
| 135 | result = -ENOENT; |
| 136 | goto out; |
| 137 | } |
| 138 | (*fields)[i] = f; |
| 139 | } |
| 140 | *num_fields = i; |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 141 | out: |
Roberto Sassu | dbc335d | 2013-11-25 20:18:52 +0100 | [diff] [blame] | 142 | if (result < 0) { |
| 143 | kfree(*fields); |
| 144 | *fields = NULL; |
| 145 | } |
| 146 | kfree(template_fmt_copy); |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 147 | return result; |
| 148 | } |
| 149 | |
| 150 | static int init_defined_templates(void) |
| 151 | { |
| 152 | int i = 0; |
| 153 | int result = 0; |
| 154 | |
| 155 | /* Init defined templates. */ |
| 156 | for (i = 0; i < ARRAY_SIZE(defined_templates); i++) { |
| 157 | struct ima_template_desc *template = &defined_templates[i]; |
| 158 | |
| 159 | result = template_desc_init_fields(template->fmt, |
| 160 | &(template->fields), |
| 161 | &(template->num_fields)); |
| 162 | if (result < 0) |
| 163 | return result; |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 168 | struct ima_template_desc *ima_template_desc_current(void) |
| 169 | { |
| 170 | if (!ima_template) |
Mimi Zohar | 4286587 | 2013-06-07 12:16:34 +0200 | [diff] [blame] | 171 | ima_template = |
| 172 | lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 173 | return ima_template; |
| 174 | } |
| 175 | |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 176 | int ima_init_template(void) |
| 177 | { |
| 178 | int result; |
| 179 | |
| 180 | result = init_defined_templates(); |
| 181 | if (result < 0) |
| 182 | return result; |
| 183 | |
| 184 | return 0; |
| 185 | } |