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