Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.93.4.4
      1 /*	$NetBSD: kern_prot.c,v 1.93.4.4 2007/01/30 13:51:41 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.4 2007/01/30 13:51:41 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 	rw_enter(&proclist_lock, RW_READER);
     92 	retval[1] = p->p_pptr->p_pid;
     93 	rw_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 	rw_enter(&proclist_lock, RW_READER);
    104 	*retval = p->p_pptr->p_pid;
    105 	rw_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 	rw_enter(&proclist_lock, RW_READER);
    116 	*retval = p->p_pgrp->pg_id;
    117 	rw_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 
    134 	rw_enter(&proclist_lock, RW_READER);
    135 	if (pid != 0)
    136 		p = l->l_proc;
    137 	else if ((p = p_find(pid, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
    138 		return (ESRCH);
    139 	*retval = p->p_session->s_sid;
    140 	rw_exit(&proclist_lock);
    141 	return (0);
    142 }
    143 
    144 int
    145 sys_getpgid(struct lwp *l, void *v, register_t *retval)
    146 {
    147 	struct sys_getpgid_args /* {
    148 		syscallarg(pid_t) pid;
    149 	} */ *uap = v;
    150 	pid_t pid = SCARG(uap, pid);
    151 	struct proc *p;
    152 
    153 	rw_enter(&proclist_lock, RW_READER);
    154 	if (pid != 0)
    155 		p = l->l_proc;
    156 	else if ((p = p_find(pid, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
    157 		return (ESRCH);
    158 	*retval = p->p_pgid;
    159 	rw_exit(&proclist_lock);
    160 	return (0);
    161 }
    162 
    163 /* ARGSUSED */
    164 int
    165 sys_getuid(struct lwp *l, void *v, register_t *retval)
    166 {
    167 
    168 	*retval = kauth_cred_getuid(l->l_cred);
    169 	return (0);
    170 }
    171 
    172 /* ARGSUSED */
    173 int
    174 sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
    175 {
    176 
    177 	retval[0] = kauth_cred_getuid(l->l_cred);
    178 	retval[1] = kauth_cred_geteuid(l->l_cred);
    179 	return (0);
    180 }
    181 
    182 /* ARGSUSED */
    183 int
    184 sys_geteuid(struct lwp *l, void *v, register_t *retval)
    185 {
    186 
    187 	*retval = kauth_cred_geteuid(l->l_cred);
    188 	return (0);
    189 }
    190 
    191 /* ARGSUSED */
    192 int
    193 sys_getgid(struct lwp *l, void *v, register_t *retval)
    194 {
    195 
    196 	*retval = kauth_cred_getgid(l->l_cred);
    197 	return (0);
    198 }
    199 
    200 /* ARGSUSED */
    201 int
    202 sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
    203 {
    204 
    205 	retval[0] = kauth_cred_getgid(l->l_cred);
    206 	retval[1] = kauth_cred_getegid(l->l_cred);
    207 	return (0);
    208 }
    209 
    210 /*
    211  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    212  * via getgroups.  This syscall exists because it is somewhat painful to do
    213  * correctly in a library function.
    214  */
    215 /* ARGSUSED */
    216 int
    217 sys_getegid(struct lwp *l, void *v, register_t *retval)
    218 {
    219 
    220 	*retval = kauth_cred_getegid(l->l_cred);
    221 	return (0);
    222 }
    223 
    224 int
    225 sys_getgroups(struct lwp *l, void *v, register_t *retval)
    226 {
    227 	struct sys_getgroups_args /* {
    228 		syscallarg(int) gidsetsize;
    229 		syscallarg(gid_t *) gidset;
    230 	} */ *uap = v;
    231 	kauth_cred_t cred = l->l_cred;
    232 	u_int ngrp;
    233 	int error;
    234 	gid_t *grbuf;
    235 
    236 	if (SCARG(uap, gidsetsize) == 0) {
    237 		*retval = kauth_cred_ngroups(cred);
    238 		return (0);
    239 	} else if (SCARG(uap, gidsetsize) < 0)
    240 		return (EINVAL);
    241 	ngrp = SCARG(uap, gidsetsize);
    242 	if (ngrp < kauth_cred_ngroups(cred))
    243 		return (EINVAL);
    244 	ngrp = kauth_cred_ngroups(cred);
    245 
    246 	grbuf = malloc(ngrp * sizeof(*grbuf), M_TEMP, M_WAITOK);
    247 	kauth_cred_getgroups(cred, grbuf, ngrp);
    248 	error = copyout(grbuf, (caddr_t)SCARG(uap, gidset),
    249 			ngrp * sizeof(gid_t));
    250 	free(grbuf, M_TEMP);
    251 	if (error)
    252 		return (error);
    253 	*retval = ngrp;
    254 	return (0);
    255 }
    256 
    257 /* ARGSUSED */
    258 int
    259 sys_setsid(struct lwp *l, void *v, register_t *retval)
    260 {
    261 	struct proc *p = l->l_proc;
    262 	int error;
    263 
    264 	error = enterpgrp(p, p->p_pid, p->p_pid, 1);
    265 	*retval = p->p_pid;
    266 	return (error);
    267 }
    268 
    269 
    270 /*
    271  * set process group (setpgid/old setpgrp)
    272  *
    273  * caller does setpgid(targpid, targpgid)
    274  *
    275  * pgid must be in valid range (EINVAL)
    276  * pid must be caller or child of caller (ESRCH)
    277  * if a child
    278  *	pid must be in same session (EPERM)
    279  *	pid can't have done an exec (EACCES)
    280  * if pgid != pid
    281  * 	there must exist some pid in same session having pgid (EPERM)
    282  * pid must not be session leader (EPERM)
    283  *
    284  * Permission checks now in enterpgrp()
    285  */
    286 /* ARGSUSED */
    287 int
    288 sys_setpgid(struct lwp *l, void *v, register_t *retval)
    289 {
    290 	struct sys_setpgid_args /* {
    291 		syscallarg(int) pid;
    292 		syscallarg(int) pgid;
    293 	} */ *uap = v;
    294 	struct proc *p = l->l_proc;
    295 	pid_t targp, pgid;
    296 
    297 	if (SCARG(uap, pgid) < 0)
    298 		return EINVAL;
    299 	if ((targp = SCARG(uap, pid)) == 0)
    300 		targp = p->p_pid;
    301 	if ((pgid = SCARG(uap, pgid)) == 0)
    302 		pgid = targp;
    303 
    304 	return enterpgrp(p, targp, pgid, 0);
    305 }
    306 
    307 /*
    308  * Set real, effective and saved uids to the requested values.
    309  * non-root callers can only ever change uids to values that match
    310  * one of the processes current uid values.
    311  * This is further restricted by the flags argument.
    312  */
    313 
    314 int
    315 do_setresuid(struct lwp *l, uid_t r, uid_t e, uid_t sv, u_int flags)
    316 {
    317 	struct proc *p = l->l_proc;
    318 	kauth_cred_t cred, ncred;
    319 
    320 	ncred = kauth_cred_alloc();
    321 
    322 	/* Get a write lock on the process credential. */
    323 	proc_crmod_enter();
    324 	cred = p->p_cred;
    325 
    326 	/*
    327 	 * Check that the new value is one of the allowed existing values,
    328 	 * or that we have root privilege.
    329 	 */
    330 	if ((r != -1
    331 	    && !((flags & ID_R_EQ_R) && r == kauth_cred_getuid(cred))
    332 	    && !((flags & ID_R_EQ_E) && r == kauth_cred_geteuid(cred))
    333 	    && !((flags & ID_R_EQ_S) && r == kauth_cred_getsvuid(cred))) ||
    334 	    (e != -1
    335 	    && !((flags & ID_E_EQ_R) && e == kauth_cred_getuid(cred))
    336 	    && !((flags & ID_E_EQ_E) && e == kauth_cred_geteuid(cred))
    337 	    && !((flags & ID_E_EQ_S) && e == kauth_cred_getsvuid(cred))) ||
    338 	    (sv != -1
    339 	    && !((flags & ID_S_EQ_R) && sv == kauth_cred_getuid(cred))
    340 	    && !((flags & ID_S_EQ_E) && sv == kauth_cred_geteuid(cred))
    341 	    && !((flags & ID_S_EQ_S) && sv == kauth_cred_getsvuid(cred)))) {
    342 		int error;
    343 
    344 		error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
    345 		    p, NULL, NULL, NULL);
    346 		if (error != 0) {
    347 		 	proc_crmod_leave(cred, ncred);
    348 			return error;
    349 		}
    350 	}
    351 
    352 	/* If nothing has changed, short circuit the request */
    353 	if ((r == -1 || r == kauth_cred_getuid(cred))
    354 	    && (e == -1 || e == kauth_cred_geteuid(cred))
    355 	    && (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
    356 		proc_crmod_leave(cred, ncred);
    357 		return 0;
    358 	}
    359 
    360 	kauth_cred_clone(cred, ncred);
    361 
    362 	if (r != -1 && r != kauth_cred_getuid(ncred)) {
    363 		/* Update count of processes for this user */
    364 		(void)chgproccnt(kauth_cred_getuid(ncred), -1);
    365 		(void)chgproccnt(r, 1);
    366 		kauth_cred_setuid(ncred, r);
    367 	}
    368 	if (sv != -1)
    369 		kauth_cred_setsvuid(ncred, sv);
    370 	if (e != -1)
    371 		kauth_cred_seteuid(ncred, e);
    372 
    373 	/* Mark process as having changed credentials, stops tracing etc. */
    374 	p_sugid(p);
    375 
    376 	/* Broadcast our credentials to the process and other LWPs. */
    377  	proc_crmod_leave(ncred, cred);
    378 
    379 	return 0;
    380 }
    381 
    382 /*
    383  * Set real, effective and saved gids to the requested values.
    384  * non-root callers can only ever change gids to values that match
    385  * one of the processes current gid values.
    386  * This is further restricted by the flags argument.
    387  */
    388 
    389 int
    390 do_setresgid(struct lwp *l, gid_t r, gid_t e, gid_t sv, u_int flags)
    391 {
    392 	struct proc *p = l->l_proc;
    393 	kauth_cred_t cred, ncred;
    394 
    395 	ncred = kauth_cred_alloc();
    396 
    397 	/* Get a write lock on the process credential. */
    398 	proc_crmod_enter();
    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_process(cred, KAUTH_PROCESS_SETID,
    420 		    p, NULL, NULL, NULL);
    421 		if (error != 0) {
    422 		 	proc_crmod_leave(cred, ncred);
    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(cred, ncred);
    432 		return 0;
    433 	}
    434 
    435 	kauth_cred_clone(cred, ncred);
    436 
    437 	if (r != -1)
    438 		kauth_cred_setgid(ncred, r);
    439 	if (sv != -1)
    440 		kauth_cred_setsvgid(ncred, sv);
    441 	if (e != -1)
    442 		kauth_cred_setegid(ncred, 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(ncred, cred);
    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 P_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 & P_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 	ncred = kauth_cred_alloc();
    627 
    628 	proc_crmod_enter();
    629 	cred = p->p_cred;
    630 
    631 	if ((error = kauth_authorize_process(cred, KAUTH_PROCESS_SETID,
    632 	    p, NULL, NULL, NULL)) != 0)
    633 		goto bad;
    634 
    635 	ngrp = SCARG(uap, gidsetsize);
    636 	if ((u_int)ngrp > NGROUPS) {
    637 		error = EINVAL;
    638 		goto bad;
    639 	}
    640 
    641 	grsize = ngrp * sizeof(gid_t);
    642 	error = copyin(SCARG(uap, gidset), grp, grsize);
    643 	if (error)
    644 		goto bad;
    645 
    646 	ngrp = grsortu(grp, ngrp);
    647 	kauth_cred_clone(cred, ncred);
    648 	kauth_cred_setgroups(ncred, grp, ngrp, -1);
    649 
    650 	/* Mark process as having changed credentials, stops tracing etc. */
    651 	p_sugid(p);
    652 
    653 	/* Broadcast our credentials to the process and other LWPs. */
    654  	proc_crmod_leave(ncred, cred);
    655 
    656 	return (0);
    657   bad:
    658   	proc_crmod_leave(cred, ncred);
    659 	return (error);
    660 }
    661 
    662 /*
    663  * Get login name, if available.
    664  */
    665 /* ARGSUSED */
    666 int
    667 sys___getlogin(struct lwp *l, void *v, register_t *retval)
    668 {
    669 	struct sys___getlogin_args /* {
    670 		syscallarg(char *) namebuf;
    671 		syscallarg(size_t) namelen;
    672 	} */ *uap = v;
    673 	struct proc *p = l->l_proc;
    674 	char login[sizeof(p->p_session->s_login)];
    675 	int namelen = SCARG(uap, namelen);
    676 
    677 	if (namelen > sizeof(login))
    678 		namelen = sizeof(login);
    679 	rw_enter(&proclist_lock, RW_WRITER);
    680 	memcpy(login, p->p_session->s_login, namelen);
    681 	rw_exit(&proclist_lock);
    682 	return (copyout(login, (void *)SCARG(uap, namebuf), namelen));
    683 }
    684 
    685 /*
    686  * Set login name.
    687  */
    688 /* ARGSUSED */
    689 int
    690 sys___setlogin(struct lwp *l, void *v, register_t *retval)
    691 {
    692 	struct sys___setlogin_args /* {
    693 		syscallarg(const char *) namebuf;
    694 	} */ *uap = v;
    695 	struct proc *p = l->l_proc;
    696 	struct session *sp;
    697 	char newname[sizeof sp->s_login + 1];
    698 	int error;
    699 
    700 	if ((error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_SETID,
    701 	    p, NULL, NULL, NULL)) != 0)
    702 		return (error);
    703 	error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
    704 	if (error != 0)
    705 		return (error == ENAMETOOLONG ? EINVAL : error);
    706 
    707 	rw_enter(&proclist_lock, RW_WRITER);
    708 	sp = p->p_session;
    709 	if (sp->s_flags & S_LOGIN_SET && p->p_pid != sp->s_sid &&
    710 	    strncmp(newname, sp->s_login, sizeof sp->s_login) != 0)
    711 		log(LOG_WARNING, "%s (pid %d) changing logname from "
    712 		    "%.*s to %s\n", p->p_comm, p->p_pid,
    713 		    (int)sizeof sp->s_login, sp->s_login, newname);
    714 	sp->s_flags |= S_LOGIN_SET;
    715 	strncpy(sp->s_login, newname, sizeof sp->s_login);
    716 	rw_exit(&proclist_lock);
    717 	return (0);
    718 }
    719 
    720