iint.c 5.4 KB

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