ima_iint.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/radix-tree.h>
  21. #include "ima.h"
  22. RADIX_TREE(ima_iint_store, GFP_ATOMIC);
  23. DEFINE_SPINLOCK(ima_iint_lock);
  24. static struct kmem_cache *iint_cache __read_mostly;
  25. int iint_initialized = 0;
  26. /* ima_iint_find_get - return the iint associated with an inode
  27. *
  28. * ima_iint_find_get gets a reference to the iint. Caller must
  29. * remember to put the iint reference.
  30. */
  31. struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
  32. {
  33. struct ima_iint_cache *iint;
  34. rcu_read_lock();
  35. iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
  36. if (!iint)
  37. goto out;
  38. kref_get(&iint->refcount);
  39. out:
  40. rcu_read_unlock();
  41. return iint;
  42. }
  43. /**
  44. * ima_inode_alloc - allocate an iint associated with an inode
  45. * @inode: pointer to the inode
  46. */
  47. int ima_inode_alloc(struct inode *inode)
  48. {
  49. struct ima_iint_cache *iint = NULL;
  50. int rc = 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. radix_tree_preload_end();
  61. out:
  62. if (rc < 0)
  63. kmem_cache_free(iint_cache, iint);
  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", __func__,
  75. iint->readcount);
  76. iint->readcount = 0;
  77. }
  78. if (iint->writecount != 0) {
  79. printk(KERN_INFO "%s: writecount: %ld\n", __func__,
  80. iint->writecount);
  81. iint->writecount = 0;
  82. }
  83. if (iint->opencount != 0) {
  84. printk(KERN_INFO "%s: opencount: %ld\n", __func__,
  85. iint->opencount);
  86. iint->opencount = 0;
  87. }
  88. kref_init(&iint->refcount);
  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. spin_lock(&ima_iint_lock);
  107. iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
  108. spin_unlock(&ima_iint_lock);
  109. if (iint)
  110. call_rcu(&iint->rcu, iint_rcu_free);
  111. }
  112. static void init_once(void *foo)
  113. {
  114. struct ima_iint_cache *iint = foo;
  115. memset(iint, 0, sizeof *iint);
  116. iint->version = 0;
  117. iint->flags = 0UL;
  118. mutex_init(&iint->mutex);
  119. iint->readcount = 0;
  120. iint->writecount = 0;
  121. iint->opencount = 0;
  122. kref_init(&iint->refcount);
  123. }
  124. static int __init ima_iintcache_init(void)
  125. {
  126. iint_cache =
  127. kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
  128. SLAB_PANIC, init_once);
  129. iint_initialized = 1;
  130. return 0;
  131. }
  132. security_initcall(ima_iintcache_init);