ima_appraise.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. return 0;
  36. }
  37. static void ima_fix_xattr(struct dentry *dentry,
  38. struct integrity_iint_cache *iint)
  39. {
  40. iint->digest[0] = IMA_XATTR_DIGEST;
  41. __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
  42. iint->digest, IMA_DIGEST_SIZE + 1, 0);
  43. }
  44. /*
  45. * ima_appraise_measurement - appraise file measurement
  46. *
  47. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  48. * Assuming success, compare the xattr hash with the collected measurement.
  49. *
  50. * Return 0 on success, error code otherwise
  51. */
  52. int ima_appraise_measurement(struct integrity_iint_cache *iint,
  53. struct file *file, const unsigned char *filename)
  54. {
  55. struct dentry *dentry = file->f_dentry;
  56. struct inode *inode = dentry->d_inode;
  57. u8 xattr_value[IMA_DIGEST_SIZE];
  58. enum integrity_status status = INTEGRITY_UNKNOWN;
  59. const char *op = "appraise_data";
  60. char *cause = "unknown";
  61. int rc;
  62. if (!ima_appraise)
  63. return 0;
  64. if (!inode->i_op->getxattr)
  65. return INTEGRITY_UNKNOWN;
  66. if (iint->flags & IMA_APPRAISED)
  67. return iint->ima_status;
  68. rc = inode->i_op->getxattr(dentry, XATTR_NAME_IMA, xattr_value,
  69. IMA_DIGEST_SIZE);
  70. if (rc <= 0) {
  71. if (rc && rc != -ENODATA)
  72. goto out;
  73. cause = "missing-hash";
  74. status =
  75. (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
  76. goto out;
  77. }
  78. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
  79. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  80. if ((status == INTEGRITY_NOLABEL)
  81. || (status == INTEGRITY_NOXATTRS))
  82. cause = "missing-HMAC";
  83. else if (status == INTEGRITY_FAIL)
  84. cause = "invalid-HMAC";
  85. goto out;
  86. }
  87. rc = memcmp(xattr_value, iint->digest, IMA_DIGEST_SIZE);
  88. if (rc) {
  89. status = INTEGRITY_FAIL;
  90. cause = "invalid-hash";
  91. print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
  92. xattr_value, IMA_DIGEST_SIZE);
  93. print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
  94. iint->digest, IMA_DIGEST_SIZE);
  95. goto out;
  96. }
  97. status = INTEGRITY_PASS;
  98. iint->flags |= IMA_APPRAISED;
  99. out:
  100. if (status != INTEGRITY_PASS) {
  101. if (ima_appraise & IMA_APPRAISE_FIX) {
  102. ima_fix_xattr(dentry, iint);
  103. status = INTEGRITY_PASS;
  104. }
  105. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  106. op, cause, rc, 0);
  107. }
  108. iint->ima_status = status;
  109. return status;
  110. }
  111. /*
  112. * ima_update_xattr - update 'security.ima' hash value
  113. */
  114. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  115. {
  116. struct dentry *dentry = file->f_dentry;
  117. int rc = 0;
  118. rc = ima_collect_measurement(iint, file);
  119. if (rc < 0)
  120. return;
  121. ima_fix_xattr(dentry, iint);
  122. }
  123. /**
  124. * ima_inode_post_setattr - reflect file metadata changes
  125. * @dentry: pointer to the affected dentry
  126. *
  127. * Changes to a dentry's metadata might result in needing to appraise.
  128. *
  129. * This function is called from notify_change(), which expects the caller
  130. * to lock the inode's i_mutex.
  131. */
  132. void ima_inode_post_setattr(struct dentry *dentry)
  133. {
  134. struct inode *inode = dentry->d_inode;
  135. struct integrity_iint_cache *iint;
  136. int must_appraise, rc;
  137. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  138. || !inode->i_op->removexattr)
  139. return;
  140. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  141. iint = integrity_iint_find(inode);
  142. if (iint) {
  143. if (must_appraise)
  144. iint->flags |= IMA_APPRAISE;
  145. else
  146. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
  147. }
  148. if (!must_appraise)
  149. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  150. return;
  151. }