iint.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <[email protected]>
  7. *
  8. * File: integrity_iint.c
  9. * - implements the integrity hooks: integrity_inode_alloc,
  10. * integrity_inode_free
  11. * - cache integrity information associated with an inode
  12. * using a rbtree tree.
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/file.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/security.h>
  21. #include <linux/lsm_hooks.h>
  22. #include "integrity.h"
  23. static struct rb_root integrity_iint_tree = RB_ROOT;
  24. static DEFINE_RWLOCK(integrity_iint_lock);
  25. static struct kmem_cache *iint_cache __read_mostly;
  26. struct dentry *integrity_dir;
  27. /*
  28. * __integrity_iint_find - return the iint associated with an inode
  29. */
  30. static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode)
  31. {
  32. struct integrity_iint_cache *iint;
  33. struct rb_node *n = integrity_iint_tree.rb_node;
  34. while (n) {
  35. iint = rb_entry(n, struct integrity_iint_cache, rb_node);
  36. if (inode < iint->inode)
  37. n = n->rb_left;
  38. else if (inode > iint->inode)
  39. n = n->rb_right;
  40. else
  41. return iint;
  42. }
  43. return NULL;
  44. }
  45. /*
  46. * integrity_iint_find - return the iint associated with an inode
  47. */
  48. struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
  49. {
  50. struct integrity_iint_cache *iint;
  51. if (!IS_IMA(inode))
  52. return NULL;
  53. read_lock(&integrity_iint_lock);
  54. iint = __integrity_iint_find(inode);
  55. read_unlock(&integrity_iint_lock);
  56. return iint;
  57. }
  58. #define IMA_MAX_NESTING (FILESYSTEM_MAX_STACK_DEPTH+1)
  59. /*
  60. * It is not clear that IMA should be nested at all, but as long is it measures
  61. * files both on overlayfs and on underlying fs, we need to annotate the iint
  62. * mutex to avoid lockdep false positives related to IMA + overlayfs.
  63. * See ovl_lockdep_annotate_inode_mutex_key() for more details.
  64. */
  65. static inline void iint_lockdep_annotate(struct integrity_iint_cache *iint,
  66. struct inode *inode)
  67. {
  68. #ifdef CONFIG_LOCKDEP
  69. static struct lock_class_key iint_mutex_key[IMA_MAX_NESTING];
  70. int depth = inode->i_sb->s_stack_depth;
  71. if (WARN_ON_ONCE(depth < 0 || depth >= IMA_MAX_NESTING))
  72. depth = 0;
  73. lockdep_set_class(&iint->mutex, &iint_mutex_key[depth]);
  74. #endif
  75. }
  76. static void iint_init_always(struct integrity_iint_cache *iint,
  77. struct inode *inode)
  78. {
  79. iint->ima_hash = NULL;
  80. iint->version = 0;
  81. iint->flags = 0UL;
  82. iint->atomic_flags = 0UL;
  83. iint->ima_file_status = INTEGRITY_UNKNOWN;
  84. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  85. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  86. iint->ima_read_status = INTEGRITY_UNKNOWN;
  87. iint->ima_creds_status = INTEGRITY_UNKNOWN;
  88. iint->evm_status = INTEGRITY_UNKNOWN;
  89. iint->measured_pcrs = 0;
  90. mutex_init(&iint->mutex);
  91. iint_lockdep_annotate(iint, inode);
  92. }
  93. static void iint_free(struct integrity_iint_cache *iint)
  94. {
  95. kfree(iint->ima_hash);
  96. mutex_destroy(&iint->mutex);
  97. kmem_cache_free(iint_cache, iint);
  98. }
  99. /**
  100. * integrity_inode_get - find or allocate an iint associated with an inode
  101. * @inode: pointer to the inode
  102. * @return: allocated iint
  103. *
  104. * Caller must lock i_mutex
  105. */
  106. struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
  107. {
  108. struct rb_node **p;
  109. struct rb_node *node, *parent = NULL;
  110. struct integrity_iint_cache *iint, *test_iint;
  111. /*
  112. * The integrity's "iint_cache" is initialized at security_init(),
  113. * unless it is not included in the ordered list of LSMs enabled
  114. * on the boot command line.
  115. */
  116. if (!iint_cache)
  117. panic("%s: lsm=integrity required.\n", __func__);
  118. iint = integrity_iint_find(inode);
  119. if (iint)
  120. return iint;
  121. iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
  122. if (!iint)
  123. return NULL;
  124. iint_init_always(iint, inode);
  125. write_lock(&integrity_iint_lock);
  126. p = &integrity_iint_tree.rb_node;
  127. while (*p) {
  128. parent = *p;
  129. test_iint = rb_entry(parent, struct integrity_iint_cache,
  130. rb_node);
  131. if (inode < test_iint->inode) {
  132. p = &(*p)->rb_left;
  133. } else if (inode > test_iint->inode) {
  134. p = &(*p)->rb_right;
  135. } else {
  136. write_unlock(&integrity_iint_lock);
  137. kmem_cache_free(iint_cache, iint);
  138. return test_iint;
  139. }
  140. }
  141. iint->inode = inode;
  142. node = &iint->rb_node;
  143. inode->i_flags |= S_IMA;
  144. rb_link_node(node, parent, p);
  145. rb_insert_color(node, &integrity_iint_tree);
  146. write_unlock(&integrity_iint_lock);
  147. return iint;
  148. }
  149. /**
  150. * integrity_inode_free - called on security_inode_free
  151. * @inode: pointer to the inode
  152. *
  153. * Free the integrity information(iint) associated with an inode.
  154. */
  155. void integrity_inode_free(struct inode *inode)
  156. {
  157. struct integrity_iint_cache *iint;
  158. if (!IS_IMA(inode))
  159. return;
  160. write_lock(&integrity_iint_lock);
  161. iint = __integrity_iint_find(inode);
  162. rb_erase(&iint->rb_node, &integrity_iint_tree);
  163. write_unlock(&integrity_iint_lock);
  164. iint_free(iint);
  165. }
  166. static void iint_init_once(void *foo)
  167. {
  168. struct integrity_iint_cache *iint = (struct integrity_iint_cache *) foo;
  169. memset(iint, 0, sizeof(*iint));
  170. }
  171. static int __init integrity_iintcache_init(void)
  172. {
  173. iint_cache =
  174. kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
  175. 0, SLAB_PANIC, iint_init_once);
  176. return 0;
  177. }
  178. DEFINE_LSM(integrity) = {
  179. .name = "integrity",
  180. .init = integrity_iintcache_init,
  181. };
  182. /*
  183. * integrity_kernel_read - read data from the file
  184. *
  185. * This is a function for reading file content instead of kernel_read().
  186. * It does not perform locking checks to ensure it cannot be blocked.
  187. * It does not perform security checks because it is irrelevant for IMA.
  188. *
  189. */
  190. int integrity_kernel_read(struct file *file, loff_t offset,
  191. void *addr, unsigned long count)
  192. {
  193. return __kernel_read(file, addr, count, &offset);
  194. }
  195. /*
  196. * integrity_load_keys - load integrity keys hook
  197. *
  198. * Hooks is called from init/main.c:kernel_init_freeable()
  199. * when rootfs is ready
  200. */
  201. void __init integrity_load_keys(void)
  202. {
  203. ima_load_x509();
  204. if (!IS_ENABLED(CONFIG_IMA_LOAD_X509))
  205. evm_load_x509();
  206. }
  207. static int __init integrity_fs_init(void)
  208. {
  209. integrity_dir = securityfs_create_dir("integrity", NULL);
  210. if (IS_ERR(integrity_dir)) {
  211. int ret = PTR_ERR(integrity_dir);
  212. if (ret != -ENODEV)
  213. pr_err("Unable to create integrity sysfs dir: %d\n",
  214. ret);
  215. integrity_dir = NULL;
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. late_initcall(integrity_fs_init)