4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/module.h>
10 #include <linux/utsname.h>
11 #include <linux/mman.h>
12 #include <linux/smp_lock.h>
13 #include <linux/notifier.h>
14 #include <linux/reboot.h>
15 #include <linux/prctl.h>
16 #include <linux/init.h>
17 #include <linux/highuid.h>
19 #include <linux/workqueue.h>
20 #include <linux/device.h>
21 #include <linux/times.h>
22 #include <linux/security.h>
23 #include <linux/dcookies.h>
24 #include <linux/suspend.h>
26 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
30 #ifndef SET_UNALIGN_CTL
31 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
33 #ifndef GET_UNALIGN_CTL
34 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
37 # define SET_FPEMU_CTL(a,b) (-EINVAL)
40 # define GET_FPEMU_CTL(a,b) (-EINVAL)
43 # define SET_FPEXC_CTL(a,b) (-EINVAL)
46 # define GET_FPEXC_CTL(a,b) (-EINVAL)
50 * this is where the system-wide overflow UID and GID are defined, for
51 * architectures that now have 32-bit UID/GID but didn't in the past
54 int overflowuid
= DEFAULT_OVERFLOWUID
;
55 int overflowgid
= DEFAULT_OVERFLOWGID
;
58 * the same as above, but for filesystems which can only store a 16-bit
59 * UID and GID. as such, this is needed on all architectures
62 int fs_overflowuid
= DEFAULT_FS_OVERFLOWUID
;
63 int fs_overflowgid
= DEFAULT_FS_OVERFLOWUID
;
66 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
72 extern int system_running
;
75 * Notifier list for kernel code which wants to be called
76 * at shutdown. This is used to stop any idling DMA operations
80 static struct notifier_block
*reboot_notifier_list
;
81 rwlock_t notifier_lock
= RW_LOCK_UNLOCKED
;
84 * notifier_chain_register - Add notifier to a notifier chain
85 * @list: Pointer to root list pointer
86 * @n: New entry in notifier chain
88 * Adds a notifier to a notifier chain.
90 * Currently always returns zero.
93 int notifier_chain_register(struct notifier_block
**list
, struct notifier_block
*n
)
95 write_lock(¬ifier_lock
);
98 if(n
->priority
> (*list
)->priority
)
100 list
= &((*list
)->next
);
104 write_unlock(¬ifier_lock
);
109 * notifier_chain_unregister - Remove notifier from a notifier chain
110 * @nl: Pointer to root list pointer
111 * @n: New entry in notifier chain
113 * Removes a notifier from a notifier chain.
115 * Returns zero on success, or %-ENOENT on failure.
118 int notifier_chain_unregister(struct notifier_block
**nl
, struct notifier_block
*n
)
120 write_lock(¬ifier_lock
);
126 write_unlock(¬ifier_lock
);
131 write_unlock(¬ifier_lock
);
136 * notifier_call_chain - Call functions in a notifier chain
137 * @n: Pointer to root pointer of notifier chain
138 * @val: Value passed unmodified to notifier function
139 * @v: Pointer passed unmodified to notifier function
141 * Calls each function in a notifier chain in turn.
143 * If the return value of the notifier can be and'd
144 * with %NOTIFY_STOP_MASK, then notifier_call_chain
145 * will return immediately, with the return value of
146 * the notifier function which halted execution.
147 * Otherwise, the return value is the return value
148 * of the last notifier function called.
151 int notifier_call_chain(struct notifier_block
**n
, unsigned long val
, void *v
)
154 struct notifier_block
*nb
= *n
;
158 ret
=nb
->notifier_call(nb
,val
,v
);
159 if(ret
&NOTIFY_STOP_MASK
)
169 * register_reboot_notifier - Register function to be called at reboot time
170 * @nb: Info about notifier function to be called
172 * Registers a function with the list of functions
173 * to be called at reboot time.
175 * Currently always returns zero, as notifier_chain_register
176 * always returns zero.
179 int register_reboot_notifier(struct notifier_block
* nb
)
181 return notifier_chain_register(&reboot_notifier_list
, nb
);
185 * unregister_reboot_notifier - Unregister previously registered reboot notifier
186 * @nb: Hook to be unregistered
188 * Unregisters a previously registered reboot
191 * Returns zero on success, or %-ENOENT on failure.
194 int unregister_reboot_notifier(struct notifier_block
* nb
)
196 return notifier_chain_unregister(&reboot_notifier_list
, nb
);
199 asmlinkage
long sys_ni_syscall(void)
204 cond_syscall(sys_nfsservctl
)
205 cond_syscall(sys_quotactl
)
206 cond_syscall(sys_acct
)
207 cond_syscall(sys_lookup_dcookie
)
208 cond_syscall(sys_swapon
)
209 cond_syscall(sys_swapoff
)
210 cond_syscall(sys_init_module
)
211 cond_syscall(sys_delete_module
)
212 cond_syscall(sys_socketpair
)
213 cond_syscall(sys_bind
)
214 cond_syscall(sys_listen
)
215 cond_syscall(sys_accept
)
216 cond_syscall(sys_connect
)
217 cond_syscall(sys_getsockname
)
218 cond_syscall(sys_getpeername
)
219 cond_syscall(sys_sendto
)
220 cond_syscall(sys_send
)
221 cond_syscall(sys_recvfrom
)
222 cond_syscall(sys_recv
)
223 cond_syscall(sys_setsockopt
)
224 cond_syscall(sys_getsockopt
)
225 cond_syscall(sys_shutdown
)
226 cond_syscall(sys_sendmsg
)
227 cond_syscall(sys_recvmsg
)
228 cond_syscall(sys_socketcall
)
229 cond_syscall(sys_futex
)
230 cond_syscall(compat_sys_futex
)
231 cond_syscall(sys_epoll_create
)
232 cond_syscall(sys_epoll_ctl
)
233 cond_syscall(sys_epoll_wait
)
235 static int set_one_prio(struct task_struct
*p
, int niceval
, int error
)
239 if (p
->uid
!= current
->euid
&&
240 p
->uid
!= current
->uid
&& !capable(CAP_SYS_NICE
)) {
244 if (niceval
< task_nice(p
) && !capable(CAP_SYS_NICE
)) {
248 no_nice
= security_task_setnice(p
, niceval
);
255 set_user_nice(p
, niceval
);
260 asmlinkage
long sys_setpriority(int which
, int who
, int niceval
)
262 struct task_struct
*g
, *p
;
263 struct user_struct
*user
;
268 if (which
> 2 || which
< 0)
271 /* normalize: avoid signed division (rounding problems) */
278 read_lock(&tasklist_lock
);
283 p
= find_task_by_pid(who
);
285 error
= set_one_prio(p
, niceval
, error
);
290 for_each_task_pid(who
, PIDTYPE_PGID
, p
, l
, pid
)
291 error
= set_one_prio(p
, niceval
, error
);
295 user
= current
->user
;
297 user
= find_user(who
);
304 error
= set_one_prio(p
, niceval
, error
);
305 while_each_thread(g
, p
);
309 read_unlock(&tasklist_lock
);
315 * Ugh. To avoid negative return values, "getpriority()" will
316 * not return the normal nice-value, but a negated value that
317 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
318 * to stay compatible.
320 asmlinkage
long sys_getpriority(int which
, int who
)
322 struct task_struct
*g
, *p
;
325 struct user_struct
*user
;
326 long niceval
, retval
= -ESRCH
;
328 if (which
> 2 || which
< 0)
331 read_lock(&tasklist_lock
);
336 p
= find_task_by_pid(who
);
338 niceval
= 20 - task_nice(p
);
339 if (niceval
> retval
)
346 for_each_task_pid(who
, PIDTYPE_PGID
, p
, l
, pid
) {
347 niceval
= 20 - task_nice(p
);
348 if (niceval
> retval
)
354 user
= current
->user
;
356 user
= find_user(who
);
363 niceval
= 20 - task_nice(p
);
364 if (niceval
> retval
)
367 while_each_thread(g
, p
);
371 read_unlock(&tasklist_lock
);
378 * Reboot system call: for obvious reasons only root may call it,
379 * and even root needs to set up some magic numbers in the registers
380 * so that some mistake won't make this reboot the whole machine.
381 * You can also set the meaning of the ctrl-alt-del-key here.
383 * reboot doesn't sync: do that yourself before calling this.
385 asmlinkage
long sys_reboot(int magic1
, int magic2
, unsigned int cmd
, void __user
* arg
)
389 /* We only trust the superuser with rebooting the system. */
390 if (!capable(CAP_SYS_BOOT
))
393 /* For safety, we require "magic" arguments. */
394 if (magic1
!= LINUX_REBOOT_MAGIC1
||
395 (magic2
!= LINUX_REBOOT_MAGIC2
&& magic2
!= LINUX_REBOOT_MAGIC2A
&&
396 magic2
!= LINUX_REBOOT_MAGIC2B
))
401 case LINUX_REBOOT_CMD_RESTART
:
402 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, NULL
);
405 printk(KERN_EMERG
"Restarting system.\n");
406 machine_restart(NULL
);
409 case LINUX_REBOOT_CMD_CAD_ON
:
413 case LINUX_REBOOT_CMD_CAD_OFF
:
417 case LINUX_REBOOT_CMD_HALT
:
418 notifier_call_chain(&reboot_notifier_list
, SYS_HALT
, NULL
);
421 printk(KERN_EMERG
"System halted.\n");
427 case LINUX_REBOOT_CMD_POWER_OFF
:
428 notifier_call_chain(&reboot_notifier_list
, SYS_POWER_OFF
, NULL
);
431 printk(KERN_EMERG
"Power down.\n");
437 case LINUX_REBOOT_CMD_RESTART2
:
438 if (strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1) < 0) {
442 buffer
[sizeof(buffer
) - 1] = '\0';
444 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, buffer
);
447 printk(KERN_EMERG
"Restarting system with command '%s'.\n", buffer
);
448 machine_restart(buffer
);
451 #ifdef CONFIG_SOFTWARE_SUSPEND
452 case LINUX_REBOOT_CMD_SW_SUSPEND
:
453 if (!software_suspend_enabled
) {
470 static void deferred_cad(void *dummy
)
472 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, NULL
);
473 machine_restart(NULL
);
477 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
478 * As it's called within an interrupt, it may NOT sync: the only choice
479 * is whether to reboot at once, or just ignore the ctrl-alt-del.
481 void ctrl_alt_del(void)
483 static DECLARE_WORK(cad_work
, deferred_cad
, NULL
);
486 schedule_work(&cad_work
);
488 kill_proc(cad_pid
, SIGINT
, 1);
493 * Unprivileged users may change the real gid to the effective gid
494 * or vice versa. (BSD-style)
496 * If you set the real gid at all, or set the effective gid to a value not
497 * equal to the real gid, then the saved gid is set to the new effective gid.
499 * This makes it possible for a setgid program to completely drop its
500 * privileges, which is often a useful assertion to make when you are doing
501 * a security audit over a program.
503 * The general idea is that a program which uses just setregid() will be
504 * 100% compatible with BSD. A program which uses just setgid() will be
505 * 100% compatible with POSIX with saved IDs.
507 * SMP: There are not races, the GIDs are checked only by filesystem
508 * operations (as far as semantic preservation is concerned).
510 asmlinkage
long sys_setregid(gid_t rgid
, gid_t egid
)
512 int old_rgid
= current
->gid
;
513 int old_egid
= current
->egid
;
514 int new_rgid
= old_rgid
;
515 int new_egid
= old_egid
;
518 retval
= security_task_setgid(rgid
, egid
, (gid_t
)-1, LSM_SETID_RE
);
522 if (rgid
!= (gid_t
) -1) {
523 if ((old_rgid
== rgid
) ||
524 (current
->egid
==rgid
) ||
530 if (egid
!= (gid_t
) -1) {
531 if ((old_rgid
== egid
) ||
532 (current
->egid
== egid
) ||
533 (current
->sgid
== egid
) ||
540 if (new_egid
!= old_egid
)
542 current
->mm
->dumpable
= 0;
545 if (rgid
!= (gid_t
) -1 ||
546 (egid
!= (gid_t
) -1 && egid
!= old_rgid
))
547 current
->sgid
= new_egid
;
548 current
->fsgid
= new_egid
;
549 current
->egid
= new_egid
;
550 current
->gid
= new_rgid
;
555 * setgid() is implemented like SysV w/ SAVED_IDS
557 * SMP: Same implicit races as above.
559 asmlinkage
long sys_setgid(gid_t gid
)
561 int old_egid
= current
->egid
;
564 retval
= security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_ID
);
568 if (capable(CAP_SETGID
))
572 current
->mm
->dumpable
=0;
575 current
->gid
= current
->egid
= current
->sgid
= current
->fsgid
= gid
;
577 else if ((gid
== current
->gid
) || (gid
== current
->sgid
))
581 current
->mm
->dumpable
=0;
584 current
->egid
= current
->fsgid
= gid
;
591 static int set_user(uid_t new_ruid
, int dumpclear
)
593 struct user_struct
*new_user
;
595 new_user
= alloc_uid(new_ruid
);
598 switch_uid(new_user
);
602 current
->mm
->dumpable
= 0;
605 current
->uid
= new_ruid
;
610 * Unprivileged users may change the real uid to the effective uid
611 * or vice versa. (BSD-style)
613 * If you set the real uid at all, or set the effective uid to a value not
614 * equal to the real uid, then the saved uid is set to the new effective uid.
616 * This makes it possible for a setuid program to completely drop its
617 * privileges, which is often a useful assertion to make when you are doing
618 * a security audit over a program.
620 * The general idea is that a program which uses just setreuid() will be
621 * 100% compatible with BSD. A program which uses just setuid() will be
622 * 100% compatible with POSIX with saved IDs.
624 asmlinkage
long sys_setreuid(uid_t ruid
, uid_t euid
)
626 int old_ruid
, old_euid
, old_suid
, new_ruid
, new_euid
;
629 retval
= security_task_setuid(ruid
, euid
, (uid_t
)-1, LSM_SETID_RE
);
633 new_ruid
= old_ruid
= current
->uid
;
634 new_euid
= old_euid
= current
->euid
;
635 old_suid
= current
->suid
;
637 if (ruid
!= (uid_t
) -1) {
639 if ((old_ruid
!= ruid
) &&
640 (current
->euid
!= ruid
) &&
641 !capable(CAP_SETUID
))
645 if (euid
!= (uid_t
) -1) {
647 if ((old_ruid
!= euid
) &&
648 (current
->euid
!= euid
) &&
649 (current
->suid
!= euid
) &&
650 !capable(CAP_SETUID
))
654 if (new_ruid
!= old_ruid
&& set_user(new_ruid
, new_euid
!= old_euid
) < 0)
657 if (new_euid
!= old_euid
)
659 current
->mm
->dumpable
=0;
662 current
->fsuid
= current
->euid
= new_euid
;
663 if (ruid
!= (uid_t
) -1 ||
664 (euid
!= (uid_t
) -1 && euid
!= old_ruid
))
665 current
->suid
= current
->euid
;
666 current
->fsuid
= current
->euid
;
668 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RE
);
674 * setuid() is implemented like SysV with SAVED_IDS
676 * Note that SAVED_ID's is deficient in that a setuid root program
677 * like sendmail, for example, cannot set its uid to be a normal
678 * user and then switch back, because if you're root, setuid() sets
679 * the saved uid too. If you don't like this, blame the bright people
680 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
681 * will allow a root program to temporarily drop privileges and be able to
682 * regain them by swapping the real and effective uid.
684 asmlinkage
long sys_setuid(uid_t uid
)
686 int old_euid
= current
->euid
;
687 int old_ruid
, old_suid
, new_ruid
, new_suid
;
690 retval
= security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_ID
);
694 old_ruid
= new_ruid
= current
->uid
;
695 old_suid
= current
->suid
;
698 if (capable(CAP_SETUID
)) {
699 if (uid
!= old_ruid
&& set_user(uid
, old_euid
!= uid
) < 0)
702 } else if ((uid
!= current
->uid
) && (uid
!= new_suid
))
707 current
->mm
->dumpable
= 0;
710 current
->fsuid
= current
->euid
= uid
;
711 current
->suid
= new_suid
;
713 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_ID
);
718 * This function implements a generic ability to update ruid, euid,
719 * and suid. This allows you to implement the 4.4 compatible seteuid().
721 asmlinkage
long sys_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
723 int old_ruid
= current
->uid
;
724 int old_euid
= current
->euid
;
725 int old_suid
= current
->suid
;
728 retval
= security_task_setuid(ruid
, euid
, suid
, LSM_SETID_RES
);
732 if (!capable(CAP_SETUID
)) {
733 if ((ruid
!= (uid_t
) -1) && (ruid
!= current
->uid
) &&
734 (ruid
!= current
->euid
) && (ruid
!= current
->suid
))
736 if ((euid
!= (uid_t
) -1) && (euid
!= current
->uid
) &&
737 (euid
!= current
->euid
) && (euid
!= current
->suid
))
739 if ((suid
!= (uid_t
) -1) && (suid
!= current
->uid
) &&
740 (suid
!= current
->euid
) && (suid
!= current
->suid
))
743 if (ruid
!= (uid_t
) -1) {
744 if (ruid
!= current
->uid
&& set_user(ruid
, euid
!= current
->euid
) < 0)
747 if (euid
!= (uid_t
) -1) {
748 if (euid
!= current
->euid
)
750 current
->mm
->dumpable
= 0;
753 current
->euid
= euid
;
755 current
->fsuid
= current
->euid
;
756 if (suid
!= (uid_t
) -1)
757 current
->suid
= suid
;
759 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RES
);
762 asmlinkage
long sys_getresuid(uid_t
*ruid
, uid_t
*euid
, uid_t
*suid
)
766 if (!(retval
= put_user(current
->uid
, ruid
)) &&
767 !(retval
= put_user(current
->euid
, euid
)))
768 retval
= put_user(current
->suid
, suid
);
774 * Same as above, but for rgid, egid, sgid.
776 asmlinkage
long sys_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
780 retval
= security_task_setgid(rgid
, egid
, sgid
, LSM_SETID_RES
);
784 if (!capable(CAP_SETGID
)) {
785 if ((rgid
!= (gid_t
) -1) && (rgid
!= current
->gid
) &&
786 (rgid
!= current
->egid
) && (rgid
!= current
->sgid
))
788 if ((egid
!= (gid_t
) -1) && (egid
!= current
->gid
) &&
789 (egid
!= current
->egid
) && (egid
!= current
->sgid
))
791 if ((sgid
!= (gid_t
) -1) && (sgid
!= current
->gid
) &&
792 (sgid
!= current
->egid
) && (sgid
!= current
->sgid
))
795 if (egid
!= (gid_t
) -1) {
796 if (egid
!= current
->egid
)
798 current
->mm
->dumpable
= 0;
801 current
->egid
= egid
;
803 current
->fsgid
= current
->egid
;
804 if (rgid
!= (gid_t
) -1)
806 if (sgid
!= (gid_t
) -1)
807 current
->sgid
= sgid
;
811 asmlinkage
long sys_getresgid(gid_t
*rgid
, gid_t
*egid
, gid_t
*sgid
)
815 if (!(retval
= put_user(current
->gid
, rgid
)) &&
816 !(retval
= put_user(current
->egid
, egid
)))
817 retval
= put_user(current
->sgid
, sgid
);
824 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
825 * is used for "access()" and for the NFS daemon (letting nfsd stay at
826 * whatever uid it wants to). It normally shadows "euid", except when
827 * explicitly set by setfsuid() or for access..
829 asmlinkage
long sys_setfsuid(uid_t uid
)
834 retval
= security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
);
838 old_fsuid
= current
->fsuid
;
839 if (uid
== current
->uid
|| uid
== current
->euid
||
840 uid
== current
->suid
|| uid
== current
->fsuid
||
843 if (uid
!= old_fsuid
)
845 current
->mm
->dumpable
= 0;
848 current
->fsuid
= uid
;
851 retval
= security_task_post_setuid(old_fsuid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
);
859 * Samma på svenska..
861 asmlinkage
long sys_setfsgid(gid_t gid
)
866 retval
= security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_FS
);
870 old_fsgid
= current
->fsgid
;
871 if (gid
== current
->gid
|| gid
== current
->egid
||
872 gid
== current
->sgid
|| gid
== current
->fsgid
||
875 if (gid
!= old_fsgid
)
877 current
->mm
->dumpable
= 0;
880 current
->fsgid
= gid
;
885 asmlinkage
long sys_times(struct tms __user
* tbuf
)
888 * In the SMP world we might just be unlucky and have one of
889 * the times increment as we use it. Since the value is an
890 * atomically safe type this is just fine. Conceptually its
891 * as if the syscall took an instant longer to occur.
895 tmp
.tms_utime
= jiffies_to_clock_t(current
->utime
);
896 tmp
.tms_stime
= jiffies_to_clock_t(current
->stime
);
897 tmp
.tms_cutime
= jiffies_to_clock_t(current
->cutime
);
898 tmp
.tms_cstime
= jiffies_to_clock_t(current
->cstime
);
899 if (copy_to_user(tbuf
, &tmp
, sizeof(struct tms
)))
902 return (long) jiffies_64_to_clock_t(get_jiffies_64());
906 * This needs some heavy checking ...
907 * I just haven't the stomach for it. I also don't fully
908 * understand sessions/pgrp etc. Let somebody who does explain it.
910 * OK, I think I have the protection semantics right.... this is really
911 * only important on a multi-user system anyway, to make sure one user
912 * can't send a signal to a process owned by another. -TYT, 12/12/91
914 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
918 asmlinkage
long sys_setpgid(pid_t pid
, pid_t pgid
)
920 struct task_struct
*p
;
930 /* From this point forward we keep holding onto the tasklist lock
931 * so that our parent does not change from under us. -DaveM
933 write_lock_irq(&tasklist_lock
);
936 p
= find_task_by_pid(pid
);
941 if (!thread_group_leader(p
))
944 if (p
->parent
== current
|| p
->real_parent
== current
) {
946 if (p
->session
!= current
->session
)
962 struct task_struct
*p
;
966 for_each_task_pid(pgid
, PIDTYPE_PGID
, p
, l
, pid
)
967 if (p
->session
== current
->session
)
973 err
= security_task_setpgid(p
, pgid
);
977 if (p
->pgrp
!= pgid
) {
978 detach_pid(p
, PIDTYPE_PGID
);
980 attach_pid(p
, PIDTYPE_PGID
, pgid
);
984 /* All paths lead to here, thus we are safe. -DaveM */
985 write_unlock_irq(&tasklist_lock
);
989 asmlinkage
long sys_getpgid(pid_t pid
)
992 return current
->pgrp
;
995 struct task_struct
*p
;
997 read_lock(&tasklist_lock
);
998 p
= find_task_by_pid(pid
);
1002 retval
= security_task_getpgid(p
);
1006 read_unlock(&tasklist_lock
);
1011 asmlinkage
long sys_getpgrp(void)
1013 /* SMP - assuming writes are word atomic this is fine */
1014 return current
->pgrp
;
1017 asmlinkage
long sys_getsid(pid_t pid
)
1020 return current
->session
;
1023 struct task_struct
*p
;
1025 read_lock(&tasklist_lock
);
1026 p
= find_task_by_pid(pid
);
1030 retval
= security_task_getsid(p
);
1032 retval
= p
->session
;
1034 read_unlock(&tasklist_lock
);
1039 asmlinkage
long sys_setsid(void)
1044 if (!thread_group_leader(current
))
1047 write_lock_irq(&tasklist_lock
);
1049 pid
= find_pid(PIDTYPE_PGID
, current
->pid
);
1053 current
->leader
= 1;
1054 __set_special_pids(current
->pid
, current
->pid
);
1055 current
->tty
= NULL
;
1056 current
->tty_old_pgrp
= 0;
1057 err
= current
->pgrp
;
1059 write_unlock_irq(&tasklist_lock
);
1064 * Supplementary group IDs
1066 asmlinkage
long sys_getgroups(int gidsetsize
, gid_t __user
*grouplist
)
1071 * SMP: Nobody else can change our grouplist. Thus we are
1077 i
= current
->ngroups
;
1081 if (copy_to_user(grouplist
, current
->groups
, sizeof(gid_t
)*i
))
1088 * SMP: Our groups are not shared. We can copy to/from them safely
1089 * without another task interfering.
1092 asmlinkage
long sys_setgroups(int gidsetsize
, gid_t __user
*grouplist
)
1094 gid_t groups
[NGROUPS
];
1097 if (!capable(CAP_SETGID
))
1099 if ((unsigned) gidsetsize
> NGROUPS
)
1101 if (copy_from_user(groups
, grouplist
, gidsetsize
* sizeof(gid_t
)))
1103 retval
= security_task_setgroups(gidsetsize
, groups
);
1106 memcpy(current
->groups
, groups
, gidsetsize
* sizeof(gid_t
));
1107 current
->ngroups
= gidsetsize
;
1111 static int supplemental_group_member(gid_t grp
)
1113 int i
= current
->ngroups
;
1116 gid_t
*groups
= current
->groups
;
1128 * Check whether we're fsgid/egid or in the supplemental group..
1130 int in_group_p(gid_t grp
)
1133 if (grp
!= current
->fsgid
)
1134 retval
= supplemental_group_member(grp
);
1138 int in_egroup_p(gid_t grp
)
1141 if (grp
!= current
->egid
)
1142 retval
= supplemental_group_member(grp
);
1146 DECLARE_RWSEM(uts_sem
);
1148 asmlinkage
long sys_newuname(struct new_utsname __user
* name
)
1152 down_read(&uts_sem
);
1153 if (copy_to_user(name
,&system_utsname
,sizeof *name
))
1159 asmlinkage
long sys_sethostname(char __user
*name
, int len
)
1163 if (!capable(CAP_SYS_ADMIN
))
1165 if (len
< 0 || len
> __NEW_UTS_LEN
)
1167 down_write(&uts_sem
);
1169 if (!copy_from_user(system_utsname
.nodename
, name
, len
)) {
1170 system_utsname
.nodename
[len
] = 0;
1177 asmlinkage
long sys_gethostname(char __user
*name
, int len
)
1183 down_read(&uts_sem
);
1184 i
= 1 + strlen(system_utsname
.nodename
);
1188 if (copy_to_user(name
, system_utsname
.nodename
, i
))
1195 * Only setdomainname; getdomainname can be implemented by calling
1198 asmlinkage
long sys_setdomainname(char __user
*name
, int len
)
1202 if (!capable(CAP_SYS_ADMIN
))
1204 if (len
< 0 || len
> __NEW_UTS_LEN
)
1207 down_write(&uts_sem
);
1209 if (!copy_from_user(system_utsname
.domainname
, name
, len
)) {
1211 system_utsname
.domainname
[len
] = 0;
1217 asmlinkage
long sys_getrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1219 if (resource
>= RLIM_NLIMITS
)
1222 return copy_to_user(rlim
, current
->rlim
+ resource
, sizeof(*rlim
))
1226 #if !defined(__ia64__) && !defined(CONFIG_V850)
1229 * Back compatibility for getrlimit. Needed for some apps.
1232 asmlinkage
long sys_old_getrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1235 if (resource
>= RLIM_NLIMITS
)
1238 memcpy(&x
, current
->rlim
+ resource
, sizeof(*rlim
));
1239 if(x
.rlim_cur
> 0x7FFFFFFF)
1240 x
.rlim_cur
= 0x7FFFFFFF;
1241 if(x
.rlim_max
> 0x7FFFFFFF)
1242 x
.rlim_max
= 0x7FFFFFFF;
1243 return copy_to_user(rlim
, &x
, sizeof(x
))?-EFAULT
:0;
1248 asmlinkage
long sys_setrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1250 struct rlimit new_rlim
, *old_rlim
;
1253 if (resource
>= RLIM_NLIMITS
)
1255 if(copy_from_user(&new_rlim
, rlim
, sizeof(*rlim
)))
1257 if (new_rlim
.rlim_cur
> new_rlim
.rlim_max
)
1259 old_rlim
= current
->rlim
+ resource
;
1260 if (((new_rlim
.rlim_cur
> old_rlim
->rlim_max
) ||
1261 (new_rlim
.rlim_max
> old_rlim
->rlim_max
)) &&
1262 !capable(CAP_SYS_RESOURCE
))
1264 if (resource
== RLIMIT_NOFILE
) {
1265 if (new_rlim
.rlim_cur
> NR_OPEN
|| new_rlim
.rlim_max
> NR_OPEN
)
1269 retval
= security_task_setrlimit(resource
, &new_rlim
);
1273 *old_rlim
= new_rlim
;
1278 * It would make sense to put struct rusage in the task_struct,
1279 * except that would make the task_struct be *really big*. After
1280 * task_struct gets moved into malloc'ed memory, it would
1281 * make sense to do this. It will make moving the rest of the information
1282 * a lot simpler! (Which we're not doing right now because we're not
1283 * measuring them yet).
1285 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1286 * below (we know we aren't going to exit/disappear and only we change our
1287 * rusage counters), or we are called from wait4() on a process which is
1288 * either stopped or zombied. In the zombied case the task won't get
1289 * reaped till shortly after the call to getrusage(), in both cases the
1290 * task being examined is in a frozen state so the counters won't change.
1292 * FIXME! Get the fault counts properly!
1294 int getrusage(struct task_struct
*p
, int who
, struct rusage __user
*ru
)
1298 memset((char *) &r
, 0, sizeof(r
));
1301 jiffies_to_timeval(p
->utime
, &r
.ru_utime
);
1302 jiffies_to_timeval(p
->stime
, &r
.ru_stime
);
1303 r
.ru_minflt
= p
->min_flt
;
1304 r
.ru_majflt
= p
->maj_flt
;
1305 r
.ru_nswap
= p
->nswap
;
1307 case RUSAGE_CHILDREN
:
1308 jiffies_to_timeval(p
->cutime
, &r
.ru_utime
);
1309 jiffies_to_timeval(p
->cstime
, &r
.ru_stime
);
1310 r
.ru_minflt
= p
->cmin_flt
;
1311 r
.ru_majflt
= p
->cmaj_flt
;
1312 r
.ru_nswap
= p
->cnswap
;
1315 jiffies_to_timeval(p
->utime
+ p
->cutime
, &r
.ru_utime
);
1316 jiffies_to_timeval(p
->stime
+ p
->cstime
, &r
.ru_stime
);
1317 r
.ru_minflt
= p
->min_flt
+ p
->cmin_flt
;
1318 r
.ru_majflt
= p
->maj_flt
+ p
->cmaj_flt
;
1319 r
.ru_nswap
= p
->nswap
+ p
->cnswap
;
1322 return copy_to_user(ru
, &r
, sizeof(r
)) ? -EFAULT
: 0;
1325 asmlinkage
long sys_getrusage(int who
, struct rusage __user
*ru
)
1327 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
)
1329 return getrusage(current
, who
, ru
);
1332 asmlinkage
long sys_umask(int mask
)
1334 mask
= xchg(¤t
->fs
->umask
, mask
& S_IRWXUGO
);
1338 asmlinkage
long sys_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1339 unsigned long arg4
, unsigned long arg5
)
1344 error
= security_task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1349 case PR_SET_PDEATHSIG
:
1351 if (sig
< 0 || sig
> _NSIG
) {
1355 current
->pdeath_signal
= sig
;
1357 case PR_GET_PDEATHSIG
:
1358 error
= put_user(current
->pdeath_signal
, (int __user
*)arg2
);
1360 case PR_GET_DUMPABLE
:
1361 if (current
->mm
->dumpable
)
1364 case PR_SET_DUMPABLE
:
1365 if (arg2
!= 0 && arg2
!= 1) {
1369 current
->mm
->dumpable
= arg2
;
1372 case PR_SET_UNALIGN
:
1373 error
= SET_UNALIGN_CTL(current
, arg2
);
1375 case PR_GET_UNALIGN
:
1376 error
= GET_UNALIGN_CTL(current
, arg2
);
1379 error
= SET_FPEMU_CTL(current
, arg2
);
1382 error
= GET_FPEMU_CTL(current
, arg2
);
1385 error
= SET_FPEXC_CTL(current
, arg2
);
1388 error
= GET_FPEXC_CTL(current
, arg2
);
1392 case PR_GET_KEEPCAPS
:
1393 if (current
->keep_capabilities
)
1396 case PR_SET_KEEPCAPS
:
1397 if (arg2
!= 0 && arg2
!= 1) {
1401 current
->keep_capabilities
= arg2
;
1410 EXPORT_SYMBOL(notifier_chain_register
);
1411 EXPORT_SYMBOL(notifier_chain_unregister
);
1412 EXPORT_SYMBOL(notifier_call_chain
);
1413 EXPORT_SYMBOL(register_reboot_notifier
);
1414 EXPORT_SYMBOL(unregister_reboot_notifier
);
1415 EXPORT_SYMBOL(in_group_p
);
1416 EXPORT_SYMBOL(in_egroup_p
);