evm_crypto.c 5.1 KB

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