2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 2008 International Business Machines Corp.
5 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
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.
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.
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
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"
29 struct kmem_cache
*ecryptfs_open_req_cache
;
31 static struct ecryptfs_kthread_ctl
{
32 #define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
35 struct list_head req_list
;
36 wait_queue_head_t wait
;
37 } ecryptfs_kthread_ctl
;
39 static struct task_struct
*ecryptfs_kthread
;
45 * The eCryptfs kernel thread that has the responsibility of getting
46 * the lower persistent file with RW permissions.
48 * Returns zero on success; non-zero otherwise
50 static int ecryptfs_threadfn(void *ignored
)
54 struct ecryptfs_open_req
*req
;
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
);
65 while (!list_empty(&ecryptfs_kthread_ctl
.req_list
)) {
66 req
= list_first_entry(&ecryptfs_kthread_ctl
.req_list
,
67 struct ecryptfs_open_req
,
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
,
76 (O_RDWR
| O_LARGEFILE
), current_cred());
77 req
->flags
|= ECRYPTFS_REQ_PROCESSED
;
80 mutex_unlock(&req
->mux
);
82 mutex_unlock(&ecryptfs_kthread_ctl
.mux
);
88 int ecryptfs_init_kthread(void)
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
,
97 if (IS_ERR(ecryptfs_kthread
)) {
98 rc
= PTR_ERR(ecryptfs_kthread
);
99 printk(KERN_ERR
"%s: Failed to create kernel thread; rc = [%d]"
105 void ecryptfs_destroy_kthread(void)
107 struct ecryptfs_open_req
*req
;
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
,
113 mutex_lock(&req
->mux
);
114 req
->flags
|= ECRYPTFS_REQ_ZOMBIE
;
116 mutex_unlock(&req
->mux
);
118 mutex_unlock(&ecryptfs_kthread_ctl
.mux
);
119 kthread_stop(ecryptfs_kthread
);
120 wake_up(&ecryptfs_kthread_ctl
.wait
);
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
129 * This function gets a r/w file opened againt the lower dentry.
131 * Returns zero on success; non-zero otherwise
133 int ecryptfs_privileged_open(struct file
**lower_file
,
134 struct dentry
*lower_dentry
,
135 struct vfsmount
*lower_mnt
,
136 const struct cred
*cred
)
138 struct ecryptfs_open_req
*req
;
141 /* Corresponding dput() and mntput() are done when the
142 * persistent file is fput() when the eCryptfs inode is
146 (*lower_file
) = dentry_open(lower_dentry
, lower_mnt
,
147 (O_RDWR
| O_LARGEFILE
), cred
);
148 if (!IS_ERR(*lower_file
))
150 req
= kmem_cache_alloc(ecryptfs_open_req_cache
, GFP_KERNEL
);
155 mutex_init(&req
->mux
);
156 req
->lower_file
= lower_file
;
157 req
->lower_dentry
= lower_dentry
;
158 req
->lower_mnt
= lower_mnt
;
159 init_waitqueue_head(&req
->wait
);
161 mutex_lock(&ecryptfs_kthread_ctl
.mux
);
162 if (ecryptfs_kthread_ctl
.flags
& ECRYPTFS_KTHREAD_ZOMBIE
) {
164 mutex_unlock(&ecryptfs_kthread_ctl
.mux
);
165 printk(KERN_ERR
"%s: We are in the middle of shutting down; "
166 "aborting privileged request to open lower file\n",
170 list_add_tail(&req
->kthread_ctl_list
, &ecryptfs_kthread_ctl
.req_list
);
171 mutex_unlock(&ecryptfs_kthread_ctl
.mux
);
172 wake_up(&ecryptfs_kthread_ctl
.wait
);
173 wait_event(req
->wait
, (req
->flags
!= 0));
174 mutex_lock(&req
->mux
);
175 BUG_ON(req
->flags
== 0);
176 if (req
->flags
& ECRYPTFS_REQ_DROPPED
177 || req
->flags
& ECRYPTFS_REQ_ZOMBIE
) {
179 printk(KERN_WARNING
"%s: Privileged open request dropped\n",
183 if (IS_ERR(*req
->lower_file
)) {
184 rc
= PTR_ERR(*req
->lower_file
);
187 (*lower_file
) = dentry_open(lower_dentry
, lower_mnt
,
188 (O_RDONLY
| O_LARGEFILE
), cred
);
189 if (IS_ERR(*lower_file
)) {
190 rc
= PTR_ERR(*req
->lower_file
);
191 (*lower_file
) = NULL
;
192 printk(KERN_WARNING
"%s: Error attempting privileged "
193 "open of lower file with either RW or RO "
194 "perms; rc = [%d]. Giving up.\n",
199 mutex_unlock(&req
->mux
);
201 kmem_cache_free(ecryptfs_open_req_cache
, req
);
This page took 0.033381 seconds and 5 git commands to generate.