kmod.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. kmod, the new module loader (replaces kerneld)
  3. Kirk Petersen
  4. Reorganized not to be a daemon by Adam Richter, with guidance
  5. from Greg Zornetzer.
  6. Modified to avoid chroot and file sharing problems.
  7. Mikael Pettersson
  8. Limit the concurrent number of kmod modprobes to catch loops from
  9. "modprobe needs a service that is in a module".
  10. Keith Owens <kaos@ocs.com.au> December 1999
  11. Unblock all signals when we exec a usermode process.
  12. Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
  13. call_usermodehelper wait flag, and remove exec_usermodehelper.
  14. Rusty Russell <rusty@rustcorp.com.au> Jan 2003
  15. */
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/unistd.h>
  20. #include <linux/kmod.h>
  21. #include <linux/slab.h>
  22. #include <linux/completion.h>
  23. #include <linux/file.h>
  24. #include <linux/fdtable.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/security.h>
  27. #include <linux/mount.h>
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/resource.h>
  31. #include <linux/notifier.h>
  32. #include <linux/suspend.h>
  33. #include <asm/uaccess.h>
  34. #include <trace/events/module.h>
  35. extern int max_threads;
  36. static struct workqueue_struct *khelper_wq;
  37. #ifdef CONFIG_MODULES
  38. /*
  39. modprobe_path is set via /proc/sys.
  40. */
  41. char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
  42. /**
  43. * __request_module - try to load a kernel module
  44. * @wait: wait (or not) for the operation to complete
  45. * @fmt: printf style format string for the name of the module
  46. * @...: arguments as specified in the format string
  47. *
  48. * Load a module using the user mode module loader. The function returns
  49. * zero on success or a negative errno code on failure. Note that a
  50. * successful module load does not mean the module did not then unload
  51. * and exit on an error of its own. Callers must check that the service
  52. * they requested is now available not blindly invoke it.
  53. *
  54. * If module auto-loading support is disabled then this function
  55. * becomes a no-operation.
  56. */
  57. int __request_module(bool wait, const char *fmt, ...)
  58. {
  59. va_list args;
  60. char module_name[MODULE_NAME_LEN];
  61. unsigned int max_modprobes;
  62. int ret;
  63. char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
  64. static char *envp[] = { "HOME=/",
  65. "TERM=linux",
  66. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  67. NULL };
  68. static atomic_t kmod_concurrent = ATOMIC_INIT(0);
  69. #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
  70. static int kmod_loop_msg;
  71. va_start(args, fmt);
  72. ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
  73. va_end(args);
  74. if (ret >= MODULE_NAME_LEN)
  75. return -ENAMETOOLONG;
  76. ret = security_kernel_module_request(module_name);
  77. if (ret)
  78. return ret;
  79. /* If modprobe needs a service that is in a module, we get a recursive
  80. * loop. Limit the number of running kmod threads to max_threads/2 or
  81. * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
  82. * would be to run the parents of this process, counting how many times
  83. * kmod was invoked. That would mean accessing the internals of the
  84. * process tables to get the command line, proc_pid_cmdline is static
  85. * and it is not worth changing the proc code just to handle this case.
  86. * KAO.
  87. *
  88. * "trace the ppid" is simple, but will fail if someone's
  89. * parent exits. I think this is as good as it gets. --RR
  90. */
  91. max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
  92. atomic_inc(&kmod_concurrent);
  93. if (atomic_read(&kmod_concurrent) > max_modprobes) {
  94. /* We may be blaming an innocent here, but unlikely */
  95. if (kmod_loop_msg++ < 5)
  96. printk(KERN_ERR
  97. "request_module: runaway loop modprobe %s\n",
  98. module_name);
  99. atomic_dec(&kmod_concurrent);
  100. return -ENOMEM;
  101. }
  102. trace_module_request(module_name, wait, _RET_IP_);
  103. ret = call_usermodehelper_fns(modprobe_path, argv, envp,
  104. wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC,
  105. NULL, NULL, NULL);
  106. atomic_dec(&kmod_concurrent);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL(__request_module);
  110. #endif /* CONFIG_MODULES */
  111. /*
  112. * This is the task which runs the usermode application
  113. */
  114. static int ____call_usermodehelper(void *data)
  115. {
  116. struct subprocess_info *sub_info = data;
  117. int retval;
  118. spin_lock_irq(&current->sighand->siglock);
  119. flush_signal_handlers(current, 1);
  120. spin_unlock_irq(&current->sighand->siglock);
  121. /* We can run anywhere, unlike our parent keventd(). */
  122. set_cpus_allowed_ptr(current, cpu_all_mask);
  123. /*
  124. * Our parent is keventd, which runs with elevated scheduling priority.
  125. * Avoid propagating that into the userspace child.
  126. */
  127. set_user_nice(current, 0);
  128. if (sub_info->init) {
  129. retval = sub_info->init(sub_info);
  130. if (retval)
  131. goto fail;
  132. }
  133. retval = kernel_execve(sub_info->path,
  134. (const char *const *)sub_info->argv,
  135. (const char *const *)sub_info->envp);
  136. /* Exec failed? */
  137. fail:
  138. sub_info->retval = retval;
  139. do_exit(0);
  140. }
  141. void call_usermodehelper_freeinfo(struct subprocess_info *info)
  142. {
  143. if (info->cleanup)
  144. (*info->cleanup)(info);
  145. kfree(info);
  146. }
  147. EXPORT_SYMBOL(call_usermodehelper_freeinfo);
  148. /* Keventd can't block, but this (a child) can. */
  149. static int wait_for_helper(void *data)
  150. {
  151. struct subprocess_info *sub_info = data;
  152. pid_t pid;
  153. /* If SIGCLD is ignored sys_wait4 won't populate the status. */
  154. spin_lock_irq(&current->sighand->siglock);
  155. current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
  156. spin_unlock_irq(&current->sighand->siglock);
  157. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  158. if (pid < 0) {
  159. sub_info->retval = pid;
  160. } else {
  161. int ret = -ECHILD;
  162. /*
  163. * Normally it is bogus to call wait4() from in-kernel because
  164. * wait4() wants to write the exit code to a userspace address.
  165. * But wait_for_helper() always runs as keventd, and put_user()
  166. * to a kernel address works OK for kernel threads, due to their
  167. * having an mm_segment_t which spans the entire address space.
  168. *
  169. * Thus the __user pointer cast is valid here.
  170. */
  171. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  172. /*
  173. * If ret is 0, either ____call_usermodehelper failed and the
  174. * real error code is already in sub_info->retval or
  175. * sub_info->retval is 0 anyway, so don't mess with it then.
  176. */
  177. if (ret)
  178. sub_info->retval = ret;
  179. }
  180. complete(sub_info->complete);
  181. return 0;
  182. }
  183. /* This is run by khelper thread */
  184. static void __call_usermodehelper(struct work_struct *work)
  185. {
  186. struct subprocess_info *sub_info =
  187. container_of(work, struct subprocess_info, work);
  188. enum umh_wait wait = sub_info->wait;
  189. pid_t pid;
  190. /* CLONE_VFORK: wait until the usermode helper has execve'd
  191. * successfully We need the data structures to stay around
  192. * until that is done. */
  193. if (wait == UMH_WAIT_PROC)
  194. pid = kernel_thread(wait_for_helper, sub_info,
  195. CLONE_FS | CLONE_FILES | SIGCHLD);
  196. else
  197. pid = kernel_thread(____call_usermodehelper, sub_info,
  198. CLONE_VFORK | SIGCHLD);
  199. switch (wait) {
  200. case UMH_NO_WAIT:
  201. call_usermodehelper_freeinfo(sub_info);
  202. break;
  203. case UMH_WAIT_PROC:
  204. if (pid > 0)
  205. break;
  206. /* FALLTHROUGH */
  207. case UMH_WAIT_EXEC:
  208. if (pid < 0)
  209. sub_info->retval = pid;
  210. complete(sub_info->complete);
  211. }
  212. }
  213. #ifdef CONFIG_PM_SLEEP
  214. /*
  215. * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
  216. * (used for preventing user land processes from being created after the user
  217. * land has been frozen during a system-wide hibernation or suspend operation).
  218. */
  219. static int usermodehelper_disabled;
  220. /* Number of helpers running */
  221. static atomic_t running_helpers = ATOMIC_INIT(0);
  222. /*
  223. * Wait queue head used by usermodehelper_pm_callback() to wait for all running
  224. * helpers to finish.
  225. */
  226. static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
  227. /*
  228. * Time to wait for running_helpers to become zero before the setting of
  229. * usermodehelper_disabled in usermodehelper_pm_callback() fails
  230. */
  231. #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
  232. /**
  233. * usermodehelper_disable - prevent new helpers from being started
  234. */
  235. int usermodehelper_disable(void)
  236. {
  237. long retval;
  238. usermodehelper_disabled = 1;
  239. smp_mb();
  240. /*
  241. * From now on call_usermodehelper_exec() won't start any new
  242. * helpers, so it is sufficient if running_helpers turns out to
  243. * be zero at one point (it may be increased later, but that
  244. * doesn't matter).
  245. */
  246. retval = wait_event_timeout(running_helpers_waitq,
  247. atomic_read(&running_helpers) == 0,
  248. RUNNING_HELPERS_TIMEOUT);
  249. if (retval)
  250. return 0;
  251. usermodehelper_disabled = 0;
  252. return -EAGAIN;
  253. }
  254. /**
  255. * usermodehelper_enable - allow new helpers to be started again
  256. */
  257. void usermodehelper_enable(void)
  258. {
  259. usermodehelper_disabled = 0;
  260. }
  261. static void helper_lock(void)
  262. {
  263. atomic_inc(&running_helpers);
  264. smp_mb__after_atomic_inc();
  265. }
  266. static void helper_unlock(void)
  267. {
  268. if (atomic_dec_and_test(&running_helpers))
  269. wake_up(&running_helpers_waitq);
  270. }
  271. #else /* CONFIG_PM_SLEEP */
  272. #define usermodehelper_disabled 0
  273. static inline void helper_lock(void) {}
  274. static inline void helper_unlock(void) {}
  275. #endif /* CONFIG_PM_SLEEP */
  276. /**
  277. * call_usermodehelper_setup - prepare to call a usermode helper
  278. * @path: path to usermode executable
  279. * @argv: arg vector for process
  280. * @envp: environment for process
  281. * @gfp_mask: gfp mask for memory allocation
  282. *
  283. * Returns either %NULL on allocation failure, or a subprocess_info
  284. * structure. This should be passed to call_usermodehelper_exec to
  285. * exec the process and free the structure.
  286. */
  287. struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
  288. char **envp, gfp_t gfp_mask)
  289. {
  290. struct subprocess_info *sub_info;
  291. sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
  292. if (!sub_info)
  293. goto out;
  294. INIT_WORK(&sub_info->work, __call_usermodehelper);
  295. sub_info->path = path;
  296. sub_info->argv = argv;
  297. sub_info->envp = envp;
  298. out:
  299. return sub_info;
  300. }
  301. EXPORT_SYMBOL(call_usermodehelper_setup);
  302. /**
  303. * call_usermodehelper_setfns - set a cleanup/init function
  304. * @info: a subprocess_info returned by call_usermodehelper_setup
  305. * @cleanup: a cleanup function
  306. * @init: an init function
  307. * @data: arbitrary context sensitive data
  308. *
  309. * The init function is used to customize the helper process prior to
  310. * exec. A non-zero return code causes the process to error out, exit,
  311. * and return the failure to the calling process
  312. *
  313. * The cleanup function is just before ethe subprocess_info is about to
  314. * be freed. This can be used for freeing the argv and envp. The
  315. * Function must be runnable in either a process context or the
  316. * context in which call_usermodehelper_exec is called.
  317. */
  318. void call_usermodehelper_setfns(struct subprocess_info *info,
  319. int (*init)(struct subprocess_info *info),
  320. void (*cleanup)(struct subprocess_info *info),
  321. void *data)
  322. {
  323. info->cleanup = cleanup;
  324. info->init = init;
  325. info->data = data;
  326. }
  327. EXPORT_SYMBOL(call_usermodehelper_setfns);
  328. /**
  329. * call_usermodehelper_exec - start a usermode application
  330. * @sub_info: information about the subprocessa
  331. * @wait: wait for the application to finish and return status.
  332. * when -1 don't wait at all, but you get no useful error back when
  333. * the program couldn't be exec'ed. This makes it safe to call
  334. * from interrupt context.
  335. *
  336. * Runs a user-space application. The application is started
  337. * asynchronously if wait is not set, and runs as a child of keventd.
  338. * (ie. it runs with full root capabilities).
  339. */
  340. int call_usermodehelper_exec(struct subprocess_info *sub_info,
  341. enum umh_wait wait)
  342. {
  343. DECLARE_COMPLETION_ONSTACK(done);
  344. int retval = 0;
  345. helper_lock();
  346. if (sub_info->path[0] == '\0')
  347. goto out;
  348. if (!khelper_wq || usermodehelper_disabled) {
  349. retval = -EBUSY;
  350. goto out;
  351. }
  352. sub_info->complete = &done;
  353. sub_info->wait = wait;
  354. queue_work(khelper_wq, &sub_info->work);
  355. if (wait == UMH_NO_WAIT) /* task has freed sub_info */
  356. goto unlock;
  357. wait_for_completion(&done);
  358. retval = sub_info->retval;
  359. out:
  360. call_usermodehelper_freeinfo(sub_info);
  361. unlock:
  362. helper_unlock();
  363. return retval;
  364. }
  365. EXPORT_SYMBOL(call_usermodehelper_exec);
  366. void __init usermodehelper_init(void)
  367. {
  368. khelper_wq = create_singlethread_workqueue("khelper");
  369. BUG_ON(!khelper_wq);
  370. }