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 static int set_one_prio(struct task_struct *p, int niceval, int error)
231 if (p->uid != current->euid &&
232 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
236 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
240 no_nice = security_task_setnice(p, niceval);
247 set_user_nice(p, niceval);
252 asmlinkage long sys_setpriority(int which, int who, int niceval)
254 struct task_struct *g, *p;
255 struct user_struct *user;
258 if (which > 2 || which < 0)
261 /* normalize: avoid signed division (rounding problems) */
268 read_lock(&tasklist_lock);
273 p = find_task_by_pid(who);
275 error = set_one_prio(p, niceval, error);
279 who = process_group(current);
280 do_each_task_pid(who, PIDTYPE_PGID, p) {
281 error = set_one_prio(p, niceval, error);
282 } while_each_task_pid(who, PIDTYPE_PGID, p);
285 user = current->user;
289 if ((who != current->uid) && !(user = find_user(who)))
290 goto out_unlock; /* No processes for this user */
294 error = set_one_prio(p, niceval, error);
295 while_each_thread(g, p);
296 if (who != current->uid)
297 free_uid(user); /* For find_user() */
301 read_unlock(&tasklist_lock);
307 * Ugh. To avoid negative return values, "getpriority()" will
308 * not return the normal nice-value, but a negated value that
309 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
310 * to stay compatible.
312 asmlinkage long sys_getpriority(int which, int who)
314 struct task_struct *g, *p;
315 struct user_struct *user;
316 long niceval, retval = -ESRCH;
318 if (which > 2 || which < 0)
321 read_lock(&tasklist_lock);
326 p = find_task_by_pid(who);
328 niceval = 20 - task_nice(p);
329 if (niceval > retval)
335 who = process_group(current);
336 do_each_task_pid(who, PIDTYPE_PGID, p) {
337 niceval = 20 - task_nice(p);
338 if (niceval > retval)
340 } while_each_task_pid(who, PIDTYPE_PGID, p);
343 user = current->user;
347 if ((who != current->uid) && !(user = find_user(who)))
348 goto out_unlock; /* No processes for this user */
352 niceval = 20 - task_nice(p);
353 if (niceval > retval)
356 while_each_thread(g, p);
357 if (who != current->uid)
358 free_uid(user); /* for find_user() */
362 read_unlock(&tasklist_lock);
368 * emergency_restart - reboot the system
370 * Without shutting down any hardware or taking any locks
371 * reboot the system. This is called when we know we are in
372 * trouble so this is our best effort to reboot. This is
373 * safe to call in interrupt context.
375 void emergency_restart(void)
377 machine_emergency_restart();
379 EXPORT_SYMBOL_GPL(emergency_restart);
381 void kernel_restart_prepare(char *cmd)
383 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
384 system_state = SYSTEM_RESTART;
389 * kernel_restart - reboot the system
390 * @cmd: pointer to buffer containing command to execute for restart
393 * Shutdown everything and perform a clean reboot.
394 * This is not safe to call in interrupt context.
396 void kernel_restart(char *cmd)
398 kernel_restart_prepare(cmd);
400 printk(KERN_EMERG "Restarting system.\n");
402 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
405 machine_restart(cmd);
407 EXPORT_SYMBOL_GPL(kernel_restart);
410 * kernel_kexec - reboot the system
412 * Move into place and start executing a preloaded standalone
413 * executable. If nothing was preloaded return an error.
415 void kernel_kexec(void)
418 struct kimage *image;
419 image = xchg(&kexec_image, NULL);
423 kernel_restart_prepare(NULL);
424 printk(KERN_EMERG "Starting new kernel\n");
426 machine_kexec(image);
429 EXPORT_SYMBOL_GPL(kernel_kexec);
431 void kernel_shutdown_prepare(enum system_states state)
433 notifier_call_chain(&reboot_notifier_list,
434 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
435 system_state = state;
439 * kernel_halt - halt the system
441 * Shutdown everything and perform a clean system halt.
443 void kernel_halt(void)
445 kernel_shutdown_prepare(SYSTEM_HALT);
446 printk(KERN_EMERG "System halted.\n");
450 EXPORT_SYMBOL_GPL(kernel_halt);
453 * kernel_power_off - power_off the system
455 * Shutdown everything and perform a clean system power_off.
457 void kernel_power_off(void)
459 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
460 printk(KERN_EMERG "Power down.\n");
463 EXPORT_SYMBOL_GPL(kernel_power_off);
465 * Reboot system call: for obvious reasons only root may call it,
466 * and even root needs to set up some magic numbers in the registers
467 * so that some mistake won't make this reboot the whole machine.
468 * You can also set the meaning of the ctrl-alt-del-key here.
470 * reboot doesn't sync: do that yourself before calling this.
472 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
476 /* We only trust the superuser with rebooting the system. */
477 if (!capable(CAP_SYS_BOOT))
480 /* For safety, we require "magic" arguments. */
481 if (magic1 != LINUX_REBOOT_MAGIC1 ||
482 (magic2 != LINUX_REBOOT_MAGIC2 &&
483 magic2 != LINUX_REBOOT_MAGIC2A &&
484 magic2 != LINUX_REBOOT_MAGIC2B &&
485 magic2 != LINUX_REBOOT_MAGIC2C))
488 /* Instead of trying to make the power_off code look like
489 * halt when pm_power_off is not set do it the easy way.
491 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
492 cmd = LINUX_REBOOT_CMD_HALT;
496 case LINUX_REBOOT_CMD_RESTART:
497 kernel_restart(NULL);
500 case LINUX_REBOOT_CMD_CAD_ON:
504 case LINUX_REBOOT_CMD_CAD_OFF:
508 case LINUX_REBOOT_CMD_HALT:
514 case LINUX_REBOOT_CMD_POWER_OFF:
520 case LINUX_REBOOT_CMD_RESTART2:
521 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
525 buffer[sizeof(buffer) - 1] = '\0';
527 kernel_restart(buffer);
530 case LINUX_REBOOT_CMD_KEXEC:
535 #ifdef CONFIG_SOFTWARE_SUSPEND
536 case LINUX_REBOOT_CMD_SW_SUSPEND:
538 int ret = software_suspend();
552 static void deferred_cad(void *dummy)
554 kernel_restart(NULL);
558 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
559 * As it's called within an interrupt, it may NOT sync: the only choice
560 * is whether to reboot at once, or just ignore the ctrl-alt-del.
562 void ctrl_alt_del(void)
564 static DECLARE_WORK(cad_work, deferred_cad, NULL);
567 schedule_work(&cad_work);
569 kill_proc(cad_pid, SIGINT, 1);
574 * Unprivileged users may change the real gid to the effective gid
575 * or vice versa. (BSD-style)
577 * If you set the real gid at all, or set the effective gid to a value not
578 * equal to the real gid, then the saved gid is set to the new effective gid.
580 * This makes it possible for a setgid program to completely drop its
581 * privileges, which is often a useful assertion to make when you are doing
582 * a security audit over a program.
584 * The general idea is that a program which uses just setregid() will be
585 * 100% compatible with BSD. A program which uses just setgid() will be
586 * 100% compatible with POSIX with saved IDs.
588 * SMP: There are not races, the GIDs are checked only by filesystem
589 * operations (as far as semantic preservation is concerned).
591 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
593 int old_rgid = current->gid;
594 int old_egid = current->egid;
595 int new_rgid = old_rgid;
596 int new_egid = old_egid;
599 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
603 if (rgid != (gid_t) -1) {
604 if ((old_rgid == rgid) ||
605 (current->egid==rgid) ||
611 if (egid != (gid_t) -1) {
612 if ((old_rgid == egid) ||
613 (current->egid == egid) ||
614 (current->sgid == egid) ||
621 if (new_egid != old_egid)
623 current->mm->dumpable = suid_dumpable;
626 if (rgid != (gid_t) -1 ||
627 (egid != (gid_t) -1 && egid != old_rgid))
628 current->sgid = new_egid;
629 current->fsgid = new_egid;
630 current->egid = new_egid;
631 current->gid = new_rgid;
632 key_fsgid_changed(current);
633 proc_id_connector(current, PROC_EVENT_GID);
638 * setgid() is implemented like SysV w/ SAVED_IDS
640 * SMP: Same implicit races as above.
642 asmlinkage long sys_setgid(gid_t gid)
644 int old_egid = current->egid;
647 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
651 if (capable(CAP_SETGID))
655 current->mm->dumpable = suid_dumpable;
658 current->gid = current->egid = current->sgid = current->fsgid = gid;
660 else if ((gid == current->gid) || (gid == current->sgid))
664 current->mm->dumpable = suid_dumpable;
667 current->egid = current->fsgid = gid;
672 key_fsgid_changed(current);
673 proc_id_connector(current, PROC_EVENT_GID);
677 static int set_user(uid_t new_ruid, int dumpclear)
679 struct user_struct *new_user;
681 new_user = alloc_uid(new_ruid);
685 if (atomic_read(&new_user->processes) >=
686 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
687 new_user != &root_user) {
692 switch_uid(new_user);
696 current->mm->dumpable = suid_dumpable;
699 current->uid = new_ruid;
704 * Unprivileged users may change the real uid to the effective uid
705 * or vice versa. (BSD-style)
707 * If you set the real uid at all, or set the effective uid to a value not
708 * equal to the real uid, then the saved uid is set to the new effective uid.
710 * This makes it possible for a setuid program to completely drop its
711 * privileges, which is often a useful assertion to make when you are doing
712 * a security audit over a program.
714 * The general idea is that a program which uses just setreuid() will be
715 * 100% compatible with BSD. A program which uses just setuid() will be
716 * 100% compatible with POSIX with saved IDs.
718 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
720 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
723 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
727 new_ruid = old_ruid = current->uid;
728 new_euid = old_euid = current->euid;
729 old_suid = current->suid;
731 if (ruid != (uid_t) -1) {
733 if ((old_ruid != ruid) &&
734 (current->euid != ruid) &&
735 !capable(CAP_SETUID))
739 if (euid != (uid_t) -1) {
741 if ((old_ruid != euid) &&
742 (current->euid != euid) &&
743 (current->suid != euid) &&
744 !capable(CAP_SETUID))
748 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
751 if (new_euid != old_euid)
753 current->mm->dumpable = suid_dumpable;
756 current->fsuid = current->euid = new_euid;
757 if (ruid != (uid_t) -1 ||
758 (euid != (uid_t) -1 && euid != old_ruid))
759 current->suid = current->euid;
760 current->fsuid = current->euid;
762 key_fsuid_changed(current);
763 proc_id_connector(current, PROC_EVENT_UID);
765 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
771 * setuid() is implemented like SysV with SAVED_IDS
773 * Note that SAVED_ID's is deficient in that a setuid root program
774 * like sendmail, for example, cannot set its uid to be a normal
775 * user and then switch back, because if you're root, setuid() sets
776 * the saved uid too. If you don't like this, blame the bright people
777 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
778 * will allow a root program to temporarily drop privileges and be able to
779 * regain them by swapping the real and effective uid.
781 asmlinkage long sys_setuid(uid_t uid)
783 int old_euid = current->euid;
784 int old_ruid, old_suid, new_ruid, new_suid;
787 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
791 old_ruid = new_ruid = current->uid;
792 old_suid = current->suid;
795 if (capable(CAP_SETUID)) {
796 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
799 } else if ((uid != current->uid) && (uid != new_suid))
804 current->mm->dumpable = suid_dumpable;
807 current->fsuid = current->euid = uid;
808 current->suid = new_suid;
810 key_fsuid_changed(current);
811 proc_id_connector(current, PROC_EVENT_UID);
813 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
818 * This function implements a generic ability to update ruid, euid,
819 * and suid. This allows you to implement the 4.4 compatible seteuid().
821 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
823 int old_ruid = current->uid;
824 int old_euid = current->euid;
825 int old_suid = current->suid;
828 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
832 if (!capable(CAP_SETUID)) {
833 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
834 (ruid != current->euid) && (ruid != current->suid))
836 if ((euid != (uid_t) -1) && (euid != current->uid) &&
837 (euid != current->euid) && (euid != current->suid))
839 if ((suid != (uid_t) -1) && (suid != current->uid) &&
840 (suid != current->euid) && (suid != current->suid))
843 if (ruid != (uid_t) -1) {
844 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
847 if (euid != (uid_t) -1) {
848 if (euid != current->euid)
850 current->mm->dumpable = suid_dumpable;
853 current->euid = euid;
855 current->fsuid = current->euid;
856 if (suid != (uid_t) -1)
857 current->suid = suid;
859 key_fsuid_changed(current);
860 proc_id_connector(current, PROC_EVENT_UID);
862 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
865 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
869 if (!(retval = put_user(current->uid, ruid)) &&
870 !(retval = put_user(current->euid, euid)))
871 retval = put_user(current->suid, suid);
877 * Same as above, but for rgid, egid, sgid.
879 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
883 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
887 if (!capable(CAP_SETGID)) {
888 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
889 (rgid != current->egid) && (rgid != current->sgid))
891 if ((egid != (gid_t) -1) && (egid != current->gid) &&
892 (egid != current->egid) && (egid != current->sgid))
894 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
895 (sgid != current->egid) && (sgid != current->sgid))
898 if (egid != (gid_t) -1) {
899 if (egid != current->egid)
901 current->mm->dumpable = suid_dumpable;
904 current->egid = egid;
906 current->fsgid = current->egid;
907 if (rgid != (gid_t) -1)
909 if (sgid != (gid_t) -1)
910 current->sgid = sgid;
912 key_fsgid_changed(current);
913 proc_id_connector(current, PROC_EVENT_GID);
917 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
921 if (!(retval = put_user(current->gid, rgid)) &&
922 !(retval = put_user(current->egid, egid)))
923 retval = put_user(current->sgid, sgid);
930 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
931 * is used for "access()" and for the NFS daemon (letting nfsd stay at
932 * whatever uid it wants to). It normally shadows "euid", except when
933 * explicitly set by setfsuid() or for access..
935 asmlinkage long sys_setfsuid(uid_t uid)
939 old_fsuid = current->fsuid;
940 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
943 if (uid == current->uid || uid == current->euid ||
944 uid == current->suid || uid == current->fsuid ||
947 if (uid != old_fsuid)
949 current->mm->dumpable = suid_dumpable;
952 current->fsuid = uid;
955 key_fsuid_changed(current);
956 proc_id_connector(current, PROC_EVENT_UID);
958 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
964 * Samma på svenska..
966 asmlinkage long sys_setfsgid(gid_t gid)
970 old_fsgid = current->fsgid;
971 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
974 if (gid == current->gid || gid == current->egid ||
975 gid == current->sgid || gid == current->fsgid ||
978 if (gid != old_fsgid)
980 current->mm->dumpable = suid_dumpable;
983 current->fsgid = gid;
984 key_fsgid_changed(current);
985 proc_id_connector(current, PROC_EVENT_GID);
990 asmlinkage long sys_times(struct tms __user * tbuf)
993 * In the SMP world we might just be unlucky and have one of
994 * the times increment as we use it. Since the value is an
995 * atomically safe type this is just fine. Conceptually its
996 * as if the syscall took an instant longer to occur.
1000 cputime_t utime, stime, cutime, cstime;
1003 if (thread_group_empty(current)) {
1005 * Single thread case without the use of any locks.
1007 * We may race with release_task if two threads are
1008 * executing. However, release task first adds up the
1009 * counters (__exit_signal) before removing the task
1010 * from the process tasklist (__unhash_process).
1011 * __exit_signal also acquires and releases the
1012 * siglock which results in the proper memory ordering
1013 * so that the list modifications are always visible
1014 * after the counters have been updated.
1016 * If the counters have been updated by the second thread
1017 * but the thread has not yet been removed from the list
1018 * then the other branch will be executing which will
1019 * block on tasklist_lock until the exit handling of the
1020 * other task is finished.
1022 * This also implies that the sighand->siglock cannot
1023 * be held by another processor. So we can also
1024 * skip acquiring that lock.
1026 utime = cputime_add(current->signal->utime, current->utime);
1027 stime = cputime_add(current->signal->utime, current->stime);
1028 cutime = current->signal->cutime;
1029 cstime = current->signal->cstime;
1034 /* Process with multiple threads */
1035 struct task_struct *tsk = current;
1036 struct task_struct *t;
1038 read_lock(&tasklist_lock);
1039 utime = tsk->signal->utime;
1040 stime = tsk->signal->stime;
1043 utime = cputime_add(utime, t->utime);
1044 stime = cputime_add(stime, t->stime);
1049 * While we have tasklist_lock read-locked, no dying thread
1050 * can be updating current->signal->[us]time. Instead,
1051 * we got their counts included in the live thread loop.
1052 * However, another thread can come in right now and
1053 * do a wait call that updates current->signal->c[us]time.
1054 * To make sure we always see that pair updated atomically,
1055 * we take the siglock around fetching them.
1057 spin_lock_irq(&tsk->sighand->siglock);
1058 cutime = tsk->signal->cutime;
1059 cstime = tsk->signal->cstime;
1060 spin_unlock_irq(&tsk->sighand->siglock);
1061 read_unlock(&tasklist_lock);
1063 tmp.tms_utime = cputime_to_clock_t(utime);
1064 tmp.tms_stime = cputime_to_clock_t(stime);
1065 tmp.tms_cutime = cputime_to_clock_t(cutime);
1066 tmp.tms_cstime = cputime_to_clock_t(cstime);
1067 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1070 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1074 * This needs some heavy checking ...
1075 * I just haven't the stomach for it. I also don't fully
1076 * understand sessions/pgrp etc. Let somebody who does explain it.
1078 * OK, I think I have the protection semantics right.... this is really
1079 * only important on a multi-user system anyway, to make sure one user
1080 * can't send a signal to a process owned by another. -TYT, 12/12/91
1082 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1086 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1088 struct task_struct *p;
1089 struct task_struct *group_leader = current->group_leader;
1093 pid = group_leader->pid;
1099 /* From this point forward we keep holding onto the tasklist lock
1100 * so that our parent does not change from under us. -DaveM
1102 write_lock_irq(&tasklist_lock);
1105 p = find_task_by_pid(pid);
1110 if (!thread_group_leader(p))
1113 if (p->real_parent == group_leader) {
1115 if (p->signal->session != group_leader->signal->session)
1122 if (p != group_leader)
1127 if (p->signal->leader)
1131 struct task_struct *p;
1133 do_each_task_pid(pgid, PIDTYPE_PGID, p) {
1134 if (p->signal->session == group_leader->signal->session)
1136 } while_each_task_pid(pgid, PIDTYPE_PGID, p);
1141 err = security_task_setpgid(p, pgid);
1145 if (process_group(p) != pgid) {
1146 detach_pid(p, PIDTYPE_PGID);
1147 p->signal->pgrp = pgid;
1148 attach_pid(p, PIDTYPE_PGID, pgid);
1153 /* All paths lead to here, thus we are safe. -DaveM */
1154 write_unlock_irq(&tasklist_lock);
1158 asmlinkage long sys_getpgid(pid_t pid)
1161 return process_group(current);
1164 struct task_struct *p;
1166 read_lock(&tasklist_lock);
1167 p = find_task_by_pid(pid);
1171 retval = security_task_getpgid(p);
1173 retval = process_group(p);
1175 read_unlock(&tasklist_lock);
1180 #ifdef __ARCH_WANT_SYS_GETPGRP
1182 asmlinkage long sys_getpgrp(void)
1184 /* SMP - assuming writes are word atomic this is fine */
1185 return process_group(current);
1190 asmlinkage long sys_getsid(pid_t pid)
1193 return current->signal->session;
1196 struct task_struct *p;
1198 read_lock(&tasklist_lock);
1199 p = find_task_by_pid(pid);
1203 retval = security_task_getsid(p);
1205 retval = p->signal->session;
1207 read_unlock(&tasklist_lock);
1212 asmlinkage long sys_setsid(void)
1214 struct task_struct *group_leader = current->group_leader;
1218 mutex_lock(&tty_mutex);
1219 write_lock_irq(&tasklist_lock);
1221 pid = find_pid(PIDTYPE_PGID, group_leader->pid);
1225 group_leader->signal->leader = 1;
1226 __set_special_pids(group_leader->pid, group_leader->pid);
1227 group_leader->signal->tty = NULL;
1228 group_leader->signal->tty_old_pgrp = 0;
1229 err = process_group(group_leader);
1231 write_unlock_irq(&tasklist_lock);
1232 mutex_unlock(&tty_mutex);
1237 * Supplementary group IDs
1240 /* init to 2 - one for init_task, one to ensure it is never freed */
1241 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1243 struct group_info *groups_alloc(int gidsetsize)
1245 struct group_info *group_info;
1249 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1250 /* Make sure we always allocate at least one indirect block pointer */
1251 nblocks = nblocks ? : 1;
1252 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1255 group_info->ngroups = gidsetsize;
1256 group_info->nblocks = nblocks;
1257 atomic_set(&group_info->usage, 1);
1259 if (gidsetsize <= NGROUPS_SMALL) {
1260 group_info->blocks[0] = group_info->small_block;
1262 for (i = 0; i < nblocks; i++) {
1264 b = (void *)__get_free_page(GFP_USER);
1266 goto out_undo_partial_alloc;
1267 group_info->blocks[i] = b;
1272 out_undo_partial_alloc:
1274 free_page((unsigned long)group_info->blocks[i]);
1280 EXPORT_SYMBOL(groups_alloc);
1282 void groups_free(struct group_info *group_info)
1284 if (group_info->blocks[0] != group_info->small_block) {
1286 for (i = 0; i < group_info->nblocks; i++)
1287 free_page((unsigned long)group_info->blocks[i]);
1292 EXPORT_SYMBOL(groups_free);
1294 /* export the group_info to a user-space array */
1295 static int groups_to_user(gid_t __user *grouplist,
1296 struct group_info *group_info)
1299 int count = group_info->ngroups;
1301 for (i = 0; i < group_info->nblocks; i++) {
1302 int cp_count = min(NGROUPS_PER_BLOCK, count);
1303 int off = i * NGROUPS_PER_BLOCK;
1304 int len = cp_count * sizeof(*grouplist);
1306 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1314 /* fill a group_info from a user-space array - it must be allocated already */
1315 static int groups_from_user(struct group_info *group_info,
1316 gid_t __user *grouplist)
1319 int count = group_info->ngroups;
1321 for (i = 0; i < group_info->nblocks; i++) {
1322 int cp_count = min(NGROUPS_PER_BLOCK, count);
1323 int off = i * NGROUPS_PER_BLOCK;
1324 int len = cp_count * sizeof(*grouplist);
1326 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1334 /* a simple Shell sort */
1335 static void groups_sort(struct group_info *group_info)
1337 int base, max, stride;
1338 int gidsetsize = group_info->ngroups;
1340 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1345 max = gidsetsize - stride;
1346 for (base = 0; base < max; base++) {
1348 int right = left + stride;
1349 gid_t tmp = GROUP_AT(group_info, right);
1351 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1352 GROUP_AT(group_info, right) =
1353 GROUP_AT(group_info, left);
1357 GROUP_AT(group_info, right) = tmp;
1363 /* a simple bsearch */
1364 int groups_search(struct group_info *group_info, gid_t grp)
1366 unsigned int left, right;
1372 right = group_info->ngroups;
1373 while (left < right) {
1374 unsigned int mid = (left+right)/2;
1375 int cmp = grp - GROUP_AT(group_info, mid);
1386 /* validate and set current->group_info */
1387 int set_current_groups(struct group_info *group_info)
1390 struct group_info *old_info;
1392 retval = security_task_setgroups(group_info);
1396 groups_sort(group_info);
1397 get_group_info(group_info);
1400 old_info = current->group_info;
1401 current->group_info = group_info;
1402 task_unlock(current);
1404 put_group_info(old_info);
1409 EXPORT_SYMBOL(set_current_groups);
1411 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1416 * SMP: Nobody else can change our grouplist. Thus we are
1423 /* no need to grab task_lock here; it cannot change */
1424 i = current->group_info->ngroups;
1426 if (i > gidsetsize) {
1430 if (groups_to_user(grouplist, current->group_info)) {
1440 * SMP: Our groups are copy-on-write. We can set them safely
1441 * without another task interfering.
1444 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1446 struct group_info *group_info;
1449 if (!capable(CAP_SETGID))
1451 if ((unsigned)gidsetsize > NGROUPS_MAX)
1454 group_info = groups_alloc(gidsetsize);
1457 retval = groups_from_user(group_info, grouplist);
1459 put_group_info(group_info);
1463 retval = set_current_groups(group_info);
1464 put_group_info(group_info);
1470 * Check whether we're fsgid/egid or in the supplemental group..
1472 int in_group_p(gid_t grp)
1475 if (grp != current->fsgid) {
1476 retval = groups_search(current->group_info, grp);
1481 EXPORT_SYMBOL(in_group_p);
1483 int in_egroup_p(gid_t grp)
1486 if (grp != current->egid) {
1487 retval = groups_search(current->group_info, grp);
1492 EXPORT_SYMBOL(in_egroup_p);
1494 DECLARE_RWSEM(uts_sem);
1496 EXPORT_SYMBOL(uts_sem);
1498 asmlinkage long sys_newuname(struct new_utsname __user * name)
1502 down_read(&uts_sem);
1503 if (copy_to_user(name,&system_utsname,sizeof *name))
1509 asmlinkage long sys_sethostname(char __user *name, int len)
1512 char tmp[__NEW_UTS_LEN];
1514 if (!capable(CAP_SYS_ADMIN))
1516 if (len < 0 || len > __NEW_UTS_LEN)
1518 down_write(&uts_sem);
1520 if (!copy_from_user(tmp, name, len)) {
1521 memcpy(system_utsname.nodename, tmp, len);
1522 system_utsname.nodename[len] = 0;
1529 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1531 asmlinkage long sys_gethostname(char __user *name, int len)
1537 down_read(&uts_sem);
1538 i = 1 + strlen(system_utsname.nodename);
1542 if (copy_to_user(name, system_utsname.nodename, i))
1551 * Only setdomainname; getdomainname can be implemented by calling
1554 asmlinkage long sys_setdomainname(char __user *name, int len)
1557 char tmp[__NEW_UTS_LEN];
1559 if (!capable(CAP_SYS_ADMIN))
1561 if (len < 0 || len > __NEW_UTS_LEN)
1564 down_write(&uts_sem);
1566 if (!copy_from_user(tmp, name, len)) {
1567 memcpy(system_utsname.domainname, tmp, len);
1568 system_utsname.domainname[len] = 0;
1575 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1577 if (resource >= RLIM_NLIMITS)
1580 struct rlimit value;
1581 task_lock(current->group_leader);
1582 value = current->signal->rlim[resource];
1583 task_unlock(current->group_leader);
1584 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1588 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1591 * Back compatibility for getrlimit. Needed for some apps.
1594 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1597 if (resource >= RLIM_NLIMITS)
1600 task_lock(current->group_leader);
1601 x = current->signal->rlim[resource];
1602 task_unlock(current->group_leader);
1603 if(x.rlim_cur > 0x7FFFFFFF)
1604 x.rlim_cur = 0x7FFFFFFF;
1605 if(x.rlim_max > 0x7FFFFFFF)
1606 x.rlim_max = 0x7FFFFFFF;
1607 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1612 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1614 struct rlimit new_rlim, *old_rlim;
1615 unsigned long it_prof_secs;
1618 if (resource >= RLIM_NLIMITS)
1620 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1622 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1624 old_rlim = current->signal->rlim + resource;
1625 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1626 !capable(CAP_SYS_RESOURCE))
1628 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1631 retval = security_task_setrlimit(resource, &new_rlim);
1635 task_lock(current->group_leader);
1636 *old_rlim = new_rlim;
1637 task_unlock(current->group_leader);
1639 if (resource != RLIMIT_CPU)
1643 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1644 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1645 * very long-standing error, and fixing it now risks breakage of
1646 * applications, so we live with it
1648 if (new_rlim.rlim_cur == RLIM_INFINITY)
1651 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1652 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
1653 unsigned long rlim_cur = new_rlim.rlim_cur;
1656 if (rlim_cur == 0) {
1658 * The caller is asking for an immediate RLIMIT_CPU
1659 * expiry. But we use the zero value to mean "it was
1660 * never set". So let's cheat and make it one second
1665 cputime = secs_to_cputime(rlim_cur);
1666 read_lock(&tasklist_lock);
1667 spin_lock_irq(¤t->sighand->siglock);
1668 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
1669 spin_unlock_irq(¤t->sighand->siglock);
1670 read_unlock(&tasklist_lock);
1677 * It would make sense to put struct rusage in the task_struct,
1678 * except that would make the task_struct be *really big*. After
1679 * task_struct gets moved into malloc'ed memory, it would
1680 * make sense to do this. It will make moving the rest of the information
1681 * a lot simpler! (Which we're not doing right now because we're not
1682 * measuring them yet).
1684 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1685 * races with threads incrementing their own counters. But since word
1686 * reads are atomic, we either get new values or old values and we don't
1687 * care which for the sums. We always take the siglock to protect reading
1688 * the c* fields from p->signal from races with exit.c updating those
1689 * fields when reaping, so a sample either gets all the additions of a
1690 * given child after it's reaped, or none so this sample is before reaping.
1692 * tasklist_lock locking optimisation:
1693 * If we are current and single threaded, we do not need to take the tasklist
1694 * lock or the siglock. No one else can take our signal_struct away,
1695 * no one else can reap the children to update signal->c* counters, and
1696 * no one else can race with the signal-> fields.
1697 * If we do not take the tasklist_lock, the signal-> fields could be read
1698 * out of order while another thread was just exiting. So we place a
1699 * read memory barrier when we avoid the lock. On the writer side,
1700 * write memory barrier is implied in __exit_signal as __exit_signal releases
1701 * the siglock spinlock after updating the signal-> fields.
1703 * We don't really need the siglock when we access the non c* fields
1704 * of the signal_struct (for RUSAGE_SELF) even in multithreaded
1705 * case, since we take the tasklist lock for read and the non c* signal->
1706 * fields are updated only in __exit_signal, which is called with
1707 * tasklist_lock taken for write, hence these two threads cannot execute
1712 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1714 struct task_struct *t;
1715 unsigned long flags;
1716 cputime_t utime, stime;
1719 memset((char *) r, 0, sizeof *r);
1720 utime = stime = cputime_zero;
1722 if (p != current || !thread_group_empty(p))
1726 read_lock(&tasklist_lock);
1727 if (unlikely(!p->signal)) {
1728 read_unlock(&tasklist_lock);
1732 /* See locking comments above */
1737 case RUSAGE_CHILDREN:
1738 spin_lock_irqsave(&p->sighand->siglock, flags);
1739 utime = p->signal->cutime;
1740 stime = p->signal->cstime;
1741 r->ru_nvcsw = p->signal->cnvcsw;
1742 r->ru_nivcsw = p->signal->cnivcsw;
1743 r->ru_minflt = p->signal->cmin_flt;
1744 r->ru_majflt = p->signal->cmaj_flt;
1745 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1747 if (who == RUSAGE_CHILDREN)
1751 utime = cputime_add(utime, p->signal->utime);
1752 stime = cputime_add(stime, p->signal->stime);
1753 r->ru_nvcsw += p->signal->nvcsw;
1754 r->ru_nivcsw += p->signal->nivcsw;
1755 r->ru_minflt += p->signal->min_flt;
1756 r->ru_majflt += p->signal->maj_flt;
1759 utime = cputime_add(utime, t->utime);
1760 stime = cputime_add(stime, t->stime);
1761 r->ru_nvcsw += t->nvcsw;
1762 r->ru_nivcsw += t->nivcsw;
1763 r->ru_minflt += t->min_flt;
1764 r->ru_majflt += t->maj_flt;
1774 read_unlock(&tasklist_lock);
1775 cputime_to_timeval(utime, &r->ru_utime);
1776 cputime_to_timeval(stime, &r->ru_stime);
1779 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1782 k_getrusage(p, who, &r);
1783 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1786 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1788 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1790 return getrusage(current, who, ru);
1793 asmlinkage long sys_umask(int mask)
1795 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1799 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1800 unsigned long arg4, unsigned long arg5)
1804 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1809 case PR_SET_PDEATHSIG:
1810 if (!valid_signal(arg2)) {
1814 current->pdeath_signal = arg2;
1816 case PR_GET_PDEATHSIG:
1817 error = put_user(current->pdeath_signal, (int __user *)arg2);
1819 case PR_GET_DUMPABLE:
1820 error = current->mm->dumpable;
1822 case PR_SET_DUMPABLE:
1823 if (arg2 < 0 || arg2 > 2) {
1827 current->mm->dumpable = arg2;
1830 case PR_SET_UNALIGN:
1831 error = SET_UNALIGN_CTL(current, arg2);
1833 case PR_GET_UNALIGN:
1834 error = GET_UNALIGN_CTL(current, arg2);
1837 error = SET_FPEMU_CTL(current, arg2);
1840 error = GET_FPEMU_CTL(current, arg2);
1843 error = SET_FPEXC_CTL(current, arg2);
1846 error = GET_FPEXC_CTL(current, arg2);
1849 error = PR_TIMING_STATISTICAL;
1852 if (arg2 == PR_TIMING_STATISTICAL)
1858 case PR_GET_KEEPCAPS:
1859 if (current->keep_capabilities)
1862 case PR_SET_KEEPCAPS:
1863 if (arg2 != 0 && arg2 != 1) {
1867 current->keep_capabilities = arg2;
1870 struct task_struct *me = current;
1871 unsigned char ncomm[sizeof(me->comm)];
1873 ncomm[sizeof(me->comm)-1] = 0;
1874 if (strncpy_from_user(ncomm, (char __user *)arg2,
1875 sizeof(me->comm)-1) < 0)
1877 set_task_comm(me, ncomm);
1881 struct task_struct *me = current;
1882 unsigned char tcomm[sizeof(me->comm)];
1884 get_task_comm(tcomm, me);
1885 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))