2 * umh - the kernel usermode helper
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/sched/task.h>
7 #include <linux/binfmts.h>
8 #include <linux/syscalls.h>
9 #include <linux/unistd.h>
10 #include <linux/kmod.h>
11 #include <linux/slab.h>
12 #include <linux/completion.h>
13 #include <linux/cred.h>
14 #include <linux/file.h>
15 #include <linux/fdtable.h>
16 #include <linux/fs_struct.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>
30 #include <trace/events/module.h>
32 #define CAP_BSET (void *)1
33 #define CAP_PI (void *)2
35 static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
36 static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
37 static DEFINE_SPINLOCK(umh_sysctl_lock);
38 static DECLARE_RWSEM(umhelper_sem);
40 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
43 (*info->cleanup)(info);
47 static void umh_complete(struct subprocess_info *sub_info)
49 struct completion *comp = xchg(&sub_info->complete, NULL);
51 * See call_usermodehelper_exec(). If xchg() returns NULL
52 * we own sub_info, the UMH_KILLABLE caller has gone away
53 * or the caller used UMH_NO_WAIT.
58 call_usermodehelper_freeinfo(sub_info);
62 * This is the task which runs the usermode application
64 static int call_usermodehelper_exec_async(void *data)
66 struct subprocess_info *sub_info = data;
70 spin_lock_irq(¤t->sighand->siglock);
71 flush_signal_handlers(current, 1);
72 spin_unlock_irq(¤t->sighand->siglock);
75 * Initial kernel threads share ther FS with init, in order to
76 * get the init root directory. But we've now created a new
77 * thread that is going to execve a user process and has its own
78 * 'struct fs_struct'. Reset umask to the default.
80 current->fs->umask = 0022;
83 * Our parent (unbound workqueue) runs with elevated scheduling
84 * priority. Avoid propagating that into the userspace child.
86 set_user_nice(current, 0);
89 new = prepare_kernel_cred(current);
93 spin_lock(&umh_sysctl_lock);
94 new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
95 new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
96 new->cap_inheritable);
97 spin_unlock(&umh_sysctl_lock);
100 retval = sub_info->init(sub_info, new);
109 retval = do_execve(getname_kernel(sub_info->path),
110 (const char __user *const __user *)sub_info->argv,
111 (const char __user *const __user *)sub_info->envp);
113 sub_info->retval = retval;
115 * call_usermodehelper_exec_sync() will call umh_complete
118 if (!(sub_info->wait & UMH_WAIT_PROC))
119 umh_complete(sub_info);
125 /* Handles UMH_WAIT_PROC. */
126 static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
130 /* If SIGCLD is ignored sys_wait4 won't populate the status. */
131 kernel_sigaction(SIGCHLD, SIG_DFL);
132 pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
134 sub_info->retval = pid;
138 * Normally it is bogus to call wait4() from in-kernel because
139 * wait4() wants to write the exit code to a userspace address.
140 * But call_usermodehelper_exec_sync() always runs as kernel
141 * thread (workqueue) and put_user() to a kernel address works
142 * OK for kernel threads, due to their having an mm_segment_t
143 * which spans the entire address space.
145 * Thus the __user pointer cast is valid here.
147 sys_wait4(pid, (int __user *)&ret, 0, NULL);
150 * If ret is 0, either call_usermodehelper_exec_async failed and
151 * the real error code is already in sub_info->retval or
152 * sub_info->retval is 0 anyway, so don't mess with it then.
155 sub_info->retval = ret;
158 /* Restore default kernel sig handler */
159 kernel_sigaction(SIGCHLD, SIG_IGN);
161 umh_complete(sub_info);
165 * We need to create the usermodehelper kernel thread from a task that is affine
166 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
167 * inherit a widest affinity irrespective of call_usermodehelper() callers with
168 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
169 * usermodehelper targets to contend a busy CPU.
171 * Unbound workqueues provide such wide affinity and allow to block on
172 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
174 * Besides, workqueues provide the privilege level that caller might not have
175 * to perform the usermodehelper request.
178 static void call_usermodehelper_exec_work(struct work_struct *work)
180 struct subprocess_info *sub_info =
181 container_of(work, struct subprocess_info, work);
183 if (sub_info->wait & UMH_WAIT_PROC) {
184 call_usermodehelper_exec_sync(sub_info);
188 * Use CLONE_PARENT to reparent it to kthreadd; we do not
189 * want to pollute current->children, and we need a parent
190 * that always ignores SIGCHLD to ensure auto-reaping.
192 pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
193 CLONE_PARENT | SIGCHLD);
195 sub_info->retval = pid;
196 umh_complete(sub_info);
202 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
203 * (used for preventing user land processes from being created after the user
204 * land has been frozen during a system-wide hibernation or suspend operation).
205 * Should always be manipulated under umhelper_sem acquired for write.
207 static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
209 /* Number of helpers running */
210 static atomic_t running_helpers = ATOMIC_INIT(0);
213 * Wait queue head used by usermodehelper_disable() to wait for all running
216 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
219 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
222 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
225 * Time to wait for running_helpers to become zero before the setting of
226 * usermodehelper_disabled in usermodehelper_disable() fails
228 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
230 int usermodehelper_read_trylock(void)
235 down_read(&umhelper_sem);
237 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
239 if (!usermodehelper_disabled)
242 if (usermodehelper_disabled == UMH_DISABLED)
245 up_read(&umhelper_sem);
253 down_read(&umhelper_sem);
255 finish_wait(&usermodehelper_disabled_waitq, &wait);
258 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
260 long usermodehelper_read_lock_wait(long timeout)
267 down_read(&umhelper_sem);
269 prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
270 TASK_UNINTERRUPTIBLE);
271 if (!usermodehelper_disabled)
274 up_read(&umhelper_sem);
276 timeout = schedule_timeout(timeout);
280 down_read(&umhelper_sem);
282 finish_wait(&usermodehelper_disabled_waitq, &wait);
285 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
287 void usermodehelper_read_unlock(void)
289 up_read(&umhelper_sem);
291 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
294 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
295 * @depth: New value to assign to usermodehelper_disabled.
297 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
298 * writing) and wakeup tasks waiting for it to change.
300 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
302 down_write(&umhelper_sem);
303 usermodehelper_disabled = depth;
304 wake_up(&usermodehelper_disabled_waitq);
305 up_write(&umhelper_sem);
309 * __usermodehelper_disable - Prevent new helpers from being started.
310 * @depth: New value to assign to usermodehelper_disabled.
312 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
314 int __usermodehelper_disable(enum umh_disable_depth depth)
321 down_write(&umhelper_sem);
322 usermodehelper_disabled = depth;
323 up_write(&umhelper_sem);
326 * From now on call_usermodehelper_exec() won't start any new
327 * helpers, so it is sufficient if running_helpers turns out to
328 * be zero at one point (it may be increased later, but that
331 retval = wait_event_timeout(running_helpers_waitq,
332 atomic_read(&running_helpers) == 0,
333 RUNNING_HELPERS_TIMEOUT);
337 __usermodehelper_set_disable_depth(UMH_ENABLED);
341 static void helper_lock(void)
343 atomic_inc(&running_helpers);
344 smp_mb__after_atomic();
347 static void helper_unlock(void)
349 if (atomic_dec_and_test(&running_helpers))
350 wake_up(&running_helpers_waitq);
354 * call_usermodehelper_setup - prepare to call a usermode helper
355 * @path: path to usermode executable
356 * @argv: arg vector for process
357 * @envp: environment for process
358 * @gfp_mask: gfp mask for memory allocation
359 * @cleanup: a cleanup function
360 * @init: an init function
361 * @data: arbitrary context sensitive data
363 * Returns either %NULL on allocation failure, or a subprocess_info
364 * structure. This should be passed to call_usermodehelper_exec to
365 * exec the process and free the structure.
367 * The init function is used to customize the helper process prior to
368 * exec. A non-zero return code causes the process to error out, exit,
369 * and return the failure to the calling process
371 * The cleanup function is just before ethe subprocess_info is about to
372 * be freed. This can be used for freeing the argv and envp. The
373 * Function must be runnable in either a process context or the
374 * context in which call_usermodehelper_exec is called.
376 struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
377 char **envp, gfp_t gfp_mask,
378 int (*init)(struct subprocess_info *info, struct cred *new),
379 void (*cleanup)(struct subprocess_info *info),
382 struct subprocess_info *sub_info;
383 sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
387 INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
389 #ifdef CONFIG_STATIC_USERMODEHELPER
390 sub_info->path = CONFIG_STATIC_USERMODEHELPER_PATH;
392 sub_info->path = path;
394 sub_info->argv = argv;
395 sub_info->envp = envp;
397 sub_info->cleanup = cleanup;
398 sub_info->init = init;
399 sub_info->data = data;
403 EXPORT_SYMBOL(call_usermodehelper_setup);
406 * call_usermodehelper_exec - start a usermode application
407 * @sub_info: information about the subprocessa
408 * @wait: wait for the application to finish and return status.
409 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
410 * when the program couldn't be exec'ed. This makes it safe to call
411 * from interrupt context.
413 * Runs a user-space application. The application is started
414 * asynchronously if wait is not set, and runs as a child of system workqueues.
415 * (ie. it runs with full root capabilities and optimized affinity).
417 * Note: successful return value does not guarantee the helper was called at
418 * all. You can't rely on sub_info->{init,cleanup} being called even for
419 * UMH_WAIT_* wait modes as STATIC_USERMODEHELPER_PATH="" turns all helpers
420 * into a successful no-op.
422 int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
424 DECLARE_COMPLETION_ONSTACK(done);
427 if (!sub_info->path) {
428 call_usermodehelper_freeinfo(sub_info);
432 if (usermodehelper_disabled) {
438 * If there is no binary for us to call, then just return and get out of
439 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
440 * disable all call_usermodehelper() calls.
442 if (strlen(sub_info->path) == 0)
446 * Set the completion pointer only if there is a waiter.
447 * This makes it possible to use umh_complete to free
448 * the data structure in case of UMH_NO_WAIT.
450 sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
451 sub_info->wait = wait;
453 queue_work(system_unbound_wq, &sub_info->work);
454 if (wait == UMH_NO_WAIT) /* task has freed sub_info */
457 if (wait & UMH_KILLABLE) {
458 retval = wait_for_completion_killable(&done);
462 /* umh_complete() will see NULL and free sub_info */
463 if (xchg(&sub_info->complete, NULL))
465 /* fallthrough, umh_complete() was already called */
468 wait_for_completion(&done);
470 retval = sub_info->retval;
472 call_usermodehelper_freeinfo(sub_info);
477 EXPORT_SYMBOL(call_usermodehelper_exec);
480 * call_usermodehelper() - prepare and start a usermode application
481 * @path: path to usermode executable
482 * @argv: arg vector for process
483 * @envp: environment for process
484 * @wait: wait for the application to finish and return status.
485 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
486 * when the program couldn't be exec'ed. This makes it safe to call
487 * from interrupt context.
489 * This function is the equivalent to use call_usermodehelper_setup() and
490 * call_usermodehelper_exec().
492 int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
494 struct subprocess_info *info;
495 gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
497 info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
502 return call_usermodehelper_exec(info, wait);
504 EXPORT_SYMBOL(call_usermodehelper);
506 static int proc_cap_handler(struct ctl_table *table, int write,
507 void __user *buffer, size_t *lenp, loff_t *ppos)
510 unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
511 kernel_cap_t new_cap;
514 if (write && (!capable(CAP_SETPCAP) ||
515 !capable(CAP_SYS_MODULE)))
519 * convert from the global kernel_cap_t to the ulong array to print to
520 * userspace if this is a read.
522 spin_lock(&umh_sysctl_lock);
523 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
524 if (table->data == CAP_BSET)
525 cap_array[i] = usermodehelper_bset.cap[i];
526 else if (table->data == CAP_PI)
527 cap_array[i] = usermodehelper_inheritable.cap[i];
531 spin_unlock(&umh_sysctl_lock);
537 * actually read or write and array of ulongs from userspace. Remember
538 * these are least significant 32 bits first
540 err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
545 * convert from the sysctl array of ulongs to the kernel_cap_t
546 * internal representation
548 for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
549 new_cap.cap[i] = cap_array[i];
552 * Drop everything not in the new_cap (but don't add things)
554 spin_lock(&umh_sysctl_lock);
556 if (table->data == CAP_BSET)
557 usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
558 if (table->data == CAP_PI)
559 usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
561 spin_unlock(&umh_sysctl_lock);
566 struct ctl_table usermodehelper_table[] = {
570 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
572 .proc_handler = proc_cap_handler,
575 .procname = "inheritable",
577 .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
579 .proc_handler = proc_cap_handler,