evm_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. * Updating 'security.evm' requires CAP_SYS_ADMIN privileges and that
  250. * the current value is valid.
  251. */
  252. int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  253. const void *xattr_value, size_t xattr_value_len)
  254. {
  255. return evm_protect_xattr(dentry, xattr_name, xattr_value,
  256. xattr_value_len);
  257. }
  258. /**
  259. * evm_inode_removexattr - protect the EVM extended attribute
  260. * @dentry: pointer to the affected dentry
  261. * @xattr_name: pointer to the affected extended attribute name
  262. *
  263. * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
  264. * the current value is valid.
  265. */
  266. int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  267. {
  268. return evm_protect_xattr(dentry, xattr_name, NULL, 0);
  269. }
  270. /**
  271. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  272. * @dentry: pointer to the affected dentry
  273. * @xattr_name: pointer to the affected extended attribute name
  274. * @xattr_value: pointer to the new extended attribute value
  275. * @xattr_value_len: pointer to the new extended attribute value length
  276. *
  277. * Update the HMAC stored in 'security.evm' to reflect the change.
  278. *
  279. * No need to take the i_mutex lock here, as this function is called from
  280. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  281. * i_mutex lock.
  282. */
  283. void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
  284. const void *xattr_value, size_t xattr_value_len)
  285. {
  286. if (!evm_initialized || (!evm_protected_xattr(xattr_name)
  287. && !posix_xattr_acl(xattr_name)))
  288. return;
  289. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  290. return;
  291. }
  292. /**
  293. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  294. * @dentry: pointer to the affected dentry
  295. * @xattr_name: pointer to the affected extended attribute name
  296. *
  297. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  298. */
  299. void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
  300. {
  301. struct inode *inode = dentry->d_inode;
  302. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  303. return;
  304. mutex_lock(&inode->i_mutex);
  305. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  306. mutex_unlock(&inode->i_mutex);
  307. return;
  308. }
  309. /**
  310. * evm_inode_setattr - prevent updating an invalid EVM extended attribute
  311. * @dentry: pointer to the affected dentry
  312. */
  313. int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
  314. {
  315. unsigned int ia_valid = attr->ia_valid;
  316. enum integrity_status evm_status;
  317. if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
  318. return 0;
  319. evm_status = evm_verify_current_integrity(dentry);
  320. if ((evm_status == INTEGRITY_PASS) ||
  321. (evm_status == INTEGRITY_NOXATTRS))
  322. return 0;
  323. return -EPERM;
  324. }
  325. /**
  326. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  327. * @dentry: pointer to the affected dentry
  328. * @ia_valid: for the UID and GID status
  329. *
  330. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  331. * changes.
  332. *
  333. * This function is called from notify_change(), which expects the caller
  334. * to lock the inode's i_mutex.
  335. */
  336. void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
  337. {
  338. if (!evm_initialized)
  339. return;
  340. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  341. evm_update_evmxattr(dentry, NULL, NULL, 0);
  342. return;
  343. }
  344. /*
  345. * evm_inode_init_security - initializes security.evm
  346. */
  347. int evm_inode_init_security(struct inode *inode,
  348. const struct xattr *lsm_xattr,
  349. struct xattr *evm_xattr)
  350. {
  351. struct evm_ima_xattr_data *xattr_data;
  352. int rc;
  353. if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
  354. return 0;
  355. xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
  356. if (!xattr_data)
  357. return -ENOMEM;
  358. xattr_data->type = EVM_XATTR_HMAC;
  359. rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
  360. if (rc < 0)
  361. goto out;
  362. evm_xattr->value = xattr_data;
  363. evm_xattr->value_len = sizeof(*xattr_data);
  364. evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS);
  365. return 0;
  366. out:
  367. kfree(xattr_data);
  368. return rc;
  369. }
  370. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  371. static int __init init_evm(void)
  372. {
  373. int error;
  374. error = evm_init_secfs();
  375. if (error < 0) {
  376. printk(KERN_INFO "EVM: Error registering secfs\n");
  377. goto err;
  378. }
  379. return 0;
  380. err:
  381. return error;
  382. }
  383. static void __exit cleanup_evm(void)
  384. {
  385. evm_cleanup_secfs();
  386. if (hmac_tfm)
  387. crypto_free_shash(hmac_tfm);
  388. if (hash_tfm)
  389. crypto_free_shash(hash_tfm);
  390. }
  391. /*
  392. * evm_display_config - list the EVM protected security extended attributes
  393. */
  394. static int __init evm_display_config(void)
  395. {
  396. char **xattrname;
  397. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
  398. printk(KERN_INFO "EVM: %s\n", *xattrname);
  399. return 0;
  400. }
  401. pure_initcall(evm_display_config);
  402. late_initcall(init_evm);
  403. MODULE_DESCRIPTION("Extended Verification Module");
  404. MODULE_LICENSE("GPL");