ima_appraise.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  93. cause = "IMA signature required";
  94. status = INTEGRITY_FAIL;
  95. break;
  96. }
  97. rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
  98. IMA_DIGEST_SIZE);
  99. if (rc) {
  100. cause = "invalid-hash";
  101. status = INTEGRITY_FAIL;
  102. break;
  103. }
  104. status = INTEGRITY_PASS;
  105. break;
  106. case EVM_IMA_XATTR_DIGSIG:
  107. iint->flags |= IMA_DIGSIG;
  108. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  109. xattr_value->digest, rc - 1,
  110. iint->ima_xattr.digest,
  111. IMA_DIGEST_SIZE);
  112. if (rc == -EOPNOTSUPP) {
  113. status = INTEGRITY_UNKNOWN;
  114. } else if (rc) {
  115. cause = "invalid-signature";
  116. status = INTEGRITY_FAIL;
  117. } else {
  118. status = INTEGRITY_PASS;
  119. }
  120. break;
  121. default:
  122. status = INTEGRITY_UNKNOWN;
  123. cause = "unknown-ima-data";
  124. break;
  125. }
  126. out:
  127. if (status != INTEGRITY_PASS) {
  128. if ((ima_appraise & IMA_APPRAISE_FIX) &&
  129. (!xattr_value ||
  130. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  131. if (!ima_fix_xattr(dentry, iint))
  132. status = INTEGRITY_PASS;
  133. }
  134. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  135. op, cause, rc, 0);
  136. } else {
  137. iint->flags |= IMA_APPRAISED;
  138. }
  139. iint->ima_status = status;
  140. kfree(xattr_value);
  141. return status;
  142. }
  143. /*
  144. * ima_update_xattr - update 'security.ima' hash value
  145. */
  146. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  147. {
  148. struct dentry *dentry = file->f_dentry;
  149. int rc = 0;
  150. /* do not collect and update hash for digital signatures */
  151. if (iint->flags & IMA_DIGSIG)
  152. return;
  153. rc = ima_collect_measurement(iint, file);
  154. if (rc < 0)
  155. return;
  156. ima_fix_xattr(dentry, iint);
  157. }
  158. /**
  159. * ima_inode_post_setattr - reflect file metadata changes
  160. * @dentry: pointer to the affected dentry
  161. *
  162. * Changes to a dentry's metadata might result in needing to appraise.
  163. *
  164. * This function is called from notify_change(), which expects the caller
  165. * to lock the inode's i_mutex.
  166. */
  167. void ima_inode_post_setattr(struct dentry *dentry)
  168. {
  169. struct inode *inode = dentry->d_inode;
  170. struct integrity_iint_cache *iint;
  171. int must_appraise, rc;
  172. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  173. || !inode->i_op->removexattr)
  174. return;
  175. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  176. iint = integrity_iint_find(inode);
  177. if (iint) {
  178. if (must_appraise)
  179. iint->flags |= IMA_APPRAISE;
  180. else
  181. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
  182. }
  183. if (!must_appraise)
  184. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  185. return;
  186. }
  187. /*
  188. * ima_protect_xattr - protect 'security.ima'
  189. *
  190. * Ensure that not just anyone can modify or remove 'security.ima'.
  191. */
  192. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  193. const void *xattr_value, size_t xattr_value_len)
  194. {
  195. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  196. if (!capable(CAP_SYS_ADMIN))
  197. return -EPERM;
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. static void ima_reset_appraise_flags(struct inode *inode)
  203. {
  204. struct integrity_iint_cache *iint;
  205. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
  206. return;
  207. iint = integrity_iint_find(inode);
  208. if (!iint)
  209. return;
  210. iint->flags &= ~IMA_DONE_MASK;
  211. return;
  212. }
  213. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  214. const void *xattr_value, size_t xattr_value_len)
  215. {
  216. int result;
  217. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  218. xattr_value_len);
  219. if (result == 1) {
  220. ima_reset_appraise_flags(dentry->d_inode);
  221. result = 0;
  222. }
  223. return result;
  224. }
  225. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  226. {
  227. int result;
  228. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  229. if (result == 1) {
  230. ima_reset_appraise_flags(dentry->d_inode);
  231. result = 0;
  232. }
  233. return result;
  234. }