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/kernel.h>
20 #include <linux/kexec.h>
21 #include <linux/workqueue.h>
22 #include <linux/device.h>
23 #include <linux/key.h>
24 #include <linux/times.h>
25 #include <linux/posix-timers.h>
26 #include <linux/security.h>
27 #include <linux/dcookies.h>
28 #include <linux/suspend.h>
29 #include <linux/tty.h>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
33 #include <linux/compat.h>
34 #include <linux/syscalls.h>
35 #include <linux/kprobes.h>
37 #include <asm/uaccess.h>
39 #include <asm/unistd.h>
41 #ifndef SET_UNALIGN_CTL
42 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
44 #ifndef GET_UNALIGN_CTL
45 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
48 # define SET_FPEMU_CTL(a,b) (-EINVAL)
51 # define GET_FPEMU_CTL(a,b) (-EINVAL)
54 # define SET_FPEXC_CTL(a,b) (-EINVAL)
57 # define GET_FPEXC_CTL(a,b) (-EINVAL)
61 * this is where the system-wide overflow UID and GID are defined, for
62 * architectures that now have 32-bit UID/GID but didn't in the past
65 int overflowuid
= DEFAULT_OVERFLOWUID
;
66 int overflowgid
= DEFAULT_OVERFLOWGID
;
69 EXPORT_SYMBOL(overflowuid
);
70 EXPORT_SYMBOL(overflowgid
);
74 * the same as above, but for filesystems which can only store a 16-bit
75 * UID and GID. as such, this is needed on all architectures
78 int fs_overflowuid
= DEFAULT_FS_OVERFLOWUID
;
79 int fs_overflowgid
= DEFAULT_FS_OVERFLOWUID
;
81 EXPORT_SYMBOL(fs_overflowuid
);
82 EXPORT_SYMBOL(fs_overflowgid
);
85 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
92 * Notifier list for kernel code which wants to be called
93 * at shutdown. This is used to stop any idling DMA operations
97 static struct notifier_block
*reboot_notifier_list
;
98 static DEFINE_RWLOCK(notifier_lock
);
101 * notifier_chain_register - Add notifier to a notifier chain
102 * @list: Pointer to root list pointer
103 * @n: New entry in notifier chain
105 * Adds a notifier to a notifier chain.
107 * Currently always returns zero.
110 int notifier_chain_register(struct notifier_block
**list
, struct notifier_block
*n
)
112 write_lock(¬ifier_lock
);
115 if(n
->priority
> (*list
)->priority
)
117 list
= &((*list
)->next
);
121 write_unlock(¬ifier_lock
);
125 EXPORT_SYMBOL(notifier_chain_register
);
128 * notifier_chain_unregister - Remove notifier from a notifier chain
129 * @nl: Pointer to root list pointer
130 * @n: New entry in notifier chain
132 * Removes a notifier from a notifier chain.
134 * Returns zero on success, or %-ENOENT on failure.
137 int notifier_chain_unregister(struct notifier_block
**nl
, struct notifier_block
*n
)
139 write_lock(¬ifier_lock
);
145 write_unlock(¬ifier_lock
);
150 write_unlock(¬ifier_lock
);
154 EXPORT_SYMBOL(notifier_chain_unregister
);
157 * notifier_call_chain - Call functions in a notifier chain
158 * @n: Pointer to root pointer of notifier chain
159 * @val: Value passed unmodified to notifier function
160 * @v: Pointer passed unmodified to notifier function
162 * Calls each function in a notifier chain in turn.
164 * If the return value of the notifier can be and'd
165 * with %NOTIFY_STOP_MASK, then notifier_call_chain
166 * will return immediately, with the return value of
167 * the notifier function which halted execution.
168 * Otherwise, the return value is the return value
169 * of the last notifier function called.
172 int __kprobes
notifier_call_chain(struct notifier_block
**n
, unsigned long val
, void *v
)
175 struct notifier_block
*nb
= *n
;
179 ret
=nb
->notifier_call(nb
,val
,v
);
180 if(ret
&NOTIFY_STOP_MASK
)
189 EXPORT_SYMBOL(notifier_call_chain
);
192 * register_reboot_notifier - Register function to be called at reboot time
193 * @nb: Info about notifier function to be called
195 * Registers a function with the list of functions
196 * to be called at reboot time.
198 * Currently always returns zero, as notifier_chain_register
199 * always returns zero.
202 int register_reboot_notifier(struct notifier_block
* nb
)
204 return notifier_chain_register(&reboot_notifier_list
, nb
);
207 EXPORT_SYMBOL(register_reboot_notifier
);
210 * unregister_reboot_notifier - Unregister previously registered reboot notifier
211 * @nb: Hook to be unregistered
213 * Unregisters a previously registered reboot
216 * Returns zero on success, or %-ENOENT on failure.
219 int unregister_reboot_notifier(struct notifier_block
* nb
)
221 return notifier_chain_unregister(&reboot_notifier_list
, nb
);
224 EXPORT_SYMBOL(unregister_reboot_notifier
);
226 static int set_one_prio(struct task_struct
*p
, int niceval
, int error
)
230 if (p
->uid
!= current
->euid
&&
231 p
->euid
!= current
->euid
&& !capable(CAP_SYS_NICE
)) {
235 if (niceval
< task_nice(p
) && !can_nice(p
, niceval
)) {
239 no_nice
= security_task_setnice(p
, niceval
);
246 set_user_nice(p
, niceval
);
251 asmlinkage
long sys_setpriority(int which
, int who
, int niceval
)
253 struct task_struct
*g
, *p
;
254 struct user_struct
*user
;
257 if (which
> 2 || which
< 0)
260 /* normalize: avoid signed division (rounding problems) */
267 read_lock(&tasklist_lock
);
272 p
= find_task_by_pid(who
);
274 error
= set_one_prio(p
, niceval
, error
);
278 who
= process_group(current
);
279 do_each_task_pid(who
, PIDTYPE_PGID
, p
) {
280 error
= set_one_prio(p
, niceval
, error
);
281 } while_each_task_pid(who
, PIDTYPE_PGID
, p
);
284 user
= current
->user
;
288 if ((who
!= current
->uid
) && !(user
= find_user(who
)))
289 goto out_unlock
; /* No processes for this user */
293 error
= set_one_prio(p
, niceval
, error
);
294 while_each_thread(g
, p
);
295 if (who
!= current
->uid
)
296 free_uid(user
); /* For find_user() */
300 read_unlock(&tasklist_lock
);
306 * Ugh. To avoid negative return values, "getpriority()" will
307 * not return the normal nice-value, but a negated value that
308 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
309 * to stay compatible.
311 asmlinkage
long sys_getpriority(int which
, int who
)
313 struct task_struct
*g
, *p
;
314 struct user_struct
*user
;
315 long niceval
, retval
= -ESRCH
;
317 if (which
> 2 || which
< 0)
320 read_lock(&tasklist_lock
);
325 p
= find_task_by_pid(who
);
327 niceval
= 20 - task_nice(p
);
328 if (niceval
> retval
)
334 who
= process_group(current
);
335 do_each_task_pid(who
, PIDTYPE_PGID
, p
) {
336 niceval
= 20 - task_nice(p
);
337 if (niceval
> retval
)
339 } while_each_task_pid(who
, PIDTYPE_PGID
, p
);
342 user
= current
->user
;
346 if ((who
!= current
->uid
) && !(user
= find_user(who
)))
347 goto out_unlock
; /* No processes for this user */
351 niceval
= 20 - task_nice(p
);
352 if (niceval
> retval
)
355 while_each_thread(g
, p
);
356 if (who
!= current
->uid
)
357 free_uid(user
); /* for find_user() */
361 read_unlock(&tasklist_lock
);
367 * emergency_restart - reboot the system
369 * Without shutting down any hardware or taking any locks
370 * reboot the system. This is called when we know we are in
371 * trouble so this is our best effort to reboot. This is
372 * safe to call in interrupt context.
374 void emergency_restart(void)
376 machine_emergency_restart();
378 EXPORT_SYMBOL_GPL(emergency_restart
);
380 void kernel_restart_prepare(char *cmd
)
382 notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, cmd
);
383 system_state
= SYSTEM_RESTART
;
388 * kernel_restart - reboot the system
389 * @cmd: pointer to buffer containing command to execute for restart
392 * Shutdown everything and perform a clean reboot.
393 * This is not safe to call in interrupt context.
395 void kernel_restart(char *cmd
)
397 kernel_restart_prepare(cmd
);
399 printk(KERN_EMERG
"Restarting system.\n");
401 printk(KERN_EMERG
"Restarting system with command '%s'.\n", cmd
);
404 machine_restart(cmd
);
406 EXPORT_SYMBOL_GPL(kernel_restart
);
409 * kernel_kexec - reboot the system
411 * Move into place and start executing a preloaded standalone
412 * executable. If nothing was preloaded return an error.
414 void kernel_kexec(void)
417 struct kimage
*image
;
418 image
= xchg(&kexec_image
, 0);
422 kernel_restart_prepare(NULL
);
423 printk(KERN_EMERG
"Starting new kernel\n");
425 machine_kexec(image
);
428 EXPORT_SYMBOL_GPL(kernel_kexec
);
431 * kernel_halt - halt the system
433 * Shutdown everything and perform a clean system halt.
435 void kernel_halt_prepare(void)
437 notifier_call_chain(&reboot_notifier_list
, SYS_HALT
, NULL
);
438 system_state
= SYSTEM_HALT
;
441 void kernel_halt(void)
443 kernel_halt_prepare();
444 printk(KERN_EMERG
"System halted.\n");
447 EXPORT_SYMBOL_GPL(kernel_halt
);
450 * kernel_power_off - power_off the system
452 * Shutdown everything and perform a clean system power_off.
454 void kernel_power_off_prepare(void)
456 notifier_call_chain(&reboot_notifier_list
, SYS_POWER_OFF
, NULL
);
457 system_state
= SYSTEM_POWER_OFF
;
460 void kernel_power_off(void)
462 kernel_power_off_prepare();
463 printk(KERN_EMERG
"Power down.\n");
466 EXPORT_SYMBOL_GPL(kernel_power_off
);
469 * Reboot system call: for obvious reasons only root may call it,
470 * and even root needs to set up some magic numbers in the registers
471 * so that some mistake won't make this reboot the whole machine.
472 * You can also set the meaning of the ctrl-alt-del-key here.
474 * reboot doesn't sync: do that yourself before calling this.
476 asmlinkage
long sys_reboot(int magic1
, int magic2
, unsigned int cmd
, void __user
* arg
)
480 /* We only trust the superuser with rebooting the system. */
481 if (!capable(CAP_SYS_BOOT
))
484 /* For safety, we require "magic" arguments. */
485 if (magic1
!= LINUX_REBOOT_MAGIC1
||
486 (magic2
!= LINUX_REBOOT_MAGIC2
&&
487 magic2
!= LINUX_REBOOT_MAGIC2A
&&
488 magic2
!= LINUX_REBOOT_MAGIC2B
&&
489 magic2
!= LINUX_REBOOT_MAGIC2C
))
492 /* Instead of trying to make the power_off code look like
493 * halt when pm_power_off is not set do it the easy way.
495 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !pm_power_off
)
496 cmd
= LINUX_REBOOT_CMD_HALT
;
500 case LINUX_REBOOT_CMD_RESTART
:
501 kernel_restart(NULL
);
504 case LINUX_REBOOT_CMD_CAD_ON
:
508 case LINUX_REBOOT_CMD_CAD_OFF
:
512 case LINUX_REBOOT_CMD_HALT
:
518 case LINUX_REBOOT_CMD_POWER_OFF
:
524 case LINUX_REBOOT_CMD_RESTART2
:
525 if (strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1) < 0) {
529 buffer
[sizeof(buffer
) - 1] = '\0';
531 kernel_restart(buffer
);
534 case LINUX_REBOOT_CMD_KEXEC
:
539 #ifdef CONFIG_SOFTWARE_SUSPEND
540 case LINUX_REBOOT_CMD_SW_SUSPEND
:
542 int ret
= software_suspend();
556 static void deferred_cad(void *dummy
)
558 kernel_restart(NULL
);
562 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
563 * As it's called within an interrupt, it may NOT sync: the only choice
564 * is whether to reboot at once, or just ignore the ctrl-alt-del.
566 void ctrl_alt_del(void)
568 static DECLARE_WORK(cad_work
, deferred_cad
, NULL
);
571 schedule_work(&cad_work
);
573 kill_proc(cad_pid
, SIGINT
, 1);
578 * Unprivileged users may change the real gid to the effective gid
579 * or vice versa. (BSD-style)
581 * If you set the real gid at all, or set the effective gid to a value not
582 * equal to the real gid, then the saved gid is set to the new effective gid.
584 * This makes it possible for a setgid program to completely drop its
585 * privileges, which is often a useful assertion to make when you are doing
586 * a security audit over a program.
588 * The general idea is that a program which uses just setregid() will be
589 * 100% compatible with BSD. A program which uses just setgid() will be
590 * 100% compatible with POSIX with saved IDs.
592 * SMP: There are not races, the GIDs are checked only by filesystem
593 * operations (as far as semantic preservation is concerned).
595 asmlinkage
long sys_setregid(gid_t rgid
, gid_t egid
)
597 int old_rgid
= current
->gid
;
598 int old_egid
= current
->egid
;
599 int new_rgid
= old_rgid
;
600 int new_egid
= old_egid
;
603 retval
= security_task_setgid(rgid
, egid
, (gid_t
)-1, LSM_SETID_RE
);
607 if (rgid
!= (gid_t
) -1) {
608 if ((old_rgid
== rgid
) ||
609 (current
->egid
==rgid
) ||
615 if (egid
!= (gid_t
) -1) {
616 if ((old_rgid
== egid
) ||
617 (current
->egid
== egid
) ||
618 (current
->sgid
== egid
) ||
625 if (new_egid
!= old_egid
)
627 current
->mm
->dumpable
= suid_dumpable
;
630 if (rgid
!= (gid_t
) -1 ||
631 (egid
!= (gid_t
) -1 && egid
!= old_rgid
))
632 current
->sgid
= new_egid
;
633 current
->fsgid
= new_egid
;
634 current
->egid
= new_egid
;
635 current
->gid
= new_rgid
;
636 key_fsgid_changed(current
);
637 proc_id_connector(current
, PROC_EVENT_GID
);
642 * setgid() is implemented like SysV w/ SAVED_IDS
644 * SMP: Same implicit races as above.
646 asmlinkage
long sys_setgid(gid_t gid
)
648 int old_egid
= current
->egid
;
651 retval
= security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_ID
);
655 if (capable(CAP_SETGID
))
659 current
->mm
->dumpable
= suid_dumpable
;
662 current
->gid
= current
->egid
= current
->sgid
= current
->fsgid
= gid
;
664 else if ((gid
== current
->gid
) || (gid
== current
->sgid
))
668 current
->mm
->dumpable
= suid_dumpable
;
671 current
->egid
= current
->fsgid
= gid
;
676 key_fsgid_changed(current
);
677 proc_id_connector(current
, PROC_EVENT_GID
);
681 static int set_user(uid_t new_ruid
, int dumpclear
)
683 struct user_struct
*new_user
;
685 new_user
= alloc_uid(new_ruid
);
689 if (atomic_read(&new_user
->processes
) >=
690 current
->signal
->rlim
[RLIMIT_NPROC
].rlim_cur
&&
691 new_user
!= &root_user
) {
696 switch_uid(new_user
);
700 current
->mm
->dumpable
= suid_dumpable
;
703 current
->uid
= new_ruid
;
708 * Unprivileged users may change the real uid to the effective uid
709 * or vice versa. (BSD-style)
711 * If you set the real uid at all, or set the effective uid to a value not
712 * equal to the real uid, then the saved uid is set to the new effective uid.
714 * This makes it possible for a setuid program to completely drop its
715 * privileges, which is often a useful assertion to make when you are doing
716 * a security audit over a program.
718 * The general idea is that a program which uses just setreuid() will be
719 * 100% compatible with BSD. A program which uses just setuid() will be
720 * 100% compatible with POSIX with saved IDs.
722 asmlinkage
long sys_setreuid(uid_t ruid
, uid_t euid
)
724 int old_ruid
, old_euid
, old_suid
, new_ruid
, new_euid
;
727 retval
= security_task_setuid(ruid
, euid
, (uid_t
)-1, LSM_SETID_RE
);
731 new_ruid
= old_ruid
= current
->uid
;
732 new_euid
= old_euid
= current
->euid
;
733 old_suid
= current
->suid
;
735 if (ruid
!= (uid_t
) -1) {
737 if ((old_ruid
!= ruid
) &&
738 (current
->euid
!= ruid
) &&
739 !capable(CAP_SETUID
))
743 if (euid
!= (uid_t
) -1) {
745 if ((old_ruid
!= euid
) &&
746 (current
->euid
!= euid
) &&
747 (current
->suid
!= euid
) &&
748 !capable(CAP_SETUID
))
752 if (new_ruid
!= old_ruid
&& set_user(new_ruid
, new_euid
!= old_euid
) < 0)
755 if (new_euid
!= old_euid
)
757 current
->mm
->dumpable
= suid_dumpable
;
760 current
->fsuid
= current
->euid
= new_euid
;
761 if (ruid
!= (uid_t
) -1 ||
762 (euid
!= (uid_t
) -1 && euid
!= old_ruid
))
763 current
->suid
= current
->euid
;
764 current
->fsuid
= current
->euid
;
766 key_fsuid_changed(current
);
767 proc_id_connector(current
, PROC_EVENT_UID
);
769 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RE
);
775 * setuid() is implemented like SysV with SAVED_IDS
777 * Note that SAVED_ID's is deficient in that a setuid root program
778 * like sendmail, for example, cannot set its uid to be a normal
779 * user and then switch back, because if you're root, setuid() sets
780 * the saved uid too. If you don't like this, blame the bright people
781 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
782 * will allow a root program to temporarily drop privileges and be able to
783 * regain them by swapping the real and effective uid.
785 asmlinkage
long sys_setuid(uid_t uid
)
787 int old_euid
= current
->euid
;
788 int old_ruid
, old_suid
, new_ruid
, new_suid
;
791 retval
= security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_ID
);
795 old_ruid
= new_ruid
= current
->uid
;
796 old_suid
= current
->suid
;
799 if (capable(CAP_SETUID
)) {
800 if (uid
!= old_ruid
&& set_user(uid
, old_euid
!= uid
) < 0)
803 } else if ((uid
!= current
->uid
) && (uid
!= new_suid
))
808 current
->mm
->dumpable
= suid_dumpable
;
811 current
->fsuid
= current
->euid
= uid
;
812 current
->suid
= new_suid
;
814 key_fsuid_changed(current
);
815 proc_id_connector(current
, PROC_EVENT_UID
);
817 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_ID
);
822 * This function implements a generic ability to update ruid, euid,
823 * and suid. This allows you to implement the 4.4 compatible seteuid().
825 asmlinkage
long sys_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
827 int old_ruid
= current
->uid
;
828 int old_euid
= current
->euid
;
829 int old_suid
= current
->suid
;
832 retval
= security_task_setuid(ruid
, euid
, suid
, LSM_SETID_RES
);
836 if (!capable(CAP_SETUID
)) {
837 if ((ruid
!= (uid_t
) -1) && (ruid
!= current
->uid
) &&
838 (ruid
!= current
->euid
) && (ruid
!= current
->suid
))
840 if ((euid
!= (uid_t
) -1) && (euid
!= current
->uid
) &&
841 (euid
!= current
->euid
) && (euid
!= current
->suid
))
843 if ((suid
!= (uid_t
) -1) && (suid
!= current
->uid
) &&
844 (suid
!= current
->euid
) && (suid
!= current
->suid
))
847 if (ruid
!= (uid_t
) -1) {
848 if (ruid
!= current
->uid
&& set_user(ruid
, euid
!= current
->euid
) < 0)
851 if (euid
!= (uid_t
) -1) {
852 if (euid
!= current
->euid
)
854 current
->mm
->dumpable
= suid_dumpable
;
857 current
->euid
= euid
;
859 current
->fsuid
= current
->euid
;
860 if (suid
!= (uid_t
) -1)
861 current
->suid
= suid
;
863 key_fsuid_changed(current
);
864 proc_id_connector(current
, PROC_EVENT_UID
);
866 return security_task_post_setuid(old_ruid
, old_euid
, old_suid
, LSM_SETID_RES
);
869 asmlinkage
long sys_getresuid(uid_t __user
*ruid
, uid_t __user
*euid
, uid_t __user
*suid
)
873 if (!(retval
= put_user(current
->uid
, ruid
)) &&
874 !(retval
= put_user(current
->euid
, euid
)))
875 retval
= put_user(current
->suid
, suid
);
881 * Same as above, but for rgid, egid, sgid.
883 asmlinkage
long sys_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
887 retval
= security_task_setgid(rgid
, egid
, sgid
, LSM_SETID_RES
);
891 if (!capable(CAP_SETGID
)) {
892 if ((rgid
!= (gid_t
) -1) && (rgid
!= current
->gid
) &&
893 (rgid
!= current
->egid
) && (rgid
!= current
->sgid
))
895 if ((egid
!= (gid_t
) -1) && (egid
!= current
->gid
) &&
896 (egid
!= current
->egid
) && (egid
!= current
->sgid
))
898 if ((sgid
!= (gid_t
) -1) && (sgid
!= current
->gid
) &&
899 (sgid
!= current
->egid
) && (sgid
!= current
->sgid
))
902 if (egid
!= (gid_t
) -1) {
903 if (egid
!= current
->egid
)
905 current
->mm
->dumpable
= suid_dumpable
;
908 current
->egid
= egid
;
910 current
->fsgid
= current
->egid
;
911 if (rgid
!= (gid_t
) -1)
913 if (sgid
!= (gid_t
) -1)
914 current
->sgid
= sgid
;
916 key_fsgid_changed(current
);
917 proc_id_connector(current
, PROC_EVENT_GID
);
921 asmlinkage
long sys_getresgid(gid_t __user
*rgid
, gid_t __user
*egid
, gid_t __user
*sgid
)
925 if (!(retval
= put_user(current
->gid
, rgid
)) &&
926 !(retval
= put_user(current
->egid
, egid
)))
927 retval
= put_user(current
->sgid
, sgid
);
934 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
935 * is used for "access()" and for the NFS daemon (letting nfsd stay at
936 * whatever uid it wants to). It normally shadows "euid", except when
937 * explicitly set by setfsuid() or for access..
939 asmlinkage
long sys_setfsuid(uid_t uid
)
943 old_fsuid
= current
->fsuid
;
944 if (security_task_setuid(uid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
))
947 if (uid
== current
->uid
|| uid
== current
->euid
||
948 uid
== current
->suid
|| uid
== current
->fsuid
||
951 if (uid
!= old_fsuid
)
953 current
->mm
->dumpable
= suid_dumpable
;
956 current
->fsuid
= uid
;
959 key_fsuid_changed(current
);
960 proc_id_connector(current
, PROC_EVENT_UID
);
962 security_task_post_setuid(old_fsuid
, (uid_t
)-1, (uid_t
)-1, LSM_SETID_FS
);
968 * Samma på svenska..
970 asmlinkage
long sys_setfsgid(gid_t gid
)
974 old_fsgid
= current
->fsgid
;
975 if (security_task_setgid(gid
, (gid_t
)-1, (gid_t
)-1, LSM_SETID_FS
))
978 if (gid
== current
->gid
|| gid
== current
->egid
||
979 gid
== current
->sgid
|| gid
== current
->fsgid
||
982 if (gid
!= old_fsgid
)
984 current
->mm
->dumpable
= suid_dumpable
;
987 current
->fsgid
= gid
;
988 key_fsgid_changed(current
);
989 proc_id_connector(current
, PROC_EVENT_GID
);
994 asmlinkage
long sys_times(struct tms __user
* tbuf
)
997 * In the SMP world we might just be unlucky and have one of
998 * the times increment as we use it. Since the value is an
999 * atomically safe type this is just fine. Conceptually its
1000 * as if the syscall took an instant longer to occur.
1004 cputime_t utime
, stime
, cutime
, cstime
;
1007 if (thread_group_empty(current
)) {
1009 * Single thread case without the use of any locks.
1011 * We may race with release_task if two threads are
1012 * executing. However, release task first adds up the
1013 * counters (__exit_signal) before removing the task
1014 * from the process tasklist (__unhash_process).
1015 * __exit_signal also acquires and releases the
1016 * siglock which results in the proper memory ordering
1017 * so that the list modifications are always visible
1018 * after the counters have been updated.
1020 * If the counters have been updated by the second thread
1021 * but the thread has not yet been removed from the list
1022 * then the other branch will be executing which will
1023 * block on tasklist_lock until the exit handling of the
1024 * other task is finished.
1026 * This also implies that the sighand->siglock cannot
1027 * be held by another processor. So we can also
1028 * skip acquiring that lock.
1030 utime
= cputime_add(current
->signal
->utime
, current
->utime
);
1031 stime
= cputime_add(current
->signal
->utime
, current
->stime
);
1032 cutime
= current
->signal
->cutime
;
1033 cstime
= current
->signal
->cstime
;
1038 /* Process with multiple threads */
1039 struct task_struct
*tsk
= current
;
1040 struct task_struct
*t
;
1042 read_lock(&tasklist_lock
);
1043 utime
= tsk
->signal
->utime
;
1044 stime
= tsk
->signal
->stime
;
1047 utime
= cputime_add(utime
, t
->utime
);
1048 stime
= cputime_add(stime
, t
->stime
);
1053 * While we have tasklist_lock read-locked, no dying thread
1054 * can be updating current->signal->[us]time. Instead,
1055 * we got their counts included in the live thread loop.
1056 * However, another thread can come in right now and
1057 * do a wait call that updates current->signal->c[us]time.
1058 * To make sure we always see that pair updated atomically,
1059 * we take the siglock around fetching them.
1061 spin_lock_irq(&tsk
->sighand
->siglock
);
1062 cutime
= tsk
->signal
->cutime
;
1063 cstime
= tsk
->signal
->cstime
;
1064 spin_unlock_irq(&tsk
->sighand
->siglock
);
1065 read_unlock(&tasklist_lock
);
1067 tmp
.tms_utime
= cputime_to_clock_t(utime
);
1068 tmp
.tms_stime
= cputime_to_clock_t(stime
);
1069 tmp
.tms_cutime
= cputime_to_clock_t(cutime
);
1070 tmp
.tms_cstime
= cputime_to_clock_t(cstime
);
1071 if (copy_to_user(tbuf
, &tmp
, sizeof(struct tms
)))
1074 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1078 * This needs some heavy checking ...
1079 * I just haven't the stomach for it. I also don't fully
1080 * understand sessions/pgrp etc. Let somebody who does explain it.
1082 * OK, I think I have the protection semantics right.... this is really
1083 * only important on a multi-user system anyway, to make sure one user
1084 * can't send a signal to a process owned by another. -TYT, 12/12/91
1086 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1090 asmlinkage
long sys_setpgid(pid_t pid
, pid_t pgid
)
1092 struct task_struct
*p
;
1093 struct task_struct
*group_leader
= current
->group_leader
;
1097 pid
= group_leader
->pid
;
1103 /* From this point forward we keep holding onto the tasklist lock
1104 * so that our parent does not change from under us. -DaveM
1106 write_lock_irq(&tasklist_lock
);
1109 p
= find_task_by_pid(pid
);
1114 if (!thread_group_leader(p
))
1117 if (p
->real_parent
== group_leader
) {
1119 if (p
->signal
->session
!= group_leader
->signal
->session
)
1126 if (p
!= group_leader
)
1131 if (p
->signal
->leader
)
1135 struct task_struct
*p
;
1137 do_each_task_pid(pgid
, PIDTYPE_PGID
, p
) {
1138 if (p
->signal
->session
== group_leader
->signal
->session
)
1140 } while_each_task_pid(pgid
, PIDTYPE_PGID
, p
);
1145 err
= security_task_setpgid(p
, pgid
);
1149 if (process_group(p
) != pgid
) {
1150 detach_pid(p
, PIDTYPE_PGID
);
1151 p
->signal
->pgrp
= pgid
;
1152 attach_pid(p
, PIDTYPE_PGID
, pgid
);
1157 /* All paths lead to here, thus we are safe. -DaveM */
1158 write_unlock_irq(&tasklist_lock
);
1162 asmlinkage
long sys_getpgid(pid_t pid
)
1165 return process_group(current
);
1168 struct task_struct
*p
;
1170 read_lock(&tasklist_lock
);
1171 p
= find_task_by_pid(pid
);
1175 retval
= security_task_getpgid(p
);
1177 retval
= process_group(p
);
1179 read_unlock(&tasklist_lock
);
1184 #ifdef __ARCH_WANT_SYS_GETPGRP
1186 asmlinkage
long sys_getpgrp(void)
1188 /* SMP - assuming writes are word atomic this is fine */
1189 return process_group(current
);
1194 asmlinkage
long sys_getsid(pid_t pid
)
1197 return current
->signal
->session
;
1200 struct task_struct
*p
;
1202 read_lock(&tasklist_lock
);
1203 p
= find_task_by_pid(pid
);
1207 retval
= security_task_getsid(p
);
1209 retval
= p
->signal
->session
;
1211 read_unlock(&tasklist_lock
);
1216 asmlinkage
long sys_setsid(void)
1218 struct task_struct
*group_leader
= current
->group_leader
;
1223 write_lock_irq(&tasklist_lock
);
1225 pid
= find_pid(PIDTYPE_PGID
, group_leader
->pid
);
1229 group_leader
->signal
->leader
= 1;
1230 __set_special_pids(group_leader
->pid
, group_leader
->pid
);
1231 group_leader
->signal
->tty
= NULL
;
1232 group_leader
->signal
->tty_old_pgrp
= 0;
1233 err
= process_group(group_leader
);
1235 write_unlock_irq(&tasklist_lock
);
1241 * Supplementary group IDs
1244 /* init to 2 - one for init_task, one to ensure it is never freed */
1245 struct group_info init_groups
= { .usage
= ATOMIC_INIT(2) };
1247 struct group_info
*groups_alloc(int gidsetsize
)
1249 struct group_info
*group_info
;
1253 nblocks
= (gidsetsize
+ NGROUPS_PER_BLOCK
- 1) / NGROUPS_PER_BLOCK
;
1254 /* Make sure we always allocate at least one indirect block pointer */
1255 nblocks
= nblocks
? : 1;
1256 group_info
= kmalloc(sizeof(*group_info
) + nblocks
*sizeof(gid_t
*), GFP_USER
);
1259 group_info
->ngroups
= gidsetsize
;
1260 group_info
->nblocks
= nblocks
;
1261 atomic_set(&group_info
->usage
, 1);
1263 if (gidsetsize
<= NGROUPS_SMALL
) {
1264 group_info
->blocks
[0] = group_info
->small_block
;
1266 for (i
= 0; i
< nblocks
; i
++) {
1268 b
= (void *)__get_free_page(GFP_USER
);
1270 goto out_undo_partial_alloc
;
1271 group_info
->blocks
[i
] = b
;
1276 out_undo_partial_alloc
:
1278 free_page((unsigned long)group_info
->blocks
[i
]);
1284 EXPORT_SYMBOL(groups_alloc
);
1286 void groups_free(struct group_info
*group_info
)
1288 if (group_info
->blocks
[0] != group_info
->small_block
) {
1290 for (i
= 0; i
< group_info
->nblocks
; i
++)
1291 free_page((unsigned long)group_info
->blocks
[i
]);
1296 EXPORT_SYMBOL(groups_free
);
1298 /* export the group_info to a user-space array */
1299 static int groups_to_user(gid_t __user
*grouplist
,
1300 struct group_info
*group_info
)
1303 int count
= group_info
->ngroups
;
1305 for (i
= 0; i
< group_info
->nblocks
; i
++) {
1306 int cp_count
= min(NGROUPS_PER_BLOCK
, count
);
1307 int off
= i
* NGROUPS_PER_BLOCK
;
1308 int len
= cp_count
* sizeof(*grouplist
);
1310 if (copy_to_user(grouplist
+off
, group_info
->blocks
[i
], len
))
1318 /* fill a group_info from a user-space array - it must be allocated already */
1319 static int groups_from_user(struct group_info
*group_info
,
1320 gid_t __user
*grouplist
)
1323 int count
= group_info
->ngroups
;
1325 for (i
= 0; i
< group_info
->nblocks
; i
++) {
1326 int cp_count
= min(NGROUPS_PER_BLOCK
, count
);
1327 int off
= i
* NGROUPS_PER_BLOCK
;
1328 int len
= cp_count
* sizeof(*grouplist
);
1330 if (copy_from_user(group_info
->blocks
[i
], grouplist
+off
, len
))
1338 /* a simple Shell sort */
1339 static void groups_sort(struct group_info
*group_info
)
1341 int base
, max
, stride
;
1342 int gidsetsize
= group_info
->ngroups
;
1344 for (stride
= 1; stride
< gidsetsize
; stride
= 3 * stride
+ 1)
1349 max
= gidsetsize
- stride
;
1350 for (base
= 0; base
< max
; base
++) {
1352 int right
= left
+ stride
;
1353 gid_t tmp
= GROUP_AT(group_info
, right
);
1355 while (left
>= 0 && GROUP_AT(group_info
, left
) > tmp
) {
1356 GROUP_AT(group_info
, right
) =
1357 GROUP_AT(group_info
, left
);
1361 GROUP_AT(group_info
, right
) = tmp
;
1367 /* a simple bsearch */
1368 int groups_search(struct group_info
*group_info
, gid_t grp
)
1376 right
= group_info
->ngroups
;
1377 while (left
< right
) {
1378 int mid
= (left
+right
)/2;
1379 int cmp
= grp
- GROUP_AT(group_info
, mid
);
1390 /* validate and set current->group_info */
1391 int set_current_groups(struct group_info
*group_info
)
1394 struct group_info
*old_info
;
1396 retval
= security_task_setgroups(group_info
);
1400 groups_sort(group_info
);
1401 get_group_info(group_info
);
1404 old_info
= current
->group_info
;
1405 current
->group_info
= group_info
;
1406 task_unlock(current
);
1408 put_group_info(old_info
);
1413 EXPORT_SYMBOL(set_current_groups
);
1415 asmlinkage
long sys_getgroups(int gidsetsize
, gid_t __user
*grouplist
)
1420 * SMP: Nobody else can change our grouplist. Thus we are
1427 /* no need to grab task_lock here; it cannot change */
1428 get_group_info(current
->group_info
);
1429 i
= current
->group_info
->ngroups
;
1431 if (i
> gidsetsize
) {
1435 if (groups_to_user(grouplist
, current
->group_info
)) {
1441 put_group_info(current
->group_info
);
1446 * SMP: Our groups are copy-on-write. We can set them safely
1447 * without another task interfering.
1450 asmlinkage
long sys_setgroups(int gidsetsize
, gid_t __user
*grouplist
)
1452 struct group_info
*group_info
;
1455 if (!capable(CAP_SETGID
))
1457 if ((unsigned)gidsetsize
> NGROUPS_MAX
)
1460 group_info
= groups_alloc(gidsetsize
);
1463 retval
= groups_from_user(group_info
, grouplist
);
1465 put_group_info(group_info
);
1469 retval
= set_current_groups(group_info
);
1470 put_group_info(group_info
);
1476 * Check whether we're fsgid/egid or in the supplemental group..
1478 int in_group_p(gid_t grp
)
1481 if (grp
!= current
->fsgid
) {
1482 get_group_info(current
->group_info
);
1483 retval
= groups_search(current
->group_info
, grp
);
1484 put_group_info(current
->group_info
);
1489 EXPORT_SYMBOL(in_group_p
);
1491 int in_egroup_p(gid_t grp
)
1494 if (grp
!= current
->egid
) {
1495 get_group_info(current
->group_info
);
1496 retval
= groups_search(current
->group_info
, grp
);
1497 put_group_info(current
->group_info
);
1502 EXPORT_SYMBOL(in_egroup_p
);
1504 DECLARE_RWSEM(uts_sem
);
1506 EXPORT_SYMBOL(uts_sem
);
1508 asmlinkage
long sys_newuname(struct new_utsname __user
* name
)
1512 down_read(&uts_sem
);
1513 if (copy_to_user(name
,&system_utsname
,sizeof *name
))
1519 asmlinkage
long sys_sethostname(char __user
*name
, int len
)
1522 char tmp
[__NEW_UTS_LEN
];
1524 if (!capable(CAP_SYS_ADMIN
))
1526 if (len
< 0 || len
> __NEW_UTS_LEN
)
1528 down_write(&uts_sem
);
1530 if (!copy_from_user(tmp
, name
, len
)) {
1531 memcpy(system_utsname
.nodename
, tmp
, len
);
1532 system_utsname
.nodename
[len
] = 0;
1539 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1541 asmlinkage
long sys_gethostname(char __user
*name
, int len
)
1547 down_read(&uts_sem
);
1548 i
= 1 + strlen(system_utsname
.nodename
);
1552 if (copy_to_user(name
, system_utsname
.nodename
, i
))
1561 * Only setdomainname; getdomainname can be implemented by calling
1564 asmlinkage
long sys_setdomainname(char __user
*name
, int len
)
1567 char tmp
[__NEW_UTS_LEN
];
1569 if (!capable(CAP_SYS_ADMIN
))
1571 if (len
< 0 || len
> __NEW_UTS_LEN
)
1574 down_write(&uts_sem
);
1576 if (!copy_from_user(tmp
, name
, len
)) {
1577 memcpy(system_utsname
.domainname
, tmp
, len
);
1578 system_utsname
.domainname
[len
] = 0;
1585 asmlinkage
long sys_getrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1587 if (resource
>= RLIM_NLIMITS
)
1590 struct rlimit value
;
1591 task_lock(current
->group_leader
);
1592 value
= current
->signal
->rlim
[resource
];
1593 task_unlock(current
->group_leader
);
1594 return copy_to_user(rlim
, &value
, sizeof(*rlim
)) ? -EFAULT
: 0;
1598 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1601 * Back compatibility for getrlimit. Needed for some apps.
1604 asmlinkage
long sys_old_getrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1607 if (resource
>= RLIM_NLIMITS
)
1610 task_lock(current
->group_leader
);
1611 x
= current
->signal
->rlim
[resource
];
1612 task_unlock(current
->group_leader
);
1613 if(x
.rlim_cur
> 0x7FFFFFFF)
1614 x
.rlim_cur
= 0x7FFFFFFF;
1615 if(x
.rlim_max
> 0x7FFFFFFF)
1616 x
.rlim_max
= 0x7FFFFFFF;
1617 return copy_to_user(rlim
, &x
, sizeof(x
))?-EFAULT
:0;
1622 asmlinkage
long sys_setrlimit(unsigned int resource
, struct rlimit __user
*rlim
)
1624 struct rlimit new_rlim
, *old_rlim
;
1627 if (resource
>= RLIM_NLIMITS
)
1629 if(copy_from_user(&new_rlim
, rlim
, sizeof(*rlim
)))
1631 if (new_rlim
.rlim_cur
> new_rlim
.rlim_max
)
1633 old_rlim
= current
->signal
->rlim
+ resource
;
1634 if ((new_rlim
.rlim_max
> old_rlim
->rlim_max
) &&
1635 !capable(CAP_SYS_RESOURCE
))
1637 if (resource
== RLIMIT_NOFILE
&& new_rlim
.rlim_max
> NR_OPEN
)
1640 retval
= security_task_setrlimit(resource
, &new_rlim
);
1644 task_lock(current
->group_leader
);
1645 *old_rlim
= new_rlim
;
1646 task_unlock(current
->group_leader
);
1648 if (resource
== RLIMIT_CPU
&& new_rlim
.rlim_cur
!= RLIM_INFINITY
&&
1649 (cputime_eq(current
->signal
->it_prof_expires
, cputime_zero
) ||
1650 new_rlim
.rlim_cur
<= cputime_to_secs(
1651 current
->signal
->it_prof_expires
))) {
1652 cputime_t cputime
= secs_to_cputime(new_rlim
.rlim_cur
);
1653 read_lock(&tasklist_lock
);
1654 spin_lock_irq(¤t
->sighand
->siglock
);
1655 set_process_cpu_timer(current
, CPUCLOCK_PROF
,
1657 spin_unlock_irq(¤t
->sighand
->siglock
);
1658 read_unlock(&tasklist_lock
);
1665 * It would make sense to put struct rusage in the task_struct,
1666 * except that would make the task_struct be *really big*. After
1667 * task_struct gets moved into malloc'ed memory, it would
1668 * make sense to do this. It will make moving the rest of the information
1669 * a lot simpler! (Which we're not doing right now because we're not
1670 * measuring them yet).
1672 * This expects to be called with tasklist_lock read-locked or better,
1673 * and the siglock not locked. It may momentarily take the siglock.
1675 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1676 * races with threads incrementing their own counters. But since word
1677 * reads are atomic, we either get new values or old values and we don't
1678 * care which for the sums. We always take the siglock to protect reading
1679 * the c* fields from p->signal from races with exit.c updating those
1680 * fields when reaping, so a sample either gets all the additions of a
1681 * given child after it's reaped, or none so this sample is before reaping.
1684 static void k_getrusage(struct task_struct
*p
, int who
, struct rusage
*r
)
1686 struct task_struct
*t
;
1687 unsigned long flags
;
1688 cputime_t utime
, stime
;
1690 memset((char *) r
, 0, sizeof *r
);
1692 if (unlikely(!p
->signal
))
1696 case RUSAGE_CHILDREN
:
1697 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
1698 utime
= p
->signal
->cutime
;
1699 stime
= p
->signal
->cstime
;
1700 r
->ru_nvcsw
= p
->signal
->cnvcsw
;
1701 r
->ru_nivcsw
= p
->signal
->cnivcsw
;
1702 r
->ru_minflt
= p
->signal
->cmin_flt
;
1703 r
->ru_majflt
= p
->signal
->cmaj_flt
;
1704 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
1705 cputime_to_timeval(utime
, &r
->ru_utime
);
1706 cputime_to_timeval(stime
, &r
->ru_stime
);
1709 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
1710 utime
= stime
= cputime_zero
;
1713 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
1714 utime
= p
->signal
->cutime
;
1715 stime
= p
->signal
->cstime
;
1716 r
->ru_nvcsw
= p
->signal
->cnvcsw
;
1717 r
->ru_nivcsw
= p
->signal
->cnivcsw
;
1718 r
->ru_minflt
= p
->signal
->cmin_flt
;
1719 r
->ru_majflt
= p
->signal
->cmaj_flt
;
1721 utime
= cputime_add(utime
, p
->signal
->utime
);
1722 stime
= cputime_add(stime
, p
->signal
->stime
);
1723 r
->ru_nvcsw
+= p
->signal
->nvcsw
;
1724 r
->ru_nivcsw
+= p
->signal
->nivcsw
;
1725 r
->ru_minflt
+= p
->signal
->min_flt
;
1726 r
->ru_majflt
+= p
->signal
->maj_flt
;
1729 utime
= cputime_add(utime
, t
->utime
);
1730 stime
= cputime_add(stime
, t
->stime
);
1731 r
->ru_nvcsw
+= t
->nvcsw
;
1732 r
->ru_nivcsw
+= t
->nivcsw
;
1733 r
->ru_minflt
+= t
->min_flt
;
1734 r
->ru_majflt
+= t
->maj_flt
;
1737 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
1738 cputime_to_timeval(utime
, &r
->ru_utime
);
1739 cputime_to_timeval(stime
, &r
->ru_stime
);
1746 int getrusage(struct task_struct
*p
, int who
, struct rusage __user
*ru
)
1749 read_lock(&tasklist_lock
);
1750 k_getrusage(p
, who
, &r
);
1751 read_unlock(&tasklist_lock
);
1752 return copy_to_user(ru
, &r
, sizeof(r
)) ? -EFAULT
: 0;
1755 asmlinkage
long sys_getrusage(int who
, struct rusage __user
*ru
)
1757 if (who
!= RUSAGE_SELF
&& who
!= RUSAGE_CHILDREN
)
1759 return getrusage(current
, who
, ru
);
1762 asmlinkage
long sys_umask(int mask
)
1764 mask
= xchg(¤t
->fs
->umask
, mask
& S_IRWXUGO
);
1768 asmlinkage
long sys_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1769 unsigned long arg4
, unsigned long arg5
)
1773 error
= security_task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1778 case PR_SET_PDEATHSIG
:
1779 if (!valid_signal(arg2
)) {
1783 current
->pdeath_signal
= arg2
;
1785 case PR_GET_PDEATHSIG
:
1786 error
= put_user(current
->pdeath_signal
, (int __user
*)arg2
);
1788 case PR_GET_DUMPABLE
:
1789 error
= current
->mm
->dumpable
;
1791 case PR_SET_DUMPABLE
:
1792 if (arg2
< 0 || arg2
> 2) {
1796 current
->mm
->dumpable
= arg2
;
1799 case PR_SET_UNALIGN
:
1800 error
= SET_UNALIGN_CTL(current
, arg2
);
1802 case PR_GET_UNALIGN
:
1803 error
= GET_UNALIGN_CTL(current
, arg2
);
1806 error
= SET_FPEMU_CTL(current
, arg2
);
1809 error
= GET_FPEMU_CTL(current
, arg2
);
1812 error
= SET_FPEXC_CTL(current
, arg2
);
1815 error
= GET_FPEXC_CTL(current
, arg2
);
1818 error
= PR_TIMING_STATISTICAL
;
1821 if (arg2
== PR_TIMING_STATISTICAL
)
1827 case PR_GET_KEEPCAPS
:
1828 if (current
->keep_capabilities
)
1831 case PR_SET_KEEPCAPS
:
1832 if (arg2
!= 0 && arg2
!= 1) {
1836 current
->keep_capabilities
= arg2
;
1839 struct task_struct
*me
= current
;
1840 unsigned char ncomm
[sizeof(me
->comm
)];
1842 ncomm
[sizeof(me
->comm
)-1] = 0;
1843 if (strncpy_from_user(ncomm
, (char __user
*)arg2
,
1844 sizeof(me
->comm
)-1) < 0)
1846 set_task_comm(me
, ncomm
);
1850 struct task_struct
*me
= current
;
1851 unsigned char tcomm
[sizeof(me
->comm
)];
1853 get_task_comm(tcomm
, me
);
1854 if (copy_to_user((char __user
*)arg2
, tcomm
, sizeof(tcomm
)))