xref: /onnv-gate/usr/src/uts/common/os/cred.c (revision 4321:a8930ec16e52)
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 /*
22*4321Scasper  * Copyright 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/sysmacros.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/systm.h>
450Sstevel@tonic-gate #include <sys/cred_impl.h>
460Sstevel@tonic-gate #include <sys/policy.h>
470Sstevel@tonic-gate #include <sys/vnode.h>
480Sstevel@tonic-gate #include <sys/errno.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/user.h>
510Sstevel@tonic-gate #include <sys/proc.h>
520Sstevel@tonic-gate #include <sys/syscall.h>
530Sstevel@tonic-gate #include <sys/debug.h>
540Sstevel@tonic-gate #include <sys/atomic.h>
550Sstevel@tonic-gate #include <sys/ucred.h>
560Sstevel@tonic-gate #include <sys/prsystm.h>
570Sstevel@tonic-gate #include <sys/modctl.h>
58*4321Scasper #include <sys/avl.h>
590Sstevel@tonic-gate #include <c2/audit.h>
600Sstevel@tonic-gate #include <sys/zone.h>
611676Sjpk #include <sys/tsol/label.h>
62*4321Scasper #include <sys/sid.h>
63*4321Scasper 
64*4321Scasper typedef struct ephidmap_data {
65*4321Scasper 	uid_t		min_uid, last_uid;
66*4321Scasper 	gid_t		min_gid, last_gid;
67*4321Scasper 	cred_t		*nobody;
68*4321Scasper 	kmutex_t	eph_lock;
69*4321Scasper } ephidmap_data_t;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static struct kmem_cache *cred_cache;
720Sstevel@tonic-gate static size_t crsize = 0;
730Sstevel@tonic-gate static int audoff = 0;
740Sstevel@tonic-gate uint32_t ucredsize;
750Sstevel@tonic-gate cred_t *kcred;
761676Sjpk static cred_t *dummycr;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static int get_c2audit_load(void);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
830Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
840Sstevel@tonic-gate 
851676Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
88*4321Scasper  * XXX: should be per-zone.
89*4321Scasper  * Start with an invalid value for atomic increments.
90*4321Scasper  */
91*4321Scasper static ephidmap_data_t ephemeral_data = {
92*4321Scasper 	MAXUID, MAXUID, MAXUID, MAXUID
93*4321Scasper };
94*4321Scasper 
95*4321Scasper static boolean_t hasephids = B_FALSE;
96*4321Scasper 
97*4321Scasper /*
980Sstevel@tonic-gate  * Initialize credentials data structures.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate void
1020Sstevel@tonic-gate cred_init(void)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	priv_init();
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1);
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * Make sure it's word-aligned.
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
1130Sstevel@tonic-gate #ifdef _LP64
1140Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
1150Sstevel@tonic-gate 		audoff = (crsize +
1160Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
1170Sstevel@tonic-gate #else	/* _LP64 */
1180Sstevel@tonic-gate 		audoff = crsize;
1190Sstevel@tonic-gate #endif	/* _LP64 */
1200Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1210Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1250Sstevel@tonic-gate 		NULL, NULL, NULL, NULL, NULL, 0);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1281676Sjpk 	 * dummycr is used to copy initial state for creds.
1291676Sjpk 	 */
1301676Sjpk 	dummycr = cralloc();
1311676Sjpk 	bzero(dummycr, crsize);
1321676Sjpk 	dummycr->cr_ref = 1;
133*4321Scasper 	dummycr->cr_uid = (uid_t)-1;
134*4321Scasper 	dummycr->cr_gid = (gid_t)-1;
135*4321Scasper 	dummycr->cr_ruid = (uid_t)-1;
136*4321Scasper 	dummycr->cr_rgid = (gid_t)-1;
137*4321Scasper 	dummycr->cr_suid = (uid_t)-1;
138*4321Scasper 	dummycr->cr_sgid = (gid_t)-1;
139*4321Scasper 
1401676Sjpk 
1411676Sjpk 	/*
1420Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
1430Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
1440Sstevel@tonic-gate 	 * sets filled in.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	kcred = cralloc();
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	bzero(kcred, crsize);
1490Sstevel@tonic-gate 	kcred->cr_ref = 1;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
1520Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
1550Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
1580Sstevel@tonic-gate 	if (!rstchown)
1590Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
1620Sstevel@tonic-gate 	if (rstlink)
1630Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
1661676Sjpk 
1671676Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Set up credentials of p0.
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
1730Sstevel@tonic-gate 	curthread->t_cred = kcred;
1740Sstevel@tonic-gate 
175*4321Scasper 	/*
176*4321Scasper 	 * nobody is used to map SID containing CRs.
177*4321Scasper 	 */
178*4321Scasper 	ephemeral_data.nobody = crdup(kcred);
179*4321Scasper 	(void) crsetugid(ephemeral_data.nobody, UID_NOBODY, GID_NOBODY);
180*4321Scasper 	CR_FLAGS(kcred) = 0;
181*4321Scasper 
1820Sstevel@tonic-gate 	ucredsize = UCRED_SIZE;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate cred_t *
1890Sstevel@tonic-gate cralloc(void)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
1920Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
1930Sstevel@tonic-gate 	cr->cr_zone = NULL;
1941676Sjpk 	cr->cr_label = NULL;
195*4321Scasper 	cr->cr_ksid = NULL;
196*4321Scasper 	return (cr);
197*4321Scasper }
198*4321Scasper 
199*4321Scasper /*
200*4321Scasper  * As cralloc but prepared for ksid change (if appropriate).
201*4321Scasper  */
202*4321Scasper cred_t *
203*4321Scasper cralloc_ksid(void)
204*4321Scasper {
205*4321Scasper 	cred_t *cr = cralloc();
206*4321Scasper 	if (hasephids)
207*4321Scasper 		cr->cr_ksid = kcrsid_alloc();
2080Sstevel@tonic-gate 	return (cr);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
2130Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate cred_t *
2160Sstevel@tonic-gate crget(void)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
2210Sstevel@tonic-gate 	cr->cr_ref = 1;
2220Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
2231676Sjpk 	if (cr->cr_label)
2241676Sjpk 		label_hold(cr->cr_label);
2250Sstevel@tonic-gate 	return (cr);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
2300Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
2310Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
2320Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
2350Sstevel@tonic-gate  * and p->p_cred set.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate void
2400Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	kthread_id_t	t;
2430Sstevel@tonic-gate 	kthread_id_t	first;
2440Sstevel@tonic-gate 	cred_t *oldcr;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
2500Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	t = curthread;
2530Sstevel@tonic-gate 	oldcr = t->t_cred;
2540Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
2550Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
2610Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
2620Sstevel@tonic-gate 		first = curthread;
2630Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
2640Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
2650Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * Put a hold on a cred structure.
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate void
2730Sstevel@tonic-gate crhold(cred_t *cr)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	atomic_add_32(&cr->cr_ref, 1);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
2801676Sjpk  * If cred uses label different from zone label, free it.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate void
2830Sstevel@tonic-gate crfree(cred_t *cr)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) {
2860Sstevel@tonic-gate 		ASSERT(cr != kcred);
2871676Sjpk 		if (cr->cr_label)
2881676Sjpk 			label_rele(cr->cr_label);
2890Sstevel@tonic-gate 		if (cr->cr_zone)
2900Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
291*4321Scasper 		if (cr->cr_ksid)
292*4321Scasper 			kcrsid_rele(cr->cr_ksid);
2930Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
2990Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3000Sstevel@tonic-gate  * 	and one for the thread.
3010Sstevel@tonic-gate  */
3020Sstevel@tonic-gate cred_t *
3030Sstevel@tonic-gate crcopy(cred_t *cr)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	cred_t *newcr;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	newcr = cralloc();
3080Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3090Sstevel@tonic-gate 	if (newcr->cr_zone)
3100Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
3111676Sjpk 	if (newcr->cr_label)
3121676Sjpk 		label_hold(cr->cr_label);
313*4321Scasper 	if (newcr->cr_ksid)
314*4321Scasper 		kcrsid_hold(cr->cr_ksid);
3150Sstevel@tonic-gate 	crfree(cr);
3160Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3170Sstevel@tonic-gate 	return (newcr);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3220Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3230Sstevel@tonic-gate  * 	and one for the thread.
3240Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
3250Sstevel@tonic-gate  * "new" cred.
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate void
3280Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
3290Sstevel@tonic-gate {
330*4321Scasper 	credsid_t *nkcr = newcr->cr_ksid;
331*4321Scasper 
3320Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
3330Sstevel@tonic-gate 	if (newcr->cr_zone)
3340Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
3351676Sjpk 	if (newcr->cr_label)
3361676Sjpk 		label_hold(newcr->cr_label);
337*4321Scasper 	if (nkcr) {
338*4321Scasper 		newcr->cr_ksid = nkcr;
339*4321Scasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
340*4321Scasper 	} else if (newcr->cr_ksid)
341*4321Scasper 		kcrsid_hold(newcr->cr_ksid);
3420Sstevel@tonic-gate 	crfree(oldcr);
3430Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * Dup a cred struct to a new held one.
3480Sstevel@tonic-gate  *	The old cred is not freed.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate cred_t *
3510Sstevel@tonic-gate crdup(cred_t *cr)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	cred_t *newcr;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	newcr = cralloc();
3560Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3570Sstevel@tonic-gate 	if (newcr->cr_zone)
3580Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
3591676Sjpk 	if (newcr->cr_label)
3601676Sjpk 		label_hold(newcr->cr_label);
361*4321Scasper 	if (newcr->cr_ksid)
362*4321Scasper 		kcrsid_hold(newcr->cr_ksid);
3630Sstevel@tonic-gate 	newcr->cr_ref = 1;
3640Sstevel@tonic-gate 	return (newcr);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate  * Dup a cred struct to a new held one.
3690Sstevel@tonic-gate  *	The old cred is not freed.
3700Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
3710Sstevel@tonic-gate  * "new" cred.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate void
3740Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
3750Sstevel@tonic-gate {
376*4321Scasper 	credsid_t *nkcr = newcr->cr_ksid;
377*4321Scasper 
3780Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
3790Sstevel@tonic-gate 	if (newcr->cr_zone)
3800Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
3811676Sjpk 	if (newcr->cr_label)
3821676Sjpk 		label_hold(newcr->cr_label);
383*4321Scasper 	if (nkcr) {
384*4321Scasper 		newcr->cr_ksid = nkcr;
385*4321Scasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
386*4321Scasper 	} else if (newcr->cr_ksid)
387*4321Scasper 		kcrsid_hold(newcr->cr_ksid);
3880Sstevel@tonic-gate 	newcr->cr_ref = 1;
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate cred_t *
3951676Sjpk crgetcred(void)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	cred_t *cr;
3980Sstevel@tonic-gate 	proc_t *p;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	p = ttoproc(curthread);
4010Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
4020Sstevel@tonic-gate 	crhold(cr = p->p_cred);
4030Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
4040Sstevel@tonic-gate 	return (cr);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * Backward compatibility check for suser().
4090Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
4100Sstevel@tonic-gate  * done through use of privilege in the audit trail.
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate int
4130Sstevel@tonic-gate suser(cred_t *cr)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
4160Sstevel@tonic-gate 	    == 0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
4210Sstevel@tonic-gate  * described by the supplied credentials.
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate int
4240Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	if (gid == cr->cr_gid)
4270Sstevel@tonic-gate 		return (1);
4280Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
4330Sstevel@tonic-gate  */
4340Sstevel@tonic-gate int
4350Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate 	const gid_t *gp, *endgp;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	endgp = &cr->cr_groups[cr->cr_ngroups];
4400Sstevel@tonic-gate 	for (gp = cr->cr_groups; gp < endgp; gp++)
4410Sstevel@tonic-gate 		if (*gp == gid)
4420Sstevel@tonic-gate 			return (1);
4430Sstevel@tonic-gate 	return (0);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * This function is called to check whether the credentials set
4480Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
4490Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
4500Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
4510Sstevel@tonic-gate  *
4520Sstevel@tonic-gate  * The rules are:
4530Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
4540Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
4550Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
4560Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
4570Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
4580Sstevel@tonic-gate  *     succeeds
4590Sstevel@tonic-gate  * (4) otherwise, the check fails
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate int
4620Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	if (scrp == tcrp)
4650Sstevel@tonic-gate 		return (1);
4660Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
4670Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
4680Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
4690Sstevel@tonic-gate 		return (0);
4700Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
4710Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
4720Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
4730Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
4740Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
4750Sstevel@tonic-gate 		return (1);
4760Sstevel@tonic-gate 	return (0);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
4810Sstevel@tonic-gate  * additionally returns success if the proc_t's match
4820Sstevel@tonic-gate  * It is the preferred interface for most uses.
4830Sstevel@tonic-gate  * And it will acquire pcrlock itself, so it assert's that it shouldn't
4840Sstevel@tonic-gate  * be held.
4850Sstevel@tonic-gate  */
4860Sstevel@tonic-gate int
4870Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	int rets;
4900Sstevel@tonic-gate 	cred_t *tcrp;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (tp == sp)
4950Sstevel@tonic-gate 		return (1);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
4980Sstevel@tonic-gate 		return (0);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
5010Sstevel@tonic-gate 	tcrp = tp->p_cred;
5020Sstevel@tonic-gate 	rets = hasprocperm(tcrp, scrp);
5030Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	return (rets);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
5100Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
5110Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
5120Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
5130Sstevel@tonic-gate  *
5140Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
5150Sstevel@tonic-gate  * 1 if they do not.
5160Sstevel@tonic-gate  */
5170Sstevel@tonic-gate int
5180Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (cr1 == cr2)
5220Sstevel@tonic-gate 		return (0);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
5250Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
5260Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
5270Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
5280Sstevel@tonic-gate 	    cr1->cr_ngroups == cr2->cr_ngroups &&
5290Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
5300Sstevel@tonic-gate 	    bcmp(cr1->cr_groups, cr2->cr_groups,
5310Sstevel@tonic-gate 		    cr1->cr_ngroups * sizeof (gid_t)) == 0) {
5320Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 	return (1);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * Read access functions to cred_t.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate uid_t
5410Sstevel@tonic-gate crgetuid(const cred_t *cr)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	return (cr->cr_uid);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate uid_t
5470Sstevel@tonic-gate crgetruid(const cred_t *cr)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	return (cr->cr_ruid);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate uid_t
5530Sstevel@tonic-gate crgetsuid(const cred_t *cr)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	return (cr->cr_suid);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate gid_t
5590Sstevel@tonic-gate crgetgid(const cred_t *cr)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	return (cr->cr_gid);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate gid_t
5650Sstevel@tonic-gate crgetrgid(const cred_t *cr)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	return (cr->cr_rgid);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate gid_t
5710Sstevel@tonic-gate crgetsgid(const cred_t *cr)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	return (cr->cr_sgid);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate const auditinfo_addr_t *
5770Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate auditinfo_addr_t *
5830Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	return (CR_AUINFO(cr));
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate zoneid_t
5890Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
5900Sstevel@tonic-gate {
5911676Sjpk 	return (cr->cr_zone == NULL ?
5921676Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
5931676Sjpk 	    cr->cr_zone->zone_id);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate projid_t
5970Sstevel@tonic-gate crgetprojid(const cred_t *cr)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	return (cr->cr_projid);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate 
6021676Sjpk zone_t *
6031676Sjpk crgetzone(const cred_t *cr)
6041676Sjpk {
6051676Sjpk 	return (cr->cr_zone);
6061676Sjpk }
6071676Sjpk 
6081676Sjpk struct ts_label_s *
6091676Sjpk crgetlabel(const cred_t *cr)
6101676Sjpk {
6111676Sjpk 	return (cr->cr_label ?
6121676Sjpk 	    cr->cr_label :
6131676Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
6141676Sjpk }
6151676Sjpk 
6161676Sjpk boolean_t
6171676Sjpk crisremote(const cred_t *cr)
6181676Sjpk {
6191676Sjpk 	return (REMOTE_PEER_CRED(cr));
6201676Sjpk }
6211676Sjpk 
622*4321Scasper #define	BADUID(x)	((x) != -1 && !VALID_UID(x))
623*4321Scasper #define	BADGID(x)	((x) != -1 && !VALID_GID(x))
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate int
6260Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6290Sstevel@tonic-gate 
630*4321Scasper 	if (BADUID(r) || BADUID(e) || BADUID(s))
6310Sstevel@tonic-gate 		return (-1);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (r != -1)
6340Sstevel@tonic-gate 		cr->cr_ruid = r;
6350Sstevel@tonic-gate 	if (e != -1)
6360Sstevel@tonic-gate 		cr->cr_uid = e;
6370Sstevel@tonic-gate 	if (s != -1)
6380Sstevel@tonic-gate 		cr->cr_suid = s;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	return (0);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate int
6440Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6470Sstevel@tonic-gate 
648*4321Scasper 	if (BADGID(r) || BADGID(e) || BADGID(s))
6490Sstevel@tonic-gate 		return (-1);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	if (r != -1)
6520Sstevel@tonic-gate 		cr->cr_rgid = r;
6530Sstevel@tonic-gate 	if (e != -1)
6540Sstevel@tonic-gate 		cr->cr_gid = e;
6550Sstevel@tonic-gate 	if (s != -1)
6560Sstevel@tonic-gate 		cr->cr_sgid = s;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	return (0);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate int
6620Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6650Sstevel@tonic-gate 
666*4321Scasper 	if (!VALID_UID(uid) || !VALID_GID(gid))
6670Sstevel@tonic-gate 		return (-1);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
6700Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	return (0);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate int
6760Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
6810Sstevel@tonic-gate 		return (-1);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	cr->cr_ngroups = n;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if (n > 0)
6860Sstevel@tonic-gate 		bcopy(grp, cr->cr_groups, n * sizeof (gid_t));
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	return (0);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate void
6920Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
6950Sstevel@tonic-gate 	cr->cr_projid = projid;
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  * This routine returns the pointer to the first element of the cr_groups
7000Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
7010Sstevel@tonic-gate  */
7020Sstevel@tonic-gate const gid_t *
7030Sstevel@tonic-gate crgetgroups(const cred_t *cr)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	return (cr->cr_groups);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate int
7090Sstevel@tonic-gate crgetngroups(const cred_t *cr)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	return (cr->cr_ngroups);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate void
7150Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
7180Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
7190Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
7200Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
7210Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
7220Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
7230Sstevel@tonic-gate 	pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max);
7240Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0;	/* in case ngroups == 0 */
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
7270Sstevel@tonic-gate 		bcopy(cr->cr_groups, pcrp->pr_groups,
7280Sstevel@tonic-gate 		    sizeof (gid_t) * cr->cr_ngroups);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate static int
7321676Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
7350Sstevel@tonic-gate 	au_tid_addr_t	tid;
7360Sstevel@tonic-gate 
7371676Sjpk 	if (secpolicy_audit_getattr(rcr) != 0)
7380Sstevel@tonic-gate 		return (-1);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
7410Sstevel@tonic-gate 	tid = ai->ai_termid;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
7440Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
7450Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
7480Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
7510Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	return (0);
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7561676Sjpk void
7571676Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
7581676Sjpk {
7591676Sjpk 	ts_label_t	*tslp;
7601676Sjpk 
7611676Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
7621676Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
7631676Sjpk }
7641676Sjpk 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
7670Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
7680Sstevel@tonic-gate  * memory and copy it twice.
7691676Sjpk  *
7701676Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
7711676Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
7721676Sjpk  * to determine whether audit info should be included.
7730Sstevel@tonic-gate  */
7740Sstevel@tonic-gate struct ucred_s *
7751676Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate 	struct ucred_s *uc;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
7800Sstevel@tonic-gate 	if (buf == NULL) {
7810Sstevel@tonic-gate 		uc = kmem_zalloc(ucredsize, KM_SLEEP);
7820Sstevel@tonic-gate 	} else {
7830Sstevel@tonic-gate 		bzero(buf, ucredsize);
7840Sstevel@tonic-gate 		uc = buf;
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 	uc->uc_size = ucredsize;
7870Sstevel@tonic-gate 	uc->uc_credoff = UCRED_CRED_OFF;
7880Sstevel@tonic-gate 	uc->uc_privoff = UCRED_PRIV_OFF;
7890Sstevel@tonic-gate 	uc->uc_audoff = UCRED_AUD_OFF;
7901676Sjpk 	uc->uc_labeloff = UCRED_LABEL_OFF;
7910Sstevel@tonic-gate 	uc->uc_pid = pid;
7920Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
7930Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
7940Sstevel@tonic-gate 
7951676Sjpk 	/*
7961676Sjpk 	 * Note that cred2uclabel() call should not be factored out
7971676Sjpk 	 * to the bottom of the if-else. UCXXX() macros depend on
7981676Sjpk 	 * uc_xxxoff values to work correctly.
7991676Sjpk 	 */
8001676Sjpk 	if (REMOTE_PEER_CRED(cr)) {
8011676Sjpk 		/*
8021676Sjpk 		 * other than label, the rest of cred info about a
8031676Sjpk 		 * remote peer isn't available.
8041676Sjpk 		 */
8051676Sjpk 		cred2uclabel(cr, UCLABEL(uc));
8061676Sjpk 		uc->uc_credoff = 0;
8071676Sjpk 		uc->uc_privoff = 0;
8080Sstevel@tonic-gate 		uc->uc_audoff = 0;
8091676Sjpk 	} else {
8101676Sjpk 		cred2prcred(cr, UCCRED(uc));
8111676Sjpk 		cred2prpriv(cr, UCPRIV(uc));
8121676Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
8131676Sjpk 			uc->uc_audoff = 0;
8141676Sjpk 		cred2uclabel(cr, UCLABEL(uc));
8151676Sjpk 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	return (uc);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate /*
8210Sstevel@tonic-gate  * Get the "ucred" of a process.
8220Sstevel@tonic-gate  */
8230Sstevel@tonic-gate struct ucred_s *
8240Sstevel@tonic-gate pgetucred(proc_t *p)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	cred_t *cr;
8270Sstevel@tonic-gate 	struct ucred_s *uc;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
8300Sstevel@tonic-gate 	cr = p->p_cred;
8310Sstevel@tonic-gate 	crhold(cr);
8320Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
8330Sstevel@tonic-gate 
8341676Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
8350Sstevel@tonic-gate 	crfree(cr);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	return (uc);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate /*
8410Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
8420Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
8430Sstevel@tonic-gate  * make that the uid instead and retry the call.
8440Sstevel@tonic-gate  * Private interface for NFS.
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate cred_t *
8470Sstevel@tonic-gate crnetadjust(cred_t *cr)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
8500Sstevel@tonic-gate 		cr = crdup(cr);
8510Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
8520Sstevel@tonic-gate 		return (cr);
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 	return (NULL);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate /*
8580Sstevel@tonic-gate  * The reference count is of interest when you want to check
8590Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
8600Sstevel@tonic-gate  */
8610Sstevel@tonic-gate uint_t
8620Sstevel@tonic-gate crgetref(const cred_t *cr)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	return (cr->cr_ref);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate static int
8680Sstevel@tonic-gate get_c2audit_load(void)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate 	static int	gotit = 0;
8710Sstevel@tonic-gate 	static int	c2audit_load;
8720Sstevel@tonic-gate 	u_longlong_t	audit_load_val;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	if (gotit)
8750Sstevel@tonic-gate 		return (c2audit_load);
8760Sstevel@tonic-gate 	audit_load_val = 0;		/* set default value once */
8770Sstevel@tonic-gate 	(void) mod_sysvar("c2audit", "audit_load", &audit_load_val);
8780Sstevel@tonic-gate 	c2audit_load = (int)audit_load_val;
8790Sstevel@tonic-gate 	gotit++;
8800Sstevel@tonic-gate 	return (c2audit_load);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate int
8840Sstevel@tonic-gate get_audit_ucrsize(void)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
8910Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
8920Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
8930Sstevel@tonic-gate  * same.
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate void
8960Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	ASSERT(cr != kcred);
9010Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
9020Sstevel@tonic-gate 	cr->cr_zone = zptr;
9030Sstevel@tonic-gate 	zone_cred_hold(zptr);
9040Sstevel@tonic-gate 	if (oldzptr)
9050Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
9060Sstevel@tonic-gate }
9071676Sjpk 
9081676Sjpk /*
9091676Sjpk  * Create a new cred based on the supplied label
9101676Sjpk  */
9111676Sjpk cred_t *
9121676Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
9131676Sjpk {
9141676Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
9151676Sjpk 	cred_t *cr = NULL;
9161676Sjpk 
9171676Sjpk 	if (lbl != NULL) {
9181676Sjpk 		if ((cr = kmem_cache_alloc(cred_cache, flags)) != NULL) {
9191676Sjpk 			bcopy(dummycr, cr, crsize);
9201676Sjpk 			cr->cr_label = lbl;
9211676Sjpk 		} else {
9221676Sjpk 			label_rele(lbl);
9231676Sjpk 		}
9241676Sjpk 	}
9251676Sjpk 
9261676Sjpk 	return (cr);
9271676Sjpk }
9281676Sjpk 
9291676Sjpk /*
9301676Sjpk  * Derive a new cred from the existing cred, but with a different label.
9311676Sjpk  * To be used when a cred is being shared, but the label needs to be changed
9321676Sjpk  * by a caller without affecting other users
9331676Sjpk  */
9341676Sjpk cred_t *
9351676Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags)
9361676Sjpk {
9371676Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
9381676Sjpk 	cred_t *newcr = NULL;
9391676Sjpk 
9401676Sjpk 	if (lbl != NULL) {
9411676Sjpk 		if ((newcr = kmem_cache_alloc(cred_cache, flags)) != NULL) {
9421676Sjpk 			bcopy(cr, newcr, crsize);
9431676Sjpk 			if (newcr->cr_zone)
9441676Sjpk 				zone_cred_hold(newcr->cr_zone);
9451676Sjpk 			newcr->cr_label = lbl;
9461676Sjpk 			newcr->cr_ref = 1;
9471676Sjpk 		} else {
9481676Sjpk 			label_rele(lbl);
9491676Sjpk 		}
9501676Sjpk 	}
9511676Sjpk 
9521676Sjpk 	return (newcr);
9531676Sjpk }
9541676Sjpk 
9551676Sjpk /*
9561676Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
9571676Sjpk  */
9581676Sjpk cred_t *
9591676Sjpk zone_kcred(void)
9601676Sjpk {
9611676Sjpk 	zone_t *zone;
9621676Sjpk 
9631676Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
9641676Sjpk 		return (zone->zone_kcred);
9651676Sjpk 	else
9661676Sjpk 		return (kcred);
9671676Sjpk }
968*4321Scasper 
969*4321Scasper boolean_t
970*4321Scasper valid_ephemeral_uid(uid_t id)
971*4321Scasper {
972*4321Scasper 	membar_consumer();
973*4321Scasper 	return (id > ephemeral_data.min_uid && id <= ephemeral_data.last_uid);
974*4321Scasper }
975*4321Scasper 
976*4321Scasper boolean_t
977*4321Scasper valid_ephemeral_gid(gid_t id)
978*4321Scasper {
979*4321Scasper 	membar_consumer();
980*4321Scasper 	return (id > ephemeral_data.min_gid && id <= ephemeral_data.last_gid);
981*4321Scasper }
982*4321Scasper 
983*4321Scasper int
984*4321Scasper eph_uid_alloc(int flags, uid_t *start, int count)
985*4321Scasper {
986*4321Scasper 	mutex_enter(&ephemeral_data.eph_lock);
987*4321Scasper 
988*4321Scasper 	/* Test for unsigned integer wrap around */
989*4321Scasper 	if (ephemeral_data.last_uid + count < ephemeral_data.last_uid) {
990*4321Scasper 		mutex_exit(&ephemeral_data.eph_lock);
991*4321Scasper 		return (-1);
992*4321Scasper 	}
993*4321Scasper 
994*4321Scasper 	/* first call or idmap crashed and state corrupted */
995*4321Scasper 	if (flags != 0)
996*4321Scasper 		ephemeral_data.min_uid = ephemeral_data.last_uid;
997*4321Scasper 
998*4321Scasper 	hasephids = B_TRUE;
999*4321Scasper 	*start = ephemeral_data.last_uid + 1;
1000*4321Scasper 	atomic_add_32(&ephemeral_data.last_uid, count);
1001*4321Scasper 	mutex_exit(&ephemeral_data.eph_lock);
1002*4321Scasper 	return (0);
1003*4321Scasper }
1004*4321Scasper 
1005*4321Scasper int
1006*4321Scasper eph_gid_alloc(int flags, gid_t *start, int count)
1007*4321Scasper {
1008*4321Scasper 	mutex_enter(&ephemeral_data.eph_lock);
1009*4321Scasper 
1010*4321Scasper 	/* Test for unsigned integer wrap around */
1011*4321Scasper 	if (ephemeral_data.last_gid + count < ephemeral_data.last_gid) {
1012*4321Scasper 		mutex_exit(&ephemeral_data.eph_lock);
1013*4321Scasper 		return (-1);
1014*4321Scasper 	}
1015*4321Scasper 
1016*4321Scasper 	/* first call or idmap crashed and state corrupted */
1017*4321Scasper 	if (flags != 0)
1018*4321Scasper 		ephemeral_data.min_gid = ephemeral_data.last_gid;
1019*4321Scasper 
1020*4321Scasper 	hasephids = B_TRUE;
1021*4321Scasper 	*start = ephemeral_data.last_gid + 1;
1022*4321Scasper 	atomic_add_32(&ephemeral_data.last_gid, count);
1023*4321Scasper 	mutex_exit(&ephemeral_data.eph_lock);
1024*4321Scasper 	return (0);
1025*4321Scasper }
1026*4321Scasper 
1027*4321Scasper /*
1028*4321Scasper  * If the credential contains any ephemeral IDs, map the credential
1029*4321Scasper  * to nobody.
1030*4321Scasper  */
1031*4321Scasper cred_t *
1032*4321Scasper crgetmapped(const cred_t *cr)
1033*4321Scasper {
1034*4321Scasper 	if (cr->cr_ksid != NULL) {
1035*4321Scasper 		int i;
1036*4321Scasper 
1037*4321Scasper 		for (i = 0; i < KSID_COUNT; i++)
1038*4321Scasper 			if (cr->cr_ksid->kr_sidx[i].ks_id > MAXUID)
1039*4321Scasper 				return (ephemeral_data.nobody);
1040*4321Scasper 		if (cr->cr_ksid->kr_sidlist != NULL &&
1041*4321Scasper 		    cr->cr_ksid->kr_sidlist->ksl_neid > 0) {
1042*4321Scasper 				return (ephemeral_data.nobody);
1043*4321Scasper 		}
1044*4321Scasper 	}
1045*4321Scasper 
1046*4321Scasper 	return ((cred_t *)cr);
1047*4321Scasper }
1048*4321Scasper 
1049*4321Scasper /* index should be in range for a ksidindex_t */
1050*4321Scasper void
1051*4321Scasper crsetsid(cred_t *cr, ksid_t *ksp, int index)
1052*4321Scasper {
1053*4321Scasper 	ASSERT(cr->cr_ref <= 2);
1054*4321Scasper 	ASSERT(index >= 0 && index < KSID_COUNT);
1055*4321Scasper 	if (cr->cr_ksid == NULL && ksp == NULL)
1056*4321Scasper 		return;
1057*4321Scasper 	cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index);
1058*4321Scasper }
1059*4321Scasper 
1060*4321Scasper void
1061*4321Scasper crsetsidlist(cred_t *cr, ksidlist_t *ksl)
1062*4321Scasper {
1063*4321Scasper 	ASSERT(cr->cr_ref <= 2);
1064*4321Scasper 	if (cr->cr_ksid == NULL && ksl == NULL)
1065*4321Scasper 		return;
1066*4321Scasper 	cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl);
1067*4321Scasper }
1068*4321Scasper 
1069*4321Scasper ksid_t *
1070*4321Scasper crgetsid(const cred_t *cr, int i)
1071*4321Scasper {
1072*4321Scasper 	ASSERT(i >= 0 && i < KSID_COUNT);
1073*4321Scasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain)
1074*4321Scasper 		return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]);
1075*4321Scasper 	return (NULL);
1076*4321Scasper }
1077*4321Scasper 
1078*4321Scasper ksidlist_t *
1079*4321Scasper crgetsidlist(const cred_t *cr)
1080*4321Scasper {
1081*4321Scasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidlist != NULL)
1082*4321Scasper 		return ((ksidlist_t *)&cr->cr_ksid->kr_sidlist);
1083*4321Scasper 	return (NULL);
1084*4321Scasper }
1085