evm_crypto.c 9.2 KB

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