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