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