4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/module.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/perf_event.h>
16 #include <linux/resource.h>
17 #include <linux/kernel.h>
18 #include <linux/kexec.h>
19 #include <linux/workqueue.h>
20 #include <linux/capability.h>
21 #include <linux/device.h>
22 #include <linux/key.h>
23 #include <linux/times.h>
24 #include <linux/posix-timers.h>
25 #include <linux/security.h>
26 #include <linux/dcookies.h>
27 #include <linux/suspend.h>
28 #include <linux/tty.h>
29 #include <linux/signal.h>
30 #include <linux/cn_proc.h>
31 #include <linux/getcpu.h>
32 #include <linux/task_io_accounting_ops.h>
33 #include <linux/seccomp.h>
34 #include <linux/cpu.h>
35 #include <linux/personality.h>
36 #include <linux/ptrace.h>
37 #include <linux/fs_struct.h>
38 #include <linux/gfp.h>
39 #include <linux/syscore_ops.h>
41 #include <linux/compat.h>
42 #include <linux/syscalls.h>
43 #include <linux/kprobes.h>
44 #include <linux/user_namespace.h>
46 #include <linux/kmsg_dump.h>
48 #include <asm/uaccess.h>
50 #include <asm/unistd.h>
52 #ifndef SET_UNALIGN_CTL
53 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
55 #ifndef GET_UNALIGN_CTL
56 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
59 # define SET_FPEMU_CTL(a,b) (-EINVAL)
62 # define GET_FPEMU_CTL(a,b) (-EINVAL)
65 # define SET_FPEXC_CTL(a,b) (-EINVAL)
68 # define GET_FPEXC_CTL(a,b) (-EINVAL)
71 # define GET_ENDIAN(a,b) (-EINVAL)
74 # define SET_ENDIAN(a,b) (-EINVAL)
77 # define GET_TSC_CTL(a) (-EINVAL)
80 # define SET_TSC_CTL(a) (-EINVAL)
84 * this is where the system-wide overflow UID and GID are defined, for
85 * architectures that now have 32-bit UID/GID but didn't in the past
88 int overflowuid = DEFAULT_OVERFLOWUID;
89 int overflowgid = DEFAULT_OVERFLOWGID;
92 EXPORT_SYMBOL(overflowuid);
93 EXPORT_SYMBOL(overflowgid);
97 * the same as above, but for filesystems which can only store a 16-bit
98 * UID and GID. as such, this is needed on all architectures
101 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
102 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
104 EXPORT_SYMBOL(fs_overflowuid);
105 EXPORT_SYMBOL(fs_overflowgid);
108 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
113 EXPORT_SYMBOL(cad_pid);
116 * If set, this is used for preparing the system to power off.
119 void (*pm_power_off_prepare)(void);
122 * Returns true if current's euid is same as p's uid or euid,
123 * or has CAP_SYS_NICE to p's user_ns.
125 * Called with rcu_read_lock, creds are safe
127 static bool set_one_prio_perm(struct task_struct *p)
129 const struct cred *cred = current_cred(), *pcred = __task_cred(p);
131 if (pcred->user->user_ns == cred->user->user_ns &&
132 (pcred->uid == cred->euid ||
133 pcred->euid == cred->euid))
135 if (ns_capable(pcred->user->user_ns, CAP_SYS_NICE))
141 * set the priority of a task
142 * - the caller must hold the RCU read lock
144 static int set_one_prio(struct task_struct *p, int niceval, int error)
148 if (!set_one_prio_perm(p)) {
152 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
156 no_nice = security_task_setnice(p, niceval);
163 set_user_nice(p, niceval);
168 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
170 struct task_struct *g, *p;
171 struct user_struct *user;
172 const struct cred *cred = current_cred();
176 if (which > PRIO_USER || which < PRIO_PROCESS)
179 /* normalize: avoid signed division (rounding problems) */
187 read_lock(&tasklist_lock);
191 p = find_task_by_vpid(who);
195 error = set_one_prio(p, niceval, error);
199 pgrp = find_vpid(who);
201 pgrp = task_pgrp(current);
202 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
203 error = set_one_prio(p, niceval, error);
204 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
207 user = (struct user_struct *) cred->user;
210 else if ((who != cred->uid) &&
211 !(user = find_user(who)))
212 goto out_unlock; /* No processes for this user */
214 do_each_thread(g, p) {
215 if (__task_cred(p)->uid == who)
216 error = set_one_prio(p, niceval, error);
217 } while_each_thread(g, p);
218 if (who != cred->uid)
219 free_uid(user); /* For find_user() */
223 read_unlock(&tasklist_lock);
230 * Ugh. To avoid negative return values, "getpriority()" will
231 * not return the normal nice-value, but a negated value that
232 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
233 * to stay compatible.
235 SYSCALL_DEFINE2(getpriority, int, which, int, who)
237 struct task_struct *g, *p;
238 struct user_struct *user;
239 const struct cred *cred = current_cred();
240 long niceval, retval = -ESRCH;
243 if (which > PRIO_USER || which < PRIO_PROCESS)
247 read_lock(&tasklist_lock);
251 p = find_task_by_vpid(who);
255 niceval = 20 - task_nice(p);
256 if (niceval > retval)
262 pgrp = find_vpid(who);
264 pgrp = task_pgrp(current);
265 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
266 niceval = 20 - task_nice(p);
267 if (niceval > retval)
269 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
272 user = (struct user_struct *) cred->user;
275 else if ((who != cred->uid) &&
276 !(user = find_user(who)))
277 goto out_unlock; /* No processes for this user */
279 do_each_thread(g, p) {
280 if (__task_cred(p)->uid == who) {
281 niceval = 20 - task_nice(p);
282 if (niceval > retval)
285 } while_each_thread(g, p);
286 if (who != cred->uid)
287 free_uid(user); /* for find_user() */
291 read_unlock(&tasklist_lock);
298 * emergency_restart - reboot the system
300 * Without shutting down any hardware or taking any locks
301 * reboot the system. This is called when we know we are in
302 * trouble so this is our best effort to reboot. This is
303 * safe to call in interrupt context.
305 void emergency_restart(void)
307 kmsg_dump(KMSG_DUMP_EMERG);
308 machine_emergency_restart();
310 EXPORT_SYMBOL_GPL(emergency_restart);
312 void kernel_restart_prepare(char *cmd)
314 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
315 system_state = SYSTEM_RESTART;
316 usermodehelper_disable();
322 * register_reboot_notifier - Register function to be called at reboot time
323 * @nb: Info about notifier function to be called
325 * Registers a function with the list of functions
326 * to be called at reboot time.
328 * Currently always returns zero, as blocking_notifier_chain_register()
329 * always returns zero.
331 int register_reboot_notifier(struct notifier_block *nb)
333 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
335 EXPORT_SYMBOL(register_reboot_notifier);
338 * unregister_reboot_notifier - Unregister previously registered reboot notifier
339 * @nb: Hook to be unregistered
341 * Unregisters a previously registered reboot
344 * Returns zero on success, or %-ENOENT on failure.
346 int unregister_reboot_notifier(struct notifier_block *nb)
348 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
350 EXPORT_SYMBOL(unregister_reboot_notifier);
353 * kernel_restart - reboot the system
354 * @cmd: pointer to buffer containing command to execute for restart
357 * Shutdown everything and perform a clean reboot.
358 * This is not safe to call in interrupt context.
360 void kernel_restart(char *cmd)
362 kernel_restart_prepare(cmd);
364 printk(KERN_EMERG "Restarting system.\n");
366 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
367 kmsg_dump(KMSG_DUMP_RESTART);
368 machine_restart(cmd);
370 EXPORT_SYMBOL_GPL(kernel_restart);
372 static void kernel_shutdown_prepare(enum system_states state)
374 blocking_notifier_call_chain(&reboot_notifier_list,
375 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
376 system_state = state;
377 usermodehelper_disable();
381 * kernel_halt - halt the system
383 * Shutdown everything and perform a clean system halt.
385 void kernel_halt(void)
387 kernel_shutdown_prepare(SYSTEM_HALT);
389 printk(KERN_EMERG "System halted.\n");
390 kmsg_dump(KMSG_DUMP_HALT);
394 EXPORT_SYMBOL_GPL(kernel_halt);
397 * kernel_power_off - power_off the system
399 * Shutdown everything and perform a clean system power_off.
401 void kernel_power_off(void)
403 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
404 if (pm_power_off_prepare)
405 pm_power_off_prepare();
406 disable_nonboot_cpus();
408 printk(KERN_EMERG "Power down.\n");
409 kmsg_dump(KMSG_DUMP_POWEROFF);
412 EXPORT_SYMBOL_GPL(kernel_power_off);
414 static DEFINE_MUTEX(reboot_mutex);
417 * Reboot system call: for obvious reasons only root may call it,
418 * and even root needs to set up some magic numbers in the registers
419 * so that some mistake won't make this reboot the whole machine.
420 * You can also set the meaning of the ctrl-alt-del-key here.
422 * reboot doesn't sync: do that yourself before calling this.
424 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
430 /* We only trust the superuser with rebooting the system. */
431 if (!capable(CAP_SYS_BOOT))
434 /* For safety, we require "magic" arguments. */
435 if (magic1 != LINUX_REBOOT_MAGIC1 ||
436 (magic2 != LINUX_REBOOT_MAGIC2 &&
437 magic2 != LINUX_REBOOT_MAGIC2A &&
438 magic2 != LINUX_REBOOT_MAGIC2B &&
439 magic2 != LINUX_REBOOT_MAGIC2C))
442 /* Instead of trying to make the power_off code look like
443 * halt when pm_power_off is not set do it the easy way.
445 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
446 cmd = LINUX_REBOOT_CMD_HALT;
448 mutex_lock(&reboot_mutex);
450 case LINUX_REBOOT_CMD_RESTART:
451 kernel_restart(NULL);
454 case LINUX_REBOOT_CMD_CAD_ON:
458 case LINUX_REBOOT_CMD_CAD_OFF:
462 case LINUX_REBOOT_CMD_HALT:
465 panic("cannot halt");
467 case LINUX_REBOOT_CMD_POWER_OFF:
472 case LINUX_REBOOT_CMD_RESTART2:
473 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
477 buffer[sizeof(buffer) - 1] = '\0';
479 kernel_restart(buffer);
483 case LINUX_REBOOT_CMD_KEXEC:
484 ret = kernel_kexec();
488 #ifdef CONFIG_HIBERNATION
489 case LINUX_REBOOT_CMD_SW_SUSPEND:
498 mutex_unlock(&reboot_mutex);
502 static void deferred_cad(struct work_struct *dummy)
504 kernel_restart(NULL);
508 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
509 * As it's called within an interrupt, it may NOT sync: the only choice
510 * is whether to reboot at once, or just ignore the ctrl-alt-del.
512 void ctrl_alt_del(void)
514 static DECLARE_WORK(cad_work, deferred_cad);
517 schedule_work(&cad_work);
519 kill_cad_pid(SIGINT, 1);
523 * Unprivileged users may change the real gid to the effective gid
524 * or vice versa. (BSD-style)
526 * If you set the real gid at all, or set the effective gid to a value not
527 * equal to the real gid, then the saved gid is set to the new effective gid.
529 * This makes it possible for a setgid program to completely drop its
530 * privileges, which is often a useful assertion to make when you are doing
531 * a security audit over a program.
533 * The general idea is that a program which uses just setregid() will be
534 * 100% compatible with BSD. A program which uses just setgid() will be
535 * 100% compatible with POSIX with saved IDs.
537 * SMP: There are not races, the GIDs are checked only by filesystem
538 * operations (as far as semantic preservation is concerned).
540 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
542 const struct cred *old;
546 new = prepare_creds();
549 old = current_cred();
552 if (rgid != (gid_t) -1) {
553 if (old->gid == rgid ||
555 nsown_capable(CAP_SETGID))
560 if (egid != (gid_t) -1) {
561 if (old->gid == egid ||
564 nsown_capable(CAP_SETGID))
570 if (rgid != (gid_t) -1 ||
571 (egid != (gid_t) -1 && egid != old->gid))
572 new->sgid = new->egid;
573 new->fsgid = new->egid;
575 return commit_creds(new);
583 * setgid() is implemented like SysV w/ SAVED_IDS
585 * SMP: Same implicit races as above.
587 SYSCALL_DEFINE1(setgid, gid_t, gid)
589 const struct cred *old;
593 new = prepare_creds();
596 old = current_cred();
599 if (nsown_capable(CAP_SETGID))
600 new->gid = new->egid = new->sgid = new->fsgid = gid;
601 else if (gid == old->gid || gid == old->sgid)
602 new->egid = new->fsgid = gid;
606 return commit_creds(new);
614 * change the user struct in a credentials set to match the new UID
616 static int set_user(struct cred *new)
618 struct user_struct *new_user;
620 new_user = alloc_uid(current_user_ns(), new->uid);
625 * We don't fail in case of NPROC limit excess here because too many
626 * poorly written programs don't check set*uid() return code, assuming
627 * it never fails if called by root. We may still enforce NPROC limit
628 * for programs doing set*uid()+execve() by harmlessly deferring the
629 * failure to the execve() stage.
631 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
632 new_user != INIT_USER)
633 current->flags |= PF_NPROC_EXCEEDED;
635 current->flags &= ~PF_NPROC_EXCEEDED;
638 new->user = new_user;
643 * Unprivileged users may change the real uid to the effective uid
644 * or vice versa. (BSD-style)
646 * If you set the real uid at all, or set the effective uid to a value not
647 * equal to the real uid, then the saved uid is set to the new effective uid.
649 * This makes it possible for a setuid program to completely drop its
650 * privileges, which is often a useful assertion to make when you are doing
651 * a security audit over a program.
653 * The general idea is that a program which uses just setreuid() will be
654 * 100% compatible with BSD. A program which uses just setuid() will be
655 * 100% compatible with POSIX with saved IDs.
657 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
659 const struct cred *old;
663 new = prepare_creds();
666 old = current_cred();
669 if (ruid != (uid_t) -1) {
671 if (old->uid != ruid &&
673 !nsown_capable(CAP_SETUID))
677 if (euid != (uid_t) -1) {
679 if (old->uid != euid &&
682 !nsown_capable(CAP_SETUID))
686 if (new->uid != old->uid) {
687 retval = set_user(new);
691 if (ruid != (uid_t) -1 ||
692 (euid != (uid_t) -1 && euid != old->uid))
693 new->suid = new->euid;
694 new->fsuid = new->euid;
696 retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
700 return commit_creds(new);
708 * setuid() is implemented like SysV with SAVED_IDS
710 * Note that SAVED_ID's is deficient in that a setuid root program
711 * like sendmail, for example, cannot set its uid to be a normal
712 * user and then switch back, because if you're root, setuid() sets
713 * the saved uid too. If you don't like this, blame the bright people
714 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
715 * will allow a root program to temporarily drop privileges and be able to
716 * regain them by swapping the real and effective uid.
718 SYSCALL_DEFINE1(setuid, uid_t, uid)
720 const struct cred *old;
724 new = prepare_creds();
727 old = current_cred();
730 if (nsown_capable(CAP_SETUID)) {
731 new->suid = new->uid = uid;
732 if (uid != old->uid) {
733 retval = set_user(new);
737 } else if (uid != old->uid && uid != new->suid) {
741 new->fsuid = new->euid = uid;
743 retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
747 return commit_creds(new);
756 * This function implements a generic ability to update ruid, euid,
757 * and suid. This allows you to implement the 4.4 compatible seteuid().
759 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
761 const struct cred *old;
765 new = prepare_creds();
769 old = current_cred();
772 if (!nsown_capable(CAP_SETUID)) {
773 if (ruid != (uid_t) -1 && ruid != old->uid &&
774 ruid != old->euid && ruid != old->suid)
776 if (euid != (uid_t) -1 && euid != old->uid &&
777 euid != old->euid && euid != old->suid)
779 if (suid != (uid_t) -1 && suid != old->uid &&
780 suid != old->euid && suid != old->suid)
784 if (ruid != (uid_t) -1) {
786 if (ruid != old->uid) {
787 retval = set_user(new);
792 if (euid != (uid_t) -1)
794 if (suid != (uid_t) -1)
796 new->fsuid = new->euid;
798 retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
802 return commit_creds(new);
809 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
811 const struct cred *cred = current_cred();
814 if (!(retval = put_user(cred->uid, ruid)) &&
815 !(retval = put_user(cred->euid, euid)))
816 retval = put_user(cred->suid, suid);
822 * Same as above, but for rgid, egid, sgid.
824 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
826 const struct cred *old;
830 new = prepare_creds();
833 old = current_cred();
836 if (!nsown_capable(CAP_SETGID)) {
837 if (rgid != (gid_t) -1 && rgid != old->gid &&
838 rgid != old->egid && rgid != old->sgid)
840 if (egid != (gid_t) -1 && egid != old->gid &&
841 egid != old->egid && egid != old->sgid)
843 if (sgid != (gid_t) -1 && sgid != old->gid &&
844 sgid != old->egid && sgid != old->sgid)
848 if (rgid != (gid_t) -1)
850 if (egid != (gid_t) -1)
852 if (sgid != (gid_t) -1)
854 new->fsgid = new->egid;
856 return commit_creds(new);
863 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
865 const struct cred *cred = current_cred();
868 if (!(retval = put_user(cred->gid, rgid)) &&
869 !(retval = put_user(cred->egid, egid)))
870 retval = put_user(cred->sgid, sgid);
877 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
878 * is used for "access()" and for the NFS daemon (letting nfsd stay at
879 * whatever uid it wants to). It normally shadows "euid", except when
880 * explicitly set by setfsuid() or for access..
882 SYSCALL_DEFINE1(setfsuid, uid_t, uid)
884 const struct cred *old;
888 new = prepare_creds();
890 return current_fsuid();
891 old = current_cred();
892 old_fsuid = old->fsuid;
894 if (uid == old->uid || uid == old->euid ||
895 uid == old->suid || uid == old->fsuid ||
896 nsown_capable(CAP_SETUID)) {
897 if (uid != old_fsuid) {
899 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
913 * Samma på svenska..
915 SYSCALL_DEFINE1(setfsgid, gid_t, gid)
917 const struct cred *old;
921 new = prepare_creds();
923 return current_fsgid();
924 old = current_cred();
925 old_fsgid = old->fsgid;
927 if (gid == old->gid || gid == old->egid ||
928 gid == old->sgid || gid == old->fsgid ||
929 nsown_capable(CAP_SETGID)) {
930 if (gid != old_fsgid) {
944 void do_sys_times(struct tms *tms)
946 cputime_t tgutime, tgstime, cutime, cstime;
948 spin_lock_irq(¤t->sighand->siglock);
949 thread_group_times(current, &tgutime, &tgstime);
950 cutime = current->signal->cutime;
951 cstime = current->signal->cstime;
952 spin_unlock_irq(¤t->sighand->siglock);
953 tms->tms_utime = cputime_to_clock_t(tgutime);
954 tms->tms_stime = cputime_to_clock_t(tgstime);
955 tms->tms_cutime = cputime_to_clock_t(cutime);
956 tms->tms_cstime = cputime_to_clock_t(cstime);
959 SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
965 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
968 force_successful_syscall_return();
969 return (long) jiffies_64_to_clock_t(get_jiffies_64());
973 * This needs some heavy checking ...
974 * I just haven't the stomach for it. I also don't fully
975 * understand sessions/pgrp etc. Let somebody who does explain it.
977 * OK, I think I have the protection semantics right.... this is really
978 * only important on a multi-user system anyway, to make sure one user
979 * can't send a signal to a process owned by another. -TYT, 12/12/91
981 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
984 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
986 struct task_struct *p;
987 struct task_struct *group_leader = current->group_leader;
992 pid = task_pid_vnr(group_leader);
999 /* From this point forward we keep holding onto the tasklist lock
1000 * so that our parent does not change from under us. -DaveM
1002 write_lock_irq(&tasklist_lock);
1005 p = find_task_by_vpid(pid);
1010 if (!thread_group_leader(p))
1013 if (same_thread_group(p->real_parent, group_leader)) {
1015 if (task_session(p) != task_session(group_leader))
1022 if (p != group_leader)
1027 if (p->signal->leader)
1032 struct task_struct *g;
1034 pgrp = find_vpid(pgid);
1035 g = pid_task(pgrp, PIDTYPE_PGID);
1036 if (!g || task_session(g) != task_session(group_leader))
1040 err = security_task_setpgid(p, pgid);
1044 if (task_pgrp(p) != pgrp)
1045 change_pid(p, PIDTYPE_PGID, pgrp);
1049 /* All paths lead to here, thus we are safe. -DaveM */
1050 write_unlock_irq(&tasklist_lock);
1055 SYSCALL_DEFINE1(getpgid, pid_t, pid)
1057 struct task_struct *p;
1063 grp = task_pgrp(current);
1066 p = find_task_by_vpid(pid);
1073 retval = security_task_getpgid(p);
1077 retval = pid_vnr(grp);
1083 #ifdef __ARCH_WANT_SYS_GETPGRP
1085 SYSCALL_DEFINE0(getpgrp)
1087 return sys_getpgid(0);
1092 SYSCALL_DEFINE1(getsid, pid_t, pid)
1094 struct task_struct *p;
1100 sid = task_session(current);
1103 p = find_task_by_vpid(pid);
1106 sid = task_session(p);
1110 retval = security_task_getsid(p);
1114 retval = pid_vnr(sid);
1120 SYSCALL_DEFINE0(setsid)
1122 struct task_struct *group_leader = current->group_leader;
1123 struct pid *sid = task_pid(group_leader);
1124 pid_t session = pid_vnr(sid);
1127 write_lock_irq(&tasklist_lock);
1128 /* Fail if I am already a session leader */
1129 if (group_leader->signal->leader)
1132 /* Fail if a process group id already exists that equals the
1133 * proposed session id.
1135 if (pid_task(sid, PIDTYPE_PGID))
1138 group_leader->signal->leader = 1;
1139 __set_special_pids(sid);
1141 proc_clear_tty(group_leader);
1145 write_unlock_irq(&tasklist_lock);
1147 proc_sid_connector(group_leader);
1148 sched_autogroup_create_attach(group_leader);
1153 DECLARE_RWSEM(uts_sem);
1155 #ifdef COMPAT_UTS_MACHINE
1156 #define override_architecture(name) \
1157 (personality(current->personality) == PER_LINUX32 && \
1158 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1159 sizeof(COMPAT_UTS_MACHINE)))
1161 #define override_architecture(name) 0
1164 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1168 down_read(&uts_sem);
1169 if (copy_to_user(name, utsname(), sizeof *name))
1173 if (!errno && override_architecture(name))
1178 #ifdef __ARCH_WANT_SYS_OLD_UNAME
1182 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1189 down_read(&uts_sem);
1190 if (copy_to_user(name, utsname(), sizeof(*name)))
1194 if (!error && override_architecture(name))
1199 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1205 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
1208 down_read(&uts_sem);
1209 error = __copy_to_user(&name->sysname, &utsname()->sysname,
1211 error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
1212 error |= __copy_to_user(&name->nodename, &utsname()->nodename,
1214 error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
1215 error |= __copy_to_user(&name->release, &utsname()->release,
1217 error |= __put_user(0, name->release + __OLD_UTS_LEN);
1218 error |= __copy_to_user(&name->version, &utsname()->version,
1220 error |= __put_user(0, name->version + __OLD_UTS_LEN);
1221 error |= __copy_to_user(&name->machine, &utsname()->machine,
1223 error |= __put_user(0, name->machine + __OLD_UTS_LEN);
1226 if (!error && override_architecture(name))
1228 return error ? -EFAULT : 0;
1232 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1235 char tmp[__NEW_UTS_LEN];
1237 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1240 if (len < 0 || len > __NEW_UTS_LEN)
1242 down_write(&uts_sem);
1244 if (!copy_from_user(tmp, name, len)) {
1245 struct new_utsname *u = utsname();
1247 memcpy(u->nodename, tmp, len);
1248 memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1255 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1257 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1260 struct new_utsname *u;
1264 down_read(&uts_sem);
1266 i = 1 + strlen(u->nodename);
1270 if (copy_to_user(name, u->nodename, i))
1279 * Only setdomainname; getdomainname can be implemented by calling
1282 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1285 char tmp[__NEW_UTS_LEN];
1287 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1289 if (len < 0 || len > __NEW_UTS_LEN)
1292 down_write(&uts_sem);
1294 if (!copy_from_user(tmp, name, len)) {
1295 struct new_utsname *u = utsname();
1297 memcpy(u->domainname, tmp, len);
1298 memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1305 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1307 struct rlimit value;
1310 ret = do_prlimit(current, resource, NULL, &value);
1312 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1317 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1320 * Back compatibility for getrlimit. Needed for some apps.
1323 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1324 struct rlimit __user *, rlim)
1327 if (resource >= RLIM_NLIMITS)
1330 task_lock(current->group_leader);
1331 x = current->signal->rlim[resource];
1332 task_unlock(current->group_leader);
1333 if (x.rlim_cur > 0x7FFFFFFF)
1334 x.rlim_cur = 0x7FFFFFFF;
1335 if (x.rlim_max > 0x7FFFFFFF)
1336 x.rlim_max = 0x7FFFFFFF;
1337 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1342 static inline bool rlim64_is_infinity(__u64 rlim64)
1344 #if BITS_PER_LONG < 64
1345 return rlim64 >= ULONG_MAX;
1347 return rlim64 == RLIM64_INFINITY;
1351 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1353 if (rlim->rlim_cur == RLIM_INFINITY)
1354 rlim64->rlim_cur = RLIM64_INFINITY;
1356 rlim64->rlim_cur = rlim->rlim_cur;
1357 if (rlim->rlim_max == RLIM_INFINITY)
1358 rlim64->rlim_max = RLIM64_INFINITY;
1360 rlim64->rlim_max = rlim->rlim_max;
1363 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1365 if (rlim64_is_infinity(rlim64->rlim_cur))
1366 rlim->rlim_cur = RLIM_INFINITY;
1368 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1369 if (rlim64_is_infinity(rlim64->rlim_max))
1370 rlim->rlim_max = RLIM_INFINITY;
1372 rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1375 /* make sure you are allowed to change @tsk limits before calling this */
1376 int do_prlimit(struct task_struct *tsk, unsigned int resource,
1377 struct rlimit *new_rlim, struct rlimit *old_rlim)
1379 struct rlimit *rlim;
1382 if (resource >= RLIM_NLIMITS)
1385 if (new_rlim->rlim_cur > new_rlim->rlim_max)
1387 if (resource == RLIMIT_NOFILE &&
1388 new_rlim->rlim_max > sysctl_nr_open)
1392 /* protect tsk->signal and tsk->sighand from disappearing */
1393 read_lock(&tasklist_lock);
1394 if (!tsk->sighand) {
1399 rlim = tsk->signal->rlim + resource;
1400 task_lock(tsk->group_leader);
1402 /* Keep the capable check against init_user_ns until
1403 cgroups can contain all limits */
1404 if (new_rlim->rlim_max > rlim->rlim_max &&
1405 !capable(CAP_SYS_RESOURCE))
1408 retval = security_task_setrlimit(tsk->group_leader,
1409 resource, new_rlim);
1410 if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1412 * The caller is asking for an immediate RLIMIT_CPU
1413 * expiry. But we use the zero value to mean "it was
1414 * never set". So let's cheat and make it one second
1417 new_rlim->rlim_cur = 1;
1426 task_unlock(tsk->group_leader);
1429 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1430 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1431 * very long-standing error, and fixing it now risks breakage of
1432 * applications, so we live with it
1434 if (!retval && new_rlim && resource == RLIMIT_CPU &&
1435 new_rlim->rlim_cur != RLIM_INFINITY)
1436 update_rlimit_cpu(tsk, new_rlim->rlim_cur);
1438 read_unlock(&tasklist_lock);
1442 /* rcu lock must be held */
1443 static int check_prlimit_permission(struct task_struct *task)
1445 const struct cred *cred = current_cred(), *tcred;
1447 if (current == task)
1450 tcred = __task_cred(task);
1451 if (cred->user->user_ns == tcred->user->user_ns &&
1452 (cred->uid == tcred->euid &&
1453 cred->uid == tcred->suid &&
1454 cred->uid == tcred->uid &&
1455 cred->gid == tcred->egid &&
1456 cred->gid == tcred->sgid &&
1457 cred->gid == tcred->gid))
1459 if (ns_capable(tcred->user->user_ns, CAP_SYS_RESOURCE))
1465 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1466 const struct rlimit64 __user *, new_rlim,
1467 struct rlimit64 __user *, old_rlim)
1469 struct rlimit64 old64, new64;
1470 struct rlimit old, new;
1471 struct task_struct *tsk;
1475 if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1477 rlim64_to_rlim(&new64, &new);
1481 tsk = pid ? find_task_by_vpid(pid) : current;
1486 ret = check_prlimit_permission(tsk);
1491 get_task_struct(tsk);
1494 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1495 old_rlim ? &old : NULL);
1497 if (!ret && old_rlim) {
1498 rlim_to_rlim64(&old, &old64);
1499 if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1503 put_task_struct(tsk);
1507 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1509 struct rlimit new_rlim;
1511 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1513 return do_prlimit(current, resource, &new_rlim, NULL);
1517 * It would make sense to put struct rusage in the task_struct,
1518 * except that would make the task_struct be *really big*. After
1519 * task_struct gets moved into malloc'ed memory, it would
1520 * make sense to do this. It will make moving the rest of the information
1521 * a lot simpler! (Which we're not doing right now because we're not
1522 * measuring them yet).
1524 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1525 * races with threads incrementing their own counters. But since word
1526 * reads are atomic, we either get new values or old values and we don't
1527 * care which for the sums. We always take the siglock to protect reading
1528 * the c* fields from p->signal from races with exit.c updating those
1529 * fields when reaping, so a sample either gets all the additions of a
1530 * given child after it's reaped, or none so this sample is before reaping.
1533 * We need to take the siglock for CHILDEREN, SELF and BOTH
1534 * for the cases current multithreaded, non-current single threaded
1535 * non-current multithreaded. Thread traversal is now safe with
1537 * Strictly speaking, we donot need to take the siglock if we are current and
1538 * single threaded, as no one else can take our signal_struct away, no one
1539 * else can reap the children to update signal->c* counters, and no one else
1540 * can race with the signal-> fields. If we do not take any lock, the
1541 * signal-> fields could be read out of order while another thread was just
1542 * exiting. So we should place a read memory barrier when we avoid the lock.
1543 * On the writer side, write memory barrier is implied in __exit_signal
1544 * as __exit_signal releases the siglock spinlock after updating the signal->
1545 * fields. But we don't do this yet to keep things simple.
1549 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1551 r->ru_nvcsw += t->nvcsw;
1552 r->ru_nivcsw += t->nivcsw;
1553 r->ru_minflt += t->min_flt;
1554 r->ru_majflt += t->maj_flt;
1555 r->ru_inblock += task_io_get_inblock(t);
1556 r->ru_oublock += task_io_get_oublock(t);
1559 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1561 struct task_struct *t;
1562 unsigned long flags;
1563 cputime_t tgutime, tgstime, utime, stime;
1564 unsigned long maxrss = 0;
1566 memset((char *) r, 0, sizeof *r);
1567 utime = stime = cputime_zero;
1569 if (who == RUSAGE_THREAD) {
1570 task_times(current, &utime, &stime);
1571 accumulate_thread_rusage(p, r);
1572 maxrss = p->signal->maxrss;
1576 if (!lock_task_sighand(p, &flags))
1581 case RUSAGE_CHILDREN:
1582 utime = p->signal->cutime;
1583 stime = p->signal->cstime;
1584 r->ru_nvcsw = p->signal->cnvcsw;
1585 r->ru_nivcsw = p->signal->cnivcsw;
1586 r->ru_minflt = p->signal->cmin_flt;
1587 r->ru_majflt = p->signal->cmaj_flt;
1588 r->ru_inblock = p->signal->cinblock;
1589 r->ru_oublock = p->signal->coublock;
1590 maxrss = p->signal->cmaxrss;
1592 if (who == RUSAGE_CHILDREN)
1596 thread_group_times(p, &tgutime, &tgstime);
1597 utime = cputime_add(utime, tgutime);
1598 stime = cputime_add(stime, tgstime);
1599 r->ru_nvcsw += p->signal->nvcsw;
1600 r->ru_nivcsw += p->signal->nivcsw;
1601 r->ru_minflt += p->signal->min_flt;
1602 r->ru_majflt += p->signal->maj_flt;
1603 r->ru_inblock += p->signal->inblock;
1604 r->ru_oublock += p->signal->oublock;
1605 if (maxrss < p->signal->maxrss)
1606 maxrss = p->signal->maxrss;
1609 accumulate_thread_rusage(t, r);
1617 unlock_task_sighand(p, &flags);
1620 cputime_to_timeval(utime, &r->ru_utime);
1621 cputime_to_timeval(stime, &r->ru_stime);
1623 if (who != RUSAGE_CHILDREN) {
1624 struct mm_struct *mm = get_task_mm(p);
1626 setmax_mm_hiwater_rss(&maxrss, mm);
1630 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1633 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1636 k_getrusage(p, who, &r);
1637 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1640 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1642 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1643 who != RUSAGE_THREAD)
1645 return getrusage(current, who, ru);
1648 SYSCALL_DEFINE1(umask, int, mask)
1650 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1654 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
1655 unsigned long, arg4, unsigned long, arg5)
1657 struct task_struct *me = current;
1658 unsigned char comm[sizeof(me->comm)];
1661 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1662 if (error != -ENOSYS)
1667 case PR_SET_PDEATHSIG:
1668 if (!valid_signal(arg2)) {
1672 me->pdeath_signal = arg2;
1675 case PR_GET_PDEATHSIG:
1676 error = put_user(me->pdeath_signal, (int __user *)arg2);
1678 case PR_GET_DUMPABLE:
1679 error = get_dumpable(me->mm);
1681 case PR_SET_DUMPABLE:
1682 if (arg2 < 0 || arg2 > 1) {
1686 set_dumpable(me->mm, arg2);
1690 case PR_SET_UNALIGN:
1691 error = SET_UNALIGN_CTL(me, arg2);
1693 case PR_GET_UNALIGN:
1694 error = GET_UNALIGN_CTL(me, arg2);
1697 error = SET_FPEMU_CTL(me, arg2);
1700 error = GET_FPEMU_CTL(me, arg2);
1703 error = SET_FPEXC_CTL(me, arg2);
1706 error = GET_FPEXC_CTL(me, arg2);
1709 error = PR_TIMING_STATISTICAL;
1712 if (arg2 != PR_TIMING_STATISTICAL)
1719 comm[sizeof(me->comm)-1] = 0;
1720 if (strncpy_from_user(comm, (char __user *)arg2,
1721 sizeof(me->comm) - 1) < 0)
1723 set_task_comm(me, comm);
1726 get_task_comm(comm, me);
1727 if (copy_to_user((char __user *)arg2, comm,
1732 error = GET_ENDIAN(me, arg2);
1735 error = SET_ENDIAN(me, arg2);
1738 case PR_GET_SECCOMP:
1739 error = prctl_get_seccomp();
1741 case PR_SET_SECCOMP:
1742 error = prctl_set_seccomp(arg2);
1745 error = GET_TSC_CTL(arg2);
1748 error = SET_TSC_CTL(arg2);
1750 case PR_TASK_PERF_EVENTS_DISABLE:
1751 error = perf_event_task_disable();
1753 case PR_TASK_PERF_EVENTS_ENABLE:
1754 error = perf_event_task_enable();
1756 case PR_GET_TIMERSLACK:
1757 error = current->timer_slack_ns;
1759 case PR_SET_TIMERSLACK:
1761 current->timer_slack_ns =
1762 current->default_timer_slack_ns;
1764 current->timer_slack_ns = arg2;
1771 case PR_MCE_KILL_CLEAR:
1774 current->flags &= ~PF_MCE_PROCESS;
1776 case PR_MCE_KILL_SET:
1777 current->flags |= PF_MCE_PROCESS;
1778 if (arg3 == PR_MCE_KILL_EARLY)
1779 current->flags |= PF_MCE_EARLY;
1780 else if (arg3 == PR_MCE_KILL_LATE)
1781 current->flags &= ~PF_MCE_EARLY;
1782 else if (arg3 == PR_MCE_KILL_DEFAULT)
1784 ~(PF_MCE_EARLY|PF_MCE_PROCESS);
1793 case PR_MCE_KILL_GET:
1794 if (arg2 | arg3 | arg4 | arg5)
1796 if (current->flags & PF_MCE_PROCESS)
1797 error = (current->flags & PF_MCE_EARLY) ?
1798 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
1800 error = PR_MCE_KILL_DEFAULT;
1809 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
1810 struct getcpu_cache __user *, unused)
1813 int cpu = raw_smp_processor_id();
1815 err |= put_user(cpu, cpup);
1817 err |= put_user(cpu_to_node(cpu), nodep);
1818 return err ? -EFAULT : 0;
1821 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
1823 static void argv_cleanup(struct subprocess_info *info)
1825 argv_free(info->argv);
1829 * orderly_poweroff - Trigger an orderly system poweroff
1830 * @force: force poweroff if command execution fails
1832 * This may be called from any context to trigger a system shutdown.
1833 * If the orderly shutdown fails, it will force an immediate shutdown.
1835 int orderly_poweroff(bool force)
1838 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
1839 static char *envp[] = {
1841 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1845 struct subprocess_info *info;
1848 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
1849 __func__, poweroff_cmd);
1853 info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
1859 call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL);
1861 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
1865 printk(KERN_WARNING "Failed to start orderly shutdown: "
1866 "forcing the issue\n");
1868 /* I guess this should try to kick off some daemon to
1869 sync and poweroff asap. Or not even bother syncing
1870 if we're doing an emergency shutdown? */
1877 EXPORT_SYMBOL_GPL(orderly_poweroff);