1 // SPDX-License-Identifier: GPL-2.0-only
3 * umh - the kernel usermode helper
5 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <linux/sched/task.h>
8 #include <linux/binfmts.h>
9 #include <linux/syscalls.h>
10 #include <linux/unistd.h>
11 #include <linux/kmod.h>
12 #include <linux/slab.h>
13 #include <linux/completion.h>
14 #include <linux/cred.h>
15 #include <linux/file.h>
16 #include <linux/fdtable.h>
17 #include <linux/workqueue.h>
18 #include <linux/security.h>
19 #include <linux/mount.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/resource.h>
23 #include <linux/notifier.h>
24 #include <linux/suspend.h>
25 #include <linux/rwsem.h>
26 #include <linux/ptrace.h>
27 #include <linux/async.h>
28 #include <linux/uaccess.h>
29 #include <linux/shmem_fs.h>
30 #include <linux/pipe_fs_i.h>
32 #include <trace/events/module.h>
34 #define CAP_BSET (void *)1
35 #define CAP_PI (void *)2
37 static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
38 static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
39 static DEFINE_SPINLOCK(umh_sysctl_lock);
40 static DECLARE_RWSEM(umhelper_sem);
41 static LIST_HEAD(umh_list);
42 static DEFINE_MUTEX(umh_list_lock);
44 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
47 (*info->cleanup)(info);
51 static void umh_complete(struct subprocess_info *sub_info)
53 struct completion *comp = xchg(&sub_info->complete, NULL);
55 * See call_usermodehelper_exec(). If xchg() returns NULL
56 * we own sub_info, the UMH_KILLABLE caller has gone away
57 * or the caller used UMH_NO_WAIT.
62 call_usermodehelper_freeinfo(sub_info);
66 * This is the task which runs the usermode application
68 static int call_usermodehelper_exec_async(void *data)
70 struct subprocess_info *sub_info = data;
74 spin_lock_irq(¤t->sighand->siglock);
75 flush_signal_handlers(current, 1);
76 spin_unlock_irq(¤t->sighand->siglock);
79 * Our parent (unbound workqueue) runs with elevated scheduling
80 * priority. Avoid propagating that into the userspace child.
82 set_user_nice(current, 0);
85 new = prepare_kernel_cred(current);
89 spin_lock(&umh_sysctl_lock);
90 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
91 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
92 new->cap_inheritable);
93 spin_unlock(&umh_sysctl_lock);
96 retval = sub_info->init(sub_info, new);
105 sub_info->pid = task_pid_nr(current);
106 if (sub_info->file) {
107 retval = do_execve_file(sub_info->file,
108 sub_info->argv, sub_info->envp);
110 current->flags |= PF_UMH;
112 retval = do_execve(getname_kernel(sub_info->path),
113 (const char __user *const __user *)sub_info->argv,
114 (const char __user *const __user *)sub_info->envp);
116 sub_info->retval = retval;
118 * call_usermodehelper_exec_sync() will call umh_complete
121 if (!(sub_info->wait & UMH_WAIT_PROC))
122 umh_complete(sub_info);
128 /* Handles UMH_WAIT_PROC. */
129 static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
133 /* If SIGCLD is ignored kernel_wait4 won't populate the status. */
134 kernel_sigaction(SIGCHLD, SIG_DFL);
135 pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
137 sub_info->retval = pid;
141 * Normally it is bogus to call wait4() from in-kernel because
142 * wait4() wants to write the exit code to a userspace address.
143 * But call_usermodehelper_exec_sync() always runs as kernel
144 * thread (workqueue) and put_user() to a kernel address works
145 * OK for kernel threads, due to their having an mm_segment_t
146 * which spans the entire address space.
148 * Thus the __user pointer cast is valid here.
150 kernel_wait4(pid, (int __user *)&ret, 0, NULL);
153 * If ret is 0, either call_usermodehelper_exec_async failed and
154 * the real error code is already in sub_info->retval or
155 * sub_info->retval is 0 anyway, so don't mess with it then.
158 sub_info->retval = ret;
161 /* Restore default kernel sig handler */
162 kernel_sigaction(SIGCHLD, SIG_IGN);
164 umh_complete(sub_info);
168 * We need to create the usermodehelper kernel thread from a task that is affine
169 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
170 * inherit a widest affinity irrespective of call_usermodehelper() callers with
171 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
172 * usermodehelper targets to contend a busy CPU.
174 * Unbound workqueues provide such wide affinity and allow to block on
175 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
177 * Besides, workqueues provide the privilege level that caller might not have
178 * to perform the usermodehelper request.
181 static void call_usermodehelper_exec_work(struct work_struct *work)
183 struct subprocess_info *sub_info =
184 container_of(work, struct subprocess_info, work);
186 if (sub_info->wait & UMH_WAIT_PROC) {
187 call_usermodehelper_exec_sync(sub_info);
191 * Use CLONE_PARENT to reparent it to kthreadd; we do not
192 * want to pollute current->children, and we need a parent
193 * that always ignores SIGCHLD to ensure auto-reaping.
195 pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
196 CLONE_PARENT | SIGCHLD);
198 sub_info->retval = pid;
199 umh_complete(sub_info);
205 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
206 * (used for preventing user land processes from being created after the user
207 * land has been frozen during a system-wide hibernation or suspend operation).
208 * Should always be manipulated under umhelper_sem acquired for write.
210 static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
212 /* Number of helpers running */
213 static atomic_t running_helpers = ATOMIC_INIT(0);
216 * Wait queue head used by usermodehelper_disable() to wait for all running
219 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
222 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
225 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
228 * Time to wait for running_helpers to become zero before the setting of
229 * usermodehelper_disabled in usermodehelper_disable() fails
231 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
233 int usermodehelper_read_trylock(void)
238 down_read(&umhelper_sem);
240 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
242 if (!usermodehelper_disabled)
245 if (usermodehelper_disabled == UMH_DISABLED)
248 up_read(&umhelper_sem);
256 down_read(&umhelper_sem);
258 finish_wait(&usermodehelper_disabled_waitq, &wait);
261 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
263 long usermodehelper_read_lock_wait(long timeout)
270 down_read(&umhelper_sem);
272 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
273 TASK_UNINTERRUPTIBLE);
274 if (!usermodehelper_disabled)
277 up_read(&umhelper_sem);
279 timeout = schedule_timeout(timeout);
283 down_read(&umhelper_sem);
285 finish_wait(&usermodehelper_disabled_waitq, &wait);
288 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
290 void usermodehelper_read_unlock(void)
292 up_read(&umhelper_sem);
294 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
297 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
298 * @depth: New value to assign to usermodehelper_disabled.
300 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
301 * writing) and wakeup tasks waiting for it to change.
303 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
305 down_write(&umhelper_sem);
306 usermodehelper_disabled = depth;
307 wake_up(&usermodehelper_disabled_waitq);
308 up_write(&umhelper_sem);
312 * __usermodehelper_disable - Prevent new helpers from being started.
313 * @depth: New value to assign to usermodehelper_disabled.
315 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
317 int __usermodehelper_disable(enum umh_disable_depth depth)
324 down_write(&umhelper_sem);
325 usermodehelper_disabled = depth;
326 up_write(&umhelper_sem);
329 * From now on call_usermodehelper_exec() won't start any new
330 * helpers, so it is sufficient if running_helpers turns out to
331 * be zero at one point (it may be increased later, but that
334 retval = wait_event_timeout(running_helpers_waitq,
335 atomic_read(&running_helpers) == 0,
336 RUNNING_HELPERS_TIMEOUT);
340 __usermodehelper_set_disable_depth(UMH_ENABLED);
344 static void helper_lock(void)
346 atomic_inc(&running_helpers);
347 smp_mb__after_atomic();
350 static void helper_unlock(void)
352 if (atomic_dec_and_test(&running_helpers))
353 wake_up(&running_helpers_waitq);
357 * call_usermodehelper_setup - prepare to call a usermode helper
358 * @path: path to usermode executable
359 * @argv: arg vector for process
360 * @envp: environment for process
361 * @gfp_mask: gfp mask for memory allocation
362 * @cleanup: a cleanup function
363 * @init: an init function
364 * @data: arbitrary context sensitive data
366 * Returns either %NULL on allocation failure, or a subprocess_info
367 * structure. This should be passed to call_usermodehelper_exec to
368 * exec the process and free the structure.
370 * The init function is used to customize the helper process prior to
371 * exec. A non-zero return code causes the process to error out, exit,
372 * and return the failure to the calling process
374 * The cleanup function is just before ethe subprocess_info is about to
375 * be freed. This can be used for freeing the argv and envp. The
376 * Function must be runnable in either a process context or the
377 * context in which call_usermodehelper_exec is called.
379 struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
380 char **envp, gfp_t gfp_mask,
381 int (*init)(struct subprocess_info *info, struct cred *new),
382 void (*cleanup)(struct subprocess_info *info),
385 struct subprocess_info *sub_info;
386 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
390 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
392 #ifdef CONFIG_STATIC_USERMODEHELPER
393 sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
395 sub_info->path = path;
397 sub_info->argv = argv;
398 sub_info->envp = envp;
400 sub_info->cleanup = cleanup;
401 sub_info->init = init;
402 sub_info->data = data;
406 EXPORT_SYMBOL(call_usermodehelper_setup);
408 struct subprocess_info *call_usermodehelper_setup_file(struct file *file,
409 int (*init)(struct subprocess_info *info, struct cred *new),
410 void (*cleanup)(struct subprocess_info *info), void *data)
412 struct subprocess_info *sub_info;
413 struct umh_info *info = data;
414 const char *cmdline = (info->cmdline) ? info->cmdline : "usermodehelper";
416 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_KERNEL);
420 sub_info->argv = argv_split(GFP_KERNEL, cmdline, NULL);
421 if (!sub_info->argv) {
426 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
427 sub_info->path = "none";
428 sub_info->file = file;
429 sub_info->init = init;
430 sub_info->cleanup = cleanup;
431 sub_info->data = data;
435 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
437 struct umh_info *umh_info = info->data;
438 struct file *from_umh[2];
439 struct file *to_umh[2];
442 /* create pipe to send data to umh */
443 err = create_pipe_files(to_umh, 0);
446 err = replace_fd(0, to_umh[0], 0);
453 /* create pipe to receive data from umh */
454 err = create_pipe_files(from_umh, 0);
457 replace_fd(0, NULL, 0);
460 err = replace_fd(1, from_umh[1], 0);
464 replace_fd(0, NULL, 0);
469 umh_info->pipe_to_umh = to_umh[1];
470 umh_info->pipe_from_umh = from_umh[0];
474 static void umh_clean_and_save_pid(struct subprocess_info *info)
476 struct umh_info *umh_info = info->data;
478 argv_free(info->argv);
479 umh_info->pid = info->pid;
483 * fork_usermode_blob - fork a blob of bytes as a usermode process
484 * @data: a blob of bytes that can be do_execv-ed as a file
485 * @len: length of the blob
486 * @info: information about usermode process (shouldn't be NULL)
488 * If info->cmdline is set it will be used as command line for the
489 * user process, else "usermodehelper" is used.
491 * Returns either negative error or zero which indicates success
492 * in executing a blob of bytes as a usermode process. In such
493 * case 'struct umh_info *info' is populated with two pipes
494 * and a pid of the process. The caller is responsible for health
495 * check of the user process, killing it via pid, and closing the
496 * pipes when user process is no longer needed.
498 int fork_usermode_blob(void *data, size_t len, struct umh_info *info)
500 struct subprocess_info *sub_info;
506 file = shmem_kernel_file_setup("", len, 0);
508 return PTR_ERR(file);
510 written = kernel_write(file, data, len, &pos);
511 if (written != len) {
519 sub_info = call_usermodehelper_setup_file(file, umh_pipe_setup,
520 umh_clean_and_save_pid, info);
524 err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
526 mutex_lock(&umh_list_lock);
527 list_add(&info->list, &umh_list);
528 mutex_unlock(&umh_list_lock);
534 EXPORT_SYMBOL_GPL(fork_usermode_blob);
537 * call_usermodehelper_exec - start a usermode application
538 * @sub_info: information about the subprocessa
539 * @wait: wait for the application to finish and return status.
540 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
541 * when the program couldn't be exec'ed. This makes it safe to call
542 * from interrupt context.
544 * Runs a user-space application. The application is started
545 * asynchronously if wait is not set, and runs as a child of system workqueues.
546 * (ie. it runs with full root capabilities and optimized affinity).
548 int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
550 DECLARE_COMPLETION_ONSTACK(done);
553 if (!sub_info->path) {
554 call_usermodehelper_freeinfo(sub_info);
558 if (usermodehelper_disabled) {
564 * If there is no binary for us to call, then just return and get out of
565 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
566 * disable all call_usermodehelper() calls.
568 if (strlen(sub_info->path) == 0)
572 * Set the completion pointer only if there is a waiter.
573 * This makes it possible to use umh_complete to free
574 * the data structure in case of UMH_NO_WAIT.
576 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
577 sub_info->wait = wait;
579 queue_work(system_unbound_wq, &sub_info->work);
580 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
583 if (wait & UMH_KILLABLE) {
584 retval = wait_for_completion_killable(&done);
588 /* umh_complete() will see NULL and free sub_info */
589 if (xchg(&sub_info->complete, NULL))
591 /* fallthrough, umh_complete() was already called */
594 wait_for_completion(&done);
596 retval = sub_info->retval;
598 call_usermodehelper_freeinfo(sub_info);
603 EXPORT_SYMBOL(call_usermodehelper_exec);
606 * call_usermodehelper() - prepare and start a usermode application
607 * @path: path to usermode executable
608 * @argv: arg vector for process
609 * @envp: environment for process
610 * @wait: wait for the application to finish and return status.
611 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
612 * when the program couldn't be exec'ed. This makes it safe to call
613 * from interrupt context.
615 * This function is the equivalent to use call_usermodehelper_setup() and
616 * call_usermodehelper_exec().
618 int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
620 struct subprocess_info *info;
621 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
623 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
628 return call_usermodehelper_exec(info, wait);
630 EXPORT_SYMBOL(call_usermodehelper);
632 static int proc_cap_handler(struct ctl_table *table, int write,
633 void __user *buffer, size_t *lenp, loff_t *ppos)
636 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
637 kernel_cap_t new_cap;
640 if (write && (!capable(CAP_SETPCAP) ||
641 !capable(CAP_SYS_MODULE)))
645 * convert from the global kernel_cap_t to the ulong array to print to
646 * userspace if this is a read.
648 spin_lock(&umh_sysctl_lock);
649 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
650 if (table->data == CAP_BSET)
651 cap_array[i] = usermodehelper_bset.cap[i];
652 else if (table->data == CAP_PI)
653 cap_array[i] = usermodehelper_inheritable.cap[i];
657 spin_unlock(&umh_sysctl_lock);
663 * actually read or write and array of ulongs from userspace. Remember
664 * these are least significant 32 bits first
666 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
671 * convert from the sysctl array of ulongs to the kernel_cap_t
672 * internal representation
674 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
675 new_cap.cap[i] = cap_array[i];
678 * Drop everything not in the new_cap (but don't add things)
681 spin_lock(&umh_sysctl_lock);
682 if (table->data == CAP_BSET)
683 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
684 if (table->data == CAP_PI)
685 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
686 spin_unlock(&umh_sysctl_lock);
692 void __exit_umh(struct task_struct *tsk)
694 struct umh_info *info;
695 pid_t pid = tsk->pid;
697 mutex_lock(&umh_list_lock);
698 list_for_each_entry(info, &umh_list, list) {
699 if (info->pid == pid) {
700 list_del(&info->list);
701 mutex_unlock(&umh_list_lock);
705 mutex_unlock(&umh_list_lock);
712 struct ctl_table usermodehelper_table[] = {
716 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
718 .proc_handler = proc_cap_handler,
721 .procname = "inheritable",
723 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
725 .proc_handler = proc_cap_handler,