ima_template.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. };
  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 __init ima_template_setup(char *str)
  39. {
  40. struct ima_template_desc *template_desc;
  41. int template_len = strlen(str);
  42. /*
  43. * Verify that a template with the supplied name exists.
  44. * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
  45. */
  46. template_desc = lookup_template_desc(str);
  47. if (!template_desc)
  48. return 1;
  49. /*
  50. * Verify whether the current hash algorithm is supported
  51. * by the 'ima' template.
  52. */
  53. if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
  54. ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
  55. pr_err("template does not support hash alg\n");
  56. return 1;
  57. }
  58. ima_template = template_desc;
  59. return 1;
  60. }
  61. __setup("ima_template=", ima_template_setup);
  62. static struct ima_template_desc *lookup_template_desc(const char *name)
  63. {
  64. int i;
  65. for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
  66. if (strcmp(defined_templates[i].name, name) == 0)
  67. return defined_templates + i;
  68. }
  69. return NULL;
  70. }
  71. static struct ima_template_field *lookup_template_field(const char *field_id)
  72. {
  73. int i;
  74. for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
  75. if (strncmp(supported_fields[i].field_id, field_id,
  76. IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
  77. return &supported_fields[i];
  78. return NULL;
  79. }
  80. static int template_fmt_size(const char *template_fmt)
  81. {
  82. char c;
  83. int template_fmt_len = strlen(template_fmt);
  84. int i = 0, j = 0;
  85. while (i < template_fmt_len) {
  86. c = template_fmt[i];
  87. if (c == '|')
  88. j++;
  89. i++;
  90. }
  91. return j + 1;
  92. }
  93. static int template_desc_init_fields(const char *template_fmt,
  94. struct ima_template_field ***fields,
  95. int *num_fields)
  96. {
  97. char *c, *template_fmt_copy, *template_fmt_ptr;
  98. int template_num_fields = template_fmt_size(template_fmt);
  99. int i, result = 0;
  100. if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX)
  101. return -EINVAL;
  102. /* copying is needed as strsep() modifies the original buffer */
  103. template_fmt_copy = kstrdup(template_fmt, GFP_KERNEL);
  104. if (template_fmt_copy == NULL)
  105. return -ENOMEM;
  106. *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
  107. if (*fields == NULL) {
  108. result = -ENOMEM;
  109. goto out;
  110. }
  111. template_fmt_ptr = template_fmt_copy;
  112. for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
  113. i < template_num_fields; i++) {
  114. struct ima_template_field *f = lookup_template_field(c);
  115. if (!f) {
  116. result = -ENOENT;
  117. goto out;
  118. }
  119. (*fields)[i] = f;
  120. }
  121. *num_fields = i;
  122. out:
  123. if (result < 0) {
  124. kfree(*fields);
  125. *fields = NULL;
  126. }
  127. kfree(template_fmt_copy);
  128. return result;
  129. }
  130. static int init_defined_templates(void)
  131. {
  132. int i = 0;
  133. int result = 0;
  134. /* Init defined templates. */
  135. for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
  136. struct ima_template_desc *template = &defined_templates[i];
  137. result = template_desc_init_fields(template->fmt,
  138. &(template->fields),
  139. &(template->num_fields));
  140. if (result < 0)
  141. return result;
  142. }
  143. return result;
  144. }
  145. struct ima_template_desc *ima_template_desc_current(void)
  146. {
  147. if (!ima_template)
  148. ima_template =
  149. lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
  150. return ima_template;
  151. }
  152. int ima_init_template(void)
  153. {
  154. int result;
  155. result = init_defined_templates();
  156. if (result < 0)
  157. return result;
  158. return 0;
  159. }