0001
0002
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
0033
0034
0035
0036
0037
0038
0039
0040
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
0048
0049
0050
0051
0052
0053
0054
0055
0056 #define MAX_KMOD_ALL_BUSY_TIMEOUT 5
0057
0058
0059
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]);
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;
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
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
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
0132
0133
0134
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);