ima_appraise.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. /* Return specific func appraised cached result */
  48. enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
  49. int func)
  50. {
  51. switch(func) {
  52. case MMAP_CHECK:
  53. return iint->ima_mmap_status;
  54. case BPRM_CHECK:
  55. return iint->ima_bprm_status;
  56. case MODULE_CHECK:
  57. return iint->ima_module_status;
  58. case FILE_CHECK:
  59. default:
  60. return iint->ima_file_status;
  61. }
  62. }
  63. static void ima_set_cache_status(struct integrity_iint_cache *iint,
  64. int func, enum integrity_status status)
  65. {
  66. switch(func) {
  67. case MMAP_CHECK:
  68. iint->ima_mmap_status = status;
  69. break;
  70. case BPRM_CHECK:
  71. iint->ima_bprm_status = status;
  72. break;
  73. case MODULE_CHECK:
  74. iint->ima_module_status = status;
  75. break;
  76. case FILE_CHECK:
  77. default:
  78. iint->ima_file_status = status;
  79. break;
  80. }
  81. }
  82. static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
  83. {
  84. switch(func) {
  85. case MMAP_CHECK:
  86. iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
  87. break;
  88. case BPRM_CHECK:
  89. iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
  90. break;
  91. case MODULE_CHECK:
  92. iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
  93. break;
  94. case FILE_CHECK:
  95. default:
  96. iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
  97. break;
  98. }
  99. }
  100. /*
  101. * ima_appraise_measurement - appraise file measurement
  102. *
  103. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  104. * Assuming success, compare the xattr hash with the collected measurement.
  105. *
  106. * Return 0 on success, error code otherwise
  107. */
  108. int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
  109. struct file *file, const unsigned char *filename)
  110. {
  111. struct dentry *dentry = file->f_dentry;
  112. struct inode *inode = dentry->d_inode;
  113. struct evm_ima_xattr_data *xattr_value = NULL;
  114. enum integrity_status status = INTEGRITY_UNKNOWN;
  115. const char *op = "appraise_data";
  116. char *cause = "unknown";
  117. int rc;
  118. if (!ima_appraise)
  119. return 0;
  120. if (!inode->i_op->getxattr)
  121. return INTEGRITY_UNKNOWN;
  122. rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
  123. 0, GFP_NOFS);
  124. if (rc <= 0) {
  125. if (rc && rc != -ENODATA)
  126. goto out;
  127. cause = "missing-hash";
  128. status =
  129. (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
  130. goto out;
  131. }
  132. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
  133. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  134. if ((status == INTEGRITY_NOLABEL)
  135. || (status == INTEGRITY_NOXATTRS))
  136. cause = "missing-HMAC";
  137. else if (status == INTEGRITY_FAIL)
  138. cause = "invalid-HMAC";
  139. goto out;
  140. }
  141. switch (xattr_value->type) {
  142. case IMA_XATTR_DIGEST:
  143. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  144. cause = "IMA signature required";
  145. status = INTEGRITY_FAIL;
  146. break;
  147. }
  148. rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
  149. IMA_DIGEST_SIZE);
  150. if (rc) {
  151. cause = "invalid-hash";
  152. status = INTEGRITY_FAIL;
  153. break;
  154. }
  155. status = INTEGRITY_PASS;
  156. break;
  157. case EVM_IMA_XATTR_DIGSIG:
  158. iint->flags |= IMA_DIGSIG;
  159. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  160. xattr_value->digest, rc - 1,
  161. iint->ima_xattr.digest,
  162. IMA_DIGEST_SIZE);
  163. if (rc == -EOPNOTSUPP) {
  164. status = INTEGRITY_UNKNOWN;
  165. } else if (rc) {
  166. cause = "invalid-signature";
  167. status = INTEGRITY_FAIL;
  168. } else {
  169. status = INTEGRITY_PASS;
  170. }
  171. break;
  172. default:
  173. status = INTEGRITY_UNKNOWN;
  174. cause = "unknown-ima-data";
  175. break;
  176. }
  177. out:
  178. if (status != INTEGRITY_PASS) {
  179. if ((ima_appraise & IMA_APPRAISE_FIX) &&
  180. (!xattr_value ||
  181. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  182. if (!ima_fix_xattr(dentry, iint))
  183. status = INTEGRITY_PASS;
  184. }
  185. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  186. op, cause, rc, 0);
  187. } else {
  188. ima_cache_flags(iint, func);
  189. }
  190. ima_set_cache_status(iint, func, status);
  191. kfree(xattr_value);
  192. return status;
  193. }
  194. /*
  195. * ima_update_xattr - update 'security.ima' hash value
  196. */
  197. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  198. {
  199. struct dentry *dentry = file->f_dentry;
  200. int rc = 0;
  201. /* do not collect and update hash for digital signatures */
  202. if (iint->flags & IMA_DIGSIG)
  203. return;
  204. rc = ima_collect_measurement(iint, file);
  205. if (rc < 0)
  206. return;
  207. ima_fix_xattr(dentry, iint);
  208. }
  209. /**
  210. * ima_inode_post_setattr - reflect file metadata changes
  211. * @dentry: pointer to the affected dentry
  212. *
  213. * Changes to a dentry's metadata might result in needing to appraise.
  214. *
  215. * This function is called from notify_change(), which expects the caller
  216. * to lock the inode's i_mutex.
  217. */
  218. void ima_inode_post_setattr(struct dentry *dentry)
  219. {
  220. struct inode *inode = dentry->d_inode;
  221. struct integrity_iint_cache *iint;
  222. int must_appraise, rc;
  223. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  224. || !inode->i_op->removexattr)
  225. return;
  226. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  227. iint = integrity_iint_find(inode);
  228. if (iint) {
  229. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
  230. IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
  231. IMA_ACTION_FLAGS);
  232. if (must_appraise)
  233. iint->flags |= IMA_APPRAISE;
  234. }
  235. if (!must_appraise)
  236. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  237. return;
  238. }
  239. /*
  240. * ima_protect_xattr - protect 'security.ima'
  241. *
  242. * Ensure that not just anyone can modify or remove 'security.ima'.
  243. */
  244. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  245. const void *xattr_value, size_t xattr_value_len)
  246. {
  247. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  248. if (!capable(CAP_SYS_ADMIN))
  249. return -EPERM;
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. static void ima_reset_appraise_flags(struct inode *inode)
  255. {
  256. struct integrity_iint_cache *iint;
  257. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
  258. return;
  259. iint = integrity_iint_find(inode);
  260. if (!iint)
  261. return;
  262. iint->flags &= ~IMA_DONE_MASK;
  263. return;
  264. }
  265. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  266. const void *xattr_value, size_t xattr_value_len)
  267. {
  268. int result;
  269. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  270. xattr_value_len);
  271. if (result == 1) {
  272. ima_reset_appraise_flags(dentry->d_inode);
  273. result = 0;
  274. }
  275. return result;
  276. }
  277. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  278. {
  279. int result;
  280. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  281. if (result == 1) {
  282. ima_reset_appraise_flags(dentry->d_inode);
  283. result = 0;
  284. }
  285. return result;
  286. }