Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Luis R. Rodriguez | 2355869 | 2017-09-08 16:17:00 -0700 | [diff] [blame] | 2 | * kmod - the kernel module loader |
| 3 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/module.h> |
| 5 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 6 | #include <linux/sched/task.h> |
Ingo Molnar | 5c2c5c5 | 2017-02-05 14:24:31 +0100 | [diff] [blame] | 7 | #include <linux/binfmts.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/syscalls.h> |
| 9 | #include <linux/unistd.h> |
| 10 | #include <linux/kmod.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/completion.h> |
Eric Paris | 17f60a7 | 2011-04-01 17:07:50 -0400 | [diff] [blame] | 13 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 15 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Andi Kleen | d025c9d | 2006-09-30 23:29:28 -0700 | [diff] [blame] | 21 | #include <linux/resource.h> |
Rafael J. Wysocki | 8cdd493 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 22 | #include <linux/notifier.h> |
| 23 | #include <linux/suspend.h> |
Srivatsa S. Bhat | b298d28 | 2011-12-09 23:36:36 +0100 | [diff] [blame] | 24 | #include <linux/rwsem.h> |
Al Viro | a74fb73 | 2012-10-10 21:28:25 -0400 | [diff] [blame] | 25 | #include <linux/ptrace.h> |
Tejun Heo | 0fdff3e | 2013-01-22 16:48:03 -0800 | [diff] [blame] | 26 | #include <linux/async.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Li Zefan | 7ead8b8 | 2009-08-17 16:56:28 +0800 | [diff] [blame] | 29 | #include <trace/events/module.h> |
| 30 | |
Luis R. Rodriguez | 165d1cc | 2017-06-23 12:19:12 -0700 | [diff] [blame] | 31 | /* |
| 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 |
Qiujun Huang | 06d4f81 | 2020-04-06 20:11:49 -0700 | [diff] [blame] | 38 | * smaller than 3200 pages. This assumes you are capable of having ~13M memory, |
Tiezhu Yang | 6f9e148 | 2020-08-11 18:36:12 -0700 | [diff] [blame] | 39 | * and this would only be an upper limit, after which the OOM killer would take |
| 40 | * effect. Systems like these are very unlikely if modules are enabled. |
Luis R. Rodriguez | 165d1cc | 2017-06-23 12:19:12 -0700 | [diff] [blame] | 41 | */ |
| 42 | #define MAX_KMOD_CONCURRENT 50 |
| 43 | static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT); |
Luis R. Rodriguez | 6d7964a | 2017-07-14 14:50:11 -0700 | [diff] [blame] | 44 | static DECLARE_WAIT_QUEUE_HEAD(kmod_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Luis R. Rodriguez | 2ba293c | 2017-08-18 15:15:58 -0700 | [diff] [blame] | 47 | * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads |
| 48 | * running at the same time without returning. When this happens we |
| 49 | * believe you've somehow ended up with a recursive module dependency |
| 50 | * creating a loop. |
| 51 | * |
| 52 | * We have no option but to fail. |
| 53 | * |
| 54 | * Userspace should proactively try to detect and prevent these. |
| 55 | */ |
| 56 | #define MAX_KMOD_ALL_BUSY_TIMEOUT 5 |
| 57 | |
| 58 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | modprobe_path is set via /proc/sys. |
| 60 | */ |
Rasmus Villemoes | 17652f4 | 2021-05-06 18:05:45 -0700 | [diff] [blame] | 61 | char modprobe_path[KMOD_PATH_LEN] = CONFIG_MODPROBE_PATH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Oleg Nesterov | 1cc684a | 2012-03-23 15:02:50 -0700 | [diff] [blame] | 63 | static void free_modprobe_argv(struct subprocess_info *info) |
| 64 | { |
| 65 | kfree(info->argv[3]); /* check call_modprobe() */ |
| 66 | kfree(info->argv); |
| 67 | } |
| 68 | |
Oleg Nesterov | 3e63a93 | 2012-03-23 15:02:49 -0700 | [diff] [blame] | 69 | static int call_modprobe(char *module_name, int wait) |
| 70 | { |
Lucas De Marchi | f634460 | 2013-04-30 15:28:03 -0700 | [diff] [blame] | 71 | struct subprocess_info *info; |
Oleg Nesterov | 3e63a93 | 2012-03-23 15:02:49 -0700 | [diff] [blame] | 72 | static char *envp[] = { |
| 73 | "HOME=/", |
| 74 | "TERM=linux", |
| 75 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 76 | NULL |
| 77 | }; |
| 78 | |
Oleg Nesterov | 1cc684a | 2012-03-23 15:02:50 -0700 | [diff] [blame] | 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; /* check free_modprobe_argv() */ |
| 91 | argv[4] = NULL; |
Oleg Nesterov | 3e63a93 | 2012-03-23 15:02:49 -0700 | [diff] [blame] | 92 | |
Lucas De Marchi | f634460 | 2013-04-30 15:28:03 -0700 | [diff] [blame] | 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 | |
| 100 | free_module_name: |
| 101 | kfree(module_name); |
Oleg Nesterov | 1cc684a | 2012-03-23 15:02:50 -0700 | [diff] [blame] | 102 | free_argv: |
| 103 | kfree(argv); |
| 104 | out: |
| 105 | return -ENOMEM; |
Oleg Nesterov | 3e63a93 | 2012-03-23 15:02:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /** |
Arjan van de Ven | acae051 | 2009-02-08 10:42:01 -0800 | [diff] [blame] | 109 | * __request_module - try to load a kernel module |
| 110 | * @wait: wait (or not) for the operation to complete |
Randy Dunlap | bd4207c | 2009-01-06 14:42:39 -0800 | [diff] [blame] | 111 | * @fmt: printf style format string for the name of the module |
| 112 | * @...: arguments as specified in the format string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * |
| 114 | * Load a module using the user mode module loader. The function returns |
NeilBrown | 60b61a6 | 2015-09-09 15:38:10 -0700 | [diff] [blame] | 115 | * zero on success or a negative errno code or positive exit code from |
| 116 | * "modprobe" on failure. Note that a successful module load does not mean |
| 117 | * the module did not then unload and exit on an error of its own. Callers |
| 118 | * must check that the service they requested is now available not blindly |
| 119 | * invoke it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * |
| 121 | * If module auto-loading support is disabled then this function |
Eric Biggers | d7d27cf | 2020-04-10 14:33:43 -0700 | [diff] [blame] | 122 | * simply returns -ENOENT. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | */ |
Arjan van de Ven | acae051 | 2009-02-08 10:42:01 -0800 | [diff] [blame] | 124 | int __request_module(bool wait, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | va_list args; |
| 127 | char module_name[MODULE_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Tejun Heo | 0fdff3e | 2013-01-22 16:48:03 -0800 | [diff] [blame] | 130 | /* |
| 131 | * We don't allow synchronous module loading from async. Module |
| 132 | * init may invoke async_synchronize_full() which will end up |
| 133 | * waiting for this task which already is waiting for the module |
| 134 | * loading to complete, leading to a deadlock. |
| 135 | */ |
| 136 | WARN_ON_ONCE(wait && current_is_async()); |
| 137 | |
Oleg Nesterov | 7f57cfa | 2013-07-03 15:08:15 -0700 | [diff] [blame] | 138 | if (!modprobe_path[0]) |
Eric Biggers | d7d27cf | 2020-04-10 14:33:43 -0700 | [diff] [blame] | 139 | return -ENOENT; |
Oleg Nesterov | 7f57cfa | 2013-07-03 15:08:15 -0700 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 147 | ret = security_kernel_module_request(module_name); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | |
Luis R. Rodriguez | 165d1cc | 2017-06-23 12:19:12 -0700 | [diff] [blame] | 151 | if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { |
Luis R. Rodriguez | 6d7964a | 2017-07-14 14:50:11 -0700 | [diff] [blame] | 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); |
Luis R. Rodriguez | 2ba293c | 2017-08-18 15:15:58 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Li Zefan | 7ead8b8 | 2009-08-17 16:56:28 +0800 | [diff] [blame] | 168 | trace_module_request(module_name, wait, _RET_IP_); |
| 169 | |
Oleg Nesterov | 3e63a93 | 2012-03-23 15:02:49 -0700 | [diff] [blame] | 170 | ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); |
Neil Horman | a06a4dc | 2010-05-26 14:42:58 -0700 | [diff] [blame] | 171 | |
Luis R. Rodriguez | 165d1cc | 2017-06-23 12:19:12 -0700 | [diff] [blame] | 172 | atomic_inc(&kmod_concurrent_max); |
Luis R. Rodriguez | 6d7964a | 2017-07-14 14:50:11 -0700 | [diff] [blame] | 173 | wake_up(&kmod_wq); |
Luis R. Rodriguez | 165d1cc | 2017-06-23 12:19:12 -0700 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return ret; |
| 176 | } |
Arjan van de Ven | acae051 | 2009-02-08 10:42:01 -0800 | [diff] [blame] | 177 | EXPORT_SYMBOL(__request_module); |