evm_crypto.c 9.5 KB

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