2 kmod, the new module loader (replaces kerneld)
5 Reorganized not to be a daemon by Adam Richter, with guidance
8 Modified to avoid chroot and file sharing problems.
11 Limit the concurrent number of kmod modprobes to catch loops from
12 "modprobe needs a service that is in a module".
13 Keith Owens <kaos@ocs.com.au> December 1999
15 Unblock all signals when we exec a usermode process.
16 Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
18 call_usermodehelper wait flag, and remove exec_usermodehelper.
19 Rusty Russell <rusty@rustcorp.com.au> Jan 2003
21 #include <linux/module.h>
22 #include <linux/sched.h>
23 #include <linux/sched/task.h>
24 #include <linux/binfmts.h>
25 #include <linux/syscalls.h>
26 #include <linux/unistd.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
29 #include <linux/completion.h>
30 #include <linux/cred.h>
31 #include <linux/file.h>
32 #include <linux/fdtable.h>
33 #include <linux/workqueue.h>
34 #include <linux/security.h>
35 #include <linux/mount.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/resource.h>
39 #include <linux/notifier.h>
40 #include <linux/suspend.h>
41 #include <linux/rwsem.h>
42 #include <linux/ptrace.h>
43 #include <linux/async.h>
44 #include <linux/uaccess.h>
46 #include <trace/events/module.h>
48 #define CAP_BSET (void *)1
49 #define CAP_PI (void *)2
51 static kernel_cap_t usermodehelper_bset
= CAP_FULL_SET
;
52 static kernel_cap_t usermodehelper_inheritable
= CAP_FULL_SET
;
53 static DEFINE_SPINLOCK(umh_sysctl_lock
);
54 static DECLARE_RWSEM(umhelper_sem
);
60 * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
61 * (u64) THREAD_SIZE * 8UL);
63 * If you need less than 50 threads would mean we're dealing with systems
64 * smaller than 3200 pages. This assuems you are capable of having ~13M memory,
65 * and this would only be an be an upper limit, after which the OOM killer
66 * would take effect. Systems like these are very unlikely if modules are
69 #define MAX_KMOD_CONCURRENT 50
70 static atomic_t kmod_concurrent_max
= ATOMIC_INIT(MAX_KMOD_CONCURRENT
);
71 static DECLARE_WAIT_QUEUE_HEAD(kmod_wq
);
74 * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads
75 * running at the same time without returning. When this happens we
76 * believe you've somehow ended up with a recursive module dependency
79 * We have no option but to fail.
81 * Userspace should proactively try to detect and prevent these.
83 #define MAX_KMOD_ALL_BUSY_TIMEOUT 5
86 modprobe_path is set via /proc/sys.
88 char modprobe_path
[KMOD_PATH_LEN
] = "/sbin/modprobe";
90 static void free_modprobe_argv(struct subprocess_info
*info
)
92 kfree(info
->argv
[3]); /* check call_modprobe() */
96 static int call_modprobe(char *module_name
, int wait
)
98 struct subprocess_info
*info
;
99 static char *envp
[] = {
102 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
106 printk("call_modprobe: %s %i \n", module_name
, wait
);
108 char **argv
= kmalloc(sizeof(char *[5]), GFP_KERNEL
);
112 module_name
= kstrdup(module_name
, GFP_KERNEL
);
116 argv
[0] = modprobe_path
;
119 argv
[3] = module_name
; /* check free_modprobe_argv() */
122 info
= call_usermodehelper_setup(modprobe_path
, argv
, envp
, GFP_KERNEL
,
123 NULL
, free_modprobe_argv
, NULL
);
125 goto free_module_name
;
127 return call_usermodehelper_exec(info
, wait
| UMH_KILLABLE
);
138 * __request_module - try to load a kernel module
139 * @wait: wait (or not) for the operation to complete
140 * @fmt: printf style format string for the name of the module
141 * @...: arguments as specified in the format string
143 * Load a module using the user mode module loader. The function returns
144 * zero on success or a negative errno code or positive exit code from
145 * "modprobe" on failure. Note that a successful module load does not mean
146 * the module did not then unload and exit on an error of its own. Callers
147 * must check that the service they requested is now available not blindly
150 * If module auto-loading support is disabled then this function
151 * becomes a no-operation.
153 int __request_module(bool wait
, const char *fmt
, ...)
156 char module_name
[MODULE_NAME_LEN
];
160 * We don't allow synchronous module loading from async. Module
161 * init may invoke async_synchronize_full() which will end up
162 * waiting for this task which already is waiting for the module
163 * loading to complete, leading to a deadlock.
165 WARN_ON_ONCE(wait
&& current_is_async());
167 if (!modprobe_path
[0])
171 ret
= vsnprintf(module_name
, MODULE_NAME_LEN
, fmt
, args
);
173 if (ret
>= MODULE_NAME_LEN
)
174 return -ENAMETOOLONG
;
176 ret
= security_kernel_module_request(module_name
);
180 if (atomic_dec_if_positive(&kmod_concurrent_max
) < 0) {
181 pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
182 atomic_read(&kmod_concurrent_max
),
183 MAX_KMOD_CONCURRENT
, module_name
);
184 ret
= wait_event_killable_timeout(kmod_wq
,
185 atomic_dec_if_positive(&kmod_concurrent_max
) >= 0,
186 MAX_KMOD_ALL_BUSY_TIMEOUT
* HZ
);
188 pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now",
189 module_name
, MAX_KMOD_CONCURRENT
, MAX_KMOD_ALL_BUSY_TIMEOUT
);
191 } else if (ret
== -ERESTARTSYS
) {
192 pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name
);
197 trace_module_request(module_name
, wait
, _RET_IP_
);
199 ret
= call_modprobe(module_name
, wait
? UMH_WAIT_PROC
: UMH_WAIT_EXEC
);
201 atomic_inc(&kmod_concurrent_max
);
206 EXPORT_SYMBOL(__request_module
);
208 #endif /* CONFIG_MODULES */
210 static void call_usermodehelper_freeinfo(struct subprocess_info
*info
)
213 (*info
->cleanup
)(info
);
217 static void umh_complete(struct subprocess_info
*sub_info
)
219 struct completion
*comp
= xchg(&sub_info
->complete
, NULL
);
221 * See call_usermodehelper_exec(). If xchg() returns NULL
222 * we own sub_info, the UMH_KILLABLE caller has gone away
223 * or the caller used UMH_NO_WAIT.
228 call_usermodehelper_freeinfo(sub_info
);
232 * This is the task which runs the usermode application
234 static int call_usermodehelper_exec_async(void *data
)
236 struct subprocess_info
*sub_info
= data
;
240 spin_lock_irq(¤t
->sighand
->siglock
);
241 flush_signal_handlers(current
, 1);
242 spin_unlock_irq(¤t
->sighand
->siglock
);
245 * Our parent (unbound workqueue) runs with elevated scheduling
246 * priority. Avoid propagating that into the userspace child.
248 set_user_nice(current
, 0);
251 new = prepare_kernel_cred(current
);
255 spin_lock(&umh_sysctl_lock
);
256 new->cap_bset
= cap_intersect(usermodehelper_bset
, new->cap_bset
);
257 new->cap_inheritable
= cap_intersect(usermodehelper_inheritable
,
258 new->cap_inheritable
);
259 spin_unlock(&umh_sysctl_lock
);
261 if (sub_info
->init
) {
262 retval
= sub_info
->init(sub_info
, new);
271 retval
= do_execve(getname_kernel(sub_info
->path
),
272 (const char __user
*const __user
*)sub_info
->argv
,
273 (const char __user
*const __user
*)sub_info
->envp
);
275 sub_info
->retval
= retval
;
277 * call_usermodehelper_exec_sync() will call umh_complete
280 if (!(sub_info
->wait
& UMH_WAIT_PROC
))
281 umh_complete(sub_info
);
287 /* Handles UMH_WAIT_PROC. */
288 static void call_usermodehelper_exec_sync(struct subprocess_info
*sub_info
)
292 /* If SIGCLD is ignored sys_wait4 won't populate the status. */
293 kernel_sigaction(SIGCHLD
, SIG_DFL
);
294 pid
= kernel_thread(call_usermodehelper_exec_async
, sub_info
, SIGCHLD
);
296 sub_info
->retval
= pid
;
300 * Normally it is bogus to call wait4() from in-kernel because
301 * wait4() wants to write the exit code to a userspace address.
302 * But call_usermodehelper_exec_sync() always runs as kernel
303 * thread (workqueue) and put_user() to a kernel address works
304 * OK for kernel threads, due to their having an mm_segment_t
305 * which spans the entire address space.
307 * Thus the __user pointer cast is valid here.
309 sys_wait4(pid
, (int __user
*)&ret
, 0, NULL
);
312 * If ret is 0, either call_usermodehelper_exec_async failed and
313 * the real error code is already in sub_info->retval or
314 * sub_info->retval is 0 anyway, so don't mess with it then.
317 sub_info
->retval
= ret
;
320 /* Restore default kernel sig handler */
321 kernel_sigaction(SIGCHLD
, SIG_IGN
);
323 umh_complete(sub_info
);
327 * We need to create the usermodehelper kernel thread from a task that is affine
328 * to an optimized set of CPUs (or nohz housekeeping ones) such that they
329 * inherit a widest affinity irrespective of call_usermodehelper() callers with
330 * possibly reduced affinity (eg: per-cpu workqueues). We don't want
331 * usermodehelper targets to contend a busy CPU.
333 * Unbound workqueues provide such wide affinity and allow to block on
334 * UMH_WAIT_PROC requests without blocking pending request (up to some limit).
336 * Besides, workqueues provide the privilege level that caller might not have
337 * to perform the usermodehelper request.
340 static void call_usermodehelper_exec_work(struct work_struct
*work
)
342 struct subprocess_info
*sub_info
=
343 container_of(work
, struct subprocess_info
, work
);
345 if (sub_info
->wait
& UMH_WAIT_PROC
) {
346 call_usermodehelper_exec_sync(sub_info
);
350 * Use CLONE_PARENT to reparent it to kthreadd; we do not
351 * want to pollute current->children, and we need a parent
352 * that always ignores SIGCHLD to ensure auto-reaping.
354 pid
= kernel_thread(call_usermodehelper_exec_async
, sub_info
,
355 CLONE_PARENT
| SIGCHLD
);
357 sub_info
->retval
= pid
;
358 umh_complete(sub_info
);
364 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
365 * (used for preventing user land processes from being created after the user
366 * land has been frozen during a system-wide hibernation or suspend operation).
367 * Should always be manipulated under umhelper_sem acquired for write.
369 static enum umh_disable_depth usermodehelper_disabled
= UMH_DISABLED
;
371 /* Number of helpers running */
372 static atomic_t running_helpers
= ATOMIC_INIT(0);
375 * Wait queue head used by usermodehelper_disable() to wait for all running
378 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq
);
381 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
384 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq
);
387 * Time to wait for running_helpers to become zero before the setting of
388 * usermodehelper_disabled in usermodehelper_disable() fails
390 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
392 int usermodehelper_read_trylock(void)
397 down_read(&umhelper_sem
);
399 prepare_to_wait(&usermodehelper_disabled_waitq
, &wait
,
401 if (!usermodehelper_disabled
)
404 if (usermodehelper_disabled
== UMH_DISABLED
)
407 up_read(&umhelper_sem
);
415 down_read(&umhelper_sem
);
417 finish_wait(&usermodehelper_disabled_waitq
, &wait
);
420 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock
);
422 long usermodehelper_read_lock_wait(long timeout
)
429 down_read(&umhelper_sem
);
431 prepare_to_wait(&usermodehelper_disabled_waitq
, &wait
,
432 TASK_UNINTERRUPTIBLE
);
433 if (!usermodehelper_disabled
)
436 up_read(&umhelper_sem
);
438 timeout
= schedule_timeout(timeout
);
442 down_read(&umhelper_sem
);
444 finish_wait(&usermodehelper_disabled_waitq
, &wait
);
447 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait
);
449 void usermodehelper_read_unlock(void)
451 up_read(&umhelper_sem
);
453 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock
);
456 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
457 * @depth: New value to assign to usermodehelper_disabled.
459 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
460 * writing) and wakeup tasks waiting for it to change.
462 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth
)
464 down_write(&umhelper_sem
);
465 usermodehelper_disabled
= depth
;
466 wake_up(&usermodehelper_disabled_waitq
);
467 up_write(&umhelper_sem
);
471 * __usermodehelper_disable - Prevent new helpers from being started.
472 * @depth: New value to assign to usermodehelper_disabled.
474 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
476 int __usermodehelper_disable(enum umh_disable_depth depth
)
483 down_write(&umhelper_sem
);
484 usermodehelper_disabled
= depth
;
485 up_write(&umhelper_sem
);
488 * From now on call_usermodehelper_exec() won't start any new
489 * helpers, so it is sufficient if running_helpers turns out to
490 * be zero at one point (it may be increased later, but that
493 retval
= wait_event_timeout(running_helpers_waitq
,
494 atomic_read(&running_helpers
) == 0,
495 RUNNING_HELPERS_TIMEOUT
);
499 __usermodehelper_set_disable_depth(UMH_ENABLED
);
503 static void helper_lock(void)
505 atomic_inc(&running_helpers
);
506 smp_mb__after_atomic();
509 static void helper_unlock(void)
511 if (atomic_dec_and_test(&running_helpers
))
512 wake_up(&running_helpers_waitq
);
516 * call_usermodehelper_setup - prepare to call a usermode helper
517 * @path: path to usermode executable
518 * @argv: arg vector for process
519 * @envp: environment for process
520 * @gfp_mask: gfp mask for memory allocation
521 * @cleanup: a cleanup function
522 * @init: an init function
523 * @data: arbitrary context sensitive data
525 * Returns either %NULL on allocation failure, or a subprocess_info
526 * structure. This should be passed to call_usermodehelper_exec to
527 * exec the process and free the structure.
529 * The init function is used to customize the helper process prior to
530 * exec. A non-zero return code causes the process to error out, exit,
531 * and return the failure to the calling process
533 * The cleanup function is just before ethe subprocess_info is about to
534 * be freed. This can be used for freeing the argv and envp. The
535 * Function must be runnable in either a process context or the
536 * context in which call_usermodehelper_exec is called.
538 struct subprocess_info
*call_usermodehelper_setup(const char *path
, char **argv
,
539 char **envp
, gfp_t gfp_mask
,
540 int (*init
)(struct subprocess_info
*info
, struct cred
*new),
541 void (*cleanup
)(struct subprocess_info
*info
),
544 struct subprocess_info
*sub_info
;
545 sub_info
= kzalloc(sizeof(struct subprocess_info
), gfp_mask
);
549 INIT_WORK(&sub_info
->work
, call_usermodehelper_exec_work
);
551 #ifdef CONFIG_STATIC_USERMODEHELPER
552 sub_info
->path
= CONFIG_STATIC_USERMODEHELPER_PATH
;
554 sub_info
->path
= path
;
556 sub_info
->argv
= argv
;
557 sub_info
->envp
= envp
;
559 sub_info
->cleanup
= cleanup
;
560 sub_info
->init
= init
;
561 sub_info
->data
= data
;
565 EXPORT_SYMBOL(call_usermodehelper_setup
);
568 * call_usermodehelper_exec - start a usermode application
569 * @sub_info: information about the subprocessa
570 * @wait: wait for the application to finish and return status.
571 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
572 * when the program couldn't be exec'ed. This makes it safe to call
573 * from interrupt context.
575 * Runs a user-space application. The application is started
576 * asynchronously if wait is not set, and runs as a child of system workqueues.
577 * (ie. it runs with full root capabilities and optimized affinity).
579 int call_usermodehelper_exec(struct subprocess_info
*sub_info
, int wait
)
581 DECLARE_COMPLETION_ONSTACK(done
);
584 if (!sub_info
->path
) {
585 call_usermodehelper_freeinfo(sub_info
);
589 if (usermodehelper_disabled
) {
595 * If there is no binary for us to call, then just return and get out of
596 * here. This allows us to set STATIC_USERMODEHELPER_PATH to "" and
597 * disable all call_usermodehelper() calls.
599 if (strlen(sub_info
->path
) == 0)
603 * Set the completion pointer only if there is a waiter.
604 * This makes it possible to use umh_complete to free
605 * the data structure in case of UMH_NO_WAIT.
607 sub_info
->complete
= (wait
== UMH_NO_WAIT
) ? NULL
: &done
;
608 sub_info
->wait
= wait
;
610 queue_work(system_unbound_wq
, &sub_info
->work
);
611 if (wait
== UMH_NO_WAIT
) /* task has freed sub_info */
614 if (wait
& UMH_KILLABLE
) {
615 retval
= wait_for_completion_killable(&done
);
619 /* umh_complete() will see NULL and free sub_info */
620 if (xchg(&sub_info
->complete
, NULL
))
622 /* fallthrough, umh_complete() was already called */
625 wait_for_completion(&done
);
627 retval
= sub_info
->retval
;
629 call_usermodehelper_freeinfo(sub_info
);
634 EXPORT_SYMBOL(call_usermodehelper_exec
);
637 * call_usermodehelper() - prepare and start a usermode application
638 * @path: path to usermode executable
639 * @argv: arg vector for process
640 * @envp: environment for process
641 * @wait: wait for the application to finish and return status.
642 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
643 * when the program couldn't be exec'ed. This makes it safe to call
644 * from interrupt context.
646 * This function is the equivalent to use call_usermodehelper_setup() and
647 * call_usermodehelper_exec().
649 int call_usermodehelper(const char *path
, char **argv
, char **envp
, int wait
)
651 struct subprocess_info
*info
;
652 gfp_t gfp_mask
= (wait
== UMH_NO_WAIT
) ? GFP_ATOMIC
: GFP_KERNEL
;
654 info
= call_usermodehelper_setup(path
, argv
, envp
, gfp_mask
,
659 return call_usermodehelper_exec(info
, wait
);
661 EXPORT_SYMBOL(call_usermodehelper
);
663 static int proc_cap_handler(struct ctl_table
*table
, int write
,
664 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
667 unsigned long cap_array
[_KERNEL_CAPABILITY_U32S
];
668 kernel_cap_t new_cap
;
671 if (write
&& (!capable(CAP_SETPCAP
) ||
672 !capable(CAP_SYS_MODULE
)))
676 * convert from the global kernel_cap_t to the ulong array to print to
677 * userspace if this is a read.
679 spin_lock(&umh_sysctl_lock
);
680 for (i
= 0; i
< _KERNEL_CAPABILITY_U32S
; i
++) {
681 if (table
->data
== CAP_BSET
)
682 cap_array
[i
] = usermodehelper_bset
.cap
[i
];
683 else if (table
->data
== CAP_PI
)
684 cap_array
[i
] = usermodehelper_inheritable
.cap
[i
];
688 spin_unlock(&umh_sysctl_lock
);
694 * actually read or write and array of ulongs from userspace. Remember
695 * these are least significant 32 bits first
697 err
= proc_doulongvec_minmax(&t
, write
, buffer
, lenp
, ppos
);
702 * convert from the sysctl array of ulongs to the kernel_cap_t
703 * internal representation
705 for (i
= 0; i
< _KERNEL_CAPABILITY_U32S
; i
++)
706 new_cap
.cap
[i
] = cap_array
[i
];
709 * Drop everything not in the new_cap (but don't add things)
711 spin_lock(&umh_sysctl_lock
);
713 if (table
->data
== CAP_BSET
)
714 usermodehelper_bset
= cap_intersect(usermodehelper_bset
, new_cap
);
715 if (table
->data
== CAP_PI
)
716 usermodehelper_inheritable
= cap_intersect(usermodehelper_inheritable
, new_cap
);
718 spin_unlock(&umh_sysctl_lock
);
723 struct ctl_table usermodehelper_table
[] = {
727 .maxlen
= _KERNEL_CAPABILITY_U32S
* sizeof(unsigned long),
729 .proc_handler
= proc_cap_handler
,
732 .procname
= "inheritable",
734 .maxlen
= _KERNEL_CAPABILITY_U32S
* sizeof(unsigned long),
736 .proc_handler
= proc_cap_handler
,