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