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/initrd.h>
31 #include <linux/freezer.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);
43 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
46 (*info->cleanup)(info);
50 static void umh_complete(struct subprocess_info *sub_info)
52 struct completion *comp = xchg(&sub_info->complete, NULL);
54 * See call_usermodehelper_exec(). If xchg() returns NULL
55 * we own sub_info, the UMH_KILLABLE caller has gone away
56 * or the caller used UMH_NO_WAIT.
61 call_usermodehelper_freeinfo(sub_info);
65 * This is the task which runs the usermode application
67 static int call_usermodehelper_exec_async(void *data)
69 struct subprocess_info *sub_info = data;
73 spin_lock_irq(¤t->sighand->siglock);
74 flush_signal_handlers(current, 1);
75 spin_unlock_irq(¤t->sighand->siglock);
78 * Initial kernel threads share ther FS with init, in order to
79 * get the init root directory. But we've now created a new
80 * thread that is going to execve a user process and has its own
81 * 'struct fs_struct'. Reset umask to the default.
83 current->fs->umask = 0022;
86 * Our parent (unbound workqueue) runs with elevated scheduling
87 * priority. Avoid propagating that into the userspace child.
89 set_user_nice(current, 0);
92 new = prepare_kernel_cred(current);
96 spin_lock(&umh_sysctl_lock);
97 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
98 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
99 new->cap_inheritable);
100 spin_unlock(&umh_sysctl_lock);
102 if (sub_info->init) {
103 retval = sub_info->init(sub_info, new);
112 wait_for_initramfs();
113 retval = kernel_execve(sub_info->path,
114 (const char *const *)sub_info->argv,
115 (const char *const *)sub_info->envp);
117 sub_info->retval = retval;
119 * call_usermodehelper_exec_sync() will call umh_complete
122 if (!(sub_info->wait & UMH_WAIT_PROC))
123 umh_complete(sub_info);
129 /* Handles UMH_WAIT_PROC. */
130 static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
134 /* If SIGCLD is ignored do_wait won't populate the status. */
135 kernel_sigaction(SIGCHLD, SIG_DFL);
136 pid = user_mode_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
138 sub_info->retval = pid;
140 kernel_wait(pid, &sub_info->retval);
142 /* Restore default kernel sig handler */
143 kernel_sigaction(SIGCHLD, SIG_IGN);
144 umh_complete(sub_info);
148 * We need to create the usermodehelper kernel thread from a task that is affine
149 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
150 * inherit a widest affinity irrespective of call_usermodehelper() callers with
151 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
152 * usermodehelper targets to contend a busy CPU.
154 * Unbound workqueues provide such wide affinity and allow to block on
155 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
157 * Besides, workqueues provide the privilege level that caller might not have
158 * to perform the usermodehelper request.
161 static void call_usermodehelper_exec_work(struct work_struct *work)
163 struct subprocess_info *sub_info =
164 container_of(work, struct subprocess_info, work);
166 if (sub_info->wait & UMH_WAIT_PROC) {
167 call_usermodehelper_exec_sync(sub_info);
171 * Use CLONE_PARENT to reparent it to kthreadd; we do not
172 * want to pollute current->children, and we need a parent
173 * that always ignores SIGCHLD to ensure auto-reaping.
175 pid = user_mode_thread(call_usermodehelper_exec_async, sub_info,
176 CLONE_PARENT | SIGCHLD);
178 sub_info->retval = pid;
179 umh_complete(sub_info);
185 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
186 * (used for preventing user land processes from being created after the user
187 * land has been frozen during a system-wide hibernation or suspend operation).
188 * Should always be manipulated under umhelper_sem acquired for write.
190 static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
192 /* Number of helpers running */
193 static atomic_t running_helpers = ATOMIC_INIT(0);
196 * Wait queue head used by usermodehelper_disable() to wait for all running
199 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
202 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
205 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
208 * Time to wait for running_helpers to become zero before the setting of
209 * usermodehelper_disabled in usermodehelper_disable() fails
211 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
213 int usermodehelper_read_trylock(void)
218 down_read(&umhelper_sem);
220 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
222 if (!usermodehelper_disabled)
225 if (usermodehelper_disabled == UMH_DISABLED)
228 up_read(&umhelper_sem);
236 down_read(&umhelper_sem);
238 finish_wait(&usermodehelper_disabled_waitq, &wait);
241 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
243 long usermodehelper_read_lock_wait(long timeout)
250 down_read(&umhelper_sem);
252 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
253 TASK_UNINTERRUPTIBLE);
254 if (!usermodehelper_disabled)
257 up_read(&umhelper_sem);
259 timeout = schedule_timeout(timeout);
263 down_read(&umhelper_sem);
265 finish_wait(&usermodehelper_disabled_waitq, &wait);
268 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
270 void usermodehelper_read_unlock(void)
272 up_read(&umhelper_sem);
274 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
277 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
278 * @depth: New value to assign to usermodehelper_disabled.
280 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
281 * writing) and wakeup tasks waiting for it to change.
283 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
285 down_write(&umhelper_sem);
286 usermodehelper_disabled = depth;
287 wake_up(&usermodehelper_disabled_waitq);
288 up_write(&umhelper_sem);
292 * __usermodehelper_disable - Prevent new helpers from being started.
293 * @depth: New value to assign to usermodehelper_disabled.
295 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
297 int __usermodehelper_disable(enum umh_disable_depth depth)
304 down_write(&umhelper_sem);
305 usermodehelper_disabled = depth;
306 up_write(&umhelper_sem);
309 * From now on call_usermodehelper_exec() won't start any new
310 * helpers, so it is sufficient if running_helpers turns out to
311 * be zero at one point (it may be increased later, but that
314 retval = wait_event_timeout(running_helpers_waitq,
315 atomic_read(&running_helpers) == 0,
316 RUNNING_HELPERS_TIMEOUT);
320 __usermodehelper_set_disable_depth(UMH_ENABLED);
324 static void helper_lock(void)
326 atomic_inc(&running_helpers);
327 smp_mb__after_atomic();
330 static void helper_unlock(void)
332 if (atomic_dec_and_test(&running_helpers))
333 wake_up(&running_helpers_waitq);
337 * call_usermodehelper_setup - prepare to call a usermode helper
338 * @path: path to usermode executable
339 * @argv: arg vector for process
340 * @envp: environment for process
341 * @gfp_mask: gfp mask for memory allocation
342 * @init: an init function
343 * @cleanup: a cleanup function
344 * @data: arbitrary context sensitive data
346 * Returns either %NULL on allocation failure, or a subprocess_info
347 * structure. This should be passed to call_usermodehelper_exec to
348 * exec the process and free the structure.
350 * The init function is used to customize the helper process prior to
351 * exec. A non-zero return code causes the process to error out, exit,
352 * and return the failure to the calling process
354 * The cleanup function is just before the subprocess_info is about to
355 * be freed. This can be used for freeing the argv and envp. The
356 * Function must be runnable in either a process context or the
357 * context in which call_usermodehelper_exec is called.
359 struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
360 char **envp, gfp_t gfp_mask,
361 int (*init)(struct subprocess_info *info, struct cred *new),
362 void (*cleanup)(struct subprocess_info *info),
365 struct subprocess_info *sub_info;
366 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
370 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
372 #ifdef CONFIG_STATIC_USERMODEHELPER
373 sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
375 sub_info->path = path;
377 sub_info->argv = argv;
378 sub_info->envp = envp;
380 sub_info->cleanup = cleanup;
381 sub_info->init = init;
382 sub_info->data = data;
386 EXPORT_SYMBOL(call_usermodehelper_setup);
389 * call_usermodehelper_exec - start a usermode application
390 * @sub_info: information about the subprocess
391 * @wait: wait for the application to finish and return status.
392 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
393 * when the program couldn't be exec'ed. This makes it safe to call
394 * from interrupt context.
396 * Runs a user-space application. The application is started
397 * asynchronously if wait is not set, and runs as a child of system workqueues.
398 * (ie. it runs with full root capabilities and optimized affinity).
400 * Note: successful return value does not guarantee the helper was called at
401 * all. You can't rely on sub_info->{init,cleanup} being called even for
402 * UMH_WAIT_* wait modes as STATIC_USERMODEHELPER_PATH="" turns all helpers
403 * into a successful no-op.
405 int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
407 unsigned int state = TASK_UNINTERRUPTIBLE;
408 DECLARE_COMPLETION_ONSTACK(done);
411 if (!sub_info->path) {
412 call_usermodehelper_freeinfo(sub_info);
416 if (usermodehelper_disabled) {
422 * If there is no binary for us to call, then just return and get out of
423 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
424 * disable all call_usermodehelper() calls.
426 if (strlen(sub_info->path) == 0)
430 * Set the completion pointer only if there is a waiter.
431 * This makes it possible to use umh_complete to free
432 * the data structure in case of UMH_NO_WAIT.
434 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
435 sub_info->wait = wait;
437 queue_work(system_unbound_wq, &sub_info->work);
438 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
441 if (wait & UMH_KILLABLE)
442 state |= TASK_KILLABLE;
444 if (wait & UMH_FREEZABLE)
445 state |= TASK_FREEZABLE;
447 retval = wait_for_completion_state(&done, state);
451 if (wait & UMH_KILLABLE) {
452 /* umh_complete() will see NULL and free sub_info */
453 if (xchg(&sub_info->complete, NULL))
458 retval = sub_info->retval;
460 call_usermodehelper_freeinfo(sub_info);
465 EXPORT_SYMBOL(call_usermodehelper_exec);
468 * call_usermodehelper() - prepare and start a usermode application
469 * @path: path to usermode executable
470 * @argv: arg vector for process
471 * @envp: environment for process
472 * @wait: wait for the application to finish and return status.
473 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
474 * when the program couldn't be exec'ed. This makes it safe to call
475 * from interrupt context.
477 * This function is the equivalent to use call_usermodehelper_setup() and
478 * call_usermodehelper_exec().
480 int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
482 struct subprocess_info *info;
483 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
485 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
490 return call_usermodehelper_exec(info, wait);
492 EXPORT_SYMBOL(call_usermodehelper);
494 static int proc_cap_handler(struct ctl_table *table, int write,
495 void *buffer, size_t *lenp, loff_t *ppos)
498 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
499 kernel_cap_t new_cap;
502 if (write && (!capable(CAP_SETPCAP) ||
503 !capable(CAP_SYS_MODULE)))
507 * convert from the global kernel_cap_t to the ulong array to print to
508 * userspace if this is a read.
510 spin_lock(&umh_sysctl_lock);
511 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
512 if (table->data == CAP_BSET)
513 cap_array[i] = usermodehelper_bset.cap[i];
514 else if (table->data == CAP_PI)
515 cap_array[i] = usermodehelper_inheritable.cap[i];
519 spin_unlock(&umh_sysctl_lock);
525 * actually read or write and array of ulongs from userspace. Remember
526 * these are least significant 32 bits first
528 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
533 * convert from the sysctl array of ulongs to the kernel_cap_t
534 * internal representation
536 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
537 new_cap.cap[i] = cap_array[i];
540 * Drop everything not in the new_cap (but don't add things)
543 spin_lock(&umh_sysctl_lock);
544 if (table->data == CAP_BSET)
545 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
546 if (table->data == CAP_PI)
547 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
548 spin_unlock(&umh_sysctl_lock);
554 struct ctl_table usermodehelper_table[] = {
558 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
560 .proc_handler = proc_cap_handler,
563 .procname = "inheritable",
565 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
567 .proc_handler = proc_cap_handler,