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