ima_iint.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <zohar@us.ibm.com>
  7. *
  8. * File: ima_iint.c
  9. * - implements the IMA hook: ima_inode_free
  10. * - cache integrity information in the inode security blob
  11. */
  12. #include <linux/slab.h>
  13. #include "ima.h"
  14. static struct kmem_cache *ima_iint_cache __ro_after_init;
  15. /**
  16. * ima_iint_find - Return the iint associated with an inode
  17. * @inode: Pointer to the inode
  18. *
  19. * Return the IMA integrity information (iint) associated with an inode, if the
  20. * inode was processed by IMA.
  21. *
  22. * Return: Found iint or NULL.
  23. */
  24. struct ima_iint_cache *ima_iint_find(struct inode *inode)
  25. {
  26. if (!IS_IMA(inode))
  27. return NULL;
  28. return ima_inode_get_iint(inode);
  29. }
  30. #define IMA_MAX_NESTING (FILESYSTEM_MAX_STACK_DEPTH + 1)
  31. /*
  32. * It is not clear that IMA should be nested at all, but as long is it measures
  33. * files both on overlayfs and on underlying fs, we need to annotate the iint
  34. * mutex to avoid lockdep false positives related to IMA + overlayfs.
  35. * See ovl_lockdep_annotate_inode_mutex_key() for more details.
  36. */
  37. static inline void ima_iint_lockdep_annotate(struct ima_iint_cache *iint,
  38. struct inode *inode)
  39. {
  40. #ifdef CONFIG_LOCKDEP
  41. static struct lock_class_key ima_iint_mutex_key[IMA_MAX_NESTING];
  42. int depth = inode->i_sb->s_stack_depth;
  43. if (WARN_ON_ONCE(depth < 0 || depth >= IMA_MAX_NESTING))
  44. depth = 0;
  45. lockdep_set_class(&iint->mutex, &ima_iint_mutex_key[depth]);
  46. #endif
  47. }
  48. static void ima_iint_init_always(struct ima_iint_cache *iint,
  49. struct inode *inode)
  50. {
  51. iint->ima_hash = NULL;
  52. iint->real_inode.version = 0;
  53. iint->flags = 0UL;
  54. iint->atomic_flags = 0UL;
  55. iint->ima_file_status = INTEGRITY_UNKNOWN;
  56. iint->ima_mmap_status = INTEGRITY_UNKNOWN;
  57. iint->ima_bprm_status = INTEGRITY_UNKNOWN;
  58. iint->ima_read_status = INTEGRITY_UNKNOWN;
  59. iint->ima_creds_status = INTEGRITY_UNKNOWN;
  60. iint->measured_pcrs = 0;
  61. mutex_init(&iint->mutex);
  62. ima_iint_lockdep_annotate(iint, inode);
  63. }
  64. static void ima_iint_free(struct ima_iint_cache *iint)
  65. {
  66. kfree(iint->ima_hash);
  67. mutex_destroy(&iint->mutex);
  68. kmem_cache_free(ima_iint_cache, iint);
  69. }
  70. /**
  71. * ima_inode_get - Find or allocate an iint associated with an inode
  72. * @inode: Pointer to the inode
  73. *
  74. * Find an iint associated with an inode, and allocate a new one if not found.
  75. * Caller must lock i_mutex.
  76. *
  77. * Return: An iint on success, NULL on error.
  78. */
  79. struct ima_iint_cache *ima_inode_get(struct inode *inode)
  80. {
  81. struct ima_iint_cache *iint;
  82. iint = ima_iint_find(inode);
  83. if (iint)
  84. return iint;
  85. iint = kmem_cache_alloc(ima_iint_cache, GFP_NOFS);
  86. if (!iint)
  87. return NULL;
  88. ima_iint_init_always(iint, inode);
  89. inode->i_flags |= S_IMA;
  90. ima_inode_set_iint(inode, iint);
  91. return iint;
  92. }
  93. /**
  94. * ima_inode_free_rcu - Called to free an inode via a RCU callback
  95. * @inode_security: The inode->i_security pointer
  96. *
  97. * Free the IMA data associated with an inode.
  98. */
  99. void ima_inode_free_rcu(void *inode_security)
  100. {
  101. struct ima_iint_cache **iint_p = inode_security + ima_blob_sizes.lbs_inode;
  102. /* *iint_p should be NULL if !IS_IMA(inode) */
  103. if (*iint_p)
  104. ima_iint_free(*iint_p);
  105. }
  106. static void ima_iint_init_once(void *foo)
  107. {
  108. struct ima_iint_cache *iint = (struct ima_iint_cache *)foo;
  109. memset(iint, 0, sizeof(*iint));
  110. }
  111. void __init ima_iintcache_init(void)
  112. {
  113. ima_iint_cache =
  114. kmem_cache_create("ima_iint_cache", sizeof(struct ima_iint_cache),
  115. 0, SLAB_PANIC, ima_iint_init_once);
  116. }