keyinfo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. };
  160. static struct fscrypt_mode *
  161. select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
  162. {
  163. if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
  164. fscrypt_warn(inode->i_sb,
  165. "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
  166. inode->i_ino, ci->ci_data_mode,
  167. ci->ci_filename_mode);
  168. return ERR_PTR(-EINVAL);
  169. }
  170. if (S_ISREG(inode->i_mode))
  171. return &available_modes[ci->ci_data_mode];
  172. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  173. return &available_modes[ci->ci_filename_mode];
  174. WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
  175. inode->i_ino, (inode->i_mode & S_IFMT));
  176. return ERR_PTR(-EINVAL);
  177. }
  178. static void put_crypt_info(struct fscrypt_info *ci)
  179. {
  180. if (!ci)
  181. return;
  182. crypto_free_skcipher(ci->ci_ctfm);
  183. crypto_free_cipher(ci->ci_essiv_tfm);
  184. kmem_cache_free(fscrypt_info_cachep, ci);
  185. }
  186. static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
  187. {
  188. struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
  189. /* init hash transform on demand */
  190. if (unlikely(!tfm)) {
  191. struct crypto_shash *prev_tfm;
  192. tfm = crypto_alloc_shash("sha256", 0, 0);
  193. if (IS_ERR(tfm)) {
  194. fscrypt_warn(NULL,
  195. "error allocating SHA-256 transform: %ld",
  196. PTR_ERR(tfm));
  197. return PTR_ERR(tfm);
  198. }
  199. prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
  200. if (prev_tfm) {
  201. crypto_free_shash(tfm);
  202. tfm = prev_tfm;
  203. }
  204. }
  205. {
  206. SHASH_DESC_ON_STACK(desc, tfm);
  207. desc->tfm = tfm;
  208. desc->flags = 0;
  209. return crypto_shash_digest(desc, key, keysize, salt);
  210. }
  211. }
  212. static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
  213. int keysize)
  214. {
  215. int err;
  216. struct crypto_cipher *essiv_tfm;
  217. u8 salt[SHA256_DIGEST_SIZE];
  218. essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
  219. if (IS_ERR(essiv_tfm))
  220. return PTR_ERR(essiv_tfm);
  221. ci->ci_essiv_tfm = essiv_tfm;
  222. err = derive_essiv_salt(raw_key, keysize, salt);
  223. if (err)
  224. goto out;
  225. /*
  226. * Using SHA256 to derive the salt/key will result in AES-256 being
  227. * used for IV generation. File contents encryption will still use the
  228. * configured keysize (AES-128) nevertheless.
  229. */
  230. err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
  231. if (err)
  232. goto out;
  233. out:
  234. memzero_explicit(salt, sizeof(salt));
  235. return err;
  236. }
  237. void __exit fscrypt_essiv_cleanup(void)
  238. {
  239. crypto_free_shash(essiv_hash_tfm);
  240. }
  241. int fscrypt_get_encryption_info(struct inode *inode)
  242. {
  243. struct fscrypt_info *crypt_info;
  244. struct fscrypt_context ctx;
  245. struct crypto_skcipher *ctfm;
  246. struct fscrypt_mode *mode;
  247. u8 *raw_key = NULL;
  248. int res;
  249. if (inode->i_crypt_info)
  250. return 0;
  251. res = fscrypt_initialize(inode->i_sb->s_cop->flags);
  252. if (res)
  253. return res;
  254. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  255. if (res < 0) {
  256. if (!fscrypt_dummy_context_enabled(inode) ||
  257. IS_ENCRYPTED(inode))
  258. return res;
  259. /* Fake up a context for an unencrypted directory */
  260. memset(&ctx, 0, sizeof(ctx));
  261. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  262. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  263. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  264. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  265. } else if (res != sizeof(ctx)) {
  266. return -EINVAL;
  267. }
  268. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  269. return -EINVAL;
  270. if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
  271. return -EINVAL;
  272. crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
  273. if (!crypt_info)
  274. return -ENOMEM;
  275. crypt_info->ci_flags = ctx.flags;
  276. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  277. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  278. crypt_info->ci_ctfm = NULL;
  279. crypt_info->ci_essiv_tfm = NULL;
  280. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  281. sizeof(crypt_info->ci_master_key));
  282. mode = select_encryption_mode(crypt_info, inode);
  283. if (IS_ERR(mode)) {
  284. res = PTR_ERR(mode);
  285. goto out;
  286. }
  287. /*
  288. * This cannot be a stack buffer because it is passed to the scatterlist
  289. * crypto API as part of key derivation.
  290. */
  291. res = -ENOMEM;
  292. raw_key = kmalloc(mode->keysize, GFP_NOFS);
  293. if (!raw_key)
  294. goto out;
  295. res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize);
  296. if (res)
  297. goto out;
  298. ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
  299. if (IS_ERR(ctfm)) {
  300. res = PTR_ERR(ctfm);
  301. fscrypt_warn(inode->i_sb,
  302. "error allocating '%s' transform for inode %lu: %d",
  303. mode->cipher_str, inode->i_ino, res);
  304. goto out;
  305. }
  306. if (unlikely(!mode->logged_impl_name)) {
  307. /*
  308. * fscrypt performance can vary greatly depending on which
  309. * crypto algorithm implementation is used. Help people debug
  310. * performance problems by logging the ->cra_driver_name the
  311. * first time a mode is used. Note that multiple threads can
  312. * race here, but it doesn't really matter.
  313. */
  314. mode->logged_impl_name = true;
  315. pr_info("fscrypt: %s using implementation \"%s\"\n",
  316. mode->friendly_name,
  317. crypto_skcipher_alg(ctfm)->base.cra_driver_name);
  318. }
  319. crypt_info->ci_ctfm = ctfm;
  320. crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
  321. res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize);
  322. if (res)
  323. goto out;
  324. if (S_ISREG(inode->i_mode) &&
  325. crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
  326. res = init_essiv_generator(crypt_info, raw_key, mode->keysize);
  327. if (res) {
  328. fscrypt_warn(inode->i_sb,
  329. "error initializing ESSIV generator for inode %lu: %d",
  330. inode->i_ino, res);
  331. goto out;
  332. }
  333. }
  334. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
  335. crypt_info = NULL;
  336. out:
  337. if (res == -ENOKEY)
  338. res = 0;
  339. put_crypt_info(crypt_info);
  340. kzfree(raw_key);
  341. return res;
  342. }
  343. EXPORT_SYMBOL(fscrypt_get_encryption_info);
  344. void fscrypt_put_encryption_info(struct inode *inode)
  345. {
  346. put_crypt_info(inode->i_crypt_info);
  347. inode->i_crypt_info = NULL;
  348. }
  349. EXPORT_SYMBOL(fscrypt_put_encryption_info);