blob: f1ea610362c6c160a9d955c6a8e946da03b83ccb [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Michael Halcrow746f1e52008-07-23 21:30:02 -070026#include <linux/wait.h>
27#include <linux/mount.h>
28#include "ecryptfs_kernel.h"
29
Al Viro3b8b4872012-06-25 11:38:56 +040030struct ecryptfs_open_req {
31 struct file **lower_file;
Al Viro765927b2012-06-26 21:58:53 +040032 struct path path;
Al Viro3b8b4872012-06-25 11:38:56 +040033 struct completion done;
34 struct list_head kthread_ctl_list;
35};
Michael Halcrow746f1e52008-07-23 21:30:02 -070036
37static struct ecryptfs_kthread_ctl {
38#define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
39 u32 flags;
40 struct mutex mux;
41 struct list_head req_list;
42 wait_queue_head_t wait;
43} ecryptfs_kthread_ctl;
44
45static struct task_struct *ecryptfs_kthread;
46
47/**
48 * ecryptfs_threadfn
49 * @ignored: ignored
50 *
51 * The eCryptfs kernel thread that has the responsibility of getting
Tyler Hicks332ab162011-04-14 15:35:11 -050052 * the lower file with RW permissions.
Michael Halcrow746f1e52008-07-23 21:30:02 -070053 *
54 * Returns zero on success; non-zero otherwise
55 */
56static int ecryptfs_threadfn(void *ignored)
57{
58 set_freezable();
59 while (1) {
60 struct ecryptfs_open_req *req;
61
62 wait_event_freezable(
63 ecryptfs_kthread_ctl.wait,
64 (!list_empty(&ecryptfs_kthread_ctl.req_list)
65 || kthread_should_stop()));
66 mutex_lock(&ecryptfs_kthread_ctl.mux);
67 if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
68 mutex_unlock(&ecryptfs_kthread_ctl.mux);
69 goto out;
70 }
71 while (!list_empty(&ecryptfs_kthread_ctl.req_list)) {
72 req = list_first_entry(&ecryptfs_kthread_ctl.req_list,
73 struct ecryptfs_open_req,
74 kthread_ctl_list);
Michael Halcrow746f1e52008-07-23 21:30:02 -070075 list_del(&req->kthread_ctl_list);
Al Viro765927b2012-06-26 21:58:53 +040076 *req->lower_file = dentry_open(&req->path,
Al Viro3b8b4872012-06-25 11:38:56 +040077 (O_RDWR | O_LARGEFILE), current_cred());
78 complete(&req->done);
Michael Halcrow746f1e52008-07-23 21:30:02 -070079 }
80 mutex_unlock(&ecryptfs_kthread_ctl.mux);
81 }
82out:
83 return 0;
84}
85
Jerome Marchand7371a382010-08-17 17:24:05 +020086int __init ecryptfs_init_kthread(void)
Michael Halcrow746f1e52008-07-23 21:30:02 -070087{
88 int rc = 0;
89
90 mutex_init(&ecryptfs_kthread_ctl.mux);
91 init_waitqueue_head(&ecryptfs_kthread_ctl.wait);
92 INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list);
93 ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL,
94 "ecryptfs-kthread");
95 if (IS_ERR(ecryptfs_kthread)) {
96 rc = PTR_ERR(ecryptfs_kthread);
97 printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]"
98 "\n", __func__, rc);
99 }
100 return rc;
101}
102
103void ecryptfs_destroy_kthread(void)
104{
Wei Yongjun8bbca572012-08-21 10:46:05 +0800105 struct ecryptfs_open_req *req, *tmp;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700106
107 mutex_lock(&ecryptfs_kthread_ctl.mux);
108 ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE;
Wei Yongjun8bbca572012-08-21 10:46:05 +0800109 list_for_each_entry_safe(req, tmp, &ecryptfs_kthread_ctl.req_list,
110 kthread_ctl_list) {
Al Viro3b8b4872012-06-25 11:38:56 +0400111 list_del(&req->kthread_ctl_list);
112 *req->lower_file = ERR_PTR(-EIO);
113 complete(&req->done);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700114 }
115 mutex_unlock(&ecryptfs_kthread_ctl.mux);
116 kthread_stop(ecryptfs_kthread);
117 wake_up(&ecryptfs_kthread_ctl.wait);
118}
119
120/**
121 * ecryptfs_privileged_open
122 * @lower_file: Result of dentry_open by root on lower dentry
123 * @lower_dentry: Lower dentry for file to open
124 * @lower_mnt: Lower vfsmount for file to open
125 *
126 * This function gets a r/w file opened againt the lower dentry.
127 *
128 * Returns zero on success; non-zero otherwise
129 */
130int ecryptfs_privileged_open(struct file **lower_file,
131 struct dentry *lower_dentry,
David Howells745ca242008-11-14 10:39:22 +1100132 struct vfsmount *lower_mnt,
133 const struct cred *cred)
Michael Halcrow746f1e52008-07-23 21:30:02 -0700134{
Al Viro3b8b4872012-06-25 11:38:56 +0400135 struct ecryptfs_open_req req;
Tyler Hicksac22ba22009-08-12 01:06:54 -0500136 int flags = O_LARGEFILE;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700137 int rc = 0;
138
Al Viro765927b2012-06-26 21:58:53 +0400139 init_completion(&req.done);
140 req.lower_file = lower_file;
141 req.path.dentry = lower_dentry;
142 req.path.mnt = lower_mnt;
143
Michael Halcrow746f1e52008-07-23 21:30:02 -0700144 /* Corresponding dput() and mntput() are done when the
Tyler Hicks332ab162011-04-14 15:35:11 -0500145 * lower file is fput() when all eCryptfs files for the inode are
146 * released. */
Tyler Hicksac22ba22009-08-12 01:06:54 -0500147 flags |= IS_RDONLY(lower_dentry->d_inode) ? O_RDONLY : O_RDWR;
Al Viro765927b2012-06-26 21:58:53 +0400148 (*lower_file) = dentry_open(&req.path, flags, cred);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700149 if (!IS_ERR(*lower_file))
150 goto out;
Tyler Hicks9fe79d72012-06-12 11:17:01 -0700151 if ((flags & O_ACCMODE) == O_RDONLY) {
Tyler Hicksac22ba22009-08-12 01:06:54 -0500152 rc = PTR_ERR((*lower_file));
153 goto out;
154 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700155 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__);
Al Viro3b8b4872012-06-25 11:38:56 +0400162 goto out;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700163 }
Al Viro3b8b4872012-06-25 11:38:56 +0400164 list_add_tail(&req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700165 mutex_unlock(&ecryptfs_kthread_ctl.mux);
166 wake_up(&ecryptfs_kthread_ctl.wait);
Al Viro3b8b4872012-06-25 11:38:56 +0400167 wait_for_completion(&req.done);
168 if (IS_ERR(*lower_file))
169 rc = PTR_ERR(*lower_file);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700170out:
171 return rc;
172}