Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * kmod - the kernel module loader
0003  */
0004 #include <linux/module.h>
0005 #include <linux/sched.h>
0006 #include <linux/sched/task.h>
0007 #include <linux/binfmts.h>
0008 #include <linux/syscalls.h>
0009 #include <linux/unistd.h>
0010 #include <linux/kmod.h>
0011 #include <linux/slab.h>
0012 #include <linux/completion.h>
0013 #include <linux/cred.h>
0014 #include <linux/file.h>
0015 #include <linux/fdtable.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/security.h>
0018 #include <linux/mount.h>
0019 #include <linux/kernel.h>
0020 #include <linux/init.h>
0021 #include <linux/resource.h>
0022 #include <linux/notifier.h>
0023 #include <linux/suspend.h>
0024 #include <linux/rwsem.h>
0025 #include <linux/ptrace.h>
0026 #include <linux/async.h>
0027 #include <linux/uaccess.h>
0028 
0029 #include <trace/events/module.h>
0030 
0031 /*
0032  * Assuming:
0033  *
0034  * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
0035  *             (u64) THREAD_SIZE * 8UL);
0036  *
0037  * If you need less than 50 threads would mean we're dealing with systems
0038  * smaller than 3200 pages. This assumes you are capable of having ~13M memory,
0039  * and this would only be an upper limit, after which the OOM killer would take
0040  * effect. Systems like these are very unlikely if modules are enabled.
0041  */
0042 #define MAX_KMOD_CONCURRENT 50
0043 static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT);
0044 static DECLARE_WAIT_QUEUE_HEAD(kmod_wq);
0045 
0046 /*
0047  * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads
0048  * running at the same time without returning. When this happens we
0049  * believe you've somehow ended up with a recursive module dependency
0050  * creating a loop.
0051  *
0052  * We have no option but to fail.
0053  *
0054  * Userspace should proactively try to detect and prevent these.
0055  */
0056 #define MAX_KMOD_ALL_BUSY_TIMEOUT 5
0057 
0058 /*
0059     modprobe_path is set via /proc/sys.
0060 */
0061 char modprobe_path[KMOD_PATH_LEN] = CONFIG_MODPROBE_PATH;
0062 
0063 static void free_modprobe_argv(struct subprocess_info *info)
0064 {
0065     kfree(info->argv[3]); /* check call_modprobe() */
0066     kfree(info->argv);
0067 }
0068 
0069 static int call_modprobe(char *module_name, int wait)
0070 {
0071     struct subprocess_info *info;
0072     static char *envp[] = {
0073         "HOME=/",
0074         "TERM=linux",
0075         "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
0076         NULL
0077     };
0078 
0079     char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
0080     if (!argv)
0081         goto out;
0082 
0083     module_name = kstrdup(module_name, GFP_KERNEL);
0084     if (!module_name)
0085         goto free_argv;
0086 
0087     argv[0] = modprobe_path;
0088     argv[1] = "-q";
0089     argv[2] = "--";
0090     argv[3] = module_name;  /* check free_modprobe_argv() */
0091     argv[4] = NULL;
0092 
0093     info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL,
0094                      NULL, free_modprobe_argv, NULL);
0095     if (!info)
0096         goto free_module_name;
0097 
0098     return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
0099 
0100 free_module_name:
0101     kfree(module_name);
0102 free_argv:
0103     kfree(argv);
0104 out:
0105     return -ENOMEM;
0106 }
0107 
0108 /**
0109  * __request_module - try to load a kernel module
0110  * @wait: wait (or not) for the operation to complete
0111  * @fmt: printf style format string for the name of the module
0112  * @...: arguments as specified in the format string
0113  *
0114  * Load a module using the user mode module loader. The function returns
0115  * zero on success or a negative errno code or positive exit code from
0116  * "modprobe" on failure. Note that a successful module load does not mean
0117  * the module did not then unload and exit on an error of its own. Callers
0118  * must check that the service they requested is now available not blindly
0119  * invoke it.
0120  *
0121  * If module auto-loading support is disabled then this function
0122  * simply returns -ENOENT.
0123  */
0124 int __request_module(bool wait, const char *fmt, ...)
0125 {
0126     va_list args;
0127     char module_name[MODULE_NAME_LEN];
0128     int ret;
0129 
0130     /*
0131      * We don't allow synchronous module loading from async.  Module
0132      * init may invoke async_synchronize_full() which will end up
0133      * waiting for this task which already is waiting for the module
0134      * loading to complete, leading to a deadlock.
0135      */
0136     WARN_ON_ONCE(wait && current_is_async());
0137 
0138     if (!modprobe_path[0])
0139         return -ENOENT;
0140 
0141     va_start(args, fmt);
0142     ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
0143     va_end(args);
0144     if (ret >= MODULE_NAME_LEN)
0145         return -ENAMETOOLONG;
0146 
0147     ret = security_kernel_module_request(module_name);
0148     if (ret)
0149         return ret;
0150 
0151     if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) {
0152         pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
0153                     atomic_read(&kmod_concurrent_max),
0154                     MAX_KMOD_CONCURRENT, module_name);
0155         ret = wait_event_killable_timeout(kmod_wq,
0156                           atomic_dec_if_positive(&kmod_concurrent_max) >= 0,
0157                           MAX_KMOD_ALL_BUSY_TIMEOUT * HZ);
0158         if (!ret) {
0159             pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now",
0160                         module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT);
0161             return -ETIME;
0162         } else if (ret == -ERESTARTSYS) {
0163             pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name);
0164             return ret;
0165         }
0166     }
0167 
0168     trace_module_request(module_name, wait, _RET_IP_);
0169 
0170     ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
0171 
0172     atomic_inc(&kmod_concurrent_max);
0173     wake_up(&kmod_wq);
0174 
0175     return ret;
0176 }
0177 EXPORT_SYMBOL(__request_module);