evm_main.c 10 KB

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