evm_main.c 12 KB

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