Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.89
      1  1.89      elad /*	$NetBSD: kern_prot.c,v 1.89 2006/05/14 21:15:11 elad 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.80       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.14       cgd  *    may be used to endorse or promote products derived from this software
     22  1.14       cgd  *    without specific prior written permission.
     23  1.14       cgd  *
     24  1.14       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.14       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.14       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.14       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.14       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.14       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.14       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.14       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.14       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.14       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.14       cgd  * SUCH DAMAGE.
     35  1.14       cgd  *
     36  1.44      fvdl  *	@(#)kern_prot.c	8.9 (Berkeley) 2/14/95
     37  1.14       cgd  */
     38  1.14       cgd 
     39  1.14       cgd /*
     40  1.14       cgd  * System calls related to processes and protection
     41  1.14       cgd  */
     42  1.65     lukem 
     43  1.65     lukem #include <sys/cdefs.h>
     44  1.89      elad __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.89 2006/05/14 21:15:11 elad Exp $");
     45  1.45   thorpej 
     46  1.51  christos #include "opt_compat_43.h"
     47  1.14       cgd 
     48  1.14       cgd #include <sys/param.h>
     49  1.14       cgd #include <sys/acct.h>
     50  1.14       cgd #include <sys/systm.h>
     51  1.14       cgd #include <sys/ucred.h>
     52  1.14       cgd #include <sys/proc.h>
     53  1.14       cgd #include <sys/timeb.h>
     54  1.14       cgd #include <sys/times.h>
     55  1.82    simonb #include <sys/pool.h>
     56  1.72       dsl #include <sys/syslog.h>
     57  1.81  christos #include <sys/resourcevar.h>
     58  1.89      elad #include <sys/kauth.h>
     59  1.14       cgd 
     60  1.19       cgd #include <sys/mount.h>
     61  1.70   thorpej #include <sys/sa.h>
     62  1.19       cgd #include <sys/syscallargs.h>
     63  1.71   thorpej 
     64  1.89      elad #include <sys/malloc.h>
     65  1.63   mycroft 
     66  1.70   thorpej int	sys_getpid(struct lwp *, void *, register_t *);
     67  1.70   thorpej int	sys_getpid_with_ppid(struct lwp *, void *, register_t *);
     68  1.70   thorpej int	sys_getuid(struct lwp *, void *, register_t *);
     69  1.70   thorpej int	sys_getuid_with_euid(struct lwp *, void *, register_t *);
     70  1.70   thorpej int	sys_getgid(struct lwp *, void *, register_t *);
     71  1.70   thorpej int	sys_getgid_with_egid(struct lwp *, void *, register_t *);
     72  1.32  christos 
     73  1.86  christos static int grsortu(gid_t *, int);
     74  1.86  christos 
     75  1.14       cgd /* ARGSUSED */
     76  1.32  christos int
     77  1.73       dsl sys_getpid(struct lwp *l, void *v, register_t *retval)
     78  1.14       cgd {
     79  1.70   thorpej 	struct proc *p = l->l_proc;
     80  1.14       cgd 
     81  1.14       cgd 	*retval = p->p_pid;
     82  1.62   mycroft 	return (0);
     83  1.62   mycroft }
     84  1.62   mycroft 
     85  1.62   mycroft /* ARGSUSED */
     86  1.62   mycroft int
     87  1.73       dsl sys_getpid_with_ppid(struct lwp *l, void *v, register_t *retval)
     88  1.62   mycroft {
     89  1.70   thorpej 	struct proc *p = l->l_proc;
     90  1.62   mycroft 
     91  1.62   mycroft 	retval[0] = p->p_pid;
     92  1.62   mycroft 	retval[1] = p->p_pptr->p_pid;
     93  1.14       cgd 	return (0);
     94  1.14       cgd }
     95  1.14       cgd 
     96  1.14       cgd /* ARGSUSED */
     97  1.32  christos int
     98  1.73       dsl sys_getppid(struct lwp *l, void *v, register_t *retval)
     99  1.14       cgd {
    100  1.70   thorpej 	struct proc *p = l->l_proc;
    101  1.14       cgd 
    102  1.14       cgd 	*retval = p->p_pptr->p_pid;
    103  1.14       cgd 	return (0);
    104  1.14       cgd }
    105  1.14       cgd 
    106  1.14       cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
    107  1.32  christos int
    108  1.73       dsl sys_getpgrp(struct lwp *l, void *v, register_t *retval)
    109  1.14       cgd {
    110  1.70   thorpej 	struct proc *p = l->l_proc;
    111  1.14       cgd 
    112  1.14       cgd 	*retval = p->p_pgrp->pg_id;
    113  1.14       cgd 	return (0);
    114  1.43   thorpej }
    115  1.43   thorpej 
    116  1.43   thorpej /*
    117  1.43   thorpej  * Return the process group ID of the session leader (session ID)
    118  1.43   thorpej  * for the specified process.
    119  1.43   thorpej  */
    120  1.43   thorpej int
    121  1.73       dsl sys_getsid(struct lwp *l, void *v, register_t *retval)
    122  1.43   thorpej {
    123  1.43   thorpej 	struct sys_getsid_args /* {
    124  1.43   thorpej 		syscalldarg(pid_t) pid;
    125  1.43   thorpej 	} */ *uap = v;
    126  1.70   thorpej 	struct proc *p = l->l_proc;
    127  1.43   thorpej 
    128  1.43   thorpej 	if (SCARG(uap, pid) == 0)
    129  1.43   thorpej 		goto found;
    130  1.43   thorpej 	if ((p = pfind(SCARG(uap, pid))) == 0)
    131  1.43   thorpej 		return (ESRCH);
    132  1.43   thorpej found:
    133  1.43   thorpej 	*retval = p->p_session->s_sid;
    134  1.60  christos 	return (0);
    135  1.36       mrg }
    136  1.36       mrg 
    137  1.36       mrg int
    138  1.73       dsl sys_getpgid(struct lwp *l, void *v, register_t *retval)
    139  1.36       mrg {
    140  1.56  augustss 	struct sys_getpgid_args /* {
    141  1.36       mrg 		syscallarg(pid_t) pid;
    142  1.36       mrg 	} */ *uap = v;
    143  1.70   thorpej 	struct proc *p = l->l_proc;
    144  1.36       mrg 
    145  1.36       mrg 	if (SCARG(uap, pid) == 0)
    146  1.36       mrg 		goto found;
    147  1.36       mrg 	if ((p = pfind(SCARG(uap, pid))) == 0)
    148  1.36       mrg 		return (ESRCH);
    149  1.36       mrg found:
    150  1.36       mrg 	*retval = p->p_pgid;
    151  1.60  christos 	return (0);
    152  1.14       cgd }
    153  1.14       cgd 
    154  1.14       cgd /* ARGSUSED */
    155  1.32  christos int
    156  1.73       dsl sys_getuid(struct lwp *l, void *v, register_t *retval)
    157  1.14       cgd {
    158  1.70   thorpej 	struct proc *p = l->l_proc;
    159  1.14       cgd 
    160  1.89      elad 	*retval = kauth_cred_getuid(p->p_cred);
    161  1.62   mycroft 	return (0);
    162  1.62   mycroft }
    163  1.62   mycroft 
    164  1.62   mycroft /* ARGSUSED */
    165  1.62   mycroft int
    166  1.73       dsl sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
    167  1.62   mycroft {
    168  1.70   thorpej 	struct proc *p = l->l_proc;
    169  1.62   mycroft 
    170  1.89      elad 	retval[0] = kauth_cred_getuid(p->p_cred);
    171  1.89      elad 	retval[1] = kauth_cred_geteuid(p->p_cred);
    172  1.14       cgd 	return (0);
    173  1.14       cgd }
    174  1.14       cgd 
    175  1.14       cgd /* ARGSUSED */
    176  1.32  christos int
    177  1.73       dsl sys_geteuid(struct lwp *l, void *v, register_t *retval)
    178  1.14       cgd {
    179  1.70   thorpej 	struct proc *p = l->l_proc;
    180  1.14       cgd 
    181  1.89      elad 	*retval = kauth_cred_geteuid(p->p_cred);
    182  1.14       cgd 	return (0);
    183  1.14       cgd }
    184  1.14       cgd 
    185  1.14       cgd /* ARGSUSED */
    186  1.32  christos int
    187  1.73       dsl sys_getgid(struct lwp *l, void *v, register_t *retval)
    188  1.14       cgd {
    189  1.70   thorpej 	struct proc *p = l->l_proc;
    190  1.14       cgd 
    191  1.89      elad 	*retval = kauth_cred_getgid(p->p_cred);
    192  1.62   mycroft 	return (0);
    193  1.62   mycroft }
    194  1.62   mycroft 
    195  1.62   mycroft /* ARGSUSED */
    196  1.62   mycroft int
    197  1.73       dsl sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
    198  1.62   mycroft {
    199  1.70   thorpej 	struct proc *p = l->l_proc;
    200  1.62   mycroft 
    201  1.89      elad 	retval[0] = kauth_cred_getgid(p->p_cred);
    202  1.89      elad 	retval[1] = kauth_cred_getegid(p->p_cred);
    203  1.14       cgd 	return (0);
    204  1.14       cgd }
    205  1.14       cgd 
    206  1.14       cgd /*
    207  1.14       cgd  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    208  1.14       cgd  * via getgroups.  This syscall exists because it is somewhat painful to do
    209  1.14       cgd  * correctly in a library function.
    210  1.14       cgd  */
    211  1.14       cgd /* ARGSUSED */
    212  1.32  christos int
    213  1.73       dsl sys_getegid(struct lwp *l, void *v, register_t *retval)
    214  1.14       cgd {
    215  1.70   thorpej 	struct proc *p = l->l_proc;
    216  1.75     enami 
    217  1.89      elad 	*retval = kauth_cred_getegid(p->p_cred);
    218  1.14       cgd 	return (0);
    219  1.14       cgd }
    220  1.14       cgd 
    221  1.32  christos int
    222  1.73       dsl sys_getgroups(struct lwp *l, void *v, register_t *retval)
    223  1.29   thorpej {
    224  1.56  augustss 	struct sys_getgroups_args /* {
    225  1.41   thorpej 		syscallarg(int) gidsetsize;
    226  1.19       cgd 		syscallarg(gid_t *) gidset;
    227  1.29   thorpej 	} */ *uap = v;
    228  1.70   thorpej 	struct proc *p = l->l_proc;
    229  1.89      elad 	kauth_cred_t pc = p->p_cred;
    230  1.69   thorpej 	u_int ngrp;
    231  1.14       cgd 	int error;
    232  1.89      elad 	gid_t *grbuf;
    233  1.14       cgd 
    234  1.69   thorpej 	if (SCARG(uap, gidsetsize) == 0) {
    235  1.89      elad 		*retval = kauth_cred_ngroups(pc);
    236  1.14       cgd 		return (0);
    237  1.69   thorpej 	} else if (SCARG(uap, gidsetsize) < 0)
    238  1.69   thorpej 		return (EINVAL);
    239  1.69   thorpej 	ngrp = SCARG(uap, gidsetsize);
    240  1.89      elad 	if (ngrp < kauth_cred_ngroups(pc))
    241  1.14       cgd 		return (EINVAL);
    242  1.89      elad 	ngrp = kauth_cred_ngroups(pc);
    243  1.89      elad 
    244  1.89      elad 	grbuf = malloc(ngrp * sizeof(*grbuf), M_TEMP, M_WAITOK);
    245  1.89      elad 	kauth_cred_getgroups(pc, grbuf, ngrp);
    246  1.89      elad 	error = copyout(grbuf, (caddr_t)SCARG(uap, gidset),
    247  1.89      elad 			ngrp * sizeof(gid_t));
    248  1.89      elad 	free(grbuf, M_TEMP);
    249  1.32  christos 	if (error)
    250  1.14       cgd 		return (error);
    251  1.14       cgd 	*retval = ngrp;
    252  1.14       cgd 	return (0);
    253  1.14       cgd }
    254  1.14       cgd 
    255  1.14       cgd /* ARGSUSED */
    256  1.32  christos int
    257  1.73       dsl sys_setsid(struct lwp *l, void *v, register_t *retval)
    258  1.14       cgd {
    259  1.70   thorpej 	struct proc *p = l->l_proc;
    260  1.14       cgd 
    261  1.14       cgd 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    262  1.14       cgd 		return (EPERM);
    263  1.14       cgd 	} else {
    264  1.15       cgd 		(void)enterpgrp(p, p->p_pid, 1);
    265  1.14       cgd 		*retval = p->p_pid;
    266  1.14       cgd 		return (0);
    267  1.14       cgd 	}
    268  1.14       cgd }
    269  1.14       cgd 
    270  1.14       cgd /*
    271  1.14       cgd  * set process group (setpgid/old setpgrp)
    272  1.14       cgd  *
    273  1.14       cgd  * caller does setpgid(targpid, targpgid)
    274  1.14       cgd  *
    275  1.39     mikel  * pgid must be in valid range (EINVAL)
    276  1.14       cgd  * pid must be caller or child of caller (ESRCH)
    277  1.14       cgd  * if a child
    278  1.14       cgd  *	pid must be in same session (EPERM)
    279  1.14       cgd  *	pid can't have done an exec (EACCES)
    280  1.14       cgd  * if pgid != pid
    281  1.14       cgd  * 	there must exist some pid in same session having pgid (EPERM)
    282  1.14       cgd  * pid must not be session leader (EPERM)
    283  1.77       dsl  *
    284  1.77       dsl  * Permission checks now in enterpgrp()
    285  1.14       cgd  */
    286  1.14       cgd /* ARGSUSED */
    287  1.32  christos int
    288  1.73       dsl sys_setpgid(struct lwp *l, void *v, register_t *retval)
    289  1.29   thorpej {
    290  1.56  augustss 	struct sys_setpgid_args /* {
    291  1.19       cgd 		syscallarg(int) pid;
    292  1.19       cgd 		syscallarg(int) pgid;
    293  1.29   thorpej 	} */ *uap = v;
    294  1.70   thorpej 	struct proc *curp = l->l_proc;
    295  1.56  augustss 	struct proc *targp;			/* target process */
    296  1.14       cgd 
    297  1.39     mikel 	if (SCARG(uap, pgid) < 0)
    298  1.77       dsl 		return EINVAL;
    299  1.14       cgd 
    300  1.77       dsl 	/* XXX MP - there is a horrid race here with targp exiting! */
    301  1.19       cgd 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    302  1.77       dsl 		targp = pfind(SCARG(uap, pid));
    303  1.77       dsl 		if (targp == NULL)
    304  1.77       dsl 			return ESRCH;
    305  1.14       cgd 	} else
    306  1.14       cgd 		targp = curp;
    307  1.77       dsl 
    308  1.19       cgd 	if (SCARG(uap, pgid) == 0)
    309  1.19       cgd 		SCARG(uap, pgid) = targp->p_pid;
    310  1.77       dsl 	return enterpgrp(targp, SCARG(uap, pgid), 0);
    311  1.14       cgd }
    312  1.14       cgd 
    313  1.76       dsl /*
    314  1.76       dsl  * Set real, effective and saved uids to the requested values.
    315  1.76       dsl  * non-root callers can only ever change uids to values that match
    316  1.76       dsl  * one of the processes current uid values.
    317  1.76       dsl  * This is further restricted by the flags argument.
    318  1.76       dsl  */
    319  1.76       dsl 
    320  1.76       dsl int
    321  1.76       dsl do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
    322  1.76       dsl {
    323  1.76       dsl 	int error;
    324  1.76       dsl 	struct proc *p = l->l_proc;
    325  1.89      elad 	kauth_cred_t cred = p->p_cred;
    326  1.76       dsl 
    327  1.76       dsl 	/* Superuser can do anything it wants to.... */
    328  1.89      elad 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag);
    329  1.76       dsl 	if (error) {
    330  1.76       dsl 		/* Otherwise check new value is one of the allowed
    331  1.76       dsl 		   existing values. */
    332  1.89      elad 		if (r != -1 && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
    333  1.89      elad 			    && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
    334  1.89      elad 			    && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred)))
    335  1.76       dsl 			return error;
    336  1.89      elad 		if (e != -1 && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
    337  1.89      elad 			    && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
    338  1.89      elad 			    && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred)))
    339  1.76       dsl 			return error;
    340  1.89      elad 		if (sv != -1 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
    341  1.89      elad 			    && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
    342  1.89      elad 			    && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))
    343  1.76       dsl 			return error;
    344  1.76       dsl 	}
    345  1.76       dsl 
    346  1.76       dsl 	/* If nothing has changed, short circuit the request */
    347  1.89      elad 	if ((r == -1 || r == kauth_cred_getuid(cred))
    348  1.89      elad 	    && (e == -1 || e == kauth_cred_geteuid(cred))
    349  1.89      elad 	    && (sv == -1 || sv == kauth_cred_getsvuid(cred)))
    350  1.76       dsl 		/* nothing to do */
    351  1.76       dsl 		return 0;
    352  1.76       dsl 
    353  1.76       dsl 	/* The pcred structure is not actually shared... */
    354  1.89      elad 	if (r != -1 && r != kauth_cred_getuid(cred)) {
    355  1.76       dsl 		/* Update count of processes for this user */
    356  1.89      elad 		(void)chgproccnt(kauth_cred_getuid(cred), -1);
    357  1.76       dsl 		(void)chgproccnt(r, 1);
    358  1.89      elad 		kauth_cred_setuid(cred, r);
    359  1.76       dsl 	}
    360  1.76       dsl 	if (sv != -1)
    361  1.89      elad 		kauth_cred_setsvuid(cred, sv);
    362  1.89      elad 	if (e != -1 && e != kauth_cred_geteuid(cred)) {
    363  1.76       dsl 		/* Update a clone of the current credentials */
    364  1.89      elad 		cred = kauth_cred_copy(cred);
    365  1.89      elad 		kauth_cred_seteuid(cred, e);
    366  1.89      elad 		p->p_cred = cred;
    367  1.76       dsl 	}
    368  1.76       dsl 
    369  1.76       dsl 	/* Mark process as having changed credentials, stops tracing etc */
    370  1.76       dsl 	p_sugid(p);
    371  1.76       dsl 	return 0;
    372  1.76       dsl }
    373  1.76       dsl 
    374  1.76       dsl /*
    375  1.76       dsl  * Set real, effective and saved gids to the requested values.
    376  1.76       dsl  * non-root callers can only ever change gids to values that match
    377  1.76       dsl  * one of the processes current gid values.
    378  1.76       dsl  * This is further restricted by the flags argument.
    379  1.76       dsl  */
    380  1.76       dsl 
    381  1.76       dsl int
    382  1.76       dsl do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
    383  1.76       dsl {
    384  1.76       dsl 	int error;
    385  1.76       dsl 	struct proc *p = l->l_proc;
    386  1.89      elad 	kauth_cred_t cred = p->p_cred;
    387  1.76       dsl 
    388  1.76       dsl 	/* Superuser can do anything it wants to.... */
    389  1.89      elad 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag);
    390  1.76       dsl 	if (error) {
    391  1.76       dsl 		/* Otherwise check new value is one of the allowed
    392  1.76       dsl 		   existing values. */
    393  1.89      elad 		if (r != -1 && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
    394  1.89      elad 			    && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
    395  1.89      elad 			    && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred)))
    396  1.76       dsl 			return error;
    397  1.89      elad 		if (e != -1 && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
    398  1.89      elad 			    && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
    399  1.89      elad 			    && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred)))
    400  1.76       dsl 			return error;
    401  1.89      elad 		if (sv != -1 && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
    402  1.89      elad 			    && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
    403  1.89      elad 			    && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))
    404  1.76       dsl 			return error;
    405  1.76       dsl 	}
    406  1.76       dsl 
    407  1.76       dsl 	/* If nothing has changed, short circuit the request */
    408  1.89      elad 	if ((r == -1 || r == kauth_cred_getgid(cred))
    409  1.89      elad 	    && (e == -1 || e == kauth_cred_getegid(cred))
    410  1.89      elad 	    && (sv == -1 || sv == kauth_cred_getsvgid(cred)))
    411  1.76       dsl 		/* nothing to do */
    412  1.76       dsl 		return 0;
    413  1.76       dsl 
    414  1.76       dsl 	/* The pcred structure is not actually shared... */
    415  1.76       dsl 	if (r != -1)
    416  1.89      elad 		kauth_cred_setgid(cred, r);
    417  1.76       dsl 	if (sv != -1)
    418  1.89      elad 		kauth_cred_setsvgid(cred, sv);
    419  1.89      elad 	if (e != -1 && e != kauth_cred_getegid(cred)) {
    420  1.76       dsl 		/* Update a clone of the current credentials */
    421  1.89      elad 		cred = kauth_cred_copy(cred);
    422  1.89      elad 		kauth_cred_setegid(cred, e);
    423  1.89      elad 		p->p_cred = cred;
    424  1.76       dsl 	}
    425  1.76       dsl 
    426  1.76       dsl 	/* Mark process as having changed credentials, stops tracing etc */
    427  1.76       dsl 	p_sugid(p);
    428  1.76       dsl 	return 0;
    429  1.76       dsl }
    430  1.76       dsl 
    431  1.14       cgd /* ARGSUSED */
    432  1.32  christos int
    433  1.73       dsl sys_setuid(struct lwp *l, void *v, register_t *retval)
    434  1.29   thorpej {
    435  1.30   mycroft 	struct sys_setuid_args /* {
    436  1.19       cgd 		syscallarg(uid_t) uid;
    437  1.29   thorpej 	} */ *uap = v;
    438  1.76       dsl 	uid_t uid = SCARG(uap, uid);
    439  1.14       cgd 
    440  1.76       dsl 	return do_setresuid(l, uid, uid, uid,
    441  1.76       dsl 			    ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
    442  1.14       cgd }
    443  1.14       cgd 
    444  1.14       cgd /* ARGSUSED */
    445  1.32  christos int
    446  1.73       dsl sys_seteuid(struct lwp *l, void *v, register_t *retval)
    447  1.29   thorpej {
    448  1.30   mycroft 	struct sys_seteuid_args /* {
    449  1.19       cgd 		syscallarg(uid_t) euid;
    450  1.29   thorpej 	} */ *uap = v;
    451  1.59  christos 
    452  1.76       dsl 	return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
    453  1.14       cgd }
    454  1.14       cgd 
    455  1.35   mycroft int
    456  1.73       dsl sys_setreuid(struct lwp *l, void *v, register_t *retval)
    457  1.35   mycroft {
    458  1.35   mycroft 	struct sys_setreuid_args /* {
    459  1.35   mycroft 		syscallarg(uid_t) ruid;
    460  1.35   mycroft 		syscallarg(uid_t) euid;
    461  1.35   mycroft 	} */ *uap = v;
    462  1.70   thorpej 	struct proc *p = l->l_proc;
    463  1.76       dsl 	uid_t ruid, euid, svuid;
    464  1.35   mycroft 
    465  1.35   mycroft 	ruid = SCARG(uap, ruid);
    466  1.35   mycroft 	euid = SCARG(uap, euid);
    467  1.76       dsl 	if (ruid == -1)
    468  1.89      elad 		ruid = kauth_cred_getuid(p->p_cred);
    469  1.76       dsl 	if (euid == -1)
    470  1.89      elad 		euid = kauth_cred_geteuid(p->p_cred);
    471  1.76       dsl 	/* Saved uid is set to the new euid if the ruid changed */
    472  1.89      elad 	svuid = (ruid == kauth_cred_getuid(p->p_cred)) ? -1 : euid;
    473  1.76       dsl 
    474  1.76       dsl 	return do_setresuid(l, ruid, euid, svuid,
    475  1.76       dsl 			    ID_R_EQ_R | ID_R_EQ_E |
    476  1.76       dsl 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    477  1.76       dsl 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
    478  1.35   mycroft }
    479  1.35   mycroft 
    480  1.14       cgd /* ARGSUSED */
    481  1.32  christos int
    482  1.73       dsl sys_setgid(struct lwp *l, void *v, register_t *retval)
    483  1.29   thorpej {
    484  1.30   mycroft 	struct sys_setgid_args /* {
    485  1.19       cgd 		syscallarg(gid_t) gid;
    486  1.29   thorpej 	} */ *uap = v;
    487  1.76       dsl 	gid_t gid = SCARG(uap, gid);
    488  1.14       cgd 
    489  1.76       dsl 	return do_setresgid(l, gid, gid, gid,
    490  1.76       dsl 			    ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
    491  1.14       cgd }
    492  1.14       cgd 
    493  1.14       cgd /* ARGSUSED */
    494  1.32  christos int
    495  1.73       dsl sys_setegid(struct lwp *l, void *v, register_t *retval)
    496  1.29   thorpej {
    497  1.30   mycroft 	struct sys_setegid_args /* {
    498  1.19       cgd 		syscallarg(gid_t) egid;
    499  1.29   thorpej 	} */ *uap = v;
    500  1.14       cgd 
    501  1.76       dsl 	return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
    502  1.35   mycroft }
    503  1.35   mycroft 
    504  1.35   mycroft int
    505  1.73       dsl sys_setregid(struct lwp *l, void *v, register_t *retval)
    506  1.35   mycroft {
    507  1.35   mycroft 	struct sys_setregid_args /* {
    508  1.35   mycroft 		syscallarg(gid_t) rgid;
    509  1.35   mycroft 		syscallarg(gid_t) egid;
    510  1.35   mycroft 	} */ *uap = v;
    511  1.70   thorpej 	struct proc *p = l->l_proc;
    512  1.76       dsl 	gid_t rgid, egid, svgid;
    513  1.35   mycroft 
    514  1.35   mycroft 	rgid = SCARG(uap, rgid);
    515  1.35   mycroft 	egid = SCARG(uap, egid);
    516  1.76       dsl 	if (rgid == -1)
    517  1.89      elad 		rgid = kauth_cred_getgid(p->p_cred);
    518  1.76       dsl 	if (egid == -1)
    519  1.89      elad 		egid = kauth_cred_getegid(p->p_cred);
    520  1.76       dsl 	/* Saved gid is set to the new egid if the rgid changed */
    521  1.89      elad 	svgid = rgid == kauth_cred_getgid(p->p_cred) ? -1 : egid;
    522  1.76       dsl 
    523  1.76       dsl 	return do_setresgid(l, rgid, egid, svgid,
    524  1.76       dsl 			ID_R_EQ_R | ID_R_EQ_E |
    525  1.76       dsl 			ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    526  1.76       dsl 			ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
    527  1.57   minoura }
    528  1.57   minoura 
    529  1.57   minoura int
    530  1.73       dsl sys_issetugid(struct lwp *l, void *v, register_t *retval)
    531  1.57   minoura {
    532  1.70   thorpej 	struct proc *p = l->l_proc;
    533  1.75     enami 
    534  1.57   minoura 	/*
    535  1.57   minoura 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    536  1.57   minoura 	 * we use P_SUGID because we consider changing the owners as
    537  1.57   minoura 	 * "tainting" as well.
    538  1.57   minoura 	 * This is significant for procs that start as root and "become"
    539  1.57   minoura 	 * a user without an exec - programs cannot know *everything*
    540  1.57   minoura 	 * that libc *might* have put in their data segment.
    541  1.57   minoura 	 */
    542  1.57   minoura 	*retval = (p->p_flag & P_SUGID) != 0;
    543  1.60  christos 	return (0);
    544  1.14       cgd }
    545  1.14       cgd 
    546  1.86  christos /*
    547  1.86  christos  * sort -u for groups.
    548  1.86  christos  */
    549  1.86  christos static int
    550  1.86  christos grsortu(gid_t *grp, int ngrp)
    551  1.86  christos {
    552  1.87    rillig 	const gid_t *src, *end;
    553  1.87    rillig 	gid_t *dst;
    554  1.87    rillig 	gid_t group;
    555  1.87    rillig 	int i, j;
    556  1.86  christos 
    557  1.86  christos 	/* bubble sort */
    558  1.86  christos 	for (i = 0; i < ngrp; i++)
    559  1.86  christos 		for (j = i + 1; j < ngrp; j++)
    560  1.86  christos 			if (grp[i] > grp[j]) {
    561  1.86  christos 				gid_t tmp = grp[i];
    562  1.86  christos 				grp[i] = grp[j];
    563  1.86  christos 				grp[j] = tmp;
    564  1.86  christos 			}
    565  1.87    rillig 
    566  1.86  christos 	/* uniq */
    567  1.87    rillig 	end = grp + ngrp;
    568  1.87    rillig 	src = grp;
    569  1.87    rillig 	dst = grp;
    570  1.87    rillig 	while (src < end) {
    571  1.87    rillig 		group = *src++;
    572  1.87    rillig 		while (src < end && *src == group)
    573  1.87    rillig 			src++;
    574  1.87    rillig 		*dst++ = group;
    575  1.87    rillig 	}
    576  1.87    rillig 
    577  1.86  christos #ifdef DIAGNOSTIC
    578  1.87    rillig 	/* zero out the rest of the array */
    579  1.87    rillig 	(void)memset(dst, 0, sizeof(*grp) * (end - dst));
    580  1.86  christos #endif
    581  1.87    rillig 
    582  1.87    rillig 	return dst - grp;
    583  1.86  christos }
    584  1.86  christos 
    585  1.15       cgd /* ARGSUSED */
    586  1.32  christos int
    587  1.73       dsl sys_setgroups(struct lwp *l, void *v, register_t *retval)
    588  1.29   thorpej {
    589  1.30   mycroft 	struct sys_setgroups_args /* {
    590  1.41   thorpej 		syscallarg(int) gidsetsize;
    591  1.38       cgd 		syscallarg(const gid_t *) gidset;
    592  1.29   thorpej 	} */ *uap = v;
    593  1.70   thorpej 	struct proc *p = l->l_proc;
    594  1.89      elad 	kauth_cred_t pc = p->p_cred;
    595  1.56  augustss 	int ngrp;
    596  1.15       cgd 	int error;
    597  1.59  christos 	gid_t grp[NGROUPS];
    598  1.59  christos 	size_t grsize;
    599  1.15       cgd 
    600  1.89      elad 	if ((error = kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER,
    601  1.89      elad 				       &p->p_acflag)) != 0)
    602  1.15       cgd 		return (error);
    603  1.59  christos 
    604  1.19       cgd 	ngrp = SCARG(uap, gidsetsize);
    605  1.42   mycroft 	if ((u_int)ngrp > NGROUPS)
    606  1.15       cgd 		return (EINVAL);
    607  1.59  christos 
    608  1.59  christos 	grsize = ngrp * sizeof(gid_t);
    609  1.59  christos 	error = copyin(SCARG(uap, gidset), grp, grsize);
    610  1.32  christos 	if (error)
    611  1.15       cgd 		return (error);
    612  1.86  christos 
    613  1.86  christos 	ngrp = grsortu(grp, ngrp);
    614  1.59  christos 
    615  1.89      elad 	pc = kauth_cred_copy(pc);
    616  1.89      elad 	p->p_cred = pc;
    617  1.14       cgd 
    618  1.89      elad 	kauth_cred_setgroups(p->p_cred, grp, ngrp, -1);
    619  1.14       cgd 
    620  1.89      elad 	p_sugid(p);
    621  1.14       cgd 	return (0);
    622  1.14       cgd }
    623  1.14       cgd 
    624  1.14       cgd /*
    625  1.14       cgd  * Get login name, if available.
    626  1.14       cgd  */
    627  1.14       cgd /* ARGSUSED */
    628  1.32  christos int
    629  1.73       dsl sys___getlogin(struct lwp *l, void *v, register_t *retval)
    630  1.29   thorpej {
    631  1.37       jtc 	struct sys___getlogin_args /* {
    632  1.19       cgd 		syscallarg(char *) namebuf;
    633  1.53    kleink 		syscallarg(size_t) namelen;
    634  1.29   thorpej 	} */ *uap = v;
    635  1.70   thorpej 	struct proc *p = l->l_proc;
    636  1.14       cgd 
    637  1.48     perry 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    638  1.48     perry 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    639  1.14       cgd 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    640  1.19       cgd 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    641  1.14       cgd }
    642  1.14       cgd 
    643  1.14       cgd /*
    644  1.14       cgd  * Set login name.
    645  1.14       cgd  */
    646  1.14       cgd /* ARGSUSED */
    647  1.32  christos int
    648  1.73       dsl sys___setlogin(struct lwp *l, void *v, register_t *retval)
    649  1.29   thorpej {
    650  1.70   thorpej 	struct sys___setlogin_args /* {
    651  1.38       cgd 		syscallarg(const char *) namebuf;
    652  1.29   thorpej 	} */ *uap = v;
    653  1.70   thorpej 	struct proc *p = l->l_proc;
    654  1.72       dsl 	struct session *s = p->p_pgrp->pg_session;
    655  1.72       dsl 	char newname[sizeof s->s_login + 1];
    656  1.14       cgd 	int error;
    657  1.14       cgd 
    658  1.89      elad 	if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    659  1.89      elad 				       &p->p_acflag)) != 0)
    660  1.14       cgd 		return (error);
    661  1.72       dsl 	error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
    662  1.72       dsl 	if (error != 0)
    663  1.75     enami 		return (error == ENAMETOOLONG ? EINVAL : error);
    664  1.72       dsl 
    665  1.72       dsl 	if (s->s_flags & S_LOGIN_SET && p->p_pid != s->s_sid &&
    666  1.72       dsl 	    strncmp(newname, s->s_login, sizeof s->s_login) != 0)
    667  1.74       wiz 		log(LOG_WARNING, "%s (pid %d) changing logname from "
    668  1.74       wiz 		    "%.*s to %s\n", p->p_comm, p->p_pid,
    669  1.74       wiz 		    (int)sizeof s->s_login, s->s_login, newname);
    670  1.72       dsl 	s->s_flags |= S_LOGIN_SET;
    671  1.72       dsl 	strncpy(s->s_login, newname, sizeof s->s_login);
    672  1.75     enami 	return (0);
    673  1.14       cgd }
    674