1
2
3
4#include <linux/module.h>
5#include <linux/sched.h>
6#include <linux/sched/task.h>
7#include <linux/binfmts.h>
8#include <linux/syscalls.h>
9#include <linux/unistd.h>
10#include <linux/kmod.h>
11#include <linux/slab.h>
12#include <linux/completion.h>
13#include <linux/cred.h>
14#include <linux/file.h>
15#include <linux/fdtable.h>
16#include <linux/workqueue.h>
17#include <linux/security.h>
18#include <linux/mount.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/resource.h>
22#include <linux/notifier.h>
23#include <linux/suspend.h>
24#include <linux/rwsem.h>
25#include <linux/ptrace.h>
26#include <linux/async.h>
27#include <linux/uaccess.h>
28
29#include <trace/events/module.h>
30
31
32
33
34
35
36
37
38
39
40
41
42#define MAX_KMOD_CONCURRENT 50
43static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT);
44static DECLARE_WAIT_QUEUE_HEAD(kmod_wq);
45
46
47
48
49
50
51
52
53
54
55
56#define MAX_KMOD_ALL_BUSY_TIMEOUT 5
57
58
59
60
61char modprobe_path[KMOD_PATH_LEN] = CONFIG_MODPROBE_PATH;
62
63static void free_modprobe_argv(struct subprocess_info *info)
64{
65 kfree(info->argv[3]);
66 kfree(info->argv);
67}
68
69static int call_modprobe(char *module_name, int wait)
70{
71 struct subprocess_info *info;
72 static char *envp[] = {
73 "HOME=/",
74 "TERM=linux",
75 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
76 NULL
77 };
78
79 char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
80 if (!argv)
81 goto out;
82
83 module_name = kstrdup(module_name, GFP_KERNEL);
84 if (!module_name)
85 goto free_argv;
86
87 argv[0] = modprobe_path;
88 argv[1] = "-q";
89 argv[2] = "--";
90 argv[3] = module_name;
91 argv[4] = NULL;
92
93 info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL,
94 NULL, free_modprobe_argv, NULL);
95 if (!info)
96 goto free_module_name;
97
98 return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
99
100free_module_name:
101 kfree(module_name);
102free_argv:
103 kfree(argv);
104out:
105 return -ENOMEM;
106}
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124int __request_module(bool wait, const char *fmt, ...)
125{
126 va_list args;
127 char module_name[MODULE_NAME_LEN];
128 int ret;
129
130
131
132
133
134
135
136 WARN_ON_ONCE(wait && current_is_async());
137
138 if (!modprobe_path[0])
139 return -ENOENT;
140
141 va_start(args, fmt);
142 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
143 va_end(args);
144 if (ret >= MODULE_NAME_LEN)
145 return -ENAMETOOLONG;
146
147 ret = security_kernel_module_request(module_name);
148 if (ret)
149 return ret;
150
151 if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) {
152 pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
153 atomic_read(&kmod_concurrent_max),
154 MAX_KMOD_CONCURRENT, module_name);
155 ret = wait_event_killable_timeout(kmod_wq,
156 atomic_dec_if_positive(&kmod_concurrent_max) >= 0,
157 MAX_KMOD_ALL_BUSY_TIMEOUT * HZ);
158 if (!ret) {
159 pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now",
160 module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT);
161 return -ETIME;
162 } else if (ret == -ERESTARTSYS) {
163 pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name);
164 return ret;
165 }
166 }
167
168 trace_module_request(module_name, wait, _RET_IP_);
169
170 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
171
172 atomic_inc(&kmod_concurrent_max);
173 wake_up(&kmod_wq);
174
175 return ret;
176}
177EXPORT_SYMBOL(__request_module);
178