evm_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /*
  36. * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
  37. *
  38. * Compute the HMAC on the dentry's protected set of extended attributes
  39. * and compare it against the stored security.evm xattr. (For performance,
  40. * use the previoulsy retrieved xattr value and length to calculate the
  41. * HMAC.)
  42. *
  43. * Returns integrity status
  44. */
  45. static enum integrity_status evm_verify_hmac(struct dentry *dentry,
  46. const char *xattr_name,
  47. char *xattr_value,
  48. size_t xattr_value_len,
  49. struct integrity_iint_cache *iint)
  50. {
  51. struct evm_ima_xattr_data xattr_data;
  52. int rc;
  53. if (iint->hmac_status == INTEGRITY_PASS)
  54. return iint->hmac_status;
  55. /* if status is not PASS, try to check again - against -ENOMEM */
  56. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  57. xattr_value_len, xattr_data.digest);
  58. if (rc < 0)
  59. goto err_out;
  60. xattr_data.type = EVM_XATTR_HMAC;
  61. rc = vfs_xattr_cmp(dentry, XATTR_NAME_EVM, (u8 *)&xattr_data,
  62. sizeof xattr_data, GFP_NOFS);
  63. if (rc < 0)
  64. goto err_out;
  65. iint->hmac_status = INTEGRITY_PASS;
  66. return iint->hmac_status;
  67. err_out:
  68. switch (rc) {
  69. case -ENODATA: /* file not labelled */
  70. iint->hmac_status = INTEGRITY_NOLABEL;
  71. break;
  72. default:
  73. iint->hmac_status = INTEGRITY_FAIL;
  74. }
  75. return iint->hmac_status;
  76. }
  77. static int evm_protected_xattr(const char *req_xattr_name)
  78. {
  79. char **xattrname;
  80. int namelen;
  81. int found = 0;
  82. namelen = strlen(req_xattr_name);
  83. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
  84. if ((strlen(*xattrname) == namelen)
  85. && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
  86. found = 1;
  87. break;
  88. }
  89. if (strncmp(req_xattr_name,
  90. *xattrname + XATTR_SECURITY_PREFIX_LEN,
  91. strlen(req_xattr_name)) == 0) {
  92. found = 1;
  93. break;
  94. }
  95. }
  96. return found;
  97. }
  98. /**
  99. * evm_verifyxattr - verify the integrity of the requested xattr
  100. * @dentry: object of the verify xattr
  101. * @xattr_name: requested xattr
  102. * @xattr_value: requested xattr value
  103. * @xattr_value_len: requested xattr value length
  104. *
  105. * Calculate the HMAC for the given dentry and verify it against the stored
  106. * security.evm xattr. For performance, use the xattr value and length
  107. * previously retrieved to calculate the HMAC.
  108. *
  109. * Returns the xattr integrity status.
  110. *
  111. * This function requires the caller to lock the inode's i_mutex before it
  112. * is executed.
  113. */
  114. enum integrity_status evm_verifyxattr(struct dentry *dentry,
  115. const char *xattr_name,
  116. void *xattr_value, size_t xattr_value_len,
  117. struct integrity_iint_cache *iint)
  118. {
  119. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  120. return INTEGRITY_UNKNOWN;
  121. if (!iint) {
  122. iint = integrity_iint_find(dentry->d_inode);
  123. if (!iint)
  124. return INTEGRITY_UNKNOWN;
  125. }
  126. return evm_verify_hmac(dentry, xattr_name, xattr_value,
  127. xattr_value_len, iint);
  128. }
  129. EXPORT_SYMBOL_GPL(evm_verifyxattr);
  130. /*
  131. * evm_protect_xattr - protect the EVM extended attribute
  132. *
  133. * Prevent security.evm from being modified or removed.
  134. */
  135. static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
  136. const void *xattr_value, size_t xattr_value_len)
  137. {
  138. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  139. if (!capable(CAP_SYS_ADMIN))
  140. return -EPERM;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * evm_inode_setxattr - protect the EVM extended attribute
  146. * @dentry: pointer to the affected dentry
  147. * @xattr_name: pointer to the affected extended attribute name
  148. * @xattr_value: pointer to the new extended attribute value
  149. * @xattr_value_len: pointer to the new extended attribute value length
  150. *
  151. * Prevent 'security.evm' from being modified
  152. */
  153. int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  154. const void *xattr_value, size_t xattr_value_len)
  155. {
  156. return evm_protect_xattr(dentry, xattr_name, xattr_value,
  157. xattr_value_len);
  158. }
  159. /**
  160. * evm_inode_removexattr - protect the EVM extended attribute
  161. * @dentry: pointer to the affected dentry
  162. * @xattr_name: pointer to the affected extended attribute name
  163. *
  164. * Prevent 'security.evm' from being removed.
  165. */
  166. int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  167. {
  168. return evm_protect_xattr(dentry, xattr_name, NULL, 0);
  169. }
  170. /**
  171. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  172. * @dentry: pointer to the affected dentry
  173. * @xattr_name: pointer to the affected extended attribute name
  174. * @xattr_value: pointer to the new extended attribute value
  175. * @xattr_value_len: pointer to the new extended attribute value length
  176. *
  177. * Update the HMAC stored in 'security.evm' to reflect the change.
  178. *
  179. * No need to take the i_mutex lock here, as this function is called from
  180. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  181. * i_mutex lock.
  182. */
  183. void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
  184. const void *xattr_value, size_t xattr_value_len)
  185. {
  186. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  187. return;
  188. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  189. return;
  190. }
  191. /**
  192. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  193. * @dentry: pointer to the affected dentry
  194. * @xattr_name: pointer to the affected extended attribute name
  195. *
  196. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  197. */
  198. void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
  199. {
  200. struct inode *inode = dentry->d_inode;
  201. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  202. return;
  203. mutex_lock(&inode->i_mutex);
  204. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  205. mutex_unlock(&inode->i_mutex);
  206. return;
  207. }
  208. /**
  209. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  210. * @dentry: pointer to the affected dentry
  211. * @ia_valid: for the UID and GID status
  212. *
  213. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  214. * changes.
  215. *
  216. * This function is called from notify_change(), which expects the caller
  217. * to lock the inode's i_mutex.
  218. */
  219. void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
  220. {
  221. if (!evm_initialized)
  222. return;
  223. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  224. evm_update_evmxattr(dentry, NULL, NULL, 0);
  225. return;
  226. }
  227. /*
  228. * evm_inode_init_security - initializes security.evm
  229. */
  230. int evm_inode_init_security(struct inode *inode,
  231. const struct xattr *lsm_xattr,
  232. struct xattr *evm_xattr)
  233. {
  234. struct evm_ima_xattr_data *xattr_data;
  235. int rc;
  236. if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
  237. return -EOPNOTSUPP;
  238. xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
  239. if (!xattr_data)
  240. return -ENOMEM;
  241. xattr_data->type = EVM_XATTR_HMAC;
  242. rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
  243. if (rc < 0)
  244. goto out;
  245. evm_xattr->value = xattr_data;
  246. evm_xattr->value_len = sizeof(*xattr_data);
  247. evm_xattr->name = kstrdup(XATTR_EVM_SUFFIX, GFP_NOFS);
  248. return 0;
  249. out:
  250. kfree(xattr_data);
  251. return rc;
  252. }
  253. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  254. static int __init init_evm(void)
  255. {
  256. int error;
  257. error = evm_init_secfs();
  258. if (error < 0) {
  259. printk(KERN_INFO "EVM: Error registering secfs\n");
  260. goto err;
  261. }
  262. err:
  263. return error;
  264. }
  265. static void __exit cleanup_evm(void)
  266. {
  267. evm_cleanup_secfs();
  268. if (hmac_tfm)
  269. crypto_free_shash(hmac_tfm);
  270. }
  271. /*
  272. * evm_display_config - list the EVM protected security extended attributes
  273. */
  274. static int __init evm_display_config(void)
  275. {
  276. char **xattrname;
  277. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
  278. printk(KERN_INFO "EVM: %s\n", *xattrname);
  279. return 0;
  280. }
  281. pure_initcall(evm_display_config);
  282. late_initcall(init_evm);
  283. MODULE_DESCRIPTION("Extended Verification Module");
  284. MODULE_LICENSE("GPL");