xref: /onnv-gate/usr/src/uts/common/os/cred.c (revision 9710:d02d4803985d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229151SThomas.Haynes@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/cred_impl.h>
440Sstevel@tonic-gate #include <sys/policy.h>
450Sstevel@tonic-gate #include <sys/vnode.h>
460Sstevel@tonic-gate #include <sys/errno.h>
470Sstevel@tonic-gate #include <sys/kmem.h>
480Sstevel@tonic-gate #include <sys/user.h>
490Sstevel@tonic-gate #include <sys/proc.h>
500Sstevel@tonic-gate #include <sys/syscall.h>
510Sstevel@tonic-gate #include <sys/debug.h>
520Sstevel@tonic-gate #include <sys/atomic.h>
530Sstevel@tonic-gate #include <sys/ucred.h>
540Sstevel@tonic-gate #include <sys/prsystm.h>
550Sstevel@tonic-gate #include <sys/modctl.h>
564321Scasper #include <sys/avl.h>
575331Samw #include <sys/door.h>
580Sstevel@tonic-gate #include <c2/audit.h>
590Sstevel@tonic-gate #include <sys/zone.h>
601676Sjpk #include <sys/tsol/label.h>
614321Scasper #include <sys/sid.h>
624520Snw141292 #include <sys/idmap.h>
636134Scasper #include <sys/klpd.h>
645331Samw #include <sys/varargs.h>
654321Scasper 
665771Sjp151216 
675771Sjp151216 /* Ephemeral IDs Zones specific data */
685771Sjp151216 typedef struct ephemeral_zsd {
695771Sjp151216 	uid_t		min_uid;
705771Sjp151216 	uid_t		last_uid;
715771Sjp151216 	gid_t		min_gid;
725771Sjp151216 	gid_t		last_gid;
734321Scasper 	kmutex_t	eph_lock;
745771Sjp151216 	cred_t		*eph_nobody;
755771Sjp151216 } ephemeral_zsd_t;
765771Sjp151216 
775771Sjp151216 
785771Sjp151216 static kmutex_t		ephemeral_zone_mutex;
795771Sjp151216 static zone_key_t	ephemeral_zone_key;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static struct kmem_cache *cred_cache;
825771Sjp151216 static size_t		crsize = 0;
835771Sjp151216 static int		audoff = 0;
845771Sjp151216 uint32_t		ucredsize;
855771Sjp151216 cred_t			*kcred;
865771Sjp151216 static cred_t		*dummycr;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static int get_c2audit_load(void);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
930Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
940Sstevel@tonic-gate 
951676Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
960Sstevel@tonic-gate 
974321Scasper 
984321Scasper static boolean_t hasephids = B_FALSE;
994321Scasper 
1005771Sjp151216 static ephemeral_zsd_t *
1015771Sjp151216 get_ephemeral_zsd(zone_t *zone)
1025771Sjp151216 {
1035771Sjp151216 	ephemeral_zsd_t *eph_zsd;
1045771Sjp151216 
1055771Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
1065771Sjp151216 	if (eph_zsd != NULL) {
1075771Sjp151216 		return (eph_zsd);
1085771Sjp151216 	}
1095771Sjp151216 
1105771Sjp151216 	mutex_enter(&ephemeral_zone_mutex);
1115771Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
1125771Sjp151216 	if (eph_zsd == NULL) {
1135771Sjp151216 		eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP);
1145771Sjp151216 		eph_zsd->min_uid = MAXUID;
1155771Sjp151216 		eph_zsd->last_uid = IDMAP_WK__MAX_UID;
1165771Sjp151216 		eph_zsd->min_gid = MAXUID;
1175771Sjp151216 		eph_zsd->last_gid = IDMAP_WK__MAX_GID;
1185771Sjp151216 		mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL);
1195771Sjp151216 
1205771Sjp151216 		/*
1215771Sjp151216 		 * nobody is used to map SID containing CRs.
1225771Sjp151216 		 */
1235771Sjp151216 		eph_zsd->eph_nobody = crdup(zone->zone_kcred);
1245771Sjp151216 		(void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY);
1255771Sjp151216 		CR_FLAGS(eph_zsd->eph_nobody) = 0;
1265771Sjp151216 		eph_zsd->eph_nobody->cr_zone = zone;
1275771Sjp151216 
1285771Sjp151216 		(void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd);
1295771Sjp151216 	}
1305771Sjp151216 	mutex_exit(&ephemeral_zone_mutex);
1315771Sjp151216 	return (eph_zsd);
1325771Sjp151216 }
1335771Sjp151216 
134*9710SKen.Powell@Sun.COM static cred_t *crdup_flags(const cred_t *, int);
1356134Scasper static cred_t *cralloc_flags(int);
1366134Scasper 
1375771Sjp151216 /*
1385771Sjp151216  * This function is called when a zone is destroyed
1395771Sjp151216  */
1405771Sjp151216 static void
1415771Sjp151216 /* ARGSUSED */
1425771Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg)
1435771Sjp151216 {
1445771Sjp151216 	ephemeral_zsd_t *eph_zsd = arg;
1455771Sjp151216 	if (eph_zsd != NULL) {
1465771Sjp151216 		mutex_destroy(&eph_zsd->eph_lock);
1475771Sjp151216 		crfree(eph_zsd->eph_nobody);
1485771Sjp151216 		kmem_free(eph_zsd, sizeof (ephemeral_zsd_t));
1495771Sjp151216 	}
1505771Sjp151216 }
1515771Sjp151216 
1525771Sjp151216 
1535771Sjp151216 
1544321Scasper /*
1550Sstevel@tonic-gate  * Initialize credentials data structures.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate void
1590Sstevel@tonic-gate cred_init(void)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	priv_init();
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1);
1640Sstevel@tonic-gate 	/*
1650Sstevel@tonic-gate 	 * Make sure it's word-aligned.
1660Sstevel@tonic-gate 	 */
1670Sstevel@tonic-gate 	crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
1700Sstevel@tonic-gate #ifdef _LP64
1710Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
1720Sstevel@tonic-gate 		audoff = (crsize +
1730Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
1740Sstevel@tonic-gate #else	/* _LP64 */
1750Sstevel@tonic-gate 		audoff = crsize;
1760Sstevel@tonic-gate #endif	/* _LP64 */
1770Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1780Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1824520Snw141292 	    NULL, NULL, NULL, NULL, NULL, 0);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/*
1851676Sjpk 	 * dummycr is used to copy initial state for creds.
1861676Sjpk 	 */
1871676Sjpk 	dummycr = cralloc();
1881676Sjpk 	bzero(dummycr, crsize);
1891676Sjpk 	dummycr->cr_ref = 1;
1904321Scasper 	dummycr->cr_uid = (uid_t)-1;
1914321Scasper 	dummycr->cr_gid = (gid_t)-1;
1924321Scasper 	dummycr->cr_ruid = (uid_t)-1;
1934321Scasper 	dummycr->cr_rgid = (gid_t)-1;
1944321Scasper 	dummycr->cr_suid = (uid_t)-1;
1954321Scasper 	dummycr->cr_sgid = (gid_t)-1;
1964321Scasper 
1971676Sjpk 
1981676Sjpk 	/*
1990Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
2000Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
2010Sstevel@tonic-gate 	 * sets filled in.
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	kcred = cralloc();
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	bzero(kcred, crsize);
2060Sstevel@tonic-gate 	kcred->cr_ref = 1;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
2090Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
2120Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
2150Sstevel@tonic-gate 	if (!rstchown)
2160Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
2190Sstevel@tonic-gate 	if (rstlink)
2200Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
2231676Sjpk 
2241676Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * Set up credentials of p0.
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
2300Sstevel@tonic-gate 	curthread->t_cred = kcred;
2310Sstevel@tonic-gate 
2325771Sjp151216 	ucredsize = UCRED_SIZE;
2334321Scasper 
2345771Sjp151216 	mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL);
2355771Sjp151216 	zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
2400Sstevel@tonic-gate  */
2416134Scasper static cred_t *
2426134Scasper cralloc_flags(int flgs)
2430Sstevel@tonic-gate {
2446134Scasper 	cred_t *cr = kmem_cache_alloc(cred_cache, flgs);
2456134Scasper 
2466134Scasper 	if (cr == NULL)
2476134Scasper 		return (NULL);
2486134Scasper 
2490Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
2500Sstevel@tonic-gate 	cr->cr_zone = NULL;
2511676Sjpk 	cr->cr_label = NULL;
2524321Scasper 	cr->cr_ksid = NULL;
2536134Scasper 	cr->cr_klpd = NULL;
2544321Scasper 	return (cr);
2554321Scasper }
2564321Scasper 
2576134Scasper cred_t *
2586134Scasper cralloc(void)
2596134Scasper {
2606134Scasper 	return (cralloc_flags(KM_SLEEP));
2616134Scasper }
2626134Scasper 
2634321Scasper /*
2644321Scasper  * As cralloc but prepared for ksid change (if appropriate).
2654321Scasper  */
2664321Scasper cred_t *
2674321Scasper cralloc_ksid(void)
2684321Scasper {
2694321Scasper 	cred_t *cr = cralloc();
2704321Scasper 	if (hasephids)
2714321Scasper 		cr->cr_ksid = kcrsid_alloc();
2720Sstevel@tonic-gate 	return (cr);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
2770Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate cred_t *
2800Sstevel@tonic-gate crget(void)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
2850Sstevel@tonic-gate 	cr->cr_ref = 1;
2860Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
2871676Sjpk 	if (cr->cr_label)
2881676Sjpk 		label_hold(cr->cr_label);
2896134Scasper 	ASSERT(cr->cr_klpd == NULL);
2900Sstevel@tonic-gate 	return (cr);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
2950Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
2960Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
2970Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
2980Sstevel@tonic-gate  *
2990Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
3000Sstevel@tonic-gate  * and p->p_cred set.
3010Sstevel@tonic-gate  *
3020Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate void
3050Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	kthread_id_t	t;
3080Sstevel@tonic-gate 	kthread_id_t	first;
3090Sstevel@tonic-gate 	cred_t *oldcr;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
3150Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 	t = curthread;
3180Sstevel@tonic-gate 	oldcr = t->t_cred;
3190Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
3200Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
3260Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
3270Sstevel@tonic-gate 		first = curthread;
3280Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
3290Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
3300Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * Put a hold on a cred structure.
3360Sstevel@tonic-gate  */
3370Sstevel@tonic-gate void
3380Sstevel@tonic-gate crhold(cred_t *cr)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	atomic_add_32(&cr->cr_ref, 1);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
3451676Sjpk  * If cred uses label different from zone label, free it.
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate void
3480Sstevel@tonic-gate crfree(cred_t *cr)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) {
3510Sstevel@tonic-gate 		ASSERT(cr != kcred);
3521676Sjpk 		if (cr->cr_label)
3531676Sjpk 			label_rele(cr->cr_label);
3546134Scasper 		if (cr->cr_klpd)
3556134Scasper 			crklpd_rele(cr->cr_klpd);
3560Sstevel@tonic-gate 		if (cr->cr_zone)
3570Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
3584321Scasper 		if (cr->cr_ksid)
3594321Scasper 			kcrsid_rele(cr->cr_ksid);
3600Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3660Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3670Sstevel@tonic-gate  * 	and one for the thread.
3680Sstevel@tonic-gate  */
3690Sstevel@tonic-gate cred_t *
3700Sstevel@tonic-gate crcopy(cred_t *cr)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	cred_t *newcr;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	newcr = cralloc();
3750Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3760Sstevel@tonic-gate 	if (newcr->cr_zone)
3770Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
3781676Sjpk 	if (newcr->cr_label)
3796134Scasper 		label_hold(newcr->cr_label);
3804321Scasper 	if (newcr->cr_ksid)
3816134Scasper 		kcrsid_hold(newcr->cr_ksid);
3826134Scasper 	if (newcr->cr_klpd)
3836134Scasper 		crklpd_hold(newcr->cr_klpd);
3840Sstevel@tonic-gate 	crfree(cr);
3850Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3860Sstevel@tonic-gate 	return (newcr);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3910Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3920Sstevel@tonic-gate  * 	and one for the thread.
3930Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
3940Sstevel@tonic-gate  * "new" cred.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate void
3970Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
3980Sstevel@tonic-gate {
3994321Scasper 	credsid_t *nkcr = newcr->cr_ksid;
4004321Scasper 
4010Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4020Sstevel@tonic-gate 	if (newcr->cr_zone)
4030Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
4041676Sjpk 	if (newcr->cr_label)
4051676Sjpk 		label_hold(newcr->cr_label);
4066134Scasper 	if (newcr->cr_klpd)
4076134Scasper 		crklpd_hold(newcr->cr_klpd);
4084321Scasper 	if (nkcr) {
4094321Scasper 		newcr->cr_ksid = nkcr;
4104321Scasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
4114321Scasper 	} else if (newcr->cr_ksid)
4124321Scasper 		kcrsid_hold(newcr->cr_ksid);
4130Sstevel@tonic-gate 	crfree(oldcr);
4140Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4190Sstevel@tonic-gate  *	The old cred is not freed.
4200Sstevel@tonic-gate  */
4216134Scasper static cred_t *
422*9710SKen.Powell@Sun.COM crdup_flags(const cred_t *cr, int flgs)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	cred_t *newcr;
4250Sstevel@tonic-gate 
4266134Scasper 	newcr = cralloc_flags(flgs);
4276134Scasper 
4286134Scasper 	if (newcr == NULL)
4296134Scasper 		return (NULL);
4306134Scasper 
4310Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
4320Sstevel@tonic-gate 	if (newcr->cr_zone)
4330Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
4341676Sjpk 	if (newcr->cr_label)
4351676Sjpk 		label_hold(newcr->cr_label);
4366134Scasper 	if (newcr->cr_klpd)
4376134Scasper 		crklpd_hold(newcr->cr_klpd);
4384321Scasper 	if (newcr->cr_ksid)
4394321Scasper 		kcrsid_hold(newcr->cr_ksid);
4400Sstevel@tonic-gate 	newcr->cr_ref = 1;
4410Sstevel@tonic-gate 	return (newcr);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4446134Scasper cred_t *
4456134Scasper crdup(cred_t *cr)
4466134Scasper {
4476134Scasper 	return (crdup_flags(cr, KM_SLEEP));
4486134Scasper }
4496134Scasper 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4520Sstevel@tonic-gate  *	The old cred is not freed.
4530Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
4540Sstevel@tonic-gate  * "new" cred.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate void
4570Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
4580Sstevel@tonic-gate {
4594321Scasper 	credsid_t *nkcr = newcr->cr_ksid;
4604321Scasper 
4610Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4620Sstevel@tonic-gate 	if (newcr->cr_zone)
4630Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
4641676Sjpk 	if (newcr->cr_label)
4651676Sjpk 		label_hold(newcr->cr_label);
4666134Scasper 	if (newcr->cr_klpd)
4676134Scasper 		crklpd_hold(newcr->cr_klpd);
4684321Scasper 	if (nkcr) {
4694321Scasper 		newcr->cr_ksid = nkcr;
4704321Scasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
4714321Scasper 	} else if (newcr->cr_ksid)
4724321Scasper 		kcrsid_hold(newcr->cr_ksid);
4730Sstevel@tonic-gate 	newcr->cr_ref = 1;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate cred_t *
4801676Sjpk crgetcred(void)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	cred_t *cr;
4830Sstevel@tonic-gate 	proc_t *p;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	p = ttoproc(curthread);
4860Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
4870Sstevel@tonic-gate 	crhold(cr = p->p_cred);
4880Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
4890Sstevel@tonic-gate 	return (cr);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate /*
4930Sstevel@tonic-gate  * Backward compatibility check for suser().
4940Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
4950Sstevel@tonic-gate  * done through use of privilege in the audit trail.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate int
4980Sstevel@tonic-gate suser(cred_t *cr)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
5010Sstevel@tonic-gate 	    == 0);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
5060Sstevel@tonic-gate  * described by the supplied credentials.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate int
5090Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	if (gid == cr->cr_gid)
5120Sstevel@tonic-gate 		return (1);
5130Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
5180Sstevel@tonic-gate  */
5190Sstevel@tonic-gate int
5200Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	const gid_t *gp, *endgp;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	endgp = &cr->cr_groups[cr->cr_ngroups];
5250Sstevel@tonic-gate 	for (gp = cr->cr_groups; gp < endgp; gp++)
5260Sstevel@tonic-gate 		if (*gp == gid)
5270Sstevel@tonic-gate 			return (1);
5280Sstevel@tonic-gate 	return (0);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /*
5320Sstevel@tonic-gate  * This function is called to check whether the credentials set
5330Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
5340Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
5350Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
5360Sstevel@tonic-gate  *
5370Sstevel@tonic-gate  * The rules are:
5380Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
5390Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
5400Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
5410Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
5420Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
5430Sstevel@tonic-gate  *     succeeds
5440Sstevel@tonic-gate  * (4) otherwise, the check fails
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate int
5470Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	if (scrp == tcrp)
5500Sstevel@tonic-gate 		return (1);
5510Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
5520Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
5530Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
5540Sstevel@tonic-gate 		return (0);
5550Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
5560Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
5570Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
5580Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
5590Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
5600Sstevel@tonic-gate 		return (1);
5610Sstevel@tonic-gate 	return (0);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
5660Sstevel@tonic-gate  * additionally returns success if the proc_t's match
5670Sstevel@tonic-gate  * It is the preferred interface for most uses.
5686134Scasper  * And it will acquire p_crlock itself, so it assert's that it shouldn't
5690Sstevel@tonic-gate  * be held.
5700Sstevel@tonic-gate  */
5710Sstevel@tonic-gate int
5720Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 	int rets;
5750Sstevel@tonic-gate 	cred_t *tcrp;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (tp == sp)
5800Sstevel@tonic-gate 		return (1);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
5830Sstevel@tonic-gate 		return (0);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
5866134Scasper 	crhold(tcrp = tp->p_cred);
5876134Scasper 	mutex_exit(&tp->p_crlock);
5880Sstevel@tonic-gate 	rets = hasprocperm(tcrp, scrp);
5896134Scasper 	crfree(tcrp);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	return (rets);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
5960Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
5970Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
5980Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
5990Sstevel@tonic-gate  *
6000Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
6010Sstevel@tonic-gate  * 1 if they do not.
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate int
6040Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (cr1 == cr2)
6080Sstevel@tonic-gate 		return (0);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
6110Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
6120Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
6130Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
6140Sstevel@tonic-gate 	    cr1->cr_ngroups == cr2->cr_ngroups &&
6150Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
6160Sstevel@tonic-gate 	    bcmp(cr1->cr_groups, cr2->cr_groups,
6174520Snw141292 	    cr1->cr_ngroups * sizeof (gid_t)) == 0) {
6180Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 	return (1);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Read access functions to cred_t.
6250Sstevel@tonic-gate  */
6260Sstevel@tonic-gate uid_t
6270Sstevel@tonic-gate crgetuid(const cred_t *cr)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	return (cr->cr_uid);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate uid_t
6330Sstevel@tonic-gate crgetruid(const cred_t *cr)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	return (cr->cr_ruid);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate uid_t
6390Sstevel@tonic-gate crgetsuid(const cred_t *cr)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	return (cr->cr_suid);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate gid_t
6450Sstevel@tonic-gate crgetgid(const cred_t *cr)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	return (cr->cr_gid);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate gid_t
6510Sstevel@tonic-gate crgetrgid(const cred_t *cr)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	return (cr->cr_rgid);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate gid_t
6570Sstevel@tonic-gate crgetsgid(const cred_t *cr)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	return (cr->cr_sgid);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate const auditinfo_addr_t *
6630Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate auditinfo_addr_t *
6690Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	return (CR_AUINFO(cr));
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate zoneid_t
6750Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
6760Sstevel@tonic-gate {
6771676Sjpk 	return (cr->cr_zone == NULL ?
6781676Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
6791676Sjpk 	    cr->cr_zone->zone_id);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate projid_t
6830Sstevel@tonic-gate crgetprojid(const cred_t *cr)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	return (cr->cr_projid);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6881676Sjpk zone_t *
6891676Sjpk crgetzone(const cred_t *cr)
6901676Sjpk {
6911676Sjpk 	return (cr->cr_zone);
6921676Sjpk }
6931676Sjpk 
6941676Sjpk struct ts_label_s *
6951676Sjpk crgetlabel(const cred_t *cr)
6961676Sjpk {
6971676Sjpk 	return (cr->cr_label ?
6981676Sjpk 	    cr->cr_label :
6991676Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
7001676Sjpk }
7011676Sjpk 
7021676Sjpk boolean_t
7031676Sjpk crisremote(const cred_t *cr)
7041676Sjpk {
7051676Sjpk 	return (REMOTE_PEER_CRED(cr));
7061676Sjpk }
7071676Sjpk 
7085771Sjp151216 #define	BADUID(x, zn)	((x) != -1 && !VALID_UID((x), (zn)))
7095771Sjp151216 #define	BADGID(x, zn)	((x) != -1 && !VALID_GID((x), (zn)))
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate int
7120Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
7130Sstevel@tonic-gate {
7145771Sjp151216 	zone_t	*zone = crgetzone(cr);
7155771Sjp151216 
7160Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7170Sstevel@tonic-gate 
7185771Sjp151216 	if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone))
7190Sstevel@tonic-gate 		return (-1);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (r != -1)
7220Sstevel@tonic-gate 		cr->cr_ruid = r;
7230Sstevel@tonic-gate 	if (e != -1)
7240Sstevel@tonic-gate 		cr->cr_uid = e;
7250Sstevel@tonic-gate 	if (s != -1)
7260Sstevel@tonic-gate 		cr->cr_suid = s;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	return (0);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate int
7320Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
7330Sstevel@tonic-gate {
7345771Sjp151216 	zone_t	*zone = crgetzone(cr);
7355771Sjp151216 
7360Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7370Sstevel@tonic-gate 
7385771Sjp151216 	if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone))
7390Sstevel@tonic-gate 		return (-1);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if (r != -1)
7420Sstevel@tonic-gate 		cr->cr_rgid = r;
7430Sstevel@tonic-gate 	if (e != -1)
7440Sstevel@tonic-gate 		cr->cr_gid = e;
7450Sstevel@tonic-gate 	if (s != -1)
7460Sstevel@tonic-gate 		cr->cr_sgid = s;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	return (0);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate int
7520Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
7530Sstevel@tonic-gate {
7545771Sjp151216 	zone_t	*zone = crgetzone(cr);
7555771Sjp151216 
7560Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7570Sstevel@tonic-gate 
7585771Sjp151216 	if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone))
7590Sstevel@tonic-gate 		return (-1);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
7620Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	return (0);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate int
7680Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
7730Sstevel@tonic-gate 		return (-1);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	cr->cr_ngroups = n;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	if (n > 0)
7780Sstevel@tonic-gate 		bcopy(grp, cr->cr_groups, n * sizeof (gid_t));
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	return (0);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate void
7840Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
7870Sstevel@tonic-gate 	cr->cr_projid = projid;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate  * This routine returns the pointer to the first element of the cr_groups
7920Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate const gid_t *
7950Sstevel@tonic-gate crgetgroups(const cred_t *cr)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	return (cr->cr_groups);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate int
8010Sstevel@tonic-gate crgetngroups(const cred_t *cr)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	return (cr->cr_ngroups);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate void
8070Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
8100Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
8110Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
8120Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
8130Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
8140Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
8150Sstevel@tonic-gate 	pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max);
8160Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0;	/* in case ngroups == 0 */
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
8190Sstevel@tonic-gate 		bcopy(cr->cr_groups, pcrp->pr_groups,
8200Sstevel@tonic-gate 		    sizeof (gid_t) * cr->cr_ngroups);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate static int
8241676Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
8270Sstevel@tonic-gate 	au_tid_addr_t	tid;
8280Sstevel@tonic-gate 
8291676Sjpk 	if (secpolicy_audit_getattr(rcr) != 0)
8300Sstevel@tonic-gate 		return (-1);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
8330Sstevel@tonic-gate 	tid = ai->ai_termid;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
8360Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
8370Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
8400Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
8430Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	return (0);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8481676Sjpk void
8491676Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
8501676Sjpk {
8511676Sjpk 	ts_label_t	*tslp;
8521676Sjpk 
8531676Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
8541676Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
8551676Sjpk }
8561676Sjpk 
8570Sstevel@tonic-gate /*
8580Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
8590Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
8600Sstevel@tonic-gate  * memory and copy it twice.
8611676Sjpk  *
8621676Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
8631676Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
8641676Sjpk  * to determine whether audit info should be included.
8650Sstevel@tonic-gate  */
8660Sstevel@tonic-gate struct ucred_s *
8671676Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	struct ucred_s *uc;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
8720Sstevel@tonic-gate 	if (buf == NULL) {
8730Sstevel@tonic-gate 		uc = kmem_zalloc(ucredsize, KM_SLEEP);
8740Sstevel@tonic-gate 	} else {
8750Sstevel@tonic-gate 		bzero(buf, ucredsize);
8760Sstevel@tonic-gate 		uc = buf;
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 	uc->uc_size = ucredsize;
8790Sstevel@tonic-gate 	uc->uc_credoff = UCRED_CRED_OFF;
8800Sstevel@tonic-gate 	uc->uc_privoff = UCRED_PRIV_OFF;
8810Sstevel@tonic-gate 	uc->uc_audoff = UCRED_AUD_OFF;
8821676Sjpk 	uc->uc_labeloff = UCRED_LABEL_OFF;
8830Sstevel@tonic-gate 	uc->uc_pid = pid;
8840Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
8850Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
8860Sstevel@tonic-gate 
8871676Sjpk 	/*
8881676Sjpk 	 * Note that cred2uclabel() call should not be factored out
8891676Sjpk 	 * to the bottom of the if-else. UCXXX() macros depend on
8901676Sjpk 	 * uc_xxxoff values to work correctly.
8911676Sjpk 	 */
8921676Sjpk 	if (REMOTE_PEER_CRED(cr)) {
8931676Sjpk 		/*
8941676Sjpk 		 * other than label, the rest of cred info about a
8951676Sjpk 		 * remote peer isn't available.
8961676Sjpk 		 */
8971676Sjpk 		cred2uclabel(cr, UCLABEL(uc));
8981676Sjpk 		uc->uc_credoff = 0;
8991676Sjpk 		uc->uc_privoff = 0;
9000Sstevel@tonic-gate 		uc->uc_audoff = 0;
9011676Sjpk 	} else {
9021676Sjpk 		cred2prcred(cr, UCCRED(uc));
9031676Sjpk 		cred2prpriv(cr, UCPRIV(uc));
9041676Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
9051676Sjpk 			uc->uc_audoff = 0;
9061676Sjpk 		cred2uclabel(cr, UCLABEL(uc));
9071676Sjpk 	}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	return (uc);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate /*
9130Sstevel@tonic-gate  * Get the "ucred" of a process.
9140Sstevel@tonic-gate  */
9150Sstevel@tonic-gate struct ucred_s *
9160Sstevel@tonic-gate pgetucred(proc_t *p)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	cred_t *cr;
9190Sstevel@tonic-gate 	struct ucred_s *uc;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
9220Sstevel@tonic-gate 	cr = p->p_cred;
9230Sstevel@tonic-gate 	crhold(cr);
9240Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
9250Sstevel@tonic-gate 
9261676Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
9270Sstevel@tonic-gate 	crfree(cr);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	return (uc);
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
9340Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
9350Sstevel@tonic-gate  * make that the uid instead and retry the call.
9360Sstevel@tonic-gate  * Private interface for NFS.
9370Sstevel@tonic-gate  */
9380Sstevel@tonic-gate cred_t *
9390Sstevel@tonic-gate crnetadjust(cred_t *cr)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
9420Sstevel@tonic-gate 		cr = crdup(cr);
9430Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
9440Sstevel@tonic-gate 		return (cr);
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 	return (NULL);
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate /*
9500Sstevel@tonic-gate  * The reference count is of interest when you want to check
9510Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
9520Sstevel@tonic-gate  */
9530Sstevel@tonic-gate uint_t
9540Sstevel@tonic-gate crgetref(const cred_t *cr)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	return (cr->cr_ref);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate static int
9600Sstevel@tonic-gate get_c2audit_load(void)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	static int	gotit = 0;
9630Sstevel@tonic-gate 	static int	c2audit_load;
9640Sstevel@tonic-gate 	u_longlong_t	audit_load_val;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (gotit)
9670Sstevel@tonic-gate 		return (c2audit_load);
9680Sstevel@tonic-gate 	audit_load_val = 0;		/* set default value once */
9690Sstevel@tonic-gate 	(void) mod_sysvar("c2audit", "audit_load", &audit_load_val);
9700Sstevel@tonic-gate 	c2audit_load = (int)audit_load_val;
9710Sstevel@tonic-gate 	gotit++;
9720Sstevel@tonic-gate 	return (c2audit_load);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate int
9760Sstevel@tonic-gate get_audit_ucrsize(void)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate /*
9820Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
9830Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
9840Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
9850Sstevel@tonic-gate  * same.
9860Sstevel@tonic-gate  */
9870Sstevel@tonic-gate void
9880Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	ASSERT(cr != kcred);
9930Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
9940Sstevel@tonic-gate 	cr->cr_zone = zptr;
9950Sstevel@tonic-gate 	zone_cred_hold(zptr);
9960Sstevel@tonic-gate 	if (oldzptr)
9970Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
9980Sstevel@tonic-gate }
9991676Sjpk 
10001676Sjpk /*
10011676Sjpk  * Create a new cred based on the supplied label
10021676Sjpk  */
10031676Sjpk cred_t *
10041676Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
10051676Sjpk {
10061676Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
10071676Sjpk 	cred_t *cr = NULL;
10081676Sjpk 
10091676Sjpk 	if (lbl != NULL) {
10106134Scasper 		if ((cr = crdup_flags(dummycr, flags)) != NULL) {
10111676Sjpk 			cr->cr_label = lbl;
10121676Sjpk 		} else {
10131676Sjpk 			label_rele(lbl);
10141676Sjpk 		}
10151676Sjpk 	}
10161676Sjpk 
10171676Sjpk 	return (cr);
10181676Sjpk }
10191676Sjpk 
10201676Sjpk /*
10211676Sjpk  * Derive a new cred from the existing cred, but with a different label.
10221676Sjpk  * To be used when a cred is being shared, but the label needs to be changed
10231676Sjpk  * by a caller without affecting other users
10241676Sjpk  */
10251676Sjpk cred_t *
1026*9710SKen.Powell@Sun.COM copycred_from_tslabel(const cred_t *cr, ts_label_t *label, int flags)
1027*9710SKen.Powell@Sun.COM {
1028*9710SKen.Powell@Sun.COM 	cred_t *newcr = NULL;
1029*9710SKen.Powell@Sun.COM 
1030*9710SKen.Powell@Sun.COM 	if ((newcr = crdup_flags(cr, flags)) != NULL) {
1031*9710SKen.Powell@Sun.COM 		if (newcr->cr_label != NULL)
1032*9710SKen.Powell@Sun.COM 			label_rele(newcr->cr_label);
1033*9710SKen.Powell@Sun.COM 		label_hold(label);
1034*9710SKen.Powell@Sun.COM 		newcr->cr_label = label;
1035*9710SKen.Powell@Sun.COM 	}
1036*9710SKen.Powell@Sun.COM 
1037*9710SKen.Powell@Sun.COM 	return (newcr);
1038*9710SKen.Powell@Sun.COM }
1039*9710SKen.Powell@Sun.COM 
1040*9710SKen.Powell@Sun.COM /*
1041*9710SKen.Powell@Sun.COM  * Derive a new cred from the existing cred, but with a different label.
1042*9710SKen.Powell@Sun.COM  */
1043*9710SKen.Powell@Sun.COM cred_t *
1044*9710SKen.Powell@Sun.COM copycred_from_bslabel(const cred_t *cr, bslabel_t *blabel,
1045*9710SKen.Powell@Sun.COM     uint32_t doi, int flags)
10461676Sjpk {
10471676Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
1048*9710SKen.Powell@Sun.COM 	cred_t  *newcr = NULL;
10491676Sjpk 
10501676Sjpk 	if (lbl != NULL) {
1051*9710SKen.Powell@Sun.COM 		newcr = copycred_from_tslabel(cr, lbl, flags);
1052*9710SKen.Powell@Sun.COM 		label_rele(lbl);
10531676Sjpk 	}
10541676Sjpk 
10551676Sjpk 	return (newcr);
10561676Sjpk }
10571676Sjpk 
10581676Sjpk /*
10591676Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
10601676Sjpk  */
10611676Sjpk cred_t *
10621676Sjpk zone_kcred(void)
10631676Sjpk {
10641676Sjpk 	zone_t *zone;
10651676Sjpk 
10661676Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
10671676Sjpk 		return (zone->zone_kcred);
10681676Sjpk 	else
10691676Sjpk 		return (kcred);
10701676Sjpk }
10714321Scasper 
10724321Scasper boolean_t
10735771Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id)
10744321Scasper {
10755771Sjp151216 	ephemeral_zsd_t *eph_zsd;
10765908Sjp151216 	if (id <= IDMAP_WK__MAX_UID)
10775771Sjp151216 		return (B_TRUE);
10785771Sjp151216 
10795771Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
10805771Sjp151216 	ASSERT(eph_zsd != NULL);
10814321Scasper 	membar_consumer();
10825771Sjp151216 	return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid);
10834321Scasper }
10844321Scasper 
10854321Scasper boolean_t
10865771Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id)
10874321Scasper {
10885771Sjp151216 	ephemeral_zsd_t *eph_zsd;
10895908Sjp151216 	if (id <= IDMAP_WK__MAX_GID)
10905771Sjp151216 		return (B_TRUE);
10915771Sjp151216 
10925771Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
10935771Sjp151216 	ASSERT(eph_zsd != NULL);
10944321Scasper 	membar_consumer();
10955771Sjp151216 	return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid);
10964321Scasper }
10974321Scasper 
10984321Scasper int
10995771Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count)
11004321Scasper {
11015771Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
11025771Sjp151216 
11035771Sjp151216 	ASSERT(eph_zsd != NULL);
11045771Sjp151216 
11055771Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
11064321Scasper 
11074321Scasper 	/* Test for unsigned integer wrap around */
11085771Sjp151216 	if (eph_zsd->last_uid + count < eph_zsd->last_uid) {
11095771Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
11104321Scasper 		return (-1);
11114321Scasper 	}
11124321Scasper 
11134321Scasper 	/* first call or idmap crashed and state corrupted */
11144321Scasper 	if (flags != 0)
11155771Sjp151216 		eph_zsd->min_uid = eph_zsd->last_uid;
11164321Scasper 
11174321Scasper 	hasephids = B_TRUE;
11185771Sjp151216 	*start = eph_zsd->last_uid + 1;
11195771Sjp151216 	atomic_add_32(&eph_zsd->last_uid, count);
11205771Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
11214321Scasper 	return (0);
11224321Scasper }
11234321Scasper 
11244321Scasper int
11255771Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count)
11264321Scasper {
11275771Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
11285771Sjp151216 
11295771Sjp151216 	ASSERT(eph_zsd != NULL);
11305771Sjp151216 
11315771Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
11324321Scasper 
11334321Scasper 	/* Test for unsigned integer wrap around */
11345771Sjp151216 	if (eph_zsd->last_gid + count < eph_zsd->last_gid) {
11355771Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
11364321Scasper 		return (-1);
11374321Scasper 	}
11384321Scasper 
11394321Scasper 	/* first call or idmap crashed and state corrupted */
11404321Scasper 	if (flags != 0)
11415771Sjp151216 		eph_zsd->min_gid = eph_zsd->last_gid;
11424321Scasper 
11434321Scasper 	hasephids = B_TRUE;
11445771Sjp151216 	*start = eph_zsd->last_gid + 1;
11455771Sjp151216 	atomic_add_32(&eph_zsd->last_gid, count);
11465771Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
11474321Scasper 	return (0);
11484321Scasper }
11494321Scasper 
11504321Scasper /*
11515771Sjp151216  * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data()
11525771Sjp151216  * are project private functions that are for use of the test system only and
11535771Sjp151216  * are not to be used for other purposes.
11545771Sjp151216  */
11555771Sjp151216 
11565771Sjp151216 void
11575771Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid,
11585771Sjp151216 	gid_t *min_gid, gid_t *last_gid)
11595771Sjp151216 {
11605771Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
11615771Sjp151216 
11625771Sjp151216 	ASSERT(eph_zsd != NULL);
11635771Sjp151216 
11645771Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
11655771Sjp151216 
11665771Sjp151216 	*min_uid = eph_zsd->min_uid;
11675771Sjp151216 	*last_uid = eph_zsd->last_uid;
11685771Sjp151216 	*min_gid = eph_zsd->min_gid;
11695771Sjp151216 	*last_gid = eph_zsd->last_gid;
11705771Sjp151216 
11715771Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
11725771Sjp151216 }
11735771Sjp151216 
11745771Sjp151216 
11755771Sjp151216 void
11765771Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid,
11775771Sjp151216 	gid_t min_gid, gid_t last_gid)
11785771Sjp151216 {
11795771Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
11805771Sjp151216 
11815771Sjp151216 	ASSERT(eph_zsd != NULL);
11825771Sjp151216 
11835771Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
11845771Sjp151216 
11855771Sjp151216 	if (min_uid != 0)
11865771Sjp151216 		eph_zsd->min_uid = min_uid;
11875771Sjp151216 	if (last_uid != 0)
11885771Sjp151216 		eph_zsd->last_uid = last_uid;
11895771Sjp151216 	if (min_gid != 0)
11905771Sjp151216 		eph_zsd->min_gid = min_gid;
11915771Sjp151216 	if (last_gid != 0)
11925771Sjp151216 		eph_zsd->last_gid = last_gid;
11935771Sjp151216 
11945771Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
11955771Sjp151216 }
11965771Sjp151216 
11975771Sjp151216 /*
11985331Samw  * If the credential user SID or group SID is mapped to an ephemeral
11995331Samw  * ID, map the credential to nobody.
12004321Scasper  */
12014321Scasper cred_t *
12024321Scasper crgetmapped(const cred_t *cr)
12034321Scasper {
12045771Sjp151216 	ephemeral_zsd_t *eph_zsd;
12054406Scasper 	/*
12064406Scasper 	 * Someone incorrectly passed a NULL cred to a vnode operation
12074406Scasper 	 * either on purpose or by calling CRED() in interrupt context.
12084406Scasper 	 */
12094406Scasper 	if (cr == NULL)
12104406Scasper 		return (NULL);
12114406Scasper 
12124321Scasper 	if (cr->cr_ksid != NULL) {
12135771Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) {
12145771Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
12155771Sjp151216 			return (eph_zsd->eph_nobody);
12165771Sjp151216 		}
12174321Scasper 
12185771Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) {
12195771Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
12205771Sjp151216 			return (eph_zsd->eph_nobody);
12215771Sjp151216 		}
12224321Scasper 	}
12234321Scasper 
12244321Scasper 	return ((cred_t *)cr);
12254321Scasper }
12264321Scasper 
12274321Scasper /* index should be in range for a ksidindex_t */
12284321Scasper void
12294321Scasper crsetsid(cred_t *cr, ksid_t *ksp, int index)
12304321Scasper {
12314321Scasper 	ASSERT(cr->cr_ref <= 2);
12324321Scasper 	ASSERT(index >= 0 && index < KSID_COUNT);
12334321Scasper 	if (cr->cr_ksid == NULL && ksp == NULL)
12344321Scasper 		return;
12354321Scasper 	cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index);
12364321Scasper }
12374321Scasper 
12384321Scasper void
12394321Scasper crsetsidlist(cred_t *cr, ksidlist_t *ksl)
12404321Scasper {
12414321Scasper 	ASSERT(cr->cr_ref <= 2);
12424321Scasper 	if (cr->cr_ksid == NULL && ksl == NULL)
12434321Scasper 		return;
12444321Scasper 	cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl);
12454321Scasper }
12464321Scasper 
12474321Scasper ksid_t *
12484321Scasper crgetsid(const cred_t *cr, int i)
12494321Scasper {
12504321Scasper 	ASSERT(i >= 0 && i < KSID_COUNT);
12514321Scasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain)
12524321Scasper 		return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]);
12534321Scasper 	return (NULL);
12544321Scasper }
12554321Scasper 
12564321Scasper ksidlist_t *
12574321Scasper crgetsidlist(const cred_t *cr)
12584321Scasper {
12595331Samw 	if (cr->cr_ksid != NULL)
12604923Scasper 		return (cr->cr_ksid->kr_sidlist);
12614321Scasper 	return (NULL);
12624321Scasper }
12635331Samw 
12645331Samw /*
12655331Samw  * Interface to set the effective and permitted privileges for
12665331Samw  * a credential; this interface does no security checks and is
12675331Samw  * intended for kernel (file)servers creating credentials with
12685331Samw  * specific privileges.
12695331Samw  */
12705331Samw int
12715331Samw crsetpriv(cred_t *cr, ...)
12725331Samw {
12735331Samw 	va_list ap;
12745331Samw 	const char *privnm;
12755331Samw 
12765331Samw 	ASSERT(cr->cr_ref <= 2);
12775331Samw 
12785331Samw 	priv_set_PA(cr);
12795331Samw 
12805331Samw 	va_start(ap, cr);
12815331Samw 
12825331Samw 	while ((privnm = va_arg(ap, const char *)) != NULL) {
12835331Samw 		int priv = priv_getbyname(privnm, 0);
12845331Samw 		if (priv < 0)
12855331Samw 			return (-1);
12865331Samw 
12875331Samw 		priv_addset(&CR_PPRIV(cr), priv);
12885331Samw 		priv_addset(&CR_EPRIV(cr), priv);
12895331Samw 	}
12905331Samw 	priv_adjust_PA(cr);
12915331Samw 	va_end(ap);
12925331Samw 	return (0);
12935331Samw }
12946134Scasper 
12959151SThomas.Haynes@Sun.COM /*
12969151SThomas.Haynes@Sun.COM  * Interface to effectively set the PRIV_ALL for
12979151SThomas.Haynes@Sun.COM  * a credential; this interface does no security checks and is
12989151SThomas.Haynes@Sun.COM  * intended for kernel (file)servers to extend the user credentials
12999151SThomas.Haynes@Sun.COM  * to be ALL, like either kcred or zcred.
13009151SThomas.Haynes@Sun.COM  */
13019151SThomas.Haynes@Sun.COM void
13029151SThomas.Haynes@Sun.COM crset_zone_privall(cred_t *cr)
13039151SThomas.Haynes@Sun.COM {
13049151SThomas.Haynes@Sun.COM 	zone_t	*zone = crgetzone(cr);
13059151SThomas.Haynes@Sun.COM 
13069151SThomas.Haynes@Sun.COM 	priv_fillset(&CR_LPRIV(cr));
13079151SThomas.Haynes@Sun.COM 	CR_EPRIV(cr) = CR_PPRIV(cr) = CR_IPRIV(cr) = CR_LPRIV(cr);
13089151SThomas.Haynes@Sun.COM 	priv_intersect(zone->zone_privset, &CR_LPRIV(cr));
13099151SThomas.Haynes@Sun.COM 	priv_intersect(zone->zone_privset, &CR_EPRIV(cr));
13109151SThomas.Haynes@Sun.COM 	priv_intersect(zone->zone_privset, &CR_IPRIV(cr));
13119151SThomas.Haynes@Sun.COM 	priv_intersect(zone->zone_privset, &CR_PPRIV(cr));
13129151SThomas.Haynes@Sun.COM }
13139151SThomas.Haynes@Sun.COM 
13146134Scasper struct credklpd *
13156134Scasper crgetcrklpd(const cred_t *cr)
13166134Scasper {
13176134Scasper 	return (cr->cr_klpd);
13186134Scasper }
13196134Scasper 
13206134Scasper void
13216134Scasper crsetcrklpd(cred_t *cr, struct credklpd *crklpd)
13226134Scasper {
13236134Scasper 	ASSERT(cr->cr_ref <= 2);
13246134Scasper 
13256134Scasper 	if (cr->cr_klpd != NULL)
13266134Scasper 		crklpd_rele(cr->cr_klpd);
13276134Scasper 	cr->cr_klpd = crklpd;
13286134Scasper }
1329