default value for vdso must be zero, we are not supporting vdso at this timewq
[projects/modsched/linux.git] / kernel / kmod.c
1 /*
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 */
21 #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/slab.h>
27 #include <linux/completion.h>
28 #include <linux/cred.h>
29 #include <linux/file.h>
30 #include <linux/fdtable.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>
36 #include <linux/resource.h>
37 #include <linux/notifier.h>
38 #include <linux/suspend.h>
39 #include <asm/uaccess.h>
40
41 #include <trace/events/module.h>
42
43 /*
44  * Multikernel
45  */
46 #include <linux/process_server.h>
47
48 extern int max_threads;
49
50 static struct workqueue_struct *khelper_wq;
51
52 #define CAP_BSET        (void *)1
53 #define CAP_PI          (void *)2
54
55 static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
56 static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
57 static DEFINE_SPINLOCK(umh_sysctl_lock);
58
59 #ifdef CONFIG_MODULES
60
61 /*
62         modprobe_path is set via /proc/sys.
63 */
64 char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
65
66 /**
67  * __request_module - try to load a kernel module
68  * @wait: wait (or not) for the operation to complete
69  * @fmt: printf style format string for the name of the module
70  * @...: arguments as specified in the format string
71  *
72  * Load a module using the user mode module loader. The function returns
73  * zero on success or a negative errno code on failure. Note that a
74  * successful module load does not mean the module did not then unload
75  * and exit on an error of its own. Callers must check that the service
76  * they requested is now available not blindly invoke it.
77  *
78  * If module auto-loading support is disabled then this function
79  * becomes a no-operation.
80  */
81 int __request_module(bool wait, const char *fmt, ...)
82 {
83         va_list args;
84         char module_name[MODULE_NAME_LEN];
85         unsigned int max_modprobes;
86         int ret;
87         char *argv[] = { modprobe_path, "-q", "--", module_name, NULL };
88         static char *envp[] = { "HOME=/",
89                                 "TERM=linux",
90                                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
91                                 NULL };
92         static atomic_t kmod_concurrent = ATOMIC_INIT(0);
93 #define MAX_KMOD_CONCURRENT 50  /* Completely arbitrary value - KAO */
94         static int kmod_loop_msg;
95
96         va_start(args, fmt);
97         ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
98         va_end(args);
99         if (ret >= MODULE_NAME_LEN)
100                 return -ENAMETOOLONG;
101
102         ret = security_kernel_module_request(module_name);
103         if (ret)
104                 return ret;
105
106         /* If modprobe needs a service that is in a module, we get a recursive
107          * loop.  Limit the number of running kmod threads to max_threads/2 or
108          * MAX_KMOD_CONCURRENT, whichever is the smaller.  A cleaner method
109          * would be to run the parents of this process, counting how many times
110          * kmod was invoked.  That would mean accessing the internals of the
111          * process tables to get the command line, proc_pid_cmdline is static
112          * and it is not worth changing the proc code just to handle this case. 
113          * KAO.
114          *
115          * "trace the ppid" is simple, but will fail if someone's
116          * parent exits.  I think this is as good as it gets. --RR
117          */
118         max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
119         atomic_inc(&kmod_concurrent);
120         if (atomic_read(&kmod_concurrent) > max_modprobes) {
121                 /* We may be blaming an innocent here, but unlikely */
122                 if (kmod_loop_msg < 5) {
123                         printk(KERN_ERR
124                                "request_module: runaway loop modprobe %s\n",
125                                module_name);
126                         kmod_loop_msg++;
127                 }
128                 atomic_dec(&kmod_concurrent);
129                 return -ENOMEM;
130         }
131
132         trace_module_request(module_name, wait, _RET_IP_);
133
134         ret = call_usermodehelper_fns(modprobe_path, argv, envp,
135                         wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC,
136                         NULL, NULL, NULL);
137
138         atomic_dec(&kmod_concurrent);
139         return ret;
140 }
141 EXPORT_SYMBOL(__request_module);
142 #endif /* CONFIG_MODULES */
143
144 /*
145  * This is the task which runs the usermode application
146  */
147 static int ____call_usermodehelper(void *data)
148 {
149         struct subprocess_info *sub_info = data;
150         struct cred *new;
151         int retval;
152
153         spin_lock_irq(&current->sighand->siglock);
154         flush_signal_handlers(current, 1);
155         spin_unlock_irq(&current->sighand->siglock);
156
157         /* We can run anywhere, unlike our parent keventd(). */
158         set_cpus_allowed_ptr(current, cpu_all_mask);
159
160         /*
161          * Our parent is keventd, which runs with elevated scheduling priority.
162          * Avoid propagating that into the userspace child.
163          */
164         set_user_nice(current, 0);
165
166         retval = -ENOMEM;
167         new = prepare_kernel_cred(current);
168         if (!new)
169                 goto fail;
170
171         spin_lock(&umh_sysctl_lock);
172         new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
173         new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
174                                              new->cap_inheritable);
175         spin_unlock(&umh_sysctl_lock);
176
177         if (sub_info->init) {
178                 retval = sub_info->init(sub_info, new);
179                 if (retval) {
180                         abort_creds(new);
181                         goto fail;
182                 }
183         }
184
185         commit_creds(new);
186
187     /*
188      * Multikernel
189      * Handle delegation case
190      */
191     if (sub_info->delegated) {
192
193         // Copy identity information to current task.
194         current->clone_request_id = sub_info->clone_request_id;
195         current->prev_pid = sub_info->remote_pid;
196         current->prev_cpu = sub_info->remote_cpu;
197         current->executing_for_remote = 1;
198         current->tgroup_distributed = 1;
199         //current->represents_remote = 0;
200         memcpy(&current->remote_regs, &sub_info->remote_regs, sizeof(struct pt_regs) );
201
202         // Notify of PID/PID pairing.
203         process_server_notify_delegated_subprocess_starting(
204                 current->pid,sub_info->remote_pid,sub_info->remote_cpu);
205     } 
206
207         retval = kernel_execve(sub_info->path,
208                                (const char *const *)sub_info->argv,
209                                (const char *const *)sub_info->envp);
210     
211         /* Exec failed? */
212 fail:
213         printk("%s: failed retval{%d}\n", __func__, retval);
214         sub_info->retval = retval;
215         do_exit(0);
216 }
217
218 void call_usermodehelper_freeinfo(struct subprocess_info *info)
219 {
220         if (info->cleanup)
221                 (*info->cleanup)(info);
222         kfree(info);
223 }
224 EXPORT_SYMBOL(call_usermodehelper_freeinfo);
225
226 /* Keventd can't block, but this (a child) can. */
227 static int wait_for_helper(void *data)
228 {
229         struct subprocess_info *sub_info = data;
230         pid_t pid;
231
232         /* If SIGCLD is ignored sys_wait4 won't populate the status. */
233         spin_lock_irq(&current->sighand->siglock);
234         current->sighand->action[SIGCHLD-1].sa.sa_handler = SIG_DFL;
235         spin_unlock_irq(&current->sighand->siglock);
236
237         pid = kernel_thread(____call_usermodehelper, sub_info, SIGCHLD);
238         if (pid < 0) {
239                 sub_info->retval = pid;
240         } else {
241                 int ret = -ECHILD;
242                 /*
243                  * Normally it is bogus to call wait4() from in-kernel because
244                  * wait4() wants to write the exit code to a userspace address.
245                  * But wait_for_helper() always runs as keventd, and put_user()
246                  * to a kernel address works OK for kernel threads, due to their
247                  * having an mm_segment_t which spans the entire address space.
248                  *
249                  * Thus the __user pointer cast is valid here.
250                  */
251                 sys_wait4(pid, (int __user *)&ret, 0, NULL);
252
253                 /*
254                  * If ret is 0, either ____call_usermodehelper failed and the
255                  * real error code is already in sub_info->retval or
256                  * sub_info->retval is 0 anyway, so don't mess with it then.
257                  */
258                 if (ret)
259                         sub_info->retval = ret;
260         }
261
262         complete(sub_info->complete);
263         return 0;
264 }
265
266 /* This is run by khelper thread  */
267 static void __call_usermodehelper(struct work_struct *work)
268 {
269         struct subprocess_info *sub_info =
270                 container_of(work, struct subprocess_info, work);
271         enum umh_wait wait = sub_info->wait;
272         pid_t pid;
273
274         /* CLONE_VFORK: wait until the usermode helper has execve'd
275          * successfully We need the data structures to stay around
276          * until that is done.  */
277         if (wait == UMH_WAIT_PROC)
278                 pid = kernel_thread(wait_for_helper, sub_info,
279                                     CLONE_FS | CLONE_FILES | SIGCHLD);
280         else
281                 pid = kernel_thread(____call_usermodehelper, sub_info,
282                                     CLONE_VFORK | SIGCHLD);
283
284         switch (wait) {
285         case UMH_NO_WAIT:
286                 call_usermodehelper_freeinfo(sub_info);
287                 break;
288
289         case UMH_WAIT_PROC:
290                 if (pid > 0)
291                         break;
292                 /* FALLTHROUGH */
293         case UMH_WAIT_EXEC:
294                 if (pid < 0)
295                         sub_info->retval = pid;
296                 complete(sub_info->complete);
297         }
298 }
299
300 /*
301  * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
302  * (used for preventing user land processes from being created after the user
303  * land has been frozen during a system-wide hibernation or suspend operation).
304  */
305 static int usermodehelper_disabled = 1;
306
307 /* Number of helpers running */
308 static atomic_t running_helpers = ATOMIC_INIT(0);
309
310 /*
311  * Wait queue head used by usermodehelper_pm_callback() to wait for all running
312  * helpers to finish.
313  */
314 static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
315
316 /*
317  * Time to wait for running_helpers to become zero before the setting of
318  * usermodehelper_disabled in usermodehelper_pm_callback() fails
319  */
320 #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
321
322 /**
323  * usermodehelper_disable - prevent new helpers from being started
324  */
325 int usermodehelper_disable(void)
326 {
327         long retval;
328
329         usermodehelper_disabled = 1;
330         smp_mb();
331         /*
332          * From now on call_usermodehelper_exec() won't start any new
333          * helpers, so it is sufficient if running_helpers turns out to
334          * be zero at one point (it may be increased later, but that
335          * doesn't matter).
336          */
337         retval = wait_event_timeout(running_helpers_waitq,
338                                         atomic_read(&running_helpers) == 0,
339                                         RUNNING_HELPERS_TIMEOUT);
340         if (retval)
341                 return 0;
342
343         usermodehelper_disabled = 0;
344         return -EAGAIN;
345 }
346
347 /**
348  * usermodehelper_enable - allow new helpers to be started again
349  */
350 void usermodehelper_enable(void)
351 {
352         usermodehelper_disabled = 0;
353 }
354
355 /**
356  * usermodehelper_is_disabled - check if new helpers are allowed to be started
357  */
358 bool usermodehelper_is_disabled(void)
359 {
360         return usermodehelper_disabled;
361 }
362 EXPORT_SYMBOL_GPL(usermodehelper_is_disabled);
363
364 static void helper_lock(void)
365 {
366         atomic_inc(&running_helpers);
367         smp_mb__after_atomic_inc();
368 }
369
370 static void helper_unlock(void)
371 {
372         if (atomic_dec_and_test(&running_helpers))
373                 wake_up(&running_helpers_waitq);
374 }
375
376 /**
377  * call_usermodehelper_setup - prepare to call a usermode helper
378  * @path: path to usermode executable
379  * @argv: arg vector for process
380  * @envp: environment for process
381  * @gfp_mask: gfp mask for memory allocation
382  *
383  * Returns either %NULL on allocation failure, or a subprocess_info
384  * structure.  This should be passed to call_usermodehelper_exec to
385  * exec the process and free the structure.
386  */
387 struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
388                                                   char **envp, gfp_t gfp_mask)
389 {
390         struct subprocess_info *sub_info;
391         sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
392         if (!sub_info)
393                 goto out;
394
395         INIT_WORK(&sub_info->work, __call_usermodehelper);
396         sub_info->path = path;
397         sub_info->argv = argv;
398         sub_info->envp = envp;
399     sub_info->delegated = 0;  // multikernel
400   out:
401         return sub_info;
402 }
403 EXPORT_SYMBOL(call_usermodehelper_setup);
404
405 /**
406  * call_usermodehelper_setfns - set a cleanup/init function
407  * @info: a subprocess_info returned by call_usermodehelper_setup
408  * @cleanup: a cleanup function
409  * @init: an init function
410  * @data: arbitrary context sensitive data
411  *
412  * The init function is used to customize the helper process prior to
413  * exec.  A non-zero return code causes the process to error out, exit,
414  * and return the failure to the calling process
415  *
416  * The cleanup function is just before ethe subprocess_info is about to
417  * be freed.  This can be used for freeing the argv and envp.  The
418  * Function must be runnable in either a process context or the
419  * context in which call_usermodehelper_exec is called.
420  */
421 void call_usermodehelper_setfns(struct subprocess_info *info,
422                     int (*init)(struct subprocess_info *info, struct cred *new),
423                     void (*cleanup)(struct subprocess_info *info),
424                     void *data)
425 {
426         info->cleanup = cleanup;
427         info->init = init;
428         info->data = data;
429 }
430 EXPORT_SYMBOL(call_usermodehelper_setfns);
431
432 /**
433  * call_usermodehelper_exec - start a usermode application
434  * @sub_info: information about the subprocessa
435  * @wait: wait for the application to finish and return status.
436  *        when -1 don't wait at all, but you get no useful error back when
437  *        the program couldn't be exec'ed. This makes it safe to call
438  *        from interrupt context.
439  *
440  * Runs a user-space application.  The application is started
441  * asynchronously if wait is not set, and runs as a child of keventd.
442  * (ie. it runs with full root capabilities).
443  */
444 int call_usermodehelper_exec(struct subprocess_info *sub_info,
445                              enum umh_wait wait)
446 {
447         DECLARE_COMPLETION_ONSTACK(done);
448         int retval = 0;
449
450         helper_lock();
451         if (sub_info->path[0] == '\0')
452                 goto out;
453
454         if (!khelper_wq || usermodehelper_disabled) {
455                 retval = -EBUSY;
456                 goto out;
457         }
458
459         sub_info->complete = &done;
460         sub_info->wait = wait;
461
462         queue_work(khelper_wq, &sub_info->work);
463         if (wait == UMH_NO_WAIT)        /* task has freed sub_info */
464                 goto unlock;
465         wait_for_completion(&done);
466         retval = sub_info->retval;
467
468 out:
469         call_usermodehelper_freeinfo(sub_info);
470 unlock:
471         helper_unlock();
472         return retval;
473 }
474 EXPORT_SYMBOL(call_usermodehelper_exec);
475
476 static int proc_cap_handler(struct ctl_table *table, int write,
477                          void __user *buffer, size_t *lenp, loff_t *ppos)
478 {
479         struct ctl_table t;
480         unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
481         kernel_cap_t new_cap;
482         int err, i;
483
484         if (write && (!capable(CAP_SETPCAP) ||
485                       !capable(CAP_SYS_MODULE)))
486                 return -EPERM;
487
488         /*
489          * convert from the global kernel_cap_t to the ulong array to print to
490          * userspace if this is a read.
491          */
492         spin_lock(&umh_sysctl_lock);
493         for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)  {
494                 if (table->data == CAP_BSET)
495                         cap_array[i] = usermodehelper_bset.cap[i];
496                 else if (table->data == CAP_PI)
497                         cap_array[i] = usermodehelper_inheritable.cap[i];
498                 else
499                         BUG();
500         }
501         spin_unlock(&umh_sysctl_lock);
502
503         t = *table;
504         t.data = &cap_array;
505
506         /*
507          * actually read or write and array of ulongs from userspace.  Remember
508          * these are least significant 32 bits first
509          */
510         err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
511         if (err < 0)
512                 return err;
513
514         /*
515          * convert from the sysctl array of ulongs to the kernel_cap_t
516          * internal representation
517          */
518         for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
519                 new_cap.cap[i] = cap_array[i];
520
521         /*
522          * Drop everything not in the new_cap (but don't add things)
523          */
524         spin_lock(&umh_sysctl_lock);
525         if (write) {
526                 if (table->data == CAP_BSET)
527                         usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
528                 if (table->data == CAP_PI)
529                         usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
530         }
531         spin_unlock(&umh_sysctl_lock);
532
533         return 0;
534 }
535
536 struct ctl_table usermodehelper_table[] = {
537         {
538                 .procname       = "bset",
539                 .data           = CAP_BSET,
540                 .maxlen         = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
541                 .mode           = 0600,
542                 .proc_handler   = proc_cap_handler,
543         },
544         {
545                 .procname       = "inheritable",
546                 .data           = CAP_PI,
547                 .maxlen         = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
548                 .mode           = 0600,
549                 .proc_handler   = proc_cap_handler,
550         },
551         { }
552 };
553
554 void __init usermodehelper_init(void)
555 {
556         khelper_wq = create_singlethread_workqueue("khelper");
557         BUG_ON(!khelper_wq);
558 }