evm_main.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2010 IBM Corporation
  4. *
  5. * Author:
  6. * Mimi Zohar <zohar@us.ibm.com>
  7. * Kylene Hall <kjhall@us.ibm.com>
  8. *
  9. * File: evm_main.c
  10. * implements evm_inode_setxattr, evm_inode_post_setxattr,
  11. * evm_inode_removexattr, evm_verifyxattr, and evm_inode_set_acl.
  12. */
  13. #define pr_fmt(fmt) "EVM: "fmt
  14. #include <linux/init.h>
  15. #include <linux/audit.h>
  16. #include <linux/xattr.h>
  17. #include <linux/integrity.h>
  18. #include <linux/evm.h>
  19. #include <linux/magic.h>
  20. #include <linux/posix_acl_xattr.h>
  21. #include <linux/lsm_hooks.h>
  22. #include <crypto/hash.h>
  23. #include <crypto/hash_info.h>
  24. #include <crypto/utils.h>
  25. #include "evm.h"
  26. int evm_initialized;
  27. static const char * const integrity_status_msg[] = {
  28. "pass", "pass_immutable", "fail", "fail_immutable", "no_label",
  29. "no_xattrs", "unknown"
  30. };
  31. int evm_hmac_attrs;
  32. static struct xattr_list evm_config_default_xattrnames[] = {
  33. {
  34. .name = XATTR_NAME_SELINUX,
  35. .enabled = IS_ENABLED(CONFIG_SECURITY_SELINUX)
  36. },
  37. {
  38. .name = XATTR_NAME_SMACK,
  39. .enabled = IS_ENABLED(CONFIG_SECURITY_SMACK)
  40. },
  41. {
  42. .name = XATTR_NAME_SMACKEXEC,
  43. .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
  44. },
  45. {
  46. .name = XATTR_NAME_SMACKTRANSMUTE,
  47. .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
  48. },
  49. {
  50. .name = XATTR_NAME_SMACKMMAP,
  51. .enabled = IS_ENABLED(CONFIG_EVM_EXTRA_SMACK_XATTRS)
  52. },
  53. {
  54. .name = XATTR_NAME_APPARMOR,
  55. .enabled = IS_ENABLED(CONFIG_SECURITY_APPARMOR)
  56. },
  57. {
  58. .name = XATTR_NAME_IMA,
  59. .enabled = IS_ENABLED(CONFIG_IMA_APPRAISE)
  60. },
  61. {
  62. .name = XATTR_NAME_CAPS,
  63. .enabled = true
  64. },
  65. };
  66. LIST_HEAD(evm_config_xattrnames);
  67. static int evm_fixmode __ro_after_init;
  68. static int __init evm_set_fixmode(char *str)
  69. {
  70. if (strncmp(str, "fix", 3) == 0)
  71. evm_fixmode = 1;
  72. else
  73. pr_err("invalid \"%s\" mode", str);
  74. return 1;
  75. }
  76. __setup("evm=", evm_set_fixmode);
  77. static void __init evm_init_config(void)
  78. {
  79. int i, xattrs;
  80. xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
  81. pr_info("Initialising EVM extended attributes:\n");
  82. for (i = 0; i < xattrs; i++) {
  83. pr_info("%s%s\n", evm_config_default_xattrnames[i].name,
  84. !evm_config_default_xattrnames[i].enabled ?
  85. " (disabled)" : "");
  86. list_add_tail(&evm_config_default_xattrnames[i].list,
  87. &evm_config_xattrnames);
  88. }
  89. #ifdef CONFIG_EVM_ATTR_FSUUID
  90. evm_hmac_attrs |= EVM_ATTR_FSUUID;
  91. #endif
  92. pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
  93. }
  94. static bool evm_key_loaded(void)
  95. {
  96. return (bool)(evm_initialized & EVM_KEY_MASK);
  97. }
  98. /*
  99. * This function determines whether or not it is safe to ignore verification
  100. * errors, based on the ability of EVM to calculate HMACs. If the HMAC key
  101. * is not loaded, and it cannot be loaded in the future due to the
  102. * EVM_SETUP_COMPLETE initialization flag, allowing an operation despite the
  103. * attrs/xattrs being found invalid will not make them valid.
  104. */
  105. static bool evm_hmac_disabled(void)
  106. {
  107. if (evm_initialized & EVM_INIT_HMAC)
  108. return false;
  109. if (!(evm_initialized & EVM_SETUP_COMPLETE))
  110. return false;
  111. return true;
  112. }
  113. static int evm_find_protected_xattrs(struct dentry *dentry)
  114. {
  115. struct inode *inode = d_backing_inode(dentry);
  116. struct xattr_list *xattr;
  117. int error;
  118. int count = 0;
  119. if (!(inode->i_opflags & IOP_XATTR))
  120. return -EOPNOTSUPP;
  121. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  122. error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
  123. if (error < 0) {
  124. if (error == -ENODATA)
  125. continue;
  126. return error;
  127. }
  128. count++;
  129. }
  130. return count;
  131. }
  132. static int is_unsupported_hmac_fs(struct dentry *dentry)
  133. {
  134. struct inode *inode = d_backing_inode(dentry);
  135. if (inode->i_sb->s_iflags & SB_I_EVM_HMAC_UNSUPPORTED) {
  136. pr_info_once("%s not supported\n", inode->i_sb->s_type->name);
  137. return 1;
  138. }
  139. return 0;
  140. }
  141. /*
  142. * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
  143. *
  144. * Compute the HMAC on the dentry's protected set of extended attributes
  145. * and compare it against the stored security.evm xattr.
  146. *
  147. * For performance:
  148. * - use the previously retrieved xattr value and length to calculate the
  149. * HMAC.)
  150. * - cache the verification result in the iint, when available.
  151. *
  152. * Returns integrity status
  153. */
  154. static enum integrity_status evm_verify_hmac(struct dentry *dentry,
  155. const char *xattr_name,
  156. char *xattr_value,
  157. size_t xattr_value_len)
  158. {
  159. struct evm_ima_xattr_data *xattr_data = NULL;
  160. struct signature_v2_hdr *hdr;
  161. enum integrity_status evm_status = INTEGRITY_PASS;
  162. struct evm_digest digest;
  163. struct inode *inode = d_backing_inode(dentry);
  164. struct evm_iint_cache *iint = evm_iint_inode(inode);
  165. int rc, xattr_len, evm_immutable = 0;
  166. if (iint && (iint->evm_status == INTEGRITY_PASS ||
  167. iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
  168. return iint->evm_status;
  169. /*
  170. * On unsupported filesystems without EVM_INIT_X509 enabled, skip
  171. * signature verification.
  172. */
  173. if (!(evm_initialized & EVM_INIT_X509) &&
  174. is_unsupported_hmac_fs(dentry))
  175. return INTEGRITY_UNKNOWN;
  176. /* if status is not PASS, try to check again - against -ENOMEM */
  177. /* first need to know the sig type */
  178. rc = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_EVM,
  179. (char **)&xattr_data, 0, GFP_NOFS);
  180. if (rc <= 0) {
  181. evm_status = INTEGRITY_FAIL;
  182. if (rc == -ENODATA) {
  183. rc = evm_find_protected_xattrs(dentry);
  184. if (rc > 0)
  185. evm_status = INTEGRITY_NOLABEL;
  186. else if (rc == 0)
  187. evm_status = INTEGRITY_NOXATTRS; /* new file */
  188. } else if (rc == -EOPNOTSUPP) {
  189. evm_status = INTEGRITY_UNKNOWN;
  190. }
  191. goto out;
  192. }
  193. xattr_len = rc;
  194. /* check value type */
  195. switch (xattr_data->type) {
  196. case EVM_XATTR_HMAC:
  197. if (xattr_len != sizeof(struct evm_xattr)) {
  198. evm_status = INTEGRITY_FAIL;
  199. goto out;
  200. }
  201. digest.hdr.algo = HASH_ALGO_SHA1;
  202. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  203. xattr_value_len, &digest, iint);
  204. if (rc)
  205. break;
  206. rc = crypto_memneq(xattr_data->data, digest.digest,
  207. SHA1_DIGEST_SIZE);
  208. if (rc)
  209. rc = -EINVAL;
  210. break;
  211. case EVM_XATTR_PORTABLE_DIGSIG:
  212. evm_immutable = 1;
  213. fallthrough;
  214. case EVM_IMA_XATTR_DIGSIG:
  215. /* accept xattr with non-empty signature field */
  216. if (xattr_len <= sizeof(struct signature_v2_hdr)) {
  217. evm_status = INTEGRITY_FAIL;
  218. goto out;
  219. }
  220. hdr = (struct signature_v2_hdr *)xattr_data;
  221. digest.hdr.algo = hdr->hash_algo;
  222. rc = evm_calc_hash(dentry, xattr_name, xattr_value,
  223. xattr_value_len, xattr_data->type, &digest,
  224. iint);
  225. if (rc)
  226. break;
  227. rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
  228. (const char *)xattr_data, xattr_len,
  229. digest.digest, digest.hdr.length);
  230. if (!rc) {
  231. if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
  232. if (iint)
  233. iint->flags |= EVM_IMMUTABLE_DIGSIG;
  234. evm_status = INTEGRITY_PASS_IMMUTABLE;
  235. } else if (!IS_RDONLY(inode) &&
  236. !(inode->i_sb->s_readonly_remount) &&
  237. !IS_IMMUTABLE(inode) &&
  238. !is_unsupported_hmac_fs(dentry)) {
  239. evm_update_evmxattr(dentry, xattr_name,
  240. xattr_value,
  241. xattr_value_len);
  242. }
  243. }
  244. break;
  245. default:
  246. rc = -EINVAL;
  247. break;
  248. }
  249. if (rc) {
  250. if (rc == -ENODATA)
  251. evm_status = INTEGRITY_NOXATTRS;
  252. else if (evm_immutable)
  253. evm_status = INTEGRITY_FAIL_IMMUTABLE;
  254. else
  255. evm_status = INTEGRITY_FAIL;
  256. }
  257. pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
  258. digest.digest);
  259. out:
  260. if (iint)
  261. iint->evm_status = evm_status;
  262. kfree(xattr_data);
  263. return evm_status;
  264. }
  265. static int evm_protected_xattr_common(const char *req_xattr_name,
  266. bool all_xattrs)
  267. {
  268. int namelen;
  269. int found = 0;
  270. struct xattr_list *xattr;
  271. namelen = strlen(req_xattr_name);
  272. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  273. if (!all_xattrs && !xattr->enabled)
  274. continue;
  275. if ((strlen(xattr->name) == namelen)
  276. && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
  277. found = 1;
  278. break;
  279. }
  280. if (strncmp(req_xattr_name,
  281. xattr->name + XATTR_SECURITY_PREFIX_LEN,
  282. strlen(req_xattr_name)) == 0) {
  283. found = 1;
  284. break;
  285. }
  286. }
  287. return found;
  288. }
  289. int evm_protected_xattr(const char *req_xattr_name)
  290. {
  291. return evm_protected_xattr_common(req_xattr_name, false);
  292. }
  293. int evm_protected_xattr_if_enabled(const char *req_xattr_name)
  294. {
  295. return evm_protected_xattr_common(req_xattr_name, true);
  296. }
  297. /**
  298. * evm_read_protected_xattrs - read EVM protected xattr names, lengths, values
  299. * @dentry: dentry of the read xattrs
  300. * @buffer: buffer xattr names, lengths or values are copied to
  301. * @buffer_size: size of buffer
  302. * @type: n: names, l: lengths, v: values
  303. * @canonical_fmt: data format (true: little endian, false: native format)
  304. *
  305. * Read protected xattr names (separated by |), lengths (u32) or values for a
  306. * given dentry and return the total size of copied data. If buffer is NULL,
  307. * just return the total size.
  308. *
  309. * Returns the total size on success, a negative value on error.
  310. */
  311. int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
  312. int buffer_size, char type, bool canonical_fmt)
  313. {
  314. struct xattr_list *xattr;
  315. int rc, size, total_size = 0;
  316. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  317. rc = __vfs_getxattr(dentry, d_backing_inode(dentry),
  318. xattr->name, NULL, 0);
  319. if (rc < 0 && rc == -ENODATA)
  320. continue;
  321. else if (rc < 0)
  322. return rc;
  323. switch (type) {
  324. case 'n':
  325. size = strlen(xattr->name) + 1;
  326. if (buffer) {
  327. if (total_size)
  328. *(buffer + total_size - 1) = '|';
  329. memcpy(buffer + total_size, xattr->name, size);
  330. }
  331. break;
  332. case 'l':
  333. size = sizeof(u32);
  334. if (buffer) {
  335. if (canonical_fmt)
  336. rc = (__force int)cpu_to_le32(rc);
  337. *(u32 *)(buffer + total_size) = rc;
  338. }
  339. break;
  340. case 'v':
  341. size = rc;
  342. if (buffer) {
  343. rc = __vfs_getxattr(dentry,
  344. d_backing_inode(dentry), xattr->name,
  345. buffer + total_size,
  346. buffer_size - total_size);
  347. if (rc < 0)
  348. return rc;
  349. }
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. total_size += size;
  355. }
  356. return total_size;
  357. }
  358. /**
  359. * evm_verifyxattr - verify the integrity of the requested xattr
  360. * @dentry: object of the verify xattr
  361. * @xattr_name: requested xattr
  362. * @xattr_value: requested xattr value
  363. * @xattr_value_len: requested xattr value length
  364. *
  365. * Calculate the HMAC for the given dentry and verify it against the stored
  366. * security.evm xattr. For performance, use the xattr value and length
  367. * previously retrieved to calculate the HMAC.
  368. *
  369. * Returns the xattr integrity status.
  370. *
  371. * This function requires the caller to lock the inode's i_mutex before it
  372. * is executed.
  373. */
  374. enum integrity_status evm_verifyxattr(struct dentry *dentry,
  375. const char *xattr_name,
  376. void *xattr_value, size_t xattr_value_len)
  377. {
  378. if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
  379. return INTEGRITY_UNKNOWN;
  380. return evm_verify_hmac(dentry, xattr_name, xattr_value,
  381. xattr_value_len);
  382. }
  383. EXPORT_SYMBOL_GPL(evm_verifyxattr);
  384. /*
  385. * evm_verify_current_integrity - verify the dentry's metadata integrity
  386. * @dentry: pointer to the affected dentry
  387. *
  388. * Verify and return the dentry's metadata integrity. The exceptions are
  389. * before EVM is initialized or in 'fix' mode.
  390. */
  391. static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
  392. {
  393. struct inode *inode = d_backing_inode(dentry);
  394. if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
  395. return INTEGRITY_PASS;
  396. return evm_verify_hmac(dentry, NULL, NULL, 0);
  397. }
  398. /*
  399. * evm_xattr_change - check if passed xattr value differs from current value
  400. * @idmap: idmap of the mount
  401. * @dentry: pointer to the affected dentry
  402. * @xattr_name: requested xattr
  403. * @xattr_value: requested xattr value
  404. * @xattr_value_len: requested xattr value length
  405. *
  406. * Check if passed xattr value differs from current value.
  407. *
  408. * Returns 1 if passed xattr value differs from current value, 0 otherwise.
  409. */
  410. static int evm_xattr_change(struct mnt_idmap *idmap,
  411. struct dentry *dentry, const char *xattr_name,
  412. const void *xattr_value, size_t xattr_value_len)
  413. {
  414. char *xattr_data = NULL;
  415. int rc = 0;
  416. rc = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, xattr_name, &xattr_data,
  417. 0, GFP_NOFS);
  418. if (rc < 0) {
  419. rc = 1;
  420. goto out;
  421. }
  422. if (rc == xattr_value_len)
  423. rc = !!memcmp(xattr_value, xattr_data, rc);
  424. else
  425. rc = 1;
  426. out:
  427. kfree(xattr_data);
  428. return rc;
  429. }
  430. /*
  431. * evm_protect_xattr - protect the EVM extended attribute
  432. *
  433. * Prevent security.evm from being modified or removed without the
  434. * necessary permissions or when the existing value is invalid.
  435. *
  436. * The posix xattr acls are 'system' prefixed, which normally would not
  437. * affect security.evm. An interesting side affect of writing posix xattr
  438. * acls is their modifying of the i_mode, which is included in security.evm.
  439. * For posix xattr acls only, permit security.evm, even if it currently
  440. * doesn't exist, to be updated unless the EVM signature is immutable.
  441. */
  442. static int evm_protect_xattr(struct mnt_idmap *idmap,
  443. struct dentry *dentry, const char *xattr_name,
  444. const void *xattr_value, size_t xattr_value_len)
  445. {
  446. enum integrity_status evm_status;
  447. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  448. if (!capable(CAP_SYS_ADMIN))
  449. return -EPERM;
  450. if (is_unsupported_hmac_fs(dentry))
  451. return -EPERM;
  452. } else if (!evm_protected_xattr(xattr_name)) {
  453. if (!posix_xattr_acl(xattr_name))
  454. return 0;
  455. if (is_unsupported_hmac_fs(dentry))
  456. return 0;
  457. evm_status = evm_verify_current_integrity(dentry);
  458. if ((evm_status == INTEGRITY_PASS) ||
  459. (evm_status == INTEGRITY_NOXATTRS))
  460. return 0;
  461. goto out;
  462. } else if (is_unsupported_hmac_fs(dentry))
  463. return 0;
  464. evm_status = evm_verify_current_integrity(dentry);
  465. if (evm_status == INTEGRITY_NOXATTRS) {
  466. struct evm_iint_cache *iint;
  467. /* Exception if the HMAC is not going to be calculated. */
  468. if (evm_hmac_disabled())
  469. return 0;
  470. iint = evm_iint_inode(d_backing_inode(dentry));
  471. if (iint && (iint->flags & EVM_NEW_FILE))
  472. return 0;
  473. /* exception for pseudo filesystems */
  474. if (dentry->d_sb->s_magic == TMPFS_MAGIC
  475. || dentry->d_sb->s_magic == SYSFS_MAGIC)
  476. return 0;
  477. integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
  478. dentry->d_inode, dentry->d_name.name,
  479. "update_metadata",
  480. integrity_status_msg[evm_status],
  481. -EPERM, 0);
  482. }
  483. out:
  484. /* Exception if the HMAC is not going to be calculated. */
  485. if (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL ||
  486. evm_status == INTEGRITY_UNKNOWN))
  487. return 0;
  488. /*
  489. * Writing other xattrs is safe for portable signatures, as portable
  490. * signatures are immutable and can never be updated.
  491. */
  492. if (evm_status == INTEGRITY_FAIL_IMMUTABLE)
  493. return 0;
  494. if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
  495. !evm_xattr_change(idmap, dentry, xattr_name, xattr_value,
  496. xattr_value_len))
  497. return 0;
  498. if (evm_status != INTEGRITY_PASS &&
  499. evm_status != INTEGRITY_PASS_IMMUTABLE)
  500. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  501. dentry->d_name.name, "appraise_metadata",
  502. integrity_status_msg[evm_status],
  503. -EPERM, 0);
  504. return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
  505. }
  506. /**
  507. * evm_inode_setxattr - protect the EVM extended attribute
  508. * @idmap: idmap of the mount
  509. * @dentry: pointer to the affected dentry
  510. * @xattr_name: pointer to the affected extended attribute name
  511. * @xattr_value: pointer to the new extended attribute value
  512. * @xattr_value_len: pointer to the new extended attribute value length
  513. * @flags: flags to pass into filesystem operations
  514. *
  515. * Before allowing the 'security.evm' protected xattr to be updated,
  516. * verify the existing value is valid. As only the kernel should have
  517. * access to the EVM encrypted key needed to calculate the HMAC, prevent
  518. * userspace from writing HMAC value. Writing 'security.evm' requires
  519. * requires CAP_SYS_ADMIN privileges.
  520. */
  521. static int evm_inode_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,
  522. const char *xattr_name, const void *xattr_value,
  523. size_t xattr_value_len, int flags)
  524. {
  525. const struct evm_ima_xattr_data *xattr_data = xattr_value;
  526. /* Policy permits modification of the protected xattrs even though
  527. * there's no HMAC key loaded
  528. */
  529. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  530. return 0;
  531. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  532. if (!xattr_value_len)
  533. return -EINVAL;
  534. if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
  535. xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
  536. return -EPERM;
  537. }
  538. return evm_protect_xattr(idmap, dentry, xattr_name, xattr_value,
  539. xattr_value_len);
  540. }
  541. /**
  542. * evm_inode_removexattr - protect the EVM extended attribute
  543. * @idmap: idmap of the mount
  544. * @dentry: pointer to the affected dentry
  545. * @xattr_name: pointer to the affected extended attribute name
  546. *
  547. * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
  548. * the current value is valid.
  549. */
  550. static int evm_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry,
  551. const char *xattr_name)
  552. {
  553. /* Policy permits modification of the protected xattrs even though
  554. * there's no HMAC key loaded
  555. */
  556. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  557. return 0;
  558. return evm_protect_xattr(idmap, dentry, xattr_name, NULL, 0);
  559. }
  560. #ifdef CONFIG_FS_POSIX_ACL
  561. static int evm_inode_set_acl_change(struct mnt_idmap *idmap,
  562. struct dentry *dentry, const char *name,
  563. struct posix_acl *kacl)
  564. {
  565. int rc;
  566. umode_t mode;
  567. struct inode *inode = d_backing_inode(dentry);
  568. if (!kacl)
  569. return 1;
  570. rc = posix_acl_update_mode(idmap, inode, &mode, &kacl);
  571. if (rc || (inode->i_mode != mode))
  572. return 1;
  573. return 0;
  574. }
  575. #else
  576. static inline int evm_inode_set_acl_change(struct mnt_idmap *idmap,
  577. struct dentry *dentry,
  578. const char *name,
  579. struct posix_acl *kacl)
  580. {
  581. return 0;
  582. }
  583. #endif
  584. /**
  585. * evm_inode_set_acl - protect the EVM extended attribute from posix acls
  586. * @idmap: idmap of the idmapped mount
  587. * @dentry: pointer to the affected dentry
  588. * @acl_name: name of the posix acl
  589. * @kacl: pointer to the posix acls
  590. *
  591. * Prevent modifying posix acls causing the EVM HMAC to be re-calculated
  592. * and 'security.evm' xattr updated, unless the existing 'security.evm' is
  593. * valid.
  594. *
  595. * Return: zero on success, -EPERM on failure.
  596. */
  597. static int evm_inode_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  598. const char *acl_name, struct posix_acl *kacl)
  599. {
  600. enum integrity_status evm_status;
  601. /* Policy permits modification of the protected xattrs even though
  602. * there's no HMAC key loaded
  603. */
  604. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  605. return 0;
  606. evm_status = evm_verify_current_integrity(dentry);
  607. if ((evm_status == INTEGRITY_PASS) ||
  608. (evm_status == INTEGRITY_NOXATTRS))
  609. return 0;
  610. /* Exception if the HMAC is not going to be calculated. */
  611. if (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL ||
  612. evm_status == INTEGRITY_UNKNOWN))
  613. return 0;
  614. /*
  615. * Writing other xattrs is safe for portable signatures, as portable
  616. * signatures are immutable and can never be updated.
  617. */
  618. if (evm_status == INTEGRITY_FAIL_IMMUTABLE)
  619. return 0;
  620. if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
  621. !evm_inode_set_acl_change(idmap, dentry, acl_name, kacl))
  622. return 0;
  623. if (evm_status != INTEGRITY_PASS_IMMUTABLE)
  624. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  625. dentry->d_name.name, "appraise_metadata",
  626. integrity_status_msg[evm_status],
  627. -EPERM, 0);
  628. return -EPERM;
  629. }
  630. /**
  631. * evm_inode_remove_acl - Protect the EVM extended attribute from posix acls
  632. * @idmap: idmap of the mount
  633. * @dentry: pointer to the affected dentry
  634. * @acl_name: name of the posix acl
  635. *
  636. * Prevent removing posix acls causing the EVM HMAC to be re-calculated
  637. * and 'security.evm' xattr updated, unless the existing 'security.evm' is
  638. * valid.
  639. *
  640. * Return: zero on success, -EPERM on failure.
  641. */
  642. static int evm_inode_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  643. const char *acl_name)
  644. {
  645. return evm_inode_set_acl(idmap, dentry, acl_name, NULL);
  646. }
  647. static void evm_reset_status(struct inode *inode)
  648. {
  649. struct evm_iint_cache *iint;
  650. iint = evm_iint_inode(inode);
  651. if (iint)
  652. iint->evm_status = INTEGRITY_UNKNOWN;
  653. }
  654. /**
  655. * evm_metadata_changed: Detect changes to the metadata
  656. * @inode: a file's inode
  657. * @metadata_inode: metadata inode
  658. *
  659. * On a stacked filesystem detect whether the metadata has changed. If this is
  660. * the case reset the evm_status associated with the inode that represents the
  661. * file.
  662. */
  663. bool evm_metadata_changed(struct inode *inode, struct inode *metadata_inode)
  664. {
  665. struct evm_iint_cache *iint = evm_iint_inode(inode);
  666. bool ret = false;
  667. if (iint) {
  668. ret = (!IS_I_VERSION(metadata_inode) ||
  669. integrity_inode_attrs_changed(&iint->metadata_inode,
  670. metadata_inode));
  671. if (ret)
  672. iint->evm_status = INTEGRITY_UNKNOWN;
  673. }
  674. return ret;
  675. }
  676. /**
  677. * evm_revalidate_status - report whether EVM status re-validation is necessary
  678. * @xattr_name: pointer to the affected extended attribute name
  679. *
  680. * Report whether callers of evm_verifyxattr() should re-validate the
  681. * EVM status.
  682. *
  683. * Return true if re-validation is necessary, false otherwise.
  684. */
  685. bool evm_revalidate_status(const char *xattr_name)
  686. {
  687. if (!evm_key_loaded())
  688. return false;
  689. /* evm_inode_post_setattr() passes NULL */
  690. if (!xattr_name)
  691. return true;
  692. if (!evm_protected_xattr(xattr_name) && !posix_xattr_acl(xattr_name) &&
  693. strcmp(xattr_name, XATTR_NAME_EVM))
  694. return false;
  695. return true;
  696. }
  697. /**
  698. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  699. * @dentry: pointer to the affected dentry
  700. * @xattr_name: pointer to the affected extended attribute name
  701. * @xattr_value: pointer to the new extended attribute value
  702. * @xattr_value_len: pointer to the new extended attribute value length
  703. * @flags: flags to pass into filesystem operations
  704. *
  705. * Update the HMAC stored in 'security.evm' to reflect the change.
  706. *
  707. * No need to take the i_mutex lock here, as this function is called from
  708. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  709. * i_mutex lock.
  710. */
  711. static void evm_inode_post_setxattr(struct dentry *dentry,
  712. const char *xattr_name,
  713. const void *xattr_value,
  714. size_t xattr_value_len,
  715. int flags)
  716. {
  717. if (!evm_revalidate_status(xattr_name))
  718. return;
  719. evm_reset_status(dentry->d_inode);
  720. if (!strcmp(xattr_name, XATTR_NAME_EVM))
  721. return;
  722. if (!(evm_initialized & EVM_INIT_HMAC))
  723. return;
  724. if (is_unsupported_hmac_fs(dentry))
  725. return;
  726. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  727. }
  728. /**
  729. * evm_inode_post_set_acl - Update the EVM extended attribute from posix acls
  730. * @dentry: pointer to the affected dentry
  731. * @acl_name: name of the posix acl
  732. * @kacl: pointer to the posix acls
  733. *
  734. * Update the 'security.evm' xattr with the EVM HMAC re-calculated after setting
  735. * posix acls.
  736. */
  737. static void evm_inode_post_set_acl(struct dentry *dentry, const char *acl_name,
  738. struct posix_acl *kacl)
  739. {
  740. return evm_inode_post_setxattr(dentry, acl_name, NULL, 0, 0);
  741. }
  742. /**
  743. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  744. * @dentry: pointer to the affected dentry
  745. * @xattr_name: pointer to the affected extended attribute name
  746. *
  747. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  748. *
  749. * No need to take the i_mutex lock here, as this function is called from
  750. * vfs_removexattr() which takes the i_mutex.
  751. */
  752. static void evm_inode_post_removexattr(struct dentry *dentry,
  753. const char *xattr_name)
  754. {
  755. if (!evm_revalidate_status(xattr_name))
  756. return;
  757. evm_reset_status(dentry->d_inode);
  758. if (!strcmp(xattr_name, XATTR_NAME_EVM))
  759. return;
  760. if (!(evm_initialized & EVM_INIT_HMAC))
  761. return;
  762. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  763. }
  764. /**
  765. * evm_inode_post_remove_acl - Update the EVM extended attribute from posix acls
  766. * @idmap: idmap of the mount
  767. * @dentry: pointer to the affected dentry
  768. * @acl_name: name of the posix acl
  769. *
  770. * Update the 'security.evm' xattr with the EVM HMAC re-calculated after
  771. * removing posix acls.
  772. */
  773. static inline void evm_inode_post_remove_acl(struct mnt_idmap *idmap,
  774. struct dentry *dentry,
  775. const char *acl_name)
  776. {
  777. evm_inode_post_removexattr(dentry, acl_name);
  778. }
  779. static int evm_attr_change(struct mnt_idmap *idmap,
  780. struct dentry *dentry, struct iattr *attr)
  781. {
  782. struct inode *inode = d_backing_inode(dentry);
  783. unsigned int ia_valid = attr->ia_valid;
  784. if (!i_uid_needs_update(idmap, attr, inode) &&
  785. !i_gid_needs_update(idmap, attr, inode) &&
  786. (!(ia_valid & ATTR_MODE) || attr->ia_mode == inode->i_mode))
  787. return 0;
  788. return 1;
  789. }
  790. /**
  791. * evm_inode_setattr - prevent updating an invalid EVM extended attribute
  792. * @idmap: idmap of the mount
  793. * @dentry: pointer to the affected dentry
  794. * @attr: iattr structure containing the new file attributes
  795. *
  796. * Permit update of file attributes when files have a valid EVM signature,
  797. * except in the case of them having an immutable portable signature.
  798. */
  799. static int evm_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  800. struct iattr *attr)
  801. {
  802. unsigned int ia_valid = attr->ia_valid;
  803. enum integrity_status evm_status;
  804. /* Policy permits modification of the protected attrs even though
  805. * there's no HMAC key loaded
  806. */
  807. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  808. return 0;
  809. if (is_unsupported_hmac_fs(dentry))
  810. return 0;
  811. if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
  812. return 0;
  813. evm_status = evm_verify_current_integrity(dentry);
  814. /*
  815. * Writing attrs is safe for portable signatures, as portable signatures
  816. * are immutable and can never be updated.
  817. */
  818. if ((evm_status == INTEGRITY_PASS) ||
  819. (evm_status == INTEGRITY_NOXATTRS) ||
  820. (evm_status == INTEGRITY_FAIL_IMMUTABLE) ||
  821. (evm_hmac_disabled() && (evm_status == INTEGRITY_NOLABEL ||
  822. evm_status == INTEGRITY_UNKNOWN)))
  823. return 0;
  824. if (evm_status == INTEGRITY_PASS_IMMUTABLE &&
  825. !evm_attr_change(idmap, dentry, attr))
  826. return 0;
  827. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  828. dentry->d_name.name, "appraise_metadata",
  829. integrity_status_msg[evm_status], -EPERM, 0);
  830. return -EPERM;
  831. }
  832. /**
  833. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  834. * @idmap: idmap of the idmapped mount
  835. * @dentry: pointer to the affected dentry
  836. * @ia_valid: for the UID and GID status
  837. *
  838. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  839. * changes.
  840. *
  841. * This function is called from notify_change(), which expects the caller
  842. * to lock the inode's i_mutex.
  843. */
  844. static void evm_inode_post_setattr(struct mnt_idmap *idmap,
  845. struct dentry *dentry, int ia_valid)
  846. {
  847. if (!evm_revalidate_status(NULL))
  848. return;
  849. evm_reset_status(dentry->d_inode);
  850. if (!(evm_initialized & EVM_INIT_HMAC))
  851. return;
  852. if (is_unsupported_hmac_fs(dentry))
  853. return;
  854. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  855. evm_update_evmxattr(dentry, NULL, NULL, 0);
  856. }
  857. static int evm_inode_copy_up_xattr(struct dentry *src, const char *name)
  858. {
  859. struct evm_ima_xattr_data *xattr_data = NULL;
  860. int rc;
  861. if (strcmp(name, XATTR_NAME_EVM) != 0)
  862. return -EOPNOTSUPP;
  863. /* first need to know the sig type */
  864. rc = vfs_getxattr_alloc(&nop_mnt_idmap, src, XATTR_NAME_EVM,
  865. (char **)&xattr_data, 0, GFP_NOFS);
  866. if (rc <= 0)
  867. return -EPERM;
  868. if (rc < offsetof(struct evm_ima_xattr_data, type) +
  869. sizeof(xattr_data->type))
  870. return -EPERM;
  871. switch (xattr_data->type) {
  872. case EVM_XATTR_PORTABLE_DIGSIG:
  873. rc = 0; /* allow copy-up */
  874. break;
  875. case EVM_XATTR_HMAC:
  876. case EVM_IMA_XATTR_DIGSIG:
  877. default:
  878. rc = -ECANCELED; /* discard */
  879. }
  880. kfree(xattr_data);
  881. return rc;
  882. }
  883. /*
  884. * evm_inode_init_security - initializes security.evm HMAC value
  885. */
  886. int evm_inode_init_security(struct inode *inode, struct inode *dir,
  887. const struct qstr *qstr, struct xattr *xattrs,
  888. int *xattr_count)
  889. {
  890. struct evm_xattr *xattr_data;
  891. struct xattr *xattr, *evm_xattr;
  892. bool evm_protected_xattrs = false;
  893. int rc;
  894. if (!(evm_initialized & EVM_INIT_HMAC) || !xattrs)
  895. return 0;
  896. /*
  897. * security_inode_init_security() makes sure that the xattrs array is
  898. * contiguous, there is enough space for security.evm, and that there is
  899. * a terminator at the end of the array.
  900. */
  901. for (xattr = xattrs; xattr->name; xattr++) {
  902. if (evm_protected_xattr(xattr->name))
  903. evm_protected_xattrs = true;
  904. }
  905. /* EVM xattr not needed. */
  906. if (!evm_protected_xattrs)
  907. return 0;
  908. evm_xattr = lsm_get_xattr_slot(xattrs, xattr_count);
  909. /*
  910. * Array terminator (xattr name = NULL) must be the first non-filled
  911. * xattr slot.
  912. */
  913. WARN_ONCE(evm_xattr != xattr,
  914. "%s: xattrs terminator is not the first non-filled slot\n",
  915. __func__);
  916. xattr_data = kzalloc_obj(*xattr_data, GFP_NOFS);
  917. if (!xattr_data)
  918. return -ENOMEM;
  919. xattr_data->data.type = EVM_XATTR_HMAC;
  920. rc = evm_init_hmac(inode, xattrs, xattr_data->digest);
  921. if (rc < 0)
  922. goto out;
  923. evm_xattr->value = xattr_data;
  924. evm_xattr->value_len = sizeof(*xattr_data);
  925. evm_xattr->name = XATTR_EVM_SUFFIX;
  926. return 0;
  927. out:
  928. kfree(xattr_data);
  929. return rc;
  930. }
  931. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  932. static int evm_inode_alloc_security(struct inode *inode)
  933. {
  934. struct evm_iint_cache *iint = evm_iint_inode(inode);
  935. /* Called by security_inode_alloc(), it cannot be NULL. */
  936. iint->flags = 0UL;
  937. iint->evm_status = INTEGRITY_UNKNOWN;
  938. return 0;
  939. }
  940. static void evm_file_release(struct file *file)
  941. {
  942. struct inode *inode = file_inode(file);
  943. struct evm_iint_cache *iint = evm_iint_inode(inode);
  944. fmode_t mode = file->f_mode;
  945. if (!S_ISREG(inode->i_mode) || !(mode & FMODE_WRITE))
  946. return;
  947. if (iint && iint->flags & EVM_NEW_FILE &&
  948. atomic_read(&inode->i_writecount) == 1)
  949. iint->flags &= ~EVM_NEW_FILE;
  950. }
  951. static void evm_post_path_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
  952. {
  953. struct inode *inode = d_backing_inode(dentry);
  954. struct evm_iint_cache *iint = evm_iint_inode(inode);
  955. if (!S_ISREG(inode->i_mode))
  956. return;
  957. if (iint)
  958. iint->flags |= EVM_NEW_FILE;
  959. }
  960. #ifdef CONFIG_EVM_LOAD_X509
  961. void __init evm_load_x509(void)
  962. {
  963. int rc;
  964. rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
  965. if (!rc)
  966. evm_initialized |= EVM_INIT_X509;
  967. }
  968. #endif
  969. static int __init init_evm(void)
  970. {
  971. int error;
  972. struct list_head *pos, *q;
  973. evm_init_config();
  974. error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
  975. if (error)
  976. goto error;
  977. error = evm_init_secfs();
  978. if (error < 0) {
  979. pr_info("Error registering secfs\n");
  980. goto error;
  981. }
  982. error:
  983. if (error != 0) {
  984. if (!list_empty(&evm_config_xattrnames)) {
  985. list_for_each_safe(pos, q, &evm_config_xattrnames)
  986. list_del(pos);
  987. }
  988. }
  989. return error;
  990. }
  991. static struct security_hook_list evm_hooks[] __ro_after_init = {
  992. LSM_HOOK_INIT(inode_setattr, evm_inode_setattr),
  993. LSM_HOOK_INIT(inode_post_setattr, evm_inode_post_setattr),
  994. LSM_HOOK_INIT(inode_copy_up_xattr, evm_inode_copy_up_xattr),
  995. LSM_HOOK_INIT(inode_setxattr, evm_inode_setxattr),
  996. LSM_HOOK_INIT(inode_post_setxattr, evm_inode_post_setxattr),
  997. LSM_HOOK_INIT(inode_set_acl, evm_inode_set_acl),
  998. LSM_HOOK_INIT(inode_post_set_acl, evm_inode_post_set_acl),
  999. LSM_HOOK_INIT(inode_remove_acl, evm_inode_remove_acl),
  1000. LSM_HOOK_INIT(inode_post_remove_acl, evm_inode_post_remove_acl),
  1001. LSM_HOOK_INIT(inode_removexattr, evm_inode_removexattr),
  1002. LSM_HOOK_INIT(inode_post_removexattr, evm_inode_post_removexattr),
  1003. LSM_HOOK_INIT(inode_init_security, evm_inode_init_security),
  1004. LSM_HOOK_INIT(inode_alloc_security, evm_inode_alloc_security),
  1005. LSM_HOOK_INIT(file_release, evm_file_release),
  1006. LSM_HOOK_INIT(path_post_mknod, evm_post_path_mknod),
  1007. };
  1008. static const struct lsm_id evm_lsmid = {
  1009. .name = "evm",
  1010. .id = LSM_ID_EVM,
  1011. };
  1012. static int __init init_evm_lsm(void)
  1013. {
  1014. security_add_hooks(evm_hooks, ARRAY_SIZE(evm_hooks), &evm_lsmid);
  1015. return 0;
  1016. }
  1017. struct lsm_blob_sizes evm_blob_sizes __ro_after_init = {
  1018. .lbs_inode = sizeof(struct evm_iint_cache),
  1019. .lbs_xattr_count = 1,
  1020. };
  1021. DEFINE_LSM(evm) = {
  1022. .id = &evm_lsmid,
  1023. .init = init_evm_lsm,
  1024. .order = LSM_ORDER_LAST,
  1025. .blobs = &evm_blob_sizes,
  1026. .initcall_late = init_evm,
  1027. };