ima_fs.c 12 KB

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