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/smp_lock.h>
12 #include <linux/notifier.h>
13 #include <linux/reboot.h>
14 #include <linux/prctl.h>
15 #include <linux/highuid.h>
17 #include <linux/perf_counter.h>
18 #include <linux/resource.h>
19 #include <linux/kernel.h>
20 #include <linux/kexec.h>
21 #include <linux/workqueue.h>
22 #include <linux/capability.h>
23 #include <linux/device.h>
24 #include <linux/key.h>
25 #include <linux/times.h>
26 #include <linux/posix-timers.h>
27 #include <linux/security.h>
28 #include <linux/dcookies.h>
29 #include <linux/suspend.h>
30 #include <linux/tty.h>
31 #include <linux/signal.h>
32 #include <linux/cn_proc.h>
33 #include <linux/getcpu.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/seccomp.h>
36 #include <linux/hardirq.h>
37 #include <linux/cpu.h>
38 #include <linux/ptrace.h>
40 #include <linux/compat.h>
41 #include <linux/syscalls.h>
42 #include <linux/rt_lock.h>
43 #include <linux/kprobes.h>
44 #include <linux/user_namespace.h>
46 #include <asm/uaccess.h>
48 #include <asm/unistd.h>
50 #ifndef SET_UNALIGN_CTL
51 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
53 #ifndef GET_UNALIGN_CTL
54 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
57 # define SET_FPEMU_CTL(a,b) (-EINVAL)
60 # define GET_FPEMU_CTL(a,b) (-EINVAL)
63 # define SET_FPEXC_CTL(a,b) (-EINVAL)
66 # define GET_FPEXC_CTL(a,b) (-EINVAL)
69 # define GET_ENDIAN(a,b) (-EINVAL)
72 # define SET_ENDIAN(a,b) (-EINVAL)
75 # define GET_TSC_CTL(a) (-EINVAL)
78 # define SET_TSC_CTL(a) (-EINVAL)
82 * this is where the system-wide overflow UID and GID are defined, for
83 * architectures that now have 32-bit UID/GID but didn't in the past
86 int overflowuid
= DEFAULT_OVERFLOWUID
;
87 int overflowgid
= DEFAULT_OVERFLOWGID
;
90 EXPORT_SYMBOL(overflowuid
);
91 EXPORT_SYMBOL(overflowgid
);
95 * the same as above, but for filesystems which can only store a 16-bit
96 * UID and GID. as such, this is needed on all architectures
99 int fs_overflowuid
= DEFAULT_FS_OVERFLOWUID
;
100 int fs_overflowgid
= DEFAULT_FS_OVERFLOWUID
;
102 EXPORT_SYMBOL(fs_overflowuid
);
103 EXPORT_SYMBOL(fs_overflowgid
);
106 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
111 EXPORT_SYMBOL(cad_pid
);
114 * If set, this is used for preparing the system to power off.
117 void (*pm_power_off_prepare
)(void);
120 * set the priority of a task
121 * - the caller must hold the RCU read lock
123 static int set_one_prio(struct task_struct
*p
, int niceval
, int error
)
125 const struct cred
*cred
= current_cred(), *pcred
= __task_cred(p
);
128 if (pcred
->uid
!= cred
->euid
&&
129 pcred
->euid
!= cred
->euid
&& !capable(CAP_SYS_NICE
)) {
133 if (niceval
< task_nice(p
) && !can_nice(p
, niceval
)) {
137 no_nice
= security_task_setnice(p
, niceval
);
144 set_user_nice(p
, niceval
);
149 SYSCALL_DEFINE3(setpriority
, int, which
, int, who
, int, niceval
)
151 struct task_struct
*g
, *p
;
152 struct user_struct
*user
;
153 const struct cred
*cred
= current_cred();
157 if (which
> PRIO_USER
|| which
< PRIO_PROCESS
)
160 /* normalize: avoid signed division (rounding problems) */
167 read_lock(&tasklist_lock
);
171 p
= find_task_by_vpid(who
);
175 error
= set_one_prio(p
, niceval
, error
);
179 pgrp
= find_vpid(who
);
181 pgrp
= task_pgrp(current
);
182 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
183 error
= set_one_prio(p
, niceval
, error
);
184 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
187 user
= (struct user_struct
*) cred
->user
;
190 else if ((who
!= cred
->uid
) &&
191 !(user
= find_user(who
)))
192 goto out_unlock
; /* No processes for this user */
195 if (__task_cred(p
)->uid
== who
)
196 error
= set_one_prio(p
, niceval
, error
);
197 while_each_thread(g
, p
);
198 if (who
!= cred
->uid
)
199 free_uid(user
); /* For find_user() */
203 read_unlock(&tasklist_lock
);
209 * Ugh. To avoid negative return values, "getpriority()" will
210 * not return the normal nice-value, but a negated value that
211 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
212 * to stay compatible.
214 SYSCALL_DEFINE2(getpriority
, int, which
, int, who
)
216 struct task_struct
*g
, *p
;
217 struct user_struct
*user
;
218 const struct cred
*cred
= current_cred();
219 long niceval
, retval
= -ESRCH
;
222 if (which
> PRIO_USER
|| which
< PRIO_PROCESS
)
225 read_lock(&tasklist_lock
);
229 p
= find_task_by_vpid(who
);
233 niceval
= 20 - task_nice(p
);
234 if (niceval
> retval
)
240 pgrp
= find_vpid(who
);
242 pgrp
= task_pgrp(current
);
243 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
244 niceval
= 20 - task_nice(p
);
245 if (niceval
> retval
)
247 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
250 user
= (struct user_struct
*) cred
->user
;
253 else if ((who
!= cred
->uid
) &&
254 !(user
= find_user(who
)))
255 goto out_unlock
; /* No processes for this user */
258 if (__task_cred(p
)->uid
== who
) {
259 niceval
= 20 - task_nice(p
);
260 if (niceval
> retval
)
263 while_each_thread(g
, p
);
264 if (who
!= cred
->uid
)
265 free_uid(user
); /* for find_user() */
269 read_unlock(&tasklist_lock
);
275 * emergency_restart - reboot the system
277 * Without shutting down any hardware or taking any locks
278 * reboot the system. This is called when we know we are in
279 * trouble so this is our best effort to reboot. This is
280 * safe to call in interrupt context.
282 void emergency_restart(void)
285 * Call the notifier chain if we are not in an
288 #ifdef CONFIG_PREEMPT
289 if (!in_atomic() && !irqs_disabled())
290 blocking_notifier_call_chain(&reboot_notifier_list
,
293 machine_emergency_restart();
295 EXPORT_SYMBOL_GPL(emergency_restart
);
297 void kernel_restart_prepare(char *cmd
)
299 blocking_notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, cmd
);
300 system_state
= SYSTEM_RESTART
;
306 * kernel_restart - reboot the system
307 * @cmd: pointer to buffer containing command to execute for restart
310 * Shutdown everything and perform a clean reboot.
311 * This is not safe to call in interrupt context.
313 void kernel_restart(char *cmd
)
315 kernel_restart_prepare(cmd
);
317 printk(KERN_EMERG
"Restarting system.\n");
319 printk(KERN_EMERG
"Restarting system with command '%s'.\n", cmd
);
320 machine_restart(cmd
);
322 EXPORT_SYMBOL_GPL(kernel_restart
);
324 static void kernel_shutdown_prepare(enum system_states state
)
326 blocking_notifier_call_chain(&reboot_notifier_list
,
327 (state
== SYSTEM_HALT
)?SYS_HALT
:SYS_POWER_OFF
, NULL
);
328 system_state
= state
;
332 * kernel_halt - halt the system
334 * Shutdown everything and perform a clean system halt.
336 void kernel_halt(void)
338 kernel_shutdown_prepare(SYSTEM_HALT
);
340 printk(KERN_EMERG
"System halted.\n");
344 EXPORT_SYMBOL_GPL(kernel_halt
);
347 * kernel_power_off - power_off the system
349 * Shutdown everything and perform a clean system power_off.
351 void kernel_power_off(void)
353 kernel_shutdown_prepare(SYSTEM_POWER_OFF
);
354 if (pm_power_off_prepare
)
355 pm_power_off_prepare();
356 disable_nonboot_cpus();
358 printk(KERN_EMERG
"Power down.\n");
361 EXPORT_SYMBOL_GPL(kernel_power_off
);
363 * Reboot system call: for obvious reasons only root may call it,
364 * and even root needs to set up some magic numbers in the registers
365 * so that some mistake won't make this reboot the whole machine.
366 * You can also set the meaning of the ctrl-alt-del-key here.
368 * reboot doesn't sync: do that yourself before calling this.
370 SYSCALL_DEFINE4(reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
375 /* We only trust the superuser with rebooting the system. */
376 if (!capable(CAP_SYS_BOOT
))
379 /* For safety, we require "magic" arguments. */
380 if (magic1
!= LINUX_REBOOT_MAGIC1
||
381 (magic2
!= LINUX_REBOOT_MAGIC2
&&
382 magic2
!= LINUX_REBOOT_MAGIC2A
&&
383 magic2
!= LINUX_REBOOT_MAGIC2B
&&
384 magic2
!= LINUX_REBOOT_MAGIC2C
))
387 /* Instead of trying to make the power_off code look like
388 * halt when pm_power_off is not set do it the easy way.
390 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !pm_power_off
)
391 cmd
= LINUX_REBOOT_CMD_HALT
;
395 case LINUX_REBOOT_CMD_RESTART
:
396 kernel_restart(NULL
);
399 case LINUX_REBOOT_CMD_CAD_ON
:
403 case LINUX_REBOOT_CMD_CAD_OFF
:
407 case LINUX_REBOOT_CMD_HALT
:
413 case LINUX_REBOOT_CMD_POWER_OFF
:
419 case LINUX_REBOOT_CMD_RESTART2
:
420 if (strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1) < 0) {
424 buffer
[sizeof(buffer
) - 1] = '\0';
426 kernel_restart(buffer
);
430 case LINUX_REBOOT_CMD_KEXEC
:
433 ret
= kernel_kexec();
439 #ifdef CONFIG_HIBERNATION
440 case LINUX_REBOOT_CMD_SW_SUSPEND
:
442 int ret
= hibernate();
456 static void deferred_cad(struct work_struct
*dummy
)
458 kernel_restart(NULL
);
462 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
463 * As it's called within an interrupt, it may NOT sync: the only choice
464 * is whether to reboot at once, or just ignore the ctrl-alt-del.
466 void ctrl_alt_del(void)
468 static DECLARE_WORK(cad_work
, deferred_cad
);
471 schedule_work(&cad_work
);
473 kill_cad_pid(SIGINT
, 1);
477 * Unprivileged users may change the real gid to the effective gid
478 * or vice versa. (BSD-style)
480 * If you set the real gid at all, or set the effective gid to a value not
481 * equal to the real gid, then the saved gid is set to the new effective gid.
483 * This makes it possible for a setgid program to completely drop its
484 * privileges, which is often a useful assertion to make when you are doing
485 * a security audit over a program.
487 * The general idea is that a program which uses just setregid() will be
488 * 100% compatible with BSD. A program which uses just setgid() will be
489 * 100% compatible with POSIX with saved IDs.
491 * SMP: There are not races, the GIDs are checked only by filesystem
492 * operations (as far as semantic preservation is concerned).
494 SYSCALL_DEFINE2(setregid
, gid_t
, rgid
, gid_t
, egid
)
496 const struct cred
*old
;
500 new = prepare_creds();
503 old
= current_cred();
505 retval
= security_task_setgid(rgid
, egid
, (gid_t
)-1, LSM_SETID_RE
);
510 if (rgid
!= (gid_t
) -1) {
511 if (old
->gid
== rgid
||
518 if (egid
!= (gid_t
) -1) {
519 if (old
->gid
== egid
||
528 if (rgid
!= (gid_t
) -1 ||
529 (egid
!= (gid_t
) -1 && egid
!= old
->gid
))
530 new->sgid
= new->egid
;
531 new->fsgid
= new->egid
;
533 return commit_creds(new);
541 * setgid() is implemented like SysV w/ SAVED_IDS
543 * SMP: Same implicit races as above.
545 SYSCALL_DEFINE1(setgid
, gid_t
, gid
)
547 const struct cred
*old
;
551 new = prepare_creds();
554 old
= current_cred();
556 retval
= security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_ID
);
561 if (capable(CAP_SETGID
))
562 new->gid
= new->egid
= new->sgid
= new->fsgid
= gid
;
563 else if (gid
== old
->gid
|| gid
== old
->sgid
)
564 new->egid
= new->fsgid
= gid
;
568 return commit_creds(new);
576 * change the user struct in a credentials set to match the new UID
578 static int set_user(struct cred
*new)
580 struct user_struct
*new_user
;
582 new_user
= alloc_uid(current_user_ns(), new->uid
);
586 if (!task_can_switch_user(new_user
, current
)) {
591 if (atomic_read(&new_user
->processes
) >=
592 current
->signal
->rlim
[RLIMIT_NPROC
].rlim_cur
&&
593 new_user
!= INIT_USER
) {
599 new->user
= new_user
;
604 * Unprivileged users may change the real uid to the effective uid
605 * or vice versa. (BSD-style)
607 * If you set the real uid at all, or set the effective uid to a value not
608 * equal to the real uid, then the saved uid is set to the new effective uid.
610 * This makes it possible for a setuid program to completely drop its
611 * privileges, which is often a useful assertion to make when you are doing
612 * a security audit over a program.
614 * The general idea is that a program which uses just setreuid() will be
615 * 100% compatible with BSD. A program which uses just setuid() will be
616 * 100% compatible with POSIX with saved IDs.
618 SYSCALL_DEFINE2(setreuid
, uid_t
, ruid
, uid_t
, euid
)
620 const struct cred
*old
;
624 new = prepare_creds();
627 old
= current_cred();
629 retval
= security_task_setuid(ruid
, euid
, (uid_t
)-1, LSM_SETID_RE
);
634 if (ruid
!= (uid_t
) -1) {
636 if (old
->uid
!= ruid
&&
638 !capable(CAP_SETUID
))
642 if (euid
!= (uid_t
) -1) {
644 if (old
->uid
!= euid
&&
647 !capable(CAP_SETUID
))
651 if (new->uid
!= old
->uid
) {
652 retval
= set_user(new);
656 if (ruid
!= (uid_t
) -1 ||
657 (euid
!= (uid_t
) -1 && euid
!= old
->uid
))
658 new->suid
= new->euid
;
659 new->fsuid
= new->euid
;
661 retval
= security_task_fix_setuid(new, old
, LSM_SETID_RE
);
665 return commit_creds(new);
673 * setuid() is implemented like SysV with SAVED_IDS
675 * Note that SAVED_ID's is deficient in that a setuid root program
676 * like sendmail, for example, cannot set its uid to be a normal
677 * user and then switch back, because if you're root, setuid() sets
678 * the saved uid too. If you don't like this, blame the bright people
679 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
680 * will allow a root program to temporarily drop privileges and be able to
681 * regain them by swapping the real and effective uid.
683 SYSCALL_DEFINE1(setuid
, uid_t
, uid
)
685 const struct cred
*old
;
689 new = prepare_creds();
692 old
= current_cred();
694 retval
= security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_ID
);
699 if (capable(CAP_SETUID
)) {
700 new->suid
= new->uid
= uid
;
701 if (uid
!= old
->uid
) {
702 retval
= set_user(new);
706 } else if (uid
!= old
->uid
&& uid
!= new->suid
) {
710 new->fsuid
= new->euid
= uid
;
712 retval
= security_task_fix_setuid(new, old
, LSM_SETID_ID
);
716 return commit_creds(new);
725 * This function implements a generic ability to update ruid, euid,
726 * and suid. This allows you to implement the 4.4 compatible seteuid().
728 SYSCALL_DEFINE3(setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
730 const struct cred
*old
;
734 new = prepare_creds();
738 retval
= security_task_setuid(ruid
, euid
, suid
, LSM_SETID_RES
);
741 old
= current_cred();
744 if (!capable(CAP_SETUID
)) {
745 if (ruid
!= (uid_t
) -1 && ruid
!= old
->uid
&&
746 ruid
!= old
->euid
&& ruid
!= old
->suid
)
748 if (euid
!= (uid_t
) -1 && euid
!= old
->uid
&&
749 euid
!= old
->euid
&& euid
!= old
->suid
)
751 if (suid
!= (uid_t
) -1 && suid
!= old
->uid
&&
752 suid
!= old
->euid
&& suid
!= old
->suid
)
756 if (ruid
!= (uid_t
) -1) {
758 if (ruid
!= old
->uid
) {
759 retval
= set_user(new);
764 if (euid
!= (uid_t
) -1)
766 if (suid
!= (uid_t
) -1)
768 new->fsuid
= new->euid
;
770 retval
= security_task_fix_setuid(new, old
, LSM_SETID_RES
);
774 return commit_creds(new);
781 SYSCALL_DEFINE3(getresuid
, uid_t __user
*, ruid
, uid_t __user
*, euid
, uid_t __user
*, suid
)
783 const struct cred
*cred
= current_cred();
786 if (!(retval
= put_user(cred
->uid
, ruid
)) &&
787 !(retval
= put_user(cred
->euid
, euid
)))
788 retval
= put_user(cred
->suid
, suid
);
794 * Same as above, but for rgid, egid, sgid.
796 SYSCALL_DEFINE3(setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
798 const struct cred
*old
;
802 new = prepare_creds();
805 old
= current_cred();
807 retval
= security_task_setgid(rgid
, egid
, sgid
, LSM_SETID_RES
);
812 if (!capable(CAP_SETGID
)) {
813 if (rgid
!= (gid_t
) -1 && rgid
!= old
->gid
&&
814 rgid
!= old
->egid
&& rgid
!= old
->sgid
)
816 if (egid
!= (gid_t
) -1 && egid
!= old
->gid
&&
817 egid
!= old
->egid
&& egid
!= old
->sgid
)
819 if (sgid
!= (gid_t
) -1 && sgid
!= old
->gid
&&
820 sgid
!= old
->egid
&& sgid
!= old
->sgid
)
824 if (rgid
!= (gid_t
) -1)
826 if (egid
!= (gid_t
) -1)
828 if (sgid
!= (gid_t
) -1)
830 new->fsgid
= new->egid
;
832 return commit_creds(new);
839 SYSCALL_DEFINE3(getresgid
, gid_t __user
*, rgid
, gid_t __user
*, egid
, gid_t __user
*, sgid
)
841 const struct cred
*cred
= current_cred();
844 if (!(retval
= put_user(cred
->gid
, rgid
)) &&
845 !(retval
= put_user(cred
->egid
, egid
)))
846 retval
= put_user(cred
->sgid
, sgid
);
853 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
854 * is used for "access()" and for the NFS daemon (letting nfsd stay at
855 * whatever uid it wants to). It normally shadows "euid", except when
856 * explicitly set by setfsuid() or for access..
858 SYSCALL_DEFINE1(setfsuid
, uid_t
, uid
)
860 const struct cred
*old
;
864 new = prepare_creds();
866 return current_fsuid();
867 old
= current_cred();
868 old_fsuid
= old
->fsuid
;
870 if (security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
) < 0)
873 if (uid
== old
->uid
|| uid
== old
->euid
||
874 uid
== old
->suid
|| uid
== old
->fsuid
||
875 capable(CAP_SETUID
)) {
876 if (uid
!= old_fsuid
) {
878 if (security_task_fix_setuid(new, old
, LSM_SETID_FS
) == 0)
893 * Samma på svenska..
895 SYSCALL_DEFINE1(setfsgid
, gid_t
, gid
)
897 const struct cred
*old
;
901 new = prepare_creds();
903 return current_fsgid();
904 old
= current_cred();
905 old_fsgid
= old
->fsgid
;
907 if (security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_FS
))
910 if (gid
== old
->gid
|| gid
== old
->egid
||
911 gid
== old
->sgid
|| gid
== old
->fsgid
||
912 capable(CAP_SETGID
)) {
913 if (gid
!= old_fsgid
) {
928 void do_sys_times(struct tms
*tms
)
930 struct task_cputime cputime
;
931 cputime_t cutime
, cstime
;
933 thread_group_cputime(current
, &cputime
);
934 spin_lock_irq(¤t
->sighand
->siglock
);
935 cutime
= current
->signal
->cutime
;
936 cstime
= current
->signal
->cstime
;
937 spin_unlock_irq(¤t
->sighand
->siglock
);
938 tms
->tms_utime
= cputime_to_clock_t(cputime
.utime
);
939 tms
->tms_stime
= cputime_to_clock_t(cputime
.stime
);
940 tms
->tms_cutime
= cputime_to_clock_t(cutime
);
941 tms
->tms_cstime
= cputime_to_clock_t(cstime
);
944 SYSCALL_DEFINE1(times
, struct tms __user
*, tbuf
)
950 if (copy_to_user(tbuf
, &tmp
, sizeof(struct tms
)))
953 force_successful_syscall_return();
954 return (long) jiffies_64_to_clock_t(get_jiffies_64());
958 * This needs some heavy checking ...
959 * I just haven't the stomach for it. I also don't fully
960 * understand sessions/pgrp etc. Let somebody who does explain it.
962 * OK, I think I have the protection semantics right.... this is really
963 * only important on a multi-user system anyway, to make sure one user
964 * can't send a signal to a process owned by another. -TYT, 12/12/91
966 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
969 SYSCALL_DEFINE2(setpgid
, pid_t
, pid
, pid_t
, pgid
)
971 struct task_struct
*p
;
972 struct task_struct
*group_leader
= current
->group_leader
;
977 pid
= task_pid_vnr(group_leader
);
983 /* From this point forward we keep holding onto the tasklist lock
984 * so that our parent does not change from under us. -DaveM
986 write_lock_irq(&tasklist_lock
);
989 p
= find_task_by_vpid(pid
);
994 if (!thread_group_leader(p
))
997 if (same_thread_group(p
->real_parent
, group_leader
)) {
999 if (task_session(p
) != task_session(group_leader
))
1006 if (p
!= group_leader
)
1011 if (p
->signal
->leader
)
1016 struct task_struct
*g
;
1018 pgrp
= find_vpid(pgid
);
1019 g
= pid_task(pgrp
, PIDTYPE_PGID
);
1020 if (!g
|| task_session(g
) != task_session(group_leader
))
1024 err
= security_task_setpgid(p
, pgid
);
1028 if (task_pgrp(p
) != pgrp
) {
1029 change_pid(p
, PIDTYPE_PGID
, pgrp
);
1030 set_task_pgrp(p
, pid_nr(pgrp
));
1035 /* All paths lead to here, thus we are safe. -DaveM */
1036 write_unlock_irq(&tasklist_lock
);
1040 SYSCALL_DEFINE1(getpgid
, pid_t
, pid
)
1042 struct task_struct
*p
;
1048 grp
= task_pgrp(current
);
1051 p
= find_task_by_vpid(pid
);
1058 retval
= security_task_getpgid(p
);
1062 retval
= pid_vnr(grp
);
1068 #ifdef __ARCH_WANT_SYS_GETPGRP
1070 SYSCALL_DEFINE0(getpgrp
)
1072 return sys_getpgid(0);
1077 SYSCALL_DEFINE1(getsid
, pid_t
, pid
)
1079 struct task_struct
*p
;
1085 sid
= task_session(current
);
1088 p
= find_task_by_vpid(pid
);
1091 sid
= task_session(p
);
1095 retval
= security_task_getsid(p
);
1099 retval
= pid_vnr(sid
);
1105 SYSCALL_DEFINE0(setsid
)
1107 struct task_struct
*group_leader
= current
->group_leader
;
1108 struct pid
*sid
= task_pid(group_leader
);
1109 pid_t session
= pid_vnr(sid
);
1112 write_lock_irq(&tasklist_lock
);
1113 /* Fail if I am already a session leader */
1114 if (group_leader
->signal
->leader
)
1117 /* Fail if a process group id already exists that equals the
1118 * proposed session id.
1120 if (pid_task(sid
, PIDTYPE_PGID
))
1123 group_leader
->signal
->leader
= 1;
1124 __set_special_pids(sid
);
1126 proc_clear_tty(group_leader
);
1130 write_unlock_irq(&tasklist_lock
);
1135 * Supplementary group IDs
1138 /* init to 2 - one for init_task, one to ensure it is never freed */
1139 struct group_info init_groups
= { .usage
= ATOMIC_INIT(2) };
1141 struct group_info
*groups_alloc(int gidsetsize
)
1143 struct group_info
*group_info
;
1147 nblocks
= (gidsetsize
+ NGROUPS_PER_BLOCK
- 1) / NGROUPS_PER_BLOCK
;
1148 /* Make sure we always allocate at least one indirect block pointer */
1149 nblocks
= nblocks
? : 1;
1150 group_info
= kmalloc(sizeof(*group_info
) + nblocks
*sizeof(gid_t
*), GFP_USER
);
1153 group_info
->ngroups
= gidsetsize
;
1154 group_info
->nblocks
= nblocks
;
1155 atomic_set(&group_info
->usage
, 1);
1157 if (gidsetsize
<= NGROUPS_SMALL
)
1158 group_info
->blocks
[0] = group_info
->small_block
;
1160 for (i
= 0; i
< nblocks
; i
++) {
1162 b
= (void *)__get_free_page(GFP_USER
);
1164 goto out_undo_partial_alloc
;
1165 group_info
->blocks
[i
] = b
;
1170 out_undo_partial_alloc
:
1172 free_page((unsigned long)group_info
->blocks
[i
]);
1178 EXPORT_SYMBOL(groups_alloc
);
1180 void groups_free(struct group_info
*group_info
)
1182 if (group_info
->blocks
[0] != group_info
->small_block
) {
1184 for (i
= 0; i
< group_info
->nblocks
; i
++)
1185 free_page((unsigned long)group_info
->blocks
[i
]);
1190 EXPORT_SYMBOL(groups_free
);
1192 /* export the group_info to a user-space array */
1193 static int groups_to_user(gid_t __user
*grouplist
,
1194 const struct group_info
*group_info
)
1197 unsigned int count
= group_info
->ngroups
;
1199 for (i
= 0; i
< group_info
->nblocks
; i
++) {
1200 unsigned int cp_count
= min(NGROUPS_PER_BLOCK
, count
);
1201 unsigned int len
= cp_count
* sizeof(*grouplist
);
1203 if (copy_to_user(grouplist
, group_info
->blocks
[i
], len
))
1206 grouplist
+= NGROUPS_PER_BLOCK
;
1212 /* fill a group_info from a user-space array - it must be allocated already */
1213 static int groups_from_user(struct group_info
*group_info
,
1214 gid_t __user
*grouplist
)
1217 unsigned int count
= group_info
->ngroups
;
1219 for (i
= 0; i
< group_info
->nblocks
; i
++) {
1220 unsigned int cp_count
= min(NGROUPS_PER_BLOCK
, count
);
1221 unsigned int len
= cp_count
* sizeof(*grouplist
);
1223 if (copy_from_user(group_info
->blocks
[i
], grouplist
, len
))
1226 grouplist
+= NGROUPS_PER_BLOCK
;
1232 /* a simple Shell sort */
1233 static void groups_sort(struct group_info
*group_info
)
1235 int base
, max
, stride
;
1236 int gidsetsize
= group_info
->ngroups
;
1238 for (stride
= 1; stride
< gidsetsize
; stride
= 3 * stride
+ 1)
1243 max
= gidsetsize
- stride
;
1244 for (base
= 0; base
< max
; base
++) {
1246 int right
= left
+ stride
;
1247 gid_t tmp
= GROUP_AT(group_info
, right
);
1249 while (left
>= 0 && GROUP_AT(group_info
, left
) > tmp
) {
1250 GROUP_AT(group_info
, right
) =
1251 GROUP_AT(group_info
, left
);
1255 GROUP_AT(group_info
, right
) = tmp
;
1261 /* a simple bsearch */
1262 int groups_search(const struct group_info
*group_info
, gid_t grp
)
1264 unsigned int left
, right
;
1270 right
= group_info
->ngroups
;
1271 while (left
< right
) {
1272 unsigned int mid
= (left
+right
)/2;
1273 int cmp
= grp
- GROUP_AT(group_info
, mid
);
1285 * set_groups - Change a group subscription in a set of credentials
1286 * @new: The newly prepared set of credentials to alter
1287 * @group_info: The group list to install
1289 * Validate a group subscription and, if valid, insert it into a set
1292 int set_groups(struct cred
*new, struct group_info
*group_info
)
1296 retval
= security_task_setgroups(group_info
);
1300 put_group_info(new->group_info
);
1301 groups_sort(group_info
);
1302 get_group_info(group_info
);
1303 new->group_info
= group_info
;
1307 EXPORT_SYMBOL(set_groups
);
1310 * set_current_groups - Change current's group subscription
1311 * @group_info: The group list to impose
1313 * Validate a group subscription and, if valid, impose it upon current's task
1316 int set_current_groups(struct group_info
*group_info
)
1321 new = prepare_creds();
1325 ret
= set_groups(new, group_info
);
1331 return commit_creds(new);
1334 EXPORT_SYMBOL(set_current_groups
);
1336 SYSCALL_DEFINE2(getgroups
, int, gidsetsize
, gid_t __user
*, grouplist
)
1338 const struct cred
*cred
= current_cred();
1344 /* no need to grab task_lock here; it cannot change */
1345 i
= cred
->group_info
->ngroups
;
1347 if (i
> gidsetsize
) {
1351 if (groups_to_user(grouplist
, cred
->group_info
)) {
1361 * SMP: Our groups are copy-on-write. We can set them safely
1362 * without another task interfering.
1365 SYSCALL_DEFINE2(setgroups
, int, gidsetsize
, gid_t __user
*, grouplist
)
1367 struct group_info
*group_info
;
1370 if (!capable(CAP_SETGID
))
1372 if ((unsigned)gidsetsize
> NGROUPS_MAX
)
1375 group_info
= groups_alloc(gidsetsize
);
1378 retval
= groups_from_user(group_info
, grouplist
);
1380 put_group_info(group_info
);
1384 retval
= set_current_groups(group_info
);
1385 put_group_info(group_info
);
1391 * Check whether we're fsgid/egid or in the supplemental group..
1393 int in_group_p(gid_t grp
)
1395 const struct cred
*cred
= current_cred();
1398 if (grp
!= cred
->fsgid
)
1399 retval
= groups_search(cred
->group_info
, grp
);
1403 EXPORT_SYMBOL(in_group_p
);
1405 int in_egroup_p(gid_t grp
)
1407 const struct cred
*cred
= current_cred();
1410 if (grp
!= cred
->egid
)
1411 retval
= groups_search(cred
->group_info
, grp
);
1415 EXPORT_SYMBOL(in_egroup_p
);
1417 DECLARE_RWSEM(uts_sem
);
1419 SYSCALL_DEFINE1(newuname
, struct new_utsname __user
*, name
)
1423 down_read(&uts_sem
);
1424 if (copy_to_user(name
, utsname(), sizeof *name
))
1430 SYSCALL_DEFINE2(sethostname
, char __user
*, name
, int, len
)
1433 char tmp
[__NEW_UTS_LEN
];
1435 if (!capable(CAP_SYS_ADMIN
))
1437 if (len
< 0 || len
> __NEW_UTS_LEN
)
1439 down_write(&uts_sem
);
1441 if (!copy_from_user(tmp
, name
, len
)) {
1442 struct new_utsname
*u
= utsname();
1444 memcpy(u
->nodename
, tmp
, len
);
1445 memset(u
->nodename
+ len
, 0, sizeof(u
->nodename
) - len
);
1452 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1454 SYSCALL_DEFINE2(gethostname
, char __user
*, name
, int, len
)
1457 struct new_utsname
*u
;
1461 down_read(&uts_sem
);
1463 i
= 1 + strlen(u
->nodename
);
1467 if (copy_to_user(name
, u
->nodename
, i
))
1476 * Only setdomainname; getdomainname can be implemented by calling
1479 SYSCALL_DEFINE2(setdomainname
, char __user
*, name
, int, len
)
1482 char tmp
[__NEW_UTS_LEN
];
1484 if (!capable(CAP_SYS_ADMIN
))
1486 if (len
< 0 || len
> __NEW_UTS_LEN
)
1489 down_write(&uts_sem
);
1491 if (!copy_from_user(tmp
, name
, len
)) {
1492 struct new_utsname
*u
= utsname();
1494 memcpy(u
->domainname
, tmp
, len
);
1495 memset(u
->domainname
+ len
, 0, sizeof(u
->domainname
) - len
);
1502 SYSCALL_DEFINE2(getrlimit
, unsigned int, resource
, struct rlimit __user
*, rlim
)
1504 if (resource
>= RLIM_NLIMITS
)
1507 struct rlimit value
;
1508 task_lock(current
->group_leader
);
1509 value
= current
->signal
->rlim
[resource
];
1510 task_unlock(current
->group_leader
);
1511 return copy_to_user(rlim
, &value
, sizeof(*rlim
)) ? -EFAULT
: 0;
1515 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1518 * Back compatibility for getrlimit. Needed for some apps.
1521 SYSCALL_DEFINE2(old_getrlimit
, unsigned int, resource
,
1522 struct rlimit __user
*, rlim
)
1525 if (resource
>= RLIM_NLIMITS
)
1528 task_lock(current
->group_leader
);
1529 x
= current
->signal
->rlim
[resource
];
1530 task_unlock(current
->group_leader
);
1531 if (x
.rlim_cur
> 0x7FFFFFFF)
1532 x
.rlim_cur
= 0x7FFFFFFF;
1533 if (x
.rlim_max
> 0x7FFFFFFF)
1534 x
.rlim_max
= 0x7FFFFFFF;
1535 return copy_to_user(rlim
, &x
, sizeof(x
))?-EFAULT
:0;
1540 SYSCALL_DEFINE2(setrlimit
, unsigned int, resource
, struct rlimit __user
*, rlim
)
1542 struct rlimit new_rlim
, *old_rlim
;
1545 if (resource
>= RLIM_NLIMITS
)
1547 if (copy_from_user(&new_rlim
, rlim
, sizeof(*rlim
)))
1549 if (new_rlim
.rlim_cur
> new_rlim
.rlim_max
)
1551 old_rlim
= current
->signal
->rlim
+ resource
;
1552 if ((new_rlim
.rlim_max
> old_rlim
->rlim_max
) &&
1553 !capable(CAP_SYS_RESOURCE
))
1555 if (resource
== RLIMIT_NOFILE
&& new_rlim
.rlim_max
> sysctl_nr_open
)
1558 retval
= security_task_setrlimit(resource
, &new_rlim
);
1562 if (resource
== RLIMIT_CPU
&& new_rlim
.rlim_cur
== 0) {
1564 * The caller is asking for an immediate RLIMIT_CPU
1565 * expiry. But we use the zero value to mean "it was
1566 * never set". So let's cheat and make it one second
1569 new_rlim
.rlim_cur
= 1;
1572 task_lock(current
->group_leader
);
1573 *old_rlim
= new_rlim
;
1574 task_unlock(current
->group_leader
);
1576 if (resource
!= RLIMIT_CPU
)
1580 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1581 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1582 * very long-standing error, and fixing it now risks breakage of
1583 * applications, so we live with it
1585 if (new_rlim
.rlim_cur
== RLIM_INFINITY
)
1588 update_rlimit_cpu(new_rlim
.rlim_cur
);
1594 * It would make sense to put struct rusage in the task_struct,
1595 * except that would make the task_struct be *really big*. After
1596 * task_struct gets moved into malloc'ed memory, it would
1597 * make sense to do this. It will make moving the rest of the information
1598 * a lot simpler! (Which we're not doing right now because we're not
1599 * measuring them yet).
1601 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1602 * races with threads incrementing their own counters. But since word
1603 * reads are atomic, we either get new values or old values and we don't
1604 * care which for the sums. We always take the siglock to protect reading
1605 * the c* fields from p->signal from races with exit.c updating those
1606 * fields when reaping, so a sample either gets all the additions of a
1607 * given child after it's reaped, or none so this sample is before reaping.
1610 * We need to take the siglock for CHILDEREN, SELF and BOTH
1611 * for the cases current multithreaded, non-current single threaded
1612 * non-current multithreaded. Thread traversal is now safe with
1614 * Strictly speaking, we donot need to take the siglock if we are current and
1615 * single threaded, as no one else can take our signal_struct away, no one
1616 * else can reap the children to update signal->c* counters, and no one else
1617 * can race with the signal-> fields. If we do not take any lock, the
1618 * signal-> fields could be read out of order while another thread was just
1619 * exiting. So we should place a read memory barrier when we avoid the lock.
1620 * On the writer side, write memory barrier is implied in __exit_signal
1621 * as __exit_signal releases the siglock spinlock after updating the signal->
1622 * fields. But we don't do this yet to keep things simple.
1626 static void accumulate_thread_rusage(struct task_struct
*t
, struct rusage
*r
)
1628 r
->ru_nvcsw
+= t
->nvcsw
;
1629 r
->ru_nivcsw
+= t
->nivcsw
;
1630 r
->ru_minflt
+= t
->min_flt
;
1631 r
->ru_majflt
+= t
->maj_flt
;
1632 r
->ru_inblock
+= task_io_get_inblock(t
);
1633 r
->ru_oublock
+= task_io_get_oublock(t
);
1636 static void k_getrusage(struct task_struct
*p
, int who
, struct rusage
*r
)
1638 struct task_struct
*t
;
1639 unsigned long flags
;
1640 cputime_t utime
, stime
;
1641 struct task_cputime cputime
;
1643 memset((char *) r
, 0, sizeof *r
);
1644 utime
= stime
= cputime_zero
;
1646 if (who
== RUSAGE_THREAD
) {
1647 utime
= task_utime(current
);
1648 stime
= task_stime(current
);
1649 accumulate_thread_rusage(p
, r
);
1653 if (!lock_task_sighand(p
, &flags
))
1658 case RUSAGE_CHILDREN
:
1659 utime
= p
->signal
->cutime
;
1660 stime
= p
->signal
->cstime
;
1661 r
->ru_nvcsw
= p
->signal
->cnvcsw
;
1662 r
->ru_nivcsw
= p
->signal
->cnivcsw
;
1663 r
->ru_minflt
= p
->signal
->cmin_flt
;
1664 r
->ru_majflt
= p
->signal
->cmaj_flt
;
1665 r
->ru_inblock
= p
->signal
->cinblock
;
1666 r
->ru_oublock
= p
->signal
->coublock
;
1668 if (who
== RUSAGE_CHILDREN
)
1672 thread_group_cputime(p
, &cputime
);
1673 utime
= cputime_add(utime
, cputime
.utime
);
1674 stime
= cputime_add(stime
, cputime
.stime
);
1675 r
->ru_nvcsw
+= p
->signal
->nvcsw
;
1676 r
->ru_nivcsw
+= p
->signal
->nivcsw
;
1677 r
->ru_minflt
+= p
->signal
->min_flt
;
1678 r
->ru_majflt
+= p
->signal
->maj_flt
;
1679 r
->ru_inblock
+= p
->signal
->inblock
;
1680 r
->ru_oublock
+= p
->signal
->oublock
;
1683 accumulate_thread_rusage(t
, r
);
1691 unlock_task_sighand(p
, &flags
);
1694 cputime_to_timeval(utime
, &r
->ru_utime
);
1695 cputime_to_timeval(stime
, &r
->ru_stime
);
1698 int getrusage(struct task_struct
*p
, int who
, struct rusage __user
*ru
)
1701 k_getrusage(p
, who
, &r
);
1702 return copy_to_user(ru
, &r
, sizeof(r
)) ? -EFAULT
: 0;
1705 SYSCALL_DEFINE2(getrusage
, int, who
, struct rusage __user
*, ru
)
1707 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
&&
1708 who
!= RUSAGE_THREAD
)
1710 return getrusage(current
, who
, ru
);
1713 SYSCALL_DEFINE1(umask
, int, mask
)
1715 mask
= xchg(¤t
->fs
->umask
, mask
& S_IRWXUGO
);
1719 SYSCALL_DEFINE5(prctl
, int, option
, unsigned long, arg2
, unsigned long, arg3
,
1720 unsigned long, arg4
, unsigned long, arg5
)
1722 struct task_struct
*me
= current
;
1723 unsigned char comm
[sizeof(me
->comm
)];
1726 error
= security_task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1727 if (error
!= -ENOSYS
)
1732 case PR_SET_PDEATHSIG
:
1733 if (!valid_signal(arg2
)) {
1737 me
->pdeath_signal
= arg2
;
1740 case PR_GET_PDEATHSIG
:
1741 error
= put_user(me
->pdeath_signal
, (int __user
*)arg2
);
1743 case PR_GET_DUMPABLE
:
1744 error
= get_dumpable(me
->mm
);
1746 case PR_SET_DUMPABLE
:
1747 if (arg2
< 0 || arg2
> 1) {
1751 set_dumpable(me
->mm
, arg2
);
1755 case PR_SET_UNALIGN
:
1756 error
= SET_UNALIGN_CTL(me
, arg2
);
1758 case PR_GET_UNALIGN
:
1759 error
= GET_UNALIGN_CTL(me
, arg2
);
1762 error
= SET_FPEMU_CTL(me
, arg2
);
1765 error
= GET_FPEMU_CTL(me
, arg2
);
1768 error
= SET_FPEXC_CTL(me
, arg2
);
1771 error
= GET_FPEXC_CTL(me
, arg2
);
1774 error
= PR_TIMING_STATISTICAL
;
1777 if (arg2
!= PR_TIMING_STATISTICAL
)
1784 comm
[sizeof(me
->comm
)-1] = 0;
1785 if (strncpy_from_user(comm
, (char __user
*)arg2
,
1786 sizeof(me
->comm
) - 1) < 0)
1788 set_task_comm(me
, comm
);
1791 get_task_comm(comm
, me
);
1792 if (copy_to_user((char __user
*)arg2
, comm
,
1797 error
= GET_ENDIAN(me
, arg2
);
1800 error
= SET_ENDIAN(me
, arg2
);
1803 case PR_GET_SECCOMP
:
1804 error
= prctl_get_seccomp();
1806 case PR_SET_SECCOMP
:
1807 error
= prctl_set_seccomp(arg2
);
1810 error
= GET_TSC_CTL(arg2
);
1813 error
= SET_TSC_CTL(arg2
);
1815 case PR_TASK_PERF_COUNTERS_DISABLE
:
1816 error
= perf_counter_task_disable();
1818 case PR_TASK_PERF_COUNTERS_ENABLE
:
1819 error
= perf_counter_task_enable();
1821 case PR_GET_TIMERSLACK
:
1822 error
= current
->timer_slack_ns
;
1824 case PR_SET_TIMERSLACK
:
1826 current
->timer_slack_ns
=
1827 current
->default_timer_slack_ns
;
1829 current
->timer_slack_ns
= arg2
;
1839 SYSCALL_DEFINE3(getcpu
, unsigned __user
*, cpup
, unsigned __user
*, nodep
,
1840 struct getcpu_cache __user
*, unused
)
1843 int cpu
= raw_smp_processor_id();
1845 err
|= put_user(cpu
, cpup
);
1847 err
|= put_user(cpu_to_node(cpu
), nodep
);
1848 return err
? -EFAULT
: 0;
1851 char poweroff_cmd
[POWEROFF_CMD_PATH_LEN
] = "/sbin/poweroff";
1853 static void argv_cleanup(char **argv
, char **envp
)
1859 * orderly_poweroff - Trigger an orderly system poweroff
1860 * @force: force poweroff if command execution fails
1862 * This may be called from any context to trigger a system shutdown.
1863 * If the orderly shutdown fails, it will force an immediate shutdown.
1865 int orderly_poweroff(bool force
)
1868 char **argv
= argv_split(GFP_ATOMIC
, poweroff_cmd
, &argc
);
1869 static char *envp
[] = {
1871 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
1875 struct subprocess_info
*info
;
1878 printk(KERN_WARNING
"%s failed to allocate memory for \"%s\"\n",
1879 __func__
, poweroff_cmd
);
1883 info
= call_usermodehelper_setup(argv
[0], argv
, envp
, GFP_ATOMIC
);
1889 call_usermodehelper_setcleanup(info
, argv_cleanup
);
1891 ret
= call_usermodehelper_exec(info
, UMH_NO_WAIT
);
1895 printk(KERN_WARNING
"Failed to start orderly shutdown: "
1896 "forcing the issue\n");
1898 /* I guess this should try to kick off some daemon to
1899 sync and poweroff asap. Or not even bother syncing
1900 if we're doing an emergency shutdown? */
1907 EXPORT_SYMBOL_GPL(orderly_poweroff
);