evm_main.c 8.5 KB

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