ima_appraise.c 12 KB

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