big_key.c 10.0 KB

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