keyinfo.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * key management facility for FS encryption support.
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption key functions.
  7. *
  8. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  9. */
  10. #include <keys/user-type.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/ratelimit.h>
  13. #include <crypto/aes.h>
  14. #include <crypto/sha.h>
  15. #include "fscrypt_private.h"
  16. static struct crypto_shash *essiv_hash_tfm;
  17. static void derive_crypt_complete(struct crypto_async_request *req, int rc)
  18. {
  19. struct fscrypt_completion_result *ecr = req->data;
  20. if (rc == -EINPROGRESS)
  21. return;
  22. ecr->res = rc;
  23. complete(&ecr->completion);
  24. }
  25. /**
  26. * derive_key_aes() - Derive a key using AES-128-ECB
  27. * @deriving_key: Encryption key used for derivation.
  28. * @source_key: Source key to which to apply derivation.
  29. * @derived_raw_key: Derived raw key.
  30. *
  31. * Return: Zero on success; non-zero otherwise.
  32. */
  33. static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
  34. const struct fscrypt_key *source_key,
  35. u8 derived_raw_key[FS_MAX_KEY_SIZE])
  36. {
  37. int res = 0;
  38. struct skcipher_request *req = NULL;
  39. DECLARE_FS_COMPLETION_RESULT(ecr);
  40. struct scatterlist src_sg, dst_sg;
  41. struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
  42. if (IS_ERR(tfm)) {
  43. res = PTR_ERR(tfm);
  44. tfm = NULL;
  45. goto out;
  46. }
  47. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  48. req = skcipher_request_alloc(tfm, GFP_NOFS);
  49. if (!req) {
  50. res = -ENOMEM;
  51. goto out;
  52. }
  53. skcipher_request_set_callback(req,
  54. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  55. derive_crypt_complete, &ecr);
  56. res = crypto_skcipher_setkey(tfm, deriving_key,
  57. FS_AES_128_ECB_KEY_SIZE);
  58. if (res < 0)
  59. goto out;
  60. sg_init_one(&src_sg, source_key->raw, source_key->size);
  61. sg_init_one(&dst_sg, derived_raw_key, source_key->size);
  62. skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size,
  63. NULL);
  64. res = crypto_skcipher_encrypt(req);
  65. if (res == -EINPROGRESS || res == -EBUSY) {
  66. wait_for_completion(&ecr.completion);
  67. res = ecr.res;
  68. }
  69. out:
  70. skcipher_request_free(req);
  71. crypto_free_skcipher(tfm);
  72. return res;
  73. }
  74. static int validate_user_key(struct fscrypt_info *crypt_info,
  75. struct fscrypt_context *ctx, u8 *raw_key,
  76. const char *prefix, int min_keysize)
  77. {
  78. char *description;
  79. struct key *keyring_key;
  80. struct fscrypt_key *master_key;
  81. const struct user_key_payload *ukp;
  82. int res;
  83. description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
  84. FS_KEY_DESCRIPTOR_SIZE,
  85. ctx->master_key_descriptor);
  86. if (!description)
  87. return -ENOMEM;
  88. keyring_key = request_key(&key_type_logon, description, NULL);
  89. kfree(description);
  90. if (IS_ERR(keyring_key))
  91. return PTR_ERR(keyring_key);
  92. down_read(&keyring_key->sem);
  93. if (keyring_key->type != &key_type_logon) {
  94. printk_once(KERN_WARNING
  95. "%s: key type must be logon\n", __func__);
  96. res = -ENOKEY;
  97. goto out;
  98. }
  99. ukp = user_key_payload_locked(keyring_key);
  100. if (ukp->datalen != sizeof(struct fscrypt_key)) {
  101. res = -EINVAL;
  102. goto out;
  103. }
  104. master_key = (struct fscrypt_key *)ukp->data;
  105. BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
  106. if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
  107. || master_key->size % AES_BLOCK_SIZE != 0) {
  108. printk_once(KERN_WARNING
  109. "%s: key size incorrect: %d\n",
  110. __func__, master_key->size);
  111. res = -ENOKEY;
  112. goto out;
  113. }
  114. res = derive_key_aes(ctx->nonce, master_key, raw_key);
  115. out:
  116. up_read(&keyring_key->sem);
  117. key_put(keyring_key);
  118. return res;
  119. }
  120. static const struct {
  121. const char *cipher_str;
  122. int keysize;
  123. } available_modes[] = {
  124. [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)",
  125. FS_AES_256_XTS_KEY_SIZE },
  126. [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))",
  127. FS_AES_256_CTS_KEY_SIZE },
  128. [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)",
  129. FS_AES_128_CBC_KEY_SIZE },
  130. [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))",
  131. FS_AES_128_CTS_KEY_SIZE },
  132. };
  133. static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
  134. const char **cipher_str_ret, int *keysize_ret)
  135. {
  136. u32 mode;
  137. if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
  138. pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
  139. inode->i_ino,
  140. ci->ci_data_mode, ci->ci_filename_mode);
  141. return -EINVAL;
  142. }
  143. if (S_ISREG(inode->i_mode)) {
  144. mode = ci->ci_data_mode;
  145. } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  146. mode = ci->ci_filename_mode;
  147. } else {
  148. WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
  149. inode->i_ino, (inode->i_mode & S_IFMT));
  150. return -EINVAL;
  151. }
  152. *cipher_str_ret = available_modes[mode].cipher_str;
  153. *keysize_ret = available_modes[mode].keysize;
  154. return 0;
  155. }
  156. static void put_crypt_info(struct fscrypt_info *ci)
  157. {
  158. if (!ci)
  159. return;
  160. crypto_free_skcipher(ci->ci_ctfm);
  161. crypto_free_cipher(ci->ci_essiv_tfm);
  162. kmem_cache_free(fscrypt_info_cachep, ci);
  163. }
  164. static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
  165. {
  166. struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
  167. /* init hash transform on demand */
  168. if (unlikely(!tfm)) {
  169. struct crypto_shash *prev_tfm;
  170. tfm = crypto_alloc_shash("sha256", 0, 0);
  171. if (IS_ERR(tfm)) {
  172. pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
  173. PTR_ERR(tfm));
  174. return PTR_ERR(tfm);
  175. }
  176. prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
  177. if (prev_tfm) {
  178. crypto_free_shash(tfm);
  179. tfm = prev_tfm;
  180. }
  181. }
  182. {
  183. SHASH_DESC_ON_STACK(desc, tfm);
  184. desc->tfm = tfm;
  185. desc->flags = 0;
  186. return crypto_shash_digest(desc, key, keysize, salt);
  187. }
  188. }
  189. static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
  190. int keysize)
  191. {
  192. int err;
  193. struct crypto_cipher *essiv_tfm;
  194. u8 salt[SHA256_DIGEST_SIZE];
  195. essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
  196. if (IS_ERR(essiv_tfm))
  197. return PTR_ERR(essiv_tfm);
  198. ci->ci_essiv_tfm = essiv_tfm;
  199. err = derive_essiv_salt(raw_key, keysize, salt);
  200. if (err)
  201. goto out;
  202. /*
  203. * Using SHA256 to derive the salt/key will result in AES-256 being
  204. * used for IV generation. File contents encryption will still use the
  205. * configured keysize (AES-128) nevertheless.
  206. */
  207. err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
  208. if (err)
  209. goto out;
  210. out:
  211. memzero_explicit(salt, sizeof(salt));
  212. return err;
  213. }
  214. void __exit fscrypt_essiv_cleanup(void)
  215. {
  216. crypto_free_shash(essiv_hash_tfm);
  217. }
  218. int fscrypt_get_encryption_info(struct inode *inode)
  219. {
  220. struct fscrypt_info *crypt_info;
  221. struct fscrypt_context ctx;
  222. struct crypto_skcipher *ctfm;
  223. const char *cipher_str;
  224. int keysize;
  225. u8 *raw_key = NULL;
  226. int res;
  227. if (inode->i_crypt_info)
  228. return 0;
  229. res = fscrypt_initialize(inode->i_sb->s_cop->flags);
  230. if (res)
  231. return res;
  232. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  233. if (res < 0) {
  234. if (!fscrypt_dummy_context_enabled(inode) ||
  235. inode->i_sb->s_cop->is_encrypted(inode))
  236. return res;
  237. /* Fake up a context for an unencrypted directory */
  238. memset(&ctx, 0, sizeof(ctx));
  239. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  240. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  241. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  242. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  243. } else if (res != sizeof(ctx)) {
  244. return -EINVAL;
  245. }
  246. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  247. return -EINVAL;
  248. if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
  249. return -EINVAL;
  250. crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
  251. if (!crypt_info)
  252. return -ENOMEM;
  253. crypt_info->ci_flags = ctx.flags;
  254. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  255. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  256. crypt_info->ci_ctfm = NULL;
  257. crypt_info->ci_essiv_tfm = NULL;
  258. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  259. sizeof(crypt_info->ci_master_key));
  260. res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
  261. if (res)
  262. goto out;
  263. /*
  264. * This cannot be a stack buffer because it is passed to the scatterlist
  265. * crypto API as part of key derivation.
  266. */
  267. res = -ENOMEM;
  268. raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
  269. if (!raw_key)
  270. goto out;
  271. res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
  272. keysize);
  273. if (res && inode->i_sb->s_cop->key_prefix) {
  274. int res2 = validate_user_key(crypt_info, &ctx, raw_key,
  275. inode->i_sb->s_cop->key_prefix,
  276. keysize);
  277. if (res2) {
  278. if (res2 == -ENOKEY)
  279. res = -ENOKEY;
  280. goto out;
  281. }
  282. } else if (res) {
  283. goto out;
  284. }
  285. ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
  286. if (!ctfm || IS_ERR(ctfm)) {
  287. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  288. pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
  289. __func__, res, inode->i_ino);
  290. goto out;
  291. }
  292. crypt_info->ci_ctfm = ctfm;
  293. crypto_skcipher_clear_flags(ctfm, ~0);
  294. crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
  295. /*
  296. * if the provided key is longer than keysize, we use the first
  297. * keysize bytes of the derived key only
  298. */
  299. res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
  300. if (res)
  301. goto out;
  302. if (S_ISREG(inode->i_mode) &&
  303. crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
  304. res = init_essiv_generator(crypt_info, raw_key, keysize);
  305. if (res) {
  306. pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
  307. __func__, res, inode->i_ino);
  308. goto out;
  309. }
  310. }
  311. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
  312. crypt_info = NULL;
  313. out:
  314. if (res == -ENOKEY)
  315. res = 0;
  316. put_crypt_info(crypt_info);
  317. kzfree(raw_key);
  318. return res;
  319. }
  320. EXPORT_SYMBOL(fscrypt_get_encryption_info);
  321. void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
  322. {
  323. struct fscrypt_info *prev;
  324. if (ci == NULL)
  325. ci = ACCESS_ONCE(inode->i_crypt_info);
  326. if (ci == NULL)
  327. return;
  328. prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
  329. if (prev != ci)
  330. return;
  331. put_crypt_info(ci);
  332. }
  333. EXPORT_SYMBOL(fscrypt_put_encryption_info);