Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.63.2.8
      1  1.63.2.8   nathanw /*	$NetBSD: kern_prot.c,v 1.63.2.8 2002/08/27 23:47:25 nathanw Exp $	*/
      2      1.16       cgd 
      3      1.14       cgd /*
      4      1.15       cgd  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
      5      1.15       cgd  *	The Regents of the University of California.  All rights reserved.
      6      1.14       cgd  * (c) UNIX System Laboratories, Inc.
      7      1.14       cgd  * All or some portions of this file are derived from material licensed
      8      1.14       cgd  * to the University of California by American Telephone and Telegraph
      9      1.14       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10      1.14       cgd  * the permission of UNIX System Laboratories, Inc.
     11      1.14       cgd  *
     12      1.14       cgd  * Redistribution and use in source and binary forms, with or without
     13      1.14       cgd  * modification, are permitted provided that the following conditions
     14      1.14       cgd  * are met:
     15      1.14       cgd  * 1. Redistributions of source code must retain the above copyright
     16      1.14       cgd  *    notice, this list of conditions and the following disclaimer.
     17      1.14       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18      1.14       cgd  *    notice, this list of conditions and the following disclaimer in the
     19      1.14       cgd  *    documentation and/or other materials provided with the distribution.
     20      1.14       cgd  * 3. All advertising materials mentioning features or use of this software
     21      1.14       cgd  *    must display the following acknowledgement:
     22      1.14       cgd  *	This product includes software developed by the University of
     23      1.14       cgd  *	California, Berkeley and its contributors.
     24      1.14       cgd  * 4. Neither the name of the University nor the names of its contributors
     25      1.14       cgd  *    may be used to endorse or promote products derived from this software
     26      1.14       cgd  *    without specific prior written permission.
     27      1.14       cgd  *
     28      1.14       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29      1.14       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30      1.14       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31      1.14       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32      1.14       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33      1.14       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34      1.14       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35      1.14       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36      1.14       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37      1.14       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38      1.14       cgd  * SUCH DAMAGE.
     39      1.14       cgd  *
     40      1.44      fvdl  *	@(#)kern_prot.c	8.9 (Berkeley) 2/14/95
     41      1.14       cgd  */
     42      1.14       cgd 
     43      1.14       cgd /*
     44      1.14       cgd  * System calls related to processes and protection
     45      1.14       cgd  */
     46  1.63.2.3   nathanw 
     47  1.63.2.3   nathanw #include <sys/cdefs.h>
     48  1.63.2.8   nathanw __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.63.2.8 2002/08/27 23:47:25 nathanw Exp $");
     49      1.45   thorpej 
     50      1.51  christos #include "opt_compat_43.h"
     51      1.14       cgd 
     52      1.14       cgd #include <sys/param.h>
     53      1.14       cgd #include <sys/acct.h>
     54      1.14       cgd #include <sys/systm.h>
     55      1.14       cgd #include <sys/ucred.h>
     56      1.14       cgd #include <sys/proc.h>
     57      1.14       cgd #include <sys/timeb.h>
     58      1.14       cgd #include <sys/times.h>
     59      1.14       cgd #include <sys/malloc.h>
     60      1.14       cgd 
     61      1.19       cgd #include <sys/mount.h>
     62  1.63.2.6   nathanw #include <sys/sa.h>
     63      1.19       cgd #include <sys/syscallargs.h>
     64      1.63   mycroft 
     65  1.63.2.1   nathanw int	sys_getpid(struct lwp *, void *, register_t *);
     66  1.63.2.1   nathanw int	sys_getpid_with_ppid(struct lwp *, void *, register_t *);
     67  1.63.2.1   nathanw int	sys_getuid(struct lwp *, void *, register_t *);
     68  1.63.2.1   nathanw int	sys_getuid_with_euid(struct lwp *, void *, register_t *);
     69  1.63.2.1   nathanw int	sys_getgid(struct lwp *, void *, register_t *);
     70  1.63.2.1   nathanw int	sys_getgid_with_egid(struct lwp *, void *, register_t *);
     71      1.32  christos 
     72      1.14       cgd /* ARGSUSED */
     73      1.32  christos int
     74  1.63.2.1   nathanw sys_getpid(l, v, retval)
     75  1.63.2.1   nathanw 	struct lwp *l;
     76      1.30   mycroft 	void *v;
     77      1.19       cgd 	register_t *retval;
     78      1.14       cgd {
     79  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
     80      1.14       cgd 
     81      1.14       cgd 	*retval = p->p_pid;
     82      1.62   mycroft 	return (0);
     83      1.62   mycroft }
     84      1.62   mycroft 
     85      1.62   mycroft /* ARGSUSED */
     86      1.62   mycroft int
     87  1.63.2.1   nathanw sys_getpid_with_ppid(l, v, retval)
     88  1.63.2.1   nathanw 	struct lwp *l;
     89      1.62   mycroft 	void *v;
     90      1.62   mycroft 	register_t *retval;
     91      1.62   mycroft {
     92  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
     93      1.62   mycroft 
     94      1.62   mycroft 	retval[0] = p->p_pid;
     95      1.62   mycroft 	retval[1] = p->p_pptr->p_pid;
     96      1.14       cgd 	return (0);
     97      1.14       cgd }
     98      1.14       cgd 
     99      1.14       cgd /* ARGSUSED */
    100      1.32  christos int
    101  1.63.2.1   nathanw sys_getppid(l, v, retval)
    102  1.63.2.1   nathanw 	struct lwp *l;
    103      1.30   mycroft 	void *v;
    104      1.19       cgd 	register_t *retval;
    105      1.14       cgd {
    106  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    107      1.14       cgd 
    108      1.14       cgd 	*retval = p->p_pptr->p_pid;
    109      1.14       cgd 	return (0);
    110      1.14       cgd }
    111      1.14       cgd 
    112      1.14       cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
    113      1.32  christos int
    114  1.63.2.1   nathanw sys_getpgrp(l, v, retval)
    115  1.63.2.1   nathanw 	struct lwp *l;
    116      1.30   mycroft 	void *v;
    117      1.19       cgd 	register_t *retval;
    118      1.14       cgd {
    119  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    120      1.14       cgd 
    121      1.14       cgd 	*retval = p->p_pgrp->pg_id;
    122      1.14       cgd 	return (0);
    123      1.43   thorpej }
    124      1.43   thorpej 
    125      1.43   thorpej /*
    126      1.43   thorpej  * Return the process group ID of the session leader (session ID)
    127      1.43   thorpej  * for the specified process.
    128      1.43   thorpej  */
    129      1.43   thorpej int
    130  1.63.2.1   nathanw sys_getsid(l, v, retval)
    131  1.63.2.1   nathanw 	struct lwp *l;
    132      1.43   thorpej 	void *v;
    133      1.43   thorpej 	register_t *retval;
    134      1.43   thorpej {
    135      1.43   thorpej 	struct sys_getsid_args /* {
    136      1.43   thorpej 		syscalldarg(pid_t) pid;
    137      1.43   thorpej 	} */ *uap = v;
    138  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    139      1.43   thorpej 
    140      1.43   thorpej 	if (SCARG(uap, pid) == 0)
    141      1.43   thorpej 		goto found;
    142      1.43   thorpej 	if ((p = pfind(SCARG(uap, pid))) == 0)
    143      1.43   thorpej 		return (ESRCH);
    144      1.43   thorpej found:
    145      1.43   thorpej 	*retval = p->p_session->s_sid;
    146      1.60  christos 	return (0);
    147      1.36       mrg }
    148      1.36       mrg 
    149      1.36       mrg int
    150  1.63.2.1   nathanw sys_getpgid(l, v, retval)
    151  1.63.2.1   nathanw 	struct lwp *l;
    152      1.36       mrg 	void *v;
    153      1.36       mrg 	register_t *retval;
    154      1.36       mrg {
    155      1.56  augustss 	struct sys_getpgid_args /* {
    156      1.36       mrg 		syscallarg(pid_t) pid;
    157      1.36       mrg 	} */ *uap = v;
    158  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    159      1.36       mrg 
    160      1.36       mrg 	if (SCARG(uap, pid) == 0)
    161      1.36       mrg 		goto found;
    162      1.36       mrg 	if ((p = pfind(SCARG(uap, pid))) == 0)
    163      1.36       mrg 		return (ESRCH);
    164      1.36       mrg found:
    165      1.36       mrg 	*retval = p->p_pgid;
    166      1.60  christos 	return (0);
    167      1.14       cgd }
    168      1.14       cgd 
    169      1.14       cgd /* ARGSUSED */
    170      1.32  christos int
    171  1.63.2.1   nathanw sys_getuid(l, v, retval)
    172  1.63.2.1   nathanw 	struct lwp *l;
    173      1.30   mycroft 	void *v;
    174      1.19       cgd 	register_t *retval;
    175      1.14       cgd {
    176  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    177      1.14       cgd 
    178      1.14       cgd 	*retval = p->p_cred->p_ruid;
    179      1.62   mycroft 	return (0);
    180      1.62   mycroft }
    181      1.62   mycroft 
    182      1.62   mycroft /* ARGSUSED */
    183      1.62   mycroft int
    184  1.63.2.1   nathanw sys_getuid_with_euid(l, v, retval)
    185  1.63.2.1   nathanw 	struct lwp *l;
    186      1.62   mycroft 	void *v;
    187      1.62   mycroft 	register_t *retval;
    188      1.62   mycroft {
    189  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    190      1.62   mycroft 
    191      1.62   mycroft 	retval[0] = p->p_cred->p_ruid;
    192      1.62   mycroft 	retval[1] = p->p_ucred->cr_uid;
    193      1.14       cgd 	return (0);
    194      1.14       cgd }
    195      1.14       cgd 
    196      1.14       cgd /* ARGSUSED */
    197      1.32  christos int
    198  1.63.2.1   nathanw sys_geteuid(l, v, retval)
    199  1.63.2.1   nathanw 	struct lwp *l;
    200      1.30   mycroft 	void *v;
    201      1.19       cgd 	register_t *retval;
    202      1.14       cgd {
    203  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    204      1.14       cgd 
    205      1.14       cgd 	*retval = p->p_ucred->cr_uid;
    206      1.14       cgd 	return (0);
    207      1.14       cgd }
    208      1.14       cgd 
    209      1.14       cgd /* ARGSUSED */
    210      1.32  christos int
    211  1.63.2.1   nathanw sys_getgid(l, v, retval)
    212  1.63.2.1   nathanw 	struct lwp *l;
    213      1.30   mycroft 	void *v;
    214      1.19       cgd 	register_t *retval;
    215      1.14       cgd {
    216  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    217      1.14       cgd 
    218      1.14       cgd 	*retval = p->p_cred->p_rgid;
    219      1.62   mycroft 	return (0);
    220      1.62   mycroft }
    221      1.62   mycroft 
    222      1.62   mycroft /* ARGSUSED */
    223      1.62   mycroft int
    224  1.63.2.1   nathanw sys_getgid_with_egid(l, v, retval)
    225  1.63.2.1   nathanw 	struct lwp *l;
    226      1.62   mycroft 	void *v;
    227      1.62   mycroft 	register_t *retval;
    228      1.62   mycroft {
    229  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    230      1.62   mycroft 
    231      1.62   mycroft 	retval[0] = p->p_cred->p_rgid;
    232      1.62   mycroft 	retval[1] = p->p_ucred->cr_gid;
    233      1.14       cgd 	return (0);
    234      1.14       cgd }
    235      1.14       cgd 
    236      1.14       cgd /*
    237      1.14       cgd  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    238      1.14       cgd  * via getgroups.  This syscall exists because it is somewhat painful to do
    239      1.14       cgd  * correctly in a library function.
    240      1.14       cgd  */
    241      1.14       cgd /* ARGSUSED */
    242      1.32  christos int
    243  1.63.2.1   nathanw sys_getegid(l, v, retval)
    244  1.63.2.1   nathanw 	struct lwp *l;
    245      1.30   mycroft 	void *v;
    246      1.19       cgd 	register_t *retval;
    247      1.14       cgd {
    248  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    249      1.27       jtc 	*retval = p->p_ucred->cr_gid;
    250      1.14       cgd 	return (0);
    251      1.14       cgd }
    252      1.14       cgd 
    253      1.32  christos int
    254  1.63.2.1   nathanw sys_getgroups(l, v, retval)
    255  1.63.2.1   nathanw 	struct lwp *l;
    256      1.29   thorpej 	void *v;
    257      1.29   thorpej 	register_t *retval;
    258      1.29   thorpej {
    259      1.56  augustss 	struct sys_getgroups_args /* {
    260      1.41   thorpej 		syscallarg(int) gidsetsize;
    261      1.19       cgd 		syscallarg(gid_t *) gidset;
    262      1.29   thorpej 	} */ *uap = v;
    263  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    264      1.56  augustss 	struct pcred *pc = p->p_cred;
    265  1.63.2.8   nathanw 	u_int ngrp;
    266      1.14       cgd 	int error;
    267      1.14       cgd 
    268  1.63.2.8   nathanw 	if (SCARG(uap, gidsetsize) == 0) {
    269      1.14       cgd 		*retval = pc->pc_ucred->cr_ngroups;
    270      1.14       cgd 		return (0);
    271  1.63.2.8   nathanw 	} else if (SCARG(uap, gidsetsize) < 0)
    272  1.63.2.8   nathanw 		return (EINVAL);
    273  1.63.2.8   nathanw 	ngrp = SCARG(uap, gidsetsize);
    274      1.14       cgd 	if (ngrp < pc->pc_ucred->cr_ngroups)
    275      1.14       cgd 		return (EINVAL);
    276      1.14       cgd 	ngrp = pc->pc_ucred->cr_ngroups;
    277      1.32  christos 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    278      1.32  christos 			(caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    279      1.32  christos 	if (error)
    280      1.14       cgd 		return (error);
    281      1.14       cgd 	*retval = ngrp;
    282      1.14       cgd 	return (0);
    283      1.14       cgd }
    284      1.14       cgd 
    285      1.14       cgd /* ARGSUSED */
    286      1.32  christos int
    287  1.63.2.1   nathanw sys_setsid(l, v, retval)
    288  1.63.2.1   nathanw 	struct lwp *l;
    289      1.30   mycroft 	void *v;
    290      1.19       cgd 	register_t *retval;
    291      1.14       cgd {
    292  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    293      1.14       cgd 
    294      1.14       cgd 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    295      1.14       cgd 		return (EPERM);
    296      1.14       cgd 	} else {
    297      1.15       cgd 		(void)enterpgrp(p, p->p_pid, 1);
    298      1.14       cgd 		*retval = p->p_pid;
    299      1.14       cgd 		return (0);
    300      1.14       cgd 	}
    301      1.14       cgd }
    302      1.14       cgd 
    303      1.14       cgd /*
    304      1.14       cgd  * set process group (setpgid/old setpgrp)
    305      1.14       cgd  *
    306      1.14       cgd  * caller does setpgid(targpid, targpgid)
    307      1.14       cgd  *
    308      1.39     mikel  * pgid must be in valid range (EINVAL)
    309      1.14       cgd  * pid must be caller or child of caller (ESRCH)
    310      1.14       cgd  * if a child
    311      1.14       cgd  *	pid must be in same session (EPERM)
    312      1.14       cgd  *	pid can't have done an exec (EACCES)
    313      1.14       cgd  * if pgid != pid
    314      1.14       cgd  * 	there must exist some pid in same session having pgid (EPERM)
    315      1.14       cgd  * pid must not be session leader (EPERM)
    316      1.14       cgd  */
    317      1.14       cgd /* ARGSUSED */
    318      1.32  christos int
    319  1.63.2.1   nathanw sys_setpgid(l, v, retval)
    320  1.63.2.1   nathanw 	struct lwp *l;
    321      1.29   thorpej 	void *v;
    322      1.29   thorpej 	register_t *retval;
    323      1.29   thorpej {
    324      1.56  augustss 	struct sys_setpgid_args /* {
    325      1.19       cgd 		syscallarg(int) pid;
    326      1.19       cgd 		syscallarg(int) pgid;
    327      1.29   thorpej 	} */ *uap = v;
    328  1.63.2.1   nathanw 	struct proc *curp = l->l_proc;
    329      1.56  augustss 	struct proc *targp;			/* target process */
    330      1.56  augustss 	struct pgrp *pgrp;			/* target pgrp */
    331      1.14       cgd 
    332      1.39     mikel 	if (SCARG(uap, pgid) < 0)
    333      1.39     mikel 		return (EINVAL);
    334      1.14       cgd 
    335      1.19       cgd 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    336      1.58  sommerfe 		if ((targp = pfind(SCARG(uap, pid))) == 0
    337      1.58  sommerfe 		    || !inferior(targp, curp))
    338      1.14       cgd 			return (ESRCH);
    339      1.14       cgd 		if (targp->p_session != curp->p_session)
    340      1.14       cgd 			return (EPERM);
    341      1.15       cgd 		if (targp->p_flag & P_EXEC)
    342      1.14       cgd 			return (EACCES);
    343      1.14       cgd 	} else
    344      1.14       cgd 		targp = curp;
    345      1.14       cgd 	if (SESS_LEADER(targp))
    346      1.14       cgd 		return (EPERM);
    347      1.19       cgd 	if (SCARG(uap, pgid) == 0)
    348      1.19       cgd 		SCARG(uap, pgid) = targp->p_pid;
    349      1.19       cgd 	else if (SCARG(uap, pgid) != targp->p_pid)
    350      1.19       cgd 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    351      1.14       cgd 	            pgrp->pg_session != curp->p_session)
    352      1.14       cgd 			return (EPERM);
    353      1.19       cgd 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    354      1.14       cgd }
    355      1.14       cgd 
    356      1.14       cgd /* ARGSUSED */
    357      1.32  christos int
    358  1.63.2.1   nathanw sys_setuid(l, v, retval)
    359  1.63.2.1   nathanw 	struct lwp *l;
    360      1.29   thorpej 	void *v;
    361      1.29   thorpej 	register_t *retval;
    362      1.29   thorpej {
    363      1.30   mycroft 	struct sys_setuid_args /* {
    364      1.19       cgd 		syscallarg(uid_t) uid;
    365      1.29   thorpej 	} */ *uap = v;
    366  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    367      1.56  augustss 	struct pcred *pc = p->p_cred;
    368      1.56  augustss 	uid_t uid;
    369      1.14       cgd 	int error;
    370      1.14       cgd 
    371      1.19       cgd 	uid = SCARG(uap, uid);
    372      1.14       cgd 	if (uid != pc->p_ruid &&
    373      1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    374      1.14       cgd 		return (error);
    375      1.14       cgd 	/*
    376      1.60  christos 	 * Check if we are all set, and this is a no-op.
    377      1.59  christos 	 */
    378      1.59  christos 	if (pc->p_ruid == uid && pc->p_svuid == uid &&
    379      1.59  christos 	    pc->pc_ucred->cr_uid == uid)
    380      1.60  christos 		return (0);
    381      1.59  christos 	/*
    382      1.15       cgd 	 * Everything's okay, do it.
    383      1.15       cgd 	 * Transfer proc count to new user.
    384      1.15       cgd 	 * Copy credentials so other references do not see our changes.
    385      1.14       cgd 	 */
    386      1.15       cgd 	(void)chgproccnt(pc->p_ruid, -1);
    387      1.15       cgd 	(void)chgproccnt(uid, 1);
    388      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    389      1.14       cgd 	pc->pc_ucred->cr_uid = uid;
    390      1.14       cgd 	pc->p_ruid = uid;
    391      1.14       cgd 	pc->p_svuid = uid;
    392      1.55    bouyer 	p_sugid(p);
    393      1.14       cgd 	return (0);
    394      1.14       cgd }
    395      1.14       cgd 
    396      1.14       cgd /* ARGSUSED */
    397      1.32  christos int
    398  1.63.2.1   nathanw sys_seteuid(l, v, retval)
    399  1.63.2.1   nathanw 	struct lwp *l;
    400      1.29   thorpej 	void *v;
    401      1.29   thorpej 	register_t *retval;
    402      1.29   thorpej {
    403      1.30   mycroft 	struct sys_seteuid_args /* {
    404      1.19       cgd 		syscallarg(uid_t) euid;
    405      1.29   thorpej 	} */ *uap = v;
    406  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    407      1.56  augustss 	struct pcred *pc = p->p_cred;
    408      1.56  augustss 	uid_t euid;
    409      1.14       cgd 	int error;
    410      1.14       cgd 
    411      1.19       cgd 	euid = SCARG(uap, euid);
    412      1.14       cgd 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    413      1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    414      1.14       cgd 		return (error);
    415      1.14       cgd 	/*
    416      1.60  christos 	 * Check if we are all set, and this is a no-op.
    417      1.59  christos 	 */
    418      1.59  christos 	if (pc->pc_ucred->cr_uid == euid)
    419      1.60  christos 		return (0);
    420      1.59  christos 
    421      1.59  christos 	/*
    422      1.14       cgd 	 * Everything's okay, do it.  Copy credentials so other references do
    423      1.14       cgd 	 * not see our changes.
    424      1.14       cgd 	 */
    425      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    426      1.14       cgd 	pc->pc_ucred->cr_uid = euid;
    427      1.55    bouyer 	p_sugid(p);
    428      1.14       cgd 	return (0);
    429      1.14       cgd }
    430      1.14       cgd 
    431      1.35   mycroft int
    432  1.63.2.1   nathanw sys_setreuid(l, v, retval)
    433  1.63.2.1   nathanw 	struct lwp *l;
    434      1.35   mycroft 	void *v;
    435      1.35   mycroft 	register_t *retval;
    436      1.35   mycroft {
    437      1.35   mycroft 	struct sys_setreuid_args /* {
    438      1.35   mycroft 		syscallarg(uid_t) ruid;
    439      1.35   mycroft 		syscallarg(uid_t) euid;
    440      1.35   mycroft 	} */ *uap = v;
    441  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    442      1.56  augustss 	struct pcred *pc = p->p_cred;
    443      1.56  augustss 	uid_t ruid, euid;
    444      1.59  christos 	int error, changed = 0;
    445      1.35   mycroft 
    446      1.35   mycroft 	ruid = SCARG(uap, ruid);
    447      1.35   mycroft 	euid = SCARG(uap, euid);
    448      1.35   mycroft 
    449      1.35   mycroft 	if (ruid != (uid_t)-1 &&
    450      1.35   mycroft 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    451      1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    452      1.35   mycroft 		return (error);
    453      1.35   mycroft 
    454      1.35   mycroft 	if (euid != (uid_t)-1 &&
    455      1.35   mycroft 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    456      1.35   mycroft 	    euid != pc->p_svuid &&
    457      1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    458      1.35   mycroft 		return (error);
    459      1.35   mycroft 
    460      1.59  christos 	if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
    461      1.35   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    462      1.35   mycroft 		pc->pc_ucred->cr_uid = euid;
    463      1.59  christos 		changed++;
    464      1.35   mycroft 	}
    465      1.35   mycroft 
    466      1.59  christos 	if (ruid != (uid_t)-1 &&
    467      1.59  christos 	    (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
    468      1.35   mycroft 		(void)chgproccnt(pc->p_ruid, -1);
    469      1.35   mycroft 		(void)chgproccnt(ruid, 1);
    470      1.35   mycroft 		pc->p_ruid = ruid;
    471      1.35   mycroft 		pc->p_svuid = pc->pc_ucred->cr_uid;
    472      1.59  christos 		changed++;
    473      1.35   mycroft 	}
    474      1.35   mycroft 
    475      1.59  christos 	if (changed)
    476      1.55    bouyer 		p_sugid(p);
    477      1.35   mycroft 	return (0);
    478      1.35   mycroft }
    479      1.35   mycroft 
    480      1.14       cgd /* ARGSUSED */
    481      1.32  christos int
    482  1.63.2.1   nathanw sys_setgid(l, v, retval)
    483  1.63.2.1   nathanw 	struct lwp *l;
    484      1.29   thorpej 	void *v;
    485      1.29   thorpej 	register_t *retval;
    486      1.29   thorpej {
    487      1.30   mycroft 	struct sys_setgid_args /* {
    488      1.19       cgd 		syscallarg(gid_t) gid;
    489      1.29   thorpej 	} */ *uap = v;
    490  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    491      1.56  augustss 	struct pcred *pc = p->p_cred;
    492      1.56  augustss 	gid_t gid;
    493      1.14       cgd 	int error;
    494      1.14       cgd 
    495      1.19       cgd 	gid = SCARG(uap, gid);
    496      1.34   mycroft 	if (gid != pc->p_rgid &&
    497      1.34   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    498      1.14       cgd 		return (error);
    499      1.59  christos 	/*
    500      1.60  christos 	 * Check if we are all set, and this is a no-op.
    501      1.59  christos 	 */
    502      1.59  christos 	if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
    503      1.59  christos 	    pc->p_svgid == gid)
    504      1.60  christos 		return (0);
    505      1.59  christos 
    506      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    507      1.27       jtc 	pc->pc_ucred->cr_gid = gid;
    508      1.14       cgd 	pc->p_rgid = gid;
    509      1.34   mycroft 	pc->p_svgid = gid;
    510      1.55    bouyer 	p_sugid(p);
    511      1.14       cgd 	return (0);
    512      1.14       cgd }
    513      1.14       cgd 
    514      1.14       cgd /* ARGSUSED */
    515      1.32  christos int
    516  1.63.2.1   nathanw sys_setegid(l, v, retval)
    517  1.63.2.1   nathanw 	struct lwp *l;
    518      1.29   thorpej 	void *v;
    519      1.29   thorpej 	register_t *retval;
    520      1.29   thorpej {
    521      1.30   mycroft 	struct sys_setegid_args /* {
    522      1.19       cgd 		syscallarg(gid_t) egid;
    523      1.29   thorpej 	} */ *uap = v;
    524  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    525      1.56  augustss 	struct pcred *pc = p->p_cred;
    526      1.56  augustss 	gid_t egid;
    527      1.14       cgd 	int error;
    528      1.14       cgd 
    529      1.19       cgd 	egid = SCARG(uap, egid);
    530      1.14       cgd 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    531      1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    532      1.14       cgd 		return (error);
    533      1.59  christos 	/*
    534      1.60  christos 	 * Check if we are all set, and this is a no-op.
    535      1.59  christos 	 */
    536      1.59  christos 	if (pc->pc_ucred->cr_gid == egid)
    537      1.60  christos 		return (0);
    538      1.59  christos 
    539      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    540      1.27       jtc 	pc->pc_ucred->cr_gid = egid;
    541      1.55    bouyer 	p_sugid(p);
    542      1.35   mycroft 	return (0);
    543      1.35   mycroft }
    544      1.35   mycroft 
    545      1.35   mycroft int
    546  1.63.2.1   nathanw sys_setregid(l, v, retval)
    547  1.63.2.1   nathanw 	struct lwp *l;
    548      1.35   mycroft 	void *v;
    549      1.35   mycroft 	register_t *retval;
    550      1.35   mycroft {
    551      1.35   mycroft 	struct sys_setregid_args /* {
    552      1.35   mycroft 		syscallarg(gid_t) rgid;
    553      1.35   mycroft 		syscallarg(gid_t) egid;
    554      1.35   mycroft 	} */ *uap = v;
    555  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    556      1.56  augustss 	struct pcred *pc = p->p_cred;
    557      1.56  augustss 	gid_t rgid, egid;
    558      1.59  christos 	int error, changed = 0;
    559      1.35   mycroft 
    560      1.35   mycroft 	rgid = SCARG(uap, rgid);
    561      1.35   mycroft 	egid = SCARG(uap, egid);
    562      1.35   mycroft 
    563      1.35   mycroft 	if (rgid != (gid_t)-1 &&
    564      1.35   mycroft 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    565      1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    566      1.35   mycroft 		return (error);
    567      1.35   mycroft 
    568      1.35   mycroft 	if (egid != (gid_t)-1 &&
    569      1.35   mycroft 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    570      1.35   mycroft 	    egid != pc->p_svgid &&
    571      1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    572      1.35   mycroft 		return (error);
    573      1.35   mycroft 
    574      1.59  christos 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
    575      1.35   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    576      1.35   mycroft 		pc->pc_ucred->cr_gid = egid;
    577      1.59  christos 		changed++;
    578      1.35   mycroft 	}
    579      1.35   mycroft 
    580      1.59  christos 	if (rgid != (gid_t)-1 &&
    581      1.59  christos 	    (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
    582      1.35   mycroft 		pc->p_rgid = rgid;
    583      1.35   mycroft 		pc->p_svgid = pc->pc_ucred->cr_gid;
    584      1.59  christos 		changed++;
    585      1.35   mycroft 	}
    586      1.35   mycroft 
    587      1.59  christos 	if (changed)
    588      1.55    bouyer 		p_sugid(p);
    589      1.14       cgd 	return (0);
    590      1.57   minoura }
    591      1.57   minoura 
    592      1.57   minoura int
    593  1.63.2.1   nathanw sys_issetugid(l, v, retval)
    594  1.63.2.1   nathanw 	struct lwp *l;
    595      1.57   minoura 	void *v;
    596      1.57   minoura 	register_t *retval;
    597      1.57   minoura {
    598  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    599      1.57   minoura 	/*
    600      1.57   minoura 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    601      1.57   minoura 	 * we use P_SUGID because we consider changing the owners as
    602      1.57   minoura 	 * "tainting" as well.
    603      1.57   minoura 	 * This is significant for procs that start as root and "become"
    604      1.57   minoura 	 * a user without an exec - programs cannot know *everything*
    605      1.57   minoura 	 * that libc *might* have put in their data segment.
    606      1.57   minoura 	 */
    607      1.57   minoura 	*retval = (p->p_flag & P_SUGID) != 0;
    608      1.60  christos 	return (0);
    609      1.14       cgd }
    610      1.14       cgd 
    611      1.15       cgd /* ARGSUSED */
    612      1.32  christos int
    613  1.63.2.1   nathanw sys_setgroups(l, v, retval)
    614  1.63.2.1   nathanw 	struct lwp *l;
    615      1.29   thorpej 	void *v;
    616      1.29   thorpej 	register_t *retval;
    617      1.29   thorpej {
    618      1.30   mycroft 	struct sys_setgroups_args /* {
    619      1.41   thorpej 		syscallarg(int) gidsetsize;
    620      1.38       cgd 		syscallarg(const gid_t *) gidset;
    621      1.29   thorpej 	} */ *uap = v;
    622  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    623      1.56  augustss 	struct pcred *pc = p->p_cred;
    624      1.56  augustss 	int ngrp;
    625      1.15       cgd 	int error;
    626      1.59  christos 	gid_t grp[NGROUPS];
    627      1.59  christos 	size_t grsize;
    628      1.15       cgd 
    629      1.32  christos 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    630      1.15       cgd 		return (error);
    631      1.59  christos 
    632      1.19       cgd 	ngrp = SCARG(uap, gidsetsize);
    633      1.42   mycroft 	if ((u_int)ngrp > NGROUPS)
    634      1.15       cgd 		return (EINVAL);
    635      1.59  christos 
    636      1.59  christos 	grsize = ngrp * sizeof(gid_t);
    637      1.59  christos 	error = copyin(SCARG(uap, gidset), grp, grsize);
    638      1.32  christos 	if (error)
    639      1.15       cgd 		return (error);
    640      1.59  christos 	/*
    641      1.60  christos 	 * Check if this is a no-op.
    642      1.59  christos 	 */
    643  1.63.2.8   nathanw 	if (pc->pc_ucred->cr_ngroups == (u_int) ngrp &&
    644      1.59  christos 	    memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
    645      1.60  christos 		return (0);
    646      1.59  christos 
    647      1.59  christos 	pc->pc_ucred = crcopy(pc->pc_ucred);
    648      1.59  christos 	(void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
    649      1.15       cgd 	pc->pc_ucred->cr_ngroups = ngrp;
    650      1.55    bouyer 	p_sugid(p);
    651      1.15       cgd 	return (0);
    652      1.15       cgd }
    653      1.14       cgd 
    654      1.14       cgd /*
    655      1.14       cgd  * Check if gid is a member of the group set.
    656      1.14       cgd  */
    657      1.32  christos int
    658      1.14       cgd groupmember(gid, cred)
    659      1.14       cgd 	gid_t gid;
    660      1.56  augustss 	struct ucred *cred;
    661      1.14       cgd {
    662      1.56  augustss 	gid_t *gp;
    663      1.14       cgd 	gid_t *egp;
    664      1.14       cgd 
    665      1.14       cgd 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    666      1.14       cgd 	for (gp = cred->cr_groups; gp < egp; gp++)
    667      1.14       cgd 		if (*gp == gid)
    668      1.14       cgd 			return (1);
    669      1.14       cgd 	return (0);
    670      1.14       cgd }
    671      1.14       cgd 
    672      1.14       cgd /*
    673      1.14       cgd  * Test whether the specified credentials imply "super-user"
    674      1.14       cgd  * privilege; if so, and we have accounting info, set the flag
    675      1.14       cgd  * indicating use of super-powers.
    676      1.14       cgd  * Returns 0 or error.
    677      1.14       cgd  */
    678      1.32  christos int
    679      1.14       cgd suser(cred, acflag)
    680      1.14       cgd 	struct ucred *cred;
    681      1.14       cgd 	u_short *acflag;
    682      1.14       cgd {
    683      1.14       cgd 	if (cred->cr_uid == 0) {
    684      1.14       cgd 		if (acflag)
    685      1.14       cgd 			*acflag |= ASU;
    686      1.14       cgd 		return (0);
    687      1.14       cgd 	}
    688      1.14       cgd 	return (EPERM);
    689      1.14       cgd }
    690      1.14       cgd 
    691      1.14       cgd /*
    692      1.14       cgd  * Allocate a zeroed cred structure.
    693      1.14       cgd  */
    694      1.14       cgd struct ucred *
    695      1.14       cgd crget()
    696      1.14       cgd {
    697      1.56  augustss 	struct ucred *cr;
    698      1.14       cgd 
    699      1.14       cgd 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    700      1.49     perry 	memset((caddr_t)cr, 0, sizeof(*cr));
    701      1.14       cgd 	cr->cr_ref = 1;
    702      1.14       cgd 	return (cr);
    703      1.14       cgd }
    704      1.14       cgd 
    705      1.14       cgd /*
    706      1.14       cgd  * Free a cred structure.
    707      1.14       cgd  * Throws away space when ref count gets to 0.
    708      1.14       cgd  */
    709      1.22       cgd void
    710      1.14       cgd crfree(cr)
    711      1.14       cgd 	struct ucred *cr;
    712      1.14       cgd {
    713      1.14       cgd 
    714      1.15       cgd 	if (--cr->cr_ref == 0)
    715      1.15       cgd 		FREE((caddr_t)cr, M_CRED);
    716      1.14       cgd }
    717      1.14       cgd 
    718      1.14       cgd /*
    719      1.14       cgd  * Copy cred structure to a new one and free the old one.
    720      1.14       cgd  */
    721      1.14       cgd struct ucred *
    722      1.14       cgd crcopy(cr)
    723      1.14       cgd 	struct ucred *cr;
    724      1.14       cgd {
    725      1.14       cgd 	struct ucred *newcr;
    726      1.14       cgd 
    727      1.14       cgd 	if (cr->cr_ref == 1)
    728      1.14       cgd 		return (cr);
    729      1.14       cgd 	newcr = crget();
    730      1.14       cgd 	*newcr = *cr;
    731      1.14       cgd 	crfree(cr);
    732      1.14       cgd 	newcr->cr_ref = 1;
    733      1.14       cgd 	return (newcr);
    734      1.14       cgd }
    735      1.14       cgd 
    736      1.14       cgd /*
    737      1.14       cgd  * Dup cred struct to a new held one.
    738      1.14       cgd  */
    739      1.14       cgd struct ucred *
    740      1.14       cgd crdup(cr)
    741      1.14       cgd 	struct ucred *cr;
    742      1.14       cgd {
    743      1.14       cgd 	struct ucred *newcr;
    744      1.14       cgd 
    745      1.14       cgd 	newcr = crget();
    746      1.14       cgd 	*newcr = *cr;
    747      1.14       cgd 	newcr->cr_ref = 1;
    748      1.14       cgd 	return (newcr);
    749  1.63.2.4   nathanw }
    750  1.63.2.4   nathanw 
    751  1.63.2.4   nathanw /*
    752  1.63.2.4   nathanw  * convert from userland credentials to kernel one
    753  1.63.2.4   nathanw  */
    754  1.63.2.4   nathanw void
    755  1.63.2.4   nathanw crcvt(uc, uuc)
    756  1.63.2.4   nathanw 	struct ucred *uc;
    757  1.63.2.4   nathanw 	const struct uucred *uuc;
    758  1.63.2.4   nathanw {
    759  1.63.2.4   nathanw 	uc->cr_ref = 0;
    760  1.63.2.4   nathanw 	uc->cr_uid = uuc->cr_uid;
    761  1.63.2.4   nathanw 	uc->cr_gid = uuc->cr_gid;
    762  1.63.2.4   nathanw 	uc->cr_ngroups = uuc->cr_ngroups;
    763  1.63.2.4   nathanw 	(void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
    764      1.14       cgd }
    765      1.14       cgd 
    766      1.14       cgd /*
    767      1.14       cgd  * Get login name, if available.
    768      1.14       cgd  */
    769      1.14       cgd /* ARGSUSED */
    770      1.32  christos int
    771  1.63.2.1   nathanw sys___getlogin(l, v, retval)
    772  1.63.2.1   nathanw 	struct lwp *l;
    773      1.29   thorpej 	void *v;
    774      1.29   thorpej 	register_t *retval;
    775      1.29   thorpej {
    776      1.37       jtc 	struct sys___getlogin_args /* {
    777      1.19       cgd 		syscallarg(char *) namebuf;
    778      1.53    kleink 		syscallarg(size_t) namelen;
    779      1.29   thorpej 	} */ *uap = v;
    780  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    781      1.14       cgd 
    782      1.48     perry 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    783      1.48     perry 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    784      1.14       cgd 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    785      1.19       cgd 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    786      1.14       cgd }
    787      1.14       cgd 
    788      1.14       cgd /*
    789      1.14       cgd  * Set login name.
    790      1.14       cgd  */
    791      1.14       cgd /* ARGSUSED */
    792      1.32  christos int
    793  1.63.2.5   nathanw sys___setlogin(l, v, retval)
    794  1.63.2.1   nathanw 	struct lwp *l;
    795      1.29   thorpej 	void *v;
    796      1.29   thorpej 	register_t *retval;
    797      1.29   thorpej {
    798  1.63.2.5   nathanw 	struct sys___setlogin_args /* {
    799      1.38       cgd 		syscallarg(const char *) namebuf;
    800      1.29   thorpej 	} */ *uap = v;
    801  1.63.2.1   nathanw 	struct proc *p = l->l_proc;
    802      1.14       cgd 	int error;
    803      1.14       cgd 
    804      1.32  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    805      1.14       cgd 		return (error);
    806      1.38       cgd 	error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
    807      1.48     perry 	    sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
    808      1.14       cgd 	if (error == ENAMETOOLONG)
    809      1.14       cgd 		error = EINVAL;
    810      1.14       cgd 	return (error);
    811      1.14       cgd }
    812