ima_template.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "ima.h"
  16. #include "ima_template_lib.h"
  17. static struct ima_template_desc defined_templates[] = {
  18. {.name = IMA_TEMPLATE_IMA_NAME,.fmt = IMA_TEMPLATE_IMA_FMT},
  19. };
  20. static struct ima_template_field supported_fields[] = {
  21. {.field_id = "d",.field_init = ima_eventdigest_init,
  22. .field_show = ima_show_template_digest},
  23. {.field_id = "n",.field_init = ima_eventname_init,
  24. .field_show = ima_show_template_string},
  25. };
  26. static struct ima_template_field *lookup_template_field(const char *field_id)
  27. {
  28. int i;
  29. for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
  30. if (strncmp(supported_fields[i].field_id, field_id,
  31. IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
  32. return &supported_fields[i];
  33. return NULL;
  34. }
  35. static int template_fmt_size(char *template_fmt)
  36. {
  37. char c;
  38. int template_fmt_len = strlen(template_fmt);
  39. int i = 0, j = 0;
  40. while (i < template_fmt_len) {
  41. c = template_fmt[i];
  42. if (c == '|')
  43. j++;
  44. i++;
  45. }
  46. return j + 1;
  47. }
  48. static int template_desc_init_fields(char *template_fmt,
  49. struct ima_template_field ***fields,
  50. int *num_fields)
  51. {
  52. char *c, *template_fmt_ptr = template_fmt;
  53. int template_num_fields = template_fmt_size(template_fmt);
  54. int i, result = 0;
  55. if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX)
  56. return -EINVAL;
  57. *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
  58. if (*fields == NULL) {
  59. result = -ENOMEM;
  60. goto out;
  61. }
  62. for (i = 0; (c = strsep(&template_fmt_ptr, "|")) != NULL &&
  63. i < template_num_fields; i++) {
  64. struct ima_template_field *f = lookup_template_field(c);
  65. if (!f) {
  66. result = -ENOENT;
  67. goto out;
  68. }
  69. (*fields)[i] = f;
  70. }
  71. *num_fields = i;
  72. return 0;
  73. out:
  74. kfree(*fields);
  75. *fields = NULL;
  76. return result;
  77. }
  78. static int init_defined_templates(void)
  79. {
  80. int i = 0;
  81. int result = 0;
  82. /* Init defined templates. */
  83. for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
  84. struct ima_template_desc *template = &defined_templates[i];
  85. result = template_desc_init_fields(template->fmt,
  86. &(template->fields),
  87. &(template->num_fields));
  88. if (result < 0)
  89. return result;
  90. }
  91. return result;
  92. }
  93. int ima_init_template(void)
  94. {
  95. int result;
  96. result = init_defined_templates();
  97. if (result < 0)
  98. return result;
  99. return 0;
  100. }