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/workqueue.h>
21 #include <linux/device.h>
22 #include <linux/key.h>
23 #include <linux/times.h>
24 #include <linux/security.h>
25 #include <linux/dcookies.h>
26 #include <linux/suspend.h>
27 #include <linux/tty.h>
29 #include <linux/compat.h>
30 #include <linux/syscalls.h>
31 #include <linux/vs_cvirt.h>
33 #include <asm/uaccess.h>
35 #include <asm/unistd.h>
37 #ifndef SET_UNALIGN_CTL
38 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
40 #ifndef GET_UNALIGN_CTL
41 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
44 # define SET_FPEMU_CTL(a,b) (-EINVAL)
47 # define GET_FPEMU_CTL(a,b) (-EINVAL)
50 # define SET_FPEXC_CTL(a,b) (-EINVAL)
53 # define GET_FPEXC_CTL(a,b) (-EINVAL)
57 * this is where the system-wide overflow UID and GID are defined, for
58 * architectures that now have 32-bit UID/GID but didn't in the past
61 int overflowuid = DEFAULT_OVERFLOWUID;
62 int overflowgid = DEFAULT_OVERFLOWGID;
65 EXPORT_SYMBOL(overflowuid);
66 EXPORT_SYMBOL(overflowgid);
70 * the same as above, but for filesystems which can only store a 16-bit
71 * UID and GID. as such, this is needed on all architectures
74 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
75 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
77 EXPORT_SYMBOL(fs_overflowuid);
78 EXPORT_SYMBOL(fs_overflowgid);
81 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
88 * Notifier list for kernel code which wants to be called
89 * at shutdown. This is used to stop any idling DMA operations
93 static struct notifier_block *reboot_notifier_list;
94 DEFINE_RWLOCK(notifier_lock);
97 * notifier_chain_register - Add notifier to a notifier chain
98 * @list: Pointer to root list pointer
99 * @n: New entry in notifier chain
101 * Adds a notifier to a notifier chain.
103 * Currently always returns zero.
106 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
108 write_lock(¬ifier_lock);
111 if(n->priority > (*list)->priority)
113 list= &((*list)->next);
117 write_unlock(¬ifier_lock);
121 EXPORT_SYMBOL(notifier_chain_register);
124 * notifier_chain_unregister - Remove notifier from a notifier chain
125 * @nl: Pointer to root list pointer
126 * @n: New entry in notifier chain
128 * Removes a notifier from a notifier chain.
130 * Returns zero on success, or %-ENOENT on failure.
133 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
135 write_lock(¬ifier_lock);
141 write_unlock(¬ifier_lock);
146 write_unlock(¬ifier_lock);
150 EXPORT_SYMBOL(notifier_chain_unregister);
153 * notifier_call_chain - Call functions in a notifier chain
154 * @n: Pointer to root pointer of notifier chain
155 * @val: Value passed unmodified to notifier function
156 * @v: Pointer passed unmodified to notifier function
158 * Calls each function in a notifier chain in turn.
160 * If the return value of the notifier can be and'd
161 * with %NOTIFY_STOP_MASK, then notifier_call_chain
162 * will return immediately, with the return value of
163 * the notifier function which halted execution.
164 * Otherwise, the return value is the return value
165 * of the last notifier function called.
168 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
171 struct notifier_block *nb = *n;
175 ret=nb->notifier_call(nb,val,v);
176 if(ret&NOTIFY_STOP_MASK)
185 EXPORT_SYMBOL(notifier_call_chain);
188 * register_reboot_notifier - Register function to be called at reboot time
189 * @nb: Info about notifier function to be called
191 * Registers a function with the list of functions
192 * to be called at reboot time.
194 * Currently always returns zero, as notifier_chain_register
195 * always returns zero.
198 int register_reboot_notifier(struct notifier_block * nb)
200 return notifier_chain_register(&reboot_notifier_list, nb);
203 EXPORT_SYMBOL(register_reboot_notifier);
206 * unregister_reboot_notifier - Unregister previously registered reboot notifier
207 * @nb: Hook to be unregistered
209 * Unregisters a previously registered reboot
212 * Returns zero on success, or %-ENOENT on failure.
215 int unregister_reboot_notifier(struct notifier_block * nb)
217 return notifier_chain_unregister(&reboot_notifier_list, nb);
220 EXPORT_SYMBOL(unregister_reboot_notifier);
221 static int set_one_prio(struct task_struct *p, int niceval, int error)
225 if (p->uid != current->euid &&
226 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
230 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE)) {
231 if (vx_flags(VXF_IGNEG_NICE, 0))
237 no_nice = security_task_setnice(p, niceval);
244 set_user_nice(p, niceval);
249 asmlinkage long sys_setpriority(int which, int who, int niceval)
251 struct task_struct *g, *p;
252 struct user_struct *user;
255 if (which > 2 || which < 0)
258 /* normalize: avoid signed division (rounding problems) */
265 read_lock(&tasklist_lock);
270 p = find_task_by_pid(who);
272 error = set_one_prio(p, niceval, error);
276 who = process_group(current);
277 do_each_task_pid(who, PIDTYPE_PGID, p) {
278 error = set_one_prio(p, niceval, error);
279 } while_each_task_pid(who, PIDTYPE_PGID, p);
282 user = current->user;
286 if ((who != current->uid) &&
287 !(user = find_user(vx_current_xid(), who)))
288 goto out_unlock; /* No processes for this user */
292 error = set_one_prio(p, niceval, error);
293 while_each_thread(g, p);
294 if (who != current->uid)
295 free_uid(user); /* For find_user() */
299 read_unlock(&tasklist_lock);
305 * Ugh. To avoid negative return values, "getpriority()" will
306 * not return the normal nice-value, but a negated value that
307 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
308 * to stay compatible.
310 asmlinkage long sys_getpriority(int which, int who)
312 struct task_struct *g, *p;
313 struct user_struct *user;
314 long niceval, retval = -ESRCH;
316 if (which > 2 || which < 0)
319 read_lock(&tasklist_lock);
324 p = find_task_by_pid(who);
326 niceval = 20 - task_nice(p);
327 if (niceval > retval)
333 who = process_group(current);
334 do_each_task_pid(who, PIDTYPE_PGID, p) {
335 niceval = 20 - task_nice(p);
336 if (niceval > retval)
338 } while_each_task_pid(who, PIDTYPE_PGID, p);
341 user = current->user;
345 if ((who != current->uid) &&
346 !(user = find_user(vx_current_xid(), who)))
347 goto out_unlock; /* No processes for this user */
351 niceval = 20 - task_nice(p);
352 if (niceval > retval)
355 while_each_thread(g, p);
356 if (who != current->uid)
357 free_uid(user); /* for find_user() */
361 read_unlock(&tasklist_lock);
366 long vs_reboot(unsigned int, void *);
369 * Reboot system call: for obvious reasons only root may call it,
370 * and even root needs to set up some magic numbers in the registers
371 * so that some mistake won't make this reboot the whole machine.
372 * You can also set the meaning of the ctrl-alt-del-key here.
374 * reboot doesn't sync: do that yourself before calling this.
376 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
380 /* We only trust the superuser with rebooting the system. */
381 if (!capable(CAP_SYS_BOOT))
384 /* For safety, we require "magic" arguments. */
385 if (magic1 != LINUX_REBOOT_MAGIC1 ||
386 (magic2 != LINUX_REBOOT_MAGIC2 &&
387 magic2 != LINUX_REBOOT_MAGIC2A &&
388 magic2 != LINUX_REBOOT_MAGIC2B &&
389 magic2 != LINUX_REBOOT_MAGIC2C))
392 if (!vx_check(0, VX_ADMIN|VX_WATCH))
393 return vs_reboot(cmd, arg);
397 case LINUX_REBOOT_CMD_RESTART:
398 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
399 system_state = SYSTEM_RESTART;
401 printk(KERN_EMERG "Restarting system.\n");
402 machine_restart(NULL);
405 case LINUX_REBOOT_CMD_CAD_ON:
409 case LINUX_REBOOT_CMD_CAD_OFF:
413 case LINUX_REBOOT_CMD_HALT:
414 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
415 system_state = SYSTEM_HALT;
417 printk(KERN_EMERG "System halted.\n");
423 case LINUX_REBOOT_CMD_POWER_OFF:
424 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
425 system_state = SYSTEM_POWER_OFF;
427 printk(KERN_EMERG "Power down.\n");
433 case LINUX_REBOOT_CMD_RESTART2:
434 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
438 buffer[sizeof(buffer) - 1] = '\0';
440 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
441 system_state = SYSTEM_RESTART;
443 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
444 machine_restart(buffer);
447 #ifdef CONFIG_SOFTWARE_SUSPEND
448 case LINUX_REBOOT_CMD_SW_SUSPEND:
450 int ret = software_suspend();
464 static void deferred_cad(void *dummy)
466 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
467 machine_restart(NULL);
471 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
472 * As it's called within an interrupt, it may NOT sync: the only choice
473 * is whether to reboot at once, or just ignore the ctrl-alt-del.
475 void ctrl_alt_del(void)
477 static DECLARE_WORK(cad_work, deferred_cad, NULL);
480 schedule_work(&cad_work);
482 kill_proc(cad_pid, SIGINT, 1);
487 * Unprivileged users may change the real gid to the effective gid
488 * or vice versa. (BSD-style)
490 * If you set the real gid at all, or set the effective gid to a value not
491 * equal to the real gid, then the saved gid is set to the new effective gid.
493 * This makes it possible for a setgid program to completely drop its
494 * privileges, which is often a useful assertion to make when you are doing
495 * a security audit over a program.
497 * The general idea is that a program which uses just setregid() will be
498 * 100% compatible with BSD. A program which uses just setgid() will be
499 * 100% compatible with POSIX with saved IDs.
501 * SMP: There are not races, the GIDs are checked only by filesystem
502 * operations (as far as semantic preservation is concerned).
504 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
506 int old_rgid = current->gid;
507 int old_egid = current->egid;
508 int new_rgid = old_rgid;
509 int new_egid = old_egid;
512 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
516 if (rgid != (gid_t) -1) {
517 if ((old_rgid == rgid) ||
518 (current->egid==rgid) ||
524 if (egid != (gid_t) -1) {
525 if ((old_rgid == egid) ||
526 (current->egid == egid) ||
527 (current->sgid == egid) ||
534 if (new_egid != old_egid)
536 current->mm->dumpable = 0;
539 if (rgid != (gid_t) -1 ||
540 (egid != (gid_t) -1 && egid != old_rgid))
541 current->sgid = new_egid;
542 current->fsgid = new_egid;
543 current->egid = new_egid;
544 current->gid = new_rgid;
545 key_fsgid_changed(current);
550 * setgid() is implemented like SysV w/ SAVED_IDS
552 * SMP: Same implicit races as above.
554 asmlinkage long sys_setgid(gid_t gid)
556 int old_egid = current->egid;
559 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
563 if (capable(CAP_SETGID))
567 current->mm->dumpable=0;
570 current->gid = current->egid = current->sgid = current->fsgid = gid;
572 else if ((gid == current->gid) || (gid == current->sgid))
576 current->mm->dumpable=0;
579 current->egid = current->fsgid = gid;
584 key_fsgid_changed(current);
588 static int set_user(uid_t new_ruid, int dumpclear)
590 struct user_struct *new_user;
592 new_user = alloc_uid(vx_current_xid(), new_ruid);
596 if (atomic_read(&new_user->processes) >=
597 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
598 new_user != &root_user) {
603 switch_uid(new_user);
607 current->mm->dumpable = 0;
610 current->uid = new_ruid;
615 * Unprivileged users may change the real uid to the effective uid
616 * or vice versa. (BSD-style)
618 * If you set the real uid at all, or set the effective uid to a value not
619 * equal to the real uid, then the saved uid is set to the new effective uid.
621 * This makes it possible for a setuid program to completely drop its
622 * privileges, which is often a useful assertion to make when you are doing
623 * a security audit over a program.
625 * The general idea is that a program which uses just setreuid() will be
626 * 100% compatible with BSD. A program which uses just setuid() will be
627 * 100% compatible with POSIX with saved IDs.
629 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
631 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
634 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
638 new_ruid = old_ruid = current->uid;
639 new_euid = old_euid = current->euid;
640 old_suid = current->suid;
642 if (ruid != (uid_t) -1) {
644 if ((old_ruid != ruid) &&
645 (current->euid != ruid) &&
646 !capable(CAP_SETUID))
650 if (euid != (uid_t) -1) {
652 if ((old_ruid != euid) &&
653 (current->euid != euid) &&
654 (current->suid != euid) &&
655 !capable(CAP_SETUID))
659 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
662 if (new_euid != old_euid)
664 current->mm->dumpable=0;
667 current->fsuid = current->euid = new_euid;
668 if (ruid != (uid_t) -1 ||
669 (euid != (uid_t) -1 && euid != old_ruid))
670 current->suid = current->euid;
671 current->fsuid = current->euid;
673 key_fsuid_changed(current);
675 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
681 * setuid() is implemented like SysV with SAVED_IDS
683 * Note that SAVED_ID's is deficient in that a setuid root program
684 * like sendmail, for example, cannot set its uid to be a normal
685 * user and then switch back, because if you're root, setuid() sets
686 * the saved uid too. If you don't like this, blame the bright people
687 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
688 * will allow a root program to temporarily drop privileges and be able to
689 * regain them by swapping the real and effective uid.
691 asmlinkage long sys_setuid(uid_t uid)
693 int old_euid = current->euid;
694 int old_ruid, old_suid, new_ruid, new_suid;
697 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
701 old_ruid = new_ruid = current->uid;
702 old_suid = current->suid;
705 if (capable(CAP_SETUID)) {
706 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
709 } else if ((uid != current->uid) && (uid != new_suid))
714 current->mm->dumpable = 0;
717 current->fsuid = current->euid = uid;
718 current->suid = new_suid;
720 key_fsuid_changed(current);
722 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
727 * This function implements a generic ability to update ruid, euid,
728 * and suid. This allows you to implement the 4.4 compatible seteuid().
730 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
732 int old_ruid = current->uid;
733 int old_euid = current->euid;
734 int old_suid = current->suid;
737 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
741 if (!capable(CAP_SETUID)) {
742 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
743 (ruid != current->euid) && (ruid != current->suid))
745 if ((euid != (uid_t) -1) && (euid != current->uid) &&
746 (euid != current->euid) && (euid != current->suid))
748 if ((suid != (uid_t) -1) && (suid != current->uid) &&
749 (suid != current->euid) && (suid != current->suid))
752 if (ruid != (uid_t) -1) {
753 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
756 if (euid != (uid_t) -1) {
757 if (euid != current->euid)
759 current->mm->dumpable = 0;
762 current->euid = euid;
764 current->fsuid = current->euid;
765 if (suid != (uid_t) -1)
766 current->suid = suid;
768 key_fsuid_changed(current);
770 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
773 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
777 if (!(retval = put_user(current->uid, ruid)) &&
778 !(retval = put_user(current->euid, euid)))
779 retval = put_user(current->suid, suid);
785 * Same as above, but for rgid, egid, sgid.
787 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
791 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
795 if (!capable(CAP_SETGID)) {
796 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
797 (rgid != current->egid) && (rgid != current->sgid))
799 if ((egid != (gid_t) -1) && (egid != current->gid) &&
800 (egid != current->egid) && (egid != current->sgid))
802 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
803 (sgid != current->egid) && (sgid != current->sgid))
806 if (egid != (gid_t) -1) {
807 if (egid != current->egid)
809 current->mm->dumpable = 0;
812 current->egid = egid;
814 current->fsgid = current->egid;
815 if (rgid != (gid_t) -1)
817 if (sgid != (gid_t) -1)
818 current->sgid = sgid;
820 key_fsgid_changed(current);
824 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
828 if (!(retval = put_user(current->gid, rgid)) &&
829 !(retval = put_user(current->egid, egid)))
830 retval = put_user(current->sgid, sgid);
837 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
838 * is used for "access()" and for the NFS daemon (letting nfsd stay at
839 * whatever uid it wants to). It normally shadows "euid", except when
840 * explicitly set by setfsuid() or for access..
842 asmlinkage long sys_setfsuid(uid_t uid)
846 old_fsuid = current->fsuid;
847 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
850 if (uid == current->uid || uid == current->euid ||
851 uid == current->suid || uid == current->fsuid ||
854 if (uid != old_fsuid)
856 current->mm->dumpable = 0;
859 current->fsuid = uid;
862 key_fsuid_changed(current);
864 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
870 * Samma på svenska..
872 asmlinkage long sys_setfsgid(gid_t gid)
876 old_fsgid = current->fsgid;
877 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
880 if (gid == current->gid || gid == current->egid ||
881 gid == current->sgid || gid == current->fsgid ||
884 if (gid != old_fsgid)
886 current->mm->dumpable = 0;
889 current->fsgid = gid;
890 key_fsgid_changed(current);
895 asmlinkage long sys_times(struct tms __user * tbuf)
898 * In the SMP world we might just be unlucky and have one of
899 * the times increment as we use it. Since the value is an
900 * atomically safe type this is just fine. Conceptually its
901 * as if the syscall took an instant longer to occur.
905 struct task_struct *tsk = current;
906 struct task_struct *t;
907 cputime_t utime, stime, cutime, cstime;
909 read_lock(&tasklist_lock);
910 utime = tsk->signal->utime;
911 stime = tsk->signal->stime;
914 utime = cputime_add(utime, t->utime);
915 stime = cputime_add(stime, t->stime);
920 * While we have tasklist_lock read-locked, no dying thread
921 * can be updating current->signal->[us]time. Instead,
922 * we got their counts included in the live thread loop.
923 * However, another thread can come in right now and
924 * do a wait call that updates current->signal->c[us]time.
925 * To make sure we always see that pair updated atomically,
926 * we take the siglock around fetching them.
928 spin_lock_irq(&tsk->sighand->siglock);
929 cutime = tsk->signal->cutime;
930 cstime = tsk->signal->cstime;
931 spin_unlock_irq(&tsk->sighand->siglock);
932 read_unlock(&tasklist_lock);
934 tmp.tms_utime = cputime_to_clock_t(utime);
935 tmp.tms_stime = cputime_to_clock_t(stime);
936 tmp.tms_cutime = cputime_to_clock_t(cutime);
937 tmp.tms_cstime = cputime_to_clock_t(cstime);
938 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
941 return (long) jiffies_64_to_clock_t(get_jiffies_64());
945 * This needs some heavy checking ...
946 * I just haven't the stomach for it. I also don't fully
947 * understand sessions/pgrp etc. Let somebody who does explain it.
949 * OK, I think I have the protection semantics right.... this is really
950 * only important on a multi-user system anyway, to make sure one user
951 * can't send a signal to a process owned by another. -TYT, 12/12/91
953 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
957 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
959 struct task_struct *p;
964 pid = vx_map_pid(current->pid);
970 rpgid = vx_rmap_pid(pgid);
972 /* From this point forward we keep holding onto the tasklist lock
973 * so that our parent does not change from under us. -DaveM
975 write_lock_irq(&tasklist_lock);
978 p = find_task_by_pid(pid);
983 if (!thread_group_leader(p))
986 if (p->parent == current || p->real_parent == current) {
988 if (p->signal->session != current->signal->session)
1000 if (p->signal->leader)
1004 struct task_struct *p;
1006 do_each_task_pid(rpgid, PIDTYPE_PGID, p) {
1007 if (p->signal->session == current->signal->session)
1009 } while_each_task_pid(rpgid, PIDTYPE_PGID, p);
1014 err = security_task_setpgid(p, rpgid);
1018 if (process_group(p) != rpgid) {
1019 detach_pid(p, PIDTYPE_PGID);
1020 p->signal->pgrp = rpgid;
1021 attach_pid(p, PIDTYPE_PGID, rpgid);
1026 /* All paths lead to here, thus we are safe. -DaveM */
1027 write_unlock_irq(&tasklist_lock);
1031 asmlinkage long sys_getpgid(pid_t pid)
1034 return vx_rmap_pid(process_group(current));
1037 struct task_struct *p;
1039 read_lock(&tasklist_lock);
1040 p = find_task_by_pid(pid);
1044 retval = security_task_getpgid(p);
1046 retval = vx_rmap_pid(process_group(p));
1048 read_unlock(&tasklist_lock);
1053 #ifdef __ARCH_WANT_SYS_GETPGRP
1055 asmlinkage long sys_getpgrp(void)
1057 /* SMP - assuming writes are word atomic this is fine */
1058 return process_group(current);
1063 asmlinkage long sys_getsid(pid_t pid)
1066 return current->signal->session;
1069 struct task_struct *p;
1071 read_lock(&tasklist_lock);
1072 p = find_task_by_pid(pid);
1076 retval = security_task_getsid(p);
1078 retval = p->signal->session;
1080 read_unlock(&tasklist_lock);
1085 asmlinkage long sys_setsid(void)
1090 if (!thread_group_leader(current))
1094 write_lock_irq(&tasklist_lock);
1096 pid = find_pid(PIDTYPE_PGID, current->pid);
1100 current->signal->leader = 1;
1101 __set_special_pids(current->pid, current->pid);
1102 current->signal->tty = NULL;
1103 current->signal->tty_old_pgrp = 0;
1104 err = process_group(current);
1106 write_unlock_irq(&tasklist_lock);
1112 * Supplementary group IDs
1115 /* init to 2 - one for init_task, one to ensure it is never freed */
1116 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1118 struct group_info *groups_alloc(int gidsetsize)
1120 struct group_info *group_info;
1124 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1125 /* Make sure we always allocate at least one indirect block pointer */
1126 nblocks = nblocks ? : 1;
1127 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1130 group_info->ngroups = gidsetsize;
1131 group_info->nblocks = nblocks;
1132 atomic_set(&group_info->usage, 1);
1134 if (gidsetsize <= NGROUPS_SMALL) {
1135 group_info->blocks[0] = group_info->small_block;
1137 for (i = 0; i < nblocks; i++) {
1139 b = (void *)__get_free_page(GFP_USER);
1141 goto out_undo_partial_alloc;
1142 group_info->blocks[i] = b;
1147 out_undo_partial_alloc:
1149 free_page((unsigned long)group_info->blocks[i]);
1155 EXPORT_SYMBOL(groups_alloc);
1157 void groups_free(struct group_info *group_info)
1159 if (group_info->blocks[0] != group_info->small_block) {
1161 for (i = 0; i < group_info->nblocks; i++)
1162 free_page((unsigned long)group_info->blocks[i]);
1167 EXPORT_SYMBOL(groups_free);
1169 /* export the group_info to a user-space array */
1170 static int groups_to_user(gid_t __user *grouplist,
1171 struct group_info *group_info)
1174 int count = group_info->ngroups;
1176 for (i = 0; i < group_info->nblocks; i++) {
1177 int cp_count = min(NGROUPS_PER_BLOCK, count);
1178 int off = i * NGROUPS_PER_BLOCK;
1179 int len = cp_count * sizeof(*grouplist);
1181 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1189 /* fill a group_info from a user-space array - it must be allocated already */
1190 static int groups_from_user(struct group_info *group_info,
1191 gid_t __user *grouplist)
1194 int count = group_info->ngroups;
1196 for (i = 0; i < group_info->nblocks; i++) {
1197 int cp_count = min(NGROUPS_PER_BLOCK, count);
1198 int off = i * NGROUPS_PER_BLOCK;
1199 int len = cp_count * sizeof(*grouplist);
1201 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1209 /* a simple shell-metzner sort */
1210 static void groups_sort(struct group_info *group_info)
1212 int base, max, stride;
1213 int gidsetsize = group_info->ngroups;
1215 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1220 max = gidsetsize - stride;
1221 for (base = 0; base < max; base++) {
1223 int right = left + stride;
1224 gid_t tmp = GROUP_AT(group_info, right);
1226 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1227 GROUP_AT(group_info, right) =
1228 GROUP_AT(group_info, left);
1232 GROUP_AT(group_info, right) = tmp;
1238 /* a simple bsearch */
1239 static int groups_search(struct group_info *group_info, gid_t grp)
1247 right = group_info->ngroups;
1248 while (left < right) {
1249 int mid = (left+right)/2;
1250 int cmp = grp - GROUP_AT(group_info, mid);
1261 /* validate and set current->group_info */
1262 int set_current_groups(struct group_info *group_info)
1265 struct group_info *old_info;
1267 retval = security_task_setgroups(group_info);
1271 groups_sort(group_info);
1272 get_group_info(group_info);
1275 old_info = current->group_info;
1276 current->group_info = group_info;
1277 task_unlock(current);
1279 put_group_info(old_info);
1284 EXPORT_SYMBOL(set_current_groups);
1286 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1291 * SMP: Nobody else can change our grouplist. Thus we are
1298 /* no need to grab task_lock here; it cannot change */
1299 get_group_info(current->group_info);
1300 i = current->group_info->ngroups;
1302 if (i > gidsetsize) {
1306 if (groups_to_user(grouplist, current->group_info)) {
1312 put_group_info(current->group_info);
1317 * SMP: Our groups are copy-on-write. We can set them safely
1318 * without another task interfering.
1321 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1323 struct group_info *group_info;
1326 if (!capable(CAP_SETGID))
1328 if ((unsigned)gidsetsize > NGROUPS_MAX)
1331 group_info = groups_alloc(gidsetsize);
1334 retval = groups_from_user(group_info, grouplist);
1336 put_group_info(group_info);
1340 retval = set_current_groups(group_info);
1341 put_group_info(group_info);
1347 * Check whether we're fsgid/egid or in the supplemental group..
1349 int in_group_p(gid_t grp)
1352 if (grp != current->fsgid) {
1353 get_group_info(current->group_info);
1354 retval = groups_search(current->group_info, grp);
1355 put_group_info(current->group_info);
1360 EXPORT_SYMBOL(in_group_p);
1362 int in_egroup_p(gid_t grp)
1365 if (grp != current->egid) {
1366 get_group_info(current->group_info);
1367 retval = groups_search(current->group_info, grp);
1368 put_group_info(current->group_info);
1373 EXPORT_SYMBOL(in_egroup_p);
1375 DECLARE_RWSEM(uts_sem);
1377 EXPORT_SYMBOL(uts_sem);
1379 asmlinkage long sys_newuname(struct new_utsname __user * name)
1383 down_read(&uts_sem);
1384 if (copy_to_user(name, vx_new_utsname(), sizeof *name))
1390 asmlinkage long sys_sethostname(char __user *name, int len)
1393 char tmp[__NEW_UTS_LEN];
1395 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SET_UTSNAME))
1397 if (len < 0 || len > __NEW_UTS_LEN)
1399 down_write(&uts_sem);
1401 if (!copy_from_user(tmp, name, len)) {
1402 char *ptr = vx_new_uts(nodename);
1404 memcpy(ptr, tmp, len);
1412 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1414 asmlinkage long sys_gethostname(char __user *name, int len)
1421 down_read(&uts_sem);
1422 ptr = vx_new_uts(nodename);
1423 i = 1 + strlen(ptr);
1427 if (copy_to_user(name, ptr, i))
1436 * Only setdomainname; getdomainname can be implemented by calling
1439 asmlinkage long sys_setdomainname(char __user *name, int len)
1442 char tmp[__NEW_UTS_LEN];
1444 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SET_UTSNAME))
1446 if (len < 0 || len > __NEW_UTS_LEN)
1449 down_write(&uts_sem);
1451 if (!copy_from_user(tmp, name, len)) {
1452 char *ptr = vx_new_uts(domainname);
1454 memcpy(ptr, tmp, len);
1462 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1464 if (resource >= RLIM_NLIMITS)
1467 struct rlimit value;
1468 task_lock(current->group_leader);
1469 value = current->signal->rlim[resource];
1470 task_unlock(current->group_leader);
1471 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1475 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1478 * Back compatibility for getrlimit. Needed for some apps.
1481 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1484 if (resource >= RLIM_NLIMITS)
1487 task_lock(current->group_leader);
1488 x = current->signal->rlim[resource];
1489 task_unlock(current->group_leader);
1490 if(x.rlim_cur > 0x7FFFFFFF)
1491 x.rlim_cur = 0x7FFFFFFF;
1492 if(x.rlim_max > 0x7FFFFFFF)
1493 x.rlim_max = 0x7FFFFFFF;
1494 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1499 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1501 struct rlimit new_rlim, *old_rlim;
1504 if (resource >= RLIM_NLIMITS)
1506 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1508 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1510 old_rlim = current->signal->rlim + resource;
1511 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1512 !capable(CAP_SYS_RESOURCE) && !vx_ccaps(VXC_SET_RLIMIT))
1514 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
1517 retval = security_task_setrlimit(resource, &new_rlim);
1521 task_lock(current->group_leader);
1522 *old_rlim = new_rlim;
1523 task_unlock(current->group_leader);
1528 * It would make sense to put struct rusage in the task_struct,
1529 * except that would make the task_struct be *really big*. After
1530 * task_struct gets moved into malloc'ed memory, it would
1531 * make sense to do this. It will make moving the rest of the information
1532 * a lot simpler! (Which we're not doing right now because we're not
1533 * measuring them yet).
1535 * This expects to be called with tasklist_lock read-locked or better,
1536 * and the siglock not locked. It may momentarily take the siglock.
1538 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1539 * races with threads incrementing their own counters. But since word
1540 * reads are atomic, we either get new values or old values and we don't
1541 * care which for the sums. We always take the siglock to protect reading
1542 * the c* fields from p->signal from races with exit.c updating those
1543 * fields when reaping, so a sample either gets all the additions of a
1544 * given child after it's reaped, or none so this sample is before reaping.
1547 void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1549 struct task_struct *t;
1550 unsigned long flags;
1551 cputime_t utime, stime;
1553 memset((char *) r, 0, sizeof *r);
1555 if (unlikely(!p->signal))
1559 case RUSAGE_CHILDREN:
1560 spin_lock_irqsave(&p->sighand->siglock, flags);
1561 utime = p->signal->cutime;
1562 stime = p->signal->cstime;
1563 r->ru_nvcsw = p->signal->cnvcsw;
1564 r->ru_nivcsw = p->signal->cnivcsw;
1565 r->ru_minflt = p->signal->cmin_flt;
1566 r->ru_majflt = p->signal->cmaj_flt;
1567 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1568 cputime_to_timeval(utime, &r->ru_utime);
1569 cputime_to_timeval(stime, &r->ru_stime);
1572 spin_lock_irqsave(&p->sighand->siglock, flags);
1573 utime = stime = cputime_zero;
1576 spin_lock_irqsave(&p->sighand->siglock, flags);
1577 utime = p->signal->cutime;
1578 stime = p->signal->cstime;
1579 r->ru_nvcsw = p->signal->cnvcsw;
1580 r->ru_nivcsw = p->signal->cnivcsw;
1581 r->ru_minflt = p->signal->cmin_flt;
1582 r->ru_majflt = p->signal->cmaj_flt;
1584 utime = cputime_add(utime, p->signal->utime);
1585 stime = cputime_add(stime, p->signal->stime);
1586 r->ru_nvcsw += p->signal->nvcsw;
1587 r->ru_nivcsw += p->signal->nivcsw;
1588 r->ru_minflt += p->signal->min_flt;
1589 r->ru_majflt += p->signal->maj_flt;
1592 utime = cputime_add(utime, t->utime);
1593 stime = cputime_add(stime, t->stime);
1594 r->ru_nvcsw += t->nvcsw;
1595 r->ru_nivcsw += t->nivcsw;
1596 r->ru_minflt += t->min_flt;
1597 r->ru_majflt += t->maj_flt;
1600 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1601 cputime_to_timeval(utime, &r->ru_utime);
1602 cputime_to_timeval(stime, &r->ru_stime);
1609 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1612 read_lock(&tasklist_lock);
1613 k_getrusage(p, who, &r);
1614 read_unlock(&tasklist_lock);
1615 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1618 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1620 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1622 return getrusage(current, who, ru);
1625 asmlinkage long sys_umask(int mask)
1627 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1631 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1632 unsigned long arg4, unsigned long arg5)
1637 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1642 case PR_SET_PDEATHSIG:
1644 if (sig < 0 || sig > _NSIG) {
1648 current->pdeath_signal = sig;
1650 case PR_GET_PDEATHSIG:
1651 error = put_user(current->pdeath_signal, (int __user *)arg2);
1653 case PR_GET_DUMPABLE:
1654 if (current->mm->dumpable)
1657 case PR_SET_DUMPABLE:
1658 if (arg2 != 0 && arg2 != 1) {
1662 current->mm->dumpable = arg2;
1665 case PR_SET_UNALIGN:
1666 error = SET_UNALIGN_CTL(current, arg2);
1668 case PR_GET_UNALIGN:
1669 error = GET_UNALIGN_CTL(current, arg2);
1672 error = SET_FPEMU_CTL(current, arg2);
1675 error = GET_FPEMU_CTL(current, arg2);
1678 error = SET_FPEXC_CTL(current, arg2);
1681 error = GET_FPEXC_CTL(current, arg2);
1684 error = PR_TIMING_STATISTICAL;
1687 if (arg2 == PR_TIMING_STATISTICAL)
1693 case PR_GET_KEEPCAPS:
1694 if (current->keep_capabilities)
1697 case PR_SET_KEEPCAPS:
1698 if (arg2 != 0 && arg2 != 1) {
1702 current->keep_capabilities = arg2;
1705 struct task_struct *me = current;
1706 unsigned char ncomm[sizeof(me->comm)];
1708 ncomm[sizeof(me->comm)-1] = 0;
1709 if (strncpy_from_user(ncomm, (char __user *)arg2,
1710 sizeof(me->comm)-1) < 0)
1712 set_task_comm(me, ncomm);
1716 struct task_struct *me = current;
1717 unsigned char tcomm[sizeof(me->comm)];
1719 get_task_comm(tcomm, me);
1720 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))