ima_template.c 2.9 KB

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