ima_fs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Kylene Hall <kjhall@us.ibm.com>
  7. * Reiner Sailer <sailer@us.ibm.com>
  8. * Mimi Zohar <zohar@us.ibm.com>
  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[n]=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. enum hash_algo algo;
  116. int i, algo_idx;
  117. algo_idx = ima_sha1_idx;
  118. algo = HASH_ALGO_SHA1;
  119. if (m->file != NULL) {
  120. algo_idx = (unsigned long)file_inode(m->file)->i_private;
  121. algo = ima_algo_array[algo_idx].algo;
  122. }
  123. /* get entry */
  124. e = qe->entry;
  125. if (e == NULL)
  126. return -1;
  127. template_name = (e->template_desc->name[0] != '\0') ?
  128. e->template_desc->name : e->template_desc->fmt;
  129. /*
  130. * 1st: PCRIndex
  131. * PCR used defaults to the same (config option) in
  132. * little-endian format, unless set in policy
  133. */
  134. pcr = !ima_canonical_fmt ? e->pcr : (__force u32)cpu_to_le32(e->pcr);
  135. ima_putc(m, &pcr, sizeof(e->pcr));
  136. /* 2nd: template digest */
  137. ima_putc(m, e->digests[algo_idx].digest, hash_digest_size[algo]);
  138. /* 3rd: template name size */
  139. namelen = !ima_canonical_fmt ? strlen(template_name) :
  140. (__force u32)cpu_to_le32(strlen(template_name));
  141. ima_putc(m, &namelen, sizeof(namelen));
  142. /* 4th: template name */
  143. ima_putc(m, template_name, strlen(template_name));
  144. /* 5th: template length (except for 'ima' template) */
  145. if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0)
  146. is_ima_template = true;
  147. if (!is_ima_template) {
  148. template_data_len = !ima_canonical_fmt ? e->template_data_len :
  149. (__force u32)cpu_to_le32(e->template_data_len);
  150. ima_putc(m, &template_data_len, sizeof(e->template_data_len));
  151. }
  152. /* 6th: template specific data */
  153. for (i = 0; i < e->template_desc->num_fields; i++) {
  154. enum ima_show_type show = IMA_SHOW_BINARY;
  155. const struct ima_template_field *field =
  156. e->template_desc->fields[i];
  157. if (is_ima_template && strcmp(field->field_id, "d") == 0)
  158. show = IMA_SHOW_BINARY_NO_FIELD_LEN;
  159. if (is_ima_template && strcmp(field->field_id, "n") == 0)
  160. show = IMA_SHOW_BINARY_OLD_STRING_FMT;
  161. field->field_show(m, show, &e->template_data[i]);
  162. }
  163. return 0;
  164. }
  165. static const struct seq_operations ima_measurments_seqops = {
  166. .start = ima_measurements_start,
  167. .next = ima_measurements_next,
  168. .stop = ima_measurements_stop,
  169. .show = ima_measurements_show
  170. };
  171. static int ima_measurements_open(struct inode *inode, struct file *file)
  172. {
  173. return seq_open(file, &ima_measurments_seqops);
  174. }
  175. static const struct file_operations ima_measurements_ops = {
  176. .open = ima_measurements_open,
  177. .read = seq_read,
  178. .llseek = seq_lseek,
  179. .release = seq_release,
  180. };
  181. void ima_print_digest(struct seq_file *m, u8 *digest, u32 size)
  182. {
  183. u32 i;
  184. for (i = 0; i < size; i++)
  185. seq_printf(m, "%02x", *(digest + i));
  186. }
  187. /* print in ascii */
  188. static int ima_ascii_measurements_show(struct seq_file *m, void *v)
  189. {
  190. /* the list never shrinks, so we don't need a lock here */
  191. struct ima_queue_entry *qe = v;
  192. struct ima_template_entry *e;
  193. char *template_name;
  194. enum hash_algo algo;
  195. int i, algo_idx;
  196. algo_idx = ima_sha1_idx;
  197. algo = HASH_ALGO_SHA1;
  198. if (m->file != NULL) {
  199. algo_idx = (unsigned long)file_inode(m->file)->i_private;
  200. algo = ima_algo_array[algo_idx].algo;
  201. }
  202. /* get entry */
  203. e = qe->entry;
  204. if (e == NULL)
  205. return -1;
  206. template_name = (e->template_desc->name[0] != '\0') ?
  207. e->template_desc->name : e->template_desc->fmt;
  208. /* 1st: PCR used (config option) */
  209. seq_printf(m, "%2d ", e->pcr);
  210. /* 2nd: template hash */
  211. ima_print_digest(m, e->digests[algo_idx].digest, hash_digest_size[algo]);
  212. /* 3th: template name */
  213. seq_printf(m, " %s", template_name);
  214. /* 4th: template specific data */
  215. for (i = 0; i < e->template_desc->num_fields; i++) {
  216. seq_puts(m, " ");
  217. if (e->template_data[i].len == 0)
  218. continue;
  219. e->template_desc->fields[i]->field_show(m, IMA_SHOW_ASCII,
  220. &e->template_data[i]);
  221. }
  222. seq_puts(m, "\n");
  223. return 0;
  224. }
  225. static const struct seq_operations ima_ascii_measurements_seqops = {
  226. .start = ima_measurements_start,
  227. .next = ima_measurements_next,
  228. .stop = ima_measurements_stop,
  229. .show = ima_ascii_measurements_show
  230. };
  231. static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
  232. {
  233. return seq_open(file, &ima_ascii_measurements_seqops);
  234. }
  235. static const struct file_operations ima_ascii_measurements_ops = {
  236. .open = ima_ascii_measurements_open,
  237. .read = seq_read,
  238. .llseek = seq_lseek,
  239. .release = seq_release,
  240. };
  241. static ssize_t ima_read_policy(char *path)
  242. {
  243. void *data = NULL;
  244. char *datap;
  245. size_t size;
  246. int rc, pathlen = strlen(path);
  247. char *p;
  248. /* remove \n */
  249. datap = path;
  250. strsep(&datap, "\n");
  251. rc = kernel_read_file_from_path(path, 0, &data, INT_MAX, NULL,
  252. READING_POLICY);
  253. if (rc < 0) {
  254. pr_err("Unable to open file: %s (%d)", path, rc);
  255. return rc;
  256. }
  257. size = rc;
  258. rc = 0;
  259. datap = data;
  260. while (size > 0 && (p = strsep(&datap, "\n"))) {
  261. pr_debug("rule: %s\n", p);
  262. rc = ima_parse_add_rule(p);
  263. if (rc < 0)
  264. break;
  265. size -= rc;
  266. }
  267. vfree(data);
  268. if (rc < 0)
  269. return rc;
  270. else if (size)
  271. return -EINVAL;
  272. else
  273. return pathlen;
  274. }
  275. static ssize_t ima_write_policy(struct file *file, const char __user *buf,
  276. size_t datalen, loff_t *ppos)
  277. {
  278. char *data;
  279. ssize_t result;
  280. if (datalen >= PAGE_SIZE)
  281. datalen = PAGE_SIZE - 1;
  282. /* No partial writes. */
  283. result = -EINVAL;
  284. if (*ppos != 0)
  285. goto out;
  286. data = memdup_user_nul(buf, datalen);
  287. if (IS_ERR(data)) {
  288. result = PTR_ERR(data);
  289. goto out;
  290. }
  291. result = mutex_lock_interruptible(&ima_write_mutex);
  292. if (result < 0)
  293. goto out_free;
  294. if (data[0] == '/') {
  295. result = ima_read_policy(data);
  296. } else if (ima_appraise & IMA_APPRAISE_POLICY) {
  297. pr_err("signed policy file (specified as an absolute pathname) required\n");
  298. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
  299. "policy_update", "signed policy required",
  300. 1, 0);
  301. result = -EACCES;
  302. } else {
  303. result = ima_parse_add_rule(data);
  304. }
  305. mutex_unlock(&ima_write_mutex);
  306. out_free:
  307. kfree(data);
  308. out:
  309. if (result < 0)
  310. valid_policy = 0;
  311. return result;
  312. }
  313. static struct dentry *ima_dir;
  314. static struct dentry *ima_symlink;
  315. enum ima_fs_flags {
  316. IMA_FS_BUSY,
  317. };
  318. static unsigned long ima_fs_flags;
  319. #ifdef CONFIG_IMA_READ_POLICY
  320. static const struct seq_operations ima_policy_seqops = {
  321. .start = ima_policy_start,
  322. .next = ima_policy_next,
  323. .stop = ima_policy_stop,
  324. .show = ima_policy_show,
  325. };
  326. #endif
  327. static int __init create_securityfs_measurement_lists(void)
  328. {
  329. int count = NR_BANKS(ima_tpm_chip);
  330. if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip))
  331. count++;
  332. for (int i = 0; i < count; i++) {
  333. u16 algo = ima_algo_array[i].algo;
  334. char file_name[NAME_MAX + 1];
  335. struct dentry *dentry;
  336. sprintf(file_name, "ascii_runtime_measurements_%s",
  337. hash_algo_name[algo]);
  338. dentry = securityfs_create_file(file_name, S_IRUSR | S_IRGRP,
  339. ima_dir, (void *)(uintptr_t)i,
  340. &ima_ascii_measurements_ops);
  341. if (IS_ERR(dentry))
  342. return PTR_ERR(dentry);
  343. sprintf(file_name, "binary_runtime_measurements_%s",
  344. hash_algo_name[algo]);
  345. dentry = securityfs_create_file(file_name, S_IRUSR | S_IRGRP,
  346. ima_dir, (void *)(uintptr_t)i,
  347. &ima_measurements_ops);
  348. if (IS_ERR(dentry))
  349. return PTR_ERR(dentry);
  350. }
  351. return 0;
  352. }
  353. /*
  354. * ima_open_policy: sequentialize access to the policy file
  355. */
  356. static int ima_open_policy(struct inode *inode, struct file *filp)
  357. {
  358. if (!(filp->f_flags & O_WRONLY)) {
  359. #ifndef CONFIG_IMA_READ_POLICY
  360. return -EACCES;
  361. #else
  362. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  363. return -EACCES;
  364. if (!capable(CAP_SYS_ADMIN))
  365. return -EPERM;
  366. return seq_open(filp, &ima_policy_seqops);
  367. #endif
  368. }
  369. if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags))
  370. return -EBUSY;
  371. return 0;
  372. }
  373. /*
  374. * ima_release_policy - start using the new measure policy rules.
  375. *
  376. * Initially, ima_measure points to the default policy rules, now
  377. * point to the new policy rules, and remove the securityfs policy file,
  378. * assuming a valid policy.
  379. */
  380. static int ima_release_policy(struct inode *inode, struct file *file)
  381. {
  382. const char *cause = valid_policy ? "completed" : "failed";
  383. if ((file->f_flags & O_ACCMODE) == O_RDONLY)
  384. return seq_release(inode, file);
  385. if (valid_policy && ima_check_policy() < 0) {
  386. cause = "failed";
  387. valid_policy = 0;
  388. }
  389. pr_info("policy update %s\n", cause);
  390. integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
  391. "policy_update", cause, !valid_policy, 0);
  392. if (!valid_policy) {
  393. ima_delete_rules();
  394. valid_policy = 1;
  395. clear_bit(IMA_FS_BUSY, &ima_fs_flags);
  396. return 0;
  397. }
  398. ima_update_policy();
  399. #if !defined(CONFIG_IMA_WRITE_POLICY) && !defined(CONFIG_IMA_READ_POLICY)
  400. securityfs_remove(file->f_path.dentry);
  401. #elif defined(CONFIG_IMA_WRITE_POLICY)
  402. clear_bit(IMA_FS_BUSY, &ima_fs_flags);
  403. #elif defined(CONFIG_IMA_READ_POLICY)
  404. inode->i_mode &= ~S_IWUSR;
  405. #endif
  406. return 0;
  407. }
  408. static const struct file_operations ima_measure_policy_ops = {
  409. .open = ima_open_policy,
  410. .write = ima_write_policy,
  411. .read = seq_read,
  412. .release = ima_release_policy,
  413. .llseek = generic_file_llseek,
  414. };
  415. int __init ima_fs_init(void)
  416. {
  417. struct dentry *dentry;
  418. int ret;
  419. ret = integrity_fs_init();
  420. if (ret < 0)
  421. return ret;
  422. ima_dir = securityfs_create_dir("ima", integrity_dir);
  423. if (IS_ERR(ima_dir)) {
  424. ret = PTR_ERR(ima_dir);
  425. goto out;
  426. }
  427. ima_symlink = securityfs_create_symlink("ima", NULL, "integrity/ima",
  428. NULL);
  429. if (IS_ERR(ima_symlink)) {
  430. ret = PTR_ERR(ima_symlink);
  431. goto out;
  432. }
  433. ret = create_securityfs_measurement_lists();
  434. if (ret != 0)
  435. goto out;
  436. dentry = securityfs_create_symlink("binary_runtime_measurements", ima_dir,
  437. "binary_runtime_measurements_sha1", NULL);
  438. if (IS_ERR(dentry)) {
  439. ret = PTR_ERR(dentry);
  440. goto out;
  441. }
  442. dentry = securityfs_create_symlink("ascii_runtime_measurements", ima_dir,
  443. "ascii_runtime_measurements_sha1", NULL);
  444. if (IS_ERR(dentry)) {
  445. ret = PTR_ERR(dentry);
  446. goto out;
  447. }
  448. dentry = securityfs_create_file("runtime_measurements_count",
  449. S_IRUSR | S_IRGRP, ima_dir, NULL,
  450. &ima_measurements_count_ops);
  451. if (IS_ERR(dentry)) {
  452. ret = PTR_ERR(dentry);
  453. goto out;
  454. }
  455. dentry = securityfs_create_file("violations", S_IRUSR | S_IRGRP,
  456. ima_dir, NULL, &ima_htable_violations_ops);
  457. if (IS_ERR(dentry)) {
  458. ret = PTR_ERR(dentry);
  459. goto out;
  460. }
  461. dentry = securityfs_create_file("policy", POLICY_FILE_FLAGS,
  462. ima_dir, NULL,
  463. &ima_measure_policy_ops);
  464. if (IS_ERR(dentry)) {
  465. ret = PTR_ERR(dentry);
  466. goto out;
  467. }
  468. return 0;
  469. out:
  470. securityfs_remove(ima_symlink);
  471. securityfs_remove(ima_dir);
  472. integrity_fs_fini();
  473. return ret;
  474. }