xref: /titanic_51/usr/src/uts/common/os/cred.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/policy.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/user.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
557c478bd9Sstevel@tonic-gate #include <sys/ucred.h>
567c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate #include <c2/audit.h>
597c478bd9Sstevel@tonic-gate #include <sys/zone.h>
60*45916cd2Sjpk #include <sys/tsol/label.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static struct kmem_cache *cred_cache;
637c478bd9Sstevel@tonic-gate static size_t crsize = 0;
647c478bd9Sstevel@tonic-gate static int audoff = 0;
657c478bd9Sstevel@tonic-gate uint32_t ucredsize;
667c478bd9Sstevel@tonic-gate cred_t *kcred;
67*45916cd2Sjpk static cred_t *dummycr;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int get_c2audit_load(void);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
747c478bd9Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
757c478bd9Sstevel@tonic-gate 
76*45916cd2Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Initialize credentials data structures.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
837c478bd9Sstevel@tonic-gate cred_init(void)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	priv_init();
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1);
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * Make sure it's word-aligned.
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 	crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
947c478bd9Sstevel@tonic-gate #ifdef _LP64
957c478bd9Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
967c478bd9Sstevel@tonic-gate 		audoff = (crsize +
977c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
987c478bd9Sstevel@tonic-gate #else	/* _LP64 */
997c478bd9Sstevel@tonic-gate 		audoff = crsize;
1007c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
1017c478bd9Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1027c478bd9Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1067c478bd9Sstevel@tonic-gate 		NULL, NULL, NULL, NULL, NULL, 0);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
109*45916cd2Sjpk 	 * dummycr is used to copy initial state for creds.
110*45916cd2Sjpk 	 */
111*45916cd2Sjpk 	dummycr = cralloc();
112*45916cd2Sjpk 	bzero(dummycr, crsize);
113*45916cd2Sjpk 	dummycr->cr_ref = 1;
114*45916cd2Sjpk 	dummycr->cr_uid = -1;
115*45916cd2Sjpk 	dummycr->cr_gid = -1;
116*45916cd2Sjpk 	dummycr->cr_ruid = -1;
117*45916cd2Sjpk 	dummycr->cr_rgid = -1;
118*45916cd2Sjpk 	dummycr->cr_suid = -1;
119*45916cd2Sjpk 	dummycr->cr_sgid = -1;
120*45916cd2Sjpk 
121*45916cd2Sjpk 	/*
1227c478bd9Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
1237c478bd9Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
1247c478bd9Sstevel@tonic-gate 	 * sets filled in.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	kcred = cralloc();
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	bzero(kcred, crsize);
1297c478bd9Sstevel@tonic-gate 	kcred->cr_ref = 1;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
1327c478bd9Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
1357c478bd9Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
1387c478bd9Sstevel@tonic-gate 	if (!rstchown)
1397c478bd9Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
1427c478bd9Sstevel@tonic-gate 	if (rstlink)
1437c478bd9Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
146*45916cd2Sjpk 
147*45916cd2Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Set up credentials of p0.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
1537c478bd9Sstevel@tonic-gate 	curthread->t_cred = kcred;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	ucredsize = UCRED_SIZE;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate cred_t *
1627c478bd9Sstevel@tonic-gate cralloc(void)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
1657c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
1667c478bd9Sstevel@tonic-gate 	cr->cr_zone = NULL;
167*45916cd2Sjpk 	cr->cr_label = NULL;
1687c478bd9Sstevel@tonic-gate 	return (cr);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
1737c478bd9Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate cred_t *
1767c478bd9Sstevel@tonic-gate crget(void)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
1817c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;
1827c478bd9Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
183*45916cd2Sjpk 	if (cr->cr_label)
184*45916cd2Sjpk 		label_hold(cr->cr_label);
1857c478bd9Sstevel@tonic-gate 	return (cr);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
1907c478bd9Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
1917c478bd9Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
1927c478bd9Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
1957c478bd9Sstevel@tonic-gate  * and p->p_cred set.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate void
2007c478bd9Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
2037c478bd9Sstevel@tonic-gate 	kthread_id_t	first;
2047c478bd9Sstevel@tonic-gate 	cred_t *oldcr;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
2107c478bd9Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	t = curthread;
2137c478bd9Sstevel@tonic-gate 	oldcr = t->t_cred;
2147c478bd9Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
2157c478bd9Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
2217c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
2227c478bd9Sstevel@tonic-gate 		first = curthread;
2237c478bd9Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
2247c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
2257c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Put a hold on a cred structure.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate void
2337c478bd9Sstevel@tonic-gate crhold(cred_t *cr)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	atomic_add_32(&cr->cr_ref, 1);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
240*45916cd2Sjpk  * If cred uses label different from zone label, free it.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate void
2437c478bd9Sstevel@tonic-gate crfree(cred_t *cr)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) {
2467c478bd9Sstevel@tonic-gate 		ASSERT(cr != kcred);
247*45916cd2Sjpk 		if (cr->cr_label)
248*45916cd2Sjpk 			label_rele(cr->cr_label);
2497c478bd9Sstevel@tonic-gate 		if (cr->cr_zone)
2507c478bd9Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
2517c478bd9Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
2577c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
2587c478bd9Sstevel@tonic-gate  * 	and one for the thread.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate cred_t *
2617c478bd9Sstevel@tonic-gate crcopy(cred_t *cr)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	cred_t *newcr;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	newcr = cralloc();
2667c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
2677c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
2687c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
269*45916cd2Sjpk 	if (newcr->cr_label)
270*45916cd2Sjpk 		label_hold(cr->cr_label);
2717c478bd9Sstevel@tonic-gate 	crfree(cr);
2727c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
2737c478bd9Sstevel@tonic-gate 	return (newcr);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
2787c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
2797c478bd9Sstevel@tonic-gate  * 	and one for the thread.
2807c478bd9Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
2817c478bd9Sstevel@tonic-gate  * "new" cred.
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate void
2847c478bd9Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
2877c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
2887c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
289*45916cd2Sjpk 	if (newcr->cr_label)
290*45916cd2Sjpk 		label_hold(newcr->cr_label);
2917c478bd9Sstevel@tonic-gate 	crfree(oldcr);
2927c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
2977c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate cred_t *
3007c478bd9Sstevel@tonic-gate crdup(cred_t *cr)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	cred_t *newcr;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	newcr = cralloc();
3057c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3067c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3077c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
308*45916cd2Sjpk 	if (newcr->cr_label)
309*45916cd2Sjpk 		label_hold(newcr->cr_label);
3107c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
3117c478bd9Sstevel@tonic-gate 	return (newcr);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
3167c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
3177c478bd9Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
3187c478bd9Sstevel@tonic-gate  * "new" cred.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate void
3217c478bd9Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
3247c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3257c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
326*45916cd2Sjpk 	if (newcr->cr_label)
327*45916cd2Sjpk 		label_hold(newcr->cr_label);
3287c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate cred_t *
335*45916cd2Sjpk crgetcred(void)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	cred_t *cr;
3387c478bd9Sstevel@tonic-gate 	proc_t *p;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
3417c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
3427c478bd9Sstevel@tonic-gate 	crhold(cr = p->p_cred);
3437c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
3447c478bd9Sstevel@tonic-gate 	return (cr);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Backward compatibility check for suser().
3497c478bd9Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
3507c478bd9Sstevel@tonic-gate  * done through use of privilege in the audit trail.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate int
3537c478bd9Sstevel@tonic-gate suser(cred_t *cr)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
3567c478bd9Sstevel@tonic-gate 	    == 0);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
3617c478bd9Sstevel@tonic-gate  * described by the supplied credentials.
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate int
3647c478bd9Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	if (gid == cr->cr_gid)
3677c478bd9Sstevel@tonic-gate 		return (1);
3687c478bd9Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate int
3757c478bd9Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	const gid_t *gp, *endgp;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	endgp = &cr->cr_groups[cr->cr_ngroups];
3807c478bd9Sstevel@tonic-gate 	for (gp = cr->cr_groups; gp < endgp; gp++)
3817c478bd9Sstevel@tonic-gate 		if (*gp == gid)
3827c478bd9Sstevel@tonic-gate 			return (1);
3837c478bd9Sstevel@tonic-gate 	return (0);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate  * This function is called to check whether the credentials set
3887c478bd9Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
3897c478bd9Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
3907c478bd9Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  * The rules are:
3937c478bd9Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
3947c478bd9Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
3957c478bd9Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
3967c478bd9Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
3977c478bd9Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
3987c478bd9Sstevel@tonic-gate  *     succeeds
3997c478bd9Sstevel@tonic-gate  * (4) otherwise, the check fails
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate int
4027c478bd9Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	if (scrp == tcrp)
4057c478bd9Sstevel@tonic-gate 		return (1);
4067c478bd9Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
4077c478bd9Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
4087c478bd9Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
4097c478bd9Sstevel@tonic-gate 		return (0);
4107c478bd9Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
4117c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
4127c478bd9Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
4137c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
4147c478bd9Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
4157c478bd9Sstevel@tonic-gate 		return (1);
4167c478bd9Sstevel@tonic-gate 	return (0);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
4217c478bd9Sstevel@tonic-gate  * additionally returns success if the proc_t's match
4227c478bd9Sstevel@tonic-gate  * It is the preferred interface for most uses.
4237c478bd9Sstevel@tonic-gate  * And it will acquire pcrlock itself, so it assert's that it shouldn't
4247c478bd9Sstevel@tonic-gate  * be held.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate int
4277c478bd9Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	int rets;
4307c478bd9Sstevel@tonic-gate 	cred_t *tcrp;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (tp == sp)
4357c478bd9Sstevel@tonic-gate 		return (1);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
4387c478bd9Sstevel@tonic-gate 		return (0);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
4417c478bd9Sstevel@tonic-gate 	tcrp = tp->p_cred;
4427c478bd9Sstevel@tonic-gate 	rets = hasprocperm(tcrp, scrp);
4437c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	return (rets);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
4507c478bd9Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
4517c478bd9Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
4527c478bd9Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
4537c478bd9Sstevel@tonic-gate  *
4547c478bd9Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
4557c478bd9Sstevel@tonic-gate  * 1 if they do not.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate int
4587c478bd9Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (cr1 == cr2)
4627c478bd9Sstevel@tonic-gate 		return (0);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
4657c478bd9Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
4667c478bd9Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
4677c478bd9Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
4687c478bd9Sstevel@tonic-gate 	    cr1->cr_ngroups == cr2->cr_ngroups &&
4697c478bd9Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
4707c478bd9Sstevel@tonic-gate 	    bcmp(cr1->cr_groups, cr2->cr_groups,
4717c478bd9Sstevel@tonic-gate 		    cr1->cr_ngroups * sizeof (gid_t)) == 0) {
4727c478bd9Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	return (1);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * Read access functions to cred_t.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate uid_t
4817c478bd9Sstevel@tonic-gate crgetuid(const cred_t *cr)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	return (cr->cr_uid);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate uid_t
4877c478bd9Sstevel@tonic-gate crgetruid(const cred_t *cr)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	return (cr->cr_ruid);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate uid_t
4937c478bd9Sstevel@tonic-gate crgetsuid(const cred_t *cr)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	return (cr->cr_suid);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate gid_t
4997c478bd9Sstevel@tonic-gate crgetgid(const cred_t *cr)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	return (cr->cr_gid);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate gid_t
5057c478bd9Sstevel@tonic-gate crgetrgid(const cred_t *cr)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	return (cr->cr_rgid);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate gid_t
5117c478bd9Sstevel@tonic-gate crgetsgid(const cred_t *cr)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	return (cr->cr_sgid);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate const auditinfo_addr_t *
5177c478bd9Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate auditinfo_addr_t *
5237c478bd9Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	return (CR_AUINFO(cr));
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate zoneid_t
5297c478bd9Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
5307c478bd9Sstevel@tonic-gate {
531*45916cd2Sjpk 	return (cr->cr_zone == NULL ?
532*45916cd2Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
533*45916cd2Sjpk 	    cr->cr_zone->zone_id);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate projid_t
5377c478bd9Sstevel@tonic-gate crgetprojid(const cred_t *cr)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	return (cr->cr_projid);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
542*45916cd2Sjpk zone_t *
543*45916cd2Sjpk crgetzone(const cred_t *cr)
544*45916cd2Sjpk {
545*45916cd2Sjpk 	return (cr->cr_zone);
546*45916cd2Sjpk }
547*45916cd2Sjpk 
548*45916cd2Sjpk struct ts_label_s *
549*45916cd2Sjpk crgetlabel(const cred_t *cr)
550*45916cd2Sjpk {
551*45916cd2Sjpk 	return (cr->cr_label ?
552*45916cd2Sjpk 	    cr->cr_label :
553*45916cd2Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
554*45916cd2Sjpk }
555*45916cd2Sjpk 
556*45916cd2Sjpk boolean_t
557*45916cd2Sjpk crisremote(const cred_t *cr)
558*45916cd2Sjpk {
559*45916cd2Sjpk 	return (REMOTE_PEER_CRED(cr));
560*45916cd2Sjpk }
561*45916cd2Sjpk 
5627c478bd9Sstevel@tonic-gate #define	BADID(x)	((x) != -1 && (unsigned int)(x) > MAXUID)
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate int
5657c478bd9Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (BADID(r) || BADID(e) || BADID(s))
5707c478bd9Sstevel@tonic-gate 		return (-1);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (r != -1)
5737c478bd9Sstevel@tonic-gate 		cr->cr_ruid = r;
5747c478bd9Sstevel@tonic-gate 	if (e != -1)
5757c478bd9Sstevel@tonic-gate 		cr->cr_uid = e;
5767c478bd9Sstevel@tonic-gate 	if (s != -1)
5777c478bd9Sstevel@tonic-gate 		cr->cr_suid = s;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	return (0);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate int
5837c478bd9Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (BADID(r) || BADID(e) || BADID(s))
5887c478bd9Sstevel@tonic-gate 		return (-1);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if (r != -1)
5917c478bd9Sstevel@tonic-gate 		cr->cr_rgid = r;
5927c478bd9Sstevel@tonic-gate 	if (e != -1)
5937c478bd9Sstevel@tonic-gate 		cr->cr_gid = e;
5947c478bd9Sstevel@tonic-gate 	if (s != -1)
5957c478bd9Sstevel@tonic-gate 		cr->cr_sgid = s;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	return (0);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate int
6017c478bd9Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (uid < 0 || uid > MAXUID || gid < 0 || gid > MAXUID)
6067c478bd9Sstevel@tonic-gate 		return (-1);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
6097c478bd9Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	return (0);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate int
6157c478bd9Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
6207c478bd9Sstevel@tonic-gate 		return (-1);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	cr->cr_ngroups = n;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (n > 0)
6257c478bd9Sstevel@tonic-gate 		bcopy(grp, cr->cr_groups, n * sizeof (gid_t));
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	return (0);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate void
6317c478bd9Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
6347c478bd9Sstevel@tonic-gate 	cr->cr_projid = projid;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * This routine returns the pointer to the first element of the cr_groups
6397c478bd9Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate const gid_t *
6427c478bd9Sstevel@tonic-gate crgetgroups(const cred_t *cr)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	return (cr->cr_groups);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate int
6487c478bd9Sstevel@tonic-gate crgetngroups(const cred_t *cr)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	return (cr->cr_ngroups);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate void
6547c478bd9Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
6577c478bd9Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
6587c478bd9Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
6597c478bd9Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
6607c478bd9Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
6617c478bd9Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
6627c478bd9Sstevel@tonic-gate 	pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max);
6637c478bd9Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0;	/* in case ngroups == 0 */
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
6667c478bd9Sstevel@tonic-gate 		bcopy(cr->cr_groups, pcrp->pr_groups,
6677c478bd9Sstevel@tonic-gate 		    sizeof (gid_t) * cr->cr_ngroups);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate static int
671*45916cd2Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
6747c478bd9Sstevel@tonic-gate 	au_tid_addr_t	tid;
6757c478bd9Sstevel@tonic-gate 
676*45916cd2Sjpk 	if (secpolicy_audit_getattr(rcr) != 0)
6777c478bd9Sstevel@tonic-gate 		return (-1);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
6807c478bd9Sstevel@tonic-gate 	tid = ai->ai_termid;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
6837c478bd9Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
6847c478bd9Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
6877c478bd9Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
6907c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	return (0);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
695*45916cd2Sjpk void
696*45916cd2Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
697*45916cd2Sjpk {
698*45916cd2Sjpk 	ts_label_t	*tslp;
699*45916cd2Sjpk 
700*45916cd2Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
701*45916cd2Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
702*45916cd2Sjpk }
703*45916cd2Sjpk 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
7067c478bd9Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
7077c478bd9Sstevel@tonic-gate  * memory and copy it twice.
708*45916cd2Sjpk  *
709*45916cd2Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
710*45916cd2Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
711*45916cd2Sjpk  * to determine whether audit info should be included.
7127c478bd9Sstevel@tonic-gate  */
7137c478bd9Sstevel@tonic-gate struct ucred_s *
714*45916cd2Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
7197c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
7207c478bd9Sstevel@tonic-gate 		uc = kmem_zalloc(ucredsize, KM_SLEEP);
7217c478bd9Sstevel@tonic-gate 	} else {
7227c478bd9Sstevel@tonic-gate 		bzero(buf, ucredsize);
7237c478bd9Sstevel@tonic-gate 		uc = buf;
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	uc->uc_size = ucredsize;
7267c478bd9Sstevel@tonic-gate 	uc->uc_credoff = UCRED_CRED_OFF;
7277c478bd9Sstevel@tonic-gate 	uc->uc_privoff = UCRED_PRIV_OFF;
7287c478bd9Sstevel@tonic-gate 	uc->uc_audoff = UCRED_AUD_OFF;
729*45916cd2Sjpk 	uc->uc_labeloff = UCRED_LABEL_OFF;
7307c478bd9Sstevel@tonic-gate 	uc->uc_pid = pid;
7317c478bd9Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
7327c478bd9Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
7337c478bd9Sstevel@tonic-gate 
734*45916cd2Sjpk 	/*
735*45916cd2Sjpk 	 * Note that cred2uclabel() call should not be factored out
736*45916cd2Sjpk 	 * to the bottom of the if-else. UCXXX() macros depend on
737*45916cd2Sjpk 	 * uc_xxxoff values to work correctly.
738*45916cd2Sjpk 	 */
739*45916cd2Sjpk 	if (REMOTE_PEER_CRED(cr)) {
740*45916cd2Sjpk 		/*
741*45916cd2Sjpk 		 * other than label, the rest of cred info about a
742*45916cd2Sjpk 		 * remote peer isn't available.
743*45916cd2Sjpk 		 */
744*45916cd2Sjpk 		cred2uclabel(cr, UCLABEL(uc));
745*45916cd2Sjpk 		uc->uc_credoff = 0;
746*45916cd2Sjpk 		uc->uc_privoff = 0;
747*45916cd2Sjpk 		uc->uc_audoff = 0;
748*45916cd2Sjpk 	} else {
7497c478bd9Sstevel@tonic-gate 		cred2prcred(cr, UCCRED(uc));
7507c478bd9Sstevel@tonic-gate 		cred2prpriv(cr, UCPRIV(uc));
751*45916cd2Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
7527c478bd9Sstevel@tonic-gate 			uc->uc_audoff = 0;
753*45916cd2Sjpk 		cred2uclabel(cr, UCLABEL(uc));
754*45916cd2Sjpk 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	return (uc);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate /*
7607c478bd9Sstevel@tonic-gate  * Get the "ucred" of a process.
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate struct ucred_s *
7637c478bd9Sstevel@tonic-gate pgetucred(proc_t *p)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	cred_t *cr;
7667c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
7697c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
7707c478bd9Sstevel@tonic-gate 	crhold(cr);
7717c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
7727c478bd9Sstevel@tonic-gate 
773*45916cd2Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
7747c478bd9Sstevel@tonic-gate 	crfree(cr);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (uc);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate /*
7807c478bd9Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
7817c478bd9Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
7827c478bd9Sstevel@tonic-gate  * make that the uid instead and retry the call.
7837c478bd9Sstevel@tonic-gate  * Private interface for NFS.
7847c478bd9Sstevel@tonic-gate  */
7857c478bd9Sstevel@tonic-gate cred_t *
7867c478bd9Sstevel@tonic-gate crnetadjust(cred_t *cr)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
7897c478bd9Sstevel@tonic-gate 		cr = crdup(cr);
7907c478bd9Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
7917c478bd9Sstevel@tonic-gate 		return (cr);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 	return (NULL);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate  * The reference count is of interest when you want to check
7987c478bd9Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
7997c478bd9Sstevel@tonic-gate  */
8007c478bd9Sstevel@tonic-gate uint_t
8017c478bd9Sstevel@tonic-gate crgetref(const cred_t *cr)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	return (cr->cr_ref);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static int
8077c478bd9Sstevel@tonic-gate get_c2audit_load(void)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	static int	gotit = 0;
8107c478bd9Sstevel@tonic-gate 	static int	c2audit_load;
8117c478bd9Sstevel@tonic-gate 	u_longlong_t	audit_load_val;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (gotit)
8147c478bd9Sstevel@tonic-gate 		return (c2audit_load);
8157c478bd9Sstevel@tonic-gate 	audit_load_val = 0;		/* set default value once */
8167c478bd9Sstevel@tonic-gate 	(void) mod_sysvar("c2audit", "audit_load", &audit_load_val);
8177c478bd9Sstevel@tonic-gate 	c2audit_load = (int)audit_load_val;
8187c478bd9Sstevel@tonic-gate 	gotit++;
8197c478bd9Sstevel@tonic-gate 	return (c2audit_load);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate int
8237c478bd9Sstevel@tonic-gate get_audit_ucrsize(void)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate /*
8297c478bd9Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
8307c478bd9Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
8317c478bd9Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
8327c478bd9Sstevel@tonic-gate  * same.
8337c478bd9Sstevel@tonic-gate  */
8347c478bd9Sstevel@tonic-gate void
8357c478bd9Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	ASSERT(cr != kcred);
8407c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
8417c478bd9Sstevel@tonic-gate 	cr->cr_zone = zptr;
8427c478bd9Sstevel@tonic-gate 	zone_cred_hold(zptr);
8437c478bd9Sstevel@tonic-gate 	if (oldzptr)
8447c478bd9Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
8457c478bd9Sstevel@tonic-gate }
846*45916cd2Sjpk 
847*45916cd2Sjpk /*
848*45916cd2Sjpk  * Create a new cred based on the supplied label
849*45916cd2Sjpk  */
850*45916cd2Sjpk cred_t *
851*45916cd2Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
852*45916cd2Sjpk {
853*45916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
854*45916cd2Sjpk 	cred_t *cr = NULL;
855*45916cd2Sjpk 
856*45916cd2Sjpk 	if (lbl != NULL) {
857*45916cd2Sjpk 		if ((cr = kmem_cache_alloc(cred_cache, flags)) != NULL) {
858*45916cd2Sjpk 			bcopy(dummycr, cr, crsize);
859*45916cd2Sjpk 			cr->cr_label = lbl;
860*45916cd2Sjpk 		} else {
861*45916cd2Sjpk 			label_rele(lbl);
862*45916cd2Sjpk 		}
863*45916cd2Sjpk 	}
864*45916cd2Sjpk 
865*45916cd2Sjpk 	return (cr);
866*45916cd2Sjpk }
867*45916cd2Sjpk 
868*45916cd2Sjpk /*
869*45916cd2Sjpk  * Derive a new cred from the existing cred, but with a different label.
870*45916cd2Sjpk  * To be used when a cred is being shared, but the label needs to be changed
871*45916cd2Sjpk  * by a caller without affecting other users
872*45916cd2Sjpk  */
873*45916cd2Sjpk cred_t *
874*45916cd2Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags)
875*45916cd2Sjpk {
876*45916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
877*45916cd2Sjpk 	cred_t *newcr = NULL;
878*45916cd2Sjpk 
879*45916cd2Sjpk 	if (lbl != NULL) {
880*45916cd2Sjpk 		if ((newcr = kmem_cache_alloc(cred_cache, flags)) != NULL) {
881*45916cd2Sjpk 			bcopy(cr, newcr, crsize);
882*45916cd2Sjpk 			if (newcr->cr_zone)
883*45916cd2Sjpk 				zone_cred_hold(newcr->cr_zone);
884*45916cd2Sjpk 			newcr->cr_label = lbl;
885*45916cd2Sjpk 			newcr->cr_ref = 1;
886*45916cd2Sjpk 		} else {
887*45916cd2Sjpk 			label_rele(lbl);
888*45916cd2Sjpk 		}
889*45916cd2Sjpk 	}
890*45916cd2Sjpk 
891*45916cd2Sjpk 	return (newcr);
892*45916cd2Sjpk }
893*45916cd2Sjpk 
894*45916cd2Sjpk /*
895*45916cd2Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
896*45916cd2Sjpk  */
897*45916cd2Sjpk cred_t *
898*45916cd2Sjpk zone_kcred(void)
899*45916cd2Sjpk {
900*45916cd2Sjpk 	zone_t *zone;
901*45916cd2Sjpk 
902*45916cd2Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
903*45916cd2Sjpk 		return (zone->zone_kcred);
904*45916cd2Sjpk 	else
905*45916cd2Sjpk 		return (kcred);
906*45916cd2Sjpk }
907