iint.c 5.7 KB

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