ima_appraise.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 IBM Corporation
  4. *
  5. * Author:
  6. * Mimi Zohar <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/file.h>
  11. #include <linux/fs.h>
  12. #include <linux/xattr.h>
  13. #include <linux/magic.h>
  14. #include <linux/ima.h>
  15. #include <linux/evm.h>
  16. #include <linux/fsverity.h>
  17. #include <keys/system_keyring.h>
  18. #include <uapi/linux/fsverity.h>
  19. #include "ima.h"
  20. #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
  21. static char *ima_appraise_cmdline_default __initdata;
  22. core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0);
  23. void __init ima_appraise_parse_cmdline(void)
  24. {
  25. const char *str = ima_appraise_cmdline_default;
  26. bool sb_state = arch_ima_get_secureboot();
  27. int appraisal_state = ima_appraise;
  28. if (!str)
  29. return;
  30. if (strncmp(str, "off", 3) == 0)
  31. appraisal_state = 0;
  32. else if (strncmp(str, "log", 3) == 0)
  33. appraisal_state = IMA_APPRAISE_LOG;
  34. else if (strncmp(str, "fix", 3) == 0)
  35. appraisal_state = IMA_APPRAISE_FIX;
  36. else if (strncmp(str, "enforce", 7) == 0)
  37. appraisal_state = IMA_APPRAISE_ENFORCE;
  38. else
  39. pr_err("invalid \"%s\" appraise option", str);
  40. /* If appraisal state was changed, but secure boot is enabled,
  41. * keep its default */
  42. if (sb_state) {
  43. if (!(appraisal_state & IMA_APPRAISE_ENFORCE))
  44. pr_info("Secure boot enabled: ignoring ima_appraise=%s option",
  45. str);
  46. } else {
  47. ima_appraise = appraisal_state;
  48. }
  49. }
  50. #endif
  51. /*
  52. * is_ima_appraise_enabled - return appraise status
  53. *
  54. * Only return enabled, if not in ima_appraise="fix" or "log" modes.
  55. */
  56. bool is_ima_appraise_enabled(void)
  57. {
  58. return ima_appraise & IMA_APPRAISE_ENFORCE;
  59. }
  60. /*
  61. * ima_must_appraise - set appraise flag
  62. *
  63. * Return 1 to appraise or hash
  64. */
  65. int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
  66. int mask, enum ima_hooks func)
  67. {
  68. u32 secid;
  69. if (!ima_appraise)
  70. return 0;
  71. security_current_getsecid_subj(&secid);
  72. return ima_match_policy(mnt_userns, inode, current_cred(), secid,
  73. func, mask, IMA_APPRAISE | IMA_HASH, NULL,
  74. NULL, NULL, NULL);
  75. }
  76. static int ima_fix_xattr(struct dentry *dentry,
  77. struct integrity_iint_cache *iint)
  78. {
  79. int rc, offset;
  80. u8 algo = iint->ima_hash->algo;
  81. if (algo <= HASH_ALGO_SHA1) {
  82. offset = 1;
  83. iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
  84. } else {
  85. offset = 0;
  86. iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
  87. iint->ima_hash->xattr.ng.algo = algo;
  88. }
  89. rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA,
  90. &iint->ima_hash->xattr.data[offset],
  91. (sizeof(iint->ima_hash->xattr) - offset) +
  92. iint->ima_hash->length, 0);
  93. return rc;
  94. }
  95. /* Return specific func appraised cached result */
  96. enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
  97. enum ima_hooks func)
  98. {
  99. switch (func) {
  100. case MMAP_CHECK:
  101. return iint->ima_mmap_status;
  102. case BPRM_CHECK:
  103. return iint->ima_bprm_status;
  104. case CREDS_CHECK:
  105. return iint->ima_creds_status;
  106. case FILE_CHECK:
  107. case POST_SETATTR:
  108. return iint->ima_file_status;
  109. case MODULE_CHECK ... MAX_CHECK - 1:
  110. default:
  111. return iint->ima_read_status;
  112. }
  113. }
  114. static void ima_set_cache_status(struct integrity_iint_cache *iint,
  115. enum ima_hooks func,
  116. enum integrity_status status)
  117. {
  118. switch (func) {
  119. case MMAP_CHECK:
  120. iint->ima_mmap_status = status;
  121. break;
  122. case BPRM_CHECK:
  123. iint->ima_bprm_status = status;
  124. break;
  125. case CREDS_CHECK:
  126. iint->ima_creds_status = status;
  127. break;
  128. case FILE_CHECK:
  129. case POST_SETATTR:
  130. iint->ima_file_status = status;
  131. break;
  132. case MODULE_CHECK ... MAX_CHECK - 1:
  133. default:
  134. iint->ima_read_status = status;
  135. break;
  136. }
  137. }
  138. static void ima_cache_flags(struct integrity_iint_cache *iint,
  139. enum ima_hooks func)
  140. {
  141. switch (func) {
  142. case MMAP_CHECK:
  143. iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
  144. break;
  145. case BPRM_CHECK:
  146. iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
  147. break;
  148. case CREDS_CHECK:
  149. iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED);
  150. break;
  151. case FILE_CHECK:
  152. case POST_SETATTR:
  153. iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
  154. break;
  155. case MODULE_CHECK ... MAX_CHECK - 1:
  156. default:
  157. iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
  158. break;
  159. }
  160. }
  161. enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
  162. int xattr_len)
  163. {
  164. struct signature_v2_hdr *sig;
  165. enum hash_algo ret;
  166. if (!xattr_value || xattr_len < 2)
  167. /* return default hash algo */
  168. return ima_hash_algo;
  169. switch (xattr_value->type) {
  170. case IMA_VERITY_DIGSIG:
  171. sig = (typeof(sig))xattr_value;
  172. if (sig->version != 3 || xattr_len <= sizeof(*sig) ||
  173. sig->hash_algo >= HASH_ALGO__LAST)
  174. return ima_hash_algo;
  175. return sig->hash_algo;
  176. case EVM_IMA_XATTR_DIGSIG:
  177. sig = (typeof(sig))xattr_value;
  178. if (sig->version != 2 || xattr_len <= sizeof(*sig)
  179. || sig->hash_algo >= HASH_ALGO__LAST)
  180. return ima_hash_algo;
  181. return sig->hash_algo;
  182. case IMA_XATTR_DIGEST_NG:
  183. /* first byte contains algorithm id */
  184. ret = xattr_value->data[0];
  185. if (ret < HASH_ALGO__LAST)
  186. return ret;
  187. break;
  188. case IMA_XATTR_DIGEST:
  189. /* this is for backward compatibility */
  190. if (xattr_len == 21) {
  191. unsigned int zero = 0;
  192. if (!memcmp(&xattr_value->data[16], &zero, 4))
  193. return HASH_ALGO_MD5;
  194. else
  195. return HASH_ALGO_SHA1;
  196. } else if (xattr_len == 17)
  197. return HASH_ALGO_MD5;
  198. break;
  199. }
  200. /* return default hash algo */
  201. return ima_hash_algo;
  202. }
  203. int ima_read_xattr(struct dentry *dentry,
  204. struct evm_ima_xattr_data **xattr_value)
  205. {
  206. ssize_t ret;
  207. ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA,
  208. (char **)xattr_value, 0, GFP_NOFS);
  209. if (ret == -EOPNOTSUPP)
  210. ret = 0;
  211. return ret;
  212. }
  213. /*
  214. * calc_file_id_hash - calculate the hash of the ima_file_id struct data
  215. * @type: xattr type [enum evm_ima_xattr_type]
  216. * @algo: hash algorithm [enum hash_algo]
  217. * @digest: pointer to the digest to be hashed
  218. * @hash: (out) pointer to the hash
  219. *
  220. * IMA signature version 3 disambiguates the data that is signed by
  221. * indirectly signing the hash of the ima_file_id structure data.
  222. *
  223. * Signing the ima_file_id struct is currently only supported for
  224. * IMA_VERITY_DIGSIG type xattrs.
  225. *
  226. * Return 0 on success, error code otherwise.
  227. */
  228. static int calc_file_id_hash(enum evm_ima_xattr_type type,
  229. enum hash_algo algo, const u8 *digest,
  230. struct ima_digest_data *hash)
  231. {
  232. struct ima_file_id file_id = {
  233. .hash_type = IMA_VERITY_DIGSIG, .hash_algorithm = algo};
  234. unsigned int unused = HASH_MAX_DIGESTSIZE - hash_digest_size[algo];
  235. if (type != IMA_VERITY_DIGSIG)
  236. return -EINVAL;
  237. memcpy(file_id.hash, digest, hash_digest_size[algo]);
  238. hash->algo = algo;
  239. hash->length = hash_digest_size[algo];
  240. return ima_calc_buffer_hash(&file_id, sizeof(file_id) - unused, hash);
  241. }
  242. /*
  243. * xattr_verify - verify xattr digest or signature
  244. *
  245. * Verify whether the hash or signature matches the file contents.
  246. *
  247. * Return 0 on success, error code otherwise.
  248. */
  249. static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
  250. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  251. enum integrity_status *status, const char **cause)
  252. {
  253. struct ima_max_digest_data hash;
  254. struct signature_v2_hdr *sig;
  255. int rc = -EINVAL, hash_start = 0;
  256. int mask;
  257. switch (xattr_value->type) {
  258. case IMA_XATTR_DIGEST_NG:
  259. /* first byte contains algorithm id */
  260. hash_start = 1;
  261. fallthrough;
  262. case IMA_XATTR_DIGEST:
  263. if (*status != INTEGRITY_PASS_IMMUTABLE) {
  264. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  265. if (iint->flags & IMA_VERITY_REQUIRED)
  266. *cause = "verity-signature-required";
  267. else
  268. *cause = "IMA-signature-required";
  269. *status = INTEGRITY_FAIL;
  270. break;
  271. }
  272. clear_bit(IMA_DIGSIG, &iint->atomic_flags);
  273. } else {
  274. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  275. }
  276. if (xattr_len - sizeof(xattr_value->type) - hash_start >=
  277. iint->ima_hash->length)
  278. /*
  279. * xattr length may be longer. md5 hash in previous
  280. * version occupied 20 bytes in xattr, instead of 16
  281. */
  282. rc = memcmp(&xattr_value->data[hash_start],
  283. iint->ima_hash->digest,
  284. iint->ima_hash->length);
  285. else
  286. rc = -EINVAL;
  287. if (rc) {
  288. *cause = "invalid-hash";
  289. *status = INTEGRITY_FAIL;
  290. break;
  291. }
  292. *status = INTEGRITY_PASS;
  293. break;
  294. case EVM_IMA_XATTR_DIGSIG:
  295. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  296. mask = IMA_DIGSIG_REQUIRED | IMA_VERITY_REQUIRED;
  297. if ((iint->flags & mask) == mask) {
  298. *cause = "verity-signature-required";
  299. *status = INTEGRITY_FAIL;
  300. break;
  301. }
  302. sig = (typeof(sig))xattr_value;
  303. if (sig->version >= 3) {
  304. *cause = "invalid-signature-version";
  305. *status = INTEGRITY_FAIL;
  306. break;
  307. }
  308. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  309. (const char *)xattr_value,
  310. xattr_len,
  311. iint->ima_hash->digest,
  312. iint->ima_hash->length);
  313. if (rc == -EOPNOTSUPP) {
  314. *status = INTEGRITY_UNKNOWN;
  315. break;
  316. }
  317. if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
  318. func == KEXEC_KERNEL_CHECK)
  319. rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM,
  320. (const char *)xattr_value,
  321. xattr_len,
  322. iint->ima_hash->digest,
  323. iint->ima_hash->length);
  324. if (rc) {
  325. *cause = "invalid-signature";
  326. *status = INTEGRITY_FAIL;
  327. } else {
  328. *status = INTEGRITY_PASS;
  329. }
  330. break;
  331. case IMA_VERITY_DIGSIG:
  332. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  333. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  334. if (!(iint->flags & IMA_VERITY_REQUIRED)) {
  335. *cause = "IMA-signature-required";
  336. *status = INTEGRITY_FAIL;
  337. break;
  338. }
  339. }
  340. sig = (typeof(sig))xattr_value;
  341. if (sig->version != 3) {
  342. *cause = "invalid-signature-version";
  343. *status = INTEGRITY_FAIL;
  344. break;
  345. }
  346. rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo,
  347. iint->ima_hash->digest, &hash.hdr);
  348. if (rc) {
  349. *cause = "sigv3-hashing-error";
  350. *status = INTEGRITY_FAIL;
  351. break;
  352. }
  353. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  354. (const char *)xattr_value,
  355. xattr_len, hash.digest,
  356. hash.hdr.length);
  357. if (rc) {
  358. *cause = "invalid-verity-signature";
  359. *status = INTEGRITY_FAIL;
  360. } else {
  361. *status = INTEGRITY_PASS;
  362. }
  363. break;
  364. default:
  365. *status = INTEGRITY_UNKNOWN;
  366. *cause = "unknown-ima-data";
  367. break;
  368. }
  369. return rc;
  370. }
  371. /*
  372. * modsig_verify - verify modsig signature
  373. *
  374. * Verify whether the signature matches the file contents.
  375. *
  376. * Return 0 on success, error code otherwise.
  377. */
  378. static int modsig_verify(enum ima_hooks func, const struct modsig *modsig,
  379. enum integrity_status *status, const char **cause)
  380. {
  381. int rc;
  382. rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig);
  383. if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc &&
  384. func == KEXEC_KERNEL_CHECK)
  385. rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM,
  386. modsig);
  387. if (rc) {
  388. *cause = "invalid-signature";
  389. *status = INTEGRITY_FAIL;
  390. } else {
  391. *status = INTEGRITY_PASS;
  392. }
  393. return rc;
  394. }
  395. /*
  396. * ima_check_blacklist - determine if the binary is blacklisted.
  397. *
  398. * Add the hash of the blacklisted binary to the measurement list, based
  399. * on policy.
  400. *
  401. * Returns -EPERM if the hash is blacklisted.
  402. */
  403. int ima_check_blacklist(struct integrity_iint_cache *iint,
  404. const struct modsig *modsig, int pcr)
  405. {
  406. enum hash_algo hash_algo;
  407. const u8 *digest = NULL;
  408. u32 digestsize = 0;
  409. int rc = 0;
  410. if (!(iint->flags & IMA_CHECK_BLACKLIST))
  411. return 0;
  412. if (iint->flags & IMA_MODSIG_ALLOWED && modsig) {
  413. ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize);
  414. rc = is_binary_blacklisted(digest, digestsize);
  415. if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
  416. process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
  417. "blacklisted-hash", NONE,
  418. pcr, NULL, false, NULL, 0);
  419. }
  420. return rc;
  421. }
  422. /*
  423. * ima_appraise_measurement - appraise file measurement
  424. *
  425. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  426. * Assuming success, compare the xattr hash with the collected measurement.
  427. *
  428. * Return 0 on success, error code otherwise
  429. */
  430. int ima_appraise_measurement(enum ima_hooks func,
  431. struct integrity_iint_cache *iint,
  432. struct file *file, const unsigned char *filename,
  433. struct evm_ima_xattr_data *xattr_value,
  434. int xattr_len, const struct modsig *modsig)
  435. {
  436. static const char op[] = "appraise_data";
  437. const char *cause = "unknown";
  438. struct dentry *dentry = file_dentry(file);
  439. struct inode *inode = d_backing_inode(dentry);
  440. enum integrity_status status = INTEGRITY_UNKNOWN;
  441. int rc = xattr_len;
  442. bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
  443. /* If not appraising a modsig, we need an xattr. */
  444. if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
  445. return INTEGRITY_UNKNOWN;
  446. /* If reading the xattr failed and there's no modsig, error out. */
  447. if (rc <= 0 && !try_modsig) {
  448. if (rc && rc != -ENODATA)
  449. goto out;
  450. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  451. if (iint->flags & IMA_VERITY_REQUIRED)
  452. cause = "verity-signature-required";
  453. else
  454. cause = "IMA-signature-required";
  455. } else {
  456. cause = "missing-hash";
  457. }
  458. status = INTEGRITY_NOLABEL;
  459. if (file->f_mode & FMODE_CREATED)
  460. iint->flags |= IMA_NEW_FILE;
  461. if ((iint->flags & IMA_NEW_FILE) &&
  462. (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
  463. (inode->i_size == 0)))
  464. status = INTEGRITY_PASS;
  465. goto out;
  466. }
  467. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
  468. rc < 0 ? 0 : rc, iint);
  469. switch (status) {
  470. case INTEGRITY_PASS:
  471. case INTEGRITY_PASS_IMMUTABLE:
  472. case INTEGRITY_UNKNOWN:
  473. break;
  474. case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
  475. /* It's fine not to have xattrs when using a modsig. */
  476. if (try_modsig)
  477. break;
  478. fallthrough;
  479. case INTEGRITY_NOLABEL: /* No security.evm xattr. */
  480. cause = "missing-HMAC";
  481. goto out;
  482. case INTEGRITY_FAIL_IMMUTABLE:
  483. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  484. cause = "invalid-fail-immutable";
  485. goto out;
  486. case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
  487. cause = "invalid-HMAC";
  488. goto out;
  489. default:
  490. WARN_ONCE(true, "Unexpected integrity status %d\n", status);
  491. }
  492. if (xattr_value)
  493. rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
  494. &cause);
  495. /*
  496. * If we have a modsig and either no imasig or the imasig's key isn't
  497. * known, then try verifying the modsig.
  498. */
  499. if (try_modsig &&
  500. (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
  501. rc == -ENOKEY))
  502. rc = modsig_verify(func, modsig, &status, &cause);
  503. out:
  504. /*
  505. * File signatures on some filesystems can not be properly verified.
  506. * When such filesystems are mounted by an untrusted mounter or on a
  507. * system not willing to accept such a risk, fail the file signature
  508. * verification.
  509. */
  510. if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
  511. ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
  512. (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
  513. status = INTEGRITY_FAIL;
  514. cause = "unverifiable-signature";
  515. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  516. op, cause, rc, 0);
  517. } else if (status != INTEGRITY_PASS) {
  518. /* Fix mode, but don't replace file signatures. */
  519. if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
  520. (!xattr_value ||
  521. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  522. if (!ima_fix_xattr(dentry, iint))
  523. status = INTEGRITY_PASS;
  524. }
  525. /*
  526. * Permit new files with file/EVM portable signatures, but
  527. * without data.
  528. */
  529. if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
  530. test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
  531. status = INTEGRITY_PASS;
  532. }
  533. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  534. op, cause, rc, 0);
  535. } else {
  536. ima_cache_flags(iint, func);
  537. }
  538. ima_set_cache_status(iint, func, status);
  539. return status;
  540. }
  541. /*
  542. * ima_update_xattr - update 'security.ima' hash value
  543. */
  544. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  545. {
  546. struct dentry *dentry = file_dentry(file);
  547. int rc = 0;
  548. /* do not collect and update hash for digital signatures */
  549. if (test_bit(IMA_DIGSIG, &iint->atomic_flags))
  550. return;
  551. if ((iint->ima_file_status != INTEGRITY_PASS) &&
  552. !(iint->flags & IMA_HASH))
  553. return;
  554. rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL);
  555. if (rc < 0)
  556. return;
  557. inode_lock(file_inode(file));
  558. ima_fix_xattr(dentry, iint);
  559. inode_unlock(file_inode(file));
  560. }
  561. /**
  562. * ima_inode_post_setattr - reflect file metadata changes
  563. * @mnt_userns: user namespace of the mount the inode was found from
  564. * @dentry: pointer to the affected dentry
  565. *
  566. * Changes to a dentry's metadata might result in needing to appraise.
  567. *
  568. * This function is called from notify_change(), which expects the caller
  569. * to lock the inode's i_mutex.
  570. */
  571. void ima_inode_post_setattr(struct user_namespace *mnt_userns,
  572. struct dentry *dentry)
  573. {
  574. struct inode *inode = d_backing_inode(dentry);
  575. struct integrity_iint_cache *iint;
  576. int action;
  577. if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
  578. || !(inode->i_opflags & IOP_XATTR))
  579. return;
  580. action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR);
  581. iint = integrity_iint_find(inode);
  582. if (iint) {
  583. set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags);
  584. if (!action)
  585. clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
  586. }
  587. }
  588. /*
  589. * ima_protect_xattr - protect 'security.ima'
  590. *
  591. * Ensure that not just anyone can modify or remove 'security.ima'.
  592. */
  593. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  594. const void *xattr_value, size_t xattr_value_len)
  595. {
  596. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  597. if (!capable(CAP_SYS_ADMIN))
  598. return -EPERM;
  599. return 1;
  600. }
  601. return 0;
  602. }
  603. static void ima_reset_appraise_flags(struct inode *inode, int digsig)
  604. {
  605. struct integrity_iint_cache *iint;
  606. if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
  607. return;
  608. iint = integrity_iint_find(inode);
  609. if (!iint)
  610. return;
  611. iint->measured_pcrs = 0;
  612. set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags);
  613. if (digsig)
  614. set_bit(IMA_DIGSIG, &iint->atomic_flags);
  615. else
  616. clear_bit(IMA_DIGSIG, &iint->atomic_flags);
  617. }
  618. /**
  619. * validate_hash_algo() - Block setxattr with unsupported hash algorithms
  620. * @dentry: object of the setxattr()
  621. * @xattr_value: userland supplied xattr value
  622. * @xattr_value_len: length of xattr_value
  623. *
  624. * The xattr value is mapped to its hash algorithm, and this algorithm
  625. * must be built in the kernel for the setxattr to be allowed.
  626. *
  627. * Emit an audit message when the algorithm is invalid.
  628. *
  629. * Return: 0 on success, else an error.
  630. */
  631. static int validate_hash_algo(struct dentry *dentry,
  632. const struct evm_ima_xattr_data *xattr_value,
  633. size_t xattr_value_len)
  634. {
  635. char *path = NULL, *pathbuf = NULL;
  636. enum hash_algo xattr_hash_algo;
  637. const char *errmsg = "unavailable-hash-algorithm";
  638. unsigned int allowed_hashes;
  639. xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len);
  640. allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms);
  641. if (allowed_hashes) {
  642. /* success if the algorithm is allowed in the ima policy */
  643. if (allowed_hashes & (1U << xattr_hash_algo))
  644. return 0;
  645. /*
  646. * We use a different audit message when the hash algorithm
  647. * is denied by a policy rule, instead of not being built
  648. * in the kernel image
  649. */
  650. errmsg = "denied-hash-algorithm";
  651. } else {
  652. if (likely(xattr_hash_algo == ima_hash_algo))
  653. return 0;
  654. /* allow any xattr using an algorithm built in the kernel */
  655. if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))
  656. return 0;
  657. }
  658. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  659. if (!pathbuf)
  660. return -EACCES;
  661. path = dentry_path(dentry, pathbuf, PATH_MAX);
  662. integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path,
  663. "set_data", errmsg, -EACCES, 0);
  664. kfree(pathbuf);
  665. return -EACCES;
  666. }
  667. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  668. const void *xattr_value, size_t xattr_value_len)
  669. {
  670. const struct evm_ima_xattr_data *xvalue = xattr_value;
  671. int digsig = 0;
  672. int result;
  673. int err;
  674. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  675. xattr_value_len);
  676. if (result == 1) {
  677. if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
  678. return -EINVAL;
  679. err = validate_hash_algo(dentry, xvalue, xattr_value_len);
  680. if (err)
  681. return err;
  682. digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG);
  683. } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) {
  684. digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG);
  685. }
  686. if (result == 1 || evm_revalidate_status(xattr_name)) {
  687. ima_reset_appraise_flags(d_backing_inode(dentry), digsig);
  688. if (result == 1)
  689. result = 0;
  690. }
  691. return result;
  692. }
  693. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  694. {
  695. int result;
  696. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  697. if (result == 1 || evm_revalidate_status(xattr_name)) {
  698. ima_reset_appraise_flags(d_backing_inode(dentry), 0);
  699. if (result == 1)
  700. result = 0;
  701. }
  702. return result;
  703. }