evm_crypto.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. static char * const evm_hmac = "hmac(sha1)";
  33. static char * const evm_hash = "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)
  67. {
  68. long rc;
  69. 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. tfm = &hash_tfm;
  81. algo = evm_hash;
  82. }
  83. if (*tfm == NULL) {
  84. mutex_lock(&mutex);
  85. if (*tfm)
  86. goto out;
  87. *tfm = crypto_alloc_shash(algo, 0,
  88. CRYPTO_ALG_ASYNC | CRYPTO_NOLOAD);
  89. if (IS_ERR(*tfm)) {
  90. rc = PTR_ERR(*tfm);
  91. pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
  92. *tfm = NULL;
  93. mutex_unlock(&mutex);
  94. return ERR_PTR(rc);
  95. }
  96. if (type == EVM_XATTR_HMAC) {
  97. rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
  98. if (rc) {
  99. crypto_free_shash(*tfm);
  100. *tfm = NULL;
  101. mutex_unlock(&mutex);
  102. return ERR_PTR(rc);
  103. }
  104. }
  105. out:
  106. mutex_unlock(&mutex);
  107. }
  108. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
  109. GFP_KERNEL);
  110. if (!desc)
  111. return ERR_PTR(-ENOMEM);
  112. desc->tfm = *tfm;
  113. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  114. rc = crypto_shash_init(desc);
  115. if (rc) {
  116. kfree(desc);
  117. return ERR_PTR(rc);
  118. }
  119. return desc;
  120. }
  121. /* Protect against 'cutting & pasting' security.evm xattr, include inode
  122. * specific info.
  123. *
  124. * (Additional directory/file metadata needs to be added for more complete
  125. * protection.)
  126. */
  127. static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
  128. char type, char *digest)
  129. {
  130. struct h_misc {
  131. unsigned long ino;
  132. __u32 generation;
  133. uid_t uid;
  134. gid_t gid;
  135. umode_t mode;
  136. } hmac_misc;
  137. memset(&hmac_misc, 0, sizeof(hmac_misc));
  138. /* Don't include the inode or generation number in portable
  139. * signatures
  140. */
  141. if (type != EVM_XATTR_PORTABLE_DIGSIG) {
  142. hmac_misc.ino = inode->i_ino;
  143. hmac_misc.generation = inode->i_generation;
  144. }
  145. /* The hmac uid and gid must be encoded in the initial user
  146. * namespace (not the filesystems user namespace) as encoding
  147. * them in the filesystems user namespace allows an attack
  148. * where first they are written in an unprivileged fuse mount
  149. * of a filesystem and then the system is tricked to mount the
  150. * filesystem for real on next boot and trust it because
  151. * everything is signed.
  152. */
  153. hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
  154. hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
  155. hmac_misc.mode = inode->i_mode;
  156. crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
  157. if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
  158. type != EVM_XATTR_PORTABLE_DIGSIG)
  159. crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0],
  160. sizeof(inode->i_sb->s_uuid));
  161. crypto_shash_final(desc, digest);
  162. }
  163. /*
  164. * Calculate the HMAC value across the set of protected security xattrs.
  165. *
  166. * Instead of retrieving the requested xattr, for performance, calculate
  167. * the hmac using the requested xattr value. Don't alloc/free memory for
  168. * each xattr, but attempt to re-use the previously allocated memory.
  169. */
  170. static int evm_calc_hmac_or_hash(struct dentry *dentry,
  171. const char *req_xattr_name,
  172. const char *req_xattr_value,
  173. size_t req_xattr_value_len,
  174. char type, char *digest)
  175. {
  176. struct inode *inode = d_backing_inode(dentry);
  177. struct xattr_list *xattr;
  178. struct shash_desc *desc;
  179. size_t xattr_size = 0;
  180. char *xattr_value = NULL;
  181. int error;
  182. int size;
  183. bool ima_present = false;
  184. if (!(inode->i_opflags & IOP_XATTR) ||
  185. inode->i_sb->s_user_ns != &init_user_ns)
  186. return -EOPNOTSUPP;
  187. desc = init_desc(type);
  188. if (IS_ERR(desc))
  189. return PTR_ERR(desc);
  190. error = -ENODATA;
  191. list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
  192. bool is_ima = false;
  193. if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
  194. is_ima = true;
  195. if ((req_xattr_name && req_xattr_value)
  196. && !strcmp(xattr->name, req_xattr_name)) {
  197. error = 0;
  198. crypto_shash_update(desc, (const u8 *)req_xattr_value,
  199. req_xattr_value_len);
  200. if (is_ima)
  201. ima_present = true;
  202. continue;
  203. }
  204. size = vfs_getxattr_alloc(dentry, xattr->name,
  205. &xattr_value, xattr_size, GFP_NOFS);
  206. if (size == -ENOMEM) {
  207. error = -ENOMEM;
  208. goto out;
  209. }
  210. if (size < 0)
  211. continue;
  212. error = 0;
  213. xattr_size = size;
  214. crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
  215. if (is_ima)
  216. ima_present = true;
  217. }
  218. hmac_add_misc(desc, inode, type, digest);
  219. /* Portable EVM signatures must include an IMA hash */
  220. if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
  221. return -EPERM;
  222. out:
  223. kfree(xattr_value);
  224. kfree(desc);
  225. return error;
  226. }
  227. int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
  228. const char *req_xattr_value, size_t req_xattr_value_len,
  229. char *digest)
  230. {
  231. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  232. req_xattr_value_len, EVM_XATTR_HMAC, digest);
  233. }
  234. int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
  235. const char *req_xattr_value, size_t req_xattr_value_len,
  236. char type, char *digest)
  237. {
  238. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  239. req_xattr_value_len, type, digest);
  240. }
  241. static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
  242. {
  243. const struct evm_ima_xattr_data *xattr_data = NULL;
  244. struct integrity_iint_cache *iint;
  245. int rc = 0;
  246. iint = integrity_iint_find(inode);
  247. if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
  248. return 1;
  249. /* Do this the hard way */
  250. rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
  251. GFP_NOFS);
  252. if (rc <= 0) {
  253. if (rc == -ENODATA)
  254. return 0;
  255. return rc;
  256. }
  257. if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
  258. rc = 1;
  259. else
  260. rc = 0;
  261. kfree(xattr_data);
  262. return rc;
  263. }
  264. /*
  265. * Calculate the hmac and update security.evm xattr
  266. *
  267. * Expects to be called with i_mutex locked.
  268. */
  269. int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
  270. const char *xattr_value, size_t xattr_value_len)
  271. {
  272. struct inode *inode = d_backing_inode(dentry);
  273. struct evm_ima_xattr_data xattr_data;
  274. int rc = 0;
  275. /*
  276. * Don't permit any transformation of the EVM xattr if the signature
  277. * is of an immutable type
  278. */
  279. rc = evm_is_immutable(dentry, inode);
  280. if (rc < 0)
  281. return rc;
  282. if (rc)
  283. return -EPERM;
  284. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  285. xattr_value_len, xattr_data.digest);
  286. if (rc == 0) {
  287. xattr_data.type = EVM_XATTR_HMAC;
  288. rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_EVM,
  289. &xattr_data,
  290. sizeof(xattr_data), 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);
  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. }