18660484eSLuis Chamberlain /* SPDX-License-Identifier: GPL-2.0-or-later */
28660484eSLuis Chamberlain /*
38660484eSLuis Chamberlain * kmod dups - the kernel module autoloader duplicate suppressor
48660484eSLuis Chamberlain *
58660484eSLuis Chamberlain * Copyright (C) 2023 Luis Chamberlain <[email protected]>
68660484eSLuis Chamberlain */
78660484eSLuis Chamberlain
88660484eSLuis Chamberlain #define pr_fmt(fmt) "module: " fmt
98660484eSLuis Chamberlain
108660484eSLuis Chamberlain #include <linux/module.h>
118660484eSLuis Chamberlain #include <linux/sched.h>
128660484eSLuis Chamberlain #include <linux/sched/task.h>
138660484eSLuis Chamberlain #include <linux/binfmts.h>
148660484eSLuis Chamberlain #include <linux/syscalls.h>
158660484eSLuis Chamberlain #include <linux/unistd.h>
168660484eSLuis Chamberlain #include <linux/kmod.h>
178660484eSLuis Chamberlain #include <linux/slab.h>
188660484eSLuis Chamberlain #include <linux/completion.h>
198660484eSLuis Chamberlain #include <linux/cred.h>
208660484eSLuis Chamberlain #include <linux/file.h>
218660484eSLuis Chamberlain #include <linux/workqueue.h>
228660484eSLuis Chamberlain #include <linux/security.h>
238660484eSLuis Chamberlain #include <linux/mount.h>
248660484eSLuis Chamberlain #include <linux/kernel.h>
258660484eSLuis Chamberlain #include <linux/init.h>
268660484eSLuis Chamberlain #include <linux/resource.h>
278660484eSLuis Chamberlain #include <linux/notifier.h>
288660484eSLuis Chamberlain #include <linux/suspend.h>
298660484eSLuis Chamberlain #include <linux/rwsem.h>
308660484eSLuis Chamberlain #include <linux/ptrace.h>
318660484eSLuis Chamberlain #include <linux/async.h>
328660484eSLuis Chamberlain #include <linux/uaccess.h>
338660484eSLuis Chamberlain
340b891c83SArnd Bergmann #include "internal.h"
350b891c83SArnd Bergmann
368660484eSLuis Chamberlain #undef MODULE_PARAM_PREFIX
378660484eSLuis Chamberlain #define MODULE_PARAM_PREFIX "module."
388660484eSLuis Chamberlain static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);
398660484eSLuis Chamberlain module_param(enable_dups_trace, bool_enable_only, 0644);
408660484eSLuis Chamberlain
418660484eSLuis Chamberlain /*
428660484eSLuis Chamberlain * Protects dup_kmod_reqs list, adds / removals with RCU.
438660484eSLuis Chamberlain */
448660484eSLuis Chamberlain static DEFINE_MUTEX(kmod_dup_mutex);
458660484eSLuis Chamberlain static LIST_HEAD(dup_kmod_reqs);
468660484eSLuis Chamberlain
478660484eSLuis Chamberlain struct kmod_dup_req {
488660484eSLuis Chamberlain struct list_head list;
498660484eSLuis Chamberlain char name[MODULE_NAME_LEN];
508660484eSLuis Chamberlain struct completion first_req_done;
518660484eSLuis Chamberlain struct work_struct complete_work;
528660484eSLuis Chamberlain struct delayed_work delete_work;
538660484eSLuis Chamberlain int dup_ret;
548660484eSLuis Chamberlain };
558660484eSLuis Chamberlain
kmod_dup_request_lookup(char * module_name)568660484eSLuis Chamberlain static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)
578660484eSLuis Chamberlain {
588660484eSLuis Chamberlain struct kmod_dup_req *kmod_req;
598660484eSLuis Chamberlain
608660484eSLuis Chamberlain list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list,
618660484eSLuis Chamberlain lockdep_is_held(&kmod_dup_mutex)) {
628660484eSLuis Chamberlain if (strlen(kmod_req->name) == strlen(module_name) &&
638660484eSLuis Chamberlain !memcmp(kmod_req->name, module_name, strlen(module_name))) {
648660484eSLuis Chamberlain return kmod_req;
658660484eSLuis Chamberlain }
668660484eSLuis Chamberlain }
678660484eSLuis Chamberlain
688660484eSLuis Chamberlain return NULL;
698660484eSLuis Chamberlain }
708660484eSLuis Chamberlain
kmod_dup_request_delete(struct work_struct * work)718660484eSLuis Chamberlain static void kmod_dup_request_delete(struct work_struct *work)
728660484eSLuis Chamberlain {
738660484eSLuis Chamberlain struct kmod_dup_req *kmod_req;
748660484eSLuis Chamberlain kmod_req = container_of(to_delayed_work(work), struct kmod_dup_req, delete_work);
758660484eSLuis Chamberlain
768660484eSLuis Chamberlain /*
778660484eSLuis Chamberlain * The typical situation is a module successully loaded. In that
788660484eSLuis Chamberlain * situation the module will be present already in userspace. If
798660484eSLuis Chamberlain * new requests come in after that, userspace will already know the
808660484eSLuis Chamberlain * module is loaded so will just return 0 right away. There is still
818660484eSLuis Chamberlain * a small chance right after we delete this entry new request_module()
828660484eSLuis Chamberlain * calls may happen after that, they can happen. These heuristics
838660484eSLuis Chamberlain * are to protect finit_module() abuse for auto-loading, if modules
848660484eSLuis Chamberlain * are still tryign to auto-load even if a module is already loaded,
858660484eSLuis Chamberlain * that's on them, and those inneficiencies should not be fixed by
868660484eSLuis Chamberlain * kmod. The inneficies there are a call to modprobe and modprobe
878660484eSLuis Chamberlain * just returning 0.
888660484eSLuis Chamberlain */
898660484eSLuis Chamberlain mutex_lock(&kmod_dup_mutex);
908660484eSLuis Chamberlain list_del_rcu(&kmod_req->list);
918660484eSLuis Chamberlain synchronize_rcu();
928660484eSLuis Chamberlain mutex_unlock(&kmod_dup_mutex);
938660484eSLuis Chamberlain kfree(kmod_req);
948660484eSLuis Chamberlain }
958660484eSLuis Chamberlain
kmod_dup_request_complete(struct work_struct * work)968660484eSLuis Chamberlain static void kmod_dup_request_complete(struct work_struct *work)
978660484eSLuis Chamberlain {
988660484eSLuis Chamberlain struct kmod_dup_req *kmod_req;
998660484eSLuis Chamberlain
1008660484eSLuis Chamberlain kmod_req = container_of(work, struct kmod_dup_req, complete_work);
1018660484eSLuis Chamberlain
1028660484eSLuis Chamberlain /*
1038660484eSLuis Chamberlain * This will ensure that the kernel will let all the waiters get
1048660484eSLuis Chamberlain * informed its time to check the return value. It's time to
1058660484eSLuis Chamberlain * go home.
1068660484eSLuis Chamberlain */
1078660484eSLuis Chamberlain complete_all(&kmod_req->first_req_done);
1088660484eSLuis Chamberlain
1098660484eSLuis Chamberlain /*
1108660484eSLuis Chamberlain * Now that we have allowed prior request_module() calls to go on
1118660484eSLuis Chamberlain * with life, let's schedule deleting this entry. We don't have
1128660484eSLuis Chamberlain * to do it right away, but we *eventually* want to do it so to not
1138660484eSLuis Chamberlain * let this linger forever as this is just a boot optimization for
1148660484eSLuis Chamberlain * possible abuses of vmalloc() incurred by finit_module() thrashing.
1158660484eSLuis Chamberlain */
1168660484eSLuis Chamberlain queue_delayed_work(system_wq, &kmod_req->delete_work, 60 * HZ);
1178660484eSLuis Chamberlain }
1188660484eSLuis Chamberlain
kmod_dup_request_exists_wait(char * module_name,bool wait,int * dup_ret)1198660484eSLuis Chamberlain bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
1208660484eSLuis Chamberlain {
1218660484eSLuis Chamberlain struct kmod_dup_req *kmod_req, *new_kmod_req;
1228660484eSLuis Chamberlain int ret;
1238660484eSLuis Chamberlain
1248660484eSLuis Chamberlain /*
1258660484eSLuis Chamberlain * Pre-allocate the entry in case we have to use it later
1268660484eSLuis Chamberlain * to avoid contention with the mutex.
1278660484eSLuis Chamberlain */
1288660484eSLuis Chamberlain new_kmod_req = kzalloc(sizeof(*new_kmod_req), GFP_KERNEL);
1298660484eSLuis Chamberlain if (!new_kmod_req)
1308660484eSLuis Chamberlain return false;
1318660484eSLuis Chamberlain
1328660484eSLuis Chamberlain memcpy(new_kmod_req->name, module_name, strlen(module_name));
1338660484eSLuis Chamberlain INIT_WORK(&new_kmod_req->complete_work, kmod_dup_request_complete);
1348660484eSLuis Chamberlain INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete);
1358660484eSLuis Chamberlain init_completion(&new_kmod_req->first_req_done);
1368660484eSLuis Chamberlain
1378660484eSLuis Chamberlain mutex_lock(&kmod_dup_mutex);
1388660484eSLuis Chamberlain
1398660484eSLuis Chamberlain kmod_req = kmod_dup_request_lookup(module_name);
1408660484eSLuis Chamberlain if (!kmod_req) {
1418660484eSLuis Chamberlain /*
1428660484eSLuis Chamberlain * If the first request that came through for a module
1438660484eSLuis Chamberlain * was with request_module_nowait() we cannot wait for it
1448660484eSLuis Chamberlain * and share its return value with other users which may
1458660484eSLuis Chamberlain * have used request_module() and need a proper return value
1468660484eSLuis Chamberlain * so just skip using them as an anchor.
1478660484eSLuis Chamberlain *
1488660484eSLuis Chamberlain * If a prior request to this one came through with
1498660484eSLuis Chamberlain * request_module() though, then a request_module_nowait()
1508660484eSLuis Chamberlain * would benefit from duplicate detection.
1518660484eSLuis Chamberlain */
1528660484eSLuis Chamberlain if (!wait) {
1538660484eSLuis Chamberlain kfree(new_kmod_req);
1548660484eSLuis Chamberlain pr_debug("New request_module_nowait() for %s -- cannot track duplicates for this request\n", module_name);
1558660484eSLuis Chamberlain mutex_unlock(&kmod_dup_mutex);
1568660484eSLuis Chamberlain return false;
1578660484eSLuis Chamberlain }
1588660484eSLuis Chamberlain
1598660484eSLuis Chamberlain /*
1608660484eSLuis Chamberlain * There was no duplicate, just add the request so we can
1618660484eSLuis Chamberlain * keep tab on duplicates later.
1628660484eSLuis Chamberlain */
1638660484eSLuis Chamberlain pr_debug("New request_module() for %s\n", module_name);
1648660484eSLuis Chamberlain list_add_rcu(&new_kmod_req->list, &dup_kmod_reqs);
1658660484eSLuis Chamberlain mutex_unlock(&kmod_dup_mutex);
1668660484eSLuis Chamberlain return false;
1678660484eSLuis Chamberlain }
1688660484eSLuis Chamberlain mutex_unlock(&kmod_dup_mutex);
1698660484eSLuis Chamberlain
1708660484eSLuis Chamberlain /* We are dealing with a duplicate request now */
1718660484eSLuis Chamberlain kfree(new_kmod_req);
1728660484eSLuis Chamberlain
1738660484eSLuis Chamberlain /*
1748660484eSLuis Chamberlain * To fix these try to use try_then_request_module() instead as that
1758660484eSLuis Chamberlain * will check if the component you are looking for is present or not.
1768660484eSLuis Chamberlain * You could also just queue a single request to load the module once,
1778660484eSLuis Chamberlain * instead of having each and everything you need try to request for
1788660484eSLuis Chamberlain * the module.
1798660484eSLuis Chamberlain *
1808660484eSLuis Chamberlain * Duplicate request_module() calls can cause quite a bit of wasted
1818660484eSLuis Chamberlain * vmalloc() space when racing with userspace.
1828660484eSLuis Chamberlain */
1838660484eSLuis Chamberlain if (enable_dups_trace)
1848660484eSLuis Chamberlain WARN(1, "module-autoload: duplicate request for module %s\n", module_name);
1858660484eSLuis Chamberlain else
1868660484eSLuis Chamberlain pr_warn("module-autoload: duplicate request for module %s\n", module_name);
1878660484eSLuis Chamberlain
1888660484eSLuis Chamberlain if (!wait) {
1898660484eSLuis Chamberlain /*
1908660484eSLuis Chamberlain * If request_module_nowait() was used then the user just
1918660484eSLuis Chamberlain * wanted to issue the request and if another module request
1928660484eSLuis Chamberlain * was already its way with the same name we don't care for
1938660484eSLuis Chamberlain * the return value either. Let duplicate request_module_nowait()
1948660484eSLuis Chamberlain * calls bail out right away.
1958660484eSLuis Chamberlain */
1968660484eSLuis Chamberlain *dup_ret = 0;
1978660484eSLuis Chamberlain return true;
1988660484eSLuis Chamberlain }
1998660484eSLuis Chamberlain
2008660484eSLuis Chamberlain /*
2018660484eSLuis Chamberlain * If a duplicate request_module() was used they *may* care for
2028660484eSLuis Chamberlain * the return value, so we have no other option but to wait for
2038660484eSLuis Chamberlain * the first caller to complete. If the first caller used
2048660484eSLuis Chamberlain * the request_module_nowait() call, subsquent callers will
2058660484eSLuis Chamberlain * deal with the comprmise of getting a successful call with this
2068660484eSLuis Chamberlain * optimization enabled ...
2078660484eSLuis Chamberlain */
2088660484eSLuis Chamberlain ret = wait_for_completion_state(&kmod_req->first_req_done,
209*f17f2c13SKevin Hao TASK_KILLABLE);
2108660484eSLuis Chamberlain if (ret) {
2118660484eSLuis Chamberlain *dup_ret = ret;
2128660484eSLuis Chamberlain return true;
2138660484eSLuis Chamberlain }
2148660484eSLuis Chamberlain
2158660484eSLuis Chamberlain /* Now the duplicate request has the same exact return value as the first request */
2168660484eSLuis Chamberlain *dup_ret = kmod_req->dup_ret;
2178660484eSLuis Chamberlain
2188660484eSLuis Chamberlain return true;
2198660484eSLuis Chamberlain }
2208660484eSLuis Chamberlain
kmod_dup_request_announce(char * module_name,int ret)2218660484eSLuis Chamberlain void kmod_dup_request_announce(char *module_name, int ret)
2228660484eSLuis Chamberlain {
2238660484eSLuis Chamberlain struct kmod_dup_req *kmod_req;
2248660484eSLuis Chamberlain
2258660484eSLuis Chamberlain mutex_lock(&kmod_dup_mutex);
2268660484eSLuis Chamberlain
2278660484eSLuis Chamberlain kmod_req = kmod_dup_request_lookup(module_name);
2288660484eSLuis Chamberlain if (!kmod_req)
2298660484eSLuis Chamberlain goto out;
2308660484eSLuis Chamberlain
2318660484eSLuis Chamberlain kmod_req->dup_ret = ret;
2328660484eSLuis Chamberlain
2338660484eSLuis Chamberlain /*
2348660484eSLuis Chamberlain * If we complete() here we may allow duplicate threads
2358660484eSLuis Chamberlain * to continue before the first one that submitted the
2368660484eSLuis Chamberlain * request. We're in no rush also, given that each and
2378660484eSLuis Chamberlain * every bounce back to userspace is slow we avoid that
2388660484eSLuis Chamberlain * with a slight delay here. So queueue up the completion
2398660484eSLuis Chamberlain * and let duplicates suffer, just wait a tad bit longer.
2408660484eSLuis Chamberlain * There is no rush. But we also don't want to hold the
2418660484eSLuis Chamberlain * caller up forever or introduce any boot delays.
2428660484eSLuis Chamberlain */
2438660484eSLuis Chamberlain queue_work(system_wq, &kmod_req->complete_work);
2448660484eSLuis Chamberlain
2458660484eSLuis Chamberlain out:
2468660484eSLuis Chamberlain mutex_unlock(&kmod_dup_mutex);
2478660484eSLuis Chamberlain }
248