Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[linux-2.6.git] / kernel / sys.c
1 /*
2  *  linux/kernel/sys.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/module.h>
8 #include <linux/mm.h>
9 #include <linux/utsname.h>
10 #include <linux/mman.h>
11 #include <linux/smp_lock.h>
12 #include <linux/notifier.h>
13 #include <linux/kmod.h>
14 #include <linux/reboot.h>
15 #include <linux/prctl.h>
16 #include <linux/highuid.h>
17 #include <linux/fs.h>
18 #include <linux/kernel.h>
19 #include <linux/kexec.h>
20 #include <linux/workqueue.h>
21 #include <linux/capability.h>
22 #include <linux/device.h>
23 #include <linux/key.h>
24 #include <linux/times.h>
25 #include <linux/posix-timers.h>
26 #include <linux/security.h>
27 #include <linux/dcookies.h>
28 #include <linux/suspend.h>
29 #include <linux/tty.h>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
32 #include <linux/vs_base.h>
33 #include <linux/vs_cvirt.h>
34
35 #include <linux/compat.h>
36 #include <linux/syscalls.h>
37 #include <linux/kprobes.h>
38
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41 #include <asm/unistd.h>
42
43 #ifndef SET_UNALIGN_CTL
44 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
45 #endif
46 #ifndef GET_UNALIGN_CTL
47 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
48 #endif
49 #ifndef SET_FPEMU_CTL
50 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
51 #endif
52 #ifndef GET_FPEMU_CTL
53 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
54 #endif
55 #ifndef SET_FPEXC_CTL
56 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
57 #endif
58 #ifndef GET_FPEXC_CTL
59 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
60 #endif
61 #ifndef GET_ENDIAN
62 # define GET_ENDIAN(a,b)        (-EINVAL)
63 #endif
64 #ifndef SET_ENDIAN
65 # define SET_ENDIAN(a,b)        (-EINVAL)
66 #endif
67
68 /*
69  * this is where the system-wide overflow UID and GID are defined, for
70  * architectures that now have 32-bit UID/GID but didn't in the past
71  */
72
73 int overflowuid = DEFAULT_OVERFLOWUID;
74 int overflowgid = DEFAULT_OVERFLOWGID;
75
76 #ifdef CONFIG_UID16
77 EXPORT_SYMBOL(overflowuid);
78 EXPORT_SYMBOL(overflowgid);
79 #endif
80
81 /*
82  * the same as above, but for filesystems which can only store a 16-bit
83  * UID and GID. as such, this is needed on all architectures
84  */
85
86 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
87 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
88
89 EXPORT_SYMBOL(fs_overflowuid);
90 EXPORT_SYMBOL(fs_overflowgid);
91
92 /*
93  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
94  */
95
96 int C_A_D = 1;
97 int cad_pid = 1;
98
99 /*
100  *      Notifier list for kernel code which wants to be called
101  *      at shutdown. This is used to stop any idling DMA operations
102  *      and the like. 
103  */
104
105 static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
106
107 /*
108  *      Notifier chain core routines.  The exported routines below
109  *      are layered on top of these, with appropriate locking added.
110  */
111
112 static int notifier_chain_register(struct notifier_block **nl,
113                 struct notifier_block *n)
114 {
115         while ((*nl) != NULL) {
116                 if (n->priority > (*nl)->priority)
117                         break;
118                 nl = &((*nl)->next);
119         }
120         n->next = *nl;
121         rcu_assign_pointer(*nl, n);
122         return 0;
123 }
124
125 static int notifier_chain_unregister(struct notifier_block **nl,
126                 struct notifier_block *n)
127 {
128         while ((*nl) != NULL) {
129                 if ((*nl) == n) {
130                         rcu_assign_pointer(*nl, n->next);
131                         return 0;
132                 }
133                 nl = &((*nl)->next);
134         }
135         return -ENOENT;
136 }
137
138 static int __kprobes notifier_call_chain(struct notifier_block **nl,
139                 unsigned long val, void *v)
140 {
141         int ret = NOTIFY_DONE;
142         struct notifier_block *nb, *next_nb;
143
144         nb = rcu_dereference(*nl);
145         while (nb) {
146                 next_nb = rcu_dereference(nb->next);
147                 ret = nb->notifier_call(nb, val, v);
148                 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
149                         break;
150                 nb = next_nb;
151         }
152         return ret;
153 }
154
155 /*
156  *      Atomic notifier chain routines.  Registration and unregistration
157  *      use a mutex, and call_chain is synchronized by RCU (no locks).
158  */
159
160 /**
161  *      atomic_notifier_chain_register - Add notifier to an atomic notifier chain
162  *      @nh: Pointer to head of the atomic notifier chain
163  *      @n: New entry in notifier chain
164  *
165  *      Adds a notifier to an atomic notifier chain.
166  *
167  *      Currently always returns zero.
168  */
169
170 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
171                 struct notifier_block *n)
172 {
173         unsigned long flags;
174         int ret;
175
176         spin_lock_irqsave(&nh->lock, flags);
177         ret = notifier_chain_register(&nh->head, n);
178         spin_unlock_irqrestore(&nh->lock, flags);
179         return ret;
180 }
181
182 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
183
184 /**
185  *      atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
186  *      @nh: Pointer to head of the atomic notifier chain
187  *      @n: Entry to remove from notifier chain
188  *
189  *      Removes a notifier from an atomic notifier chain.
190  *
191  *      Returns zero on success or %-ENOENT on failure.
192  */
193 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
194                 struct notifier_block *n)
195 {
196         unsigned long flags;
197         int ret;
198
199         spin_lock_irqsave(&nh->lock, flags);
200         ret = notifier_chain_unregister(&nh->head, n);
201         spin_unlock_irqrestore(&nh->lock, flags);
202         synchronize_rcu();
203         return ret;
204 }
205
206 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
207
208 /**
209  *      atomic_notifier_call_chain - Call functions in an atomic notifier chain
210  *      @nh: Pointer to head of the atomic notifier chain
211  *      @val: Value passed unmodified to notifier function
212  *      @v: Pointer passed unmodified to notifier function
213  *
214  *      Calls each function in a notifier chain in turn.  The functions
215  *      run in an atomic context, so they must not block.
216  *      This routine uses RCU to synchronize with changes to the chain.
217  *
218  *      If the return value of the notifier can be and'ed
219  *      with %NOTIFY_STOP_MASK then atomic_notifier_call_chain
220  *      will return immediately, with the return value of
221  *      the notifier function which halted execution.
222  *      Otherwise the return value is the return value
223  *      of the last notifier function called.
224  */
225  
226 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
227                 unsigned long val, void *v)
228 {
229         int ret;
230
231         rcu_read_lock();
232         ret = notifier_call_chain(&nh->head, val, v);
233         rcu_read_unlock();
234         return ret;
235 }
236
237 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
238
239 /*
240  *      Blocking notifier chain routines.  All access to the chain is
241  *      synchronized by an rwsem.
242  */
243
244 /**
245  *      blocking_notifier_chain_register - Add notifier to a blocking notifier chain
246  *      @nh: Pointer to head of the blocking notifier chain
247  *      @n: New entry in notifier chain
248  *
249  *      Adds a notifier to a blocking notifier chain.
250  *      Must be called in process context.
251  *
252  *      Currently always returns zero.
253  */
254  
255 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
256                 struct notifier_block *n)
257 {
258         int ret;
259
260         /*
261          * This code gets used during boot-up, when task switching is
262          * not yet working and interrupts must remain disabled.  At
263          * such times we must not call down_write().
264          */
265         if (unlikely(system_state == SYSTEM_BOOTING))
266                 return notifier_chain_register(&nh->head, n);
267
268         down_write(&nh->rwsem);
269         ret = notifier_chain_register(&nh->head, n);
270         up_write(&nh->rwsem);
271         return ret;
272 }
273
274 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
275
276 /**
277  *      blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
278  *      @nh: Pointer to head of the blocking notifier chain
279  *      @n: Entry to remove from notifier chain
280  *
281  *      Removes a notifier from a blocking notifier chain.
282  *      Must be called from process context.
283  *
284  *      Returns zero on success or %-ENOENT on failure.
285  */
286 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
287                 struct notifier_block *n)
288 {
289         int ret;
290
291         /*
292          * This code gets used during boot-up, when task switching is
293          * not yet working and interrupts must remain disabled.  At
294          * such times we must not call down_write().
295          */
296         if (unlikely(system_state == SYSTEM_BOOTING))
297                 return notifier_chain_unregister(&nh->head, n);
298
299         down_write(&nh->rwsem);
300         ret = notifier_chain_unregister(&nh->head, n);
301         up_write(&nh->rwsem);
302         return ret;
303 }
304
305 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
306
307 /**
308  *      blocking_notifier_call_chain - Call functions in a blocking notifier chain
309  *      @nh: Pointer to head of the blocking notifier chain
310  *      @val: Value passed unmodified to notifier function
311  *      @v: Pointer passed unmodified to notifier function
312  *
313  *      Calls each function in a notifier chain in turn.  The functions
314  *      run in a process context, so they are allowed to block.
315  *
316  *      If the return value of the notifier can be and'ed
317  *      with %NOTIFY_STOP_MASK then blocking_notifier_call_chain
318  *      will return immediately, with the return value of
319  *      the notifier function which halted execution.
320  *      Otherwise the return value is the return value
321  *      of the last notifier function called.
322  */
323  
324 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
325                 unsigned long val, void *v)
326 {
327         int ret;
328
329         down_read(&nh->rwsem);
330         ret = notifier_call_chain(&nh->head, val, v);
331         up_read(&nh->rwsem);
332         return ret;
333 }
334
335 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
336
337 /*
338  *      Raw notifier chain routines.  There is no protection;
339  *      the caller must provide it.  Use at your own risk!
340  */
341
342 /**
343  *      raw_notifier_chain_register - Add notifier to a raw notifier chain
344  *      @nh: Pointer to head of the raw notifier chain
345  *      @n: New entry in notifier chain
346  *
347  *      Adds a notifier to a raw notifier chain.
348  *      All locking must be provided by the caller.
349  *
350  *      Currently always returns zero.
351  */
352
353 int raw_notifier_chain_register(struct raw_notifier_head *nh,
354                 struct notifier_block *n)
355 {
356         return notifier_chain_register(&nh->head, n);
357 }
358
359 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
360
361 /**
362  *      raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
363  *      @nh: Pointer to head of the raw notifier chain
364  *      @n: Entry to remove from notifier chain
365  *
366  *      Removes a notifier from a raw notifier chain.
367  *      All locking must be provided by the caller.
368  *
369  *      Returns zero on success or %-ENOENT on failure.
370  */
371 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
372                 struct notifier_block *n)
373 {
374         return notifier_chain_unregister(&nh->head, n);
375 }
376
377 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
378
379 /**
380  *      raw_notifier_call_chain - Call functions in a raw notifier chain
381  *      @nh: Pointer to head of the raw notifier chain
382  *      @val: Value passed unmodified to notifier function
383  *      @v: Pointer passed unmodified to notifier function
384  *
385  *      Calls each function in a notifier chain in turn.  The functions
386  *      run in an undefined context.
387  *      All locking must be provided by the caller.
388  *
389  *      If the return value of the notifier can be and'ed
390  *      with %NOTIFY_STOP_MASK then raw_notifier_call_chain
391  *      will return immediately, with the return value of
392  *      the notifier function which halted execution.
393  *      Otherwise the return value is the return value
394  *      of the last notifier function called.
395  */
396
397 int raw_notifier_call_chain(struct raw_notifier_head *nh,
398                 unsigned long val, void *v)
399 {
400         return notifier_call_chain(&nh->head, val, v);
401 }
402
403 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
404
405 /**
406  *      register_reboot_notifier - Register function to be called at reboot time
407  *      @nb: Info about notifier function to be called
408  *
409  *      Registers a function with the list of functions
410  *      to be called at reboot time.
411  *
412  *      Currently always returns zero, as blocking_notifier_chain_register
413  *      always returns zero.
414  */
415  
416 int register_reboot_notifier(struct notifier_block * nb)
417 {
418         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
419 }
420
421 EXPORT_SYMBOL(register_reboot_notifier);
422
423 /**
424  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
425  *      @nb: Hook to be unregistered
426  *
427  *      Unregisters a previously registered reboot
428  *      notifier function.
429  *
430  *      Returns zero on success, or %-ENOENT on failure.
431  */
432  
433 int unregister_reboot_notifier(struct notifier_block * nb)
434 {
435         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
436 }
437
438 EXPORT_SYMBOL(unregister_reboot_notifier);
439
440 static int set_one_prio(struct task_struct *p, int niceval, int error)
441 {
442         int no_nice;
443
444         if (p->uid != current->euid &&
445                 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
446                 error = -EPERM;
447                 goto out;
448         }
449         if (niceval < task_nice(p) && !can_nice(p, niceval)) {
450                 if (vx_flags(VXF_IGNEG_NICE, 0))
451                         error = 0;
452                 else
453                         error = -EACCES;
454                 goto out;
455         }
456         no_nice = security_task_setnice(p, niceval);
457         if (no_nice) {
458                 error = no_nice;
459                 goto out;
460         }
461         if (error == -ESRCH)
462                 error = 0;
463         set_user_nice(p, niceval);
464 out:
465         return error;
466 }
467
468 asmlinkage long sys_setpriority(int which, int who, int niceval)
469 {
470         struct task_struct *g, *p;
471         struct user_struct *user;
472         int error = -EINVAL;
473
474         if (which > 2 || which < 0)
475                 goto out;
476
477         /* normalize: avoid signed division (rounding problems) */
478         error = -ESRCH;
479         if (niceval < -20)
480                 niceval = -20;
481         if (niceval > 19)
482                 niceval = 19;
483
484         read_lock(&tasklist_lock);
485         switch (which) {
486                 case PRIO_PROCESS:
487                         if (!who)
488                                 who = current->pid;
489                         p = find_task_by_pid(who);
490                         if (p)
491                                 error = set_one_prio(p, niceval, error);
492                         break;
493                 case PRIO_PGRP:
494                         if (!who)
495                                 who = process_group(current);
496                         do_each_task_pid(who, PIDTYPE_PGID, p) {
497                                 error = set_one_prio(p, niceval, error);
498                         } while_each_task_pid(who, PIDTYPE_PGID, p);
499                         break;
500                 case PRIO_USER:
501                         user = current->user;
502                         if (!who)
503                                 who = current->uid;
504                         else
505                                 if ((who != current->uid) &&
506                                         !(user = find_user(vx_current_xid(), who)))
507                                         goto out_unlock;        /* No processes for this user */
508
509                         do_each_thread(g, p)
510                                 if (p->uid == who)
511                                         error = set_one_prio(p, niceval, error);
512                         while_each_thread(g, p);
513                         if (who != current->uid)
514                                 free_uid(user);         /* For find_user() */
515                         break;
516         }
517 out_unlock:
518         read_unlock(&tasklist_lock);
519 out:
520         return error;
521 }
522
523 /*
524  * Ugh. To avoid negative return values, "getpriority()" will
525  * not return the normal nice-value, but a negated value that
526  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
527  * to stay compatible.
528  */
529 asmlinkage long sys_getpriority(int which, int who)
530 {
531         struct task_struct *g, *p;
532         struct user_struct *user;
533         long niceval, retval = -ESRCH;
534
535         if (which > 2 || which < 0)
536                 return -EINVAL;
537
538         read_lock(&tasklist_lock);
539         switch (which) {
540                 case PRIO_PROCESS:
541                         if (!who)
542                                 who = current->pid;
543                         p = find_task_by_pid(who);
544                         if (p) {
545                                 niceval = 20 - task_nice(p);
546                                 if (niceval > retval)
547                                         retval = niceval;
548                         }
549                         break;
550                 case PRIO_PGRP:
551                         if (!who)
552                                 who = process_group(current);
553                         do_each_task_pid(who, PIDTYPE_PGID, p) {
554                                 niceval = 20 - task_nice(p);
555                                 if (niceval > retval)
556                                         retval = niceval;
557                         } while_each_task_pid(who, PIDTYPE_PGID, p);
558                         break;
559                 case PRIO_USER:
560                         user = current->user;
561                         if (!who)
562                                 who = current->uid;
563                         else
564                                 if ((who != current->uid) &&
565                                         !(user = find_user(vx_current_xid(), who)))
566                                         goto out_unlock;        /* No processes for this user */
567
568                         do_each_thread(g, p)
569                                 if (p->uid == who) {
570                                         niceval = 20 - task_nice(p);
571                                         if (niceval > retval)
572                                                 retval = niceval;
573                                 }
574                         while_each_thread(g, p);
575                         if (who != current->uid)
576                                 free_uid(user);         /* for find_user() */
577                         break;
578         }
579 out_unlock:
580         read_unlock(&tasklist_lock);
581
582         return retval;
583 }
584
585 /**
586  *      emergency_restart - reboot the system
587  *
588  *      Without shutting down any hardware or taking any locks
589  *      reboot the system.  This is called when we know we are in
590  *      trouble so this is our best effort to reboot.  This is
591  *      safe to call in interrupt context.
592  */
593 void emergency_restart(void)
594 {
595         machine_emergency_restart();
596 }
597 EXPORT_SYMBOL_GPL(emergency_restart);
598
599 static void kernel_restart_prepare(char *cmd)
600 {
601         blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
602         system_state = SYSTEM_RESTART;
603         device_shutdown();
604 }
605
606 /**
607  *      kernel_restart - reboot the system
608  *      @cmd: pointer to buffer containing command to execute for restart
609  *              or %NULL
610  *
611  *      Shutdown everything and perform a clean reboot.
612  *      This is not safe to call in interrupt context.
613  */
614 void kernel_restart(char *cmd)
615 {
616         kernel_restart_prepare(cmd);
617         if (!cmd) {
618                 printk(KERN_EMERG "Restarting system.\n");
619         } else {
620                 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
621         }
622         printk(".\n");
623         machine_restart(cmd);
624 }
625 EXPORT_SYMBOL_GPL(kernel_restart);
626
627 /**
628  *      kernel_kexec - reboot the system
629  *
630  *      Move into place and start executing a preloaded standalone
631  *      executable.  If nothing was preloaded return an error.
632  */
633 static void kernel_kexec(void)
634 {
635 #ifdef CONFIG_KEXEC
636         struct kimage *image;
637         image = xchg(&kexec_image, NULL);
638         if (!image) {
639                 return;
640         }
641         kernel_restart_prepare(NULL);
642         printk(KERN_EMERG "Starting new kernel\n");
643         machine_shutdown();
644         machine_kexec(image);
645 #endif
646 }
647
648 void kernel_shutdown_prepare(enum system_states state)
649 {
650         blocking_notifier_call_chain(&reboot_notifier_list,
651                 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
652         system_state = state;
653         device_shutdown();
654 }
655 /**
656  *      kernel_halt - halt the system
657  *
658  *      Shutdown everything and perform a clean system halt.
659  */
660 void kernel_halt(void)
661 {
662         kernel_shutdown_prepare(SYSTEM_HALT);
663         printk(KERN_EMERG "System halted.\n");
664         machine_halt();
665 }
666
667 EXPORT_SYMBOL_GPL(kernel_halt);
668
669 /**
670  *      kernel_power_off - power_off the system
671  *
672  *      Shutdown everything and perform a clean system power_off.
673  */
674 void kernel_power_off(void)
675 {
676         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
677         printk(KERN_EMERG "Power down.\n");
678         machine_power_off();
679 }
680 EXPORT_SYMBOL_GPL(kernel_power_off);
681
682 long vs_reboot(unsigned int, void __user *);
683
684 /*
685  * Reboot system call: for obvious reasons only root may call it,
686  * and even root needs to set up some magic numbers in the registers
687  * so that some mistake won't make this reboot the whole machine.
688  * You can also set the meaning of the ctrl-alt-del-key here.
689  *
690  * reboot doesn't sync: do that yourself before calling this.
691  */
692 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
693 {
694         char buffer[256];
695
696         /* We only trust the superuser with rebooting the system. */
697         if (!capable(CAP_SYS_BOOT))
698                 return -EPERM;
699
700         /* For safety, we require "magic" arguments. */
701         if (magic1 != LINUX_REBOOT_MAGIC1 ||
702             (magic2 != LINUX_REBOOT_MAGIC2 &&
703                         magic2 != LINUX_REBOOT_MAGIC2A &&
704                         magic2 != LINUX_REBOOT_MAGIC2B &&
705                         magic2 != LINUX_REBOOT_MAGIC2C))
706                 return -EINVAL;
707
708         /* Instead of trying to make the power_off code look like
709          * halt when pm_power_off is not set do it the easy way.
710          */
711         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
712                 cmd = LINUX_REBOOT_CMD_HALT;
713
714         if (!vx_check(0, VX_ADMIN|VX_WATCH))
715                 return vs_reboot(cmd, arg);
716
717         lock_kernel();
718         switch (cmd) {
719         case LINUX_REBOOT_CMD_RESTART:
720                 kernel_restart(NULL);
721                 break;
722
723         case LINUX_REBOOT_CMD_CAD_ON:
724                 C_A_D = 1;
725                 break;
726
727         case LINUX_REBOOT_CMD_CAD_OFF:
728                 C_A_D = 0;
729                 break;
730
731         case LINUX_REBOOT_CMD_HALT:
732                 kernel_halt();
733                 unlock_kernel();
734                 do_exit(0);
735                 break;
736
737         case LINUX_REBOOT_CMD_POWER_OFF:
738                 kernel_power_off();
739                 unlock_kernel();
740                 do_exit(0);
741                 break;
742
743         case LINUX_REBOOT_CMD_RESTART2:
744                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
745                         unlock_kernel();
746                         return -EFAULT;
747                 }
748                 buffer[sizeof(buffer) - 1] = '\0';
749
750                 kernel_restart(buffer);
751                 break;
752
753         case LINUX_REBOOT_CMD_KEXEC:
754                 kernel_kexec();
755                 unlock_kernel();
756                 return -EINVAL;
757
758 #ifdef CONFIG_SOFTWARE_SUSPEND
759         case LINUX_REBOOT_CMD_SW_SUSPEND:
760                 {
761                         int ret = software_suspend();
762                         unlock_kernel();
763                         return ret;
764                 }
765 #endif
766
767         default:
768                 unlock_kernel();
769                 return -EINVAL;
770         }
771         unlock_kernel();
772         return 0;
773 }
774
775 static void deferred_cad(void *dummy)
776 {
777         kernel_restart(NULL);
778 }
779
780 /*
781  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
782  * As it's called within an interrupt, it may NOT sync: the only choice
783  * is whether to reboot at once, or just ignore the ctrl-alt-del.
784  */
785 void ctrl_alt_del(void)
786 {
787         static DECLARE_WORK(cad_work, deferred_cad, NULL);
788
789         if (C_A_D)
790                 schedule_work(&cad_work);
791         else
792                 kill_proc(cad_pid, SIGINT, 1);
793 }
794         
795
796 /*
797  * Unprivileged users may change the real gid to the effective gid
798  * or vice versa.  (BSD-style)
799  *
800  * If you set the real gid at all, or set the effective gid to a value not
801  * equal to the real gid, then the saved gid is set to the new effective gid.
802  *
803  * This makes it possible for a setgid program to completely drop its
804  * privileges, which is often a useful assertion to make when you are doing
805  * a security audit over a program.
806  *
807  * The general idea is that a program which uses just setregid() will be
808  * 100% compatible with BSD.  A program which uses just setgid() will be
809  * 100% compatible with POSIX with saved IDs. 
810  *
811  * SMP: There are not races, the GIDs are checked only by filesystem
812  *      operations (as far as semantic preservation is concerned).
813  */
814 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
815 {
816         int old_rgid = current->gid;
817         int old_egid = current->egid;
818         int new_rgid = old_rgid;
819         int new_egid = old_egid;
820         int retval;
821
822         retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
823         if (retval)
824                 return retval;
825
826         if (rgid != (gid_t) -1) {
827                 if ((old_rgid == rgid) ||
828                     (current->egid==rgid) ||
829                     capable(CAP_SETGID))
830                         new_rgid = rgid;
831                 else
832                         return -EPERM;
833         }
834         if (egid != (gid_t) -1) {
835                 if ((old_rgid == egid) ||
836                     (current->egid == egid) ||
837                     (current->sgid == egid) ||
838                     capable(CAP_SETGID))
839                         new_egid = egid;
840                 else {
841                         return -EPERM;
842                 }
843         }
844         if (new_egid != old_egid)
845         {
846                 current->mm->dumpable = suid_dumpable;
847                 smp_wmb();
848         }
849         if (rgid != (gid_t) -1 ||
850             (egid != (gid_t) -1 && egid != old_rgid))
851                 current->sgid = new_egid;
852         current->fsgid = new_egid;
853         current->egid = new_egid;
854         current->gid = new_rgid;
855         key_fsgid_changed(current);
856         proc_id_connector(current, PROC_EVENT_GID);
857         return 0;
858 }
859
860 /*
861  * setgid() is implemented like SysV w/ SAVED_IDS 
862  *
863  * SMP: Same implicit races as above.
864  */
865 asmlinkage long sys_setgid(gid_t gid)
866 {
867         int old_egid = current->egid;
868         int retval;
869
870         retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
871         if (retval)
872                 return retval;
873
874         if (capable(CAP_SETGID))
875         {
876                 if(old_egid != gid)
877                 {
878                         current->mm->dumpable = suid_dumpable;
879                         smp_wmb();
880                 }
881                 current->gid = current->egid = current->sgid = current->fsgid = gid;
882         }
883         else if ((gid == current->gid) || (gid == current->sgid))
884         {
885                 if(old_egid != gid)
886                 {
887                         current->mm->dumpable = suid_dumpable;
888                         smp_wmb();
889                 }
890                 current->egid = current->fsgid = gid;
891         }
892         else
893                 return -EPERM;
894
895         key_fsgid_changed(current);
896         proc_id_connector(current, PROC_EVENT_GID);
897         return 0;
898 }
899   
900 static int set_user(uid_t new_ruid, int dumpclear)
901 {
902         struct user_struct *new_user;
903
904         new_user = alloc_uid(vx_current_xid(), new_ruid);
905         if (!new_user)
906                 return -EAGAIN;
907
908         if (atomic_read(&new_user->processes) >=
909                                 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
910                         new_user != &root_user) {
911                 free_uid(new_user);
912                 return -EAGAIN;
913         }
914
915         switch_uid(new_user);
916
917         if(dumpclear)
918         {
919                 current->mm->dumpable = suid_dumpable;
920                 smp_wmb();
921         }
922         current->uid = new_ruid;
923         return 0;
924 }
925
926 /*
927  * Unprivileged users may change the real uid to the effective uid
928  * or vice versa.  (BSD-style)
929  *
930  * If you set the real uid at all, or set the effective uid to a value not
931  * equal to the real uid, then the saved uid is set to the new effective uid.
932  *
933  * This makes it possible for a setuid program to completely drop its
934  * privileges, which is often a useful assertion to make when you are doing
935  * a security audit over a program.
936  *
937  * The general idea is that a program which uses just setreuid() will be
938  * 100% compatible with BSD.  A program which uses just setuid() will be
939  * 100% compatible with POSIX with saved IDs. 
940  */
941 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
942 {
943         int old_ruid, old_euid, old_suid, new_ruid, new_euid;
944         int retval;
945
946         retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
947         if (retval)
948                 return retval;
949
950         new_ruid = old_ruid = current->uid;
951         new_euid = old_euid = current->euid;
952         old_suid = current->suid;
953
954         if (ruid != (uid_t) -1) {
955                 new_ruid = ruid;
956                 if ((old_ruid != ruid) &&
957                     (current->euid != ruid) &&
958                     !capable(CAP_SETUID))
959                         return -EPERM;
960         }
961
962         if (euid != (uid_t) -1) {
963                 new_euid = euid;
964                 if ((old_ruid != euid) &&
965                     (current->euid != euid) &&
966                     (current->suid != euid) &&
967                     !capable(CAP_SETUID))
968                         return -EPERM;
969         }
970
971         if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
972                 return -EAGAIN;
973
974         if (new_euid != old_euid)
975         {
976                 current->mm->dumpable = suid_dumpable;
977                 smp_wmb();
978         }
979         current->fsuid = current->euid = new_euid;
980         if (ruid != (uid_t) -1 ||
981             (euid != (uid_t) -1 && euid != old_ruid))
982                 current->suid = current->euid;
983         current->fsuid = current->euid;
984
985         key_fsuid_changed(current);
986         proc_id_connector(current, PROC_EVENT_UID);
987
988         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
989 }
990
991
992                 
993 /*
994  * setuid() is implemented like SysV with SAVED_IDS 
995  * 
996  * Note that SAVED_ID's is deficient in that a setuid root program
997  * like sendmail, for example, cannot set its uid to be a normal 
998  * user and then switch back, because if you're root, setuid() sets
999  * the saved uid too.  If you don't like this, blame the bright people
1000  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
1001  * will allow a root program to temporarily drop privileges and be able to
1002  * regain them by swapping the real and effective uid.  
1003  */
1004 asmlinkage long sys_setuid(uid_t uid)
1005 {
1006         int old_euid = current->euid;
1007         int old_ruid, old_suid, new_ruid, new_suid;
1008         int retval;
1009
1010         retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1011         if (retval)
1012                 return retval;
1013
1014         old_ruid = new_ruid = current->uid;
1015         old_suid = current->suid;
1016         new_suid = old_suid;
1017         
1018         if (capable(CAP_SETUID)) {
1019                 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1020                         return -EAGAIN;
1021                 new_suid = uid;
1022         } else if ((uid != current->uid) && (uid != new_suid))
1023                 return -EPERM;
1024
1025         if (old_euid != uid)
1026         {
1027                 current->mm->dumpable = suid_dumpable;
1028                 smp_wmb();
1029         }
1030         current->fsuid = current->euid = uid;
1031         current->suid = new_suid;
1032
1033         key_fsuid_changed(current);
1034         proc_id_connector(current, PROC_EVENT_UID);
1035
1036         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1037 }
1038
1039
1040 /*
1041  * This function implements a generic ability to update ruid, euid,
1042  * and suid.  This allows you to implement the 4.4 compatible seteuid().
1043  */
1044 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1045 {
1046         int old_ruid = current->uid;
1047         int old_euid = current->euid;
1048         int old_suid = current->suid;
1049         int retval;
1050
1051         retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1052         if (retval)
1053                 return retval;
1054
1055         if (!capable(CAP_SETUID)) {
1056                 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1057                     (ruid != current->euid) && (ruid != current->suid))
1058                         return -EPERM;
1059                 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1060                     (euid != current->euid) && (euid != current->suid))
1061                         return -EPERM;
1062                 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1063                     (suid != current->euid) && (suid != current->suid))
1064                         return -EPERM;
1065         }
1066         if (ruid != (uid_t) -1) {
1067                 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1068                         return -EAGAIN;
1069         }
1070         if (euid != (uid_t) -1) {
1071                 if (euid != current->euid)
1072                 {
1073                         current->mm->dumpable = suid_dumpable;
1074                         smp_wmb();
1075                 }
1076                 current->euid = euid;
1077         }
1078         current->fsuid = current->euid;
1079         if (suid != (uid_t) -1)
1080                 current->suid = suid;
1081
1082         key_fsuid_changed(current);
1083         proc_id_connector(current, PROC_EVENT_UID);
1084
1085         return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1086 }
1087
1088 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1089 {
1090         int retval;
1091
1092         if (!(retval = put_user(current->uid, ruid)) &&
1093             !(retval = put_user(current->euid, euid)))
1094                 retval = put_user(current->suid, suid);
1095
1096         return retval;
1097 }
1098
1099 /*
1100  * Same as above, but for rgid, egid, sgid.
1101  */
1102 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1103 {
1104         int retval;
1105
1106         retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1107         if (retval)
1108                 return retval;
1109
1110         if (!capable(CAP_SETGID)) {
1111                 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1112                     (rgid != current->egid) && (rgid != current->sgid))
1113                         return -EPERM;
1114                 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1115                     (egid != current->egid) && (egid != current->sgid))
1116                         return -EPERM;
1117                 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1118                     (sgid != current->egid) && (sgid != current->sgid))
1119                         return -EPERM;
1120         }
1121         if (egid != (gid_t) -1) {
1122                 if (egid != current->egid)
1123                 {
1124                         current->mm->dumpable = suid_dumpable;
1125                         smp_wmb();
1126                 }
1127                 current->egid = egid;
1128         }
1129         current->fsgid = current->egid;
1130         if (rgid != (gid_t) -1)
1131                 current->gid = rgid;
1132         if (sgid != (gid_t) -1)
1133                 current->sgid = sgid;
1134
1135         key_fsgid_changed(current);
1136         proc_id_connector(current, PROC_EVENT_GID);
1137         return 0;
1138 }
1139
1140 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1141 {
1142         int retval;
1143
1144         if (!(retval = put_user(current->gid, rgid)) &&
1145             !(retval = put_user(current->egid, egid)))
1146                 retval = put_user(current->sgid, sgid);
1147
1148         return retval;
1149 }
1150
1151
1152 /*
1153  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1154  * is used for "access()" and for the NFS daemon (letting nfsd stay at
1155  * whatever uid it wants to). It normally shadows "euid", except when
1156  * explicitly set by setfsuid() or for access..
1157  */
1158 asmlinkage long sys_setfsuid(uid_t uid)
1159 {
1160         int old_fsuid;
1161
1162         old_fsuid = current->fsuid;
1163         if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1164                 return old_fsuid;
1165
1166         if (uid == current->uid || uid == current->euid ||
1167             uid == current->suid || uid == current->fsuid || 
1168             capable(CAP_SETUID))
1169         {
1170                 if (uid != old_fsuid)
1171                 {
1172                         current->mm->dumpable = suid_dumpable;
1173                         smp_wmb();
1174                 }
1175                 current->fsuid = uid;
1176         }
1177
1178         key_fsuid_changed(current);
1179         proc_id_connector(current, PROC_EVENT_UID);
1180
1181         security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1182
1183         return old_fsuid;
1184 }
1185
1186 /*
1187  * Samma pÃ¥ svenska..
1188  */
1189 asmlinkage long sys_setfsgid(gid_t gid)
1190 {
1191         int old_fsgid;
1192
1193         old_fsgid = current->fsgid;
1194         if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1195                 return old_fsgid;
1196
1197         if (gid == current->gid || gid == current->egid ||
1198             gid == current->sgid || gid == current->fsgid || 
1199             capable(CAP_SETGID))
1200         {
1201                 if (gid != old_fsgid)
1202                 {
1203                         current->mm->dumpable = suid_dumpable;
1204                         smp_wmb();
1205                 }
1206                 current->fsgid = gid;
1207                 key_fsgid_changed(current);
1208                 proc_id_connector(current, PROC_EVENT_GID);
1209         }
1210         return old_fsgid;
1211 }
1212
1213 asmlinkage long sys_times(struct tms __user * tbuf)
1214 {
1215         /*
1216          *      In the SMP world we might just be unlucky and have one of
1217          *      the times increment as we use it. Since the value is an
1218          *      atomically safe type this is just fine. Conceptually its
1219          *      as if the syscall took an instant longer to occur.
1220          */
1221         if (tbuf) {
1222                 struct tms tmp;
1223                 struct task_struct *tsk = current;
1224                 struct task_struct *t;
1225                 cputime_t utime, stime, cutime, cstime;
1226
1227                 spin_lock_irq(&tsk->sighand->siglock);
1228                 utime = tsk->signal->utime;
1229                 stime = tsk->signal->stime;
1230                 t = tsk;
1231                 do {
1232                         utime = cputime_add(utime, t->utime);
1233                         stime = cputime_add(stime, t->stime);
1234                         t = next_thread(t);
1235                 } while (t != tsk);
1236
1237                 cutime = tsk->signal->cutime;
1238                 cstime = tsk->signal->cstime;
1239                 spin_unlock_irq(&tsk->sighand->siglock);
1240
1241                 tmp.tms_utime = cputime_to_clock_t(utime);
1242                 tmp.tms_stime = cputime_to_clock_t(stime);
1243                 tmp.tms_cutime = cputime_to_clock_t(cutime);
1244                 tmp.tms_cstime = cputime_to_clock_t(cstime);
1245                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1246                         return -EFAULT;
1247         }
1248         return (long) jiffies_64_to_clock_t(get_jiffies_64());
1249 }
1250
1251 /*
1252  * This needs some heavy checking ...
1253  * I just haven't the stomach for it. I also don't fully
1254  * understand sessions/pgrp etc. Let somebody who does explain it.
1255  *
1256  * OK, I think I have the protection semantics right.... this is really
1257  * only important on a multi-user system anyway, to make sure one user
1258  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1259  *
1260  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1261  * LBT 04.03.94
1262  */
1263
1264 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1265 {
1266         struct task_struct *p;
1267         struct task_struct *group_leader = current->group_leader;
1268         pid_t rpgid;
1269         int err = -EINVAL;
1270
1271         if (!pid)
1272                 pid = vx_map_pid(group_leader->pid);
1273         if (!pgid)
1274                 pgid = pid;
1275         if (pgid < 0)
1276                 return -EINVAL;
1277
1278         rpgid = vx_rmap_pid(pgid);
1279
1280         /* From this point forward we keep holding onto the tasklist lock
1281          * so that our parent does not change from under us. -DaveM
1282          */
1283         write_lock_irq(&tasklist_lock);
1284
1285         err = -ESRCH;
1286         p = find_task_by_pid(pid);
1287         if (!p)
1288                 goto out;
1289
1290         err = -EINVAL;
1291         if (!thread_group_leader(p))
1292                 goto out;
1293
1294         if (p->real_parent == group_leader) {
1295                 err = -EPERM;
1296                 if (p->signal->session != group_leader->signal->session)
1297                         goto out;
1298                 err = -EACCES;
1299                 if (p->did_exec)
1300                         goto out;
1301         } else {
1302                 err = -ESRCH;
1303                 if (p != group_leader)
1304                         goto out;
1305         }
1306
1307         err = -EPERM;
1308         if (p->signal->leader)
1309                 goto out;
1310
1311         if (pgid != pid) {
1312                 struct task_struct *p;
1313
1314                 do_each_task_pid(rpgid, PIDTYPE_PGID, p) {
1315                         if (p->signal->session == group_leader->signal->session)
1316                                 goto ok_pgid;
1317                 } while_each_task_pid(rpgid, PIDTYPE_PGID, p);
1318                 goto out;
1319         }
1320
1321 ok_pgid:
1322         err = security_task_setpgid(p, rpgid);
1323         if (err)
1324                 goto out;
1325
1326         if (process_group(p) != rpgid) {
1327                 detach_pid(p, PIDTYPE_PGID);
1328                 p->signal->pgrp = rpgid;
1329                 attach_pid(p, PIDTYPE_PGID, rpgid);
1330         }
1331
1332         err = 0;
1333 out:
1334         /* All paths lead to here, thus we are safe. -DaveM */
1335         write_unlock_irq(&tasklist_lock);
1336         return err;
1337 }
1338
1339 asmlinkage long sys_getpgid(pid_t pid)
1340 {
1341         if (!pid) {
1342                 return vx_rmap_pid(process_group(current));
1343         } else {
1344                 int retval;
1345                 struct task_struct *p;
1346
1347                 read_lock(&tasklist_lock);
1348                 p = find_task_by_pid(pid);
1349
1350                 retval = -ESRCH;
1351                 if (p) {
1352                         retval = security_task_getpgid(p);
1353                         if (!retval)
1354                                 retval = vx_rmap_pid(process_group(p));
1355                 }
1356                 read_unlock(&tasklist_lock);
1357                 return retval;
1358         }
1359 }
1360
1361 #ifdef __ARCH_WANT_SYS_GETPGRP
1362
1363 asmlinkage long sys_getpgrp(void)
1364 {
1365         /* SMP - assuming writes are word atomic this is fine */
1366         return process_group(current);
1367 }
1368
1369 #endif
1370
1371 asmlinkage long sys_getsid(pid_t pid)
1372 {
1373         if (!pid) {
1374                 return current->signal->session;
1375         } else {
1376                 int retval;
1377                 struct task_struct *p;
1378
1379                 read_lock(&tasklist_lock);
1380                 p = find_task_by_pid(pid);
1381
1382                 retval = -ESRCH;
1383                 if(p) {
1384                         retval = security_task_getsid(p);
1385                         if (!retval)
1386                                 retval = p->signal->session;
1387                 }
1388                 read_unlock(&tasklist_lock);
1389                 return retval;
1390         }
1391 }
1392
1393 asmlinkage long sys_setsid(void)
1394 {
1395         struct task_struct *group_leader = current->group_leader;
1396         pid_t session;
1397         int err = -EPERM;
1398
1399         write_lock_irq(&tasklist_lock);
1400
1401         /* Fail if I am already a session leader */
1402         if (group_leader->signal->leader)
1403                 goto out;
1404
1405         session = group_leader->pid;
1406         /* Fail if a process group id already exists that equals the
1407          * proposed session id.
1408          *
1409          * Don't check if session id == 1 because kernel threads use this
1410          * session id and so the check will always fail and make it so
1411          * init cannot successfully call setsid.
1412          */
1413         if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1414                 goto out;
1415
1416         group_leader->signal->leader = 1;
1417         __set_special_pids(session, session);
1418
1419         spin_lock(&group_leader->sighand->siglock);
1420         group_leader->signal->tty = NULL;
1421         group_leader->signal->tty_old_pgrp = 0;
1422         spin_unlock(&group_leader->sighand->siglock);
1423
1424         err = process_group(group_leader);
1425 out:
1426         write_unlock_irq(&tasklist_lock);
1427         return err;
1428 }
1429
1430 /*
1431  * Supplementary group IDs
1432  */
1433
1434 /* init to 2 - one for init_task, one to ensure it is never freed */
1435 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1436
1437 struct group_info *groups_alloc(int gidsetsize)
1438 {
1439         struct group_info *group_info;
1440         int nblocks;
1441         int i;
1442
1443         nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1444         /* Make sure we always allocate at least one indirect block pointer */
1445         nblocks = nblocks ? : 1;
1446         group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1447         if (!group_info)
1448                 return NULL;
1449         group_info->ngroups = gidsetsize;
1450         group_info->nblocks = nblocks;
1451         atomic_set(&group_info->usage, 1);
1452
1453         if (gidsetsize <= NGROUPS_SMALL) {
1454                 group_info->blocks[0] = group_info->small_block;
1455         } else {
1456                 for (i = 0; i < nblocks; i++) {
1457                         gid_t *b;
1458                         b = (void *)__get_free_page(GFP_USER);
1459                         if (!b)
1460                                 goto out_undo_partial_alloc;
1461                         group_info->blocks[i] = b;
1462                 }
1463         }
1464         return group_info;
1465
1466 out_undo_partial_alloc:
1467         while (--i >= 0) {
1468                 free_page((unsigned long)group_info->blocks[i]);
1469         }
1470         kfree(group_info);
1471         return NULL;
1472 }
1473
1474 EXPORT_SYMBOL(groups_alloc);
1475
1476 void groups_free(struct group_info *group_info)
1477 {
1478         if (group_info->blocks[0] != group_info->small_block) {
1479                 int i;
1480                 for (i = 0; i < group_info->nblocks; i++)
1481                         free_page((unsigned long)group_info->blocks[i]);
1482         }
1483         kfree(group_info);
1484 }
1485
1486 EXPORT_SYMBOL(groups_free);
1487
1488 /* export the group_info to a user-space array */
1489 static int groups_to_user(gid_t __user *grouplist,
1490     struct group_info *group_info)
1491 {
1492         int i;
1493         int count = group_info->ngroups;
1494
1495         for (i = 0; i < group_info->nblocks; i++) {
1496                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1497                 int off = i * NGROUPS_PER_BLOCK;
1498                 int len = cp_count * sizeof(*grouplist);
1499
1500                 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1501                         return -EFAULT;
1502
1503                 count -= cp_count;
1504         }
1505         return 0;
1506 }
1507
1508 /* fill a group_info from a user-space array - it must be allocated already */
1509 static int groups_from_user(struct group_info *group_info,
1510     gid_t __user *grouplist)
1511  {
1512         int i;
1513         int count = group_info->ngroups;
1514
1515         for (i = 0; i < group_info->nblocks; i++) {
1516                 int cp_count = min(NGROUPS_PER_BLOCK, count);
1517                 int off = i * NGROUPS_PER_BLOCK;
1518                 int len = cp_count * sizeof(*grouplist);
1519
1520                 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1521                         return -EFAULT;
1522
1523                 count -= cp_count;
1524         }
1525         return 0;
1526 }
1527
1528 /* a simple Shell sort */
1529 static void groups_sort(struct group_info *group_info)
1530 {
1531         int base, max, stride;
1532         int gidsetsize = group_info->ngroups;
1533
1534         for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1535                 ; /* nothing */
1536         stride /= 3;
1537
1538         while (stride) {
1539                 max = gidsetsize - stride;
1540                 for (base = 0; base < max; base++) {
1541                         int left = base;
1542                         int right = left + stride;
1543                         gid_t tmp = GROUP_AT(group_info, right);
1544
1545                         while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1546                                 GROUP_AT(group_info, right) =
1547                                     GROUP_AT(group_info, left);
1548                                 right = left;
1549                                 left -= stride;
1550                         }
1551                         GROUP_AT(group_info, right) = tmp;
1552                 }
1553                 stride /= 3;
1554         }
1555 }
1556
1557 /* a simple bsearch */
1558 int groups_search(struct group_info *group_info, gid_t grp)
1559 {
1560         unsigned int left, right;
1561
1562         if (!group_info)
1563                 return 0;
1564
1565         left = 0;
1566         right = group_info->ngroups;
1567         while (left < right) {
1568                 unsigned int mid = (left+right)/2;
1569                 int cmp = grp - GROUP_AT(group_info, mid);
1570                 if (cmp > 0)
1571                         left = mid + 1;
1572                 else if (cmp < 0)
1573                         right = mid;
1574                 else
1575                         return 1;
1576         }
1577         return 0;
1578 }
1579
1580 /* validate and set current->group_info */
1581 int set_current_groups(struct group_info *group_info)
1582 {
1583         int retval;
1584         struct group_info *old_info;
1585
1586         retval = security_task_setgroups(group_info);
1587         if (retval)
1588                 return retval;
1589
1590         groups_sort(group_info);
1591         get_group_info(group_info);
1592
1593         task_lock(current);
1594         old_info = current->group_info;
1595         current->group_info = group_info;
1596         task_unlock(current);
1597
1598         put_group_info(old_info);
1599
1600         return 0;
1601 }
1602
1603 EXPORT_SYMBOL(set_current_groups);
1604
1605 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1606 {
1607         int i = 0;
1608
1609         /*
1610          *      SMP: Nobody else can change our grouplist. Thus we are
1611          *      safe.
1612          */
1613
1614         if (gidsetsize < 0)
1615                 return -EINVAL;
1616
1617         /* no need to grab task_lock here; it cannot change */
1618         i = current->group_info->ngroups;
1619         if (gidsetsize) {
1620                 if (i > gidsetsize) {
1621                         i = -EINVAL;
1622                         goto out;
1623                 }
1624                 if (groups_to_user(grouplist, current->group_info)) {
1625                         i = -EFAULT;
1626                         goto out;
1627                 }
1628         }
1629 out:
1630         return i;
1631 }
1632
1633 /*
1634  *      SMP: Our groups are copy-on-write. We can set them safely
1635  *      without another task interfering.
1636  */
1637  
1638 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1639 {
1640         struct group_info *group_info;
1641         int retval;
1642
1643         if (!capable(CAP_SETGID))
1644                 return -EPERM;
1645         if ((unsigned)gidsetsize > NGROUPS_MAX)
1646                 return -EINVAL;
1647
1648         group_info = groups_alloc(gidsetsize);
1649         if (!group_info)
1650                 return -ENOMEM;
1651         retval = groups_from_user(group_info, grouplist);
1652         if (retval) {
1653                 put_group_info(group_info);
1654                 return retval;
1655         }
1656
1657         retval = set_current_groups(group_info);
1658         put_group_info(group_info);
1659
1660         return retval;
1661 }
1662
1663 /*
1664  * Check whether we're fsgid/egid or in the supplemental group..
1665  */
1666 int in_group_p(gid_t grp)
1667 {
1668         int retval = 1;
1669         if (grp != current->fsgid) {
1670                 retval = groups_search(current->group_info, grp);
1671         }
1672         return retval;
1673 }
1674
1675 EXPORT_SYMBOL(in_group_p);
1676
1677 int in_egroup_p(gid_t grp)
1678 {
1679         int retval = 1;
1680         if (grp != current->egid) {
1681                 retval = groups_search(current->group_info, grp);
1682         }
1683         return retval;
1684 }
1685
1686 EXPORT_SYMBOL(in_egroup_p);
1687
1688 DECLARE_RWSEM(uts_sem);
1689
1690 EXPORT_SYMBOL(uts_sem);
1691
1692 asmlinkage long sys_newuname(struct new_utsname __user * name)
1693 {
1694         int errno = 0;
1695
1696         down_read(&uts_sem);
1697         if (copy_to_user(name, vx_new_utsname(), sizeof *name))
1698                 errno = -EFAULT;
1699         up_read(&uts_sem);
1700         return errno;
1701 }
1702
1703 asmlinkage long sys_sethostname(char __user *name, int len)
1704 {
1705         int errno;
1706         char tmp[__NEW_UTS_LEN];
1707
1708         if (!vx_capable(CAP_SYS_ADMIN, VXC_SET_UTSNAME))
1709                 return -EPERM;
1710         if (len < 0 || len > __NEW_UTS_LEN)
1711                 return -EINVAL;
1712         down_write(&uts_sem);
1713         errno = -EFAULT;
1714         if (!copy_from_user(tmp, name, len)) {
1715                 char *ptr = vx_new_uts(nodename);
1716
1717                 memcpy(ptr, tmp, len);
1718                 ptr[len] = 0;
1719                 errno = 0;
1720         }
1721         up_write(&uts_sem);
1722         return errno;
1723 }
1724
1725 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1726
1727 asmlinkage long sys_gethostname(char __user *name, int len)
1728 {
1729         int i, errno;
1730         char *ptr;
1731
1732         if (len < 0)
1733                 return -EINVAL;
1734         down_read(&uts_sem);
1735         ptr = vx_new_uts(nodename);
1736         i = 1 + strlen(ptr);
1737         if (i > len)
1738                 i = len;
1739         errno = 0;
1740         if (copy_to_user(name, ptr, i))
1741                 errno = -EFAULT;
1742         up_read(&uts_sem);
1743         return errno;
1744 }
1745
1746 #endif
1747
1748 /*
1749  * Only setdomainname; getdomainname can be implemented by calling
1750  * uname()
1751  */
1752 asmlinkage long sys_setdomainname(char __user *name, int len)
1753 {
1754         int errno;
1755         char tmp[__NEW_UTS_LEN];
1756
1757         if (!vx_capable(CAP_SYS_ADMIN, VXC_SET_UTSNAME))
1758                 return -EPERM;
1759         if (len < 0 || len > __NEW_UTS_LEN)
1760                 return -EINVAL;
1761
1762         down_write(&uts_sem);
1763         errno = -EFAULT;
1764         if (!copy_from_user(tmp, name, len)) {
1765                 char *ptr = vx_new_uts(domainname);
1766
1767                 memcpy(ptr, tmp, len);
1768                 ptr[len] = 0;
1769                 errno = 0;
1770         }
1771         up_write(&uts_sem);
1772         return errno;
1773 }
1774
1775 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1776 {
1777         if (resource >= RLIM_NLIMITS)
1778                 return -EINVAL;
1779         else {
1780                 struct rlimit value;
1781                 task_lock(current->group_leader);
1782                 value = current->signal->rlim[resource];
1783                 task_unlock(current->group_leader);
1784                 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1785         }
1786 }
1787
1788 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1789
1790 /*
1791  *      Back compatibility for getrlimit. Needed for some apps.
1792  */
1793  
1794 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1795 {
1796         struct rlimit x;
1797         if (resource >= RLIM_NLIMITS)
1798                 return -EINVAL;
1799
1800         task_lock(current->group_leader);
1801         x = current->signal->rlim[resource];
1802         task_unlock(current->group_leader);
1803         if(x.rlim_cur > 0x7FFFFFFF)
1804                 x.rlim_cur = 0x7FFFFFFF;
1805         if(x.rlim_max > 0x7FFFFFFF)
1806                 x.rlim_max = 0x7FFFFFFF;
1807         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1808 }
1809
1810 #endif
1811
1812 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1813 {
1814         struct rlimit new_rlim, *old_rlim;
1815         unsigned long it_prof_secs;
1816         int retval;
1817
1818         if (resource >= RLIM_NLIMITS)
1819                 return -EINVAL;
1820         if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1821                 return -EFAULT;
1822         if (new_rlim.rlim_cur > new_rlim.rlim_max)
1823                 return -EINVAL;
1824         old_rlim = current->signal->rlim + resource;
1825         if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1826             !vx_capable(CAP_SYS_RESOURCE, VXC_SET_RLIMIT))
1827                 return -EPERM;
1828         if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1829                 return -EPERM;
1830
1831         retval = security_task_setrlimit(resource, &new_rlim);
1832         if (retval)
1833                 return retval;
1834
1835         task_lock(current->group_leader);
1836         *old_rlim = new_rlim;
1837         task_unlock(current->group_leader);
1838
1839         if (resource != RLIMIT_CPU)
1840                 goto out;
1841
1842         /*
1843          * RLIMIT_CPU handling.   Note that the kernel fails to return an error
1844          * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a
1845          * very long-standing error, and fixing it now risks breakage of
1846          * applications, so we live with it
1847          */
1848         if (new_rlim.rlim_cur == RLIM_INFINITY)
1849                 goto out;
1850
1851         it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1852         if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
1853                 unsigned long rlim_cur = new_rlim.rlim_cur;
1854                 cputime_t cputime;
1855
1856                 if (rlim_cur == 0) {
1857                         /*
1858                          * The caller is asking for an immediate RLIMIT_CPU
1859                          * expiry.  But we use the zero value to mean "it was
1860                          * never set".  So let's cheat and make it one second
1861                          * instead
1862                          */
1863                         rlim_cur = 1;
1864                 }
1865                 cputime = secs_to_cputime(rlim_cur);
1866                 read_lock(&tasklist_lock);
1867                 spin_lock_irq(&current->sighand->siglock);
1868                 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
1869                 spin_unlock_irq(&current->sighand->siglock);
1870                 read_unlock(&tasklist_lock);
1871         }
1872 out:
1873         return 0;
1874 }
1875
1876 /*
1877  * It would make sense to put struct rusage in the task_struct,
1878  * except that would make the task_struct be *really big*.  After
1879  * task_struct gets moved into malloc'ed memory, it would
1880  * make sense to do this.  It will make moving the rest of the information
1881  * a lot simpler!  (Which we're not doing right now because we're not
1882  * measuring them yet).
1883  *
1884  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1885  * races with threads incrementing their own counters.  But since word
1886  * reads are atomic, we either get new values or old values and we don't
1887  * care which for the sums.  We always take the siglock to protect reading
1888  * the c* fields from p->signal from races with exit.c updating those
1889  * fields when reaping, so a sample either gets all the additions of a
1890  * given child after it's reaped, or none so this sample is before reaping.
1891  *
1892  * Locking:
1893  * We need to take the siglock for CHILDEREN, SELF and BOTH
1894  * for  the cases current multithreaded, non-current single threaded
1895  * non-current multithreaded.  Thread traversal is now safe with
1896  * the siglock held.
1897  * Strictly speaking, we donot need to take the siglock if we are current and
1898  * single threaded,  as no one else can take our signal_struct away, no one
1899  * else can  reap the  children to update signal->c* counters, and no one else
1900  * can race with the signal-> fields. If we do not take any lock, the
1901  * signal-> fields could be read out of order while another thread was just
1902  * exiting. So we should  place a read memory barrier when we avoid the lock.
1903  * On the writer side,  write memory barrier is implied in  __exit_signal
1904  * as __exit_signal releases  the siglock spinlock after updating the signal->
1905  * fields. But we don't do this yet to keep things simple.
1906  *
1907  */
1908
1909 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1910 {
1911         struct task_struct *t;
1912         unsigned long flags;
1913         cputime_t utime, stime;
1914
1915         memset((char *) r, 0, sizeof *r);
1916         utime = stime = cputime_zero;
1917
1918         rcu_read_lock();
1919         if (!lock_task_sighand(p, &flags)) {
1920                 rcu_read_unlock();
1921                 return;
1922         }
1923
1924         switch (who) {
1925                 case RUSAGE_BOTH:
1926                 case RUSAGE_CHILDREN:
1927                         utime = p->signal->cutime;
1928                         stime = p->signal->cstime;
1929                         r->ru_nvcsw = p->signal->cnvcsw;
1930                         r->ru_nivcsw = p->signal->cnivcsw;
1931                         r->ru_minflt = p->signal->cmin_flt;
1932                         r->ru_majflt = p->signal->cmaj_flt;
1933
1934                         if (who == RUSAGE_CHILDREN)
1935                                 break;
1936
1937                 case RUSAGE_SELF:
1938                         utime = cputime_add(utime, p->signal->utime);
1939                         stime = cputime_add(stime, p->signal->stime);
1940                         r->ru_nvcsw += p->signal->nvcsw;
1941                         r->ru_nivcsw += p->signal->nivcsw;
1942                         r->ru_minflt += p->signal->min_flt;
1943                         r->ru_majflt += p->signal->maj_flt;
1944                         t = p;
1945                         do {
1946                                 utime = cputime_add(utime, t->utime);
1947                                 stime = cputime_add(stime, t->stime);
1948                                 r->ru_nvcsw += t->nvcsw;
1949                                 r->ru_nivcsw += t->nivcsw;
1950                                 r->ru_minflt += t->min_flt;
1951                                 r->ru_majflt += t->maj_flt;
1952                                 t = next_thread(t);
1953                         } while (t != p);
1954                         break;
1955
1956                 default:
1957                         BUG();
1958         }
1959
1960         unlock_task_sighand(p, &flags);
1961         rcu_read_unlock();
1962
1963         cputime_to_timeval(utime, &r->ru_utime);
1964         cputime_to_timeval(stime, &r->ru_stime);
1965 }
1966
1967 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1968 {
1969         struct rusage r;
1970         k_getrusage(p, who, &r);
1971         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1972 }
1973
1974 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1975 {
1976         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1977                 return -EINVAL;
1978         return getrusage(current, who, ru);
1979 }
1980
1981 asmlinkage long sys_umask(int mask)
1982 {
1983         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1984         return mask;
1985 }
1986     
1987 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1988                           unsigned long arg4, unsigned long arg5)
1989 {
1990         long error;
1991
1992         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1993         if (error)
1994                 return error;
1995
1996         switch (option) {
1997                 case PR_SET_PDEATHSIG:
1998                         if (!valid_signal(arg2)) {
1999                                 error = -EINVAL;
2000                                 break;
2001                         }
2002                         current->pdeath_signal = arg2;
2003                         break;
2004                 case PR_GET_PDEATHSIG:
2005                         error = put_user(current->pdeath_signal, (int __user *)arg2);
2006                         break;
2007                 case PR_GET_DUMPABLE:
2008                         error = current->mm->dumpable;
2009                         break;
2010                 case PR_SET_DUMPABLE:
2011                         if (arg2 < 0 || arg2 > 1) {
2012                                 error = -EINVAL;
2013                                 break;
2014                         }
2015                         current->mm->dumpable = arg2;
2016                         break;
2017
2018                 case PR_SET_UNALIGN:
2019                         error = SET_UNALIGN_CTL(current, arg2);
2020                         break;
2021                 case PR_GET_UNALIGN:
2022                         error = GET_UNALIGN_CTL(current, arg2);
2023                         break;
2024                 case PR_SET_FPEMU:
2025                         error = SET_FPEMU_CTL(current, arg2);
2026                         break;
2027                 case PR_GET_FPEMU:
2028                         error = GET_FPEMU_CTL(current, arg2);
2029                         break;
2030                 case PR_SET_FPEXC:
2031                         error = SET_FPEXC_CTL(current, arg2);
2032                         break;
2033                 case PR_GET_FPEXC:
2034                         error = GET_FPEXC_CTL(current, arg2);
2035                         break;
2036                 case PR_GET_TIMING:
2037                         error = PR_TIMING_STATISTICAL;
2038                         break;
2039                 case PR_SET_TIMING:
2040                         if (arg2 == PR_TIMING_STATISTICAL)
2041                                 error = 0;
2042                         else
2043                                 error = -EINVAL;
2044                         break;
2045
2046                 case PR_GET_KEEPCAPS:
2047                         if (current->keep_capabilities)
2048                                 error = 1;
2049                         break;
2050                 case PR_SET_KEEPCAPS:
2051                         if (arg2 != 0 && arg2 != 1) {
2052                                 error = -EINVAL;
2053                                 break;
2054                         }
2055                         current->keep_capabilities = arg2;
2056                         break;
2057                 case PR_SET_NAME: {
2058                         struct task_struct *me = current;
2059                         unsigned char ncomm[sizeof(me->comm)];
2060
2061                         ncomm[sizeof(me->comm)-1] = 0;
2062                         if (strncpy_from_user(ncomm, (char __user *)arg2,
2063                                                 sizeof(me->comm)-1) < 0)
2064                                 return -EFAULT;
2065                         set_task_comm(me, ncomm);
2066                         return 0;
2067                 }
2068                 case PR_GET_NAME: {
2069                         struct task_struct *me = current;
2070                         unsigned char tcomm[sizeof(me->comm)];
2071
2072                         get_task_comm(tcomm, me);
2073                         if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2074                                 return -EFAULT;
2075                         return 0;
2076                 }
2077                 case PR_GET_ENDIAN:
2078                         error = GET_ENDIAN(current, arg2);
2079                         break;
2080                 case PR_SET_ENDIAN:
2081                         error = SET_ENDIAN(current, arg2);
2082                         break;
2083
2084                 default:
2085                         error = -EINVAL;
2086                         break;
2087         }
2088         return error;
2089 }