kmod.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #define __KERNEL_SYSCALLS__
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/unistd.h>
  22. #include <linux/kmod.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/slab.h>
  25. #include <linux/namespace.h>
  26. #include <linux/completion.h>
  27. #include <linux/file.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/security.h>
  30. #include <linux/mount.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.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 completion *complete;
  105. char *path;
  106. char **argv;
  107. char **envp;
  108. struct key *ring;
  109. int wait;
  110. int retval;
  111. };
  112. /*
  113. * This is the task which runs the usermode application
  114. */
  115. static int ____call_usermodehelper(void *data)
  116. {
  117. struct subprocess_info *sub_info = data;
  118. struct key *new_session, *old_session;
  119. int retval;
  120. /* Unblock all signals and set the session keyring. */
  121. new_session = key_get(sub_info->ring);
  122. flush_signals(current);
  123. spin_lock_irq(&current->sighand->siglock);
  124. old_session = __install_session_keyring(current, new_session);
  125. flush_signal_handlers(current, 1);
  126. sigemptyset(&current->blocked);
  127. recalc_sigpending();
  128. spin_unlock_irq(&current->sighand->siglock);
  129. key_put(old_session);
  130. /* We can run anywhere, unlike our parent keventd(). */
  131. set_cpus_allowed(current, CPU_MASK_ALL);
  132. retval = -EPERM;
  133. if (current->fs->root)
  134. retval = execve(sub_info->path, sub_info->argv,sub_info->envp);
  135. /* Exec failed? */
  136. sub_info->retval = retval;
  137. do_exit(0);
  138. }
  139. /* Keventd can't block, but this (a child) can. */
  140. static int wait_for_helper(void *data)
  141. {
  142. struct subprocess_info *sub_info = data;
  143. pid_t pid;
  144. struct k_sigaction sa;
  145. /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
  146. * populate the status, but will return -ECHILD. */
  147. sa.sa.sa_handler = SIG_IGN;
  148. sa.sa.sa_flags = 0;
  149. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  150. do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
  151. allow_signal(SIGCHLD);
  152. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  153. if (pid < 0) {
  154. sub_info->retval = pid;
  155. } else {
  156. /*
  157. * Normally it is bogus to call wait4() from in-kernel because
  158. * wait4() wants to write the exit code to a userspace address.
  159. * But wait_for_helper() always runs as keventd, and put_user()
  160. * to a kernel address works OK for kernel threads, due to their
  161. * having an mm_segment_t which spans the entire address space.
  162. *
  163. * Thus the __user pointer cast is valid here.
  164. */
  165. sys_wait4(pid, (int __user *) &sub_info->retval, 0, NULL);
  166. }
  167. complete(sub_info->complete);
  168. return 0;
  169. }
  170. /* This is run by khelper thread */
  171. static void __call_usermodehelper(void *data)
  172. {
  173. struct subprocess_info *sub_info = data;
  174. pid_t pid;
  175. /* CLONE_VFORK: wait until the usermode helper has execve'd
  176. * successfully We need the data structures to stay around
  177. * until that is done. */
  178. if (sub_info->wait)
  179. pid = kernel_thread(wait_for_helper, sub_info,
  180. CLONE_FS | CLONE_FILES | SIGCHLD);
  181. else
  182. pid = kernel_thread(____call_usermodehelper, sub_info,
  183. CLONE_VFORK | SIGCHLD);
  184. if (pid < 0) {
  185. sub_info->retval = pid;
  186. complete(sub_info->complete);
  187. } else if (!sub_info->wait)
  188. complete(sub_info->complete);
  189. }
  190. /**
  191. * call_usermodehelper_keys - start a usermode application
  192. * @path: pathname for the application
  193. * @argv: null-terminated argument list
  194. * @envp: null-terminated environment list
  195. * @session_keyring: session keyring for process (NULL for an empty keyring)
  196. * @wait: wait for the application to finish and return status.
  197. *
  198. * Runs a user-space application. The application is started
  199. * asynchronously if wait is not set, and runs as a child of keventd.
  200. * (ie. it runs with full root capabilities).
  201. *
  202. * Must be called from process context. Returns a negative error code
  203. * if program was not execed successfully, or 0.
  204. */
  205. int call_usermodehelper_keys(char *path, char **argv, char **envp,
  206. struct key *session_keyring, int wait)
  207. {
  208. DECLARE_COMPLETION(done);
  209. struct subprocess_info sub_info = {
  210. .complete = &done,
  211. .path = path,
  212. .argv = argv,
  213. .envp = envp,
  214. .ring = session_keyring,
  215. .wait = wait,
  216. .retval = 0,
  217. };
  218. DECLARE_WORK(work, __call_usermodehelper, &sub_info);
  219. if (!khelper_wq)
  220. return -EBUSY;
  221. if (path[0] == '\0')
  222. return 0;
  223. queue_work(khelper_wq, &work);
  224. wait_for_completion(&done);
  225. return sub_info.retval;
  226. }
  227. EXPORT_SYMBOL(call_usermodehelper_keys);
  228. void __init usermodehelper_init(void)
  229. {
  230. khelper_wq = create_singlethread_workqueue("khelper");
  231. BUG_ON(!khelper_wq);
  232. }