evm_main.c 14 KB

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