ima_iint.c 3.4 KB

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