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>
25 #include <asm/uaccess.h>
27 #include <asm/unistd.h>
29 #ifndef SET_UNALIGN_CTL
30 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
32 #ifndef GET_UNALIGN_CTL
33 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
36 # define SET_FPEMU_CTL(a,b) (-EINVAL)
39 # define GET_FPEMU_CTL(a,b) (-EINVAL)
42 # define SET_FPEXC_CTL(a,b) (-EINVAL)
45 # define GET_FPEXC_CTL(a,b) (-EINVAL)
49 * this is where the system-wide overflow UID and GID are defined, for
50 * architectures that now have 32-bit UID/GID but didn't in the past
53 int overflowuid
= DEFAULT_OVERFLOWUID
;
54 int overflowgid
= DEFAULT_OVERFLOWGID
;
57 * the same as above, but for filesystems which can only store a 16-bit
58 * UID and GID. as such, this is needed on all architectures
61 int fs_overflowuid
= DEFAULT_FS_OVERFLOWUID
;
62 int fs_overflowgid
= DEFAULT_FS_OVERFLOWUID
;
65 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
71 extern int system_running
;
74 * Notifier list for kernel code which wants to be called
75 * at shutdown. This is used to stop any idling DMA operations
79 static struct notifier_block
*reboot_notifier_list
;
80 rwlock_t notifier_lock
= RW_LOCK_UNLOCKED
;
83 * notifier_chain_register - Add notifier to a notifier chain
84 * @list: Pointer to root list pointer
85 * @n: New entry in notifier chain
87 * Adds a notifier to a notifier chain.
89 * Currently always returns zero.
92 int notifier_chain_register(struct notifier_block
**list
, struct notifier_block
*n
)
94 write_lock(¬ifier_lock
);
97 if(n
->priority
> (*list
)->priority
)
99 list
= &((*list
)->next
);
103 write_unlock(¬ifier_lock
);
108 * notifier_chain_unregister - Remove notifier from a notifier chain
109 * @nl: Pointer to root list pointer
110 * @n: New entry in notifier chain
112 * Removes a notifier from a notifier chain.
114 * Returns zero on success, or %-ENOENT on failure.
117 int notifier_chain_unregister(struct notifier_block
**nl
, struct notifier_block
*n
)
119 write_lock(¬ifier_lock
);
125 write_unlock(¬ifier_lock
);
130 write_unlock(¬ifier_lock
);
135 * notifier_call_chain - Call functions in a notifier chain
136 * @n: Pointer to root pointer of notifier chain
137 * @val: Value passed unmodified to notifier function
138 * @v: Pointer passed unmodified to notifier function
140 * Calls each function in a notifier chain in turn.
142 * If the return value of the notifier can be and'd
143 * with %NOTIFY_STOP_MASK, then notifier_call_chain
144 * will return immediately, with the return value of
145 * the notifier function which halted execution.
146 * Otherwise, the return value is the return value
147 * of the last notifier function called.
150 int notifier_call_chain(struct notifier_block
**n
, unsigned long val
, void *v
)
153 struct notifier_block
*nb
= *n
;
157 ret
=nb
->notifier_call(nb
,val
,v
);
158 if(ret
&NOTIFY_STOP_MASK
)
168 * register_reboot_notifier - Register function to be called at reboot time
169 * @nb: Info about notifier function to be called
171 * Registers a function with the list of functions
172 * to be called at reboot time.
174 * Currently always returns zero, as notifier_chain_register
175 * always returns zero.
178 int register_reboot_notifier(struct notifier_block
* nb
)
180 return notifier_chain_register(&reboot_notifier_list
, nb
);
184 * unregister_reboot_notifier - Unregister previously registered reboot notifier
185 * @nb: Hook to be unregistered
187 * Unregisters a previously registered reboot
190 * Returns zero on success, or %-ENOENT on failure.
193 int unregister_reboot_notifier(struct notifier_block
* nb
)
195 return notifier_chain_unregister(&reboot_notifier_list
, nb
);
198 asmlinkage
long sys_ni_syscall(void)
203 cond_syscall(sys_nfsservctl
)
204 cond_syscall(sys_quotactl
)
205 cond_syscall(sys_acct
)
206 cond_syscall(sys_lookup_dcookie
)
208 static int set_one_prio(struct task_struct
*p
, int niceval
, int error
)
210 if (p
->uid
!= current
->euid
&&
211 p
->uid
!= current
->uid
&& !capable(CAP_SYS_NICE
)) {
218 if (niceval
< task_nice(p
) && !capable(CAP_SYS_NICE
))
221 set_user_nice(p
, niceval
);
226 asmlinkage
long sys_setpriority(int which
, int who
, int niceval
)
228 struct task_struct
*g
, *p
;
229 struct user_struct
*user
;
234 if (which
> 2 || which
< 0)
237 /* normalize: avoid signed division (rounding problems) */
244 read_lock(&tasklist_lock
);
249 p
= find_task_by_pid(who
);
251 error
= set_one_prio(p
, niceval
, error
);
256 for_each_task_pid(who
, PIDTYPE_PGID
, p
, l
, pid
)
257 error
= set_one_prio(p
, niceval
, error
);
261 user
= current
->user
;
263 user
= find_user(who
);
270 error
= set_one_prio(p
, niceval
, error
);
271 while_each_thread(g
, p
);
275 read_unlock(&tasklist_lock
);
281 * Ugh. To avoid negative return values, "getpriority()" will
282 * not return the normal nice-value, but a negated value that
283 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
284 * to stay compatible.
286 asmlinkage
long sys_getpriority(int which
, int who
)
288 struct task_struct
*g
, *p
;
291 struct user_struct
*user
;
292 long niceval
, retval
= -ESRCH
;
294 if (which
> 2 || which
< 0)
297 read_lock(&tasklist_lock
);
302 p
= find_task_by_pid(who
);
304 niceval
= 20 - task_nice(p
);
305 if (niceval
> retval
)
312 for_each_task_pid(who
, PIDTYPE_PGID
, p
, l
, pid
) {
313 niceval
= 20 - task_nice(p
);
314 if (niceval
> retval
)
320 user
= current
->user
;
322 user
= find_user(who
);
329 niceval
= 20 - task_nice(p
);
330 if (niceval
> retval
)
333 while_each_thread(g
, p
);
337 read_unlock(&tasklist_lock
);
344 * Reboot system call: for obvious reasons only root may call it,
345 * and even root needs to set up some magic numbers in the registers
346 * so that some mistake won't make this reboot the whole machine.
347 * You can also set the meaning of the ctrl-alt-del-key here.
349 * reboot doesn't sync: do that yourself before calling this.
351 asmlinkage
long sys_reboot(int magic1
, int magic2
, unsigned int cmd
, void * arg
)
355 /* We only trust the superuser with rebooting the system. */
356 if (!capable(CAP_SYS_BOOT
))
359 /* For safety, we require "magic" arguments. */
360 if (magic1
!= LINUX_REBOOT_MAGIC1
||
361 (magic2
!= LINUX_REBOOT_MAGIC2
&& magic2
!= LINUX_REBOOT_MAGIC2A
&&
362 magic2
!= LINUX_REBOOT_MAGIC2B
))
367 case LINUX_REBOOT_CMD_RESTART
:
368 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, NULL
);
371 printk(KERN_EMERG
"Restarting system.\n");
372 machine_restart(NULL
);
375 case LINUX_REBOOT_CMD_CAD_ON
:
379 case LINUX_REBOOT_CMD_CAD_OFF
:
383 case LINUX_REBOOT_CMD_HALT
:
384 notifier_call_chain(&reboot_notifier_list
, SYS_HALT
, NULL
);
387 printk(KERN_EMERG
"System halted.\n");
392 case LINUX_REBOOT_CMD_POWER_OFF
:
393 notifier_call_chain(&reboot_notifier_list
, SYS_POWER_OFF
, NULL
);
396 printk(KERN_EMERG
"Power down.\n");
401 case LINUX_REBOOT_CMD_RESTART2
:
402 if (strncpy_from_user(&buffer
[0], (char *)arg
, sizeof(buffer
) - 1) < 0) {
406 buffer
[sizeof(buffer
) - 1] = '\0';
408 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, buffer
);
411 printk(KERN_EMERG
"Restarting system with command '%s'.\n", buffer
);
412 machine_restart(buffer
);
415 #ifdef CONFIG_SOFTWARE_SUSPEND
416 case LINUX_REBOOT_CMD_SW_SUSPEND
:
417 if (!software_suspend_enabled
) {
434 static void deferred_cad(void *dummy
)
436 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, NULL
);
437 machine_restart(NULL
);
441 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
442 * As it's called within an interrupt, it may NOT sync: the only choice
443 * is whether to reboot at once, or just ignore the ctrl-alt-del.
445 void ctrl_alt_del(void)
447 static DECLARE_WORK(cad_work
, deferred_cad
, NULL
);
450 schedule_work(&cad_work
);
452 kill_proc(cad_pid
, SIGINT
, 1);
457 * Unprivileged users may change the real gid to the effective gid
458 * or vice versa. (BSD-style)
460 * If you set the real gid at all, or set the effective gid to a value not
461 * equal to the real gid, then the saved gid is set to the new effective gid.
463 * This makes it possible for a setgid program to completely drop its
464 * privileges, which is often a useful assertion to make when you are doing
465 * a security audit over a program.
467 * The general idea is that a program which uses just setregid() will be
468 * 100% compatible with BSD. A program which uses just setgid() will be
469 * 100% compatible with POSIX with saved IDs.
471 * SMP: There are not races, the GIDs are checked only by filesystem
472 * operations (as far as semantic preservation is concerned).
474 asmlinkage
long sys_setregid(gid_t rgid
, gid_t egid
)
476 int old_rgid
= current
->gid
;
477 int old_egid
= current
->egid
;
478 int new_rgid
= old_rgid
;
479 int new_egid
= old_egid
;
482 retval
= security_ops
->task_setgid(rgid
, egid
, (gid_t
)-1, LSM_SETID_RE
);
486 if (rgid
!= (gid_t
) -1) {
487 if ((old_rgid
== rgid
) ||
488 (current
->egid
==rgid
) ||
494 if (egid
!= (gid_t
) -1) {
495 if ((old_rgid
== egid
) ||
496 (current
->egid
== egid
) ||
497 (current
->sgid
== egid
) ||
504 if (new_egid
!= old_egid
)
506 current
->mm
->dumpable
= 0;
509 if (rgid
!= (gid_t
) -1 ||
510 (egid
!= (gid_t
) -1 && egid
!= old_rgid
))
511 current
->sgid
= new_egid
;
512 current
->fsgid
= new_egid
;
513 current
->egid
= new_egid
;
514 current
->gid
= new_rgid
;
519 * setgid() is implemented like SysV w/ SAVED_IDS
521 * SMP: Same implicit races as above.
523 asmlinkage
long sys_setgid(gid_t gid
)
525 int old_egid
= current
->egid
;
528 retval
= security_ops
->task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_ID
);
532 if (capable(CAP_SETGID
))
536 current
->mm
->dumpable
=0;
539 current
->gid
= current
->egid
= current
->sgid
= current
->fsgid
= gid
;
541 else if ((gid
== current
->gid
) || (gid
== current
->sgid
))
545 current
->mm
->dumpable
=0;
548 current
->egid
= current
->fsgid
= gid
;
555 static int set_user(uid_t new_ruid
, int dumpclear
)
557 struct user_struct
*new_user
, *old_user
;
559 /* What if a process setreuid()'s and this brings the
560 * new uid over his NPROC rlimit? We can check this now
561 * cheaply with the new uid cache, so if it matters
562 * we should be checking for it. -DaveM
564 new_user
= alloc_uid(new_ruid
);
567 old_user
= current
->user
;
568 atomic_dec(&old_user
->processes
);
569 atomic_inc(&new_user
->processes
);
573 current
->mm
->dumpable
= 0;
576 current
->uid
= new_ruid
;
577 current
->user
= new_user
;
583 * Unprivileged users may change the real uid to the effective uid
584 * or vice versa. (BSD-style)
586 * If you set the real uid at all, or set the effective uid to a value not
587 * equal to the real uid, then the saved uid is set to the new effective uid.
589 * This makes it possible for a setuid program to completely drop its
590 * privileges, which is often a useful assertion to make when you are doing
591 * a security audit over a program.
593 * The general idea is that a program which uses just setreuid() will be
594 * 100% compatible with BSD. A program which uses just setuid() will be
595 * 100% compatible with POSIX with saved IDs.
597 asmlinkage
long sys_setreuid(uid_t ruid
, uid_t euid
)
599 int old_ruid
, old_euid
, old_suid
, new_ruid
, new_euid
;
602 retval
= security_ops
->task_setuid(ruid
, euid
, (uid_t
)-1, LSM_SETID_RE
);
606 new_ruid
= old_ruid
= current
->uid
;
607 new_euid
= old_euid
= current
->euid
;
608 old_suid
= current
->suid
;
610 if (ruid
!= (uid_t
) -1) {
612 if ((old_ruid
!= ruid
) &&
613 (current
->euid
!= ruid
) &&
614 !capable(CAP_SETUID
))
618 if (euid
!= (uid_t
) -1) {
620 if ((old_ruid
!= euid
) &&
621 (current
->euid
!= euid
) &&
622 (current
->suid
!= euid
) &&
623 !capable(CAP_SETUID
))
627 if (new_ruid
!= old_ruid
&& set_user(new_ruid
, new_euid
!= old_euid
) < 0)
630 if (new_euid
!= old_euid
)
632 current
->mm
->dumpable
=0;
635 current
->fsuid
= current
->euid
= new_euid
;
636 if (ruid
!= (uid_t
) -1 ||
637 (euid
!= (uid_t
) -1 && euid
!= old_ruid
))
638 current
->suid
= current
->euid
;
639 current
->fsuid
= current
->euid
;
641 return security_ops
->task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RE
);
647 * setuid() is implemented like SysV with SAVED_IDS
649 * Note that SAVED_ID's is deficient in that a setuid root program
650 * like sendmail, for example, cannot set its uid to be a normal
651 * user and then switch back, because if you're root, setuid() sets
652 * the saved uid too. If you don't like this, blame the bright people
653 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
654 * will allow a root program to temporarily drop privileges and be able to
655 * regain them by swapping the real and effective uid.
657 asmlinkage
long sys_setuid(uid_t uid
)
659 int old_euid
= current
->euid
;
660 int old_ruid
, old_suid
, new_ruid
, new_suid
;
663 retval
= security_ops
->task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_ID
);
667 old_ruid
= new_ruid
= current
->uid
;
668 old_suid
= current
->suid
;
671 if (capable(CAP_SETUID
)) {
672 if (uid
!= old_ruid
&& set_user(uid
, old_euid
!= uid
) < 0)
675 } else if ((uid
!= current
->uid
) && (uid
!= new_suid
))
680 current
->mm
->dumpable
= 0;
683 current
->fsuid
= current
->euid
= uid
;
684 current
->suid
= new_suid
;
686 return security_ops
->task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_ID
);
691 * This function implements a generic ability to update ruid, euid,
692 * and suid. This allows you to implement the 4.4 compatible seteuid().
694 asmlinkage
long sys_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
696 int old_ruid
= current
->uid
;
697 int old_euid
= current
->euid
;
698 int old_suid
= current
->suid
;
701 retval
= security_ops
->task_setuid(ruid
, euid
, suid
, LSM_SETID_RES
);
705 if (!capable(CAP_SETUID
)) {
706 if ((ruid
!= (uid_t
) -1) && (ruid
!= current
->uid
) &&
707 (ruid
!= current
->euid
) && (ruid
!= current
->suid
))
709 if ((euid
!= (uid_t
) -1) && (euid
!= current
->uid
) &&
710 (euid
!= current
->euid
) && (euid
!= current
->suid
))
712 if ((suid
!= (uid_t
) -1) && (suid
!= current
->uid
) &&
713 (suid
!= current
->euid
) && (suid
!= current
->suid
))
716 if (ruid
!= (uid_t
) -1) {
717 if (ruid
!= current
->uid
&& set_user(ruid
, euid
!= current
->euid
) < 0)
720 if (euid
!= (uid_t
) -1) {
721 if (euid
!= current
->euid
)
723 current
->mm
->dumpable
= 0;
726 current
->euid
= euid
;
728 current
->fsuid
= current
->euid
;
729 if (suid
!= (uid_t
) -1)
730 current
->suid
= suid
;
732 return security_ops
->task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RES
);
735 asmlinkage
long sys_getresuid(uid_t
*ruid
, uid_t
*euid
, uid_t
*suid
)
739 if (!(retval
= put_user(current
->uid
, ruid
)) &&
740 !(retval
= put_user(current
->euid
, euid
)))
741 retval
= put_user(current
->suid
, suid
);
747 * Same as above, but for rgid, egid, sgid.
749 asmlinkage
long sys_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
753 retval
= security_ops
->task_setgid(rgid
, egid
, sgid
, LSM_SETID_RES
);
757 if (!capable(CAP_SETGID
)) {
758 if ((rgid
!= (gid_t
) -1) && (rgid
!= current
->gid
) &&
759 (rgid
!= current
->egid
) && (rgid
!= current
->sgid
))
761 if ((egid
!= (gid_t
) -1) && (egid
!= current
->gid
) &&
762 (egid
!= current
->egid
) && (egid
!= current
->sgid
))
764 if ((sgid
!= (gid_t
) -1) && (sgid
!= current
->gid
) &&
765 (sgid
!= current
->egid
) && (sgid
!= current
->sgid
))
768 if (egid
!= (gid_t
) -1) {
769 if (egid
!= current
->egid
)
771 current
->mm
->dumpable
= 0;
774 current
->egid
= egid
;
776 current
->fsgid
= current
->egid
;
777 if (rgid
!= (gid_t
) -1)
779 if (sgid
!= (gid_t
) -1)
780 current
->sgid
= sgid
;
784 asmlinkage
long sys_getresgid(gid_t
*rgid
, gid_t
*egid
, gid_t
*sgid
)
788 if (!(retval
= put_user(current
->gid
, rgid
)) &&
789 !(retval
= put_user(current
->egid
, egid
)))
790 retval
= put_user(current
->sgid
, sgid
);
797 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
798 * is used for "access()" and for the NFS daemon (letting nfsd stay at
799 * whatever uid it wants to). It normally shadows "euid", except when
800 * explicitly set by setfsuid() or for access..
802 asmlinkage
long sys_setfsuid(uid_t uid
)
807 retval
= security_ops
->task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
);
811 old_fsuid
= current
->fsuid
;
812 if (uid
== current
->uid
|| uid
== current
->euid
||
813 uid
== current
->suid
|| uid
== current
->fsuid
||
816 if (uid
!= old_fsuid
)
818 current
->mm
->dumpable
= 0;
821 current
->fsuid
= uid
;
824 retval
= security_ops
->task_post_setuid(old_fsuid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
);
832 * Samma på svenska..
834 asmlinkage
long sys_setfsgid(gid_t gid
)
839 retval
= security_ops
->task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_FS
);
843 old_fsgid
= current
->fsgid
;
844 if (gid
== current
->gid
|| gid
== current
->egid
||
845 gid
== current
->sgid
|| gid
== current
->fsgid
||
848 if (gid
!= old_fsgid
)
850 current
->mm
->dumpable
= 0;
853 current
->fsgid
= gid
;
858 asmlinkage
long sys_times(struct tms
* tbuf
)
861 * In the SMP world we might just be unlucky and have one of
862 * the times increment as we use it. Since the value is an
863 * atomically safe type this is just fine. Conceptually its
864 * as if the syscall took an instant longer to occur.
868 tmp
.tms_utime
= jiffies_to_clock_t(current
->utime
);
869 tmp
.tms_stime
= jiffies_to_clock_t(current
->stime
);
870 tmp
.tms_cutime
= jiffies_to_clock_t(current
->cutime
);
871 tmp
.tms_cstime
= jiffies_to_clock_t(current
->cstime
);
872 if (copy_to_user(tbuf
, &tmp
, sizeof(struct tms
)))
875 return jiffies_to_clock_t(jiffies
);
879 * This needs some heavy checking ...
880 * I just haven't the stomach for it. I also don't fully
881 * understand sessions/pgrp etc. Let somebody who does explain it.
883 * OK, I think I have the protection semantics right.... this is really
884 * only important on a multi-user system anyway, to make sure one user
885 * can't send a signal to a process owned by another. -TYT, 12/12/91
887 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
891 asmlinkage
long sys_setpgid(pid_t pid
, pid_t pgid
)
893 struct task_struct
*p
;
903 /* From this point forward we keep holding onto the tasklist lock
904 * so that our parent does not change from under us. -DaveM
906 write_lock_irq(&tasklist_lock
);
909 p
= find_task_by_pid(pid
);
913 if (!thread_group_leader(p
))
916 if (p
->parent
== current
|| p
->real_parent
== current
) {
918 if (p
->session
!= current
->session
)
923 } else if (p
!= current
)
929 struct task_struct
*p
;
933 for_each_task_pid(pgid
, PIDTYPE_PGID
, p
, l
, pid
)
934 if (p
->session
== current
->session
)
940 if (p
->pgrp
!= pgid
) {
941 detach_pid(p
, PIDTYPE_PGID
);
943 attach_pid(p
, PIDTYPE_PGID
, pgid
);
947 /* All paths lead to here, thus we are safe. -DaveM */
948 write_unlock_irq(&tasklist_lock
);
952 asmlinkage
long sys_getpgid(pid_t pid
)
955 return current
->pgrp
;
958 struct task_struct
*p
;
960 read_lock(&tasklist_lock
);
961 p
= find_task_by_pid(pid
);
965 retval
= security_ops
->task_getpgid(p
);
969 read_unlock(&tasklist_lock
);
974 asmlinkage
long sys_getpgrp(void)
976 /* SMP - assuming writes are word atomic this is fine */
977 return current
->pgrp
;
980 asmlinkage
long sys_getsid(pid_t pid
)
983 return current
->session
;
986 struct task_struct
*p
;
988 read_lock(&tasklist_lock
);
989 p
= find_task_by_pid(pid
);
993 retval
= security_ops
->task_getsid(p
);
997 read_unlock(&tasklist_lock
);
1002 asmlinkage
long sys_setsid(void)
1007 if (!thread_group_leader(current
))
1010 write_lock_irq(&tasklist_lock
);
1012 pid
= find_pid(PIDTYPE_PGID
, current
->pid
);
1016 current
->leader
= 1;
1017 if (current
->session
!= current
->pid
) {
1018 detach_pid(current
, PIDTYPE_SID
);
1019 current
->session
= current
->pid
;
1020 attach_pid(current
, PIDTYPE_SID
, current
->pid
);
1022 if (current
->pgrp
!= current
->pid
) {
1023 detach_pid(current
, PIDTYPE_PGID
);
1024 current
->pgrp
= current
->pid
;
1025 attach_pid(current
, PIDTYPE_PGID
, current
->pid
);
1027 current
->tty
= NULL
;
1028 current
->tty_old_pgrp
= 0;
1029 err
= current
->pgrp
;
1031 write_unlock_irq(&tasklist_lock
);
1036 * Supplementary group IDs
1038 asmlinkage
long sys_getgroups(int gidsetsize
, gid_t
*grouplist
)
1043 * SMP: Nobody else can change our grouplist. Thus we are
1049 i
= current
->ngroups
;
1053 if (copy_to_user(grouplist
, current
->groups
, sizeof(gid_t
)*i
))
1060 * SMP: Our groups are not shared. We can copy to/from them safely
1061 * without another task interfering.
1064 asmlinkage
long sys_setgroups(int gidsetsize
, gid_t
*grouplist
)
1066 gid_t groups
[NGROUPS
];
1069 if (!capable(CAP_SETGID
))
1071 if ((unsigned) gidsetsize
> NGROUPS
)
1073 if(copy_from_user(groups
, grouplist
, gidsetsize
* sizeof(gid_t
)))
1075 retval
= security_ops
->task_setgroups(gidsetsize
, groups
);
1078 memcpy(current
->groups
, groups
, gidsetsize
* sizeof(gid_t
));
1079 current
->ngroups
= gidsetsize
;
1083 static int supplemental_group_member(gid_t grp
)
1085 int i
= current
->ngroups
;
1088 gid_t
*groups
= current
->groups
;
1100 * Check whether we're fsgid/egid or in the supplemental group..
1102 int in_group_p(gid_t grp
)
1105 if (grp
!= current
->fsgid
)
1106 retval
= supplemental_group_member(grp
);
1110 int in_egroup_p(gid_t grp
)
1113 if (grp
!= current
->egid
)
1114 retval
= supplemental_group_member(grp
);
1118 DECLARE_RWSEM(uts_sem
);
1120 asmlinkage
long sys_newuname(struct new_utsname
* name
)
1124 down_read(&uts_sem
);
1125 if (copy_to_user(name
,&system_utsname
,sizeof *name
))
1131 asmlinkage
long sys_sethostname(char *name
, int len
)
1135 if (!capable(CAP_SYS_ADMIN
))
1137 if (len
< 0 || len
> __NEW_UTS_LEN
)
1139 down_write(&uts_sem
);
1141 if (!copy_from_user(system_utsname
.nodename
, name
, len
)) {
1142 system_utsname
.nodename
[len
] = 0;
1149 asmlinkage
long sys_gethostname(char *name
, int len
)
1155 down_read(&uts_sem
);
1156 i
= 1 + strlen(system_utsname
.nodename
);
1160 if (copy_to_user(name
, system_utsname
.nodename
, i
))
1167 * Only setdomainname; getdomainname can be implemented by calling
1170 asmlinkage
long sys_setdomainname(char *name
, int len
)
1174 if (!capable(CAP_SYS_ADMIN
))
1176 if (len
< 0 || len
> __NEW_UTS_LEN
)
1179 down_write(&uts_sem
);
1181 if (!copy_from_user(system_utsname
.domainname
, name
, len
)) {
1183 system_utsname
.domainname
[len
] = 0;
1189 asmlinkage
long sys_getrlimit(unsigned int resource
, struct rlimit
*rlim
)
1191 if (resource
>= RLIM_NLIMITS
)
1194 return copy_to_user(rlim
, current
->rlim
+ resource
, sizeof(*rlim
))
1198 #if !defined(__ia64__)
1201 * Back compatibility for getrlimit. Needed for some apps.
1204 asmlinkage
long sys_old_getrlimit(unsigned int resource
, struct rlimit
*rlim
)
1207 if (resource
>= RLIM_NLIMITS
)
1210 memcpy(&x
, current
->rlim
+ resource
, sizeof(*rlim
));
1211 if(x
.rlim_cur
> 0x7FFFFFFF)
1212 x
.rlim_cur
= 0x7FFFFFFF;
1213 if(x
.rlim_max
> 0x7FFFFFFF)
1214 x
.rlim_max
= 0x7FFFFFFF;
1215 return copy_to_user(rlim
, &x
, sizeof(x
))?-EFAULT
:0;
1220 asmlinkage
long sys_setrlimit(unsigned int resource
, struct rlimit
*rlim
)
1222 struct rlimit new_rlim
, *old_rlim
;
1225 if (resource
>= RLIM_NLIMITS
)
1227 if(copy_from_user(&new_rlim
, rlim
, sizeof(*rlim
)))
1229 old_rlim
= current
->rlim
+ resource
;
1230 if (((new_rlim
.rlim_cur
> old_rlim
->rlim_max
) ||
1231 (new_rlim
.rlim_max
> old_rlim
->rlim_max
)) &&
1232 !capable(CAP_SYS_RESOURCE
))
1234 if (resource
== RLIMIT_NOFILE
) {
1235 if (new_rlim
.rlim_cur
> NR_OPEN
|| new_rlim
.rlim_max
> NR_OPEN
)
1239 retval
= security_ops
->task_setrlimit(resource
, &new_rlim
);
1243 *old_rlim
= new_rlim
;
1248 * It would make sense to put struct rusage in the task_struct,
1249 * except that would make the task_struct be *really big*. After
1250 * task_struct gets moved into malloc'ed memory, it would
1251 * make sense to do this. It will make moving the rest of the information
1252 * a lot simpler! (Which we're not doing right now because we're not
1253 * measuring them yet).
1255 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1256 * below (we know we aren't going to exit/disappear and only we change our
1257 * rusage counters), or we are called from wait4() on a process which is
1258 * either stopped or zombied. In the zombied case the task won't get
1259 * reaped till shortly after the call to getrusage(), in both cases the
1260 * task being examined is in a frozen state so the counters won't change.
1262 * FIXME! Get the fault counts properly!
1264 int getrusage(struct task_struct
*p
, int who
, struct rusage
*ru
)
1268 memset((char *) &r
, 0, sizeof(r
));
1271 jiffies_to_timeval(p
->utime
, &r
.ru_utime
);
1272 jiffies_to_timeval(p
->stime
, &r
.ru_stime
);
1273 r
.ru_minflt
= p
->min_flt
;
1274 r
.ru_majflt
= p
->maj_flt
;
1275 r
.ru_nswap
= p
->nswap
;
1277 case RUSAGE_CHILDREN
:
1278 jiffies_to_timeval(p
->cutime
, &r
.ru_utime
);
1279 jiffies_to_timeval(p
->cstime
, &r
.ru_stime
);
1280 r
.ru_minflt
= p
->cmin_flt
;
1281 r
.ru_majflt
= p
->cmaj_flt
;
1282 r
.ru_nswap
= p
->cnswap
;
1285 jiffies_to_timeval(p
->utime
+ p
->cutime
, &r
.ru_utime
);
1286 jiffies_to_timeval(p
->stime
+ p
->cstime
, &r
.ru_stime
);
1287 r
.ru_minflt
= p
->min_flt
+ p
->cmin_flt
;
1288 r
.ru_majflt
= p
->maj_flt
+ p
->cmaj_flt
;
1289 r
.ru_nswap
= p
->nswap
+ p
->cnswap
;
1292 return copy_to_user(ru
, &r
, sizeof(r
)) ? -EFAULT
: 0;
1295 asmlinkage
long sys_getrusage(int who
, struct rusage
*ru
)
1297 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
)
1299 return getrusage(current
, who
, ru
);
1302 asmlinkage
long sys_umask(int mask
)
1304 mask
= xchg(¤t
->fs
->umask
, mask
& S_IRWXUGO
);
1308 asmlinkage
long sys_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1309 unsigned long arg4
, unsigned long arg5
)
1314 error
= security_ops
->task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1319 case PR_SET_PDEATHSIG
:
1321 if (sig
< 0 || sig
> _NSIG
) {
1325 current
->pdeath_signal
= sig
;
1327 case PR_GET_PDEATHSIG
:
1328 error
= put_user(current
->pdeath_signal
, (int *)arg2
);
1330 case PR_GET_DUMPABLE
:
1331 if (current
->mm
->dumpable
)
1334 case PR_SET_DUMPABLE
:
1335 if (arg2
!= 0 && arg2
!= 1) {
1339 current
->mm
->dumpable
= arg2
;
1342 case PR_SET_UNALIGN
:
1343 error
= SET_UNALIGN_CTL(current
, arg2
);
1345 case PR_GET_UNALIGN
:
1346 error
= GET_UNALIGN_CTL(current
, arg2
);
1349 error
= SET_FPEMU_CTL(current
, arg2
);
1352 error
= GET_FPEMU_CTL(current
, arg2
);
1355 error
= SET_FPEXC_CTL(current
, arg2
);
1358 error
= GET_FPEXC_CTL(current
, arg2
);
1362 case PR_GET_KEEPCAPS
:
1363 if (current
->keep_capabilities
)
1366 case PR_SET_KEEPCAPS
:
1367 if (arg2
!= 0 && arg2
!= 1) {
1371 current
->keep_capabilities
= arg2
;
1380 EXPORT_SYMBOL(notifier_chain_register
);
1381 EXPORT_SYMBOL(notifier_chain_unregister
);
1382 EXPORT_SYMBOL(notifier_call_chain
);
1383 EXPORT_SYMBOL(register_reboot_notifier
);
1384 EXPORT_SYMBOL(unregister_reboot_notifier
);
1385 EXPORT_SYMBOL(in_group_p
);
1386 EXPORT_SYMBOL(in_egroup_p
);