evm_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. }
  273. out:
  274. if (evm_status != INTEGRITY_PASS)
  275. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  276. dentry->d_name.name, "appraise_metadata",
  277. integrity_status_msg[evm_status],
  278. -EPERM, 0);
  279. return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
  280. }
  281. /**
  282. * evm_inode_setxattr - protect the EVM extended attribute
  283. * @dentry: pointer to the affected dentry
  284. * @xattr_name: pointer to the affected extended attribute name
  285. * @xattr_value: pointer to the new extended attribute value
  286. * @xattr_value_len: pointer to the new extended attribute value length
  287. *
  288. * Before allowing the 'security.evm' protected xattr to be updated,
  289. * verify the existing value is valid. As only the kernel should have
  290. * access to the EVM encrypted key needed to calculate the HMAC, prevent
  291. * userspace from writing HMAC value. Writing 'security.evm' requires
  292. * requires CAP_SYS_ADMIN privileges.
  293. */
  294. int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  295. const void *xattr_value, size_t xattr_value_len)
  296. {
  297. const struct evm_ima_xattr_data *xattr_data = xattr_value;
  298. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  299. if (!xattr_value_len)
  300. return -EINVAL;
  301. if (xattr_data->type != EVM_IMA_XATTR_DIGSIG)
  302. return -EPERM;
  303. }
  304. return evm_protect_xattr(dentry, xattr_name, xattr_value,
  305. xattr_value_len);
  306. }
  307. /**
  308. * evm_inode_removexattr - protect the EVM extended attribute
  309. * @dentry: pointer to the affected dentry
  310. * @xattr_name: pointer to the affected extended attribute name
  311. *
  312. * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
  313. * the current value is valid.
  314. */
  315. int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  316. {
  317. return evm_protect_xattr(dentry, xattr_name, NULL, 0);
  318. }
  319. /**
  320. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  321. * @dentry: pointer to the affected dentry
  322. * @xattr_name: pointer to the affected extended attribute name
  323. * @xattr_value: pointer to the new extended attribute value
  324. * @xattr_value_len: pointer to the new extended attribute value length
  325. *
  326. * Update the HMAC stored in 'security.evm' to reflect the change.
  327. *
  328. * No need to take the i_mutex lock here, as this function is called from
  329. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  330. * i_mutex lock.
  331. */
  332. void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
  333. const void *xattr_value, size_t xattr_value_len)
  334. {
  335. if (!evm_initialized || (!evm_protected_xattr(xattr_name)
  336. && !posix_xattr_acl(xattr_name)))
  337. return;
  338. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  339. }
  340. /**
  341. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  342. * @dentry: pointer to the affected dentry
  343. * @xattr_name: pointer to the affected extended attribute name
  344. *
  345. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  346. */
  347. void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
  348. {
  349. struct inode *inode = d_backing_inode(dentry);
  350. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  351. return;
  352. mutex_lock(&inode->i_mutex);
  353. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  354. mutex_unlock(&inode->i_mutex);
  355. }
  356. /**
  357. * evm_inode_setattr - prevent updating an invalid EVM extended attribute
  358. * @dentry: pointer to the affected dentry
  359. */
  360. int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
  361. {
  362. unsigned int ia_valid = attr->ia_valid;
  363. enum integrity_status evm_status;
  364. if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
  365. return 0;
  366. evm_status = evm_verify_current_integrity(dentry);
  367. if ((evm_status == INTEGRITY_PASS) ||
  368. (evm_status == INTEGRITY_NOXATTRS))
  369. return 0;
  370. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry),
  371. dentry->d_name.name, "appraise_metadata",
  372. integrity_status_msg[evm_status], -EPERM, 0);
  373. return -EPERM;
  374. }
  375. /**
  376. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  377. * @dentry: pointer to the affected dentry
  378. * @ia_valid: for the UID and GID status
  379. *
  380. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  381. * changes.
  382. *
  383. * This function is called from notify_change(), which expects the caller
  384. * to lock the inode's i_mutex.
  385. */
  386. void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
  387. {
  388. if (!evm_initialized)
  389. return;
  390. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  391. evm_update_evmxattr(dentry, NULL, NULL, 0);
  392. }
  393. /*
  394. * evm_inode_init_security - initializes security.evm
  395. */
  396. int evm_inode_init_security(struct inode *inode,
  397. const struct xattr *lsm_xattr,
  398. struct xattr *evm_xattr)
  399. {
  400. struct evm_ima_xattr_data *xattr_data;
  401. int rc;
  402. if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
  403. return 0;
  404. xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
  405. if (!xattr_data)
  406. return -ENOMEM;
  407. xattr_data->type = EVM_XATTR_HMAC;
  408. rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
  409. if (rc < 0)
  410. goto out;
  411. evm_xattr->value = xattr_data;
  412. evm_xattr->value_len = sizeof(*xattr_data);
  413. evm_xattr->name = XATTR_EVM_SUFFIX;
  414. return 0;
  415. out:
  416. kfree(xattr_data);
  417. return rc;
  418. }
  419. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  420. static int __init init_evm(void)
  421. {
  422. int error;
  423. evm_init_config();
  424. error = evm_init_secfs();
  425. if (error < 0) {
  426. pr_info("Error registering secfs\n");
  427. goto err;
  428. }
  429. return 0;
  430. err:
  431. return error;
  432. }
  433. /*
  434. * evm_display_config - list the EVM protected security extended attributes
  435. */
  436. static int __init evm_display_config(void)
  437. {
  438. char **xattrname;
  439. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
  440. pr_info("%s\n", *xattrname);
  441. return 0;
  442. }
  443. pure_initcall(evm_display_config);
  444. late_initcall(init_evm);
  445. MODULE_DESCRIPTION("Extended Verification Module");
  446. MODULE_LICENSE("GPL");