evm_main.c 12 KB

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