ima_appraise.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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, int mask, enum ima_hooks func)
  34. {
  35. if (!ima_appraise)
  36. return 0;
  37. return ima_match_policy(inode, func, mask, IMA_APPRAISE);
  38. }
  39. static int ima_fix_xattr(struct dentry *dentry,
  40. struct integrity_iint_cache *iint)
  41. {
  42. iint->ima_xattr.type = IMA_XATTR_DIGEST;
  43. return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
  44. (u8 *)&iint->ima_xattr,
  45. sizeof(iint->ima_xattr), 0);
  46. }
  47. /*
  48. * ima_appraise_measurement - appraise file measurement
  49. *
  50. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  51. * Assuming success, compare the xattr hash with the collected measurement.
  52. *
  53. * Return 0 on success, error code otherwise
  54. */
  55. int ima_appraise_measurement(struct integrity_iint_cache *iint,
  56. struct file *file, const unsigned char *filename)
  57. {
  58. struct dentry *dentry = file->f_dentry;
  59. struct inode *inode = dentry->d_inode;
  60. struct evm_ima_xattr_data *xattr_value = NULL;
  61. enum integrity_status status = INTEGRITY_UNKNOWN;
  62. const char *op = "appraise_data";
  63. char *cause = "unknown";
  64. int rc;
  65. if (!ima_appraise)
  66. return 0;
  67. if (!inode->i_op->getxattr)
  68. return INTEGRITY_UNKNOWN;
  69. if (iint->flags & IMA_APPRAISED)
  70. return iint->ima_status;
  71. rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
  72. 0, GFP_NOFS);
  73. if (rc <= 0) {
  74. if (rc && rc != -ENODATA)
  75. goto out;
  76. cause = "missing-hash";
  77. status =
  78. (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
  79. goto out;
  80. }
  81. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, 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. switch (xattr_value->type) {
  91. case IMA_XATTR_DIGEST:
  92. rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
  93. IMA_DIGEST_SIZE);
  94. if (rc) {
  95. cause = "invalid-hash";
  96. status = INTEGRITY_FAIL;
  97. break;
  98. }
  99. status = INTEGRITY_PASS;
  100. break;
  101. case EVM_IMA_XATTR_DIGSIG:
  102. iint->flags |= IMA_DIGSIG;
  103. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  104. xattr_value->digest, rc - 1,
  105. iint->ima_xattr.digest,
  106. IMA_DIGEST_SIZE);
  107. if (rc == -EOPNOTSUPP) {
  108. status = INTEGRITY_UNKNOWN;
  109. } else if (rc) {
  110. cause = "invalid-signature";
  111. status = INTEGRITY_FAIL;
  112. } else {
  113. status = INTEGRITY_PASS;
  114. }
  115. break;
  116. default:
  117. status = INTEGRITY_UNKNOWN;
  118. cause = "unknown-ima-data";
  119. break;
  120. }
  121. out:
  122. if (status != INTEGRITY_PASS) {
  123. if ((ima_appraise & IMA_APPRAISE_FIX) &&
  124. (!xattr_value ||
  125. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  126. if (!ima_fix_xattr(dentry, iint))
  127. status = INTEGRITY_PASS;
  128. }
  129. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  130. op, cause, rc, 0);
  131. } else {
  132. iint->flags |= IMA_APPRAISED;
  133. }
  134. iint->ima_status = status;
  135. kfree(xattr_value);
  136. return status;
  137. }
  138. /*
  139. * ima_update_xattr - update 'security.ima' hash value
  140. */
  141. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  142. {
  143. struct dentry *dentry = file->f_dentry;
  144. int rc = 0;
  145. /* do not collect and update hash for digital signatures */
  146. if (iint->flags & IMA_DIGSIG)
  147. return;
  148. rc = ima_collect_measurement(iint, file);
  149. if (rc < 0)
  150. return;
  151. ima_fix_xattr(dentry, iint);
  152. }
  153. /**
  154. * ima_inode_post_setattr - reflect file metadata changes
  155. * @dentry: pointer to the affected dentry
  156. *
  157. * Changes to a dentry's metadata might result in needing to appraise.
  158. *
  159. * This function is called from notify_change(), which expects the caller
  160. * to lock the inode's i_mutex.
  161. */
  162. void ima_inode_post_setattr(struct dentry *dentry)
  163. {
  164. struct inode *inode = dentry->d_inode;
  165. struct integrity_iint_cache *iint;
  166. int must_appraise, rc;
  167. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  168. || !inode->i_op->removexattr)
  169. return;
  170. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  171. iint = integrity_iint_find(inode);
  172. if (iint) {
  173. if (must_appraise)
  174. iint->flags |= IMA_APPRAISE;
  175. else
  176. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
  177. }
  178. if (!must_appraise)
  179. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  180. return;
  181. }
  182. /*
  183. * ima_protect_xattr - protect 'security.ima'
  184. *
  185. * Ensure that not just anyone can modify or remove 'security.ima'.
  186. */
  187. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  188. const void *xattr_value, size_t xattr_value_len)
  189. {
  190. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  191. if (!capable(CAP_SYS_ADMIN))
  192. return -EPERM;
  193. return 1;
  194. }
  195. return 0;
  196. }
  197. static void ima_reset_appraise_flags(struct inode *inode)
  198. {
  199. struct integrity_iint_cache *iint;
  200. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
  201. return;
  202. iint = integrity_iint_find(inode);
  203. if (!iint)
  204. return;
  205. iint->flags &= ~IMA_DONE_MASK;
  206. return;
  207. }
  208. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  209. const void *xattr_value, size_t xattr_value_len)
  210. {
  211. int result;
  212. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  213. xattr_value_len);
  214. if (result == 1) {
  215. ima_reset_appraise_flags(dentry->d_inode);
  216. result = 0;
  217. }
  218. return result;
  219. }
  220. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  221. {
  222. int result;
  223. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  224. if (result == 1) {
  225. ima_reset_appraise_flags(dentry->d_inode);
  226. result = 0;
  227. }
  228. return result;
  229. }