big_key.c 10 KB

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