big_key.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Large capacity key type
  2. *
  3. * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/file.h>
  15. #include <linux/shmem_fs.h>
  16. #include <linux/err.h>
  17. #include <keys/user-type.h>
  18. #include <keys/big_key-type.h>
  19. MODULE_LICENSE("GPL");
  20. /*
  21. * If the data is under this limit, there's no point creating a shm file to
  22. * hold it as the permanently resident metadata for the shmem fs will be at
  23. * least as large as the data.
  24. */
  25. #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
  26. /*
  27. * big_key defined keys take an arbitrary string as the description and an
  28. * arbitrary blob of data as the payload
  29. */
  30. struct key_type key_type_big_key = {
  31. .name = "big_key",
  32. .def_lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  33. .preparse = big_key_preparse,
  34. .free_preparse = big_key_free_preparse,
  35. .instantiate = generic_key_instantiate,
  36. .match = user_match,
  37. .revoke = big_key_revoke,
  38. .destroy = big_key_destroy,
  39. .describe = big_key_describe,
  40. .read = big_key_read,
  41. };
  42. /*
  43. * Preparse a big key
  44. */
  45. int big_key_preparse(struct key_preparsed_payload *prep)
  46. {
  47. struct path *path = (struct path *)&prep->payload;
  48. struct file *file;
  49. ssize_t written;
  50. size_t datalen = prep->datalen;
  51. int ret;
  52. ret = -EINVAL;
  53. if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
  54. goto error;
  55. /* Set an arbitrary quota */
  56. prep->quotalen = 16;
  57. prep->type_data[1] = (void *)(unsigned long)datalen;
  58. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  59. /* Create a shmem file to store the data in. This will permit the data
  60. * to be swapped out if needed.
  61. *
  62. * TODO: Encrypt the stored data with a temporary key.
  63. */
  64. file = shmem_kernel_file_setup("", datalen, 0);
  65. if (IS_ERR(file)) {
  66. ret = PTR_ERR(file);
  67. goto error;
  68. }
  69. written = kernel_write(file, prep->data, prep->datalen, 0);
  70. if (written != datalen) {
  71. ret = written;
  72. if (written >= 0)
  73. ret = -ENOMEM;
  74. goto err_fput;
  75. }
  76. /* Pin the mount and dentry to the key so that we can open it again
  77. * later
  78. */
  79. *path = file->f_path;
  80. path_get(path);
  81. fput(file);
  82. } else {
  83. /* Just store the data in a buffer */
  84. void *data = kmalloc(datalen, GFP_KERNEL);
  85. if (!data)
  86. return -ENOMEM;
  87. prep->payload[0] = memcpy(data, prep->data, prep->datalen);
  88. }
  89. return 0;
  90. err_fput:
  91. fput(file);
  92. error:
  93. return ret;
  94. }
  95. /*
  96. * Clear preparsement.
  97. */
  98. void big_key_free_preparse(struct key_preparsed_payload *prep)
  99. {
  100. if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
  101. struct path *path = (struct path *)&prep->payload;
  102. path_put(path);
  103. } else {
  104. kfree(prep->payload[0]);
  105. }
  106. }
  107. /*
  108. * dispose of the links from a revoked keyring
  109. * - called with the key sem write-locked
  110. */
  111. void big_key_revoke(struct key *key)
  112. {
  113. struct path *path = (struct path *)&key->payload.data2;
  114. /* clear the quota */
  115. key_payload_reserve(key, 0);
  116. if (key_is_instantiated(key) && key->type_data.x[1] > BIG_KEY_FILE_THRESHOLD)
  117. vfs_truncate(path, 0);
  118. }
  119. /*
  120. * dispose of the data dangling from the corpse of a big_key key
  121. */
  122. void big_key_destroy(struct key *key)
  123. {
  124. if (key->type_data.x[1] > BIG_KEY_FILE_THRESHOLD) {
  125. struct path *path = (struct path *)&key->payload.data2;
  126. path_put(path);
  127. path->mnt = NULL;
  128. path->dentry = NULL;
  129. } else {
  130. kfree(key->payload.data);
  131. key->payload.data = NULL;
  132. }
  133. }
  134. /*
  135. * describe the big_key key
  136. */
  137. void big_key_describe(const struct key *key, struct seq_file *m)
  138. {
  139. unsigned long datalen = key->type_data.x[1];
  140. seq_puts(m, key->description);
  141. if (key_is_instantiated(key))
  142. seq_printf(m, ": %lu [%s]",
  143. datalen,
  144. datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
  145. }
  146. /*
  147. * read the key data
  148. * - the key's semaphore is read-locked
  149. */
  150. long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
  151. {
  152. unsigned long datalen = key->type_data.x[1];
  153. long ret;
  154. if (!buffer || buflen < datalen)
  155. return datalen;
  156. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  157. struct path *path = (struct path *)&key->payload.data2;
  158. struct file *file;
  159. loff_t pos;
  160. file = dentry_open(path, O_RDONLY, current_cred());
  161. if (IS_ERR(file))
  162. return PTR_ERR(file);
  163. pos = 0;
  164. ret = vfs_read(file, buffer, datalen, &pos);
  165. fput(file);
  166. if (ret >= 0 && ret != datalen)
  167. ret = -EIO;
  168. } else {
  169. ret = datalen;
  170. if (copy_to_user(buffer, key->payload.data, datalen) != 0)
  171. ret = -EFAULT;
  172. }
  173. return ret;
  174. }
  175. /*
  176. * Module stuff
  177. */
  178. static int __init big_key_init(void)
  179. {
  180. return register_key_type(&key_type_big_key);
  181. }
  182. static void __exit big_key_cleanup(void)
  183. {
  184. unregister_key_type(&key_type_big_key);
  185. }
  186. module_init(big_key_init);
  187. module_exit(big_key_cleanup);