Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 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 | |
| 23 | #include <linux/kthread.h> |
| 24 | #include <linux/freezer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 26 | #include <linux/wait.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include "ecryptfs_kernel.h" |
| 29 | |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 30 | struct ecryptfs_open_req { |
| 31 | struct file **lower_file; |
| 32 | struct dentry *lower_dentry; |
| 33 | struct vfsmount *lower_mnt; |
| 34 | struct completion done; |
| 35 | struct list_head kthread_ctl_list; |
| 36 | }; |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 37 | |
| 38 | static 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 | |
| 46 | static 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 Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 53 | * the lower file with RW permissions. |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 54 | * |
| 55 | * Returns zero on success; non-zero otherwise |
| 56 | */ |
| 57 | static 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 Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 76 | list_del(&req->kthread_ctl_list); |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 77 | dget(req->lower_dentry); |
| 78 | mntget(req->lower_mnt); |
| 79 | (*req->lower_file) = dentry_open( |
| 80 | req->lower_dentry, req->lower_mnt, |
| 81 | (O_RDWR | O_LARGEFILE), current_cred()); |
| 82 | complete(&req->done); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 83 | } |
| 84 | mutex_unlock(&ecryptfs_kthread_ctl.mux); |
| 85 | } |
| 86 | out: |
| 87 | return 0; |
| 88 | } |
| 89 | |
Jerome Marchand | 7371a38 | 2010-08-17 17:24:05 +0200 | [diff] [blame] | 90 | int __init ecryptfs_init_kthread(void) |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 91 | { |
| 92 | int rc = 0; |
| 93 | |
| 94 | mutex_init(&ecryptfs_kthread_ctl.mux); |
| 95 | init_waitqueue_head(&ecryptfs_kthread_ctl.wait); |
| 96 | INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list); |
| 97 | ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL, |
| 98 | "ecryptfs-kthread"); |
| 99 | if (IS_ERR(ecryptfs_kthread)) { |
| 100 | rc = PTR_ERR(ecryptfs_kthread); |
| 101 | printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]" |
| 102 | "\n", __func__, rc); |
| 103 | } |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | void ecryptfs_destroy_kthread(void) |
| 108 | { |
| 109 | struct ecryptfs_open_req *req; |
| 110 | |
| 111 | mutex_lock(&ecryptfs_kthread_ctl.mux); |
| 112 | ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE; |
| 113 | list_for_each_entry(req, &ecryptfs_kthread_ctl.req_list, |
| 114 | kthread_ctl_list) { |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 115 | list_del(&req->kthread_ctl_list); |
| 116 | *req->lower_file = ERR_PTR(-EIO); |
| 117 | complete(&req->done); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 118 | } |
| 119 | mutex_unlock(&ecryptfs_kthread_ctl.mux); |
| 120 | kthread_stop(ecryptfs_kthread); |
| 121 | wake_up(&ecryptfs_kthread_ctl.wait); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * ecryptfs_privileged_open |
| 126 | * @lower_file: Result of dentry_open by root on lower dentry |
| 127 | * @lower_dentry: Lower dentry for file to open |
| 128 | * @lower_mnt: Lower vfsmount for file to open |
| 129 | * |
| 130 | * This function gets a r/w file opened againt the lower dentry. |
| 131 | * |
| 132 | * Returns zero on success; non-zero otherwise |
| 133 | */ |
| 134 | int ecryptfs_privileged_open(struct file **lower_file, |
| 135 | struct dentry *lower_dentry, |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 136 | struct vfsmount *lower_mnt, |
| 137 | const struct cred *cred) |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 138 | { |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 139 | struct ecryptfs_open_req req; |
Tyler Hicks | ac22ba2 | 2009-08-12 01:06:54 -0500 | [diff] [blame] | 140 | int flags = O_LARGEFILE; |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 141 | int rc = 0; |
| 142 | |
| 143 | /* Corresponding dput() and mntput() are done when the |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 144 | * lower file is fput() when all eCryptfs files for the inode are |
| 145 | * released. */ |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 146 | dget(lower_dentry); |
| 147 | mntget(lower_mnt); |
Tyler Hicks | ac22ba2 | 2009-08-12 01:06:54 -0500 | [diff] [blame] | 148 | flags |= IS_RDONLY(lower_dentry->d_inode) ? O_RDONLY : O_RDWR; |
| 149 | (*lower_file) = dentry_open(lower_dentry, lower_mnt, flags, cred); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 150 | if (!IS_ERR(*lower_file)) |
| 151 | goto out; |
Tyler Hicks | 9fe79d7 | 2012-06-12 11:17:01 -0700 | [diff] [blame] | 152 | if ((flags & O_ACCMODE) == O_RDONLY) { |
Tyler Hicks | ac22ba2 | 2009-08-12 01:06:54 -0500 | [diff] [blame] | 153 | rc = PTR_ERR((*lower_file)); |
| 154 | goto out; |
| 155 | } |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 156 | init_completion(&req.done); |
| 157 | req.lower_file = lower_file; |
| 158 | req.lower_dentry = lower_dentry; |
| 159 | req.lower_mnt = lower_mnt; |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 160 | mutex_lock(&ecryptfs_kthread_ctl.mux); |
| 161 | if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) { |
| 162 | rc = -EIO; |
| 163 | mutex_unlock(&ecryptfs_kthread_ctl.mux); |
| 164 | printk(KERN_ERR "%s: We are in the middle of shutting down; " |
| 165 | "aborting privileged request to open lower file\n", |
| 166 | __func__); |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 167 | goto out; |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 168 | } |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 169 | list_add_tail(&req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 170 | mutex_unlock(&ecryptfs_kthread_ctl.mux); |
| 171 | wake_up(&ecryptfs_kthread_ctl.wait); |
Al Viro | 3b8b487 | 2012-06-25 11:38:56 +0400 | [diff] [blame^] | 172 | wait_for_completion(&req.done); |
| 173 | if (IS_ERR(*lower_file)) |
| 174 | rc = PTR_ERR(*lower_file); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 175 | out: |
| 176 | return rc; |
| 177 | } |