ima_template.c 4.5 KB

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