ima_appraise.c 9.3 KB

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