ima_fs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 = NULL;
  207. ssize_t result;
  208. if (datalen >= PAGE_SIZE)
  209. datalen = PAGE_SIZE - 1;
  210. /* No partial writes. */
  211. result = -EINVAL;
  212. if (*ppos != 0)
  213. goto out;
  214. result = -ENOMEM;
  215. data = kmalloc(datalen + 1, GFP_KERNEL);
  216. if (!data)
  217. goto out;
  218. *(data + datalen) = '\0';
  219. result = -EFAULT;
  220. if (copy_from_user(data, buf, datalen))
  221. goto out;
  222. result = ima_parse_add_rule(data);
  223. out:
  224. if (result < 0)
  225. valid_policy = 0;
  226. kfree(data);
  227. return result;
  228. }
  229. static struct dentry *ima_dir;
  230. static struct dentry *binary_runtime_measurements;
  231. static struct dentry *ascii_runtime_measurements;
  232. static struct dentry *runtime_measurements_count;
  233. static struct dentry *violations;
  234. static struct dentry *ima_policy;
  235. static atomic_t policy_opencount = ATOMIC_INIT(1);
  236. /*
  237. * ima_open_policy: sequentialize access to the policy file
  238. */
  239. int ima_open_policy(struct inode * inode, struct file * filp)
  240. {
  241. /* No point in being allowed to open it if you aren't going to write */
  242. if (!(filp->f_flags & O_WRONLY))
  243. return -EACCES;
  244. if (atomic_dec_and_test(&policy_opencount))
  245. return 0;
  246. return -EBUSY;
  247. }
  248. /*
  249. * ima_release_policy - start using the new measure policy rules.
  250. *
  251. * Initially, ima_measure points to the default policy rules, now
  252. * point to the new policy rules, and remove the securityfs policy file,
  253. * assuming a valid policy.
  254. */
  255. static int ima_release_policy(struct inode *inode, struct file *file)
  256. {
  257. if (!valid_policy) {
  258. ima_delete_rules();
  259. valid_policy = 1;
  260. atomic_set(&policy_opencount, 1);
  261. return 0;
  262. }
  263. ima_update_policy();
  264. securityfs_remove(ima_policy);
  265. ima_policy = NULL;
  266. return 0;
  267. }
  268. static const struct file_operations ima_measure_policy_ops = {
  269. .open = ima_open_policy,
  270. .write = ima_write_policy,
  271. .release = ima_release_policy
  272. };
  273. int __init ima_fs_init(void)
  274. {
  275. ima_dir = securityfs_create_dir("ima", NULL);
  276. if (IS_ERR(ima_dir))
  277. return -1;
  278. binary_runtime_measurements =
  279. securityfs_create_file("binary_runtime_measurements",
  280. S_IRUSR | S_IRGRP, ima_dir, NULL,
  281. &ima_measurements_ops);
  282. if (IS_ERR(binary_runtime_measurements))
  283. goto out;
  284. ascii_runtime_measurements =
  285. securityfs_create_file("ascii_runtime_measurements",
  286. S_IRUSR | S_IRGRP, ima_dir, NULL,
  287. &ima_ascii_measurements_ops);
  288. if (IS_ERR(ascii_runtime_measurements))
  289. goto out;
  290. runtime_measurements_count =
  291. securityfs_create_file("runtime_measurements_count",
  292. S_IRUSR | S_IRGRP, ima_dir, NULL,
  293. &ima_measurements_count_ops);
  294. if (IS_ERR(runtime_measurements_count))
  295. goto out;
  296. violations =
  297. securityfs_create_file("violations", S_IRUSR | S_IRGRP,
  298. ima_dir, NULL, &ima_htable_violations_ops);
  299. if (IS_ERR(violations))
  300. goto out;
  301. ima_policy = securityfs_create_file("policy",
  302. S_IWUSR,
  303. ima_dir, NULL,
  304. &ima_measure_policy_ops);
  305. if (IS_ERR(ima_policy))
  306. goto out;
  307. return 0;
  308. out:
  309. securityfs_remove(runtime_measurements_count);
  310. securityfs_remove(ascii_runtime_measurements);
  311. securityfs_remove(binary_runtime_measurements);
  312. securityfs_remove(ima_dir);
  313. securityfs_remove(ima_policy);
  314. return -1;
  315. }
  316. void __exit ima_fs_cleanup(void)
  317. {
  318. securityfs_remove(violations);
  319. securityfs_remove(runtime_measurements_count);
  320. securityfs_remove(ascii_runtime_measurements);
  321. securityfs_remove(binary_runtime_measurements);
  322. securityfs_remove(ima_dir);
  323. securityfs_remove(ima_policy);
  324. }