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