ima_fs.c 9.2 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/module.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/rculist.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/parser.h>
  24. #include "ima.h"
  25. static int valid_policy = 1;
  26. #define TMPBUFLEN 12
  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[TMPBUFLEN];
  31. ssize_t len;
  32. len = scnprintf(tmpbuf, TMPBUFLEN, "%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. };
  44. static ssize_t ima_show_measurements_count(struct file *filp,
  45. char __user *buf,
  46. size_t count, loff_t *ppos)
  47. {
  48. return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
  49. }
  50. static const struct file_operations ima_measurements_count_ops = {
  51. .read = ima_show_measurements_count
  52. };
  53. /* returns pointer to hlist_node */
  54. static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
  55. {
  56. loff_t l = *pos;
  57. struct ima_queue_entry *qe;
  58. /* we need a lock since pos could point beyond last element */
  59. rcu_read_lock();
  60. list_for_each_entry_rcu(qe, &ima_measurements, later) {
  61. if (!l--) {
  62. rcu_read_unlock();
  63. return qe;
  64. }
  65. }
  66. rcu_read_unlock();
  67. return NULL;
  68. }
  69. static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
  70. {
  71. struct ima_queue_entry *qe = v;
  72. /* lock protects when reading beyond last element
  73. * against concurrent list-extension
  74. */
  75. rcu_read_lock();
  76. qe = list_entry_rcu(qe->later.next,
  77. struct ima_queue_entry, later);
  78. rcu_read_unlock();
  79. (*pos)++;
  80. return (&qe->later == &ima_measurements) ? NULL : qe;
  81. }
  82. static void ima_measurements_stop(struct seq_file *m, void *v)
  83. {
  84. }
  85. static void ima_putc(struct seq_file *m, void *data, int datalen)
  86. {
  87. while (datalen--)
  88. seq_putc(m, *(char *)data++);
  89. }
  90. /* print format:
  91. * 32bit-le=pcr#
  92. * char[20]=template digest
  93. * 32bit-le=template name size
  94. * char[n]=template name
  95. * eventdata[n]=template specific data
  96. */
  97. static int ima_measurements_show(struct seq_file *m, void *v)
  98. {
  99. /* the list never shrinks, so we don't need a lock here */
  100. struct ima_queue_entry *qe = v;
  101. struct ima_template_entry *e;
  102. int namelen;
  103. u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  104. /* get entry */
  105. e = qe->entry;
  106. if (e == NULL)
  107. return -1;
  108. /*
  109. * 1st: PCRIndex
  110. * PCR used is always the same (config option) in
  111. * little-endian format
  112. */
  113. ima_putc(m, &pcr, sizeof pcr);
  114. /* 2nd: template digest */
  115. ima_putc(m, e->digest, IMA_DIGEST_SIZE);
  116. /* 3rd: template name size */
  117. namelen = strlen(e->template_name);
  118. ima_putc(m, &namelen, sizeof namelen);
  119. /* 4th: template name */
  120. ima_putc(m, (void *)e->template_name, namelen);
  121. /* 5th: template specific data */
  122. ima_template_show(m, (struct ima_template_data *)&e->template,
  123. IMA_SHOW_BINARY);
  124. return 0;
  125. }
  126. static const struct seq_operations ima_measurments_seqops = {
  127. .start = ima_measurements_start,
  128. .next = ima_measurements_next,
  129. .stop = ima_measurements_stop,
  130. .show = ima_measurements_show
  131. };
  132. static int ima_measurements_open(struct inode *inode, struct file *file)
  133. {
  134. return seq_open(file, &ima_measurments_seqops);
  135. }
  136. static const struct file_operations ima_measurements_ops = {
  137. .open = ima_measurements_open,
  138. .read = seq_read,
  139. .llseek = seq_lseek,
  140. .release = seq_release,
  141. };
  142. static void ima_print_digest(struct seq_file *m, u8 *digest)
  143. {
  144. int i;
  145. for (i = 0; i < IMA_DIGEST_SIZE; i++)
  146. seq_printf(m, "%02x", *(digest + i));
  147. }
  148. void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show)
  149. {
  150. struct ima_template_data *entry = e;
  151. int namelen;
  152. switch (show) {
  153. case IMA_SHOW_ASCII:
  154. ima_print_digest(m, entry->digest);
  155. seq_printf(m, " %s\n", entry->file_name);
  156. break;
  157. case IMA_SHOW_BINARY:
  158. ima_putc(m, entry->digest, IMA_DIGEST_SIZE);
  159. namelen = strlen(entry->file_name);
  160. ima_putc(m, &namelen, sizeof namelen);
  161. ima_putc(m, entry->file_name, namelen);
  162. default:
  163. break;
  164. }
  165. }
  166. /* print in ascii */
  167. static int ima_ascii_measurements_show(struct seq_file *m, void *v)
  168. {
  169. /* the list never shrinks, so we don't need a lock here */
  170. struct ima_queue_entry *qe = v;
  171. struct ima_template_entry *e;
  172. /* get entry */
  173. e = qe->entry;
  174. if (e == NULL)
  175. return -1;
  176. /* 1st: PCR used (config option) */
  177. seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
  178. /* 2nd: SHA1 template hash */
  179. ima_print_digest(m, e->digest);
  180. /* 3th: template name */
  181. seq_printf(m, " %s ", e->template_name);
  182. /* 4th: template specific data */
  183. ima_template_show(m, (struct ima_template_data *)&e->template,
  184. IMA_SHOW_ASCII);
  185. return 0;
  186. }
  187. static const struct seq_operations ima_ascii_measurements_seqops = {
  188. .start = ima_measurements_start,
  189. .next = ima_measurements_next,
  190. .stop = ima_measurements_stop,
  191. .show = ima_ascii_measurements_show
  192. };
  193. static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
  194. {
  195. return seq_open(file, &ima_ascii_measurements_seqops);
  196. }
  197. static const struct file_operations ima_ascii_measurements_ops = {
  198. .open = ima_ascii_measurements_open,
  199. .read = seq_read,
  200. .llseek = seq_lseek,
  201. .release = seq_release,
  202. };
  203. static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  204. size_t datalen, loff_t *ppos)
  205. {
  206. char *data;
  207. int rc;
  208. if (datalen >= PAGE_SIZE)
  209. return -ENOMEM;
  210. if (*ppos != 0) {
  211. /* No partial writes. */
  212. return -EINVAL;
  213. }
  214. data = kmalloc(datalen + 1, GFP_KERNEL);
  215. if (!data)
  216. return -ENOMEM;
  217. if (copy_from_user(data, buf, datalen)) {
  218. kfree(data);
  219. return -EFAULT;
  220. }
  221. *(data + datalen) = '\0';
  222. rc = ima_parse_add_rule(data);
  223. if (rc < 0) {
  224. datalen = -EINVAL;
  225. valid_policy = 0;
  226. }
  227. kfree(data);
  228. return datalen;
  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. }