4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/export.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>
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>
46 #include <linux/mempolicy.h>
47 #include <linux/sched.h>
49 #include <linux/compat.h>
50 #include <linux/syscalls.h>
51 #include <linux/kprobes.h>
52 #include <linux/user_namespace.h>
53 #include <linux/binfmts.h>
55 #include <linux/sched.h>
56 #include <linux/rcupdate.h>
57 #include <linux/uidgid.h>
58 #include <linux/cred.h>
60 #include <linux/kmsg_dump.h>
61 /* Move somewhere else to avoid recompiling? */
62 #include <generated/utsrelease.h>
64 #include <asm/uaccess.h>
66 #include <asm/unistd.h>
68 #ifndef SET_UNALIGN_CTL
69 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
71 #ifndef GET_UNALIGN_CTL
72 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
75 # define SET_FPEMU_CTL(a,b) (-EINVAL)
78 # define GET_FPEMU_CTL(a,b) (-EINVAL)
81 # define SET_FPEXC_CTL(a,b) (-EINVAL)
84 # define GET_FPEXC_CTL(a,b) (-EINVAL)
87 # define GET_ENDIAN(a,b) (-EINVAL)
90 # define SET_ENDIAN(a,b) (-EINVAL)
93 # define GET_TSC_CTL(a) (-EINVAL)
96 # define SET_TSC_CTL(a) (-EINVAL)
100 * this is where the system-wide overflow UID and GID are defined, for
101 * architectures that now have 32-bit UID/GID but didn't in the past
104 int overflowuid = DEFAULT_OVERFLOWUID;
105 int overflowgid = DEFAULT_OVERFLOWGID;
107 EXPORT_SYMBOL(overflowuid);
108 EXPORT_SYMBOL(overflowgid);
111 * the same as above, but for filesystems which can only store a 16-bit
112 * UID and GID. as such, this is needed on all architectures
115 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
116 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
118 EXPORT_SYMBOL(fs_overflowuid);
119 EXPORT_SYMBOL(fs_overflowgid);
122 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
127 EXPORT_SYMBOL(cad_pid);
130 * If set, this is used for preparing the system to power off.
133 void (*pm_power_off_prepare)(void);
136 * Returns true if current's euid is same as p's uid or euid,
137 * or has CAP_SYS_NICE to p's user_ns.
139 * Called with rcu_read_lock, creds are safe
141 static bool set_one_prio_perm(struct task_struct *p)
143 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
145 if (uid_eq(pcred->uid, cred->euid) ||
146 uid_eq(pcred->euid, cred->euid))
148 if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
154 * set the priority of a task
155 * - the caller must hold the RCU read lock
157 static int set_one_prio(struct task_struct *p, int niceval, int error)
161 if (!set_one_prio_perm(p)) {
165 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
169 no_nice = security_task_setnice(p, niceval);
176 set_user_nice(p, niceval);
181 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
183 struct task_struct *g, *p;
184 struct user_struct *user;
185 const struct cred *cred = current_cred();
190 if (which > PRIO_USER || which < PRIO_PROCESS)
193 /* normalize: avoid signed division (rounding problems) */
201 read_lock(&tasklist_lock);
205 p = find_task_by_vpid(who);
209 error = set_one_prio(p, niceval, error);
213 pgrp = find_vpid(who);
215 pgrp = task_pgrp(current);
216 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
217 error = set_one_prio(p, niceval, error);
218 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
221 uid = make_kuid(cred->user_ns, who);
225 else if (!uid_eq(uid, cred->uid) &&
226 !(user = find_user(uid)))
227 goto out_unlock; /* No processes for this user */
229 do_each_thread(g, p) {
230 if (uid_eq(task_uid(p), uid))
231 error = set_one_prio(p, niceval, error);
232 } while_each_thread(g, p);
233 if (!uid_eq(uid, cred->uid))
234 free_uid(user); /* For find_user() */
238 read_unlock(&tasklist_lock);
245 * Ugh. To avoid negative return values, "getpriority()" will
246 * not return the normal nice-value, but a negated value that
247 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
248 * to stay compatible.
250 SYSCALL_DEFINE2(getpriority, int, which, int, who)
252 struct task_struct *g, *p;
253 struct user_struct *user;
254 const struct cred *cred = current_cred();
255 long niceval, retval = -ESRCH;
259 if (which > PRIO_USER || which < PRIO_PROCESS)
263 read_lock(&tasklist_lock);
267 p = find_task_by_vpid(who);
271 niceval = 20 - task_nice(p);
272 if (niceval > retval)
278 pgrp = find_vpid(who);
280 pgrp = task_pgrp(current);
281 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
282 niceval = 20 - task_nice(p);
283 if (niceval > retval)
285 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
288 uid = make_kuid(cred->user_ns, who);
292 else if (!uid_eq(uid, cred->uid) &&
293 !(user = find_user(uid)))
294 goto out_unlock; /* No processes for this user */
296 do_each_thread(g, p) {
297 if (uid_eq(task_uid(p), uid)) {
298 niceval = 20 - task_nice(p);
299 if (niceval > retval)
302 } while_each_thread(g, p);
303 if (!uid_eq(uid, cred->uid))
304 free_uid(user); /* for find_user() */
308 read_unlock(&tasklist_lock);
315 * emergency_restart - reboot the system
317 * Without shutting down any hardware or taking any locks
318 * reboot the system. This is called when we know we are in
319 * trouble so this is our best effort to reboot. This is
320 * safe to call in interrupt context.
322 void emergency_restart(void)
324 kmsg_dump(KMSG_DUMP_EMERG);
325 machine_emergency_restart();
327 EXPORT_SYMBOL_GPL(emergency_restart);
329 void kernel_restart_prepare(char *cmd)
331 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
332 system_state = SYSTEM_RESTART;
333 usermodehelper_disable();
338 * register_reboot_notifier - Register function to be called at reboot time
339 * @nb: Info about notifier function to be called
341 * Registers a function with the list of functions
342 * to be called at reboot time.
344 * Currently always returns zero, as blocking_notifier_chain_register()
345 * always returns zero.
347 int register_reboot_notifier(struct notifier_block *nb)
349 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
351 EXPORT_SYMBOL(register_reboot_notifier);
354 * unregister_reboot_notifier - Unregister previously registered reboot notifier
355 * @nb: Hook to be unregistered
357 * Unregisters a previously registered reboot
360 * Returns zero on success, or %-ENOENT on failure.
362 int unregister_reboot_notifier(struct notifier_block *nb)
364 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
366 EXPORT_SYMBOL(unregister_reboot_notifier);
368 /* Add backwards compatibility for stable trees. */
369 #ifndef PF_NO_SETAFFINITY
370 #define PF_NO_SETAFFINITY PF_THREAD_BOUND
373 static void migrate_to_reboot_cpu(void)
375 /* The boot cpu is always logical cpu 0 */
378 cpu_hotplug_disable();
380 /* Make certain the cpu I'm about to reboot on is online */
381 if (!cpu_online(cpu))
382 cpu = cpumask_first(cpu_online_mask);
384 /* Prevent races with other tasks migrating this task */
385 current->flags |= PF_NO_SETAFFINITY;
387 /* Make certain I only run on the appropriate processor */
388 set_cpus_allowed_ptr(current, cpumask_of(cpu));
392 * kernel_restart - reboot the system
393 * @cmd: pointer to buffer containing command to execute for restart
396 * Shutdown everything and perform a clean reboot.
397 * This is not safe to call in interrupt context.
399 void kernel_restart(char *cmd)
401 kernel_restart_prepare(cmd);
402 migrate_to_reboot_cpu();
405 printk(KERN_EMERG "Restarting system.\n");
407 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
408 kmsg_dump(KMSG_DUMP_RESTART);
409 machine_restart(cmd);
411 EXPORT_SYMBOL_GPL(kernel_restart);
413 static void kernel_shutdown_prepare(enum system_states state)
415 blocking_notifier_call_chain(&reboot_notifier_list,
416 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
417 system_state = state;
418 usermodehelper_disable();
422 * kernel_halt - halt the system
424 * Shutdown everything and perform a clean system halt.
426 void kernel_halt(void)
428 kernel_shutdown_prepare(SYSTEM_HALT);
429 migrate_to_reboot_cpu();
431 printk(KERN_EMERG "System halted.\n");
432 kmsg_dump(KMSG_DUMP_HALT);
436 EXPORT_SYMBOL_GPL(kernel_halt);
439 * kernel_power_off - power_off the system
441 * Shutdown everything and perform a clean system power_off.
443 void kernel_power_off(void)
445 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
446 if (pm_power_off_prepare)
447 pm_power_off_prepare();
448 migrate_to_reboot_cpu();
450 printk(KERN_EMERG "Power down.\n");
451 kmsg_dump(KMSG_DUMP_POWEROFF);
454 EXPORT_SYMBOL_GPL(kernel_power_off);
456 static DEFINE_MUTEX(reboot_mutex);
459 * Reboot system call: for obvious reasons only root may call it,
460 * and even root needs to set up some magic numbers in the registers
461 * so that some mistake won't make this reboot the whole machine.
462 * You can also set the meaning of the ctrl-alt-del-key here.
464 * reboot doesn't sync: do that yourself before calling this.
466 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
469 struct pid_namespace *pid_ns = task_active_pid_ns(current);
473 /* We only trust the superuser with rebooting the system. */
474 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
477 /* For safety, we require "magic" arguments. */
478 if (magic1 != LINUX_REBOOT_MAGIC1 ||
479 (magic2 != LINUX_REBOOT_MAGIC2 &&
480 magic2 != LINUX_REBOOT_MAGIC2A &&
481 magic2 != LINUX_REBOOT_MAGIC2B &&
482 magic2 != LINUX_REBOOT_MAGIC2C))
486 * If pid namespaces are enabled and the current task is in a child
487 * pid_namespace, the command is handled by reboot_pid_ns() which will
490 ret = reboot_pid_ns(pid_ns, cmd);
494 /* Instead of trying to make the power_off code look like
495 * halt when pm_power_off is not set do it the easy way.
497 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
498 cmd = LINUX_REBOOT_CMD_HALT;
500 mutex_lock(&reboot_mutex);
502 case LINUX_REBOOT_CMD_RESTART:
503 kernel_restart(NULL);
506 case LINUX_REBOOT_CMD_CAD_ON:
510 case LINUX_REBOOT_CMD_CAD_OFF:
514 case LINUX_REBOOT_CMD_HALT:
517 panic("cannot halt");
519 case LINUX_REBOOT_CMD_POWER_OFF:
524 case LINUX_REBOOT_CMD_RESTART2:
525 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
529 buffer[sizeof(buffer) - 1] = '\0';
531 kernel_restart(buffer);
535 case LINUX_REBOOT_CMD_KEXEC:
536 ret = kernel_kexec();
540 #ifdef CONFIG_HIBERNATION
541 case LINUX_REBOOT_CMD_SW_SUSPEND:
550 mutex_unlock(&reboot_mutex);
554 static void deferred_cad(struct work_struct *dummy)
556 kernel_restart(NULL);
560 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
561 * As it's called within an interrupt, it may NOT sync: the only choice
562 * is whether to reboot at once, or just ignore the ctrl-alt-del.
564 void ctrl_alt_del(void)
566 static DECLARE_WORK(cad_work, deferred_cad);
569 schedule_work(&cad_work);
571 kill_cad_pid(SIGINT, 1);
575 * Unprivileged users may change the real gid to the effective gid
576 * or vice versa. (BSD-style)
578 * If you set the real gid at all, or set the effective gid to a value not
579 * equal to the real gid, then the saved gid is set to the new effective gid.
581 * This makes it possible for a setgid program to completely drop its
582 * privileges, which is often a useful assertion to make when you are doing
583 * a security audit over a program.
585 * The general idea is that a program which uses just setregid() will be
586 * 100% compatible with BSD. A program which uses just setgid() will be
587 * 100% compatible with POSIX with saved IDs.
589 * SMP: There are not races, the GIDs are checked only by filesystem
590 * operations (as far as semantic preservation is concerned).
592 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
594 struct user_namespace *ns = current_user_ns();
595 const struct cred *old;
600 krgid = make_kgid(ns, rgid);
601 kegid = make_kgid(ns, egid);
603 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
605 if ((egid != (gid_t) -1) && !gid_valid(kegid))
608 new = prepare_creds();
611 old = current_cred();
614 if (rgid != (gid_t) -1) {
615 if (gid_eq(old->gid, krgid) ||
616 gid_eq(old->egid, krgid) ||
617 nsown_capable(CAP_SETGID))
622 if (egid != (gid_t) -1) {
623 if (gid_eq(old->gid, kegid) ||
624 gid_eq(old->egid, kegid) ||
625 gid_eq(old->sgid, kegid) ||
626 nsown_capable(CAP_SETGID))
632 if (rgid != (gid_t) -1 ||
633 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
634 new->sgid = new->egid;
635 new->fsgid = new->egid;
637 return commit_creds(new);
645 * setgid() is implemented like SysV w/ SAVED_IDS
647 * SMP: Same implicit races as above.
649 SYSCALL_DEFINE1(setgid, gid_t, gid)
651 struct user_namespace *ns = current_user_ns();
652 const struct cred *old;
657 kgid = make_kgid(ns, gid);
658 if (!gid_valid(kgid))
661 new = prepare_creds();
664 old = current_cred();
667 if (nsown_capable(CAP_SETGID))
668 new->gid = new->egid = new->sgid = new->fsgid = kgid;
669 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
670 new->egid = new->fsgid = kgid;
674 return commit_creds(new);
682 * change the user struct in a credentials set to match the new UID
684 static int set_user(struct cred *new)
686 struct user_struct *new_user;
688 new_user = alloc_uid(new->uid);
693 * We don't fail in case of NPROC limit excess here because too many
694 * poorly written programs don't check set*uid() return code, assuming
695 * it never fails if called by root. We may still enforce NPROC limit
696 * for programs doing set*uid()+execve() by harmlessly deferring the
697 * failure to the execve() stage.
699 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
700 new_user != INIT_USER)
701 current->flags |= PF_NPROC_EXCEEDED;
703 current->flags &= ~PF_NPROC_EXCEEDED;
706 new->user = new_user;
711 * Unprivileged users may change the real uid to the effective uid
712 * or vice versa. (BSD-style)
714 * If you set the real uid at all, or set the effective uid to a value not
715 * equal to the real uid, then the saved uid is set to the new effective uid.
717 * This makes it possible for a setuid program to completely drop its
718 * privileges, which is often a useful assertion to make when you are doing
719 * a security audit over a program.
721 * The general idea is that a program which uses just setreuid() will be
722 * 100% compatible with BSD. A program which uses just setuid() will be
723 * 100% compatible with POSIX with saved IDs.
725 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
727 struct user_namespace *ns = current_user_ns();
728 const struct cred *old;
733 kruid = make_kuid(ns, ruid);
734 keuid = make_kuid(ns, euid);
736 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
738 if ((euid != (uid_t) -1) && !uid_valid(keuid))
741 new = prepare_creds();
744 old = current_cred();
747 if (ruid != (uid_t) -1) {
749 if (!uid_eq(old->uid, kruid) &&
750 !uid_eq(old->euid, kruid) &&
751 !nsown_capable(CAP_SETUID))
755 if (euid != (uid_t) -1) {
757 if (!uid_eq(old->uid, keuid) &&
758 !uid_eq(old->euid, keuid) &&
759 !uid_eq(old->suid, keuid) &&
760 !nsown_capable(CAP_SETUID))
764 if (!uid_eq(new->uid, old->uid)) {
765 retval = set_user(new);
769 if (ruid != (uid_t) -1 ||
770 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
771 new->suid = new->euid;
772 new->fsuid = new->euid;
774 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
778 return commit_creds(new);
786 * setuid() is implemented like SysV with SAVED_IDS
788 * Note that SAVED_ID's is deficient in that a setuid root program
789 * like sendmail, for example, cannot set its uid to be a normal
790 * user and then switch back, because if you're root, setuid() sets
791 * the saved uid too. If you don't like this, blame the bright people
792 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
793 * will allow a root program to temporarily drop privileges and be able to
794 * regain them by swapping the real and effective uid.
796 SYSCALL_DEFINE1(setuid, uid_t, uid)
798 struct user_namespace *ns = current_user_ns();
799 const struct cred *old;
804 kuid = make_kuid(ns, uid);
805 if (!uid_valid(kuid))
808 new = prepare_creds();
811 old = current_cred();
814 if (nsown_capable(CAP_SETUID)) {
815 new->suid = new->uid = kuid;
816 if (!uid_eq(kuid, old->uid)) {
817 retval = set_user(new);
821 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
825 new->fsuid = new->euid = kuid;
827 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
831 return commit_creds(new);
840 * This function implements a generic ability to update ruid, euid,
841 * and suid. This allows you to implement the 4.4 compatible seteuid().
843 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
845 struct user_namespace *ns = current_user_ns();
846 const struct cred *old;
849 kuid_t kruid, keuid, ksuid;
851 kruid = make_kuid(ns, ruid);
852 keuid = make_kuid(ns, euid);
853 ksuid = make_kuid(ns, suid);
855 if ((ruid != (uid_t) -1) && !uid_valid(kruid))
858 if ((euid != (uid_t) -1) && !uid_valid(keuid))
861 if ((suid != (uid_t) -1) && !uid_valid(ksuid))
864 new = prepare_creds();
868 old = current_cred();
871 if (!nsown_capable(CAP_SETUID)) {
872 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
873 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
875 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
876 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
878 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
879 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
883 if (ruid != (uid_t) -1) {
885 if (!uid_eq(kruid, old->uid)) {
886 retval = set_user(new);
891 if (euid != (uid_t) -1)
893 if (suid != (uid_t) -1)
895 new->fsuid = new->euid;
897 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
901 return commit_creds(new);
908 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
910 const struct cred *cred = current_cred();
912 uid_t ruid, euid, suid;
914 ruid = from_kuid_munged(cred->user_ns, cred->uid);
915 euid = from_kuid_munged(cred->user_ns, cred->euid);
916 suid = from_kuid_munged(cred->user_ns, cred->suid);
918 if (!(retval = put_user(ruid, ruidp)) &&
919 !(retval = put_user(euid, euidp)))
920 retval = put_user(suid, suidp);
926 * Same as above, but for rgid, egid, sgid.
928 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
930 struct user_namespace *ns = current_user_ns();
931 const struct cred *old;
934 kgid_t krgid, kegid, ksgid;
936 krgid = make_kgid(ns, rgid);
937 kegid = make_kgid(ns, egid);
938 ksgid = make_kgid(ns, sgid);
940 if ((rgid != (gid_t) -1) && !gid_valid(krgid))
942 if ((egid != (gid_t) -1) && !gid_valid(kegid))
944 if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
947 new = prepare_creds();
950 old = current_cred();
953 if (!nsown_capable(CAP_SETGID)) {
954 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
955 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
957 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
958 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
960 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
961 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
965 if (rgid != (gid_t) -1)
967 if (egid != (gid_t) -1)
969 if (sgid != (gid_t) -1)
971 new->fsgid = new->egid;
973 return commit_creds(new);
980 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
982 const struct cred *cred = current_cred();
984 gid_t rgid, egid, sgid;
986 rgid = from_kgid_munged(cred->user_ns, cred->gid);
987 egid = from_kgid_munged(cred->user_ns, cred->egid);
988 sgid = from_kgid_munged(cred->user_ns, cred->sgid);
990 if (!(retval = put_user(rgid, rgidp)) &&
991 !(retval = put_user(egid, egidp)))
992 retval = put_user(sgid, sgidp);
999 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1000 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1001 * whatever uid it wants to). It normally shadows "euid", except when
1002 * explicitly set by setfsuid() or for access..
1004 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
1006 const struct cred *old;
1011 old = current_cred();
1012 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
1014 kuid = make_kuid(old->user_ns, uid);
1015 if (!uid_valid(kuid))
1018 new = prepare_creds();
1022 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
1023 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
1024 nsown_capable(CAP_SETUID)) {
1025 if (!uid_eq(kuid, old->fsuid)) {
1027 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
1041 * Samma på svenska..
1043 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
1045 const struct cred *old;
1050 old = current_cred();
1051 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
1053 kgid = make_kgid(old->user_ns, gid);
1054 if (!gid_valid(kgid))
1057 new = prepare_creds();
1061 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
1062 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
1063 nsown_capable(CAP_SETGID)) {
1064 if (!gid_eq(kgid, old->fsgid)) {
1079 * sys_getpid - return the thread group id of the current process
1081 * Note, despite the name, this returns the tgid not the pid. The tgid and
1082 * the pid are identical unless CLONE_THREAD was specified on clone() in
1083 * which case the tgid is the same in all threads of the same group.
1085 * This is SMP safe as current->tgid does not change.
1087 SYSCALL_DEFINE0(getpid)
1089 return task_tgid_vnr(current);
1092 /* Thread ID - the internal kernel "pid" */
1093 SYSCALL_DEFINE0(gettid)
1095 return task_pid_vnr(current);
1099 * Accessing ->real_parent is not SMP-safe, it could
1100 * change from under us. However, we can use a stale
1101 * value of ->real_parent under rcu_read_lock(), see
1102 * release_task()->call_rcu(delayed_put_task_struct).
1104 SYSCALL_DEFINE0(getppid)
1109 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
1115 SYSCALL_DEFINE0(getuid)
1117 /* Only we change this so SMP safe */
1118 return from_kuid_munged(current_user_ns(), current_uid());
1121 SYSCALL_DEFINE0(geteuid)
1123 /* Only we change this so SMP safe */
1124 return from_kuid_munged(current_user_ns(), current_euid());
1127 SYSCALL_DEFINE0(getgid)
1129 /* Only we change this so SMP safe */
1130 return from_kgid_munged(current_user_ns(), current_gid());
1133 SYSCALL_DEFINE0(getegid)
1135 /* Only we change this so SMP safe */
1136 return from_kgid_munged(current_user_ns(), current_egid());
1139 void do_sys_times(struct tms *tms)
1141 cputime_t tgutime, tgstime, cutime, cstime;
1143 spin_lock_irq(¤t->sighand->siglock);
1144 thread_group_cputime_adjusted(current, &tgutime, &tgstime);
1145 cutime = current->signal->cutime;
1146 cstime = current->signal->cstime;
1147 spin_unlock_irq(¤t->sighand->siglock);
1148 tms->tms_utime = cputime_to_clock_t(tgutime);
1149 tms->tms_stime = cputime_to_clock_t(tgstime);
1150 tms->tms_cutime = cputime_to_clock_t(cutime);
1151 tms->tms_cstime = cputime_to_clock_t(cstime);
1154 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
1160 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1163 force_successful_syscall_return();
1164 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1168 * This needs some heavy checking ...
1169 * I just haven't the stomach for it. I also don't fully
1170 * understand sessions/pgrp etc. Let somebody who does explain it.
1172 * OK, I think I have the protection semantics right.... this is really
1173 * only important on a multi-user system anyway, to make sure one user
1174 * can't send a signal to a process owned by another. -TYT, 12/12/91
1176 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1179 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1181 struct task_struct *p;
1182 struct task_struct *group_leader = current->group_leader;
1187 pid = task_pid_vnr(group_leader);
1194 /* From this point forward we keep holding onto the tasklist lock
1195 * so that our parent does not change from under us. -DaveM
1197 write_lock_irq(&tasklist_lock);
1200 p = find_task_by_vpid(pid);
1205 if (!thread_group_leader(p))
1208 if (same_thread_group(p->real_parent, group_leader)) {
1210 if (task_session(p) != task_session(group_leader))
1217 if (p != group_leader)
1222 if (p->signal->leader)
1227 struct task_struct *g;
1229 pgrp = find_vpid(pgid);
1230 g = pid_task(pgrp, PIDTYPE_PGID);
1231 if (!g || task_session(g) != task_session(group_leader))
1235 err = security_task_setpgid(p, pgid);
1239 if (task_pgrp(p) != pgrp)
1240 change_pid(p, PIDTYPE_PGID, pgrp);
1244 /* All paths lead to here, thus we are safe. -DaveM */
1245 write_unlock_irq(&tasklist_lock);
1250 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1252 struct task_struct *p;
1258 grp = task_pgrp(current);
1261 p = find_task_by_vpid(pid);
1268 retval = security_task_getpgid(p);
1272 retval = pid_vnr(grp);
1278 #ifdef __ARCH_WANT_SYS_GETPGRP
1280 SYSCALL_DEFINE0(getpgrp)
1282 return sys_getpgid(0);
1287 SYSCALL_DEFINE1(getsid, pid_t, pid)
1289 struct task_struct *p;
1295 sid = task_session(current);
1298 p = find_task_by_vpid(pid);
1301 sid = task_session(p);
1305 retval = security_task_getsid(p);
1309 retval = pid_vnr(sid);
1315 SYSCALL_DEFINE0(setsid)
1317 struct task_struct *group_leader = current->group_leader;
1318 struct pid *sid = task_pid(group_leader);
1319 pid_t session = pid_vnr(sid);
1322 write_lock_irq(&tasklist_lock);
1323 /* Fail if I am already a session leader */
1324 if (group_leader->signal->leader)
1327 /* Fail if a process group id already exists that equals the
1328 * proposed session id.
1330 if (pid_task(sid, PIDTYPE_PGID))
1333 group_leader->signal->leader = 1;
1334 __set_special_pids(sid);
1336 proc_clear_tty(group_leader);
1340 write_unlock_irq(&tasklist_lock);
1342 proc_sid_connector(group_leader);
1343 sched_autogroup_create_attach(group_leader);
1348 DECLARE_RWSEM(uts_sem);
1350 #ifdef COMPAT_UTS_MACHINE
1351 #define override_architecture(name) \
1352 (personality(current->personality) == PER_LINUX32 && \
1353 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1354 sizeof(COMPAT_UTS_MACHINE)))
1356 #define override_architecture(name) 0
1360 * Work around broken programs that cannot handle "Linux 3.0".
1361 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1363 static int override_release(char __user *release, size_t len)
1367 if (current->personality & UNAME26) {
1368 const char *rest = UTS_RELEASE;
1369 char buf[65] = { 0 };
1375 if (*rest == '.' && ++ndots >= 3)
1377 if (!isdigit(*rest) && *rest != '.')
1381 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
1382 copy = clamp_t(size_t, len, 1, sizeof(buf));
1383 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1384 ret = copy_to_user(release, buf, copy + 1);
1389 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1393 down_read(&uts_sem);
1394 if (copy_to_user(name, utsname(), sizeof *name))
1398 if (!errno && override_release(name->release, sizeof(name->release)))
1400 if (!errno && override_architecture(name))
1405 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1409 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1416 down_read(&uts_sem);
1417 if (copy_to_user(name, utsname(), sizeof(*name)))
1421 if (!error && override_release(name->release, sizeof(name->release)))
1423 if (!error && override_architecture(name))
1428 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1434 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1437 down_read(&uts_sem);
1438 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1440 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1441 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1443 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1444 error |= __copy_to_user(&name->release, &utsname()->release,
1446 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1447 error |= __copy_to_user(&name->version, &utsname()->version,
1449 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1450 error |= __copy_to_user(&name->machine, &utsname()->machine,
1452 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1455 if (!error && override_architecture(name))
1457 if (!error && override_release(name->release, sizeof(name->release)))
1459 return error ? -EFAULT : 0;
1463 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1466 char tmp[__NEW_UTS_LEN];
1468 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1471 if (len < 0 || len > __NEW_UTS_LEN)
1473 down_write(&uts_sem);
1475 if (!copy_from_user(tmp, name, len)) {
1476 struct new_utsname *u = utsname();
1478 memcpy(u->nodename, tmp, len);
1479 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1481 uts_proc_notify(UTS_PROC_HOSTNAME);
1487 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1489 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1492 struct new_utsname *u;
1496 down_read(&uts_sem);
1498 i = 1 + strlen(u->nodename);
1502 if (copy_to_user(name, u->nodename, i))
1511 * Only setdomainname; getdomainname can be implemented by calling
1514 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1517 char tmp[__NEW_UTS_LEN];
1519 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1521 if (len < 0 || len > __NEW_UTS_LEN)
1524 down_write(&uts_sem);
1526 if (!copy_from_user(tmp, name, len)) {
1527 struct new_utsname *u = utsname();
1529 memcpy(u->domainname, tmp, len);
1530 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1532 uts_proc_notify(UTS_PROC_DOMAINNAME);
1538 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1540 struct rlimit value;
1543 ret = do_prlimit(current, resource, NULL, &value);
1545 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1550 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1553 * Back compatibility for getrlimit. Needed for some apps.
1556 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1557 struct rlimit __user *, rlim)
1560 if (resource >= RLIM_NLIMITS)
1563 task_lock(current->group_leader);
1564 x = current->signal->rlim[resource];
1565 task_unlock(current->group_leader);
1566 if (x.rlim_cur > 0x7FFFFFFF)
1567 x.rlim_cur = 0x7FFFFFFF;
1568 if (x.rlim_max > 0x7FFFFFFF)
1569 x.rlim_max = 0x7FFFFFFF;
1570 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1575 static inline bool rlim64_is_infinity(__u64 rlim64)
1577 #if BITS_PER_LONG < 64
1578 return rlim64 >= ULONG_MAX;
1580 return rlim64 == RLIM64_INFINITY;
1584 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1586 if (rlim->rlim_cur == RLIM_INFINITY)
1587 rlim64->rlim_cur = RLIM64_INFINITY;
1589 rlim64->rlim_cur = rlim->rlim_cur;
1590 if (rlim->rlim_max == RLIM_INFINITY)
1591 rlim64->rlim_max = RLIM64_INFINITY;
1593 rlim64->rlim_max = rlim->rlim_max;
1596 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1598 if (rlim64_is_infinity(rlim64->rlim_cur))
1599 rlim->rlim_cur = RLIM_INFINITY;
1601 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1602 if (rlim64_is_infinity(rlim64->rlim_max))
1603 rlim->rlim_max = RLIM_INFINITY;
1605 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1608 /* make sure you are allowed to change @tsk limits before calling this */
1609 int do_prlimit(struct task_struct *tsk, unsigned int resource,
1610 struct rlimit *new_rlim, struct rlimit *old_rlim)
1612 struct rlimit *rlim;
1615 if (resource >= RLIM_NLIMITS)
1618 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1620 if (resource == RLIMIT_NOFILE &&
1621 new_rlim->rlim_max > sysctl_nr_open)
1625 /* protect tsk->signal and tsk->sighand from disappearing */
1626 read_lock(&tasklist_lock);
1627 if (!tsk->sighand) {
1632 rlim = tsk->signal->rlim + resource;
1633 task_lock(tsk->group_leader);
1635 /* Keep the capable check against init_user_ns until
1636 cgroups can contain all limits */
1637 if (new_rlim->rlim_max > rlim->rlim_max &&
1638 !capable(CAP_SYS_RESOURCE))
1641 retval = security_task_setrlimit(tsk->group_leader,
1642 resource, new_rlim);
1643 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1645 * The caller is asking for an immediate RLIMIT_CPU
1646 * expiry. But we use the zero value to mean "it was
1647 * never set". So let's cheat and make it one second
1650 new_rlim->rlim_cur = 1;
1659 task_unlock(tsk->group_leader);
1662 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1663 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1664 * very long-standing error, and fixing it now risks breakage of
1665 * applications, so we live with it
1667 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1668 new_rlim->rlim_cur != RLIM_INFINITY)
1669 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
1671 read_unlock(&tasklist_lock);
1675 /* rcu lock must be held */
1676 static int check_prlimit_permission(struct task_struct *task)
1678 const struct cred *cred = current_cred(), *tcred;
1680 if (current == task)
1683 tcred = __task_cred(task);
1684 if (uid_eq(cred->uid, tcred->euid) &&
1685 uid_eq(cred->uid, tcred->suid) &&
1686 uid_eq(cred->uid, tcred->uid) &&
1687 gid_eq(cred->gid, tcred->egid) &&
1688 gid_eq(cred->gid, tcred->sgid) &&
1689 gid_eq(cred->gid, tcred->gid))
1691 if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1697 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1698 const struct rlimit64 __user *, new_rlim,
1699 struct rlimit64 __user *, old_rlim)
1701 struct rlimit64 old64, new64;
1702 struct rlimit old, new;
1703 struct task_struct *tsk;
1707 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1709 rlim64_to_rlim(&new64, &new);
1713 tsk = pid ? find_task_by_vpid(pid) : current;
1718 ret = check_prlimit_permission(tsk);
1723 get_task_struct(tsk);
1726 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1727 old_rlim ? &old : NULL);
1729 if (!ret && old_rlim) {
1730 rlim_to_rlim64(&old, &old64);
1731 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1735 put_task_struct(tsk);
1739 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1741 struct rlimit new_rlim;
1743 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1745 return do_prlimit(current, resource, &new_rlim, NULL);
1749 * It would make sense to put struct rusage in the task_struct,
1750 * except that would make the task_struct be *really big*. After
1751 * task_struct gets moved into malloc'ed memory, it would
1752 * make sense to do this. It will make moving the rest of the information
1753 * a lot simpler! (Which we're not doing right now because we're not
1754 * measuring them yet).
1756 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1757 * races with threads incrementing their own counters. But since word
1758 * reads are atomic, we either get new values or old values and we don't
1759 * care which for the sums. We always take the siglock to protect reading
1760 * the c* fields from p->signal from races with exit.c updating those
1761 * fields when reaping, so a sample either gets all the additions of a
1762 * given child after it's reaped, or none so this sample is before reaping.
1765 * We need to take the siglock for CHILDEREN, SELF and BOTH
1766 * for the cases current multithreaded, non-current single threaded
1767 * non-current multithreaded. Thread traversal is now safe with
1769 * Strictly speaking, we donot need to take the siglock if we are current and
1770 * single threaded, as no one else can take our signal_struct away, no one
1771 * else can reap the children to update signal->c* counters, and no one else
1772 * can race with the signal-> fields. If we do not take any lock, the
1773 * signal-> fields could be read out of order while another thread was just
1774 * exiting. So we should place a read memory barrier when we avoid the lock.
1775 * On the writer side, write memory barrier is implied in __exit_signal
1776 * as __exit_signal releases the siglock spinlock after updating the signal->
1777 * fields. But we don't do this yet to keep things simple.
1781 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1783 r->ru_nvcsw += t->nvcsw;
1784 r->ru_nivcsw += t->nivcsw;
1785 r->ru_minflt += t->min_flt;
1786 r->ru_majflt += t->maj_flt;
1787 r->ru_inblock += task_io_get_inblock(t);
1788 r->ru_oublock += task_io_get_oublock(t);
1791 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1793 struct task_struct *t;
1794 unsigned long flags;
1795 cputime_t tgutime, tgstime, utime, stime;
1796 unsigned long maxrss = 0;
1798 memset((char *) r, 0, sizeof *r);
1801 if (who == RUSAGE_THREAD) {
1802 task_cputime_adjusted(current, &utime, &stime);
1803 accumulate_thread_rusage(p, r);
1804 maxrss = p->signal->maxrss;
1808 if (!lock_task_sighand(p, &flags))
1813 case RUSAGE_CHILDREN:
1814 utime = p->signal->cutime;
1815 stime = p->signal->cstime;
1816 r->ru_nvcsw = p->signal->cnvcsw;
1817 r->ru_nivcsw = p->signal->cnivcsw;
1818 r->ru_minflt = p->signal->cmin_flt;
1819 r->ru_majflt = p->signal->cmaj_flt;
1820 r->ru_inblock = p->signal->cinblock;
1821 r->ru_oublock = p->signal->coublock;
1822 maxrss = p->signal->cmaxrss;
1824 if (who == RUSAGE_CHILDREN)
1828 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1831 r->ru_nvcsw += p->signal->nvcsw;
1832 r->ru_nivcsw += p->signal->nivcsw;
1833 r->ru_minflt += p->signal->min_flt;
1834 r->ru_majflt += p->signal->maj_flt;
1835 r->ru_inblock += p->signal->inblock;
1836 r->ru_oublock += p->signal->oublock;
1837 if (maxrss < p->signal->maxrss)
1838 maxrss = p->signal->maxrss;
1841 accumulate_thread_rusage(t, r);
1849 unlock_task_sighand(p, &flags);
1852 cputime_to_timeval(utime, &r->ru_utime);
1853 cputime_to_timeval(stime, &r->ru_stime);
1855 if (who != RUSAGE_CHILDREN) {
1856 struct mm_struct *mm = get_task_mm(p);
1858 setmax_mm_hiwater_rss(&maxrss, mm);
1862 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1865 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1868 k_getrusage(p, who, &r);
1869 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1872 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1874 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1875 who != RUSAGE_THREAD)
1877 return getrusage(current, who, ru);
1880 #ifdef CONFIG_COMPAT
1881 COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1885 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1886 who != RUSAGE_THREAD)
1889 k_getrusage(current, who, &r);
1890 return put_compat_rusage(&r, ru);
1894 SYSCALL_DEFINE1(umask, int, mask)
1896 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1900 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1903 struct inode *inode;
1910 inode = file_inode(exe.file);
1913 * Because the original mm->exe_file points to executable file, make
1914 * sure that this one is executable as well, to avoid breaking an
1918 if (!S_ISREG(inode->i_mode) ||
1919 exe.file->f_path.mnt->mnt_flags & MNT_NOEXEC)
1922 err = inode_permission(inode, MAY_EXEC);
1926 down_write(&mm->mmap_sem);
1929 * Forbid mm->exe_file change if old file still mapped.
1933 struct vm_area_struct *vma;
1935 for (vma = mm->mmap; vma; vma = vma->vm_next)
1937 path_equal(&vma->vm_file->f_path,
1938 &mm->exe_file->f_path))
1943 * The symlink can be changed only once, just to disallow arbitrary
1944 * transitions malicious software might bring in. This means one
1945 * could make a snapshot over all processes running and monitor
1946 * /proc/pid/exe changes to notice unusual activity if needed.
1949 if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
1953 set_mm_exe_file(mm, exe.file); /* this grabs a reference to exe.file */
1955 up_write(&mm->mmap_sem);
1962 static int prctl_set_mm(int opt, unsigned long addr,
1963 unsigned long arg4, unsigned long arg5)
1965 unsigned long rlim = rlimit(RLIMIT_DATA);
1966 struct mm_struct *mm = current->mm;
1967 struct vm_area_struct *vma;
1970 if (arg5 || (arg4 && opt != PR_SET_MM_AUXV))
1973 if (!capable(CAP_SYS_RESOURCE))
1976 if (opt == PR_SET_MM_EXE_FILE)
1977 return prctl_set_mm_exe_file(mm, (unsigned int)addr);
1979 if (addr >= TASK_SIZE || addr < mmap_min_addr)
1984 down_read(&mm->mmap_sem);
1985 vma = find_vma(mm, addr);
1988 case PR_SET_MM_START_CODE:
1989 mm->start_code = addr;
1991 case PR_SET_MM_END_CODE:
1992 mm->end_code = addr;
1994 case PR_SET_MM_START_DATA:
1995 mm->start_data = addr;
1997 case PR_SET_MM_END_DATA:
1998 mm->end_data = addr;
2001 case PR_SET_MM_START_BRK:
2002 if (addr <= mm->end_data)
2005 if (rlim < RLIM_INFINITY &&
2007 (mm->end_data - mm->start_data) > rlim)
2010 mm->start_brk = addr;
2014 if (addr <= mm->end_data)
2017 if (rlim < RLIM_INFINITY &&
2018 (addr - mm->start_brk) +
2019 (mm->end_data - mm->start_data) > rlim)
2026 * If command line arguments and environment
2027 * are placed somewhere else on stack, we can
2028 * set them up here, ARG_START/END to setup
2029 * command line argumets and ENV_START/END
2032 case PR_SET_MM_START_STACK:
2033 case PR_SET_MM_ARG_START:
2034 case PR_SET_MM_ARG_END:
2035 case PR_SET_MM_ENV_START:
2036 case PR_SET_MM_ENV_END:
2041 if (opt == PR_SET_MM_START_STACK)
2042 mm->start_stack = addr;
2043 else if (opt == PR_SET_MM_ARG_START)
2044 mm->arg_start = addr;
2045 else if (opt == PR_SET_MM_ARG_END)
2047 else if (opt == PR_SET_MM_ENV_START)
2048 mm->env_start = addr;
2049 else if (opt == PR_SET_MM_ENV_END)
2054 * This doesn't move auxiliary vector itself
2055 * since it's pinned to mm_struct, but allow
2056 * to fill vector with new values. It's up
2057 * to a caller to provide sane values here
2058 * otherwise user space tools which use this
2059 * vector might be unhappy.
2061 case PR_SET_MM_AUXV: {
2062 unsigned long user_auxv[AT_VECTOR_SIZE];
2064 if (arg4 > sizeof(user_auxv))
2066 up_read(&mm->mmap_sem);
2068 if (copy_from_user(user_auxv, (const void __user *)addr, arg4))
2071 /* Make sure the last entry is always AT_NULL */
2072 user_auxv[AT_VECTOR_SIZE - 2] = 0;
2073 user_auxv[AT_VECTOR_SIZE - 1] = 0;
2075 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2078 memcpy(mm->saved_auxv, user_auxv, arg4);
2079 task_unlock(current);
2089 up_read(&mm->mmap_sem);
2093 #ifdef CONFIG_CHECKPOINT_RESTORE
2094 static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2096 return put_user(me->clear_child_tid, tid_addr);
2099 static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
2106 static int prctl_update_vma_anon_name(struct vm_area_struct *vma,
2107 struct vm_area_struct **prev,
2108 unsigned long start, unsigned long end,
2109 const char __user *name_addr)
2111 struct mm_struct * mm = vma->vm_mm;
2115 if (name_addr == vma_get_anon_name(vma)) {
2120 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2121 *prev = vma_merge(mm, *prev, start, end, vma->vm_flags, vma->anon_vma,
2122 vma->vm_file, pgoff, vma_policy(vma),
2131 if (start != vma->vm_start) {
2132 error = split_vma(mm, vma, start, 1);
2137 if (end != vma->vm_end) {
2138 error = split_vma(mm, vma, end, 0);
2145 vma->shared.anon_name = name_addr;
2148 if (error == -ENOMEM)
2153 static int prctl_set_vma_anon_name(unsigned long start, unsigned long end,
2157 struct vm_area_struct * vma, *prev;
2158 int unmapped_error = 0;
2159 int error = -EINVAL;
2162 * If the interval [start,end) covers some unmapped address
2163 * ranges, just ignore them, but return -ENOMEM at the end.
2164 * - this matches the handling in madvise.
2166 vma = find_vma_prev(current->mm, start, &prev);
2167 if (vma && start > vma->vm_start)
2171 /* Still start < end. */
2176 /* Here start < (end|vma->vm_end). */
2177 if (start < vma->vm_start) {
2178 unmapped_error = -ENOMEM;
2179 start = vma->vm_start;
2184 /* Here vma->vm_start <= start < (end|vma->vm_end) */
2189 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
2190 error = prctl_update_vma_anon_name(vma, &prev, start, end,
2191 (const char __user *)arg);
2195 if (prev && start < prev->vm_end)
2196 start = prev->vm_end;
2197 error = unmapped_error;
2201 vma = prev->vm_next;
2202 else /* madvise_remove dropped mmap_sem */
2203 vma = find_vma(current->mm, start);
2207 static int prctl_set_vma(unsigned long opt, unsigned long start,
2208 unsigned long len_in, unsigned long arg)
2210 struct mm_struct *mm = current->mm;
2215 if (start & ~PAGE_MASK)
2217 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
2219 /* Check to see whether len was rounded up from small -ve to zero */
2230 down_write(&mm->mmap_sem);
2233 case PR_SET_VMA_ANON_NAME:
2234 error = prctl_set_vma_anon_name(start, end, arg);
2240 up_write(&mm->mmap_sem);
2244 #else /* CONFIG_MMU */
2245 static int prctl_set_vma(unsigned long opt, unsigned long start,
2246 unsigned long len_in, unsigned long arg)
2252 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2253 unsigned long, arg4, unsigned long, arg5)
2255 struct task_struct *me = current;
2256 struct task_struct *tsk;
2257 unsigned char comm[sizeof(me->comm)];
2260 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2261 if (error != -ENOSYS)
2266 case PR_SET_PDEATHSIG:
2267 if (!valid_signal(arg2)) {
2271 me->pdeath_signal = arg2;
2273 case PR_GET_PDEATHSIG:
2274 error = put_user(me->pdeath_signal, (int __user *)arg2);
2276 case PR_GET_DUMPABLE:
2277 error = get_dumpable(me->mm);
2279 case PR_SET_DUMPABLE:
2280 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2284 set_dumpable(me->mm, arg2);
2287 case PR_SET_UNALIGN:
2288 error = SET_UNALIGN_CTL(me, arg2);
2290 case PR_GET_UNALIGN:
2291 error = GET_UNALIGN_CTL(me, arg2);
2294 error = SET_FPEMU_CTL(me, arg2);
2297 error = GET_FPEMU_CTL(me, arg2);
2300 error = SET_FPEXC_CTL(me, arg2);
2303 error = GET_FPEXC_CTL(me, arg2);
2306 error = PR_TIMING_STATISTICAL;
2309 if (arg2 != PR_TIMING_STATISTICAL)
2313 comm[sizeof(me->comm) - 1] = 0;
2314 if (strncpy_from_user(comm, (char __user *)arg2,
2315 sizeof(me->comm) - 1) < 0)
2317 set_task_comm(me, comm);
2318 proc_comm_connector(me);
2321 get_task_comm(comm, me);
2322 if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2326 error = GET_ENDIAN(me, arg2);
2329 error = SET_ENDIAN(me, arg2);
2331 case PR_GET_SECCOMP:
2332 error = prctl_get_seccomp();
2334 case PR_SET_SECCOMP:
2335 error = prctl_set_seccomp(arg2, (char __user *)arg3);
2338 error = GET_TSC_CTL(arg2);
2341 error = SET_TSC_CTL(arg2);
2343 case PR_TASK_PERF_EVENTS_DISABLE:
2344 error = perf_event_task_disable();
2346 case PR_TASK_PERF_EVENTS_ENABLE:
2347 error = perf_event_task_enable();
2349 case PR_GET_TIMERSLACK:
2350 error = current->timer_slack_ns;
2352 case PR_SET_TIMERSLACK:
2354 current->timer_slack_ns =
2355 current->default_timer_slack_ns;
2357 current->timer_slack_ns = arg2;
2363 case PR_MCE_KILL_CLEAR:
2366 current->flags &= ~PF_MCE_PROCESS;
2368 case PR_MCE_KILL_SET:
2369 current->flags |= PF_MCE_PROCESS;
2370 if (arg3 == PR_MCE_KILL_EARLY)
2371 current->flags |= PF_MCE_EARLY;
2372 else if (arg3 == PR_MCE_KILL_LATE)
2373 current->flags &= ~PF_MCE_EARLY;
2374 else if (arg3 == PR_MCE_KILL_DEFAULT)
2376 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2380 case PR_SET_TIMERSLACK_PID:
2381 if (current->pid != (pid_t)arg3 &&
2382 !capable(CAP_SYS_NICE))
2385 tsk = find_task_by_pid_ns((pid_t)arg3, &init_pid_ns);
2390 get_task_struct(tsk);
2393 tsk->timer_slack_ns =
2394 tsk->default_timer_slack_ns;
2396 tsk->timer_slack_ns = arg2;
2397 put_task_struct(tsk);
2404 case PR_MCE_KILL_GET:
2405 if (arg2 | arg3 | arg4 | arg5)
2407 if (current->flags & PF_MCE_PROCESS)
2408 error = (current->flags & PF_MCE_EARLY) ?
2409 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2411 error = PR_MCE_KILL_DEFAULT;
2414 error = prctl_set_mm(arg2, arg3, arg4, arg5);
2416 case PR_GET_TID_ADDRESS:
2417 error = prctl_get_tid_address(me, (int __user **)arg2);
2419 case PR_SET_CHILD_SUBREAPER:
2420 me->signal->is_child_subreaper = !!arg2;
2422 case PR_GET_CHILD_SUBREAPER:
2423 error = put_user(me->signal->is_child_subreaper,
2424 (int __user *)arg2);
2426 case PR_SET_NO_NEW_PRIVS:
2427 if (arg2 != 1 || arg3 || arg4 || arg5)
2430 current->no_new_privs = 1;
2432 case PR_GET_NO_NEW_PRIVS:
2433 if (arg2 || arg3 || arg4 || arg5)
2435 return current->no_new_privs ? 1 : 0;
2437 error = prctl_set_vma(arg2, arg3, arg4, arg5);
2446 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2447 struct getcpu_cache __user *, unused)
2450 int cpu = raw_smp_processor_id();
2452 err |= put_user(cpu, cpup);
2454 err |= put_user(cpu_to_node(cpu), nodep);
2455 return err ? -EFAULT : 0;
2458 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2460 static int __orderly_poweroff(bool force)
2463 static char *envp[] = {
2465 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2470 argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
2472 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
2475 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2476 __func__, poweroff_cmd);
2481 printk(KERN_WARNING "Failed to start orderly shutdown: "
2482 "forcing the issue\n");
2484 * I guess this should try to kick off some daemon to sync and
2485 * poweroff asap. Or not even bother syncing if we're doing an
2486 * emergency shutdown?
2495 static bool poweroff_force;
2497 static void poweroff_work_func(struct work_struct *work)
2499 __orderly_poweroff(poweroff_force);
2502 static DECLARE_WORK(poweroff_work, poweroff_work_func);
2505 * orderly_poweroff - Trigger an orderly system poweroff
2506 * @force: force poweroff if command execution fails
2508 * This may be called from any context to trigger a system shutdown.
2509 * If the orderly shutdown fails, it will force an immediate shutdown.
2511 int orderly_poweroff(bool force)
2513 if (force) /* do not override the pending "true" */
2514 poweroff_force = true;
2515 schedule_work(&poweroff_work);
2518 EXPORT_SYMBOL_GPL(orderly_poweroff);
2521 * do_sysinfo - fill in sysinfo struct
2522 * @info: pointer to buffer to fill
2524 static int do_sysinfo(struct sysinfo *info)
2526 unsigned long mem_total, sav_total;
2527 unsigned int mem_unit, bitcount;
2530 memset(info, 0, sizeof(struct sysinfo));
2533 monotonic_to_bootbased(&tp);
2534 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2536 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2538 info->procs = nr_threads;
2544 * If the sum of all the available memory (i.e. ram + swap)
2545 * is less than can be stored in a 32 bit unsigned long then
2546 * we can be binary compatible with 2.2.x kernels. If not,
2547 * well, in that case 2.2.x was broken anyways...
2549 * -Erik Andersen <andersee@debian.org>
2552 mem_total = info->totalram + info->totalswap;
2553 if (mem_total < info->totalram || mem_total < info->totalswap)
2556 mem_unit = info->mem_unit;
2557 while (mem_unit > 1) {
2560 sav_total = mem_total;
2562 if (mem_total < sav_total)
2567 * If mem_total did not overflow, multiply all memory values by
2568 * info->mem_unit and set it to 1. This leaves things compatible
2569 * with 2.2.x, and also retains compatibility with earlier 2.4.x
2574 info->totalram <<= bitcount;
2575 info->freeram <<= bitcount;
2576 info->sharedram <<= bitcount;
2577 info->bufferram <<= bitcount;
2578 info->totalswap <<= bitcount;
2579 info->freeswap <<= bitcount;
2580 info->totalhigh <<= bitcount;
2581 info->freehigh <<= bitcount;
2587 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2593 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2599 #ifdef CONFIG_COMPAT
2600 struct compat_sysinfo {
2614 char _f[20-2*sizeof(u32)-sizeof(int)];
2617 COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2623 /* Check to see if any memory value is too large for 32-bit and scale
2626 if ((s.totalram >> 32) || (s.totalswap >> 32)) {
2629 while (s.mem_unit < PAGE_SIZE) {
2634 s.totalram >>= bitcount;
2635 s.freeram >>= bitcount;
2636 s.sharedram >>= bitcount;
2637 s.bufferram >>= bitcount;
2638 s.totalswap >>= bitcount;
2639 s.freeswap >>= bitcount;
2640 s.totalhigh >>= bitcount;
2641 s.freehigh >>= bitcount;
2644 if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
2645 __put_user(s.uptime, &info->uptime) ||
2646 __put_user(s.loads[0], &info->loads[0]) ||
2647 __put_user(s.loads[1], &info->loads[1]) ||
2648 __put_user(s.loads[2], &info->loads[2]) ||
2649 __put_user(s.totalram, &info->totalram) ||
2650 __put_user(s.freeram, &info->freeram) ||
2651 __put_user(s.sharedram, &info->sharedram) ||
2652 __put_user(s.bufferram, &info->bufferram) ||
2653 __put_user(s.totalswap, &info->totalswap) ||
2654 __put_user(s.freeswap, &info->freeswap) ||
2655 __put_user(s.procs, &info->procs) ||
2656 __put_user(s.totalhigh, &info->totalhigh) ||
2657 __put_user(s.freehigh, &info->freehigh) ||
2658 __put_user(s.mem_unit, &info->mem_unit))
2663 #endif /* CONFIG_COMPAT */