kmod.c 4.8 KB

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