kmod.c 14 KB

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