keysetup_v1.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Key setup for v1 encryption policies
  4. *
  5. * Copyright 2015, 2019 Google LLC
  6. */
  7. /*
  8. * This file implements compatibility functions for the original encryption
  9. * policy version ("v1"), including:
  10. *
  11. * - Deriving per-file encryption keys using the AES-128-ECB based KDF
  12. * (rather than the new method of using HKDF-SHA512)
  13. *
  14. * - Retrieving fscrypt master keys from process-subscribed keyrings
  15. * (rather than the new method of using a filesystem-level keyring)
  16. *
  17. * - Handling policies with the DIRECT_KEY flag set using a master key table
  18. * (rather than the new method of implementing DIRECT_KEY with per-mode keys
  19. * managed alongside the master keys in the filesystem-level keyring)
  20. */
  21. #include <crypto/skcipher.h>
  22. #include <crypto/utils.h>
  23. #include <keys/user-type.h>
  24. #include <linux/hashtable.h>
  25. #include <linux/scatterlist.h>
  26. #include "fscrypt_private.h"
  27. /* Table of keys referenced by DIRECT_KEY policies */
  28. static DEFINE_HASHTABLE(fscrypt_direct_keys, 6); /* 6 bits = 64 buckets */
  29. static DEFINE_SPINLOCK(fscrypt_direct_keys_lock);
  30. /*
  31. * v1 key derivation function. This generates the derived key by encrypting the
  32. * master key with AES-128-ECB using the nonce as the AES key. This provides a
  33. * unique derived key with sufficient entropy for each inode. However, it's
  34. * nonstandard, non-extensible, doesn't evenly distribute the entropy from the
  35. * master key, and is trivially reversible: an attacker who compromises a
  36. * derived key can "decrypt" it to get back to the master key, then derive any
  37. * other key. For all new code, use HKDF instead.
  38. *
  39. * The master key must be at least as long as the derived key. If the master
  40. * key is longer, then only the first 'derived_keysize' bytes are used.
  41. */
  42. static int derive_key_aes(const u8 *master_key,
  43. const u8 nonce[FSCRYPT_FILE_NONCE_SIZE],
  44. u8 *derived_key, unsigned int derived_keysize)
  45. {
  46. int res = 0;
  47. struct skcipher_request *req = NULL;
  48. DECLARE_CRYPTO_WAIT(wait);
  49. struct scatterlist src_sg, dst_sg;
  50. struct crypto_skcipher *tfm =
  51. crypto_alloc_skcipher("ecb(aes)", 0, FSCRYPT_CRYPTOAPI_MASK);
  52. if (IS_ERR(tfm)) {
  53. res = PTR_ERR(tfm);
  54. tfm = NULL;
  55. goto out;
  56. }
  57. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
  58. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  59. if (!req) {
  60. res = -ENOMEM;
  61. goto out;
  62. }
  63. skcipher_request_set_callback(req,
  64. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  65. crypto_req_done, &wait);
  66. res = crypto_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE);
  67. if (res < 0)
  68. goto out;
  69. sg_init_one(&src_sg, master_key, derived_keysize);
  70. sg_init_one(&dst_sg, derived_key, derived_keysize);
  71. skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
  72. NULL);
  73. res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  74. out:
  75. skcipher_request_free(req);
  76. crypto_free_skcipher(tfm);
  77. return res;
  78. }
  79. /*
  80. * Search the current task's subscribed keyrings for a "logon" key with
  81. * description prefix:descriptor, and if found acquire a read lock on it and
  82. * return a pointer to its validated payload in *payload_ret.
  83. */
  84. static struct key *
  85. find_and_lock_process_key(const char *prefix,
  86. const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
  87. unsigned int min_keysize,
  88. const struct fscrypt_key **payload_ret)
  89. {
  90. char *description;
  91. struct key *key;
  92. const struct user_key_payload *ukp;
  93. const struct fscrypt_key *payload;
  94. description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
  95. FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
  96. if (!description)
  97. return ERR_PTR(-ENOMEM);
  98. key = request_key(&key_type_logon, description, NULL);
  99. kfree(description);
  100. if (IS_ERR(key))
  101. return key;
  102. down_read(&key->sem);
  103. ukp = user_key_payload_locked(key);
  104. if (!ukp) /* was the key revoked before we acquired its semaphore? */
  105. goto invalid;
  106. payload = (const struct fscrypt_key *)ukp->data;
  107. if (ukp->datalen != sizeof(struct fscrypt_key) ||
  108. payload->size < 1 || payload->size > FSCRYPT_MAX_KEY_SIZE) {
  109. fscrypt_warn(NULL,
  110. "key with description '%s' has invalid payload",
  111. key->description);
  112. goto invalid;
  113. }
  114. if (payload->size < min_keysize) {
  115. fscrypt_warn(NULL,
  116. "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
  117. key->description, payload->size, min_keysize);
  118. goto invalid;
  119. }
  120. *payload_ret = payload;
  121. return key;
  122. invalid:
  123. up_read(&key->sem);
  124. key_put(key);
  125. return ERR_PTR(-ENOKEY);
  126. }
  127. /* Master key referenced by DIRECT_KEY policy */
  128. struct fscrypt_direct_key {
  129. struct super_block *dk_sb;
  130. struct hlist_node dk_node;
  131. refcount_t dk_refcount;
  132. const struct fscrypt_mode *dk_mode;
  133. struct fscrypt_prepared_key dk_key;
  134. u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  135. u8 dk_raw[FSCRYPT_MAX_KEY_SIZE];
  136. };
  137. static void free_direct_key(struct fscrypt_direct_key *dk)
  138. {
  139. if (dk) {
  140. fscrypt_destroy_prepared_key(dk->dk_sb, &dk->dk_key);
  141. kfree_sensitive(dk);
  142. }
  143. }
  144. void fscrypt_put_direct_key(struct fscrypt_direct_key *dk)
  145. {
  146. if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
  147. return;
  148. hash_del(&dk->dk_node);
  149. spin_unlock(&fscrypt_direct_keys_lock);
  150. free_direct_key(dk);
  151. }
  152. /*
  153. * Find/insert the given key into the fscrypt_direct_keys table. If found, it
  154. * is returned with elevated refcount, and 'to_insert' is freed if non-NULL. If
  155. * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
  156. * NULL is returned.
  157. */
  158. static struct fscrypt_direct_key *
  159. find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
  160. const u8 *raw_key,
  161. const struct fscrypt_inode_info *ci)
  162. {
  163. unsigned long hash_key;
  164. struct fscrypt_direct_key *dk;
  165. /*
  166. * Careful: to avoid potentially leaking secret key bytes via timing
  167. * information, we must key the hash table by descriptor rather than by
  168. * raw key, and use crypto_memneq() when comparing raw keys.
  169. */
  170. BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
  171. memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
  172. sizeof(hash_key));
  173. spin_lock(&fscrypt_direct_keys_lock);
  174. hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
  175. if (memcmp(ci->ci_policy.v1.master_key_descriptor,
  176. dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
  177. continue;
  178. if (ci->ci_mode != dk->dk_mode)
  179. continue;
  180. if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
  181. continue;
  182. if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
  183. continue;
  184. /* using existing tfm with same (descriptor, mode, raw_key) */
  185. refcount_inc(&dk->dk_refcount);
  186. spin_unlock(&fscrypt_direct_keys_lock);
  187. free_direct_key(to_insert);
  188. return dk;
  189. }
  190. if (to_insert)
  191. hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
  192. spin_unlock(&fscrypt_direct_keys_lock);
  193. return to_insert;
  194. }
  195. /* Prepare to encrypt directly using the master key in the given mode */
  196. static struct fscrypt_direct_key *
  197. fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
  198. {
  199. struct fscrypt_direct_key *dk;
  200. int err;
  201. /* Is there already a tfm for this key? */
  202. dk = find_or_insert_direct_key(NULL, raw_key, ci);
  203. if (dk)
  204. return dk;
  205. /* Nope, allocate one. */
  206. dk = kzalloc(sizeof(*dk), GFP_KERNEL);
  207. if (!dk)
  208. return ERR_PTR(-ENOMEM);
  209. dk->dk_sb = ci->ci_inode->i_sb;
  210. refcount_set(&dk->dk_refcount, 1);
  211. dk->dk_mode = ci->ci_mode;
  212. err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
  213. if (err)
  214. goto err_free_dk;
  215. memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
  216. FSCRYPT_KEY_DESCRIPTOR_SIZE);
  217. memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
  218. return find_or_insert_direct_key(dk, raw_key, ci);
  219. err_free_dk:
  220. free_direct_key(dk);
  221. return ERR_PTR(err);
  222. }
  223. /* v1 policy, DIRECT_KEY: use the master key directly */
  224. static int setup_v1_file_key_direct(struct fscrypt_inode_info *ci,
  225. const u8 *raw_master_key)
  226. {
  227. struct fscrypt_direct_key *dk;
  228. dk = fscrypt_get_direct_key(ci, raw_master_key);
  229. if (IS_ERR(dk))
  230. return PTR_ERR(dk);
  231. ci->ci_direct_key = dk;
  232. ci->ci_enc_key = dk->dk_key;
  233. return 0;
  234. }
  235. /* v1 policy, !DIRECT_KEY: derive the file's encryption key */
  236. static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci,
  237. const u8 *raw_master_key)
  238. {
  239. u8 *derived_key;
  240. int err;
  241. /*
  242. * This cannot be a stack buffer because it will be passed to the
  243. * scatterlist crypto API during derive_key_aes().
  244. */
  245. derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
  246. if (!derived_key)
  247. return -ENOMEM;
  248. err = derive_key_aes(raw_master_key, ci->ci_nonce,
  249. derived_key, ci->ci_mode->keysize);
  250. if (err)
  251. goto out;
  252. err = fscrypt_set_per_file_enc_key(ci, derived_key);
  253. out:
  254. kfree_sensitive(derived_key);
  255. return err;
  256. }
  257. int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
  258. const u8 *raw_master_key)
  259. {
  260. if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
  261. return setup_v1_file_key_direct(ci, raw_master_key);
  262. else
  263. return setup_v1_file_key_derived(ci, raw_master_key);
  264. }
  265. int
  266. fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info *ci)
  267. {
  268. const struct super_block *sb = ci->ci_inode->i_sb;
  269. struct key *key;
  270. const struct fscrypt_key *payload;
  271. int err;
  272. key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
  273. ci->ci_policy.v1.master_key_descriptor,
  274. ci->ci_mode->keysize, &payload);
  275. if (key == ERR_PTR(-ENOKEY) && sb->s_cop->legacy_key_prefix) {
  276. key = find_and_lock_process_key(sb->s_cop->legacy_key_prefix,
  277. ci->ci_policy.v1.master_key_descriptor,
  278. ci->ci_mode->keysize, &payload);
  279. }
  280. if (IS_ERR(key))
  281. return PTR_ERR(key);
  282. err = fscrypt_setup_v1_file_key(ci, payload->raw);
  283. up_read(&key->sem);
  284. key_put(key);
  285. return err;
  286. }