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/usermode_driver.h>
9 static LIST_HEAD(umh_list);
10 static DEFINE_MUTEX(umh_list_lock);
12 static int umd_setup(struct subprocess_info *info, struct cred *new)
14 struct umd_info *umd_info = info->data;
15 struct file *from_umh[2];
16 struct file *to_umh[2];
19 /* create pipe to send data to umh */
20 err = create_pipe_files(to_umh, 0);
23 err = replace_fd(0, to_umh[0], 0);
30 /* create pipe to receive data from umh */
31 err = create_pipe_files(from_umh, 0);
34 replace_fd(0, NULL, 0);
37 err = replace_fd(1, from_umh[1], 0);
41 replace_fd(0, NULL, 0);
46 umd_info->pipe_to_umh = to_umh[1];
47 umd_info->pipe_from_umh = from_umh[0];
48 umd_info->pid = task_pid_nr(current);
49 current->flags |= PF_UMH;
53 static void umd_cleanup(struct subprocess_info *info)
55 struct umd_info *umd_info = info->data;
57 /* cleanup if umh_setup() was successful but exec failed */
59 fput(umd_info->pipe_to_umh);
60 fput(umd_info->pipe_from_umh);
65 * fork_usermode_blob - fork a blob of bytes as a usermode process
66 * @data: a blob of bytes that can be do_execv-ed as a file
67 * @len: length of the blob
68 * @info: information about usermode process (shouldn't be NULL)
70 * If info->cmdline is set it will be used as command line for the
71 * user process, else "usermodehelper" is used.
73 * Returns either negative error or zero which indicates success
74 * in executing a blob of bytes as a usermode process. In such
75 * case 'struct umd_info *info' is populated with two pipes
76 * and a pid of the process. The caller is responsible for health
77 * check of the user process, killing it via pid, and closing the
78 * pipes when user process is no longer needed.
80 int fork_usermode_blob(void *data, size_t len, struct umd_info *info)
82 const char *cmdline = (info->cmdline) ? info->cmdline : "usermodehelper";
83 struct subprocess_info *sub_info;
90 file = shmem_kernel_file_setup("", len, 0);
94 written = kernel_write(file, data, len, &pos);
103 argv = argv_split(GFP_KERNEL, cmdline, NULL);
107 sub_info = call_usermodehelper_setup("none", argv, NULL, GFP_KERNEL,
108 umd_setup, umd_cleanup, info);
112 sub_info->file = file;
113 err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
115 mutex_lock(&umh_list_lock);
116 list_add(&info->list, &umh_list);
117 mutex_unlock(&umh_list_lock);
125 EXPORT_SYMBOL_GPL(fork_usermode_blob);
127 void __exit_umh(struct task_struct *tsk)
129 struct umd_info *info;
130 pid_t pid = tsk->pid;
132 mutex_lock(&umh_list_lock);
133 list_for_each_entry(info, &umh_list, list) {
134 if (info->pid == pid) {
135 list_del(&info->list);
136 mutex_unlock(&umh_list_lock);
140 mutex_unlock(&umh_list_lock);