big_key.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <linux/vmalloc.h>
  21. #include <keys/user-type.h>
  22. #include <keys/big_key-type.h>
  23. #include <crypto/aead.h>
  24. struct big_key_buf {
  25. unsigned int nr_pages;
  26. void *virt;
  27. struct scatterlist *sg;
  28. struct page *pages[];
  29. };
  30. /*
  31. * Layout of key payload words.
  32. */
  33. enum {
  34. big_key_data,
  35. big_key_path,
  36. big_key_path_2nd_part,
  37. big_key_len,
  38. };
  39. /*
  40. * Crypto operation with big_key data
  41. */
  42. enum big_key_op {
  43. BIG_KEY_ENC,
  44. BIG_KEY_DEC,
  45. };
  46. /*
  47. * If the data is under this limit, there's no point creating a shm file to
  48. * hold it as the permanently resident metadata for the shmem fs will be at
  49. * least as large as the data.
  50. */
  51. #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
  52. /*
  53. * Key size for big_key data encryption
  54. */
  55. #define ENC_KEY_SIZE 32
  56. /*
  57. * Authentication tag length
  58. */
  59. #define ENC_AUTHTAG_SIZE 16
  60. /*
  61. * big_key defined keys take an arbitrary string as the description and an
  62. * arbitrary blob of data as the payload
  63. */
  64. struct key_type key_type_big_key = {
  65. .name = "big_key",
  66. .preparse = big_key_preparse,
  67. .free_preparse = big_key_free_preparse,
  68. .instantiate = generic_key_instantiate,
  69. .revoke = big_key_revoke,
  70. .destroy = big_key_destroy,
  71. .describe = big_key_describe,
  72. .read = big_key_read,
  73. /* no ->update(); don't add it without changing big_key_crypt() nonce */
  74. };
  75. /*
  76. * Crypto names for big_key data authenticated encryption
  77. */
  78. static const char big_key_alg_name[] = "gcm(aes)";
  79. /*
  80. * Crypto algorithms for big_key data authenticated encryption
  81. */
  82. static struct crypto_aead *big_key_aead;
  83. /*
  84. * Since changing the key affects the entire object, we need a mutex.
  85. */
  86. static DEFINE_MUTEX(big_key_aead_lock);
  87. /*
  88. * Encrypt/decrypt big_key data
  89. */
  90. static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t datalen, u8 *key)
  91. {
  92. int ret;
  93. struct aead_request *aead_req;
  94. /* We always use a zero nonce. The reason we can get away with this is
  95. * because we're using a different randomly generated key for every
  96. * different encryption. Notably, too, key_type_big_key doesn't define
  97. * an .update function, so there's no chance we'll wind up reusing the
  98. * key to encrypt updated data. Simply put: one key, one encryption.
  99. */
  100. u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];
  101. aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
  102. if (!aead_req)
  103. return -ENOMEM;
  104. memset(zero_nonce, 0, sizeof(zero_nonce));
  105. aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce);
  106. aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  107. aead_request_set_ad(aead_req, 0);
  108. mutex_lock(&big_key_aead_lock);
  109. if (crypto_aead_setkey(big_key_aead, key, ENC_KEY_SIZE)) {
  110. ret = -EAGAIN;
  111. goto error;
  112. }
  113. if (op == BIG_KEY_ENC)
  114. ret = crypto_aead_encrypt(aead_req);
  115. else
  116. ret = crypto_aead_decrypt(aead_req);
  117. error:
  118. mutex_unlock(&big_key_aead_lock);
  119. aead_request_free(aead_req);
  120. return ret;
  121. }
  122. /*
  123. * Free up the buffer.
  124. */
  125. static void big_key_free_buffer(struct big_key_buf *buf)
  126. {
  127. unsigned int i;
  128. if (buf->virt) {
  129. memset(buf->virt, 0, buf->nr_pages * PAGE_SIZE);
  130. vunmap(buf->virt);
  131. }
  132. for (i = 0; i < buf->nr_pages; i++)
  133. if (buf->pages[i])
  134. __free_page(buf->pages[i]);
  135. kfree(buf);
  136. }
  137. /*
  138. * Allocate a buffer consisting of a set of pages with a virtual mapping
  139. * applied over them.
  140. */
  141. static void *big_key_alloc_buffer(size_t len)
  142. {
  143. struct big_key_buf *buf;
  144. unsigned int npg = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  145. unsigned int i, l;
  146. buf = kzalloc(sizeof(struct big_key_buf) +
  147. sizeof(struct page) * npg +
  148. sizeof(struct scatterlist) * npg,
  149. GFP_KERNEL);
  150. if (!buf)
  151. return NULL;
  152. buf->nr_pages = npg;
  153. buf->sg = (void *)(buf->pages + npg);
  154. sg_init_table(buf->sg, npg);
  155. for (i = 0; i < buf->nr_pages; i++) {
  156. buf->pages[i] = alloc_page(GFP_KERNEL);
  157. if (!buf->pages[i])
  158. goto nomem;
  159. l = min_t(size_t, len, PAGE_SIZE);
  160. sg_set_page(&buf->sg[i], buf->pages[i], l, 0);
  161. len -= l;
  162. }
  163. buf->virt = vmap(buf->pages, buf->nr_pages, VM_MAP, PAGE_KERNEL);
  164. if (!buf->virt)
  165. goto nomem;
  166. return buf;
  167. nomem:
  168. big_key_free_buffer(buf);
  169. return NULL;
  170. }
  171. /*
  172. * Preparse a big key
  173. */
  174. int big_key_preparse(struct key_preparsed_payload *prep)
  175. {
  176. struct big_key_buf *buf;
  177. struct path *path = (struct path *)&prep->payload.data[big_key_path];
  178. struct file *file;
  179. u8 *enckey;
  180. ssize_t written;
  181. size_t datalen = prep->datalen, enclen = datalen + ENC_AUTHTAG_SIZE;
  182. int ret;
  183. if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
  184. return -EINVAL;
  185. /* Set an arbitrary quota */
  186. prep->quotalen = 16;
  187. prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
  188. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  189. /* Create a shmem file to store the data in. This will permit the data
  190. * to be swapped out if needed.
  191. *
  192. * File content is stored encrypted with randomly generated key.
  193. */
  194. loff_t pos = 0;
  195. buf = big_key_alloc_buffer(enclen);
  196. if (!buf)
  197. return -ENOMEM;
  198. memcpy(buf->virt, prep->data, datalen);
  199. /* generate random key */
  200. enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
  201. if (!enckey) {
  202. ret = -ENOMEM;
  203. goto error;
  204. }
  205. ret = get_random_bytes_wait(enckey, ENC_KEY_SIZE);
  206. if (unlikely(ret))
  207. goto err_enckey;
  208. /* encrypt aligned data */
  209. ret = big_key_crypt(BIG_KEY_ENC, buf, datalen, enckey);
  210. if (ret)
  211. goto err_enckey;
  212. /* save aligned data to file */
  213. file = shmem_kernel_file_setup("", enclen, 0);
  214. if (IS_ERR(file)) {
  215. ret = PTR_ERR(file);
  216. goto err_enckey;
  217. }
  218. written = kernel_write(file, buf->virt, enclen, &pos);
  219. if (written != enclen) {
  220. ret = written;
  221. if (written >= 0)
  222. ret = -ENOMEM;
  223. goto err_fput;
  224. }
  225. /* Pin the mount and dentry to the key so that we can open it again
  226. * later
  227. */
  228. prep->payload.data[big_key_data] = enckey;
  229. *path = file->f_path;
  230. path_get(path);
  231. fput(file);
  232. big_key_free_buffer(buf);
  233. } else {
  234. /* Just store the data in a buffer */
  235. void *data = kmalloc(datalen, GFP_KERNEL);
  236. if (!data)
  237. return -ENOMEM;
  238. prep->payload.data[big_key_data] = data;
  239. memcpy(data, prep->data, prep->datalen);
  240. }
  241. return 0;
  242. err_fput:
  243. fput(file);
  244. err_enckey:
  245. kzfree(enckey);
  246. error:
  247. big_key_free_buffer(buf);
  248. return ret;
  249. }
  250. /*
  251. * Clear preparsement.
  252. */
  253. void big_key_free_preparse(struct key_preparsed_payload *prep)
  254. {
  255. if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
  256. struct path *path = (struct path *)&prep->payload.data[big_key_path];
  257. path_put(path);
  258. }
  259. kzfree(prep->payload.data[big_key_data]);
  260. }
  261. /*
  262. * dispose of the links from a revoked keyring
  263. * - called with the key sem write-locked
  264. */
  265. void big_key_revoke(struct key *key)
  266. {
  267. struct path *path = (struct path *)&key->payload.data[big_key_path];
  268. /* clear the quota */
  269. key_payload_reserve(key, 0);
  270. if (key_is_positive(key) &&
  271. (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
  272. vfs_truncate(path, 0);
  273. }
  274. /*
  275. * dispose of the data dangling from the corpse of a big_key key
  276. */
  277. void big_key_destroy(struct key *key)
  278. {
  279. size_t datalen = (size_t)key->payload.data[big_key_len];
  280. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  281. struct path *path = (struct path *)&key->payload.data[big_key_path];
  282. path_put(path);
  283. path->mnt = NULL;
  284. path->dentry = NULL;
  285. }
  286. kzfree(key->payload.data[big_key_data]);
  287. key->payload.data[big_key_data] = NULL;
  288. }
  289. /*
  290. * describe the big_key key
  291. */
  292. void big_key_describe(const struct key *key, struct seq_file *m)
  293. {
  294. size_t datalen = (size_t)key->payload.data[big_key_len];
  295. seq_puts(m, key->description);
  296. if (key_is_positive(key))
  297. seq_printf(m, ": %zu [%s]",
  298. datalen,
  299. datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
  300. }
  301. /*
  302. * read the key data
  303. * - the key's semaphore is read-locked
  304. */
  305. long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
  306. {
  307. size_t datalen = (size_t)key->payload.data[big_key_len];
  308. long ret;
  309. if (!buffer || buflen < datalen)
  310. return datalen;
  311. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  312. struct big_key_buf *buf;
  313. struct path *path = (struct path *)&key->payload.data[big_key_path];
  314. struct file *file;
  315. u8 *enckey = (u8 *)key->payload.data[big_key_data];
  316. size_t enclen = datalen + ENC_AUTHTAG_SIZE;
  317. loff_t pos = 0;
  318. buf = big_key_alloc_buffer(enclen);
  319. if (!buf)
  320. return -ENOMEM;
  321. file = dentry_open(path, O_RDONLY, current_cred());
  322. if (IS_ERR(file)) {
  323. ret = PTR_ERR(file);
  324. goto error;
  325. }
  326. /* read file to kernel and decrypt */
  327. ret = kernel_read(file, buf->virt, enclen, &pos);
  328. if (ret >= 0 && ret != enclen) {
  329. ret = -EIO;
  330. goto err_fput;
  331. }
  332. ret = big_key_crypt(BIG_KEY_DEC, buf, enclen, enckey);
  333. if (ret)
  334. goto err_fput;
  335. ret = datalen;
  336. /* copy decrypted data to user */
  337. if (copy_to_user(buffer, buf->virt, datalen) != 0)
  338. ret = -EFAULT;
  339. err_fput:
  340. fput(file);
  341. error:
  342. big_key_free_buffer(buf);
  343. } else {
  344. ret = datalen;
  345. if (copy_to_user(buffer, key->payload.data[big_key_data],
  346. datalen) != 0)
  347. ret = -EFAULT;
  348. }
  349. return ret;
  350. }
  351. /*
  352. * Register key type
  353. */
  354. static int __init big_key_init(void)
  355. {
  356. int ret;
  357. /* init block cipher */
  358. big_key_aead = crypto_alloc_aead(big_key_alg_name, 0, CRYPTO_ALG_ASYNC);
  359. if (IS_ERR(big_key_aead)) {
  360. ret = PTR_ERR(big_key_aead);
  361. pr_err("Can't alloc crypto: %d\n", ret);
  362. return ret;
  363. }
  364. ret = crypto_aead_setauthsize(big_key_aead, ENC_AUTHTAG_SIZE);
  365. if (ret < 0) {
  366. pr_err("Can't set crypto auth tag len: %d\n", ret);
  367. goto free_aead;
  368. }
  369. ret = register_key_type(&key_type_big_key);
  370. if (ret < 0) {
  371. pr_err("Can't register type: %d\n", ret);
  372. goto free_aead;
  373. }
  374. return 0;
  375. free_aead:
  376. crypto_free_aead(big_key_aead);
  377. return ret;
  378. }
  379. late_initcall(big_key_init);