big_key.c 6.6 KB

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