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