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