blob: e818f5ac7a2692b6bb5c1a132d7bbfd967ad956a [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>
Jann Horn2f36db72016-06-01 11:55:06 +020028#include <linux/file.h>
Michael Halcrow746f1e52008-07-23 21:30:02 -070029#include "ecryptfs_kernel.h"
30
Al Viro3b8b4872012-06-25 11:38:56 +040031struct ecryptfs_open_req {
32 struct file **lower_file;
Al Viro765927b2012-06-26 21:58:53 +040033 struct path path;
Al Viro3b8b4872012-06-25 11:38:56 +040034 struct completion done;
35 struct list_head kthread_ctl_list;
36};
Michael Halcrow746f1e52008-07-23 21:30:02 -070037
38static struct ecryptfs_kthread_ctl {
39#define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
40 u32 flags;
41 struct mutex mux;
42 struct list_head req_list;
43 wait_queue_head_t wait;
44} ecryptfs_kthread_ctl;
45
46static struct task_struct *ecryptfs_kthread;
47
48/**
49 * ecryptfs_threadfn
50 * @ignored: ignored
51 *
52 * The eCryptfs kernel thread that has the responsibility of getting
Tyler Hicks332ab162011-04-14 15:35:11 -050053 * the lower file with RW permissions.
Michael Halcrow746f1e52008-07-23 21:30:02 -070054 *
55 * Returns zero on success; non-zero otherwise
56 */
57static int ecryptfs_threadfn(void *ignored)
58{
59 set_freezable();
60 while (1) {
61 struct ecryptfs_open_req *req;
62
63 wait_event_freezable(
64 ecryptfs_kthread_ctl.wait,
65 (!list_empty(&ecryptfs_kthread_ctl.req_list)
66 || kthread_should_stop()));
67 mutex_lock(&ecryptfs_kthread_ctl.mux);
68 if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
69 mutex_unlock(&ecryptfs_kthread_ctl.mux);
70 goto out;
71 }
72 while (!list_empty(&ecryptfs_kthread_ctl.req_list)) {
73 req = list_first_entry(&ecryptfs_kthread_ctl.req_list,
74 struct ecryptfs_open_req,
75 kthread_ctl_list);
Michael Halcrow746f1e52008-07-23 21:30:02 -070076 list_del(&req->kthread_ctl_list);
Al Viro765927b2012-06-26 21:58:53 +040077 *req->lower_file = dentry_open(&req->path,
Al Viro3b8b4872012-06-25 11:38:56 +040078 (O_RDWR | O_LARGEFILE), current_cred());
79 complete(&req->done);
Michael Halcrow746f1e52008-07-23 21:30:02 -070080 }
81 mutex_unlock(&ecryptfs_kthread_ctl.mux);
82 }
83out:
84 return 0;
85}
86
Jerome Marchand7371a382010-08-17 17:24:05 +020087int __init ecryptfs_init_kthread(void)
Michael Halcrow746f1e52008-07-23 21:30:02 -070088{
89 int rc = 0;
90
91 mutex_init(&ecryptfs_kthread_ctl.mux);
92 init_waitqueue_head(&ecryptfs_kthread_ctl.wait);
93 INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list);
94 ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL,
95 "ecryptfs-kthread");
96 if (IS_ERR(ecryptfs_kthread)) {
97 rc = PTR_ERR(ecryptfs_kthread);
98 printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]"
99 "\n", __func__, rc);
100 }
101 return rc;
102}
103
104void ecryptfs_destroy_kthread(void)
105{
Wei Yongjun8bbca572012-08-21 10:46:05 +0800106 struct ecryptfs_open_req *req, *tmp;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700107
108 mutex_lock(&ecryptfs_kthread_ctl.mux);
109 ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE;
Wei Yongjun8bbca572012-08-21 10:46:05 +0800110 list_for_each_entry_safe(req, tmp, &ecryptfs_kthread_ctl.req_list,
111 kthread_ctl_list) {
Al Viro3b8b4872012-06-25 11:38:56 +0400112 list_del(&req->kthread_ctl_list);
113 *req->lower_file = ERR_PTR(-EIO);
114 complete(&req->done);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700115 }
116 mutex_unlock(&ecryptfs_kthread_ctl.mux);
117 kthread_stop(ecryptfs_kthread);
118 wake_up(&ecryptfs_kthread_ctl.wait);
119}
120
121/**
122 * ecryptfs_privileged_open
123 * @lower_file: Result of dentry_open by root on lower dentry
124 * @lower_dentry: Lower dentry for file to open
125 * @lower_mnt: Lower vfsmount for file to open
126 *
127 * This function gets a r/w file opened againt the lower dentry.
128 *
129 * Returns zero on success; non-zero otherwise
130 */
131int ecryptfs_privileged_open(struct file **lower_file,
132 struct dentry *lower_dentry,
David Howells745ca242008-11-14 10:39:22 +1100133 struct vfsmount *lower_mnt,
134 const struct cred *cred)
Michael Halcrow746f1e52008-07-23 21:30:02 -0700135{
Al Viro3b8b4872012-06-25 11:38:56 +0400136 struct ecryptfs_open_req req;
Tyler Hicksac22ba22009-08-12 01:06:54 -0500137 int flags = O_LARGEFILE;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700138 int rc = 0;
139
Al Viro765927b2012-06-26 21:58:53 +0400140 init_completion(&req.done);
141 req.lower_file = lower_file;
142 req.path.dentry = lower_dentry;
143 req.path.mnt = lower_mnt;
144
Michael Halcrow746f1e52008-07-23 21:30:02 -0700145 /* Corresponding dput() and mntput() are done when the
Tyler Hicks332ab162011-04-14 15:35:11 -0500146 * lower file is fput() when all eCryptfs files for the inode are
147 * released. */
David Howells2b0143b2015-03-17 22:25:59 +0000148 flags |= IS_RDONLY(d_inode(lower_dentry)) ? O_RDONLY : O_RDWR;
Al Viro765927b2012-06-26 21:58:53 +0400149 (*lower_file) = dentry_open(&req.path, flags, cred);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700150 if (!IS_ERR(*lower_file))
Jann Horn2f36db72016-06-01 11:55:06 +0200151 goto have_file;
Tyler Hicks9fe79d72012-06-12 11:17:01 -0700152 if ((flags & O_ACCMODE) == O_RDONLY) {
Tyler Hicksac22ba22009-08-12 01:06:54 -0500153 rc = PTR_ERR((*lower_file));
154 goto out;
155 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700156 mutex_lock(&ecryptfs_kthread_ctl.mux);
157 if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
158 rc = -EIO;
159 mutex_unlock(&ecryptfs_kthread_ctl.mux);
160 printk(KERN_ERR "%s: We are in the middle of shutting down; "
161 "aborting privileged request to open lower file\n",
162 __func__);
Al Viro3b8b4872012-06-25 11:38:56 +0400163 goto out;
Michael Halcrow746f1e52008-07-23 21:30:02 -0700164 }
Al Viro3b8b4872012-06-25 11:38:56 +0400165 list_add_tail(&req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list);
Michael Halcrow746f1e52008-07-23 21:30:02 -0700166 mutex_unlock(&ecryptfs_kthread_ctl.mux);
167 wake_up(&ecryptfs_kthread_ctl.wait);
Al Viro3b8b4872012-06-25 11:38:56 +0400168 wait_for_completion(&req.done);
Jann Horn2f36db72016-06-01 11:55:06 +0200169 if (IS_ERR(*lower_file)) {
Al Viro3b8b4872012-06-25 11:38:56 +0400170 rc = PTR_ERR(*lower_file);
Jann Horn2f36db72016-06-01 11:55:06 +0200171 goto out;
172 }
173have_file:
174 if ((*lower_file)->f_op->mmap == NULL) {
175 fput(*lower_file);
176 *lower_file = NULL;
177 rc = -EMEDIUMTYPE;
178 }
Michael Halcrow746f1e52008-07-23 21:30:02 -0700179out:
180 return rc;
181}