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