ima_fs.c 13 KB

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