1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * kmod dups - the kernel module autoloader duplicate suppressor
8 #define pr_fmt(fmt) "module: " fmt
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/sched/task.h>
13 #include <linux/binfmts.h>
14 #include <linux/syscalls.h>
15 #include <linux/unistd.h>
16 #include <linux/kmod.h>
17 #include <linux/slab.h>
18 #include <linux/completion.h>
19 #include <linux/cred.h>
20 #include <linux/file.h>
21 #include <linux/fdtable.h>
22 #include <linux/workqueue.h>
23 #include <linux/security.h>
24 #include <linux/mount.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/resource.h>
28 #include <linux/notifier.h>
29 #include <linux/suspend.h>
30 #include <linux/rwsem.h>
31 #include <linux/ptrace.h>
32 #include <linux/async.h>
33 #include <linux/uaccess.h>
37 #undef MODULE_PARAM_PREFIX
38 #define MODULE_PARAM_PREFIX "module."
39 static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);
40 module_param(enable_dups_trace, bool_enable_only, 0644);
43 * Protects dup_kmod_reqs list, adds / removals with RCU.
45 static DEFINE_MUTEX(kmod_dup_mutex);
46 static LIST_HEAD(dup_kmod_reqs);
49 struct list_head list;
50 char name[MODULE_NAME_LEN];
51 struct completion first_req_done;
52 struct work_struct complete_work;
53 struct delayed_work delete_work;
57 static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)
59 struct kmod_dup_req *kmod_req;
61 list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list,
62 lockdep_is_held(&kmod_dup_mutex)) {
63 if (strlen(kmod_req->name) == strlen(module_name) &&
64 !memcmp(kmod_req->name, module_name, strlen(module_name))) {
72 static void kmod_dup_request_delete(struct work_struct *work)
74 struct kmod_dup_req *kmod_req;
75 kmod_req = container_of(to_delayed_work(work), struct kmod_dup_req, delete_work);
78 * The typical situation is a module successully loaded. In that
79 * situation the module will be present already in userspace. If
80 * new requests come in after that, userspace will already know the
81 * module is loaded so will just return 0 right away. There is still
82 * a small chance right after we delete this entry new request_module()
83 * calls may happen after that, they can happen. These heuristics
84 * are to protect finit_module() abuse for auto-loading, if modules
85 * are still tryign to auto-load even if a module is already loaded,
86 * that's on them, and those inneficiencies should not be fixed by
87 * kmod. The inneficies there are a call to modprobe and modprobe
90 mutex_lock(&kmod_dup_mutex);
91 list_del_rcu(&kmod_req->list);
93 mutex_unlock(&kmod_dup_mutex);
97 static void kmod_dup_request_complete(struct work_struct *work)
99 struct kmod_dup_req *kmod_req;
101 kmod_req = container_of(work, struct kmod_dup_req, complete_work);
104 * This will ensure that the kernel will let all the waiters get
105 * informed its time to check the return value. It's time to
108 complete_all(&kmod_req->first_req_done);
111 * Now that we have allowed prior request_module() calls to go on
112 * with life, let's schedule deleting this entry. We don't have
113 * to do it right away, but we *eventually* want to do it so to not
114 * let this linger forever as this is just a boot optimization for
115 * possible abuses of vmalloc() incurred by finit_module() thrashing.
117 queue_delayed_work(system_wq, &kmod_req->delete_work, 60 * HZ);
120 bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
122 struct kmod_dup_req *kmod_req, *new_kmod_req;
126 * Pre-allocate the entry in case we have to use it later
127 * to avoid contention with the mutex.
129 new_kmod_req = kzalloc(sizeof(*new_kmod_req), GFP_KERNEL);
133 memcpy(new_kmod_req->name, module_name, strlen(module_name));
134 INIT_WORK(&new_kmod_req->complete_work, kmod_dup_request_complete);
135 INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete);
136 init_completion(&new_kmod_req->first_req_done);
138 mutex_lock(&kmod_dup_mutex);
140 kmod_req = kmod_dup_request_lookup(module_name);
143 * If the first request that came through for a module
144 * was with request_module_nowait() we cannot wait for it
145 * and share its return value with other users which may
146 * have used request_module() and need a proper return value
147 * so just skip using them as an anchor.
149 * If a prior request to this one came through with
150 * request_module() though, then a request_module_nowait()
151 * would benefit from duplicate detection.
155 pr_debug("New request_module_nowait() for %s -- cannot track duplicates for this request\n", module_name);
156 mutex_unlock(&kmod_dup_mutex);
161 * There was no duplicate, just add the request so we can
162 * keep tab on duplicates later.
164 pr_debug("New request_module() for %s\n", module_name);
165 list_add_rcu(&new_kmod_req->list, &dup_kmod_reqs);
166 mutex_unlock(&kmod_dup_mutex);
169 mutex_unlock(&kmod_dup_mutex);
171 /* We are dealing with a duplicate request now */
175 * To fix these try to use try_then_request_module() instead as that
176 * will check if the component you are looking for is present or not.
177 * You could also just queue a single request to load the module once,
178 * instead of having each and everything you need try to request for
181 * Duplicate request_module() calls can cause quite a bit of wasted
182 * vmalloc() space when racing with userspace.
184 if (enable_dups_trace)
185 WARN(1, "module-autoload: duplicate request for module %s\n", module_name);
187 pr_warn("module-autoload: duplicate request for module %s\n", module_name);
191 * If request_module_nowait() was used then the user just
192 * wanted to issue the request and if another module request
193 * was already its way with the same name we don't care for
194 * the return value either. Let duplicate request_module_nowait()
195 * calls bail out right away.
202 * If a duplicate request_module() was used they *may* care for
203 * the return value, so we have no other option but to wait for
204 * the first caller to complete. If the first caller used
205 * the request_module_nowait() call, subsquent callers will
206 * deal with the comprmise of getting a successful call with this
207 * optimization enabled ...
209 ret = wait_for_completion_state(&kmod_req->first_req_done,
216 /* Now the duplicate request has the same exact return value as the first request */
217 *dup_ret = kmod_req->dup_ret;
222 void kmod_dup_request_announce(char *module_name, int ret)
224 struct kmod_dup_req *kmod_req;
226 mutex_lock(&kmod_dup_mutex);
228 kmod_req = kmod_dup_request_lookup(module_name);
232 kmod_req->dup_ret = ret;
235 * If we complete() here we may allow duplicate threads
236 * to continue before the first one that submitted the
237 * request. We're in no rush also, given that each and
238 * every bounce back to userspace is slow we avoid that
239 * with a slight delay here. So queueue up the completion
240 * and let duplicates suffer, just wait a tad bit longer.
241 * There is no rush. But we also don't want to hold the
242 * caller up forever or introduce any boot delays.
244 queue_work(system_wq, &kmod_req->complete_work);
247 mutex_unlock(&kmod_dup_mutex);