kthread.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/slab.h>
  25. #include <linux/wait.h>
  26. #include <linux/mount.h>
  27. #include "ecryptfs_kernel.h"
  28. struct kmem_cache *ecryptfs_open_req_cache;
  29. static struct ecryptfs_kthread_ctl {
  30. #define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
  31. u32 flags;
  32. struct mutex mux;
  33. struct list_head req_list;
  34. wait_queue_head_t wait;
  35. } ecryptfs_kthread_ctl;
  36. static struct task_struct *ecryptfs_kthread;
  37. /**
  38. * ecryptfs_threadfn
  39. * @ignored: ignored
  40. *
  41. * The eCryptfs kernel thread that has the responsibility of getting
  42. * the lower file with RW permissions.
  43. *
  44. * Returns zero on success; non-zero otherwise
  45. */
  46. static int ecryptfs_threadfn(void *ignored)
  47. {
  48. set_freezable();
  49. while (1) {
  50. struct ecryptfs_open_req *req;
  51. wait_event_freezable(
  52. ecryptfs_kthread_ctl.wait,
  53. (!list_empty(&ecryptfs_kthread_ctl.req_list)
  54. || kthread_should_stop()));
  55. mutex_lock(&ecryptfs_kthread_ctl.mux);
  56. if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
  57. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  58. goto out;
  59. }
  60. while (!list_empty(&ecryptfs_kthread_ctl.req_list)) {
  61. req = list_first_entry(&ecryptfs_kthread_ctl.req_list,
  62. struct ecryptfs_open_req,
  63. kthread_ctl_list);
  64. mutex_lock(&req->mux);
  65. list_del(&req->kthread_ctl_list);
  66. if (!(req->flags & ECRYPTFS_REQ_ZOMBIE)) {
  67. dget(req->lower_dentry);
  68. mntget(req->lower_mnt);
  69. (*req->lower_file) = dentry_open(
  70. req->lower_dentry, req->lower_mnt,
  71. (O_RDWR | O_LARGEFILE), current_cred());
  72. req->flags |= ECRYPTFS_REQ_PROCESSED;
  73. }
  74. wake_up(&req->wait);
  75. mutex_unlock(&req->mux);
  76. }
  77. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  78. }
  79. out:
  80. return 0;
  81. }
  82. int __init ecryptfs_init_kthread(void)
  83. {
  84. int rc = 0;
  85. mutex_init(&ecryptfs_kthread_ctl.mux);
  86. init_waitqueue_head(&ecryptfs_kthread_ctl.wait);
  87. INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list);
  88. ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL,
  89. "ecryptfs-kthread");
  90. if (IS_ERR(ecryptfs_kthread)) {
  91. rc = PTR_ERR(ecryptfs_kthread);
  92. printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]"
  93. "\n", __func__, rc);
  94. }
  95. return rc;
  96. }
  97. void ecryptfs_destroy_kthread(void)
  98. {
  99. struct ecryptfs_open_req *req;
  100. mutex_lock(&ecryptfs_kthread_ctl.mux);
  101. ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE;
  102. list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list,
  103. kthread_ctl_list) {
  104. mutex_lock(&req->mux);
  105. req->flags |= ECRYPTFS_REQ_ZOMBIE;
  106. wake_up(&req->wait);
  107. mutex_unlock(&req->mux);
  108. }
  109. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  110. kthread_stop(ecryptfs_kthread);
  111. wake_up(&ecryptfs_kthread_ctl.wait);
  112. }
  113. /**
  114. * ecryptfs_privileged_open
  115. * @lower_file: Result of dentry_open by root on lower dentry
  116. * @lower_dentry: Lower dentry for file to open
  117. * @lower_mnt: Lower vfsmount for file to open
  118. *
  119. * This function gets a r/w file opened againt the lower dentry.
  120. *
  121. * Returns zero on success; non-zero otherwise
  122. */
  123. int ecryptfs_privileged_open(struct file **lower_file,
  124. struct dentry *lower_dentry,
  125. struct vfsmount *lower_mnt,
  126. const struct cred *cred)
  127. {
  128. struct ecryptfs_open_req *req;
  129. int flags = O_LARGEFILE;
  130. int rc = 0;
  131. /* Corresponding dput() and mntput() are done when the
  132. * lower file is fput() when all eCryptfs files for the inode are
  133. * released. */
  134. dget(lower_dentry);
  135. mntget(lower_mnt);
  136. flags |= IS_RDONLY(lower_dentry->d_inode) ? O_RDONLY : O_RDWR;
  137. (*lower_file) = dentry_open(lower_dentry, lower_mnt, flags, cred);
  138. if (!IS_ERR(*lower_file))
  139. goto out;
  140. if ((flags & O_ACCMODE) == O_RDONLY) {
  141. rc = PTR_ERR((*lower_file));
  142. goto out;
  143. }
  144. req = kmem_cache_alloc(ecryptfs_open_req_cache, GFP_KERNEL);
  145. if (!req) {
  146. rc = -ENOMEM;
  147. goto out;
  148. }
  149. mutex_init(&req->mux);
  150. req->lower_file = lower_file;
  151. req->lower_dentry = lower_dentry;
  152. req->lower_mnt = lower_mnt;
  153. init_waitqueue_head(&req->wait);
  154. req->flags = 0;
  155. mutex_lock(&ecryptfs_kthread_ctl.mux);
  156. if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
  157. rc = -EIO;
  158. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  159. printk(KERN_ERR "%s: We are in the middle of shutting down; "
  160. "aborting privileged request to open lower file\n",
  161. __func__);
  162. goto out_free;
  163. }
  164. list_add_tail(&req->kthread_ctl_list, &ecryptfs_kthread_ctl.req_list);
  165. mutex_unlock(&ecryptfs_kthread_ctl.mux);
  166. wake_up(&ecryptfs_kthread_ctl.wait);
  167. wait_event(req->wait, (req->flags != 0));
  168. mutex_lock(&req->mux);
  169. BUG_ON(req->flags == 0);
  170. if (req->flags & ECRYPTFS_REQ_DROPPED
  171. || req->flags & ECRYPTFS_REQ_ZOMBIE) {
  172. rc = -EIO;
  173. printk(KERN_WARNING "%s: Privileged open request dropped\n",
  174. __func__);
  175. goto out_unlock;
  176. }
  177. if (IS_ERR(*req->lower_file))
  178. rc = PTR_ERR(*req->lower_file);
  179. out_unlock:
  180. mutex_unlock(&req->mux);
  181. out_free:
  182. kmem_cache_free(ecryptfs_open_req_cache, req);
  183. out:
  184. return rc;
  185. }