xref: /titanic_51/usr/src/uts/common/os/cred.c (revision 134a1f4e3289b54e0f980e9cf05352e419a60bee)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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*134a1f4eSCasper H.S. Dik  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/policy.h>
447c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
457c478bd9Sstevel@tonic-gate #include <sys/errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/proc.h>
497c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
527c478bd9Sstevel@tonic-gate #include <sys/ucred.h>
537c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
547c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
55f48205beScasper #include <sys/avl.h>
56da6c28aaSamw #include <sys/door.h>
577c478bd9Sstevel@tonic-gate #include <c2/audit.h>
587c478bd9Sstevel@tonic-gate #include <sys/zone.h>
5945916cd2Sjpk #include <sys/tsol/label.h>
60f48205beScasper #include <sys/sid.h>
61c5c4113dSnw141292 #include <sys/idmap.h>
62ddf7fe95Scasper #include <sys/klpd.h>
63da6c28aaSamw #include <sys/varargs.h>
64005d3febSMarek Pospisil #include <sys/sysconf.h>
6567dbe2beSCasper H.S. Dik #include <util/qsort.h>
66f48205beScasper 
67bda89588Sjp151216 
68bda89588Sjp151216 /* Ephemeral IDs Zones specific data */
69bda89588Sjp151216 typedef struct ephemeral_zsd {
70bda89588Sjp151216 	uid_t		min_uid;
71bda89588Sjp151216 	uid_t		last_uid;
72bda89588Sjp151216 	gid_t		min_gid;
73bda89588Sjp151216 	gid_t		last_gid;
74f48205beScasper 	kmutex_t	eph_lock;
75bda89588Sjp151216 	cred_t		*eph_nobody;
76bda89588Sjp151216 } ephemeral_zsd_t;
77bda89588Sjp151216 
7867dbe2beSCasper H.S. Dik /* Supplemental groups list. */
7967dbe2beSCasper H.S. Dik typedef struct credgrp {
8067dbe2beSCasper H.S. Dik 	uint_t		crg_ref;
8167dbe2beSCasper H.S. Dik 	uint_t		crg_ngroups;
8267dbe2beSCasper H.S. Dik 	gid_t		crg_groups[1];
8367dbe2beSCasper H.S. Dik } credgrp_t;
8467dbe2beSCasper H.S. Dik 
8567dbe2beSCasper H.S. Dik static void crgrphold(credgrp_t *);
8667dbe2beSCasper H.S. Dik 
8767dbe2beSCasper H.S. Dik #define	CREDGRPSZ(ngrp)	(sizeof (credgrp_t) + ((ngrp - 1) * sizeof (gid_t)))
88bda89588Sjp151216 
89bda89588Sjp151216 static kmutex_t		ephemeral_zone_mutex;
90bda89588Sjp151216 static zone_key_t	ephemeral_zone_key;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static struct kmem_cache *cred_cache;
937c478bd9Sstevel@tonic-gate static size_t		crsize = 0;
947c478bd9Sstevel@tonic-gate static int		audoff = 0;
957c478bd9Sstevel@tonic-gate uint32_t		ucredsize;
967c478bd9Sstevel@tonic-gate cred_t			*kcred;
9745916cd2Sjpk static cred_t		*dummycr;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int get_c2audit_load(void);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
1047c478bd9Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
1057c478bd9Sstevel@tonic-gate 
10645916cd2Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
1077c478bd9Sstevel@tonic-gate 
10867dbe2beSCasper H.S. Dik #define	BIN_GROUP_SEARCH_CUTOFF	16
109f48205beScasper 
110f48205beScasper static boolean_t hasephids = B_FALSE;
111f48205beScasper 
112bda89588Sjp151216 static ephemeral_zsd_t *
113bda89588Sjp151216 get_ephemeral_zsd(zone_t *zone)
114bda89588Sjp151216 {
115bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
116bda89588Sjp151216 
117bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
118bda89588Sjp151216 	if (eph_zsd != NULL) {
119bda89588Sjp151216 		return (eph_zsd);
120bda89588Sjp151216 	}
121bda89588Sjp151216 
122bda89588Sjp151216 	mutex_enter(&ephemeral_zone_mutex);
123bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
124bda89588Sjp151216 	if (eph_zsd == NULL) {
125bda89588Sjp151216 		eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP);
126bda89588Sjp151216 		eph_zsd->min_uid = MAXUID;
127bda89588Sjp151216 		eph_zsd->last_uid = IDMAP_WK__MAX_UID;
128bda89588Sjp151216 		eph_zsd->min_gid = MAXUID;
129bda89588Sjp151216 		eph_zsd->last_gid = IDMAP_WK__MAX_GID;
130bda89588Sjp151216 		mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL);
131bda89588Sjp151216 
132bda89588Sjp151216 		/*
133bda89588Sjp151216 		 * nobody is used to map SID containing CRs.
134bda89588Sjp151216 		 */
135bda89588Sjp151216 		eph_zsd->eph_nobody = crdup(zone->zone_kcred);
136bda89588Sjp151216 		(void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY);
137bda89588Sjp151216 		CR_FLAGS(eph_zsd->eph_nobody) = 0;
138bda89588Sjp151216 		eph_zsd->eph_nobody->cr_zone = zone;
139bda89588Sjp151216 
140bda89588Sjp151216 		(void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd);
141bda89588Sjp151216 	}
142bda89588Sjp151216 	mutex_exit(&ephemeral_zone_mutex);
143bda89588Sjp151216 	return (eph_zsd);
144bda89588Sjp151216 }
145bda89588Sjp151216 
1465f9878b0Sken Powell - Sun Microsystem static cred_t *crdup_flags(const cred_t *, int);
147ddf7fe95Scasper static cred_t *cralloc_flags(int);
148ddf7fe95Scasper 
149bda89588Sjp151216 /*
150bda89588Sjp151216  * This function is called when a zone is destroyed
151bda89588Sjp151216  */
152bda89588Sjp151216 static void
153bda89588Sjp151216 /* ARGSUSED */
154bda89588Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg)
155bda89588Sjp151216 {
156bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = arg;
157bda89588Sjp151216 	if (eph_zsd != NULL) {
158bda89588Sjp151216 		mutex_destroy(&eph_zsd->eph_lock);
159bda89588Sjp151216 		crfree(eph_zsd->eph_nobody);
160bda89588Sjp151216 		kmem_free(eph_zsd, sizeof (ephemeral_zsd_t));
161bda89588Sjp151216 	}
162bda89588Sjp151216 }
163bda89588Sjp151216 
164bda89588Sjp151216 
165bda89588Sjp151216 
166f48205beScasper /*
1677c478bd9Sstevel@tonic-gate  * Initialize credentials data structures.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate void
1717c478bd9Sstevel@tonic-gate cred_init(void)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	priv_init();
1747c478bd9Sstevel@tonic-gate 
17567dbe2beSCasper H.S. Dik 	crsize = sizeof (cred_t);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
1787c478bd9Sstevel@tonic-gate #ifdef _LP64
1797c478bd9Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
1807c478bd9Sstevel@tonic-gate 		audoff = (crsize +
1817c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
1827c478bd9Sstevel@tonic-gate #else	/* _LP64 */
1837c478bd9Sstevel@tonic-gate 		audoff = crsize;
1847c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
1857c478bd9Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1867c478bd9Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1907c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
19345916cd2Sjpk 	 * dummycr is used to copy initial state for creds.
19445916cd2Sjpk 	 */
19545916cd2Sjpk 	dummycr = cralloc();
19645916cd2Sjpk 	bzero(dummycr, crsize);
19745916cd2Sjpk 	dummycr->cr_ref = 1;
198f48205beScasper 	dummycr->cr_uid = (uid_t)-1;
199f48205beScasper 	dummycr->cr_gid = (gid_t)-1;
200f48205beScasper 	dummycr->cr_ruid = (uid_t)-1;
201f48205beScasper 	dummycr->cr_rgid = (gid_t)-1;
202f48205beScasper 	dummycr->cr_suid = (uid_t)-1;
203f48205beScasper 	dummycr->cr_sgid = (gid_t)-1;
204f48205beScasper 
20545916cd2Sjpk 
20645916cd2Sjpk 	/*
2077c478bd9Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
2087c478bd9Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
2097c478bd9Sstevel@tonic-gate 	 * sets filled in.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	kcred = cralloc();
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	bzero(kcred, crsize);
2147c478bd9Sstevel@tonic-gate 	kcred->cr_ref = 1;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
2177c478bd9Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
2207c478bd9Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
2237c478bd9Sstevel@tonic-gate 	if (!rstchown)
2247c478bd9Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
2277c478bd9Sstevel@tonic-gate 	if (rstlink)
2287c478bd9Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
23145916cd2Sjpk 
23245916cd2Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * Set up credentials of p0.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
2387c478bd9Sstevel@tonic-gate 	curthread->t_cred = kcred;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	ucredsize = UCRED_SIZE;
241bda89588Sjp151216 
242bda89588Sjp151216 	mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL);
243bda89588Sjp151216 	zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
2487c478bd9Sstevel@tonic-gate  */
249ddf7fe95Scasper static cred_t *
250ddf7fe95Scasper cralloc_flags(int flgs)
2517c478bd9Sstevel@tonic-gate {
252ddf7fe95Scasper 	cred_t *cr = kmem_cache_alloc(cred_cache, flgs);
253ddf7fe95Scasper 
254ddf7fe95Scasper 	if (cr == NULL)
255ddf7fe95Scasper 		return (NULL);
256ddf7fe95Scasper 
2577c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
2587c478bd9Sstevel@tonic-gate 	cr->cr_zone = NULL;
25945916cd2Sjpk 	cr->cr_label = NULL;
260f48205beScasper 	cr->cr_ksid = NULL;
261ddf7fe95Scasper 	cr->cr_klpd = NULL;
26267dbe2beSCasper H.S. Dik 	cr->cr_grps = NULL;
263f48205beScasper 	return (cr);
264f48205beScasper }
265f48205beScasper 
266ddf7fe95Scasper cred_t *
267ddf7fe95Scasper cralloc(void)
268ddf7fe95Scasper {
269ddf7fe95Scasper 	return (cralloc_flags(KM_SLEEP));
270ddf7fe95Scasper }
271ddf7fe95Scasper 
272f48205beScasper /*
273f48205beScasper  * As cralloc but prepared for ksid change (if appropriate).
274f48205beScasper  */
275f48205beScasper cred_t *
276f48205beScasper cralloc_ksid(void)
277f48205beScasper {
278f48205beScasper 	cred_t *cr = cralloc();
279f48205beScasper 	if (hasephids)
280f48205beScasper 		cr->cr_ksid = kcrsid_alloc();
2817c478bd9Sstevel@tonic-gate 	return (cr);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
2867c478bd9Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate cred_t *
2897c478bd9Sstevel@tonic-gate crget(void)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
2947c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;
2957c478bd9Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
29645916cd2Sjpk 	if (cr->cr_label)
29745916cd2Sjpk 		label_hold(cr->cr_label);
298ddf7fe95Scasper 	ASSERT(cr->cr_klpd == NULL);
29967dbe2beSCasper H.S. Dik 	ASSERT(cr->cr_grps == NULL);
3007c478bd9Sstevel@tonic-gate 	return (cr);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
3057c478bd9Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
3067c478bd9Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
3077c478bd9Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
3087c478bd9Sstevel@tonic-gate  *
3097c478bd9Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
3107c478bd9Sstevel@tonic-gate  * and p->p_cred set.
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate void
3157c478bd9Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
3187c478bd9Sstevel@tonic-gate 	kthread_id_t	first;
3197c478bd9Sstevel@tonic-gate 	cred_t *oldcr;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
3257c478bd9Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	t = curthread;
3287c478bd9Sstevel@tonic-gate 	oldcr = t->t_cred;
3297c478bd9Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
3307c478bd9Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
3367c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
3377c478bd9Sstevel@tonic-gate 		first = curthread;
3387c478bd9Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
3397c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
3407c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Put a hold on a cred structure.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate void
3487c478bd9Sstevel@tonic-gate crhold(cred_t *cr)
3497c478bd9Sstevel@tonic-gate {
350*134a1f4eSCasper H.S. Dik 	ASSERT(cr->cr_ref != 0xdeadbeef && cr->cr_ref != 0);
3517c478bd9Sstevel@tonic-gate 	atomic_add_32(&cr->cr_ref, 1);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
35645916cd2Sjpk  * If cred uses label different from zone label, free it.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate void
3597c478bd9Sstevel@tonic-gate crfree(cred_t *cr)
3607c478bd9Sstevel@tonic-gate {
361*134a1f4eSCasper H.S. Dik 	ASSERT(cr->cr_ref != 0xdeadbeef && cr->cr_ref != 0);
3627c478bd9Sstevel@tonic-gate 	if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) {
3637c478bd9Sstevel@tonic-gate 		ASSERT(cr != kcred);
36445916cd2Sjpk 		if (cr->cr_label)
36545916cd2Sjpk 			label_rele(cr->cr_label);
366ddf7fe95Scasper 		if (cr->cr_klpd)
367ddf7fe95Scasper 			crklpd_rele(cr->cr_klpd);
3687c478bd9Sstevel@tonic-gate 		if (cr->cr_zone)
3697c478bd9Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
370f48205beScasper 		if (cr->cr_ksid)
371f48205beScasper 			kcrsid_rele(cr->cr_ksid);
37267dbe2beSCasper H.S. Dik 		if (cr->cr_grps)
37367dbe2beSCasper H.S. Dik 			crgrprele(cr->cr_grps);
37467dbe2beSCasper H.S. Dik 
3757c478bd9Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3817c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3827c478bd9Sstevel@tonic-gate  * 	and one for the thread.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate cred_t *
3857c478bd9Sstevel@tonic-gate crcopy(cred_t *cr)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	cred_t *newcr;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	newcr = cralloc();
3907c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3917c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3927c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
39345916cd2Sjpk 	if (newcr->cr_label)
394ddf7fe95Scasper 		label_hold(newcr->cr_label);
395f48205beScasper 	if (newcr->cr_ksid)
396ddf7fe95Scasper 		kcrsid_hold(newcr->cr_ksid);
397ddf7fe95Scasper 	if (newcr->cr_klpd)
398ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
39967dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
40067dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
4017c478bd9Sstevel@tonic-gate 	crfree(cr);
4027c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
4037c478bd9Sstevel@tonic-gate 	return (newcr);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
4087c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
4097c478bd9Sstevel@tonic-gate  * 	and one for the thread.
4107c478bd9Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
4117c478bd9Sstevel@tonic-gate  * "new" cred.
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@tonic-gate void
4147c478bd9Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
4157c478bd9Sstevel@tonic-gate {
416f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
417f48205beScasper 
4187c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4197c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4207c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
42145916cd2Sjpk 	if (newcr->cr_label)
42245916cd2Sjpk 		label_hold(newcr->cr_label);
423ddf7fe95Scasper 	if (newcr->cr_klpd)
424ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
42567dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
42667dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
427f48205beScasper 	if (nkcr) {
428f48205beScasper 		newcr->cr_ksid = nkcr;
429f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
430f48205beScasper 	} else if (newcr->cr_ksid)
431f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4327c478bd9Sstevel@tonic-gate 	crfree(oldcr);
4337c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4387c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4397c478bd9Sstevel@tonic-gate  */
440ddf7fe95Scasper static cred_t *
4415f9878b0Sken Powell - Sun Microsystem crdup_flags(const cred_t *cr, int flgs)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	cred_t *newcr;
4447c478bd9Sstevel@tonic-gate 
445ddf7fe95Scasper 	newcr = cralloc_flags(flgs);
446ddf7fe95Scasper 
447ddf7fe95Scasper 	if (newcr == NULL)
448ddf7fe95Scasper 		return (NULL);
449ddf7fe95Scasper 
4507c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
4517c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4527c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
45345916cd2Sjpk 	if (newcr->cr_label)
45445916cd2Sjpk 		label_hold(newcr->cr_label);
455ddf7fe95Scasper 	if (newcr->cr_klpd)
456ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
457f48205beScasper 	if (newcr->cr_ksid)
458f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
45967dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
46067dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
4617c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4627c478bd9Sstevel@tonic-gate 	return (newcr);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
465ddf7fe95Scasper cred_t *
466ddf7fe95Scasper crdup(cred_t *cr)
467ddf7fe95Scasper {
468ddf7fe95Scasper 	return (crdup_flags(cr, KM_SLEEP));
469ddf7fe95Scasper }
470ddf7fe95Scasper 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4737c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4747c478bd9Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
4757c478bd9Sstevel@tonic-gate  * "new" cred.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate void
4787c478bd9Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
4797c478bd9Sstevel@tonic-gate {
480f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
481f48205beScasper 
4827c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4837c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4847c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
48545916cd2Sjpk 	if (newcr->cr_label)
48645916cd2Sjpk 		label_hold(newcr->cr_label);
487ddf7fe95Scasper 	if (newcr->cr_klpd)
488ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
48967dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
49067dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
491f48205beScasper 	if (nkcr) {
492f48205beScasper 		newcr->cr_ksid = nkcr;
493f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
494f48205beScasper 	} else if (newcr->cr_ksid)
495f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4967c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate cred_t *
50345916cd2Sjpk crgetcred(void)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	cred_t *cr;
5067c478bd9Sstevel@tonic-gate 	proc_t *p;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
5097c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
5107c478bd9Sstevel@tonic-gate 	crhold(cr = p->p_cred);
5117c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
5127c478bd9Sstevel@tonic-gate 	return (cr);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate  * Backward compatibility check for suser().
5177c478bd9Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
5187c478bd9Sstevel@tonic-gate  * done through use of privilege in the audit trail.
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate int
5217c478bd9Sstevel@tonic-gate suser(cred_t *cr)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
5247c478bd9Sstevel@tonic-gate 	    == 0);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
5297c478bd9Sstevel@tonic-gate  * described by the supplied credentials.
5307c478bd9Sstevel@tonic-gate  */
5317c478bd9Sstevel@tonic-gate int
5327c478bd9Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	if (gid == cr->cr_gid)
5357c478bd9Sstevel@tonic-gate 		return (1);
5367c478bd9Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate int
5437c478bd9Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
5447c478bd9Sstevel@tonic-gate {
54567dbe2beSCasper H.S. Dik 	int hi, lo;
54667dbe2beSCasper H.S. Dik 	credgrp_t *grps = cr->cr_grps;
5477c478bd9Sstevel@tonic-gate 	const gid_t *gp, *endgp;
5487c478bd9Sstevel@tonic-gate 
54967dbe2beSCasper H.S. Dik 	if (grps == NULL)
55067dbe2beSCasper H.S. Dik 		return (0);
55167dbe2beSCasper H.S. Dik 
55267dbe2beSCasper H.S. Dik 	/* For a small number of groups, use sequentials search. */
55367dbe2beSCasper H.S. Dik 	if (grps->crg_ngroups <= BIN_GROUP_SEARCH_CUTOFF) {
55467dbe2beSCasper H.S. Dik 		endgp = &grps->crg_groups[grps->crg_ngroups];
55567dbe2beSCasper H.S. Dik 		for (gp = grps->crg_groups; gp < endgp; gp++)
5567c478bd9Sstevel@tonic-gate 			if (*gp == gid)
5577c478bd9Sstevel@tonic-gate 				return (1);
5587c478bd9Sstevel@tonic-gate 		return (0);
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
56167dbe2beSCasper H.S. Dik 	/* We use binary search when we have many groups. */
56267dbe2beSCasper H.S. Dik 	lo = 0;
56367dbe2beSCasper H.S. Dik 	hi = grps->crg_ngroups - 1;
56467dbe2beSCasper H.S. Dik 	gp = grps->crg_groups;
56567dbe2beSCasper H.S. Dik 
56667dbe2beSCasper H.S. Dik 	do {
56767dbe2beSCasper H.S. Dik 		int m = (lo + hi) / 2;
56867dbe2beSCasper H.S. Dik 
56967dbe2beSCasper H.S. Dik 		if (gid > gp[m])
57067dbe2beSCasper H.S. Dik 			lo = m + 1;
57167dbe2beSCasper H.S. Dik 		else if (gid < gp[m])
57267dbe2beSCasper H.S. Dik 			hi = m - 1;
57367dbe2beSCasper H.S. Dik 		else
57467dbe2beSCasper H.S. Dik 			return (1);
57567dbe2beSCasper H.S. Dik 	} while (lo <= hi);
57667dbe2beSCasper H.S. Dik 
57767dbe2beSCasper H.S. Dik 	return (0);
57867dbe2beSCasper H.S. Dik }
57967dbe2beSCasper H.S. Dik 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * This function is called to check whether the credentials set
5827c478bd9Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
5837c478bd9Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
5847c478bd9Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * The rules are:
5877c478bd9Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
5887c478bd9Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
5897c478bd9Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
5907c478bd9Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
5917c478bd9Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
5927c478bd9Sstevel@tonic-gate  *     succeeds
5937c478bd9Sstevel@tonic-gate  * (4) otherwise, the check fails
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate int
5967c478bd9Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	if (scrp == tcrp)
5997c478bd9Sstevel@tonic-gate 		return (1);
6007c478bd9Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
6017c478bd9Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
6027c478bd9Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
6037c478bd9Sstevel@tonic-gate 		return (0);
6047c478bd9Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
6057c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
6067c478bd9Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
6077c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
6087c478bd9Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
6097c478bd9Sstevel@tonic-gate 		return (1);
6107c478bd9Sstevel@tonic-gate 	return (0);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
6157c478bd9Sstevel@tonic-gate  * additionally returns success if the proc_t's match
6167c478bd9Sstevel@tonic-gate  * It is the preferred interface for most uses.
617ddf7fe95Scasper  * And it will acquire p_crlock itself, so it assert's that it shouldn't
6187c478bd9Sstevel@tonic-gate  * be held.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate int
6217c478bd9Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	int rets;
6247c478bd9Sstevel@tonic-gate 	cred_t *tcrp;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if (tp == sp)
6297c478bd9Sstevel@tonic-gate 		return (1);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
6327c478bd9Sstevel@tonic-gate 		return (0);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
635ddf7fe95Scasper 	crhold(tcrp = tp->p_cred);
6367c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
637ddf7fe95Scasper 	rets = hasprocperm(tcrp, scrp);
638ddf7fe95Scasper 	crfree(tcrp);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	return (rets);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
6457c478bd9Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
6467c478bd9Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
6477c478bd9Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
6487c478bd9Sstevel@tonic-gate  *
6497c478bd9Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
6507c478bd9Sstevel@tonic-gate  * 1 if they do not.
6517c478bd9Sstevel@tonic-gate  */
6527c478bd9Sstevel@tonic-gate int
6537c478bd9Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
6547c478bd9Sstevel@tonic-gate {
65567dbe2beSCasper H.S. Dik 	credgrp_t *grp1, *grp2;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (cr1 == cr2)
6587c478bd9Sstevel@tonic-gate 		return (0);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
6617c478bd9Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
6627c478bd9Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
6637c478bd9Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
6647c478bd9Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
66567dbe2beSCasper H.S. Dik 	    ((grp1 = cr1->cr_grps) == (grp2 = cr2->cr_grps) ||
66667dbe2beSCasper H.S. Dik 	    (grp1 != NULL && grp2 != NULL &&
66767dbe2beSCasper H.S. Dik 	    grp1->crg_ngroups == grp2->crg_ngroups &&
66867dbe2beSCasper H.S. Dik 	    bcmp(grp1->crg_groups, grp2->crg_groups,
66967dbe2beSCasper H.S. Dik 	    grp1->crg_ngroups * sizeof (gid_t)) == 0))) {
6707c478bd9Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	return (1);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate  * Read access functions to cred_t.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate uid_t
6797c478bd9Sstevel@tonic-gate crgetuid(const cred_t *cr)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	return (cr->cr_uid);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate uid_t
6857c478bd9Sstevel@tonic-gate crgetruid(const cred_t *cr)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	return (cr->cr_ruid);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate uid_t
6917c478bd9Sstevel@tonic-gate crgetsuid(const cred_t *cr)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	return (cr->cr_suid);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate gid_t
6977c478bd9Sstevel@tonic-gate crgetgid(const cred_t *cr)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	return (cr->cr_gid);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate gid_t
7037c478bd9Sstevel@tonic-gate crgetrgid(const cred_t *cr)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	return (cr->cr_rgid);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate gid_t
7097c478bd9Sstevel@tonic-gate crgetsgid(const cred_t *cr)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	return (cr->cr_sgid);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate const auditinfo_addr_t *
7157c478bd9Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate auditinfo_addr_t *
7217c478bd9Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	return (CR_AUINFO(cr));
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate zoneid_t
7277c478bd9Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
7287c478bd9Sstevel@tonic-gate {
72945916cd2Sjpk 	return (cr->cr_zone == NULL ?
73045916cd2Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
73145916cd2Sjpk 	    cr->cr_zone->zone_id);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate projid_t
7357c478bd9Sstevel@tonic-gate crgetprojid(const cred_t *cr)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	return (cr->cr_projid);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
74045916cd2Sjpk zone_t *
74145916cd2Sjpk crgetzone(const cred_t *cr)
74245916cd2Sjpk {
74345916cd2Sjpk 	return (cr->cr_zone);
74445916cd2Sjpk }
74545916cd2Sjpk 
74645916cd2Sjpk struct ts_label_s *
74745916cd2Sjpk crgetlabel(const cred_t *cr)
74845916cd2Sjpk {
74945916cd2Sjpk 	return (cr->cr_label ?
75045916cd2Sjpk 	    cr->cr_label :
75145916cd2Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
75245916cd2Sjpk }
75345916cd2Sjpk 
75445916cd2Sjpk boolean_t
75545916cd2Sjpk crisremote(const cred_t *cr)
75645916cd2Sjpk {
75745916cd2Sjpk 	return (REMOTE_PEER_CRED(cr));
75845916cd2Sjpk }
75945916cd2Sjpk 
760bda89588Sjp151216 #define	BADUID(x, zn)	((x) != -1 && !VALID_UID((x), (zn)))
761bda89588Sjp151216 #define	BADGID(x, zn)	((x) != -1 && !VALID_GID((x), (zn)))
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate int
7647c478bd9Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
7657c478bd9Sstevel@tonic-gate {
766bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
767bda89588Sjp151216 
7687c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7697c478bd9Sstevel@tonic-gate 
770bda89588Sjp151216 	if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone))
7717c478bd9Sstevel@tonic-gate 		return (-1);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (r != -1)
7747c478bd9Sstevel@tonic-gate 		cr->cr_ruid = r;
7757c478bd9Sstevel@tonic-gate 	if (e != -1)
7767c478bd9Sstevel@tonic-gate 		cr->cr_uid = e;
7777c478bd9Sstevel@tonic-gate 	if (s != -1)
7787c478bd9Sstevel@tonic-gate 		cr->cr_suid = s;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate int
7847c478bd9Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
7857c478bd9Sstevel@tonic-gate {
786bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
787bda89588Sjp151216 
7887c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7897c478bd9Sstevel@tonic-gate 
790bda89588Sjp151216 	if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone))
7917c478bd9Sstevel@tonic-gate 		return (-1);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (r != -1)
7947c478bd9Sstevel@tonic-gate 		cr->cr_rgid = r;
7957c478bd9Sstevel@tonic-gate 	if (e != -1)
7967c478bd9Sstevel@tonic-gate 		cr->cr_gid = e;
7977c478bd9Sstevel@tonic-gate 	if (s != -1)
7987c478bd9Sstevel@tonic-gate 		cr->cr_sgid = s;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	return (0);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate int
8047c478bd9Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
8057c478bd9Sstevel@tonic-gate {
806bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
807bda89588Sjp151216 
8087c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
8097c478bd9Sstevel@tonic-gate 
810bda89588Sjp151216 	if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone))
8117c478bd9Sstevel@tonic-gate 		return (-1);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
8147c478bd9Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	return (0);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
81967dbe2beSCasper H.S. Dik static int
82067dbe2beSCasper H.S. Dik gidcmp(const void *v1, const void *v2)
82167dbe2beSCasper H.S. Dik {
82267dbe2beSCasper H.S. Dik 	gid_t g1 = *(gid_t *)v1;
82367dbe2beSCasper H.S. Dik 	gid_t g2 = *(gid_t *)v2;
82467dbe2beSCasper H.S. Dik 
82567dbe2beSCasper H.S. Dik 	if (g1 < g2)
82667dbe2beSCasper H.S. Dik 		return (-1);
82767dbe2beSCasper H.S. Dik 	else if (g1 > g2)
82867dbe2beSCasper H.S. Dik 		return (1);
82967dbe2beSCasper H.S. Dik 	else
83067dbe2beSCasper H.S. Dik 		return (0);
83167dbe2beSCasper H.S. Dik }
83267dbe2beSCasper H.S. Dik 
8337c478bd9Sstevel@tonic-gate int
8347c478bd9Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
8397c478bd9Sstevel@tonic-gate 		return (-1);
8407c478bd9Sstevel@tonic-gate 
84167dbe2beSCasper H.S. Dik 	if (cr->cr_grps != NULL)
84267dbe2beSCasper H.S. Dik 		crgrprele(cr->cr_grps);
8437c478bd9Sstevel@tonic-gate 
84467dbe2beSCasper H.S. Dik 	if (n > 0) {
84567dbe2beSCasper H.S. Dik 		cr->cr_grps = kmem_alloc(CREDGRPSZ(n), KM_SLEEP);
84667dbe2beSCasper H.S. Dik 		bcopy(grp, cr->cr_grps->crg_groups, n * sizeof (gid_t));
84767dbe2beSCasper H.S. Dik 		cr->cr_grps->crg_ref = 1;
84867dbe2beSCasper H.S. Dik 		cr->cr_grps->crg_ngroups = n;
84967dbe2beSCasper H.S. Dik 		qsort(cr->cr_grps->crg_groups, n, sizeof (gid_t), gidcmp);
85067dbe2beSCasper H.S. Dik 	} else {
85167dbe2beSCasper H.S. Dik 		cr->cr_grps = NULL;
85267dbe2beSCasper H.S. Dik 	}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	return (0);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate void
8587c478bd9Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
8617c478bd9Sstevel@tonic-gate 	cr->cr_projid = projid;
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /*
86567dbe2beSCasper H.S. Dik  * This routine returns the pointer to the first element of the crg_groups
8667c478bd9Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
86767dbe2beSCasper H.S. Dik  * Note that when we have no grouplist, we return one element but the
86867dbe2beSCasper H.S. Dik  * caller should never reference it.
8697c478bd9Sstevel@tonic-gate  */
8707c478bd9Sstevel@tonic-gate const gid_t *
8717c478bd9Sstevel@tonic-gate crgetgroups(const cred_t *cr)
8727c478bd9Sstevel@tonic-gate {
87367dbe2beSCasper H.S. Dik 	return (cr->cr_grps == NULL ? &cr->cr_gid : cr->cr_grps->crg_groups);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate int
8777c478bd9Sstevel@tonic-gate crgetngroups(const cred_t *cr)
8787c478bd9Sstevel@tonic-gate {
87967dbe2beSCasper H.S. Dik 	return (cr->cr_grps == NULL ? 0 : cr->cr_grps->crg_ngroups);
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate void
8837c478bd9Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
8867c478bd9Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
8877c478bd9Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
8887c478bd9Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
8897c478bd9Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
8907c478bd9Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
8917c478bd9Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0; /* in case ngroups == 0 */
89267dbe2beSCasper H.S. Dik 	pcrp->pr_ngroups = cr->cr_grps == NULL ? 0 : cr->cr_grps->crg_ngroups;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
89567dbe2beSCasper H.S. Dik 		bcopy(cr->cr_grps->crg_groups, pcrp->pr_groups,
89667dbe2beSCasper H.S. Dik 		    sizeof (gid_t) * pcrp->pr_ngroups);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate static int
90045916cd2Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
9017c478bd9Sstevel@tonic-gate {
9027c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
9037c478bd9Sstevel@tonic-gate 	au_tid_addr_t	tid;
9047c478bd9Sstevel@tonic-gate 
905*134a1f4eSCasper H.S. Dik 	if (secpolicy_audit_getattr(rcr, B_TRUE) != 0)
9067c478bd9Sstevel@tonic-gate 		return (-1);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
9097c478bd9Sstevel@tonic-gate 	tid = ai->ai_termid;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
9127c478bd9Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
9137c478bd9Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
9167c478bd9Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
9197c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	return (0);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
92445916cd2Sjpk void
92545916cd2Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
92645916cd2Sjpk {
92745916cd2Sjpk 	ts_label_t	*tslp;
92845916cd2Sjpk 
92945916cd2Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
93045916cd2Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
93145916cd2Sjpk }
93245916cd2Sjpk 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
9357c478bd9Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
9367c478bd9Sstevel@tonic-gate  * memory and copy it twice.
93745916cd2Sjpk  *
93845916cd2Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
93945916cd2Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
94045916cd2Sjpk  * to determine whether audit info should be included.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate struct ucred_s *
94345916cd2Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
94667dbe2beSCasper H.S. Dik 	uint32_t realsz = ucredminsize(cr);
94767dbe2beSCasper H.S. Dik 	ts_label_t *tslp = is_system_labeled() ? crgetlabel(cr) : NULL;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
9507c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
95167dbe2beSCasper H.S. Dik 		uc = kmem_zalloc(realsz, KM_SLEEP);
9527c478bd9Sstevel@tonic-gate 	} else {
95367dbe2beSCasper H.S. Dik 		bzero(buf, realsz);
9547c478bd9Sstevel@tonic-gate 		uc = buf;
9557c478bd9Sstevel@tonic-gate 	}
95667dbe2beSCasper H.S. Dik 	uc->uc_size = realsz;
9577c478bd9Sstevel@tonic-gate 	uc->uc_pid = pid;
9587c478bd9Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
9597c478bd9Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
9607c478bd9Sstevel@tonic-gate 
96145916cd2Sjpk 	if (REMOTE_PEER_CRED(cr)) {
96245916cd2Sjpk 		/*
96367dbe2beSCasper H.S. Dik 		 * Other than label, the rest of cred info about a
96467dbe2beSCasper H.S. Dik 		 * remote peer isn't available. Copy the label directly
96567dbe2beSCasper H.S. Dik 		 * after the header where we generally copy the prcred.
96667dbe2beSCasper H.S. Dik 		 * That's why we use sizeof (struct ucred_s).  The other
96767dbe2beSCasper H.S. Dik 		 * offset fields are initialized to 0.
96845916cd2Sjpk 		 */
96967dbe2beSCasper H.S. Dik 		uc->uc_labeloff = tslp == NULL ? 0 : sizeof (struct ucred_s);
97045916cd2Sjpk 	} else {
97167dbe2beSCasper H.S. Dik 		uc->uc_credoff = UCRED_CRED_OFF;
97267dbe2beSCasper H.S. Dik 		uc->uc_privoff = UCRED_PRIV_OFF;
97367dbe2beSCasper H.S. Dik 		uc->uc_audoff = UCRED_AUD_OFF;
97467dbe2beSCasper H.S. Dik 		uc->uc_labeloff = tslp == NULL ? 0 : UCRED_LABEL_OFF;
97567dbe2beSCasper H.S. Dik 
9767c478bd9Sstevel@tonic-gate 		cred2prcred(cr, UCCRED(uc));
9777c478bd9Sstevel@tonic-gate 		cred2prpriv(cr, UCPRIV(uc));
97867dbe2beSCasper H.S. Dik 
97945916cd2Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
9807c478bd9Sstevel@tonic-gate 			uc->uc_audoff = 0;
98145916cd2Sjpk 	}
98267dbe2beSCasper H.S. Dik 	if (tslp != NULL)
98367dbe2beSCasper H.S. Dik 		bcopy(&tslp->tsl_label, UCLABEL(uc), sizeof (bslabel_t));
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	return (uc);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
98967dbe2beSCasper H.S. Dik  * Don't allocate the non-needed group entries.  Note: this function
99067dbe2beSCasper H.S. Dik  * must match the code in cred2ucred; they must agree about the
99167dbe2beSCasper H.S. Dik  * minimal size of the ucred.
99267dbe2beSCasper H.S. Dik  */
99367dbe2beSCasper H.S. Dik uint32_t
99467dbe2beSCasper H.S. Dik ucredminsize(const cred_t *cr)
99567dbe2beSCasper H.S. Dik {
99667dbe2beSCasper H.S. Dik 	int ndiff;
99767dbe2beSCasper H.S. Dik 
99867dbe2beSCasper H.S. Dik 	if (cr == NULL)
99967dbe2beSCasper H.S. Dik 		return (ucredsize);
100067dbe2beSCasper H.S. Dik 
100167dbe2beSCasper H.S. Dik 	if (REMOTE_PEER_CRED(cr)) {
100267dbe2beSCasper H.S. Dik 		if (is_system_labeled())
100367dbe2beSCasper H.S. Dik 			return (sizeof (struct ucred_s) + sizeof (bslabel_t));
100467dbe2beSCasper H.S. Dik 		else
100567dbe2beSCasper H.S. Dik 			return (sizeof (struct ucred_s));
100667dbe2beSCasper H.S. Dik 	}
100767dbe2beSCasper H.S. Dik 
100867dbe2beSCasper H.S. Dik 	if (cr->cr_grps == NULL)
100967dbe2beSCasper H.S. Dik 		ndiff = ngroups_max - 1;	/* Needs one for prcred_t */
101067dbe2beSCasper H.S. Dik 	else
101167dbe2beSCasper H.S. Dik 		ndiff = ngroups_max - cr->cr_grps->crg_ngroups;
101267dbe2beSCasper H.S. Dik 
101367dbe2beSCasper H.S. Dik 	return (ucredsize - ndiff * sizeof (gid_t));
101467dbe2beSCasper H.S. Dik }
101567dbe2beSCasper H.S. Dik 
101667dbe2beSCasper H.S. Dik /*
10177c478bd9Sstevel@tonic-gate  * Get the "ucred" of a process.
10187c478bd9Sstevel@tonic-gate  */
10197c478bd9Sstevel@tonic-gate struct ucred_s *
10207c478bd9Sstevel@tonic-gate pgetucred(proc_t *p)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	cred_t *cr;
10237c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
10267c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
10277c478bd9Sstevel@tonic-gate 	crhold(cr);
10287c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
10297c478bd9Sstevel@tonic-gate 
103045916cd2Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
10317c478bd9Sstevel@tonic-gate 	crfree(cr);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	return (uc);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
10387c478bd9Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
10397c478bd9Sstevel@tonic-gate  * make that the uid instead and retry the call.
10407c478bd9Sstevel@tonic-gate  * Private interface for NFS.
10417c478bd9Sstevel@tonic-gate  */
10427c478bd9Sstevel@tonic-gate cred_t *
10437c478bd9Sstevel@tonic-gate crnetadjust(cred_t *cr)
10447c478bd9Sstevel@tonic-gate {
10457c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
10467c478bd9Sstevel@tonic-gate 		cr = crdup(cr);
10477c478bd9Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
10487c478bd9Sstevel@tonic-gate 		return (cr);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	return (NULL);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate /*
10547c478bd9Sstevel@tonic-gate  * The reference count is of interest when you want to check
10557c478bd9Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
10567c478bd9Sstevel@tonic-gate  */
10577c478bd9Sstevel@tonic-gate uint_t
10587c478bd9Sstevel@tonic-gate crgetref(const cred_t *cr)
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate 	return (cr->cr_ref);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate static int
10647c478bd9Sstevel@tonic-gate get_c2audit_load(void)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 	static int	gotit = 0;
10677c478bd9Sstevel@tonic-gate 	static int	c2audit_load;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (gotit)
10707c478bd9Sstevel@tonic-gate 		return (c2audit_load);
1071005d3febSMarek Pospisil 	c2audit_load = 1;		/* set default value once */
1072005d3febSMarek Pospisil 	if (mod_sysctl(SYS_CHECK_EXCLUDE, "c2audit") != 0)
1073005d3febSMarek Pospisil 		c2audit_load = 0;
10747c478bd9Sstevel@tonic-gate 	gotit++;
1075005d3febSMarek Pospisil 
10767c478bd9Sstevel@tonic-gate 	return (c2audit_load);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate int
10807c478bd9Sstevel@tonic-gate get_audit_ucrsize(void)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate /*
10867c478bd9Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
10877c478bd9Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
10887c478bd9Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
10897c478bd9Sstevel@tonic-gate  * same.
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate void
10927c478bd9Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	ASSERT(cr != kcred);
10977c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
10987c478bd9Sstevel@tonic-gate 	cr->cr_zone = zptr;
10997c478bd9Sstevel@tonic-gate 	zone_cred_hold(zptr);
11007c478bd9Sstevel@tonic-gate 	if (oldzptr)
11017c478bd9Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
11027c478bd9Sstevel@tonic-gate }
110345916cd2Sjpk 
110445916cd2Sjpk /*
110545916cd2Sjpk  * Create a new cred based on the supplied label
110645916cd2Sjpk  */
110745916cd2Sjpk cred_t *
110845916cd2Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
110945916cd2Sjpk {
111045916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
111145916cd2Sjpk 	cred_t *cr = NULL;
111245916cd2Sjpk 
111345916cd2Sjpk 	if (lbl != NULL) {
1114ddf7fe95Scasper 		if ((cr = crdup_flags(dummycr, flags)) != NULL) {
111545916cd2Sjpk 			cr->cr_label = lbl;
111645916cd2Sjpk 		} else {
111745916cd2Sjpk 			label_rele(lbl);
111845916cd2Sjpk 		}
111945916cd2Sjpk 	}
112045916cd2Sjpk 
112145916cd2Sjpk 	return (cr);
112245916cd2Sjpk }
112345916cd2Sjpk 
112445916cd2Sjpk /*
112545916cd2Sjpk  * Derive a new cred from the existing cred, but with a different label.
112645916cd2Sjpk  * To be used when a cred is being shared, but the label needs to be changed
112745916cd2Sjpk  * by a caller without affecting other users
112845916cd2Sjpk  */
112945916cd2Sjpk cred_t *
11305f9878b0Sken Powell - Sun Microsystem copycred_from_tslabel(const cred_t *cr, ts_label_t *label, int flags)
11315f9878b0Sken Powell - Sun Microsystem {
11325f9878b0Sken Powell - Sun Microsystem 	cred_t *newcr = NULL;
11335f9878b0Sken Powell - Sun Microsystem 
11345f9878b0Sken Powell - Sun Microsystem 	if ((newcr = crdup_flags(cr, flags)) != NULL) {
11355f9878b0Sken Powell - Sun Microsystem 		if (newcr->cr_label != NULL)
11365f9878b0Sken Powell - Sun Microsystem 			label_rele(newcr->cr_label);
11375f9878b0Sken Powell - Sun Microsystem 		label_hold(label);
11385f9878b0Sken Powell - Sun Microsystem 		newcr->cr_label = label;
11395f9878b0Sken Powell - Sun Microsystem 	}
11405f9878b0Sken Powell - Sun Microsystem 
11415f9878b0Sken Powell - Sun Microsystem 	return (newcr);
11425f9878b0Sken Powell - Sun Microsystem }
11435f9878b0Sken Powell - Sun Microsystem 
11445f9878b0Sken Powell - Sun Microsystem /*
11455f9878b0Sken Powell - Sun Microsystem  * Derive a new cred from the existing cred, but with a different label.
11465f9878b0Sken Powell - Sun Microsystem  */
11475f9878b0Sken Powell - Sun Microsystem cred_t *
11485f9878b0Sken Powell - Sun Microsystem copycred_from_bslabel(const cred_t *cr, bslabel_t *blabel,
11495f9878b0Sken Powell - Sun Microsystem     uint32_t doi, int flags)
115045916cd2Sjpk {
115145916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
115245916cd2Sjpk 	cred_t  *newcr = NULL;
115345916cd2Sjpk 
115445916cd2Sjpk 	if (lbl != NULL) {
11555f9878b0Sken Powell - Sun Microsystem 		newcr = copycred_from_tslabel(cr, lbl, flags);
115645916cd2Sjpk 		label_rele(lbl);
115745916cd2Sjpk 	}
115845916cd2Sjpk 
115945916cd2Sjpk 	return (newcr);
116045916cd2Sjpk }
116145916cd2Sjpk 
116245916cd2Sjpk /*
116345916cd2Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
116445916cd2Sjpk  */
116545916cd2Sjpk cred_t *
116645916cd2Sjpk zone_kcred(void)
116745916cd2Sjpk {
116845916cd2Sjpk 	zone_t *zone;
116945916cd2Sjpk 
117045916cd2Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
117145916cd2Sjpk 		return (zone->zone_kcred);
117245916cd2Sjpk 	else
117345916cd2Sjpk 		return (kcred);
117445916cd2Sjpk }
1175f48205beScasper 
1176f48205beScasper boolean_t
1177bda89588Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id)
1178f48205beScasper {
1179bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
11804edd44c5Sjp151216 	if (id <= IDMAP_WK__MAX_UID)
1181bda89588Sjp151216 		return (B_TRUE);
1182bda89588Sjp151216 
1183bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1184bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1185f48205beScasper 	membar_consumer();
1186bda89588Sjp151216 	return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid);
1187f48205beScasper }
1188f48205beScasper 
1189f48205beScasper boolean_t
1190bda89588Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id)
1191f48205beScasper {
1192bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
11934edd44c5Sjp151216 	if (id <= IDMAP_WK__MAX_GID)
1194bda89588Sjp151216 		return (B_TRUE);
1195bda89588Sjp151216 
1196bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1197bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1198f48205beScasper 	membar_consumer();
1199bda89588Sjp151216 	return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid);
1200f48205beScasper }
1201f48205beScasper 
1202f48205beScasper int
1203bda89588Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count)
1204f48205beScasper {
1205bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1206bda89588Sjp151216 
1207bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1208bda89588Sjp151216 
1209bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1210f48205beScasper 
1211f48205beScasper 	/* Test for unsigned integer wrap around */
1212bda89588Sjp151216 	if (eph_zsd->last_uid + count < eph_zsd->last_uid) {
1213bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1214f48205beScasper 		return (-1);
1215f48205beScasper 	}
1216f48205beScasper 
1217f48205beScasper 	/* first call or idmap crashed and state corrupted */
1218f48205beScasper 	if (flags != 0)
1219bda89588Sjp151216 		eph_zsd->min_uid = eph_zsd->last_uid;
1220f48205beScasper 
1221f48205beScasper 	hasephids = B_TRUE;
1222bda89588Sjp151216 	*start = eph_zsd->last_uid + 1;
1223bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_uid, count);
1224bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1225f48205beScasper 	return (0);
1226f48205beScasper }
1227f48205beScasper 
1228f48205beScasper int
1229bda89588Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count)
1230f48205beScasper {
1231bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1232bda89588Sjp151216 
1233bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1234bda89588Sjp151216 
1235bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1236f48205beScasper 
1237f48205beScasper 	/* Test for unsigned integer wrap around */
1238bda89588Sjp151216 	if (eph_zsd->last_gid + count < eph_zsd->last_gid) {
1239bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1240f48205beScasper 		return (-1);
1241f48205beScasper 	}
1242f48205beScasper 
1243f48205beScasper 	/* first call or idmap crashed and state corrupted */
1244f48205beScasper 	if (flags != 0)
1245bda89588Sjp151216 		eph_zsd->min_gid = eph_zsd->last_gid;
1246f48205beScasper 
1247f48205beScasper 	hasephids = B_TRUE;
1248bda89588Sjp151216 	*start = eph_zsd->last_gid + 1;
1249bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_gid, count);
1250bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1251f48205beScasper 	return (0);
1252f48205beScasper }
1253f48205beScasper 
1254f48205beScasper /*
1255bda89588Sjp151216  * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data()
1256bda89588Sjp151216  * are project private functions that are for use of the test system only and
1257bda89588Sjp151216  * are not to be used for other purposes.
1258bda89588Sjp151216  */
1259bda89588Sjp151216 
1260bda89588Sjp151216 void
1261bda89588Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid,
1262bda89588Sjp151216 	gid_t *min_gid, gid_t *last_gid)
1263bda89588Sjp151216 {
1264bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1265bda89588Sjp151216 
1266bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1267bda89588Sjp151216 
1268bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1269bda89588Sjp151216 
1270bda89588Sjp151216 	*min_uid = eph_zsd->min_uid;
1271bda89588Sjp151216 	*last_uid = eph_zsd->last_uid;
1272bda89588Sjp151216 	*min_gid = eph_zsd->min_gid;
1273bda89588Sjp151216 	*last_gid = eph_zsd->last_gid;
1274bda89588Sjp151216 
1275bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1276bda89588Sjp151216 }
1277bda89588Sjp151216 
1278bda89588Sjp151216 
1279bda89588Sjp151216 void
1280bda89588Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid,
1281bda89588Sjp151216 	gid_t min_gid, gid_t last_gid)
1282bda89588Sjp151216 {
1283bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1284bda89588Sjp151216 
1285bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1286bda89588Sjp151216 
1287bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1288bda89588Sjp151216 
1289bda89588Sjp151216 	if (min_uid != 0)
1290bda89588Sjp151216 		eph_zsd->min_uid = min_uid;
1291bda89588Sjp151216 	if (last_uid != 0)
1292bda89588Sjp151216 		eph_zsd->last_uid = last_uid;
1293bda89588Sjp151216 	if (min_gid != 0)
1294bda89588Sjp151216 		eph_zsd->min_gid = min_gid;
1295bda89588Sjp151216 	if (last_gid != 0)
1296bda89588Sjp151216 		eph_zsd->last_gid = last_gid;
1297bda89588Sjp151216 
1298bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1299bda89588Sjp151216 }
1300bda89588Sjp151216 
1301bda89588Sjp151216 /*
1302da6c28aaSamw  * If the credential user SID or group SID is mapped to an ephemeral
1303da6c28aaSamw  * ID, map the credential to nobody.
1304f48205beScasper  */
1305f48205beScasper cred_t *
1306f48205beScasper crgetmapped(const cred_t *cr)
1307f48205beScasper {
1308bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
130990c3d472Scasper 	/*
131090c3d472Scasper 	 * Someone incorrectly passed a NULL cred to a vnode operation
131190c3d472Scasper 	 * either on purpose or by calling CRED() in interrupt context.
131290c3d472Scasper 	 */
131390c3d472Scasper 	if (cr == NULL)
131490c3d472Scasper 		return (NULL);
131590c3d472Scasper 
1316f48205beScasper 	if (cr->cr_ksid != NULL) {
1317bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) {
1318bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1319bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1320bda89588Sjp151216 		}
1321f48205beScasper 
1322bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) {
1323bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1324bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1325bda89588Sjp151216 		}
1326f48205beScasper 	}
1327f48205beScasper 
1328f48205beScasper 	return ((cred_t *)cr);
1329f48205beScasper }
1330f48205beScasper 
1331f48205beScasper /* index should be in range for a ksidindex_t */
1332f48205beScasper void
1333f48205beScasper crsetsid(cred_t *cr, ksid_t *ksp, int index)
1334f48205beScasper {
1335f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1336f48205beScasper 	ASSERT(index >= 0 && index < KSID_COUNT);
1337f48205beScasper 	if (cr->cr_ksid == NULL && ksp == NULL)
1338f48205beScasper 		return;
1339f48205beScasper 	cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index);
1340f48205beScasper }
1341f48205beScasper 
1342f48205beScasper void
1343f48205beScasper crsetsidlist(cred_t *cr, ksidlist_t *ksl)
1344f48205beScasper {
1345f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1346f48205beScasper 	if (cr->cr_ksid == NULL && ksl == NULL)
1347f48205beScasper 		return;
1348f48205beScasper 	cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl);
1349f48205beScasper }
1350f48205beScasper 
1351f48205beScasper ksid_t *
1352f48205beScasper crgetsid(const cred_t *cr, int i)
1353f48205beScasper {
1354f48205beScasper 	ASSERT(i >= 0 && i < KSID_COUNT);
1355f48205beScasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain)
1356f48205beScasper 		return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]);
1357f48205beScasper 	return (NULL);
1358f48205beScasper }
1359f48205beScasper 
1360f48205beScasper ksidlist_t *
1361f48205beScasper crgetsidlist(const cred_t *cr)
1362f48205beScasper {
1363da6c28aaSamw 	if (cr->cr_ksid != NULL)
136428a151a0Scasper 		return (cr->cr_ksid->kr_sidlist);
1365f48205beScasper 	return (NULL);
1366f48205beScasper }
1367da6c28aaSamw 
1368da6c28aaSamw /*
1369da6c28aaSamw  * Interface to set the effective and permitted privileges for
1370da6c28aaSamw  * a credential; this interface does no security checks and is
1371da6c28aaSamw  * intended for kernel (file)servers creating credentials with
1372da6c28aaSamw  * specific privileges.
1373da6c28aaSamw  */
1374da6c28aaSamw int
1375da6c28aaSamw crsetpriv(cred_t *cr, ...)
1376da6c28aaSamw {
1377da6c28aaSamw 	va_list ap;
1378da6c28aaSamw 	const char *privnm;
1379da6c28aaSamw 
1380da6c28aaSamw 	ASSERT(cr->cr_ref <= 2);
1381da6c28aaSamw 
1382da6c28aaSamw 	priv_set_PA(cr);
1383da6c28aaSamw 
1384da6c28aaSamw 	va_start(ap, cr);
1385da6c28aaSamw 
1386da6c28aaSamw 	while ((privnm = va_arg(ap, const char *)) != NULL) {
1387da6c28aaSamw 		int priv = priv_getbyname(privnm, 0);
1388da6c28aaSamw 		if (priv < 0)
1389da6c28aaSamw 			return (-1);
1390da6c28aaSamw 
1391da6c28aaSamw 		priv_addset(&CR_PPRIV(cr), priv);
1392da6c28aaSamw 		priv_addset(&CR_EPRIV(cr), priv);
1393da6c28aaSamw 	}
1394da6c28aaSamw 	priv_adjust_PA(cr);
1395da6c28aaSamw 	va_end(ap);
1396da6c28aaSamw 	return (0);
1397da6c28aaSamw }
1398ddf7fe95Scasper 
1399546a3997SThomas Haynes /*
1400546a3997SThomas Haynes  * Interface to effectively set the PRIV_ALL for
1401546a3997SThomas Haynes  * a credential; this interface does no security checks and is
1402546a3997SThomas Haynes  * intended for kernel (file)servers to extend the user credentials
1403546a3997SThomas Haynes  * to be ALL, like either kcred or zcred.
1404546a3997SThomas Haynes  */
1405546a3997SThomas Haynes void
1406546a3997SThomas Haynes crset_zone_privall(cred_t *cr)
1407546a3997SThomas Haynes {
1408546a3997SThomas Haynes 	zone_t	*zone = crgetzone(cr);
1409546a3997SThomas Haynes 
1410546a3997SThomas Haynes 	priv_fillset(&CR_LPRIV(cr));
1411546a3997SThomas Haynes 	CR_EPRIV(cr) = CR_PPRIV(cr) = CR_IPRIV(cr) = CR_LPRIV(cr);
1412546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_LPRIV(cr));
1413546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_EPRIV(cr));
1414546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_IPRIV(cr));
1415546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_PPRIV(cr));
1416546a3997SThomas Haynes }
1417546a3997SThomas Haynes 
1418ddf7fe95Scasper struct credklpd *
1419ddf7fe95Scasper crgetcrklpd(const cred_t *cr)
1420ddf7fe95Scasper {
1421ddf7fe95Scasper 	return (cr->cr_klpd);
1422ddf7fe95Scasper }
1423ddf7fe95Scasper 
1424ddf7fe95Scasper void
1425ddf7fe95Scasper crsetcrklpd(cred_t *cr, struct credklpd *crklpd)
1426ddf7fe95Scasper {
1427ddf7fe95Scasper 	ASSERT(cr->cr_ref <= 2);
1428ddf7fe95Scasper 
1429ddf7fe95Scasper 	if (cr->cr_klpd != NULL)
1430ddf7fe95Scasper 		crklpd_rele(cr->cr_klpd);
1431ddf7fe95Scasper 	cr->cr_klpd = crklpd;
1432ddf7fe95Scasper }
143367dbe2beSCasper H.S. Dik 
143467dbe2beSCasper H.S. Dik credgrp_t *
143567dbe2beSCasper H.S. Dik crgrpcopyin(int n, gid_t *gidset)
143667dbe2beSCasper H.S. Dik {
143767dbe2beSCasper H.S. Dik 	credgrp_t *mem;
143867dbe2beSCasper H.S. Dik 	size_t sz = CREDGRPSZ(n);
143967dbe2beSCasper H.S. Dik 
144067dbe2beSCasper H.S. Dik 	ASSERT(n > 0);
144167dbe2beSCasper H.S. Dik 
144267dbe2beSCasper H.S. Dik 	mem = kmem_alloc(sz, KM_SLEEP);
144367dbe2beSCasper H.S. Dik 
144467dbe2beSCasper H.S. Dik 	if (copyin(gidset, mem->crg_groups, sizeof (gid_t) * n)) {
144567dbe2beSCasper H.S. Dik 		kmem_free(mem, sz);
144667dbe2beSCasper H.S. Dik 		return (NULL);
144767dbe2beSCasper H.S. Dik 	}
144867dbe2beSCasper H.S. Dik 	mem->crg_ref = 1;
144967dbe2beSCasper H.S. Dik 	mem->crg_ngroups = n;
145067dbe2beSCasper H.S. Dik 	return (mem);
145167dbe2beSCasper H.S. Dik }
145267dbe2beSCasper H.S. Dik 
145367dbe2beSCasper H.S. Dik const gid_t *
145467dbe2beSCasper H.S. Dik crgetggroups(const credgrp_t *grps)
145567dbe2beSCasper H.S. Dik {
145667dbe2beSCasper H.S. Dik 	return (grps->crg_groups);
145767dbe2beSCasper H.S. Dik }
145867dbe2beSCasper H.S. Dik 
145967dbe2beSCasper H.S. Dik void
146067dbe2beSCasper H.S. Dik crsetcredgrp(cred_t *cr, credgrp_t *grps)
146167dbe2beSCasper H.S. Dik {
146267dbe2beSCasper H.S. Dik 	ASSERT(cr->cr_ref <= 2);
146367dbe2beSCasper H.S. Dik 
146467dbe2beSCasper H.S. Dik 	if (cr->cr_grps != NULL)
146567dbe2beSCasper H.S. Dik 		crgrprele(cr->cr_grps);
146667dbe2beSCasper H.S. Dik 
146767dbe2beSCasper H.S. Dik 	cr->cr_grps = grps;
146867dbe2beSCasper H.S. Dik }
146967dbe2beSCasper H.S. Dik 
147067dbe2beSCasper H.S. Dik void
147167dbe2beSCasper H.S. Dik crgrprele(credgrp_t *grps)
147267dbe2beSCasper H.S. Dik {
147367dbe2beSCasper H.S. Dik 	if (atomic_add_32_nv(&grps->crg_ref, -1) == 0)
147467dbe2beSCasper H.S. Dik 		kmem_free(grps, CREDGRPSZ(grps->crg_ngroups));
147567dbe2beSCasper H.S. Dik }
147667dbe2beSCasper H.S. Dik 
147767dbe2beSCasper H.S. Dik static void
147867dbe2beSCasper H.S. Dik crgrphold(credgrp_t *grps)
147967dbe2beSCasper H.S. Dik {
148067dbe2beSCasper H.S. Dik 	atomic_add_32(&grps->crg_ref, 1);
148167dbe2beSCasper H.S. Dik }
1482