ima_appraise.c 11 KB

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