evm_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Copyright (C) 2005-2010 IBM Corporation
  3. *
  4. * Author:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. * Kylene Hall <kjhall@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 2 of the License.
  11. *
  12. * File: evm_main.c
  13. * implements evm_inode_setxattr, evm_inode_post_setxattr,
  14. * evm_inode_removexattr, and evm_verifyxattr
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/crypto.h>
  19. #include <linux/audit.h>
  20. #include <linux/xattr.h>
  21. #include <linux/integrity.h>
  22. #include <linux/evm.h>
  23. #include <linux/magic.h>
  24. #include <crypto/hash.h>
  25. #include <crypto/hash_info.h>
  26. #include <crypto/algapi.h>
  27. #include "evm.h"
  28. int evm_initialized;
  29. static const char * const integrity_status_msg[] = {
  30. "pass", "pass_immutable", "fail", "no_label", "no_xattrs", "unknown"
  31. };
  32. int evm_hmac_attrs;
  33. static struct xattr_list evm_config_default_xattrnames[] = {
  34. #ifdef CONFIG_SECURITY_SELINUX
  35. {.name = XATTR_NAME_SELINUX},
  36. #endif
  37. #ifdef CONFIG_SECURITY_SMACK
  38. {.name = XATTR_NAME_SMACK},
  39. #ifdef CONFIG_EVM_EXTRA_SMACK_XATTRS
  40. {.name = XATTR_NAME_SMACKEXEC},
  41. {.name = XATTR_NAME_SMACKTRANSMUTE},
  42. {.name = XATTR_NAME_SMACKMMAP},
  43. #endif
  44. #endif
  45. #ifdef CONFIG_SECURITY_APPARMOR
  46. {.name = XATTR_NAME_APPARMOR},
  47. #endif
  48. #ifdef CONFIG_IMA_APPRAISE
  49. {.name = XATTR_NAME_IMA},
  50. #endif
  51. {.name = XATTR_NAME_CAPS},
  52. };
  53. LIST_HEAD(evm_config_xattrnames);
  54. static int evm_fixmode;
  55. static int __init evm_set_fixmode(char *str)
  56. {
  57. if (strncmp(str, "fix", 3) == 0)
  58. evm_fixmode = 1;
  59. return 0;
  60. }
  61. __setup("evm=", evm_set_fixmode);
  62. static void __init evm_init_config(void)
  63. {
  64. int i, xattrs;
  65. xattrs = ARRAY_SIZE(evm_config_default_xattrnames);
  66. pr_info("Initialising EVM extended attributes:\n");
  67. for (i = 0; i < xattrs; i++) {
  68. pr_info("%s\n", evm_config_default_xattrnames[i].name);
  69. list_add_tail(&evm_config_default_xattrnames[i].list,
  70. &evm_config_xattrnames);
  71. }
  72. #ifdef CONFIG_EVM_ATTR_FSUUID
  73. evm_hmac_attrs |= EVM_ATTR_FSUUID;
  74. #endif
  75. pr_info("HMAC attrs: 0x%x\n", evm_hmac_attrs);
  76. }
  77. static bool evm_key_loaded(void)
  78. {
  79. return (bool)(evm_initialized & EVM_KEY_MASK);
  80. }
  81. static int evm_find_protected_xattrs(struct dentry *dentry)
  82. {
  83. struct inode *inode = d_backing_inode(dentry);
  84. struct xattr_list *xattr;
  85. int error;
  86. int count = 0;
  87. if (!(inode->i_opflags & IOP_XATTR))
  88. return -EOPNOTSUPP;
  89. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  90. error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
  91. if (error < 0) {
  92. if (error == -ENODATA)
  93. continue;
  94. return error;
  95. }
  96. count++;
  97. }
  98. return count;
  99. }
  100. /*
  101. * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
  102. *
  103. * Compute the HMAC on the dentry's protected set of extended attributes
  104. * and compare it against the stored security.evm xattr.
  105. *
  106. * For performance:
  107. * - use the previoulsy retrieved xattr value and length to calculate the
  108. * HMAC.)
  109. * - cache the verification result in the iint, when available.
  110. *
  111. * Returns integrity status
  112. */
  113. static enum integrity_status evm_verify_hmac(struct dentry *dentry,
  114. const char *xattr_name,
  115. char *xattr_value,
  116. size_t xattr_value_len,
  117. struct integrity_iint_cache *iint)
  118. {
  119. struct evm_ima_xattr_data *xattr_data = NULL;
  120. struct signature_v2_hdr *hdr;
  121. enum integrity_status evm_status = INTEGRITY_PASS;
  122. struct evm_digest digest;
  123. struct inode *inode;
  124. int rc, xattr_len;
  125. if (iint && (iint->evm_status == INTEGRITY_PASS ||
  126. iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
  127. return iint->evm_status;
  128. /* if status is not PASS, try to check again - against -ENOMEM */
  129. /* first need to know the sig type */
  130. rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
  131. GFP_NOFS);
  132. if (rc <= 0) {
  133. evm_status = INTEGRITY_FAIL;
  134. if (rc == -ENODATA) {
  135. rc = evm_find_protected_xattrs(dentry);
  136. if (rc > 0)
  137. evm_status = INTEGRITY_NOLABEL;
  138. else if (rc == 0)
  139. evm_status = INTEGRITY_NOXATTRS; /* new file */
  140. } else if (rc == -EOPNOTSUPP) {
  141. evm_status = INTEGRITY_UNKNOWN;
  142. }
  143. goto out;
  144. }
  145. xattr_len = rc;
  146. /* check value type */
  147. switch (xattr_data->type) {
  148. case EVM_XATTR_HMAC:
  149. if (xattr_len != sizeof(struct evm_ima_xattr_data)) {
  150. evm_status = INTEGRITY_FAIL;
  151. goto out;
  152. }
  153. digest.hdr.algo = HASH_ALGO_SHA1;
  154. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  155. xattr_value_len, &digest);
  156. if (rc)
  157. break;
  158. rc = crypto_memneq(xattr_data->digest, digest.digest,
  159. SHA1_DIGEST_SIZE);
  160. if (rc)
  161. rc = -EINVAL;
  162. break;
  163. case EVM_IMA_XATTR_DIGSIG:
  164. case EVM_XATTR_PORTABLE_DIGSIG:
  165. /* accept xattr with non-empty signature field */
  166. if (xattr_len <= sizeof(struct signature_v2_hdr)) {
  167. evm_status = INTEGRITY_FAIL;
  168. goto out;
  169. }
  170. hdr = (struct signature_v2_hdr *)xattr_data;
  171. digest.hdr.algo = hdr->hash_algo;
  172. rc = evm_calc_hash(dentry, xattr_name, xattr_value,
  173. xattr_value_len, xattr_data->type, &digest);
  174. if (rc)
  175. break;
  176. rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
  177. (const char *)xattr_data, xattr_len,
  178. digest.digest, digest.hdr.length);
  179. if (!rc) {
  180. inode = d_backing_inode(dentry);
  181. if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
  182. if (iint)
  183. iint->flags |= EVM_IMMUTABLE_DIGSIG;
  184. evm_status = INTEGRITY_PASS_IMMUTABLE;
  185. } else if (!IS_RDONLY(inode) &&
  186. !(inode->i_sb->s_readonly_remount) &&
  187. !IS_IMMUTABLE(inode)) {
  188. evm_update_evmxattr(dentry, xattr_name,
  189. xattr_value,
  190. xattr_value_len);
  191. }
  192. }
  193. break;
  194. default:
  195. rc = -EINVAL;
  196. break;
  197. }
  198. if (rc)
  199. evm_status = (rc == -ENODATA) ?
  200. INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
  201. out:
  202. if (iint)
  203. iint->evm_status = evm_status;
  204. kfree(xattr_data);
  205. return evm_status;
  206. }
  207. static int evm_protected_xattr(const char *req_xattr_name)
  208. {
  209. int namelen;
  210. int found = 0;
  211. struct xattr_list *xattr;
  212. namelen = strlen(req_xattr_name);
  213. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  214. if ((strlen(xattr->name) == namelen)
  215. && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
  216. found = 1;
  217. break;
  218. }
  219. if (strncmp(req_xattr_name,
  220. xattr->name + XATTR_SECURITY_PREFIX_LEN,
  221. strlen(req_xattr_name)) == 0) {
  222. found = 1;
  223. break;
  224. }
  225. }
  226. return found;
  227. }
  228. /**
  229. * evm_verifyxattr - verify the integrity of the requested xattr
  230. * @dentry: object of the verify xattr
  231. * @xattr_name: requested xattr
  232. * @xattr_value: requested xattr value
  233. * @xattr_value_len: requested xattr value length
  234. *
  235. * Calculate the HMAC for the given dentry and verify it against the stored
  236. * security.evm xattr. For performance, use the xattr value and length
  237. * previously retrieved to calculate the HMAC.
  238. *
  239. * Returns the xattr integrity status.
  240. *
  241. * This function requires the caller to lock the inode's i_mutex before it
  242. * is executed.
  243. */
  244. enum integrity_status evm_verifyxattr(struct dentry *dentry,
  245. const char *xattr_name,
  246. void *xattr_value, size_t xattr_value_len,
  247. struct integrity_iint_cache *iint)
  248. {
  249. if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
  250. return INTEGRITY_UNKNOWN;
  251. if (!iint) {
  252. iint = integrity_iint_find(d_backing_inode(dentry));
  253. if (!iint)
  254. return INTEGRITY_UNKNOWN;
  255. }
  256. return evm_verify_hmac(dentry, xattr_name, xattr_value,
  257. xattr_value_len, iint);
  258. }
  259. EXPORT_SYMBOL_GPL(evm_verifyxattr);
  260. /*
  261. * evm_verify_current_integrity - verify the dentry's metadata integrity
  262. * @dentry: pointer to the affected dentry
  263. *
  264. * Verify and return the dentry's metadata integrity. The exceptions are
  265. * before EVM is initialized or in 'fix' mode.
  266. */
  267. static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
  268. {
  269. struct inode *inode = d_backing_inode(dentry);
  270. if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
  271. return 0;
  272. return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
  273. }
  274. /*
  275. * evm_protect_xattr - protect the EVM extended attribute
  276. *
  277. * Prevent security.evm from being modified or removed without the
  278. * necessary permissions or when the existing value is invalid.
  279. *
  280. * The posix xattr acls are 'system' prefixed, which normally would not
  281. * affect security.evm. An interesting side affect of writing posix xattr
  282. * acls is their modifying of the i_mode, which is included in security.evm.
  283. * For posix xattr acls only, permit security.evm, even if it currently
  284. * doesn't exist, to be updated unless the EVM signature is immutable.
  285. */
  286. static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
  287. const void *xattr_value, size_t xattr_value_len)
  288. {
  289. enum integrity_status evm_status;
  290. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  291. if (!capable(CAP_SYS_ADMIN))
  292. return -EPERM;
  293. } else if (!evm_protected_xattr(xattr_name)) {
  294. if (!posix_xattr_acl(xattr_name))
  295. return 0;
  296. evm_status = evm_verify_current_integrity(dentry);
  297. if ((evm_status == INTEGRITY_PASS) ||
  298. (evm_status == INTEGRITY_NOXATTRS))
  299. return 0;
  300. goto out;
  301. }
  302. evm_status = evm_verify_current_integrity(dentry);
  303. if (evm_status == INTEGRITY_NOXATTRS) {
  304. struct integrity_iint_cache *iint;
  305. iint = integrity_iint_find(d_backing_inode(dentry));
  306. if (iint && (iint->flags & IMA_NEW_FILE))
  307. return 0;
  308. /* exception for pseudo filesystems */
  309. if (dentry->d_sb->s_magic == TMPFS_MAGIC
  310. || dentry->d_sb->s_magic == SYSFS_MAGIC)
  311. return 0;
  312. integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
  313. dentry->d_inode, dentry->d_name.name,
  314. "update_metadata",
  315. integrity_status_msg[evm_status],
  316. -EPERM, 0);
  317. }
  318. out:
  319. if (evm_status != INTEGRITY_PASS)
  320. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  321. dentry->d_name.name, "appraise_metadata",
  322. integrity_status_msg[evm_status],
  323. -EPERM, 0);
  324. return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
  325. }
  326. /**
  327. * evm_inode_setxattr - protect the EVM extended attribute
  328. * @dentry: pointer to the affected dentry
  329. * @xattr_name: pointer to the affected extended attribute name
  330. * @xattr_value: pointer to the new extended attribute value
  331. * @xattr_value_len: pointer to the new extended attribute value length
  332. *
  333. * Before allowing the 'security.evm' protected xattr to be updated,
  334. * verify the existing value is valid. As only the kernel should have
  335. * access to the EVM encrypted key needed to calculate the HMAC, prevent
  336. * userspace from writing HMAC value. Writing 'security.evm' requires
  337. * requires CAP_SYS_ADMIN privileges.
  338. */
  339. int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  340. const void *xattr_value, size_t xattr_value_len)
  341. {
  342. const struct evm_ima_xattr_data *xattr_data = xattr_value;
  343. /* Policy permits modification of the protected xattrs even though
  344. * there's no HMAC key loaded
  345. */
  346. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  347. return 0;
  348. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  349. if (!xattr_value_len)
  350. return -EINVAL;
  351. if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
  352. xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
  353. return -EPERM;
  354. }
  355. return evm_protect_xattr(dentry, xattr_name, xattr_value,
  356. xattr_value_len);
  357. }
  358. /**
  359. * evm_inode_removexattr - protect the EVM extended attribute
  360. * @dentry: pointer to the affected dentry
  361. * @xattr_name: pointer to the affected extended attribute name
  362. *
  363. * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
  364. * the current value is valid.
  365. */
  366. int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  367. {
  368. /* Policy permits modification of the protected xattrs even though
  369. * there's no HMAC key loaded
  370. */
  371. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  372. return 0;
  373. return evm_protect_xattr(dentry, xattr_name, NULL, 0);
  374. }
  375. static void evm_reset_status(struct inode *inode)
  376. {
  377. struct integrity_iint_cache *iint;
  378. iint = integrity_iint_find(inode);
  379. if (iint)
  380. iint->evm_status = INTEGRITY_UNKNOWN;
  381. }
  382. /**
  383. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  384. * @dentry: pointer to the affected dentry
  385. * @xattr_name: pointer to the affected extended attribute name
  386. * @xattr_value: pointer to the new extended attribute value
  387. * @xattr_value_len: pointer to the new extended attribute value length
  388. *
  389. * Update the HMAC stored in 'security.evm' to reflect the change.
  390. *
  391. * No need to take the i_mutex lock here, as this function is called from
  392. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  393. * i_mutex lock.
  394. */
  395. void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
  396. const void *xattr_value, size_t xattr_value_len)
  397. {
  398. if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
  399. && !posix_xattr_acl(xattr_name)))
  400. return;
  401. evm_reset_status(dentry->d_inode);
  402. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  403. }
  404. /**
  405. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  406. * @dentry: pointer to the affected dentry
  407. * @xattr_name: pointer to the affected extended attribute name
  408. *
  409. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  410. *
  411. * No need to take the i_mutex lock here, as this function is called from
  412. * vfs_removexattr() which takes the i_mutex.
  413. */
  414. void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
  415. {
  416. if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
  417. return;
  418. evm_reset_status(dentry->d_inode);
  419. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  420. }
  421. /**
  422. * evm_inode_setattr - prevent updating an invalid EVM extended attribute
  423. * @dentry: pointer to the affected dentry
  424. *
  425. * Permit update of file attributes when files have a valid EVM signature,
  426. * except in the case of them having an immutable portable signature.
  427. */
  428. int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
  429. {
  430. unsigned int ia_valid = attr->ia_valid;
  431. enum integrity_status evm_status;
  432. /* Policy permits modification of the protected attrs even though
  433. * there's no HMAC key loaded
  434. */
  435. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  436. return 0;
  437. if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
  438. return 0;
  439. evm_status = evm_verify_current_integrity(dentry);
  440. if ((evm_status == INTEGRITY_PASS) ||
  441. (evm_status == INTEGRITY_NOXATTRS))
  442. return 0;
  443. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  444. dentry->d_name.name, "appraise_metadata",
  445. integrity_status_msg[evm_status], -EPERM, 0);
  446. return -EPERM;
  447. }
  448. /**
  449. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  450. * @dentry: pointer to the affected dentry
  451. * @ia_valid: for the UID and GID status
  452. *
  453. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  454. * changes.
  455. *
  456. * This function is called from notify_change(), which expects the caller
  457. * to lock the inode's i_mutex.
  458. */
  459. void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
  460. {
  461. if (!evm_key_loaded())
  462. return;
  463. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  464. evm_update_evmxattr(dentry, NULL, NULL, 0);
  465. }
  466. /*
  467. * evm_inode_init_security - initializes security.evm
  468. */
  469. int evm_inode_init_security(struct inode *inode,
  470. const struct xattr *lsm_xattr,
  471. struct xattr *evm_xattr)
  472. {
  473. struct evm_ima_xattr_data *xattr_data;
  474. int rc;
  475. if (!evm_key_loaded() || !evm_protected_xattr(lsm_xattr->name))
  476. return 0;
  477. xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
  478. if (!xattr_data)
  479. return -ENOMEM;
  480. xattr_data->type = EVM_XATTR_HMAC;
  481. rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
  482. if (rc < 0)
  483. goto out;
  484. evm_xattr->value = xattr_data;
  485. evm_xattr->value_len = sizeof(*xattr_data);
  486. evm_xattr->name = XATTR_EVM_SUFFIX;
  487. return 0;
  488. out:
  489. kfree(xattr_data);
  490. return rc;
  491. }
  492. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  493. #ifdef CONFIG_EVM_LOAD_X509
  494. void __init evm_load_x509(void)
  495. {
  496. int rc;
  497. rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
  498. if (!rc)
  499. evm_initialized |= EVM_INIT_X509;
  500. }
  501. #endif
  502. static int __init init_evm(void)
  503. {
  504. int error;
  505. struct list_head *pos, *q;
  506. struct xattr_list *xattr;
  507. evm_init_config();
  508. error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
  509. if (error)
  510. goto error;
  511. error = evm_init_secfs();
  512. if (error < 0) {
  513. pr_info("Error registering secfs\n");
  514. goto error;
  515. }
  516. error:
  517. if (error != 0) {
  518. if (!list_empty(&evm_config_xattrnames)) {
  519. list_for_each_safe(pos, q, &evm_config_xattrnames) {
  520. xattr = list_entry(pos, struct xattr_list,
  521. list);
  522. list_del(pos);
  523. }
  524. }
  525. }
  526. return error;
  527. }
  528. late_initcall(init_evm);
  529. MODULE_DESCRIPTION("Extended Verification Module");
  530. MODULE_LICENSE("GPL");