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>
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 * Our parent (unbound workqueue) runs with elevated scheduling
76 * priority. Avoid propagating that into the userspace child.
78 set_user_nice(current
, 0);
81 new = prepare_kernel_cred(current
);
85 spin_lock(&umh_sysctl_lock
);
86 new->cap_bset
= cap_intersect(usermodehelper_bset
, new->cap_bset
);
87 new->cap_inheritable
= cap_intersect(usermodehelper_inheritable
,
88 new->cap_inheritable
);
89 spin_unlock(&umh_sysctl_lock
);
92 retval
= sub_info
->init(sub_info
, new);
101 retval
= kernel_execve(sub_info
->path
,
102 (const char *const *)sub_info
->argv
,
103 (const char *const *)sub_info
->envp
);
105 sub_info
->retval
= retval
;
107 * call_usermodehelper_exec_sync() will call umh_complete
110 if (!(sub_info
->wait
& UMH_WAIT_PROC
))
111 umh_complete(sub_info
);
117 /* Handles UMH_WAIT_PROC. */
118 static void call_usermodehelper_exec_sync(struct subprocess_info
*sub_info
)
122 /* If SIGCLD is ignored kernel_wait4 won't populate the status. */
123 kernel_sigaction(SIGCHLD
, SIG_DFL
);
124 pid
= kernel_thread(call_usermodehelper_exec_async
, sub_info
, SIGCHLD
);
126 sub_info
->retval
= pid
;
130 * Normally it is bogus to call wait4() from in-kernel because
131 * wait4() wants to write the exit code to a userspace address.
132 * But call_usermodehelper_exec_sync() always runs as kernel
133 * thread (workqueue) and put_user() to a kernel address works
134 * OK for kernel threads, due to their having an mm_segment_t
135 * which spans the entire address space.
137 * Thus the __user pointer cast is valid here.
139 kernel_wait4(pid
, (int __user
*)&ret
, 0, NULL
);
142 * If ret is 0, either call_usermodehelper_exec_async failed and
143 * the real error code is already in sub_info->retval or
144 * sub_info->retval is 0 anyway, so don't mess with it then.
147 sub_info
->retval
= ret
;
150 /* Restore default kernel sig handler */
151 kernel_sigaction(SIGCHLD
, SIG_IGN
);
153 umh_complete(sub_info
);
157 * We need to create the usermodehelper kernel thread from a task that is affine
158 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
159 * inherit a widest affinity irrespective of call_usermodehelper() callers with
160 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
161 * usermodehelper targets to contend a busy CPU.
163 * Unbound workqueues provide such wide affinity and allow to block on
164 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
166 * Besides, workqueues provide the privilege level that caller might not have
167 * to perform the usermodehelper request.
170 static void call_usermodehelper_exec_work(struct work_struct
*work
)
172 struct subprocess_info
*sub_info
=
173 container_of(work
, struct subprocess_info
, work
);
175 if (sub_info
->wait
& UMH_WAIT_PROC
) {
176 call_usermodehelper_exec_sync(sub_info
);
180 * Use CLONE_PARENT to reparent it to kthreadd; we do not
181 * want to pollute current->children, and we need a parent
182 * that always ignores SIGCHLD to ensure auto-reaping.
184 pid
= kernel_thread(call_usermodehelper_exec_async
, sub_info
,
185 CLONE_PARENT
| SIGCHLD
);
187 sub_info
->retval
= pid
;
188 umh_complete(sub_info
);
194 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
195 * (used for preventing user land processes from being created after the user
196 * land has been frozen during a system-wide hibernation or suspend operation).
197 * Should always be manipulated under umhelper_sem acquired for write.
199 static enum umh_disable_depth usermodehelper_disabled
= UMH_DISABLED
;
201 /* Number of helpers running */
202 static atomic_t running_helpers
= ATOMIC_INIT(0);
205 * Wait queue head used by usermodehelper_disable() to wait for all running
208 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq
);
211 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
214 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq
);
217 * Time to wait for running_helpers to become zero before the setting of
218 * usermodehelper_disabled in usermodehelper_disable() fails
220 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
222 int usermodehelper_read_trylock(void)
227 down_read(&umhelper_sem
);
229 prepare_to_wait(&usermodehelper_disabled_waitq
, &wait
,
231 if (!usermodehelper_disabled
)
234 if (usermodehelper_disabled
== UMH_DISABLED
)
237 up_read(&umhelper_sem
);
245 down_read(&umhelper_sem
);
247 finish_wait(&usermodehelper_disabled_waitq
, &wait
);
250 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock
);
252 long usermodehelper_read_lock_wait(long timeout
)
259 down_read(&umhelper_sem
);
261 prepare_to_wait(&usermodehelper_disabled_waitq
, &wait
,
262 TASK_UNINTERRUPTIBLE
);
263 if (!usermodehelper_disabled
)
266 up_read(&umhelper_sem
);
268 timeout
= schedule_timeout(timeout
);
272 down_read(&umhelper_sem
);
274 finish_wait(&usermodehelper_disabled_waitq
, &wait
);
277 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait
);
279 void usermodehelper_read_unlock(void)
281 up_read(&umhelper_sem
);
283 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock
);
286 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
287 * @depth: New value to assign to usermodehelper_disabled.
289 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
290 * writing) and wakeup tasks waiting for it to change.
292 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth
)
294 down_write(&umhelper_sem
);
295 usermodehelper_disabled
= depth
;
296 wake_up(&usermodehelper_disabled_waitq
);
297 up_write(&umhelper_sem
);
301 * __usermodehelper_disable - Prevent new helpers from being started.
302 * @depth: New value to assign to usermodehelper_disabled.
304 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
306 int __usermodehelper_disable(enum umh_disable_depth depth
)
313 down_write(&umhelper_sem
);
314 usermodehelper_disabled
= depth
;
315 up_write(&umhelper_sem
);
318 * From now on call_usermodehelper_exec() won't start any new
319 * helpers, so it is sufficient if running_helpers turns out to
320 * be zero at one point (it may be increased later, but that
323 retval
= wait_event_timeout(running_helpers_waitq
,
324 atomic_read(&running_helpers
) == 0,
325 RUNNING_HELPERS_TIMEOUT
);
329 __usermodehelper_set_disable_depth(UMH_ENABLED
);
333 static void helper_lock(void)
335 atomic_inc(&running_helpers
);
336 smp_mb__after_atomic();
339 static void helper_unlock(void)
341 if (atomic_dec_and_test(&running_helpers
))
342 wake_up(&running_helpers_waitq
);
346 * call_usermodehelper_setup - prepare to call a usermode helper
347 * @path: path to usermode executable
348 * @argv: arg vector for process
349 * @envp: environment for process
350 * @gfp_mask: gfp mask for memory allocation
351 * @cleanup: a cleanup function
352 * @init: an init function
353 * @data: arbitrary context sensitive data
355 * Returns either %NULL on allocation failure, or a subprocess_info
356 * structure. This should be passed to call_usermodehelper_exec to
357 * exec the process and free the structure.
359 * The init function is used to customize the helper process prior to
360 * exec. A non-zero return code causes the process to error out, exit,
361 * and return the failure to the calling process
363 * The cleanup function is just before ethe subprocess_info is about to
364 * be freed. This can be used for freeing the argv and envp. The
365 * Function must be runnable in either a process context or the
366 * context in which call_usermodehelper_exec is called.
368 struct subprocess_info
*call_usermodehelper_setup(const char *path
, char **argv
,
369 char **envp
, gfp_t gfp_mask
,
370 int (*init
)(struct subprocess_info
*info
, struct cred
*new),
371 void (*cleanup
)(struct subprocess_info
*info
),
374 struct subprocess_info
*sub_info
;
375 sub_info
= kzalloc(sizeof(struct subprocess_info
), gfp_mask
);
379 INIT_WORK(&sub_info
->work
, call_usermodehelper_exec_work
);
381 #ifdef CONFIG_STATIC_USERMODEHELPER
382 sub_info
->path
= CONFIG_STATIC_USERMODEHELPER_PATH
;
384 sub_info
->path
= path
;
386 sub_info
->argv
= argv
;
387 sub_info
->envp
= envp
;
389 sub_info
->cleanup
= cleanup
;
390 sub_info
->init
= init
;
391 sub_info
->data
= data
;
395 EXPORT_SYMBOL(call_usermodehelper_setup
);
398 * call_usermodehelper_exec - start a usermode application
399 * @sub_info: information about the subprocessa
400 * @wait: wait for the application to finish and return status.
401 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
402 * when the program couldn't be exec'ed. This makes it safe to call
403 * from interrupt context.
405 * Runs a user-space application. The application is started
406 * asynchronously if wait is not set, and runs as a child of system workqueues.
407 * (ie. it runs with full root capabilities and optimized affinity).
409 * Note: successful return value does not guarantee the helper was called at
410 * all. You can't rely on sub_info->{init,cleanup} being called even for
411 * UMH_WAIT_* wait modes as STATIC_USERMODEHELPER_PATH="" turns all helpers
412 * into a successful no-op.
414 int call_usermodehelper_exec(struct subprocess_info
*sub_info
, int wait
)
416 DECLARE_COMPLETION_ONSTACK(done
);
419 if (!sub_info
->path
) {
420 call_usermodehelper_freeinfo(sub_info
);
424 if (usermodehelper_disabled
) {
430 * If there is no binary for us to call, then just return and get out of
431 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
432 * disable all call_usermodehelper() calls.
434 if (strlen(sub_info
->path
) == 0)
438 * Set the completion pointer only if there is a waiter.
439 * This makes it possible to use umh_complete to free
440 * the data structure in case of UMH_NO_WAIT.
442 sub_info
->complete
= (wait
== UMH_NO_WAIT
) ? NULL
: &done
;
443 sub_info
->wait
= wait
;
445 queue_work(system_unbound_wq
, &sub_info
->work
);
446 if (wait
== UMH_NO_WAIT
) /* task has freed sub_info */
449 if (wait
& UMH_KILLABLE
) {
450 retval
= wait_for_completion_killable(&done
);
454 /* umh_complete() will see NULL and free sub_info */
455 if (xchg(&sub_info
->complete
, NULL
))
457 /* fallthrough, umh_complete() was already called */
460 wait_for_completion(&done
);
462 retval
= sub_info
->retval
;
464 call_usermodehelper_freeinfo(sub_info
);
469 EXPORT_SYMBOL(call_usermodehelper_exec
);
472 * call_usermodehelper() - prepare and start a usermode application
473 * @path: path to usermode executable
474 * @argv: arg vector for process
475 * @envp: environment for process
476 * @wait: wait for the application to finish and return status.
477 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
478 * when the program couldn't be exec'ed. This makes it safe to call
479 * from interrupt context.
481 * This function is the equivalent to use call_usermodehelper_setup() and
482 * call_usermodehelper_exec().
484 int call_usermodehelper(const char *path
, char **argv
, char **envp
, int wait
)
486 struct subprocess_info
*info
;
487 gfp_t gfp_mask
= (wait
== UMH_NO_WAIT
) ? GFP_ATOMIC
: GFP_KERNEL
;
489 info
= call_usermodehelper_setup(path
, argv
, envp
, gfp_mask
,
494 return call_usermodehelper_exec(info
, wait
);
496 EXPORT_SYMBOL(call_usermodehelper
);
498 static int proc_cap_handler(struct ctl_table
*table
, int write
,
499 void *buffer
, size_t *lenp
, loff_t
*ppos
)
502 unsigned long cap_array
[_KERNEL_CAPABILITY_U32S
];
503 kernel_cap_t new_cap
;
506 if (write
&& (!capable(CAP_SETPCAP
) ||
507 !capable(CAP_SYS_MODULE
)))
511 * convert from the global kernel_cap_t to the ulong array to print to
512 * userspace if this is a read.
514 spin_lock(&umh_sysctl_lock
);
515 for (i
= 0; i
< _KERNEL_CAPABILITY_U32S
; i
++) {
516 if (table
->data
== CAP_BSET
)
517 cap_array
[i
] = usermodehelper_bset
.cap
[i
];
518 else if (table
->data
== CAP_PI
)
519 cap_array
[i
] = usermodehelper_inheritable
.cap
[i
];
523 spin_unlock(&umh_sysctl_lock
);
529 * actually read or write and array of ulongs from userspace. Remember
530 * these are least significant 32 bits first
532 err
= proc_doulongvec_minmax(&t
, write
, buffer
, lenp
, ppos
);
537 * convert from the sysctl array of ulongs to the kernel_cap_t
538 * internal representation
540 for (i
= 0; i
< _KERNEL_CAPABILITY_U32S
; i
++)
541 new_cap
.cap
[i
] = cap_array
[i
];
544 * Drop everything not in the new_cap (but don't add things)
547 spin_lock(&umh_sysctl_lock
);
548 if (table
->data
== CAP_BSET
)
549 usermodehelper_bset
= cap_intersect(usermodehelper_bset
, new_cap
);
550 if (table
->data
== CAP_PI
)
551 usermodehelper_inheritable
= cap_intersect(usermodehelper_inheritable
, new_cap
);
552 spin_unlock(&umh_sysctl_lock
);
558 struct ctl_table usermodehelper_table
[] = {
562 .maxlen
= _KERNEL_CAPABILITY_U32S
* sizeof(unsigned long),
564 .proc_handler
= proc_cap_handler
,
567 .procname
= "inheritable",
569 .maxlen
= _KERNEL_CAPABILITY_U32S
* sizeof(unsigned long),
571 .proc_handler
= proc_cap_handler
,