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 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. 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 struct ima_template_field *lookup_template_field(const char *field_id)
  108. {
  109. int i;
  110. for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
  111. if (strncmp(supported_fields[i].field_id, field_id,
  112. IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
  113. return &supported_fields[i];
  114. return NULL;
  115. }
  116. static int template_fmt_size(const char *template_fmt)
  117. {
  118. char c;
  119. int template_fmt_len = strlen(template_fmt);
  120. int i = 0, j = 0;
  121. while (i < template_fmt_len) {
  122. c = template_fmt[i];
  123. if (c == '|')
  124. j++;
  125. i++;
  126. }
  127. return j + 1;
  128. }
  129. static int template_desc_init_fields(const char *template_fmt,
  130. struct ima_template_field ***fields,
  131. int *num_fields)
  132. {
  133. const char *template_fmt_ptr;
  134. struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
  135. int template_num_fields;
  136. int i, len;
  137. if (num_fields && *num_fields > 0) /* already initialized? */
  138. return 0;
  139. template_num_fields = template_fmt_size(template_fmt);
  140. if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
  141. pr_err("format string '%s' contains too many fields\n",
  142. template_fmt);
  143. return -EINVAL;
  144. }
  145. for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
  146. i++, template_fmt_ptr += len + 1) {
  147. char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
  148. len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
  149. if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
  150. pr_err("Invalid field with length %d\n", len);
  151. return -EINVAL;
  152. }
  153. memcpy(tmp_field_id, template_fmt_ptr, len);
  154. tmp_field_id[len] = '\0';
  155. found_fields[i] = lookup_template_field(tmp_field_id);
  156. if (!found_fields[i]) {
  157. pr_err("field '%s' not found\n", tmp_field_id);
  158. return -ENOENT;
  159. }
  160. }
  161. if (fields && num_fields) {
  162. *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
  163. if (*fields == NULL)
  164. return -ENOMEM;
  165. memcpy(*fields, found_fields, i * sizeof(*fields));
  166. *num_fields = i;
  167. }
  168. return 0;
  169. }
  170. void ima_init_template_list(void)
  171. {
  172. int i;
  173. if (!list_empty(&defined_templates))
  174. return;
  175. spin_lock(&template_list);
  176. for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
  177. list_add_tail_rcu(&builtin_templates[i].list,
  178. &defined_templates);
  179. }
  180. spin_unlock(&template_list);
  181. }
  182. struct ima_template_desc *ima_template_desc_current(void)
  183. {
  184. if (!ima_template) {
  185. ima_init_template_list();
  186. ima_template =
  187. lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
  188. }
  189. return ima_template;
  190. }
  191. int __init ima_init_template(void)
  192. {
  193. struct ima_template_desc *template = ima_template_desc_current();
  194. int result;
  195. result = template_desc_init_fields(template->fmt,
  196. &(template->fields),
  197. &(template->num_fields));
  198. if (result < 0)
  199. pr_err("template %s init failed, result: %d\n",
  200. (strlen(template->name) ?
  201. template->name : template->fmt), result);
  202. return result;
  203. }
  204. static struct ima_template_desc *restore_template_fmt(char *template_name)
  205. {
  206. struct ima_template_desc *template_desc = NULL;
  207. int ret;
  208. ret = template_desc_init_fields(template_name, NULL, NULL);
  209. if (ret < 0) {
  210. pr_err("attempting to initialize the template \"%s\" failed\n",
  211. template_name);
  212. goto out;
  213. }
  214. template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
  215. if (!template_desc)
  216. goto out;
  217. template_desc->name = "";
  218. template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
  219. if (!template_desc->fmt)
  220. goto out;
  221. spin_lock(&template_list);
  222. list_add_tail_rcu(&template_desc->list, &defined_templates);
  223. spin_unlock(&template_list);
  224. out:
  225. return template_desc;
  226. }
  227. static int ima_restore_template_data(struct ima_template_desc *template_desc,
  228. void *template_data,
  229. int template_data_size,
  230. struct ima_template_entry **entry)
  231. {
  232. int ret = 0;
  233. int i;
  234. *entry = kzalloc(sizeof(**entry) +
  235. template_desc->num_fields * sizeof(struct ima_field_data),
  236. GFP_NOFS);
  237. if (!*entry)
  238. return -ENOMEM;
  239. ret = ima_parse_buf(template_data, template_data + template_data_size,
  240. NULL, template_desc->num_fields,
  241. (*entry)->template_data, NULL, NULL,
  242. ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
  243. if (ret < 0) {
  244. kfree(*entry);
  245. return ret;
  246. }
  247. (*entry)->template_desc = template_desc;
  248. for (i = 0; i < template_desc->num_fields; i++) {
  249. struct ima_field_data *field_data = &(*entry)->template_data[i];
  250. u8 *data = field_data->data;
  251. (*entry)->template_data[i].data =
  252. kzalloc(field_data->len + 1, GFP_KERNEL);
  253. if (!(*entry)->template_data[i].data) {
  254. ret = -ENOMEM;
  255. break;
  256. }
  257. memcpy((*entry)->template_data[i].data, data, field_data->len);
  258. (*entry)->template_data_len += sizeof(field_data->len);
  259. (*entry)->template_data_len += field_data->len;
  260. }
  261. if (ret < 0) {
  262. ima_free_template_entry(*entry);
  263. *entry = NULL;
  264. }
  265. return ret;
  266. }
  267. /* Restore the serialized binary measurement list without extending PCRs. */
  268. int ima_restore_measurement_list(loff_t size, void *buf)
  269. {
  270. char template_name[MAX_TEMPLATE_NAME_LEN];
  271. struct ima_kexec_hdr *khdr = buf;
  272. struct ima_field_data hdr[HDR__LAST] = {
  273. [HDR_PCR] = {.len = sizeof(u32)},
  274. [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
  275. };
  276. void *bufp = buf + sizeof(*khdr);
  277. void *bufendp;
  278. struct ima_template_entry *entry;
  279. struct ima_template_desc *template_desc;
  280. DECLARE_BITMAP(hdr_mask, HDR__LAST);
  281. unsigned long count = 0;
  282. int ret = 0;
  283. if (!buf || size < sizeof(*khdr))
  284. return 0;
  285. if (ima_canonical_fmt) {
  286. khdr->version = le16_to_cpu(khdr->version);
  287. khdr->count = le64_to_cpu(khdr->count);
  288. khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
  289. }
  290. if (khdr->version != 1) {
  291. pr_err("attempting to restore a incompatible measurement list");
  292. return -EINVAL;
  293. }
  294. if (khdr->count > ULONG_MAX - 1) {
  295. pr_err("attempting to restore too many measurements");
  296. return -EINVAL;
  297. }
  298. bitmap_zero(hdr_mask, HDR__LAST);
  299. bitmap_set(hdr_mask, HDR_PCR, 1);
  300. bitmap_set(hdr_mask, HDR_DIGEST, 1);
  301. /*
  302. * ima kexec buffer prefix: version, buffer size, count
  303. * v1 format: pcr, digest, template-name-len, template-name,
  304. * template-data-size, template-data
  305. */
  306. bufendp = buf + khdr->buffer_size;
  307. while ((bufp < bufendp) && (count++ < khdr->count)) {
  308. int enforce_mask = ENFORCE_FIELDS;
  309. enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
  310. ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
  311. hdr_mask, enforce_mask, "entry header");
  312. if (ret < 0)
  313. break;
  314. if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
  315. pr_err("attempting to restore a template name that is too long\n");
  316. ret = -EINVAL;
  317. break;
  318. }
  319. /* template name is not null terminated */
  320. memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
  321. hdr[HDR_TEMPLATE_NAME].len);
  322. template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
  323. if (strcmp(template_name, "ima") == 0) {
  324. pr_err("attempting to restore an unsupported template \"%s\" failed\n",
  325. template_name);
  326. ret = -EINVAL;
  327. break;
  328. }
  329. template_desc = lookup_template_desc(template_name);
  330. if (!template_desc) {
  331. template_desc = restore_template_fmt(template_name);
  332. if (!template_desc)
  333. break;
  334. }
  335. /*
  336. * Only the running system's template format is initialized
  337. * on boot. As needed, initialize the other template formats.
  338. */
  339. ret = template_desc_init_fields(template_desc->fmt,
  340. &(template_desc->fields),
  341. &(template_desc->num_fields));
  342. if (ret < 0) {
  343. pr_err("attempting to restore the template fmt \"%s\" failed\n",
  344. template_desc->fmt);
  345. ret = -EINVAL;
  346. break;
  347. }
  348. ret = ima_restore_template_data(template_desc,
  349. hdr[HDR_TEMPLATE_DATA].data,
  350. hdr[HDR_TEMPLATE_DATA].len,
  351. &entry);
  352. if (ret < 0)
  353. break;
  354. memcpy(entry->digest, hdr[HDR_DIGEST].data,
  355. hdr[HDR_DIGEST].len);
  356. entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
  357. le32_to_cpu(*(hdr[HDR_PCR].data));
  358. ret = ima_restore_measurement_entry(entry);
  359. if (ret < 0)
  360. break;
  361. }
  362. return ret;
  363. }