Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.75
      1 /*	$NetBSD: kern_prot.c,v 1.75 2003/02/28 23:24:40 enami 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kern_prot.c	8.9 (Berkeley) 2/14/95
     41  */
     42 
     43 /*
     44  * System calls related to processes and protection
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.75 2003/02/28 23:24:40 enami Exp $");
     49 
     50 #include "opt_compat_43.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/acct.h>
     54 #include <sys/systm.h>
     55 #include <sys/ucred.h>
     56 #include <sys/proc.h>
     57 #include <sys/timeb.h>
     58 #include <sys/times.h>
     59 #include <sys/malloc.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <sys/mount.h>
     63 #include <sys/sa.h>
     64 #include <sys/syscallargs.h>
     65 
     66 MALLOC_DEFINE(M_CRED, "cred", "credentials");
     67 
     68 int	sys_getpid(struct lwp *, void *, register_t *);
     69 int	sys_getpid_with_ppid(struct lwp *, void *, register_t *);
     70 int	sys_getuid(struct lwp *, void *, register_t *);
     71 int	sys_getuid_with_euid(struct lwp *, void *, register_t *);
     72 int	sys_getgid(struct lwp *, void *, register_t *);
     73 int	sys_getgid_with_egid(struct lwp *, void *, register_t *);
     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 	retval[1] = p->p_pptr->p_pid;
     93 	return (0);
     94 }
     95 
     96 /* ARGSUSED */
     97 int
     98 sys_getppid(struct lwp *l, void *v, register_t *retval)
     99 {
    100 	struct proc *p = l->l_proc;
    101 
    102 	*retval = p->p_pptr->p_pid;
    103 	return (0);
    104 }
    105 
    106 /* Get process group ID; note that POSIX getpgrp takes no parameter */
    107 int
    108 sys_getpgrp(struct lwp *l, void *v, register_t *retval)
    109 {
    110 	struct proc *p = l->l_proc;
    111 
    112 	*retval = p->p_pgrp->pg_id;
    113 	return (0);
    114 }
    115 
    116 /*
    117  * Return the process group ID of the session leader (session ID)
    118  * for the specified process.
    119  */
    120 int
    121 sys_getsid(struct lwp *l, void *v, register_t *retval)
    122 {
    123 	struct sys_getsid_args /* {
    124 		syscalldarg(pid_t) pid;
    125 	} */ *uap = v;
    126 	struct proc *p = l->l_proc;
    127 
    128 	if (SCARG(uap, pid) == 0)
    129 		goto found;
    130 	if ((p = pfind(SCARG(uap, pid))) == 0)
    131 		return (ESRCH);
    132 found:
    133 	*retval = p->p_session->s_sid;
    134 	return (0);
    135 }
    136 
    137 int
    138 sys_getpgid(struct lwp *l, void *v, register_t *retval)
    139 {
    140 	struct sys_getpgid_args /* {
    141 		syscallarg(pid_t) pid;
    142 	} */ *uap = v;
    143 	struct proc *p = l->l_proc;
    144 
    145 	if (SCARG(uap, pid) == 0)
    146 		goto found;
    147 	if ((p = pfind(SCARG(uap, pid))) == 0)
    148 		return (ESRCH);
    149 found:
    150 	*retval = p->p_pgid;
    151 	return (0);
    152 }
    153 
    154 /* ARGSUSED */
    155 int
    156 sys_getuid(struct lwp *l, void *v, register_t *retval)
    157 {
    158 	struct proc *p = l->l_proc;
    159 
    160 	*retval = p->p_cred->p_ruid;
    161 	return (0);
    162 }
    163 
    164 /* ARGSUSED */
    165 int
    166 sys_getuid_with_euid(struct lwp *l, void *v, register_t *retval)
    167 {
    168 	struct proc *p = l->l_proc;
    169 
    170 	retval[0] = p->p_cred->p_ruid;
    171 	retval[1] = p->p_ucred->cr_uid;
    172 	return (0);
    173 }
    174 
    175 /* ARGSUSED */
    176 int
    177 sys_geteuid(struct lwp *l, void *v, register_t *retval)
    178 {
    179 	struct proc *p = l->l_proc;
    180 
    181 	*retval = p->p_ucred->cr_uid;
    182 	return (0);
    183 }
    184 
    185 /* ARGSUSED */
    186 int
    187 sys_getgid(struct lwp *l, void *v, register_t *retval)
    188 {
    189 	struct proc *p = l->l_proc;
    190 
    191 	*retval = p->p_cred->p_rgid;
    192 	return (0);
    193 }
    194 
    195 /* ARGSUSED */
    196 int
    197 sys_getgid_with_egid(struct lwp *l, void *v, register_t *retval)
    198 {
    199 	struct proc *p = l->l_proc;
    200 
    201 	retval[0] = p->p_cred->p_rgid;
    202 	retval[1] = p->p_ucred->cr_gid;
    203 	return (0);
    204 }
    205 
    206 /*
    207  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    208  * via getgroups.  This syscall exists because it is somewhat painful to do
    209  * correctly in a library function.
    210  */
    211 /* ARGSUSED */
    212 int
    213 sys_getegid(struct lwp *l, void *v, register_t *retval)
    214 {
    215 	struct proc *p = l->l_proc;
    216 
    217 	*retval = p->p_ucred->cr_gid;
    218 	return (0);
    219 }
    220 
    221 int
    222 sys_getgroups(struct lwp *l, void *v, register_t *retval)
    223 {
    224 	struct sys_getgroups_args /* {
    225 		syscallarg(int) gidsetsize;
    226 		syscallarg(gid_t *) gidset;
    227 	} */ *uap = v;
    228 	struct proc *p = l->l_proc;
    229 	struct pcred *pc = p->p_cred;
    230 	u_int ngrp;
    231 	int error;
    232 
    233 	if (SCARG(uap, gidsetsize) == 0) {
    234 		*retval = pc->pc_ucred->cr_ngroups;
    235 		return (0);
    236 	} else if (SCARG(uap, gidsetsize) < 0)
    237 		return (EINVAL);
    238 	ngrp = SCARG(uap, gidsetsize);
    239 	if (ngrp < pc->pc_ucred->cr_ngroups)
    240 		return (EINVAL);
    241 	ngrp = pc->pc_ucred->cr_ngroups;
    242 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    243 	    (caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    244 	if (error)
    245 		return (error);
    246 	*retval = ngrp;
    247 	return (0);
    248 }
    249 
    250 /* ARGSUSED */
    251 int
    252 sys_setsid(struct lwp *l, void *v, register_t *retval)
    253 {
    254 	struct proc *p = l->l_proc;
    255 
    256 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    257 		return (EPERM);
    258 	} else {
    259 		(void)enterpgrp(p, p->p_pid, 1);
    260 		*retval = p->p_pid;
    261 		return (0);
    262 	}
    263 }
    264 
    265 /*
    266  * set process group (setpgid/old setpgrp)
    267  *
    268  * caller does setpgid(targpid, targpgid)
    269  *
    270  * pgid must be in valid range (EINVAL)
    271  * pid must be caller or child of caller (ESRCH)
    272  * if a child
    273  *	pid must be in same session (EPERM)
    274  *	pid can't have done an exec (EACCES)
    275  * if pgid != pid
    276  * 	there must exist some pid in same session having pgid (EPERM)
    277  * pid must not be session leader (EPERM)
    278  */
    279 /* ARGSUSED */
    280 int
    281 sys_setpgid(struct lwp *l, void *v, register_t *retval)
    282 {
    283 	struct sys_setpgid_args /* {
    284 		syscallarg(int) pid;
    285 		syscallarg(int) pgid;
    286 	} */ *uap = v;
    287 	struct proc *curp = l->l_proc;
    288 	struct proc *targp;			/* target process */
    289 	struct pgrp *pgrp;			/* target pgrp */
    290 
    291 	if (SCARG(uap, pgid) < 0)
    292 		return (EINVAL);
    293 
    294 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    295 		if ((targp = pfind(SCARG(uap, pid))) == 0 ||
    296 		    !inferior(targp, curp))
    297 			return (ESRCH);
    298 		if (targp->p_session != curp->p_session)
    299 			return (EPERM);
    300 		if (targp->p_flag & P_EXEC)
    301 			return (EACCES);
    302 	} else
    303 		targp = curp;
    304 	if (SESS_LEADER(targp))
    305 		return (EPERM);
    306 	if (SCARG(uap, pgid) == 0)
    307 		SCARG(uap, pgid) = targp->p_pid;
    308 	else if (SCARG(uap, pgid) != targp->p_pid)
    309 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    310 		    pgrp->pg_session != curp->p_session)
    311 			return (EPERM);
    312 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    313 }
    314 
    315 /* ARGSUSED */
    316 int
    317 sys_setuid(struct lwp *l, void *v, register_t *retval)
    318 {
    319 	struct sys_setuid_args /* {
    320 		syscallarg(uid_t) uid;
    321 	} */ *uap = v;
    322 	struct proc *p = l->l_proc;
    323 	struct pcred *pc = p->p_cred;
    324 	uid_t uid;
    325 	int error;
    326 
    327 	uid = SCARG(uap, uid);
    328 	if (uid != pc->p_ruid &&
    329 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    330 		return (error);
    331 	/*
    332 	 * Check if we are all set, and this is a no-op.
    333 	 */
    334 	if (pc->p_ruid == uid && pc->p_svuid == uid &&
    335 	    pc->pc_ucred->cr_uid == uid)
    336 		return (0);
    337 	/*
    338 	 * Everything's okay, do it.
    339 	 * Transfer proc count to new user.
    340 	 * Copy credentials so other references do not see our changes.
    341 	 */
    342 	(void)chgproccnt(pc->p_ruid, -1);
    343 	(void)chgproccnt(uid, 1);
    344 	pc->pc_ucred = crcopy(pc->pc_ucred);
    345 	pc->pc_ucred->cr_uid = uid;
    346 	pc->p_ruid = uid;
    347 	pc->p_svuid = uid;
    348 	p_sugid(p);
    349 	return (0);
    350 }
    351 
    352 /* ARGSUSED */
    353 int
    354 sys_seteuid(struct lwp *l, void *v, register_t *retval)
    355 {
    356 	struct sys_seteuid_args /* {
    357 		syscallarg(uid_t) euid;
    358 	} */ *uap = v;
    359 	struct proc *p = l->l_proc;
    360 	struct pcred *pc = p->p_cred;
    361 	uid_t euid;
    362 	int error;
    363 
    364 	euid = SCARG(uap, euid);
    365 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    366 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    367 		return (error);
    368 	/*
    369 	 * Check if we are all set, and this is a no-op.
    370 	 */
    371 	if (pc->pc_ucred->cr_uid == euid)
    372 		return (0);
    373 
    374 	/*
    375 	 * Everything's okay, do it.  Copy credentials so other references do
    376 	 * not see our changes.
    377 	 */
    378 	pc->pc_ucred = crcopy(pc->pc_ucred);
    379 	pc->pc_ucred->cr_uid = euid;
    380 	p_sugid(p);
    381 	return (0);
    382 }
    383 
    384 int
    385 sys_setreuid(struct lwp *l, void *v, register_t *retval)
    386 {
    387 	struct sys_setreuid_args /* {
    388 		syscallarg(uid_t) ruid;
    389 		syscallarg(uid_t) euid;
    390 	} */ *uap = v;
    391 	struct proc *p = l->l_proc;
    392 	struct pcred *pc = p->p_cred;
    393 	uid_t ruid, euid;
    394 	int error, changed = 0;
    395 
    396 	ruid = SCARG(uap, ruid);
    397 	euid = SCARG(uap, euid);
    398 
    399 	if (ruid != (uid_t)-1 &&
    400 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    401 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    402 		return (error);
    403 
    404 	if (euid != (uid_t)-1 &&
    405 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    406 	    euid != pc->p_svuid &&
    407 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    408 		return (error);
    409 
    410 	if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
    411 		pc->pc_ucred = crcopy(pc->pc_ucred);
    412 		pc->pc_ucred->cr_uid = euid;
    413 		changed++;
    414 	}
    415 
    416 	if (ruid != (uid_t)-1 &&
    417 	    (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
    418 		(void)chgproccnt(pc->p_ruid, -1);
    419 		(void)chgproccnt(ruid, 1);
    420 		pc->p_ruid = ruid;
    421 		pc->p_svuid = pc->pc_ucred->cr_uid;
    422 		changed++;
    423 	}
    424 
    425 	if (changed)
    426 		p_sugid(p);
    427 	return (0);
    428 }
    429 
    430 /* ARGSUSED */
    431 int
    432 sys_setgid(struct lwp *l, void *v, register_t *retval)
    433 {
    434 	struct sys_setgid_args /* {
    435 		syscallarg(gid_t) gid;
    436 	} */ *uap = v;
    437 	struct proc *p = l->l_proc;
    438 	struct pcred *pc = p->p_cred;
    439 	gid_t gid;
    440 	int error;
    441 
    442 	gid = SCARG(uap, gid);
    443 	if (gid != pc->p_rgid &&
    444 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    445 		return (error);
    446 	/*
    447 	 * Check if we are all set, and this is a no-op.
    448 	 */
    449 	if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
    450 	    pc->p_svgid == gid)
    451 		return (0);
    452 
    453 	pc->pc_ucred = crcopy(pc->pc_ucred);
    454 	pc->pc_ucred->cr_gid = gid;
    455 	pc->p_rgid = gid;
    456 	pc->p_svgid = gid;
    457 	p_sugid(p);
    458 	return (0);
    459 }
    460 
    461 /* ARGSUSED */
    462 int
    463 sys_setegid(struct lwp *l, void *v, register_t *retval)
    464 {
    465 	struct sys_setegid_args /* {
    466 		syscallarg(gid_t) egid;
    467 	} */ *uap = v;
    468 	struct proc *p = l->l_proc;
    469 	struct pcred *pc = p->p_cred;
    470 	gid_t egid;
    471 	int error;
    472 
    473 	egid = SCARG(uap, egid);
    474 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    475 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    476 		return (error);
    477 	/*
    478 	 * Check if we are all set, and this is a no-op.
    479 	 */
    480 	if (pc->pc_ucred->cr_gid == egid)
    481 		return (0);
    482 
    483 	pc->pc_ucred = crcopy(pc->pc_ucred);
    484 	pc->pc_ucred->cr_gid = egid;
    485 	p_sugid(p);
    486 	return (0);
    487 }
    488 
    489 int
    490 sys_setregid(struct lwp *l, void *v, register_t *retval)
    491 {
    492 	struct sys_setregid_args /* {
    493 		syscallarg(gid_t) rgid;
    494 		syscallarg(gid_t) egid;
    495 	} */ *uap = v;
    496 	struct proc *p = l->l_proc;
    497 	struct pcred *pc = p->p_cred;
    498 	gid_t rgid, egid;
    499 	int error, changed = 0;
    500 
    501 	rgid = SCARG(uap, rgid);
    502 	egid = SCARG(uap, egid);
    503 
    504 	if (rgid != (gid_t)-1 &&
    505 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    506 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    507 		return (error);
    508 
    509 	if (egid != (gid_t)-1 &&
    510 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    511 	    egid != pc->p_svgid &&
    512 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    513 		return (error);
    514 
    515 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
    516 		pc->pc_ucred = crcopy(pc->pc_ucred);
    517 		pc->pc_ucred->cr_gid = egid;
    518 		changed++;
    519 	}
    520 
    521 	if (rgid != (gid_t)-1 &&
    522 	    (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
    523 		pc->p_rgid = rgid;
    524 		pc->p_svgid = pc->pc_ucred->cr_gid;
    525 		changed++;
    526 	}
    527 
    528 	if (changed)
    529 		p_sugid(p);
    530 	return (0);
    531 }
    532 
    533 int
    534 sys_issetugid(struct lwp *l, void *v, register_t *retval)
    535 {
    536 	struct proc *p = l->l_proc;
    537 
    538 	/*
    539 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    540 	 * we use P_SUGID because we consider changing the owners as
    541 	 * "tainting" as well.
    542 	 * This is significant for procs that start as root and "become"
    543 	 * a user without an exec - programs cannot know *everything*
    544 	 * that libc *might* have put in their data segment.
    545 	 */
    546 	*retval = (p->p_flag & P_SUGID) != 0;
    547 	return (0);
    548 }
    549 
    550 /* ARGSUSED */
    551 int
    552 sys_setgroups(struct lwp *l, void *v, register_t *retval)
    553 {
    554 	struct sys_setgroups_args /* {
    555 		syscallarg(int) gidsetsize;
    556 		syscallarg(const gid_t *) gidset;
    557 	} */ *uap = v;
    558 	struct proc *p = l->l_proc;
    559 	struct pcred *pc = p->p_cred;
    560 	int ngrp;
    561 	int error;
    562 	gid_t grp[NGROUPS];
    563 	size_t grsize;
    564 
    565 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    566 		return (error);
    567 
    568 	ngrp = SCARG(uap, gidsetsize);
    569 	if ((u_int)ngrp > NGROUPS)
    570 		return (EINVAL);
    571 
    572 	grsize = ngrp * sizeof(gid_t);
    573 	error = copyin(SCARG(uap, gidset), grp, grsize);
    574 	if (error)
    575 		return (error);
    576 	/*
    577 	 * Check if this is a no-op.
    578 	 */
    579 	if (pc->pc_ucred->cr_ngroups == (u_int) ngrp &&
    580 	    memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
    581 		return (0);
    582 
    583 	pc->pc_ucred = crcopy(pc->pc_ucred);
    584 	(void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
    585 	pc->pc_ucred->cr_ngroups = ngrp;
    586 	p_sugid(p);
    587 	return (0);
    588 }
    589 
    590 /*
    591  * Check if gid is a member of the group set.
    592  */
    593 int
    594 groupmember(gid_t gid, const struct ucred *cred)
    595 {
    596 	const gid_t *gp;
    597 	const gid_t *egp;
    598 
    599 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    600 	for (gp = cred->cr_groups; gp < egp; gp++)
    601 		if (*gp == gid)
    602 			return (1);
    603 	return (0);
    604 }
    605 
    606 /*
    607  * Test whether the specified credentials imply "super-user"
    608  * privilege; if so, and we have accounting info, set the flag
    609  * indicating use of super-powers.
    610  * Returns 0 or error.
    611  */
    612 int
    613 suser(const struct ucred *cred, u_short *acflag)
    614 {
    615 
    616 	if (cred->cr_uid == 0) {
    617 		if (acflag)
    618 			*acflag |= ASU;
    619 		return (0);
    620 	}
    621 	return (EPERM);
    622 }
    623 
    624 /*
    625  * Allocate a zeroed cred structure.
    626  */
    627 struct ucred *
    628 crget(void)
    629 {
    630 	struct ucred *cr;
    631 
    632 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    633 	memset((caddr_t)cr, 0, sizeof(*cr));
    634 	cr->cr_ref = 1;
    635 	return (cr);
    636 }
    637 
    638 /*
    639  * Free a cred structure.
    640  * Throws away space when ref count gets to 0.
    641  */
    642 void
    643 crfree(struct ucred *cr)
    644 {
    645 
    646 	if (--cr->cr_ref == 0)
    647 		FREE((caddr_t)cr, M_CRED);
    648 }
    649 
    650 /*
    651  * Copy cred structure to a new one and free the old one.
    652  */
    653 struct ucred *
    654 crcopy(struct ucred *cr)
    655 {
    656 	struct ucred *newcr;
    657 
    658 	if (cr->cr_ref == 1)
    659 		return (cr);
    660 	newcr = crget();
    661 	*newcr = *cr;
    662 	crfree(cr);
    663 	newcr->cr_ref = 1;
    664 	return (newcr);
    665 }
    666 
    667 /*
    668  * Dup cred struct to a new held one.
    669  */
    670 struct ucred *
    671 crdup(const struct ucred *cr)
    672 {
    673 	struct ucred *newcr;
    674 
    675 	newcr = crget();
    676 	*newcr = *cr;
    677 	newcr->cr_ref = 1;
    678 	return (newcr);
    679 }
    680 
    681 /*
    682  * convert from userland credentials to kernel one
    683  */
    684 void
    685 crcvt(struct ucred *uc, const struct uucred *uuc)
    686 {
    687 
    688 	uc->cr_ref = 0;
    689 	uc->cr_uid = uuc->cr_uid;
    690 	uc->cr_gid = uuc->cr_gid;
    691 	uc->cr_ngroups = uuc->cr_ngroups;
    692 	(void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
    693 }
    694 
    695 /*
    696  * Get login name, if available.
    697  */
    698 /* ARGSUSED */
    699 int
    700 sys___getlogin(struct lwp *l, void *v, register_t *retval)
    701 {
    702 	struct sys___getlogin_args /* {
    703 		syscallarg(char *) namebuf;
    704 		syscallarg(size_t) namelen;
    705 	} */ *uap = v;
    706 	struct proc *p = l->l_proc;
    707 
    708 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    709 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    710 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    711 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    712 }
    713 
    714 /*
    715  * Set login name.
    716  */
    717 /* ARGSUSED */
    718 int
    719 sys___setlogin(struct lwp *l, void *v, register_t *retval)
    720 {
    721 	struct sys___setlogin_args /* {
    722 		syscallarg(const char *) namebuf;
    723 	} */ *uap = v;
    724 	struct proc *p = l->l_proc;
    725 	struct session *s = p->p_pgrp->pg_session;
    726 	char newname[sizeof s->s_login + 1];
    727 	int error;
    728 
    729 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    730 		return (error);
    731 	error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
    732 	if (error != 0)
    733 		return (error == ENAMETOOLONG ? EINVAL : error);
    734 
    735 	if (s->s_flags & S_LOGIN_SET && p->p_pid != s->s_sid &&
    736 	    strncmp(newname, s->s_login, sizeof s->s_login) != 0)
    737 		log(LOG_WARNING, "%s (pid %d) changing logname from "
    738 		    "%.*s to %s\n", p->p_comm, p->p_pid,
    739 		    (int)sizeof s->s_login, s->s_login, newname);
    740 	s->s_flags |= S_LOGIN_SET;
    741 	strncpy(s->s_login, newname, sizeof s->s_login);
    742 	return (0);
    743 }
    744