1 // SPDX-License-Identifier: GPL-2.0-only
3 * umd - User mode driver support
5 #include <linux/shmem_fs.h>
6 #include <linux/pipe_fs_i.h>
7 #include <linux/mount.h>
8 #include <linux/fs_struct.h>
9 #include <linux/task_work.h>
10 #include <linux/usermode_driver.h>
12 static struct vfsmount *blob_to_mnt(const void *data, size_t len, const char *name)
14 struct file_system_type *type;
20 type = get_fs_type("tmpfs");
22 return ERR_PTR(-ENODEV);
24 mnt = kern_mount(type);
29 file = file_open_root(mnt->mnt_root, mnt, name, O_CREAT | O_WRONLY, 0700);
32 return ERR_CAST(file);
35 written = kernel_write(file, data, len, &pos);
40 filp_close(file, NULL);
47 /* Flush delayed fput so exec can open the file read-only */
54 * umd_load_blob - Remember a blob of bytes for fork_usermode_driver
55 * @info: information about usermode driver
56 * @data: a blob of bytes that can be executed as a file
57 * @len: The lentgh of the blob
60 int umd_load_blob(struct umd_info *info, const void *data, size_t len)
64 if (WARN_ON_ONCE(info->wd.dentry || info->wd.mnt))
67 mnt = blob_to_mnt(data, len, info->driver_name);
72 info->wd.dentry = mnt->mnt_root;
75 EXPORT_SYMBOL_GPL(umd_load_blob);
78 * umd_unload_blob - Disassociate @info from a previously loaded blob
79 * @info: information about usermode driver
82 int umd_unload_blob(struct umd_info *info)
84 if (WARN_ON_ONCE(!info->wd.mnt ||
86 info->wd.mnt->mnt_root != info->wd.dentry))
89 kern_unmount(info->wd.mnt);
91 info->wd.dentry = NULL;
94 EXPORT_SYMBOL_GPL(umd_unload_blob);
96 static int umd_setup(struct subprocess_info *info, struct cred *new)
98 struct umd_info *umd_info = info->data;
99 struct file *from_umh[2];
100 struct file *to_umh[2];
103 /* create pipe to send data to umh */
104 err = create_pipe_files(to_umh, 0);
107 err = replace_fd(0, to_umh[0], 0);
114 /* create pipe to receive data from umh */
115 err = create_pipe_files(from_umh, 0);
118 replace_fd(0, NULL, 0);
121 err = replace_fd(1, from_umh[1], 0);
125 replace_fd(0, NULL, 0);
130 set_fs_pwd(current->fs, &umd_info->wd);
131 umd_info->pipe_to_umh = to_umh[1];
132 umd_info->pipe_from_umh = from_umh[0];
133 umd_info->tgid = get_pid(task_tgid(current));
137 static void umd_cleanup(struct subprocess_info *info)
139 struct umd_info *umd_info = info->data;
141 /* cleanup if umh_setup() was successful but exec failed */
143 umd_cleanup_helper(umd_info);
147 * umd_cleanup_helper - release the resources which were allocated in umd_setup
148 * @info: information about usermode driver
150 void umd_cleanup_helper(struct umd_info *info)
152 fput(info->pipe_to_umh);
153 fput(info->pipe_from_umh);
157 EXPORT_SYMBOL_GPL(umd_cleanup_helper);
160 * fork_usermode_driver - fork a usermode driver
161 * @info: information about usermode driver (shouldn't be NULL)
163 * Returns either negative error or zero which indicates success in
164 * executing a usermode driver. In such case 'struct umd_info *info'
165 * is populated with two pipes and a tgid of the process. The caller is
166 * responsible for health check of the user process, killing it via
167 * tgid, and closing the pipes when user process is no longer needed.
169 int fork_usermode_driver(struct umd_info *info)
171 struct subprocess_info *sub_info;
172 const char *argv[] = { info->driver_name, NULL };
175 if (WARN_ON_ONCE(info->tgid))
179 sub_info = call_usermodehelper_setup(info->driver_name,
180 (char **)argv, NULL, GFP_KERNEL,
181 umd_setup, umd_cleanup, info);
185 err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
189 EXPORT_SYMBOL_GPL(fork_usermode_driver);