blob: 9f923f8ce6a0cd3c225f73eb79c865777ac60967 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 kmod, the new module loader (replaces kerneld)
3 Kirk Petersen
4
5 Reorganized not to be a daemon by Adam Richter, with guidance
6 from Greg Zornetzer.
7
8 Modified to avoid chroot and file sharing problems.
9 Mikael Pettersson
10
11 Limit the concurrent number of kmod modprobes to catch loops from
12 "modprobe needs a service that is in a module".
13 Keith Owens <kaos@ocs.com.au> December 1999
14
15 Unblock all signals when we exec a usermode process.
16 Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
17
18 call_usermodehelper wait flag, and remove exec_usermodehelper.
19 Rusty Russell <rusty@rustcorp.com.au> Jan 2003
20*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/sched.h>
23#include <linux/syscalls.h>
24#include <linux/unistd.h>
25#include <linux/kmod.h>
26#include <linux/smp_lock.h>
27#include <linux/slab.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080028#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/completion.h>
30#include <linux/file.h>
31#include <linux/workqueue.h>
32#include <linux/security.h>
33#include <linux/mount.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
Andi Kleend025c9d2006-09-30 23:29:28 -070036#include <linux/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Kay Sieversc353c3f2007-02-02 16:39:12 +010039extern int delete_module(const char *name, unsigned int flags);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041extern int max_threads;
42
43static struct workqueue_struct *khelper_wq;
44
45#ifdef CONFIG_KMOD
46
47/*
48 modprobe_path is set via /proc/sys.
49*/
50char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
Kay Sieversc353c3f2007-02-02 16:39:12 +010051struct module_kobject kmod_mk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/**
54 * request_module - try to load a kernel module
55 * @fmt: printf style format string for the name of the module
56 * @varargs: arguements as specified in the format string
57 *
58 * Load a module using the user mode module loader. The function returns
59 * zero on success or a negative errno code on failure. Note that a
60 * successful module load does not mean the module did not then unload
61 * and exit on an error of its own. Callers must check that the service
62 * they requested is now available not blindly invoke it.
63 *
64 * If module auto-loading support is disabled then this function
65 * becomes a no-operation.
66 */
67int request_module(const char *fmt, ...)
68{
69 va_list args;
70 char module_name[MODULE_NAME_LEN];
71 unsigned int max_modprobes;
72 int ret;
73 char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
74 static char *envp[] = { "HOME=/",
75 "TERM=linux",
76 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
77 NULL };
78 static atomic_t kmod_concurrent = ATOMIC_INIT(0);
79#define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */
80 static int kmod_loop_msg;
Kay Sieversc353c3f2007-02-02 16:39:12 +010081 char modalias[16 + MODULE_NAME_LEN] = "MODALIAS=";
82 char *uevent_envp[2] = {
83 modalias,
84 NULL
85 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 va_start(args, fmt);
88 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
89 va_end(args);
90 if (ret >= MODULE_NAME_LEN)
91 return -ENAMETOOLONG;
92
Kay Sieversc353c3f2007-02-02 16:39:12 +010093 strcpy(&modalias[strlen("MODALIAS=")], module_name);
94 kobject_uevent_env(&kmod_mk.kobj, KOBJ_CHANGE, uevent_envp);
95
96 if (modprobe_path[0] == '\0')
97 goto out;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* If modprobe needs a service that is in a module, we get a recursive
100 * loop. Limit the number of running kmod threads to max_threads/2 or
101 * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method
102 * would be to run the parents of this process, counting how many times
103 * kmod was invoked. That would mean accessing the internals of the
104 * process tables to get the command line, proc_pid_cmdline is static
105 * and it is not worth changing the proc code just to handle this case.
106 * KAO.
107 *
108 * "trace the ppid" is simple, but will fail if someone's
109 * parent exits. I think this is as good as it gets. --RR
110 */
111 max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
112 atomic_inc(&kmod_concurrent);
113 if (atomic_read(&kmod_concurrent) > max_modprobes) {
114 /* We may be blaming an innocent here, but unlikely */
115 if (kmod_loop_msg++ < 5)
116 printk(KERN_ERR
117 "request_module: runaway loop modprobe %s\n",
118 module_name);
119 atomic_dec(&kmod_concurrent);
120 return -ENOMEM;
121 }
122
123 ret = call_usermodehelper(modprobe_path, argv, envp, 1);
124 atomic_dec(&kmod_concurrent);
Kay Sieversc353c3f2007-02-02 16:39:12 +0100125out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return ret;
127}
128EXPORT_SYMBOL(request_module);
Kay Sieversc353c3f2007-02-02 16:39:12 +0100129
130static ssize_t store_mod_request(struct module_attribute *mattr,
131 struct module *mod,
132 const char *buffer, size_t count)
133{
134 char name[MODULE_NAME_LEN];
135 int ret;
136
137 if (count < 1 || count+1 > MODULE_NAME_LEN)
138 return -EINVAL;
139 memcpy(name, buffer, count);
140 name[count] = '\0';
141 if (name[count-1] == '\n')
142 name[count-1] = '\0';
143
144 ret = request_module(name);
145 if (ret < 0)
146 return ret;
147 return count;
148}
149
150static struct module_attribute mod_request = {
151 .attr = { .name = "mod_request", .mode = S_IWUSR, .owner = THIS_MODULE },
152 .store = store_mod_request,
153};
154
155#ifdef CONFIG_MODULE_UNLOAD
156static ssize_t store_mod_unload(struct module_attribute *mattr,
157 struct module *mod,
158 const char *buffer, size_t count)
159{
160 char name[MODULE_NAME_LEN];
161 int ret;
162
163 if (count < 1 || count+1 > MODULE_NAME_LEN)
164 return -EINVAL;
165 memcpy(name, buffer, count);
166 name[count] = '\0';
167 if (name[count-1] == '\n')
168 name[count-1] = '\0';
169
170 ret = delete_module(name, O_NONBLOCK);
171 if (ret < 0)
172 return ret;
173 return count;
174}
175
176static struct module_attribute mod_unload = {
177 .attr = { .name = "mod_unload", .mode = S_IWUSR, .owner = THIS_MODULE },
178 .store = store_mod_unload,
179};
180#endif
181
182static ssize_t show_mod_request_helper(struct module_attribute *mattr,
183 struct module *mod,
184 char *buffer)
185{
186 return sprintf(buffer, "%s\n", modprobe_path);
187}
188
189static ssize_t store_mod_request_helper(struct module_attribute *mattr,
190 struct module *mod,
191 const char *buffer, size_t count)
192{
193 if (count < 1 || count+1 > KMOD_PATH_LEN)
194 return -EINVAL;
195 memcpy(modprobe_path, buffer, count);
196 modprobe_path[count] = '\0';
197 if (modprobe_path[count-1] == '\n')
198 modprobe_path[count-1] = '\0';
199 return count;
200}
201
202static struct module_attribute mod_request_helper = {
203 .attr = {
204 .name = "mod_request_helper",
205 .mode = S_IWUSR | S_IRUGO,
206 .owner = THIS_MODULE
207 },
208 .show = show_mod_request_helper,
209 .store = store_mod_request_helper,
210};
211
212void __init kmod_sysfs_init(void)
213{
214 int ret;
215
216 kmod_mk.mod = THIS_MODULE;
217 kobj_set_kset_s(&kmod_mk, module_subsys);
218 kobject_set_name(&kmod_mk.kobj, "kmod");
219 kobject_init(&kmod_mk.kobj);
220 ret = kobject_add(&kmod_mk.kobj);
221 if (ret < 0)
222 goto out;
223
224 ret = sysfs_create_file(&kmod_mk.kobj, &mod_request_helper.attr);
225 ret = sysfs_create_file(&kmod_mk.kobj, &mod_request.attr);
226#ifdef CONFIG_MODULE_UNLOAD
227 ret = sysfs_create_file(&kmod_mk.kobj, &mod_unload.attr);
228#endif
229
230 kobject_uevent(&kmod_mk.kobj, KOBJ_ADD);
231out:
232 return;
233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif /* CONFIG_KMOD */
235
236struct subprocess_info {
David Howells65f27f32006-11-22 14:55:48 +0000237 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct completion *complete;
239 char *path;
240 char **argv;
241 char **envp;
David Howells7888e7f2005-06-23 22:00:51 -0700242 struct key *ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int wait;
244 int retval;
Andi Kleene239ca52006-09-30 23:29:27 -0700245 struct file *stdin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
248/*
249 * This is the task which runs the usermode application
250 */
251static int ____call_usermodehelper(void *data)
252{
253 struct subprocess_info *sub_info = data;
David Howells20e11292005-10-30 15:02:44 -0800254 struct key *new_session, *old_session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int retval;
256
David Howells7888e7f2005-06-23 22:00:51 -0700257 /* Unblock all signals and set the session keyring. */
David Howells20e11292005-10-30 15:02:44 -0800258 new_session = key_get(sub_info->ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 flush_signals(current);
260 spin_lock_irq(&current->sighand->siglock);
David Howells20e11292005-10-30 15:02:44 -0800261 old_session = __install_session_keyring(current, new_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 flush_signal_handlers(current, 1);
263 sigemptyset(&current->blocked);
264 recalc_sigpending();
265 spin_unlock_irq(&current->sighand->siglock);
266
David Howells7888e7f2005-06-23 22:00:51 -0700267 key_put(old_session);
268
Andi Kleene239ca52006-09-30 23:29:27 -0700269 /* Install input pipe when needed */
270 if (sub_info->stdin) {
271 struct files_struct *f = current->files;
272 struct fdtable *fdt;
273 /* no races because files should be private here */
274 sys_close(0);
275 fd_install(0, sub_info->stdin);
276 spin_lock(&f->file_lock);
277 fdt = files_fdtable(f);
278 FD_SET(0, fdt->open_fds);
279 FD_CLR(0, fdt->close_on_exec);
280 spin_unlock(&f->file_lock);
Andi Kleend025c9d2006-09-30 23:29:28 -0700281
282 /* and disallow core files too */
283 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
Andi Kleene239ca52006-09-30 23:29:27 -0700284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* We can run anywhere, unlike our parent keventd(). */
287 set_cpus_allowed(current, CPU_MASK_ALL);
288
289 retval = -EPERM;
290 if (current->fs->root)
Arnd Bergmann67608562006-10-02 02:18:26 -0700291 retval = kernel_execve(sub_info->path,
292 sub_info->argv, sub_info->envp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /* Exec failed? */
295 sub_info->retval = retval;
296 do_exit(0);
297}
298
299/* Keventd can't block, but this (a child) can. */
300static int wait_for_helper(void *data)
301{
302 struct subprocess_info *sub_info = data;
303 pid_t pid;
304 struct k_sigaction sa;
305
306 /* Install a handler: if SIGCLD isn't handled sys_wait4 won't
307 * populate the status, but will return -ECHILD. */
308 sa.sa.sa_handler = SIG_IGN;
309 sa.sa.sa_flags = 0;
310 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
Oleg Nesterov8292d632006-03-28 16:11:10 -0800311 do_sigaction(SIGCHLD, &sa, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 allow_signal(SIGCHLD);
313
314 pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
315 if (pid < 0) {
316 sub_info->retval = pid;
317 } else {
Björn Steinbrink111dbe02006-09-29 02:00:46 -0700318 int ret;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
321 * Normally it is bogus to call wait4() from in-kernel because
322 * wait4() wants to write the exit code to a userspace address.
323 * But wait_for_helper() always runs as keventd, and put_user()
324 * to a kernel address works OK for kernel threads, due to their
325 * having an mm_segment_t which spans the entire address space.
326 *
327 * Thus the __user pointer cast is valid here.
328 */
Björn Steinbrink111dbe02006-09-29 02:00:46 -0700329 sys_wait4(pid, (int __user *)&ret, 0, NULL);
330
331 /*
332 * If ret is 0, either ____call_usermodehelper failed and the
333 * real error code is already in sub_info->retval or
334 * sub_info->retval is 0 anyway, so don't mess with it then.
335 */
336 if (ret)
337 sub_info->retval = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Andi Kleena98f0dd2007-02-13 13:26:23 +0100340 if (sub_info->wait < 0)
341 kfree(sub_info);
342 else
343 complete(sub_info->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345}
346
347/* This is run by khelper thread */
David Howells65f27f32006-11-22 14:55:48 +0000348static void __call_usermodehelper(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
David Howells65f27f32006-11-22 14:55:48 +0000350 struct subprocess_info *sub_info =
351 container_of(work, struct subprocess_info, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 pid_t pid;
Kenneth Leee4b69aa2006-09-16 12:15:55 -0700353 int wait = sub_info->wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* CLONE_VFORK: wait until the usermode helper has execve'd
356 * successfully We need the data structures to stay around
357 * until that is done. */
Kenneth Leee4b69aa2006-09-16 12:15:55 -0700358 if (wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pid = kernel_thread(wait_for_helper, sub_info,
360 CLONE_FS | CLONE_FILES | SIGCHLD);
361 else
362 pid = kernel_thread(____call_usermodehelper, sub_info,
363 CLONE_VFORK | SIGCHLD);
364
Andi Kleena98f0dd2007-02-13 13:26:23 +0100365 if (wait < 0)
366 return;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (pid < 0) {
369 sub_info->retval = pid;
370 complete(sub_info->complete);
Kenneth Leee4b69aa2006-09-16 12:15:55 -0700371 } else if (!wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 complete(sub_info->complete);
373}
374
375/**
David Howells7888e7f2005-06-23 22:00:51 -0700376 * call_usermodehelper_keys - start a usermode application
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * @path: pathname for the application
378 * @argv: null-terminated argument list
379 * @envp: null-terminated environment list
David Howells7888e7f2005-06-23 22:00:51 -0700380 * @session_keyring: session keyring for process (NULL for an empty keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * @wait: wait for the application to finish and return status.
Andi Kleena98f0dd2007-02-13 13:26:23 +0100382 * when -1 don't wait at all, but you get no useful error back when
383 * the program couldn't be exec'ed. This makes it safe to call
384 * from interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
386 * Runs a user-space application. The application is started
387 * asynchronously if wait is not set, and runs as a child of keventd.
388 * (ie. it runs with full root capabilities).
389 *
390 * Must be called from process context. Returns a negative error code
391 * if program was not execed successfully, or 0.
392 */
David Howells7888e7f2005-06-23 22:00:51 -0700393int call_usermodehelper_keys(char *path, char **argv, char **envp,
394 struct key *session_keyring, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Ingo Molnar60be6b92006-07-03 00:25:26 -0700396 DECLARE_COMPLETION_ONSTACK(done);
Andi Kleena98f0dd2007-02-13 13:26:23 +0100397 struct subprocess_info *sub_info;
398 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (!khelper_wq)
401 return -EBUSY;
402
403 if (path[0] == '\0')
404 return 0;
405
Andi Kleena98f0dd2007-02-13 13:26:23 +0100406 sub_info = kzalloc(sizeof(struct subprocess_info), GFP_ATOMIC);
407 if (!sub_info)
408 return -ENOMEM;
409
410 INIT_WORK(&sub_info->work, __call_usermodehelper);
411 sub_info->complete = &done;
412 sub_info->path = path;
413 sub_info->argv = argv;
414 sub_info->envp = envp;
415 sub_info->ring = session_keyring;
416 sub_info->wait = wait;
417
418 queue_work(khelper_wq, &sub_info->work);
419 if (wait < 0) /* task has freed sub_info */
420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 wait_for_completion(&done);
Andi Kleena98f0dd2007-02-13 13:26:23 +0100422 retval = sub_info->retval;
423 kfree(sub_info);
424 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
David Howells7888e7f2005-06-23 22:00:51 -0700426EXPORT_SYMBOL(call_usermodehelper_keys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Andi Kleene239ca52006-09-30 23:29:27 -0700428int call_usermodehelper_pipe(char *path, char **argv, char **envp,
429 struct file **filp)
430{
431 DECLARE_COMPLETION(done);
432 struct subprocess_info sub_info = {
David Howells65f27f32006-11-22 14:55:48 +0000433 .work = __WORK_INITIALIZER(sub_info.work,
434 __call_usermodehelper),
Andi Kleene239ca52006-09-30 23:29:27 -0700435 .complete = &done,
436 .path = path,
437 .argv = argv,
438 .envp = envp,
439 .retval = 0,
440 };
441 struct file *f;
Andi Kleene239ca52006-09-30 23:29:27 -0700442
443 if (!khelper_wq)
444 return -EBUSY;
445
446 if (path[0] == '\0')
447 return 0;
448
449 f = create_write_pipe();
Akinobu Mita3cce4852006-11-28 12:29:43 -0800450 if (IS_ERR(f))
451 return PTR_ERR(f);
Andi Kleene239ca52006-09-30 23:29:27 -0700452 *filp = f;
453
454 f = create_read_pipe(f);
Akinobu Mita3cce4852006-11-28 12:29:43 -0800455 if (IS_ERR(f)) {
Andi Kleene239ca52006-09-30 23:29:27 -0700456 free_write_pipe(*filp);
Akinobu Mita3cce4852006-11-28 12:29:43 -0800457 return PTR_ERR(f);
Andi Kleene239ca52006-09-30 23:29:27 -0700458 }
459 sub_info.stdin = f;
460
David Howells65f27f32006-11-22 14:55:48 +0000461 queue_work(khelper_wq, &sub_info.work);
Andi Kleene239ca52006-09-30 23:29:27 -0700462 wait_for_completion(&done);
463 return sub_info.retval;
464}
465EXPORT_SYMBOL(call_usermodehelper_pipe);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467void __init usermodehelper_init(void)
468{
469 khelper_wq = create_singlethread_workqueue("khelper");
470 BUG_ON(!khelper_wq);
471}