dups.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * kmod dups - the kernel module autoloader duplicate suppressor
  4. *
  5. * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org>
  6. */
  7. #define pr_fmt(fmt) "module: " fmt
  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/fdtable.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/security.h>
  22. #include <linux/mount.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/resource.h>
  26. #include <linux/notifier.h>
  27. #include <linux/suspend.h>
  28. #include <linux/rwsem.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/async.h>
  31. #include <linux/uaccess.h>
  32. #include "internal.h"
  33. #undef MODULE_PARAM_PREFIX
  34. #define MODULE_PARAM_PREFIX "module."
  35. static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);
  36. module_param(enable_dups_trace, bool_enable_only, 0644);
  37. /*
  38. * Protects dup_kmod_reqs list, adds / removals with RCU.
  39. */
  40. static DEFINE_MUTEX(kmod_dup_mutex);
  41. static LIST_HEAD(dup_kmod_reqs);
  42. struct kmod_dup_req {
  43. struct list_head list;
  44. char name[MODULE_NAME_LEN];
  45. struct completion first_req_done;
  46. struct work_struct complete_work;
  47. struct delayed_work delete_work;
  48. int dup_ret;
  49. };
  50. static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)
  51. {
  52. struct kmod_dup_req *kmod_req;
  53. list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list,
  54. lockdep_is_held(&kmod_dup_mutex)) {
  55. if (strlen(kmod_req->name) == strlen(module_name) &&
  56. !memcmp(kmod_req->name, module_name, strlen(module_name))) {
  57. return kmod_req;
  58. }
  59. }
  60. return NULL;
  61. }
  62. static void kmod_dup_request_delete(struct work_struct *work)
  63. {
  64. struct kmod_dup_req *kmod_req;
  65. kmod_req = container_of(to_delayed_work(work), struct kmod_dup_req, delete_work);
  66. /*
  67. * The typical situation is a module successully loaded. In that
  68. * situation the module will be present already in userspace. If
  69. * new requests come in after that, userspace will already know the
  70. * module is loaded so will just return 0 right away. There is still
  71. * a small chance right after we delete this entry new request_module()
  72. * calls may happen after that, they can happen. These heuristics
  73. * are to protect finit_module() abuse for auto-loading, if modules
  74. * are still tryign to auto-load even if a module is already loaded,
  75. * that's on them, and those inneficiencies should not be fixed by
  76. * kmod. The inneficies there are a call to modprobe and modprobe
  77. * just returning 0.
  78. */
  79. mutex_lock(&kmod_dup_mutex);
  80. list_del_rcu(&kmod_req->list);
  81. synchronize_rcu();
  82. mutex_unlock(&kmod_dup_mutex);
  83. kfree(kmod_req);
  84. }
  85. static void kmod_dup_request_complete(struct work_struct *work)
  86. {
  87. struct kmod_dup_req *kmod_req;
  88. kmod_req = container_of(work, struct kmod_dup_req, complete_work);
  89. /*
  90. * This will ensure that the kernel will let all the waiters get
  91. * informed its time to check the return value. It's time to
  92. * go home.
  93. */
  94. complete_all(&kmod_req->first_req_done);
  95. /*
  96. * Now that we have allowed prior request_module() calls to go on
  97. * with life, let's schedule deleting this entry. We don't have
  98. * to do it right away, but we *eventually* want to do it so to not
  99. * let this linger forever as this is just a boot optimization for
  100. * possible abuses of vmalloc() incurred by finit_module() thrashing.
  101. */
  102. queue_delayed_work(system_wq, &kmod_req->delete_work, 60 * HZ);
  103. }
  104. bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
  105. {
  106. struct kmod_dup_req *kmod_req, *new_kmod_req;
  107. int ret;
  108. /*
  109. * Pre-allocate the entry in case we have to use it later
  110. * to avoid contention with the mutex.
  111. */
  112. new_kmod_req = kzalloc(sizeof(*new_kmod_req), GFP_KERNEL);
  113. if (!new_kmod_req)
  114. return false;
  115. memcpy(new_kmod_req->name, module_name, strlen(module_name));
  116. INIT_WORK(&new_kmod_req->complete_work, kmod_dup_request_complete);
  117. INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete);
  118. init_completion(&new_kmod_req->first_req_done);
  119. mutex_lock(&kmod_dup_mutex);
  120. kmod_req = kmod_dup_request_lookup(module_name);
  121. if (!kmod_req) {
  122. /*
  123. * If the first request that came through for a module
  124. * was with request_module_nowait() we cannot wait for it
  125. * and share its return value with other users which may
  126. * have used request_module() and need a proper return value
  127. * so just skip using them as an anchor.
  128. *
  129. * If a prior request to this one came through with
  130. * request_module() though, then a request_module_nowait()
  131. * would benefit from duplicate detection.
  132. */
  133. if (!wait) {
  134. kfree(new_kmod_req);
  135. pr_debug("New request_module_nowait() for %s -- cannot track duplicates for this request\n", module_name);
  136. mutex_unlock(&kmod_dup_mutex);
  137. return false;
  138. }
  139. /*
  140. * There was no duplicate, just add the request so we can
  141. * keep tab on duplicates later.
  142. */
  143. pr_debug("New request_module() for %s\n", module_name);
  144. list_add_rcu(&new_kmod_req->list, &dup_kmod_reqs);
  145. mutex_unlock(&kmod_dup_mutex);
  146. return false;
  147. }
  148. mutex_unlock(&kmod_dup_mutex);
  149. /* We are dealing with a duplicate request now */
  150. kfree(new_kmod_req);
  151. /*
  152. * To fix these try to use try_then_request_module() instead as that
  153. * will check if the component you are looking for is present or not.
  154. * You could also just queue a single request to load the module once,
  155. * instead of having each and everything you need try to request for
  156. * the module.
  157. *
  158. * Duplicate request_module() calls can cause quite a bit of wasted
  159. * vmalloc() space when racing with userspace.
  160. */
  161. if (enable_dups_trace)
  162. WARN(1, "module-autoload: duplicate request for module %s\n", module_name);
  163. else
  164. pr_warn("module-autoload: duplicate request for module %s\n", module_name);
  165. if (!wait) {
  166. /*
  167. * If request_module_nowait() was used then the user just
  168. * wanted to issue the request and if another module request
  169. * was already its way with the same name we don't care for
  170. * the return value either. Let duplicate request_module_nowait()
  171. * calls bail out right away.
  172. */
  173. *dup_ret = 0;
  174. return true;
  175. }
  176. /*
  177. * If a duplicate request_module() was used they *may* care for
  178. * the return value, so we have no other option but to wait for
  179. * the first caller to complete. If the first caller used
  180. * the request_module_nowait() call, subsquent callers will
  181. * deal with the comprmise of getting a successful call with this
  182. * optimization enabled ...
  183. */
  184. ret = wait_for_completion_state(&kmod_req->first_req_done,
  185. TASK_KILLABLE);
  186. if (ret) {
  187. *dup_ret = ret;
  188. return true;
  189. }
  190. /* Now the duplicate request has the same exact return value as the first request */
  191. *dup_ret = kmod_req->dup_ret;
  192. return true;
  193. }
  194. void kmod_dup_request_announce(char *module_name, int ret)
  195. {
  196. struct kmod_dup_req *kmod_req;
  197. mutex_lock(&kmod_dup_mutex);
  198. kmod_req = kmod_dup_request_lookup(module_name);
  199. if (!kmod_req)
  200. goto out;
  201. kmod_req->dup_ret = ret;
  202. /*
  203. * If we complete() here we may allow duplicate threads
  204. * to continue before the first one that submitted the
  205. * request. We're in no rush also, given that each and
  206. * every bounce back to userspace is slow we avoid that
  207. * with a slight delay here. So queueue up the completion
  208. * and let duplicates suffer, just wait a tad bit longer.
  209. * There is no rush. But we also don't want to hold the
  210. * caller up forever or introduce any boot delays.
  211. */
  212. queue_work(system_wq, &kmod_req->complete_work);
  213. out:
  214. mutex_unlock(&kmod_dup_mutex);
  215. }