kmod.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 delete_module(const char *name, unsigned int flags);
  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. struct module_kobject kmod_mk;
  42. /**
  43. * request_module - try to load a kernel module
  44. * @fmt: printf style format string for the name of the module
  45. * @varargs: arguements 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(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. char modalias[16 + MODULE_NAME_LEN] = "MODALIAS=";
  71. char *uevent_envp[2] = {
  72. modalias,
  73. NULL
  74. };
  75. va_start(args, fmt);
  76. ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
  77. va_end(args);
  78. if (ret >= MODULE_NAME_LEN)
  79. return -ENAMETOOLONG;
  80. strcpy(&modalias[strlen("MODALIAS=")], module_name);
  81. kobject_uevent_env(&kmod_mk.kobj, KOBJ_CHANGE, uevent_envp);
  82. if (modprobe_path[0] == '\0')
  83. goto out;
  84. /* If modprobe needs a service that is in a module, we get a recursive
  85. * loop. Limit the number of running kmod threads to max_threads/2 or
  86. * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
  87. * would be to run the parents of this process, counting how many times
  88. * kmod was invoked. That would mean accessing the internals of the
  89. * process tables to get the command line, proc_pid_cmdline is static
  90. * and it is not worth changing the proc code just to handle this case.
  91. * KAO.
  92. *
  93. * "trace the ppid" is simple, but will fail if someone's
  94. * parent exits. I think this is as good as it gets. --RR
  95. */
  96. max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
  97. atomic_inc(&kmod_concurrent);
  98. if (atomic_read(&kmod_concurrent) > max_modprobes) {
  99. /* We may be blaming an innocent here, but unlikely */
  100. if (kmod_loop_msg++ < 5)
  101. printk(KERN_ERR
  102. "request_module: runaway loop modprobe %s\n",
  103. module_name);
  104. atomic_dec(&kmod_concurrent);
  105. return -ENOMEM;
  106. }
  107. ret = call_usermodehelper(modprobe_path, argv, envp, 1);
  108. atomic_dec(&kmod_concurrent);
  109. out:
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(request_module);
  113. static ssize_t store_mod_request(struct module_attribute *mattr,
  114. struct module *mod,
  115. const char *buffer, size_t count)
  116. {
  117. char name[MODULE_NAME_LEN];
  118. int ret;
  119. if (count < 1 || count+1 > MODULE_NAME_LEN)
  120. return -EINVAL;
  121. memcpy(name, buffer, count);
  122. name[count] = '\0';
  123. if (name[count-1] == '\n')
  124. name[count-1] = '\0';
  125. ret = request_module(name);
  126. if (ret < 0)
  127. return ret;
  128. return count;
  129. }
  130. static struct module_attribute mod_request = {
  131. .attr = { .name = "mod_request", .mode = S_IWUSR, .owner = THIS_MODULE },
  132. .store = store_mod_request,
  133. };
  134. #ifdef CONFIG_MODULE_UNLOAD
  135. static ssize_t store_mod_unload(struct module_attribute *mattr,
  136. struct module *mod,
  137. const char *buffer, size_t count)
  138. {
  139. char name[MODULE_NAME_LEN];
  140. int ret;
  141. if (count < 1 || count+1 > MODULE_NAME_LEN)
  142. return -EINVAL;
  143. memcpy(name, buffer, count);
  144. name[count] = '\0';
  145. if (name[count-1] == '\n')
  146. name[count-1] = '\0';
  147. ret = delete_module(name, O_NONBLOCK);
  148. if (ret < 0)
  149. return ret;
  150. return count;
  151. }
  152. static struct module_attribute mod_unload = {
  153. .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE },
  154. .store = store_mod_unload,
  155. };
  156. #endif
  157. static ssize_t show_mod_request_helper(struct module_attribute *mattr,
  158. struct module *mod,
  159. char *buffer)
  160. {
  161. return sprintf(buffer, "%s\n", modprobe_path);
  162. }
  163. static ssize_t store_mod_request_helper(struct module_attribute *mattr,
  164. struct module *mod,
  165. const char *buffer, size_t count)
  166. {
  167. if (count < 1 || count+1 > KMOD_PATH_LEN)
  168. return -EINVAL;
  169. memcpy(modprobe_path, buffer, count);
  170. modprobe_path[count] = '\0';
  171. if (modprobe_path[count-1] == '\n')
  172. modprobe_path[count-1] = '\0';
  173. return count;
  174. }
  175. static struct module_attribute mod_request_helper = {
  176. .attr = {
  177. .name = "mod_request_helper",
  178. .mode = S_IWUSR | S_IRUGO,
  179. .owner = THIS_MODULE
  180. },
  181. .show = show_mod_request_helper,
  182. .store = store_mod_request_helper,
  183. };
  184. void __init kmod_sysfs_init(void)
  185. {
  186. int ret;
  187. kmod_mk.mod = THIS_MODULE;
  188. kobj_set_kset_s(&kmod_mk, module_subsys);
  189. kobject_set_name(&kmod_mk.kobj, "kmod");
  190. kobject_init(&kmod_mk.kobj);
  191. ret = kobject_add(&kmod_mk.kobj);
  192. if (ret < 0)
  193. goto out;
  194. ret = sysfs_create_file(&kmod_mk.kobj, &mod_request_helper.attr);
  195. ret = sysfs_create_file(&kmod_mk.kobj, &mod_request.attr);
  196. #ifdef CONFIG_MODULE_UNLOAD
  197. ret = sysfs_create_file(&kmod_mk.kobj, &mod_unload.attr);
  198. #endif
  199. kobject_uevent(&kmod_mk.kobj, KOBJ_ADD);
  200. out:
  201. return;
  202. }
  203. #endif /* CONFIG_KMOD */
  204. struct subprocess_info {
  205. struct work_struct work;
  206. struct completion *complete;
  207. char *path;
  208. char **argv;
  209. char **envp;
  210. struct key *ring;
  211. int wait;
  212. int retval;
  213. struct file *stdin;
  214. };
  215. /*
  216. * This is the task which runs the usermode application
  217. */
  218. static int ____call_usermodehelper(void *data)
  219. {
  220. struct subprocess_info *sub_info = data;
  221. struct key *new_session, *old_session;
  222. int retval;
  223. /* Unblock all signals and set the session keyring. */
  224. new_session = key_get(sub_info->ring);
  225. flush_signals(current);
  226. spin_lock_irq(&current->sighand->siglock);
  227. old_session = __install_session_keyring(current, new_session);
  228. flush_signal_handlers(current, 1);
  229. sigemptyset(&current->blocked);
  230. recalc_sigpending();
  231. spin_unlock_irq(&current->sighand->siglock);
  232. key_put(old_session);
  233. /* Install input pipe when needed */
  234. if (sub_info->stdin) {
  235. struct files_struct *f = current->files;
  236. struct fdtable *fdt;
  237. /* no races because files should be private here */
  238. sys_close(0);
  239. fd_install(0, sub_info->stdin);
  240. spin_lock(&f->file_lock);
  241. fdt = files_fdtable(f);
  242. FD_SET(0, fdt->open_fds);
  243. FD_CLR(0, fdt->close_on_exec);
  244. spin_unlock(&f->file_lock);
  245. /* and disallow core files too */
  246. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
  247. }
  248. /* We can run anywhere, unlike our parent keventd(). */
  249. set_cpus_allowed(current, CPU_MASK_ALL);
  250. retval = -EPERM;
  251. if (current->fs->root)
  252. retval = kernel_execve(sub_info->path,
  253. sub_info->argv, sub_info->envp);
  254. /* Exec failed? */
  255. sub_info->retval = retval;
  256. do_exit(0);
  257. }
  258. /* Keventd can't block, but this (a child) can. */
  259. static int wait_for_helper(void *data)
  260. {
  261. struct subprocess_info *sub_info = data;
  262. pid_t pid;
  263. struct k_sigaction sa;
  264. /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
  265. * populate the status, but will return -ECHILD. */
  266. sa.sa.sa_handler = SIG_IGN;
  267. sa.sa.sa_flags = 0;
  268. siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
  269. do_sigaction(SIGCHLD, &sa, NULL);
  270. allow_signal(SIGCHLD);
  271. pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
  272. if (pid < 0) {
  273. sub_info->retval = pid;
  274. } else {
  275. int ret;
  276. /*
  277. * Normally it is bogus to call wait4() from in-kernel because
  278. * wait4() wants to write the exit code to a userspace address.
  279. * But wait_for_helper() always runs as keventd, and put_user()
  280. * to a kernel address works OK for kernel threads, due to their
  281. * having an mm_segment_t which spans the entire address space.
  282. *
  283. * Thus the __user pointer cast is valid here.
  284. */
  285. sys_wait4(pid, (int __user *)&ret, 0, NULL);
  286. /*
  287. * If ret is 0, either ____call_usermodehelper failed and the
  288. * real error code is already in sub_info->retval or
  289. * sub_info->retval is 0 anyway, so don't mess with it then.
  290. */
  291. if (ret)
  292. sub_info->retval = ret;
  293. }
  294. if (sub_info->wait < 0)
  295. kfree(sub_info);
  296. else
  297. complete(sub_info->complete);
  298. return 0;
  299. }
  300. /* This is run by khelper thread */
  301. static void __call_usermodehelper(struct work_struct *work)
  302. {
  303. struct subprocess_info *sub_info =
  304. container_of(work, struct subprocess_info, work);
  305. pid_t pid;
  306. int wait = sub_info->wait;
  307. /* CLONE_VFORK: wait until the usermode helper has execve'd
  308. * successfully We need the data structures to stay around
  309. * until that is done. */
  310. if (wait)
  311. pid = kernel_thread(wait_for_helper, sub_info,
  312. CLONE_FS | CLONE_FILES | SIGCHLD);
  313. else
  314. pid = kernel_thread(____call_usermodehelper, sub_info,
  315. CLONE_VFORK | SIGCHLD);
  316. if (wait < 0)
  317. return;
  318. if (pid < 0) {
  319. sub_info->retval = pid;
  320. complete(sub_info->complete);
  321. } else if (!wait)
  322. complete(sub_info->complete);
  323. }
  324. /**
  325. * call_usermodehelper_keys - start a usermode application
  326. * @path: pathname for the application
  327. * @argv: null-terminated argument list
  328. * @envp: null-terminated environment list
  329. * @session_keyring: session keyring for process (NULL for an empty keyring)
  330. * @wait: wait for the application to finish and return status.
  331. * when -1 don't wait at all, but you get no useful error back when
  332. * the program couldn't be exec'ed. This makes it safe to call
  333. * from interrupt context.
  334. *
  335. * Runs a user-space application. The application is started
  336. * asynchronously if wait is not set, and runs as a child of keventd.
  337. * (ie. it runs with full root capabilities).
  338. *
  339. * Must be called from process context. Returns a negative error code
  340. * if program was not execed successfully, or 0.
  341. */
  342. int call_usermodehelper_keys(char *path, char **argv, char **envp,
  343. struct key *session_keyring, int wait)
  344. {
  345. DECLARE_COMPLETION_ONSTACK(done);
  346. struct subprocess_info *sub_info;
  347. int retval;
  348. if (!khelper_wq)
  349. return -EBUSY;
  350. if (path[0] == '\0')
  351. return 0;
  352. sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
  353. if (!sub_info)
  354. return -ENOMEM;
  355. INIT_WORK(&sub_info->work, __call_usermodehelper);
  356. sub_info->complete = &done;
  357. sub_info->path = path;
  358. sub_info->argv = argv;
  359. sub_info->envp = envp;
  360. sub_info->ring = session_keyring;
  361. sub_info->wait = wait;
  362. queue_work(khelper_wq, &sub_info->work);
  363. if (wait < 0) /* task has freed sub_info */
  364. return 0;
  365. wait_for_completion(&done);
  366. retval = sub_info->retval;
  367. kfree(sub_info);
  368. return retval;
  369. }
  370. EXPORT_SYMBOL(call_usermodehelper_keys);
  371. int call_usermodehelper_pipe(char *path, char **argv, char **envp,
  372. struct file **filp)
  373. {
  374. DECLARE_COMPLETION(done);
  375. struct subprocess_info sub_info = {
  376. .work = __WORK_INITIALIZER(sub_info.work,
  377. __call_usermodehelper),
  378. .complete = &done,
  379. .path = path,
  380. .argv = argv,
  381. .envp = envp,
  382. .retval = 0,
  383. };
  384. struct file *f;
  385. if (!khelper_wq)
  386. return -EBUSY;
  387. if (path[0] == '\0')
  388. return 0;
  389. f = create_write_pipe();
  390. if (IS_ERR(f))
  391. return PTR_ERR(f);
  392. *filp = f;
  393. f = create_read_pipe(f);
  394. if (IS_ERR(f)) {
  395. free_write_pipe(*filp);
  396. return PTR_ERR(f);
  397. }
  398. sub_info.stdin = f;
  399. queue_work(khelper_wq, &sub_info.work);
  400. wait_for_completion(&done);
  401. return sub_info.retval;
  402. }
  403. EXPORT_SYMBOL(call_usermodehelper_pipe);
  404. void __init usermodehelper_init(void)
  405. {
  406. khelper_wq = create_singlethread_workqueue("khelper");
  407. BUG_ON(!khelper_wq);
  408. }