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