iint.c 5.7 KB

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