rk: temp revert rk change
[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/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/file.h>
40 #include <linux/mount.h>
41 #include <linux/gfp.h>
42 #include <linux/syscore_ops.h>
43 #include <linux/version.h>
44 #include <linux/ctype.h>
45 #include <linux/mm.h>
46 #include <linux/mempolicy.h>
47 #include <linux/sched.h>
48
49 #include <linux/compat.h>
50 #include <linux/syscalls.h>
51 #include <linux/kprobes.h>
52 #include <linux/user_namespace.h>
53 #include <linux/binfmts.h>
54
55 #include <linux/sched.h>
56 #include <linux/rcupdate.h>
57 #include <linux/uidgid.h>
58 #include <linux/cred.h>
59
60 #include <linux/kmsg_dump.h>
61 /* Move somewhere else to avoid recompiling? */
62 #include <generated/utsrelease.h>
63
64 #include <asm/uaccess.h>
65 #include <asm/io.h>
66 #include <asm/unistd.h>
67
68 #ifndef SET_UNALIGN_CTL
69 # define SET_UNALIGN_CTL(a,b)   (-EINVAL)
70 #endif
71 #ifndef GET_UNALIGN_CTL
72 # define GET_UNALIGN_CTL(a,b)   (-EINVAL)
73 #endif
74 #ifndef SET_FPEMU_CTL
75 # define SET_FPEMU_CTL(a,b)     (-EINVAL)
76 #endif
77 #ifndef GET_FPEMU_CTL
78 # define GET_FPEMU_CTL(a,b)     (-EINVAL)
79 #endif
80 #ifndef SET_FPEXC_CTL
81 # define SET_FPEXC_CTL(a,b)     (-EINVAL)
82 #endif
83 #ifndef GET_FPEXC_CTL
84 # define GET_FPEXC_CTL(a,b)     (-EINVAL)
85 #endif
86 #ifndef GET_ENDIAN
87 # define GET_ENDIAN(a,b)        (-EINVAL)
88 #endif
89 #ifndef SET_ENDIAN
90 # define SET_ENDIAN(a,b)        (-EINVAL)
91 #endif
92 #ifndef GET_TSC_CTL
93 # define GET_TSC_CTL(a)         (-EINVAL)
94 #endif
95 #ifndef SET_TSC_CTL
96 # define SET_TSC_CTL(a)         (-EINVAL)
97 #endif
98
99 /*
100  * this is where the system-wide overflow UID and GID are defined, for
101  * architectures that now have 32-bit UID/GID but didn't in the past
102  */
103
104 int overflowuid = DEFAULT_OVERFLOWUID;
105 int overflowgid = DEFAULT_OVERFLOWGID;
106
107 EXPORT_SYMBOL(overflowuid);
108 EXPORT_SYMBOL(overflowgid);
109
110 /*
111  * the same as above, but for filesystems which can only store a 16-bit
112  * UID and GID. as such, this is needed on all architectures
113  */
114
115 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
116 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
117
118 EXPORT_SYMBOL(fs_overflowuid);
119 EXPORT_SYMBOL(fs_overflowgid);
120
121 /*
122  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
123  */
124
125 int C_A_D = 1;
126 struct pid *cad_pid;
127 EXPORT_SYMBOL(cad_pid);
128
129 /*
130  * If set, this is used for preparing the system to power off.
131  */
132
133 void (*pm_power_off_prepare)(void);
134
135 /*
136  * Returns true if current's euid is same as p's uid or euid,
137  * or has CAP_SYS_NICE to p's user_ns.
138  *
139  * Called with rcu_read_lock, creds are safe
140  */
141 static bool set_one_prio_perm(struct task_struct *p)
142 {
143         const struct cred *cred = current_cred(), *pcred = __task_cred(p);
144
145         if (uid_eq(pcred->uid,  cred->euid) ||
146             uid_eq(pcred->euid, cred->euid))
147                 return true;
148         if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
149                 return true;
150         return false;
151 }
152
153 /*
154  * set the priority of a task
155  * - the caller must hold the RCU read lock
156  */
157 static int set_one_prio(struct task_struct *p, int niceval, int error)
158 {
159         int no_nice;
160
161         if (!set_one_prio_perm(p)) {
162                 error = -EPERM;
163                 goto out;
164         }
165         if (niceval < task_nice(p) && !can_nice(p, niceval)) {
166                 error = -EACCES;
167                 goto out;
168         }
169         no_nice = security_task_setnice(p, niceval);
170         if (no_nice) {
171                 error = no_nice;
172                 goto out;
173         }
174         if (error == -ESRCH)
175                 error = 0;
176         set_user_nice(p, niceval);
177 out:
178         return error;
179 }
180
181 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
182 {
183         struct task_struct *g, *p;
184         struct user_struct *user;
185         const struct cred *cred = current_cred();
186         int error = -EINVAL;
187         struct pid *pgrp;
188         kuid_t uid;
189
190         if (which > PRIO_USER || which < PRIO_PROCESS)
191                 goto out;
192
193         /* normalize: avoid signed division (rounding problems) */
194         error = -ESRCH;
195         if (niceval < -20)
196                 niceval = -20;
197         if (niceval > 19)
198                 niceval = 19;
199
200         rcu_read_lock();
201         read_lock(&tasklist_lock);
202         switch (which) {
203                 case PRIO_PROCESS:
204                         if (who)
205                                 p = find_task_by_vpid(who);
206                         else
207                                 p = current;
208                         if (p)
209                                 error = set_one_prio(p, niceval, error);
210                         break;
211                 case PRIO_PGRP:
212                         if (who)
213                                 pgrp = find_vpid(who);
214                         else
215                                 pgrp = task_pgrp(current);
216                         do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
217                                 error = set_one_prio(p, niceval, error);
218                         } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
219                         break;
220                 case PRIO_USER:
221                         uid = make_kuid(cred->user_ns, who);
222                         user = cred->user;
223                         if (!who)
224                                 uid = cred->uid;
225                         else if (!uid_eq(uid, cred->uid) &&
226                                  !(user = find_user(uid)))
227                                 goto out_unlock;        /* No processes for this user */
228
229                         do_each_thread(g, p) {
230                                 if (uid_eq(task_uid(p), uid))
231                                         error = set_one_prio(p, niceval, error);
232                         } while_each_thread(g, p);
233                         if (!uid_eq(uid, cred->uid))
234                                 free_uid(user);         /* For find_user() */
235                         break;
236         }
237 out_unlock:
238         read_unlock(&tasklist_lock);
239         rcu_read_unlock();
240 out:
241         return error;
242 }
243
244 /*
245  * Ugh. To avoid negative return values, "getpriority()" will
246  * not return the normal nice-value, but a negated value that
247  * has been offset by 20 (ie it returns 40..1 instead of -20..19)
248  * to stay compatible.
249  */
250 SYSCALL_DEFINE2(getpriority, int, which, int, who)
251 {
252         struct task_struct *g, *p;
253         struct user_struct *user;
254         const struct cred *cred = current_cred();
255         long niceval, retval = -ESRCH;
256         struct pid *pgrp;
257         kuid_t uid;
258
259         if (which > PRIO_USER || which < PRIO_PROCESS)
260                 return -EINVAL;
261
262         rcu_read_lock();
263         read_lock(&tasklist_lock);
264         switch (which) {
265                 case PRIO_PROCESS:
266                         if (who)
267                                 p = find_task_by_vpid(who);
268                         else
269                                 p = current;
270                         if (p) {
271                                 niceval = 20 - task_nice(p);
272                                 if (niceval > retval)
273                                         retval = niceval;
274                         }
275                         break;
276                 case PRIO_PGRP:
277                         if (who)
278                                 pgrp = find_vpid(who);
279                         else
280                                 pgrp = task_pgrp(current);
281                         do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
282                                 niceval = 20 - task_nice(p);
283                                 if (niceval > retval)
284                                         retval = niceval;
285                         } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
286                         break;
287                 case PRIO_USER:
288                         uid = make_kuid(cred->user_ns, who);
289                         user = cred->user;
290                         if (!who)
291                                 uid = cred->uid;
292                         else if (!uid_eq(uid, cred->uid) &&
293                                  !(user = find_user(uid)))
294                                 goto out_unlock;        /* No processes for this user */
295
296                         do_each_thread(g, p) {
297                                 if (uid_eq(task_uid(p), uid)) {
298                                         niceval = 20 - task_nice(p);
299                                         if (niceval > retval)
300                                                 retval = niceval;
301                                 }
302                         } while_each_thread(g, p);
303                         if (!uid_eq(uid, 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 }
336
337 /**
338  *      register_reboot_notifier - Register function to be called at reboot time
339  *      @nb: Info about notifier function to be called
340  *
341  *      Registers a function with the list of functions
342  *      to be called at reboot time.
343  *
344  *      Currently always returns zero, as blocking_notifier_chain_register()
345  *      always returns zero.
346  */
347 int register_reboot_notifier(struct notifier_block *nb)
348 {
349         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
350 }
351 EXPORT_SYMBOL(register_reboot_notifier);
352
353 /**
354  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
355  *      @nb: Hook to be unregistered
356  *
357  *      Unregisters a previously registered reboot
358  *      notifier function.
359  *
360  *      Returns zero on success, or %-ENOENT on failure.
361  */
362 int unregister_reboot_notifier(struct notifier_block *nb)
363 {
364         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
365 }
366 EXPORT_SYMBOL(unregister_reboot_notifier);
367
368 /* Add backwards compatibility for stable trees. */
369 #ifndef PF_NO_SETAFFINITY
370 #define PF_NO_SETAFFINITY               PF_THREAD_BOUND
371 #endif
372
373 static void migrate_to_reboot_cpu(void)
374 {
375         /* The boot cpu is always logical cpu 0 */
376         int cpu = 0;
377
378         cpu_hotplug_disable();
379
380         /* Make certain the cpu I'm about to reboot on is online */
381         if (!cpu_online(cpu))
382                 cpu = cpumask_first(cpu_online_mask);
383
384         /* Prevent races with other tasks migrating this task */
385         current->flags |= PF_NO_SETAFFINITY;
386
387         /* Make certain I only run on the appropriate processor */
388         set_cpus_allowed_ptr(current, cpumask_of(cpu));
389 }
390
391 /**
392  *      kernel_restart - reboot the system
393  *      @cmd: pointer to buffer containing command to execute for restart
394  *              or %NULL
395  *
396  *      Shutdown everything and perform a clean reboot.
397  *      This is not safe to call in interrupt context.
398  */
399 void kernel_restart(char *cmd)
400 {
401         kernel_restart_prepare(cmd);
402         migrate_to_reboot_cpu();
403         syscore_shutdown();
404         if (!cmd)
405                 printk(KERN_EMERG "Restarting system.\n");
406         else
407                 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
408         kmsg_dump(KMSG_DUMP_RESTART);
409         machine_restart(cmd);
410 }
411 EXPORT_SYMBOL_GPL(kernel_restart);
412
413 static void kernel_shutdown_prepare(enum system_states state)
414 {
415         blocking_notifier_call_chain(&reboot_notifier_list,
416                 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
417         system_state = state;
418         usermodehelper_disable();
419         device_shutdown();
420 }
421 /**
422  *      kernel_halt - halt the system
423  *
424  *      Shutdown everything and perform a clean system halt.
425  */
426 void kernel_halt(void)
427 {
428         kernel_shutdown_prepare(SYSTEM_HALT);
429         migrate_to_reboot_cpu();
430         syscore_shutdown();
431         printk(KERN_EMERG "System halted.\n");
432         kmsg_dump(KMSG_DUMP_HALT);
433         machine_halt();
434 }
435
436 EXPORT_SYMBOL_GPL(kernel_halt);
437
438 /**
439  *      kernel_power_off - power_off the system
440  *
441  *      Shutdown everything and perform a clean system power_off.
442  */
443 void kernel_power_off(void)
444 {
445         kernel_shutdown_prepare(SYSTEM_POWER_OFF);
446         if (pm_power_off_prepare)
447                 pm_power_off_prepare();
448         migrate_to_reboot_cpu();
449         syscore_shutdown();
450         printk(KERN_EMERG "Power down.\n");
451         kmsg_dump(KMSG_DUMP_POWEROFF);
452         machine_power_off();
453 }
454 EXPORT_SYMBOL_GPL(kernel_power_off);
455
456 static DEFINE_MUTEX(reboot_mutex);
457
458 /*
459  * Reboot system call: for obvious reasons only root may call it,
460  * and even root needs to set up some magic numbers in the registers
461  * so that some mistake won't make this reboot the whole machine.
462  * You can also set the meaning of the ctrl-alt-del-key here.
463  *
464  * reboot doesn't sync: do that yourself before calling this.
465  */
466 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
467                 void __user *, arg)
468 {
469         struct pid_namespace *pid_ns = task_active_pid_ns(current);
470         char buffer[256];
471         int ret = 0;
472
473         /* We only trust the superuser with rebooting the system. */
474         if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
475                 return -EPERM;
476
477         /* For safety, we require "magic" arguments. */
478         if (magic1 != LINUX_REBOOT_MAGIC1 ||
479             (magic2 != LINUX_REBOOT_MAGIC2 &&
480                         magic2 != LINUX_REBOOT_MAGIC2A &&
481                         magic2 != LINUX_REBOOT_MAGIC2B &&
482                         magic2 != LINUX_REBOOT_MAGIC2C))
483                 return -EINVAL;
484
485         /*
486          * If pid namespaces are enabled and the current task is in a child
487          * pid_namespace, the command is handled by reboot_pid_ns() which will
488          * call do_exit().
489          */
490         ret = reboot_pid_ns(pid_ns, cmd);
491         if (ret)
492                 return ret;
493
494         /* Instead of trying to make the power_off code look like
495          * halt when pm_power_off is not set do it the easy way.
496          */
497         if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
498                 cmd = LINUX_REBOOT_CMD_HALT;
499
500         mutex_lock(&reboot_mutex);
501         switch (cmd) {
502         case LINUX_REBOOT_CMD_RESTART:
503                 kernel_restart(NULL);
504                 break;
505
506         case LINUX_REBOOT_CMD_CAD_ON:
507                 C_A_D = 1;
508                 break;
509
510         case LINUX_REBOOT_CMD_CAD_OFF:
511                 C_A_D = 0;
512                 break;
513
514         case LINUX_REBOOT_CMD_HALT:
515                 kernel_halt();
516                 do_exit(0);
517                 panic("cannot halt");
518
519         case LINUX_REBOOT_CMD_POWER_OFF:
520                 kernel_power_off();
521                 do_exit(0);
522                 break;
523
524         case LINUX_REBOOT_CMD_RESTART2:
525                 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
526                         ret = -EFAULT;
527                         break;
528                 }
529                 buffer[sizeof(buffer) - 1] = '\0';
530
531                 kernel_restart(buffer);
532                 break;
533
534 #ifdef CONFIG_KEXEC
535         case LINUX_REBOOT_CMD_KEXEC:
536                 ret = kernel_kexec();
537                 break;
538 #endif
539
540 #ifdef CONFIG_HIBERNATION
541         case LINUX_REBOOT_CMD_SW_SUSPEND:
542                 ret = hibernate();
543                 break;
544 #endif
545
546         default:
547                 ret = -EINVAL;
548                 break;
549         }
550         mutex_unlock(&reboot_mutex);
551         return ret;
552 }
553
554 static void deferred_cad(struct work_struct *dummy)
555 {
556         kernel_restart(NULL);
557 }
558
559 /*
560  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
561  * As it's called within an interrupt, it may NOT sync: the only choice
562  * is whether to reboot at once, or just ignore the ctrl-alt-del.
563  */
564 void ctrl_alt_del(void)
565 {
566         static DECLARE_WORK(cad_work, deferred_cad);
567
568         if (C_A_D)
569                 schedule_work(&cad_work);
570         else
571                 kill_cad_pid(SIGINT, 1);
572 }
573         
574 /*
575  * Unprivileged users may change the real gid to the effective gid
576  * or vice versa.  (BSD-style)
577  *
578  * If you set the real gid at all, or set the effective gid to a value not
579  * equal to the real gid, then the saved gid is set to the new effective gid.
580  *
581  * This makes it possible for a setgid program to completely drop its
582  * privileges, which is often a useful assertion to make when you are doing
583  * a security audit over a program.
584  *
585  * The general idea is that a program which uses just setregid() will be
586  * 100% compatible with BSD.  A program which uses just setgid() will be
587  * 100% compatible with POSIX with saved IDs. 
588  *
589  * SMP: There are not races, the GIDs are checked only by filesystem
590  *      operations (as far as semantic preservation is concerned).
591  */
592 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
593 {
594         struct user_namespace *ns = current_user_ns();
595         const struct cred *old;
596         struct cred *new;
597         int retval;
598         kgid_t krgid, kegid;
599
600         krgid = make_kgid(ns, rgid);
601         kegid = make_kgid(ns, egid);
602
603         if ((rgid != (gid_t) -1) && !gid_valid(krgid))
604                 return -EINVAL;
605         if ((egid != (gid_t) -1) && !gid_valid(kegid))
606                 return -EINVAL;
607
608         new = prepare_creds();
609         if (!new)
610                 return -ENOMEM;
611         old = current_cred();
612
613         retval = -EPERM;
614         if (rgid != (gid_t) -1) {
615                 if (gid_eq(old->gid, krgid) ||
616                     gid_eq(old->egid, krgid) ||
617                     nsown_capable(CAP_SETGID))
618                         new->gid = krgid;
619                 else
620                         goto error;
621         }
622         if (egid != (gid_t) -1) {
623                 if (gid_eq(old->gid, kegid) ||
624                     gid_eq(old->egid, kegid) ||
625                     gid_eq(old->sgid, kegid) ||
626                     nsown_capable(CAP_SETGID))
627                         new->egid = kegid;
628                 else
629                         goto error;
630         }
631
632         if (rgid != (gid_t) -1 ||
633             (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
634                 new->sgid = new->egid;
635         new->fsgid = new->egid;
636
637         return commit_creds(new);
638
639 error:
640         abort_creds(new);
641         return retval;
642 }
643
644 /*
645  * setgid() is implemented like SysV w/ SAVED_IDS 
646  *
647  * SMP: Same implicit races as above.
648  */
649 SYSCALL_DEFINE1(setgid, gid_t, gid)
650 {
651         struct user_namespace *ns = current_user_ns();
652         const struct cred *old;
653         struct cred *new;
654         int retval;
655         kgid_t kgid;
656
657         kgid = make_kgid(ns, gid);
658         if (!gid_valid(kgid))
659                 return -EINVAL;
660
661         new = prepare_creds();
662         if (!new)
663                 return -ENOMEM;
664         old = current_cred();
665
666         retval = -EPERM;
667         if (nsown_capable(CAP_SETGID))
668                 new->gid = new->egid = new->sgid = new->fsgid = kgid;
669         else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
670                 new->egid = new->fsgid = kgid;
671         else
672                 goto error;
673
674         return commit_creds(new);
675
676 error:
677         abort_creds(new);
678         return retval;
679 }
680
681 /*
682  * change the user struct in a credentials set to match the new UID
683  */
684 static int set_user(struct cred *new)
685 {
686         struct user_struct *new_user;
687
688         new_user = alloc_uid(new->uid);
689         if (!new_user)
690                 return -EAGAIN;
691
692         /*
693          * We don't fail in case of NPROC limit excess here because too many
694          * poorly written programs don't check set*uid() return code, assuming
695          * it never fails if called by root.  We may still enforce NPROC limit
696          * for programs doing set*uid()+execve() by harmlessly deferring the
697          * failure to the execve() stage.
698          */
699         if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
700                         new_user != INIT_USER)
701                 current->flags |= PF_NPROC_EXCEEDED;
702         else
703                 current->flags &= ~PF_NPROC_EXCEEDED;
704
705         free_uid(new->user);
706         new->user = new_user;
707         return 0;
708 }
709
710 /*
711  * Unprivileged users may change the real uid to the effective uid
712  * or vice versa.  (BSD-style)
713  *
714  * If you set the real uid at all, or set the effective uid to a value not
715  * equal to the real uid, then the saved uid is set to the new effective uid.
716  *
717  * This makes it possible for a setuid program to completely drop its
718  * privileges, which is often a useful assertion to make when you are doing
719  * a security audit over a program.
720  *
721  * The general idea is that a program which uses just setreuid() will be
722  * 100% compatible with BSD.  A program which uses just setuid() will be
723  * 100% compatible with POSIX with saved IDs. 
724  */
725 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
726 {
727         struct user_namespace *ns = current_user_ns();
728         const struct cred *old;
729         struct cred *new;
730         int retval;
731         kuid_t kruid, keuid;
732
733         kruid = make_kuid(ns, ruid);
734         keuid = make_kuid(ns, euid);
735
736         if ((ruid != (uid_t) -1) && !uid_valid(kruid))
737                 return -EINVAL;
738         if ((euid != (uid_t) -1) && !uid_valid(keuid))
739                 return -EINVAL;
740
741         new = prepare_creds();
742         if (!new)
743                 return -ENOMEM;
744         old = current_cred();
745
746         retval = -EPERM;
747         if (ruid != (uid_t) -1) {
748                 new->uid = kruid;
749                 if (!uid_eq(old->uid, kruid) &&
750                     !uid_eq(old->euid, kruid) &&
751                     !nsown_capable(CAP_SETUID))
752                         goto error;
753         }
754
755         if (euid != (uid_t) -1) {
756                 new->euid = keuid;
757                 if (!uid_eq(old->uid, keuid) &&
758                     !uid_eq(old->euid, keuid) &&
759                     !uid_eq(old->suid, keuid) &&
760                     !nsown_capable(CAP_SETUID))
761                         goto error;
762         }
763
764         if (!uid_eq(new->uid, old->uid)) {
765                 retval = set_user(new);
766                 if (retval < 0)
767                         goto error;
768         }
769         if (ruid != (uid_t) -1 ||
770             (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
771                 new->suid = new->euid;
772         new->fsuid = new->euid;
773
774         retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
775         if (retval < 0)
776                 goto error;
777
778         return commit_creds(new);
779
780 error:
781         abort_creds(new);
782         return retval;
783 }
784                 
785 /*
786  * setuid() is implemented like SysV with SAVED_IDS 
787  * 
788  * Note that SAVED_ID's is deficient in that a setuid root program
789  * like sendmail, for example, cannot set its uid to be a normal 
790  * user and then switch back, because if you're root, setuid() sets
791  * the saved uid too.  If you don't like this, blame the bright people
792  * in the POSIX committee and/or USG.  Note that the BSD-style setreuid()
793  * will allow a root program to temporarily drop privileges and be able to
794  * regain them by swapping the real and effective uid.  
795  */
796 SYSCALL_DEFINE1(setuid, uid_t, uid)
797 {
798         struct user_namespace *ns = current_user_ns();
799         const struct cred *old;
800         struct cred *new;
801         int retval;
802         kuid_t kuid;
803
804         kuid = make_kuid(ns, uid);
805         if (!uid_valid(kuid))
806                 return -EINVAL;
807
808         new = prepare_creds();
809         if (!new)
810                 return -ENOMEM;
811         old = current_cred();
812
813         retval = -EPERM;
814         if (nsown_capable(CAP_SETUID)) {
815                 new->suid = new->uid = kuid;
816                 if (!uid_eq(kuid, old->uid)) {
817                         retval = set_user(new);
818                         if (retval < 0)
819                                 goto error;
820                 }
821         } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
822                 goto error;
823         }
824
825         new->fsuid = new->euid = kuid;
826
827         retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
828         if (retval < 0)
829                 goto error;
830
831         return commit_creds(new);
832
833 error:
834         abort_creds(new);
835         return retval;
836 }
837
838
839 /*
840  * This function implements a generic ability to update ruid, euid,
841  * and suid.  This allows you to implement the 4.4 compatible seteuid().
842  */
843 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
844 {
845         struct user_namespace *ns = current_user_ns();
846         const struct cred *old;
847         struct cred *new;
848         int retval;
849         kuid_t kruid, keuid, ksuid;
850
851         kruid = make_kuid(ns, ruid);
852         keuid = make_kuid(ns, euid);
853         ksuid = make_kuid(ns, suid);
854
855         if ((ruid != (uid_t) -1) && !uid_valid(kruid))
856                 return -EINVAL;
857
858         if ((euid != (uid_t) -1) && !uid_valid(keuid))
859                 return -EINVAL;
860
861         if ((suid != (uid_t) -1) && !uid_valid(ksuid))
862                 return -EINVAL;
863
864         new = prepare_creds();
865         if (!new)
866                 return -ENOMEM;
867
868         old = current_cred();
869
870         retval = -EPERM;
871         if (!nsown_capable(CAP_SETUID)) {
872                 if (ruid != (uid_t) -1        && !uid_eq(kruid, old->uid) &&
873                     !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
874                         goto error;
875                 if (euid != (uid_t) -1        && !uid_eq(keuid, old->uid) &&
876                     !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
877                         goto error;
878                 if (suid != (uid_t) -1        && !uid_eq(ksuid, old->uid) &&
879                     !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
880                         goto error;
881         }
882
883         if (ruid != (uid_t) -1) {
884                 new->uid = kruid;
885                 if (!uid_eq(kruid, old->uid)) {
886                         retval = set_user(new);
887                         if (retval < 0)
888                                 goto error;
889                 }
890         }
891         if (euid != (uid_t) -1)
892                 new->euid = keuid;
893         if (suid != (uid_t) -1)
894                 new->suid = ksuid;
895         new->fsuid = new->euid;
896
897         retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
898         if (retval < 0)
899                 goto error;
900
901         return commit_creds(new);
902
903 error:
904         abort_creds(new);
905         return retval;
906 }
907
908 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
909 {
910         const struct cred *cred = current_cred();
911         int retval;
912         uid_t ruid, euid, suid;
913
914         ruid = from_kuid_munged(cred->user_ns, cred->uid);
915         euid = from_kuid_munged(cred->user_ns, cred->euid);
916         suid = from_kuid_munged(cred->user_ns, cred->suid);
917
918         if (!(retval   = put_user(ruid, ruidp)) &&
919             !(retval   = put_user(euid, euidp)))
920                 retval = put_user(suid, suidp);
921
922         return retval;
923 }
924
925 /*
926  * Same as above, but for rgid, egid, sgid.
927  */
928 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
929 {
930         struct user_namespace *ns = current_user_ns();
931         const struct cred *old;
932         struct cred *new;
933         int retval;
934         kgid_t krgid, kegid, ksgid;
935
936         krgid = make_kgid(ns, rgid);
937         kegid = make_kgid(ns, egid);
938         ksgid = make_kgid(ns, sgid);
939
940         if ((rgid != (gid_t) -1) && !gid_valid(krgid))
941                 return -EINVAL;
942         if ((egid != (gid_t) -1) && !gid_valid(kegid))
943                 return -EINVAL;
944         if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
945                 return -EINVAL;
946
947         new = prepare_creds();
948         if (!new)
949                 return -ENOMEM;
950         old = current_cred();
951
952         retval = -EPERM;
953         if (!nsown_capable(CAP_SETGID)) {
954                 if (rgid != (gid_t) -1        && !gid_eq(krgid, old->gid) &&
955                     !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
956                         goto error;
957                 if (egid != (gid_t) -1        && !gid_eq(kegid, old->gid) &&
958                     !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
959                         goto error;
960                 if (sgid != (gid_t) -1        && !gid_eq(ksgid, old->gid) &&
961                     !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
962                         goto error;
963         }
964
965         if (rgid != (gid_t) -1)
966                 new->gid = krgid;
967         if (egid != (gid_t) -1)
968                 new->egid = kegid;
969         if (sgid != (gid_t) -1)
970                 new->sgid = ksgid;
971         new->fsgid = new->egid;
972
973         return commit_creds(new);
974
975 error:
976         abort_creds(new);
977         return retval;
978 }
979
980 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
981 {
982         const struct cred *cred = current_cred();
983         int retval;
984         gid_t rgid, egid, sgid;
985
986         rgid = from_kgid_munged(cred->user_ns, cred->gid);
987         egid = from_kgid_munged(cred->user_ns, cred->egid);
988         sgid = from_kgid_munged(cred->user_ns, cred->sgid);
989
990         if (!(retval   = put_user(rgid, rgidp)) &&
991             !(retval   = put_user(egid, egidp)))
992                 retval = put_user(sgid, sgidp);
993
994         return retval;
995 }
996
997
998 /*
999  * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1000  * is used for "access()" and for the NFS daemon (letting nfsd stay at
1001  * whatever uid it wants to). It normally shadows "euid", except when
1002  * explicitly set by setfsuid() or for access..
1003  */
1004 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
1005 {
1006         const struct cred *old;
1007         struct cred *new;
1008         uid_t old_fsuid;
1009         kuid_t kuid;
1010
1011         old = current_cred();
1012         old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
1013
1014         kuid = make_kuid(old->user_ns, uid);
1015         if (!uid_valid(kuid))
1016                 return old_fsuid;
1017
1018         new = prepare_creds();
1019         if (!new)
1020                 return old_fsuid;
1021
1022         if (uid_eq(kuid, old->uid)  || uid_eq(kuid, old->euid)  ||
1023             uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
1024             nsown_capable(CAP_SETUID)) {
1025                 if (!uid_eq(kuid, old->fsuid)) {
1026                         new->fsuid = kuid;
1027                         if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
1028                                 goto change_okay;
1029                 }
1030         }
1031
1032         abort_creds(new);
1033         return old_fsuid;
1034
1035 change_okay:
1036         commit_creds(new);
1037         return old_fsuid;
1038 }
1039
1040 /*
1041  * Samma pÃ¥ svenska..
1042  */
1043 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
1044 {
1045         const struct cred *old;
1046         struct cred *new;
1047         gid_t old_fsgid;
1048         kgid_t kgid;
1049
1050         old = current_cred();
1051         old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
1052
1053         kgid = make_kgid(old->user_ns, gid);
1054         if (!gid_valid(kgid))
1055                 return old_fsgid;
1056
1057         new = prepare_creds();
1058         if (!new)
1059                 return old_fsgid;
1060
1061         if (gid_eq(kgid, old->gid)  || gid_eq(kgid, old->egid)  ||
1062             gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
1063             nsown_capable(CAP_SETGID)) {
1064                 if (!gid_eq(kgid, old->fsgid)) {
1065                         new->fsgid = kgid;
1066                         goto change_okay;
1067                 }
1068         }
1069
1070         abort_creds(new);
1071         return old_fsgid;
1072
1073 change_okay:
1074         commit_creds(new);
1075         return old_fsgid;
1076 }
1077
1078 /**
1079  * sys_getpid - return the thread group id of the current process
1080  *
1081  * Note, despite the name, this returns the tgid not the pid.  The tgid and
1082  * the pid are identical unless CLONE_THREAD was specified on clone() in
1083  * which case the tgid is the same in all threads of the same group.
1084  *
1085  * This is SMP safe as current->tgid does not change.
1086  */
1087 SYSCALL_DEFINE0(getpid)
1088 {
1089         return task_tgid_vnr(current);
1090 }
1091
1092 /* Thread ID - the internal kernel "pid" */
1093 SYSCALL_DEFINE0(gettid)
1094 {
1095         return task_pid_vnr(current);
1096 }
1097
1098 /*
1099  * Accessing ->real_parent is not SMP-safe, it could
1100  * change from under us. However, we can use a stale
1101  * value of ->real_parent under rcu_read_lock(), see
1102  * release_task()->call_rcu(delayed_put_task_struct).
1103  */
1104 SYSCALL_DEFINE0(getppid)
1105 {
1106         int pid;
1107
1108         rcu_read_lock();
1109         pid = task_tgid_vnr(rcu_dereference(current->real_parent));
1110         rcu_read_unlock();
1111
1112         return pid;
1113 }
1114
1115 SYSCALL_DEFINE0(getuid)
1116 {
1117         /* Only we change this so SMP safe */
1118         return from_kuid_munged(current_user_ns(), current_uid());
1119 }
1120
1121 SYSCALL_DEFINE0(geteuid)
1122 {
1123         /* Only we change this so SMP safe */
1124         return from_kuid_munged(current_user_ns(), current_euid());
1125 }
1126
1127 SYSCALL_DEFINE0(getgid)
1128 {
1129         /* Only we change this so SMP safe */
1130         return from_kgid_munged(current_user_ns(), current_gid());
1131 }
1132
1133 SYSCALL_DEFINE0(getegid)
1134 {
1135         /* Only we change this so SMP safe */
1136         return from_kgid_munged(current_user_ns(), current_egid());
1137 }
1138
1139 void do_sys_times(struct tms *tms)
1140 {
1141         cputime_t tgutime, tgstime, cutime, cstime;
1142
1143         spin_lock_irq(&current->sighand->siglock);
1144         thread_group_cputime_adjusted(current, &tgutime, &tgstime);
1145         cutime = current->signal->cutime;
1146         cstime = current->signal->cstime;
1147         spin_unlock_irq(&current->sighand->siglock);
1148         tms->tms_utime = cputime_to_clock_t(tgutime);
1149         tms->tms_stime = cputime_to_clock_t(tgstime);
1150         tms->tms_cutime = cputime_to_clock_t(cutime);
1151         tms->tms_cstime = cputime_to_clock_t(cstime);
1152 }
1153
1154 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
1155 {
1156         if (tbuf) {
1157                 struct tms tmp;
1158
1159                 do_sys_times(&tmp);
1160                 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1161                         return -EFAULT;
1162         }
1163         force_successful_syscall_return();
1164         return (long) jiffies_64_to_clock_t(get_jiffies_64());
1165 }
1166
1167 /*
1168  * This needs some heavy checking ...
1169  * I just haven't the stomach for it. I also don't fully
1170  * understand sessions/pgrp etc. Let somebody who does explain it.
1171  *
1172  * OK, I think I have the protection semantics right.... this is really
1173  * only important on a multi-user system anyway, to make sure one user
1174  * can't send a signal to a process owned by another.  -TYT, 12/12/91
1175  *
1176  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1177  * LBT 04.03.94
1178  */
1179 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1180 {
1181         struct task_struct *p;
1182         struct task_struct *group_leader = current->group_leader;
1183         struct pid *pgrp;
1184         int err;
1185
1186         if (!pid)
1187                 pid = task_pid_vnr(group_leader);
1188         if (!pgid)
1189                 pgid = pid;
1190         if (pgid < 0)
1191                 return -EINVAL;
1192         rcu_read_lock();
1193
1194         /* From this point forward we keep holding onto the tasklist lock
1195          * so that our parent does not change from under us. -DaveM
1196          */
1197         write_lock_irq(&tasklist_lock);
1198
1199         err = -ESRCH;
1200         p = find_task_by_vpid(pid);
1201         if (!p)
1202                 goto out;
1203
1204         err = -EINVAL;
1205         if (!thread_group_leader(p))
1206                 goto out;
1207
1208         if (same_thread_group(p->real_parent, group_leader)) {
1209                 err = -EPERM;
1210                 if (task_session(p) != task_session(group_leader))
1211                         goto out;
1212                 err = -EACCES;
1213                 if (p->did_exec)
1214                         goto out;
1215         } else {
1216                 err = -ESRCH;
1217                 if (p != group_leader)
1218                         goto out;
1219         }
1220
1221         err = -EPERM;
1222         if (p->signal->leader)
1223                 goto out;
1224
1225         pgrp = task_pid(p);
1226         if (pgid != pid) {
1227                 struct task_struct *g;
1228
1229                 pgrp = find_vpid(pgid);
1230                 g = pid_task(pgrp, PIDTYPE_PGID);
1231                 if (!g || task_session(g) != task_session(group_leader))
1232                         goto out;
1233         }
1234
1235         err = security_task_setpgid(p, pgid);
1236         if (err)
1237                 goto out;
1238
1239         if (task_pgrp(p) != pgrp)
1240                 change_pid(p, PIDTYPE_PGID, pgrp);
1241
1242         err = 0;
1243 out:
1244         /* All paths lead to here, thus we are safe. -DaveM */
1245         write_unlock_irq(&tasklist_lock);
1246         rcu_read_unlock();
1247         return err;
1248 }
1249
1250 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1251 {
1252         struct task_struct *p;
1253         struct pid *grp;
1254         int retval;
1255
1256         rcu_read_lock();
1257         if (!pid)
1258                 grp = task_pgrp(current);
1259         else {
1260                 retval = -ESRCH;
1261                 p = find_task_by_vpid(pid);
1262                 if (!p)
1263                         goto out;
1264                 grp = task_pgrp(p);
1265                 if (!grp)
1266                         goto out;
1267
1268                 retval = security_task_getpgid(p);
1269                 if (retval)
1270                         goto out;
1271         }
1272         retval = pid_vnr(grp);
1273 out:
1274         rcu_read_unlock();
1275         return retval;
1276 }
1277
1278 #ifdef __ARCH_WANT_SYS_GETPGRP
1279
1280 SYSCALL_DEFINE0(getpgrp)
1281 {
1282         return sys_getpgid(0);
1283 }
1284
1285 #endif
1286
1287 SYSCALL_DEFINE1(getsid, pid_t, pid)
1288 {
1289         struct task_struct *p;
1290         struct pid *sid;
1291         int retval;
1292
1293         rcu_read_lock();
1294         if (!pid)
1295                 sid = task_session(current);
1296         else {
1297                 retval = -ESRCH;
1298                 p = find_task_by_vpid(pid);
1299                 if (!p)
1300                         goto out;
1301                 sid = task_session(p);
1302                 if (!sid)
1303                         goto out;
1304
1305                 retval = security_task_getsid(p);
1306                 if (retval)
1307                         goto out;
1308         }
1309         retval = pid_vnr(sid);
1310 out:
1311         rcu_read_unlock();
1312         return retval;
1313 }
1314
1315 SYSCALL_DEFINE0(setsid)
1316 {
1317         struct task_struct *group_leader = current->group_leader;
1318         struct pid *sid = task_pid(group_leader);
1319         pid_t session = pid_vnr(sid);
1320         int err = -EPERM;
1321
1322         write_lock_irq(&tasklist_lock);
1323         /* Fail if I am already a session leader */
1324         if (group_leader->signal->leader)
1325                 goto out;
1326
1327         /* Fail if a process group id already exists that equals the
1328          * proposed session id.
1329          */
1330         if (pid_task(sid, PIDTYPE_PGID))
1331                 goto out;
1332
1333         group_leader->signal->leader = 1;
1334         __set_special_pids(sid);
1335
1336         proc_clear_tty(group_leader);
1337
1338         err = session;
1339 out:
1340         write_unlock_irq(&tasklist_lock);
1341         if (err > 0) {
1342                 proc_sid_connector(group_leader);
1343                 sched_autogroup_create_attach(group_leader);
1344         }
1345         return err;
1346 }
1347
1348 DECLARE_RWSEM(uts_sem);
1349
1350 #ifdef COMPAT_UTS_MACHINE
1351 #define override_architecture(name) \
1352         (personality(current->personality) == PER_LINUX32 && \
1353          copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1354                       sizeof(COMPAT_UTS_MACHINE)))
1355 #else
1356 #define override_architecture(name)     0
1357 #endif
1358
1359 /*
1360  * Work around broken programs that cannot handle "Linux 3.0".
1361  * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1362  */
1363 static int override_release(char __user *release, size_t len)
1364 {
1365         int ret = 0;
1366
1367         if (current->personality & UNAME26) {
1368                 const char *rest = UTS_RELEASE;
1369                 char buf[65] = { 0 };
1370                 int ndots = 0;
1371                 unsigned v;
1372                 size_t copy;
1373
1374                 while (*rest) {
1375                         if (*rest == '.' && ++ndots >= 3)
1376                                 break;
1377                         if (!isdigit(*rest) && *rest != '.')
1378                                 break;
1379                         rest++;
1380                 }
1381                 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
1382                 copy = clamp_t(size_t, len, 1, sizeof(buf));
1383                 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1384                 ret = copy_to_user(release, buf, copy + 1);
1385         }
1386         return ret;
1387 }
1388
1389 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1390 {
1391         int errno = 0;
1392
1393         down_read(&uts_sem);
1394         if (copy_to_user(name, utsname(), sizeof *name))
1395                 errno = -EFAULT;
1396         up_read(&uts_sem);
1397
1398         if (!errno && override_release(name->release, sizeof(name->release)))
1399                 errno = -EFAULT;
1400         if (!errno && override_architecture(name))
1401                 errno = -EFAULT;
1402         return errno;
1403 }
1404
1405 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1406 /*
1407  * Old cruft
1408  */
1409 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1410 {
1411         int error = 0;
1412
1413         if (!name)
1414                 return -EFAULT;
1415
1416         down_read(&uts_sem);
1417         if (copy_to_user(name, utsname(), sizeof(*name)))
1418                 error = -EFAULT;
1419         up_read(&uts_sem);
1420
1421         if (!error && override_release(name->release, sizeof(name->release)))
1422                 error = -EFAULT;
1423         if (!error && override_architecture(name))
1424                 error = -EFAULT;
1425         return error;
1426 }
1427
1428 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1429 {
1430         int error;
1431
1432         if (!name)
1433                 return -EFAULT;
1434         if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1435                 return -EFAULT;
1436
1437         down_read(&uts_sem);
1438         error = __copy_to_user(&name->sysname, &utsname()->sysname,
1439                                __OLD_UTS_LEN);
1440         error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1441         error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1442                                 __OLD_UTS_LEN);
1443         error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1444         error |= __copy_to_user(&name->release, &utsname()->release,
1445                                 __OLD_UTS_LEN);
1446         error |= __put_user(0, name->release + __OLD_UTS_LEN);
1447         error |= __copy_to_user(&name->version, &utsname()->version,
1448                                 __OLD_UTS_LEN);
1449         error |= __put_user(0, name->version + __OLD_UTS_LEN);
1450         error |= __copy_to_user(&name->machine, &utsname()->machine,
1451                                 __OLD_UTS_LEN);
1452         error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1453         up_read(&uts_sem);
1454
1455         if (!error && override_architecture(name))
1456                 error = -EFAULT;
1457         if (!error && override_release(name->release, sizeof(name->release)))
1458                 error = -EFAULT;
1459         return error ? -EFAULT : 0;
1460 }
1461 #endif
1462
1463 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1464 {
1465         int errno;
1466         char tmp[__NEW_UTS_LEN];
1467
1468         if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1469                 return -EPERM;
1470
1471         if (len < 0 || len > __NEW_UTS_LEN)
1472                 return -EINVAL;
1473         down_write(&uts_sem);
1474         errno = -EFAULT;
1475         if (!copy_from_user(tmp, name, len)) {
1476                 struct new_utsname *u = utsname();
1477
1478                 memcpy(u->nodename, tmp, len);
1479                 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1480                 errno = 0;
1481                 uts_proc_notify(UTS_PROC_HOSTNAME);
1482         }
1483         up_write(&uts_sem);
1484         return errno;
1485 }
1486
1487 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1488
1489 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1490 {
1491         int i, errno;
1492         struct new_utsname *u;
1493
1494         if (len < 0)
1495                 return -EINVAL;
1496         down_read(&uts_sem);
1497         u = utsname();
1498         i = 1 + strlen(u->nodename);
1499         if (i > len)
1500                 i = len;
1501         errno = 0;
1502         if (copy_to_user(name, u->nodename, i))
1503                 errno = -EFAULT;
1504         up_read(&uts_sem);
1505         return errno;
1506 }
1507
1508 #endif
1509
1510 /*
1511  * Only setdomainname; getdomainname can be implemented by calling
1512  * uname()
1513  */
1514 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1515 {
1516         int errno;
1517         char tmp[__NEW_UTS_LEN];
1518
1519         if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1520                 return -EPERM;
1521         if (len < 0 || len > __NEW_UTS_LEN)
1522                 return -EINVAL;
1523
1524         down_write(&uts_sem);
1525         errno = -EFAULT;
1526         if (!copy_from_user(tmp, name, len)) {
1527                 struct new_utsname *u = utsname();
1528
1529                 memcpy(u->domainname, tmp, len);
1530                 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1531                 errno = 0;
1532                 uts_proc_notify(UTS_PROC_DOMAINNAME);
1533         }
1534         up_write(&uts_sem);
1535         return errno;
1536 }
1537
1538 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1539 {
1540         struct rlimit value;
1541         int ret;
1542
1543         ret = do_prlimit(current, resource, NULL, &value);
1544         if (!ret)
1545                 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1546
1547         return ret;
1548 }
1549
1550 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1551
1552 /*
1553  *      Back compatibility for getrlimit. Needed for some apps.
1554  */
1555  
1556 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1557                 struct rlimit __user *, rlim)
1558 {
1559         struct rlimit x;
1560         if (resource >= RLIM_NLIMITS)
1561                 return -EINVAL;
1562
1563         task_lock(current->group_leader);
1564         x = current->signal->rlim[resource];
1565         task_unlock(current->group_leader);
1566         if (x.rlim_cur > 0x7FFFFFFF)
1567                 x.rlim_cur = 0x7FFFFFFF;
1568         if (x.rlim_max > 0x7FFFFFFF)
1569                 x.rlim_max = 0x7FFFFFFF;
1570         return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1571 }
1572
1573 #endif
1574
1575 static inline bool rlim64_is_infinity(__u64 rlim64)
1576 {
1577 #if BITS_PER_LONG < 64
1578         return rlim64 >= ULONG_MAX;
1579 #else
1580         return rlim64 == RLIM64_INFINITY;
1581 #endif
1582 }
1583
1584 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1585 {
1586         if (rlim->rlim_cur == RLIM_INFINITY)
1587                 rlim64->rlim_cur = RLIM64_INFINITY;
1588         else
1589                 rlim64->rlim_cur = rlim->rlim_cur;
1590         if (rlim->rlim_max == RLIM_INFINITY)
1591                 rlim64->rlim_max = RLIM64_INFINITY;
1592         else
1593                 rlim64->rlim_max = rlim->rlim_max;
1594 }
1595
1596 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1597 {
1598         if (rlim64_is_infinity(rlim64->rlim_cur))
1599                 rlim->rlim_cur = RLIM_INFINITY;
1600         else
1601                 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1602         if (rlim64_is_infinity(rlim64->rlim_max))
1603                 rlim->rlim_max = RLIM_INFINITY;
1604         else
1605                 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1606 }
1607
1608 /* make sure you are allowed to change @tsk limits before calling this */
1609 int do_prlimit(struct task_struct *tsk, unsigned int resource,
1610                 struct rlimit *new_rlim, struct rlimit *old_rlim)
1611 {
1612         struct rlimit *rlim;
1613         int retval = 0;
1614
1615         if (resource >= RLIM_NLIMITS)
1616                 return -EINVAL;
1617         if (new_rlim) {
1618                 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1619                         return -EINVAL;
1620                 if (resource == RLIMIT_NOFILE &&
1621                                 new_rlim->rlim_max > sysctl_nr_open)
1622                         return -EPERM;
1623         }
1624
1625         /* protect tsk->signal and tsk->sighand from disappearing */
1626         read_lock(&tasklist_lock);
1627         if (!tsk->sighand) {
1628                 retval = -ESRCH;
1629                 goto out;
1630         }
1631
1632         rlim = tsk->signal->rlim + resource;
1633         task_lock(tsk->group_leader);
1634         if (new_rlim) {
1635                 /* Keep the capable check against init_user_ns until
1636                    cgroups can contain all limits */
1637                 if (new_rlim->rlim_max > rlim->rlim_max &&
1638                                 !capable(CAP_SYS_RESOURCE))
1639                         retval = -EPERM;
1640                 if (!retval)
1641                         retval = security_task_setrlimit(tsk->group_leader,
1642                                         resource, new_rlim);
1643                 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1644                         /*
1645                          * The caller is asking for an immediate RLIMIT_CPU
1646                          * expiry.  But we use the zero value to mean "it was
1647                          * never set".  So let's cheat and make it one second
1648                          * instead
1649                          */
1650                         new_rlim->rlim_cur = 1;
1651                 }
1652         }
1653         if (!retval) {
1654                 if (old_rlim)
1655                         *old_rlim = *rlim;
1656                 if (new_rlim)
1657                         *rlim = *new_rlim;
1658         }
1659         task_unlock(tsk->group_leader);
1660
1661         /*
1662          * RLIMIT_CPU handling.   Note that the kernel fails to return an error
1663          * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a
1664          * very long-standing error, and fixing it now risks breakage of
1665          * applications, so we live with it
1666          */
1667          if (!retval && new_rlim && resource == RLIMIT_CPU &&
1668                          new_rlim->rlim_cur != RLIM_INFINITY)
1669                 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
1670 out:
1671         read_unlock(&tasklist_lock);
1672         return retval;
1673 }
1674
1675 /* rcu lock must be held */
1676 static int check_prlimit_permission(struct task_struct *task)
1677 {
1678         const struct cred *cred = current_cred(), *tcred;
1679
1680         if (current == task)
1681                 return 0;
1682
1683         tcred = __task_cred(task);
1684         if (uid_eq(cred->uid, tcred->euid) &&
1685             uid_eq(cred->uid, tcred->suid) &&
1686             uid_eq(cred->uid, tcred->uid)  &&
1687             gid_eq(cred->gid, tcred->egid) &&
1688             gid_eq(cred->gid, tcred->sgid) &&
1689             gid_eq(cred->gid, tcred->gid))
1690                 return 0;
1691         if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1692                 return 0;
1693
1694         return -EPERM;
1695 }
1696
1697 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1698                 const struct rlimit64 __user *, new_rlim,
1699                 struct rlimit64 __user *, old_rlim)
1700 {
1701         struct rlimit64 old64, new64;
1702         struct rlimit old, new;
1703         struct task_struct *tsk;
1704         int ret;
1705
1706         if (new_rlim) {
1707                 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1708                         return -EFAULT;
1709                 rlim64_to_rlim(&new64, &new);
1710         }
1711
1712         rcu_read_lock();
1713         tsk = pid ? find_task_by_vpid(pid) : current;
1714         if (!tsk) {
1715                 rcu_read_unlock();
1716                 return -ESRCH;
1717         }
1718         ret = check_prlimit_permission(tsk);
1719         if (ret) {
1720                 rcu_read_unlock();
1721                 return ret;
1722         }
1723         get_task_struct(tsk);
1724         rcu_read_unlock();
1725
1726         ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1727                         old_rlim ? &old : NULL);
1728
1729         if (!ret && old_rlim) {
1730                 rlim_to_rlim64(&old, &old64);
1731                 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1732                         ret = -EFAULT;
1733         }
1734
1735         put_task_struct(tsk);
1736         return ret;
1737 }
1738
1739 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1740 {
1741         struct rlimit new_rlim;
1742
1743         if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1744                 return -EFAULT;
1745         return do_prlimit(current, resource, &new_rlim, NULL);
1746 }
1747
1748 /*
1749  * It would make sense to put struct rusage in the task_struct,
1750  * except that would make the task_struct be *really big*.  After
1751  * task_struct gets moved into malloc'ed memory, it would
1752  * make sense to do this.  It will make moving the rest of the information
1753  * a lot simpler!  (Which we're not doing right now because we're not
1754  * measuring them yet).
1755  *
1756  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1757  * races with threads incrementing their own counters.  But since word
1758  * reads are atomic, we either get new values or old values and we don't
1759  * care which for the sums.  We always take the siglock to protect reading
1760  * the c* fields from p->signal from races with exit.c updating those
1761  * fields when reaping, so a sample either gets all the additions of a
1762  * given child after it's reaped, or none so this sample is before reaping.
1763  *
1764  * Locking:
1765  * We need to take the siglock for CHILDEREN, SELF and BOTH
1766  * for  the cases current multithreaded, non-current single threaded
1767  * non-current multithreaded.  Thread traversal is now safe with
1768  * the siglock held.
1769  * Strictly speaking, we donot need to take the siglock if we are current and
1770  * single threaded,  as no one else can take our signal_struct away, no one
1771  * else can  reap the  children to update signal->c* counters, and no one else
1772  * can race with the signal-> fields. If we do not take any lock, the
1773  * signal-> fields could be read out of order while another thread was just
1774  * exiting. So we should  place a read memory barrier when we avoid the lock.
1775  * On the writer side,  write memory barrier is implied in  __exit_signal
1776  * as __exit_signal releases  the siglock spinlock after updating the signal->
1777  * fields. But we don't do this yet to keep things simple.
1778  *
1779  */
1780
1781 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1782 {
1783         r->ru_nvcsw += t->nvcsw;
1784         r->ru_nivcsw += t->nivcsw;
1785         r->ru_minflt += t->min_flt;
1786         r->ru_majflt += t->maj_flt;
1787         r->ru_inblock += task_io_get_inblock(t);
1788         r->ru_oublock += task_io_get_oublock(t);
1789 }
1790
1791 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1792 {
1793         struct task_struct *t;
1794         unsigned long flags;
1795         cputime_t tgutime, tgstime, utime, stime;
1796         unsigned long maxrss = 0;
1797
1798         memset((char *) r, 0, sizeof *r);
1799         utime = stime = 0;
1800
1801         if (who == RUSAGE_THREAD) {
1802                 task_cputime_adjusted(current, &utime, &stime);
1803                 accumulate_thread_rusage(p, r);
1804                 maxrss = p->signal->maxrss;
1805                 goto out;
1806         }
1807
1808         if (!lock_task_sighand(p, &flags))
1809                 return;
1810
1811         switch (who) {
1812                 case RUSAGE_BOTH:
1813                 case RUSAGE_CHILDREN:
1814                         utime = p->signal->cutime;
1815                         stime = p->signal->cstime;
1816                         r->ru_nvcsw = p->signal->cnvcsw;
1817                         r->ru_nivcsw = p->signal->cnivcsw;
1818                         r->ru_minflt = p->signal->cmin_flt;
1819                         r->ru_majflt = p->signal->cmaj_flt;
1820                         r->ru_inblock = p->signal->cinblock;
1821                         r->ru_oublock = p->signal->coublock;
1822                         maxrss = p->signal->cmaxrss;
1823
1824                         if (who == RUSAGE_CHILDREN)
1825                                 break;
1826
1827                 case RUSAGE_SELF:
1828                         thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1829                         utime += tgutime;
1830                         stime += tgstime;
1831                         r->ru_nvcsw += p->signal->nvcsw;
1832                         r->ru_nivcsw += p->signal->nivcsw;
1833                         r->ru_minflt += p->signal->min_flt;
1834                         r->ru_majflt += p->signal->maj_flt;
1835                         r->ru_inblock += p->signal->inblock;
1836                         r->ru_oublock += p->signal->oublock;
1837                         if (maxrss < p->signal->maxrss)
1838                                 maxrss = p->signal->maxrss;
1839                         t = p;
1840                         do {
1841                                 accumulate_thread_rusage(t, r);
1842                                 t = next_thread(t);
1843                         } while (t != p);
1844                         break;
1845
1846                 default:
1847                         BUG();
1848         }
1849         unlock_task_sighand(p, &flags);
1850
1851 out:
1852         cputime_to_timeval(utime, &r->ru_utime);
1853         cputime_to_timeval(stime, &r->ru_stime);
1854
1855         if (who != RUSAGE_CHILDREN) {
1856                 struct mm_struct *mm = get_task_mm(p);
1857                 if (mm) {
1858                         setmax_mm_hiwater_rss(&maxrss, mm);
1859                         mmput(mm);
1860                 }
1861         }
1862         r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1863 }
1864
1865 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1866 {
1867         struct rusage r;
1868         k_getrusage(p, who, &r);
1869         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1870 }
1871
1872 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1873 {
1874         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1875             who != RUSAGE_THREAD)
1876                 return -EINVAL;
1877         return getrusage(current, who, ru);
1878 }
1879
1880 #ifdef CONFIG_COMPAT
1881 COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1882 {
1883         struct rusage r;
1884
1885         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1886             who != RUSAGE_THREAD)
1887                 return -EINVAL;
1888
1889         k_getrusage(current, who, &r);
1890         return put_compat_rusage(&r, ru);
1891 }
1892 #endif
1893
1894 SYSCALL_DEFINE1(umask, int, mask)
1895 {
1896         mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
1897         return mask;
1898 }
1899
1900 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1901 {
1902         struct fd exe;
1903         struct inode *inode;
1904         int err;
1905
1906         exe = fdget(fd);
1907         if (!exe.file)
1908                 return -EBADF;
1909
1910         inode = file_inode(exe.file);
1911
1912         /*
1913          * Because the original mm->exe_file points to executable file, make
1914          * sure that this one is executable as well, to avoid breaking an
1915          * overall picture.
1916          */
1917         err = -EACCES;
1918         if (!S_ISREG(inode->i_mode)     ||
1919             exe.file->f_path.mnt->mnt_flags & MNT_NOEXEC)
1920                 goto exit;
1921
1922         err = inode_permission(inode, MAY_EXEC);
1923         if (err)
1924                 goto exit;
1925
1926         down_write(&mm->mmap_sem);
1927
1928         /*
1929          * Forbid mm->exe_file change if old file still mapped.
1930          */
1931         err = -EBUSY;
1932         if (mm->exe_file) {
1933                 struct vm_area_struct *vma;
1934
1935                 for (vma = mm->mmap; vma; vma = vma->vm_next)
1936                         if (vma->vm_file &&
1937                             path_equal(&vma->vm_file->f_path,
1938                                        &mm->exe_file->f_path))
1939                                 goto exit_unlock;
1940         }
1941
1942         /*
1943          * The symlink can be changed only once, just to disallow arbitrary
1944          * transitions malicious software might bring in. This means one
1945          * could make a snapshot over all processes running and monitor
1946          * /proc/pid/exe changes to notice unusual activity if needed.
1947          */
1948         err = -EPERM;
1949         if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
1950                 goto exit_unlock;
1951
1952         err = 0;
1953         set_mm_exe_file(mm, exe.file);  /* this grabs a reference to exe.file */
1954 exit_unlock:
1955         up_write(&mm->mmap_sem);
1956
1957 exit:
1958         fdput(exe);
1959         return err;
1960 }
1961
1962 static int prctl_set_mm(int opt, unsigned long addr,
1963                         unsigned long arg4, unsigned long arg5)
1964 {
1965         unsigned long rlim = rlimit(RLIMIT_DATA);
1966         struct mm_struct *mm = current->mm;
1967         struct vm_area_struct *vma;
1968         int error;
1969
1970         if (arg5 || (arg4 && opt != PR_SET_MM_AUXV))
1971                 return -EINVAL;
1972
1973         if (!capable(CAP_SYS_RESOURCE))
1974                 return -EPERM;
1975
1976         if (opt == PR_SET_MM_EXE_FILE)
1977                 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
1978
1979         if (addr >= TASK_SIZE || addr < mmap_min_addr)
1980                 return -EINVAL;
1981
1982         error = -EINVAL;
1983
1984         down_read(&mm->mmap_sem);
1985         vma = find_vma(mm, addr);
1986
1987         switch (opt) {
1988         case PR_SET_MM_START_CODE:
1989                 mm->start_code = addr;
1990                 break;
1991         case PR_SET_MM_END_CODE:
1992                 mm->end_code = addr;
1993                 break;
1994         case PR_SET_MM_START_DATA:
1995                 mm->start_data = addr;
1996                 break;
1997         case PR_SET_MM_END_DATA:
1998                 mm->end_data = addr;
1999                 break;
2000
2001         case PR_SET_MM_START_BRK:
2002                 if (addr <= mm->end_data)
2003                         goto out;
2004
2005                 if (rlim < RLIM_INFINITY &&
2006                     (mm->brk - addr) +
2007                     (mm->end_data - mm->start_data) > rlim)
2008                         goto out;
2009
2010                 mm->start_brk = addr;
2011                 break;
2012
2013         case PR_SET_MM_BRK:
2014                 if (addr <= mm->end_data)
2015                         goto out;
2016
2017                 if (rlim < RLIM_INFINITY &&
2018                     (addr - mm->start_brk) +
2019                     (mm->end_data - mm->start_data) > rlim)
2020                         goto out;
2021
2022                 mm->brk = addr;
2023                 break;
2024
2025         /*
2026          * If command line arguments and environment
2027          * are placed somewhere else on stack, we can
2028          * set them up here, ARG_START/END to setup
2029          * command line argumets and ENV_START/END
2030          * for environment.
2031          */
2032         case PR_SET_MM_START_STACK:
2033         case PR_SET_MM_ARG_START:
2034         case PR_SET_MM_ARG_END:
2035         case PR_SET_MM_ENV_START:
2036         case PR_SET_MM_ENV_END:
2037                 if (!vma) {
2038                         error = -EFAULT;
2039                         goto out;
2040                 }
2041                 if (opt == PR_SET_MM_START_STACK)
2042                         mm->start_stack = addr;
2043                 else if (opt == PR_SET_MM_ARG_START)
2044                         mm->arg_start = addr;
2045                 else if (opt == PR_SET_MM_ARG_END)
2046                         mm->arg_end = addr;
2047                 else if (opt == PR_SET_MM_ENV_START)
2048                         mm->env_start = addr;
2049                 else if (opt == PR_SET_MM_ENV_END)
2050                         mm->env_end = addr;
2051                 break;
2052
2053         /*
2054          * This doesn't move auxiliary vector itself
2055          * since it's pinned to mm_struct, but allow
2056          * to fill vector with new values. It's up
2057          * to a caller to provide sane values here
2058          * otherwise user space tools which use this
2059          * vector might be unhappy.
2060          */
2061         case PR_SET_MM_AUXV: {
2062                 unsigned long user_auxv[AT_VECTOR_SIZE];
2063
2064                 if (arg4 > sizeof(user_auxv))
2065                         goto out;
2066                 up_read(&mm->mmap_sem);
2067
2068                 if (copy_from_user(user_auxv, (const void __user *)addr, arg4))
2069                         return -EFAULT;
2070
2071                 /* Make sure the last entry is always AT_NULL */
2072                 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2073                 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2074
2075                 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2076
2077                 task_lock(current);
2078                 memcpy(mm->saved_auxv, user_auxv, arg4);
2079                 task_unlock(current);
2080
2081                 return 0;
2082         }
2083         default:
2084                 goto out;
2085         }
2086
2087         error = 0;
2088 out:
2089         up_read(&mm->mmap_sem);
2090         return error;
2091 }
2092
2093 #ifdef CONFIG_CHECKPOINT_RESTORE
2094 static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2095 {
2096         return put_user(me->clear_child_tid, tid_addr);
2097 }
2098 #else
2099 static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2100 {
2101         return -EINVAL;
2102 }
2103 #endif
2104
2105 #ifdef CONFIG_MMU
2106 static int prctl_update_vma_anon_name(struct vm_area_struct *vma,
2107                 struct vm_area_struct **prev,
2108                 unsigned long start, unsigned long end,
2109                 const char __user *name_addr)
2110 {
2111         struct mm_struct * mm = vma->vm_mm;
2112         int error = 0;
2113         pgoff_t pgoff;
2114
2115         if (name_addr == vma_get_anon_name(vma)) {
2116                 *prev = vma;
2117                 goto out;
2118         }
2119
2120         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2121         *prev = vma_merge(mm, *prev, start, end, vma->vm_flags, vma->anon_vma,
2122                                 vma->vm_file, pgoff, vma_policy(vma),
2123                                 name_addr);
2124         if (*prev) {
2125                 vma = *prev;
2126                 goto success;
2127         }
2128
2129         *prev = vma;
2130
2131         if (start != vma->vm_start) {
2132                 error = split_vma(mm, vma, start, 1);
2133                 if (error)
2134                         goto out;
2135         }
2136
2137         if (end != vma->vm_end) {
2138                 error = split_vma(mm, vma, end, 0);
2139                 if (error)
2140                         goto out;
2141         }
2142
2143 success:
2144         if (!vma->vm_file)
2145                 vma->shared.anon_name = name_addr;
2146
2147 out:
2148         if (error == -ENOMEM)
2149                 error = -EAGAIN;
2150         return error;
2151 }
2152
2153 static int prctl_set_vma_anon_name(unsigned long start, unsigned long end,
2154                         unsigned long arg)
2155 {
2156         unsigned long tmp;
2157         struct vm_area_struct * vma, *prev;
2158         int unmapped_error = 0;
2159         int error = -EINVAL;
2160
2161         /*
2162          * If the interval [start,end) covers some unmapped address
2163          * ranges, just ignore them, but return -ENOMEM at the end.
2164          * - this matches the handling in madvise.
2165          */
2166         vma = find_vma_prev(current->mm, start, &prev);
2167         if (vma && start > vma->vm_start)
2168                 prev = vma;
2169
2170         for (;;) {
2171                 /* Still start < end. */
2172                 error = -ENOMEM;
2173                 if (!vma)
2174                         return error;
2175
2176                 /* Here start < (end|vma->vm_end). */
2177                 if (start < vma->vm_start) {
2178                         unmapped_error = -ENOMEM;
2179                         start = vma->vm_start;
2180                         if (start >= end)
2181                                 return error;
2182                 }
2183
2184                 /* Here vma->vm_start <= start < (end|vma->vm_end) */
2185                 tmp = vma->vm_end;
2186                 if (end < tmp)
2187                         tmp = end;
2188
2189                 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
2190                 error = prctl_update_vma_anon_name(vma, &prev, start, tmp,
2191                                 (const char __user *)arg);
2192                 if (error)
2193                         return error;
2194                 start = tmp;
2195                 if (prev && start < prev->vm_end)
2196                         start = prev->vm_end;
2197                 error = unmapped_error;
2198                 if (start >= end)
2199                         return error;
2200                 if (prev)
2201                         vma = prev->vm_next;
2202                 else    /* madvise_remove dropped mmap_sem */
2203                         vma = find_vma(current->mm, start);
2204         }
2205 }
2206
2207 static int prctl_set_vma(unsigned long opt, unsigned long start,
2208                 unsigned long len_in, unsigned long arg)
2209 {
2210         struct mm_struct *mm = current->mm;
2211         int error;
2212         unsigned long len;
2213         unsigned long end;
2214
2215         if (start & ~PAGE_MASK)
2216                 return -EINVAL;
2217         len = (len_in + ~PAGE_MASK) & PAGE_MASK;
2218
2219         /* Check to see whether len was rounded up from small -ve to zero */
2220         if (len_in && !len)
2221                 return -EINVAL;
2222
2223         end = start + len;
2224         if (end < start)
2225                 return -EINVAL;
2226
2227         if (end == start)
2228                 return 0;
2229
2230         down_write(&mm->mmap_sem);
2231
2232         switch (opt) {
2233         case PR_SET_VMA_ANON_NAME:
2234                 error = prctl_set_vma_anon_name(start, end, arg);
2235                 break;
2236         default:
2237                 error = -EINVAL;
2238         }
2239
2240         up_write(&mm->mmap_sem);
2241
2242         return error;
2243 }
2244 #else /* CONFIG_MMU */
2245 static int prctl_set_vma(unsigned long opt, unsigned long start,
2246                 unsigned long len_in, unsigned long arg)
2247 {
2248         return -EINVAL;
2249 }
2250 #endif
2251
2252 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2253                 unsigned long, arg4, unsigned long, arg5)
2254 {
2255         struct task_struct *me = current;
2256         struct task_struct *tsk;
2257         unsigned char comm[sizeof(me->comm)];
2258         long error;
2259
2260         error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2261         if (error != -ENOSYS)
2262                 return error;
2263
2264         error = 0;
2265         switch (option) {
2266         case PR_SET_PDEATHSIG:
2267                 if (!valid_signal(arg2)) {
2268                         error = -EINVAL;
2269                         break;
2270                 }
2271                 me->pdeath_signal = arg2;
2272                 break;
2273         case PR_GET_PDEATHSIG:
2274                 error = put_user(me->pdeath_signal, (int __user *)arg2);
2275                 break;
2276         case PR_GET_DUMPABLE:
2277                 error = get_dumpable(me->mm);
2278                 break;
2279         case PR_SET_DUMPABLE:
2280                 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2281                         error = -EINVAL;
2282                         break;
2283                 }
2284                 set_dumpable(me->mm, arg2);
2285                 break;
2286
2287         case PR_SET_UNALIGN:
2288                 error = SET_UNALIGN_CTL(me, arg2);
2289                 break;
2290         case PR_GET_UNALIGN:
2291                 error = GET_UNALIGN_CTL(me, arg2);
2292                 break;
2293         case PR_SET_FPEMU:
2294                 error = SET_FPEMU_CTL(me, arg2);
2295                 break;
2296         case PR_GET_FPEMU:
2297                 error = GET_FPEMU_CTL(me, arg2);
2298                 break;
2299         case PR_SET_FPEXC:
2300                 error = SET_FPEXC_CTL(me, arg2);
2301                 break;
2302         case PR_GET_FPEXC:
2303                 error = GET_FPEXC_CTL(me, arg2);
2304                 break;
2305         case PR_GET_TIMING:
2306                 error = PR_TIMING_STATISTICAL;
2307                 break;
2308         case PR_SET_TIMING:
2309                 if (arg2 != PR_TIMING_STATISTICAL)
2310                         error = -EINVAL;
2311                 break;
2312         case PR_SET_NAME:
2313                 comm[sizeof(me->comm) - 1] = 0;
2314                 if (strncpy_from_user(comm, (char __user *)arg2,
2315                                       sizeof(me->comm) - 1) < 0)
2316                         return -EFAULT;
2317                 set_task_comm(me, comm);
2318                 proc_comm_connector(me);
2319                 break;
2320         case PR_GET_NAME:
2321                 get_task_comm(comm, me);
2322                 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2323                         return -EFAULT;
2324                 break;
2325         case PR_GET_ENDIAN:
2326                 error = GET_ENDIAN(me, arg2);
2327                 break;
2328         case PR_SET_ENDIAN:
2329                 error = SET_ENDIAN(me, arg2);
2330                 break;
2331         case PR_GET_SECCOMP:
2332                 error = prctl_get_seccomp();
2333                 break;
2334         case PR_SET_SECCOMP:
2335                 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2336                 break;
2337         case PR_GET_TSC:
2338                 error = GET_TSC_CTL(arg2);
2339                 break;
2340         case PR_SET_TSC:
2341                 error = SET_TSC_CTL(arg2);
2342                 break;
2343         case PR_TASK_PERF_EVENTS_DISABLE:
2344                 error = perf_event_task_disable();
2345                 break;
2346         case PR_TASK_PERF_EVENTS_ENABLE:
2347                 error = perf_event_task_enable();
2348                 break;
2349         case PR_GET_TIMERSLACK:
2350                 error = current->timer_slack_ns;
2351                 break;
2352         case PR_SET_TIMERSLACK:
2353                 if (arg2 <= 0)
2354                         current->timer_slack_ns =
2355                                         current->default_timer_slack_ns;
2356                 else
2357                         current->timer_slack_ns = arg2;
2358                 break;
2359         case PR_MCE_KILL:
2360                 if (arg4 | arg5)
2361                         return -EINVAL;
2362                 switch (arg2) {
2363                 case PR_MCE_KILL_CLEAR:
2364                         if (arg3 != 0)
2365                                 return -EINVAL;
2366                         current->flags &= ~PF_MCE_PROCESS;
2367                         break;
2368                 case PR_MCE_KILL_SET:
2369                         current->flags |= PF_MCE_PROCESS;
2370                         if (arg3 == PR_MCE_KILL_EARLY)
2371                                 current->flags |= PF_MCE_EARLY;
2372                         else if (arg3 == PR_MCE_KILL_LATE)
2373                                 current->flags &= ~PF_MCE_EARLY;
2374                         else if (arg3 == PR_MCE_KILL_DEFAULT)
2375                                 current->flags &=
2376                                                 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2377                         else
2378                                 return -EINVAL;
2379                         break;
2380                 default:
2381                         return -EINVAL;
2382                 }
2383                 break;
2384         case PR_MCE_KILL_GET:
2385                 if (arg2 | arg3 | arg4 | arg5)
2386                         return -EINVAL;
2387                 if (current->flags & PF_MCE_PROCESS)
2388                         error = (current->flags & PF_MCE_EARLY) ?
2389                                 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2390                 else
2391                         error = PR_MCE_KILL_DEFAULT;
2392                 break;
2393         case PR_SET_MM:
2394                 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2395                 break;
2396         case PR_GET_TID_ADDRESS:
2397                 error = prctl_get_tid_address(me, (int __user **)arg2);
2398                 break;
2399         case PR_SET_TIMERSLACK_PID:
2400                 if (task_pid_vnr(current) != (pid_t)arg3 &&
2401                                 !capable(CAP_SYS_NICE))
2402                         return -EPERM;
2403                 rcu_read_lock();
2404                 tsk = find_task_by_vpid((pid_t)arg3);
2405                 if (tsk == NULL) {
2406                         rcu_read_unlock();
2407                         return -EINVAL;
2408                 }
2409                 get_task_struct(tsk);
2410                 rcu_read_unlock();
2411                 if (arg2 <= 0)
2412                         tsk->timer_slack_ns =
2413                                 tsk->default_timer_slack_ns;
2414                 else
2415                         tsk->timer_slack_ns = arg2;
2416                 put_task_struct(tsk);
2417                 error = 0;
2418                 break;
2419         case PR_SET_CHILD_SUBREAPER:
2420                 me->signal->is_child_subreaper = !!arg2;
2421                 break;
2422         case PR_GET_CHILD_SUBREAPER:
2423                 error = put_user(me->signal->is_child_subreaper,
2424                                  (int __user *)arg2);
2425                 break;
2426         case PR_SET_NO_NEW_PRIVS:
2427                 if (arg2 != 1 || arg3 || arg4 || arg5)
2428                         return -EINVAL;
2429
2430                 task_set_no_new_privs(current);
2431                 break;
2432         case PR_GET_NO_NEW_PRIVS:
2433                 if (arg2 || arg3 || arg4 || arg5)
2434                         return -EINVAL;
2435                 return task_no_new_privs(current) ? 1 : 0;
2436         case PR_SET_VMA:
2437                 error = prctl_set_vma(arg2, arg3, arg4, arg5);
2438                 break;
2439         default:
2440                 error = -EINVAL;
2441                 break;
2442         }
2443         return error;
2444 }
2445
2446 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2447                 struct getcpu_cache __user *, unused)
2448 {
2449         int err = 0;
2450         int cpu = raw_smp_processor_id();
2451         if (cpup)
2452                 err |= put_user(cpu, cpup);
2453         if (nodep)
2454                 err |= put_user(cpu_to_node(cpu), nodep);
2455         return err ? -EFAULT : 0;
2456 }
2457
2458 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2459
2460 static int __orderly_poweroff(bool force)
2461 {
2462         char **argv;
2463         static char *envp[] = {
2464                 "HOME=/",
2465                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2466                 NULL
2467         };
2468         int ret;
2469
2470         argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
2471         if (argv) {
2472                 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
2473                 argv_free(argv);
2474         } else {
2475                 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2476                                          __func__, poweroff_cmd);
2477                 ret = -ENOMEM;
2478         }
2479
2480         if (ret && force) {
2481                 printk(KERN_WARNING "Failed to start orderly shutdown: "
2482                                         "forcing the issue\n");
2483                 /*
2484                  * I guess this should try to kick off some daemon to sync and
2485                  * poweroff asap.  Or not even bother syncing if we're doing an
2486                  * emergency shutdown?
2487                  */
2488                 emergency_sync();
2489                 kernel_power_off();
2490         }
2491
2492         return ret;
2493 }
2494
2495 static bool poweroff_force;
2496
2497 static void poweroff_work_func(struct work_struct *work)
2498 {
2499         __orderly_poweroff(poweroff_force);
2500 }
2501
2502 static DECLARE_WORK(poweroff_work, poweroff_work_func);
2503
2504 /**
2505  * orderly_poweroff - Trigger an orderly system poweroff
2506  * @force: force poweroff if command execution fails
2507  *
2508  * This may be called from any context to trigger a system shutdown.
2509  * If the orderly shutdown fails, it will force an immediate shutdown.
2510  */
2511 int orderly_poweroff(bool force)
2512 {
2513         if (force) /* do not override the pending "true" */
2514                 poweroff_force = true;
2515         schedule_work(&poweroff_work);
2516         return 0;
2517 }
2518 EXPORT_SYMBOL_GPL(orderly_poweroff);
2519
2520 /**
2521  * do_sysinfo - fill in sysinfo struct
2522  * @info: pointer to buffer to fill
2523  */
2524 static int do_sysinfo(struct sysinfo *info)
2525 {
2526         unsigned long mem_total, sav_total;
2527         unsigned int mem_unit, bitcount;
2528         struct timespec tp;
2529
2530         memset(info, 0, sizeof(struct sysinfo));
2531
2532         ktime_get_ts(&tp);
2533         monotonic_to_bootbased(&tp);
2534         info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2535
2536         get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2537
2538         info->procs = nr_threads;
2539
2540         si_meminfo(info);
2541         si_swapinfo(info);
2542
2543         /*
2544          * If the sum of all the available memory (i.e. ram + swap)
2545          * is less than can be stored in a 32 bit unsigned long then
2546          * we can be binary compatible with 2.2.x kernels.  If not,
2547          * well, in that case 2.2.x was broken anyways...
2548          *
2549          *  -Erik Andersen <andersee@debian.org>
2550          */
2551
2552         mem_total = info->totalram + info->totalswap;
2553         if (mem_total < info->totalram || mem_total < info->totalswap)
2554                 goto out;
2555         bitcount = 0;
2556         mem_unit = info->mem_unit;
2557         while (mem_unit > 1) {
2558                 bitcount++;
2559                 mem_unit >>= 1;
2560                 sav_total = mem_total;
2561                 mem_total <<= 1;
2562                 if (mem_total < sav_total)
2563                         goto out;
2564         }
2565
2566         /*
2567          * If mem_total did not overflow, multiply all memory values by
2568          * info->mem_unit and set it to 1.  This leaves things compatible
2569          * with 2.2.x, and also retains compatibility with earlier 2.4.x
2570          * kernels...
2571          */
2572
2573         info->mem_unit = 1;
2574         info->totalram <<= bitcount;
2575         info->freeram <<= bitcount;
2576         info->sharedram <<= bitcount;
2577         info->bufferram <<= bitcount;
2578         info->totalswap <<= bitcount;
2579         info->freeswap <<= bitcount;
2580         info->totalhigh <<= bitcount;
2581         info->freehigh <<= bitcount;
2582
2583 out:
2584         return 0;
2585 }
2586
2587 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2588 {
2589         struct sysinfo val;
2590
2591         do_sysinfo(&val);
2592
2593         if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2594                 return -EFAULT;
2595
2596         return 0;
2597 }
2598
2599 #ifdef CONFIG_COMPAT
2600 struct compat_sysinfo {
2601         s32 uptime;
2602         u32 loads[3];
2603         u32 totalram;
2604         u32 freeram;
2605         u32 sharedram;
2606         u32 bufferram;
2607         u32 totalswap;
2608         u32 freeswap;
2609         u16 procs;
2610         u16 pad;
2611         u32 totalhigh;
2612         u32 freehigh;
2613         u32 mem_unit;
2614         char _f[20-2*sizeof(u32)-sizeof(int)];
2615 };
2616
2617 COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2618 {
2619         struct sysinfo s;
2620
2621         do_sysinfo(&s);
2622
2623         /* Check to see if any memory value is too large for 32-bit and scale
2624          *  down if needed
2625          */
2626         if ((s.totalram >> 32) || (s.totalswap >> 32)) {
2627                 int bitcount = 0;
2628
2629                 while (s.mem_unit < PAGE_SIZE) {
2630                         s.mem_unit <<= 1;
2631                         bitcount++;
2632                 }
2633
2634                 s.totalram >>= bitcount;
2635                 s.freeram >>= bitcount;
2636                 s.sharedram >>= bitcount;
2637                 s.bufferram >>= bitcount;
2638                 s.totalswap >>= bitcount;
2639                 s.freeswap >>= bitcount;
2640                 s.totalhigh >>= bitcount;
2641                 s.freehigh >>= bitcount;
2642         }
2643
2644         if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2645             __put_user(s.uptime, &info->uptime) ||
2646             __put_user(s.loads[0], &info->loads[0]) ||
2647             __put_user(s.loads[1], &info->loads[1]) ||
2648             __put_user(s.loads[2], &info->loads[2]) ||
2649             __put_user(s.totalram, &info->totalram) ||
2650             __put_user(s.freeram, &info->freeram) ||
2651             __put_user(s.sharedram, &info->sharedram) ||
2652             __put_user(s.bufferram, &info->bufferram) ||
2653             __put_user(s.totalswap, &info->totalswap) ||
2654             __put_user(s.freeswap, &info->freeswap) ||
2655             __put_user(s.procs, &info->procs) ||
2656             __put_user(s.totalhigh, &info->totalhigh) ||
2657             __put_user(s.freehigh, &info->freehigh) ||
2658             __put_user(s.mem_unit, &info->mem_unit))
2659                 return -EFAULT;
2660
2661         return 0;
2662 }
2663 #endif /* CONFIG_COMPAT */