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 | */ |
| 15 | #include "ima.h" |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 16 | #include "ima_template_lib.h" |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 17 | |
| 18 | static struct ima_template_desc defined_templates[] = { |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 19 | {.name = IMA_TEMPLATE_IMA_NAME,.fmt = IMA_TEMPLATE_IMA_FMT}, |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | static struct ima_template_field supported_fields[] = { |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 23 | {.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 Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Roberto Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 29 | static struct ima_template_field *lookup_template_field(const char *field_id) |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 30 | { |
| 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 Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 40 | static int template_fmt_size(char *template_fmt) |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 41 | { |
| 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 | |
| 56 | static 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 Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 61 | int template_num_fields = template_fmt_size(template_fmt); |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 62 | 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 Sassu | 3ce1217d | 2013-06-07 12:16:30 +0200 | [diff] [blame^] | 74 | struct ima_template_field *f = lookup_template_field(c); |
Roberto Sassu | adf53a7 | 2013-06-07 12:16:29 +0200 | [diff] [blame] | 75 | |
| 76 | if (!f) { |
| 77 | result = -ENOENT; |
| 78 | goto out; |
| 79 | } |
| 80 | (*fields)[i] = f; |
| 81 | } |
| 82 | *num_fields = i; |
| 83 | return 0; |
| 84 | out: |
| 85 | kfree(*fields); |
| 86 | *fields = NULL; |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | static 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 | |
| 108 | int 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 | } |