ima_appraise.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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->ima_xattr.type = IMA_XATTR_DIGEST;
  43. __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA, (u8 *)&iint->ima_xattr,
  44. sizeof iint->ima_xattr, 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. struct evm_ima_xattr_data xattr_value;
  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, (u8 *)&xattr_value,
  71. sizeof xattr_value);
  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, (u8 *)&xattr_value,
  81. rc, iint);
  82. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  83. if ((status == INTEGRITY_NOLABEL)
  84. || (status == INTEGRITY_NOXATTRS))
  85. cause = "missing-HMAC";
  86. else if (status == INTEGRITY_FAIL)
  87. cause = "invalid-HMAC";
  88. goto out;
  89. }
  90. rc = memcmp(xattr_value.digest, iint->ima_xattr.digest,
  91. IMA_DIGEST_SIZE);
  92. if (rc) {
  93. status = INTEGRITY_FAIL;
  94. cause = "invalid-hash";
  95. print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
  96. &xattr_value, sizeof xattr_value);
  97. print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
  98. (u8 *)&iint->ima_xattr,
  99. sizeof iint->ima_xattr);
  100. goto out;
  101. }
  102. status = INTEGRITY_PASS;
  103. iint->flags |= IMA_APPRAISED;
  104. out:
  105. if (status != INTEGRITY_PASS) {
  106. if (ima_appraise & IMA_APPRAISE_FIX) {
  107. ima_fix_xattr(dentry, iint);
  108. status = INTEGRITY_PASS;
  109. }
  110. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  111. op, cause, rc, 0);
  112. }
  113. iint->ima_status = status;
  114. return status;
  115. }
  116. /*
  117. * ima_update_xattr - update 'security.ima' hash value
  118. */
  119. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  120. {
  121. struct dentry *dentry = file->f_dentry;
  122. int rc = 0;
  123. rc = ima_collect_measurement(iint, file);
  124. if (rc < 0)
  125. return;
  126. ima_fix_xattr(dentry, iint);
  127. }
  128. /**
  129. * ima_inode_post_setattr - reflect file metadata changes
  130. * @dentry: pointer to the affected dentry
  131. *
  132. * Changes to a dentry's metadata might result in needing to appraise.
  133. *
  134. * This function is called from notify_change(), which expects the caller
  135. * to lock the inode's i_mutex.
  136. */
  137. void ima_inode_post_setattr(struct dentry *dentry)
  138. {
  139. struct inode *inode = dentry->d_inode;
  140. struct integrity_iint_cache *iint;
  141. int must_appraise, rc;
  142. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  143. || !inode->i_op->removexattr)
  144. return;
  145. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  146. iint = integrity_iint_find(inode);
  147. if (iint) {
  148. if (must_appraise)
  149. iint->flags |= IMA_APPRAISE;
  150. else
  151. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
  152. }
  153. if (!must_appraise)
  154. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  155. return;
  156. }
  157. /*
  158. * ima_protect_xattr - protect 'security.ima'
  159. *
  160. * Ensure that not just anyone can modify or remove 'security.ima'.
  161. */
  162. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  163. const void *xattr_value, size_t xattr_value_len)
  164. {
  165. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  166. if (!capable(CAP_SYS_ADMIN))
  167. return -EPERM;
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static void ima_reset_appraise_flags(struct inode *inode)
  173. {
  174. struct integrity_iint_cache *iint;
  175. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
  176. return;
  177. iint = integrity_iint_find(inode);
  178. if (!iint)
  179. return;
  180. iint->flags &= ~(IMA_COLLECTED | IMA_APPRAISED | IMA_MEASURED);
  181. return;
  182. }
  183. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  184. const void *xattr_value, size_t xattr_value_len)
  185. {
  186. int result;
  187. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  188. xattr_value_len);
  189. if (result == 1) {
  190. ima_reset_appraise_flags(dentry->d_inode);
  191. result = 0;
  192. }
  193. return result;
  194. }
  195. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  196. {
  197. int result;
  198. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  199. if (result == 1) {
  200. ima_reset_appraise_flags(dentry->d_inode);
  201. result = 0;
  202. }
  203. return result;
  204. }