ima_fs.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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,
  80. struct ima_queue_entry, later);
  81. rcu_read_unlock();
  82. (*pos)++;
  83. return (&qe->later == &ima_measurements) ? NULL : qe;
  84. }
  85. static void ima_measurements_stop(struct seq_file *m, void *v)
  86. {
  87. }
  88. static void ima_putc(struct seq_file *m, void *data, int datalen)
  89. {
  90. while (datalen--)
  91. seq_putc(m, *(char *)data++);
  92. }
  93. /* print format:
  94. * 32bit-le=pcr#
  95. * char[20]=template digest
  96. * 32bit-le=template name size
  97. * char[n]=template name
  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. /* get entry */
  108. e = qe->entry;
  109. if (e == NULL)
  110. return -1;
  111. /*
  112. * 1st: PCRIndex
  113. * PCR used is always the same (config option) in
  114. * little-endian format
  115. */
  116. ima_putc(m, &pcr, sizeof pcr);
  117. /* 2nd: template digest */
  118. ima_putc(m, e->digest, IMA_DIGEST_SIZE);
  119. /* 3rd: template name size */
  120. namelen = strlen(e->template_name);
  121. ima_putc(m, &namelen, sizeof namelen);
  122. /* 4th: template name */
  123. ima_putc(m, (void *)e->template_name, namelen);
  124. /* 5th: template specific data */
  125. ima_template_show(m, (struct ima_template_data *)&e->template,
  126. IMA_SHOW_BINARY);
  127. return 0;
  128. }
  129. static const struct seq_operations ima_measurments_seqops = {
  130. .start = ima_measurements_start,
  131. .next = ima_measurements_next,
  132. .stop = ima_measurements_stop,
  133. .show = ima_measurements_show
  134. };
  135. static int ima_measurements_open(struct inode *inode, struct file *file)
  136. {
  137. return seq_open(file, &ima_measurments_seqops);
  138. }
  139. static const struct file_operations ima_measurements_ops = {
  140. .open = ima_measurements_open,
  141. .read = seq_read,
  142. .llseek = seq_lseek,
  143. .release = seq_release,
  144. };
  145. static void ima_print_digest(struct seq_file *m, u8 *digest)
  146. {
  147. int i;
  148. for (i = 0; i < IMA_DIGEST_SIZE; i++)
  149. seq_printf(m, "%02x", *(digest + i));
  150. }
  151. void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
  152. {
  153. struct ima_template_data *entry = e;
  154. int namelen;
  155. switch (show) {
  156. case IMA_SHOW_ASCII:
  157. ima_print_digest(m, entry->digest);
  158. seq_printf(m, " %s\n", entry->file_name);
  159. break;
  160. case IMA_SHOW_BINARY:
  161. ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
  162. namelen = strlen(entry->file_name);
  163. ima_putc(m, &namelen, sizeof namelen);
  164. ima_putc(m, entry->file_name, namelen);
  165. default:
  166. break;
  167. }
  168. }
  169. /* print in ascii */
  170. static int ima_ascii_measurements_show(struct seq_file *m, void *v)
  171. {
  172. /* the list never shrinks, so we don't need a lock here */
  173. struct ima_queue_entry *qe = v;
  174. struct ima_template_entry *e;
  175. /* get entry */
  176. e = qe->entry;
  177. if (e == NULL)
  178. return -1;
  179. /* 1st: PCR used (config option) */
  180. seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
  181. /* 2nd: SHA1 template hash */
  182. ima_print_digest(m, e->digest);
  183. /* 3th: template name */
  184. seq_printf(m, " %s ", e->template_name);
  185. /* 4th: template specific data */
  186. ima_template_show(m, (struct ima_template_data *)&e->template,
  187. IMA_SHOW_ASCII);
  188. return 0;
  189. }
  190. static const struct seq_operations ima_ascii_measurements_seqops = {
  191. .start = ima_measurements_start,
  192. .next = ima_measurements_next,
  193. .stop = ima_measurements_stop,
  194. .show = ima_ascii_measurements_show
  195. };
  196. static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
  197. {
  198. return seq_open(file, &ima_ascii_measurements_seqops);
  199. }
  200. static const struct file_operations ima_ascii_measurements_ops = {
  201. .open = ima_ascii_measurements_open,
  202. .read = seq_read,
  203. .llseek = seq_lseek,
  204. .release = seq_release,
  205. };
  206. static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  207. size_t datalen, loff_t *ppos)
  208. {
  209. char *data = NULL;
  210. ssize_t result;
  211. if (datalen >= PAGE_SIZE)
  212. datalen = PAGE_SIZE - 1;
  213. /* No partial writes. */
  214. result = -EINVAL;
  215. if (*ppos != 0)
  216. goto out;
  217. result = -ENOMEM;
  218. data = kmalloc(datalen + 1, GFP_KERNEL);
  219. if (!data)
  220. goto out;
  221. *(data + datalen) = '\0';
  222. result = -EFAULT;
  223. if (copy_from_user(data, buf, datalen))
  224. goto out;
  225. result = ima_parse_add_rule(data);
  226. out:
  227. if (result < 0)
  228. valid_policy = 0;
  229. kfree(data);
  230. return result;
  231. }
  232. static struct dentry *ima_dir;
  233. static struct dentry *binary_runtime_measurements;
  234. static struct dentry *ascii_runtime_measurements;
  235. static struct dentry *runtime_measurements_count;
  236. static struct dentry *violations;
  237. static struct dentry *ima_policy;
  238. static atomic_t policy_opencount = ATOMIC_INIT(1);
  239. /*
  240. * ima_open_policy: sequentialize access to the policy file
  241. */
  242. static int ima_open_policy(struct inode * inode, struct file * filp)
  243. {
  244. /* No point in being allowed to open it if you aren't going to write */
  245. if (!(filp->f_flags & O_WRONLY))
  246. return -EACCES;
  247. if (atomic_dec_and_test(&policy_opencount))
  248. return 0;
  249. return -EBUSY;
  250. }
  251. /*
  252. * ima_release_policy - start using the new measure policy rules.
  253. *
  254. * Initially, ima_measure points to the default policy rules, now
  255. * point to the new policy rules, and remove the securityfs policy file,
  256. * assuming a valid policy.
  257. */
  258. static int ima_release_policy(struct inode *inode, struct file *file)
  259. {
  260. if (!valid_policy) {
  261. ima_delete_rules();
  262. valid_policy = 1;
  263. atomic_set(&policy_opencount, 1);
  264. return 0;
  265. }
  266. ima_update_policy();
  267. securityfs_remove(ima_policy);
  268. ima_policy = NULL;
  269. return 0;
  270. }
  271. static const struct file_operations ima_measure_policy_ops = {
  272. .open = ima_open_policy,
  273. .write = ima_write_policy,
  274. .release = ima_release_policy,
  275. .llseek = generic_file_llseek,
  276. };
  277. int __init ima_fs_init(void)
  278. {
  279. ima_dir = securityfs_create_dir("ima", NULL);
  280. if (IS_ERR(ima_dir))
  281. return -1;
  282. binary_runtime_measurements =
  283. securityfs_create_file("binary_runtime_measurements",
  284. S_IRUSR | S_IRGRP, ima_dir, NULL,
  285. &ima_measurements_ops);
  286. if (IS_ERR(binary_runtime_measurements))
  287. goto out;
  288. ascii_runtime_measurements =
  289. securityfs_create_file("ascii_runtime_measurements",
  290. S_IRUSR | S_IRGRP, ima_dir, NULL,
  291. &ima_ascii_measurements_ops);
  292. if (IS_ERR(ascii_runtime_measurements))
  293. goto out;
  294. runtime_measurements_count =
  295. securityfs_create_file("runtime_measurements_count",
  296. S_IRUSR | S_IRGRP, ima_dir, NULL,
  297. &ima_measurements_count_ops);
  298. if (IS_ERR(runtime_measurements_count))
  299. goto out;
  300. violations =
  301. securityfs_create_file("violations", S_IRUSR | S_IRGRP,
  302. ima_dir, NULL, &ima_htable_violations_ops);
  303. if (IS_ERR(violations))
  304. goto out;
  305. ima_policy = securityfs_create_file("policy",
  306. S_IWUSR,
  307. ima_dir, NULL,
  308. &ima_measure_policy_ops);
  309. if (IS_ERR(ima_policy))
  310. goto out;
  311. return 0;
  312. out:
  313. securityfs_remove(runtime_measurements_count);
  314. securityfs_remove(ascii_runtime_measurements);
  315. securityfs_remove(binary_runtime_measurements);
  316. securityfs_remove(ima_dir);
  317. securityfs_remove(ima_policy);
  318. return -1;
  319. }
  320. void __exit ima_fs_cleanup(void)
  321. {
  322. securityfs_remove(violations);
  323. securityfs_remove(runtime_measurements_count);
  324. securityfs_remove(ascii_runtime_measurements);
  325. securityfs_remove(binary_runtime_measurements);
  326. securityfs_remove(ima_dir);
  327. securityfs_remove(ima_policy);
  328. }