ima_template.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/rculist.h>
  17. #include "ima.h"
  18. #include "ima_template_lib.h"
  19. enum header_fields { HDR_PCR, HDR_DIGEST, HDR_TEMPLATE_NAME,
  20. HDR_TEMPLATE_DATA, HDR__LAST };
  21. static struct ima_template_desc builtin_templates[] = {
  22. {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
  23. {.name = "ima-ng", .fmt = "d-ng|n-ng"},
  24. {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
  25. {.name = "", .fmt = ""}, /* placeholder for a custom format */
  26. };
  27. static LIST_HEAD(defined_templates);
  28. static DEFINE_SPINLOCK(template_list);
  29. static const struct ima_template_field supported_fields[] = {
  30. {.field_id = "d", .field_init = ima_eventdigest_init,
  31. .field_show = ima_show_template_digest},
  32. {.field_id = "n", .field_init = ima_eventname_init,
  33. .field_show = ima_show_template_string},
  34. {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
  35. .field_show = ima_show_template_digest_ng},
  36. {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
  37. .field_show = ima_show_template_string},
  38. {.field_id = "sig", .field_init = ima_eventsig_init,
  39. .field_show = ima_show_template_sig},
  40. };
  41. #define MAX_TEMPLATE_NAME_LEN 15
  42. static struct ima_template_desc *ima_template;
  43. static struct ima_template_desc *lookup_template_desc(const char *name);
  44. static int template_desc_init_fields(const char *template_fmt,
  45. const struct ima_template_field ***fields,
  46. int *num_fields);
  47. static int __init ima_template_setup(char *str)
  48. {
  49. struct ima_template_desc *template_desc;
  50. int template_len = strlen(str);
  51. if (ima_template)
  52. return 1;
  53. ima_init_template_list();
  54. /*
  55. * Verify that a template with the supplied name exists.
  56. * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
  57. */
  58. template_desc = lookup_template_desc(str);
  59. if (!template_desc) {
  60. pr_err("template %s not found, using %s\n",
  61. str, CONFIG_IMA_DEFAULT_TEMPLATE);
  62. return 1;
  63. }
  64. /*
  65. * Verify whether the current hash algorithm is supported
  66. * by the 'ima' template.
  67. */
  68. if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
  69. ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
  70. pr_err("template does not support hash alg\n");
  71. return 1;
  72. }
  73. ima_template = template_desc;
  74. return 1;
  75. }
  76. __setup("ima_template=", ima_template_setup);
  77. static int __init ima_template_fmt_setup(char *str)
  78. {
  79. int num_templates = ARRAY_SIZE(builtin_templates);
  80. if (ima_template)
  81. return 1;
  82. if (template_desc_init_fields(str, NULL, NULL) < 0) {
  83. pr_err("format string '%s' not valid, using template %s\n",
  84. str, CONFIG_IMA_DEFAULT_TEMPLATE);
  85. return 1;
  86. }
  87. builtin_templates[num_templates - 1].fmt = str;
  88. ima_template = builtin_templates + num_templates - 1;
  89. return 1;
  90. }
  91. __setup("ima_template_fmt=", ima_template_fmt_setup);
  92. static struct ima_template_desc *lookup_template_desc(const char *name)
  93. {
  94. struct ima_template_desc *template_desc;
  95. int found = 0;
  96. rcu_read_lock();
  97. list_for_each_entry_rcu(template_desc, &defined_templates, list) {
  98. if ((strcmp(template_desc->name, name) == 0) ||
  99. (strcmp(template_desc->fmt, name) == 0)) {
  100. found = 1;
  101. break;
  102. }
  103. }
  104. rcu_read_unlock();
  105. return found ? template_desc : NULL;
  106. }
  107. static const struct ima_template_field *
  108. lookup_template_field(const char *field_id)
  109. {
  110. int i;
  111. for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
  112. if (strncmp(supported_fields[i].field_id, field_id,
  113. IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
  114. return &supported_fields[i];
  115. return NULL;
  116. }
  117. static int template_fmt_size(const char *template_fmt)
  118. {
  119. char c;
  120. int template_fmt_len = strlen(template_fmt);
  121. int i = 0, j = 0;
  122. while (i < template_fmt_len) {
  123. c = template_fmt[i];
  124. if (c == '|')
  125. j++;
  126. i++;
  127. }
  128. return j + 1;
  129. }
  130. static int template_desc_init_fields(const char *template_fmt,
  131. const struct ima_template_field ***fields,
  132. int *num_fields)
  133. {
  134. const char *template_fmt_ptr;
  135. const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
  136. int template_num_fields;
  137. int i, len;
  138. if (num_fields && *num_fields > 0) /* already initialized? */
  139. return 0;
  140. template_num_fields = template_fmt_size(template_fmt);
  141. if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
  142. pr_err("format string '%s' contains too many fields\n",
  143. template_fmt);
  144. return -EINVAL;
  145. }
  146. for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
  147. i++, template_fmt_ptr += len + 1) {
  148. char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
  149. len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
  150. if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
  151. pr_err("Invalid field with length %d\n", len);
  152. return -EINVAL;
  153. }
  154. memcpy(tmp_field_id, template_fmt_ptr, len);
  155. tmp_field_id[len] = '\0';
  156. found_fields[i] = lookup_template_field(tmp_field_id);
  157. if (!found_fields[i]) {
  158. pr_err("field '%s' not found\n", tmp_field_id);
  159. return -ENOENT;
  160. }
  161. }
  162. if (fields && num_fields) {
  163. *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
  164. if (*fields == NULL)
  165. return -ENOMEM;
  166. memcpy(*fields, found_fields, i * sizeof(*fields));
  167. *num_fields = i;
  168. }
  169. return 0;
  170. }
  171. void ima_init_template_list(void)
  172. {
  173. int i;
  174. if (!list_empty(&defined_templates))
  175. return;
  176. spin_lock(&template_list);
  177. for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
  178. list_add_tail_rcu(&builtin_templates[i].list,
  179. &defined_templates);
  180. }
  181. spin_unlock(&template_list);
  182. }
  183. struct ima_template_desc *ima_template_desc_current(void)
  184. {
  185. if (!ima_template) {
  186. ima_init_template_list();
  187. ima_template =
  188. lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
  189. }
  190. return ima_template;
  191. }
  192. int __init ima_init_template(void)
  193. {
  194. struct ima_template_desc *template = ima_template_desc_current();
  195. int result;
  196. result = template_desc_init_fields(template->fmt,
  197. &(template->fields),
  198. &(template->num_fields));
  199. if (result < 0)
  200. pr_err("template %s init failed, result: %d\n",
  201. (strlen(template->name) ?
  202. template->name : template->fmt), result);
  203. return result;
  204. }
  205. static struct ima_template_desc *restore_template_fmt(char *template_name)
  206. {
  207. struct ima_template_desc *template_desc = NULL;
  208. int ret;
  209. ret = template_desc_init_fields(template_name, NULL, NULL);
  210. if (ret < 0) {
  211. pr_err("attempting to initialize the template \"%s\" failed\n",
  212. template_name);
  213. goto out;
  214. }
  215. template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
  216. if (!template_desc)
  217. goto out;
  218. template_desc->name = "";
  219. template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
  220. if (!template_desc->fmt)
  221. goto out;
  222. spin_lock(&template_list);
  223. list_add_tail_rcu(&template_desc->list, &defined_templates);
  224. spin_unlock(&template_list);
  225. out:
  226. return template_desc;
  227. }
  228. static int ima_restore_template_data(struct ima_template_desc *template_desc,
  229. void *template_data,
  230. int template_data_size,
  231. struct ima_template_entry **entry)
  232. {
  233. int ret = 0;
  234. int i;
  235. *entry = kzalloc(sizeof(**entry) +
  236. template_desc->num_fields * sizeof(struct ima_field_data),
  237. GFP_NOFS);
  238. if (!*entry)
  239. return -ENOMEM;
  240. ret = ima_parse_buf(template_data, template_data + template_data_size,
  241. NULL, template_desc->num_fields,
  242. (*entry)->template_data, NULL, NULL,
  243. ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
  244. if (ret < 0) {
  245. kfree(*entry);
  246. return ret;
  247. }
  248. (*entry)->template_desc = template_desc;
  249. for (i = 0; i < template_desc->num_fields; i++) {
  250. struct ima_field_data *field_data = &(*entry)->template_data[i];
  251. u8 *data = field_data->data;
  252. (*entry)->template_data[i].data =
  253. kzalloc(field_data->len + 1, GFP_KERNEL);
  254. if (!(*entry)->template_data[i].data) {
  255. ret = -ENOMEM;
  256. break;
  257. }
  258. memcpy((*entry)->template_data[i].data, data, field_data->len);
  259. (*entry)->template_data_len += sizeof(field_data->len);
  260. (*entry)->template_data_len += field_data->len;
  261. }
  262. if (ret < 0) {
  263. ima_free_template_entry(*entry);
  264. *entry = NULL;
  265. }
  266. return ret;
  267. }
  268. /* Restore the serialized binary measurement list without extending PCRs. */
  269. int ima_restore_measurement_list(loff_t size, void *buf)
  270. {
  271. char template_name[MAX_TEMPLATE_NAME_LEN];
  272. struct ima_kexec_hdr *khdr = buf;
  273. struct ima_field_data hdr[HDR__LAST] = {
  274. [HDR_PCR] = {.len = sizeof(u32)},
  275. [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
  276. };
  277. void *bufp = buf + sizeof(*khdr);
  278. void *bufendp;
  279. struct ima_template_entry *entry;
  280. struct ima_template_desc *template_desc;
  281. DECLARE_BITMAP(hdr_mask, HDR__LAST);
  282. unsigned long count = 0;
  283. int ret = 0;
  284. if (!buf || size < sizeof(*khdr))
  285. return 0;
  286. if (ima_canonical_fmt) {
  287. khdr->version = le16_to_cpu(khdr->version);
  288. khdr->count = le64_to_cpu(khdr->count);
  289. khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
  290. }
  291. if (khdr->version != 1) {
  292. pr_err("attempting to restore a incompatible measurement list");
  293. return -EINVAL;
  294. }
  295. if (khdr->count > ULONG_MAX - 1) {
  296. pr_err("attempting to restore too many measurements");
  297. return -EINVAL;
  298. }
  299. bitmap_zero(hdr_mask, HDR__LAST);
  300. bitmap_set(hdr_mask, HDR_PCR, 1);
  301. bitmap_set(hdr_mask, HDR_DIGEST, 1);
  302. /*
  303. * ima kexec buffer prefix: version, buffer size, count
  304. * v1 format: pcr, digest, template-name-len, template-name,
  305. * template-data-size, template-data
  306. */
  307. bufendp = buf + khdr->buffer_size;
  308. while ((bufp < bufendp) && (count++ < khdr->count)) {
  309. int enforce_mask = ENFORCE_FIELDS;
  310. enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
  311. ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
  312. hdr_mask, enforce_mask, "entry header");
  313. if (ret < 0)
  314. break;
  315. if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
  316. pr_err("attempting to restore a template name that is too long\n");
  317. ret = -EINVAL;
  318. break;
  319. }
  320. /* template name is not null terminated */
  321. memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
  322. hdr[HDR_TEMPLATE_NAME].len);
  323. template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
  324. if (strcmp(template_name, "ima") == 0) {
  325. pr_err("attempting to restore an unsupported template \"%s\" failed\n",
  326. template_name);
  327. ret = -EINVAL;
  328. break;
  329. }
  330. template_desc = lookup_template_desc(template_name);
  331. if (!template_desc) {
  332. template_desc = restore_template_fmt(template_name);
  333. if (!template_desc)
  334. break;
  335. }
  336. /*
  337. * Only the running system's template format is initialized
  338. * on boot. As needed, initialize the other template formats.
  339. */
  340. ret = template_desc_init_fields(template_desc->fmt,
  341. &(template_desc->fields),
  342. &(template_desc->num_fields));
  343. if (ret < 0) {
  344. pr_err("attempting to restore the template fmt \"%s\" failed\n",
  345. template_desc->fmt);
  346. ret = -EINVAL;
  347. break;
  348. }
  349. ret = ima_restore_template_data(template_desc,
  350. hdr[HDR_TEMPLATE_DATA].data,
  351. hdr[HDR_TEMPLATE_DATA].len,
  352. &entry);
  353. if (ret < 0)
  354. break;
  355. memcpy(entry->digest, hdr[HDR_DIGEST].data,
  356. hdr[HDR_DIGEST].len);
  357. entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
  358. le32_to_cpu(*(hdr[HDR_PCR].data));
  359. ret = ima_restore_measurement_entry(entry);
  360. if (ret < 0)
  361. break;
  362. }
  363. return ret;
  364. }