kthread.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2008 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.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 License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <linux/kthread.h>
  23. #include <linux/freezer.h>
  24. #include <linux/wait.h>
  25. #include <linux/mount.h>
  26. #include "ecryptfs_kernel.h"
  27. struct kmem_cache *ecryptfs_open_req_cache;
  28. static struct ecryptfs_kthread_ctl {
  29. #define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
  30. u32 flags;
  31. struct mutex mux;
  32. struct list_head req_list;
  33. wait_queue_head_t wait;
  34. } ecryptfs_kthread_ctl;
  35. static struct task_struct *ecryptfs_kthread;
  36. /**
  37. * ecryptfs_threadfn
  38. * @ignored: ignored
  39. *
  40. * The eCryptfs kernel thread that has the responsibility of getting
  41. * the lower persistent file with RW permissions.
  42. *
  43. * Returns zero on success; non-zero otherwise
  44. */
  45. static int ecryptfs_threadfn(void *ignored)
  46. {
  47. set_freezable();
  48. while (1) {
  49. struct ecryptfs_open_req *req;
  50. wait_event_freezable(
  51. ecryptfs_kthread_ctl.wait,
  52. (!list_empty(&ecryptfs_kthread_ctl.req_list)
  53. || kthread_should_stop()));
  54. mutex_lock(&ecryptfs_kthread_ctl.mux);
  55. if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
  56. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  57. goto out;
  58. }
  59. while (!list_empty(&ecryptfs_kthread_ctl.req_list)) {
  60. req = list_first_entry(&ecryptfs_kthread_ctl.req_list,
  61. struct ecryptfs_open_req,
  62. kthread_ctl_list);
  63. mutex_lock(&req->mux);
  64. list_del(&req->kthread_ctl_list);
  65. if (!(req->flags & ECRYPTFS_REQ_ZOMBIE)) {
  66. dget(req->lower_dentry);
  67. mntget(req->lower_mnt);
  68. (*req->lower_file) = dentry_open(
  69. req->lower_dentry, req->lower_mnt,
  70. (O_RDWR | O_LARGEFILE), current_cred());
  71. req->flags |= ECRYPTFS_REQ_PROCESSED;
  72. }
  73. wake_up(&req->wait);
  74. mutex_unlock(&req->mux);
  75. }
  76. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  77. }
  78. out:
  79. return 0;
  80. }
  81. int ecryptfs_init_kthread(void)
  82. {
  83. int rc = 0;
  84. mutex_init(&ecryptfs_kthread_ctl.mux);
  85. init_waitqueue_head(&ecryptfs_kthread_ctl.wait);
  86. INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list);
  87. ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL,
  88. "ecryptfs-kthread");
  89. if (IS_ERR(ecryptfs_kthread)) {
  90. rc = PTR_ERR(ecryptfs_kthread);
  91. printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]"
  92. "\n", __func__, rc);
  93. }
  94. return rc;
  95. }
  96. void ecryptfs_destroy_kthread(void)
  97. {
  98. struct ecryptfs_open_req *req;
  99. mutex_lock(&ecryptfs_kthread_ctl.mux);
  100. ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE;
  101. list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list,
  102. kthread_ctl_list) {
  103. mutex_lock(&req->mux);
  104. req->flags |= ECRYPTFS_REQ_ZOMBIE;
  105. wake_up(&req->wait);
  106. mutex_unlock(&req->mux);
  107. }
  108. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  109. kthread_stop(ecryptfs_kthread);
  110. wake_up(&ecryptfs_kthread_ctl.wait);
  111. }
  112. /**
  113. * ecryptfs_privileged_open
  114. * @lower_file: Result of dentry_open by root on lower dentry
  115. * @lower_dentry: Lower dentry for file to open
  116. * @lower_mnt: Lower vfsmount for file to open
  117. *
  118. * This function gets a r/w file opened againt the lower dentry.
  119. *
  120. * Returns zero on success; non-zero otherwise
  121. */
  122. int ecryptfs_privileged_open(struct file **lower_file,
  123. struct dentry *lower_dentry,
  124. struct vfsmount *lower_mnt,
  125. const struct cred *cred)
  126. {
  127. struct ecryptfs_open_req *req;
  128. int rc = 0;
  129. /* Corresponding dput() and mntput() are done when the
  130. * persistent file is fput() when the eCryptfs inode is
  131. * destroyed. */
  132. dget(lower_dentry);
  133. mntget(lower_mnt);
  134. (*lower_file) = dentry_open(lower_dentry, lower_mnt,
  135. (O_RDWR | O_LARGEFILE), cred);
  136. if (!IS_ERR(*lower_file))
  137. goto out;
  138. req = kmem_cache_alloc(ecryptfs_open_req_cache, GFP_KERNEL);
  139. if (!req) {
  140. rc = -ENOMEM;
  141. goto out;
  142. }
  143. mutex_init(&req->mux);
  144. req->lower_file = lower_file;
  145. req->lower_dentry = lower_dentry;
  146. req->lower_mnt = lower_mnt;
  147. init_waitqueue_head(&req->wait);
  148. req->flags = 0;
  149. mutex_lock(&ecryptfs_kthread_ctl.mux);
  150. if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
  151. rc = -EIO;
  152. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  153. printk(KERN_ERR "%s: We are in the middle of shutting down; "
  154. "aborting privileged request to open lower file\n",
  155. __func__);
  156. goto out_free;
  157. }
  158. list_add_tail(&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list);
  159. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  160. wake_up(&ecryptfs_kthread_ctl.wait);
  161. wait_event(req->wait, (req->flags != 0));
  162. mutex_lock(&req->mux);
  163. BUG_ON(req->flags == 0);
  164. if (req->flags & ECRYPTFS_REQ_DROPPED
  165. || req->flags & ECRYPTFS_REQ_ZOMBIE) {
  166. rc = -EIO;
  167. printk(KERN_WARNING "%s: Privileged open request dropped\n",
  168. __func__);
  169. goto out_unlock;
  170. }
  171. if (IS_ERR(*req->lower_file)) {
  172. rc = PTR_ERR(*req->lower_file);
  173. dget(lower_dentry);
  174. mntget(lower_mnt);
  175. (*lower_file) = dentry_open(lower_dentry, lower_mnt,
  176. (O_RDONLY | O_LARGEFILE), cred);
  177. if (IS_ERR(*lower_file)) {
  178. rc = PTR_ERR(*req->lower_file);
  179. (*lower_file) = NULL;
  180. printk(KERN_WARNING "%s: Error attempting privileged "
  181. "open of lower file with either RW or RO "
  182. "perms; rc = [%d]. Giving up.\n",
  183. __func__, rc);
  184. }
  185. }
  186. out_unlock:
  187. mutex_unlock(&req->mux);
  188. out_free:
  189. kmem_cache_free(ecryptfs_open_req_cache, req);
  190. out:
  191. return rc;
  192. }