kmod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. extern int max_threads;
  35. static struct workqueue_struct *khelper_wq;
  36. #ifdef CONFIG_MODULES
  37. /*
  38. modprobe_path is set via /proc/sys.
  39. */
  40. char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
  41. /**
  42. * __request_module - try to load a kernel module
  43. * @wait: wait (or not) for the operation to complete
  44. * @fmt: printf style format string for the name of the module
  45. * @...: arguments as specified in the format string
  46. *
  47. * Load a module using the user mode module loader. The function returns
  48. * zero on success or a negative errno code on failure. Note that a
  49. * successful module load does not mean the module did not then unload
  50. * and exit on an error of its own. Callers must check that the service
  51. * they requested is now available not blindly invoke it.
  52. *
  53. * If module auto-loading support is disabled then this function
  54. * becomes a no-operation.
  55. */
  56. int __request_module(bool wait, const char *fmt, ...)
  57. {
  58. va_list args;
  59. char module_name[MODULE_NAME_LEN];
  60. unsigned int max_modprobes;
  61. int ret;
  62. char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
  63. static char *envp[] = { "HOME=/",
  64. "TERM=linux",
  65. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  66. NULL };
  67. static atomic_t kmod_concurrent = ATOMIC_INIT(0);
  68. #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
  69. static int kmod_loop_msg;
  70. va_start(args, fmt);
  71. ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
  72. va_end(args);
  73. if (ret >= MODULE_NAME_LEN)
  74. return -ENAMETOOLONG;
  75. /* If modprobe needs a service that is in a module, we get a recursive
  76. * loop. Limit the number of running kmod threads to max_threads/2 or
  77. * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
  78. * would be to run the parents of this process, counting how many times
  79. * kmod was invoked. That would mean accessing the internals of the
  80. * process tables to get the command line, proc_pid_cmdline is static
  81. * and it is not worth changing the proc code just to handle this case.
  82. * KAO.
  83. *
  84. * "trace the ppid" is simple, but will fail if someone's
  85. * parent exits. I think this is as good as it gets. --RR
  86. */
  87. max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
  88. atomic_inc(&kmod_concurrent);
  89. if (atomic_read(&kmod_concurrent) > max_modprobes) {
  90. /* We may be blaming an innocent here, but unlikely */
  91. if (kmod_loop_msg++ < 5)
  92. printk(KERN_ERR
  93. "request_module: runaway loop modprobe %s\n",
  94. module_name);
  95. atomic_dec(&kmod_concurrent);
  96. return -ENOMEM;
  97. }
  98. ret = call_usermodehelper(modprobe_path, argv, envp,
  99. wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
  100. atomic_dec(&kmod_concurrent);
  101. return ret;
  102. }
  103. EXPORT_SYMBOL(__request_module);
  104. #endif /* CONFIG_MODULES */
  105. struct subprocess_info {
  106. struct work_struct work;
  107. struct completion *complete;
  108. struct cred *cred;
  109. char *path;
  110. char **argv;
  111. char **envp;
  112. enum umh_wait wait;
  113. int retval;
  114. struct file *stdin;
  115. void (*cleanup)(char **argv, char **envp);
  116. };
  117. /*
  118. * This is the task which runs the usermode application
  119. */
  120. static int ____call_usermodehelper(void *data)
  121. {
  122. struct subprocess_info *sub_info = data;
  123. int retval;
  124. BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
  125. /* Unblock all signals */
  126. spin_lock_irq(&current->sighand->siglock);
  127. flush_signal_handlers(current, 1);
  128. sigemptyset(&current->blocked);
  129. recalc_sigpending();
  130. spin_unlock_irq(&current->sighand->siglock);
  131. /* Install the credentials */
  132. commit_creds(sub_info->cred);
  133. sub_info->cred = NULL;
  134. /* Install input pipe when needed */
  135. if (sub_info->stdin) {
  136. struct files_struct *f = current->files;
  137. struct fdtable *fdt;
  138. /* no races because files should be private here */
  139. sys_close(0);
  140. fd_install(0, sub_info->stdin);
  141. spin_lock(&f->file_lock);
  142. fdt = files_fdtable(f);
  143. FD_SET(0, fdt->open_fds);
  144. FD_CLR(0, fdt->close_on_exec);
  145. spin_unlock(&f->file_lock);
  146. /* and disallow core files too */
  147. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
  148. }
  149. /* We can run anywhere, unlike our parent keventd(). */
  150. set_cpus_allowed_ptr(current, cpu_all_mask);
  151. /*
  152. * Our parent is keventd, which runs with elevated scheduling priority.
  153. * Avoid propagating that into the userspace child.
  154. */
  155. set_user_nice(current, 0);
  156. retval = kernel_execve(sub_info->path, sub_info->argv, sub_info->envp);
  157. /* Exec failed? */
  158. sub_info->retval = retval;
  159. do_exit(0);
  160. }
  161. void call_usermodehelper_freeinfo(struct subprocess_info *info)
  162. {
  163. if (info->cleanup)
  164. (*info->cleanup)(info->argv, info->envp);
  165. if (info->cred)
  166. put_cred(info->cred);
  167. kfree(info);
  168. }
  169. EXPORT_SYMBOL(call_usermodehelper_freeinfo);
  170. /* Keventd can't block, but this (a child) can. */
  171. static int wait_for_helper(void *data)
  172. {
  173. struct subprocess_info *sub_info = data;
  174. pid_t pid;
  175. /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
  176. * populate the status, but will return -ECHILD. */
  177. allow_signal(SIGCHLD);
  178. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  179. if (pid < 0) {
  180. sub_info->retval = pid;
  181. } else {
  182. int ret;
  183. /*
  184. * Normally it is bogus to call wait4() from in-kernel because
  185. * wait4() wants to write the exit code to a userspace address.
  186. * But wait_for_helper() always runs as keventd, and put_user()
  187. * to a kernel address works OK for kernel threads, due to their
  188. * having an mm_segment_t which spans the entire address space.
  189. *
  190. * Thus the __user pointer cast is valid here.
  191. */
  192. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  193. /*
  194. * If ret is 0, either ____call_usermodehelper failed and the
  195. * real error code is already in sub_info->retval or
  196. * sub_info->retval is 0 anyway, so don't mess with it then.
  197. */
  198. if (ret)
  199. sub_info->retval = ret;
  200. }
  201. if (sub_info->wait == UMH_NO_WAIT)
  202. call_usermodehelper_freeinfo(sub_info);
  203. else
  204. complete(sub_info->complete);
  205. return 0;
  206. }
  207. /* This is run by khelper thread */
  208. static void __call_usermodehelper(struct work_struct *work)
  209. {
  210. struct subprocess_info *sub_info =
  211. container_of(work, struct subprocess_info, work);
  212. pid_t pid;
  213. enum umh_wait wait = sub_info->wait;
  214. BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
  215. /* CLONE_VFORK: wait until the usermode helper has execve'd
  216. * successfully We need the data structures to stay around
  217. * until that is done. */
  218. if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT)
  219. pid = kernel_thread(wait_for_helper, sub_info,
  220. CLONE_FS | CLONE_FILES | SIGCHLD);
  221. else
  222. pid = kernel_thread(____call_usermodehelper, sub_info,
  223. CLONE_VFORK | SIGCHLD);
  224. switch (wait) {
  225. case UMH_NO_WAIT:
  226. break;
  227. case UMH_WAIT_PROC:
  228. if (pid > 0)
  229. break;
  230. sub_info->retval = pid;
  231. /* FALLTHROUGH */
  232. case UMH_WAIT_EXEC:
  233. complete(sub_info->complete);
  234. }
  235. }
  236. #ifdef CONFIG_PM_SLEEP
  237. /*
  238. * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
  239. * (used for preventing user land processes from being created after the user
  240. * land has been frozen during a system-wide hibernation or suspend operation).
  241. */
  242. static int usermodehelper_disabled;
  243. /* Number of helpers running */
  244. static atomic_t running_helpers = ATOMIC_INIT(0);
  245. /*
  246. * Wait queue head used by usermodehelper_pm_callback() to wait for all running
  247. * helpers to finish.
  248. */
  249. static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
  250. /*
  251. * Time to wait for running_helpers to become zero before the setting of
  252. * usermodehelper_disabled in usermodehelper_pm_callback() fails
  253. */
  254. #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
  255. /**
  256. * usermodehelper_disable - prevent new helpers from being started
  257. */
  258. int usermodehelper_disable(void)
  259. {
  260. long retval;
  261. usermodehelper_disabled = 1;
  262. smp_mb();
  263. /*
  264. * From now on call_usermodehelper_exec() won't start any new
  265. * helpers, so it is sufficient if running_helpers turns out to
  266. * be zero at one point (it may be increased later, but that
  267. * doesn't matter).
  268. */
  269. retval = wait_event_timeout(running_helpers_waitq,
  270. atomic_read(&running_helpers) == 0,
  271. RUNNING_HELPERS_TIMEOUT);
  272. if (retval)
  273. return 0;
  274. usermodehelper_disabled = 0;
  275. return -EAGAIN;
  276. }
  277. /**
  278. * usermodehelper_enable - allow new helpers to be started again
  279. */
  280. void usermodehelper_enable(void)
  281. {
  282. usermodehelper_disabled = 0;
  283. }
  284. static void helper_lock(void)
  285. {
  286. atomic_inc(&running_helpers);
  287. smp_mb__after_atomic_inc();
  288. }
  289. static void helper_unlock(void)
  290. {
  291. if (atomic_dec_and_test(&running_helpers))
  292. wake_up(&running_helpers_waitq);
  293. }
  294. #else /* CONFIG_PM_SLEEP */
  295. #define usermodehelper_disabled 0
  296. static inline void helper_lock(void) {}
  297. static inline void helper_unlock(void) {}
  298. #endif /* CONFIG_PM_SLEEP */
  299. /**
  300. * call_usermodehelper_setup - prepare to call a usermode helper
  301. * @path: path to usermode executable
  302. * @argv: arg vector for process
  303. * @envp: environment for process
  304. * @gfp_mask: gfp mask for memory allocation
  305. *
  306. * Returns either %NULL on allocation failure, or a subprocess_info
  307. * structure. This should be passed to call_usermodehelper_exec to
  308. * exec the process and free the structure.
  309. */
  310. struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
  311. char **envp, gfp_t gfp_mask)
  312. {
  313. struct subprocess_info *sub_info;
  314. sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
  315. if (!sub_info)
  316. goto out;
  317. INIT_WORK(&sub_info->work, __call_usermodehelper);
  318. sub_info->path = path;
  319. sub_info->argv = argv;
  320. sub_info->envp = envp;
  321. sub_info->cred = prepare_usermodehelper_creds();
  322. if (!sub_info->cred) {
  323. kfree(sub_info);
  324. return NULL;
  325. }
  326. out:
  327. return sub_info;
  328. }
  329. EXPORT_SYMBOL(call_usermodehelper_setup);
  330. /**
  331. * call_usermodehelper_setkeys - set the session keys for usermode helper
  332. * @info: a subprocess_info returned by call_usermodehelper_setup
  333. * @session_keyring: the session keyring for the process
  334. */
  335. void call_usermodehelper_setkeys(struct subprocess_info *info,
  336. struct key *session_keyring)
  337. {
  338. #ifdef CONFIG_KEYS
  339. struct thread_group_cred *tgcred = info->cred->tgcred;
  340. key_put(tgcred->session_keyring);
  341. tgcred->session_keyring = key_get(session_keyring);
  342. #else
  343. BUG();
  344. #endif
  345. }
  346. EXPORT_SYMBOL(call_usermodehelper_setkeys);
  347. /**
  348. * call_usermodehelper_setcleanup - set a cleanup function
  349. * @info: a subprocess_info returned by call_usermodehelper_setup
  350. * @cleanup: a cleanup function
  351. *
  352. * The cleanup function is just befor ethe subprocess_info is about to
  353. * be freed. This can be used for freeing the argv and envp. The
  354. * Function must be runnable in either a process context or the
  355. * context in which call_usermodehelper_exec is called.
  356. */
  357. void call_usermodehelper_setcleanup(struct subprocess_info *info,
  358. void (*cleanup)(char **argv, char **envp))
  359. {
  360. info->cleanup = cleanup;
  361. }
  362. EXPORT_SYMBOL(call_usermodehelper_setcleanup);
  363. /**
  364. * call_usermodehelper_stdinpipe - set up a pipe to be used for stdin
  365. * @sub_info: a subprocess_info returned by call_usermodehelper_setup
  366. * @filp: set to the write-end of a pipe
  367. *
  368. * This constructs a pipe, and sets the read end to be the stdin of the
  369. * subprocess, and returns the write-end in *@filp.
  370. */
  371. int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
  372. struct file **filp)
  373. {
  374. struct file *f;
  375. f = create_write_pipe(0);
  376. if (IS_ERR(f))
  377. return PTR_ERR(f);
  378. *filp = f;
  379. f = create_read_pipe(f, 0);
  380. if (IS_ERR(f)) {
  381. free_write_pipe(*filp);
  382. return PTR_ERR(f);
  383. }
  384. sub_info->stdin = f;
  385. return 0;
  386. }
  387. EXPORT_SYMBOL(call_usermodehelper_stdinpipe);
  388. /**
  389. * call_usermodehelper_exec - start a usermode application
  390. * @sub_info: information about the subprocessa
  391. * @wait: wait for the application to finish and return status.
  392. * when -1 don't wait at all, but you get no useful error back when
  393. * the program couldn't be exec'ed. This makes it safe to call
  394. * from interrupt context.
  395. *
  396. * Runs a user-space application. The application is started
  397. * asynchronously if wait is not set, and runs as a child of keventd.
  398. * (ie. it runs with full root capabilities).
  399. */
  400. int call_usermodehelper_exec(struct subprocess_info *sub_info,
  401. enum umh_wait wait)
  402. {
  403. DECLARE_COMPLETION_ONSTACK(done);
  404. int retval = 0;
  405. BUG_ON(atomic_read(&sub_info->cred->usage) != 1);
  406. helper_lock();
  407. if (sub_info->path[0] == '\0')
  408. goto out;
  409. if (!khelper_wq || usermodehelper_disabled) {
  410. retval = -EBUSY;
  411. goto out;
  412. }
  413. sub_info->complete = &done;
  414. sub_info->wait = wait;
  415. queue_work(khelper_wq, &sub_info->work);
  416. if (wait == UMH_NO_WAIT) /* task has freed sub_info */
  417. goto unlock;
  418. wait_for_completion(&done);
  419. retval = sub_info->retval;
  420. out:
  421. call_usermodehelper_freeinfo(sub_info);
  422. unlock:
  423. helper_unlock();
  424. return retval;
  425. }
  426. EXPORT_SYMBOL(call_usermodehelper_exec);
  427. /**
  428. * call_usermodehelper_pipe - call a usermode helper process with a pipe stdin
  429. * @path: path to usermode executable
  430. * @argv: arg vector for process
  431. * @envp: environment for process
  432. * @filp: set to the write-end of a pipe
  433. *
  434. * This is a simple wrapper which executes a usermode-helper function
  435. * with a pipe as stdin. It is implemented entirely in terms of
  436. * lower-level call_usermodehelper_* functions.
  437. */
  438. int call_usermodehelper_pipe(char *path, char **argv, char **envp,
  439. struct file **filp)
  440. {
  441. struct subprocess_info *sub_info;
  442. int ret;
  443. sub_info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL);
  444. if (sub_info == NULL)
  445. return -ENOMEM;
  446. ret = call_usermodehelper_stdinpipe(sub_info, filp);
  447. if (ret < 0)
  448. goto out;
  449. return call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
  450. out:
  451. call_usermodehelper_freeinfo(sub_info);
  452. return ret;
  453. }
  454. EXPORT_SYMBOL(call_usermodehelper_pipe);
  455. void __init usermodehelper_init(void)
  456. {
  457. khelper_wq = create_singlethread_workqueue("khelper");
  458. BUG_ON(!khelper_wq);
  459. }