iint.c 3.9 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_RWLOCK(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. while (n) {
  35. iint = rb_entry(n, struct integrity_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. * integrity_iint_find - return the iint associated with an inode
  49. */
  50. struct integrity_iint_cache *integrity_iint_find(struct inode *inode)
  51. {
  52. struct integrity_iint_cache *iint;
  53. if (!IS_IMA(inode))
  54. return NULL;
  55. read_lock(&integrity_iint_lock);
  56. iint = __integrity_iint_find(inode);
  57. read_unlock(&integrity_iint_lock);
  58. return iint;
  59. }
  60. static void iint_free(struct integrity_iint_cache *iint)
  61. {
  62. iint->version = 0;
  63. iint->flags = 0UL;
  64. iint->ima_file_status = INTEGRITY_UNKNOWN;
  65. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  66. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  67. iint->ima_module_status = INTEGRITY_UNKNOWN;
  68. iint->evm_status = INTEGRITY_UNKNOWN;
  69. kmem_cache_free(iint_cache, iint);
  70. }
  71. /**
  72. * integrity_inode_get - find or allocate an iint associated with an inode
  73. * @inode: pointer to the inode
  74. * @return: allocated iint
  75. *
  76. * Caller must lock i_mutex
  77. */
  78. struct integrity_iint_cache *integrity_inode_get(struct inode *inode)
  79. {
  80. struct rb_node **p;
  81. struct rb_node *node, *parent = NULL;
  82. struct integrity_iint_cache *iint, *test_iint;
  83. iint = integrity_iint_find(inode);
  84. if (iint)
  85. return iint;
  86. iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
  87. if (!iint)
  88. return NULL;
  89. write_lock(&integrity_iint_lock);
  90. p = &integrity_iint_tree.rb_node;
  91. while (*p) {
  92. parent = *p;
  93. test_iint = rb_entry(parent, struct integrity_iint_cache,
  94. rb_node);
  95. if (inode < test_iint->inode)
  96. p = &(*p)->rb_left;
  97. else
  98. p = &(*p)->rb_right;
  99. }
  100. iint->inode = inode;
  101. node = &iint->rb_node;
  102. inode->i_flags |= S_IMA;
  103. rb_link_node(node, parent, p);
  104. rb_insert_color(node, &integrity_iint_tree);
  105. write_unlock(&integrity_iint_lock);
  106. return iint;
  107. }
  108. /**
  109. * integrity_inode_free - called on security_inode_free
  110. * @inode: pointer to the inode
  111. *
  112. * Free the integrity information(iint) associated with an inode.
  113. */
  114. void integrity_inode_free(struct inode *inode)
  115. {
  116. struct integrity_iint_cache *iint;
  117. if (!IS_IMA(inode))
  118. return;
  119. write_lock(&integrity_iint_lock);
  120. iint = __integrity_iint_find(inode);
  121. rb_erase(&iint->rb_node, &integrity_iint_tree);
  122. write_unlock(&integrity_iint_lock);
  123. iint_free(iint);
  124. }
  125. static void init_once(void *foo)
  126. {
  127. struct integrity_iint_cache *iint = foo;
  128. memset(iint, 0, sizeof *iint);
  129. iint->version = 0;
  130. iint->flags = 0UL;
  131. iint->ima_file_status = INTEGRITY_UNKNOWN;
  132. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  133. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  134. iint->ima_module_status = INTEGRITY_UNKNOWN;
  135. iint->evm_status = INTEGRITY_UNKNOWN;
  136. }
  137. static int __init integrity_iintcache_init(void)
  138. {
  139. iint_cache =
  140. kmem_cache_create("iint_cache", sizeof(struct integrity_iint_cache),
  141. 0, SLAB_PANIC, init_once);
  142. iint_initialized = 1;
  143. return 0;
  144. }
  145. security_initcall(integrity_iintcache_init);