ima_fs.c 15 KB

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