keyinfo.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/fscrypto.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. 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(keyring_key);
  100. if (!ukp) {
  101. /* key was revoked before we acquired its semaphore */
  102. res = -EKEYREVOKED;
  103. goto out;
  104. }
  105. if (ukp->datalen != sizeof(struct fscrypt_key)) {
  106. res = -EINVAL;
  107. goto out;
  108. }
  109. master_key = (struct fscrypt_key *)ukp->data;
  110. BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
  111. if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
  112. printk_once(KERN_WARNING
  113. "%s: key size incorrect: %d\n",
  114. __func__, master_key->size);
  115. res = -ENOKEY;
  116. goto out;
  117. }
  118. res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
  119. out:
  120. up_read(&keyring_key->sem);
  121. key_put(keyring_key);
  122. return res;
  123. }
  124. static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
  125. const char **cipher_str_ret, int *keysize_ret)
  126. {
  127. if (S_ISREG(inode->i_mode)) {
  128. if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
  129. *cipher_str_ret = "xts(aes)";
  130. *keysize_ret = FS_AES_256_XTS_KEY_SIZE;
  131. return 0;
  132. }
  133. pr_warn_once("fscrypto: unsupported contents encryption mode "
  134. "%d for inode %lu\n",
  135. ci->ci_data_mode, inode->i_ino);
  136. return -ENOKEY;
  137. }
  138. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  139. if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
  140. *cipher_str_ret = "cts(cbc(aes))";
  141. *keysize_ret = FS_AES_256_CTS_KEY_SIZE;
  142. return 0;
  143. }
  144. pr_warn_once("fscrypto: unsupported filenames encryption mode "
  145. "%d for inode %lu\n",
  146. ci->ci_filename_mode, inode->i_ino);
  147. return -ENOKEY;
  148. }
  149. pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
  150. (inode->i_mode & S_IFMT), inode->i_ino);
  151. return -ENOKEY;
  152. }
  153. static void put_crypt_info(struct fscrypt_info *ci)
  154. {
  155. if (!ci)
  156. return;
  157. crypto_free_skcipher(ci->ci_ctfm);
  158. kmem_cache_free(fscrypt_info_cachep, ci);
  159. }
  160. int fscrypt_get_encryption_info(struct inode *inode)
  161. {
  162. struct fscrypt_info *crypt_info;
  163. struct fscrypt_context ctx;
  164. struct crypto_skcipher *ctfm;
  165. const char *cipher_str;
  166. int keysize;
  167. u8 *raw_key = NULL;
  168. int res;
  169. if (inode->i_crypt_info)
  170. return 0;
  171. res = fscrypt_initialize();
  172. if (res)
  173. return res;
  174. if (!inode->i_sb->s_cop->get_context)
  175. return -EOPNOTSUPP;
  176. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  177. if (res < 0) {
  178. if (!fscrypt_dummy_context_enabled(inode))
  179. return res;
  180. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  181. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  182. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  183. ctx.flags = 0;
  184. } else if (res != sizeof(ctx)) {
  185. return -EINVAL;
  186. }
  187. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  188. return -EINVAL;
  189. if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
  190. return -EINVAL;
  191. crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
  192. if (!crypt_info)
  193. return -ENOMEM;
  194. crypt_info->ci_flags = ctx.flags;
  195. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  196. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  197. crypt_info->ci_ctfm = NULL;
  198. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  199. sizeof(crypt_info->ci_master_key));
  200. res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
  201. if (res)
  202. goto out;
  203. /*
  204. * This cannot be a stack buffer because it is passed to the scatterlist
  205. * crypto API as part of key derivation.
  206. */
  207. res = -ENOMEM;
  208. raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
  209. if (!raw_key)
  210. goto out;
  211. if (fscrypt_dummy_context_enabled(inode)) {
  212. memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
  213. goto got_key;
  214. }
  215. res = validate_user_key(crypt_info, &ctx, raw_key,
  216. FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
  217. if (res && inode->i_sb->s_cop->key_prefix) {
  218. u8 *prefix = NULL;
  219. int prefix_size, res2;
  220. prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
  221. res2 = validate_user_key(crypt_info, &ctx, raw_key,
  222. prefix, prefix_size);
  223. if (res2) {
  224. if (res2 == -ENOKEY)
  225. res = -ENOKEY;
  226. goto out;
  227. }
  228. } else if (res) {
  229. goto out;
  230. }
  231. got_key:
  232. ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
  233. if (!ctfm || IS_ERR(ctfm)) {
  234. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  235. printk(KERN_DEBUG
  236. "%s: error %d (inode %u) allocating crypto tfm\n",
  237. __func__, res, (unsigned) inode->i_ino);
  238. goto out;
  239. }
  240. crypt_info->ci_ctfm = ctfm;
  241. crypto_skcipher_clear_flags(ctfm, ~0);
  242. crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
  243. res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
  244. if (res)
  245. goto out;
  246. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
  247. crypt_info = NULL;
  248. out:
  249. if (res == -ENOKEY)
  250. res = 0;
  251. put_crypt_info(crypt_info);
  252. kzfree(raw_key);
  253. return res;
  254. }
  255. EXPORT_SYMBOL(fscrypt_get_encryption_info);
  256. void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
  257. {
  258. struct fscrypt_info *prev;
  259. if (ci == NULL)
  260. ci = ACCESS_ONCE(inode->i_crypt_info);
  261. if (ci == NULL)
  262. return;
  263. prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
  264. if (prev != ci)
  265. return;
  266. put_crypt_info(ci);
  267. }
  268. EXPORT_SYMBOL(fscrypt_put_encryption_info);