kmod.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/smp_lock.h>
  22. #include <linux/slab.h>
  23. #include <linux/mnt_namespace.h>
  24. #include <linux/completion.h>
  25. #include <linux/file.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 <asm/uaccess.h>
  33. extern int max_threads;
  34. static struct workqueue_struct *khelper_wq;
  35. #ifdef CONFIG_KMOD
  36. /*
  37. modprobe_path is set via /proc/sys.
  38. */
  39. char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
  40. /**
  41. * request_module - try to load a kernel module
  42. * @fmt: printf style format string for the name of the module
  43. * @varargs: arguements as specified in the format string
  44. *
  45. * Load a module using the user mode module loader. The function returns
  46. * zero on success or a negative errno code on failure. Note that a
  47. * successful module load does not mean the module did not then unload
  48. * and exit on an error of its own. Callers must check that the service
  49. * they requested is now available not blindly invoke it.
  50. *
  51. * If module auto-loading support is disabled then this function
  52. * becomes a no-operation.
  53. */
  54. int request_module(const char *fmt, ...)
  55. {
  56. va_list args;
  57. char module_name[MODULE_NAME_LEN];
  58. unsigned int max_modprobes;
  59. int ret;
  60. char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
  61. static char *envp[] = { "HOME=/",
  62. "TERM=linux",
  63. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  64. NULL };
  65. static atomic_t kmod_concurrent = ATOMIC_INIT(0);
  66. #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
  67. static int kmod_loop_msg;
  68. va_start(args, fmt);
  69. ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
  70. va_end(args);
  71. if (ret >= MODULE_NAME_LEN)
  72. return -ENAMETOOLONG;
  73. /* If modprobe needs a service that is in a module, we get a recursive
  74. * loop. Limit the number of running kmod threads to max_threads/2 or
  75. * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
  76. * would be to run the parents of this process, counting how many times
  77. * kmod was invoked. That would mean accessing the internals of the
  78. * process tables to get the command line, proc_pid_cmdline is static
  79. * and it is not worth changing the proc code just to handle this case.
  80. * KAO.
  81. *
  82. * "trace the ppid" is simple, but will fail if someone's
  83. * parent exits. I think this is as good as it gets. --RR
  84. */
  85. max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
  86. atomic_inc(&kmod_concurrent);
  87. if (atomic_read(&kmod_concurrent) > max_modprobes) {
  88. /* We may be blaming an innocent here, but unlikely */
  89. if (kmod_loop_msg++ < 5)
  90. printk(KERN_ERR
  91. "request_module: runaway loop modprobe %s\n",
  92. module_name);
  93. atomic_dec(&kmod_concurrent);
  94. return -ENOMEM;
  95. }
  96. ret = call_usermodehelper(modprobe_path, argv, envp, 1);
  97. atomic_dec(&kmod_concurrent);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL(request_module);
  101. #endif /* CONFIG_KMOD */
  102. struct subprocess_info {
  103. struct work_struct work;
  104. struct completion *complete;
  105. char *path;
  106. char **argv;
  107. char **envp;
  108. struct key *ring;
  109. int wait;
  110. int retval;
  111. struct file *stdin;
  112. };
  113. /*
  114. * This is the task which runs the usermode application
  115. */
  116. static int ____call_usermodehelper(void *data)
  117. {
  118. struct subprocess_info *sub_info = data;
  119. struct key *new_session, *old_session;
  120. int retval;
  121. /* Unblock all signals and set the session keyring. */
  122. new_session = key_get(sub_info->ring);
  123. flush_signals(current);
  124. spin_lock_irq(&current->sighand->siglock);
  125. old_session = __install_session_keyring(current, new_session);
  126. flush_signal_handlers(current, 1);
  127. sigemptyset(&current->blocked);
  128. recalc_sigpending();
  129. spin_unlock_irq(&current->sighand->siglock);
  130. key_put(old_session);
  131. /* Install input pipe when needed */
  132. if (sub_info->stdin) {
  133. struct files_struct *f = current->files;
  134. struct fdtable *fdt;
  135. /* no races because files should be private here */
  136. sys_close(0);
  137. fd_install(0, sub_info->stdin);
  138. spin_lock(&f->file_lock);
  139. fdt = files_fdtable(f);
  140. FD_SET(0, fdt->open_fds);
  141. FD_CLR(0, fdt->close_on_exec);
  142. spin_unlock(&f->file_lock);
  143. /* and disallow core files too */
  144. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
  145. }
  146. /* We can run anywhere, unlike our parent keventd(). */
  147. set_cpus_allowed(current, CPU_MASK_ALL);
  148. retval = -EPERM;
  149. if (current->fs->root)
  150. retval = kernel_execve(sub_info->path,
  151. sub_info->argv, sub_info->envp);
  152. /* Exec failed? */
  153. sub_info->retval = retval;
  154. do_exit(0);
  155. }
  156. /* Keventd can't block, but this (a child) can. */
  157. static int wait_for_helper(void *data)
  158. {
  159. struct subprocess_info *sub_info = data;
  160. pid_t pid;
  161. struct k_sigaction sa;
  162. /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
  163. * populate the status, but will return -ECHILD. */
  164. sa.sa.sa_handler = SIG_IGN;
  165. sa.sa.sa_flags = 0;
  166. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  167. do_sigaction(SIGCHLD, &sa, NULL);
  168. allow_signal(SIGCHLD);
  169. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  170. if (pid < 0) {
  171. sub_info->retval = pid;
  172. } else {
  173. int ret;
  174. /*
  175. * Normally it is bogus to call wait4() from in-kernel because
  176. * wait4() wants to write the exit code to a userspace address.
  177. * But wait_for_helper() always runs as keventd, and put_user()
  178. * to a kernel address works OK for kernel threads, due to their
  179. * having an mm_segment_t which spans the entire address space.
  180. *
  181. * Thus the __user pointer cast is valid here.
  182. */
  183. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  184. /*
  185. * If ret is 0, either ____call_usermodehelper failed and the
  186. * real error code is already in sub_info->retval or
  187. * sub_info->retval is 0 anyway, so don't mess with it then.
  188. */
  189. if (ret)
  190. sub_info->retval = ret;
  191. }
  192. if (sub_info->wait < 0)
  193. kfree(sub_info);
  194. else
  195. complete(sub_info->complete);
  196. return 0;
  197. }
  198. /* This is run by khelper thread */
  199. static void __call_usermodehelper(struct work_struct *work)
  200. {
  201. struct subprocess_info *sub_info =
  202. container_of(work, struct subprocess_info, work);
  203. pid_t pid;
  204. int wait = sub_info->wait;
  205. /* CLONE_VFORK: wait until the usermode helper has execve'd
  206. * successfully We need the data structures to stay around
  207. * until that is done. */
  208. if (wait)
  209. pid = kernel_thread(wait_for_helper, sub_info,
  210. CLONE_FS | CLONE_FILES | SIGCHLD);
  211. else
  212. pid = kernel_thread(____call_usermodehelper, sub_info,
  213. CLONE_VFORK | SIGCHLD);
  214. if (wait < 0)
  215. return;
  216. if (pid < 0) {
  217. sub_info->retval = pid;
  218. complete(sub_info->complete);
  219. } else if (!wait)
  220. complete(sub_info->complete);
  221. }
  222. /**
  223. * call_usermodehelper_keys - start a usermode application
  224. * @path: pathname for the application
  225. * @argv: null-terminated argument list
  226. * @envp: null-terminated environment list
  227. * @session_keyring: session keyring for process (NULL for an empty keyring)
  228. * @wait: wait for the application to finish and return status.
  229. * when -1 don't wait at all, but you get no useful error back when
  230. * the program couldn't be exec'ed. This makes it safe to call
  231. * from interrupt context.
  232. *
  233. * Runs a user-space application. The application is started
  234. * asynchronously if wait is not set, and runs as a child of keventd.
  235. * (ie. it runs with full root capabilities).
  236. *
  237. * Must be called from process context. Returns a negative error code
  238. * if program was not execed successfully, or 0.
  239. */
  240. int call_usermodehelper_keys(char *path, char **argv, char **envp,
  241. struct key *session_keyring, int wait)
  242. {
  243. DECLARE_COMPLETION_ONSTACK(done);
  244. struct subprocess_info *sub_info;
  245. int retval;
  246. if (!khelper_wq)
  247. return -EBUSY;
  248. if (path[0] == '\0')
  249. return 0;
  250. sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
  251. if (!sub_info)
  252. return -ENOMEM;
  253. INIT_WORK(&sub_info->work, __call_usermodehelper);
  254. sub_info->complete = &done;
  255. sub_info->path = path;
  256. sub_info->argv = argv;
  257. sub_info->envp = envp;
  258. sub_info->ring = session_keyring;
  259. sub_info->wait = wait;
  260. queue_work(khelper_wq, &sub_info->work);
  261. if (wait < 0) /* task has freed sub_info */
  262. return 0;
  263. wait_for_completion(&done);
  264. retval = sub_info->retval;
  265. kfree(sub_info);
  266. return retval;
  267. }
  268. EXPORT_SYMBOL(call_usermodehelper_keys);
  269. int call_usermodehelper_pipe(char *path, char **argv, char **envp,
  270. struct file **filp)
  271. {
  272. DECLARE_COMPLETION(done);
  273. struct subprocess_info sub_info = {
  274. .work = __WORK_INITIALIZER(sub_info.work,
  275. __call_usermodehelper),
  276. .complete = &done,
  277. .path = path,
  278. .argv = argv,
  279. .envp = envp,
  280. .retval = 0,
  281. };
  282. struct file *f;
  283. if (!khelper_wq)
  284. return -EBUSY;
  285. if (path[0] == '\0')
  286. return 0;
  287. f = create_write_pipe();
  288. if (IS_ERR(f))
  289. return PTR_ERR(f);
  290. *filp = f;
  291. f = create_read_pipe(f);
  292. if (IS_ERR(f)) {
  293. free_write_pipe(*filp);
  294. return PTR_ERR(f);
  295. }
  296. sub_info.stdin = f;
  297. queue_work(khelper_wq, &sub_info.work);
  298. wait_for_completion(&done);
  299. return sub_info.retval;
  300. }
  301. EXPORT_SYMBOL(call_usermodehelper_pipe);
  302. void __init usermodehelper_init(void)
  303. {
  304. khelper_wq = create_singlethread_workqueue("khelper");
  305. BUG_ON(!khelper_wq);
  306. }