ima_fs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Kylene Hall <kjhall@us.ibm.com>
  6. * Reiner Sailer <sailer@us.ibm.com>
  7. * Mimi Zohar <zohar@us.ibm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. *
  14. * File: ima_fs.c
  15. * implemenents security file system for reporting
  16. * current measurement list and IMA statistics
  17. */
  18. #include <linux/fcntl.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/rculist.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/parser.h>
  25. #include "ima.h"
  26. static int valid_policy = 1;
  27. static ssize_t ima_show_htable_value(char __user *buf, size_t count,
  28. loff_t *ppos, atomic_long_t *val)
  29. {
  30. char tmpbuf[32]; /* greater than largest 'long' string value */
  31. ssize_t len;
  32. len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
  33. return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
  34. }
  35. static ssize_t ima_show_htable_violations(struct file *filp,
  36. char __user *buf,
  37. size_t count, loff_t *ppos)
  38. {
  39. return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
  40. }
  41. static const struct file_operations ima_htable_violations_ops = {
  42. .read = ima_show_htable_violations,
  43. .llseek = generic_file_llseek,
  44. };
  45. static ssize_t ima_show_measurements_count(struct file *filp,
  46. char __user *buf,
  47. size_t count, loff_t *ppos)
  48. {
  49. return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
  50. }
  51. static const struct file_operations ima_measurements_count_ops = {
  52. .read = ima_show_measurements_count,
  53. .llseek = generic_file_llseek,
  54. };
  55. /* returns pointer to hlist_node */
  56. static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
  57. {
  58. loff_t l = *pos;
  59. struct ima_queue_entry *qe;
  60. /* we need a lock since pos could point beyond last element */
  61. rcu_read_lock();
  62. list_for_each_entry_rcu(qe, &ima_measurements, later) {
  63. if (!l--) {
  64. rcu_read_unlock();
  65. return qe;
  66. }
  67. }
  68. rcu_read_unlock();
  69. return NULL;
  70. }
  71. static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
  72. {
  73. struct ima_queue_entry *qe = v;
  74. /* lock protects when reading beyond last element
  75. * against concurrent list-extension
  76. */
  77. rcu_read_lock();
  78. qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
  79. rcu_read_unlock();
  80. (*pos)++;
  81. return (&qe->later == &ima_measurements) ? NULL : qe;
  82. }
  83. static void ima_measurements_stop(struct seq_file *m, void *v)
  84. {
  85. }
  86. void ima_putc(struct seq_file *m, void *data, int datalen)
  87. {
  88. while (datalen--)
  89. seq_putc(m, *(char *)data++);
  90. }
  91. /* print format:
  92. * 32bit-le=pcr#
  93. * char[20]=template digest
  94. * 32bit-le=template name size
  95. * char[n]=template name
  96. * [eventdata length]
  97. * eventdata[n]=template specific data
  98. */
  99. static int ima_measurements_show(struct seq_file *m, void *v)
  100. {
  101. /* the list never shrinks, so we don't need a lock here */
  102. struct ima_queue_entry *qe = v;
  103. struct ima_template_entry *e;
  104. char *template_name;
  105. int namelen;
  106. u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  107. bool is_ima_template = false;
  108. int i;
  109. /* get entry */
  110. e = qe->entry;
  111. if (e == NULL)
  112. return -1;
  113. template_name = (e->template_desc->name[0] != '\0') ?
  114. e->template_desc->name : e->template_desc->fmt;
  115. /*
  116. * 1st: PCRIndex
  117. * PCR used is always the same (config option) in
  118. * little-endian format
  119. */
  120. ima_putc(m, &pcr, sizeof(pcr));
  121. /* 2nd: template digest */
  122. ima_putc(m, e->digest, TPM_DIGEST_SIZE);
  123. /* 3rd: template name size */
  124. namelen = strlen(template_name);
  125. ima_putc(m, &namelen, sizeof(namelen));
  126. /* 4th: template name */
  127. ima_putc(m, template_name, namelen);
  128. /* 5th: template length (except for 'ima' template) */
  129. if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
  130. is_ima_template = true;
  131. if (!is_ima_template)
  132. ima_putc(m, &e->template_data_len,
  133. sizeof(e->template_data_len));
  134. /* 6th: template specific data */
  135. for (i = 0; i < e->template_desc->num_fields; i++) {
  136. enum ima_show_type show = IMA_SHOW_BINARY;
  137. struct ima_template_field *field = e->template_desc->fields[i];
  138. if (is_ima_template && strcmp(field->field_id, "d") == 0)
  139. show = IMA_SHOW_BINARY_NO_FIELD_LEN;
  140. if (is_ima_template && strcmp(field->field_id, "n") == 0)
  141. show = IMA_SHOW_BINARY_OLD_STRING_FMT;
  142. field->field_show(m, show, &e->template_data[i]);
  143. }
  144. return 0;
  145. }
  146. static const struct seq_operations ima_measurments_seqops = {
  147. .start = ima_measurements_start,
  148. .next = ima_measurements_next,
  149. .stop = ima_measurements_stop,
  150. .show = ima_measurements_show
  151. };
  152. static int ima_measurements_open(struct inode *inode, struct file *file)
  153. {
  154. return seq_open(file, &ima_measurments_seqops);
  155. }
  156. static const struct file_operations ima_measurements_ops = {
  157. .open = ima_measurements_open,
  158. .read = seq_read,
  159. .llseek = seq_lseek,
  160. .release = seq_release,
  161. };
  162. void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
  163. {
  164. u32 i;
  165. for (i = 0; i < size; i++)
  166. seq_printf(m, "%02x", *(digest + i));
  167. }
  168. /* print in ascii */
  169. static int ima_ascii_measurements_show(struct seq_file *m, void *v)
  170. {
  171. /* the list never shrinks, so we don't need a lock here */
  172. struct ima_queue_entry *qe = v;
  173. struct ima_template_entry *e;
  174. char *template_name;
  175. int i;
  176. /* get entry */
  177. e = qe->entry;
  178. if (e == NULL)
  179. return -1;
  180. template_name = (e->template_desc->name[0] != '\0') ?
  181. e->template_desc->name : e->template_desc->fmt;
  182. /* 1st: PCR used (config option) */
  183. seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
  184. /* 2nd: SHA1 template hash */
  185. ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
  186. /* 3th: template name */
  187. seq_printf(m, " %s", template_name);
  188. /* 4th: template specific data */
  189. for (i = 0; i < e->template_desc->num_fields; i++) {
  190. seq_puts(m, " ");
  191. if (e->template_data[i].len == 0)
  192. continue;
  193. e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
  194. &e->template_data[i]);
  195. }
  196. seq_puts(m, "\n");
  197. return 0;
  198. }
  199. static const struct seq_operations ima_ascii_measurements_seqops = {
  200. .start = ima_measurements_start,
  201. .next = ima_measurements_next,
  202. .stop = ima_measurements_stop,
  203. .show = ima_ascii_measurements_show
  204. };
  205. static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
  206. {
  207. return seq_open(file, &ima_ascii_measurements_seqops);
  208. }
  209. static const struct file_operations ima_ascii_measurements_ops = {
  210. .open = ima_ascii_measurements_open,
  211. .read = seq_read,
  212. .llseek = seq_lseek,
  213. .release = seq_release,
  214. };
  215. static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  216. size_t datalen, loff_t *ppos)
  217. {
  218. char *data = NULL;
  219. ssize_t result;
  220. if (datalen >= PAGE_SIZE)
  221. datalen = PAGE_SIZE - 1;
  222. /* No partial writes. */
  223. result = -EINVAL;
  224. if (*ppos != 0)
  225. goto out;
  226. result = -ENOMEM;
  227. data = kmalloc(datalen + 1, GFP_KERNEL);
  228. if (!data)
  229. goto out;
  230. *(data + datalen) = '\0';
  231. result = -EFAULT;
  232. if (copy_from_user(data, buf, datalen))
  233. goto out;
  234. result = ima_parse_add_rule(data);
  235. out:
  236. if (result < 0)
  237. valid_policy = 0;
  238. kfree(data);
  239. return result;
  240. }
  241. static struct dentry *ima_dir;
  242. static struct dentry *binary_runtime_measurements;
  243. static struct dentry *ascii_runtime_measurements;
  244. static struct dentry *runtime_measurements_count;
  245. static struct dentry *violations;
  246. static struct dentry *ima_policy;
  247. enum ima_fs_flags {
  248. IMA_FS_BUSY,
  249. };
  250. static unsigned long ima_fs_flags;
  251. /*
  252. * ima_open_policy: sequentialize access to the policy file
  253. */
  254. static int ima_open_policy(struct inode *inode, struct file *filp)
  255. {
  256. /* No point in being allowed to open it if you aren't going to write */
  257. if (!(filp->f_flags & O_WRONLY))
  258. return -EACCES;
  259. if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
  260. return -EBUSY;
  261. return 0;
  262. }
  263. /*
  264. * ima_release_policy - start using the new measure policy rules.
  265. *
  266. * Initially, ima_measure points to the default policy rules, now
  267. * point to the new policy rules, and remove the securityfs policy file,
  268. * assuming a valid policy.
  269. */
  270. static int ima_release_policy(struct inode *inode, struct file *file)
  271. {
  272. const char *cause = valid_policy ? "completed" : "failed";
  273. pr_info("IMA: policy update %s\n", cause);
  274. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
  275. "policy_update", cause, !valid_policy, 0);
  276. if (!valid_policy) {
  277. ima_delete_rules();
  278. valid_policy = 1;
  279. clear_bit(IMA_FS_BUSY, &ima_fs_flags);
  280. return 0;
  281. }
  282. ima_update_policy();
  283. securityfs_remove(ima_policy);
  284. ima_policy = NULL;
  285. return 0;
  286. }
  287. static const struct file_operations ima_measure_policy_ops = {
  288. .open = ima_open_policy,
  289. .write = ima_write_policy,
  290. .release = ima_release_policy,
  291. .llseek = generic_file_llseek,
  292. };
  293. int __init ima_fs_init(void)
  294. {
  295. ima_dir = securityfs_create_dir("ima", NULL);
  296. if (IS_ERR(ima_dir))
  297. return -1;
  298. binary_runtime_measurements =
  299. securityfs_create_file("binary_runtime_measurements",
  300. S_IRUSR | S_IRGRP, ima_dir, NULL,
  301. &ima_measurements_ops);
  302. if (IS_ERR(binary_runtime_measurements))
  303. goto out;
  304. ascii_runtime_measurements =
  305. securityfs_create_file("ascii_runtime_measurements",
  306. S_IRUSR | S_IRGRP, ima_dir, NULL,
  307. &ima_ascii_measurements_ops);
  308. if (IS_ERR(ascii_runtime_measurements))
  309. goto out;
  310. runtime_measurements_count =
  311. securityfs_create_file("runtime_measurements_count",
  312. S_IRUSR | S_IRGRP, ima_dir, NULL,
  313. &ima_measurements_count_ops);
  314. if (IS_ERR(runtime_measurements_count))
  315. goto out;
  316. violations =
  317. securityfs_create_file("violations", S_IRUSR | S_IRGRP,
  318. ima_dir, NULL, &ima_htable_violations_ops);
  319. if (IS_ERR(violations))
  320. goto out;
  321. ima_policy = securityfs_create_file("policy",
  322. S_IWUSR,
  323. ima_dir, NULL,
  324. &ima_measure_policy_ops);
  325. if (IS_ERR(ima_policy))
  326. goto out;
  327. return 0;
  328. out:
  329. securityfs_remove(violations);
  330. securityfs_remove(runtime_measurements_count);
  331. securityfs_remove(ascii_runtime_measurements);
  332. securityfs_remove(binary_runtime_measurements);
  333. securityfs_remove(ima_dir);
  334. securityfs_remove(ima_policy);
  335. return -1;
  336. }