ima_appraise.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_hash->type = IMA_XATTR_DIGEST;
  43. return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
  44. &iint->ima_hash->type,
  45. 1 + iint->ima_hash->length, 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. void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
  101. struct ima_digest_data *hash)
  102. {
  103. struct signature_v2_hdr *sig;
  104. if (!xattr_value || xattr_len < 0 || xattr_len <= 1 + sizeof(*sig))
  105. return;
  106. sig = (typeof(sig)) xattr_value->digest;
  107. if (xattr_value->type != EVM_IMA_XATTR_DIGSIG || sig->version != 2)
  108. return;
  109. hash->algo = sig->hash_algo;
  110. }
  111. int ima_read_xattr(struct dentry *dentry,
  112. struct evm_ima_xattr_data **xattr_value)
  113. {
  114. struct inode *inode = dentry->d_inode;
  115. if (!inode->i_op->getxattr)
  116. return 0;
  117. return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
  118. 0, GFP_NOFS);
  119. }
  120. /*
  121. * ima_appraise_measurement - appraise file measurement
  122. *
  123. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  124. * Assuming success, compare the xattr hash with the collected measurement.
  125. *
  126. * Return 0 on success, error code otherwise
  127. */
  128. int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
  129. struct file *file, const unsigned char *filename,
  130. struct evm_ima_xattr_data *xattr_value,
  131. int xattr_len)
  132. {
  133. struct dentry *dentry = file->f_dentry;
  134. struct inode *inode = dentry->d_inode;
  135. enum integrity_status status = INTEGRITY_UNKNOWN;
  136. const char *op = "appraise_data";
  137. char *cause = "unknown";
  138. int rc = xattr_len;
  139. if (!ima_appraise)
  140. return 0;
  141. if (!inode->i_op->getxattr)
  142. return INTEGRITY_UNKNOWN;
  143. if (rc <= 0) {
  144. if (rc && rc != -ENODATA)
  145. goto out;
  146. cause = "missing-hash";
  147. status =
  148. (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
  149. goto out;
  150. }
  151. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
  152. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  153. if ((status == INTEGRITY_NOLABEL)
  154. || (status == INTEGRITY_NOXATTRS))
  155. cause = "missing-HMAC";
  156. else if (status == INTEGRITY_FAIL)
  157. cause = "invalid-HMAC";
  158. goto out;
  159. }
  160. switch (xattr_value->type) {
  161. case IMA_XATTR_DIGEST:
  162. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  163. cause = "IMA signature required";
  164. status = INTEGRITY_FAIL;
  165. break;
  166. }
  167. if (xattr_len - 1 >= iint->ima_hash->length)
  168. /* xattr length may be longer. md5 hash in previous
  169. version occupied 20 bytes in xattr, instead of 16
  170. */
  171. rc = memcmp(xattr_value->digest,
  172. iint->ima_hash->digest,
  173. iint->ima_hash->length);
  174. else
  175. rc = -EINVAL;
  176. if (rc) {
  177. cause = "invalid-hash";
  178. status = INTEGRITY_FAIL;
  179. break;
  180. }
  181. status = INTEGRITY_PASS;
  182. break;
  183. case EVM_IMA_XATTR_DIGSIG:
  184. iint->flags |= IMA_DIGSIG;
  185. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  186. (const char *)xattr_value, rc,
  187. iint->ima_hash->digest,
  188. iint->ima_hash->length);
  189. if (rc == -EOPNOTSUPP) {
  190. status = INTEGRITY_UNKNOWN;
  191. } else if (rc) {
  192. cause = "invalid-signature";
  193. status = INTEGRITY_FAIL;
  194. } else {
  195. status = INTEGRITY_PASS;
  196. }
  197. break;
  198. default:
  199. status = INTEGRITY_UNKNOWN;
  200. cause = "unknown-ima-data";
  201. break;
  202. }
  203. out:
  204. if (status != INTEGRITY_PASS) {
  205. if ((ima_appraise & IMA_APPRAISE_FIX) &&
  206. (!xattr_value ||
  207. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  208. if (!ima_fix_xattr(dentry, iint))
  209. status = INTEGRITY_PASS;
  210. }
  211. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  212. op, cause, rc, 0);
  213. } else {
  214. ima_cache_flags(iint, func);
  215. }
  216. ima_set_cache_status(iint, func, status);
  217. return status;
  218. }
  219. /*
  220. * ima_update_xattr - update 'security.ima' hash value
  221. */
  222. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  223. {
  224. struct dentry *dentry = file->f_dentry;
  225. int rc = 0;
  226. /* do not collect and update hash for digital signatures */
  227. if (iint->flags & IMA_DIGSIG)
  228. return;
  229. rc = ima_collect_measurement(iint, file, NULL, NULL);
  230. if (rc < 0)
  231. return;
  232. ima_fix_xattr(dentry, iint);
  233. }
  234. /**
  235. * ima_inode_post_setattr - reflect file metadata changes
  236. * @dentry: pointer to the affected dentry
  237. *
  238. * Changes to a dentry's metadata might result in needing to appraise.
  239. *
  240. * This function is called from notify_change(), which expects the caller
  241. * to lock the inode's i_mutex.
  242. */
  243. void ima_inode_post_setattr(struct dentry *dentry)
  244. {
  245. struct inode *inode = dentry->d_inode;
  246. struct integrity_iint_cache *iint;
  247. int must_appraise, rc;
  248. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  249. || !inode->i_op->removexattr)
  250. return;
  251. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  252. iint = integrity_iint_find(inode);
  253. if (iint) {
  254. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
  255. IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
  256. IMA_ACTION_FLAGS);
  257. if (must_appraise)
  258. iint->flags |= IMA_APPRAISE;
  259. }
  260. if (!must_appraise)
  261. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  262. return;
  263. }
  264. /*
  265. * ima_protect_xattr - protect 'security.ima'
  266. *
  267. * Ensure that not just anyone can modify or remove 'security.ima'.
  268. */
  269. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  270. const void *xattr_value, size_t xattr_value_len)
  271. {
  272. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  273. if (!capable(CAP_SYS_ADMIN))
  274. return -EPERM;
  275. return 1;
  276. }
  277. return 0;
  278. }
  279. static void ima_reset_appraise_flags(struct inode *inode)
  280. {
  281. struct integrity_iint_cache *iint;
  282. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
  283. return;
  284. iint = integrity_iint_find(inode);
  285. if (!iint)
  286. return;
  287. iint->flags &= ~IMA_DONE_MASK;
  288. return;
  289. }
  290. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  291. const void *xattr_value, size_t xattr_value_len)
  292. {
  293. int result;
  294. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  295. xattr_value_len);
  296. if (result == 1) {
  297. ima_reset_appraise_flags(dentry->d_inode);
  298. result = 0;
  299. }
  300. return result;
  301. }
  302. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  303. {
  304. int result;
  305. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  306. if (result == 1) {
  307. ima_reset_appraise_flags(dentry->d_inode);
  308. result = 0;
  309. }
  310. return result;
  311. }