evm_crypto.c 5.1 KB

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