ima_template.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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(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(char *template_fmt,
  93. struct ima_template_field ***fields,
  94. int *num_fields)
  95. {
  96. char *c, *template_fmt_ptr = template_fmt;
  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. *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
  102. if (*fields == NULL) {
  103. result = -ENOMEM;
  104. goto out;
  105. }
  106. for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
  107. i < template_num_fields; i++) {
  108. struct ima_template_field *f = lookup_template_field(c);
  109. if (!f) {
  110. result = -ENOENT;
  111. goto out;
  112. }
  113. (*fields)[i] = f;
  114. }
  115. *num_fields = i;
  116. return 0;
  117. out:
  118. kfree(*fields);
  119. *fields = NULL;
  120. return result;
  121. }
  122. static int init_defined_templates(void)
  123. {
  124. int i = 0;
  125. int result = 0;
  126. /* Init defined templates. */
  127. for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
  128. struct ima_template_desc *template = &defined_templates[i];
  129. result = template_desc_init_fields(template->fmt,
  130. &(template->fields),
  131. &(template->num_fields));
  132. if (result < 0)
  133. return result;
  134. }
  135. return result;
  136. }
  137. struct ima_template_desc *ima_template_desc_current(void)
  138. {
  139. if (!ima_template)
  140. ima_template =
  141. lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
  142. return ima_template;
  143. }
  144. int ima_init_template(void)
  145. {
  146. int result;
  147. result = init_defined_templates();
  148. if (result < 0)
  149. return result;
  150. return 0;
  151. }