keysetup_v1.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. struct crypto_sync_skcipher *tfm;
  47. int err;
  48. tfm = crypto_alloc_sync_skcipher("ecb(aes)", 0, FSCRYPT_CRYPTOAPI_MASK);
  49. if (IS_ERR(tfm))
  50. return PTR_ERR(tfm);
  51. err = crypto_sync_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE);
  52. if (err == 0) {
  53. SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
  54. struct scatterlist src_sg, dst_sg;
  55. skcipher_request_set_callback(req,
  56. CRYPTO_TFM_REQ_MAY_BACKLOG |
  57. CRYPTO_TFM_REQ_MAY_SLEEP,
  58. NULL, NULL);
  59. sg_init_one(&src_sg, master_key, derived_keysize);
  60. sg_init_one(&dst_sg, derived_key, derived_keysize);
  61. skcipher_request_set_crypt(req, &src_sg, &dst_sg,
  62. derived_keysize, NULL);
  63. err = crypto_skcipher_encrypt(req);
  64. }
  65. crypto_free_sync_skcipher(tfm);
  66. return err;
  67. }
  68. /*
  69. * Search the current task's subscribed keyrings for a "logon" key with
  70. * description prefix:descriptor, and if found acquire a read lock on it and
  71. * return a pointer to its validated payload in *payload_ret.
  72. */
  73. static struct key *
  74. find_and_lock_process_key(const char *prefix,
  75. const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
  76. unsigned int min_keysize,
  77. const struct fscrypt_key **payload_ret)
  78. {
  79. char *description;
  80. struct key *key;
  81. const struct user_key_payload *ukp;
  82. const struct fscrypt_key *payload;
  83. description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
  84. FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
  85. if (!description)
  86. return ERR_PTR(-ENOMEM);
  87. key = request_key(&key_type_logon, description, NULL);
  88. kfree(description);
  89. if (IS_ERR(key))
  90. return key;
  91. down_read(&key->sem);
  92. ukp = user_key_payload_locked(key);
  93. if (!ukp) /* was the key revoked before we acquired its semaphore? */
  94. goto invalid;
  95. payload = (const struct fscrypt_key *)ukp->data;
  96. if (ukp->datalen != sizeof(struct fscrypt_key) ||
  97. payload->size < 1 || payload->size > sizeof(payload->raw)) {
  98. fscrypt_warn(NULL,
  99. "key with description '%s' has invalid payload",
  100. key->description);
  101. goto invalid;
  102. }
  103. if (payload->size < min_keysize) {
  104. fscrypt_warn(NULL,
  105. "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
  106. key->description, payload->size, min_keysize);
  107. goto invalid;
  108. }
  109. *payload_ret = payload;
  110. return key;
  111. invalid:
  112. up_read(&key->sem);
  113. key_put(key);
  114. return ERR_PTR(-ENOKEY);
  115. }
  116. /* Master key referenced by DIRECT_KEY policy */
  117. struct fscrypt_direct_key {
  118. struct super_block *dk_sb;
  119. struct hlist_node dk_node;
  120. refcount_t dk_refcount;
  121. const struct fscrypt_mode *dk_mode;
  122. struct fscrypt_prepared_key dk_key;
  123. u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  124. u8 dk_raw[FSCRYPT_MAX_RAW_KEY_SIZE];
  125. };
  126. static void free_direct_key(struct fscrypt_direct_key *dk)
  127. {
  128. if (dk) {
  129. fscrypt_destroy_prepared_key(dk->dk_sb, &dk->dk_key);
  130. kfree_sensitive(dk);
  131. }
  132. }
  133. void fscrypt_put_direct_key(struct fscrypt_direct_key *dk)
  134. {
  135. if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
  136. return;
  137. hash_del(&dk->dk_node);
  138. spin_unlock(&fscrypt_direct_keys_lock);
  139. free_direct_key(dk);
  140. }
  141. /*
  142. * Find/insert the given key into the fscrypt_direct_keys table. If found, it
  143. * is returned with elevated refcount, and 'to_insert' is freed if non-NULL. If
  144. * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
  145. * NULL is returned.
  146. */
  147. static struct fscrypt_direct_key *
  148. find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
  149. const u8 *raw_key,
  150. const struct fscrypt_inode_info *ci)
  151. {
  152. unsigned long hash_key;
  153. struct fscrypt_direct_key *dk;
  154. /*
  155. * Careful: to avoid potentially leaking secret key bytes via timing
  156. * information, we must key the hash table by descriptor rather than by
  157. * raw key, and use crypto_memneq() when comparing raw keys.
  158. */
  159. BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
  160. memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor,
  161. sizeof(hash_key));
  162. spin_lock(&fscrypt_direct_keys_lock);
  163. hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
  164. if (memcmp(ci->ci_policy.v1.master_key_descriptor,
  165. dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
  166. continue;
  167. if (ci->ci_mode != dk->dk_mode)
  168. continue;
  169. if (!fscrypt_is_key_prepared(&dk->dk_key, ci))
  170. continue;
  171. if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
  172. continue;
  173. /* using existing tfm with same (descriptor, mode, raw_key) */
  174. refcount_inc(&dk->dk_refcount);
  175. spin_unlock(&fscrypt_direct_keys_lock);
  176. free_direct_key(to_insert);
  177. return dk;
  178. }
  179. if (to_insert)
  180. hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
  181. spin_unlock(&fscrypt_direct_keys_lock);
  182. return to_insert;
  183. }
  184. /* Prepare to encrypt directly using the master key in the given mode */
  185. static struct fscrypt_direct_key *
  186. fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
  187. {
  188. struct fscrypt_direct_key *dk;
  189. int err;
  190. /* Is there already a tfm for this key? */
  191. dk = find_or_insert_direct_key(NULL, raw_key, ci);
  192. if (dk)
  193. return dk;
  194. /* Nope, allocate one. */
  195. dk = kzalloc_obj(*dk);
  196. if (!dk)
  197. return ERR_PTR(-ENOMEM);
  198. dk->dk_sb = ci->ci_inode->i_sb;
  199. refcount_set(&dk->dk_refcount, 1);
  200. dk->dk_mode = ci->ci_mode;
  201. err = fscrypt_prepare_key(&dk->dk_key, raw_key, ci);
  202. if (err)
  203. goto err_free_dk;
  204. memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor,
  205. FSCRYPT_KEY_DESCRIPTOR_SIZE);
  206. memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
  207. return find_or_insert_direct_key(dk, raw_key, ci);
  208. err_free_dk:
  209. free_direct_key(dk);
  210. return ERR_PTR(err);
  211. }
  212. /* v1 policy, DIRECT_KEY: use the master key directly */
  213. static int setup_v1_file_key_direct(struct fscrypt_inode_info *ci,
  214. const u8 *raw_master_key)
  215. {
  216. struct fscrypt_direct_key *dk;
  217. dk = fscrypt_get_direct_key(ci, raw_master_key);
  218. if (IS_ERR(dk))
  219. return PTR_ERR(dk);
  220. ci->ci_direct_key = dk;
  221. ci->ci_enc_key = dk->dk_key;
  222. return 0;
  223. }
  224. /* v1 policy, !DIRECT_KEY: derive the file's encryption key */
  225. static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci,
  226. const u8 *raw_master_key)
  227. {
  228. u8 *derived_key;
  229. int err;
  230. /*
  231. * This cannot be a stack buffer because it will be passed to the
  232. * scatterlist crypto API during derive_key_aes().
  233. */
  234. derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
  235. if (!derived_key)
  236. return -ENOMEM;
  237. err = derive_key_aes(raw_master_key, ci->ci_nonce,
  238. derived_key, ci->ci_mode->keysize);
  239. if (err)
  240. goto out;
  241. err = fscrypt_set_per_file_enc_key(ci, derived_key);
  242. out:
  243. kfree_sensitive(derived_key);
  244. return err;
  245. }
  246. int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,
  247. const u8 *raw_master_key)
  248. {
  249. if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
  250. return setup_v1_file_key_direct(ci, raw_master_key);
  251. else
  252. return setup_v1_file_key_derived(ci, raw_master_key);
  253. }
  254. int
  255. fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_inode_info *ci)
  256. {
  257. const struct super_block *sb = ci->ci_inode->i_sb;
  258. struct key *key;
  259. const struct fscrypt_key *payload;
  260. int err;
  261. key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
  262. ci->ci_policy.v1.master_key_descriptor,
  263. ci->ci_mode->keysize, &payload);
  264. if (key == ERR_PTR(-ENOKEY) && sb->s_cop->legacy_key_prefix) {
  265. key = find_and_lock_process_key(sb->s_cop->legacy_key_prefix,
  266. ci->ci_policy.v1.master_key_descriptor,
  267. ci->ci_mode->keysize, &payload);
  268. }
  269. if (IS_ERR(key))
  270. return PTR_ERR(key);
  271. err = fscrypt_setup_v1_file_key(ci, payload->raw);
  272. up_read(&key->sem);
  273. key_put(key);
  274. return err;
  275. }