Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.74
      1 /*	$NetBSD: kern_prot.c,v 1.74 2003/02/18 19:26:23 wiz 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.74 2003/02/18 19:26:23 wiz 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 	*retval = p->p_ucred->cr_gid;
    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 	struct pcred *pc = p->p_cred;
    229 	u_int ngrp;
    230 	int error;
    231 
    232 	if (SCARG(uap, gidsetsize) == 0) {
    233 		*retval = pc->pc_ucred->cr_ngroups;
    234 		return (0);
    235 	} else if (SCARG(uap, gidsetsize) < 0)
    236 		return (EINVAL);
    237 	ngrp = SCARG(uap, gidsetsize);
    238 	if (ngrp < pc->pc_ucred->cr_ngroups)
    239 		return (EINVAL);
    240 	ngrp = pc->pc_ucred->cr_ngroups;
    241 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    242 			(caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    243 	if (error)
    244 		return (error);
    245 	*retval = ngrp;
    246 	return (0);
    247 }
    248 
    249 /* ARGSUSED */
    250 int
    251 sys_setsid(struct lwp *l, void *v, register_t *retval)
    252 {
    253 	struct proc *p = l->l_proc;
    254 
    255 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    256 		return (EPERM);
    257 	} else {
    258 		(void)enterpgrp(p, p->p_pid, 1);
    259 		*retval = p->p_pid;
    260 		return (0);
    261 	}
    262 }
    263 
    264 /*
    265  * set process group (setpgid/old setpgrp)
    266  *
    267  * caller does setpgid(targpid, targpgid)
    268  *
    269  * pgid must be in valid range (EINVAL)
    270  * pid must be caller or child of caller (ESRCH)
    271  * if a child
    272  *	pid must be in same session (EPERM)
    273  *	pid can't have done an exec (EACCES)
    274  * if pgid != pid
    275  * 	there must exist some pid in same session having pgid (EPERM)
    276  * pid must not be session leader (EPERM)
    277  */
    278 /* ARGSUSED */
    279 int
    280 sys_setpgid(struct lwp *l, void *v, register_t *retval)
    281 {
    282 	struct sys_setpgid_args /* {
    283 		syscallarg(int) pid;
    284 		syscallarg(int) pgid;
    285 	} */ *uap = v;
    286 	struct proc *curp = l->l_proc;
    287 	struct proc *targp;			/* target process */
    288 	struct pgrp *pgrp;			/* target pgrp */
    289 
    290 	if (SCARG(uap, pgid) < 0)
    291 		return (EINVAL);
    292 
    293 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    294 		if ((targp = pfind(SCARG(uap, pid))) == 0
    295 		    || !inferior(targp, curp))
    296 			return (ESRCH);
    297 		if (targp->p_session != curp->p_session)
    298 			return (EPERM);
    299 		if (targp->p_flag & P_EXEC)
    300 			return (EACCES);
    301 	} else
    302 		targp = curp;
    303 	if (SESS_LEADER(targp))
    304 		return (EPERM);
    305 	if (SCARG(uap, pgid) == 0)
    306 		SCARG(uap, pgid) = targp->p_pid;
    307 	else if (SCARG(uap, pgid) != targp->p_pid)
    308 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    309 	            pgrp->pg_session != curp->p_session)
    310 			return (EPERM);
    311 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    312 }
    313 
    314 /* ARGSUSED */
    315 int
    316 sys_setuid(struct lwp *l, void *v, register_t *retval)
    317 {
    318 	struct sys_setuid_args /* {
    319 		syscallarg(uid_t) uid;
    320 	} */ *uap = v;
    321 	struct proc *p = l->l_proc;
    322 	struct pcred *pc = p->p_cred;
    323 	uid_t uid;
    324 	int error;
    325 
    326 	uid = SCARG(uap, uid);
    327 	if (uid != pc->p_ruid &&
    328 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    329 		return (error);
    330 	/*
    331 	 * Check if we are all set, and this is a no-op.
    332 	 */
    333 	if (pc->p_ruid == uid && pc->p_svuid == uid &&
    334 	    pc->pc_ucred->cr_uid == uid)
    335 		return (0);
    336 	/*
    337 	 * Everything's okay, do it.
    338 	 * Transfer proc count to new user.
    339 	 * Copy credentials so other references do not see our changes.
    340 	 */
    341 	(void)chgproccnt(pc->p_ruid, -1);
    342 	(void)chgproccnt(uid, 1);
    343 	pc->pc_ucred = crcopy(pc->pc_ucred);
    344 	pc->pc_ucred->cr_uid = uid;
    345 	pc->p_ruid = uid;
    346 	pc->p_svuid = uid;
    347 	p_sugid(p);
    348 	return (0);
    349 }
    350 
    351 /* ARGSUSED */
    352 int
    353 sys_seteuid(struct lwp *l, void *v, register_t *retval)
    354 {
    355 	struct sys_seteuid_args /* {
    356 		syscallarg(uid_t) euid;
    357 	} */ *uap = v;
    358 	struct proc *p = l->l_proc;
    359 	struct pcred *pc = p->p_cred;
    360 	uid_t euid;
    361 	int error;
    362 
    363 	euid = SCARG(uap, euid);
    364 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    365 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    366 		return (error);
    367 	/*
    368 	 * Check if we are all set, and this is a no-op.
    369 	 */
    370 	if (pc->pc_ucred->cr_uid == euid)
    371 		return (0);
    372 
    373 	/*
    374 	 * Everything's okay, do it.  Copy credentials so other references do
    375 	 * not see our changes.
    376 	 */
    377 	pc->pc_ucred = crcopy(pc->pc_ucred);
    378 	pc->pc_ucred->cr_uid = euid;
    379 	p_sugid(p);
    380 	return (0);
    381 }
    382 
    383 int
    384 sys_setreuid(struct lwp *l, void *v, register_t *retval)
    385 {
    386 	struct sys_setreuid_args /* {
    387 		syscallarg(uid_t) ruid;
    388 		syscallarg(uid_t) euid;
    389 	} */ *uap = v;
    390 	struct proc *p = l->l_proc;
    391 	struct pcred *pc = p->p_cred;
    392 	uid_t ruid, euid;
    393 	int error, changed = 0;
    394 
    395 	ruid = SCARG(uap, ruid);
    396 	euid = SCARG(uap, euid);
    397 
    398 	if (ruid != (uid_t)-1 &&
    399 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    400 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    401 		return (error);
    402 
    403 	if (euid != (uid_t)-1 &&
    404 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    405 	    euid != pc->p_svuid &&
    406 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    407 		return (error);
    408 
    409 	if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
    410 		pc->pc_ucred = crcopy(pc->pc_ucred);
    411 		pc->pc_ucred->cr_uid = euid;
    412 		changed++;
    413 	}
    414 
    415 	if (ruid != (uid_t)-1 &&
    416 	    (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
    417 		(void)chgproccnt(pc->p_ruid, -1);
    418 		(void)chgproccnt(ruid, 1);
    419 		pc->p_ruid = ruid;
    420 		pc->p_svuid = pc->pc_ucred->cr_uid;
    421 		changed++;
    422 	}
    423 
    424 	if (changed)
    425 		p_sugid(p);
    426 	return (0);
    427 }
    428 
    429 /* ARGSUSED */
    430 int
    431 sys_setgid(struct lwp *l, void *v, register_t *retval)
    432 {
    433 	struct sys_setgid_args /* {
    434 		syscallarg(gid_t) gid;
    435 	} */ *uap = v;
    436 	struct proc *p = l->l_proc;
    437 	struct pcred *pc = p->p_cred;
    438 	gid_t gid;
    439 	int error;
    440 
    441 	gid = SCARG(uap, gid);
    442 	if (gid != pc->p_rgid &&
    443 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    444 		return (error);
    445 	/*
    446 	 * Check if we are all set, and this is a no-op.
    447 	 */
    448 	if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
    449 	    pc->p_svgid == gid)
    450 		return (0);
    451 
    452 	pc->pc_ucred = crcopy(pc->pc_ucred);
    453 	pc->pc_ucred->cr_gid = gid;
    454 	pc->p_rgid = gid;
    455 	pc->p_svgid = gid;
    456 	p_sugid(p);
    457 	return (0);
    458 }
    459 
    460 /* ARGSUSED */
    461 int
    462 sys_setegid(struct lwp *l, void *v, register_t *retval)
    463 {
    464 	struct sys_setegid_args /* {
    465 		syscallarg(gid_t) egid;
    466 	} */ *uap = v;
    467 	struct proc *p = l->l_proc;
    468 	struct pcred *pc = p->p_cred;
    469 	gid_t egid;
    470 	int error;
    471 
    472 	egid = SCARG(uap, egid);
    473 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    474 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    475 		return (error);
    476 	/*
    477 	 * Check if we are all set, and this is a no-op.
    478 	 */
    479 	if (pc->pc_ucred->cr_gid == egid)
    480 		return (0);
    481 
    482 	pc->pc_ucred = crcopy(pc->pc_ucred);
    483 	pc->pc_ucred->cr_gid = egid;
    484 	p_sugid(p);
    485 	return (0);
    486 }
    487 
    488 int
    489 sys_setregid(struct lwp *l, void *v, register_t *retval)
    490 {
    491 	struct sys_setregid_args /* {
    492 		syscallarg(gid_t) rgid;
    493 		syscallarg(gid_t) egid;
    494 	} */ *uap = v;
    495 	struct proc *p = l->l_proc;
    496 	struct pcred *pc = p->p_cred;
    497 	gid_t rgid, egid;
    498 	int error, changed = 0;
    499 
    500 	rgid = SCARG(uap, rgid);
    501 	egid = SCARG(uap, egid);
    502 
    503 	if (rgid != (gid_t)-1 &&
    504 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    505 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    506 		return (error);
    507 
    508 	if (egid != (gid_t)-1 &&
    509 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    510 	    egid != pc->p_svgid &&
    511 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    512 		return (error);
    513 
    514 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
    515 		pc->pc_ucred = crcopy(pc->pc_ucred);
    516 		pc->pc_ucred->cr_gid = egid;
    517 		changed++;
    518 	}
    519 
    520 	if (rgid != (gid_t)-1 &&
    521 	    (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
    522 		pc->p_rgid = rgid;
    523 		pc->p_svgid = pc->pc_ucred->cr_gid;
    524 		changed++;
    525 	}
    526 
    527 	if (changed)
    528 		p_sugid(p);
    529 	return (0);
    530 }
    531 
    532 int
    533 sys_issetugid(struct lwp *l, void *v, register_t *retval)
    534 {
    535 	struct proc *p = l->l_proc;
    536 	/*
    537 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    538 	 * we use P_SUGID because we consider changing the owners as
    539 	 * "tainting" as well.
    540 	 * This is significant for procs that start as root and "become"
    541 	 * a user without an exec - programs cannot know *everything*
    542 	 * that libc *might* have put in their data segment.
    543 	 */
    544 	*retval = (p->p_flag & P_SUGID) != 0;
    545 	return (0);
    546 }
    547 
    548 /* ARGSUSED */
    549 int
    550 sys_setgroups(struct lwp *l, void *v, register_t *retval)
    551 {
    552 	struct sys_setgroups_args /* {
    553 		syscallarg(int) gidsetsize;
    554 		syscallarg(const gid_t *) gidset;
    555 	} */ *uap = v;
    556 	struct proc *p = l->l_proc;
    557 	struct pcred *pc = p->p_cred;
    558 	int ngrp;
    559 	int error;
    560 	gid_t grp[NGROUPS];
    561 	size_t grsize;
    562 
    563 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    564 		return (error);
    565 
    566 	ngrp = SCARG(uap, gidsetsize);
    567 	if ((u_int)ngrp > NGROUPS)
    568 		return (EINVAL);
    569 
    570 	grsize = ngrp * sizeof(gid_t);
    571 	error = copyin(SCARG(uap, gidset), grp, grsize);
    572 	if (error)
    573 		return (error);
    574 	/*
    575 	 * Check if this is a no-op.
    576 	 */
    577 	if (pc->pc_ucred->cr_ngroups == (u_int) ngrp &&
    578 	    memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
    579 		return (0);
    580 
    581 	pc->pc_ucred = crcopy(pc->pc_ucred);
    582 	(void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
    583 	pc->pc_ucred->cr_ngroups = ngrp;
    584 	p_sugid(p);
    585 	return (0);
    586 }
    587 
    588 /*
    589  * Check if gid is a member of the group set.
    590  */
    591 int
    592 groupmember(gid_t gid, const struct ucred *cred)
    593 {
    594 	const gid_t *gp;
    595 	const gid_t *egp;
    596 
    597 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    598 	for (gp = cred->cr_groups; gp < egp; gp++)
    599 		if (*gp == gid)
    600 			return (1);
    601 	return (0);
    602 }
    603 
    604 /*
    605  * Test whether the specified credentials imply "super-user"
    606  * privilege; if so, and we have accounting info, set the flag
    607  * indicating use of super-powers.
    608  * Returns 0 or error.
    609  */
    610 int
    611 suser(const struct ucred *cred, u_short *acflag)
    612 {
    613 	if (cred->cr_uid == 0) {
    614 		if (acflag)
    615 			*acflag |= ASU;
    616 		return (0);
    617 	}
    618 	return (EPERM);
    619 }
    620 
    621 /*
    622  * Allocate a zeroed cred structure.
    623  */
    624 struct ucred *
    625 crget(void)
    626 {
    627 	struct ucred *cr;
    628 
    629 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    630 	memset((caddr_t)cr, 0, sizeof(*cr));
    631 	cr->cr_ref = 1;
    632 	return (cr);
    633 }
    634 
    635 /*
    636  * Free a cred structure.
    637  * Throws away space when ref count gets to 0.
    638  */
    639 void
    640 crfree(struct ucred *cr)
    641 {
    642 
    643 	if (--cr->cr_ref == 0)
    644 		FREE((caddr_t)cr, M_CRED);
    645 }
    646 
    647 /*
    648  * Copy cred structure to a new one and free the old one.
    649  */
    650 struct ucred *
    651 crcopy(struct ucred *cr)
    652 {
    653 	struct ucred *newcr;
    654 
    655 	if (cr->cr_ref == 1)
    656 		return (cr);
    657 	newcr = crget();
    658 	*newcr = *cr;
    659 	crfree(cr);
    660 	newcr->cr_ref = 1;
    661 	return (newcr);
    662 }
    663 
    664 /*
    665  * Dup cred struct to a new held one.
    666  */
    667 struct ucred *
    668 crdup(const struct ucred *cr)
    669 {
    670 	struct ucred *newcr;
    671 
    672 	newcr = crget();
    673 	*newcr = *cr;
    674 	newcr->cr_ref = 1;
    675 	return (newcr);
    676 }
    677 
    678 /*
    679  * convert from userland credentials to kernel one
    680  */
    681 void
    682 crcvt(struct ucred *uc, const struct uucred *uuc)
    683 {
    684 	uc->cr_ref = 0;
    685 	uc->cr_uid = uuc->cr_uid;
    686 	uc->cr_gid = uuc->cr_gid;
    687 	uc->cr_ngroups = uuc->cr_ngroups;
    688 	(void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
    689 }
    690 
    691 /*
    692  * Get login name, if available.
    693  */
    694 /* ARGSUSED */
    695 int
    696 sys___getlogin(struct lwp *l, void *v, register_t *retval)
    697 {
    698 	struct sys___getlogin_args /* {
    699 		syscallarg(char *) namebuf;
    700 		syscallarg(size_t) namelen;
    701 	} */ *uap = v;
    702 	struct proc *p = l->l_proc;
    703 
    704 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    705 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    706 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    707 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    708 }
    709 
    710 /*
    711  * Set login name.
    712  */
    713 /* ARGSUSED */
    714 int
    715 sys___setlogin(struct lwp *l, void *v, register_t *retval)
    716 {
    717 	struct sys___setlogin_args /* {
    718 		syscallarg(const char *) namebuf;
    719 	} */ *uap = v;
    720 	struct proc *p = l->l_proc;
    721 	struct session *s = p->p_pgrp->pg_session;
    722 	char newname[sizeof s->s_login + 1];
    723 	int error;
    724 
    725 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    726 		return (error);
    727 	error = copyinstr(SCARG(uap, namebuf), &newname, sizeof newname, NULL);
    728 	if (error != 0)
    729 		return error == ENAMETOOLONG ? EINVAL : error;
    730 
    731 	if (s->s_flags & S_LOGIN_SET && p->p_pid != s->s_sid &&
    732 	    strncmp(newname, s->s_login, sizeof s->s_login) != 0)
    733 		log(LOG_WARNING, "%s (pid %d) changing logname from "
    734 		    "%.*s to %s\n", p->p_comm, p->p_pid,
    735 		    (int)sizeof s->s_login, s->s_login, newname);
    736 	s->s_flags |= S_LOGIN_SET;
    737 	strncpy(s->s_login, newname, sizeof s->s_login);
    738 	return 0;
    739 }
    740