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