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