1746f1e55SMichael Halcrow /** 2746f1e55SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3746f1e55SMichael Halcrow * 4746f1e55SMichael Halcrow * Copyright (C) 2008 International Business Machines Corp. 5746f1e55SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 6746f1e55SMichael Halcrow * 7746f1e55SMichael Halcrow * This program is free software; you can redistribute it and/or 8746f1e55SMichael Halcrow * modify it under the terms of the GNU General Public License as 9746f1e55SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 10746f1e55SMichael Halcrow * License, or (at your option) any later version. 11746f1e55SMichael Halcrow * 12746f1e55SMichael Halcrow * This program is distributed in the hope that it will be useful, but 13746f1e55SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 14746f1e55SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15746f1e55SMichael Halcrow * General Public License for more details. 16746f1e55SMichael Halcrow * 17746f1e55SMichael Halcrow * You should have received a copy of the GNU General Public License 18746f1e55SMichael Halcrow * along with this program; if not, write to the Free Software 19746f1e55SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 20746f1e55SMichael Halcrow * 02111-1307, USA. 21746f1e55SMichael Halcrow */ 22746f1e55SMichael Halcrow 23746f1e55SMichael Halcrow #include <linux/kthread.h> 24746f1e55SMichael Halcrow #include <linux/freezer.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 26746f1e55SMichael Halcrow #include <linux/wait.h> 27746f1e55SMichael Halcrow #include <linux/mount.h> 28746f1e55SMichael Halcrow #include "ecryptfs_kernel.h" 29746f1e55SMichael Halcrow 303b8b4871SAl Viro struct ecryptfs_open_req { 313b8b4871SAl Viro struct file **lower_file; 32765927b2SAl Viro struct path path; 333b8b4871SAl Viro struct completion done; 343b8b4871SAl Viro struct list_head kthread_ctl_list; 353b8b4871SAl Viro }; 36746f1e55SMichael Halcrow 37746f1e55SMichael Halcrow static struct ecryptfs_kthread_ctl { 38746f1e55SMichael Halcrow #define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001 39746f1e55SMichael Halcrow u32 flags; 40746f1e55SMichael Halcrow struct mutex mux; 41746f1e55SMichael Halcrow struct list_head req_list; 42746f1e55SMichael Halcrow wait_queue_head_t wait; 43746f1e55SMichael Halcrow } ecryptfs_kthread_ctl; 44746f1e55SMichael Halcrow 45746f1e55SMichael Halcrow static struct task_struct *ecryptfs_kthread; 46746f1e55SMichael Halcrow 47746f1e55SMichael Halcrow /** 48746f1e55SMichael Halcrow * ecryptfs_threadfn 49746f1e55SMichael Halcrow * @ignored: ignored 50746f1e55SMichael Halcrow * 51746f1e55SMichael Halcrow * The eCryptfs kernel thread that has the responsibility of getting 52332ab16fSTyler Hicks * the lower file with RW permissions. 53746f1e55SMichael Halcrow * 54746f1e55SMichael Halcrow * Returns zero on success; non-zero otherwise 55746f1e55SMichael Halcrow */ 56746f1e55SMichael Halcrow static int ecryptfs_threadfn(void *ignored) 57746f1e55SMichael Halcrow { 58746f1e55SMichael Halcrow set_freezable(); 59746f1e55SMichael Halcrow while (1) { 60746f1e55SMichael Halcrow struct ecryptfs_open_req *req; 61746f1e55SMichael Halcrow 62746f1e55SMichael Halcrow wait_event_freezable( 63746f1e55SMichael Halcrow ecryptfs_kthread_ctl.wait, 64746f1e55SMichael Halcrow (!list_empty(&ecryptfs_kthread_ctl.req_list) 65746f1e55SMichael Halcrow || kthread_should_stop())); 66746f1e55SMichael Halcrow mutex_lock(&ecryptfs_kthread_ctl.mux); 67746f1e55SMichael Halcrow if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) { 68746f1e55SMichael Halcrow mutex_unlock(&ecryptfs_kthread_ctl.mux); 69746f1e55SMichael Halcrow goto out; 70746f1e55SMichael Halcrow } 71746f1e55SMichael Halcrow while (!list_empty(&ecryptfs_kthread_ctl.req_list)) { 72746f1e55SMichael Halcrow req = list_first_entry(&ecryptfs_kthread_ctl.req_list, 73746f1e55SMichael Halcrow struct ecryptfs_open_req, 74746f1e55SMichael Halcrow kthread_ctl_list); 75746f1e55SMichael Halcrow list_del(&req->kthread_ctl_list); 76765927b2SAl Viro *req->lower_file = dentry_open(&req->path, 77745ca247SDavid Howells (O_RDWR | O_LARGEFILE), current_cred()); 783b8b4871SAl Viro complete(&req->done); 79746f1e55SMichael Halcrow } 80746f1e55SMichael Halcrow mutex_unlock(&ecryptfs_kthread_ctl.mux); 81746f1e55SMichael Halcrow } 82746f1e55SMichael Halcrow out: 83746f1e55SMichael Halcrow return 0; 84746f1e55SMichael Halcrow } 85746f1e55SMichael Halcrow 867371a382SJerome Marchand int __init ecryptfs_init_kthread(void) 87746f1e55SMichael Halcrow { 88746f1e55SMichael Halcrow int rc = 0; 89746f1e55SMichael Halcrow 90746f1e55SMichael Halcrow mutex_init(&ecryptfs_kthread_ctl.mux); 91746f1e55SMichael Halcrow init_waitqueue_head(&ecryptfs_kthread_ctl.wait); 92746f1e55SMichael Halcrow INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list); 93746f1e55SMichael Halcrow ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL, 94746f1e55SMichael Halcrow "ecryptfs-kthread"); 95746f1e55SMichael Halcrow if (IS_ERR(ecryptfs_kthread)) { 96746f1e55SMichael Halcrow rc = PTR_ERR(ecryptfs_kthread); 97746f1e55SMichael Halcrow printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]" 98746f1e55SMichael Halcrow "\n", __func__, rc); 99746f1e55SMichael Halcrow } 100746f1e55SMichael Halcrow return rc; 101746f1e55SMichael Halcrow } 102746f1e55SMichael Halcrow 103746f1e55SMichael Halcrow void ecryptfs_destroy_kthread(void) 104746f1e55SMichael Halcrow { 105*8bbca57cSWei Yongjun struct ecryptfs_open_req *req, *tmp; 106746f1e55SMichael Halcrow 107746f1e55SMichael Halcrow mutex_lock(&ecryptfs_kthread_ctl.mux); 108746f1e55SMichael Halcrow ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE; 109*8bbca57cSWei Yongjun list_for_each_entry_safe(req, tmp, &ecryptfs_kthread_ctl.req_list, 110746f1e55SMichael Halcrow kthread_ctl_list) { 1113b8b4871SAl Viro list_del(&req->kthread_ctl_list); 1123b8b4871SAl Viro *req->lower_file = ERR_PTR(-EIO); 1133b8b4871SAl Viro complete(&req->done); 114746f1e55SMichael Halcrow } 115746f1e55SMichael Halcrow mutex_unlock(&ecryptfs_kthread_ctl.mux); 116746f1e55SMichael Halcrow kthread_stop(ecryptfs_kthread); 117746f1e55SMichael Halcrow wake_up(&ecryptfs_kthread_ctl.wait); 118746f1e55SMichael Halcrow } 119746f1e55SMichael Halcrow 120746f1e55SMichael Halcrow /** 121746f1e55SMichael Halcrow * ecryptfs_privileged_open 122746f1e55SMichael Halcrow * @lower_file: Result of dentry_open by root on lower dentry 123746f1e55SMichael Halcrow * @lower_dentry: Lower dentry for file to open 124746f1e55SMichael Halcrow * @lower_mnt: Lower vfsmount for file to open 125746f1e55SMichael Halcrow * 126746f1e55SMichael Halcrow * This function gets a r/w file opened againt the lower dentry. 127746f1e55SMichael Halcrow * 128746f1e55SMichael Halcrow * Returns zero on success; non-zero otherwise 129746f1e55SMichael Halcrow */ 130746f1e55SMichael Halcrow int ecryptfs_privileged_open(struct file **lower_file, 131746f1e55SMichael Halcrow struct dentry *lower_dentry, 132745ca247SDavid Howells struct vfsmount *lower_mnt, 133745ca247SDavid Howells const struct cred *cred) 134746f1e55SMichael Halcrow { 1353b8b4871SAl Viro struct ecryptfs_open_req req; 136ac22ba23STyler Hicks int flags = O_LARGEFILE; 137746f1e55SMichael Halcrow int rc = 0; 138746f1e55SMichael Halcrow 139765927b2SAl Viro init_completion(&req.done); 140765927b2SAl Viro req.lower_file = lower_file; 141765927b2SAl Viro req.path.dentry = lower_dentry; 142765927b2SAl Viro req.path.mnt = lower_mnt; 143765927b2SAl Viro 144746f1e55SMichael Halcrow /* Corresponding dput() and mntput() are done when the 145332ab16fSTyler Hicks * lower file is fput() when all eCryptfs files for the inode are 146332ab16fSTyler Hicks * released. */ 147ac22ba23STyler Hicks flags |= IS_RDONLY(lower_dentry->d_inode) ? O_RDONLY : O_RDWR; 148765927b2SAl Viro (*lower_file) = dentry_open(&req.path, flags, cred); 149746f1e55SMichael Halcrow if (!IS_ERR(*lower_file)) 150746f1e55SMichael Halcrow goto out; 1519fe79d76STyler Hicks if ((flags & O_ACCMODE) == O_RDONLY) { 152ac22ba23STyler Hicks rc = PTR_ERR((*lower_file)); 153ac22ba23STyler Hicks goto out; 154ac22ba23STyler Hicks } 155746f1e55SMichael Halcrow mutex_lock(&ecryptfs_kthread_ctl.mux); 156746f1e55SMichael Halcrow if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) { 157746f1e55SMichael Halcrow rc = -EIO; 158746f1e55SMichael Halcrow mutex_unlock(&ecryptfs_kthread_ctl.mux); 159746f1e55SMichael Halcrow printk(KERN_ERR "%s: We are in the middle of shutting down; " 160746f1e55SMichael Halcrow "aborting privileged request to open lower file\n", 161746f1e55SMichael Halcrow __func__); 1623b8b4871SAl Viro goto out; 163746f1e55SMichael Halcrow } 1643b8b4871SAl Viro list_add_tail(&req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list); 165746f1e55SMichael Halcrow mutex_unlock(&ecryptfs_kthread_ctl.mux); 166746f1e55SMichael Halcrow wake_up(&ecryptfs_kthread_ctl.wait); 1673b8b4871SAl Viro wait_for_completion(&req.done); 1683b8b4871SAl Viro if (IS_ERR(*lower_file)) 1693b8b4871SAl Viro rc = PTR_ERR(*lower_file); 170746f1e55SMichael Halcrow out: 171746f1e55SMichael Halcrow return rc; 172746f1e55SMichael Halcrow } 173