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