Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.59
      1 /*	$NetBSD: kern_prot.c,v 1.59 2000/10/17 20:53:45 christos 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 "opt_compat_freebsd.h"
     48 #include "opt_compat_ibcs2.h"
     49 #include "opt_compat_sunos.h"
     50 #include "opt_compat_linux.h"
     51 #include "opt_compat_43.h"
     52 #include "opt_compat_osf1.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/acct.h>
     56 #include <sys/systm.h>
     57 #include <sys/ucred.h>
     58 #include <sys/proc.h>
     59 #include <sys/timeb.h>
     60 #include <sys/times.h>
     61 #include <sys/malloc.h>
     62 
     63 #include <sys/mount.h>
     64 #include <sys/syscallargs.h>
     65 
     66 /* ARGSUSED */
     67 int
     68 sys_getpid(p, v, retval)
     69 	struct proc *p;
     70 	void *v;
     71 	register_t *retval;
     72 {
     73 
     74 	*retval = p->p_pid;
     75 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
     76     defined(COMPAT_FREEBSD) || defined(COMPAT_OSF1) || \
     77     (defined(COMPAT_LINUX) && defined(__alpha__))
     78 	retval[1] = p->p_pptr->p_pid;
     79 #endif
     80 	return (0);
     81 }
     82 
     83 /* ARGSUSED */
     84 int
     85 sys_getppid(p, v, retval)
     86 	struct proc *p;
     87 	void *v;
     88 	register_t *retval;
     89 {
     90 
     91 	*retval = p->p_pptr->p_pid;
     92 	return (0);
     93 }
     94 
     95 /* Get process group ID; note that POSIX getpgrp takes no parameter */
     96 int
     97 sys_getpgrp(p, v, retval)
     98 	struct proc *p;
     99 	void *v;
    100 	register_t *retval;
    101 {
    102 
    103 	*retval = p->p_pgrp->pg_id;
    104 	return (0);
    105 }
    106 
    107 /*
    108  * Return the process group ID of the session leader (session ID)
    109  * for the specified process.
    110  */
    111 int
    112 sys_getsid(p, v, retval)
    113 	struct proc *p;
    114 	void *v;
    115 	register_t *retval;
    116 {
    117 	struct sys_getsid_args /* {
    118 		syscalldarg(pid_t) pid;
    119 	} */ *uap = v;
    120 
    121 	if (SCARG(uap, pid) == 0)
    122 		goto found;
    123 	if ((p = pfind(SCARG(uap, pid))) == 0)
    124 		return (ESRCH);
    125 found:
    126 	*retval = p->p_session->s_sid;
    127 	return 0;
    128 }
    129 
    130 int
    131 sys_getpgid(p, v, retval)
    132 	struct proc *p;
    133 	void *v;
    134 	register_t *retval;
    135 {
    136 	struct sys_getpgid_args /* {
    137 		syscallarg(pid_t) pid;
    138 	} */ *uap = v;
    139 
    140 	if (SCARG(uap, pid) == 0)
    141 		goto found;
    142 	if ((p = pfind(SCARG(uap, pid))) == 0)
    143 		return (ESRCH);
    144 found:
    145 	*retval = p->p_pgid;
    146 	return 0;
    147 }
    148 
    149 /* ARGSUSED */
    150 int
    151 sys_getuid(p, v, retval)
    152 	struct proc *p;
    153 	void *v;
    154 	register_t *retval;
    155 {
    156 
    157 	*retval = p->p_cred->p_ruid;
    158 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
    159     defined(COMPAT_FREEBSD) || defined(COMPAT_OSF1) || \
    160     (defined(COMPAT_LINUX) && defined(__alpha__))
    161 	retval[1] = p->p_ucred->cr_uid;
    162 #endif
    163 	return (0);
    164 }
    165 
    166 /* ARGSUSED */
    167 int
    168 sys_geteuid(p, v, retval)
    169 	struct proc *p;
    170 	void *v;
    171 	register_t *retval;
    172 {
    173 
    174 	*retval = p->p_ucred->cr_uid;
    175 	return (0);
    176 }
    177 
    178 /* ARGSUSED */
    179 int
    180 sys_getgid(p, v, retval)
    181 	struct proc *p;
    182 	void *v;
    183 	register_t *retval;
    184 {
    185 
    186 	*retval = p->p_cred->p_rgid;
    187 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_FREEBSD) || \
    188     defined(COMPAT_OSF1) || (defined(COMPAT_LINUX) && defined(alpha))
    189 	retval[1] = p->p_ucred->cr_gid;
    190 #endif
    191 	return (0);
    192 }
    193 
    194 /*
    195  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    196  * via getgroups.  This syscall exists because it is somewhat painful to do
    197  * correctly in a library function.
    198  */
    199 /* ARGSUSED */
    200 int
    201 sys_getegid(p, v, retval)
    202 	struct proc *p;
    203 	void *v;
    204 	register_t *retval;
    205 {
    206 
    207 	*retval = p->p_ucred->cr_gid;
    208 	return (0);
    209 }
    210 
    211 int
    212 sys_getgroups(p, v, retval)
    213 	struct proc *p;
    214 	void *v;
    215 	register_t *retval;
    216 {
    217 	struct sys_getgroups_args /* {
    218 		syscallarg(int) gidsetsize;
    219 		syscallarg(gid_t *) gidset;
    220 	} */ *uap = v;
    221 	struct pcred *pc = p->p_cred;
    222 	int ngrp;
    223 	int error;
    224 
    225 	ngrp = SCARG(uap, gidsetsize);
    226 	if (ngrp == 0) {
    227 		*retval = pc->pc_ucred->cr_ngroups;
    228 		return (0);
    229 	}
    230 	if (ngrp < pc->pc_ucred->cr_ngroups)
    231 		return (EINVAL);
    232 	ngrp = pc->pc_ucred->cr_ngroups;
    233 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    234 			(caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    235 	if (error)
    236 		return (error);
    237 	*retval = ngrp;
    238 	return (0);
    239 }
    240 
    241 /* ARGSUSED */
    242 int
    243 sys_setsid(p, v, retval)
    244 	struct proc *p;
    245 	void *v;
    246 	register_t *retval;
    247 {
    248 
    249 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    250 		return (EPERM);
    251 	} else {
    252 		(void)enterpgrp(p, p->p_pid, 1);
    253 		*retval = p->p_pid;
    254 		return (0);
    255 	}
    256 }
    257 
    258 /*
    259  * set process group (setpgid/old setpgrp)
    260  *
    261  * caller does setpgid(targpid, targpgid)
    262  *
    263  * pgid must be in valid range (EINVAL)
    264  * pid must be caller or child of caller (ESRCH)
    265  * if a child
    266  *	pid must be in same session (EPERM)
    267  *	pid can't have done an exec (EACCES)
    268  * if pgid != pid
    269  * 	there must exist some pid in same session having pgid (EPERM)
    270  * pid must not be session leader (EPERM)
    271  */
    272 /* ARGSUSED */
    273 int
    274 sys_setpgid(curp, v, retval)
    275 	struct proc *curp;
    276 	void *v;
    277 	register_t *retval;
    278 {
    279 	struct sys_setpgid_args /* {
    280 		syscallarg(int) pid;
    281 		syscallarg(int) pgid;
    282 	} */ *uap = v;
    283 	struct proc *targp;			/* target process */
    284 	struct pgrp *pgrp;			/* target pgrp */
    285 
    286 	if (SCARG(uap, pgid) < 0)
    287 		return (EINVAL);
    288 
    289 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    290 		if ((targp = pfind(SCARG(uap, pid))) == 0
    291 		    || !inferior(targp, curp))
    292 			return (ESRCH);
    293 		if (targp->p_session != curp->p_session)
    294 			return (EPERM);
    295 		if (targp->p_flag & P_EXEC)
    296 			return (EACCES);
    297 	} else
    298 		targp = curp;
    299 	if (SESS_LEADER(targp))
    300 		return (EPERM);
    301 	if (SCARG(uap, pgid) == 0)
    302 		SCARG(uap, pgid) = targp->p_pid;
    303 	else if (SCARG(uap, pgid) != targp->p_pid)
    304 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    305 	            pgrp->pg_session != curp->p_session)
    306 			return (EPERM);
    307 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    308 }
    309 
    310 /* ARGSUSED */
    311 int
    312 sys_setuid(p, v, retval)
    313 	struct proc *p;
    314 	void *v;
    315 	register_t *retval;
    316 {
    317 	struct sys_setuid_args /* {
    318 		syscallarg(uid_t) uid;
    319 	} */ *uap = v;
    320 	struct pcred *pc = p->p_cred;
    321 	uid_t uid;
    322 	int error;
    323 
    324 	uid = SCARG(uap, uid);
    325 	if (uid != pc->p_ruid &&
    326 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    327 		return (error);
    328 	/*
    329 	 * check if we are all set, and this is a no-op
    330 	 */
    331 	if (pc->p_ruid == uid && pc->p_svuid == uid &&
    332 	    pc->pc_ucred->cr_uid == uid)
    333 		return 0;
    334 	/*
    335 	 * Everything's okay, do it.
    336 	 * Transfer proc count to new user.
    337 	 * Copy credentials so other references do not see our changes.
    338 	 */
    339 	(void)chgproccnt(pc->p_ruid, -1);
    340 	(void)chgproccnt(uid, 1);
    341 	pc->pc_ucred = crcopy(pc->pc_ucred);
    342 	pc->pc_ucred->cr_uid = uid;
    343 	pc->p_ruid = uid;
    344 	pc->p_svuid = uid;
    345 	p_sugid(p);
    346 	return (0);
    347 }
    348 
    349 /* ARGSUSED */
    350 int
    351 sys_seteuid(p, v, retval)
    352 	struct proc *p;
    353 	void *v;
    354 	register_t *retval;
    355 {
    356 	struct sys_seteuid_args /* {
    357 		syscallarg(uid_t) euid;
    358 	} */ *uap = v;
    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(p, v, retval)
    385 	struct proc *p;
    386 	void *v;
    387 	register_t *retval;
    388 {
    389 	struct sys_setreuid_args /* {
    390 		syscallarg(uid_t) ruid;
    391 		syscallarg(uid_t) euid;
    392 	} */ *uap = v;
    393 	struct pcred *pc = p->p_cred;
    394 	uid_t ruid, euid;
    395 	int error, changed = 0;
    396 
    397 	ruid = SCARG(uap, ruid);
    398 	euid = SCARG(uap, euid);
    399 
    400 	if (ruid != (uid_t)-1 &&
    401 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    402 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    403 		return (error);
    404 
    405 	if (euid != (uid_t)-1 &&
    406 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    407 	    euid != pc->p_svuid &&
    408 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    409 		return (error);
    410 
    411 	if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
    412 		pc->pc_ucred = crcopy(pc->pc_ucred);
    413 		pc->pc_ucred->cr_uid = euid;
    414 		changed++;
    415 	}
    416 
    417 	if (ruid != (uid_t)-1 &&
    418 	    (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
    419 		(void)chgproccnt(pc->p_ruid, -1);
    420 		(void)chgproccnt(ruid, 1);
    421 		pc->p_ruid = ruid;
    422 		pc->p_svuid = pc->pc_ucred->cr_uid;
    423 		changed++;
    424 	}
    425 
    426 	if (changed)
    427 		p_sugid(p);
    428 	return (0);
    429 }
    430 
    431 /* ARGSUSED */
    432 int
    433 sys_setgid(p, v, retval)
    434 	struct proc *p;
    435 	void *v;
    436 	register_t *retval;
    437 {
    438 	struct sys_setgid_args /* {
    439 		syscallarg(gid_t) gid;
    440 	} */ *uap = v;
    441 	struct pcred *pc = p->p_cred;
    442 	gid_t gid;
    443 	int error;
    444 
    445 	gid = SCARG(uap, gid);
    446 	if (gid != pc->p_rgid &&
    447 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    448 		return (error);
    449 	/*
    450 	 * check if we are all set, and this is a no-op
    451 	 */
    452 	if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
    453 	    pc->p_svgid == gid)
    454 		return 0;
    455 
    456 	pc->pc_ucred = crcopy(pc->pc_ucred);
    457 	pc->pc_ucred->cr_gid = gid;
    458 	pc->p_rgid = gid;
    459 	pc->p_svgid = gid;
    460 	p_sugid(p);
    461 	return (0);
    462 }
    463 
    464 /* ARGSUSED */
    465 int
    466 sys_setegid(p, v, retval)
    467 	struct proc *p;
    468 	void *v;
    469 	register_t *retval;
    470 {
    471 	struct sys_setegid_args /* {
    472 		syscallarg(gid_t) egid;
    473 	} */ *uap = v;
    474 	struct pcred *pc = p->p_cred;
    475 	gid_t egid;
    476 	int error;
    477 
    478 	egid = SCARG(uap, egid);
    479 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    480 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    481 		return (error);
    482 	/*
    483 	 * check if we are all set, and this is a no-op
    484 	 */
    485 	if (pc->pc_ucred->cr_gid == egid)
    486 		return 0;
    487 
    488 	pc->pc_ucred = crcopy(pc->pc_ucred);
    489 	pc->pc_ucred->cr_gid = egid;
    490 	p_sugid(p);
    491 	return (0);
    492 }
    493 
    494 int
    495 sys_setregid(p, v, retval)
    496 	struct proc *p;
    497 	void *v;
    498 	register_t *retval;
    499 {
    500 	struct sys_setregid_args /* {
    501 		syscallarg(gid_t) rgid;
    502 		syscallarg(gid_t) egid;
    503 	} */ *uap = v;
    504 	struct pcred *pc = p->p_cred;
    505 	gid_t rgid, egid;
    506 	int error, changed = 0;
    507 
    508 	rgid = SCARG(uap, rgid);
    509 	egid = SCARG(uap, egid);
    510 
    511 	if (rgid != (gid_t)-1 &&
    512 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    513 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    514 		return (error);
    515 
    516 	if (egid != (gid_t)-1 &&
    517 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    518 	    egid != pc->p_svgid &&
    519 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    520 		return (error);
    521 
    522 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
    523 		pc->pc_ucred = crcopy(pc->pc_ucred);
    524 		pc->pc_ucred->cr_gid = egid;
    525 		changed++;
    526 	}
    527 
    528 	if (rgid != (gid_t)-1 &&
    529 	    (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
    530 		pc->p_rgid = rgid;
    531 		pc->p_svgid = pc->pc_ucred->cr_gid;
    532 		changed++;
    533 	}
    534 
    535 	if (changed)
    536 		p_sugid(p);
    537 	return (0);
    538 }
    539 
    540 int
    541 sys_issetugid(p, v, retval)
    542 	struct proc *p;
    543 	void *v;
    544 	register_t *retval;
    545 {
    546 	/*
    547 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    548 	 * we use P_SUGID because we consider changing the owners as
    549 	 * "tainting" as well.
    550 	 * This is significant for procs that start as root and "become"
    551 	 * a user without an exec - programs cannot know *everything*
    552 	 * that libc *might* have put in their data segment.
    553 	 */
    554 	*retval = (p->p_flag & P_SUGID) != 0;
    555 	return 0;
    556 }
    557 
    558 /* ARGSUSED */
    559 int
    560 sys_setgroups(p, v, retval)
    561 	struct proc *p;
    562 	void *v;
    563 	register_t *retval;
    564 {
    565 	struct sys_setgroups_args /* {
    566 		syscallarg(int) gidsetsize;
    567 		syscallarg(const gid_t *) gidset;
    568 	} */ *uap = v;
    569 	struct pcred *pc = p->p_cred;
    570 	int ngrp;
    571 	int error;
    572 	gid_t grp[NGROUPS];
    573 	size_t grsize;
    574 
    575 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    576 		return (error);
    577 
    578 	ngrp = SCARG(uap, gidsetsize);
    579 	if ((u_int)ngrp > NGROUPS)
    580 		return (EINVAL);
    581 
    582 	grsize = ngrp * sizeof(gid_t);
    583 	error = copyin(SCARG(uap, gidset), grp, grsize);
    584 	if (error)
    585 		return (error);
    586 	/*
    587 	 * check if this is a no-op
    588 	 */
    589 	if (pc->pc_ucred->cr_ngroups == ngrp &&
    590 	    memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
    591 		return 0;
    592 
    593 	pc->pc_ucred = crcopy(pc->pc_ucred);
    594 	(void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
    595 	pc->pc_ucred->cr_ngroups = ngrp;
    596 	p_sugid(p);
    597 	return (0);
    598 }
    599 
    600 /*
    601  * Check if gid is a member of the group set.
    602  */
    603 int
    604 groupmember(gid, cred)
    605 	gid_t gid;
    606 	struct ucred *cred;
    607 {
    608 	gid_t *gp;
    609 	gid_t *egp;
    610 
    611 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    612 	for (gp = cred->cr_groups; gp < egp; gp++)
    613 		if (*gp == gid)
    614 			return (1);
    615 	return (0);
    616 }
    617 
    618 /*
    619  * Test whether the specified credentials imply "super-user"
    620  * privilege; if so, and we have accounting info, set the flag
    621  * indicating use of super-powers.
    622  * Returns 0 or error.
    623  */
    624 int
    625 suser(cred, acflag)
    626 	struct ucred *cred;
    627 	u_short *acflag;
    628 {
    629 	if (cred->cr_uid == 0) {
    630 		if (acflag)
    631 			*acflag |= ASU;
    632 		return (0);
    633 	}
    634 	return (EPERM);
    635 }
    636 
    637 /*
    638  * Allocate a zeroed cred structure.
    639  */
    640 struct ucred *
    641 crget()
    642 {
    643 	struct ucred *cr;
    644 
    645 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    646 	memset((caddr_t)cr, 0, sizeof(*cr));
    647 	cr->cr_ref = 1;
    648 	return (cr);
    649 }
    650 
    651 /*
    652  * Free a cred structure.
    653  * Throws away space when ref count gets to 0.
    654  */
    655 void
    656 crfree(cr)
    657 	struct ucred *cr;
    658 {
    659 	int s;
    660 
    661 	s = splimp();				/* ??? */
    662 	if (--cr->cr_ref == 0)
    663 		FREE((caddr_t)cr, M_CRED);
    664 	(void) splx(s);
    665 }
    666 
    667 /*
    668  * Copy cred structure to a new one and free the old one.
    669  */
    670 struct ucred *
    671 crcopy(cr)
    672 	struct ucred *cr;
    673 {
    674 	struct ucred *newcr;
    675 
    676 	if (cr->cr_ref == 1)
    677 		return (cr);
    678 	newcr = crget();
    679 	*newcr = *cr;
    680 	crfree(cr);
    681 	newcr->cr_ref = 1;
    682 	return (newcr);
    683 }
    684 
    685 /*
    686  * Dup cred struct to a new held one.
    687  */
    688 struct ucred *
    689 crdup(cr)
    690 	struct ucred *cr;
    691 {
    692 	struct ucred *newcr;
    693 
    694 	newcr = crget();
    695 	*newcr = *cr;
    696 	newcr->cr_ref = 1;
    697 	return (newcr);
    698 }
    699 
    700 /*
    701  * Get login name, if available.
    702  */
    703 /* ARGSUSED */
    704 int
    705 sys___getlogin(p, v, retval)
    706 	struct proc *p;
    707 	void *v;
    708 	register_t *retval;
    709 {
    710 	struct sys___getlogin_args /* {
    711 		syscallarg(char *) namebuf;
    712 		syscallarg(size_t) namelen;
    713 	} */ *uap = v;
    714 
    715 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    716 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    717 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    718 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    719 }
    720 
    721 /*
    722  * Set login name.
    723  */
    724 /* ARGSUSED */
    725 int
    726 sys_setlogin(p, v, retval)
    727 	struct proc *p;
    728 	void *v;
    729 	register_t *retval;
    730 {
    731 	struct sys_setlogin_args /* {
    732 		syscallarg(const char *) namebuf;
    733 	} */ *uap = v;
    734 	int error;
    735 
    736 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    737 		return (error);
    738 	error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
    739 	    sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
    740 	if (error == ENAMETOOLONG)
    741 		error = EINVAL;
    742 	return (error);
    743 }
    744