evm_crypto.c 5.4 KB

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