ima_iint.c 3.4 KB

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