iint.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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->atomic_flags = 0UL;
  68. iint->ima_file_status = INTEGRITY_UNKNOWN;
  69. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  70. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  71. iint->ima_read_status = INTEGRITY_UNKNOWN;
  72. iint->evm_status = INTEGRITY_UNKNOWN;
  73. iint->measured_pcrs = 0;
  74. kmem_cache_free(iint_cache, iint);
  75. }
  76. /**
  77. * integrity_inode_get - find or allocate an iint associated with an inode
  78. * @inode: pointer to the inode
  79. * @return: allocated iint
  80. *
  81. * Caller must lock i_mutex
  82. */
  83. struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
  84. {
  85. struct rb_node **p;
  86. struct rb_node *node, *parent = NULL;
  87. struct integrity_iint_cache *iint, *test_iint;
  88. iint = integrity_iint_find(inode);
  89. if (iint)
  90. return iint;
  91. iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
  92. if (!iint)
  93. return NULL;
  94. write_lock(&integrity_iint_lock);
  95. p = &integrity_iint_tree.rb_node;
  96. while (*p) {
  97. parent = *p;
  98. test_iint = rb_entry(parent, struct integrity_iint_cache,
  99. rb_node);
  100. if (inode < test_iint->inode)
  101. p = &(*p)->rb_left;
  102. else
  103. p = &(*p)->rb_right;
  104. }
  105. iint->inode = inode;
  106. node = &iint->rb_node;
  107. inode->i_flags |= S_IMA;
  108. rb_link_node(node, parent, p);
  109. rb_insert_color(node, &integrity_iint_tree);
  110. write_unlock(&integrity_iint_lock);
  111. return iint;
  112. }
  113. /**
  114. * integrity_inode_free - called on security_inode_free
  115. * @inode: pointer to the inode
  116. *
  117. * Free the integrity information(iint) associated with an inode.
  118. */
  119. void integrity_inode_free(struct inode *inode)
  120. {
  121. struct integrity_iint_cache *iint;
  122. if (!IS_IMA(inode))
  123. return;
  124. write_lock(&integrity_iint_lock);
  125. iint = __integrity_iint_find(inode);
  126. rb_erase(&iint->rb_node, &integrity_iint_tree);
  127. write_unlock(&integrity_iint_lock);
  128. iint_free(iint);
  129. }
  130. static void init_once(void *foo)
  131. {
  132. struct integrity_iint_cache *iint = foo;
  133. memset(iint, 0, sizeof(*iint));
  134. iint->version = 0;
  135. iint->flags = 0UL;
  136. iint->atomic_flags = 0;
  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->evm_status = INTEGRITY_UNKNOWN;
  142. iint->measured_pcrs = 0;
  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. char *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_read_file - read entire file content into the buffer
  177. *
  178. * This is function opens a file, allocates the buffer of required
  179. * size, read entire file content to the buffer and closes the file
  180. *
  181. * It is used only by init code.
  182. *
  183. */
  184. int __init integrity_read_file(const char *path, char **data)
  185. {
  186. struct file *file;
  187. loff_t size;
  188. char *buf;
  189. int rc = -EINVAL;
  190. if (!path || !*path)
  191. return -EINVAL;
  192. file = filp_open(path, O_RDONLY, 0);
  193. if (IS_ERR(file)) {
  194. rc = PTR_ERR(file);
  195. pr_err("Unable to open file: %s (%d)", path, rc);
  196. return rc;
  197. }
  198. size = i_size_read(file_inode(file));
  199. if (size <= 0)
  200. goto out;
  201. buf = kmalloc(size, GFP_KERNEL);
  202. if (!buf) {
  203. rc = -ENOMEM;
  204. goto out;
  205. }
  206. rc = integrity_kernel_read(file, 0, buf, size);
  207. if (rc == size) {
  208. *data = buf;
  209. } else {
  210. kfree(buf);
  211. if (rc >= 0)
  212. rc = -EIO;
  213. }
  214. out:
  215. fput(file);
  216. return rc;
  217. }
  218. /*
  219. * integrity_load_keys - load integrity keys hook
  220. *
  221. * Hooks is called from init/main.c:kernel_init_freeable()
  222. * when rootfs is ready
  223. */
  224. void __init integrity_load_keys(void)
  225. {
  226. ima_load_x509();
  227. evm_load_x509();
  228. }