ima_appraise.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2011 IBM Corporation
  3. *
  4. * Author:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/xattr.h>
  15. #include <linux/magic.h>
  16. #include <linux/ima.h>
  17. #include <linux/evm.h>
  18. #include "ima.h"
  19. static int __init default_appraise_setup(char *str)
  20. {
  21. if (strncmp(str, "off", 3) == 0)
  22. ima_appraise = 0;
  23. else if (strncmp(str, "fix", 3) == 0)
  24. ima_appraise = IMA_APPRAISE_FIX;
  25. return 1;
  26. }
  27. __setup("ima_appraise=", default_appraise_setup);
  28. /*
  29. * ima_must_appraise - set appraise flag
  30. *
  31. * Return 1 to appraise
  32. */
  33. int ima_must_appraise(struct inode *inode, enum ima_hooks func, int mask)
  34. {
  35. if (!ima_appraise)
  36. return 0;
  37. return ima_match_policy(inode, func, mask, IMA_APPRAISE);
  38. }
  39. static void ima_fix_xattr(struct dentry *dentry,
  40. struct integrity_iint_cache *iint)
  41. {
  42. iint->digest[0] = IMA_XATTR_DIGEST;
  43. __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
  44. iint->digest, IMA_DIGEST_SIZE + 1, 0);
  45. }
  46. /*
  47. * ima_appraise_measurement - appraise file measurement
  48. *
  49. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  50. * Assuming success, compare the xattr hash with the collected measurement.
  51. *
  52. * Return 0 on success, error code otherwise
  53. */
  54. int ima_appraise_measurement(struct integrity_iint_cache *iint,
  55. struct file *file, const unsigned char *filename)
  56. {
  57. struct dentry *dentry = file->f_dentry;
  58. struct inode *inode = dentry->d_inode;
  59. u8 xattr_value[IMA_DIGEST_SIZE];
  60. enum integrity_status status = INTEGRITY_UNKNOWN;
  61. const char *op = "appraise_data";
  62. char *cause = "unknown";
  63. int rc;
  64. if (!ima_appraise)
  65. return 0;
  66. if (!inode->i_op->getxattr)
  67. return INTEGRITY_UNKNOWN;
  68. if (iint->flags & IMA_APPRAISED)
  69. return iint->ima_status;
  70. rc = inode->i_op->getxattr(dentry, XATTR_NAME_IMA, xattr_value,
  71. IMA_DIGEST_SIZE);
  72. if (rc <= 0) {
  73. if (rc && rc != -ENODATA)
  74. goto out;
  75. cause = "missing-hash";
  76. status =
  77. (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
  78. goto out;
  79. }
  80. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
  81. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  82. if ((status == INTEGRITY_NOLABEL)
  83. || (status == INTEGRITY_NOXATTRS))
  84. cause = "missing-HMAC";
  85. else if (status == INTEGRITY_FAIL)
  86. cause = "invalid-HMAC";
  87. goto out;
  88. }
  89. rc = memcmp(xattr_value, iint->digest, IMA_DIGEST_SIZE);
  90. if (rc) {
  91. status = INTEGRITY_FAIL;
  92. cause = "invalid-hash";
  93. print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
  94. xattr_value, IMA_DIGEST_SIZE);
  95. print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
  96. iint->digest, IMA_DIGEST_SIZE);
  97. goto out;
  98. }
  99. status = INTEGRITY_PASS;
  100. iint->flags |= IMA_APPRAISED;
  101. out:
  102. if (status != INTEGRITY_PASS) {
  103. if (ima_appraise & IMA_APPRAISE_FIX) {
  104. ima_fix_xattr(dentry, iint);
  105. status = INTEGRITY_PASS;
  106. }
  107. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  108. op, cause, rc, 0);
  109. }
  110. iint->ima_status = status;
  111. return status;
  112. }
  113. /*
  114. * ima_update_xattr - update 'security.ima' hash value
  115. */
  116. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  117. {
  118. struct dentry *dentry = file->f_dentry;
  119. int rc = 0;
  120. rc = ima_collect_measurement(iint, file);
  121. if (rc < 0)
  122. return;
  123. ima_fix_xattr(dentry, iint);
  124. }
  125. /**
  126. * ima_inode_post_setattr - reflect file metadata changes
  127. * @dentry: pointer to the affected dentry
  128. *
  129. * Changes to a dentry's metadata might result in needing to appraise.
  130. *
  131. * This function is called from notify_change(), which expects the caller
  132. * to lock the inode's i_mutex.
  133. */
  134. void ima_inode_post_setattr(struct dentry *dentry)
  135. {
  136. struct inode *inode = dentry->d_inode;
  137. struct integrity_iint_cache *iint;
  138. int must_appraise, rc;
  139. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  140. || !inode->i_op->removexattr)
  141. return;
  142. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  143. iint = integrity_iint_find(inode);
  144. if (iint) {
  145. if (must_appraise)
  146. iint->flags |= IMA_APPRAISE;
  147. else
  148. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
  149. }
  150. if (!must_appraise)
  151. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  152. return;
  153. }