Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.60
      1  1.60  christos /*	$NetBSD: kern_prot.c,v 1.60 2000/10/17 23:38:55 christos 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.60  christos 	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.60  christos 	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.60  christos 	 * Check if we are all set, and this is a no-op.
    330  1.59  christos 	 */
    331  1.59  christos 	if (pc->p_ruid == uid && pc->p_svuid == uid &&
    332  1.59  christos 	    pc->pc_ucred->cr_uid == uid)
    333  1.60  christos 		return (0);
    334  1.59  christos 	/*
    335  1.15       cgd 	 * Everything's okay, do it.
    336  1.15       cgd 	 * Transfer proc count to new user.
    337  1.15       cgd 	 * Copy credentials so other references do not see our changes.
    338  1.14       cgd 	 */
    339  1.15       cgd 	(void)chgproccnt(pc->p_ruid, -1);
    340  1.15       cgd 	(void)chgproccnt(uid, 1);
    341  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    342  1.14       cgd 	pc->pc_ucred->cr_uid = uid;
    343  1.14       cgd 	pc->p_ruid = uid;
    344  1.14       cgd 	pc->p_svuid = uid;
    345  1.55    bouyer 	p_sugid(p);
    346  1.14       cgd 	return (0);
    347  1.14       cgd }
    348  1.14       cgd 
    349  1.14       cgd /* ARGSUSED */
    350  1.32  christos int
    351  1.30   mycroft sys_seteuid(p, v, retval)
    352  1.14       cgd 	struct proc *p;
    353  1.29   thorpej 	void *v;
    354  1.29   thorpej 	register_t *retval;
    355  1.29   thorpej {
    356  1.30   mycroft 	struct sys_seteuid_args /* {
    357  1.19       cgd 		syscallarg(uid_t) euid;
    358  1.29   thorpej 	} */ *uap = v;
    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.35   mycroft sys_setreuid(p, v, retval)
    385  1.35   mycroft 	struct proc *p;
    386  1.35   mycroft 	void *v;
    387  1.35   mycroft 	register_t *retval;
    388  1.35   mycroft {
    389  1.35   mycroft 	struct sys_setreuid_args /* {
    390  1.35   mycroft 		syscallarg(uid_t) ruid;
    391  1.35   mycroft 		syscallarg(uid_t) euid;
    392  1.35   mycroft 	} */ *uap = v;
    393  1.56  augustss 	struct pcred *pc = p->p_cred;
    394  1.56  augustss 	uid_t ruid, euid;
    395  1.59  christos 	int error, changed = 0;
    396  1.35   mycroft 
    397  1.35   mycroft 	ruid = SCARG(uap, ruid);
    398  1.35   mycroft 	euid = SCARG(uap, euid);
    399  1.35   mycroft 
    400  1.35   mycroft 	if (ruid != (uid_t)-1 &&
    401  1.35   mycroft 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    402  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    403  1.35   mycroft 		return (error);
    404  1.35   mycroft 
    405  1.35   mycroft 	if (euid != (uid_t)-1 &&
    406  1.35   mycroft 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    407  1.35   mycroft 	    euid != pc->p_svuid &&
    408  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    409  1.35   mycroft 		return (error);
    410  1.35   mycroft 
    411  1.59  christos 	if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
    412  1.35   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    413  1.35   mycroft 		pc->pc_ucred->cr_uid = euid;
    414  1.59  christos 		changed++;
    415  1.35   mycroft 	}
    416  1.35   mycroft 
    417  1.59  christos 	if (ruid != (uid_t)-1 &&
    418  1.59  christos 	    (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
    419  1.35   mycroft 		(void)chgproccnt(pc->p_ruid, -1);
    420  1.35   mycroft 		(void)chgproccnt(ruid, 1);
    421  1.35   mycroft 		pc->p_ruid = ruid;
    422  1.35   mycroft 		pc->p_svuid = pc->pc_ucred->cr_uid;
    423  1.59  christos 		changed++;
    424  1.35   mycroft 	}
    425  1.35   mycroft 
    426  1.59  christos 	if (changed)
    427  1.55    bouyer 		p_sugid(p);
    428  1.35   mycroft 	return (0);
    429  1.35   mycroft }
    430  1.35   mycroft 
    431  1.14       cgd /* ARGSUSED */
    432  1.32  christos int
    433  1.30   mycroft sys_setgid(p, v, retval)
    434  1.14       cgd 	struct proc *p;
    435  1.29   thorpej 	void *v;
    436  1.29   thorpej 	register_t *retval;
    437  1.29   thorpej {
    438  1.30   mycroft 	struct sys_setgid_args /* {
    439  1.19       cgd 		syscallarg(gid_t) gid;
    440  1.29   thorpej 	} */ *uap = v;
    441  1.56  augustss 	struct pcred *pc = p->p_cred;
    442  1.56  augustss 	gid_t gid;
    443  1.14       cgd 	int error;
    444  1.14       cgd 
    445  1.19       cgd 	gid = SCARG(uap, gid);
    446  1.34   mycroft 	if (gid != pc->p_rgid &&
    447  1.34   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    448  1.14       cgd 		return (error);
    449  1.59  christos 	/*
    450  1.60  christos 	 * Check if we are all set, and this is a no-op.
    451  1.59  christos 	 */
    452  1.59  christos 	if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
    453  1.59  christos 	    pc->p_svgid == gid)
    454  1.60  christos 		return (0);
    455  1.59  christos 
    456  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    457  1.27       jtc 	pc->pc_ucred->cr_gid = gid;
    458  1.14       cgd 	pc->p_rgid = gid;
    459  1.34   mycroft 	pc->p_svgid = gid;
    460  1.55    bouyer 	p_sugid(p);
    461  1.14       cgd 	return (0);
    462  1.14       cgd }
    463  1.14       cgd 
    464  1.14       cgd /* ARGSUSED */
    465  1.32  christos int
    466  1.30   mycroft sys_setegid(p, v, retval)
    467  1.14       cgd 	struct proc *p;
    468  1.29   thorpej 	void *v;
    469  1.29   thorpej 	register_t *retval;
    470  1.29   thorpej {
    471  1.30   mycroft 	struct sys_setegid_args /* {
    472  1.19       cgd 		syscallarg(gid_t) egid;
    473  1.29   thorpej 	} */ *uap = v;
    474  1.56  augustss 	struct pcred *pc = p->p_cred;
    475  1.56  augustss 	gid_t egid;
    476  1.14       cgd 	int error;
    477  1.14       cgd 
    478  1.19       cgd 	egid = SCARG(uap, egid);
    479  1.14       cgd 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    480  1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    481  1.14       cgd 		return (error);
    482  1.59  christos 	/*
    483  1.60  christos 	 * Check if we are all set, and this is a no-op.
    484  1.59  christos 	 */
    485  1.59  christos 	if (pc->pc_ucred->cr_gid == egid)
    486  1.60  christos 		return (0);
    487  1.59  christos 
    488  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    489  1.27       jtc 	pc->pc_ucred->cr_gid = egid;
    490  1.55    bouyer 	p_sugid(p);
    491  1.35   mycroft 	return (0);
    492  1.35   mycroft }
    493  1.35   mycroft 
    494  1.35   mycroft int
    495  1.35   mycroft sys_setregid(p, v, retval)
    496  1.35   mycroft 	struct proc *p;
    497  1.35   mycroft 	void *v;
    498  1.35   mycroft 	register_t *retval;
    499  1.35   mycroft {
    500  1.35   mycroft 	struct sys_setregid_args /* {
    501  1.35   mycroft 		syscallarg(gid_t) rgid;
    502  1.35   mycroft 		syscallarg(gid_t) egid;
    503  1.35   mycroft 	} */ *uap = v;
    504  1.56  augustss 	struct pcred *pc = p->p_cred;
    505  1.56  augustss 	gid_t rgid, egid;
    506  1.59  christos 	int error, changed = 0;
    507  1.35   mycroft 
    508  1.35   mycroft 	rgid = SCARG(uap, rgid);
    509  1.35   mycroft 	egid = SCARG(uap, egid);
    510  1.35   mycroft 
    511  1.35   mycroft 	if (rgid != (gid_t)-1 &&
    512  1.35   mycroft 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    513  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    514  1.35   mycroft 		return (error);
    515  1.35   mycroft 
    516  1.35   mycroft 	if (egid != (gid_t)-1 &&
    517  1.35   mycroft 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    518  1.35   mycroft 	    egid != pc->p_svgid &&
    519  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    520  1.35   mycroft 		return (error);
    521  1.35   mycroft 
    522  1.59  christos 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
    523  1.35   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    524  1.35   mycroft 		pc->pc_ucred->cr_gid = egid;
    525  1.59  christos 		changed++;
    526  1.35   mycroft 	}
    527  1.35   mycroft 
    528  1.59  christos 	if (rgid != (gid_t)-1 &&
    529  1.59  christos 	    (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
    530  1.35   mycroft 		pc->p_rgid = rgid;
    531  1.35   mycroft 		pc->p_svgid = pc->pc_ucred->cr_gid;
    532  1.59  christos 		changed++;
    533  1.35   mycroft 	}
    534  1.35   mycroft 
    535  1.59  christos 	if (changed)
    536  1.55    bouyer 		p_sugid(p);
    537  1.14       cgd 	return (0);
    538  1.57   minoura }
    539  1.57   minoura 
    540  1.57   minoura int
    541  1.57   minoura sys_issetugid(p, v, retval)
    542  1.57   minoura 	struct proc *p;
    543  1.57   minoura 	void *v;
    544  1.57   minoura 	register_t *retval;
    545  1.57   minoura {
    546  1.57   minoura 	/*
    547  1.57   minoura 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    548  1.57   minoura 	 * we use P_SUGID because we consider changing the owners as
    549  1.57   minoura 	 * "tainting" as well.
    550  1.57   minoura 	 * This is significant for procs that start as root and "become"
    551  1.57   minoura 	 * a user without an exec - programs cannot know *everything*
    552  1.57   minoura 	 * that libc *might* have put in their data segment.
    553  1.57   minoura 	 */
    554  1.57   minoura 	*retval = (p->p_flag & P_SUGID) != 0;
    555  1.60  christos 	return (0);
    556  1.14       cgd }
    557  1.14       cgd 
    558  1.15       cgd /* ARGSUSED */
    559  1.32  christos int
    560  1.30   mycroft sys_setgroups(p, v, retval)
    561  1.15       cgd 	struct proc *p;
    562  1.29   thorpej 	void *v;
    563  1.29   thorpej 	register_t *retval;
    564  1.29   thorpej {
    565  1.30   mycroft 	struct sys_setgroups_args /* {
    566  1.41   thorpej 		syscallarg(int) gidsetsize;
    567  1.38       cgd 		syscallarg(const gid_t *) gidset;
    568  1.29   thorpej 	} */ *uap = v;
    569  1.56  augustss 	struct pcred *pc = p->p_cred;
    570  1.56  augustss 	int ngrp;
    571  1.15       cgd 	int error;
    572  1.59  christos 	gid_t grp[NGROUPS];
    573  1.59  christos 	size_t grsize;
    574  1.15       cgd 
    575  1.32  christos 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    576  1.15       cgd 		return (error);
    577  1.59  christos 
    578  1.19       cgd 	ngrp = SCARG(uap, gidsetsize);
    579  1.42   mycroft 	if ((u_int)ngrp > NGROUPS)
    580  1.15       cgd 		return (EINVAL);
    581  1.59  christos 
    582  1.59  christos 	grsize = ngrp * sizeof(gid_t);
    583  1.59  christos 	error = copyin(SCARG(uap, gidset), grp, grsize);
    584  1.32  christos 	if (error)
    585  1.15       cgd 		return (error);
    586  1.59  christos 	/*
    587  1.60  christos 	 * Check if this is a no-op.
    588  1.59  christos 	 */
    589  1.59  christos 	if (pc->pc_ucred->cr_ngroups == ngrp &&
    590  1.59  christos 	    memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
    591  1.60  christos 		return (0);
    592  1.59  christos 
    593  1.59  christos 	pc->pc_ucred = crcopy(pc->pc_ucred);
    594  1.59  christos 	(void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
    595  1.15       cgd 	pc->pc_ucred->cr_ngroups = ngrp;
    596  1.55    bouyer 	p_sugid(p);
    597  1.15       cgd 	return (0);
    598  1.15       cgd }
    599  1.14       cgd 
    600  1.14       cgd /*
    601  1.14       cgd  * Check if gid is a member of the group set.
    602  1.14       cgd  */
    603  1.32  christos int
    604  1.14       cgd groupmember(gid, cred)
    605  1.14       cgd 	gid_t gid;
    606  1.56  augustss 	struct ucred *cred;
    607  1.14       cgd {
    608  1.56  augustss 	gid_t *gp;
    609  1.14       cgd 	gid_t *egp;
    610  1.14       cgd 
    611  1.14       cgd 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    612  1.14       cgd 	for (gp = cred->cr_groups; gp < egp; gp++)
    613  1.14       cgd 		if (*gp == gid)
    614  1.14       cgd 			return (1);
    615  1.14       cgd 	return (0);
    616  1.14       cgd }
    617  1.14       cgd 
    618  1.14       cgd /*
    619  1.14       cgd  * Test whether the specified credentials imply "super-user"
    620  1.14       cgd  * privilege; if so, and we have accounting info, set the flag
    621  1.14       cgd  * indicating use of super-powers.
    622  1.14       cgd  * Returns 0 or error.
    623  1.14       cgd  */
    624  1.32  christos int
    625  1.14       cgd suser(cred, acflag)
    626  1.14       cgd 	struct ucred *cred;
    627  1.14       cgd 	u_short *acflag;
    628  1.14       cgd {
    629  1.14       cgd 	if (cred->cr_uid == 0) {
    630  1.14       cgd 		if (acflag)
    631  1.14       cgd 			*acflag |= ASU;
    632  1.14       cgd 		return (0);
    633  1.14       cgd 	}
    634  1.14       cgd 	return (EPERM);
    635  1.14       cgd }
    636  1.14       cgd 
    637  1.14       cgd /*
    638  1.14       cgd  * Allocate a zeroed cred structure.
    639  1.14       cgd  */
    640  1.14       cgd struct ucred *
    641  1.14       cgd crget()
    642  1.14       cgd {
    643  1.56  augustss 	struct ucred *cr;
    644  1.14       cgd 
    645  1.14       cgd 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    646  1.49     perry 	memset((caddr_t)cr, 0, sizeof(*cr));
    647  1.14       cgd 	cr->cr_ref = 1;
    648  1.14       cgd 	return (cr);
    649  1.14       cgd }
    650  1.14       cgd 
    651  1.14       cgd /*
    652  1.14       cgd  * Free a cred structure.
    653  1.14       cgd  * Throws away space when ref count gets to 0.
    654  1.14       cgd  */
    655  1.22       cgd void
    656  1.14       cgd crfree(cr)
    657  1.14       cgd 	struct ucred *cr;
    658  1.14       cgd {
    659  1.15       cgd 	int s;
    660  1.14       cgd 
    661  1.15       cgd 	s = splimp();				/* ??? */
    662  1.15       cgd 	if (--cr->cr_ref == 0)
    663  1.15       cgd 		FREE((caddr_t)cr, M_CRED);
    664  1.14       cgd 	(void) splx(s);
    665  1.14       cgd }
    666  1.14       cgd 
    667  1.14       cgd /*
    668  1.14       cgd  * Copy cred structure to a new one and free the old one.
    669  1.14       cgd  */
    670  1.14       cgd struct ucred *
    671  1.14       cgd crcopy(cr)
    672  1.14       cgd 	struct ucred *cr;
    673  1.14       cgd {
    674  1.14       cgd 	struct ucred *newcr;
    675  1.14       cgd 
    676  1.14       cgd 	if (cr->cr_ref == 1)
    677  1.14       cgd 		return (cr);
    678  1.14       cgd 	newcr = crget();
    679  1.14       cgd 	*newcr = *cr;
    680  1.14       cgd 	crfree(cr);
    681  1.14       cgd 	newcr->cr_ref = 1;
    682  1.14       cgd 	return (newcr);
    683  1.14       cgd }
    684  1.14       cgd 
    685  1.14       cgd /*
    686  1.14       cgd  * Dup cred struct to a new held one.
    687  1.14       cgd  */
    688  1.14       cgd struct ucred *
    689  1.14       cgd crdup(cr)
    690  1.14       cgd 	struct ucred *cr;
    691  1.14       cgd {
    692  1.14       cgd 	struct ucred *newcr;
    693  1.14       cgd 
    694  1.14       cgd 	newcr = crget();
    695  1.14       cgd 	*newcr = *cr;
    696  1.14       cgd 	newcr->cr_ref = 1;
    697  1.14       cgd 	return (newcr);
    698  1.14       cgd }
    699  1.14       cgd 
    700  1.14       cgd /*
    701  1.14       cgd  * Get login name, if available.
    702  1.14       cgd  */
    703  1.14       cgd /* ARGSUSED */
    704  1.32  christos int
    705  1.37       jtc sys___getlogin(p, v, retval)
    706  1.14       cgd 	struct proc *p;
    707  1.29   thorpej 	void *v;
    708  1.29   thorpej 	register_t *retval;
    709  1.29   thorpej {
    710  1.37       jtc 	struct sys___getlogin_args /* {
    711  1.19       cgd 		syscallarg(char *) namebuf;
    712  1.53    kleink 		syscallarg(size_t) namelen;
    713  1.29   thorpej 	} */ *uap = v;
    714  1.14       cgd 
    715  1.48     perry 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    716  1.48     perry 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    717  1.14       cgd 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    718  1.19       cgd 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    719  1.14       cgd }
    720  1.14       cgd 
    721  1.14       cgd /*
    722  1.14       cgd  * Set login name.
    723  1.14       cgd  */
    724  1.14       cgd /* ARGSUSED */
    725  1.32  christos int
    726  1.30   mycroft sys_setlogin(p, v, retval)
    727  1.14       cgd 	struct proc *p;
    728  1.29   thorpej 	void *v;
    729  1.29   thorpej 	register_t *retval;
    730  1.29   thorpej {
    731  1.30   mycroft 	struct sys_setlogin_args /* {
    732  1.38       cgd 		syscallarg(const char *) namebuf;
    733  1.29   thorpej 	} */ *uap = v;
    734  1.14       cgd 	int error;
    735  1.14       cgd 
    736  1.32  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    737  1.14       cgd 		return (error);
    738  1.38       cgd 	error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
    739  1.48     perry 	    sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
    740  1.14       cgd 	if (error == ENAMETOOLONG)
    741  1.14       cgd 		error = EINVAL;
    742  1.14       cgd 	return (error);
    743  1.14       cgd }
    744