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