blob: bc6addd9152b4db58c57757f1fe0a2911a16e459 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Luis R. Rodriguez23558692017-09-08 16:17:00 -07002 * kmod - the kernel module loader
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/module.h>
5#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +01006#include <linux/sched/task.h>
Ingo Molnar5c2c5c52017-02-05 14:24:31 +01007#include <linux/binfmts.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/syscalls.h>
9#include <linux/unistd.h>
10#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/completion.h>
Eric Paris17f60a72011-04-01 17:07:50 -040013#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040015#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/workqueue.h>
17#include <linux/security.h>
18#include <linux/mount.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
Andi Kleend025c9d2006-09-30 23:29:28 -070021#include <linux/resource.h>
Rafael J. Wysocki8cdd4932007-07-19 01:47:36 -070022#include <linux/notifier.h>
23#include <linux/suspend.h>
Srivatsa S. Bhatb298d282011-12-09 23:36:36 +010024#include <linux/rwsem.h>
Al Viroa74fb732012-10-10 21:28:25 -040025#include <linux/ptrace.h>
Tejun Heo0fdff3e2013-01-22 16:48:03 -080026#include <linux/async.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Li Zefan7ead8b82009-08-17 16:56:28 +080029#include <trace/events/module.h>
30
Luis R. Rodriguez165d1cc2017-06-23 12:19:12 -070031/*
32 * Assuming:
33 *
34 * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
35 * (u64) THREAD_SIZE * 8UL);
36 *
37 * If you need less than 50 threads would mean we're dealing with systems
38 * smaller than 3200 pages. This assuems you are capable of having ~13M memory,
39 * and this would only be an be an upper limit, after which the OOM killer
40 * would take effect. Systems like these are very unlikely if modules are
41 * enabled.
42 */
43#define MAX_KMOD_CONCURRENT 50
44static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT);
Luis R. Rodriguez6d7964a2017-07-14 14:50:11 -070045static DECLARE_WAIT_QUEUE_HEAD(kmod_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
Luis R. Rodriguez2ba293c2017-08-18 15:15:58 -070048 * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads
49 * running at the same time without returning. When this happens we
50 * believe you've somehow ended up with a recursive module dependency
51 * creating a loop.
52 *
53 * We have no option but to fail.
54 *
55 * Userspace should proactively try to detect and prevent these.
56 */
57#define MAX_KMOD_ALL_BUSY_TIMEOUT 5
58
59/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 modprobe_path is set via /proc/sys.
61*/
62char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
63
Oleg Nesterov1cc684a2012-03-23 15:02:50 -070064static void free_modprobe_argv(struct subprocess_info *info)
65{
66 kfree(info->argv[3]); /* check call_modprobe() */
67 kfree(info->argv);
68}
69
Oleg Nesterov3e63a932012-03-23 15:02:49 -070070static int call_modprobe(char *module_name, int wait)
71{
Lucas De Marchif6344602013-04-30 15:28:03 -070072 struct subprocess_info *info;
Oleg Nesterov3e63a932012-03-23 15:02:49 -070073 static char *envp[] = {
74 "HOME=/",
75 "TERM=linux",
76 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
77 NULL
78 };
79
Oleg Nesterov1cc684a2012-03-23 15:02:50 -070080 char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
81 if (!argv)
82 goto out;
83
84 module_name = kstrdup(module_name, GFP_KERNEL);
85 if (!module_name)
86 goto free_argv;
87
88 argv[0] = modprobe_path;
89 argv[1] = "-q";
90 argv[2] = "--";
91 argv[3] = module_name; /* check free_modprobe_argv() */
92 argv[4] = NULL;
Oleg Nesterov3e63a932012-03-23 15:02:49 -070093
Lucas De Marchif6344602013-04-30 15:28:03 -070094 info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL,
95 NULL, free_modprobe_argv, NULL);
96 if (!info)
97 goto free_module_name;
98
99 return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
100
101free_module_name:
102 kfree(module_name);
Oleg Nesterov1cc684a2012-03-23 15:02:50 -0700103free_argv:
104 kfree(argv);
105out:
106 return -ENOMEM;
Oleg Nesterov3e63a932012-03-23 15:02:49 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/**
Arjan van de Venacae0512009-02-08 10:42:01 -0800110 * __request_module - try to load a kernel module
111 * @wait: wait (or not) for the operation to complete
Randy Dunlapbd4207c2009-01-06 14:42:39 -0800112 * @fmt: printf style format string for the name of the module
113 * @...: arguments as specified in the format string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
115 * Load a module using the user mode module loader. The function returns
NeilBrown60b61a62015-09-09 15:38:10 -0700116 * zero on success or a negative errno code or positive exit code from
117 * "modprobe" on failure. Note that a successful module load does not mean
118 * the module did not then unload and exit on an error of its own. Callers
119 * must check that the service they requested is now available not blindly
120 * invoke it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
122 * If module auto-loading support is disabled then this function
123 * becomes a no-operation.
124 */
Arjan van de Venacae0512009-02-08 10:42:01 -0800125int __request_module(bool wait, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 va_list args;
128 char module_name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Tejun Heo0fdff3e2013-01-22 16:48:03 -0800131 /*
132 * We don't allow synchronous module loading from async. Module
133 * init may invoke async_synchronize_full() which will end up
134 * waiting for this task which already is waiting for the module
135 * loading to complete, leading to a deadlock.
136 */
137 WARN_ON_ONCE(wait && current_is_async());
138
Oleg Nesterov7f57cfa2013-07-03 15:08:15 -0700139 if (!modprobe_path[0])
140 return 0;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 va_start(args, fmt);
143 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
144 va_end(args);
145 if (ret >= MODULE_NAME_LEN)
146 return -ENAMETOOLONG;
147
Eric Parisdd8dbf22009-11-03 16:35:32 +1100148 ret = security_kernel_module_request(module_name);
149 if (ret)
150 return ret;
151
Luis R. Rodriguez165d1cc2017-06-23 12:19:12 -0700152 if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) {
Luis R. Rodriguez6d7964a2017-07-14 14:50:11 -0700153 pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...",
154 atomic_read(&kmod_concurrent_max),
155 MAX_KMOD_CONCURRENT, module_name);
Luis R. Rodriguez2ba293c2017-08-18 15:15:58 -0700156 ret = wait_event_killable_timeout(kmod_wq,
157 atomic_dec_if_positive(&kmod_concurrent_max) >= 0,
158 MAX_KMOD_ALL_BUSY_TIMEOUT * HZ);
159 if (!ret) {
160 pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now",
161 module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT);
162 return -ETIME;
163 } else if (ret == -ERESTARTSYS) {
164 pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name);
165 return ret;
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
Li Zefan7ead8b82009-08-17 16:56:28 +0800169 trace_module_request(module_name, wait, _RET_IP_);
170
Oleg Nesterov3e63a932012-03-23 15:02:49 -0700171 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
Neil Hormana06a4dc2010-05-26 14:42:58 -0700172
Luis R. Rodriguez165d1cc2017-06-23 12:19:12 -0700173 atomic_inc(&kmod_concurrent_max);
Luis R. Rodriguez6d7964a2017-07-14 14:50:11 -0700174 wake_up(&kmod_wq);
Luis R. Rodriguez165d1cc2017-06-23 12:19:12 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return ret;
177}
Arjan van de Venacae0512009-02-08 10:42:01 -0800178EXPORT_SYMBOL(__request_module);