evm_crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. #define pr_fmt(fmt) "EVM: "fmt
  13. #include <linux/export.h>
  14. #include <linux/crypto.h>
  15. #include <linux/xattr.h>
  16. #include <linux/evm.h>
  17. #include <keys/encrypted-type.h>
  18. #include <crypto/hash.h>
  19. #include <crypto/hash_info.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 const int evmkey_len = MAX_KEY_SIZE;
  25. static struct crypto_shash *hmac_tfm;
  26. static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
  27. static DEFINE_MUTEX(mutex);
  28. #define EVM_SET_KEY_BUSY 0
  29. static unsigned long evm_set_key_flags;
  30. static const char evm_hmac[] = "hmac(sha1)";
  31. /**
  32. * evm_set_key() - set EVM HMAC key from the kernel
  33. * @key: pointer to a buffer with the key data
  34. * @keylen: length of the key data
  35. *
  36. * This function allows setting the EVM HMAC key from the kernel
  37. * without using the "encrypted" key subsystem keys. It can be used
  38. * by the crypto HW kernel module which has its own way of managing
  39. * keys.
  40. *
  41. * key length should be between 32 and 128 bytes long
  42. */
  43. int evm_set_key(void *key, size_t keylen)
  44. {
  45. int rc;
  46. rc = -EBUSY;
  47. if (test_and_set_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags))
  48. goto busy;
  49. rc = -EINVAL;
  50. if (keylen > MAX_KEY_SIZE)
  51. goto inval;
  52. memcpy(evmkey, key, keylen);
  53. evm_initialized |= EVM_INIT_HMAC;
  54. pr_info("key initialized\n");
  55. return 0;
  56. inval:
  57. clear_bit(EVM_SET_KEY_BUSY, &evm_set_key_flags);
  58. busy:
  59. pr_err("key initialization failed\n");
  60. return rc;
  61. }
  62. EXPORT_SYMBOL_GPL(evm_set_key);
  63. static struct shash_desc *init_desc(char type, uint8_t hash_algo)
  64. {
  65. long rc;
  66. const char *algo;
  67. struct crypto_shash **tfm, *tmp_tfm;
  68. struct shash_desc *desc;
  69. if (type == EVM_XATTR_HMAC) {
  70. if (!(evm_initialized & EVM_INIT_HMAC)) {
  71. pr_err_once("HMAC key is not set\n");
  72. return ERR_PTR(-ENOKEY);
  73. }
  74. tfm = &hmac_tfm;
  75. algo = evm_hmac;
  76. } else {
  77. if (hash_algo >= HASH_ALGO__LAST)
  78. return ERR_PTR(-EINVAL);
  79. tfm = &evm_tfm[hash_algo];
  80. algo = hash_algo_name[hash_algo];
  81. }
  82. if (*tfm)
  83. goto alloc;
  84. mutex_lock(&mutex);
  85. if (*tfm)
  86. goto unlock;
  87. tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
  88. if (IS_ERR(tmp_tfm)) {
  89. pr_err("Can not allocate %s (reason: %ld)\n", algo,
  90. PTR_ERR(tmp_tfm));
  91. mutex_unlock(&mutex);
  92. return ERR_CAST(tmp_tfm);
  93. }
  94. if (type == EVM_XATTR_HMAC) {
  95. rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
  96. if (rc) {
  97. crypto_free_shash(tmp_tfm);
  98. mutex_unlock(&mutex);
  99. return ERR_PTR(rc);
  100. }
  101. }
  102. *tfm = tmp_tfm;
  103. unlock:
  104. mutex_unlock(&mutex);
  105. alloc:
  106. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
  107. GFP_KERNEL);
  108. if (!desc)
  109. return ERR_PTR(-ENOMEM);
  110. desc->tfm = *tfm;
  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 type, 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. /* Don't include the inode or generation number in portable
  136. * signatures
  137. */
  138. if (type != EVM_XATTR_PORTABLE_DIGSIG) {
  139. hmac_misc.ino = inode->i_ino;
  140. hmac_misc.generation = inode->i_generation;
  141. }
  142. /* The hmac uid and gid must be encoded in the initial user
  143. * namespace (not the filesystems user namespace) as encoding
  144. * them in the filesystems user namespace allows an attack
  145. * where first they are written in an unprivileged fuse mount
  146. * of a filesystem and then the system is tricked to mount the
  147. * filesystem for real on next boot and trust it because
  148. * everything is signed.
  149. */
  150. hmac_misc.uid = from_kuid(&init_user_ns, inode->i_uid);
  151. hmac_misc.gid = from_kgid(&init_user_ns, inode->i_gid);
  152. hmac_misc.mode = inode->i_mode;
  153. crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
  154. if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
  155. type != EVM_XATTR_PORTABLE_DIGSIG)
  156. crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
  157. crypto_shash_final(desc, digest);
  158. pr_debug("hmac_misc: (%zu) [%*phN]\n", sizeof(struct h_misc),
  159. (int)sizeof(struct h_misc), &hmac_misc);
  160. }
  161. /*
  162. * Dump large security xattr values as a continuous ascii hexademical string.
  163. * (pr_debug is limited to 64 bytes.)
  164. */
  165. static void dump_security_xattr_l(const char *prefix, const void *src,
  166. size_t count)
  167. {
  168. #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
  169. char *asciihex, *p;
  170. p = asciihex = kmalloc(count * 2 + 1, GFP_KERNEL);
  171. if (!asciihex)
  172. return;
  173. p = bin2hex(p, src, count);
  174. *p = 0;
  175. pr_debug("%s: (%zu) %.*s\n", prefix, count, (int)count * 2, asciihex);
  176. kfree(asciihex);
  177. #endif
  178. }
  179. static void dump_security_xattr(const char *name, const char *value,
  180. size_t value_len)
  181. {
  182. if (value_len < 64)
  183. pr_debug("%s: (%zu) [%*phN]\n", name, value_len,
  184. (int)value_len, value);
  185. else
  186. dump_security_xattr_l(name, value, value_len);
  187. }
  188. /*
  189. * Calculate the HMAC value across the set of protected security xattrs.
  190. *
  191. * Instead of retrieving the requested xattr, for performance, calculate
  192. * the hmac using the requested xattr value. Don't alloc/free memory for
  193. * each xattr, but attempt to re-use the previously allocated memory.
  194. */
  195. static int evm_calc_hmac_or_hash(struct dentry *dentry,
  196. const char *req_xattr_name,
  197. const char *req_xattr_value,
  198. size_t req_xattr_value_len,
  199. uint8_t type, struct evm_digest *data,
  200. struct evm_iint_cache *iint)
  201. {
  202. struct inode *inode = d_inode(d_real(dentry, D_REAL_METADATA));
  203. struct xattr_list *xattr;
  204. struct shash_desc *desc;
  205. size_t xattr_size = 0;
  206. char *xattr_value = NULL;
  207. int error;
  208. int size, user_space_size;
  209. bool ima_present = false;
  210. u64 i_version = 0;
  211. if (!(inode->i_opflags & IOP_XATTR) ||
  212. inode->i_sb->s_user_ns != &init_user_ns)
  213. return -EOPNOTSUPP;
  214. desc = init_desc(type, data->hdr.algo);
  215. if (IS_ERR(desc))
  216. return PTR_ERR(desc);
  217. data->hdr.length = crypto_shash_digestsize(desc->tfm);
  218. error = -ENODATA;
  219. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  220. bool is_ima = false;
  221. if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
  222. is_ima = true;
  223. /*
  224. * Skip non-enabled xattrs for locally calculated
  225. * signatures/HMACs.
  226. */
  227. if (type != EVM_XATTR_PORTABLE_DIGSIG && !xattr->enabled)
  228. continue;
  229. if ((req_xattr_name && req_xattr_value)
  230. && !strcmp(xattr->name, req_xattr_name)) {
  231. error = 0;
  232. crypto_shash_update(desc, (const u8 *)req_xattr_value,
  233. req_xattr_value_len);
  234. if (is_ima)
  235. ima_present = true;
  236. dump_security_xattr(req_xattr_name,
  237. req_xattr_value,
  238. req_xattr_value_len);
  239. continue;
  240. }
  241. size = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, xattr->name,
  242. &xattr_value, xattr_size, GFP_NOFS);
  243. if (size == -ENOMEM) {
  244. error = -ENOMEM;
  245. goto out;
  246. }
  247. if (size < 0)
  248. continue;
  249. user_space_size = vfs_getxattr(&nop_mnt_idmap, dentry,
  250. xattr->name, NULL, 0);
  251. if (user_space_size != size)
  252. pr_debug("file %s: xattr %s size mismatch (kernel: %d, user: %d)\n",
  253. dentry->d_name.name, xattr->name, size,
  254. user_space_size);
  255. error = 0;
  256. xattr_size = size;
  257. crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
  258. if (is_ima)
  259. ima_present = true;
  260. dump_security_xattr(xattr->name, xattr_value, xattr_size);
  261. }
  262. hmac_add_misc(desc, inode, type, data->digest);
  263. if (inode != d_backing_inode(dentry) && iint) {
  264. if (IS_I_VERSION(inode))
  265. i_version = inode_query_iversion(inode);
  266. integrity_inode_attrs_store(&iint->metadata_inode, i_version,
  267. inode);
  268. }
  269. /* Portable EVM signatures must include an IMA hash */
  270. if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
  271. error = -EPERM;
  272. out:
  273. kfree(xattr_value);
  274. kfree(desc);
  275. return error;
  276. }
  277. int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
  278. const char *req_xattr_value, size_t req_xattr_value_len,
  279. struct evm_digest *data, struct evm_iint_cache *iint)
  280. {
  281. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  282. req_xattr_value_len, EVM_XATTR_HMAC, data,
  283. iint);
  284. }
  285. int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
  286. const char *req_xattr_value, size_t req_xattr_value_len,
  287. char type, struct evm_digest *data, struct evm_iint_cache *iint)
  288. {
  289. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  290. req_xattr_value_len, type, data, iint);
  291. }
  292. static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
  293. {
  294. const struct evm_ima_xattr_data *xattr_data = NULL;
  295. struct evm_iint_cache *iint;
  296. int rc = 0;
  297. iint = evm_iint_inode(inode);
  298. if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
  299. return 1;
  300. /* Do this the hard way */
  301. rc = vfs_getxattr_alloc(&nop_mnt_idmap, dentry, XATTR_NAME_EVM,
  302. (char **)&xattr_data, 0, GFP_NOFS);
  303. if (rc <= 0) {
  304. if (rc == -ENODATA)
  305. rc = 0;
  306. goto out;
  307. }
  308. if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
  309. rc = 1;
  310. else
  311. rc = 0;
  312. out:
  313. kfree(xattr_data);
  314. return rc;
  315. }
  316. /*
  317. * Calculate the hmac and update security.evm xattr
  318. *
  319. * Expects to be called with i_mutex locked.
  320. */
  321. int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
  322. const char *xattr_value, size_t xattr_value_len)
  323. {
  324. struct inode *inode = d_backing_inode(dentry);
  325. struct evm_iint_cache *iint = evm_iint_inode(inode);
  326. struct evm_digest data;
  327. int rc = 0;
  328. /*
  329. * Don't permit any transformation of the EVM xattr if the signature
  330. * is of an immutable type
  331. */
  332. rc = evm_is_immutable(dentry, inode);
  333. if (rc < 0)
  334. return rc;
  335. if (rc)
  336. return -EPERM;
  337. data.hdr.algo = HASH_ALGO_SHA1;
  338. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  339. xattr_value_len, &data, iint);
  340. if (rc == 0) {
  341. data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
  342. rc = __vfs_setxattr_noperm(&nop_mnt_idmap, dentry,
  343. XATTR_NAME_EVM,
  344. &data.hdr.xattr.data[1],
  345. SHA1_DIGEST_SIZE + 1, 0);
  346. } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
  347. rc = __vfs_removexattr(&nop_mnt_idmap, dentry, XATTR_NAME_EVM);
  348. }
  349. return rc;
  350. }
  351. int evm_init_hmac(struct inode *inode, const struct xattr *xattrs,
  352. char *hmac_val)
  353. {
  354. struct shash_desc *desc;
  355. const struct xattr *xattr;
  356. desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
  357. if (IS_ERR(desc)) {
  358. pr_info("init_desc failed\n");
  359. return PTR_ERR(desc);
  360. }
  361. for (xattr = xattrs; xattr->name; xattr++) {
  362. if (!evm_protected_xattr(xattr->name))
  363. continue;
  364. crypto_shash_update(desc, xattr->value, xattr->value_len);
  365. }
  366. hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
  367. kfree(desc);
  368. return 0;
  369. }
  370. /*
  371. * Get the key from the TPM for the SHA1-HMAC
  372. */
  373. int evm_init_key(void)
  374. {
  375. struct key *evm_key;
  376. struct encrypted_key_payload *ekp;
  377. int rc;
  378. evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
  379. if (IS_ERR(evm_key))
  380. return -ENOENT;
  381. down_read(&evm_key->sem);
  382. ekp = evm_key->payload.data[0];
  383. rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
  384. /* burn the original key contents */
  385. memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
  386. up_read(&evm_key->sem);
  387. key_put(evm_key);
  388. return rc;
  389. }