evm_main.c 12 KB

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