1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * kmod - the kernel module loader 4 * 5 * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/sched.h> 10 #include <linux/sched/task.h> 11 #include <linux/binfmts.h> 12 #include <linux/syscalls.h> 13 #include <linux/unistd.h> 14 #include <linux/kmod.h> 15 #include <linux/slab.h> 16 #include <linux/completion.h> 17 #include <linux/cred.h> 18 #include <linux/file.h> 19 #include <linux/workqueue.h> 20 #include <linux/security.h> 21 #include <linux/mount.h> 22 #include <linux/kernel.h> 23 #include <linux/init.h> 24 #include <linux/resource.h> 25 #include <linux/notifier.h> 26 #include <linux/suspend.h> 27 #include <linux/rwsem.h> 28 #include <linux/ptrace.h> 29 #include <linux/async.h> 30 #include <linux/uaccess.h> 31 #include <linux/umh.h> 32 33 #include <trace/events/module.h> 34 #include "internal.h" 35 36 /* 37 * Assuming: 38 * 39 * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, 40 * (u64) THREAD_SIZE * 8UL); 41 * 42 * If you need less than 50 threads would mean we're dealing with systems 43 * smaller than 3200 pages. This assumes you are capable of having ~13M memory, 44 * and this would only be an upper limit, after which the OOM killer would take 45 * effect. Systems like these are very unlikely if modules are enabled. 46 */ 47 #define MAX_KMOD_CONCURRENT 50 48 static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT); 49 50 /* 51 * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads 52 * running at the same time without returning. When this happens we 53 * believe you've somehow ended up with a recursive module dependency 54 * creating a loop. 55 * 56 * We have no option but to fail. 57 * 58 * Userspace should proactively try to detect and prevent these. 59 */ 60 #define MAX_KMOD_ALL_BUSY_TIMEOUT 5 61 62 /* 63 modprobe_path is set via /proc/sys. 64 */ 65 char modprobe_path[KMOD_PATH_LEN] = CONFIG_MODPROBE_PATH; 66 67 static void free_modprobe_argv(struct subprocess_info *info) 68 { 69 kfree(info->argv[3]); /* check call_modprobe() */ 70 kfree(info->argv); 71 } 72 73 static int call_modprobe(char *orig_module_name, int wait) 74 { 75 struct subprocess_info *info; 76 static char *envp[] = { 77 "HOME=/", 78 "TERM=linux", 79 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", 80 NULL 81 }; 82 char *module_name; 83 int ret; 84 85 char **argv = kmalloc_objs(*argv, 5); 86 if (!argv) 87 goto out; 88 89 module_name = kstrdup(orig_module_name, GFP_KERNEL); 90 if (!module_name) 91 goto free_argv; 92 93 argv[0] = modprobe_path; 94 argv[1] = "-q"; 95 argv[2] = "--"; 96 argv[3] = module_name; /* check free_modprobe_argv() */ 97 argv[4] = NULL; 98 99 info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL, 100 NULL, free_modprobe_argv, NULL); 101 if (!info) 102 goto free_module_name; 103 104 ret = call_usermodehelper_exec(info, wait | UMH_KILLABLE); 105 kmod_dup_request_announce(orig_module_name, ret); 106 return ret; 107 108 free_module_name: 109 kfree(module_name); 110 free_argv: 111 kfree(argv); 112 out: 113 kmod_dup_request_announce(orig_module_name, -ENOMEM); 114 return -ENOMEM; 115 } 116 117 /** 118 * __request_module - try to load a kernel module 119 * @wait: wait (or not) for the operation to complete 120 * @fmt: printf style format string for the name of the module 121 * @...: arguments as specified in the format string 122 * 123 * Load a module using the user mode module loader. The function returns 124 * zero on success or a negative errno code or positive exit code from 125 * "modprobe" on failure. Note that a successful module load does not mean 126 * the module did not then unload and exit on an error of its own. Callers 127 * must check that the service they requested is now available not blindly 128 * invoke it. 129 * 130 * If module auto-loading support is disabled then this function 131 * simply returns -ENOENT. 132 */ 133 int __request_module(bool wait, const char *fmt, ...) 134 { 135 va_list args; 136 char module_name[MODULE_NAME_LEN]; 137 int ret, dup_ret; 138 139 /* 140 * We don't allow synchronous module loading from async. Module 141 * init may invoke async_synchronize_full() which will end up 142 * waiting for this task which already is waiting for the module 143 * loading to complete, leading to a deadlock. 144 */ 145 WARN_ON_ONCE(wait && current_is_async()); 146 147 if (!modprobe_path[0]) 148 return -ENOENT; 149 150 va_start(args, fmt); 151 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); 152 va_end(args); 153 if (ret >= MODULE_NAME_LEN) 154 return -ENAMETOOLONG; 155 156 ret = security_kernel_module_request(module_name); 157 if (ret) 158 return ret; 159 160 ret = down_timeout(&kmod_concurrent_max, MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); 161 if (ret) { 162 pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", 163 module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); 164 return ret; 165 } 166 167 trace_module_request(module_name, wait, _RET_IP_); 168 169 if (kmod_dup_request_exists_wait(module_name, wait, &dup_ret)) { 170 ret = dup_ret; 171 goto out; 172 } 173 174 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); 175 176 out: 177 up(&kmod_concurrent_max); 178 179 return ret; 180 } 181 EXPORT_SYMBOL(__request_module); 182