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