keysetup_v1.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/algapi.h>
  22. #include <crypto/skcipher.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 = crypto_alloc_skcipher("ecb(aes)", 0, 0);
  51. if (IS_ERR(tfm)) {
  52. res = PTR_ERR(tfm);
  53. tfm = NULL;
  54. goto out;
  55. }
  56. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
  57. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  58. if (!req) {
  59. res = -ENOMEM;
  60. goto out;
  61. }
  62. skcipher_request_set_callback(req,
  63. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  64. crypto_req_done, &wait);
  65. res = crypto_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE);
  66. if (res < 0)
  67. goto out;
  68. sg_init_one(&src_sg, master_key, derived_keysize);
  69. sg_init_one(&dst_sg, derived_key, derived_keysize);
  70. skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
  71. NULL);
  72. res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  73. out:
  74. skcipher_request_free(req);
  75. crypto_free_skcipher(tfm);
  76. return res;
  77. }
  78. /*
  79. * Search the current task's subscribed keyrings for a "logon" key with
  80. * description prefix:descriptor, and if found acquire a read lock on it and
  81. * return a pointer to its validated payload in *payload_ret.
  82. */
  83. static struct key *
  84. find_and_lock_process_key(const char *prefix,
  85. const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
  86. unsigned int min_keysize,
  87. const struct fscrypt_key **payload_ret)
  88. {
  89. char *description;
  90. struct key *key;
  91. const struct user_key_payload *ukp;
  92. const struct fscrypt_key *payload;
  93. description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
  94. FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
  95. if (!description)
  96. return ERR_PTR(-ENOMEM);
  97. key = request_key(&key_type_logon, description, NULL);
  98. kfree(description);
  99. if (IS_ERR(key))
  100. return key;
  101. down_read(&key->sem);
  102. ukp = user_key_payload_locked(key);
  103. if (!ukp) /* was the key revoked before we acquired its semaphore? */
  104. goto invalid;
  105. payload = (const struct fscrypt_key *)ukp->data;
  106. if (ukp->datalen != sizeof(struct fscrypt_key) ||
  107. payload->size < 1 ||
  108. payload->size > FSCRYPT_MAX_STANDARD_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_STANDARD_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, const struct fscrypt_info *ci)
  161. {
  162. unsigned long hash_key;
  163. struct fscrypt_direct_key *dk;
  164. /*
  165. * Careful: to avoid potentially leaking secret key bytes via timing
  166. * information, we must key the hash table by descriptor rather than by
  167. * raw key, and use crypto_memneq() when comparing raw keys.
  168. */
  169. BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
  170. memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
  171. sizeof(hash_key));
  172. spin_lock(&fscrypt_direct_keys_lock);
  173. hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
  174. if (memcmp(ci->ci_policy.v1.master_key_descriptor,
  175. dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
  176. continue;
  177. if (ci->ci_mode != dk->dk_mode)
  178. continue;
  179. if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
  180. continue;
  181. if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
  182. continue;
  183. /* using existing tfm with same (descriptor, mode, raw_key) */
  184. refcount_inc(&dk->dk_refcount);
  185. spin_unlock(&fscrypt_direct_keys_lock);
  186. free_direct_key(to_insert);
  187. return dk;
  188. }
  189. if (to_insert)
  190. hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
  191. spin_unlock(&fscrypt_direct_keys_lock);
  192. return to_insert;
  193. }
  194. /* Prepare to encrypt directly using the master key in the given mode */
  195. static struct fscrypt_direct_key *
  196. fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key)
  197. {
  198. struct fscrypt_direct_key *dk;
  199. int err;
  200. /* Is there already a tfm for this key? */
  201. dk = find_or_insert_direct_key(NULL, raw_key, ci);
  202. if (dk)
  203. return dk;
  204. /* Nope, allocate one. */
  205. dk = kzalloc(sizeof(*dk), GFP_KERNEL);
  206. if (!dk)
  207. return ERR_PTR(-ENOMEM);
  208. dk->dk_sb = ci->ci_inode->i_sb;
  209. refcount_set(&dk->dk_refcount, 1);
  210. dk->dk_mode = ci->ci_mode;
  211. err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
  212. if (err)
  213. goto err_free_dk;
  214. memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
  215. FSCRYPT_KEY_DESCRIPTOR_SIZE);
  216. memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
  217. return find_or_insert_direct_key(dk, raw_key, ci);
  218. err_free_dk:
  219. free_direct_key(dk);
  220. return ERR_PTR(err);
  221. }
  222. /* v1 policy, DIRECT_KEY: use the master key directly */
  223. static int setup_v1_file_key_direct(struct fscrypt_info *ci,
  224. const u8 *raw_master_key)
  225. {
  226. struct fscrypt_direct_key *dk;
  227. dk = fscrypt_get_direct_key(ci, raw_master_key);
  228. if (IS_ERR(dk))
  229. return PTR_ERR(dk);
  230. ci->ci_direct_key = dk;
  231. ci->ci_enc_key = dk->dk_key;
  232. return 0;
  233. }
  234. /* v1 policy, !DIRECT_KEY: derive the file's encryption key */
  235. static int setup_v1_file_key_derived(struct fscrypt_info *ci,
  236. const u8 *raw_master_key)
  237. {
  238. u8 *derived_key;
  239. int err;
  240. /*
  241. * This cannot be a stack buffer because it will be passed to the
  242. * scatterlist crypto API during derive_key_aes().
  243. */
  244. derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
  245. if (!derived_key)
  246. return -ENOMEM;
  247. err = derive_key_aes(raw_master_key, ci->ci_nonce,
  248. derived_key, ci->ci_mode->keysize);
  249. if (err)
  250. goto out;
  251. err = fscrypt_set_per_file_enc_key(ci, derived_key);
  252. out:
  253. kfree_sensitive(derived_key);
  254. return err;
  255. }
  256. int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, const u8 *raw_master_key)
  257. {
  258. if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
  259. return setup_v1_file_key_direct(ci, raw_master_key);
  260. else
  261. return setup_v1_file_key_derived(ci, raw_master_key);
  262. }
  263. int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci)
  264. {
  265. struct key *key;
  266. const struct fscrypt_key *payload;
  267. int err;
  268. key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
  269. ci->ci_policy.v1.master_key_descriptor,
  270. ci->ci_mode->keysize, &payload);
  271. if (key == ERR_PTR(-ENOKEY) && ci->ci_inode->i_sb->s_cop->key_prefix) {
  272. key = find_and_lock_process_key(ci->ci_inode->i_sb->s_cop->key_prefix,
  273. ci->ci_policy.v1.master_key_descriptor,
  274. ci->ci_mode->keysize, &payload);
  275. }
  276. if (IS_ERR(key))
  277. return PTR_ERR(key);
  278. err = fscrypt_setup_v1_file_key(ci, payload->raw);
  279. up_read(&key->sem);
  280. key_put(key);
  281. return err;
  282. }