ima_fs.c 9.2 KB

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