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