evm_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_rcu(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. hdr = (struct signature_v2_hdr *)xattr_data;
  166. digest.hdr.algo = hdr->hash_algo;
  167. rc = evm_calc_hash(dentry, xattr_name, xattr_value,
  168. xattr_value_len, xattr_data->type, &digest);
  169. if (rc)
  170. break;
  171. rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
  172. (const char *)xattr_data, xattr_len,
  173. digest.digest, digest.hdr.length);
  174. if (!rc) {
  175. inode = d_backing_inode(dentry);
  176. if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
  177. if (iint)
  178. iint->flags |= EVM_IMMUTABLE_DIGSIG;
  179. evm_status = INTEGRITY_PASS_IMMUTABLE;
  180. } else if (!IS_RDONLY(inode) &&
  181. !(inode->i_sb->s_readonly_remount) &&
  182. !IS_IMMUTABLE(inode)) {
  183. evm_update_evmxattr(dentry, xattr_name,
  184. xattr_value,
  185. xattr_value_len);
  186. }
  187. }
  188. break;
  189. default:
  190. rc = -EINVAL;
  191. break;
  192. }
  193. if (rc)
  194. evm_status = (rc == -ENODATA) ?
  195. INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
  196. out:
  197. if (iint)
  198. iint->evm_status = evm_status;
  199. kfree(xattr_data);
  200. return evm_status;
  201. }
  202. static int evm_protected_xattr(const char *req_xattr_name)
  203. {
  204. int namelen;
  205. int found = 0;
  206. struct xattr_list *xattr;
  207. namelen = strlen(req_xattr_name);
  208. list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
  209. if ((strlen(xattr->name) == namelen)
  210. && (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
  211. found = 1;
  212. break;
  213. }
  214. if (strncmp(req_xattr_name,
  215. xattr->name + XATTR_SECURITY_PREFIX_LEN,
  216. strlen(req_xattr_name)) == 0) {
  217. found = 1;
  218. break;
  219. }
  220. }
  221. return found;
  222. }
  223. /**
  224. * evm_verifyxattr - verify the integrity of the requested xattr
  225. * @dentry: object of the verify xattr
  226. * @xattr_name: requested xattr
  227. * @xattr_value: requested xattr value
  228. * @xattr_value_len: requested xattr value length
  229. *
  230. * Calculate the HMAC for the given dentry and verify it against the stored
  231. * security.evm xattr. For performance, use the xattr value and length
  232. * previously retrieved to calculate the HMAC.
  233. *
  234. * Returns the xattr integrity status.
  235. *
  236. * This function requires the caller to lock the inode's i_mutex before it
  237. * is executed.
  238. */
  239. enum integrity_status evm_verifyxattr(struct dentry *dentry,
  240. const char *xattr_name,
  241. void *xattr_value, size_t xattr_value_len,
  242. struct integrity_iint_cache *iint)
  243. {
  244. if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
  245. return INTEGRITY_UNKNOWN;
  246. if (!iint) {
  247. iint = integrity_iint_find(d_backing_inode(dentry));
  248. if (!iint)
  249. return INTEGRITY_UNKNOWN;
  250. }
  251. return evm_verify_hmac(dentry, xattr_name, xattr_value,
  252. xattr_value_len, iint);
  253. }
  254. EXPORT_SYMBOL_GPL(evm_verifyxattr);
  255. /*
  256. * evm_verify_current_integrity - verify the dentry's metadata integrity
  257. * @dentry: pointer to the affected dentry
  258. *
  259. * Verify and return the dentry's metadata integrity. The exceptions are
  260. * before EVM is initialized or in 'fix' mode.
  261. */
  262. static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
  263. {
  264. struct inode *inode = d_backing_inode(dentry);
  265. if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
  266. return 0;
  267. return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
  268. }
  269. /*
  270. * evm_protect_xattr - protect the EVM extended attribute
  271. *
  272. * Prevent security.evm from being modified or removed without the
  273. * necessary permissions or when the existing value is invalid.
  274. *
  275. * The posix xattr acls are 'system' prefixed, which normally would not
  276. * affect security.evm. An interesting side affect of writing posix xattr
  277. * acls is their modifying of the i_mode, which is included in security.evm.
  278. * For posix xattr acls only, permit security.evm, even if it currently
  279. * doesn't exist, to be updated unless the EVM signature is immutable.
  280. */
  281. static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
  282. const void *xattr_value, size_t xattr_value_len)
  283. {
  284. enum integrity_status evm_status;
  285. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  286. if (!capable(CAP_SYS_ADMIN))
  287. return -EPERM;
  288. } else if (!evm_protected_xattr(xattr_name)) {
  289. if (!posix_xattr_acl(xattr_name))
  290. return 0;
  291. evm_status = evm_verify_current_integrity(dentry);
  292. if ((evm_status == INTEGRITY_PASS) ||
  293. (evm_status == INTEGRITY_NOXATTRS))
  294. return 0;
  295. goto out;
  296. }
  297. evm_status = evm_verify_current_integrity(dentry);
  298. if (evm_status == INTEGRITY_NOXATTRS) {
  299. struct integrity_iint_cache *iint;
  300. iint = integrity_iint_find(d_backing_inode(dentry));
  301. if (iint && (iint->flags & IMA_NEW_FILE))
  302. return 0;
  303. /* exception for pseudo filesystems */
  304. if (dentry->d_sb->s_magic == TMPFS_MAGIC
  305. || dentry->d_sb->s_magic == SYSFS_MAGIC)
  306. return 0;
  307. integrity_audit_msg(AUDIT_INTEGRITY_METADATA,
  308. dentry->d_inode, dentry->d_name.name,
  309. "update_metadata",
  310. integrity_status_msg[evm_status],
  311. -EPERM, 0);
  312. }
  313. out:
  314. if (evm_status != INTEGRITY_PASS)
  315. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  316. dentry->d_name.name, "appraise_metadata",
  317. integrity_status_msg[evm_status],
  318. -EPERM, 0);
  319. return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
  320. }
  321. /**
  322. * evm_inode_setxattr - protect the EVM extended attribute
  323. * @dentry: pointer to the affected dentry
  324. * @xattr_name: pointer to the affected extended attribute name
  325. * @xattr_value: pointer to the new extended attribute value
  326. * @xattr_value_len: pointer to the new extended attribute value length
  327. *
  328. * Before allowing the 'security.evm' protected xattr to be updated,
  329. * verify the existing value is valid. As only the kernel should have
  330. * access to the EVM encrypted key needed to calculate the HMAC, prevent
  331. * userspace from writing HMAC value. Writing 'security.evm' requires
  332. * requires CAP_SYS_ADMIN privileges.
  333. */
  334. int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  335. const void *xattr_value, size_t xattr_value_len)
  336. {
  337. const struct evm_ima_xattr_data *xattr_data = xattr_value;
  338. /* Policy permits modification of the protected xattrs even though
  339. * there's no HMAC key loaded
  340. */
  341. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  342. return 0;
  343. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  344. if (!xattr_value_len)
  345. return -EINVAL;
  346. if (xattr_data->type != EVM_IMA_XATTR_DIGSIG &&
  347. xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG)
  348. return -EPERM;
  349. }
  350. return evm_protect_xattr(dentry, xattr_name, xattr_value,
  351. xattr_value_len);
  352. }
  353. /**
  354. * evm_inode_removexattr - protect the EVM extended attribute
  355. * @dentry: pointer to the affected dentry
  356. * @xattr_name: pointer to the affected extended attribute name
  357. *
  358. * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
  359. * the current value is valid.
  360. */
  361. int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  362. {
  363. /* Policy permits modification of the protected xattrs even though
  364. * there's no HMAC key loaded
  365. */
  366. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  367. return 0;
  368. return evm_protect_xattr(dentry, xattr_name, NULL, 0);
  369. }
  370. static void evm_reset_status(struct inode *inode)
  371. {
  372. struct integrity_iint_cache *iint;
  373. iint = integrity_iint_find(inode);
  374. if (iint)
  375. iint->evm_status = INTEGRITY_UNKNOWN;
  376. }
  377. /**
  378. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  379. * @dentry: pointer to the affected dentry
  380. * @xattr_name: pointer to the affected extended attribute name
  381. * @xattr_value: pointer to the new extended attribute value
  382. * @xattr_value_len: pointer to the new extended attribute value length
  383. *
  384. * Update the HMAC stored in 'security.evm' to reflect the change.
  385. *
  386. * No need to take the i_mutex lock here, as this function is called from
  387. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  388. * i_mutex lock.
  389. */
  390. void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
  391. const void *xattr_value, size_t xattr_value_len)
  392. {
  393. if (!evm_key_loaded() || (!evm_protected_xattr(xattr_name)
  394. && !posix_xattr_acl(xattr_name)))
  395. return;
  396. evm_reset_status(dentry->d_inode);
  397. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  398. }
  399. /**
  400. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  401. * @dentry: pointer to the affected dentry
  402. * @xattr_name: pointer to the affected extended attribute name
  403. *
  404. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  405. *
  406. * No need to take the i_mutex lock here, as this function is called from
  407. * vfs_removexattr() which takes the i_mutex.
  408. */
  409. void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
  410. {
  411. if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
  412. return;
  413. evm_reset_status(dentry->d_inode);
  414. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  415. }
  416. /**
  417. * evm_inode_setattr - prevent updating an invalid EVM extended attribute
  418. * @dentry: pointer to the affected dentry
  419. *
  420. * Permit update of file attributes when files have a valid EVM signature,
  421. * except in the case of them having an immutable portable signature.
  422. */
  423. int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
  424. {
  425. unsigned int ia_valid = attr->ia_valid;
  426. enum integrity_status evm_status;
  427. /* Policy permits modification of the protected attrs even though
  428. * there's no HMAC key loaded
  429. */
  430. if (evm_initialized & EVM_ALLOW_METADATA_WRITES)
  431. return 0;
  432. if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
  433. return 0;
  434. evm_status = evm_verify_current_integrity(dentry);
  435. if ((evm_status == INTEGRITY_PASS) ||
  436. (evm_status == INTEGRITY_NOXATTRS))
  437. return 0;
  438. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  439. dentry->d_name.name, "appraise_metadata",
  440. integrity_status_msg[evm_status], -EPERM, 0);
  441. return -EPERM;
  442. }
  443. /**
  444. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  445. * @dentry: pointer to the affected dentry
  446. * @ia_valid: for the UID and GID status
  447. *
  448. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  449. * changes.
  450. *
  451. * This function is called from notify_change(), which expects the caller
  452. * to lock the inode's i_mutex.
  453. */
  454. void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
  455. {
  456. if (!evm_key_loaded())
  457. return;
  458. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  459. evm_update_evmxattr(dentry, NULL, NULL, 0);
  460. }
  461. /*
  462. * evm_inode_init_security - initializes security.evm
  463. */
  464. int evm_inode_init_security(struct inode *inode,
  465. const struct xattr *lsm_xattr,
  466. struct xattr *evm_xattr)
  467. {
  468. struct evm_ima_xattr_data *xattr_data;
  469. int rc;
  470. if (!evm_key_loaded() || !evm_protected_xattr(lsm_xattr->name))
  471. return 0;
  472. xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
  473. if (!xattr_data)
  474. return -ENOMEM;
  475. xattr_data->type = EVM_XATTR_HMAC;
  476. rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
  477. if (rc < 0)
  478. goto out;
  479. evm_xattr->value = xattr_data;
  480. evm_xattr->value_len = sizeof(*xattr_data);
  481. evm_xattr->name = XATTR_EVM_SUFFIX;
  482. return 0;
  483. out:
  484. kfree(xattr_data);
  485. return rc;
  486. }
  487. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  488. #ifdef CONFIG_EVM_LOAD_X509
  489. void __init evm_load_x509(void)
  490. {
  491. int rc;
  492. rc = integrity_load_x509(INTEGRITY_KEYRING_EVM, CONFIG_EVM_X509_PATH);
  493. if (!rc)
  494. evm_initialized |= EVM_INIT_X509;
  495. }
  496. #endif
  497. static int __init init_evm(void)
  498. {
  499. int error;
  500. struct list_head *pos, *q;
  501. struct xattr_list *xattr;
  502. evm_init_config();
  503. error = integrity_init_keyring(INTEGRITY_KEYRING_EVM);
  504. if (error)
  505. goto error;
  506. error = evm_init_secfs();
  507. if (error < 0) {
  508. pr_info("Error registering secfs\n");
  509. goto error;
  510. }
  511. error:
  512. if (error != 0) {
  513. if (!list_empty(&evm_config_xattrnames)) {
  514. list_for_each_safe(pos, q, &evm_config_xattrnames) {
  515. xattr = list_entry(pos, struct xattr_list,
  516. list);
  517. list_del(pos);
  518. }
  519. }
  520. }
  521. return error;
  522. }
  523. late_initcall(init_evm);
  524. MODULE_DESCRIPTION("Extended Verification Module");
  525. MODULE_LICENSE("GPL");