ima_iint.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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: ima_iint.c
  13. * - implements the IMA hooks: ima_inode_alloc, ima_inode_free
  14. * - cache integrity information associated with an inode
  15. * using a radix tree.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/radix-tree.h>
  20. #include "ima.h"
  21. #define ima_iint_delete ima_inode_free
  22. RADIX_TREE(ima_iint_store, GFP_ATOMIC);
  23. DEFINE_SPINLOCK(ima_iint_lock);
  24. static struct kmem_cache *iint_cache __read_mostly;
  25. /* ima_iint_find_get - return the iint associated with an inode
  26. *
  27. * ima_iint_find_get gets a reference to the iint. Caller must
  28. * remember to put the iint reference.
  29. */
  30. struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
  31. {
  32. struct ima_iint_cache *iint;
  33. rcu_read_lock();
  34. iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
  35. if (!iint)
  36. goto out;
  37. kref_get(&iint->refcount);
  38. out:
  39. rcu_read_unlock();
  40. return iint;
  41. }
  42. /* Allocate memory for the iint associated with the inode
  43. * from the iint_cache slab, initialize the iint, and
  44. * insert it into the radix tree.
  45. *
  46. * On success return a pointer to the iint; on failure return NULL.
  47. */
  48. struct ima_iint_cache *ima_iint_insert(struct inode *inode)
  49. {
  50. struct ima_iint_cache *iint = NULL;
  51. int rc = 0;
  52. if (!ima_initialized)
  53. return iint;
  54. iint = kmem_cache_alloc(iint_cache, GFP_KERNEL);
  55. if (!iint)
  56. return iint;
  57. rc = radix_tree_preload(GFP_KERNEL);
  58. if (rc < 0)
  59. goto out;
  60. spin_lock(&ima_iint_lock);
  61. rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
  62. spin_unlock(&ima_iint_lock);
  63. out:
  64. if (rc < 0) {
  65. kmem_cache_free(iint_cache, iint);
  66. if (rc == -EEXIST) {
  67. iint = radix_tree_lookup(&ima_iint_store,
  68. (unsigned long)inode);
  69. } else
  70. iint = NULL;
  71. }
  72. radix_tree_preload_end();
  73. return iint;
  74. }
  75. /**
  76. * ima_inode_alloc - allocate an iint associated with an inode
  77. * @inode: pointer to the inode
  78. *
  79. * Return 0 on success, 1 on failure.
  80. */
  81. int ima_inode_alloc(struct inode *inode)
  82. {
  83. struct ima_iint_cache *iint;
  84. if (!ima_initialized)
  85. return 0;
  86. iint = ima_iint_insert(inode);
  87. if (!iint)
  88. return 1;
  89. return 0;
  90. }
  91. /* ima_iint_find_insert_get - get the iint associated with an inode
  92. *
  93. * Most insertions are done at inode_alloc, except those allocated
  94. * before late_initcall. When the iint does not exist, allocate it,
  95. * initialize and insert it, and increment the iint refcount.
  96. *
  97. * (Can't initialize at security_initcall before any inodes are
  98. * allocated, got to wait at least until proc_init.)
  99. *
  100. * Return the iint.
  101. */
  102. struct ima_iint_cache *ima_iint_find_insert_get(struct inode *inode)
  103. {
  104. struct ima_iint_cache *iint = NULL;
  105. iint = ima_iint_find_get(inode);
  106. if (iint)
  107. return iint;
  108. iint = ima_iint_insert(inode);
  109. if (iint)
  110. kref_get(&iint->refcount);
  111. return iint;
  112. }
  113. EXPORT_SYMBOL_GPL(ima_iint_find_insert_get);
  114. /* iint_free - called when the iint refcount goes to zero */
  115. void iint_free(struct kref *kref)
  116. {
  117. struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
  118. refcount);
  119. iint->version = 0;
  120. iint->flags = 0UL;
  121. if (iint->readcount != 0) {
  122. printk(KERN_INFO "%s: readcount: %ld\n", __FUNCTION__,
  123. iint->readcount);
  124. iint->readcount = 0;
  125. }
  126. if (iint->writecount != 0) {
  127. printk(KERN_INFO "%s: writecount: %ld\n", __FUNCTION__,
  128. iint->writecount);
  129. iint->writecount = 0;
  130. }
  131. if (iint->opencount != 0) {
  132. printk(KERN_INFO "%s: opencount: %ld\n", __FUNCTION__,
  133. iint->opencount);
  134. iint->opencount = 0;
  135. }
  136. kref_set(&iint->refcount, 1);
  137. kmem_cache_free(iint_cache, iint);
  138. }
  139. void iint_rcu_free(struct rcu_head *rcu_head)
  140. {
  141. struct ima_iint_cache *iint = container_of(rcu_head,
  142. struct ima_iint_cache, rcu);
  143. kref_put(&iint->refcount, iint_free);
  144. }
  145. /**
  146. * ima_iint_delete - called on integrity_inode_free
  147. * @inode: pointer to the inode
  148. *
  149. * Free the integrity information(iint) associated with an inode.
  150. */
  151. void ima_iint_delete(struct inode *inode)
  152. {
  153. struct ima_iint_cache *iint;
  154. if (!ima_initialized)
  155. return;
  156. spin_lock(&ima_iint_lock);
  157. iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
  158. spin_unlock(&ima_iint_lock);
  159. if (iint)
  160. call_rcu(&iint->rcu, iint_rcu_free);
  161. }
  162. static void init_once(void *foo)
  163. {
  164. struct ima_iint_cache *iint = foo;
  165. memset(iint, 0, sizeof *iint);
  166. iint->version = 0;
  167. iint->flags = 0UL;
  168. mutex_init(&iint->mutex);
  169. iint->readcount = 0;
  170. iint->writecount = 0;
  171. iint->opencount = 0;
  172. kref_set(&iint->refcount, 1);
  173. }
  174. void ima_iintcache_init(void)
  175. {
  176. iint_cache =
  177. kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
  178. SLAB_PANIC, init_once);
  179. }