blob: f77f8d7ce9e324b499adaaf258442f001e52cfdf [file] [log] [blame]
Eric W. Biederman884c5e62020-06-26 12:23:00 -05001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * umd - User mode driver support
4 */
5#include <linux/shmem_fs.h>
6#include <linux/pipe_fs_i.h>
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -05007#include <linux/mount.h>
8#include <linux/fs_struct.h>
9#include <linux/task_work.h>
Eric W. Biederman884c5e62020-06-26 12:23:00 -050010#include <linux/usermode_driver.h>
11
12static LIST_HEAD(umh_list);
13static DEFINE_MUTEX(umh_list_lock);
14
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -050015static struct vfsmount *blob_to_mnt(const void *data, size_t len, const char *name)
16{
17 struct file_system_type *type;
18 struct vfsmount *mnt;
19 struct file *file;
20 ssize_t written;
21 loff_t pos = 0;
22
23 type = get_fs_type("tmpfs");
24 if (!type)
25 return ERR_PTR(-ENODEV);
26
27 mnt = kern_mount(type);
28 put_filesystem(type);
29 if (IS_ERR(mnt))
30 return mnt;
31
32 file = file_open_root(mnt->mnt_root, mnt, name, O_CREAT | O_WRONLY, 0700);
33 if (IS_ERR(file)) {
34 mntput(mnt);
35 return ERR_CAST(file);
36 }
37
38 written = kernel_write(file, data, len, &pos);
39 if (written != len) {
40 int err = written;
41 if (err >= 0)
42 err = -ENOMEM;
43 filp_close(file, NULL);
44 mntput(mnt);
45 return ERR_PTR(err);
46 }
47
48 fput(file);
49
50 /* Flush delayed fput so exec can open the file read-only */
51 flush_delayed_fput();
52 task_work_run();
53 return mnt;
54}
55
56/**
57 * umd_load_blob - Remember a blob of bytes for fork_usermode_driver
58 * @info: information about usermode driver
59 * @data: a blob of bytes that can be executed as a file
60 * @len: The lentgh of the blob
61 *
62 */
63int umd_load_blob(struct umd_info *info, const void *data, size_t len)
64{
65 struct vfsmount *mnt;
66
67 if (WARN_ON_ONCE(info->wd.dentry || info->wd.mnt))
68 return -EBUSY;
69
70 mnt = blob_to_mnt(data, len, info->driver_name);
71 if (IS_ERR(mnt))
72 return PTR_ERR(mnt);
73
74 info->wd.mnt = mnt;
75 info->wd.dentry = mnt->mnt_root;
76 return 0;
77}
78EXPORT_SYMBOL_GPL(umd_load_blob);
79
80/**
81 * umd_unload_blob - Disassociate @info from a previously loaded blob
82 * @info: information about usermode driver
83 *
84 */
85int umd_unload_blob(struct umd_info *info)
86{
87 if (WARN_ON_ONCE(!info->wd.mnt ||
88 !info->wd.dentry ||
89 info->wd.mnt->mnt_root != info->wd.dentry))
90 return -EINVAL;
91
92 kern_unmount(info->wd.mnt);
93 info->wd.mnt = NULL;
94 info->wd.dentry = NULL;
95 return 0;
96}
97EXPORT_SYMBOL_GPL(umd_unload_blob);
98
Eric W. Biederman884c5e62020-06-26 12:23:00 -050099static int umd_setup(struct subprocess_info *info, struct cred *new)
100{
Eric W. Biederman74be2d32020-06-26 11:16:06 -0500101 struct umd_info *umd_info = info->data;
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500102 struct file *from_umh[2];
103 struct file *to_umh[2];
104 int err;
105
106 /* create pipe to send data to umh */
107 err = create_pipe_files(to_umh, 0);
108 if (err)
109 return err;
110 err = replace_fd(0, to_umh[0], 0);
111 fput(to_umh[0]);
112 if (err < 0) {
113 fput(to_umh[1]);
114 return err;
115 }
116
117 /* create pipe to receive data from umh */
118 err = create_pipe_files(from_umh, 0);
119 if (err) {
120 fput(to_umh[1]);
121 replace_fd(0, NULL, 0);
122 return err;
123 }
124 err = replace_fd(1, from_umh[1], 0);
125 fput(from_umh[1]);
126 if (err < 0) {
127 fput(to_umh[1]);
128 replace_fd(0, NULL, 0);
129 fput(from_umh[0]);
130 return err;
131 }
132
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -0500133 set_fs_pwd(current->fs, &umd_info->wd);
Eric W. Biederman74be2d32020-06-26 11:16:06 -0500134 umd_info->pipe_to_umh = to_umh[1];
135 umd_info->pipe_from_umh = from_umh[0];
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500136 umd_info->tgid = get_pid(task_tgid(current));
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500137 current->flags |= PF_UMH;
138 return 0;
139}
140
141static void umd_cleanup(struct subprocess_info *info)
142{
Eric W. Biederman74be2d32020-06-26 11:16:06 -0500143 struct umd_info *umd_info = info->data;
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500144
145 /* cleanup if umh_setup() was successful but exec failed */
146 if (info->retval) {
Eric W. Biederman74be2d32020-06-26 11:16:06 -0500147 fput(umd_info->pipe_to_umh);
148 fput(umd_info->pipe_from_umh);
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500149 put_pid(umd_info->tgid);
150 umd_info->tgid = NULL;
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500151 }
152}
153
154/**
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -0500155 * fork_usermode_driver - fork a usermode driver
156 * @info: information about usermode driver (shouldn't be NULL)
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500157 *
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -0500158 * Returns either negative error or zero which indicates success in
159 * executing a usermode driver. In such case 'struct umd_info *info'
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500160 * is populated with two pipes and a tgid of the process. The caller is
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -0500161 * responsible for health check of the user process, killing it via
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500162 * tgid, and closing the pipes when user process is no longer needed.
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500163 */
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -0500164int fork_usermode_driver(struct umd_info *info)
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500165{
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500166 struct subprocess_info *sub_info;
167 char **argv = NULL;
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500168 int err;
169
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500170 if (WARN_ON_ONCE(info->tgid))
171 return -EBUSY;
172
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500173 err = -ENOMEM;
Eric W. Biederman1199c6c2020-06-25 11:38:08 -0500174 argv = argv_split(GFP_KERNEL, info->driver_name, NULL);
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500175 if (!argv)
176 goto out;
177
Eric W. Biederman1199c6c2020-06-25 11:38:08 -0500178 sub_info = call_usermodehelper_setup(info->driver_name, argv, NULL,
179 GFP_KERNEL,
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500180 umd_setup, umd_cleanup, info);
181 if (!sub_info)
182 goto out;
183
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500184 err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
185 if (!err) {
186 mutex_lock(&umh_list_lock);
187 list_add(&info->list, &umh_list);
188 mutex_unlock(&umh_list_lock);
189 }
190out:
191 if (argv)
192 argv_free(argv);
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500193 return err;
194}
Eric W. Biedermane2dc9bf2020-06-25 13:12:59 -0500195EXPORT_SYMBOL_GPL(fork_usermode_driver);
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500196
197void __exit_umh(struct task_struct *tsk)
198{
Eric W. Biederman74be2d32020-06-26 11:16:06 -0500199 struct umd_info *info;
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500200 struct pid *tgid = task_tgid(tsk);
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500201
202 mutex_lock(&umh_list_lock);
203 list_for_each_entry(info, &umh_list, list) {
Eric W. Biederman1c340ea2020-06-25 16:48:26 -0500204 if (info->tgid == tgid) {
Eric W. Biederman884c5e62020-06-26 12:23:00 -0500205 list_del(&info->list);
206 mutex_unlock(&umh_list_lock);
207 goto out;
208 }
209 }
210 mutex_unlock(&umh_list_lock);
211 return;
212out:
213 if (info->cleanup)
214 info->cleanup(info);
215}
216