/*1kmod, the new module loader (replaces kerneld)2Kirk Petersen34Reorganized not to be a daemon by Adam Richter, with guidance5from Greg Zornetzer.67Modified to avoid chroot and file sharing problems.8Mikael Pettersson910Limit the concurrent number of kmod modprobes to catch loops from11"modprobe needs a service that is in a module".12Keith Owens <[email protected]> December 19991314Unblock all signals when we exec a usermode process.15Shuu Yamaguchi <[email protected]> December 20001617call_usermodehelper wait flag, and remove exec_usermodehelper.18Rusty Russell <[email protected]> Jan 200319*/20#include <linux/module.h>21#include <linux/sched.h>22#include <linux/syscalls.h>23#include <linux/unistd.h>24#include <linux/kmod.h>25#include <linux/slab.h>26#include <linux/completion.h>27#include <linux/cred.h>28#include <linux/file.h>29#include <linux/fdtable.h>30#include <linux/workqueue.h>31#include <linux/security.h>32#include <linux/mount.h>33#include <linux/kernel.h>34#include <linux/init.h>35#include <linux/resource.h>36#include <linux/notifier.h>37#include <linux/suspend.h>38#include <asm/uaccess.h>3940#include <trace/events/module.h>4142extern int max_threads;4344static struct workqueue_struct *khelper_wq;4546#define CAP_BSET (void *)147#define CAP_PI (void *)24849static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;50static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;51static DEFINE_SPINLOCK(umh_sysctl_lock);5253#ifdef CONFIG_MODULES5455/*56modprobe_path is set via /proc/sys.57*/58char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";5960/**61* __request_module - try to load a kernel module62* @wait: wait (or not) for the operation to complete63* @fmt: printf style format string for the name of the module64* @...: arguments as specified in the format string65*66* Load a module using the user mode module loader. The function returns67* zero on success or a negative errno code on failure. Note that a68* successful module load does not mean the module did not then unload69* and exit on an error of its own. Callers must check that the service70* they requested is now available not blindly invoke it.71*72* If module auto-loading support is disabled then this function73* becomes a no-operation.74*/75int __request_module(bool wait, const char *fmt, ...)76{77va_list args;78char module_name[MODULE_NAME_LEN];79unsigned int max_modprobes;80int ret;81char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };82static char *envp[] = { "HOME=/",83"TERM=linux",84"PATH=/sbin:/usr/sbin:/bin:/usr/bin",85NULL };86static atomic_t kmod_concurrent = ATOMIC_INIT(0);87#define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */88static int kmod_loop_msg;8990va_start(args, fmt);91ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);92va_end(args);93if (ret >= MODULE_NAME_LEN)94return -ENAMETOOLONG;9596ret = security_kernel_module_request(module_name);97if (ret)98return ret;99100/* If modprobe needs a service that is in a module, we get a recursive101* loop. Limit the number of running kmod threads to max_threads/2 or102* MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method103* would be to run the parents of this process, counting how many times104* kmod was invoked. That would mean accessing the internals of the105* process tables to get the command line, proc_pid_cmdline is static106* and it is not worth changing the proc code just to handle this case.107* KAO.108*109* "trace the ppid" is simple, but will fail if someone's110* parent exits. I think this is as good as it gets. --RR111*/112max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);113atomic_inc(&kmod_concurrent);114if (atomic_read(&kmod_concurrent) > max_modprobes) {115/* We may be blaming an innocent here, but unlikely */116if (kmod_loop_msg++ < 5)117printk(KERN_ERR118"request_module: runaway loop modprobe %s\n",119module_name);120atomic_dec(&kmod_concurrent);121return -ENOMEM;122}123124trace_module_request(module_name, wait, _RET_IP_);125126ret = call_usermodehelper_fns(modprobe_path, argv, envp,127wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC,128NULL, NULL, NULL);129130atomic_dec(&kmod_concurrent);131return ret;132}133EXPORT_SYMBOL(__request_module);134#endif /* CONFIG_MODULES */135136/*137* This is the task which runs the usermode application138*/139static int ____call_usermodehelper(void *data)140{141struct subprocess_info *sub_info = data;142struct cred *new;143int retval;144145spin_lock_irq(¤t->sighand->siglock);146flush_signal_handlers(current, 1);147spin_unlock_irq(¤t->sighand->siglock);148149/* We can run anywhere, unlike our parent keventd(). */150set_cpus_allowed_ptr(current, cpu_all_mask);151152/*153* Our parent is keventd, which runs with elevated scheduling priority.154* Avoid propagating that into the userspace child.155*/156set_user_nice(current, 0);157158retval = -ENOMEM;159new = prepare_kernel_cred(current);160if (!new)161goto fail;162163spin_lock(&umh_sysctl_lock);164new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);165new->cap_inheritable = cap_intersect(usermodehelper_inheritable,166new->cap_inheritable);167spin_unlock(&umh_sysctl_lock);168169if (sub_info->init) {170retval = sub_info->init(sub_info, new);171if (retval) {172abort_creds(new);173goto fail;174}175}176177commit_creds(new);178179retval = kernel_execve(sub_info->path,180(const char *const *)sub_info->argv,181(const char *const *)sub_info->envp);182183/* Exec failed? */184fail:185sub_info->retval = retval;186do_exit(0);187}188189void call_usermodehelper_freeinfo(struct subprocess_info *info)190{191if (info->cleanup)192(*info->cleanup)(info);193kfree(info);194}195EXPORT_SYMBOL(call_usermodehelper_freeinfo);196197/* Keventd can't block, but this (a child) can. */198static int wait_for_helper(void *data)199{200struct subprocess_info *sub_info = data;201pid_t pid;202203/* If SIGCLD is ignored sys_wait4 won't populate the status. */204spin_lock_irq(¤t->sighand->siglock);205current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;206spin_unlock_irq(¤t->sighand->siglock);207208pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);209if (pid < 0) {210sub_info->retval = pid;211} else {212int ret = -ECHILD;213/*214* Normally it is bogus to call wait4() from in-kernel because215* wait4() wants to write the exit code to a userspace address.216* But wait_for_helper() always runs as keventd, and put_user()217* to a kernel address works OK for kernel threads, due to their218* having an mm_segment_t which spans the entire address space.219*220* Thus the __user pointer cast is valid here.221*/222sys_wait4(pid, (int __user *)&ret, 0, NULL);223224/*225* If ret is 0, either ____call_usermodehelper failed and the226* real error code is already in sub_info->retval or227* sub_info->retval is 0 anyway, so don't mess with it then.228*/229if (ret)230sub_info->retval = ret;231}232233complete(sub_info->complete);234return 0;235}236237/* This is run by khelper thread */238static void __call_usermodehelper(struct work_struct *work)239{240struct subprocess_info *sub_info =241container_of(work, struct subprocess_info, work);242enum umh_wait wait = sub_info->wait;243pid_t pid;244245/* CLONE_VFORK: wait until the usermode helper has execve'd246* successfully We need the data structures to stay around247* until that is done. */248if (wait == UMH_WAIT_PROC)249pid = kernel_thread(wait_for_helper, sub_info,250CLONE_FS | CLONE_FILES | SIGCHLD);251else252pid = kernel_thread(____call_usermodehelper, sub_info,253CLONE_VFORK | SIGCHLD);254255switch (wait) {256case UMH_NO_WAIT:257call_usermodehelper_freeinfo(sub_info);258break;259260case UMH_WAIT_PROC:261if (pid > 0)262break;263/* FALLTHROUGH */264case UMH_WAIT_EXEC:265if (pid < 0)266sub_info->retval = pid;267complete(sub_info->complete);268}269}270271/*272* If set, call_usermodehelper_exec() will exit immediately returning -EBUSY273* (used for preventing user land processes from being created after the user274* land has been frozen during a system-wide hibernation or suspend operation).275*/276static int usermodehelper_disabled;277278/* Number of helpers running */279static atomic_t running_helpers = ATOMIC_INIT(0);280281/*282* Wait queue head used by usermodehelper_pm_callback() to wait for all running283* helpers to finish.284*/285static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);286287/*288* Time to wait for running_helpers to become zero before the setting of289* usermodehelper_disabled in usermodehelper_pm_callback() fails290*/291#define RUNNING_HELPERS_TIMEOUT (5 * HZ)292293/**294* usermodehelper_disable - prevent new helpers from being started295*/296int usermodehelper_disable(void)297{298long retval;299300usermodehelper_disabled = 1;301smp_mb();302/*303* From now on call_usermodehelper_exec() won't start any new304* helpers, so it is sufficient if running_helpers turns out to305* be zero at one point (it may be increased later, but that306* doesn't matter).307*/308retval = wait_event_timeout(running_helpers_waitq,309atomic_read(&running_helpers) == 0,310RUNNING_HELPERS_TIMEOUT);311if (retval)312return 0;313314usermodehelper_disabled = 0;315return -EAGAIN;316}317318/**319* usermodehelper_enable - allow new helpers to be started again320*/321void usermodehelper_enable(void)322{323usermodehelper_disabled = 0;324}325326/**327* usermodehelper_is_disabled - check if new helpers are allowed to be started328*/329bool usermodehelper_is_disabled(void)330{331return usermodehelper_disabled;332}333EXPORT_SYMBOL_GPL(usermodehelper_is_disabled);334335static void helper_lock(void)336{337atomic_inc(&running_helpers);338smp_mb__after_atomic_inc();339}340341static void helper_unlock(void)342{343if (atomic_dec_and_test(&running_helpers))344wake_up(&running_helpers_waitq);345}346347/**348* call_usermodehelper_setup - prepare to call a usermode helper349* @path: path to usermode executable350* @argv: arg vector for process351* @envp: environment for process352* @gfp_mask: gfp mask for memory allocation353*354* Returns either %NULL on allocation failure, or a subprocess_info355* structure. This should be passed to call_usermodehelper_exec to356* exec the process and free the structure.357*/358struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,359char **envp, gfp_t gfp_mask)360{361struct subprocess_info *sub_info;362sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);363if (!sub_info)364goto out;365366INIT_WORK(&sub_info->work, __call_usermodehelper);367sub_info->path = path;368sub_info->argv = argv;369sub_info->envp = envp;370out:371return sub_info;372}373EXPORT_SYMBOL(call_usermodehelper_setup);374375/**376* call_usermodehelper_setfns - set a cleanup/init function377* @info: a subprocess_info returned by call_usermodehelper_setup378* @cleanup: a cleanup function379* @init: an init function380* @data: arbitrary context sensitive data381*382* The init function is used to customize the helper process prior to383* exec. A non-zero return code causes the process to error out, exit,384* and return the failure to the calling process385*386* The cleanup function is just before ethe subprocess_info is about to387* be freed. This can be used for freeing the argv and envp. The388* Function must be runnable in either a process context or the389* context in which call_usermodehelper_exec is called.390*/391void call_usermodehelper_setfns(struct subprocess_info *info,392int (*init)(struct subprocess_info *info, struct cred *new),393void (*cleanup)(struct subprocess_info *info),394void *data)395{396info->cleanup = cleanup;397info->init = init;398info->data = data;399}400EXPORT_SYMBOL(call_usermodehelper_setfns);401402/**403* call_usermodehelper_exec - start a usermode application404* @sub_info: information about the subprocessa405* @wait: wait for the application to finish and return status.406* when -1 don't wait at all, but you get no useful error back when407* the program couldn't be exec'ed. This makes it safe to call408* from interrupt context.409*410* Runs a user-space application. The application is started411* asynchronously if wait is not set, and runs as a child of keventd.412* (ie. it runs with full root capabilities).413*/414int call_usermodehelper_exec(struct subprocess_info *sub_info,415enum umh_wait wait)416{417DECLARE_COMPLETION_ONSTACK(done);418int retval = 0;419420helper_lock();421if (sub_info->path[0] == '\0')422goto out;423424if (!khelper_wq || usermodehelper_disabled) {425retval = -EBUSY;426goto out;427}428429sub_info->complete = &done;430sub_info->wait = wait;431432queue_work(khelper_wq, &sub_info->work);433if (wait == UMH_NO_WAIT) /* task has freed sub_info */434goto unlock;435wait_for_completion(&done);436retval = sub_info->retval;437438out:439call_usermodehelper_freeinfo(sub_info);440unlock:441helper_unlock();442return retval;443}444EXPORT_SYMBOL(call_usermodehelper_exec);445446static int proc_cap_handler(struct ctl_table *table, int write,447void __user *buffer, size_t *lenp, loff_t *ppos)448{449struct ctl_table t;450unsigned long cap_array[_KERNEL_CAPABILITY_U32S];451kernel_cap_t new_cap;452int err, i;453454if (write && (!capable(CAP_SETPCAP) ||455!capable(CAP_SYS_MODULE)))456return -EPERM;457458/*459* convert from the global kernel_cap_t to the ulong array to print to460* userspace if this is a read.461*/462spin_lock(&umh_sysctl_lock);463for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {464if (table->data == CAP_BSET)465cap_array[i] = usermodehelper_bset.cap[i];466else if (table->data == CAP_PI)467cap_array[i] = usermodehelper_inheritable.cap[i];468else469BUG();470}471spin_unlock(&umh_sysctl_lock);472473t = *table;474t.data = &cap_array;475476/*477* actually read or write and array of ulongs from userspace. Remember478* these are least significant 32 bits first479*/480err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);481if (err < 0)482return err;483484/*485* convert from the sysctl array of ulongs to the kernel_cap_t486* internal representation487*/488for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)489new_cap.cap[i] = cap_array[i];490491/*492* Drop everything not in the new_cap (but don't add things)493*/494spin_lock(&umh_sysctl_lock);495if (write) {496if (table->data == CAP_BSET)497usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);498if (table->data == CAP_PI)499usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);500}501spin_unlock(&umh_sysctl_lock);502503return 0;504}505506struct ctl_table usermodehelper_table[] = {507{508.procname = "bset",509.data = CAP_BSET,510.maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),511.mode = 0600,512.proc_handler = proc_cap_handler,513},514{515.procname = "inheritable",516.data = CAP_PI,517.maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),518.mode = 0600,519.proc_handler = proc_cap_handler,520},521{ }522};523524void __init usermodehelper_init(void)525{526khelper_wq = create_singlethread_workqueue("khelper");527BUG_ON(!khelper_wq);528}529530531