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