big_key.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Large capacity key type
  3. *
  4. * Copyright (C) 2017-2020 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  5. * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells ([email protected])
  7. */
  8. #define pr_fmt(fmt) "big_key: "fmt
  9. #include <linux/init.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/file.h>
  12. #include <linux/shmem_fs.h>
  13. #include <linux/err.h>
  14. #include <linux/random.h>
  15. #include <keys/user-type.h>
  16. #include <keys/big_key-type.h>
  17. #include <crypto/chacha20poly1305.h>
  18. /*
  19. * Layout of key payload words.
  20. */
  21. struct big_key_payload {
  22. u8 *data;
  23. struct path path;
  24. size_t length;
  25. };
  26. #define to_big_key_payload(payload) \
  27. (struct big_key_payload *)((payload).data)
  28. /*
  29. * If the data is under this limit, there's no point creating a shm file to
  30. * hold it as the permanently resident metadata for the shmem fs will be at
  31. * least as large as the data.
  32. */
  33. #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
  34. /*
  35. * big_key defined keys take an arbitrary string as the description and an
  36. * arbitrary blob of data as the payload
  37. */
  38. struct key_type key_type_big_key = {
  39. .name = "big_key",
  40. .preparse = big_key_preparse,
  41. .free_preparse = big_key_free_preparse,
  42. .instantiate = generic_key_instantiate,
  43. .revoke = big_key_revoke,
  44. .destroy = big_key_destroy,
  45. .describe = big_key_describe,
  46. .read = big_key_read,
  47. .update = big_key_update,
  48. };
  49. /*
  50. * Preparse a big key
  51. */
  52. int big_key_preparse(struct key_preparsed_payload *prep)
  53. {
  54. struct big_key_payload *payload = to_big_key_payload(prep->payload);
  55. struct file *file;
  56. u8 *buf, *enckey;
  57. ssize_t written;
  58. size_t datalen = prep->datalen;
  59. size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
  60. int ret;
  61. BUILD_BUG_ON(sizeof(*payload) != sizeof(prep->payload.data));
  62. if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
  63. return -EINVAL;
  64. /* Set an arbitrary quota */
  65. prep->quotalen = 16;
  66. payload->length = datalen;
  67. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  68. /* Create a shmem file to store the data in. This will permit the data
  69. * to be swapped out if needed.
  70. *
  71. * File content is stored encrypted with randomly generated key.
  72. * Since the key is random for each file, we can set the nonce
  73. * to zero, provided we never define a ->update() call.
  74. */
  75. loff_t pos = 0;
  76. buf = kvmalloc(enclen, GFP_KERNEL);
  77. if (!buf)
  78. return -ENOMEM;
  79. /* generate random key */
  80. enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
  81. if (!enckey) {
  82. ret = -ENOMEM;
  83. goto error;
  84. }
  85. ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE);
  86. if (unlikely(ret))
  87. goto err_enckey;
  88. /* encrypt data */
  89. chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
  90. 0, enckey);
  91. /* save aligned data to file */
  92. file = shmem_kernel_file_setup("", enclen, 0);
  93. if (IS_ERR(file)) {
  94. ret = PTR_ERR(file);
  95. goto err_enckey;
  96. }
  97. written = kernel_write(file, buf, enclen, &pos);
  98. if (written != enclen) {
  99. ret = written;
  100. if (written >= 0)
  101. ret = -EIO;
  102. goto err_fput;
  103. }
  104. /* Pin the mount and dentry to the key so that we can open it again
  105. * later
  106. */
  107. payload->data = enckey;
  108. payload->path = file->f_path;
  109. path_get(&payload->path);
  110. fput(file);
  111. kvfree_sensitive(buf, enclen);
  112. } else {
  113. /* Just store the data in a buffer */
  114. void *data = kmalloc(datalen, GFP_KERNEL);
  115. if (!data)
  116. return -ENOMEM;
  117. payload->data = data;
  118. memcpy(data, prep->data, prep->datalen);
  119. }
  120. return 0;
  121. err_fput:
  122. fput(file);
  123. err_enckey:
  124. kfree_sensitive(enckey);
  125. error:
  126. kvfree_sensitive(buf, enclen);
  127. return ret;
  128. }
  129. /*
  130. * Clear preparsement.
  131. */
  132. void big_key_free_preparse(struct key_preparsed_payload *prep)
  133. {
  134. struct big_key_payload *payload = to_big_key_payload(prep->payload);
  135. if (prep->datalen > BIG_KEY_FILE_THRESHOLD)
  136. path_put(&payload->path);
  137. kfree_sensitive(payload->data);
  138. }
  139. /*
  140. * dispose of the links from a revoked keyring
  141. * - called with the key sem write-locked
  142. */
  143. void big_key_revoke(struct key *key)
  144. {
  145. struct big_key_payload *payload = to_big_key_payload(key->payload);
  146. /* clear the quota */
  147. key_payload_reserve(key, 0);
  148. if (key_is_positive(key) && payload->length > BIG_KEY_FILE_THRESHOLD)
  149. vfs_truncate(&payload->path, 0);
  150. }
  151. /*
  152. * dispose of the data dangling from the corpse of a big_key key
  153. */
  154. void big_key_destroy(struct key *key)
  155. {
  156. struct big_key_payload *payload = to_big_key_payload(key->payload);
  157. if (payload->length > BIG_KEY_FILE_THRESHOLD) {
  158. path_put(&payload->path);
  159. payload->path.mnt = NULL;
  160. payload->path.dentry = NULL;
  161. }
  162. kfree_sensitive(payload->data);
  163. payload->data = NULL;
  164. }
  165. /*
  166. * Update a big key
  167. */
  168. int big_key_update(struct key *key, struct key_preparsed_payload *prep)
  169. {
  170. int ret;
  171. ret = key_payload_reserve(key, prep->datalen);
  172. if (ret < 0)
  173. return ret;
  174. if (key_is_positive(key))
  175. big_key_destroy(key);
  176. return generic_key_instantiate(key, prep);
  177. }
  178. /*
  179. * describe the big_key key
  180. */
  181. void big_key_describe(const struct key *key, struct seq_file *m)
  182. {
  183. struct big_key_payload *payload = to_big_key_payload(key->payload);
  184. seq_puts(m, key->description);
  185. if (key_is_positive(key))
  186. seq_printf(m, ": %zu [%s]",
  187. payload->length,
  188. payload->length > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
  189. }
  190. /*
  191. * read the key data
  192. * - the key's semaphore is read-locked
  193. */
  194. long big_key_read(const struct key *key, char *buffer, size_t buflen)
  195. {
  196. struct big_key_payload *payload = to_big_key_payload(key->payload);
  197. size_t datalen = payload->length;
  198. long ret;
  199. if (!buffer || buflen < datalen)
  200. return datalen;
  201. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  202. struct file *file;
  203. u8 *buf, *enckey = payload->data;
  204. size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
  205. loff_t pos = 0;
  206. buf = kvmalloc(enclen, GFP_KERNEL);
  207. if (!buf)
  208. return -ENOMEM;
  209. file = dentry_open(&payload->path, O_RDONLY, current_cred());
  210. if (IS_ERR(file)) {
  211. ret = PTR_ERR(file);
  212. goto error;
  213. }
  214. /* read file to kernel and decrypt */
  215. ret = kernel_read(file, buf, enclen, &pos);
  216. if (ret != enclen) {
  217. if (ret >= 0)
  218. ret = -EIO;
  219. goto err_fput;
  220. }
  221. ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
  222. enckey) ? 0 : -EBADMSG;
  223. if (unlikely(ret))
  224. goto err_fput;
  225. ret = datalen;
  226. /* copy out decrypted data */
  227. memcpy(buffer, buf, datalen);
  228. err_fput:
  229. fput(file);
  230. error:
  231. kvfree_sensitive(buf, enclen);
  232. } else {
  233. ret = datalen;
  234. memcpy(buffer, payload->data, datalen);
  235. }
  236. return ret;
  237. }
  238. /*
  239. * Register key type
  240. */
  241. static int __init big_key_init(void)
  242. {
  243. return register_key_type(&key_type_big_key);
  244. }
  245. late_initcall(big_key_init);