big_key.c 8.7 KB

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