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/capability.h>
23 #include <linux/device.h>
24 #include <linux/key.h>
25 #include <linux/times.h>
26 #include <linux/posix-timers.h>
27 #include <linux/security.h>
28 #include <linux/dcookies.h>
29 #include <linux/suspend.h>
30 #include <linux/tty.h>
31 #include <linux/signal.h>
32 #include <linux/cn_proc.h>
34 #include <linux/compat.h>
35 #include <linux/syscalls.h>
36 #include <linux/kprobes.h>
38 #include <asm/uaccess.h>
40 #include <asm/unistd.h>
42 #ifndef SET_UNALIGN_CTL
43 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
45 #ifndef GET_UNALIGN_CTL
46 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
49 # define SET_FPEMU_CTL(a,b) (-EINVAL)
52 # define GET_FPEMU_CTL(a,b) (-EINVAL)
55 # define SET_FPEXC_CTL(a,b) (-EINVAL)
58 # define GET_FPEXC_CTL(a,b) (-EINVAL)
62 * this is where the system-wide overflow UID and GID are defined, for
63 * architectures that now have 32-bit UID/GID but didn't in the past
66 int overflowuid = DEFAULT_OVERFLOWUID;
67 int overflowgid = DEFAULT_OVERFLOWGID;
70 EXPORT_SYMBOL(overflowuid);
71 EXPORT_SYMBOL(overflowgid);
75 * the same as above, but for filesystems which can only store a 16-bit
76 * UID and GID. as such, this is needed on all architectures
79 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
80 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
82 EXPORT_SYMBOL(fs_overflowuid);
83 EXPORT_SYMBOL(fs_overflowgid);
86 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
93 * Notifier list for kernel code which wants to be called
94 * at shutdown. This is used to stop any idling DMA operations
98 static struct notifier_block *reboot_notifier_list;
99 static DEFINE_RWLOCK(notifier_lock);
102 * notifier_chain_register - Add notifier to a notifier chain
103 * @list: Pointer to root list pointer
104 * @n: New entry in notifier chain
106 * Adds a notifier to a notifier chain.
108 * Currently always returns zero.
111 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
113 write_lock(¬ifier_lock);
116 if(n->priority > (*list)->priority)
118 list= &((*list)->next);
122 write_unlock(¬ifier_lock);
126 EXPORT_SYMBOL(notifier_chain_register);
129 * notifier_chain_unregister - Remove notifier from a notifier chain
130 * @nl: Pointer to root list pointer
131 * @n: New entry in notifier chain
133 * Removes a notifier from a notifier chain.
135 * Returns zero on success, or %-ENOENT on failure.
138 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
140 write_lock(¬ifier_lock);
146 write_unlock(¬ifier_lock);
151 write_unlock(¬ifier_lock);
155 EXPORT_SYMBOL(notifier_chain_unregister);
158 * notifier_call_chain - Call functions in a notifier chain
159 * @n: Pointer to root pointer of notifier chain
160 * @val: Value passed unmodified to notifier function
161 * @v: Pointer passed unmodified to notifier function
163 * Calls each function in a notifier chain in turn.
165 * If the return value of the notifier can be and'd
166 * with %NOTIFY_STOP_MASK, then notifier_call_chain
167 * will return immediately, with the return value of
168 * the notifier function which halted execution.
169 * Otherwise, the return value is the return value
170 * of the last notifier function called.
173 int __kprobes notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
176 struct notifier_block *nb = *n;
180 ret=nb->notifier_call(nb,val,v);
181 if(ret&NOTIFY_STOP_MASK)
190 EXPORT_SYMBOL(notifier_call_chain);
193 * register_reboot_notifier - Register function to be called at reboot time
194 * @nb: Info about notifier function to be called
196 * Registers a function with the list of functions
197 * to be called at reboot time.
199 * Currently always returns zero, as notifier_chain_register
200 * always returns zero.
203 int register_reboot_notifier(struct notifier_block * nb)
205 return notifier_chain_register(&reboot_notifier_list, nb);
208 EXPORT_SYMBOL(register_reboot_notifier);
211 * unregister_reboot_notifier - Unregister previously registered reboot notifier
212 * @nb: Hook to be unregistered
214 * Unregisters a previously registered reboot
217 * Returns zero on success, or %-ENOENT on failure.
220 int unregister_reboot_notifier(struct notifier_block * nb)
222 return notifier_chain_unregister(&reboot_notifier_list, nb);
225 EXPORT_SYMBOL(unregister_reboot_notifier);
227 #ifndef CONFIG_SECURITY
230 if (cap_raised(current->cap_effective, cap)) {
231 current->flags |= PF_SUPERPRIV;
236 EXPORT_SYMBOL(capable);
239 static int set_one_prio(struct task_struct *p, int niceval, int error)
243 if (p->uid != current->euid &&
244 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
248 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
252 no_nice = security_task_setnice(p, niceval);
259 set_user_nice(p, niceval);
264 asmlinkage long sys_setpriority(int which, int who, int niceval)
266 struct task_struct *g, *p;
267 struct user_struct *user;
270 if (which > 2 || which < 0)
273 /* normalize: avoid signed division (rounding problems) */
280 read_lock(&tasklist_lock);
285 p = find_task_by_pid(who);
287 error = set_one_prio(p, niceval, error);
291 who = process_group(current);
292 do_each_task_pid(who, PIDTYPE_PGID, p) {
293 error = set_one_prio(p, niceval, error);
294 } while_each_task_pid(who, PIDTYPE_PGID, p);
297 user = current->user;
301 if ((who != current->uid) && !(user = find_user(who)))
302 goto out_unlock; /* No processes for this user */
306 error = set_one_prio(p, niceval, error);
307 while_each_thread(g, p);
308 if (who != current->uid)
309 free_uid(user); /* For find_user() */
313 read_unlock(&tasklist_lock);
319 * Ugh. To avoid negative return values, "getpriority()" will
320 * not return the normal nice-value, but a negated value that
321 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
322 * to stay compatible.
324 asmlinkage long sys_getpriority(int which, int who)
326 struct task_struct *g, *p;
327 struct user_struct *user;
328 long niceval, retval = -ESRCH;
330 if (which > 2 || which < 0)
333 read_lock(&tasklist_lock);
338 p = find_task_by_pid(who);
340 niceval = 20 - task_nice(p);
341 if (niceval > retval)
347 who = process_group(current);
348 do_each_task_pid(who, PIDTYPE_PGID, p) {
349 niceval = 20 - task_nice(p);
350 if (niceval > retval)
352 } while_each_task_pid(who, PIDTYPE_PGID, p);
355 user = current->user;
359 if ((who != current->uid) && !(user = find_user(who)))
360 goto out_unlock; /* No processes for this user */
364 niceval = 20 - task_nice(p);
365 if (niceval > retval)
368 while_each_thread(g, p);
369 if (who != current->uid)
370 free_uid(user); /* for find_user() */
374 read_unlock(&tasklist_lock);
380 * emergency_restart - reboot the system
382 * Without shutting down any hardware or taking any locks
383 * reboot the system. This is called when we know we are in
384 * trouble so this is our best effort to reboot. This is
385 * safe to call in interrupt context.
387 void emergency_restart(void)
389 machine_emergency_restart();
391 EXPORT_SYMBOL_GPL(emergency_restart);
393 void kernel_restart_prepare(char *cmd)
395 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
396 system_state = SYSTEM_RESTART;
401 * kernel_restart - reboot the system
402 * @cmd: pointer to buffer containing command to execute for restart
405 * Shutdown everything and perform a clean reboot.
406 * This is not safe to call in interrupt context.
408 void kernel_restart(char *cmd)
410 kernel_restart_prepare(cmd);
412 printk(KERN_EMERG "Restarting system.\n");
414 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
417 machine_restart(cmd);
419 EXPORT_SYMBOL_GPL(kernel_restart);
422 * kernel_kexec - reboot the system
424 * Move into place and start executing a preloaded standalone
425 * executable. If nothing was preloaded return an error.
427 void kernel_kexec(void)
430 struct kimage *image;
431 image = xchg(&kexec_image, NULL);
435 kernel_restart_prepare(NULL);
436 printk(KERN_EMERG "Starting new kernel\n");
438 machine_kexec(image);
441 EXPORT_SYMBOL_GPL(kernel_kexec);
443 void kernel_shutdown_prepare(enum system_states state)
445 notifier_call_chain(&reboot_notifier_list,
446 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
447 system_state = state;
451 * kernel_halt - halt the system
453 * Shutdown everything and perform a clean system halt.
455 void kernel_halt(void)
457 kernel_shutdown_prepare(SYSTEM_HALT);
458 printk(KERN_EMERG "System halted.\n");
462 EXPORT_SYMBOL_GPL(kernel_halt);
465 * kernel_power_off - power_off the system
467 * Shutdown everything and perform a clean system power_off.
469 void kernel_power_off(void)
471 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
472 printk(KERN_EMERG "Power down.\n");
475 EXPORT_SYMBOL_GPL(kernel_power_off);
477 * Reboot system call: for obvious reasons only root may call it,
478 * and even root needs to set up some magic numbers in the registers
479 * so that some mistake won't make this reboot the whole machine.
480 * You can also set the meaning of the ctrl-alt-del-key here.
482 * reboot doesn't sync: do that yourself before calling this.
484 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
488 /* We only trust the superuser with rebooting the system. */
489 if (!capable(CAP_SYS_BOOT))
492 /* For safety, we require "magic" arguments. */
493 if (magic1 != LINUX_REBOOT_MAGIC1 ||
494 (magic2 != LINUX_REBOOT_MAGIC2 &&
495 magic2 != LINUX_REBOOT_MAGIC2A &&
496 magic2 != LINUX_REBOOT_MAGIC2B &&
497 magic2 != LINUX_REBOOT_MAGIC2C))
500 /* Instead of trying to make the power_off code look like
501 * halt when pm_power_off is not set do it the easy way.
503 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
504 cmd = LINUX_REBOOT_CMD_HALT;
508 case LINUX_REBOOT_CMD_RESTART:
509 kernel_restart(NULL);
512 case LINUX_REBOOT_CMD_CAD_ON:
516 case LINUX_REBOOT_CMD_CAD_OFF:
520 case LINUX_REBOOT_CMD_HALT:
526 case LINUX_REBOOT_CMD_POWER_OFF:
532 case LINUX_REBOOT_CMD_RESTART2:
533 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
537 buffer[sizeof(buffer) - 1] = '\0';
539 kernel_restart(buffer);
542 case LINUX_REBOOT_CMD_KEXEC:
547 #ifdef CONFIG_SOFTWARE_SUSPEND
548 case LINUX_REBOOT_CMD_SW_SUSPEND:
550 int ret = software_suspend();
564 static void deferred_cad(void *dummy)
566 kernel_restart(NULL);
570 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
571 * As it's called within an interrupt, it may NOT sync: the only choice
572 * is whether to reboot at once, or just ignore the ctrl-alt-del.
574 void ctrl_alt_del(void)
576 static DECLARE_WORK(cad_work, deferred_cad, NULL);
579 schedule_work(&cad_work);
581 kill_proc(cad_pid, SIGINT, 1);
586 * Unprivileged users may change the real gid to the effective gid
587 * or vice versa. (BSD-style)
589 * If you set the real gid at all, or set the effective gid to a value not
590 * equal to the real gid, then the saved gid is set to the new effective gid.
592 * This makes it possible for a setgid program to completely drop its
593 * privileges, which is often a useful assertion to make when you are doing
594 * a security audit over a program.
596 * The general idea is that a program which uses just setregid() will be
597 * 100% compatible with BSD. A program which uses just setgid() will be
598 * 100% compatible with POSIX with saved IDs.
600 * SMP: There are not races, the GIDs are checked only by filesystem
601 * operations (as far as semantic preservation is concerned).
603 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
605 int old_rgid = current->gid;
606 int old_egid = current->egid;
607 int new_rgid = old_rgid;
608 int new_egid = old_egid;
611 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
615 if (rgid != (gid_t) -1) {
616 if ((old_rgid == rgid) ||
617 (current->egid==rgid) ||
623 if (egid != (gid_t) -1) {
624 if ((old_rgid == egid) ||
625 (current->egid == egid) ||
626 (current->sgid == egid) ||
633 if (new_egid != old_egid)
635 current->mm->dumpable = suid_dumpable;
638 if (rgid != (gid_t) -1 ||
639 (egid != (gid_t) -1 && egid != old_rgid))
640 current->sgid = new_egid;
641 current->fsgid = new_egid;
642 current->egid = new_egid;
643 current->gid = new_rgid;
644 key_fsgid_changed(current);
645 proc_id_connector(current, PROC_EVENT_GID);
650 * setgid() is implemented like SysV w/ SAVED_IDS
652 * SMP: Same implicit races as above.
654 asmlinkage long sys_setgid(gid_t gid)
656 int old_egid = current->egid;
659 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
663 if (capable(CAP_SETGID))
667 current->mm->dumpable = suid_dumpable;
670 current->gid = current->egid = current->sgid = current->fsgid = gid;
672 else if ((gid == current->gid) || (gid == current->sgid))
676 current->mm->dumpable = suid_dumpable;
679 current->egid = current->fsgid = gid;
684 key_fsgid_changed(current);
685 proc_id_connector(current, PROC_EVENT_GID);
689 static int set_user(uid_t new_ruid, int dumpclear)
691 struct user_struct *new_user;
693 new_user = alloc_uid(new_ruid);
697 if (atomic_read(&new_user->processes) >=
698 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
699 new_user != &root_user) {
704 switch_uid(new_user);
708 current->mm->dumpable = suid_dumpable;
711 current->uid = new_ruid;
716 * Unprivileged users may change the real uid to the effective uid
717 * or vice versa. (BSD-style)
719 * If you set the real uid at all, or set the effective uid to a value not
720 * equal to the real uid, then the saved uid is set to the new effective uid.
722 * This makes it possible for a setuid program to completely drop its
723 * privileges, which is often a useful assertion to make when you are doing
724 * a security audit over a program.
726 * The general idea is that a program which uses just setreuid() will be
727 * 100% compatible with BSD. A program which uses just setuid() will be
728 * 100% compatible with POSIX with saved IDs.
730 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
732 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
735 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
739 new_ruid = old_ruid = current->uid;
740 new_euid = old_euid = current->euid;
741 old_suid = current->suid;
743 if (ruid != (uid_t) -1) {
745 if ((old_ruid != ruid) &&
746 (current->euid != ruid) &&
747 !capable(CAP_SETUID))
751 if (euid != (uid_t) -1) {
753 if ((old_ruid != euid) &&
754 (current->euid != euid) &&
755 (current->suid != euid) &&
756 !capable(CAP_SETUID))
760 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
763 if (new_euid != old_euid)
765 current->mm->dumpable = suid_dumpable;
768 current->fsuid = current->euid = new_euid;
769 if (ruid != (uid_t) -1 ||
770 (euid != (uid_t) -1 && euid != old_ruid))
771 current->suid = current->euid;
772 current->fsuid = current->euid;
774 key_fsuid_changed(current);
775 proc_id_connector(current, PROC_EVENT_UID);
777 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
783 * setuid() is implemented like SysV with SAVED_IDS
785 * Note that SAVED_ID's is deficient in that a setuid root program
786 * like sendmail, for example, cannot set its uid to be a normal
787 * user and then switch back, because if you're root, setuid() sets
788 * the saved uid too. If you don't like this, blame the bright people
789 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
790 * will allow a root program to temporarily drop privileges and be able to
791 * regain them by swapping the real and effective uid.
793 asmlinkage long sys_setuid(uid_t uid)
795 int old_euid = current->euid;
796 int old_ruid, old_suid, new_ruid, new_suid;
799 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
803 old_ruid = new_ruid = current->uid;
804 old_suid = current->suid;
807 if (capable(CAP_SETUID)) {
808 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
811 } else if ((uid != current->uid) && (uid != new_suid))
816 current->mm->dumpable = suid_dumpable;
819 current->fsuid = current->euid = uid;
820 current->suid = new_suid;
822 key_fsuid_changed(current);
823 proc_id_connector(current, PROC_EVENT_UID);
825 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
830 * This function implements a generic ability to update ruid, euid,
831 * and suid. This allows you to implement the 4.4 compatible seteuid().
833 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
835 int old_ruid = current->uid;
836 int old_euid = current->euid;
837 int old_suid = current->suid;
840 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
844 if (!capable(CAP_SETUID)) {
845 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
846 (ruid != current->euid) && (ruid != current->suid))
848 if ((euid != (uid_t) -1) && (euid != current->uid) &&
849 (euid != current->euid) && (euid != current->suid))
851 if ((suid != (uid_t) -1) && (suid != current->uid) &&
852 (suid != current->euid) && (suid != current->suid))
855 if (ruid != (uid_t) -1) {
856 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
859 if (euid != (uid_t) -1) {
860 if (euid != current->euid)
862 current->mm->dumpable = suid_dumpable;
865 current->euid = euid;
867 current->fsuid = current->euid;
868 if (suid != (uid_t) -1)
869 current->suid = suid;
871 key_fsuid_changed(current);
872 proc_id_connector(current, PROC_EVENT_UID);
874 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
877 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
881 if (!(retval = put_user(current->uid, ruid)) &&
882 !(retval = put_user(current->euid, euid)))
883 retval = put_user(current->suid, suid);
889 * Same as above, but for rgid, egid, sgid.
891 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
895 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
899 if (!capable(CAP_SETGID)) {
900 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
901 (rgid != current->egid) && (rgid != current->sgid))
903 if ((egid != (gid_t) -1) && (egid != current->gid) &&
904 (egid != current->egid) && (egid != current->sgid))
906 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
907 (sgid != current->egid) && (sgid != current->sgid))
910 if (egid != (gid_t) -1) {
911 if (egid != current->egid)
913 current->mm->dumpable = suid_dumpable;
916 current->egid = egid;
918 current->fsgid = current->egid;
919 if (rgid != (gid_t) -1)
921 if (sgid != (gid_t) -1)
922 current->sgid = sgid;
924 key_fsgid_changed(current);
925 proc_id_connector(current, PROC_EVENT_GID);
929 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
933 if (!(retval = put_user(current->gid, rgid)) &&
934 !(retval = put_user(current->egid, egid)))
935 retval = put_user(current->sgid, sgid);
942 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
943 * is used for "access()" and for the NFS daemon (letting nfsd stay at
944 * whatever uid it wants to). It normally shadows "euid", except when
945 * explicitly set by setfsuid() or for access..
947 asmlinkage long sys_setfsuid(uid_t uid)
951 old_fsuid = current->fsuid;
952 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
955 if (uid == current->uid || uid == current->euid ||
956 uid == current->suid || uid == current->fsuid ||
959 if (uid != old_fsuid)
961 current->mm->dumpable = suid_dumpable;
964 current->fsuid = uid;
967 key_fsuid_changed(current);
968 proc_id_connector(current, PROC_EVENT_UID);
970 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
976 * Samma på svenska..
978 asmlinkage long sys_setfsgid(gid_t gid)
982 old_fsgid = current->fsgid;
983 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
986 if (gid == current->gid || gid == current->egid ||
987 gid == current->sgid || gid == current->fsgid ||
990 if (gid != old_fsgid)
992 current->mm->dumpable = suid_dumpable;
995 current->fsgid = gid;
996 key_fsgid_changed(current);
997 proc_id_connector(current, PROC_EVENT_GID);
1002 asmlinkage long sys_times(struct tms __user * tbuf)
1005 * In the SMP world we might just be unlucky and have one of
1006 * the times increment as we use it. Since the value is an
1007 * atomically safe type this is just fine. Conceptually its
1008 * as if the syscall took an instant longer to occur.
1012 cputime_t utime, stime, cutime, cstime;
1015 if (thread_group_empty(current)) {
1017 * Single thread case without the use of any locks.
1019 * We may race with release_task if two threads are
1020 * executing. However, release task first adds up the
1021 * counters (__exit_signal) before removing the task
1022 * from the process tasklist (__unhash_process).
1023 * __exit_signal also acquires and releases the
1024 * siglock which results in the proper memory ordering
1025 * so that the list modifications are always visible
1026 * after the counters have been updated.
1028 * If the counters have been updated by the second thread
1029 * but the thread has not yet been removed from the list
1030 * then the other branch will be executing which will
1031 * block on tasklist_lock until the exit handling of the
1032 * other task is finished.
1034 * This also implies that the sighand->siglock cannot
1035 * be held by another processor. So we can also
1036 * skip acquiring that lock.
1038 utime = cputime_add(current->signal->utime, current->utime);
1039 stime = cputime_add(current->signal->utime, current->stime);
1040 cutime = current->signal->cutime;
1041 cstime = current->signal->cstime;
1046 /* Process with multiple threads */
1047 struct task_struct *tsk = current;
1048 struct task_struct *t;
1050 read_lock(&tasklist_lock);
1051 utime = tsk->signal->utime;
1052 stime = tsk->signal->stime;
1055 utime = cputime_add(utime, t->utime);
1056 stime = cputime_add(stime, t->stime);
1061 * While we have tasklist_lock read-locked, no dying thread
1062 * can be updating current->signal->[us]time. Instead,
1063 * we got their counts included in the live thread loop.
1064 * However, another thread can come in right now and
1065 * do a wait call that updates current->signal->c[us]time.
1066 * To make sure we always see that pair updated atomically,
1067 * we take the siglock around fetching them.
1069 spin_lock_irq(&tsk->sighand->siglock);
1070 cutime = tsk->signal->cutime;
1071 cstime = tsk->signal->cstime;
1072 spin_unlock_irq(&tsk->sighand->siglock);
1073 read_unlock(&tasklist_lock);
1075 tmp.tms_utime = cputime_to_clock_t(utime);
1076 tmp.tms_stime = cputime_to_clock_t(stime);
1077 tmp.tms_cutime = cputime_to_clock_t(cutime);
1078 tmp.tms_cstime = cputime_to_clock_t(cstime);
1079 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1082 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1086 * This needs some heavy checking ...
1087 * I just haven't the stomach for it. I also don't fully
1088 * understand sessions/pgrp etc. Let somebody who does explain it.
1090 * OK, I think I have the protection semantics right.... this is really
1091 * only important on a multi-user system anyway, to make sure one user
1092 * can't send a signal to a process owned by another. -TYT, 12/12/91
1094 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1098 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1100 struct task_struct *p;
1101 struct task_struct *group_leader = current->group_leader;
1105 pid = group_leader->pid;
1111 /* From this point forward we keep holding onto the tasklist lock
1112 * so that our parent does not change from under us. -DaveM
1114 write_lock_irq(&tasklist_lock);
1117 p = find_task_by_pid(pid);
1122 if (!thread_group_leader(p))
1125 if (p->real_parent == group_leader) {
1127 if (p->signal->session != group_leader->signal->session)
1134 if (p != group_leader)
1139 if (p->signal->leader)
1143 struct task_struct *p;
1145 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
1146 if (p->signal->session == group_leader->signal->session)
1148 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1153 err = security_task_setpgid(p, pgid);
1157 if (process_group(p) != pgid) {
1158 detach_pid(p, PIDTYPE_PGID);
1159 p->signal->pgrp = pgid;
1160 attach_pid(p, PIDTYPE_PGID, pgid);
1165 /* All paths lead to here, thus we are safe. -DaveM */
1166 write_unlock_irq(&tasklist_lock);
1170 asmlinkage long sys_getpgid(pid_t pid)
1173 return process_group(current);
1176 struct task_struct *p;
1178 read_lock(&tasklist_lock);
1179 p = find_task_by_pid(pid);
1183 retval = security_task_getpgid(p);
1185 retval = process_group(p);
1187 read_unlock(&tasklist_lock);
1192 #ifdef __ARCH_WANT_SYS_GETPGRP
1194 asmlinkage long sys_getpgrp(void)
1196 /* SMP - assuming writes are word atomic this is fine */
1197 return process_group(current);
1202 asmlinkage long sys_getsid(pid_t pid)
1205 return current->signal->session;
1208 struct task_struct *p;
1210 read_lock(&tasklist_lock);
1211 p = find_task_by_pid(pid);
1215 retval = security_task_getsid(p);
1217 retval = p->signal->session;
1219 read_unlock(&tasklist_lock);
1224 asmlinkage long sys_setsid(void)
1226 struct task_struct *group_leader = current->group_leader;
1230 mutex_lock(&tty_mutex);
1231 write_lock_irq(&tasklist_lock);
1233 pid = find_pid(PIDTYPE_PGID, group_leader->pid);
1237 group_leader->signal->leader = 1;
1238 __set_special_pids(group_leader->pid, group_leader->pid);
1239 group_leader->signal->tty = NULL;
1240 group_leader->signal->tty_old_pgrp = 0;
1241 err = process_group(group_leader);
1243 write_unlock_irq(&tasklist_lock);
1244 mutex_unlock(&tty_mutex);
1249 * Supplementary group IDs
1252 /* init to 2 - one for init_task, one to ensure it is never freed */
1253 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1255 struct group_info *groups_alloc(int gidsetsize)
1257 struct group_info *group_info;
1261 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1262 /* Make sure we always allocate at least one indirect block pointer */
1263 nblocks = nblocks ? : 1;
1264 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1267 group_info->ngroups = gidsetsize;
1268 group_info->nblocks = nblocks;
1269 atomic_set(&group_info->usage, 1);
1271 if (gidsetsize <= NGROUPS_SMALL) {
1272 group_info->blocks[0] = group_info->small_block;
1274 for (i = 0; i < nblocks; i++) {
1276 b = (void *)__get_free_page(GFP_USER);
1278 goto out_undo_partial_alloc;
1279 group_info->blocks[i] = b;
1284 out_undo_partial_alloc:
1286 free_page((unsigned long)group_info->blocks[i]);
1292 EXPORT_SYMBOL(groups_alloc);
1294 void groups_free(struct group_info *group_info)
1296 if (group_info->blocks[0] != group_info->small_block) {
1298 for (i = 0; i < group_info->nblocks; i++)
1299 free_page((unsigned long)group_info->blocks[i]);
1304 EXPORT_SYMBOL(groups_free);
1306 /* export the group_info to a user-space array */
1307 static int groups_to_user(gid_t __user *grouplist,
1308 struct group_info *group_info)
1311 int count = group_info->ngroups;
1313 for (i = 0; i < group_info->nblocks; i++) {
1314 int cp_count = min(NGROUPS_PER_BLOCK, count);
1315 int off = i * NGROUPS_PER_BLOCK;
1316 int len = cp_count * sizeof(*grouplist);
1318 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1326 /* fill a group_info from a user-space array - it must be allocated already */
1327 static int groups_from_user(struct group_info *group_info,
1328 gid_t __user *grouplist)
1331 int count = group_info->ngroups;
1333 for (i = 0; i < group_info->nblocks; i++) {
1334 int cp_count = min(NGROUPS_PER_BLOCK, count);
1335 int off = i * NGROUPS_PER_BLOCK;
1336 int len = cp_count * sizeof(*grouplist);
1338 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1346 /* a simple Shell sort */
1347 static void groups_sort(struct group_info *group_info)
1349 int base, max, stride;
1350 int gidsetsize = group_info->ngroups;
1352 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1357 max = gidsetsize - stride;
1358 for (base = 0; base < max; base++) {
1360 int right = left + stride;
1361 gid_t tmp = GROUP_AT(group_info, right);
1363 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1364 GROUP_AT(group_info, right) =
1365 GROUP_AT(group_info, left);
1369 GROUP_AT(group_info, right) = tmp;
1375 /* a simple bsearch */
1376 int groups_search(struct group_info *group_info, gid_t grp)
1384 right = group_info->ngroups;
1385 while (left < right) {
1386 int mid = (left+right)/2;
1387 int cmp = grp - GROUP_AT(group_info, mid);
1398 /* validate and set current->group_info */
1399 int set_current_groups(struct group_info *group_info)
1402 struct group_info *old_info;
1404 retval = security_task_setgroups(group_info);
1408 groups_sort(group_info);
1409 get_group_info(group_info);
1412 old_info = current->group_info;
1413 current->group_info = group_info;
1414 task_unlock(current);
1416 put_group_info(old_info);
1421 EXPORT_SYMBOL(set_current_groups);
1423 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1428 * SMP: Nobody else can change our grouplist. Thus we are
1435 /* no need to grab task_lock here; it cannot change */
1436 get_group_info(current->group_info);
1437 i = current->group_info->ngroups;
1439 if (i > gidsetsize) {
1443 if (groups_to_user(grouplist, current->group_info)) {
1449 put_group_info(current->group_info);
1454 * SMP: Our groups are copy-on-write. We can set them safely
1455 * without another task interfering.
1458 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1460 struct group_info *group_info;
1463 if (!capable(CAP_SETGID))
1465 if ((unsigned)gidsetsize > NGROUPS_MAX)
1468 group_info = groups_alloc(gidsetsize);
1471 retval = groups_from_user(group_info, grouplist);
1473 put_group_info(group_info);
1477 retval = set_current_groups(group_info);
1478 put_group_info(group_info);
1484 * Check whether we're fsgid/egid or in the supplemental group..
1486 int in_group_p(gid_t grp)
1489 if (grp != current->fsgid) {
1490 get_group_info(current->group_info);
1491 retval = groups_search(current->group_info, grp);
1492 put_group_info(current->group_info);
1497 EXPORT_SYMBOL(in_group_p);
1499 int in_egroup_p(gid_t grp)
1502 if (grp != current->egid) {
1503 get_group_info(current->group_info);
1504 retval = groups_search(current->group_info, grp);
1505 put_group_info(current->group_info);
1510 EXPORT_SYMBOL(in_egroup_p);
1512 DECLARE_RWSEM(uts_sem);
1514 EXPORT_SYMBOL(uts_sem);
1516 asmlinkage long sys_newuname(struct new_utsname __user * name)
1520 down_read(&uts_sem);
1521 if (copy_to_user(name,&system_utsname,sizeof *name))
1527 asmlinkage long sys_sethostname(char __user *name, int len)
1530 char tmp[__NEW_UTS_LEN];
1532 if (!capable(CAP_SYS_ADMIN))
1534 if (len < 0 || len > __NEW_UTS_LEN)
1536 down_write(&uts_sem);
1538 if (!copy_from_user(tmp, name, len)) {
1539 memcpy(system_utsname.nodename, tmp, len);
1540 system_utsname.nodename[len] = 0;
1547 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1549 asmlinkage long sys_gethostname(char __user *name, int len)
1555 down_read(&uts_sem);
1556 i = 1 + strlen(system_utsname.nodename);
1560 if (copy_to_user(name, system_utsname.nodename, i))
1569 * Only setdomainname; getdomainname can be implemented by calling
1572 asmlinkage long sys_setdomainname(char __user *name, int len)
1575 char tmp[__NEW_UTS_LEN];
1577 if (!capable(CAP_SYS_ADMIN))
1579 if (len < 0 || len > __NEW_UTS_LEN)
1582 down_write(&uts_sem);
1584 if (!copy_from_user(tmp, name, len)) {
1585 memcpy(system_utsname.domainname, tmp, len);
1586 system_utsname.domainname[len] = 0;
1593 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1595 if (resource >= RLIM_NLIMITS)
1598 struct rlimit value;
1599 task_lock(current->group_leader);
1600 value = current->signal->rlim[resource];
1601 task_unlock(current->group_leader);
1602 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1606 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1609 * Back compatibility for getrlimit. Needed for some apps.
1612 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1615 if (resource >= RLIM_NLIMITS)
1618 task_lock(current->group_leader);
1619 x = current->signal->rlim[resource];
1620 task_unlock(current->group_leader);
1621 if(x.rlim_cur > 0x7FFFFFFF)
1622 x.rlim_cur = 0x7FFFFFFF;
1623 if(x.rlim_max > 0x7FFFFFFF)
1624 x.rlim_max = 0x7FFFFFFF;
1625 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1630 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1632 struct rlimit new_rlim, *old_rlim;
1633 unsigned long it_prof_secs;
1636 if (resource >= RLIM_NLIMITS)
1638 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1640 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1642 old_rlim = current->signal->rlim + resource;
1643 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1644 !capable(CAP_SYS_RESOURCE))
1646 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1649 retval = security_task_setrlimit(resource, &new_rlim);
1653 task_lock(current->group_leader);
1654 *old_rlim = new_rlim;
1655 task_unlock(current->group_leader);
1657 if (resource != RLIMIT_CPU)
1659 if (new_rlim.rlim_cur == RLIM_INFINITY)
1662 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1663 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
1664 unsigned long rlim_cur = new_rlim.rlim_cur;
1667 if (rlim_cur == 0) {
1669 * The caller is asking for an immediate RLIMIT_CPU
1670 * expiry. But we use the zero value to mean "it was
1671 * never set". So let's cheat and make it one second
1676 cputime = secs_to_cputime(rlim_cur);
1677 read_lock(&tasklist_lock);
1678 spin_lock_irq(¤t->sighand->siglock);
1679 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
1680 spin_unlock_irq(¤t->sighand->siglock);
1681 read_unlock(&tasklist_lock);
1688 * It would make sense to put struct rusage in the task_struct,
1689 * except that would make the task_struct be *really big*. After
1690 * task_struct gets moved into malloc'ed memory, it would
1691 * make sense to do this. It will make moving the rest of the information
1692 * a lot simpler! (Which we're not doing right now because we're not
1693 * measuring them yet).
1695 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1696 * races with threads incrementing their own counters. But since word
1697 * reads are atomic, we either get new values or old values and we don't
1698 * care which for the sums. We always take the siglock to protect reading
1699 * the c* fields from p->signal from races with exit.c updating those
1700 * fields when reaping, so a sample either gets all the additions of a
1701 * given child after it's reaped, or none so this sample is before reaping.
1703 * tasklist_lock locking optimisation:
1704 * If we are current and single threaded, we do not need to take the tasklist
1705 * lock or the siglock. No one else can take our signal_struct away,
1706 * no one else can reap the children to update signal->c* counters, and
1707 * no one else can race with the signal-> fields.
1708 * If we do not take the tasklist_lock, the signal-> fields could be read
1709 * out of order while another thread was just exiting. So we place a
1710 * read memory barrier when we avoid the lock. On the writer side,
1711 * write memory barrier is implied in __exit_signal as __exit_signal releases
1712 * the siglock spinlock after updating the signal-> fields.
1714 * We don't really need the siglock when we access the non c* fields
1715 * of the signal_struct (for RUSAGE_SELF) even in multithreaded
1716 * case, since we take the tasklist lock for read and the non c* signal->
1717 * fields are updated only in __exit_signal, which is called with
1718 * tasklist_lock taken for write, hence these two threads cannot execute
1723 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1725 struct task_struct *t;
1726 unsigned long flags;
1727 cputime_t utime, stime;
1730 memset((char *) r, 0, sizeof *r);
1731 utime = stime = cputime_zero;
1733 if (p != current || !thread_group_empty(p))
1737 read_lock(&tasklist_lock);
1738 if (unlikely(!p->signal)) {
1739 read_unlock(&tasklist_lock);
1743 /* See locking comments above */
1748 case RUSAGE_CHILDREN:
1749 spin_lock_irqsave(&p->sighand->siglock, flags);
1750 utime = p->signal->cutime;
1751 stime = p->signal->cstime;
1752 r->ru_nvcsw = p->signal->cnvcsw;
1753 r->ru_nivcsw = p->signal->cnivcsw;
1754 r->ru_minflt = p->signal->cmin_flt;
1755 r->ru_majflt = p->signal->cmaj_flt;
1756 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1758 if (who == RUSAGE_CHILDREN)
1762 utime = cputime_add(utime, p->signal->utime);
1763 stime = cputime_add(stime, p->signal->stime);
1764 r->ru_nvcsw += p->signal->nvcsw;
1765 r->ru_nivcsw += p->signal->nivcsw;
1766 r->ru_minflt += p->signal->min_flt;
1767 r->ru_majflt += p->signal->maj_flt;
1770 utime = cputime_add(utime, t->utime);
1771 stime = cputime_add(stime, t->stime);
1772 r->ru_nvcsw += t->nvcsw;
1773 r->ru_nivcsw += t->nivcsw;
1774 r->ru_minflt += t->min_flt;
1775 r->ru_majflt += t->maj_flt;
1785 read_unlock(&tasklist_lock);
1786 cputime_to_timeval(utime, &r->ru_utime);
1787 cputime_to_timeval(stime, &r->ru_stime);
1790 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1793 k_getrusage(p, who, &r);
1794 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1797 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1799 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1801 return getrusage(current, who, ru);
1804 asmlinkage long sys_umask(int mask)
1806 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1810 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1811 unsigned long arg4, unsigned long arg5)
1815 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1820 case PR_SET_PDEATHSIG:
1821 if (!valid_signal(arg2)) {
1825 current->pdeath_signal = arg2;
1827 case PR_GET_PDEATHSIG:
1828 error = put_user(current->pdeath_signal, (int __user *)arg2);
1830 case PR_GET_DUMPABLE:
1831 error = current->mm->dumpable;
1833 case PR_SET_DUMPABLE:
1834 if (arg2 < 0 || arg2 > 2) {
1838 current->mm->dumpable = arg2;
1841 case PR_SET_UNALIGN:
1842 error = SET_UNALIGN_CTL(current, arg2);
1844 case PR_GET_UNALIGN:
1845 error = GET_UNALIGN_CTL(current, arg2);
1848 error = SET_FPEMU_CTL(current, arg2);
1851 error = GET_FPEMU_CTL(current, arg2);
1854 error = SET_FPEXC_CTL(current, arg2);
1857 error = GET_FPEXC_CTL(current, arg2);
1860 error = PR_TIMING_STATISTICAL;
1863 if (arg2 == PR_TIMING_STATISTICAL)
1869 case PR_GET_KEEPCAPS:
1870 if (current->keep_capabilities)
1873 case PR_SET_KEEPCAPS:
1874 if (arg2 != 0 && arg2 != 1) {
1878 current->keep_capabilities = arg2;
1881 struct task_struct *me = current;
1882 unsigned char ncomm[sizeof(me->comm)];
1884 ncomm[sizeof(me->comm)-1] = 0;
1885 if (strncpy_from_user(ncomm, (char __user *)arg2,
1886 sizeof(me->comm)-1) < 0)
1888 set_task_comm(me, ncomm);
1892 struct task_struct *me = current;
1893 unsigned char tcomm[sizeof(me->comm)];
1895 get_task_comm(tcomm, me);
1896 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))