evm_crypto.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2005-2010 IBM Corporation
  3. *
  4. * Authors:
  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_crypto.c
  13. * Using root's kernel master key (kmk), calculate the HMAC
  14. */
  15. #include <linux/module.h>
  16. #include <linux/crypto.h>
  17. #include <linux/xattr.h>
  18. #include <keys/encrypted-type.h>
  19. #include <crypto/hash.h>
  20. #include "evm.h"
  21. #define EVMKEY "evm-key"
  22. #define MAX_KEY_SIZE 128
  23. static unsigned char evmkey[MAX_KEY_SIZE];
  24. static int evmkey_len = MAX_KEY_SIZE;
  25. struct crypto_shash *hmac_tfm;
  26. struct crypto_shash *hash_tfm;
  27. static DEFINE_MUTEX(mutex);
  28. static struct shash_desc *init_desc(char type)
  29. {
  30. long rc;
  31. char *algo;
  32. struct crypto_shash **tfm;
  33. struct shash_desc *desc;
  34. if (type == EVM_XATTR_HMAC) {
  35. tfm = &hmac_tfm;
  36. algo = evm_hmac;
  37. } else {
  38. tfm = &hash_tfm;
  39. algo = evm_hash;
  40. }
  41. if (*tfm == NULL) {
  42. mutex_lock(&mutex);
  43. if (*tfm)
  44. goto out;
  45. *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
  46. if (IS_ERR(*tfm)) {
  47. rc = PTR_ERR(*tfm);
  48. pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
  49. *tfm = NULL;
  50. mutex_unlock(&mutex);
  51. return ERR_PTR(rc);
  52. }
  53. if (type == EVM_XATTR_HMAC) {
  54. rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
  55. if (rc) {
  56. crypto_free_shash(*tfm);
  57. *tfm = NULL;
  58. mutex_unlock(&mutex);
  59. return ERR_PTR(rc);
  60. }
  61. }
  62. out:
  63. mutex_unlock(&mutex);
  64. }
  65. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
  66. GFP_KERNEL);
  67. if (!desc)
  68. return ERR_PTR(-ENOMEM);
  69. desc->tfm = *tfm;
  70. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  71. rc = crypto_shash_init(desc);
  72. if (rc) {
  73. kfree(desc);
  74. return ERR_PTR(rc);
  75. }
  76. return desc;
  77. }
  78. /* Protect against 'cutting & pasting' security.evm xattr, include inode
  79. * specific info.
  80. *
  81. * (Additional directory/file metadata needs to be added for more complete
  82. * protection.)
  83. */
  84. static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
  85. char *digest)
  86. {
  87. struct h_misc {
  88. unsigned long ino;
  89. __u32 generation;
  90. uid_t uid;
  91. gid_t gid;
  92. umode_t mode;
  93. } hmac_misc;
  94. memset(&hmac_misc, 0, sizeof hmac_misc);
  95. hmac_misc.ino = inode->i_ino;
  96. hmac_misc.generation = inode->i_generation;
  97. hmac_misc.uid = inode->i_uid;
  98. hmac_misc.gid = inode->i_gid;
  99. hmac_misc.mode = inode->i_mode;
  100. crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof hmac_misc);
  101. crypto_shash_final(desc, digest);
  102. }
  103. /*
  104. * Calculate the HMAC value across the set of protected security xattrs.
  105. *
  106. * Instead of retrieving the requested xattr, for performance, calculate
  107. * the hmac using the requested xattr value. Don't alloc/free memory for
  108. * each xattr, but attempt to re-use the previously allocated memory.
  109. */
  110. static int evm_calc_hmac_or_hash(struct dentry *dentry,
  111. const char *req_xattr_name,
  112. const char *req_xattr_value,
  113. size_t req_xattr_value_len,
  114. char type, char *digest)
  115. {
  116. struct inode *inode = dentry->d_inode;
  117. struct shash_desc *desc;
  118. char **xattrname;
  119. size_t xattr_size = 0;
  120. char *xattr_value = NULL;
  121. int error;
  122. int size;
  123. if (!inode->i_op || !inode->i_op->getxattr)
  124. return -EOPNOTSUPP;
  125. desc = init_desc(type);
  126. if (IS_ERR(desc))
  127. return PTR_ERR(desc);
  128. error = -ENODATA;
  129. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
  130. if ((req_xattr_name && req_xattr_value)
  131. && !strcmp(*xattrname, req_xattr_name)) {
  132. error = 0;
  133. crypto_shash_update(desc, (const u8 *)req_xattr_value,
  134. req_xattr_value_len);
  135. continue;
  136. }
  137. size = vfs_getxattr_alloc(dentry, *xattrname,
  138. &xattr_value, xattr_size, GFP_NOFS);
  139. if (size == -ENOMEM) {
  140. error = -ENOMEM;
  141. goto out;
  142. }
  143. if (size < 0)
  144. continue;
  145. error = 0;
  146. xattr_size = size;
  147. crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
  148. }
  149. hmac_add_misc(desc, inode, digest);
  150. out:
  151. kfree(xattr_value);
  152. kfree(desc);
  153. return error;
  154. }
  155. int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
  156. const char *req_xattr_value, size_t req_xattr_value_len,
  157. char *digest)
  158. {
  159. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  160. req_xattr_value_len, EVM_XATTR_HMAC, digest);
  161. }
  162. int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
  163. const char *req_xattr_value, size_t req_xattr_value_len,
  164. char *digest)
  165. {
  166. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  167. req_xattr_value_len, IMA_XATTR_DIGEST, digest);
  168. }
  169. /*
  170. * Calculate the hmac and update security.evm xattr
  171. *
  172. * Expects to be called with i_mutex locked.
  173. */
  174. int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
  175. const char *xattr_value, size_t xattr_value_len)
  176. {
  177. struct inode *inode = dentry->d_inode;
  178. struct evm_ima_xattr_data xattr_data;
  179. int rc = 0;
  180. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  181. xattr_value_len, xattr_data.digest);
  182. if (rc == 0) {
  183. xattr_data.type = EVM_XATTR_HMAC;
  184. rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
  185. &xattr_data,
  186. sizeof(xattr_data), 0);
  187. } else if (rc == -ENODATA && inode->i_op->removexattr) {
  188. rc = inode->i_op->removexattr(dentry, XATTR_NAME_EVM);
  189. }
  190. return rc;
  191. }
  192. int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
  193. char *hmac_val)
  194. {
  195. struct shash_desc *desc;
  196. desc = init_desc(EVM_XATTR_HMAC);
  197. if (IS_ERR(desc)) {
  198. printk(KERN_INFO "init_desc failed\n");
  199. return PTR_ERR(desc);
  200. }
  201. crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
  202. hmac_add_misc(desc, inode, hmac_val);
  203. kfree(desc);
  204. return 0;
  205. }
  206. /*
  207. * Get the key from the TPM for the SHA1-HMAC
  208. */
  209. int evm_init_key(void)
  210. {
  211. struct key *evm_key;
  212. struct encrypted_key_payload *ekp;
  213. int rc = 0;
  214. evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
  215. if (IS_ERR(evm_key))
  216. return -ENOENT;
  217. down_read(&evm_key->sem);
  218. ekp = evm_key->payload.data;
  219. if (ekp->decrypted_datalen > MAX_KEY_SIZE) {
  220. rc = -EINVAL;
  221. goto out;
  222. }
  223. memcpy(evmkey, ekp->decrypted_data, ekp->decrypted_datalen);
  224. out:
  225. /* burn the original key contents */
  226. memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
  227. up_read(&evm_key->sem);
  228. key_put(evm_key);
  229. return rc;
  230. }