evm_crypto.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/crypto.h>
  18. #include <linux/xattr.h>
  19. #include <linux/evm.h>
  20. #include <keys/encrypted-type.h>
  21. #include <crypto/hash.h>
  22. #include "evm.h"
  23. #define EVMKEY "evm-key"
  24. #define MAX_KEY_SIZE 128
  25. static unsigned char evmkey[MAX_KEY_SIZE];
  26. static int evmkey_len = MAX_KEY_SIZE;
  27. struct crypto_shash *hmac_tfm;
  28. struct crypto_shash *hash_tfm;
  29. static DEFINE_MUTEX(mutex);
  30. #define EVM_SET_KEY_BUSY 0
  31. static unsigned long evm_set_key_flags;
  32. /**
  33. * evm_set_key() - set EVM HMAC key from the kernel
  34. * @key: pointer to a buffer with the key data
  35. * @size: length of the key data
  36. *
  37. * This function allows setting the EVM HMAC key from the kernel
  38. * without using the "encrypted" key subsystem keys. It can be used
  39. * by the crypto HW kernel module which has its own way of managing
  40. * keys.
  41. *
  42. * key length should be between 32 and 128 bytes long
  43. */
  44. int evm_set_key(void *key, size_t keylen)
  45. {
  46. int rc;
  47. rc = -EBUSY;
  48. if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
  49. goto busy;
  50. rc = -EINVAL;
  51. if (keylen > MAX_KEY_SIZE)
  52. goto inval;
  53. memcpy(evmkey, key, keylen);
  54. evm_initialized |= EVM_INIT_HMAC;
  55. pr_info("key initialized\n");
  56. return 0;
  57. inval:
  58. clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
  59. busy:
  60. pr_err("key initialization failed\n");
  61. return rc;
  62. }
  63. EXPORT_SYMBOL_GPL(evm_set_key);
  64. static struct shash_desc *init_desc(char type)
  65. {
  66. long rc;
  67. char *algo;
  68. struct crypto_shash **tfm;
  69. struct shash_desc *desc;
  70. if (type == EVM_XATTR_HMAC) {
  71. if (!(evm_initialized & EVM_INIT_HMAC)) {
  72. pr_err("HMAC key is not set\n");
  73. return ERR_PTR(-ENOKEY);
  74. }
  75. tfm = &hmac_tfm;
  76. algo = evm_hmac;
  77. } else {
  78. tfm = &hash_tfm;
  79. algo = evm_hash;
  80. }
  81. if (*tfm == NULL) {
  82. mutex_lock(&mutex);
  83. if (*tfm)
  84. goto out;
  85. *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
  86. if (IS_ERR(*tfm)) {
  87. rc = PTR_ERR(*tfm);
  88. pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
  89. *tfm = NULL;
  90. mutex_unlock(&mutex);
  91. return ERR_PTR(rc);
  92. }
  93. if (type == EVM_XATTR_HMAC) {
  94. rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
  95. if (rc) {
  96. crypto_free_shash(*tfm);
  97. *tfm = NULL;
  98. mutex_unlock(&mutex);
  99. return ERR_PTR(rc);
  100. }
  101. }
  102. out:
  103. mutex_unlock(&mutex);
  104. }
  105. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
  106. GFP_KERNEL);
  107. if (!desc)
  108. return ERR_PTR(-ENOMEM);
  109. desc->tfm = *tfm;
  110. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  111. rc = crypto_shash_init(desc);
  112. if (rc) {
  113. kfree(desc);
  114. return ERR_PTR(rc);
  115. }
  116. return desc;
  117. }
  118. /* Protect against 'cutting & pasting' security.evm xattr, include inode
  119. * specific info.
  120. *
  121. * (Additional directory/file metadata needs to be added for more complete
  122. * protection.)
  123. */
  124. static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
  125. char *digest)
  126. {
  127. struct h_misc {
  128. unsigned long ino;
  129. __u32 generation;
  130. uid_t uid;
  131. gid_t gid;
  132. umode_t mode;
  133. } hmac_misc;
  134. memset(&hmac_misc, 0, sizeof(hmac_misc));
  135. hmac_misc.ino = inode->i_ino;
  136. hmac_misc.generation = inode->i_generation;
  137. /* The hmac uid and gid must be encoded in the initial user
  138. * namespace (not the filesystems user namespace) as encoding
  139. * them in the filesystems user namespace allows an attack
  140. * where first they are written in an unprivileged fuse mount
  141. * of a filesystem and then the system is tricked to mount the
  142. * filesystem for real on next boot and trust it because
  143. * everything is signed.
  144. */
  145. hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
  146. hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
  147. hmac_misc.mode = inode->i_mode;
  148. crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
  149. if (evm_hmac_attrs & EVM_ATTR_FSUUID)
  150. crypto_shash_update(desc, inode->i_sb->s_uuid,
  151. sizeof(inode->i_sb->s_uuid));
  152. crypto_shash_final(desc, digest);
  153. }
  154. /*
  155. * Calculate the HMAC value across the set of protected security xattrs.
  156. *
  157. * Instead of retrieving the requested xattr, for performance, calculate
  158. * the hmac using the requested xattr value. Don't alloc/free memory for
  159. * each xattr, but attempt to re-use the previously allocated memory.
  160. */
  161. static int evm_calc_hmac_or_hash(struct dentry *dentry,
  162. const char *req_xattr_name,
  163. const char *req_xattr_value,
  164. size_t req_xattr_value_len,
  165. char type, char *digest)
  166. {
  167. struct inode *inode = d_backing_inode(dentry);
  168. struct shash_desc *desc;
  169. char **xattrname;
  170. size_t xattr_size = 0;
  171. char *xattr_value = NULL;
  172. int error;
  173. int size;
  174. if (!(inode->i_opflags & IOP_XATTR))
  175. return -EOPNOTSUPP;
  176. desc = init_desc(type);
  177. if (IS_ERR(desc))
  178. return PTR_ERR(desc);
  179. error = -ENODATA;
  180. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
  181. if ((req_xattr_name && req_xattr_value)
  182. && !strcmp(*xattrname, req_xattr_name)) {
  183. error = 0;
  184. crypto_shash_update(desc, (const u8 *)req_xattr_value,
  185. req_xattr_value_len);
  186. continue;
  187. }
  188. size = vfs_getxattr_alloc(dentry, *xattrname,
  189. &xattr_value, xattr_size, GFP_NOFS);
  190. if (size == -ENOMEM) {
  191. error = -ENOMEM;
  192. goto out;
  193. }
  194. if (size < 0)
  195. continue;
  196. error = 0;
  197. xattr_size = size;
  198. crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
  199. }
  200. hmac_add_misc(desc, inode, digest);
  201. out:
  202. kfree(xattr_value);
  203. kfree(desc);
  204. return error;
  205. }
  206. int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
  207. const char *req_xattr_value, size_t req_xattr_value_len,
  208. char *digest)
  209. {
  210. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  211. req_xattr_value_len, EVM_XATTR_HMAC, digest);
  212. }
  213. int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
  214. const char *req_xattr_value, size_t req_xattr_value_len,
  215. char *digest)
  216. {
  217. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  218. req_xattr_value_len, IMA_XATTR_DIGEST, digest);
  219. }
  220. /*
  221. * Calculate the hmac and update security.evm xattr
  222. *
  223. * Expects to be called with i_mutex locked.
  224. */
  225. int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
  226. const char *xattr_value, size_t xattr_value_len)
  227. {
  228. struct inode *inode = d_backing_inode(dentry);
  229. struct evm_ima_xattr_data xattr_data;
  230. int rc = 0;
  231. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  232. xattr_value_len, xattr_data.digest);
  233. if (rc == 0) {
  234. xattr_data.type = EVM_XATTR_HMAC;
  235. rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
  236. &xattr_data,
  237. sizeof(xattr_data), 0);
  238. } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
  239. rc = __vfs_removexattr(dentry, XATTR_NAME_EVM);
  240. }
  241. return rc;
  242. }
  243. int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
  244. char *hmac_val)
  245. {
  246. struct shash_desc *desc;
  247. desc = init_desc(EVM_XATTR_HMAC);
  248. if (IS_ERR(desc)) {
  249. pr_info("init_desc failed\n");
  250. return PTR_ERR(desc);
  251. }
  252. crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
  253. hmac_add_misc(desc, inode, hmac_val);
  254. kfree(desc);
  255. return 0;
  256. }
  257. /*
  258. * Get the key from the TPM for the SHA1-HMAC
  259. */
  260. int evm_init_key(void)
  261. {
  262. struct key *evm_key;
  263. struct encrypted_key_payload *ekp;
  264. int rc;
  265. evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
  266. if (IS_ERR(evm_key))
  267. return -ENOENT;
  268. down_read(&evm_key->sem);
  269. ekp = evm_key->payload.data[0];
  270. rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
  271. /* burn the original key contents */
  272. memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
  273. up_read(&evm_key->sem);
  274. key_put(evm_key);
  275. return rc;
  276. }