ima_iint.c 3.4 KB

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