keyinfo.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "fscrypt_private.h"
  13. static void derive_crypt_complete(struct crypto_async_request *req, int rc)
  14. {
  15. struct fscrypt_completion_result *ecr = req->data;
  16. if (rc == -EINPROGRESS)
  17. return;
  18. ecr->res = rc;
  19. complete(&ecr->completion);
  20. }
  21. /**
  22. * derive_key_aes() - Derive a key using AES-128-ECB
  23. * @deriving_key: Encryption key used for derivation.
  24. * @source_key: Source key to which to apply derivation.
  25. * @derived_key: Derived key.
  26. *
  27. * Return: Zero on success; non-zero otherwise.
  28. */
  29. static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
  30. u8 source_key[FS_AES_256_XTS_KEY_SIZE],
  31. u8 derived_key[FS_AES_256_XTS_KEY_SIZE])
  32. {
  33. int res = 0;
  34. struct skcipher_request *req = NULL;
  35. DECLARE_FS_COMPLETION_RESULT(ecr);
  36. struct scatterlist src_sg, dst_sg;
  37. struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
  38. if (IS_ERR(tfm)) {
  39. res = PTR_ERR(tfm);
  40. tfm = NULL;
  41. goto out;
  42. }
  43. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  44. req = skcipher_request_alloc(tfm, GFP_NOFS);
  45. if (!req) {
  46. res = -ENOMEM;
  47. goto out;
  48. }
  49. skcipher_request_set_callback(req,
  50. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  51. derive_crypt_complete, &ecr);
  52. res = crypto_skcipher_setkey(tfm, deriving_key,
  53. FS_AES_128_ECB_KEY_SIZE);
  54. if (res < 0)
  55. goto out;
  56. sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE);
  57. sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE);
  58. skcipher_request_set_crypt(req, &src_sg, &dst_sg,
  59. FS_AES_256_XTS_KEY_SIZE, NULL);
  60. res = crypto_skcipher_encrypt(req);
  61. if (res == -EINPROGRESS || res == -EBUSY) {
  62. wait_for_completion(&ecr.completion);
  63. res = ecr.res;
  64. }
  65. out:
  66. skcipher_request_free(req);
  67. crypto_free_skcipher(tfm);
  68. return res;
  69. }
  70. static int validate_user_key(struct fscrypt_info *crypt_info,
  71. struct fscrypt_context *ctx, u8 *raw_key,
  72. u8 *prefix, int prefix_size)
  73. {
  74. u8 *full_key_descriptor;
  75. struct key *keyring_key;
  76. struct fscrypt_key *master_key;
  77. const struct user_key_payload *ukp;
  78. int full_key_len = prefix_size + (FS_KEY_DESCRIPTOR_SIZE * 2) + 1;
  79. int res;
  80. full_key_descriptor = kmalloc(full_key_len, GFP_NOFS);
  81. if (!full_key_descriptor)
  82. return -ENOMEM;
  83. memcpy(full_key_descriptor, prefix, prefix_size);
  84. sprintf(full_key_descriptor + prefix_size,
  85. "%*phN", FS_KEY_DESCRIPTOR_SIZE,
  86. ctx->master_key_descriptor);
  87. full_key_descriptor[full_key_len - 1] = '\0';
  88. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  89. kfree(full_key_descriptor);
  90. if (IS_ERR(keyring_key))
  91. return PTR_ERR(keyring_key);
  92. if (keyring_key->type != &key_type_logon) {
  93. printk_once(KERN_WARNING
  94. "%s: key type must be logon\n", __func__);
  95. res = -ENOKEY;
  96. goto out;
  97. }
  98. down_read(&keyring_key->sem);
  99. ukp = user_key_payload(keyring_key);
  100. if (ukp->datalen != sizeof(struct fscrypt_key)) {
  101. res = -EINVAL;
  102. up_read(&keyring_key->sem);
  103. goto out;
  104. }
  105. master_key = (struct fscrypt_key *)ukp->data;
  106. BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
  107. if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
  108. printk_once(KERN_WARNING
  109. "%s: key size incorrect: %d\n",
  110. __func__, master_key->size);
  111. res = -ENOKEY;
  112. up_read(&keyring_key->sem);
  113. goto out;
  114. }
  115. res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
  116. up_read(&keyring_key->sem);
  117. if (res)
  118. goto out;
  119. crypt_info->ci_keyring_key = keyring_key;
  120. return 0;
  121. out:
  122. key_put(keyring_key);
  123. return res;
  124. }
  125. static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
  126. const char **cipher_str_ret, int *keysize_ret)
  127. {
  128. if (S_ISREG(inode->i_mode)) {
  129. if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
  130. *cipher_str_ret = "xts(aes)";
  131. *keysize_ret = FS_AES_256_XTS_KEY_SIZE;
  132. return 0;
  133. }
  134. pr_warn_once("fscrypto: unsupported contents encryption mode "
  135. "%d for inode %lu\n",
  136. ci->ci_data_mode, inode->i_ino);
  137. return -ENOKEY;
  138. }
  139. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  140. if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
  141. *cipher_str_ret = "cts(cbc(aes))";
  142. *keysize_ret = FS_AES_256_CTS_KEY_SIZE;
  143. return 0;
  144. }
  145. pr_warn_once("fscrypto: unsupported filenames encryption mode "
  146. "%d for inode %lu\n",
  147. ci->ci_filename_mode, inode->i_ino);
  148. return -ENOKEY;
  149. }
  150. pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
  151. (inode->i_mode & S_IFMT), inode->i_ino);
  152. return -ENOKEY;
  153. }
  154. static void put_crypt_info(struct fscrypt_info *ci)
  155. {
  156. if (!ci)
  157. return;
  158. key_put(ci->ci_keyring_key);
  159. crypto_free_skcipher(ci->ci_ctfm);
  160. kmem_cache_free(fscrypt_info_cachep, ci);
  161. }
  162. int fscrypt_get_crypt_info(struct inode *inode)
  163. {
  164. struct fscrypt_info *crypt_info;
  165. struct fscrypt_context ctx;
  166. struct crypto_skcipher *ctfm;
  167. const char *cipher_str;
  168. int keysize;
  169. u8 *raw_key = NULL;
  170. int res;
  171. res = fscrypt_initialize(inode->i_sb->s_cop->flags);
  172. if (res)
  173. return res;
  174. if (!inode->i_sb->s_cop->get_context)
  175. return -EOPNOTSUPP;
  176. retry:
  177. crypt_info = ACCESS_ONCE(inode->i_crypt_info);
  178. if (crypt_info) {
  179. if (!crypt_info->ci_keyring_key ||
  180. key_validate(crypt_info->ci_keyring_key) == 0)
  181. return 0;
  182. fscrypt_put_encryption_info(inode, crypt_info);
  183. goto retry;
  184. }
  185. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  186. if (res < 0) {
  187. if (!fscrypt_dummy_context_enabled(inode))
  188. return res;
  189. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  190. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  191. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  192. ctx.flags = 0;
  193. } else if (res != sizeof(ctx)) {
  194. return -EINVAL;
  195. }
  196. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  197. return -EINVAL;
  198. if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
  199. return -EINVAL;
  200. crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
  201. if (!crypt_info)
  202. return -ENOMEM;
  203. crypt_info->ci_flags = ctx.flags;
  204. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  205. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  206. crypt_info->ci_ctfm = NULL;
  207. crypt_info->ci_keyring_key = NULL;
  208. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  209. sizeof(crypt_info->ci_master_key));
  210. res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
  211. if (res)
  212. goto out;
  213. /*
  214. * This cannot be a stack buffer because it is passed to the scatterlist
  215. * crypto API as part of key derivation.
  216. */
  217. res = -ENOMEM;
  218. raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
  219. if (!raw_key)
  220. goto out;
  221. if (fscrypt_dummy_context_enabled(inode)) {
  222. memset(raw_key, 0x42, keysize/2);
  223. memset(raw_key+keysize/2, 0x24, keysize - (keysize/2));
  224. goto got_key;
  225. }
  226. res = validate_user_key(crypt_info, &ctx, raw_key,
  227. FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
  228. if (res && inode->i_sb->s_cop->key_prefix) {
  229. u8 *prefix = NULL;
  230. int prefix_size, res2;
  231. prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
  232. res2 = validate_user_key(crypt_info, &ctx, raw_key,
  233. prefix, prefix_size);
  234. if (res2) {
  235. if (res2 == -ENOKEY)
  236. res = -ENOKEY;
  237. goto out;
  238. }
  239. } else if (res) {
  240. goto out;
  241. }
  242. got_key:
  243. ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
  244. if (!ctfm || IS_ERR(ctfm)) {
  245. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  246. printk(KERN_DEBUG
  247. "%s: error %d (inode %u) allocating crypto tfm\n",
  248. __func__, res, (unsigned) inode->i_ino);
  249. goto out;
  250. }
  251. crypt_info->ci_ctfm = ctfm;
  252. crypto_skcipher_clear_flags(ctfm, ~0);
  253. crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
  254. res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
  255. if (res)
  256. goto out;
  257. kzfree(raw_key);
  258. raw_key = NULL;
  259. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) {
  260. put_crypt_info(crypt_info);
  261. goto retry;
  262. }
  263. return 0;
  264. out:
  265. if (res == -ENOKEY)
  266. res = 0;
  267. put_crypt_info(crypt_info);
  268. kzfree(raw_key);
  269. return res;
  270. }
  271. void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
  272. {
  273. struct fscrypt_info *prev;
  274. if (ci == NULL)
  275. ci = ACCESS_ONCE(inode->i_crypt_info);
  276. if (ci == NULL)
  277. return;
  278. prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
  279. if (prev != ci)
  280. return;
  281. put_crypt_info(ci);
  282. }
  283. EXPORT_SYMBOL(fscrypt_put_encryption_info);
  284. int fscrypt_get_encryption_info(struct inode *inode)
  285. {
  286. struct fscrypt_info *ci = inode->i_crypt_info;
  287. if (!ci ||
  288. (ci->ci_keyring_key &&
  289. (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
  290. (1 << KEY_FLAG_REVOKED) |
  291. (1 << KEY_FLAG_DEAD)))))
  292. return fscrypt_get_crypt_info(inode);
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(fscrypt_get_encryption_info);