keyinfo.c 7.7 KB

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