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