ima_fs.c 7.2 KB

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