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/kmod.h>
15 #include <linux/reboot.h>
16 #include <linux/prctl.h>
17 #include <linux/init.h>
18 #include <linux/highuid.h>
20 #include <linux/kernel.h>
21 #include <linux/kexec.h>
22 #include <linux/workqueue.h>
23 #include <linux/capability.h>
24 #include <linux/device.h>
25 #include <linux/key.h>
26 #include <linux/times.h>
27 #include <linux/posix-timers.h>
28 #include <linux/security.h>
29 #include <linux/dcookies.h>
30 #include <linux/suspend.h>
31 #include <linux/tty.h>
32 #include <linux/signal.h>
33 #include <linux/cn_proc.h>
34 #include <linux/vs_cvirt.h>
36 #include <linux/compat.h>
37 #include <linux/syscalls.h>
38 #include <linux/kprobes.h>
40 #include <asm/uaccess.h>
42 #include <asm/unistd.h>
44 #ifndef SET_UNALIGN_CTL
45 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
47 #ifndef GET_UNALIGN_CTL
48 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
51 # define SET_FPEMU_CTL(a,b) (-EINVAL)
54 # define GET_FPEMU_CTL(a,b) (-EINVAL)
57 # define SET_FPEXC_CTL(a,b) (-EINVAL)
60 # define GET_FPEXC_CTL(a,b) (-EINVAL)
64 * this is where the system-wide overflow UID and GID are defined, for
65 * architectures that now have 32-bit UID/GID but didn't in the past
68 int overflowuid = DEFAULT_OVERFLOWUID;
69 int overflowgid = DEFAULT_OVERFLOWGID;
72 EXPORT_SYMBOL(overflowuid);
73 EXPORT_SYMBOL(overflowgid);
77 * the same as above, but for filesystems which can only store a 16-bit
78 * UID and GID. as such, this is needed on all architectures
81 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
82 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
84 EXPORT_SYMBOL(fs_overflowuid);
85 EXPORT_SYMBOL(fs_overflowgid);
88 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
95 * Notifier list for kernel code which wants to be called
96 * at shutdown. This is used to stop any idling DMA operations
100 static struct notifier_block *reboot_notifier_list;
101 static DEFINE_RWLOCK(notifier_lock);
104 * notifier_chain_register - Add notifier to a notifier chain
105 * @list: Pointer to root list pointer
106 * @n: New entry in notifier chain
108 * Adds a notifier to a notifier chain.
110 * Currently always returns zero.
113 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
115 write_lock(¬ifier_lock);
118 if(n->priority > (*list)->priority)
120 list= &((*list)->next);
124 write_unlock(¬ifier_lock);
128 EXPORT_SYMBOL(notifier_chain_register);
131 * notifier_chain_unregister - Remove notifier from a notifier chain
132 * @nl: Pointer to root list pointer
133 * @n: New entry in notifier chain
135 * Removes a notifier from a notifier chain.
137 * Returns zero on success, or %-ENOENT on failure.
140 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
142 write_lock(¬ifier_lock);
148 write_unlock(¬ifier_lock);
153 write_unlock(¬ifier_lock);
157 EXPORT_SYMBOL(notifier_chain_unregister);
160 * notifier_call_chain - Call functions in a notifier chain
161 * @n: Pointer to root pointer of notifier chain
162 * @val: Value passed unmodified to notifier function
163 * @v: Pointer passed unmodified to notifier function
165 * Calls each function in a notifier chain in turn.
167 * If the return value of the notifier can be and'd
168 * with %NOTIFY_STOP_MASK, then notifier_call_chain
169 * will return immediately, with the return value of
170 * the notifier function which halted execution.
171 * Otherwise, the return value is the return value
172 * of the last notifier function called.
175 int __kprobes notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
178 struct notifier_block *nb = *n;
182 ret=nb->notifier_call(nb,val,v);
183 if(ret&NOTIFY_STOP_MASK)
192 EXPORT_SYMBOL(notifier_call_chain);
195 * register_reboot_notifier - Register function to be called at reboot time
196 * @nb: Info about notifier function to be called
198 * Registers a function with the list of functions
199 * to be called at reboot time.
201 * Currently always returns zero, as notifier_chain_register
202 * always returns zero.
205 int register_reboot_notifier(struct notifier_block * nb)
207 return notifier_chain_register(&reboot_notifier_list, nb);
210 EXPORT_SYMBOL(register_reboot_notifier);
213 * unregister_reboot_notifier - Unregister previously registered reboot notifier
214 * @nb: Hook to be unregistered
216 * Unregisters a previously registered reboot
219 * Returns zero on success, or %-ENOENT on failure.
222 int unregister_reboot_notifier(struct notifier_block * nb)
224 return notifier_chain_unregister(&reboot_notifier_list, nb);
227 EXPORT_SYMBOL(unregister_reboot_notifier);
229 #ifndef CONFIG_SECURITY
232 if (vx_check_bit(VXC_CAP_MASK, cap) && !vx_mcaps(1L << cap))
234 if (cap_raised(current->cap_effective, cap)) {
235 current->flags |= PF_SUPERPRIV;
240 EXPORT_SYMBOL(capable);
243 static int set_one_prio(struct task_struct *p, int niceval, int error)
247 if (p->uid != current->euid &&
248 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
252 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
253 if (vx_flags(VXF_IGNEG_NICE, 0))
259 no_nice = security_task_setnice(p, niceval);
266 set_user_nice(p, niceval);
271 asmlinkage long sys_setpriority(int which, int who, int niceval)
273 struct task_struct *g, *p;
274 struct user_struct *user;
277 if (which > 2 || which < 0)
280 /* normalize: avoid signed division (rounding problems) */
287 read_lock(&tasklist_lock);
292 p = find_task_by_pid(who);
294 error = set_one_prio(p, niceval, error);
298 who = process_group(current);
299 do_each_task_pid(who, PIDTYPE_PGID, p) {
300 error = set_one_prio(p, niceval, error);
301 } while_each_task_pid(who, PIDTYPE_PGID, p);
304 user = current->user;
308 if ((who != current->uid) &&
309 !(user = find_user(vx_current_xid(), who)))
310 goto out_unlock; /* No processes for this user */
314 error = set_one_prio(p, niceval, error);
315 while_each_thread(g, p);
316 if (who != current->uid)
317 free_uid(user); /* For find_user() */
321 read_unlock(&tasklist_lock);
327 * Ugh. To avoid negative return values, "getpriority()" will
328 * not return the normal nice-value, but a negated value that
329 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
330 * to stay compatible.
332 asmlinkage long sys_getpriority(int which, int who)
334 struct task_struct *g, *p;
335 struct user_struct *user;
336 long niceval, retval = -ESRCH;
338 if (which > 2 || which < 0)
341 read_lock(&tasklist_lock);
346 p = find_task_by_pid(who);
348 niceval = 20 - task_nice(p);
349 if (niceval > retval)
355 who = process_group(current);
356 do_each_task_pid(who, PIDTYPE_PGID, p) {
357 niceval = 20 - task_nice(p);
358 if (niceval > retval)
360 } while_each_task_pid(who, PIDTYPE_PGID, p);
363 user = current->user;
367 if ((who != current->uid) &&
368 !(user = find_user(vx_current_xid(), who)))
369 goto out_unlock; /* No processes for this user */
373 niceval = 20 - task_nice(p);
374 if (niceval > retval)
377 while_each_thread(g, p);
378 if (who != current->uid)
379 free_uid(user); /* for find_user() */
383 read_unlock(&tasklist_lock);
389 * emergency_restart - reboot the system
391 * Without shutting down any hardware or taking any locks
392 * reboot the system. This is called when we know we are in
393 * trouble so this is our best effort to reboot. This is
394 * safe to call in interrupt context.
396 void emergency_restart(void)
398 machine_emergency_restart();
400 EXPORT_SYMBOL_GPL(emergency_restart);
402 void kernel_restart_prepare(char *cmd)
404 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
405 system_state = SYSTEM_RESTART;
410 * kernel_restart - reboot the system
411 * @cmd: pointer to buffer containing command to execute for restart
414 * Shutdown everything and perform a clean reboot.
415 * This is not safe to call in interrupt context.
417 void kernel_restart(char *cmd)
419 kernel_restart_prepare(cmd);
421 printk(KERN_EMERG "Restarting system.\n");
423 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
426 machine_restart(cmd);
428 EXPORT_SYMBOL_GPL(kernel_restart);
431 * kernel_kexec - reboot the system
433 * Move into place and start executing a preloaded standalone
434 * executable. If nothing was preloaded return an error.
436 void kernel_kexec(void)
439 struct kimage *image;
440 image = xchg(&kexec_image, NULL);
444 kernel_restart_prepare(NULL);
445 printk(KERN_EMERG "Starting new kernel\n");
447 machine_kexec(image);
450 EXPORT_SYMBOL_GPL(kernel_kexec);
452 void kernel_shutdown_prepare(enum system_states state)
454 notifier_call_chain(&reboot_notifier_list,
455 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
456 system_state = state;
460 * kernel_halt - halt the system
462 * Shutdown everything and perform a clean system halt.
464 void kernel_halt(void)
466 kernel_shutdown_prepare(SYSTEM_HALT);
467 printk(KERN_EMERG "System halted.\n");
471 EXPORT_SYMBOL_GPL(kernel_halt);
474 * kernel_power_off - power_off the system
476 * Shutdown everything and perform a clean system power_off.
478 void kernel_power_off(void)
480 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
481 printk(KERN_EMERG "Power down.\n");
484 EXPORT_SYMBOL_GPL(kernel_power_off);
486 long vs_reboot(unsigned int, void __user *);
489 * Reboot system call: for obvious reasons only root may call it,
490 * and even root needs to set up some magic numbers in the registers
491 * so that some mistake won't make this reboot the whole machine.
492 * You can also set the meaning of the ctrl-alt-del-key here.
494 * reboot doesn't sync: do that yourself before calling this.
496 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
500 /* We only trust the superuser with rebooting the system. */
501 if (!capable(CAP_SYS_BOOT))
504 /* For safety, we require "magic" arguments. */
505 if (magic1 != LINUX_REBOOT_MAGIC1 ||
506 (magic2 != LINUX_REBOOT_MAGIC2 &&
507 magic2 != LINUX_REBOOT_MAGIC2A &&
508 magic2 != LINUX_REBOOT_MAGIC2B &&
509 magic2 != LINUX_REBOOT_MAGIC2C))
512 /* Instead of trying to make the power_off code look like
513 * halt when pm_power_off is not set do it the easy way.
515 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
516 cmd = LINUX_REBOOT_CMD_HALT;
518 if (!vx_check(0, VX_ADMIN|VX_WATCH))
519 return vs_reboot(cmd, arg);
523 case LINUX_REBOOT_CMD_RESTART:
524 kernel_restart(NULL);
527 case LINUX_REBOOT_CMD_CAD_ON:
531 case LINUX_REBOOT_CMD_CAD_OFF:
535 case LINUX_REBOOT_CMD_HALT:
541 case LINUX_REBOOT_CMD_POWER_OFF:
547 case LINUX_REBOOT_CMD_RESTART2:
548 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
552 buffer[sizeof(buffer) - 1] = '\0';
554 kernel_restart(buffer);
557 case LINUX_REBOOT_CMD_KEXEC:
562 #ifdef CONFIG_SOFTWARE_SUSPEND
563 case LINUX_REBOOT_CMD_SW_SUSPEND:
565 int ret = software_suspend();
579 static void deferred_cad(void *dummy)
581 kernel_restart(NULL);
585 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
586 * As it's called within an interrupt, it may NOT sync: the only choice
587 * is whether to reboot at once, or just ignore the ctrl-alt-del.
589 void ctrl_alt_del(void)
591 static DECLARE_WORK(cad_work, deferred_cad, NULL);
594 schedule_work(&cad_work);
596 kill_proc(cad_pid, SIGINT, 1);
601 * Unprivileged users may change the real gid to the effective gid
602 * or vice versa. (BSD-style)
604 * If you set the real gid at all, or set the effective gid to a value not
605 * equal to the real gid, then the saved gid is set to the new effective gid.
607 * This makes it possible for a setgid program to completely drop its
608 * privileges, which is often a useful assertion to make when you are doing
609 * a security audit over a program.
611 * The general idea is that a program which uses just setregid() will be
612 * 100% compatible with BSD. A program which uses just setgid() will be
613 * 100% compatible with POSIX with saved IDs.
615 * SMP: There are not races, the GIDs are checked only by filesystem
616 * operations (as far as semantic preservation is concerned).
618 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
620 int old_rgid = current->gid;
621 int old_egid = current->egid;
622 int new_rgid = old_rgid;
623 int new_egid = old_egid;
626 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
630 if (rgid != (gid_t) -1) {
631 if ((old_rgid == rgid) ||
632 (current->egid==rgid) ||
638 if (egid != (gid_t) -1) {
639 if ((old_rgid == egid) ||
640 (current->egid == egid) ||
641 (current->sgid == egid) ||
648 if (new_egid != old_egid)
650 current->mm->dumpable = suid_dumpable;
653 if (rgid != (gid_t) -1 ||
654 (egid != (gid_t) -1 && egid != old_rgid))
655 current->sgid = new_egid;
656 current->fsgid = new_egid;
657 current->egid = new_egid;
658 current->gid = new_rgid;
659 key_fsgid_changed(current);
660 proc_id_connector(current, PROC_EVENT_GID);
665 * setgid() is implemented like SysV w/ SAVED_IDS
667 * SMP: Same implicit races as above.
669 asmlinkage long sys_setgid(gid_t gid)
671 int old_egid = current->egid;
674 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
678 if (capable(CAP_SETGID))
682 current->mm->dumpable = suid_dumpable;
685 current->gid = current->egid = current->sgid = current->fsgid = gid;
687 else if ((gid == current->gid) || (gid == current->sgid))
691 current->mm->dumpable = suid_dumpable;
694 current->egid = current->fsgid = gid;
699 key_fsgid_changed(current);
700 proc_id_connector(current, PROC_EVENT_GID);
704 static int set_user(uid_t new_ruid, int dumpclear)
706 struct user_struct *new_user;
708 new_user = alloc_uid(vx_current_xid(), new_ruid);
712 if (atomic_read(&new_user->processes) >=
713 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
714 new_user != &root_user) {
719 switch_uid(new_user);
723 current->mm->dumpable = suid_dumpable;
726 current->uid = new_ruid;
731 * Unprivileged users may change the real uid to the effective uid
732 * or vice versa. (BSD-style)
734 * If you set the real uid at all, or set the effective uid to a value not
735 * equal to the real uid, then the saved uid is set to the new effective uid.
737 * This makes it possible for a setuid program to completely drop its
738 * privileges, which is often a useful assertion to make when you are doing
739 * a security audit over a program.
741 * The general idea is that a program which uses just setreuid() will be
742 * 100% compatible with BSD. A program which uses just setuid() will be
743 * 100% compatible with POSIX with saved IDs.
745 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
747 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
750 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
754 new_ruid = old_ruid = current->uid;
755 new_euid = old_euid = current->euid;
756 old_suid = current->suid;
758 if (ruid != (uid_t) -1) {
760 if ((old_ruid != ruid) &&
761 (current->euid != ruid) &&
762 !capable(CAP_SETUID))
766 if (euid != (uid_t) -1) {
768 if ((old_ruid != euid) &&
769 (current->euid != euid) &&
770 (current->suid != euid) &&
771 !capable(CAP_SETUID))
775 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
778 if (new_euid != old_euid)
780 current->mm->dumpable = suid_dumpable;
783 current->fsuid = current->euid = new_euid;
784 if (ruid != (uid_t) -1 ||
785 (euid != (uid_t) -1 && euid != old_ruid))
786 current->suid = current->euid;
787 current->fsuid = current->euid;
789 key_fsuid_changed(current);
790 proc_id_connector(current, PROC_EVENT_UID);
792 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
798 * setuid() is implemented like SysV with SAVED_IDS
800 * Note that SAVED_ID's is deficient in that a setuid root program
801 * like sendmail, for example, cannot set its uid to be a normal
802 * user and then switch back, because if you're root, setuid() sets
803 * the saved uid too. If you don't like this, blame the bright people
804 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
805 * will allow a root program to temporarily drop privileges and be able to
806 * regain them by swapping the real and effective uid.
808 asmlinkage long sys_setuid(uid_t uid)
810 int old_euid = current->euid;
811 int old_ruid, old_suid, new_ruid, new_suid;
814 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
818 old_ruid = new_ruid = current->uid;
819 old_suid = current->suid;
822 if (capable(CAP_SETUID)) {
823 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
826 } else if ((uid != current->uid) && (uid != new_suid))
831 current->mm->dumpable = suid_dumpable;
834 current->fsuid = current->euid = uid;
835 current->suid = new_suid;
837 key_fsuid_changed(current);
838 proc_id_connector(current, PROC_EVENT_UID);
840 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
845 * This function implements a generic ability to update ruid, euid,
846 * and suid. This allows you to implement the 4.4 compatible seteuid().
848 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
850 int old_ruid = current->uid;
851 int old_euid = current->euid;
852 int old_suid = current->suid;
855 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
859 if (!capable(CAP_SETUID)) {
860 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
861 (ruid != current->euid) && (ruid != current->suid))
863 if ((euid != (uid_t) -1) && (euid != current->uid) &&
864 (euid != current->euid) && (euid != current->suid))
866 if ((suid != (uid_t) -1) && (suid != current->uid) &&
867 (suid != current->euid) && (suid != current->suid))
870 if (ruid != (uid_t) -1) {
871 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
874 if (euid != (uid_t) -1) {
875 if (euid != current->euid)
877 current->mm->dumpable = suid_dumpable;
880 current->euid = euid;
882 current->fsuid = current->euid;
883 if (suid != (uid_t) -1)
884 current->suid = suid;
886 key_fsuid_changed(current);
887 proc_id_connector(current, PROC_EVENT_UID);
889 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
892 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
896 if (!(retval = put_user(current->uid, ruid)) &&
897 !(retval = put_user(current->euid, euid)))
898 retval = put_user(current->suid, suid);
904 * Same as above, but for rgid, egid, sgid.
906 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
910 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
914 if (!capable(CAP_SETGID)) {
915 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
916 (rgid != current->egid) && (rgid != current->sgid))
918 if ((egid != (gid_t) -1) && (egid != current->gid) &&
919 (egid != current->egid) && (egid != current->sgid))
921 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
922 (sgid != current->egid) && (sgid != current->sgid))
925 if (egid != (gid_t) -1) {
926 if (egid != current->egid)
928 current->mm->dumpable = suid_dumpable;
931 current->egid = egid;
933 current->fsgid = current->egid;
934 if (rgid != (gid_t) -1)
936 if (sgid != (gid_t) -1)
937 current->sgid = sgid;
939 key_fsgid_changed(current);
940 proc_id_connector(current, PROC_EVENT_GID);
944 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
948 if (!(retval = put_user(current->gid, rgid)) &&
949 !(retval = put_user(current->egid, egid)))
950 retval = put_user(current->sgid, sgid);
957 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
958 * is used for "access()" and for the NFS daemon (letting nfsd stay at
959 * whatever uid it wants to). It normally shadows "euid", except when
960 * explicitly set by setfsuid() or for access..
962 asmlinkage long sys_setfsuid(uid_t uid)
966 old_fsuid = current->fsuid;
967 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
970 if (uid == current->uid || uid == current->euid ||
971 uid == current->suid || uid == current->fsuid ||
974 if (uid != old_fsuid)
976 current->mm->dumpable = suid_dumpable;
979 current->fsuid = uid;
982 key_fsuid_changed(current);
983 proc_id_connector(current, PROC_EVENT_UID);
985 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
991 * Samma på svenska..
993 asmlinkage long sys_setfsgid(gid_t gid)
997 old_fsgid = current->fsgid;
998 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1001 if (gid == current->gid || gid == current->egid ||
1002 gid == current->sgid || gid == current->fsgid ||
1003 capable(CAP_SETGID))
1005 if (gid != old_fsgid)
1007 current->mm->dumpable = suid_dumpable;
1010 current->fsgid = gid;
1011 key_fsgid_changed(current);
1012 proc_id_connector(current, PROC_EVENT_GID);
1017 asmlinkage long sys_times(struct tms __user * tbuf)
1020 * In the SMP world we might just be unlucky and have one of
1021 * the times increment as we use it. Since the value is an
1022 * atomically safe type this is just fine. Conceptually its
1023 * as if the syscall took an instant longer to occur.
1027 cputime_t utime, stime, cutime, cstime;
1030 if (thread_group_empty(current)) {
1032 * Single thread case without the use of any locks.
1034 * We may race with release_task if two threads are
1035 * executing. However, release task first adds up the
1036 * counters (__exit_signal) before removing the task
1037 * from the process tasklist (__unhash_process).
1038 * __exit_signal also acquires and releases the
1039 * siglock which results in the proper memory ordering
1040 * so that the list modifications are always visible
1041 * after the counters have been updated.
1043 * If the counters have been updated by the second thread
1044 * but the thread has not yet been removed from the list
1045 * then the other branch will be executing which will
1046 * block on tasklist_lock until the exit handling of the
1047 * other task is finished.
1049 * This also implies that the sighand->siglock cannot
1050 * be held by another processor. So we can also
1051 * skip acquiring that lock.
1053 utime = cputime_add(current->signal->utime, current->utime);
1054 stime = cputime_add(current->signal->utime, current->stime);
1055 cutime = current->signal->cutime;
1056 cstime = current->signal->cstime;
1061 /* Process with multiple threads */
1062 struct task_struct *tsk = current;
1063 struct task_struct *t;
1065 read_lock(&tasklist_lock);
1066 utime = tsk->signal->utime;
1067 stime = tsk->signal->stime;
1070 utime = cputime_add(utime, t->utime);
1071 stime = cputime_add(stime, t->stime);
1076 * While we have tasklist_lock read-locked, no dying thread
1077 * can be updating current->signal->[us]time. Instead,
1078 * we got their counts included in the live thread loop.
1079 * However, another thread can come in right now and
1080 * do a wait call that updates current->signal->c[us]time.
1081 * To make sure we always see that pair updated atomically,
1082 * we take the siglock around fetching them.
1084 spin_lock_irq(&tsk->sighand->siglock);
1085 cutime = tsk->signal->cutime;
1086 cstime = tsk->signal->cstime;
1087 spin_unlock_irq(&tsk->sighand->siglock);
1088 read_unlock(&tasklist_lock);
1090 tmp.tms_utime = cputime_to_clock_t(utime);
1091 tmp.tms_stime = cputime_to_clock_t(stime);
1092 tmp.tms_cutime = cputime_to_clock_t(cutime);
1093 tmp.tms_cstime = cputime_to_clock_t(cstime);
1094 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1097 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1101 * This needs some heavy checking ...
1102 * I just haven't the stomach for it. I also don't fully
1103 * understand sessions/pgrp etc. Let somebody who does explain it.
1105 * OK, I think I have the protection semantics right.... this is really
1106 * only important on a multi-user system anyway, to make sure one user
1107 * can't send a signal to a process owned by another. -TYT, 12/12/91
1109 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1113 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1115 struct task_struct *p;
1116 struct task_struct *group_leader = current->group_leader;
1121 pid = vx_map_pid(group_leader->pid);
1127 rpgid = vx_rmap_pid(pgid);
1129 /* From this point forward we keep holding onto the tasklist lock
1130 * so that our parent does not change from under us. -DaveM
1132 write_lock_irq(&tasklist_lock);
1135 p = find_task_by_pid(pid);
1140 if (!thread_group_leader(p))
1143 if (p->real_parent == group_leader) {
1145 if (p->signal->session != group_leader->signal->session)
1152 if (p != group_leader)
1157 if (p->signal->leader)
1161 struct task_struct *p;
1163 do_each_task_pid(rpgid, PIDTYPE_PGID, p) {
1164 if (p->signal->session == group_leader->signal->session)
1166 } while_each_task_pid(rpgid, PIDTYPE_PGID, p);
1171 err = security_task_setpgid(p, rpgid);
1175 if (process_group(p) != rpgid) {
1176 detach_pid(p, PIDTYPE_PGID);
1177 p->signal->pgrp = rpgid;
1178 attach_pid(p, PIDTYPE_PGID, rpgid);
1183 /* All paths lead to here, thus we are safe. -DaveM */
1184 write_unlock_irq(&tasklist_lock);
1188 asmlinkage long sys_getpgid(pid_t pid)
1191 return vx_rmap_pid(process_group(current));
1194 struct task_struct *p;
1196 read_lock(&tasklist_lock);
1197 p = find_task_by_pid(pid);
1201 retval = security_task_getpgid(p);
1203 retval = vx_rmap_pid(process_group(p));
1205 read_unlock(&tasklist_lock);
1210 #ifdef __ARCH_WANT_SYS_GETPGRP
1212 asmlinkage long sys_getpgrp(void)
1214 /* SMP - assuming writes are word atomic this is fine */
1215 return process_group(current);
1220 asmlinkage long sys_getsid(pid_t pid)
1223 return current->signal->session;
1226 struct task_struct *p;
1228 read_lock(&tasklist_lock);
1229 p = find_task_by_pid(pid);
1233 retval = security_task_getsid(p);
1235 retval = p->signal->session;
1237 read_unlock(&tasklist_lock);
1242 asmlinkage long sys_setsid(void)
1244 struct task_struct *group_leader = current->group_leader;
1249 write_lock_irq(&tasklist_lock);
1251 pid = find_pid(PIDTYPE_PGID, group_leader->pid);
1255 group_leader->signal->leader = 1;
1256 __set_special_pids(group_leader->pid, group_leader->pid);
1257 group_leader->signal->tty = NULL;
1258 group_leader->signal->tty_old_pgrp = 0;
1259 err = process_group(group_leader);
1261 write_unlock_irq(&tasklist_lock);
1267 * Supplementary group IDs
1270 /* init to 2 - one for init_task, one to ensure it is never freed */
1271 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1273 struct group_info *groups_alloc(int gidsetsize)
1275 struct group_info *group_info;
1279 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1280 /* Make sure we always allocate at least one indirect block pointer */
1281 nblocks = nblocks ? : 1;
1282 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1285 group_info->ngroups = gidsetsize;
1286 group_info->nblocks = nblocks;
1287 atomic_set(&group_info->usage, 1);
1289 if (gidsetsize <= NGROUPS_SMALL) {
1290 group_info->blocks[0] = group_info->small_block;
1292 for (i = 0; i < nblocks; i++) {
1294 b = (void *)__get_free_page(GFP_USER);
1296 goto out_undo_partial_alloc;
1297 group_info->blocks[i] = b;
1302 out_undo_partial_alloc:
1304 free_page((unsigned long)group_info->blocks[i]);
1310 EXPORT_SYMBOL(groups_alloc);
1312 void groups_free(struct group_info *group_info)
1314 if (group_info->blocks[0] != group_info->small_block) {
1316 for (i = 0; i < group_info->nblocks; i++)
1317 free_page((unsigned long)group_info->blocks[i]);
1322 EXPORT_SYMBOL(groups_free);
1324 /* export the group_info to a user-space array */
1325 static int groups_to_user(gid_t __user *grouplist,
1326 struct group_info *group_info)
1329 int count = group_info->ngroups;
1331 for (i = 0; i < group_info->nblocks; i++) {
1332 int cp_count = min(NGROUPS_PER_BLOCK, count);
1333 int off = i * NGROUPS_PER_BLOCK;
1334 int len = cp_count * sizeof(*grouplist);
1336 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1344 /* fill a group_info from a user-space array - it must be allocated already */
1345 static int groups_from_user(struct group_info *group_info,
1346 gid_t __user *grouplist)
1349 int count = group_info->ngroups;
1351 for (i = 0; i < group_info->nblocks; i++) {
1352 int cp_count = min(NGROUPS_PER_BLOCK, count);
1353 int off = i * NGROUPS_PER_BLOCK;
1354 int len = cp_count * sizeof(*grouplist);
1356 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1364 /* a simple Shell sort */
1365 static void groups_sort(struct group_info *group_info)
1367 int base, max, stride;
1368 int gidsetsize = group_info->ngroups;
1370 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1375 max = gidsetsize - stride;
1376 for (base = 0; base < max; base++) {
1378 int right = left + stride;
1379 gid_t tmp = GROUP_AT(group_info, right);
1381 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1382 GROUP_AT(group_info, right) =
1383 GROUP_AT(group_info, left);
1387 GROUP_AT(group_info, right) = tmp;
1393 /* a simple bsearch */
1394 int groups_search(struct group_info *group_info, gid_t grp)
1402 right = group_info->ngroups;
1403 while (left < right) {
1404 int mid = (left+right)/2;
1405 int cmp = grp - GROUP_AT(group_info, mid);
1416 /* validate and set current->group_info */
1417 int set_current_groups(struct group_info *group_info)
1420 struct group_info *old_info;
1422 retval = security_task_setgroups(group_info);
1426 groups_sort(group_info);
1427 get_group_info(group_info);
1430 old_info = current->group_info;
1431 current->group_info = group_info;
1432 task_unlock(current);
1434 put_group_info(old_info);
1439 EXPORT_SYMBOL(set_current_groups);
1441 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1446 * SMP: Nobody else can change our grouplist. Thus we are
1453 /* no need to grab task_lock here; it cannot change */
1454 get_group_info(current->group_info);
1455 i = current->group_info->ngroups;
1457 if (i > gidsetsize) {
1461 if (groups_to_user(grouplist, current->group_info)) {
1467 put_group_info(current->group_info);
1472 * SMP: Our groups are copy-on-write. We can set them safely
1473 * without another task interfering.
1476 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1478 struct group_info *group_info;
1481 if (!capable(CAP_SETGID))
1483 if ((unsigned)gidsetsize > NGROUPS_MAX)
1486 group_info = groups_alloc(gidsetsize);
1489 retval = groups_from_user(group_info, grouplist);
1491 put_group_info(group_info);
1495 retval = set_current_groups(group_info);
1496 put_group_info(group_info);
1502 * Check whether we're fsgid/egid or in the supplemental group..
1504 int in_group_p(gid_t grp)
1507 if (grp != current->fsgid) {
1508 get_group_info(current->group_info);
1509 retval = groups_search(current->group_info, grp);
1510 put_group_info(current->group_info);
1515 EXPORT_SYMBOL(in_group_p);
1517 int in_egroup_p(gid_t grp)
1520 if (grp != current->egid) {
1521 get_group_info(current->group_info);
1522 retval = groups_search(current->group_info, grp);
1523 put_group_info(current->group_info);
1528 EXPORT_SYMBOL(in_egroup_p);
1530 DECLARE_RWSEM(uts_sem);
1532 EXPORT_SYMBOL(uts_sem);
1534 asmlinkage long sys_newuname(struct new_utsname __user * name)
1538 down_read(&uts_sem);
1539 if (copy_to_user(name, vx_new_utsname(), sizeof *name))
1545 asmlinkage long sys_sethostname(char __user *name, int len)
1548 char tmp[__NEW_UTS_LEN];
1550 if (!vx_capable(CAP_SYS_ADMIN, VXC_SET_UTSNAME))
1552 if (len < 0 || len > __NEW_UTS_LEN)
1554 down_write(&uts_sem);
1556 if (!copy_from_user(tmp, name, len)) {
1557 char *ptr = vx_new_uts(nodename);
1559 memcpy(ptr, tmp, len);
1567 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1569 asmlinkage long sys_gethostname(char __user *name, int len)
1576 down_read(&uts_sem);
1577 ptr = vx_new_uts(nodename);
1578 i = 1 + strlen(ptr);
1582 if (copy_to_user(name, ptr, i))
1591 * Only setdomainname; getdomainname can be implemented by calling
1594 asmlinkage long sys_setdomainname(char __user *name, int len)
1597 char tmp[__NEW_UTS_LEN];
1599 if (!vx_capable(CAP_SYS_ADMIN, VXC_SET_UTSNAME))
1601 if (len < 0 || len > __NEW_UTS_LEN)
1604 down_write(&uts_sem);
1606 if (!copy_from_user(tmp, name, len)) {
1607 char *ptr = vx_new_uts(domainname);
1609 memcpy(ptr, tmp, len);
1617 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1619 if (resource >= RLIM_NLIMITS)
1622 struct rlimit value;
1623 task_lock(current->group_leader);
1624 value = current->signal->rlim[resource];
1625 task_unlock(current->group_leader);
1626 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1630 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1633 * Back compatibility for getrlimit. Needed for some apps.
1636 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1639 if (resource >= RLIM_NLIMITS)
1642 task_lock(current->group_leader);
1643 x = current->signal->rlim[resource];
1644 task_unlock(current->group_leader);
1645 if(x.rlim_cur > 0x7FFFFFFF)
1646 x.rlim_cur = 0x7FFFFFFF;
1647 if(x.rlim_max > 0x7FFFFFFF)
1648 x.rlim_max = 0x7FFFFFFF;
1649 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1654 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1656 struct rlimit new_rlim, *old_rlim;
1659 if (resource >= RLIM_NLIMITS)
1661 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1663 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1665 old_rlim = current->signal->rlim + resource;
1666 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1667 !vx_capable(CAP_SYS_RESOURCE, VXC_SET_RLIMIT))
1669 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1672 retval = security_task_setrlimit(resource, &new_rlim);
1676 task_lock(current->group_leader);
1677 *old_rlim = new_rlim;
1678 task_unlock(current->group_leader);
1680 if (resource == RLIMIT_CPU && new_rlim.rlim_cur != RLIM_INFINITY &&
1681 (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
1682 new_rlim.rlim_cur <= cputime_to_secs(
1683 current->signal->it_prof_expires))) {
1684 unsigned long rlim_cur = new_rlim.rlim_cur;
1687 if (rlim_cur == 0) {
1689 * The caller is asking for an immediate RLIMIT_CPU
1690 * expiry. But we use the zero value to mean "it was
1691 * never set". So let's cheat and make it one second
1696 cputime = secs_to_cputime(rlim_cur);
1697 read_lock(&tasklist_lock);
1698 spin_lock_irq(¤t->sighand->siglock);
1699 set_process_cpu_timer(current, CPUCLOCK_PROF,
1701 spin_unlock_irq(¤t->sighand->siglock);
1702 read_unlock(&tasklist_lock);
1709 * It would make sense to put struct rusage in the task_struct,
1710 * except that would make the task_struct be *really big*. After
1711 * task_struct gets moved into malloc'ed memory, it would
1712 * make sense to do this. It will make moving the rest of the information
1713 * a lot simpler! (Which we're not doing right now because we're not
1714 * measuring them yet).
1716 * This expects to be called with tasklist_lock read-locked or better,
1717 * and the siglock not locked. It may momentarily take the siglock.
1719 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1720 * races with threads incrementing their own counters. But since word
1721 * reads are atomic, we either get new values or old values and we don't
1722 * care which for the sums. We always take the siglock to protect reading
1723 * the c* fields from p->signal from races with exit.c updating those
1724 * fields when reaping, so a sample either gets all the additions of a
1725 * given child after it's reaped, or none so this sample is before reaping.
1728 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1730 struct task_struct *t;
1731 unsigned long flags;
1732 cputime_t utime, stime;
1734 memset((char *) r, 0, sizeof *r);
1736 if (unlikely(!p->signal))
1739 utime = stime = cputime_zero;
1743 case RUSAGE_CHILDREN:
1744 spin_lock_irqsave(&p->sighand->siglock, flags);
1745 utime = p->signal->cutime;
1746 stime = p->signal->cstime;
1747 r->ru_nvcsw = p->signal->cnvcsw;
1748 r->ru_nivcsw = p->signal->cnivcsw;
1749 r->ru_minflt = p->signal->cmin_flt;
1750 r->ru_majflt = p->signal->cmaj_flt;
1751 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1753 if (who == RUSAGE_CHILDREN)
1757 utime = cputime_add(utime, p->signal->utime);
1758 stime = cputime_add(stime, p->signal->stime);
1759 r->ru_nvcsw += p->signal->nvcsw;
1760 r->ru_nivcsw += p->signal->nivcsw;
1761 r->ru_minflt += p->signal->min_flt;
1762 r->ru_majflt += p->signal->maj_flt;
1765 utime = cputime_add(utime, t->utime);
1766 stime = cputime_add(stime, t->stime);
1767 r->ru_nvcsw += t->nvcsw;
1768 r->ru_nivcsw += t->nivcsw;
1769 r->ru_minflt += t->min_flt;
1770 r->ru_majflt += t->maj_flt;
1779 cputime_to_timeval(utime, &r->ru_utime);
1780 cputime_to_timeval(stime, &r->ru_stime);
1783 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1786 read_lock(&tasklist_lock);
1787 k_getrusage(p, who, &r);
1788 read_unlock(&tasklist_lock);
1789 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1792 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1794 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1796 return getrusage(current, who, ru);
1799 asmlinkage long sys_umask(int mask)
1801 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1805 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1806 unsigned long arg4, unsigned long arg5)
1810 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1815 case PR_SET_PDEATHSIG:
1816 if (!valid_signal(arg2)) {
1820 current->pdeath_signal = arg2;
1822 case PR_GET_PDEATHSIG:
1823 error = put_user(current->pdeath_signal, (int __user *)arg2);
1825 case PR_GET_DUMPABLE:
1826 error = current->mm->dumpable;
1828 case PR_SET_DUMPABLE:
1829 if (arg2 < 0 || arg2 > 1) {
1833 current->mm->dumpable = arg2;
1836 case PR_SET_UNALIGN:
1837 error = SET_UNALIGN_CTL(current, arg2);
1839 case PR_GET_UNALIGN:
1840 error = GET_UNALIGN_CTL(current, arg2);
1843 error = SET_FPEMU_CTL(current, arg2);
1846 error = GET_FPEMU_CTL(current, arg2);
1849 error = SET_FPEXC_CTL(current, arg2);
1852 error = GET_FPEXC_CTL(current, arg2);
1855 error = PR_TIMING_STATISTICAL;
1858 if (arg2 == PR_TIMING_STATISTICAL)
1864 case PR_GET_KEEPCAPS:
1865 if (current->keep_capabilities)
1868 case PR_SET_KEEPCAPS:
1869 if (arg2 != 0 && arg2 != 1) {
1873 current->keep_capabilities = arg2;
1876 struct task_struct *me = current;
1877 unsigned char ncomm[sizeof(me->comm)];
1879 ncomm[sizeof(me->comm)-1] = 0;
1880 if (strncpy_from_user(ncomm, (char __user *)arg2,
1881 sizeof(me->comm)-1) < 0)
1883 set_task_comm(me, ncomm);
1887 struct task_struct *me = current;
1888 unsigned char tcomm[sizeof(me->comm)];
1890 get_task_comm(tcomm, me);
1891 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))