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/syscalls.h>
24 #include <linux/unistd.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/completion.h>
28 #include <linux/cred.h>
29 #include <linux/file.h>
30 #include <linux/fdtable.h>
31 #include <linux/workqueue.h>
32 #include <linux/security.h>
33 #include <linux/mount.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/resource.h>
37 #include <linux/notifier.h>
38 #include <linux/suspend.h>
39 #include <linux/rwsem.h>
40 #include <linux/ptrace.h>
41 #include <linux/async.h>
42 #include <asm/uaccess.h>
44 #include <trace/events/module.h>
46 extern int max_threads
;
48 static struct workqueue_struct
*khelper_wq
;
50 #define CAP_BSET (void *)1
51 #define CAP_PI (void *)2
53 static kernel_cap_t usermodehelper_bset
= CAP_FULL_SET
;
54 static kernel_cap_t usermodehelper_inheritable
= CAP_FULL_SET
;
55 static DEFINE_SPINLOCK(umh_sysctl_lock
);
56 static DECLARE_RWSEM(umhelper_sem
);
61 modprobe_path is set via /proc/sys.
63 char modprobe_path
[KMOD_PATH_LEN
] = "/sbin/modprobe";
65 static void free_modprobe_argv(struct subprocess_info
*info
)
67 kfree(info
->argv
[3]); /* check call_modprobe() */
71 static int call_modprobe(char *module_name
, char *module_param
, int wait
)
73 struct subprocess_info
*info
;
74 static char *envp
[] = {
77 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
81 char **argv
= kmalloc(sizeof(char *[6]), GFP_KERNEL
);
85 module_name
= kstrdup(module_name
, GFP_KERNEL
);
89 argv
[0] = modprobe_path
;
92 argv
[3] = module_name
; /* check free_modprobe_argv() */
93 argv
[4] = module_param
;
96 info
= call_usermodehelper_setup(modprobe_path
, argv
, envp
, GFP_KERNEL
,
97 NULL
, free_modprobe_argv
, NULL
);
99 goto free_module_name
;
101 return call_usermodehelper_exec(info
, wait
| UMH_KILLABLE
);
112 * __request_module - try to load a kernel module
113 * @wait: wait (or not) for the operation to complete
114 * @fmt: printf style format string for the name of the module
115 * @...: arguments as specified in the format string
117 * Load a module using the user mode module loader. The function returns
118 * zero on success or a negative errno code on failure. Note that a
119 * successful module load does not mean the module did not then unload
120 * and exit on an error of its own. Callers must check that the service
121 * they requested is now available not blindly invoke it.
123 * If module auto-loading support is disabled then this function
124 * becomes a no-operation.
126 static int ____request_module(bool wait
, char *module_param
, const char *fmt
, va_list ap
)
128 char module_name
[MODULE_NAME_LEN
];
129 unsigned int max_modprobes
;
131 static atomic_t kmod_concurrent
= ATOMIC_INIT(0);
132 #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
133 static int kmod_loop_msg
;
136 * We don't allow synchronous module loading from async. Module
137 * init may invoke async_synchronize_full() which will end up
138 * waiting for this task which already is waiting for the module
139 * loading to complete, leading to a deadlock.
141 WARN_ON_ONCE(wait
&& current_is_async());
143 if (!modprobe_path
[0])
146 ret
= vsnprintf(module_name
, MODULE_NAME_LEN
, fmt
, ap
);
147 if (ret
>= MODULE_NAME_LEN
)
148 return -ENAMETOOLONG
;
150 ret
= security_kernel_module_request(module_name
);
154 #ifdef CONFIG_GRKERNSEC_MODHARDEN
155 if (uid_eq(current_uid(), GLOBAL_ROOT_UID
)) {
156 /* hack to workaround consolekit/udisks stupidity */
157 read_lock(&tasklist_lock
);
158 if (!strcmp(current
->comm
, "mount") &&
159 current
->real_parent
&& !strncmp(current
->real_parent
->comm
, "udisk", 5)) {
160 read_unlock(&tasklist_lock
);
161 printk(KERN_ALERT
"grsec: denied attempt to auto-load fs module %.64s by udisks\n", module_name
);
164 read_unlock(&tasklist_lock
);
168 /* If modprobe needs a service that is in a module, we get a recursive
169 * loop. Limit the number of running kmod threads to max_threads/2 or
170 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
171 * would be to run the parents of this process, counting how many times
172 * kmod was invoked. That would mean accessing the internals of the
173 * process tables to get the command line, proc_pid_cmdline is static
174 * and it is not worth changing the proc code just to handle this case.
177 * "trace the ppid" is simple, but will fail if someone's
178 * parent exits. I think this is as good as it gets. --RR
180 max_modprobes
= min(max_threads
/2, MAX_KMOD_CONCURRENT
);
181 atomic_inc(&kmod_concurrent
);
182 if (atomic_read(&kmod_concurrent
) > max_modprobes
) {
183 /* We may be blaming an innocent here, but unlikely */
184 if (kmod_loop_msg
< 5) {
186 "request_module: runaway loop modprobe %s\n",
190 atomic_dec(&kmod_concurrent
);
194 trace_module_request(module_name
, wait
, _RET_IP_
);
196 ret
= call_modprobe(module_name
, module_param
, wait
? UMH_WAIT_PROC
: UMH_WAIT_EXEC
);
198 atomic_dec(&kmod_concurrent
);
202 int ___request_module(bool wait
, char *module_param
, const char *fmt
, ...)
208 ret
= ____request_module(wait
, module_param
, fmt
, args
);
214 int __request_module(bool wait
, const char *fmt
, ...)
219 #ifdef CONFIG_GRKERNSEC_MODHARDEN
220 if (!uid_eq(current_uid(), GLOBAL_ROOT_UID
)) {
221 char module_param
[MODULE_NAME_LEN
];
223 memset(module_param
, 0, sizeof(module_param
));
225 snprintf(module_param
, sizeof(module_param
) - 1, "grsec_modharden_normal%u_", GR_GLOBAL_UID(current_uid()));
228 ret
= ____request_module(wait
, module_param
, fmt
, args
);
236 ret
= ____request_module(wait
, NULL
, fmt
, args
);
242 EXPORT_SYMBOL(__request_module
);
243 #endif /* CONFIG_MODULES */
245 static void call_usermodehelper_freeinfo(struct subprocess_info
*info
)
247 #ifdef CONFIG_GRKERNSEC
249 info
->path
= info
->origpath
;
252 (*info
->cleanup
)(info
);
256 static void umh_complete(struct subprocess_info
*sub_info
)
258 struct completion
*comp
= xchg(&sub_info
->complete
, NULL
);
260 * See call_usermodehelper_exec(). If xchg() returns NULL
261 * we own sub_info, the UMH_KILLABLE caller has gone away
262 * or the caller used UMH_NO_WAIT.
267 call_usermodehelper_freeinfo(sub_info
);
271 * This is the task which runs the usermode application
273 static int ____call_usermodehelper(void *data
)
275 struct subprocess_info
*sub_info
= data
;
279 spin_lock_irq(¤t
->sighand
->siglock
);
280 flush_signal_handlers(current
, 1);
281 spin_unlock_irq(¤t
->sighand
->siglock
);
283 /* We can run anywhere, unlike our parent keventd(). */
284 set_cpus_allowed_ptr(current
, cpu_all_mask
);
287 * Our parent is keventd, which runs with elevated scheduling priority.
288 * Avoid propagating that into the userspace child.
290 set_user_nice(current
, 0);
292 #ifdef CONFIG_GRKERNSEC
293 /* this is race-free as far as userland is concerned as we copied
294 out the path to be used prior to this point and are now operating
297 if ((strncmp(sub_info
->path
, "/sbin/", 6) && strncmp(sub_info
->path
, "/usr/lib/", 9) &&
298 strncmp(sub_info
->path
, "/lib/", 5) && strncmp(sub_info
->path
, "/lib64/", 7) &&
299 strcmp(sub_info
->path
, "/usr/share/apport/apport")) || strstr(sub_info
->path
, "..")) {
300 printk(KERN_ALERT
"grsec: denied exec of usermode helper binary %.950s located outside of /sbin and system library paths\n", sub_info
->path
);
307 new = prepare_kernel_cred(current
);
311 spin_lock(&umh_sysctl_lock
);
312 new->cap_bset
= cap_intersect(usermodehelper_bset
, new->cap_bset
);
313 new->cap_inheritable
= cap_intersect(usermodehelper_inheritable
,
314 new->cap_inheritable
);
315 spin_unlock(&umh_sysctl_lock
);
317 if (sub_info
->init
) {
318 retval
= sub_info
->init(sub_info
, new);
327 retval
= do_execve(getname_kernel(sub_info
->path
),
328 (const char __user
*const __force_user
*)sub_info
->argv
,
329 (const char __user
*const __force_user
*)sub_info
->envp
);
331 sub_info
->retval
= retval
;
332 /* wait_for_helper() will call umh_complete if UHM_WAIT_PROC. */
333 if (!(sub_info
->wait
& UMH_WAIT_PROC
))
334 umh_complete(sub_info
);
340 /* Keventd can't block, but this (a child) can. */
341 static int wait_for_helper(void *data
)
343 struct subprocess_info
*sub_info
= data
;
346 /* If SIGCLD is ignored sys_wait4 won't populate the status. */
347 kernel_sigaction(SIGCHLD
, SIG_DFL
);
348 pid
= kernel_thread(____call_usermodehelper
, sub_info
, SIGCHLD
);
350 sub_info
->retval
= pid
;
354 * Normally it is bogus to call wait4() from in-kernel because
355 * wait4() wants to write the exit code to a userspace address.
356 * But wait_for_helper() always runs as keventd, and put_user()
357 * to a kernel address works OK for kernel threads, due to their
358 * having an mm_segment_t which spans the entire address space.
360 * Thus the __user pointer cast is valid here.
362 sys_wait4(pid
, (int __force_user
*)&ret
, 0, NULL
);
365 * If ret is 0, either ____call_usermodehelper failed and the
366 * real error code is already in sub_info->retval or
367 * sub_info->retval is 0 anyway, so don't mess with it then.
370 sub_info
->retval
= ret
;
373 umh_complete(sub_info
);
377 /* This is run by khelper thread */
378 static void __call_usermodehelper(struct work_struct
*work
)
380 struct subprocess_info
*sub_info
=
381 container_of(work
, struct subprocess_info
, work
);
384 if (sub_info
->wait
& UMH_WAIT_PROC
)
385 pid
= kernel_thread(wait_for_helper
, sub_info
,
386 CLONE_FS
| CLONE_FILES
| SIGCHLD
);
388 pid
= kernel_thread(____call_usermodehelper
, sub_info
,
392 sub_info
->retval
= pid
;
393 umh_complete(sub_info
);
398 * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
399 * (used for preventing user land processes from being created after the user
400 * land has been frozen during a system-wide hibernation or suspend operation).
401 * Should always be manipulated under umhelper_sem acquired for write.
403 static enum umh_disable_depth usermodehelper_disabled
= UMH_DISABLED
;
405 /* Number of helpers running */
406 static atomic_t running_helpers
= ATOMIC_INIT(0);
409 * Wait queue head used by usermodehelper_disable() to wait for all running
412 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq
);
415 * Used by usermodehelper_read_lock_wait() to wait for usermodehelper_disabled
418 static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq
);
421 * Time to wait for running_helpers to become zero before the setting of
422 * usermodehelper_disabled in usermodehelper_disable() fails
424 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
426 int usermodehelper_read_trylock(void)
431 down_read(&umhelper_sem
);
433 prepare_to_wait(&usermodehelper_disabled_waitq
, &wait
,
435 if (!usermodehelper_disabled
)
438 if (usermodehelper_disabled
== UMH_DISABLED
)
441 up_read(&umhelper_sem
);
449 down_read(&umhelper_sem
);
451 finish_wait(&usermodehelper_disabled_waitq
, &wait
);
454 EXPORT_SYMBOL_GPL(usermodehelper_read_trylock
);
456 long usermodehelper_read_lock_wait(long timeout
)
463 down_read(&umhelper_sem
);
465 prepare_to_wait(&usermodehelper_disabled_waitq
, &wait
,
466 TASK_UNINTERRUPTIBLE
);
467 if (!usermodehelper_disabled
)
470 up_read(&umhelper_sem
);
472 timeout
= schedule_timeout(timeout
);
476 down_read(&umhelper_sem
);
478 finish_wait(&usermodehelper_disabled_waitq
, &wait
);
481 EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait
);
483 void usermodehelper_read_unlock(void)
485 up_read(&umhelper_sem
);
487 EXPORT_SYMBOL_GPL(usermodehelper_read_unlock
);
490 * __usermodehelper_set_disable_depth - Modify usermodehelper_disabled.
491 * @depth: New value to assign to usermodehelper_disabled.
493 * Change the value of usermodehelper_disabled (under umhelper_sem locked for
494 * writing) and wakeup tasks waiting for it to change.
496 void __usermodehelper_set_disable_depth(enum umh_disable_depth depth
)
498 down_write(&umhelper_sem
);
499 usermodehelper_disabled
= depth
;
500 wake_up(&usermodehelper_disabled_waitq
);
501 up_write(&umhelper_sem
);
505 * __usermodehelper_disable - Prevent new helpers from being started.
506 * @depth: New value to assign to usermodehelper_disabled.
508 * Set usermodehelper_disabled to @depth and wait for running helpers to exit.
510 int __usermodehelper_disable(enum umh_disable_depth depth
)
517 down_write(&umhelper_sem
);
518 usermodehelper_disabled
= depth
;
519 up_write(&umhelper_sem
);
522 * From now on call_usermodehelper_exec() won't start any new
523 * helpers, so it is sufficient if running_helpers turns out to
524 * be zero at one point (it may be increased later, but that
527 retval
= wait_event_timeout(running_helpers_waitq
,
528 atomic_read(&running_helpers
) == 0,
529 RUNNING_HELPERS_TIMEOUT
);
533 __usermodehelper_set_disable_depth(UMH_ENABLED
);
537 static void helper_lock(void)
539 atomic_inc(&running_helpers
);
540 smp_mb__after_atomic();
543 static void helper_unlock(void)
545 if (atomic_dec_and_test(&running_helpers
))
546 wake_up(&running_helpers_waitq
);
550 * call_usermodehelper_setup - prepare to call a usermode helper
551 * @path: path to usermode executable
552 * @argv: arg vector for process
553 * @envp: environment for process
554 * @gfp_mask: gfp mask for memory allocation
555 * @cleanup: a cleanup function
556 * @init: an init function
557 * @data: arbitrary context sensitive data
559 * Returns either %NULL on allocation failure, or a subprocess_info
560 * structure. This should be passed to call_usermodehelper_exec to
561 * exec the process and free the structure.
563 * The init function is used to customize the helper process prior to
564 * exec. A non-zero return code causes the process to error out, exit,
565 * and return the failure to the calling process
567 * The cleanup function is just before ethe subprocess_info is about to
568 * be freed. This can be used for freeing the argv and envp. The
569 * Function must be runnable in either a process context or the
570 * context in which call_usermodehelper_exec is called.
572 struct subprocess_info
*call_usermodehelper_setup(char *path
, char **argv
,
573 char **envp
, gfp_t gfp_mask
,
574 int (*init
)(struct subprocess_info
*info
, struct cred
*new),
575 void (*cleanup
)(struct subprocess_info
*info
),
578 struct subprocess_info
*sub_info
;
579 sub_info
= kzalloc(sizeof(struct subprocess_info
), gfp_mask
);
583 INIT_WORK(&sub_info
->work
, __call_usermodehelper
);
584 #ifdef CONFIG_GRKERNSEC
585 sub_info
->origpath
= path
;
586 sub_info
->path
= kstrdup(path
, gfp_mask
);
588 sub_info
->path
= path
;
590 sub_info
->argv
= argv
;
591 sub_info
->envp
= envp
;
593 sub_info
->cleanup
= cleanup
;
594 sub_info
->init
= init
;
595 sub_info
->data
= data
;
599 EXPORT_SYMBOL(call_usermodehelper_setup
);
602 * call_usermodehelper_exec - start a usermode application
603 * @sub_info: information about the subprocessa
604 * @wait: wait for the application to finish and return status.
605 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
606 * when the program couldn't be exec'ed. This makes it safe to call
607 * from interrupt context.
609 * Runs a user-space application. The application is started
610 * asynchronously if wait is not set, and runs as a child of keventd.
611 * (ie. it runs with full root capabilities).
613 int call_usermodehelper_exec(struct subprocess_info
*sub_info
, int wait
)
615 DECLARE_COMPLETION_ONSTACK(done
);
618 if (!sub_info
->path
) {
619 call_usermodehelper_freeinfo(sub_info
);
623 if (!khelper_wq
|| usermodehelper_disabled
) {
628 * Set the completion pointer only if there is a waiter.
629 * This makes it possible to use umh_complete to free
630 * the data structure in case of UMH_NO_WAIT.
632 sub_info
->complete
= (wait
== UMH_NO_WAIT
) ? NULL
: &done
;
633 sub_info
->wait
= wait
;
635 queue_work(khelper_wq
, &sub_info
->work
);
636 if (wait
== UMH_NO_WAIT
) /* task has freed sub_info */
639 if (wait
& UMH_KILLABLE
) {
640 retval
= wait_for_completion_killable(&done
);
644 /* umh_complete() will see NULL and free sub_info */
645 if (xchg(&sub_info
->complete
, NULL
))
647 /* fallthrough, umh_complete() was already called */
650 wait_for_completion(&done
);
652 retval
= sub_info
->retval
;
654 call_usermodehelper_freeinfo(sub_info
);
659 EXPORT_SYMBOL(call_usermodehelper_exec
);
662 * call_usermodehelper() - prepare and start a usermode application
663 * @path: path to usermode executable
664 * @argv: arg vector for process
665 * @envp: environment for process
666 * @wait: wait for the application to finish and return status.
667 * when UMH_NO_WAIT don't wait at all, but you get no useful error back
668 * when the program couldn't be exec'ed. This makes it safe to call
669 * from interrupt context.
671 * This function is the equivalent to use call_usermodehelper_setup() and
672 * call_usermodehelper_exec().
674 int call_usermodehelper(char *path
, char **argv
, char **envp
, int wait
)
676 struct subprocess_info
*info
;
677 gfp_t gfp_mask
= (wait
== UMH_NO_WAIT
) ? GFP_ATOMIC
: GFP_KERNEL
;
679 info
= call_usermodehelper_setup(path
, argv
, envp
, gfp_mask
,
684 return call_usermodehelper_exec(info
, wait
);
686 EXPORT_SYMBOL(call_usermodehelper
);
688 static int proc_cap_handler(struct ctl_table
*table
, int write
,
689 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
691 ctl_table_no_const t
;
692 unsigned long cap_array
[_KERNEL_CAPABILITY_U32S
];
693 kernel_cap_t new_cap
;
696 if (write
&& (!capable(CAP_SETPCAP
) ||
697 !capable(CAP_SYS_MODULE
)))
701 * convert from the global kernel_cap_t to the ulong array to print to
702 * userspace if this is a read.
704 spin_lock(&umh_sysctl_lock
);
705 for (i
= 0; i
< _KERNEL_CAPABILITY_U32S
; i
++) {
706 if (table
->data
== CAP_BSET
)
707 cap_array
[i
] = usermodehelper_bset
.cap
[i
];
708 else if (table
->data
== CAP_PI
)
709 cap_array
[i
] = usermodehelper_inheritable
.cap
[i
];
713 spin_unlock(&umh_sysctl_lock
);
719 * actually read or write and array of ulongs from userspace. Remember
720 * these are least significant 32 bits first
722 err
= proc_doulongvec_minmax(&t
, write
, buffer
, lenp
, ppos
);
727 * convert from the sysctl array of ulongs to the kernel_cap_t
728 * internal representation
730 for (i
= 0; i
< _KERNEL_CAPABILITY_U32S
; i
++)
731 new_cap
.cap
[i
] = cap_array
[i
];
734 * Drop everything not in the new_cap (but don't add things)
736 spin_lock(&umh_sysctl_lock
);
738 if (table
->data
== CAP_BSET
)
739 usermodehelper_bset
= cap_intersect(usermodehelper_bset
, new_cap
);
740 if (table
->data
== CAP_PI
)
741 usermodehelper_inheritable
= cap_intersect(usermodehelper_inheritable
, new_cap
);
743 spin_unlock(&umh_sysctl_lock
);
748 struct ctl_table usermodehelper_table
[] = {
752 .maxlen
= _KERNEL_CAPABILITY_U32S
* sizeof(unsigned long),
754 .proc_handler
= proc_cap_handler
,
757 .procname
= "inheritable",
759 .maxlen
= _KERNEL_CAPABILITY_U32S
* sizeof(unsigned long),
761 .proc_handler
= proc_cap_handler
,
766 void __init
usermodehelper_init(void)
768 khelper_wq
= create_singlethread_workqueue("khelper");