ima_appraise.c 6.4 KB

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