keyinfo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * key management facility for FS encryption support.
  4. *
  5. * Copyright (C) 2015, Google, Inc.
  6. *
  7. * This contains encryption key functions.
  8. *
  9. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  10. */
  11. #include <keys/user-type.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/ratelimit.h>
  14. #include <crypto/aes.h>
  15. #include <crypto/sha.h>
  16. #include <crypto/skcipher.h>
  17. #include "fscrypt_private.h"
  18. static struct crypto_shash *essiv_hash_tfm;
  19. /*
  20. * Key derivation function. This generates the derived key by encrypting the
  21. * master key with AES-128-ECB using the inode's nonce as the AES key.
  22. *
  23. * The master key must be at least as long as the derived key. If the master
  24. * key is longer, then only the first 'derived_keysize' bytes are used.
  25. */
  26. static int derive_key_aes(const u8 *master_key,
  27. const struct fscrypt_context *ctx,
  28. u8 *derived_key, unsigned int derived_keysize)
  29. {
  30. int res = 0;
  31. struct skcipher_request *req = NULL;
  32. DECLARE_CRYPTO_WAIT(wait);
  33. struct scatterlist src_sg, dst_sg;
  34. struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
  35. if (IS_ERR(tfm)) {
  36. res = PTR_ERR(tfm);
  37. tfm = NULL;
  38. goto out;
  39. }
  40. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  41. req = skcipher_request_alloc(tfm, GFP_NOFS);
  42. if (!req) {
  43. res = -ENOMEM;
  44. goto out;
  45. }
  46. skcipher_request_set_callback(req,
  47. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  48. crypto_req_done, &wait);
  49. res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
  50. if (res < 0)
  51. goto out;
  52. sg_init_one(&src_sg, master_key, derived_keysize);
  53. sg_init_one(&dst_sg, derived_key, derived_keysize);
  54. skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
  55. NULL);
  56. res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  57. out:
  58. skcipher_request_free(req);
  59. crypto_free_skcipher(tfm);
  60. return res;
  61. }
  62. /*
  63. * Search the current task's subscribed keyrings for a "logon" key with
  64. * description prefix:descriptor, and if found acquire a read lock on it and
  65. * return a pointer to its validated payload in *payload_ret.
  66. */
  67. static struct key *
  68. find_and_lock_process_key(const char *prefix,
  69. const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
  70. unsigned int min_keysize,
  71. const struct fscrypt_key **payload_ret)
  72. {
  73. char *description;
  74. struct key *key;
  75. const struct user_key_payload *ukp;
  76. const struct fscrypt_key *payload;
  77. description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
  78. FS_KEY_DESCRIPTOR_SIZE, descriptor);
  79. if (!description)
  80. return ERR_PTR(-ENOMEM);
  81. key = request_key(&key_type_logon, description, NULL);
  82. kfree(description);
  83. if (IS_ERR(key))
  84. return key;
  85. down_read(&key->sem);
  86. ukp = user_key_payload_locked(key);
  87. if (!ukp) /* was the key revoked before we acquired its semaphore? */
  88. goto invalid;
  89. payload = (const struct fscrypt_key *)ukp->data;
  90. if (ukp->datalen != sizeof(struct fscrypt_key) ||
  91. payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
  92. fscrypt_warn(NULL,
  93. "key with description '%s' has invalid payload",
  94. key->description);
  95. goto invalid;
  96. }
  97. if (payload->size < min_keysize) {
  98. fscrypt_warn(NULL,
  99. "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
  100. key->description, payload->size, min_keysize);
  101. goto invalid;
  102. }
  103. *payload_ret = payload;
  104. return key;
  105. invalid:
  106. up_read(&key->sem);
  107. key_put(key);
  108. return ERR_PTR(-ENOKEY);
  109. }
  110. /* Find the master key, then derive the inode's actual encryption key */
  111. static int find_and_derive_key(const struct inode *inode,
  112. const struct fscrypt_context *ctx,
  113. u8 *derived_key, unsigned int derived_keysize)
  114. {
  115. struct key *key;
  116. const struct fscrypt_key *payload;
  117. int err;
  118. key = find_and_lock_process_key(FS_KEY_DESC_PREFIX,
  119. ctx->master_key_descriptor,
  120. derived_keysize, &payload);
  121. if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) {
  122. key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix,
  123. ctx->master_key_descriptor,
  124. derived_keysize, &payload);
  125. }
  126. if (IS_ERR(key))
  127. return PTR_ERR(key);
  128. err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize);
  129. up_read(&key->sem);
  130. key_put(key);
  131. return err;
  132. }
  133. static struct fscrypt_mode {
  134. const char *friendly_name;
  135. const char *cipher_str;
  136. int keysize;
  137. bool logged_impl_name;
  138. } available_modes[] = {
  139. [FS_ENCRYPTION_MODE_AES_256_XTS] = {
  140. .friendly_name = "AES-256-XTS",
  141. .cipher_str = "xts(aes)",
  142. .keysize = 64,
  143. },
  144. [FS_ENCRYPTION_MODE_AES_256_CTS] = {
  145. .friendly_name = "AES-256-CTS-CBC",
  146. .cipher_str = "cts(cbc(aes))",
  147. .keysize = 32,
  148. },
  149. [FS_ENCRYPTION_MODE_AES_128_CBC] = {
  150. .friendly_name = "AES-128-CBC",
  151. .cipher_str = "cbc(aes)",
  152. .keysize = 16,
  153. },
  154. [FS_ENCRYPTION_MODE_AES_128_CTS] = {
  155. .friendly_name = "AES-128-CTS-CBC",
  156. .cipher_str = "cts(cbc(aes))",
  157. .keysize = 16,
  158. },
  159. [FS_ENCRYPTION_MODE_SPECK128_256_XTS] = {
  160. .friendly_name = "Speck128/256-XTS",
  161. .cipher_str = "xts(speck128)",
  162. .keysize = 64,
  163. },
  164. [FS_ENCRYPTION_MODE_SPECK128_256_CTS] = {
  165. .friendly_name = "Speck128/256-CTS-CBC",
  166. .cipher_str = "cts(cbc(speck128))",
  167. .keysize = 32,
  168. },
  169. };
  170. static struct fscrypt_mode *
  171. select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
  172. {
  173. if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
  174. fscrypt_warn(inode->i_sb,
  175. "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
  176. inode->i_ino, ci->ci_data_mode,
  177. ci->ci_filename_mode);
  178. return ERR_PTR(-EINVAL);
  179. }
  180. if (S_ISREG(inode->i_mode))
  181. return &available_modes[ci->ci_data_mode];
  182. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  183. return &available_modes[ci->ci_filename_mode];
  184. WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
  185. inode->i_ino, (inode->i_mode & S_IFMT));
  186. return ERR_PTR(-EINVAL);
  187. }
  188. static void put_crypt_info(struct fscrypt_info *ci)
  189. {
  190. if (!ci)
  191. return;
  192. crypto_free_skcipher(ci->ci_ctfm);
  193. crypto_free_cipher(ci->ci_essiv_tfm);
  194. kmem_cache_free(fscrypt_info_cachep, ci);
  195. }
  196. static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
  197. {
  198. struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
  199. /* init hash transform on demand */
  200. if (unlikely(!tfm)) {
  201. struct crypto_shash *prev_tfm;
  202. tfm = crypto_alloc_shash("sha256", 0, 0);
  203. if (IS_ERR(tfm)) {
  204. fscrypt_warn(NULL,
  205. "error allocating SHA-256 transform: %ld",
  206. PTR_ERR(tfm));
  207. return PTR_ERR(tfm);
  208. }
  209. prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
  210. if (prev_tfm) {
  211. crypto_free_shash(tfm);
  212. tfm = prev_tfm;
  213. }
  214. }
  215. {
  216. SHASH_DESC_ON_STACK(desc, tfm);
  217. desc->tfm = tfm;
  218. desc->flags = 0;
  219. return crypto_shash_digest(desc, key, keysize, salt);
  220. }
  221. }
  222. static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
  223. int keysize)
  224. {
  225. int err;
  226. struct crypto_cipher *essiv_tfm;
  227. u8 salt[SHA256_DIGEST_SIZE];
  228. essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
  229. if (IS_ERR(essiv_tfm))
  230. return PTR_ERR(essiv_tfm);
  231. ci->ci_essiv_tfm = essiv_tfm;
  232. err = derive_essiv_salt(raw_key, keysize, salt);
  233. if (err)
  234. goto out;
  235. /*
  236. * Using SHA256 to derive the salt/key will result in AES-256 being
  237. * used for IV generation. File contents encryption will still use the
  238. * configured keysize (AES-128) nevertheless.
  239. */
  240. err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
  241. if (err)
  242. goto out;
  243. out:
  244. memzero_explicit(salt, sizeof(salt));
  245. return err;
  246. }
  247. void __exit fscrypt_essiv_cleanup(void)
  248. {
  249. crypto_free_shash(essiv_hash_tfm);
  250. }
  251. int fscrypt_get_encryption_info(struct inode *inode)
  252. {
  253. struct fscrypt_info *crypt_info;
  254. struct fscrypt_context ctx;
  255. struct crypto_skcipher *ctfm;
  256. struct fscrypt_mode *mode;
  257. u8 *raw_key = NULL;
  258. int res;
  259. if (inode->i_crypt_info)
  260. return 0;
  261. res = fscrypt_initialize(inode->i_sb->s_cop->flags);
  262. if (res)
  263. return res;
  264. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  265. if (res < 0) {
  266. if (!fscrypt_dummy_context_enabled(inode) ||
  267. IS_ENCRYPTED(inode))
  268. return res;
  269. /* Fake up a context for an unencrypted directory */
  270. memset(&ctx, 0, sizeof(ctx));
  271. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  272. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  273. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  274. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  275. } else if (res != sizeof(ctx)) {
  276. return -EINVAL;
  277. }
  278. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  279. return -EINVAL;
  280. if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
  281. return -EINVAL;
  282. crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
  283. if (!crypt_info)
  284. return -ENOMEM;
  285. crypt_info->ci_flags = ctx.flags;
  286. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  287. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  288. crypt_info->ci_ctfm = NULL;
  289. crypt_info->ci_essiv_tfm = NULL;
  290. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  291. sizeof(crypt_info->ci_master_key));
  292. mode = select_encryption_mode(crypt_info, inode);
  293. if (IS_ERR(mode)) {
  294. res = PTR_ERR(mode);
  295. goto out;
  296. }
  297. /*
  298. * This cannot be a stack buffer because it is passed to the scatterlist
  299. * crypto API as part of key derivation.
  300. */
  301. res = -ENOMEM;
  302. raw_key = kmalloc(mode->keysize, GFP_NOFS);
  303. if (!raw_key)
  304. goto out;
  305. res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize);
  306. if (res)
  307. goto out;
  308. ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
  309. if (IS_ERR(ctfm)) {
  310. res = PTR_ERR(ctfm);
  311. fscrypt_warn(inode->i_sb,
  312. "error allocating '%s' transform for inode %lu: %d",
  313. mode->cipher_str, inode->i_ino, res);
  314. goto out;
  315. }
  316. if (unlikely(!mode->logged_impl_name)) {
  317. /*
  318. * fscrypt performance can vary greatly depending on which
  319. * crypto algorithm implementation is used. Help people debug
  320. * performance problems by logging the ->cra_driver_name the
  321. * first time a mode is used. Note that multiple threads can
  322. * race here, but it doesn't really matter.
  323. */
  324. mode->logged_impl_name = true;
  325. pr_info("fscrypt: %s using implementation \"%s\"\n",
  326. mode->friendly_name,
  327. crypto_skcipher_alg(ctfm)->base.cra_driver_name);
  328. }
  329. crypt_info->ci_ctfm = ctfm;
  330. crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
  331. res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize);
  332. if (res)
  333. goto out;
  334. if (S_ISREG(inode->i_mode) &&
  335. crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
  336. res = init_essiv_generator(crypt_info, raw_key, mode->keysize);
  337. if (res) {
  338. fscrypt_warn(inode->i_sb,
  339. "error initializing ESSIV generator for inode %lu: %d",
  340. inode->i_ino, res);
  341. goto out;
  342. }
  343. }
  344. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
  345. crypt_info = NULL;
  346. out:
  347. if (res == -ENOKEY)
  348. res = 0;
  349. put_crypt_info(crypt_info);
  350. kzfree(raw_key);
  351. return res;
  352. }
  353. EXPORT_SYMBOL(fscrypt_get_encryption_info);
  354. void fscrypt_put_encryption_info(struct inode *inode)
  355. {
  356. put_crypt_info(inode->i_crypt_info);
  357. inode->i_crypt_info = NULL;
  358. }
  359. EXPORT_SYMBOL(fscrypt_put_encryption_info);