big_key.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. * Layout of key payload words.
  22. */
  23. enum {
  24. big_key_data,
  25. big_key_path,
  26. big_key_path_2nd_part,
  27. big_key_len,
  28. };
  29. /*
  30. * If the data is under this limit, there's no point creating a shm file to
  31. * hold it as the permanently resident metadata for the shmem fs will be at
  32. * least as large as the data.
  33. */
  34. #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
  35. /*
  36. * big_key defined keys take an arbitrary string as the description and an
  37. * arbitrary blob of data as the payload
  38. */
  39. struct key_type key_type_big_key = {
  40. .name = "big_key",
  41. .preparse = big_key_preparse,
  42. .free_preparse = big_key_free_preparse,
  43. .instantiate = generic_key_instantiate,
  44. .revoke = big_key_revoke,
  45. .destroy = big_key_destroy,
  46. .describe = big_key_describe,
  47. .read = big_key_read,
  48. };
  49. /*
  50. * Preparse a big key
  51. */
  52. int big_key_preparse(struct key_preparsed_payload *prep)
  53. {
  54. struct path *path = (struct path *)&prep->payload.data[big_key_path];
  55. struct file *file;
  56. ssize_t written;
  57. size_t datalen = prep->datalen;
  58. int ret;
  59. ret = -EINVAL;
  60. if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
  61. goto error;
  62. /* Set an arbitrary quota */
  63. prep->quotalen = 16;
  64. prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
  65. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  66. /* Create a shmem file to store the data in. This will permit the data
  67. * to be swapped out if needed.
  68. *
  69. * TODO: Encrypt the stored data with a temporary key.
  70. */
  71. file = shmem_kernel_file_setup("", datalen, 0);
  72. if (IS_ERR(file)) {
  73. ret = PTR_ERR(file);
  74. goto error;
  75. }
  76. written = kernel_write(file, prep->data, prep->datalen, 0);
  77. if (written != datalen) {
  78. ret = written;
  79. if (written >= 0)
  80. ret = -ENOMEM;
  81. goto err_fput;
  82. }
  83. /* Pin the mount and dentry to the key so that we can open it again
  84. * later
  85. */
  86. *path = file->f_path;
  87. path_get(path);
  88. fput(file);
  89. } else {
  90. /* Just store the data in a buffer */
  91. void *data = kmalloc(datalen, GFP_KERNEL);
  92. if (!data)
  93. return -ENOMEM;
  94. prep->payload.data[big_key_data] = data;
  95. memcpy(data, prep->data, prep->datalen);
  96. }
  97. return 0;
  98. err_fput:
  99. fput(file);
  100. error:
  101. return ret;
  102. }
  103. /*
  104. * Clear preparsement.
  105. */
  106. void big_key_free_preparse(struct key_preparsed_payload *prep)
  107. {
  108. if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
  109. struct path *path = (struct path *)&prep->payload.data[big_key_path];
  110. path_put(path);
  111. } else {
  112. kfree(prep->payload.data[big_key_data]);
  113. }
  114. }
  115. /*
  116. * dispose of the links from a revoked keyring
  117. * - called with the key sem write-locked
  118. */
  119. void big_key_revoke(struct key *key)
  120. {
  121. struct path *path = (struct path *)&key->payload.data[big_key_path];
  122. /* clear the quota */
  123. key_payload_reserve(key, 0);
  124. if (key_is_positive(key) &&
  125. (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
  126. vfs_truncate(path, 0);
  127. }
  128. /*
  129. * dispose of the data dangling from the corpse of a big_key key
  130. */
  131. void big_key_destroy(struct key *key)
  132. {
  133. size_t datalen = (size_t)key->payload.data[big_key_len];
  134. if (datalen) {
  135. struct path *path = (struct path *)&key->payload.data[big_key_path];
  136. path_put(path);
  137. path->mnt = NULL;
  138. path->dentry = NULL;
  139. } else {
  140. kfree(key->payload.data[big_key_data]);
  141. key->payload.data[big_key_data] = NULL;
  142. }
  143. }
  144. /*
  145. * describe the big_key key
  146. */
  147. void big_key_describe(const struct key *key, struct seq_file *m)
  148. {
  149. size_t datalen = (size_t)key->payload.data[big_key_len];
  150. seq_puts(m, key->description);
  151. if (key_is_positive(key))
  152. seq_printf(m, ": %zu [%s]",
  153. datalen,
  154. datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
  155. }
  156. /*
  157. * read the key data
  158. * - the key's semaphore is read-locked
  159. */
  160. long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
  161. {
  162. size_t datalen = (size_t)key->payload.data[big_key_len];
  163. long ret;
  164. if (!buffer || buflen < datalen)
  165. return datalen;
  166. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  167. struct path *path = (struct path *)&key->payload.data[big_key_path];
  168. struct file *file;
  169. loff_t pos;
  170. file = dentry_open(path, O_RDONLY, current_cred());
  171. if (IS_ERR(file))
  172. return PTR_ERR(file);
  173. pos = 0;
  174. ret = vfs_read(file, buffer, datalen, &pos);
  175. fput(file);
  176. if (ret >= 0 && ret != datalen)
  177. ret = -EIO;
  178. } else {
  179. ret = datalen;
  180. if (copy_to_user(buffer, key->payload.data[big_key_data],
  181. datalen) != 0)
  182. ret = -EFAULT;
  183. }
  184. return ret;
  185. }
  186. /*
  187. * Module stuff
  188. */
  189. static int __init big_key_init(void)
  190. {
  191. return register_key_type(&key_type_big_key);
  192. }
  193. static void __exit big_key_cleanup(void)
  194. {
  195. unregister_key_type(&key_type_big_key);
  196. }
  197. module_init(big_key_init);
  198. module_exit(big_key_cleanup);