evm_main.c 14 KB

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