big_key.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. .instantiate = big_key_instantiate,
  34. .match = user_match,
  35. .revoke = big_key_revoke,
  36. .destroy = big_key_destroy,
  37. .describe = big_key_describe,
  38. .read = big_key_read,
  39. };
  40. /*
  41. * Instantiate a big key
  42. */
  43. int big_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
  44. {
  45. struct path *path = (struct path *)&key->payload.data2;
  46. struct file *file;
  47. ssize_t written;
  48. size_t datalen = prep->datalen;
  49. int ret;
  50. ret = -EINVAL;
  51. if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
  52. goto error;
  53. /* Set an arbitrary quota */
  54. ret = key_payload_reserve(key, 16);
  55. if (ret < 0)
  56. goto error;
  57. key->type_data.x[1] = 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_file_setup("", datalen, 0);
  65. if (IS_ERR(file)) {
  66. ret = PTR_ERR(file);
  67. goto err_quota;
  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. ret = -ENOMEM;
  87. goto err_quota;
  88. }
  89. key->payload.data = memcpy(data, prep->data, prep->datalen);
  90. }
  91. return 0;
  92. err_fput:
  93. fput(file);
  94. err_quota:
  95. key_payload_reserve(key, 0);
  96. error:
  97. return ret;
  98. }
  99. /*
  100. * dispose of the links from a revoked keyring
  101. * - called with the key sem write-locked
  102. */
  103. void big_key_revoke(struct key *key)
  104. {
  105. struct path *path = (struct path *)&key->payload.data2;
  106. /* clear the quota */
  107. key_payload_reserve(key, 0);
  108. if (key_is_instantiated(key) && key->type_data.x[1] > BIG_KEY_FILE_THRESHOLD)
  109. vfs_truncate(path, 0);
  110. }
  111. /*
  112. * dispose of the data dangling from the corpse of a big_key key
  113. */
  114. void big_key_destroy(struct key *key)
  115. {
  116. if (key->type_data.x[1] > BIG_KEY_FILE_THRESHOLD) {
  117. struct path *path = (struct path *)&key->payload.data2;
  118. path_put(path);
  119. path->mnt = NULL;
  120. path->dentry = NULL;
  121. } else {
  122. kfree(key->payload.data);
  123. key->payload.data = NULL;
  124. }
  125. }
  126. /*
  127. * describe the big_key key
  128. */
  129. void big_key_describe(const struct key *key, struct seq_file *m)
  130. {
  131. unsigned long datalen = key->type_data.x[1];
  132. seq_puts(m, key->description);
  133. if (key_is_instantiated(key))
  134. seq_printf(m, ": %lu [%s]",
  135. datalen,
  136. datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
  137. }
  138. /*
  139. * read the key data
  140. * - the key's semaphore is read-locked
  141. */
  142. long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
  143. {
  144. unsigned long datalen = key->type_data.x[1];
  145. long ret;
  146. if (!buffer || buflen < datalen)
  147. return datalen;
  148. if (datalen > BIG_KEY_FILE_THRESHOLD) {
  149. struct path *path = (struct path *)&key->payload.data2;
  150. struct file *file;
  151. loff_t pos;
  152. file = dentry_open(path, O_RDONLY, current_cred());
  153. if (IS_ERR(file))
  154. return PTR_ERR(file);
  155. pos = 0;
  156. ret = vfs_read(file, buffer, datalen, &pos);
  157. fput(file);
  158. if (ret >= 0 && ret != datalen)
  159. ret = -EIO;
  160. } else {
  161. ret = datalen;
  162. if (copy_to_user(buffer, key->payload.data, datalen) != 0)
  163. ret = -EFAULT;
  164. }
  165. return ret;
  166. }
  167. /*
  168. * Module stuff
  169. */
  170. static int __init big_key_init(void)
  171. {
  172. return register_key_type(&key_type_big_key);
  173. }
  174. static void __exit big_key_cleanup(void)
  175. {
  176. unregister_key_type(&key_type_big_key);
  177. }
  178. module_init(big_key_init);
  179. module_exit(big_key_cleanup);