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