ima_fs.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #define TMPBUFLEN 12
  28. static ssize_t ima_show_htable_value(char __user *buf, size_t count,
  29. loff_t *ppos, atomic_long_t *val)
  30. {
  31. char tmpbuf[TMPBUFLEN];
  32. ssize_t len;
  33. len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
  34. return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
  35. }
  36. static ssize_t ima_show_htable_violations(struct file *filp,
  37. char __user *buf,
  38. size_t count, loff_t *ppos)
  39. {
  40. return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
  41. }
  42. static const struct file_operations ima_htable_violations_ops = {
  43. .read = ima_show_htable_violations,
  44. .llseek = generic_file_llseek,
  45. };
  46. static ssize_t ima_show_measurements_count(struct file *filp,
  47. char __user *buf,
  48. size_t count, loff_t *ppos)
  49. {
  50. return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
  51. }
  52. static const struct file_operations ima_measurements_count_ops = {
  53. .read = ima_show_measurements_count,
  54. .llseek = generic_file_llseek,
  55. };
  56. /* returns pointer to hlist_node */
  57. static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
  58. {
  59. loff_t l = *pos;
  60. struct ima_queue_entry *qe;
  61. /* we need a lock since pos could point beyond last element */
  62. rcu_read_lock();
  63. list_for_each_entry_rcu(qe, &ima_measurements, later) {
  64. if (!l--) {
  65. rcu_read_unlock();
  66. return qe;
  67. }
  68. }
  69. rcu_read_unlock();
  70. return NULL;
  71. }
  72. static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
  73. {
  74. struct ima_queue_entry *qe = v;
  75. /* lock protects when reading beyond last element
  76. * against concurrent list-extension
  77. */
  78. rcu_read_lock();
  79. qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
  80. rcu_read_unlock();
  81. (*pos)++;
  82. return (&qe->later == &ima_measurements) ? NULL : qe;
  83. }
  84. static void ima_measurements_stop(struct seq_file *m, void *v)
  85. {
  86. }
  87. void ima_putc(struct seq_file *m, void *data, int datalen)
  88. {
  89. while (datalen--)
  90. seq_putc(m, *(char *)data++);
  91. }
  92. /* print format:
  93. * 32bit-le=pcr#
  94. * char[20]=template digest
  95. * 32bit-le=template name size
  96. * char[n]=template name
  97. * [eventdata length]
  98. * eventdata[n]=template specific data
  99. */
  100. static int ima_measurements_show(struct seq_file *m, void *v)
  101. {
  102. /* the list never shrinks, so we don't need a lock here */
  103. struct ima_queue_entry *qe = v;
  104. struct ima_template_entry *e;
  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. /*
  114. * 1st: PCRIndex
  115. * PCR used is always the same (config option) in
  116. * little-endian format
  117. */
  118. ima_putc(m, &pcr, sizeof pcr);
  119. /* 2nd: template digest */
  120. ima_putc(m, e->digest, TPM_DIGEST_SIZE);
  121. /* 3rd: template name size */
  122. namelen = strlen(e->template_desc->name);
  123. ima_putc(m, &namelen, sizeof namelen);
  124. /* 4th: template name */
  125. ima_putc(m, e->template_desc->name, namelen);
  126. /* 5th: template length (except for 'ima' template) */
  127. if (strcmp(e->template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0)
  128. is_ima_template = true;
  129. if (!is_ima_template)
  130. ima_putc(m, &e->template_data_len,
  131. sizeof(e->template_data_len));
  132. /* 6th: template specific data */
  133. for (i = 0; i < e->template_desc->num_fields; i++) {
  134. enum ima_show_type show = IMA_SHOW_BINARY;
  135. struct ima_template_field *field = e->template_desc->fields[i];
  136. if (is_ima_template && strcmp(field->field_id, "d") == 0)
  137. show = IMA_SHOW_BINARY_NO_FIELD_LEN;
  138. field->field_show(m, show, &e->template_data[i]);
  139. }
  140. return 0;
  141. }
  142. static const struct seq_operations ima_measurments_seqops = {
  143. .start = ima_measurements_start,
  144. .next = ima_measurements_next,
  145. .stop = ima_measurements_stop,
  146. .show = ima_measurements_show
  147. };
  148. static int ima_measurements_open(struct inode *inode, struct file *file)
  149. {
  150. return seq_open(file, &ima_measurments_seqops);
  151. }
  152. static const struct file_operations ima_measurements_ops = {
  153. .open = ima_measurements_open,
  154. .read = seq_read,
  155. .llseek = seq_lseek,
  156. .release = seq_release,
  157. };
  158. void ima_print_digest(struct seq_file *m, u8 *digest, int size)
  159. {
  160. int i;
  161. for (i = 0; i < size; i++)
  162. seq_printf(m, "%02x", *(digest + i));
  163. }
  164. /* print in ascii */
  165. static int ima_ascii_measurements_show(struct seq_file *m, void *v)
  166. {
  167. /* the list never shrinks, so we don't need a lock here */
  168. struct ima_queue_entry *qe = v;
  169. struct ima_template_entry *e;
  170. int i;
  171. /* get entry */
  172. e = qe->entry;
  173. if (e == NULL)
  174. return -1;
  175. /* 1st: PCR used (config option) */
  176. seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
  177. /* 2nd: SHA1 template hash */
  178. ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
  179. /* 3th: template name */
  180. seq_printf(m, " %s", e->template_desc->name);
  181. /* 4th: template specific data */
  182. for (i = 0; i < e->template_desc->num_fields; i++) {
  183. seq_puts(m, " ");
  184. if (e->template_data[i].len == 0)
  185. continue;
  186. e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
  187. &e->template_data[i]);
  188. }
  189. seq_puts(m, "\n");
  190. return 0;
  191. }
  192. static const struct seq_operations ima_ascii_measurements_seqops = {
  193. .start = ima_measurements_start,
  194. .next = ima_measurements_next,
  195. .stop = ima_measurements_stop,
  196. .show = ima_ascii_measurements_show
  197. };
  198. static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
  199. {
  200. return seq_open(file, &ima_ascii_measurements_seqops);
  201. }
  202. static const struct file_operations ima_ascii_measurements_ops = {
  203. .open = ima_ascii_measurements_open,
  204. .read = seq_read,
  205. .llseek = seq_lseek,
  206. .release = seq_release,
  207. };
  208. static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  209. size_t datalen, loff_t *ppos)
  210. {
  211. char *data = NULL;
  212. ssize_t result;
  213. if (datalen >= PAGE_SIZE)
  214. datalen = PAGE_SIZE - 1;
  215. /* No partial writes. */
  216. result = -EINVAL;
  217. if (*ppos != 0)
  218. goto out;
  219. result = -ENOMEM;
  220. data = kmalloc(datalen + 1, GFP_KERNEL);
  221. if (!data)
  222. goto out;
  223. *(data + datalen) = '\0';
  224. result = -EFAULT;
  225. if (copy_from_user(data, buf, datalen))
  226. goto out;
  227. result = ima_parse_add_rule(data);
  228. out:
  229. if (result < 0)
  230. valid_policy = 0;
  231. kfree(data);
  232. return result;
  233. }
  234. static struct dentry *ima_dir;
  235. static struct dentry *binary_runtime_measurements;
  236. static struct dentry *ascii_runtime_measurements;
  237. static struct dentry *runtime_measurements_count;
  238. static struct dentry *violations;
  239. static struct dentry *ima_policy;
  240. static atomic_t policy_opencount = ATOMIC_INIT(1);
  241. /*
  242. * ima_open_policy: sequentialize access to the policy file
  243. */
  244. static int ima_open_policy(struct inode * inode, struct file * filp)
  245. {
  246. /* No point in being allowed to open it if you aren't going to write */
  247. if (!(filp->f_flags & O_WRONLY))
  248. return -EACCES;
  249. if (atomic_dec_and_test(&policy_opencount))
  250. return 0;
  251. return -EBUSY;
  252. }
  253. /*
  254. * ima_release_policy - start using the new measure policy rules.
  255. *
  256. * Initially, ima_measure points to the default policy rules, now
  257. * point to the new policy rules, and remove the securityfs policy file,
  258. * assuming a valid policy.
  259. */
  260. static int ima_release_policy(struct inode *inode, struct file *file)
  261. {
  262. if (!valid_policy) {
  263. ima_delete_rules();
  264. valid_policy = 1;
  265. atomic_set(&policy_opencount, 1);
  266. return 0;
  267. }
  268. ima_update_policy();
  269. securityfs_remove(ima_policy);
  270. ima_policy = NULL;
  271. return 0;
  272. }
  273. static const struct file_operations ima_measure_policy_ops = {
  274. .open = ima_open_policy,
  275. .write = ima_write_policy,
  276. .release = ima_release_policy,
  277. .llseek = generic_file_llseek,
  278. };
  279. int __init ima_fs_init(void)
  280. {
  281. ima_dir = securityfs_create_dir("ima", NULL);
  282. if (IS_ERR(ima_dir))
  283. return -1;
  284. binary_runtime_measurements =
  285. securityfs_create_file("binary_runtime_measurements",
  286. S_IRUSR | S_IRGRP, ima_dir, NULL,
  287. &ima_measurements_ops);
  288. if (IS_ERR(binary_runtime_measurements))
  289. goto out;
  290. ascii_runtime_measurements =
  291. securityfs_create_file("ascii_runtime_measurements",
  292. S_IRUSR | S_IRGRP, ima_dir, NULL,
  293. &ima_ascii_measurements_ops);
  294. if (IS_ERR(ascii_runtime_measurements))
  295. goto out;
  296. runtime_measurements_count =
  297. securityfs_create_file("runtime_measurements_count",
  298. S_IRUSR | S_IRGRP, ima_dir, NULL,
  299. &ima_measurements_count_ops);
  300. if (IS_ERR(runtime_measurements_count))
  301. goto out;
  302. violations =
  303. securityfs_create_file("violations", S_IRUSR | S_IRGRP,
  304. ima_dir, NULL, &ima_htable_violations_ops);
  305. if (IS_ERR(violations))
  306. goto out;
  307. ima_policy = securityfs_create_file("policy",
  308. S_IWUSR,
  309. ima_dir, NULL,
  310. &ima_measure_policy_ops);
  311. if (IS_ERR(ima_policy))
  312. goto out;
  313. return 0;
  314. out:
  315. securityfs_remove(violations);
  316. securityfs_remove(runtime_measurements_count);
  317. securityfs_remove(ascii_runtime_measurements);
  318. securityfs_remove(binary_runtime_measurements);
  319. securityfs_remove(ima_dir);
  320. securityfs_remove(ima_policy);
  321. return -1;
  322. }