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