ima_iint.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 rbtree tree.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/rbtree.h>
  21. #include "ima.h"
  22. static struct rb_root ima_iint_tree = RB_ROOT;
  23. static DEFINE_SPINLOCK(ima_iint_lock);
  24. static struct kmem_cache *iint_cache __read_mostly;
  25. int iint_initialized = 0;
  26. /*
  27. * __ima_iint_find - return the iint associated with an inode
  28. */
  29. static struct ima_iint_cache *__ima_iint_find(struct inode *inode)
  30. {
  31. struct ima_iint_cache *iint;
  32. struct rb_node *n = ima_iint_tree.rb_node;
  33. assert_spin_locked(&ima_iint_lock);
  34. while (n) {
  35. iint = rb_entry(n, struct ima_iint_cache, rb_node);
  36. if (inode < iint->inode)
  37. n = n->rb_left;
  38. else if (inode > iint->inode)
  39. n = n->rb_right;
  40. else
  41. break;
  42. }
  43. if (!n)
  44. return NULL;
  45. return iint;
  46. }
  47. /*
  48. * ima_iint_find - return the iint associated with an inode
  49. */
  50. struct ima_iint_cache *ima_iint_find(struct inode *inode)
  51. {
  52. struct ima_iint_cache *iint;
  53. if (!IS_IMA(inode))
  54. return NULL;
  55. spin_lock(&ima_iint_lock);
  56. iint = __ima_iint_find(inode);
  57. spin_unlock(&ima_iint_lock);
  58. return iint;
  59. }
  60. static void iint_free(struct ima_iint_cache *iint)
  61. {
  62. iint->version = 0;
  63. iint->flags = 0UL;
  64. kmem_cache_free(iint_cache, iint);
  65. }
  66. /**
  67. * ima_inode_alloc - allocate an iint associated with an inode
  68. * @inode: pointer to the inode
  69. */
  70. int ima_inode_alloc(struct inode *inode)
  71. {
  72. struct rb_node **p;
  73. struct rb_node *new_node, *parent = NULL;
  74. struct ima_iint_cache *new_iint, *test_iint;
  75. int rc;
  76. new_iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
  77. if (!new_iint)
  78. return -ENOMEM;
  79. new_iint->inode = inode;
  80. new_node = &new_iint->rb_node;
  81. mutex_lock(&inode->i_mutex); /* i_flags */
  82. spin_lock(&ima_iint_lock);
  83. p = &ima_iint_tree.rb_node;
  84. while (*p) {
  85. parent = *p;
  86. test_iint = rb_entry(parent, struct ima_iint_cache, rb_node);
  87. rc = -EEXIST;
  88. if (inode < test_iint->inode)
  89. p = &(*p)->rb_left;
  90. else if (inode > test_iint->inode)
  91. p = &(*p)->rb_right;
  92. else
  93. goto out_err;
  94. }
  95. inode->i_flags |= S_IMA;
  96. rb_link_node(new_node, parent, p);
  97. rb_insert_color(new_node, &ima_iint_tree);
  98. spin_unlock(&ima_iint_lock);
  99. mutex_unlock(&inode->i_mutex); /* i_flags */
  100. return 0;
  101. out_err:
  102. spin_unlock(&ima_iint_lock);
  103. mutex_unlock(&inode->i_mutex); /* i_flags */
  104. iint_free(new_iint);
  105. return rc;
  106. }
  107. /**
  108. * ima_inode_free - called on security_inode_free
  109. * @inode: pointer to the inode
  110. *
  111. * Free the integrity information(iint) associated with an inode.
  112. */
  113. void ima_inode_free(struct inode *inode)
  114. {
  115. struct ima_iint_cache *iint;
  116. if (!IS_IMA(inode))
  117. return;
  118. spin_lock(&ima_iint_lock);
  119. iint = __ima_iint_find(inode);
  120. rb_erase(&iint->rb_node, &ima_iint_tree);
  121. spin_unlock(&ima_iint_lock);
  122. iint_free(iint);
  123. }
  124. static void init_once(void *foo)
  125. {
  126. struct ima_iint_cache *iint = foo;
  127. memset(iint, 0, sizeof *iint);
  128. iint->version = 0;
  129. iint->flags = 0UL;
  130. mutex_init(&iint->mutex);
  131. }
  132. static int __init ima_iintcache_init(void)
  133. {
  134. iint_cache =
  135. kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
  136. SLAB_PANIC, init_once);
  137. iint_initialized = 1;
  138. return 0;
  139. }
  140. security_initcall(ima_iintcache_init);