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