ima_template.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <crypto/hash_info.h>
  17. #include "ima.h"
  18. #include "ima_template_lib.h"
  19. static struct ima_template_desc defined_templates[] = {
  20. {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
  21. {.name = "ima-ng", .fmt = "d-ng|n-ng"},
  22. {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
  23. {.name = "", .fmt = ""}, /* placeholder for a custom format */
  24. };
  25. static struct ima_template_field supported_fields[] = {
  26. {.field_id = "d", .field_init = ima_eventdigest_init,
  27. .field_show = ima_show_template_digest},
  28. {.field_id = "n", .field_init = ima_eventname_init,
  29. .field_show = ima_show_template_string},
  30. {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
  31. .field_show = ima_show_template_digest_ng},
  32. {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
  33. .field_show = ima_show_template_string},
  34. {.field_id = "sig", .field_init = ima_eventsig_init,
  35. .field_show = ima_show_template_sig},
  36. };
  37. static struct ima_template_desc *ima_template;
  38. static struct ima_template_desc *lookup_template_desc(const char *name);
  39. static int template_desc_init_fields(const char *template_fmt,
  40. struct ima_template_field ***fields,
  41. int *num_fields);
  42. static int __init ima_template_setup(char *str)
  43. {
  44. struct ima_template_desc *template_desc;
  45. int template_len = strlen(str);
  46. if (ima_template)
  47. return 1;
  48. /*
  49. * Verify that a template with the supplied name exists.
  50. * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
  51. */
  52. template_desc = lookup_template_desc(str);
  53. if (!template_desc) {
  54. pr_err("template %s not found, using %s\n",
  55. str, CONFIG_IMA_DEFAULT_TEMPLATE);
  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) {
  64. pr_err("template does not support hash alg\n");
  65. return 1;
  66. }
  67. ima_template = template_desc;
  68. return 1;
  69. }
  70. __setup("ima_template=", ima_template_setup);
  71. static int __init ima_template_fmt_setup(char *str)
  72. {
  73. int num_templates = ARRAY_SIZE(defined_templates);
  74. if (ima_template)
  75. return 1;
  76. if (template_desc_init_fields(str, NULL, NULL) < 0) {
  77. pr_err("format string '%s' not valid, using template %s\n",
  78. str, CONFIG_IMA_DEFAULT_TEMPLATE);
  79. return 1;
  80. }
  81. defined_templates[num_templates - 1].fmt = str;
  82. ima_template = defined_templates + num_templates - 1;
  83. return 1;
  84. }
  85. __setup("ima_template_fmt=", ima_template_fmt_setup);
  86. static struct ima_template_desc *lookup_template_desc(const char *name)
  87. {
  88. int i;
  89. for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
  90. if (strcmp(defined_templates[i].name, name) == 0)
  91. return defined_templates + i;
  92. }
  93. return NULL;
  94. }
  95. static struct ima_template_field *lookup_template_field(const char *field_id)
  96. {
  97. int i;
  98. for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
  99. if (strncmp(supported_fields[i].field_id, field_id,
  100. IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
  101. return &supported_fields[i];
  102. return NULL;
  103. }
  104. static int template_fmt_size(const char *template_fmt)
  105. {
  106. char c;
  107. int template_fmt_len = strlen(template_fmt);
  108. int i = 0, j = 0;
  109. while (i < template_fmt_len) {
  110. c = template_fmt[i];
  111. if (c == '|')
  112. j++;
  113. i++;
  114. }
  115. return j + 1;
  116. }
  117. static int template_desc_init_fields(const char *template_fmt,
  118. struct ima_template_field ***fields,
  119. int *num_fields)
  120. {
  121. const char *template_fmt_ptr;
  122. struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
  123. int template_num_fields = template_fmt_size(template_fmt);
  124. int i, len;
  125. if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
  126. pr_err("format string '%s' contains too many fields\n",
  127. template_fmt);
  128. return -EINVAL;
  129. }
  130. for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
  131. i++, template_fmt_ptr += len + 1) {
  132. char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
  133. len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
  134. if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
  135. pr_err("Invalid field with length %d\n", len);
  136. return -EINVAL;
  137. }
  138. memcpy(tmp_field_id, template_fmt_ptr, len);
  139. tmp_field_id[len] = '\0';
  140. found_fields[i] = lookup_template_field(tmp_field_id);
  141. if (!found_fields[i]) {
  142. pr_err("field '%s' not found\n", tmp_field_id);
  143. return -ENOENT;
  144. }
  145. }
  146. if (fields && num_fields) {
  147. *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
  148. if (*fields == NULL)
  149. return -ENOMEM;
  150. memcpy(*fields, found_fields, i * sizeof(*fields));
  151. *num_fields = i;
  152. }
  153. return 0;
  154. }
  155. struct ima_template_desc *ima_template_desc_current(void)
  156. {
  157. if (!ima_template)
  158. ima_template =
  159. lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
  160. return ima_template;
  161. }
  162. int __init ima_init_template(void)
  163. {
  164. struct ima_template_desc *template = ima_template_desc_current();
  165. int result;
  166. result = template_desc_init_fields(template->fmt,
  167. &(template->fields),
  168. &(template->num_fields));
  169. if (result < 0)
  170. pr_err("template %s init failed, result: %d\n",
  171. (strlen(template->name) ?
  172. template->name : template->fmt), result);
  173. return result;
  174. }