iint.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, version 2 of the
  10. * License.
  11. *
  12. * File: integrity_iint.c
  13. * - implements the integrity hooks: integrity_inode_alloc,
  14. * integrity_inode_free
  15. * - cache integrity information associated with an inode
  16. * using a rbtree tree.
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/rbtree.h>
  22. #include <linux/file.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/security.h>
  25. #include <linux/lsm_hooks.h>
  26. #include "integrity.h"
  27. static struct rb_root integrity_iint_tree = RB_ROOT;
  28. static DEFINE_RWLOCK(integrity_iint_lock);
  29. static struct kmem_cache *iint_cache __read_mostly;
  30. struct dentry *integrity_dir;
  31. /*
  32. * __integrity_iint_find - return the iint associated with an inode
  33. */
  34. static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
  35. {
  36. struct integrity_iint_cache *iint;
  37. struct rb_node *n = integrity_iint_tree.rb_node;
  38. while (n) {
  39. iint = rb_entry(n, struct integrity_iint_cache, rb_node);
  40. if (inode < iint->inode)
  41. n = n->rb_left;
  42. else if (inode > iint->inode)
  43. n = n->rb_right;
  44. else
  45. break;
  46. }
  47. if (!n)
  48. return NULL;
  49. return iint;
  50. }
  51. /*
  52. * integrity_iint_find - return the iint associated with an inode
  53. */
  54. struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
  55. {
  56. struct integrity_iint_cache *iint;
  57. if (!IS_IMA(inode))
  58. return NULL;
  59. read_lock(&integrity_iint_lock);
  60. iint = __integrity_iint_find(inode);
  61. read_unlock(&integrity_iint_lock);
  62. return iint;
  63. }
  64. static void iint_free(struct integrity_iint_cache *iint)
  65. {
  66. kfree(iint->ima_hash);
  67. iint->ima_hash = NULL;
  68. iint->version = 0;
  69. iint->flags = 0UL;
  70. iint->atomic_flags = 0UL;
  71. iint->ima_file_status = INTEGRITY_UNKNOWN;
  72. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  73. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  74. iint->ima_read_status = INTEGRITY_UNKNOWN;
  75. iint->ima_creds_status = INTEGRITY_UNKNOWN;
  76. iint->evm_status = INTEGRITY_UNKNOWN;
  77. iint->measured_pcrs = 0;
  78. kmem_cache_free(iint_cache, iint);
  79. }
  80. /**
  81. * integrity_inode_get - find or allocate an iint associated with an inode
  82. * @inode: pointer to the inode
  83. * @return: allocated iint
  84. *
  85. * Caller must lock i_mutex
  86. */
  87. struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
  88. {
  89. struct rb_node **p;
  90. struct rb_node *node, *parent = NULL;
  91. struct integrity_iint_cache *iint, *test_iint;
  92. iint = integrity_iint_find(inode);
  93. if (iint)
  94. return iint;
  95. iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
  96. if (!iint)
  97. return NULL;
  98. write_lock(&integrity_iint_lock);
  99. p = &integrity_iint_tree.rb_node;
  100. while (*p) {
  101. parent = *p;
  102. test_iint = rb_entry(parent, struct integrity_iint_cache,
  103. rb_node);
  104. if (inode < test_iint->inode)
  105. p = &(*p)->rb_left;
  106. else
  107. p = &(*p)->rb_right;
  108. }
  109. iint->inode = inode;
  110. node = &iint->rb_node;
  111. inode->i_flags |= S_IMA;
  112. rb_link_node(node, parent, p);
  113. rb_insert_color(node, &integrity_iint_tree);
  114. write_unlock(&integrity_iint_lock);
  115. return iint;
  116. }
  117. /**
  118. * integrity_inode_free - called on security_inode_free
  119. * @inode: pointer to the inode
  120. *
  121. * Free the integrity information(iint) associated with an inode.
  122. */
  123. void integrity_inode_free(struct inode *inode)
  124. {
  125. struct integrity_iint_cache *iint;
  126. if (!IS_IMA(inode))
  127. return;
  128. write_lock(&integrity_iint_lock);
  129. iint = __integrity_iint_find(inode);
  130. rb_erase(&iint->rb_node, &integrity_iint_tree);
  131. write_unlock(&integrity_iint_lock);
  132. iint_free(iint);
  133. }
  134. static void init_once(void *foo)
  135. {
  136. struct integrity_iint_cache *iint = foo;
  137. memset(iint, 0, sizeof(*iint));
  138. iint->ima_file_status = INTEGRITY_UNKNOWN;
  139. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  140. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  141. iint->ima_read_status = INTEGRITY_UNKNOWN;
  142. iint->ima_creds_status = INTEGRITY_UNKNOWN;
  143. iint->evm_status = INTEGRITY_UNKNOWN;
  144. mutex_init(&iint->mutex);
  145. }
  146. static int __init integrity_iintcache_init(void)
  147. {
  148. iint_cache =
  149. kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
  150. 0, SLAB_PANIC, init_once);
  151. return 0;
  152. }
  153. DEFINE_LSM(integrity) = {
  154. .name = "integrity",
  155. .init = integrity_iintcache_init,
  156. };
  157. /*
  158. * integrity_kernel_read - read data from the file
  159. *
  160. * This is a function for reading file content instead of kernel_read().
  161. * It does not perform locking checks to ensure it cannot be blocked.
  162. * It does not perform security checks because it is irrelevant for IMA.
  163. *
  164. */
  165. int integrity_kernel_read(struct file *file, loff_t offset,
  166. void *addr, unsigned long count)
  167. {
  168. mm_segment_t old_fs;
  169. char __user *buf = (char __user *)addr;
  170. ssize_t ret;
  171. if (!(file->f_mode & FMODE_READ))
  172. return -EBADF;
  173. old_fs = get_fs();
  174. set_fs(get_ds());
  175. ret = __vfs_read(file, buf, count, &offset);
  176. set_fs(old_fs);
  177. return ret;
  178. }
  179. /*
  180. * integrity_load_keys - load integrity keys hook
  181. *
  182. * Hooks is called from init/main.c:kernel_init_freeable()
  183. * when rootfs is ready
  184. */
  185. void __init integrity_load_keys(void)
  186. {
  187. ima_load_x509();
  188. evm_load_x509();
  189. }
  190. static int __init integrity_fs_init(void)
  191. {
  192. integrity_dir = securityfs_create_dir("integrity", NULL);
  193. if (IS_ERR(integrity_dir)) {
  194. int ret = PTR_ERR(integrity_dir);
  195. if (ret != -ENODEV)
  196. pr_err("Unable to create integrity sysfs dir: %d\n",
  197. ret);
  198. integrity_dir = NULL;
  199. return ret;
  200. }
  201. return 0;
  202. }
  203. late_initcall(integrity_fs_init)