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