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