big_key.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Large capacity key type
  2. *
  3. * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "big_key: "fmt
  12. #include <linux/init.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/file.h>
  15. #include <linux/shmem_fs.h>
  16. #include <linux/err.h>
  17. #include <linux/scatterlist.h>
  18. #include <keys/user-type.h>
  19. #include <keys/big_key-type.h>
  20. #include <crypto/rng.h>
  21. #include <crypto/skcipher.h>
  22. /*
  23. * Layout of key payload words.
  24. */
  25. enum {
  26. big_key_data,
  27. big_key_path,
  28. big_key_path_2nd_part,
  29. big_key_len,
  30. };
  31. /*
  32. * Crypto operation with big_key data
  33. */
  34. enum big_key_op {
  35. BIG_KEY_ENC,
  36. BIG_KEY_DEC,
  37. };
  38. /*
  39. * If the data is under this limit, there's no point creating a shm file to
  40. * hold it as the permanently resident metadata for the shmem fs will be at
  41. * least as large as the data.
  42. */
  43. #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
  44. /*
  45. * Key size for big_key data encryption
  46. */
  47. #define ENC_KEY_SIZE 16
  48. /*
  49. * big_key defined keys take an arbitrary string as the description and an
  50. * arbitrary blob of data as the payload
  51. */
  52. struct key_type key_type_big_key = {
  53. .name = "big_key",
  54. .preparse = big_key_preparse,
  55. .free_preparse = big_key_free_preparse,
  56. .instantiate = generic_key_instantiate,
  57. .revoke = big_key_revoke,
  58. .destroy = big_key_destroy,
  59. .describe = big_key_describe,
  60. .read = big_key_read,
  61. };
  62. /*
  63. * Crypto names for big_key data encryption
  64. */
  65. static const char big_key_rng_name[] = "stdrng";
  66. static const char big_key_alg_name[] = "ecb(aes)";
  67. /*
  68. * Crypto algorithms for big_key data encryption
  69. */
  70. static struct crypto_rng *big_key_rng;
  71. static struct crypto_skcipher *big_key_skcipher;
  72. /*
  73. * Generate random key to encrypt big_key data
  74. */
  75. static inline int big_key_gen_enckey(u8 *key)
  76. {
  77. return crypto_rng_get_bytes(big_key_rng, key, ENC_KEY_SIZE);
  78. }
  79. /*
  80. * Encrypt/decrypt big_key data
  81. */
  82. static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
  83. {
  84. int ret = -EINVAL;
  85. struct scatterlist sgio;
  86. SKCIPHER_REQUEST_ON_STACK(req, big_key_skcipher);
  87. if (crypto_skcipher_setkey(big_key_skcipher, key, ENC_KEY_SIZE)) {
  88. ret = -EAGAIN;
  89. goto error;
  90. }
  91. skcipher_request_set_tfm(req, big_key_skcipher);
  92. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  93. NULL, NULL);
  94. sg_init_one(&sgio, data, datalen);
  95. skcipher_request_set_crypt(req, &sgio, &sgio, datalen, NULL);
  96. if (op == BIG_KEY_ENC)
  97. ret = crypto_skcipher_encrypt(req);
  98. else
  99. ret = crypto_skcipher_decrypt(req);
  100. skcipher_request_zero(req);
  101. error:
  102. return ret;
  103. }
  104. /*
  105. * Preparse a big key
  106. */
  107. int big_key_preparse(struct key_preparsed_payload *prep)
  108. {
  109. struct path *path = (struct path *)&prep->payload.data[big_key_path];
  110. struct file *file;
  111. u8 *enckey;
  112. u8 *data = NULL;
  113. ssize_t written;
  114. size_t datalen = prep->datalen;
  115. int ret;
  116. ret = -EINVAL;
  117. if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
  118. goto error;
  119. /* Set an arbitrary quota */
  120. prep->quotalen = 16;
  121. prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
  122. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  123. /* Create a shmem file to store the data in. This will permit the data
  124. * to be swapped out if needed.
  125. *
  126. * File content is stored encrypted with randomly generated key.
  127. */
  128. size_t enclen = ALIGN(datalen, crypto_skcipher_blocksize(big_key_skcipher));
  129. /* prepare aligned data to encrypt */
  130. data = kmalloc(enclen, GFP_KERNEL);
  131. if (!data)
  132. return -ENOMEM;
  133. memcpy(data, prep->data, datalen);
  134. memset(data + datalen, 0x00, enclen - datalen);
  135. /* generate random key */
  136. enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
  137. if (!enckey) {
  138. ret = -ENOMEM;
  139. goto error;
  140. }
  141. ret = big_key_gen_enckey(enckey);
  142. if (ret)
  143. goto err_enckey;
  144. /* encrypt aligned data */
  145. ret = big_key_crypt(BIG_KEY_ENC, data, enclen, enckey);
  146. if (ret)
  147. goto err_enckey;
  148. /* save aligned data to file */
  149. file = shmem_kernel_file_setup("", enclen, 0);
  150. if (IS_ERR(file)) {
  151. ret = PTR_ERR(file);
  152. goto err_enckey;
  153. }
  154. written = kernel_write(file, data, enclen, 0);
  155. if (written != enclen) {
  156. ret = written;
  157. if (written >= 0)
  158. ret = -ENOMEM;
  159. goto err_fput;
  160. }
  161. /* Pin the mount and dentry to the key so that we can open it again
  162. * later
  163. */
  164. prep->payload.data[big_key_data] = enckey;
  165. *path = file->f_path;
  166. path_get(path);
  167. fput(file);
  168. kfree(data);
  169. } else {
  170. /* Just store the data in a buffer */
  171. void *data = kmalloc(datalen, GFP_KERNEL);
  172. if (!data)
  173. return -ENOMEM;
  174. prep->payload.data[big_key_data] = data;
  175. memcpy(data, prep->data, prep->datalen);
  176. }
  177. return 0;
  178. err_fput:
  179. fput(file);
  180. err_enckey:
  181. kfree(enckey);
  182. error:
  183. kfree(data);
  184. return ret;
  185. }
  186. /*
  187. * Clear preparsement.
  188. */
  189. void big_key_free_preparse(struct key_preparsed_payload *prep)
  190. {
  191. if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
  192. struct path *path = (struct path *)&prep->payload.data[big_key_path];
  193. path_put(path);
  194. }
  195. kfree(prep->payload.data[big_key_data]);
  196. }
  197. /*
  198. * dispose of the links from a revoked keyring
  199. * - called with the key sem write-locked
  200. */
  201. void big_key_revoke(struct key *key)
  202. {
  203. struct path *path = (struct path *)&key->payload.data[big_key_path];
  204. /* clear the quota */
  205. key_payload_reserve(key, 0);
  206. if (key_is_instantiated(key) &&
  207. (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
  208. vfs_truncate(path, 0);
  209. }
  210. /*
  211. * dispose of the data dangling from the corpse of a big_key key
  212. */
  213. void big_key_destroy(struct key *key)
  214. {
  215. size_t datalen = (size_t)key->payload.data[big_key_len];
  216. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  217. struct path *path = (struct path *)&key->payload.data[big_key_path];
  218. path_put(path);
  219. path->mnt = NULL;
  220. path->dentry = NULL;
  221. }
  222. kfree(key->payload.data[big_key_data]);
  223. key->payload.data[big_key_data] = NULL;
  224. }
  225. /*
  226. * describe the big_key key
  227. */
  228. void big_key_describe(const struct key *key, struct seq_file *m)
  229. {
  230. size_t datalen = (size_t)key->payload.data[big_key_len];
  231. seq_puts(m, key->description);
  232. if (key_is_instantiated(key))
  233. seq_printf(m, ": %zu [%s]",
  234. datalen,
  235. datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
  236. }
  237. /*
  238. * read the key data
  239. * - the key's semaphore is read-locked
  240. */
  241. long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
  242. {
  243. size_t datalen = (size_t)key->payload.data[big_key_len];
  244. long ret;
  245. if (!buffer || buflen < datalen)
  246. return datalen;
  247. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  248. struct path *path = (struct path *)&key->payload.data[big_key_path];
  249. struct file *file;
  250. u8 *data;
  251. u8 *enckey = (u8 *)key->payload.data[big_key_data];
  252. size_t enclen = ALIGN(datalen, crypto_skcipher_blocksize(big_key_skcipher));
  253. data = kmalloc(enclen, GFP_KERNEL);
  254. if (!data)
  255. return -ENOMEM;
  256. file = dentry_open(path, O_RDONLY, current_cred());
  257. if (IS_ERR(file)) {
  258. ret = PTR_ERR(file);
  259. goto error;
  260. }
  261. /* read file to kernel and decrypt */
  262. ret = kernel_read(file, 0, data, enclen);
  263. if (ret >= 0 && ret != enclen) {
  264. ret = -EIO;
  265. goto err_fput;
  266. }
  267. ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey);
  268. if (ret)
  269. goto err_fput;
  270. ret = datalen;
  271. /* copy decrypted data to user */
  272. if (copy_to_user(buffer, data, datalen) != 0)
  273. ret = -EFAULT;
  274. err_fput:
  275. fput(file);
  276. error:
  277. kfree(data);
  278. } else {
  279. ret = datalen;
  280. if (copy_to_user(buffer, key->payload.data[big_key_data],
  281. datalen) != 0)
  282. ret = -EFAULT;
  283. }
  284. return ret;
  285. }
  286. /*
  287. * Register key type
  288. */
  289. static int __init big_key_init(void)
  290. {
  291. struct crypto_skcipher *cipher;
  292. struct crypto_rng *rng;
  293. int ret;
  294. rng = crypto_alloc_rng(big_key_rng_name, 0, 0);
  295. if (IS_ERR(rng)) {
  296. pr_err("Can't alloc rng: %ld\n", PTR_ERR(rng));
  297. return PTR_ERR(rng);
  298. }
  299. big_key_rng = rng;
  300. /* seed RNG */
  301. ret = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
  302. if (ret) {
  303. pr_err("Can't reset rng: %d\n", ret);
  304. goto error_rng;
  305. }
  306. /* init block cipher */
  307. cipher = crypto_alloc_skcipher(big_key_alg_name, 0, CRYPTO_ALG_ASYNC);
  308. if (IS_ERR(cipher)) {
  309. ret = PTR_ERR(cipher);
  310. pr_err("Can't alloc crypto: %d\n", ret);
  311. goto error_rng;
  312. }
  313. big_key_skcipher = cipher;
  314. ret = register_key_type(&key_type_big_key);
  315. if (ret < 0) {
  316. pr_err("Can't register type: %d\n", ret);
  317. goto error_cipher;
  318. }
  319. return 0;
  320. error_cipher:
  321. crypto_free_skcipher(big_key_skcipher);
  322. error_rng:
  323. crypto_free_rng(big_key_rng);
  324. return ret;
  325. }
  326. late_initcall(big_key_init);