evm_crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2010 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <[email protected]>
  7. * Kylene Hall <[email protected]>
  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(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. /*
  180. * Calculate the HMAC value across the set of protected security xattrs.
  181. *
  182. * Instead of retrieving the requested xattr, for performance, calculate
  183. * the hmac using the requested xattr value. Don't alloc/free memory for
  184. * each xattr, but attempt to re-use the previously allocated memory.
  185. */
  186. static int evm_calc_hmac_or_hash(struct dentry *dentry,
  187. const char *req_xattr_name,
  188. const char *req_xattr_value,
  189. size_t req_xattr_value_len,
  190. uint8_t type, struct evm_digest *data)
  191. {
  192. struct inode *inode = d_backing_inode(dentry);
  193. struct xattr_list *xattr;
  194. struct shash_desc *desc;
  195. size_t xattr_size = 0;
  196. char *xattr_value = NULL;
  197. int error;
  198. int size, user_space_size;
  199. bool ima_present = false;
  200. if (!(inode->i_opflags & IOP_XATTR) ||
  201. inode->i_sb->s_user_ns != &init_user_ns)
  202. return -EOPNOTSUPP;
  203. desc = init_desc(type, data->hdr.algo);
  204. if (IS_ERR(desc))
  205. return PTR_ERR(desc);
  206. data->hdr.length = crypto_shash_digestsize(desc->tfm);
  207. error = -ENODATA;
  208. list_for_each_entry_lockless(xattr, &evm_config_xattrnames, list) {
  209. bool is_ima = false;
  210. if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)
  211. is_ima = true;
  212. /*
  213. * Skip non-enabled xattrs for locally calculated
  214. * signatures/HMACs.
  215. */
  216. if (type != EVM_XATTR_PORTABLE_DIGSIG && !xattr->enabled)
  217. continue;
  218. if ((req_xattr_name && req_xattr_value)
  219. && !strcmp(xattr->name, req_xattr_name)) {
  220. error = 0;
  221. crypto_shash_update(desc, (const u8 *)req_xattr_value,
  222. req_xattr_value_len);
  223. if (is_ima)
  224. ima_present = true;
  225. if (req_xattr_value_len < 64)
  226. pr_debug("%s: (%zu) [%*phN]\n", req_xattr_name,
  227. req_xattr_value_len,
  228. (int)req_xattr_value_len,
  229. req_xattr_value);
  230. else
  231. dump_security_xattr(req_xattr_name,
  232. req_xattr_value,
  233. req_xattr_value_len);
  234. continue;
  235. }
  236. size = vfs_getxattr_alloc(&init_user_ns, dentry, xattr->name,
  237. &xattr_value, xattr_size, GFP_NOFS);
  238. if (size == -ENOMEM) {
  239. error = -ENOMEM;
  240. goto out;
  241. }
  242. if (size < 0)
  243. continue;
  244. user_space_size = vfs_getxattr(&init_user_ns, dentry,
  245. xattr->name, NULL, 0);
  246. if (user_space_size != size)
  247. pr_debug("file %s: xattr %s size mismatch (kernel: %d, user: %d)\n",
  248. dentry->d_name.name, xattr->name, size,
  249. user_space_size);
  250. error = 0;
  251. xattr_size = size;
  252. crypto_shash_update(desc, (const u8 *)xattr_value, xattr_size);
  253. if (is_ima)
  254. ima_present = true;
  255. if (xattr_size < 64)
  256. pr_debug("%s: (%zu) [%*phN]", xattr->name, xattr_size,
  257. (int)xattr_size, xattr_value);
  258. else
  259. dump_security_xattr(xattr->name, xattr_value,
  260. xattr_size);
  261. }
  262. hmac_add_misc(desc, inode, type, data->digest);
  263. /* Portable EVM signatures must include an IMA hash */
  264. if (type == EVM_XATTR_PORTABLE_DIGSIG && !ima_present)
  265. error = -EPERM;
  266. out:
  267. kfree(xattr_value);
  268. kfree(desc);
  269. return error;
  270. }
  271. int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
  272. const char *req_xattr_value, size_t req_xattr_value_len,
  273. struct evm_digest *data)
  274. {
  275. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  276. req_xattr_value_len, EVM_XATTR_HMAC, data);
  277. }
  278. int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
  279. const char *req_xattr_value, size_t req_xattr_value_len,
  280. char type, struct evm_digest *data)
  281. {
  282. return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
  283. req_xattr_value_len, type, data);
  284. }
  285. static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
  286. {
  287. const struct evm_ima_xattr_data *xattr_data = NULL;
  288. struct integrity_iint_cache *iint;
  289. int rc = 0;
  290. iint = integrity_iint_find(inode);
  291. if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
  292. return 1;
  293. /* Do this the hard way */
  294. rc = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_EVM,
  295. (char **)&xattr_data, 0, GFP_NOFS);
  296. if (rc <= 0) {
  297. if (rc == -ENODATA)
  298. return 0;
  299. return rc;
  300. }
  301. if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG)
  302. rc = 1;
  303. else
  304. rc = 0;
  305. kfree(xattr_data);
  306. return rc;
  307. }
  308. /*
  309. * Calculate the hmac and update security.evm xattr
  310. *
  311. * Expects to be called with i_mutex locked.
  312. */
  313. int evm_update_evmxattr(struct dentry *dentry, const char *xattr_name,
  314. const char *xattr_value, size_t xattr_value_len)
  315. {
  316. struct inode *inode = d_backing_inode(dentry);
  317. struct evm_digest data;
  318. int rc = 0;
  319. /*
  320. * Don't permit any transformation of the EVM xattr if the signature
  321. * is of an immutable type
  322. */
  323. rc = evm_is_immutable(dentry, inode);
  324. if (rc < 0)
  325. return rc;
  326. if (rc)
  327. return -EPERM;
  328. data.hdr.algo = HASH_ALGO_SHA1;
  329. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  330. xattr_value_len, &data);
  331. if (rc == 0) {
  332. data.hdr.xattr.sha1.type = EVM_XATTR_HMAC;
  333. rc = __vfs_setxattr_noperm(&init_user_ns, dentry,
  334. XATTR_NAME_EVM,
  335. &data.hdr.xattr.data[1],
  336. SHA1_DIGEST_SIZE + 1, 0);
  337. } else if (rc == -ENODATA && (inode->i_opflags & IOP_XATTR)) {
  338. rc = __vfs_removexattr(&init_user_ns, dentry, XATTR_NAME_EVM);
  339. }
  340. return rc;
  341. }
  342. int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
  343. char *hmac_val)
  344. {
  345. struct shash_desc *desc;
  346. desc = init_desc(EVM_XATTR_HMAC, HASH_ALGO_SHA1);
  347. if (IS_ERR(desc)) {
  348. pr_info("init_desc failed\n");
  349. return PTR_ERR(desc);
  350. }
  351. crypto_shash_update(desc, lsm_xattr->value, lsm_xattr->value_len);
  352. hmac_add_misc(desc, inode, EVM_XATTR_HMAC, hmac_val);
  353. kfree(desc);
  354. return 0;
  355. }
  356. /*
  357. * Get the key from the TPM for the SHA1-HMAC
  358. */
  359. int evm_init_key(void)
  360. {
  361. struct key *evm_key;
  362. struct encrypted_key_payload *ekp;
  363. int rc;
  364. evm_key = request_key(&key_type_encrypted, EVMKEY, NULL);
  365. if (IS_ERR(evm_key))
  366. return -ENOENT;
  367. down_read(&evm_key->sem);
  368. ekp = evm_key->payload.data[0];
  369. rc = evm_set_key(ekp->decrypted_data, ekp->decrypted_datalen);
  370. /* burn the original key contents */
  371. memset(ekp->decrypted_data, 0, ekp->decrypted_datalen);
  372. up_read(&evm_key->sem);
  373. key_put(evm_key);
  374. return rc;
  375. }