Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.93.4.1
      1 /*	$NetBSD: kern_prot.c,v 1.93.4.1 2006/09/11 18:19:09 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.93.4.1 2006/09/11 18:19:09 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/sa.h>
     62 #include <sys/syscallargs.h>
     63 
     64 #include <sys/malloc.h>
     65 
     66 int	sys_getpid(struct lwp *, void *, register_t *);
     67 int	sys_getpid_with_ppid(struct lwp *, void *, register_t *);
     68 int	sys_getuid(struct lwp *, void *, register_t *);
     69 int	sys_getuid_with_euid(struct lwp *, void *, register_t *);
     70 int	sys_getgid(struct lwp *, void *, register_t *);
     71 int	sys_getgid_with_egid(struct lwp *, void *, register_t *);
     72 
     73 static int grsortu(gid_t *, int);
     74 
     75 /* ARGSUSED */
     76 int
     77 sys_getpid(struct lwp *l, void *v, register_t *retval)
     78 {
     79 	struct proc *p = l->l_proc;
     80 
     81 	*retval = p->p_pid;
     82 	return (0);
     83 }
     84 
     85 /* ARGSUSED */
     86 int
     87 sys_getpid_with_ppid(struct lwp *l, void *v, register_t *retval)
     88 {
     89 	struct proc *p = l->l_proc;
     90 
     91 	retval[0] = p->p_pid;
     92 	rw_enter(&proclist_lock, RW_READER);
     93 	retval[1] = p->p_pptr->p_pid;
     94 	rw_exit(&proclist_lock);
     95 	return (0);
     96 }
     97 
     98 /* ARGSUSED */
     99 int
    100 sys_getppid(struct lwp *l, void *v, register_t *retval)
    101 {
    102 	struct proc *p = l->l_proc;
    103 
    104 	rw_enter(&proclist_lock, RW_READER);
    105 	*retval = p->p_pptr->p_pid;
    106 	rw_exit(&proclist_lock);
    107 	return (0);
    108 }
    109 
    110 /* Get process group ID; note that POSIX getpgrp takes no parameter */
    111 int
    112 sys_getpgrp(struct lwp *l, void *v, register_t *retval)
    113 {
    114 	struct proc *p = l->l_proc;
    115 
    116 	rw_enter(&proclist_lock, RW_READER);
    117 	*retval = p->p_pgrp->pg_id;
    118 	rw_exit(&proclist_lock);
    119 	return (0);
    120 }
    121 
    122 /*
    123  * Return the process group ID of the session leader (session ID)
    124  * for the specified process.
    125  */
    126 int
    127 sys_getsid(struct lwp *l, void *v, register_t *retval)
    128 {
    129 	struct sys_getsid_args /* {
    130 		syscalldarg(pid_t) pid;
    131 	} */ *uap = v;
    132 	pid_t pid = SCARG(uap, pid);
    133 	struct proc *p;
    134 
    135 	rw_enter(&proclist_lock, RW_READER);
    136 	if (pid != 0)
    137 		p = l->l_proc;
    138 	else if ((p = p_find(pid, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
    139 		return (ESRCH);
    140 	*retval = p->p_session->s_sid;
    141 	rw_exit(&proclist_lock);
    142 	return (0);
    143 }
    144 
    145 int
    146 sys_getpgid(struct lwp *l, void *v, register_t *retval)
    147 {
    148 	struct sys_getpgid_args /* {
    149 		syscallarg(pid_t) pid;
    150 	} */ *uap = v;
    151 	pid_t pid = SCARG(uap, pid);
    152 	struct proc *p;
    153 
    154 	rw_enter(&proclist_lock, RW_READER);
    155 	if (pid != 0)
    156 		p = l->l_proc;
    157 	else if ((p = p_find(pid, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
    158 		return (ESRCH);
    159 	*retval = p->p_pgid;
    160 	rw_exit(&proclist_lock);
    161 	return (0);
    162 }
    163 
    164 /* ARGSUSED */
    165 int
    166 sys_getuid(struct lwp *l, void *v, register_t *retval)
    167 {
    168 
    169 	*retval = kauth_cred_getuid(l->l_cred);
    170 	return (0);
    171 }
    172 
    173 /* ARGSUSED */
    174 int
    175 sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
    176 {
    177 
    178 	retval[0] = kauth_cred_getuid(l->l_cred);
    179 	retval[1] = kauth_cred_geteuid(l->l_cred);
    180 	return (0);
    181 }
    182 
    183 /* ARGSUSED */
    184 int
    185 sys_geteuid(struct lwp *l, void *v, register_t *retval)
    186 {
    187 
    188 	*retval = kauth_cred_geteuid(l->l_cred);
    189 	return (0);
    190 }
    191 
    192 /* ARGSUSED */
    193 int
    194 sys_getgid(struct lwp *l, void *v, register_t *retval)
    195 {
    196 
    197 	*retval = kauth_cred_getgid(l->l_cred);
    198 	return (0);
    199 }
    200 
    201 /* ARGSUSED */
    202 int
    203 sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
    204 {
    205 
    206 	retval[0] = kauth_cred_getgid(l->l_cred);
    207 	retval[1] = kauth_cred_getegid(l->l_cred);
    208 	return (0);
    209 }
    210 
    211 /*
    212  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    213  * via getgroups.  This syscall exists because it is somewhat painful to do
    214  * correctly in a library function.
    215  */
    216 /* ARGSUSED */
    217 int
    218 sys_getegid(struct lwp *l, void *v, register_t *retval)
    219 {
    220 
    221 	*retval = kauth_cred_getegid(l->l_cred);
    222 	return (0);
    223 }
    224 
    225 int
    226 sys_getgroups(struct lwp *l, void *v, register_t *retval)
    227 {
    228 	struct sys_getgroups_args /* {
    229 		syscallarg(int) gidsetsize;
    230 		syscallarg(gid_t *) gidset;
    231 	} */ *uap = v;
    232 	kauth_cred_t cred = l->l_cred;
    233 	u_int ngrp;
    234 	int error;
    235 	gid_t *grbuf;
    236 
    237 	if (SCARG(uap, gidsetsize) == 0) {
    238 		*retval = kauth_cred_ngroups(cred);
    239 		return (0);
    240 	} else if (SCARG(uap, gidsetsize) < 0)
    241 		return (EINVAL);
    242 	ngrp = SCARG(uap, gidsetsize);
    243 	if (ngrp < kauth_cred_ngroups(cred))
    244 		return (EINVAL);
    245 	ngrp = kauth_cred_ngroups(cred);
    246 
    247 	grbuf = malloc(ngrp * sizeof(*grbuf), M_TEMP, M_WAITOK);
    248 	kauth_cred_getgroups(cred, grbuf, ngrp);
    249 	error = copyout(grbuf, (caddr_t)SCARG(uap, gidset),
    250 			ngrp * sizeof(gid_t));
    251 	free(grbuf, M_TEMP);
    252 	if (error)
    253 		return (error);
    254 	*retval = ngrp;
    255 	return (0);
    256 }
    257 
    258 /* ARGSUSED */
    259 int
    260 sys_setsid(struct lwp *l, void *v, register_t *retval)
    261 {
    262 	struct proc *p = l->l_proc;
    263 	int error;
    264 
    265 	error = enterpgrp(p, p->p_pid, p->p_pid, 1);
    266 	*retval = p->p_pid;
    267 	return (error);
    268 }
    269 
    270 
    271 /*
    272  * set process group (setpgid/old setpgrp)
    273  *
    274  * caller does setpgid(targpid, targpgid)
    275  *
    276  * pgid must be in valid range (EINVAL)
    277  * pid must be caller or child of caller (ESRCH)
    278  * if a child
    279  *	pid must be in same session (EPERM)
    280  *	pid can't have done an exec (EACCES)
    281  * if pgid != pid
    282  * 	there must exist some pid in same session having pgid (EPERM)
    283  * pid must not be session leader (EPERM)
    284  *
    285  * Permission checks now in enterpgrp()
    286  */
    287 /* ARGSUSED */
    288 int
    289 sys_setpgid(struct lwp *l, void *v, register_t *retval)
    290 {
    291 	struct sys_setpgid_args /* {
    292 		syscallarg(int) pid;
    293 		syscallarg(int) pgid;
    294 	} */ *uap = v;
    295 	struct proc *p = l->l_proc;
    296 	pid_t targp, pgid;
    297 
    298 	if (SCARG(uap, pgid) < 0)
    299 		return EINVAL;
    300 	if ((targp = SCARG(uap, pid)) == 0)
    301 		targp = p->p_pid;
    302 	if ((pgid = SCARG(uap, pgid)) == 0)
    303 		pgid = targp;
    304 
    305 	return enterpgrp(p, targp, pgid, 0);
    306 }
    307 
    308 /*
    309  * Set real, effective and saved uids to the requested values.
    310  * non-root callers can only ever change uids to values that match
    311  * one of the processes current uid values.
    312  * This is further restricted by the flags argument.
    313  */
    314 
    315 int
    316 do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
    317 {
    318 	struct proc *p = l->l_proc;
    319 	kauth_cred_t cred;
    320 
    321 	/* Get a write lock on the process credential. */
    322 	proc_crmod_enter(p);
    323 	cred = p->p_cred;
    324 
    325 	/*
    326 	 * Check that the new value is one of the allowed existing values,
    327 	 * or that we have root privilege.
    328 	 */
    329 	if ((r != -1
    330 	    && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
    331 	    && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
    332 	    && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred))) ||
    333 	    (e != -1
    334 	    && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
    335 	    && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
    336 	    && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred))) ||
    337 	    (sv != -1
    338 	    && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
    339 	    && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
    340 	    && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))) {
    341 		int error;
    342 
    343 		error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    344 		    &l->l_acflag);
    345 		if (error != 0) {
    346 		 	proc_crmod_leave(p, cred, NULL);
    347 			return error;
    348 		}
    349 	}
    350 
    351 	/* If nothing has changed, short circuit the request */
    352 	if ((r == -1 || r == kauth_cred_getuid(cred))
    353 	    && (e == -1 || e == kauth_cred_geteuid(cred))
    354 	    && (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
    355 		proc_crmod_leave(p, cred, NULL);
    356 		return 0;
    357 	}
    358 
    359 	cred = kauth_cred_dup(cred);
    360 
    361 	if (r != -1 && r != kauth_cred_getuid(cred)) {
    362 		/* Update count of processes for this user */
    363 		(void)chgproccnt(kauth_cred_getuid(cred), -1);
    364 		(void)chgproccnt(r, 1);
    365 		kauth_cred_setuid(cred, r);
    366 	}
    367 	if (sv != -1)
    368 		kauth_cred_setsvuid(cred, sv);
    369 	if (e != -1)
    370 		kauth_cred_seteuid(cred, e);
    371 
    372 	/* Mark process as having changed credentials, stops tracing etc. */
    373 	p_sugid(p);
    374 
    375 	/* Broadcast our credentials to the process and other LWPs. */
    376  	proc_crmod_leave(p, cred, p->p_cred);
    377 
    378 	/* Update our copy of the credentials. */
    379  	lwp_update_creds(l);
    380 
    381 	return 0;
    382 }
    383 
    384 /*
    385  * Set real, effective and saved gids to the requested values.
    386  * non-root callers can only ever change gids to values that match
    387  * one of the processes current gid values.
    388  * This is further restricted by the flags argument.
    389  */
    390 
    391 int
    392 do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
    393 {
    394 	struct proc *p = l->l_proc;
    395 	kauth_cred_t cred;
    396 
    397 	/* Get a write lock on the process credential. */
    398 	proc_crmod_enter(p);
    399 	cred = p->p_cred;
    400 
    401 	/*
    402 	 * check new value is one of the allowed existing values.
    403 	 * otherwise, check if we have root privilege.
    404 	 */
    405 	if ((r != -1
    406 	    && !((flags & ID_R_EQ_R) && r == kauth_cred_getgid(cred))
    407 	    && !((flags & ID_R_EQ_E) && r == kauth_cred_getegid(cred))
    408 	    && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvgid(cred))) ||
    409 	    (e != -1
    410 	    && !((flags & ID_E_EQ_R) && e == kauth_cred_getgid(cred))
    411 	    && !((flags & ID_E_EQ_E) && e == kauth_cred_getegid(cred))
    412 	    && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvgid(cred))) ||
    413 	    (sv != -1
    414 	    && !((flags & ID_S_EQ_R) && sv == kauth_cred_getgid(cred))
    415 	    && !((flags & ID_S_EQ_E) && sv == kauth_cred_getegid(cred))
    416 	    && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvgid(cred)))) {
    417 		int error;
    418 
    419 		error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    420 		    &l->l_acflag);
    421 		if (error != 0) {
    422 		 	proc_crmod_leave(p, cred, NULL);
    423 			return error;
    424 		}
    425 	}
    426 
    427 	/* If nothing has changed, short circuit the request */
    428 	if ((r == -1 || r == kauth_cred_getgid(cred))
    429 	    && (e == -1 || e == kauth_cred_getegid(cred))
    430 	    && (sv == -1 || sv == kauth_cred_getsvgid(cred))) {
    431 	 	proc_crmod_leave(p, cred, NULL);
    432 		return 0;
    433 	}
    434 
    435 	cred = kauth_cred_dup(cred);
    436 
    437 	if (r != -1)
    438 		kauth_cred_setgid(cred, r);
    439 	if (sv != -1)
    440 		kauth_cred_setsvgid(cred, sv);
    441 	if (e != -1)
    442 		kauth_cred_setegid(cred, e);
    443 
    444 	/* Mark process as having changed credentials, stops tracing etc. */
    445 	p_sugid(p);
    446 
    447 	/* Broadcast our credentials to the process and other LWPs. */
    448  	proc_crmod_leave(p, cred, p->p_cred);
    449 
    450 	/* Update our copy of the credentials. */
    451  	lwp_update_creds(l);
    452 
    453 	return 0;
    454 }
    455 
    456 /* ARGSUSED */
    457 int
    458 sys_setuid(struct lwp *l, void *v, register_t *retval)
    459 {
    460 	struct sys_setuid_args /* {
    461 		syscallarg(uid_t) uid;
    462 	} */ *uap = v;
    463 	uid_t uid = SCARG(uap, uid);
    464 
    465 	return do_setresuid(l, uid, uid, uid,
    466 			    ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
    467 }
    468 
    469 /* ARGSUSED */
    470 int
    471 sys_seteuid(struct lwp *l, void *v, register_t *retval)
    472 {
    473 	struct sys_seteuid_args /* {
    474 		syscallarg(uid_t) euid;
    475 	} */ *uap = v;
    476 
    477 	return do_setresuid(l, -1, SCARG(uap, euid), -1, ID_E_EQ_R | ID_E_EQ_S);
    478 }
    479 
    480 int
    481 sys_setreuid(struct lwp *l, void *v, register_t *retval)
    482 {
    483 	struct sys_setreuid_args /* {
    484 		syscallarg(uid_t) ruid;
    485 		syscallarg(uid_t) euid;
    486 	} */ *uap = v;
    487 	kauth_cred_t cred = l->l_cred;
    488 	uid_t ruid, euid, svuid;
    489 
    490 	ruid = SCARG(uap, ruid);
    491 	euid = SCARG(uap, euid);
    492 
    493 	if (ruid == -1)
    494 		ruid = kauth_cred_getuid(cred);
    495 	if (euid == -1)
    496 		euid = kauth_cred_geteuid(cred);
    497 
    498 	/* Saved uid is set to the new euid if the ruid changed */
    499 	svuid = (ruid == kauth_cred_getuid(cred)) ? -1 : euid;
    500 
    501 	return do_setresuid(l, ruid, euid, svuid,
    502 			    ID_R_EQ_R | ID_R_EQ_E |
    503 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    504 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
    505 }
    506 
    507 /* ARGSUSED */
    508 int
    509 sys_setgid(struct lwp *l, void *v, register_t *retval)
    510 {
    511 	struct sys_setgid_args /* {
    512 		syscallarg(gid_t) gid;
    513 	} */ *uap = v;
    514 	gid_t gid = SCARG(uap, gid);
    515 
    516 	return do_setresgid(l, gid, gid, gid,
    517 			    ID_R_EQ_R | ID_E_EQ_R | ID_S_EQ_R);
    518 }
    519 
    520 /* ARGSUSED */
    521 int
    522 sys_setegid(struct lwp *l, void *v, register_t *retval)
    523 {
    524 	struct sys_setegid_args /* {
    525 		syscallarg(gid_t) egid;
    526 	} */ *uap = v;
    527 
    528 	return do_setresgid(l, -1, SCARG(uap, egid), -1, ID_E_EQ_R | ID_E_EQ_S);
    529 }
    530 
    531 int
    532 sys_setregid(struct lwp *l, void *v, register_t *retval)
    533 {
    534 	struct sys_setregid_args /* {
    535 		syscallarg(gid_t) rgid;
    536 		syscallarg(gid_t) egid;
    537 	} */ *uap = v;
    538 	kauth_cred_t cred = l->l_cred;
    539 	gid_t rgid, egid, svgid;
    540 
    541 	rgid = SCARG(uap, rgid);
    542 	egid = SCARG(uap, egid);
    543 
    544 	if (rgid == -1)
    545 		rgid = kauth_cred_getgid(cred);
    546 	if (egid == -1)
    547 		egid = kauth_cred_getegid(cred);
    548 
    549 	/* Saved gid is set to the new egid if the rgid changed */
    550 	svgid = rgid == kauth_cred_getgid(cred) ? -1 : egid;
    551 
    552 	return do_setresgid(l, rgid, egid, svgid,
    553 			ID_R_EQ_R | ID_R_EQ_E |
    554 			ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    555 			ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S);
    556 }
    557 
    558 int
    559 sys_issetugid(struct lwp *l, void *v, register_t *retval)
    560 {
    561 	struct proc *p = l->l_proc;
    562 
    563 	/*
    564 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    565 	 * we use P_SUGID because we consider changing the owners as
    566 	 * "tainting" as well.
    567 	 * This is significant for procs that start as root and "become"
    568 	 * a user without an exec - programs cannot know *everything*
    569 	 * that libc *might* have put in their data segment.
    570 	 */
    571 	*retval = (p->p_flag & P_SUGID) != 0;
    572 	return (0);
    573 }
    574 
    575 /*
    576  * sort -u for groups.
    577  */
    578 static int
    579 grsortu(gid_t *grp, int ngrp)
    580 {
    581 	const gid_t *src, *end;
    582 	gid_t *dst;
    583 	gid_t group;
    584 	int i, j;
    585 
    586 	/* bubble sort */
    587 	for (i = 0; i < ngrp; i++)
    588 		for (j = i + 1; j < ngrp; j++)
    589 			if (grp[i] > grp[j]) {
    590 				gid_t tmp = grp[i];
    591 				grp[i] = grp[j];
    592 				grp[j] = tmp;
    593 			}
    594 
    595 	/* uniq */
    596 	end = grp + ngrp;
    597 	src = grp;
    598 	dst = grp;
    599 	while (src < end) {
    600 		group = *src++;
    601 		while (src < end && *src == group)
    602 			src++;
    603 		*dst++ = group;
    604 	}
    605 
    606 #ifdef DIAGNOSTIC
    607 	/* zero out the rest of the array */
    608 	(void)memset(dst, 0, sizeof(*grp) * (end - dst));
    609 #endif
    610 
    611 	return dst - grp;
    612 }
    613 
    614 /* ARGSUSED */
    615 int
    616 sys_setgroups(struct lwp *l, void *v, register_t *retval)
    617 {
    618 	struct sys_setgroups_args /* {
    619 		syscallarg(int) gidsetsize;
    620 		syscallarg(const gid_t *) gidset;
    621 	} */ *uap = v;
    622 	kauth_cred_t cred;
    623 	struct proc *p = l->l_proc;
    624 	int ngrp;
    625 	int error;
    626 	gid_t grp[NGROUPS];
    627 	size_t grsize;
    628 
    629 	proc_crmod_enter(p);
    630 	cred = p->p_cred;
    631 
    632 	if ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    633 	    &l->l_acflag)) != 0)
    634 		goto bad;
    635 
    636 	ngrp = SCARG(uap, gidsetsize);
    637 	if ((u_int)ngrp > NGROUPS) {
    638 		error = EINVAL;
    639 		goto bad;
    640 	}
    641 
    642 	grsize = ngrp * sizeof(gid_t);
    643 	error = copyin(SCARG(uap, gidset), grp, grsize);
    644 	if (error)
    645 		goto bad;
    646 
    647 	ngrp = grsortu(grp, ngrp);
    648 	cred = kauth_cred_dup(cred);
    649 	kauth_cred_setgroups(cred, grp, ngrp, -1);
    650 
    651 	/* Mark process as having changed credentials, stops tracing etc. */
    652 	p_sugid(p);
    653 
    654 	/* Broadcast our credentials to the process and other LWPs. */
    655  	proc_crmod_leave(p, cred, p->p_cred);
    656 
    657 	/* Update our copy of the credentials. */
    658  	lwp_update_creds(l);
    659 
    660 	return (0);
    661   bad:
    662   	proc_crmod_leave(p, cred, NULL);
    663 	return (error);
    664 }
    665 
    666 /*
    667  * Get login name, if available.
    668  */
    669 /* ARGSUSED */
    670 int
    671 sys___getlogin(struct lwp *l, void *v, register_t *retval)
    672 {
    673 	struct sys___getlogin_args /* {
    674 		syscallarg(char *) namebuf;
    675 		syscallarg(size_t) namelen;
    676 	} */ *uap = v;
    677 	struct proc *p = l->l_proc;
    678 	char login[sizeof(p->p_session->s_login)];
    679 	int namelen = SCARG(uap, namelen);
    680 
    681 	if (namelen > sizeof(login))
    682 		namelen = sizeof(login);
    683 	rw_enter(&proclist_lock, RW_WRITER);
    684 	memcpy(login, p->p_session->s_login, namelen);
    685 	rw_exit(&proclist_lock);
    686 	return (copyout(login, (void *)SCARG(uap, namebuf), namelen));
    687 }
    688 
    689 /*
    690  * Set login name.
    691  */
    692 /* ARGSUSED */
    693 int
    694 sys___setlogin(struct lwp *l, void *v, register_t *retval)
    695 {
    696 	struct sys___setlogin_args /* {
    697 		syscallarg(const char *) namebuf;
    698 	} */ *uap = v;
    699 	struct proc *p = l->l_proc;
    700 	struct session *sp;
    701 	char newname[sizeof sp->s_login + 1];
    702 	int error;
    703 
    704 	if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
    705 	    &l->l_acflag)) != 0)
    706 		return (error);
    707 	error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
    708 	if (error != 0)
    709 		return (error == ENAMETOOLONG ? EINVAL : error);
    710 
    711 	rw_enter(&proclist_lock, RW_WRITER);
    712 	sp = p->p_session;
    713 	if (sp->s_flags & S_LOGIN_SET && p->p_pid != sp->s_sid &&
    714 	    strncmp(newname, sp->s_login, sizeof sp->s_login) != 0)
    715 		log(LOG_WARNING, "%s (pid %d) changing logname from "
    716 		    "%.*s to %s\n", p->p_comm, p->p_pid,
    717 		    (int)sizeof sp->s_login, sp->s_login, newname);
    718 	sp->s_flags |= S_LOGIN_SET;
    719 	strncpy(sp->s_login, newname, sizeof sp->s_login);
    720 	rw_exit(&proclist_lock);
    721 	return (0);
    722 }
    723 
    724