ima_appraise.c 22 KB

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