4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/compat.h>
9 #include <linux/module.h>
11 #include <linux/utsname.h>
12 #include <linux/mman.h>
13 #include <linux/smp_lock.h>
14 #include <linux/notifier.h>
15 #include <linux/kmod.h>
16 #include <linux/reboot.h>
17 #include <linux/prctl.h>
18 #include <linux/init.h>
19 #include <linux/highuid.h>
21 #include <linux/workqueue.h>
22 #include <linux/device.h>
23 #include <linux/times.h>
24 #include <linux/security.h>
25 #include <linux/dcookies.h>
26 #include <linux/suspend.h>
27 #include <linux/ckrm.h>
28 #include <linux/vs_base.h>
29 #include <linux/vs_cvirt.h>
31 #include <asm/uaccess.h>
33 #include <asm/unistd.h>
35 #ifndef SET_UNALIGN_CTL
36 # define SET_UNALIGN_CTL(a,b) (-EINVAL)
38 #ifndef GET_UNALIGN_CTL
39 # define GET_UNALIGN_CTL(a,b) (-EINVAL)
42 # define SET_FPEMU_CTL(a,b) (-EINVAL)
45 # define GET_FPEMU_CTL(a,b) (-EINVAL)
48 # define SET_FPEXC_CTL(a,b) (-EINVAL)
51 # define GET_FPEXC_CTL(a,b) (-EINVAL)
55 * this is where the system-wide overflow UID and GID are defined, for
56 * architectures that now have 32-bit UID/GID but didn't in the past
59 int overflowuid = DEFAULT_OVERFLOWUID;
60 int overflowgid = DEFAULT_OVERFLOWGID;
63 EXPORT_SYMBOL(overflowuid);
64 EXPORT_SYMBOL(overflowgid);
68 * the same as above, but for filesystems which can only store a 16-bit
69 * UID and GID. as such, this is needed on all architectures
72 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
73 int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
75 EXPORT_SYMBOL(fs_overflowuid);
76 EXPORT_SYMBOL(fs_overflowgid);
79 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
86 * Notifier list for kernel code which wants to be called
87 * at shutdown. This is used to stop any idling DMA operations
91 static struct notifier_block *reboot_notifier_list;
92 rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
95 * notifier_chain_register - Add notifier to a notifier chain
96 * @list: Pointer to root list pointer
97 * @n: New entry in notifier chain
99 * Adds a notifier to a notifier chain.
101 * Currently always returns zero.
104 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
106 write_lock(¬ifier_lock);
109 if(n->priority > (*list)->priority)
111 list= &((*list)->next);
115 write_unlock(¬ifier_lock);
119 EXPORT_SYMBOL(notifier_chain_register);
122 * notifier_chain_unregister - Remove notifier from a notifier chain
123 * @nl: Pointer to root list pointer
124 * @n: New entry in notifier chain
126 * Removes a notifier from a notifier chain.
128 * Returns zero on success, or %-ENOENT on failure.
131 int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
133 write_lock(¬ifier_lock);
139 write_unlock(¬ifier_lock);
144 write_unlock(¬ifier_lock);
148 EXPORT_SYMBOL(notifier_chain_unregister);
151 * notifier_call_chain - Call functions in a notifier chain
152 * @n: Pointer to root pointer of notifier chain
153 * @val: Value passed unmodified to notifier function
154 * @v: Pointer passed unmodified to notifier function
156 * Calls each function in a notifier chain in turn.
158 * If the return value of the notifier can be and'd
159 * with %NOTIFY_STOP_MASK, then notifier_call_chain
160 * will return immediately, with the return value of
161 * the notifier function which halted execution.
162 * Otherwise, the return value is the return value
163 * of the last notifier function called.
166 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
169 struct notifier_block *nb = *n;
173 ret=nb->notifier_call(nb,val,v);
174 if(ret&NOTIFY_STOP_MASK)
183 EXPORT_SYMBOL(notifier_call_chain);
186 * register_reboot_notifier - Register function to be called at reboot time
187 * @nb: Info about notifier function to be called
189 * Registers a function with the list of functions
190 * to be called at reboot time.
192 * Currently always returns zero, as notifier_chain_register
193 * always returns zero.
196 int register_reboot_notifier(struct notifier_block * nb)
198 return notifier_chain_register(&reboot_notifier_list, nb);
201 EXPORT_SYMBOL(register_reboot_notifier);
204 * unregister_reboot_notifier - Unregister previously registered reboot notifier
205 * @nb: Hook to be unregistered
207 * Unregisters a previously registered reboot
210 * Returns zero on success, or %-ENOENT on failure.
213 int unregister_reboot_notifier(struct notifier_block * nb)
215 return notifier_chain_unregister(&reboot_notifier_list, nb);
218 EXPORT_SYMBOL(unregister_reboot_notifier);
220 asmlinkage long sys_ni_syscall(void)
225 cond_syscall(sys_nfsservctl)
226 cond_syscall(sys_quotactl)
227 cond_syscall(sys_acct)
228 cond_syscall(sys_lookup_dcookie)
229 cond_syscall(sys_swapon)
230 cond_syscall(sys_swapoff)
231 cond_syscall(sys_init_module)
232 cond_syscall(sys_delete_module)
233 cond_syscall(sys_socketpair)
234 cond_syscall(sys_bind)
235 cond_syscall(sys_listen)
236 cond_syscall(sys_accept)
237 cond_syscall(sys_connect)
238 cond_syscall(sys_getsockname)
239 cond_syscall(sys_getpeername)
240 cond_syscall(sys_sendto)
241 cond_syscall(sys_send)
242 cond_syscall(sys_recvfrom)
243 cond_syscall(sys_recv)
244 cond_syscall(sys_socket)
245 cond_syscall(sys_setsockopt)
246 cond_syscall(sys_getsockopt)
247 cond_syscall(sys_shutdown)
248 cond_syscall(sys_sendmsg)
249 cond_syscall(sys_recvmsg)
250 cond_syscall(sys_socketcall)
251 cond_syscall(sys_futex)
252 cond_syscall(compat_sys_futex)
253 cond_syscall(sys_epoll_create)
254 cond_syscall(sys_epoll_ctl)
255 cond_syscall(sys_epoll_wait)
256 cond_syscall(sys_semget)
257 cond_syscall(sys_semop)
258 cond_syscall(sys_semtimedop)
259 cond_syscall(sys_semctl)
260 cond_syscall(sys_msgget)
261 cond_syscall(sys_msgsnd)
262 cond_syscall(sys_msgrcv)
263 cond_syscall(sys_msgctl)
264 cond_syscall(sys_shmget)
265 cond_syscall(sys_shmdt)
266 cond_syscall(sys_shmctl)
267 cond_syscall(sys_mq_open)
268 cond_syscall(sys_mq_unlink)
269 cond_syscall(sys_mq_timedsend)
270 cond_syscall(sys_mq_timedreceive)
271 cond_syscall(sys_mq_notify)
272 cond_syscall(sys_mq_getsetattr)
273 cond_syscall(compat_sys_mq_open)
274 cond_syscall(compat_sys_mq_timedsend)
275 cond_syscall(compat_sys_mq_timedreceive)
276 cond_syscall(compat_sys_mq_notify)
277 cond_syscall(compat_sys_mq_getsetattr)
278 cond_syscall(sys_mbind)
279 cond_syscall(sys_get_mempolicy)
280 cond_syscall(sys_set_mempolicy)
281 cond_syscall(compat_get_mempolicy)
283 /* arch-specific weak syscall entries */
284 cond_syscall(sys_pciconfig_read)
285 cond_syscall(sys_pciconfig_write)
286 cond_syscall(sys_pciconfig_iobase)
288 static int set_one_prio(struct task_struct *p, int niceval, int error)
292 if (p->uid != current->euid &&
293 p->uid != current->uid && !capable(CAP_SYS_NICE)) {
297 if (niceval < task_nice(p) && !capable(CAP_SYS_NICE)) {
301 no_nice = security_task_setnice(p, niceval);
308 set_user_nice(p, niceval);
313 asmlinkage long sys_setpriority(int which, int who, int niceval)
315 struct task_struct *g, *p;
316 struct user_struct *user;
321 if (which > 2 || which < 0)
324 /* normalize: avoid signed division (rounding problems) */
331 read_lock(&tasklist_lock);
336 p = find_task_by_pid(who);
338 error = set_one_prio(p, niceval, error);
342 who = process_group(current);
343 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid)
344 error = set_one_prio(p, niceval, error);
348 user = current->user;
350 user = find_user(vx_current_xid(), who);
357 error = set_one_prio(p, niceval, error);
358 while_each_thread(g, p);
360 free_uid(user); /* For find_user() */
364 read_unlock(&tasklist_lock);
370 * Ugh. To avoid negative return values, "getpriority()" will
371 * not return the normal nice-value, but a negated value that
372 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
373 * to stay compatible.
375 asmlinkage long sys_getpriority(int which, int who)
377 struct task_struct *g, *p;
380 struct user_struct *user;
381 long niceval, retval = -ESRCH;
383 if (which > 2 || which < 0)
386 read_lock(&tasklist_lock);
391 p = find_task_by_pid(who);
393 niceval = 20 - task_nice(p);
394 if (niceval > retval)
400 who = process_group(current);
401 for_each_task_pid(who, PIDTYPE_PGID, p, l, pid) {
402 niceval = 20 - task_nice(p);
403 if (niceval > retval)
409 user = current->user;
411 user = find_user(vx_current_xid(), who);
418 niceval = 20 - task_nice(p);
419 if (niceval > retval)
422 while_each_thread(g, p);
424 free_uid(user); /* for find_user() */
428 read_unlock(&tasklist_lock);
433 long vs_reboot(unsigned int, void *);
436 * Reboot system call: for obvious reasons only root may call it,
437 * and even root needs to set up some magic numbers in the registers
438 * so that some mistake won't make this reboot the whole machine.
439 * You can also set the meaning of the ctrl-alt-del-key here.
441 * reboot doesn't sync: do that yourself before calling this.
443 asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
447 /* We only trust the superuser with rebooting the system. */
448 if (!capable(CAP_SYS_BOOT))
451 /* For safety, we require "magic" arguments. */
452 if (magic1 != LINUX_REBOOT_MAGIC1 ||
453 (magic2 != LINUX_REBOOT_MAGIC2 &&
454 magic2 != LINUX_REBOOT_MAGIC2A &&
455 magic2 != LINUX_REBOOT_MAGIC2B &&
456 magic2 != LINUX_REBOOT_MAGIC2C))
459 if (!vx_check(0, VX_ADMIN|VX_WATCH))
460 return vs_reboot(cmd, arg);
464 case LINUX_REBOOT_CMD_RESTART:
465 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
466 system_state = SYSTEM_RESTART;
468 printk(KERN_EMERG "Restarting system.\n");
469 machine_restart(NULL);
472 case LINUX_REBOOT_CMD_CAD_ON:
476 case LINUX_REBOOT_CMD_CAD_OFF:
480 case LINUX_REBOOT_CMD_HALT:
481 notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
482 system_state = SYSTEM_HALT;
484 printk(KERN_EMERG "System halted.\n");
490 case LINUX_REBOOT_CMD_POWER_OFF:
491 notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
492 system_state = SYSTEM_POWER_OFF;
494 printk(KERN_EMERG "Power down.\n");
500 case LINUX_REBOOT_CMD_RESTART2:
501 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
505 buffer[sizeof(buffer) - 1] = '\0';
507 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, buffer);
508 system_state = SYSTEM_RESTART;
510 printk(KERN_EMERG "Restarting system with command '%s'.\n", buffer);
511 machine_restart(buffer);
514 #ifdef CONFIG_SOFTWARE_SUSPEND
515 case LINUX_REBOOT_CMD_SW_SUSPEND:
517 int ret = software_suspend();
531 static void deferred_cad(void *dummy)
533 notifier_call_chain(&reboot_notifier_list, SYS_RESTART, NULL);
534 machine_restart(NULL);
538 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
539 * As it's called within an interrupt, it may NOT sync: the only choice
540 * is whether to reboot at once, or just ignore the ctrl-alt-del.
542 void ctrl_alt_del(void)
544 static DECLARE_WORK(cad_work, deferred_cad, NULL);
547 schedule_work(&cad_work);
549 kill_proc(cad_pid, SIGINT, 1);
554 * Unprivileged users may change the real gid to the effective gid
555 * or vice versa. (BSD-style)
557 * If you set the real gid at all, or set the effective gid to a value not
558 * equal to the real gid, then the saved gid is set to the new effective gid.
560 * This makes it possible for a setgid program to completely drop its
561 * privileges, which is often a useful assertion to make when you are doing
562 * a security audit over a program.
564 * The general idea is that a program which uses just setregid() will be
565 * 100% compatible with BSD. A program which uses just setgid() will be
566 * 100% compatible with POSIX with saved IDs.
568 * SMP: There are not races, the GIDs are checked only by filesystem
569 * operations (as far as semantic preservation is concerned).
571 asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
573 int old_rgid = current->gid;
574 int old_egid = current->egid;
575 int new_rgid = old_rgid;
576 int new_egid = old_egid;
579 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
583 if (rgid != (gid_t) -1) {
584 if ((old_rgid == rgid) ||
585 (current->egid==rgid) ||
591 if (egid != (gid_t) -1) {
592 if ((old_rgid == egid) ||
593 (current->egid == egid) ||
594 (current->sgid == egid) ||
601 if (new_egid != old_egid)
603 current->mm->dumpable = 0;
606 if (rgid != (gid_t) -1 ||
607 (egid != (gid_t) -1 && egid != old_rgid))
608 current->sgid = new_egid;
609 current->fsgid = new_egid;
610 current->egid = new_egid;
611 current->gid = new_rgid;
619 * setgid() is implemented like SysV w/ SAVED_IDS
621 * SMP: Same implicit races as above.
623 asmlinkage long sys_setgid(gid_t gid)
625 int old_egid = current->egid;
628 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
632 if (capable(CAP_SETGID))
636 current->mm->dumpable=0;
639 current->gid = current->egid = current->sgid = current->fsgid = gid;
641 else if ((gid == current->gid) || (gid == current->sgid))
645 current->mm->dumpable=0;
648 current->egid = current->fsgid = gid;
658 static int set_user(uid_t new_ruid, int dumpclear)
660 struct user_struct *new_user;
662 new_user = alloc_uid(vx_current_xid(), new_ruid);
666 if (atomic_read(&new_user->processes) >=
667 current->rlim[RLIMIT_NPROC].rlim_cur &&
668 new_user != &root_user) {
673 switch_uid(new_user);
677 current->mm->dumpable = 0;
680 current->uid = new_ruid;
685 * Unprivileged users may change the real uid to the effective uid
686 * or vice versa. (BSD-style)
688 * If you set the real uid at all, or set the effective uid to a value not
689 * equal to the real uid, then the saved uid is set to the new effective uid.
691 * This makes it possible for a setuid program to completely drop its
692 * privileges, which is often a useful assertion to make when you are doing
693 * a security audit over a program.
695 * The general idea is that a program which uses just setreuid() will be
696 * 100% compatible with BSD. A program which uses just setuid() will be
697 * 100% compatible with POSIX with saved IDs.
699 asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
701 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
704 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
708 new_ruid = old_ruid = current->uid;
709 new_euid = old_euid = current->euid;
710 old_suid = current->suid;
712 if (ruid != (uid_t) -1) {
714 if ((old_ruid != ruid) &&
715 (current->euid != ruid) &&
716 !capable(CAP_SETUID))
720 if (euid != (uid_t) -1) {
722 if ((old_ruid != euid) &&
723 (current->euid != euid) &&
724 (current->suid != euid) &&
725 !capable(CAP_SETUID))
729 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
732 if (new_euid != old_euid)
734 current->mm->dumpable=0;
737 current->fsuid = current->euid = new_euid;
738 if (ruid != (uid_t) -1 ||
739 (euid != (uid_t) -1 && euid != old_ruid))
740 current->suid = current->euid;
741 current->fsuid = current->euid;
745 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
751 * setuid() is implemented like SysV with SAVED_IDS
753 * Note that SAVED_ID's is deficient in that a setuid root program
754 * like sendmail, for example, cannot set its uid to be a normal
755 * user and then switch back, because if you're root, setuid() sets
756 * the saved uid too. If you don't like this, blame the bright people
757 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
758 * will allow a root program to temporarily drop privileges and be able to
759 * regain them by swapping the real and effective uid.
761 asmlinkage long sys_setuid(uid_t uid)
763 int old_euid = current->euid;
764 int old_ruid, old_suid, new_ruid, new_suid;
767 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
771 old_ruid = new_ruid = current->uid;
772 old_suid = current->suid;
775 if (capable(CAP_SETUID)) {
776 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
779 } else if ((uid != current->uid) && (uid != new_suid))
784 current->mm->dumpable = 0;
787 current->fsuid = current->euid = uid;
788 current->suid = new_suid;
792 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
797 * This function implements a generic ability to update ruid, euid,
798 * and suid. This allows you to implement the 4.4 compatible seteuid().
800 asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
802 int old_ruid = current->uid;
803 int old_euid = current->euid;
804 int old_suid = current->suid;
807 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
811 if (!capable(CAP_SETUID)) {
812 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
813 (ruid != current->euid) && (ruid != current->suid))
815 if ((euid != (uid_t) -1) && (euid != current->uid) &&
816 (euid != current->euid) && (euid != current->suid))
818 if ((suid != (uid_t) -1) && (suid != current->uid) &&
819 (suid != current->euid) && (suid != current->suid))
822 if (ruid != (uid_t) -1) {
823 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
826 if (euid != (uid_t) -1) {
827 if (euid != current->euid)
829 current->mm->dumpable = 0;
832 current->euid = euid;
834 current->fsuid = current->euid;
835 if (suid != (uid_t) -1)
836 current->suid = suid;
840 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
843 asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
847 if (!(retval = put_user(current->uid, ruid)) &&
848 !(retval = put_user(current->euid, euid)))
849 retval = put_user(current->suid, suid);
855 * Same as above, but for rgid, egid, sgid.
857 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
861 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
865 if (!capable(CAP_SETGID)) {
866 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
867 (rgid != current->egid) && (rgid != current->sgid))
869 if ((egid != (gid_t) -1) && (egid != current->gid) &&
870 (egid != current->egid) && (egid != current->sgid))
872 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
873 (sgid != current->egid) && (sgid != current->sgid))
876 if (egid != (gid_t) -1) {
877 if (egid != current->egid)
879 current->mm->dumpable = 0;
882 current->egid = egid;
884 current->fsgid = current->egid;
885 if (rgid != (gid_t) -1)
887 if (sgid != (gid_t) -1)
888 current->sgid = sgid;
895 asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
899 if (!(retval = put_user(current->gid, rgid)) &&
900 !(retval = put_user(current->egid, egid)))
901 retval = put_user(current->sgid, sgid);
908 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
909 * is used for "access()" and for the NFS daemon (letting nfsd stay at
910 * whatever uid it wants to). It normally shadows "euid", except when
911 * explicitly set by setfsuid() or for access..
913 asmlinkage long sys_setfsuid(uid_t uid)
917 old_fsuid = current->fsuid;
918 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
921 if (uid == current->uid || uid == current->euid ||
922 uid == current->suid || uid == current->fsuid ||
925 if (uid != old_fsuid)
927 current->mm->dumpable = 0;
930 current->fsuid = uid;
933 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
939 * Samma på svenska..
941 asmlinkage long sys_setfsgid(gid_t gid)
945 old_fsgid = current->fsgid;
946 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
949 if (gid == current->gid || gid == current->egid ||
950 gid == current->sgid || gid == current->fsgid ||
953 if (gid != old_fsgid)
955 current->mm->dumpable = 0;
958 current->fsgid = gid;
963 asmlinkage long sys_times(struct tms __user * tbuf)
966 * In the SMP world we might just be unlucky and have one of
967 * the times increment as we use it. Since the value is an
968 * atomically safe type this is just fine. Conceptually its
969 * as if the syscall took an instant longer to occur.
973 tmp.tms_utime = jiffies_to_clock_t(current->utime);
974 tmp.tms_stime = jiffies_to_clock_t(current->stime);
975 tmp.tms_cutime = jiffies_to_clock_t(current->cutime);
976 tmp.tms_cstime = jiffies_to_clock_t(current->cstime);
977 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
980 return (long) jiffies_64_to_clock_t(get_jiffies_64());
984 * This needs some heavy checking ...
985 * I just haven't the stomach for it. I also don't fully
986 * understand sessions/pgrp etc. Let somebody who does explain it.
988 * OK, I think I have the protection semantics right.... this is really
989 * only important on a multi-user system anyway, to make sure one user
990 * can't send a signal to a process owned by another. -TYT, 12/12/91
992 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
996 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
998 struct task_struct *p;
1008 /* From this point forward we keep holding onto the tasklist lock
1009 * so that our parent does not change from under us. -DaveM
1011 write_lock_irq(&tasklist_lock);
1014 p = find_task_by_pid(pid);
1019 if (!thread_group_leader(p))
1022 if (p->parent == current || p->real_parent == current) {
1024 if (p->signal->session != current->signal->session)
1036 if (p->signal->leader)
1040 struct task_struct *p;
1042 struct list_head *l;
1044 for_each_task_pid(pgid, PIDTYPE_PGID, p, l, pid)
1045 if (p->signal->session == current->signal->session)
1051 err = security_task_setpgid(p, pgid);
1055 if (process_group(p) != pgid) {
1056 detach_pid(p, PIDTYPE_PGID);
1057 p->signal->pgrp = pgid;
1058 attach_pid(p, PIDTYPE_PGID, pgid);
1063 /* All paths lead to here, thus we are safe. -DaveM */
1064 write_unlock_irq(&tasklist_lock);
1068 asmlinkage long sys_getpgid(pid_t pid)
1071 return process_group(current);
1074 struct task_struct *p;
1076 read_lock(&tasklist_lock);
1077 p = find_task_by_pid(pid);
1081 retval = security_task_getpgid(p);
1083 retval = process_group(p);
1085 read_unlock(&tasklist_lock);
1090 #ifdef __ARCH_WANT_SYS_GETPGRP
1092 asmlinkage long sys_getpgrp(void)
1094 /* SMP - assuming writes are word atomic this is fine */
1095 return process_group(current);
1100 asmlinkage long sys_getsid(pid_t pid)
1103 return current->signal->session;
1106 struct task_struct *p;
1108 read_lock(&tasklist_lock);
1109 p = find_task_by_pid(pid);
1113 retval = security_task_getsid(p);
1115 retval = p->signal->session;
1117 read_unlock(&tasklist_lock);
1122 asmlinkage long sys_setsid(void)
1127 if (!thread_group_leader(current))
1130 write_lock_irq(&tasklist_lock);
1132 pid = find_pid(PIDTYPE_PGID, current->pid);
1136 current->signal->leader = 1;
1137 __set_special_pids(current->pid, current->pid);
1138 current->signal->tty = NULL;
1139 current->signal->tty_old_pgrp = 0;
1140 err = process_group(current);
1142 write_unlock_irq(&tasklist_lock);
1147 * Supplementary group IDs
1150 /* init to 2 - one for init_task, one to ensure it is never freed */
1151 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1153 struct group_info *groups_alloc(int gidsetsize)
1155 struct group_info *group_info;
1159 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1160 /* Make sure we always allocate at least one indirect block pointer */
1161 nblocks = nblocks ? : 1;
1162 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1165 group_info->ngroups = gidsetsize;
1166 group_info->nblocks = nblocks;
1167 atomic_set(&group_info->usage, 1);
1169 if (gidsetsize <= NGROUPS_SMALL) {
1170 group_info->blocks[0] = group_info->small_block;
1172 for (i = 0; i < nblocks; i++) {
1174 b = (void *)__get_free_page(GFP_USER);
1176 goto out_undo_partial_alloc;
1177 group_info->blocks[i] = b;
1182 out_undo_partial_alloc:
1184 free_page((unsigned long)group_info->blocks[i]);
1190 EXPORT_SYMBOL(groups_alloc);
1192 void groups_free(struct group_info *group_info)
1194 if (group_info->blocks[0] != group_info->small_block) {
1196 for (i = 0; i < group_info->nblocks; i++)
1197 free_page((unsigned long)group_info->blocks[i]);
1202 EXPORT_SYMBOL(groups_free);
1204 /* export the group_info to a user-space array */
1205 static int groups_to_user(gid_t __user *grouplist,
1206 struct group_info *group_info)
1209 int count = group_info->ngroups;
1211 for (i = 0; i < group_info->nblocks; i++) {
1212 int cp_count = min(NGROUPS_PER_BLOCK, count);
1213 int off = i * NGROUPS_PER_BLOCK;
1214 int len = cp_count * sizeof(*grouplist);
1216 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1224 /* fill a group_info from a user-space array - it must be allocated already */
1225 static int groups_from_user(struct group_info *group_info,
1226 gid_t __user *grouplist)
1229 int count = group_info->ngroups;
1231 for (i = 0; i < group_info->nblocks; i++) {
1232 int cp_count = min(NGROUPS_PER_BLOCK, count);
1233 int off = i * NGROUPS_PER_BLOCK;
1234 int len = cp_count * sizeof(*grouplist);
1236 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1244 /* a simple shell-metzner sort */
1245 static void groups_sort(struct group_info *group_info)
1247 int base, max, stride;
1248 int gidsetsize = group_info->ngroups;
1250 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1255 max = gidsetsize - stride;
1256 for (base = 0; base < max; base++) {
1258 int right = left + stride;
1259 gid_t tmp = GROUP_AT(group_info, right);
1261 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1262 GROUP_AT(group_info, right) =
1263 GROUP_AT(group_info, left);
1267 GROUP_AT(group_info, right) = tmp;
1273 /* a simple bsearch */
1274 static int groups_search(struct group_info *group_info, gid_t grp)
1282 right = group_info->ngroups;
1283 while (left < right) {
1284 int mid = (left+right)/2;
1285 int cmp = grp - GROUP_AT(group_info, mid);
1296 /* validate and set current->group_info */
1297 int set_current_groups(struct group_info *group_info)
1300 struct group_info *old_info;
1302 retval = security_task_setgroups(group_info);
1306 groups_sort(group_info);
1307 get_group_info(group_info);
1310 old_info = current->group_info;
1311 current->group_info = group_info;
1312 task_unlock(current);
1314 put_group_info(old_info);
1319 EXPORT_SYMBOL(set_current_groups);
1321 asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1326 * SMP: Nobody else can change our grouplist. Thus we are
1333 /* no need to grab task_lock here; it cannot change */
1334 get_group_info(current->group_info);
1335 i = current->group_info->ngroups;
1337 if (i > gidsetsize) {
1341 if (groups_to_user(grouplist, current->group_info)) {
1347 put_group_info(current->group_info);
1352 * SMP: Our groups are copy-on-write. We can set them safely
1353 * without another task interfering.
1356 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1358 struct group_info *group_info;
1361 if (!capable(CAP_SETGID))
1363 if ((unsigned)gidsetsize > NGROUPS_MAX)
1366 group_info = groups_alloc(gidsetsize);
1369 retval = groups_from_user(group_info, grouplist);
1371 put_group_info(group_info);
1375 retval = set_current_groups(group_info);
1376 put_group_info(group_info);
1382 * Check whether we're fsgid/egid or in the supplemental group..
1384 int in_group_p(gid_t grp)
1387 if (grp != current->fsgid) {
1388 get_group_info(current->group_info);
1389 retval = groups_search(current->group_info, grp);
1390 put_group_info(current->group_info);
1395 EXPORT_SYMBOL(in_group_p);
1397 int in_egroup_p(gid_t grp)
1400 if (grp != current->egid) {
1401 get_group_info(current->group_info);
1402 retval = groups_search(current->group_info, grp);
1403 put_group_info(current->group_info);
1408 EXPORT_SYMBOL(in_egroup_p);
1410 DECLARE_RWSEM(uts_sem);
1412 EXPORT_SYMBOL(uts_sem);
1414 asmlinkage long sys_newuname(struct new_utsname __user * name)
1418 down_read(&uts_sem);
1419 if (copy_to_user(name, vx_new_utsname(), sizeof *name))
1425 asmlinkage long sys_sethostname(char __user *name, int len)
1428 char tmp[__NEW_UTS_LEN];
1430 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SET_UTSNAME))
1432 if (len < 0 || len > __NEW_UTS_LEN)
1434 down_write(&uts_sem);
1436 if (!copy_from_user(tmp, name, len)) {
1437 char *ptr = vx_new_uts(nodename);
1439 memcpy(ptr, tmp, len);
1447 #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1449 asmlinkage long sys_gethostname(char __user *name, int len)
1456 down_read(&uts_sem);
1457 ptr = vx_new_uts(nodename);
1458 i = 1 + strlen(ptr);
1462 if (copy_to_user(name, ptr, i))
1471 * Only setdomainname; getdomainname can be implemented by calling
1474 asmlinkage long sys_setdomainname(char __user *name, int len)
1477 char tmp[__NEW_UTS_LEN];
1479 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SET_UTSNAME))
1481 if (len < 0 || len > __NEW_UTS_LEN)
1484 down_write(&uts_sem);
1486 if (!copy_from_user(tmp, name, len)) {
1487 char *ptr = vx_new_uts(domainname);
1489 memcpy(ptr, tmp, len);
1497 asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1499 if (resource >= RLIM_NLIMITS)
1502 return copy_to_user(rlim, current->rlim + resource, sizeof(*rlim))
1506 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1509 * Back compatibility for getrlimit. Needed for some apps.
1512 asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1515 if (resource >= RLIM_NLIMITS)
1518 memcpy(&x, current->rlim + resource, sizeof(*rlim));
1519 if(x.rlim_cur > 0x7FFFFFFF)
1520 x.rlim_cur = 0x7FFFFFFF;
1521 if(x.rlim_max > 0x7FFFFFFF)
1522 x.rlim_max = 0x7FFFFFFF;
1523 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1528 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1530 struct rlimit new_rlim, *old_rlim;
1533 if (resource >= RLIM_NLIMITS)
1535 if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1537 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1539 old_rlim = current->rlim + resource;
1540 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
1541 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
1542 !capable(CAP_SYS_RESOURCE) && !vx_ccaps(VXC_SET_RLIMIT))
1544 if (resource == RLIMIT_NOFILE) {
1545 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
1549 retval = security_task_setrlimit(resource, &new_rlim);
1553 *old_rlim = new_rlim;
1558 * It would make sense to put struct rusage in the task_struct,
1559 * except that would make the task_struct be *really big*. After
1560 * task_struct gets moved into malloc'ed memory, it would
1561 * make sense to do this. It will make moving the rest of the information
1562 * a lot simpler! (Which we're not doing right now because we're not
1563 * measuring them yet).
1565 * This is SMP safe. Either we are called from sys_getrusage on ourselves
1566 * below (we know we aren't going to exit/disappear and only we change our
1567 * rusage counters), or we are called from wait4() on a process which is
1568 * either stopped or zombied. In the zombied case the task won't get
1569 * reaped till shortly after the call to getrusage(), in both cases the
1570 * task being examined is in a frozen state so the counters won't change.
1572 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
1576 memset((char *) &r, 0, sizeof(r));
1579 jiffies_to_timeval(p->utime, &r.ru_utime);
1580 jiffies_to_timeval(p->stime, &r.ru_stime);
1581 r.ru_nvcsw = p->nvcsw;
1582 r.ru_nivcsw = p->nivcsw;
1583 r.ru_minflt = p->min_flt;
1584 r.ru_majflt = p->maj_flt;
1586 case RUSAGE_CHILDREN:
1587 jiffies_to_timeval(p->cutime, &r.ru_utime);
1588 jiffies_to_timeval(p->cstime, &r.ru_stime);
1589 r.ru_nvcsw = p->cnvcsw;
1590 r.ru_nivcsw = p->cnivcsw;
1591 r.ru_minflt = p->cmin_flt;
1592 r.ru_majflt = p->cmaj_flt;
1595 jiffies_to_timeval(p->utime + p->cutime, &r.ru_utime);
1596 jiffies_to_timeval(p->stime + p->cstime, &r.ru_stime);
1597 r.ru_nvcsw = p->nvcsw + p->cnvcsw;
1598 r.ru_nivcsw = p->nivcsw + p->cnivcsw;
1599 r.ru_minflt = p->min_flt + p->cmin_flt;
1600 r.ru_majflt = p->maj_flt + p->cmaj_flt;
1603 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1606 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
1608 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
1610 return getrusage(current, who, ru);
1613 asmlinkage long sys_umask(int mask)
1615 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1619 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
1620 unsigned long arg4, unsigned long arg5)
1625 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
1630 case PR_SET_PDEATHSIG:
1632 if (sig < 0 || sig > _NSIG) {
1636 current->pdeath_signal = sig;
1638 case PR_GET_PDEATHSIG:
1639 error = put_user(current->pdeath_signal, (int __user *)arg2);
1641 case PR_GET_DUMPABLE:
1642 if (current->mm->dumpable)
1645 case PR_SET_DUMPABLE:
1646 if (arg2 != 0 && arg2 != 1) {
1650 current->mm->dumpable = arg2;
1653 case PR_SET_UNALIGN:
1654 error = SET_UNALIGN_CTL(current, arg2);
1656 case PR_GET_UNALIGN:
1657 error = GET_UNALIGN_CTL(current, arg2);
1660 error = SET_FPEMU_CTL(current, arg2);
1663 error = GET_FPEMU_CTL(current, arg2);
1666 error = SET_FPEXC_CTL(current, arg2);
1669 error = GET_FPEXC_CTL(current, arg2);
1672 error = PR_TIMING_STATISTICAL;
1675 if (arg2 == PR_TIMING_STATISTICAL)
1681 case PR_GET_KEEPCAPS:
1682 if (current->keep_capabilities)
1685 case PR_SET_KEEPCAPS:
1686 if (arg2 != 0 && arg2 != 1) {
1690 current->keep_capabilities = arg2;