ima_iint.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  43. * ima_inode_alloc - allocate an iint associated with an inode
  44. * @inode: pointer to the inode
  45. */
  46. int ima_inode_alloc(struct inode *inode)
  47. {
  48. struct ima_iint_cache *iint = NULL;
  49. int rc = 0;
  50. if (!ima_initialized)
  51. return 0;
  52. iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
  53. if (!iint)
  54. return -ENOMEM;
  55. rc = radix_tree_preload(GFP_NOFS);
  56. if (rc < 0)
  57. goto out;
  58. spin_lock(&ima_iint_lock);
  59. rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
  60. spin_unlock(&ima_iint_lock);
  61. out:
  62. if (rc < 0)
  63. kmem_cache_free(iint_cache, iint);
  64. radix_tree_preload_end();
  65. return rc;
  66. }
  67. /* iint_free - called when the iint refcount goes to zero */
  68. void iint_free(struct kref *kref)
  69. {
  70. struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
  71. refcount);
  72. iint->version = 0;
  73. iint->flags = 0UL;
  74. if (iint->readcount != 0) {
  75. printk(KERN_INFO "%s: readcount: %ld\n", __FUNCTION__,
  76. iint->readcount);
  77. iint->readcount = 0;
  78. }
  79. if (iint->writecount != 0) {
  80. printk(KERN_INFO "%s: writecount: %ld\n", __FUNCTION__,
  81. iint->writecount);
  82. iint->writecount = 0;
  83. }
  84. if (iint->opencount != 0) {
  85. printk(KERN_INFO "%s: opencount: %ld\n", __FUNCTION__,
  86. iint->opencount);
  87. iint->opencount = 0;
  88. }
  89. kref_set(&iint->refcount, 1);
  90. kmem_cache_free(iint_cache, iint);
  91. }
  92. void iint_rcu_free(struct rcu_head *rcu_head)
  93. {
  94. struct ima_iint_cache *iint = container_of(rcu_head,
  95. struct ima_iint_cache, rcu);
  96. kref_put(&iint->refcount, iint_free);
  97. }
  98. /**
  99. * ima_iint_delete - called on integrity_inode_free
  100. * @inode: pointer to the inode
  101. *
  102. * Free the integrity information(iint) associated with an inode.
  103. */
  104. void ima_iint_delete(struct inode *inode)
  105. {
  106. struct ima_iint_cache *iint;
  107. if (!ima_initialized)
  108. return;
  109. spin_lock(&ima_iint_lock);
  110. iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
  111. spin_unlock(&ima_iint_lock);
  112. if (iint)
  113. call_rcu(&iint->rcu, iint_rcu_free);
  114. }
  115. static void init_once(void *foo)
  116. {
  117. struct ima_iint_cache *iint = foo;
  118. memset(iint, 0, sizeof *iint);
  119. iint->version = 0;
  120. iint->flags = 0UL;
  121. mutex_init(&iint->mutex);
  122. iint->readcount = 0;
  123. iint->writecount = 0;
  124. iint->opencount = 0;
  125. kref_set(&iint->refcount, 1);
  126. }
  127. void __init ima_iintcache_init(void)
  128. {
  129. iint_cache =
  130. kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
  131. SLAB_PANIC, init_once);
  132. }