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/gfp.h>
40 #include <linux/syscore_ops.h>
41 #include <linux/version.h>
42 #include <linux/ctype.h>
44 #include <linux/compat.h>
45 #include <linux/syscalls.h>
46 #include <linux/kprobes.h>
47 #include <linux/user_namespace.h>
49 #include <linux/kmsg_dump.h>
50 /* Move somewhere else to avoid recompiling? */
51 #include <generated/utsrelease.h>
53 #include <asm/uaccess.h>
55 #include <asm/unistd.h>
57 #ifndef SET_UNALIGN_CTL
58 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
60 #ifndef GET_UNALIGN_CTL
61 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
64 # define SET_FPEMU_CTL(a,b) (-EINVAL)
67 # define GET_FPEMU_CTL(a,b) (-EINVAL)
70 # define SET_FPEXC_CTL(a,b) (-EINVAL)
73 # define GET_FPEXC_CTL(a,b) (-EINVAL)
76 # define GET_ENDIAN(a,b) (-EINVAL)
79 # define SET_ENDIAN(a,b) (-EINVAL)
82 # define GET_TSC_CTL(a) (-EINVAL)
85 # define SET_TSC_CTL(a) (-EINVAL)
89 * this is where the system-wide overflow UID and GID are defined, for
90 * architectures that now have 32-bit UID/GID but didn't in the past
93 int overflowuid = DEFAULT_OVERFLOWUID;
94 int overflowgid = DEFAULT_OVERFLOWGID;
97 EXPORT_SYMBOL(overflowuid);
98 EXPORT_SYMBOL(overflowgid);
102 * the same as above, but for filesystems which can only store a 16-bit
103 * UID and GID. as such, this is needed on all architectures
106 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
107 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
109 EXPORT_SYMBOL(fs_overflowuid);
110 EXPORT_SYMBOL(fs_overflowgid);
113 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
118 EXPORT_SYMBOL(cad_pid);
121 * If set, this is used for preparing the system to power off.
124 void (*pm_power_off_prepare)(void);
127 * Returns true if current's euid is same as p's uid or euid,
128 * or has CAP_SYS_NICE to p's user_ns.
130 * Called with rcu_read_lock, creds are safe
132 static bool set_one_prio_perm(struct task_struct *p)
134 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
136 if (pcred->user->user_ns == cred->user->user_ns &&
137 (pcred->uid == cred->euid ||
138 pcred->euid == cred->euid))
140 if (ns_capable(pcred->user->user_ns, CAP_SYS_NICE))
146 * set the priority of a task
147 * - the caller must hold the RCU read lock
149 static int set_one_prio(struct task_struct *p, int niceval, int error)
153 if (!set_one_prio_perm(p)) {
157 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
161 no_nice = security_task_setnice(p, niceval);
168 set_user_nice(p, niceval);
173 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
175 struct task_struct *g, *p;
176 struct user_struct *user;
177 const struct cred *cred = current_cred();
181 if (which > PRIO_USER || which < PRIO_PROCESS)
184 /* normalize: avoid signed division (rounding problems) */
192 read_lock(&tasklist_lock);
196 p = find_task_by_vpid(who);
200 error = set_one_prio(p, niceval, error);
204 pgrp = find_vpid(who);
206 pgrp = task_pgrp(current);
207 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
208 error = set_one_prio(p, niceval, error);
209 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
212 user = (struct user_struct *) cred->user;
215 else if ((who != cred->uid) &&
216 !(user = find_user(who)))
217 goto out_unlock; /* No processes for this user */
219 do_each_thread(g, p) {
220 if (__task_cred(p)->uid == who)
221 error = set_one_prio(p, niceval, error);
222 } while_each_thread(g, p);
223 if (who != cred->uid)
224 free_uid(user); /* For find_user() */
228 read_unlock(&tasklist_lock);
235 * Ugh. To avoid negative return values, "getpriority()" will
236 * not return the normal nice-value, but a negated value that
237 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
238 * to stay compatible.
240 SYSCALL_DEFINE2(getpriority, int, which, int, who)
242 struct task_struct *g, *p;
243 struct user_struct *user;
244 const struct cred *cred = current_cred();
245 long niceval, retval = -ESRCH;
248 if (which > PRIO_USER || which < PRIO_PROCESS)
252 read_lock(&tasklist_lock);
256 p = find_task_by_vpid(who);
260 niceval = 20 - task_nice(p);
261 if (niceval > retval)
267 pgrp = find_vpid(who);
269 pgrp = task_pgrp(current);
270 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
271 niceval = 20 - task_nice(p);
272 if (niceval > retval)
274 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
277 user = (struct user_struct *) cred->user;
280 else if ((who != cred->uid) &&
281 !(user = find_user(who)))
282 goto out_unlock; /* No processes for this user */
284 do_each_thread(g, p) {
285 if (__task_cred(p)->uid == who) {
286 niceval = 20 - task_nice(p);
287 if (niceval > retval)
290 } while_each_thread(g, p);
291 if (who != cred->uid)
292 free_uid(user); /* for find_user() */
296 read_unlock(&tasklist_lock);
303 * emergency_restart - reboot the system
305 * Without shutting down any hardware or taking any locks
306 * reboot the system. This is called when we know we are in
307 * trouble so this is our best effort to reboot. This is
308 * safe to call in interrupt context.
310 void emergency_restart(void)
312 kmsg_dump(KMSG_DUMP_EMERG);
313 machine_emergency_restart();
315 EXPORT_SYMBOL_GPL(emergency_restart);
317 void kernel_restart_prepare(char *cmd)
319 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
320 system_state = SYSTEM_RESTART;
321 usermodehelper_disable();
326 * register_reboot_notifier - Register function to be called at reboot time
327 * @nb: Info about notifier function to be called
329 * Registers a function with the list of functions
330 * to be called at reboot time.
332 * Currently always returns zero, as blocking_notifier_chain_register()
333 * always returns zero.
335 int register_reboot_notifier(struct notifier_block *nb)
337 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
339 EXPORT_SYMBOL(register_reboot_notifier);
342 * unregister_reboot_notifier - Unregister previously registered reboot notifier
343 * @nb: Hook to be unregistered
345 * Unregisters a previously registered reboot
348 * Returns zero on success, or %-ENOENT on failure.
350 int unregister_reboot_notifier(struct notifier_block *nb)
352 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
354 EXPORT_SYMBOL(unregister_reboot_notifier);
356 /* Add backwards compatibility for stable trees. */
357 #ifndef PF_NO_SETAFFINITY
358 #define PF_NO_SETAFFINITY PF_THREAD_BOUND
361 static void migrate_to_reboot_cpu(void)
363 /* The boot cpu is always logical cpu 0 */
366 cpu_hotplug_disable();
368 /* Make certain the cpu I'm about to reboot on is online */
369 if (!cpu_online(cpu))
370 cpu = cpumask_first(cpu_online_mask);
372 /* Prevent races with other tasks migrating this task */
373 current->flags |= PF_NO_SETAFFINITY;
375 /* Make certain I only run on the appropriate processor */
376 set_cpus_allowed_ptr(current, cpumask_of(cpu));
380 * kernel_restart - reboot the system
381 * @cmd: pointer to buffer containing command to execute for restart
384 * Shutdown everything and perform a clean reboot.
385 * This is not safe to call in interrupt context.
387 void kernel_restart(char *cmd)
389 kernel_restart_prepare(cmd);
390 migrate_to_reboot_cpu();
393 printk(KERN_EMERG "Restarting system.\n");
395 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
396 kmsg_dump(KMSG_DUMP_RESTART);
397 machine_restart(cmd);
399 EXPORT_SYMBOL_GPL(kernel_restart);
401 static void kernel_shutdown_prepare(enum system_states state)
403 blocking_notifier_call_chain(&reboot_notifier_list,
404 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
405 system_state = state;
406 usermodehelper_disable();
410 * kernel_halt - halt the system
412 * Shutdown everything and perform a clean system halt.
414 void kernel_halt(void)
416 kernel_shutdown_prepare(SYSTEM_HALT);
417 migrate_to_reboot_cpu();
419 printk(KERN_EMERG "System halted.\n");
420 kmsg_dump(KMSG_DUMP_HALT);
424 EXPORT_SYMBOL_GPL(kernel_halt);
427 * kernel_power_off - power_off the system
429 * Shutdown everything and perform a clean system power_off.
431 void kernel_power_off(void)
433 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
434 if (pm_power_off_prepare)
435 pm_power_off_prepare();
436 migrate_to_reboot_cpu();
438 printk(KERN_EMERG "Power down.\n");
439 kmsg_dump(KMSG_DUMP_POWEROFF);
442 EXPORT_SYMBOL_GPL(kernel_power_off);
444 static DEFINE_MUTEX(reboot_mutex);
447 * Reboot system call: for obvious reasons only root may call it,
448 * and even root needs to set up some magic numbers in the registers
449 * so that some mistake won't make this reboot the whole machine.
450 * You can also set the meaning of the ctrl-alt-del-key here.
452 * reboot doesn't sync: do that yourself before calling this.
454 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
460 /* We only trust the superuser with rebooting the system. */
461 if (!capable(CAP_SYS_BOOT))
464 /* For safety, we require "magic" arguments. */
465 if (magic1 != LINUX_REBOOT_MAGIC1 ||
466 (magic2 != LINUX_REBOOT_MAGIC2 &&
467 magic2 != LINUX_REBOOT_MAGIC2A &&
468 magic2 != LINUX_REBOOT_MAGIC2B &&
469 magic2 != LINUX_REBOOT_MAGIC2C))
472 /* Instead of trying to make the power_off code look like
473 * halt when pm_power_off is not set do it the easy way.
475 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
476 cmd = LINUX_REBOOT_CMD_HALT;
478 mutex_lock(&reboot_mutex);
480 case LINUX_REBOOT_CMD_RESTART:
481 kernel_restart(NULL);
484 case LINUX_REBOOT_CMD_CAD_ON:
488 case LINUX_REBOOT_CMD_CAD_OFF:
492 case LINUX_REBOOT_CMD_HALT:
495 panic("cannot halt");
497 case LINUX_REBOOT_CMD_POWER_OFF:
502 case LINUX_REBOOT_CMD_RESTART2:
503 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
507 buffer[sizeof(buffer) - 1] = '\0';
509 kernel_restart(buffer);
513 case LINUX_REBOOT_CMD_KEXEC:
514 ret = kernel_kexec();
518 #ifdef CONFIG_HIBERNATION
519 case LINUX_REBOOT_CMD_SW_SUSPEND:
528 mutex_unlock(&reboot_mutex);
532 static void deferred_cad(struct work_struct *dummy)
534 kernel_restart(NULL);
538 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
539 * As it's called within an interrupt, it may NOT sync: the only choice
540 * is whether to reboot at once, or just ignore the ctrl-alt-del.
542 void ctrl_alt_del(void)
544 static DECLARE_WORK(cad_work, deferred_cad);
547 schedule_work(&cad_work);
549 kill_cad_pid(SIGINT, 1);
553 * Unprivileged users may change the real gid to the effective gid
554 * or vice versa. (BSD-style)
556 * If you set the real gid at all, or set the effective gid to a value not
557 * equal to the real gid, then the saved gid is set to the new effective gid.
559 * This makes it possible for a setgid program to completely drop its
560 * privileges, which is often a useful assertion to make when you are doing
561 * a security audit over a program.
563 * The general idea is that a program which uses just setregid() will be
564 * 100% compatible with BSD. A program which uses just setgid() will be
565 * 100% compatible with POSIX with saved IDs.
567 * SMP: There are not races, the GIDs are checked only by filesystem
568 * operations (as far as semantic preservation is concerned).
570 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
572 const struct cred *old;
576 new = prepare_creds();
579 old = current_cred();
582 if (rgid != (gid_t) -1) {
583 if (old->gid == rgid ||
585 nsown_capable(CAP_SETGID))
590 if (egid != (gid_t) -1) {
591 if (old->gid == egid ||
594 nsown_capable(CAP_SETGID))
600 if (rgid != (gid_t) -1 ||
601 (egid != (gid_t) -1 && egid != old->gid))
602 new->sgid = new->egid;
603 new->fsgid = new->egid;
605 return commit_creds(new);
613 * setgid() is implemented like SysV w/ SAVED_IDS
615 * SMP: Same implicit races as above.
617 SYSCALL_DEFINE1(setgid, gid_t, gid)
619 const struct cred *old;
623 new = prepare_creds();
626 old = current_cred();
629 if (nsown_capable(CAP_SETGID))
630 new->gid = new->egid = new->sgid = new->fsgid = gid;
631 else if (gid == old->gid || gid == old->sgid)
632 new->egid = new->fsgid = gid;
636 return commit_creds(new);
644 * change the user struct in a credentials set to match the new UID
646 static int set_user(struct cred *new)
648 struct user_struct *new_user;
650 new_user = alloc_uid(current_user_ns(), new->uid);
655 * We don't fail in case of NPROC limit excess here because too many
656 * poorly written programs don't check set*uid() return code, assuming
657 * it never fails if called by root. We may still enforce NPROC limit
658 * for programs doing set*uid()+execve() by harmlessly deferring the
659 * failure to the execve() stage.
661 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
662 new_user != INIT_USER)
663 current->flags |= PF_NPROC_EXCEEDED;
665 current->flags &= ~PF_NPROC_EXCEEDED;
668 new->user = new_user;
673 * Unprivileged users may change the real uid to the effective uid
674 * or vice versa. (BSD-style)
676 * If you set the real uid at all, or set the effective uid to a value not
677 * equal to the real uid, then the saved uid is set to the new effective uid.
679 * This makes it possible for a setuid program to completely drop its
680 * privileges, which is often a useful assertion to make when you are doing
681 * a security audit over a program.
683 * The general idea is that a program which uses just setreuid() will be
684 * 100% compatible with BSD. A program which uses just setuid() will be
685 * 100% compatible with POSIX with saved IDs.
687 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
689 const struct cred *old;
693 new = prepare_creds();
696 old = current_cred();
699 if (ruid != (uid_t) -1) {
701 if (old->uid != ruid &&
703 !nsown_capable(CAP_SETUID))
707 if (euid != (uid_t) -1) {
709 if (old->uid != euid &&
712 !nsown_capable(CAP_SETUID))
716 if (new->uid != old->uid) {
717 retval = set_user(new);
721 if (ruid != (uid_t) -1 ||
722 (euid != (uid_t) -1 && euid != old->uid))
723 new->suid = new->euid;
724 new->fsuid = new->euid;
726 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
730 return commit_creds(new);
738 * setuid() is implemented like SysV with SAVED_IDS
740 * Note that SAVED_ID's is deficient in that a setuid root program
741 * like sendmail, for example, cannot set its uid to be a normal
742 * user and then switch back, because if you're root, setuid() sets
743 * the saved uid too. If you don't like this, blame the bright people
744 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
745 * will allow a root program to temporarily drop privileges and be able to
746 * regain them by swapping the real and effective uid.
748 SYSCALL_DEFINE1(setuid, uid_t, uid)
750 const struct cred *old;
754 new = prepare_creds();
757 old = current_cred();
760 if (nsown_capable(CAP_SETUID)) {
761 new->suid = new->uid = uid;
762 if (uid != old->uid) {
763 retval = set_user(new);
767 } else if (uid != old->uid && uid != new->suid) {
771 new->fsuid = new->euid = uid;
773 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
777 return commit_creds(new);
786 * This function implements a generic ability to update ruid, euid,
787 * and suid. This allows you to implement the 4.4 compatible seteuid().
789 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
791 const struct cred *old;
795 new = prepare_creds();
799 old = current_cred();
802 if (!nsown_capable(CAP_SETUID)) {
803 if (ruid != (uid_t) -1 && ruid != old->uid &&
804 ruid != old->euid && ruid != old->suid)
806 if (euid != (uid_t) -1 && euid != old->uid &&
807 euid != old->euid && euid != old->suid)
809 if (suid != (uid_t) -1 && suid != old->uid &&
810 suid != old->euid && suid != old->suid)
814 if (ruid != (uid_t) -1) {
816 if (ruid != old->uid) {
817 retval = set_user(new);
822 if (euid != (uid_t) -1)
824 if (suid != (uid_t) -1)
826 new->fsuid = new->euid;
828 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
832 return commit_creds(new);
839 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
841 const struct cred *cred = current_cred();
844 if (!(retval = put_user(cred->uid, ruid)) &&
845 !(retval = put_user(cred->euid, euid)))
846 retval = put_user(cred->suid, suid);
852 * Same as above, but for rgid, egid, sgid.
854 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
856 const struct cred *old;
860 new = prepare_creds();
863 old = current_cred();
866 if (!nsown_capable(CAP_SETGID)) {
867 if (rgid != (gid_t) -1 && rgid != old->gid &&
868 rgid != old->egid && rgid != old->sgid)
870 if (egid != (gid_t) -1 && egid != old->gid &&
871 egid != old->egid && egid != old->sgid)
873 if (sgid != (gid_t) -1 && sgid != old->gid &&
874 sgid != old->egid && sgid != old->sgid)
878 if (rgid != (gid_t) -1)
880 if (egid != (gid_t) -1)
882 if (sgid != (gid_t) -1)
884 new->fsgid = new->egid;
886 return commit_creds(new);
893 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
895 const struct cred *cred = current_cred();
898 if (!(retval = put_user(cred->gid, rgid)) &&
899 !(retval = put_user(cred->egid, egid)))
900 retval = put_user(cred->sgid, sgid);
907 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
908 * is used for "access()" and for the NFS daemon (letting nfsd stay at
909 * whatever uid it wants to). It normally shadows "euid", except when
910 * explicitly set by setfsuid() or for access..
912 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
914 const struct cred *old;
918 new = prepare_creds();
920 return current_fsuid();
921 old = current_cred();
922 old_fsuid = old->fsuid;
924 if (uid == old->uid || uid == old->euid ||
925 uid == old->suid || uid == old->fsuid ||
926 nsown_capable(CAP_SETUID)) {
927 if (uid != old_fsuid) {
929 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
943 * Samma på svenska..
945 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
947 const struct cred *old;
951 new = prepare_creds();
953 return current_fsgid();
954 old = current_cred();
955 old_fsgid = old->fsgid;
957 if (gid == old->gid || gid == old->egid ||
958 gid == old->sgid || gid == old->fsgid ||
959 nsown_capable(CAP_SETGID)) {
960 if (gid != old_fsgid) {
974 void do_sys_times(struct tms *tms)
976 cputime_t tgutime, tgstime, cutime, cstime;
978 spin_lock_irq(¤t->sighand->siglock);
979 thread_group_times(current, &tgutime, &tgstime);
980 cutime = current->signal->cutime;
981 cstime = current->signal->cstime;
982 spin_unlock_irq(¤t->sighand->siglock);
983 tms->tms_utime = cputime_to_clock_t(tgutime);
984 tms->tms_stime = cputime_to_clock_t(tgstime);
985 tms->tms_cutime = cputime_to_clock_t(cutime);
986 tms->tms_cstime = cputime_to_clock_t(cstime);
989 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
995 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
998 force_successful_syscall_return();
999 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1003 * This needs some heavy checking ...
1004 * I just haven't the stomach for it. I also don't fully
1005 * understand sessions/pgrp etc. Let somebody who does explain it.
1007 * OK, I think I have the protection semantics right.... this is really
1008 * only important on a multi-user system anyway, to make sure one user
1009 * can't send a signal to a process owned by another. -TYT, 12/12/91
1011 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1014 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1016 struct task_struct *p;
1017 struct task_struct *group_leader = current->group_leader;
1022 pid = task_pid_vnr(group_leader);
1029 /* From this point forward we keep holding onto the tasklist lock
1030 * so that our parent does not change from under us. -DaveM
1032 write_lock_irq(&tasklist_lock);
1035 p = find_task_by_vpid(pid);
1040 if (!thread_group_leader(p))
1043 if (same_thread_group(p->real_parent, group_leader)) {
1045 if (task_session(p) != task_session(group_leader))
1052 if (p != group_leader)
1057 if (p->signal->leader)
1062 struct task_struct *g;
1064 pgrp = find_vpid(pgid);
1065 g = pid_task(pgrp, PIDTYPE_PGID);
1066 if (!g || task_session(g) != task_session(group_leader))
1070 err = security_task_setpgid(p, pgid);
1074 if (task_pgrp(p) != pgrp)
1075 change_pid(p, PIDTYPE_PGID, pgrp);
1079 /* All paths lead to here, thus we are safe. -DaveM */
1080 write_unlock_irq(&tasklist_lock);
1085 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1087 struct task_struct *p;
1093 grp = task_pgrp(current);
1096 p = find_task_by_vpid(pid);
1103 retval = security_task_getpgid(p);
1107 retval = pid_vnr(grp);
1113 #ifdef __ARCH_WANT_SYS_GETPGRP
1115 SYSCALL_DEFINE0(getpgrp)
1117 return sys_getpgid(0);
1122 SYSCALL_DEFINE1(getsid, pid_t, pid)
1124 struct task_struct *p;
1130 sid = task_session(current);
1133 p = find_task_by_vpid(pid);
1136 sid = task_session(p);
1140 retval = security_task_getsid(p);
1144 retval = pid_vnr(sid);
1150 SYSCALL_DEFINE0(setsid)
1152 struct task_struct *group_leader = current->group_leader;
1153 struct pid *sid = task_pid(group_leader);
1154 pid_t session = pid_vnr(sid);
1157 write_lock_irq(&tasklist_lock);
1158 /* Fail if I am already a session leader */
1159 if (group_leader->signal->leader)
1162 /* Fail if a process group id already exists that equals the
1163 * proposed session id.
1165 if (pid_task(sid, PIDTYPE_PGID))
1168 group_leader->signal->leader = 1;
1169 __set_special_pids(sid);
1171 proc_clear_tty(group_leader);
1175 write_unlock_irq(&tasklist_lock);
1177 proc_sid_connector(group_leader);
1178 sched_autogroup_create_attach(group_leader);
1183 DECLARE_RWSEM(uts_sem);
1185 #ifdef COMPAT_UTS_MACHINE
1186 #define override_architecture(name) \
1187 (personality(current->personality) == PER_LINUX32 && \
1188 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1189 sizeof(COMPAT_UTS_MACHINE)))
1191 #define override_architecture(name) 0
1195 * Work around broken programs that cannot handle "Linux 3.0".
1196 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1198 static int override_release(char __user *release, size_t len)
1202 if (current->personality & UNAME26) {
1203 const char *rest = UTS_RELEASE;
1204 char buf[65] = { 0 };
1210 if (*rest == '.' && ++ndots >= 3)
1212 if (!isdigit(*rest) && *rest != '.')
1216 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
1217 copy = clamp_t(size_t, len, 1, sizeof(buf));
1218 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1219 ret = copy_to_user(release, buf, copy + 1);
1224 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1228 down_read(&uts_sem);
1229 if (copy_to_user(name, utsname(), sizeof *name))
1233 if (!errno && override_release(name->release, sizeof(name->release)))
1235 if (!errno && override_architecture(name))
1240 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1244 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1251 down_read(&uts_sem);
1252 if (copy_to_user(name, utsname(), sizeof(*name)))
1256 if (!error && override_release(name->release, sizeof(name->release)))
1258 if (!error && override_architecture(name))
1263 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1269 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1272 down_read(&uts_sem);
1273 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1275 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1276 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1278 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1279 error |= __copy_to_user(&name->release, &utsname()->release,
1281 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1282 error |= __copy_to_user(&name->version, &utsname()->version,
1284 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1285 error |= __copy_to_user(&name->machine, &utsname()->machine,
1287 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1290 if (!error && override_architecture(name))
1292 if (!error && override_release(name->release, sizeof(name->release)))
1294 return error ? -EFAULT : 0;
1298 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1301 char tmp[__NEW_UTS_LEN];
1303 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1306 if (len < 0 || len > __NEW_UTS_LEN)
1308 down_write(&uts_sem);
1310 if (!copy_from_user(tmp, name, len)) {
1311 struct new_utsname *u = utsname();
1313 memcpy(u->nodename, tmp, len);
1314 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1317 uts_proc_notify(UTS_PROC_HOSTNAME);
1322 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1324 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1327 struct new_utsname *u;
1331 down_read(&uts_sem);
1333 i = 1 + strlen(u->nodename);
1337 if (copy_to_user(name, u->nodename, i))
1346 * Only setdomainname; getdomainname can be implemented by calling
1349 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1352 char tmp[__NEW_UTS_LEN];
1354 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1356 if (len < 0 || len > __NEW_UTS_LEN)
1359 down_write(&uts_sem);
1361 if (!copy_from_user(tmp, name, len)) {
1362 struct new_utsname *u = utsname();
1364 memcpy(u->domainname, tmp, len);
1365 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1368 uts_proc_notify(UTS_PROC_DOMAINNAME);
1373 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1375 struct rlimit value;
1378 ret = do_prlimit(current, resource, NULL, &value);
1380 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1385 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1388 * Back compatibility for getrlimit. Needed for some apps.
1391 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1392 struct rlimit __user *, rlim)
1395 if (resource >= RLIM_NLIMITS)
1398 task_lock(current->group_leader);
1399 x = current->signal->rlim[resource];
1400 task_unlock(current->group_leader);
1401 if (x.rlim_cur > 0x7FFFFFFF)
1402 x.rlim_cur = 0x7FFFFFFF;
1403 if (x.rlim_max > 0x7FFFFFFF)
1404 x.rlim_max = 0x7FFFFFFF;
1405 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1410 static inline bool rlim64_is_infinity(__u64 rlim64)
1412 #if BITS_PER_LONG < 64
1413 return rlim64 >= ULONG_MAX;
1415 return rlim64 == RLIM64_INFINITY;
1419 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1421 if (rlim->rlim_cur == RLIM_INFINITY)
1422 rlim64->rlim_cur = RLIM64_INFINITY;
1424 rlim64->rlim_cur = rlim->rlim_cur;
1425 if (rlim->rlim_max == RLIM_INFINITY)
1426 rlim64->rlim_max = RLIM64_INFINITY;
1428 rlim64->rlim_max = rlim->rlim_max;
1431 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1433 if (rlim64_is_infinity(rlim64->rlim_cur))
1434 rlim->rlim_cur = RLIM_INFINITY;
1436 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1437 if (rlim64_is_infinity(rlim64->rlim_max))
1438 rlim->rlim_max = RLIM_INFINITY;
1440 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1443 /* make sure you are allowed to change @tsk limits before calling this */
1444 int do_prlimit(struct task_struct *tsk, unsigned int resource,
1445 struct rlimit *new_rlim, struct rlimit *old_rlim)
1447 struct rlimit *rlim;
1450 if (resource >= RLIM_NLIMITS)
1453 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1455 if (resource == RLIMIT_NOFILE &&
1456 new_rlim->rlim_max > sysctl_nr_open)
1460 /* protect tsk->signal and tsk->sighand from disappearing */
1461 read_lock(&tasklist_lock);
1462 if (!tsk->sighand) {
1467 rlim = tsk->signal->rlim + resource;
1468 task_lock(tsk->group_leader);
1470 /* Keep the capable check against init_user_ns until
1471 cgroups can contain all limits */
1472 if (new_rlim->rlim_max > rlim->rlim_max &&
1473 !capable(CAP_SYS_RESOURCE))
1476 retval = security_task_setrlimit(tsk->group_leader,
1477 resource, new_rlim);
1478 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1480 * The caller is asking for an immediate RLIMIT_CPU
1481 * expiry. But we use the zero value to mean "it was
1482 * never set". So let's cheat and make it one second
1485 new_rlim->rlim_cur = 1;
1494 task_unlock(tsk->group_leader);
1497 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1498 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1499 * very long-standing error, and fixing it now risks breakage of
1500 * applications, so we live with it
1502 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1503 new_rlim->rlim_cur != RLIM_INFINITY)
1504 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
1506 read_unlock(&tasklist_lock);
1510 /* rcu lock must be held */
1511 static int check_prlimit_permission(struct task_struct *task)
1513 const struct cred *cred = current_cred(), *tcred;
1515 if (current == task)
1518 tcred = __task_cred(task);
1519 if (cred->user->user_ns == tcred->user->user_ns &&
1520 (cred->uid == tcred->euid &&
1521 cred->uid == tcred->suid &&
1522 cred->uid == tcred->uid &&
1523 cred->gid == tcred->egid &&
1524 cred->gid == tcred->sgid &&
1525 cred->gid == tcred->gid))
1527 if (ns_capable(tcred->user->user_ns, CAP_SYS_RESOURCE))
1533 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1534 const struct rlimit64 __user *, new_rlim,
1535 struct rlimit64 __user *, old_rlim)
1537 struct rlimit64 old64, new64;
1538 struct rlimit old, new;
1539 struct task_struct *tsk;
1543 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1545 rlim64_to_rlim(&new64, &new);
1549 tsk = pid ? find_task_by_vpid(pid) : current;
1554 ret = check_prlimit_permission(tsk);
1559 get_task_struct(tsk);
1562 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1563 old_rlim ? &old : NULL);
1565 if (!ret && old_rlim) {
1566 rlim_to_rlim64(&old, &old64);
1567 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1571 put_task_struct(tsk);
1575 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1577 struct rlimit new_rlim;
1579 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1581 return do_prlimit(current, resource, &new_rlim, NULL);
1585 * It would make sense to put struct rusage in the task_struct,
1586 * except that would make the task_struct be *really big*. After
1587 * task_struct gets moved into malloc'ed memory, it would
1588 * make sense to do this. It will make moving the rest of the information
1589 * a lot simpler! (Which we're not doing right now because we're not
1590 * measuring them yet).
1592 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1593 * races with threads incrementing their own counters. But since word
1594 * reads are atomic, we either get new values or old values and we don't
1595 * care which for the sums. We always take the siglock to protect reading
1596 * the c* fields from p->signal from races with exit.c updating those
1597 * fields when reaping, so a sample either gets all the additions of a
1598 * given child after it's reaped, or none so this sample is before reaping.
1601 * We need to take the siglock for CHILDEREN, SELF and BOTH
1602 * for the cases current multithreaded, non-current single threaded
1603 * non-current multithreaded. Thread traversal is now safe with
1605 * Strictly speaking, we donot need to take the siglock if we are current and
1606 * single threaded, as no one else can take our signal_struct away, no one
1607 * else can reap the children to update signal->c* counters, and no one else
1608 * can race with the signal-> fields. If we do not take any lock, the
1609 * signal-> fields could be read out of order while another thread was just
1610 * exiting. So we should place a read memory barrier when we avoid the lock.
1611 * On the writer side, write memory barrier is implied in __exit_signal
1612 * as __exit_signal releases the siglock spinlock after updating the signal->
1613 * fields. But we don't do this yet to keep things simple.
1617 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1619 r->ru_nvcsw += t->nvcsw;
1620 r->ru_nivcsw += t->nivcsw;
1621 r->ru_minflt += t->min_flt;
1622 r->ru_majflt += t->maj_flt;
1623 r->ru_inblock += task_io_get_inblock(t);
1624 r->ru_oublock += task_io_get_oublock(t);
1627 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1629 struct task_struct *t;
1630 unsigned long flags;
1631 cputime_t tgutime, tgstime, utime, stime;
1632 unsigned long maxrss = 0;
1634 memset((char *) r, 0, sizeof *r);
1635 utime = stime = cputime_zero;
1637 if (who == RUSAGE_THREAD) {
1638 task_times(current, &utime, &stime);
1639 accumulate_thread_rusage(p, r);
1640 maxrss = p->signal->maxrss;
1644 if (!lock_task_sighand(p, &flags))
1649 case RUSAGE_CHILDREN:
1650 utime = p->signal->cutime;
1651 stime = p->signal->cstime;
1652 r->ru_nvcsw = p->signal->cnvcsw;
1653 r->ru_nivcsw = p->signal->cnivcsw;
1654 r->ru_minflt = p->signal->cmin_flt;
1655 r->ru_majflt = p->signal->cmaj_flt;
1656 r->ru_inblock = p->signal->cinblock;
1657 r->ru_oublock = p->signal->coublock;
1658 maxrss = p->signal->cmaxrss;
1660 if (who == RUSAGE_CHILDREN)
1664 thread_group_times(p, &tgutime, &tgstime);
1665 utime = cputime_add(utime, tgutime);
1666 stime = cputime_add(stime, tgstime);
1667 r->ru_nvcsw += p->signal->nvcsw;
1668 r->ru_nivcsw += p->signal->nivcsw;
1669 r->ru_minflt += p->signal->min_flt;
1670 r->ru_majflt += p->signal->maj_flt;
1671 r->ru_inblock += p->signal->inblock;
1672 r->ru_oublock += p->signal->oublock;
1673 if (maxrss < p->signal->maxrss)
1674 maxrss = p->signal->maxrss;
1677 accumulate_thread_rusage(t, r);
1685 unlock_task_sighand(p, &flags);
1688 cputime_to_timeval(utime, &r->ru_utime);
1689 cputime_to_timeval(stime, &r->ru_stime);
1691 if (who != RUSAGE_CHILDREN) {
1692 struct mm_struct *mm = get_task_mm(p);
1694 setmax_mm_hiwater_rss(&maxrss, mm);
1698 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1701 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1704 k_getrusage(p, who, &r);
1705 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1708 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1710 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1711 who != RUSAGE_THREAD)
1713 return getrusage(current, who, ru);
1716 SYSCALL_DEFINE1(umask, int, mask)
1718 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1722 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1723 unsigned long, arg4, unsigned long, arg5)
1725 struct task_struct *me = current;
1726 unsigned char comm[sizeof(me->comm)];
1729 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1730 if (error != -ENOSYS)
1735 case PR_SET_PDEATHSIG:
1736 if (!valid_signal(arg2)) {
1740 me->pdeath_signal = arg2;
1743 case PR_GET_PDEATHSIG:
1744 error = put_user(me->pdeath_signal, (int __user *)arg2);
1746 case PR_GET_DUMPABLE:
1747 error = get_dumpable(me->mm);
1749 case PR_SET_DUMPABLE:
1750 if (arg2 < 0 || arg2 > 1) {
1754 set_dumpable(me->mm, arg2);
1758 case PR_SET_UNALIGN:
1759 error = SET_UNALIGN_CTL(me, arg2);
1761 case PR_GET_UNALIGN:
1762 error = GET_UNALIGN_CTL(me, arg2);
1765 error = SET_FPEMU_CTL(me, arg2);
1768 error = GET_FPEMU_CTL(me, arg2);
1771 error = SET_FPEXC_CTL(me, arg2);
1774 error = GET_FPEXC_CTL(me, arg2);
1777 error = PR_TIMING_STATISTICAL;
1780 if (arg2 != PR_TIMING_STATISTICAL)
1787 comm[sizeof(me->comm)-1] = 0;
1788 if (strncpy_from_user(comm, (char __user *)arg2,
1789 sizeof(me->comm) - 1) < 0)
1791 set_task_comm(me, comm);
1792 proc_comm_connector(me);
1795 get_task_comm(comm, me);
1796 if (copy_to_user((char __user *)arg2, comm,
1801 error = GET_ENDIAN(me, arg2);
1804 error = SET_ENDIAN(me, arg2);
1807 case PR_GET_SECCOMP:
1808 error = prctl_get_seccomp();
1810 case PR_SET_SECCOMP:
1811 error = prctl_set_seccomp(arg2);
1814 error = GET_TSC_CTL(arg2);
1817 error = SET_TSC_CTL(arg2);
1819 case PR_TASK_PERF_EVENTS_DISABLE:
1820 error = perf_event_task_disable();
1822 case PR_TASK_PERF_EVENTS_ENABLE:
1823 error = perf_event_task_enable();
1825 case PR_GET_TIMERSLACK:
1826 error = current->timer_slack_ns;
1828 case PR_SET_TIMERSLACK:
1830 current->timer_slack_ns =
1831 current->default_timer_slack_ns;
1833 current->timer_slack_ns = arg2;
1840 case PR_MCE_KILL_CLEAR:
1843 current->flags &= ~PF_MCE_PROCESS;
1845 case PR_MCE_KILL_SET:
1846 current->flags |= PF_MCE_PROCESS;
1847 if (arg3 == PR_MCE_KILL_EARLY)
1848 current->flags |= PF_MCE_EARLY;
1849 else if (arg3 == PR_MCE_KILL_LATE)
1850 current->flags &= ~PF_MCE_EARLY;
1851 else if (arg3 == PR_MCE_KILL_DEFAULT)
1853 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
1862 case PR_MCE_KILL_GET:
1863 if (arg2 | arg3 | arg4 | arg5)
1865 if (current->flags & PF_MCE_PROCESS)
1866 error = (current->flags & PF_MCE_EARLY) ?
1867 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
1869 error = PR_MCE_KILL_DEFAULT;
1878 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
1879 struct getcpu_cache __user *, unused)
1882 int cpu = raw_smp_processor_id();
1884 err |= put_user(cpu, cpup);
1886 err |= put_user(cpu_to_node(cpu), nodep);
1887 return err ? -EFAULT : 0;
1890 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1892 static void argv_cleanup(struct subprocess_info *info)
1894 argv_free(info->argv);
1898 * orderly_poweroff - Trigger an orderly system poweroff
1899 * @force: force poweroff if command execution fails
1901 * This may be called from any context to trigger a system shutdown.
1902 * If the orderly shutdown fails, it will force an immediate shutdown.
1904 int orderly_poweroff(bool force)
1907 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1908 static char *envp[] = {
1910 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1914 struct subprocess_info *info;
1917 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1918 __func__, poweroff_cmd);
1922 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
1928 call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
1930 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
1934 printk(KERN_WARNING "Failed to start orderly shutdown: "
1935 "forcing the issue\n");
1937 /* I guess this should try to kick off some daemon to
1938 sync and poweroff asap. Or not even bother syncing
1939 if we're doing an emergency shutdown? */
1946 EXPORT_SYMBOL_GPL(orderly_poweroff);