ima_iint.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_NOFS);
  55. if (!iint)
  56. return iint;
  57. rc = radix_tree_preload(GFP_NOFS);
  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. spin_lock(&ima_iint_lock);
  68. iint = radix_tree_lookup(&ima_iint_store,
  69. (unsigned long)inode);
  70. spin_unlock(&ima_iint_lock);
  71. } else
  72. iint = NULL;
  73. }
  74. radix_tree_preload_end();
  75. return iint;
  76. }
  77. /**
  78. * ima_inode_alloc - allocate an iint associated with an inode
  79. * @inode: pointer to the inode
  80. *
  81. * Return 0 on success, 1 on failure.
  82. */
  83. int ima_inode_alloc(struct inode *inode)
  84. {
  85. struct ima_iint_cache *iint;
  86. if (!ima_initialized)
  87. return 0;
  88. iint = ima_iint_insert(inode);
  89. if (!iint)
  90. return 1;
  91. return 0;
  92. }
  93. /* ima_iint_find_insert_get - get the iint associated with an inode
  94. *
  95. * Most insertions are done at inode_alloc, except those allocated
  96. * before late_initcall. When the iint does not exist, allocate it,
  97. * initialize and insert it, and increment the iint refcount.
  98. *
  99. * (Can't initialize at security_initcall before any inodes are
  100. * allocated, got to wait at least until proc_init.)
  101. *
  102. * Return the iint.
  103. */
  104. struct ima_iint_cache *ima_iint_find_insert_get(struct inode *inode)
  105. {
  106. struct ima_iint_cache *iint = NULL;
  107. iint = ima_iint_find_get(inode);
  108. if (iint)
  109. return iint;
  110. iint = ima_iint_insert(inode);
  111. if (iint)
  112. kref_get(&iint->refcount);
  113. return iint;
  114. }
  115. EXPORT_SYMBOL_GPL(ima_iint_find_insert_get);
  116. /* iint_free - called when the iint refcount goes to zero */
  117. void iint_free(struct kref *kref)
  118. {
  119. struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
  120. refcount);
  121. iint->version = 0;
  122. iint->flags = 0UL;
  123. if (iint->readcount != 0) {
  124. printk(KERN_INFO "%s: readcount: %ld\n", __FUNCTION__,
  125. iint->readcount);
  126. iint->readcount = 0;
  127. }
  128. if (iint->writecount != 0) {
  129. printk(KERN_INFO "%s: writecount: %ld\n", __FUNCTION__,
  130. iint->writecount);
  131. iint->writecount = 0;
  132. }
  133. if (iint->opencount != 0) {
  134. printk(KERN_INFO "%s: opencount: %ld\n", __FUNCTION__,
  135. iint->opencount);
  136. iint->opencount = 0;
  137. }
  138. kref_set(&iint->refcount, 1);
  139. kmem_cache_free(iint_cache, iint);
  140. }
  141. void iint_rcu_free(struct rcu_head *rcu_head)
  142. {
  143. struct ima_iint_cache *iint = container_of(rcu_head,
  144. struct ima_iint_cache, rcu);
  145. kref_put(&iint->refcount, iint_free);
  146. }
  147. /**
  148. * ima_iint_delete - called on integrity_inode_free
  149. * @inode: pointer to the inode
  150. *
  151. * Free the integrity information(iint) associated with an inode.
  152. */
  153. void ima_iint_delete(struct inode *inode)
  154. {
  155. struct ima_iint_cache *iint;
  156. if (!ima_initialized)
  157. return;
  158. spin_lock(&ima_iint_lock);
  159. iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
  160. spin_unlock(&ima_iint_lock);
  161. if (iint)
  162. call_rcu(&iint->rcu, iint_rcu_free);
  163. }
  164. static void init_once(void *foo)
  165. {
  166. struct ima_iint_cache *iint = foo;
  167. memset(iint, 0, sizeof *iint);
  168. iint->version = 0;
  169. iint->flags = 0UL;
  170. mutex_init(&iint->mutex);
  171. iint->readcount = 0;
  172. iint->writecount = 0;
  173. iint->opencount = 0;
  174. kref_set(&iint->refcount, 1);
  175. }
  176. void __init ima_iintcache_init(void)
  177. {
  178. iint_cache =
  179. kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
  180. SLAB_PANIC, init_once);
  181. }