ima_fs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <linux/vmalloc.h>
  26. #include "ima.h"
  27. static DEFINE_MUTEX(ima_write_mutex);
  28. static int valid_policy = 1;
  29. #define TMPBUFLEN 12
  30. static ssize_t ima_show_htable_value(char __user *buf, size_t count,
  31. loff_t *ppos, atomic_long_t *val)
  32. {
  33. char tmpbuf[TMPBUFLEN];
  34. ssize_t len;
  35. len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
  36. return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
  37. }
  38. static ssize_t ima_show_htable_violations(struct file *filp,
  39. char __user *buf,
  40. size_t count, loff_t *ppos)
  41. {
  42. return ima_show_htable_value(buf, count, ppos, &ima_htable.violations);
  43. }
  44. static const struct file_operations ima_htable_violations_ops = {
  45. .read = ima_show_htable_violations,
  46. .llseek = generic_file_llseek,
  47. };
  48. static ssize_t ima_show_measurements_count(struct file *filp,
  49. char __user *buf,
  50. size_t count, loff_t *ppos)
  51. {
  52. return ima_show_htable_value(buf, count, ppos, &ima_htable.len);
  53. }
  54. static const struct file_operations ima_measurements_count_ops = {
  55. .read = ima_show_measurements_count,
  56. .llseek = generic_file_llseek,
  57. };
  58. /* returns pointer to hlist_node */
  59. static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
  60. {
  61. loff_t l = *pos;
  62. struct ima_queue_entry *qe;
  63. /* we need a lock since pos could point beyond last element */
  64. rcu_read_lock();
  65. list_for_each_entry_rcu(qe, &ima_measurements, later) {
  66. if (!l--) {
  67. rcu_read_unlock();
  68. return qe;
  69. }
  70. }
  71. rcu_read_unlock();
  72. return NULL;
  73. }
  74. static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
  75. {
  76. struct ima_queue_entry *qe = v;
  77. /* lock protects when reading beyond last element
  78. * against concurrent list-extension
  79. */
  80. rcu_read_lock();
  81. qe = list_entry_rcu(qe->later.next, struct ima_queue_entry, later);
  82. rcu_read_unlock();
  83. (*pos)++;
  84. return (&qe->later == &ima_measurements) ? NULL : qe;
  85. }
  86. static void ima_measurements_stop(struct seq_file *m, void *v)
  87. {
  88. }
  89. void ima_putc(struct seq_file *m, void *data, int datalen)
  90. {
  91. while (datalen--)
  92. seq_putc(m, *(char *)data++);
  93. }
  94. /* print format:
  95. * 32bit-le=pcr#
  96. * char[20]=template digest
  97. * 32bit-le=template name size
  98. * char[n]=template name
  99. * [eventdata length]
  100. * eventdata[n]=template specific data
  101. */
  102. static int ima_measurements_show(struct seq_file *m, void *v)
  103. {
  104. /* the list never shrinks, so we don't need a lock here */
  105. struct ima_queue_entry *qe = v;
  106. struct ima_template_entry *e;
  107. char *template_name;
  108. int namelen;
  109. u32 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
  110. bool is_ima_template = false;
  111. int i;
  112. /* get entry */
  113. e = qe->entry;
  114. if (e == NULL)
  115. return -1;
  116. template_name = (e->template_desc->name[0] != '\0') ?
  117. e->template_desc->name : e->template_desc->fmt;
  118. /*
  119. * 1st: PCRIndex
  120. * PCR used is always the same (config option) in
  121. * little-endian format
  122. */
  123. ima_putc(m, &pcr, sizeof(pcr));
  124. /* 2nd: template digest */
  125. ima_putc(m, e->digest, TPM_DIGEST_SIZE);
  126. /* 3rd: template name size */
  127. namelen = strlen(template_name);
  128. ima_putc(m, &namelen, sizeof(namelen));
  129. /* 4th: template name */
  130. ima_putc(m, template_name, namelen);
  131. /* 5th: template length (except for 'ima' template) */
  132. if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
  133. is_ima_template = true;
  134. if (!is_ima_template)
  135. ima_putc(m, &e->template_data_len,
  136. sizeof(e->template_data_len));
  137. /* 6th: template specific data */
  138. for (i = 0; i < e->template_desc->num_fields; i++) {
  139. enum ima_show_type show = IMA_SHOW_BINARY;
  140. struct ima_template_field *field = e->template_desc->fields[i];
  141. if (is_ima_template && strcmp(field->field_id, "d") == 0)
  142. show = IMA_SHOW_BINARY_NO_FIELD_LEN;
  143. if (is_ima_template && strcmp(field->field_id, "n") == 0)
  144. show = IMA_SHOW_BINARY_OLD_STRING_FMT;
  145. field->field_show(m, show, &e->template_data[i]);
  146. }
  147. return 0;
  148. }
  149. static const struct seq_operations ima_measurments_seqops = {
  150. .start = ima_measurements_start,
  151. .next = ima_measurements_next,
  152. .stop = ima_measurements_stop,
  153. .show = ima_measurements_show
  154. };
  155. static int ima_measurements_open(struct inode *inode, struct file *file)
  156. {
  157. return seq_open(file, &ima_measurments_seqops);
  158. }
  159. static const struct file_operations ima_measurements_ops = {
  160. .open = ima_measurements_open,
  161. .read = seq_read,
  162. .llseek = seq_lseek,
  163. .release = seq_release,
  164. };
  165. void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
  166. {
  167. u32 i;
  168. for (i = 0; i < size; i++)
  169. seq_printf(m, "%02x", *(digest + i));
  170. }
  171. /* print in ascii */
  172. static int ima_ascii_measurements_show(struct seq_file *m, void *v)
  173. {
  174. /* the list never shrinks, so we don't need a lock here */
  175. struct ima_queue_entry *qe = v;
  176. struct ima_template_entry *e;
  177. char *template_name;
  178. int i;
  179. /* get entry */
  180. e = qe->entry;
  181. if (e == NULL)
  182. return -1;
  183. template_name = (e->template_desc->name[0] != '\0') ?
  184. e->template_desc->name : e->template_desc->fmt;
  185. /* 1st: PCR used (config option) */
  186. seq_printf(m, "%2d ", CONFIG_IMA_MEASURE_PCR_IDX);
  187. /* 2nd: SHA1 template hash */
  188. ima_print_digest(m, e->digest, TPM_DIGEST_SIZE);
  189. /* 3th: template name */
  190. seq_printf(m, " %s", template_name);
  191. /* 4th: template specific data */
  192. for (i = 0; i < e->template_desc->num_fields; i++) {
  193. seq_puts(m, " ");
  194. if (e->template_data[i].len == 0)
  195. continue;
  196. e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
  197. &e->template_data[i]);
  198. }
  199. seq_puts(m, "\n");
  200. return 0;
  201. }
  202. static const struct seq_operations ima_ascii_measurements_seqops = {
  203. .start = ima_measurements_start,
  204. .next = ima_measurements_next,
  205. .stop = ima_measurements_stop,
  206. .show = ima_ascii_measurements_show
  207. };
  208. static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
  209. {
  210. return seq_open(file, &ima_ascii_measurements_seqops);
  211. }
  212. static const struct file_operations ima_ascii_measurements_ops = {
  213. .open = ima_ascii_measurements_open,
  214. .read = seq_read,
  215. .llseek = seq_lseek,
  216. .release = seq_release,
  217. };
  218. static ssize_t ima_read_policy(char *path)
  219. {
  220. void *data;
  221. char *datap;
  222. loff_t size;
  223. int rc, pathlen = strlen(path);
  224. char *p;
  225. /* remove \n */
  226. datap = path;
  227. strsep(&datap, "\n");
  228. rc = kernel_read_file_from_path(path, &data, &size, 0, READING_POLICY);
  229. if (rc < 0) {
  230. pr_err("Unable to open file: %s (%d)", path, rc);
  231. return rc;
  232. }
  233. datap = data;
  234. while (size > 0 && (p = strsep(&datap, "\n"))) {
  235. pr_debug("rule: %s\n", p);
  236. rc = ima_parse_add_rule(p);
  237. if (rc < 0)
  238. break;
  239. size -= rc;
  240. }
  241. vfree(data);
  242. if (rc < 0)
  243. return rc;
  244. else if (size)
  245. return -EINVAL;
  246. else
  247. return pathlen;
  248. }
  249. static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  250. size_t datalen, loff_t *ppos)
  251. {
  252. char *data;
  253. ssize_t result;
  254. if (datalen >= PAGE_SIZE)
  255. datalen = PAGE_SIZE - 1;
  256. /* No partial writes. */
  257. result = -EINVAL;
  258. if (*ppos != 0)
  259. goto out;
  260. result = -ENOMEM;
  261. data = kmalloc(datalen + 1, GFP_KERNEL);
  262. if (!data)
  263. goto out;
  264. *(data + datalen) = '\0';
  265. result = -EFAULT;
  266. if (copy_from_user(data, buf, datalen))
  267. goto out_free;
  268. result = mutex_lock_interruptible(&ima_write_mutex);
  269. if (result < 0)
  270. goto out_free;
  271. if (data[0] == '/') {
  272. result = ima_read_policy(data);
  273. } else if (ima_appraise & IMA_APPRAISE_POLICY) {
  274. pr_err("IMA: signed policy file (specified as an absolute pathname) required\n");
  275. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
  276. "policy_update", "signed policy required",
  277. 1, 0);
  278. if (ima_appraise & IMA_APPRAISE_ENFORCE)
  279. result = -EACCES;
  280. } else {
  281. result = ima_parse_add_rule(data);
  282. }
  283. mutex_unlock(&ima_write_mutex);
  284. out_free:
  285. kfree(data);
  286. out:
  287. if (result < 0)
  288. valid_policy = 0;
  289. return result;
  290. }
  291. static struct dentry *ima_dir;
  292. static struct dentry *binary_runtime_measurements;
  293. static struct dentry *ascii_runtime_measurements;
  294. static struct dentry *runtime_measurements_count;
  295. static struct dentry *violations;
  296. static struct dentry *ima_policy;
  297. enum ima_fs_flags {
  298. IMA_FS_BUSY,
  299. };
  300. static unsigned long ima_fs_flags;
  301. #ifdef CONFIG_IMA_READ_POLICY
  302. static const struct seq_operations ima_policy_seqops = {
  303. .start = ima_policy_start,
  304. .next = ima_policy_next,
  305. .stop = ima_policy_stop,
  306. .show = ima_policy_show,
  307. };
  308. #endif
  309. /*
  310. * ima_open_policy: sequentialize access to the policy file
  311. */
  312. static int ima_open_policy(struct inode *inode, struct file *filp)
  313. {
  314. if (!(filp->f_flags & O_WRONLY)) {
  315. #ifndef CONFIG_IMA_READ_POLICY
  316. return -EACCES;
  317. #else
  318. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  319. return -EACCES;
  320. if (!capable(CAP_SYS_ADMIN))
  321. return -EPERM;
  322. return seq_open(filp, &ima_policy_seqops);
  323. #endif
  324. }
  325. if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
  326. return -EBUSY;
  327. return 0;
  328. }
  329. /*
  330. * ima_release_policy - start using the new measure policy rules.
  331. *
  332. * Initially, ima_measure points to the default policy rules, now
  333. * point to the new policy rules, and remove the securityfs policy file,
  334. * assuming a valid policy.
  335. */
  336. static int ima_release_policy(struct inode *inode, struct file *file)
  337. {
  338. const char *cause = valid_policy ? "completed" : "failed";
  339. if ((file->f_flags & O_ACCMODE) == O_RDONLY)
  340. return 0;
  341. if (valid_policy && ima_check_policy() < 0) {
  342. cause = "failed";
  343. valid_policy = 0;
  344. }
  345. pr_info("IMA: policy update %s\n", cause);
  346. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
  347. "policy_update", cause, !valid_policy, 0);
  348. if (!valid_policy) {
  349. ima_delete_rules();
  350. valid_policy = 1;
  351. clear_bit(IMA_FS_BUSY, &ima_fs_flags);
  352. return 0;
  353. }
  354. ima_update_policy();
  355. #ifndef CONFIG_IMA_WRITE_POLICY
  356. securityfs_remove(ima_policy);
  357. ima_policy = NULL;
  358. #else
  359. clear_bit(IMA_FS_BUSY, &ima_fs_flags);
  360. #endif
  361. return 0;
  362. }
  363. static const struct file_operations ima_measure_policy_ops = {
  364. .open = ima_open_policy,
  365. .write = ima_write_policy,
  366. .read = seq_read,
  367. .release = ima_release_policy,
  368. .llseek = generic_file_llseek,
  369. };
  370. int __init ima_fs_init(void)
  371. {
  372. ima_dir = securityfs_create_dir("ima", NULL);
  373. if (IS_ERR(ima_dir))
  374. return -1;
  375. binary_runtime_measurements =
  376. securityfs_create_file("binary_runtime_measurements",
  377. S_IRUSR | S_IRGRP, ima_dir, NULL,
  378. &ima_measurements_ops);
  379. if (IS_ERR(binary_runtime_measurements))
  380. goto out;
  381. ascii_runtime_measurements =
  382. securityfs_create_file("ascii_runtime_measurements",
  383. S_IRUSR | S_IRGRP, ima_dir, NULL,
  384. &ima_ascii_measurements_ops);
  385. if (IS_ERR(ascii_runtime_measurements))
  386. goto out;
  387. runtime_measurements_count =
  388. securityfs_create_file("runtime_measurements_count",
  389. S_IRUSR | S_IRGRP, ima_dir, NULL,
  390. &ima_measurements_count_ops);
  391. if (IS_ERR(runtime_measurements_count))
  392. goto out;
  393. violations =
  394. securityfs_create_file("violations", S_IRUSR | S_IRGRP,
  395. ima_dir, NULL, &ima_htable_violations_ops);
  396. if (IS_ERR(violations))
  397. goto out;
  398. ima_policy = securityfs_create_file("policy", POLICY_FILE_FLAGS,
  399. ima_dir, NULL,
  400. &ima_measure_policy_ops);
  401. if (IS_ERR(ima_policy))
  402. goto out;
  403. return 0;
  404. out:
  405. securityfs_remove(violations);
  406. securityfs_remove(runtime_measurements_count);
  407. securityfs_remove(ascii_runtime_measurements);
  408. securityfs_remove(binary_runtime_measurements);
  409. securityfs_remove(ima_dir);
  410. securityfs_remove(ima_policy);
  411. return -1;
  412. }