kmod.c 14 KB

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