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