ima_template.c 4.1 KB

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