ima_appraise.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <crypto/hash_info.h>
  19. #include "ima.h"
  20. static int __init default_appraise_setup(char *str)
  21. {
  22. if (strncmp(str, "off", 3) == 0)
  23. ima_appraise = 0;
  24. else if (strncmp(str, "log", 3) == 0)
  25. ima_appraise = IMA_APPRAISE_LOG;
  26. else if (strncmp(str, "fix", 3) == 0)
  27. ima_appraise = IMA_APPRAISE_FIX;
  28. return 1;
  29. }
  30. __setup("ima_appraise=", default_appraise_setup);
  31. /*
  32. * ima_must_appraise - set appraise flag
  33. *
  34. * Return 1 to appraise
  35. */
  36. int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
  37. {
  38. if (!ima_appraise)
  39. return 0;
  40. return ima_match_policy(inode, func, mask, IMA_APPRAISE);
  41. }
  42. static int ima_fix_xattr(struct dentry *dentry,
  43. struct integrity_iint_cache *iint)
  44. {
  45. int rc, offset;
  46. u8 algo = iint->ima_hash->algo;
  47. if (algo <= HASH_ALGO_SHA1) {
  48. offset = 1;
  49. iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
  50. } else {
  51. offset = 0;
  52. iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
  53. iint->ima_hash->xattr.ng.algo = algo;
  54. }
  55. rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
  56. &iint->ima_hash->xattr.data[offset],
  57. (sizeof(iint->ima_hash->xattr) - offset) +
  58. iint->ima_hash->length, 0);
  59. return rc;
  60. }
  61. /* Return specific func appraised cached result */
  62. enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
  63. int func)
  64. {
  65. switch (func) {
  66. case MMAP_CHECK:
  67. return iint->ima_mmap_status;
  68. case BPRM_CHECK:
  69. return iint->ima_bprm_status;
  70. case MODULE_CHECK:
  71. return iint->ima_module_status;
  72. case FIRMWARE_CHECK:
  73. return iint->ima_firmware_status;
  74. case FILE_CHECK:
  75. default:
  76. return iint->ima_file_status;
  77. }
  78. }
  79. static void ima_set_cache_status(struct integrity_iint_cache *iint,
  80. int func, enum integrity_status status)
  81. {
  82. switch (func) {
  83. case MMAP_CHECK:
  84. iint->ima_mmap_status = status;
  85. break;
  86. case BPRM_CHECK:
  87. iint->ima_bprm_status = status;
  88. break;
  89. case MODULE_CHECK:
  90. iint->ima_module_status = status;
  91. break;
  92. case FIRMWARE_CHECK:
  93. iint->ima_firmware_status = status;
  94. break;
  95. case FILE_CHECK:
  96. default:
  97. iint->ima_file_status = status;
  98. break;
  99. }
  100. }
  101. static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
  102. {
  103. switch (func) {
  104. case MMAP_CHECK:
  105. iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
  106. break;
  107. case BPRM_CHECK:
  108. iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
  109. break;
  110. case MODULE_CHECK:
  111. iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
  112. break;
  113. case FIRMWARE_CHECK:
  114. iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
  115. break;
  116. case FILE_CHECK:
  117. default:
  118. iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
  119. break;
  120. }
  121. }
  122. void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
  123. struct ima_digest_data *hash)
  124. {
  125. struct signature_v2_hdr *sig;
  126. if (!xattr_value || xattr_len < 2)
  127. return;
  128. switch (xattr_value->type) {
  129. case EVM_IMA_XATTR_DIGSIG:
  130. sig = (typeof(sig))xattr_value;
  131. if (sig->version != 2 || xattr_len <= sizeof(*sig))
  132. return;
  133. hash->algo = sig->hash_algo;
  134. break;
  135. case IMA_XATTR_DIGEST_NG:
  136. hash->algo = xattr_value->digest[0];
  137. break;
  138. case IMA_XATTR_DIGEST:
  139. /* this is for backward compatibility */
  140. if (xattr_len == 21) {
  141. unsigned int zero = 0;
  142. if (!memcmp(&xattr_value->digest[16], &zero, 4))
  143. hash->algo = HASH_ALGO_MD5;
  144. else
  145. hash->algo = HASH_ALGO_SHA1;
  146. } else if (xattr_len == 17)
  147. hash->algo = HASH_ALGO_MD5;
  148. break;
  149. }
  150. }
  151. int ima_read_xattr(struct dentry *dentry,
  152. struct evm_ima_xattr_data **xattr_value)
  153. {
  154. struct inode *inode = d_backing_inode(dentry);
  155. if (!inode->i_op->getxattr)
  156. return 0;
  157. return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
  158. 0, GFP_NOFS);
  159. }
  160. /*
  161. * ima_appraise_measurement - appraise file measurement
  162. *
  163. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  164. * Assuming success, compare the xattr hash with the collected measurement.
  165. *
  166. * Return 0 on success, error code otherwise
  167. */
  168. int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
  169. struct file *file, const unsigned char *filename,
  170. struct evm_ima_xattr_data *xattr_value,
  171. int xattr_len, int opened)
  172. {
  173. static const char op[] = "appraise_data";
  174. char *cause = "unknown";
  175. struct dentry *dentry = file_dentry(file);
  176. struct inode *inode = d_backing_inode(dentry);
  177. enum integrity_status status = INTEGRITY_UNKNOWN;
  178. int rc = xattr_len, hash_start = 0;
  179. if (!inode->i_op->getxattr)
  180. return INTEGRITY_UNKNOWN;
  181. if (rc <= 0) {
  182. if (rc && rc != -ENODATA)
  183. goto out;
  184. cause = "missing-hash";
  185. status = INTEGRITY_NOLABEL;
  186. if (opened & FILE_CREATED)
  187. iint->flags |= IMA_NEW_FILE;
  188. if ((iint->flags & IMA_NEW_FILE) &&
  189. (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
  190. (inode->i_size == 0)))
  191. status = INTEGRITY_PASS;
  192. goto out;
  193. }
  194. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
  195. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  196. if ((status == INTEGRITY_NOLABEL)
  197. || (status == INTEGRITY_NOXATTRS))
  198. cause = "missing-HMAC";
  199. else if (status == INTEGRITY_FAIL)
  200. cause = "invalid-HMAC";
  201. goto out;
  202. }
  203. switch (xattr_value->type) {
  204. case IMA_XATTR_DIGEST_NG:
  205. /* first byte contains algorithm id */
  206. hash_start = 1;
  207. case IMA_XATTR_DIGEST:
  208. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  209. cause = "IMA-signature-required";
  210. status = INTEGRITY_FAIL;
  211. break;
  212. }
  213. if (xattr_len - sizeof(xattr_value->type) - hash_start >=
  214. iint->ima_hash->length)
  215. /* xattr length may be longer. md5 hash in previous
  216. version occupied 20 bytes in xattr, instead of 16
  217. */
  218. rc = memcmp(&xattr_value->digest[hash_start],
  219. iint->ima_hash->digest,
  220. iint->ima_hash->length);
  221. else
  222. rc = -EINVAL;
  223. if (rc) {
  224. cause = "invalid-hash";
  225. status = INTEGRITY_FAIL;
  226. break;
  227. }
  228. status = INTEGRITY_PASS;
  229. break;
  230. case EVM_IMA_XATTR_DIGSIG:
  231. iint->flags |= IMA_DIGSIG;
  232. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  233. (const char *)xattr_value, rc,
  234. iint->ima_hash->digest,
  235. iint->ima_hash->length);
  236. if (rc == -EOPNOTSUPP) {
  237. status = INTEGRITY_UNKNOWN;
  238. } else if (rc) {
  239. cause = "invalid-signature";
  240. status = INTEGRITY_FAIL;
  241. } else {
  242. status = INTEGRITY_PASS;
  243. }
  244. break;
  245. default:
  246. status = INTEGRITY_UNKNOWN;
  247. cause = "unknown-ima-data";
  248. break;
  249. }
  250. out:
  251. if (status != INTEGRITY_PASS) {
  252. if ((ima_appraise & IMA_APPRAISE_FIX) &&
  253. (!xattr_value ||
  254. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  255. if (!ima_fix_xattr(dentry, iint))
  256. status = INTEGRITY_PASS;
  257. }
  258. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  259. op, cause, rc, 0);
  260. } else {
  261. ima_cache_flags(iint, func);
  262. }
  263. ima_set_cache_status(iint, func, status);
  264. return status;
  265. }
  266. /*
  267. * ima_update_xattr - update 'security.ima' hash value
  268. */
  269. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  270. {
  271. struct dentry *dentry = file_dentry(file);
  272. int rc = 0;
  273. /* do not collect and update hash for digital signatures */
  274. if (iint->flags & IMA_DIGSIG)
  275. return;
  276. if (iint->ima_file_status != INTEGRITY_PASS)
  277. return;
  278. rc = ima_collect_measurement(iint, file, NULL, NULL);
  279. if (rc < 0)
  280. return;
  281. ima_fix_xattr(dentry, iint);
  282. }
  283. /**
  284. * ima_inode_post_setattr - reflect file metadata changes
  285. * @dentry: pointer to the affected dentry
  286. *
  287. * Changes to a dentry's metadata might result in needing to appraise.
  288. *
  289. * This function is called from notify_change(), which expects the caller
  290. * to lock the inode's i_mutex.
  291. */
  292. void ima_inode_post_setattr(struct dentry *dentry)
  293. {
  294. struct inode *inode = d_backing_inode(dentry);
  295. struct integrity_iint_cache *iint;
  296. int must_appraise, rc;
  297. if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
  298. || !inode->i_op->removexattr)
  299. return;
  300. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  301. iint = integrity_iint_find(inode);
  302. if (iint) {
  303. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
  304. IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
  305. IMA_ACTION_FLAGS);
  306. if (must_appraise)
  307. iint->flags |= IMA_APPRAISE;
  308. }
  309. if (!must_appraise)
  310. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  311. return;
  312. }
  313. /*
  314. * ima_protect_xattr - protect 'security.ima'
  315. *
  316. * Ensure that not just anyone can modify or remove 'security.ima'.
  317. */
  318. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  319. const void *xattr_value, size_t xattr_value_len)
  320. {
  321. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  322. if (!capable(CAP_SYS_ADMIN))
  323. return -EPERM;
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. static void ima_reset_appraise_flags(struct inode *inode, int digsig)
  329. {
  330. struct integrity_iint_cache *iint;
  331. if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
  332. return;
  333. iint = integrity_iint_find(inode);
  334. if (!iint)
  335. return;
  336. iint->flags &= ~IMA_DONE_MASK;
  337. if (digsig)
  338. iint->flags |= IMA_DIGSIG;
  339. return;
  340. }
  341. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  342. const void *xattr_value, size_t xattr_value_len)
  343. {
  344. const struct evm_ima_xattr_data *xvalue = xattr_value;
  345. int result;
  346. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  347. xattr_value_len);
  348. if (result == 1) {
  349. if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
  350. return -EINVAL;
  351. ima_reset_appraise_flags(d_backing_inode(dentry),
  352. (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
  353. result = 0;
  354. }
  355. return result;
  356. }
  357. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  358. {
  359. int result;
  360. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  361. if (result == 1) {
  362. ima_reset_appraise_flags(d_backing_inode(dentry), 0);
  363. result = 0;
  364. }
  365. return result;
  366. }