kmod.c 5.0 KB

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