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/fs_struct.h>
18 #include <linux/workqueue.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/resource.h>
24 #include <linux/notifier.h>
25 #include <linux/suspend.h>
26 #include <linux/rwsem.h>
27 #include <linux/ptrace.h>
28 #include <linux/async.h>
29 #include <linux/uaccess.h>
30 #include <linux/shmem_fs.h>
31 #include <linux/pipe_fs_i.h>
33 #include <trace/events/module.h>
35 #define CAP_BSET (void *)1
36 #define CAP_PI (void *)2
38 static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
39 static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
40 static DEFINE_SPINLOCK(umh_sysctl_lock);
41 static DECLARE_RWSEM(umhelper_sem);
42 static LIST_HEAD(umh_list);
43 static DEFINE_MUTEX(umh_list_lock);
45 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
48 (*info->cleanup)(info);
52 static void umh_complete(struct subprocess_info *sub_info)
54 struct completion *comp = xchg(&sub_info->complete, NULL);
56 * See call_usermodehelper_exec(). If xchg() returns NULL
57 * we own sub_info, the UMH_KILLABLE caller has gone away
58 * or the caller used UMH_NO_WAIT.
63 call_usermodehelper_freeinfo(sub_info);
67 * This is the task which runs the usermode application
69 static int call_usermodehelper_exec_async(void *data)
71 struct subprocess_info *sub_info = data;
75 spin_lock_irq(¤t->sighand->siglock);
76 flush_signal_handlers(current, 1);
77 spin_unlock_irq(¤t->sighand->siglock);
80 * Initial kernel threads share ther FS with init, in order to
81 * get the init root directory. But we've now created a new
82 * thread that is going to execve a user process and has its own
83 * 'struct fs_struct'. Reset umask to the default.
85 current->fs->umask = 0022;
88 * Our parent (unbound workqueue) runs with elevated scheduling
89 * priority. Avoid propagating that into the userspace child.
91 set_user_nice(current, 0);
94 new = prepare_kernel_cred(current);
98 spin_lock(&umh_sysctl_lock);
99 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
100 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
101 new->cap_inheritable);
102 spin_unlock(&umh_sysctl_lock);
104 if (sub_info->init) {
105 retval = sub_info->init(sub_info, new);
114 sub_info->pid = task_pid_nr(current);
115 if (sub_info->file) {
116 retval = do_execve_file(sub_info->file,
117 sub_info->argv, sub_info->envp);
119 current->flags |= PF_UMH;
121 retval = do_execve(getname_kernel(sub_info->path),
122 (const char __user *const __user *)sub_info->argv,
123 (const char __user *const __user *)sub_info->envp);
125 sub_info->retval = retval;
127 * call_usermodehelper_exec_sync() will call umh_complete
130 if (!(sub_info->wait & UMH_WAIT_PROC))
131 umh_complete(sub_info);
137 /* Handles UMH_WAIT_PROC. */
138 static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
142 /* If SIGCLD is ignored kernel_wait4 won't populate the status. */
143 kernel_sigaction(SIGCHLD, SIG_DFL);
144 pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
146 sub_info->retval = pid;
150 * Normally it is bogus to call wait4() from in-kernel because
151 * wait4() wants to write the exit code to a userspace address.
152 * But call_usermodehelper_exec_sync() always runs as kernel
153 * thread (workqueue) and put_user() to a kernel address works
154 * OK for kernel threads, due to their having an mm_segment_t
155 * which spans the entire address space.
157 * Thus the __user pointer cast is valid here.
159 kernel_wait4(pid, (int __user *)&ret, 0, NULL);
162 * If ret is 0, either call_usermodehelper_exec_async failed and
163 * the real error code is already in sub_info->retval or
164 * sub_info->retval is 0 anyway, so don't mess with it then.
167 sub_info->retval = ret;
170 /* Restore default kernel sig handler */
171 kernel_sigaction(SIGCHLD, SIG_IGN);
173 umh_complete(sub_info);
177 * We need to create the usermodehelper kernel thread from a task that is affine
178 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
179 * inherit a widest affinity irrespective of call_usermodehelper() callers with
180 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
181 * usermodehelper targets to contend a busy CPU.
183 * Unbound workqueues provide such wide affinity and allow to block on
184 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
186 * Besides, workqueues provide the privilege level that caller might not have
187 * to perform the usermodehelper request.
190 static void call_usermodehelper_exec_work(struct work_struct *work)
192 struct subprocess_info *sub_info =
193 container_of(work, struct subprocess_info, work);
195 if (sub_info->wait & UMH_WAIT_PROC) {
196 call_usermodehelper_exec_sync(sub_info);
200 * Use CLONE_PARENT to reparent it to kthreadd; we do not
201 * want to pollute current->children, and we need a parent
202 * that always ignores SIGCHLD to ensure auto-reaping.
204 pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
205 CLONE_PARENT | SIGCHLD);
207 sub_info->retval = pid;
208 umh_complete(sub_info);
214 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
215 * (used for preventing user land processes from being created after the user
216 * land has been frozen during a system-wide hibernation or suspend operation).
217 * Should always be manipulated under umhelper_sem acquired for write.
219 static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
221 /* Number of helpers running */
222 static atomic_t running_helpers = ATOMIC_INIT(0);
225 * Wait queue head used by usermodehelper_disable() to wait for all running
228 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
231 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
234 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
237 * Time to wait for running_helpers to become zero before the setting of
238 * usermodehelper_disabled in usermodehelper_disable() fails
240 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
242 int usermodehelper_read_trylock(void)
247 down_read(&umhelper_sem);
249 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
251 if (!usermodehelper_disabled)
254 if (usermodehelper_disabled == UMH_DISABLED)
257 up_read(&umhelper_sem);
265 down_read(&umhelper_sem);
267 finish_wait(&usermodehelper_disabled_waitq, &wait);
270 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
272 long usermodehelper_read_lock_wait(long timeout)
279 down_read(&umhelper_sem);
281 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
282 TASK_UNINTERRUPTIBLE);
283 if (!usermodehelper_disabled)
286 up_read(&umhelper_sem);
288 timeout = schedule_timeout(timeout);
292 down_read(&umhelper_sem);
294 finish_wait(&usermodehelper_disabled_waitq, &wait);
297 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
299 void usermodehelper_read_unlock(void)
301 up_read(&umhelper_sem);
303 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
306 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
307 * @depth: New value to assign to usermodehelper_disabled.
309 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
310 * writing) and wakeup tasks waiting for it to change.
312 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
314 down_write(&umhelper_sem);
315 usermodehelper_disabled = depth;
316 wake_up(&usermodehelper_disabled_waitq);
317 up_write(&umhelper_sem);
321 * __usermodehelper_disable - Prevent new helpers from being started.
322 * @depth: New value to assign to usermodehelper_disabled.
324 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
326 int __usermodehelper_disable(enum umh_disable_depth depth)
333 down_write(&umhelper_sem);
334 usermodehelper_disabled = depth;
335 up_write(&umhelper_sem);
338 * From now on call_usermodehelper_exec() won't start any new
339 * helpers, so it is sufficient if running_helpers turns out to
340 * be zero at one point (it may be increased later, but that
343 retval = wait_event_timeout(running_helpers_waitq,
344 atomic_read(&running_helpers) == 0,
345 RUNNING_HELPERS_TIMEOUT);
349 __usermodehelper_set_disable_depth(UMH_ENABLED);
353 static void helper_lock(void)
355 atomic_inc(&running_helpers);
356 smp_mb__after_atomic();
359 static void helper_unlock(void)
361 if (atomic_dec_and_test(&running_helpers))
362 wake_up(&running_helpers_waitq);
366 * call_usermodehelper_setup - prepare to call a usermode helper
367 * @path: path to usermode executable
368 * @argv: arg vector for process
369 * @envp: environment for process
370 * @gfp_mask: gfp mask for memory allocation
371 * @cleanup: a cleanup function
372 * @init: an init function
373 * @data: arbitrary context sensitive data
375 * Returns either %NULL on allocation failure, or a subprocess_info
376 * structure. This should be passed to call_usermodehelper_exec to
377 * exec the process and free the structure.
379 * The init function is used to customize the helper process prior to
380 * exec. A non-zero return code causes the process to error out, exit,
381 * and return the failure to the calling process
383 * The cleanup function is just before ethe subprocess_info is about to
384 * be freed. This can be used for freeing the argv and envp. The
385 * Function must be runnable in either a process context or the
386 * context in which call_usermodehelper_exec is called.
388 struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
389 char **envp, gfp_t gfp_mask,
390 int (*init)(struct subprocess_info *info, struct cred *new),
391 void (*cleanup)(struct subprocess_info *info),
394 struct subprocess_info *sub_info;
395 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
399 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
401 #ifdef CONFIG_STATIC_USERMODEHELPER
402 sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
404 sub_info->path = path;
406 sub_info->argv = argv;
407 sub_info->envp = envp;
409 sub_info->cleanup = cleanup;
410 sub_info->init = init;
411 sub_info->data = data;
415 EXPORT_SYMBOL(call_usermodehelper_setup);
417 struct subprocess_info *call_usermodehelper_setup_file(struct file *file,
418 int (*init)(struct subprocess_info *info, struct cred *new),
419 void (*cleanup)(struct subprocess_info *info), void *data)
421 struct subprocess_info *sub_info;
422 struct umh_info *info = data;
423 const char *cmdline = (info->cmdline) ? info->cmdline : "usermodehelper";
425 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_KERNEL);
429 sub_info->argv = argv_split(GFP_KERNEL, cmdline, NULL);
430 if (!sub_info->argv) {
435 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
436 sub_info->path = "none";
437 sub_info->file = file;
438 sub_info->init = init;
439 sub_info->cleanup = cleanup;
440 sub_info->data = data;
444 static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
446 struct umh_info *umh_info = info->data;
447 struct file *from_umh[2];
448 struct file *to_umh[2];
451 /* create pipe to send data to umh */
452 err = create_pipe_files(to_umh, 0);
455 err = replace_fd(0, to_umh[0], 0);
462 /* create pipe to receive data from umh */
463 err = create_pipe_files(from_umh, 0);
466 replace_fd(0, NULL, 0);
469 err = replace_fd(1, from_umh[1], 0);
473 replace_fd(0, NULL, 0);
478 umh_info->pipe_to_umh = to_umh[1];
479 umh_info->pipe_from_umh = from_umh[0];
483 static void umh_clean_and_save_pid(struct subprocess_info *info)
485 struct umh_info *umh_info = info->data;
487 /* cleanup if umh_pipe_setup() was successful but exec failed */
488 if (info->pid && info->retval) {
489 fput(umh_info->pipe_to_umh);
490 fput(umh_info->pipe_from_umh);
493 argv_free(info->argv);
494 umh_info->pid = info->pid;
498 * fork_usermode_blob - fork a blob of bytes as a usermode process
499 * @data: a blob of bytes that can be do_execv-ed as a file
500 * @len: length of the blob
501 * @info: information about usermode process (shouldn't be NULL)
503 * If info->cmdline is set it will be used as command line for the
504 * user process, else "usermodehelper" is used.
506 * Returns either negative error or zero which indicates success
507 * in executing a blob of bytes as a usermode process. In such
508 * case 'struct umh_info *info' is populated with two pipes
509 * and a pid of the process. The caller is responsible for health
510 * check of the user process, killing it via pid, and closing the
511 * pipes when user process is no longer needed.
513 int fork_usermode_blob(void *data, size_t len, struct umh_info *info)
515 struct subprocess_info *sub_info;
521 file = shmem_kernel_file_setup("", len, 0);
523 return PTR_ERR(file);
525 written = kernel_write(file, data, len, &pos);
526 if (written != len) {
534 sub_info = call_usermodehelper_setup_file(file, umh_pipe_setup,
535 umh_clean_and_save_pid, info);
539 err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
541 mutex_lock(&umh_list_lock);
542 list_add(&info->list, &umh_list);
543 mutex_unlock(&umh_list_lock);
549 EXPORT_SYMBOL_GPL(fork_usermode_blob);
552 * call_usermodehelper_exec - start a usermode application
553 * @sub_info: information about the subprocessa
554 * @wait: wait for the application to finish and return status.
555 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
556 * when the program couldn't be exec'ed. This makes it safe to call
557 * from interrupt context.
559 * Runs a user-space application. The application is started
560 * asynchronously if wait is not set, and runs as a child of system workqueues.
561 * (ie. it runs with full root capabilities and optimized affinity).
563 * Note: successful return value does not guarantee the helper was called at
564 * all. You can't rely on sub_info->{init,cleanup} being called even for
565 * UMH_WAIT_* wait modes as STATIC_USERMODEHELPER_PATH="" turns all helpers
566 * into a successful no-op.
568 int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
570 DECLARE_COMPLETION_ONSTACK(done);
573 if (!sub_info->path) {
574 call_usermodehelper_freeinfo(sub_info);
578 if (usermodehelper_disabled) {
584 * If there is no binary for us to call, then just return and get out of
585 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
586 * disable all call_usermodehelper() calls.
588 if (strlen(sub_info->path) == 0)
592 * Set the completion pointer only if there is a waiter.
593 * This makes it possible to use umh_complete to free
594 * the data structure in case of UMH_NO_WAIT.
596 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
597 sub_info->wait = wait;
599 queue_work(system_unbound_wq, &sub_info->work);
600 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
603 if (wait & UMH_KILLABLE) {
604 retval = wait_for_completion_killable(&done);
608 /* umh_complete() will see NULL and free sub_info */
609 if (xchg(&sub_info->complete, NULL))
611 /* fallthrough, umh_complete() was already called */
614 wait_for_completion(&done);
616 retval = sub_info->retval;
618 call_usermodehelper_freeinfo(sub_info);
623 EXPORT_SYMBOL(call_usermodehelper_exec);
626 * call_usermodehelper() - prepare and start a usermode application
627 * @path: path to usermode executable
628 * @argv: arg vector for process
629 * @envp: environment for process
630 * @wait: wait for the application to finish and return status.
631 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
632 * when the program couldn't be exec'ed. This makes it safe to call
633 * from interrupt context.
635 * This function is the equivalent to use call_usermodehelper_setup() and
636 * call_usermodehelper_exec().
638 int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
640 struct subprocess_info *info;
641 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
643 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
648 return call_usermodehelper_exec(info, wait);
650 EXPORT_SYMBOL(call_usermodehelper);
652 static int proc_cap_handler(struct ctl_table *table, int write,
653 void __user *buffer, size_t *lenp, loff_t *ppos)
656 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
657 kernel_cap_t new_cap;
660 if (write && (!capable(CAP_SETPCAP) ||
661 !capable(CAP_SYS_MODULE)))
665 * convert from the global kernel_cap_t to the ulong array to print to
666 * userspace if this is a read.
668 spin_lock(&umh_sysctl_lock);
669 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
670 if (table->data == CAP_BSET)
671 cap_array[i] = usermodehelper_bset.cap[i];
672 else if (table->data == CAP_PI)
673 cap_array[i] = usermodehelper_inheritable.cap[i];
677 spin_unlock(&umh_sysctl_lock);
683 * actually read or write and array of ulongs from userspace. Remember
684 * these are least significant 32 bits first
686 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
691 * convert from the sysctl array of ulongs to the kernel_cap_t
692 * internal representation
694 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
695 new_cap.cap[i] = cap_array[i];
698 * Drop everything not in the new_cap (but don't add things)
701 spin_lock(&umh_sysctl_lock);
702 if (table->data == CAP_BSET)
703 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
704 if (table->data == CAP_PI)
705 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
706 spin_unlock(&umh_sysctl_lock);
712 void __exit_umh(struct task_struct *tsk)
714 struct umh_info *info;
715 pid_t pid = tsk->pid;
717 mutex_lock(&umh_list_lock);
718 list_for_each_entry(info, &umh_list, list) {
719 if (info->pid == pid) {
720 list_del(&info->list);
721 mutex_unlock(&umh_list_lock);
725 mutex_unlock(&umh_list_lock);
732 struct ctl_table usermodehelper_table[] = {
736 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
738 .proc_handler = proc_cap_handler,
741 .procname = "inheritable",
743 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
745 .proc_handler = proc_cap_handler,