kmod.c 14 KB

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