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