xref: /openbmc/linux/fs/ecryptfs/kthread.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2*446b5836SLee Jones /*
3746f1e55SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
4746f1e55SMichael Halcrow  *
5746f1e55SMichael Halcrow  * Copyright (C) 2008 International Business Machines Corp.
6746f1e55SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
7746f1e55SMichael Halcrow  */
8746f1e55SMichael Halcrow 
9746f1e55SMichael Halcrow #include <linux/kthread.h>
10746f1e55SMichael Halcrow #include <linux/freezer.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12746f1e55SMichael Halcrow #include <linux/wait.h>
13746f1e55SMichael Halcrow #include <linux/mount.h>
14746f1e55SMichael Halcrow #include "ecryptfs_kernel.h"
15746f1e55SMichael Halcrow 
163b8b4871SAl Viro struct ecryptfs_open_req {
173b8b4871SAl Viro 	struct file **lower_file;
18765927b2SAl Viro 	struct path path;
193b8b4871SAl Viro 	struct completion done;
203b8b4871SAl Viro 	struct list_head kthread_ctl_list;
213b8b4871SAl Viro };
22746f1e55SMichael Halcrow 
23746f1e55SMichael Halcrow static struct ecryptfs_kthread_ctl {
24746f1e55SMichael Halcrow #define ECRYPTFS_KTHREAD_ZOMBIE 0x00000001
25746f1e55SMichael Halcrow 	u32 flags;
26746f1e55SMichael Halcrow 	struct mutex mux;
27746f1e55SMichael Halcrow 	struct list_head req_list;
28746f1e55SMichael Halcrow 	wait_queue_head_t wait;
29746f1e55SMichael Halcrow } ecryptfs_kthread_ctl;
30746f1e55SMichael Halcrow 
31746f1e55SMichael Halcrow static struct task_struct *ecryptfs_kthread;
32746f1e55SMichael Halcrow 
33746f1e55SMichael Halcrow /**
34746f1e55SMichael Halcrow  * ecryptfs_threadfn
35746f1e55SMichael Halcrow  * @ignored: ignored
36746f1e55SMichael Halcrow  *
37746f1e55SMichael Halcrow  * The eCryptfs kernel thread that has the responsibility of getting
38332ab16fSTyler Hicks  * the lower file with RW permissions.
39746f1e55SMichael Halcrow  *
40746f1e55SMichael Halcrow  * Returns zero on success; non-zero otherwise
41746f1e55SMichael Halcrow  */
ecryptfs_threadfn(void * ignored)42746f1e55SMichael Halcrow static int ecryptfs_threadfn(void *ignored)
43746f1e55SMichael Halcrow {
44746f1e55SMichael Halcrow 	set_freezable();
45746f1e55SMichael Halcrow 	while (1)  {
46746f1e55SMichael Halcrow 		struct ecryptfs_open_req *req;
47746f1e55SMichael Halcrow 
48746f1e55SMichael Halcrow 		wait_event_freezable(
49746f1e55SMichael Halcrow 			ecryptfs_kthread_ctl.wait,
50746f1e55SMichael Halcrow 			(!list_empty(&ecryptfs_kthread_ctl.req_list)
51746f1e55SMichael Halcrow 			 || kthread_should_stop()));
52746f1e55SMichael Halcrow 		mutex_lock(&ecryptfs_kthread_ctl.mux);
53746f1e55SMichael Halcrow 		if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
54746f1e55SMichael Halcrow 			mutex_unlock(&ecryptfs_kthread_ctl.mux);
55746f1e55SMichael Halcrow 			goto out;
56746f1e55SMichael Halcrow 		}
57746f1e55SMichael Halcrow 		while (!list_empty(&ecryptfs_kthread_ctl.req_list)) {
58746f1e55SMichael Halcrow 			req = list_first_entry(&ecryptfs_kthread_ctl.req_list,
59746f1e55SMichael Halcrow 					       struct ecryptfs_open_req,
60746f1e55SMichael Halcrow 					       kthread_ctl_list);
61746f1e55SMichael Halcrow 			list_del(&req->kthread_ctl_list);
62765927b2SAl Viro 			*req->lower_file = dentry_open(&req->path,
63745ca247SDavid Howells 				(O_RDWR | O_LARGEFILE), current_cred());
643b8b4871SAl Viro 			complete(&req->done);
65746f1e55SMichael Halcrow 		}
66746f1e55SMichael Halcrow 		mutex_unlock(&ecryptfs_kthread_ctl.mux);
67746f1e55SMichael Halcrow 	}
68746f1e55SMichael Halcrow out:
69746f1e55SMichael Halcrow 	return 0;
70746f1e55SMichael Halcrow }
71746f1e55SMichael Halcrow 
ecryptfs_init_kthread(void)727371a382SJerome Marchand int __init ecryptfs_init_kthread(void)
73746f1e55SMichael Halcrow {
74746f1e55SMichael Halcrow 	int rc = 0;
75746f1e55SMichael Halcrow 
76746f1e55SMichael Halcrow 	mutex_init(&ecryptfs_kthread_ctl.mux);
77746f1e55SMichael Halcrow 	init_waitqueue_head(&ecryptfs_kthread_ctl.wait);
78746f1e55SMichael Halcrow 	INIT_LIST_HEAD(&ecryptfs_kthread_ctl.req_list);
79746f1e55SMichael Halcrow 	ecryptfs_kthread = kthread_run(&ecryptfs_threadfn, NULL,
80746f1e55SMichael Halcrow 				       "ecryptfs-kthread");
81746f1e55SMichael Halcrow 	if (IS_ERR(ecryptfs_kthread)) {
82746f1e55SMichael Halcrow 		rc = PTR_ERR(ecryptfs_kthread);
83746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: Failed to create kernel thread; rc = [%d]"
84746f1e55SMichael Halcrow 		       "\n", __func__, rc);
85746f1e55SMichael Halcrow 	}
86746f1e55SMichael Halcrow 	return rc;
87746f1e55SMichael Halcrow }
88746f1e55SMichael Halcrow 
ecryptfs_destroy_kthread(void)89746f1e55SMichael Halcrow void ecryptfs_destroy_kthread(void)
90746f1e55SMichael Halcrow {
918bbca57cSWei Yongjun 	struct ecryptfs_open_req *req, *tmp;
92746f1e55SMichael Halcrow 
93746f1e55SMichael Halcrow 	mutex_lock(&ecryptfs_kthread_ctl.mux);
94746f1e55SMichael Halcrow 	ecryptfs_kthread_ctl.flags |= ECRYPTFS_KTHREAD_ZOMBIE;
958bbca57cSWei Yongjun 	list_for_each_entry_safe(req, tmp, &ecryptfs_kthread_ctl.req_list,
96746f1e55SMichael Halcrow 				 kthread_ctl_list) {
973b8b4871SAl Viro 		list_del(&req->kthread_ctl_list);
983b8b4871SAl Viro 		*req->lower_file = ERR_PTR(-EIO);
993b8b4871SAl Viro 		complete(&req->done);
100746f1e55SMichael Halcrow 	}
101746f1e55SMichael Halcrow 	mutex_unlock(&ecryptfs_kthread_ctl.mux);
102746f1e55SMichael Halcrow 	kthread_stop(ecryptfs_kthread);
103746f1e55SMichael Halcrow 	wake_up(&ecryptfs_kthread_ctl.wait);
104746f1e55SMichael Halcrow }
105746f1e55SMichael Halcrow 
106746f1e55SMichael Halcrow /**
107746f1e55SMichael Halcrow  * ecryptfs_privileged_open
108746f1e55SMichael Halcrow  * @lower_file: Result of dentry_open by root on lower dentry
109746f1e55SMichael Halcrow  * @lower_dentry: Lower dentry for file to open
110746f1e55SMichael Halcrow  * @lower_mnt: Lower vfsmount for file to open
111*446b5836SLee Jones  * @cred: credential to use for this call
112746f1e55SMichael Halcrow  *
11357366a8dSMasahiro Yamada  * This function gets a r/w file opened against the lower dentry.
114746f1e55SMichael Halcrow  *
115746f1e55SMichael Halcrow  * Returns zero on success; non-zero otherwise
116746f1e55SMichael Halcrow  */
ecryptfs_privileged_open(struct file ** lower_file,struct dentry * lower_dentry,struct vfsmount * lower_mnt,const struct cred * cred)117746f1e55SMichael Halcrow int ecryptfs_privileged_open(struct file **lower_file,
118746f1e55SMichael Halcrow 			     struct dentry *lower_dentry,
119745ca247SDavid Howells 			     struct vfsmount *lower_mnt,
120745ca247SDavid Howells 			     const struct cred *cred)
121746f1e55SMichael Halcrow {
1223b8b4871SAl Viro 	struct ecryptfs_open_req req;
123ac22ba23STyler Hicks 	int flags = O_LARGEFILE;
124746f1e55SMichael Halcrow 	int rc = 0;
125746f1e55SMichael Halcrow 
126765927b2SAl Viro 	init_completion(&req.done);
127765927b2SAl Viro 	req.lower_file = lower_file;
128765927b2SAl Viro 	req.path.dentry = lower_dentry;
129765927b2SAl Viro 	req.path.mnt = lower_mnt;
130765927b2SAl Viro 
131746f1e55SMichael Halcrow 	/* Corresponding dput() and mntput() are done when the
132332ab16fSTyler Hicks 	 * lower file is fput() when all eCryptfs files for the inode are
133332ab16fSTyler Hicks 	 * released. */
1342b0143b5SDavid Howells 	flags |= IS_RDONLY(d_inode(lower_dentry)) ? O_RDONLY : O_RDWR;
135765927b2SAl Viro 	(*lower_file) = dentry_open(&req.path, flags, cred);
136746f1e55SMichael Halcrow 	if (!IS_ERR(*lower_file))
13778c4e172SJeff Mahoney 		goto out;
1389fe79d76STyler Hicks 	if ((flags & O_ACCMODE) == O_RDONLY) {
139ac22ba23STyler Hicks 		rc = PTR_ERR((*lower_file));
140ac22ba23STyler Hicks 		goto out;
141ac22ba23STyler Hicks 	}
142746f1e55SMichael Halcrow 	mutex_lock(&ecryptfs_kthread_ctl.mux);
143746f1e55SMichael Halcrow 	if (ecryptfs_kthread_ctl.flags & ECRYPTFS_KTHREAD_ZOMBIE) {
144746f1e55SMichael Halcrow 		rc = -EIO;
145746f1e55SMichael Halcrow 		mutex_unlock(&ecryptfs_kthread_ctl.mux);
146746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: We are in the middle of shutting down; "
147746f1e55SMichael Halcrow 		       "aborting privileged request to open lower file\n",
148746f1e55SMichael Halcrow 			__func__);
1493b8b4871SAl Viro 		goto out;
150746f1e55SMichael Halcrow 	}
1513b8b4871SAl Viro 	list_add_tail(&req.kthread_ctl_list, &ecryptfs_kthread_ctl.req_list);
152746f1e55SMichael Halcrow 	mutex_unlock(&ecryptfs_kthread_ctl.mux);
153746f1e55SMichael Halcrow 	wake_up(&ecryptfs_kthread_ctl.wait);
1543b8b4871SAl Viro 	wait_for_completion(&req.done);
15578c4e172SJeff Mahoney 	if (IS_ERR(*lower_file))
1563b8b4871SAl Viro 		rc = PTR_ERR(*lower_file);
157746f1e55SMichael Halcrow out:
158746f1e55SMichael Halcrow 	return rc;
159746f1e55SMichael Halcrow }
160