ima_template.c 5.5 KB

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