xref: /titanic_51/usr/src/uts/common/os/cred.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
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 /*
22b77dfcc8SIra Cooper  * Copyright (c) 2013, Ira Cooper.  All rights reserved.
23b77dfcc8SIra Cooper  */
24b77dfcc8SIra Cooper /*
25134a1f4eSCasper H.S. Dik  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
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>
58f48205beScasper #include <sys/avl.h>
59da6c28aaSamw #include <sys/door.h>
607c478bd9Sstevel@tonic-gate #include <c2/audit.h>
617c478bd9Sstevel@tonic-gate #include <sys/zone.h>
6245916cd2Sjpk #include <sys/tsol/label.h>
63f48205beScasper #include <sys/sid.h>
64c5c4113dSnw141292 #include <sys/idmap.h>
65ddf7fe95Scasper #include <sys/klpd.h>
66da6c28aaSamw #include <sys/varargs.h>
67005d3febSMarek Pospisil #include <sys/sysconf.h>
6867dbe2beSCasper H.S. Dik #include <util/qsort.h>
69f48205beScasper 
70bda89588Sjp151216 
71bda89588Sjp151216 /* Ephemeral IDs Zones specific data */
72bda89588Sjp151216 typedef struct ephemeral_zsd {
73bda89588Sjp151216 	uid_t		min_uid;
74bda89588Sjp151216 	uid_t		last_uid;
75bda89588Sjp151216 	gid_t		min_gid;
76bda89588Sjp151216 	gid_t		last_gid;
77f48205beScasper 	kmutex_t	eph_lock;
78bda89588Sjp151216 	cred_t		*eph_nobody;
79bda89588Sjp151216 } ephemeral_zsd_t;
80bda89588Sjp151216 
8167dbe2beSCasper H.S. Dik static void crgrphold(credgrp_t *);
8267dbe2beSCasper H.S. Dik 
8367dbe2beSCasper H.S. Dik #define	CREDGRPSZ(ngrp)	(sizeof (credgrp_t) + ((ngrp - 1) * sizeof (gid_t)))
84bda89588Sjp151216 
85bda89588Sjp151216 static kmutex_t		ephemeral_zone_mutex;
86bda89588Sjp151216 static zone_key_t	ephemeral_zone_key;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static struct kmem_cache *cred_cache;
897c478bd9Sstevel@tonic-gate static size_t		crsize = 0;
907c478bd9Sstevel@tonic-gate static int		audoff = 0;
917c478bd9Sstevel@tonic-gate uint32_t		ucredsize;
927c478bd9Sstevel@tonic-gate cred_t			*kcred;
9345916cd2Sjpk static cred_t		*dummycr;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int get_c2audit_load(void);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
1007c478bd9Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
1017c478bd9Sstevel@tonic-gate 
10245916cd2Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
1037c478bd9Sstevel@tonic-gate 
10467dbe2beSCasper H.S. Dik #define	BIN_GROUP_SEARCH_CUTOFF	16
105f48205beScasper 
106f48205beScasper static boolean_t hasephids = B_FALSE;
107f48205beScasper 
108bda89588Sjp151216 static ephemeral_zsd_t *
109bda89588Sjp151216 get_ephemeral_zsd(zone_t *zone)
110bda89588Sjp151216 {
111bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
112bda89588Sjp151216 
113bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
114bda89588Sjp151216 	if (eph_zsd != NULL) {
115bda89588Sjp151216 		return (eph_zsd);
116bda89588Sjp151216 	}
117bda89588Sjp151216 
118bda89588Sjp151216 	mutex_enter(&ephemeral_zone_mutex);
119bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
120bda89588Sjp151216 	if (eph_zsd == NULL) {
121bda89588Sjp151216 		eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP);
122bda89588Sjp151216 		eph_zsd->min_uid = MAXUID;
123bda89588Sjp151216 		eph_zsd->last_uid = IDMAP_WK__MAX_UID;
124bda89588Sjp151216 		eph_zsd->min_gid = MAXUID;
125bda89588Sjp151216 		eph_zsd->last_gid = IDMAP_WK__MAX_GID;
126bda89588Sjp151216 		mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL);
127bda89588Sjp151216 
128bda89588Sjp151216 		/*
129bda89588Sjp151216 		 * nobody is used to map SID containing CRs.
130bda89588Sjp151216 		 */
131bda89588Sjp151216 		eph_zsd->eph_nobody = crdup(zone->zone_kcred);
132bda89588Sjp151216 		(void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY);
133bda89588Sjp151216 		CR_FLAGS(eph_zsd->eph_nobody) = 0;
134bda89588Sjp151216 		eph_zsd->eph_nobody->cr_zone = zone;
135bda89588Sjp151216 
136bda89588Sjp151216 		(void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd);
137bda89588Sjp151216 	}
138bda89588Sjp151216 	mutex_exit(&ephemeral_zone_mutex);
139bda89588Sjp151216 	return (eph_zsd);
140bda89588Sjp151216 }
141bda89588Sjp151216 
1425f9878b0Sken Powell - Sun Microsystem static cred_t *crdup_flags(const cred_t *, int);
143ddf7fe95Scasper static cred_t *cralloc_flags(int);
144ddf7fe95Scasper 
145bda89588Sjp151216 /*
146bda89588Sjp151216  * This function is called when a zone is destroyed
147bda89588Sjp151216  */
148bda89588Sjp151216 static void
149bda89588Sjp151216 /* ARGSUSED */
150bda89588Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg)
151bda89588Sjp151216 {
152bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = arg;
153bda89588Sjp151216 	if (eph_zsd != NULL) {
154bda89588Sjp151216 		mutex_destroy(&eph_zsd->eph_lock);
155bda89588Sjp151216 		crfree(eph_zsd->eph_nobody);
156bda89588Sjp151216 		kmem_free(eph_zsd, sizeof (ephemeral_zsd_t));
157bda89588Sjp151216 	}
158bda89588Sjp151216 }
159bda89588Sjp151216 
160bda89588Sjp151216 
161bda89588Sjp151216 
162f48205beScasper /*
1637c478bd9Sstevel@tonic-gate  * Initialize credentials data structures.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate void
1677c478bd9Sstevel@tonic-gate cred_init(void)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	priv_init();
1707c478bd9Sstevel@tonic-gate 
17167dbe2beSCasper H.S. Dik 	crsize = sizeof (cred_t);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
1747c478bd9Sstevel@tonic-gate #ifdef _LP64
1757c478bd9Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
1767c478bd9Sstevel@tonic-gate 		audoff = (crsize +
1777c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
1787c478bd9Sstevel@tonic-gate #else	/* _LP64 */
1797c478bd9Sstevel@tonic-gate 		audoff = crsize;
1807c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
1817c478bd9Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1827c478bd9Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1867c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
18945916cd2Sjpk 	 * dummycr is used to copy initial state for creds.
19045916cd2Sjpk 	 */
19145916cd2Sjpk 	dummycr = cralloc();
19245916cd2Sjpk 	bzero(dummycr, crsize);
19345916cd2Sjpk 	dummycr->cr_ref = 1;
194f48205beScasper 	dummycr->cr_uid = (uid_t)-1;
195f48205beScasper 	dummycr->cr_gid = (gid_t)-1;
196f48205beScasper 	dummycr->cr_ruid = (uid_t)-1;
197f48205beScasper 	dummycr->cr_rgid = (gid_t)-1;
198f48205beScasper 	dummycr->cr_suid = (uid_t)-1;
199f48205beScasper 	dummycr->cr_sgid = (gid_t)-1;
200f48205beScasper 
20145916cd2Sjpk 
20245916cd2Sjpk 	/*
2037c478bd9Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
2047c478bd9Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
2057c478bd9Sstevel@tonic-gate 	 * sets filled in.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	kcred = cralloc();
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	bzero(kcred, crsize);
2107c478bd9Sstevel@tonic-gate 	kcred->cr_ref = 1;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
2137c478bd9Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
2167c478bd9Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
2197c478bd9Sstevel@tonic-gate 	if (!rstchown)
2207c478bd9Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
2237c478bd9Sstevel@tonic-gate 	if (rstlink)
2247c478bd9Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
22745916cd2Sjpk 
22845916cd2Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * Set up credentials of p0.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
2347c478bd9Sstevel@tonic-gate 	curthread->t_cred = kcred;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	ucredsize = UCRED_SIZE;
237bda89588Sjp151216 
238bda89588Sjp151216 	mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL);
239bda89588Sjp151216 	zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
2447c478bd9Sstevel@tonic-gate  */
245ddf7fe95Scasper static cred_t *
246ddf7fe95Scasper cralloc_flags(int flgs)
2477c478bd9Sstevel@tonic-gate {
248ddf7fe95Scasper 	cred_t *cr = kmem_cache_alloc(cred_cache, flgs);
249ddf7fe95Scasper 
250ddf7fe95Scasper 	if (cr == NULL)
251ddf7fe95Scasper 		return (NULL);
252ddf7fe95Scasper 
2537c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
2547c478bd9Sstevel@tonic-gate 	cr->cr_zone = NULL;
25545916cd2Sjpk 	cr->cr_label = NULL;
256f48205beScasper 	cr->cr_ksid = NULL;
257ddf7fe95Scasper 	cr->cr_klpd = NULL;
25867dbe2beSCasper H.S. Dik 	cr->cr_grps = NULL;
259f48205beScasper 	return (cr);
260f48205beScasper }
261f48205beScasper 
262ddf7fe95Scasper cred_t *
263ddf7fe95Scasper cralloc(void)
264ddf7fe95Scasper {
265ddf7fe95Scasper 	return (cralloc_flags(KM_SLEEP));
266ddf7fe95Scasper }
267ddf7fe95Scasper 
268f48205beScasper /*
269f48205beScasper  * As cralloc but prepared for ksid change (if appropriate).
270f48205beScasper  */
271f48205beScasper cred_t *
272f48205beScasper cralloc_ksid(void)
273f48205beScasper {
274f48205beScasper 	cred_t *cr = cralloc();
275f48205beScasper 	if (hasephids)
276f48205beScasper 		cr->cr_ksid = kcrsid_alloc();
2777c478bd9Sstevel@tonic-gate 	return (cr);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
2827c478bd9Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate cred_t *
2857c478bd9Sstevel@tonic-gate crget(void)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
2907c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;
2917c478bd9Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
29245916cd2Sjpk 	if (cr->cr_label)
29345916cd2Sjpk 		label_hold(cr->cr_label);
294ddf7fe95Scasper 	ASSERT(cr->cr_klpd == NULL);
29567dbe2beSCasper H.S. Dik 	ASSERT(cr->cr_grps == NULL);
2967c478bd9Sstevel@tonic-gate 	return (cr);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
3017c478bd9Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
3027c478bd9Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
3037c478bd9Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
3047c478bd9Sstevel@tonic-gate  *
3057c478bd9Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
3067c478bd9Sstevel@tonic-gate  * and p->p_cred set.
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate void
3117c478bd9Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
3147c478bd9Sstevel@tonic-gate 	kthread_id_t	first;
3157c478bd9Sstevel@tonic-gate 	cred_t *oldcr;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
3217c478bd9Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	t = curthread;
3247c478bd9Sstevel@tonic-gate 	oldcr = t->t_cred;
3257c478bd9Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
3267c478bd9Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
3327c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
3337c478bd9Sstevel@tonic-gate 		first = curthread;
3347c478bd9Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
3357c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
3367c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * Put a hold on a cred structure.
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate void
3447c478bd9Sstevel@tonic-gate crhold(cred_t *cr)
3457c478bd9Sstevel@tonic-gate {
346134a1f4eSCasper H.S. Dik 	ASSERT(cr->cr_ref != 0xdeadbeef && cr->cr_ref != 0);
347*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&cr->cr_ref);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
35245916cd2Sjpk  * If cred uses label different from zone label, free it.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate void
3557c478bd9Sstevel@tonic-gate crfree(cred_t *cr)
3567c478bd9Sstevel@tonic-gate {
357134a1f4eSCasper H.S. Dik 	ASSERT(cr->cr_ref != 0xdeadbeef && cr->cr_ref != 0);
358*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&cr->cr_ref) == 0) {
3597c478bd9Sstevel@tonic-gate 		ASSERT(cr != kcred);
36045916cd2Sjpk 		if (cr->cr_label)
36145916cd2Sjpk 			label_rele(cr->cr_label);
362ddf7fe95Scasper 		if (cr->cr_klpd)
363ddf7fe95Scasper 			crklpd_rele(cr->cr_klpd);
3647c478bd9Sstevel@tonic-gate 		if (cr->cr_zone)
3657c478bd9Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
366f48205beScasper 		if (cr->cr_ksid)
367f48205beScasper 			kcrsid_rele(cr->cr_ksid);
36867dbe2beSCasper H.S. Dik 		if (cr->cr_grps)
36967dbe2beSCasper H.S. Dik 			crgrprele(cr->cr_grps);
37067dbe2beSCasper H.S. Dik 
3717c478bd9Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3777c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3787c478bd9Sstevel@tonic-gate  * 	and one for the thread.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate cred_t *
3817c478bd9Sstevel@tonic-gate crcopy(cred_t *cr)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	cred_t *newcr;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	newcr = cralloc();
3867c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3877c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3887c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
38945916cd2Sjpk 	if (newcr->cr_label)
390ddf7fe95Scasper 		label_hold(newcr->cr_label);
391f48205beScasper 	if (newcr->cr_ksid)
392ddf7fe95Scasper 		kcrsid_hold(newcr->cr_ksid);
393ddf7fe95Scasper 	if (newcr->cr_klpd)
394ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
39567dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
39667dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
3977c478bd9Sstevel@tonic-gate 	crfree(cr);
3987c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3997c478bd9Sstevel@tonic-gate 	return (newcr);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
4047c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
4057c478bd9Sstevel@tonic-gate  * 	and one for the thread.
4067c478bd9Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
4077c478bd9Sstevel@tonic-gate  * "new" cred.
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate void
4107c478bd9Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
4117c478bd9Sstevel@tonic-gate {
412f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
413f48205beScasper 
4147c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4157c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4167c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
41745916cd2Sjpk 	if (newcr->cr_label)
41845916cd2Sjpk 		label_hold(newcr->cr_label);
419ddf7fe95Scasper 	if (newcr->cr_klpd)
420ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
42167dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
42267dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
423f48205beScasper 	if (nkcr) {
424f48205beScasper 		newcr->cr_ksid = nkcr;
425f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
426f48205beScasper 	} else if (newcr->cr_ksid)
427f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4287c478bd9Sstevel@tonic-gate 	crfree(oldcr);
4297c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4347c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4357c478bd9Sstevel@tonic-gate  */
436ddf7fe95Scasper static cred_t *
4375f9878b0Sken Powell - Sun Microsystem crdup_flags(const cred_t *cr, int flgs)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	cred_t *newcr;
4407c478bd9Sstevel@tonic-gate 
441ddf7fe95Scasper 	newcr = cralloc_flags(flgs);
442ddf7fe95Scasper 
443ddf7fe95Scasper 	if (newcr == NULL)
444ddf7fe95Scasper 		return (NULL);
445ddf7fe95Scasper 
4467c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
4477c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4487c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
44945916cd2Sjpk 	if (newcr->cr_label)
45045916cd2Sjpk 		label_hold(newcr->cr_label);
451ddf7fe95Scasper 	if (newcr->cr_klpd)
452ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
453f48205beScasper 	if (newcr->cr_ksid)
454f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
45567dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
45667dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
4577c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4587c478bd9Sstevel@tonic-gate 	return (newcr);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
461ddf7fe95Scasper cred_t *
462ddf7fe95Scasper crdup(cred_t *cr)
463ddf7fe95Scasper {
464ddf7fe95Scasper 	return (crdup_flags(cr, KM_SLEEP));
465ddf7fe95Scasper }
466ddf7fe95Scasper 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4697c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4707c478bd9Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
4717c478bd9Sstevel@tonic-gate  * "new" cred.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate void
4747c478bd9Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
4757c478bd9Sstevel@tonic-gate {
476f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
477f48205beScasper 
4787c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4797c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4807c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
48145916cd2Sjpk 	if (newcr->cr_label)
48245916cd2Sjpk 		label_hold(newcr->cr_label);
483ddf7fe95Scasper 	if (newcr->cr_klpd)
484ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
48567dbe2beSCasper H.S. Dik 	if (newcr->cr_grps)
48667dbe2beSCasper H.S. Dik 		crgrphold(newcr->cr_grps);
487f48205beScasper 	if (nkcr) {
488f48205beScasper 		newcr->cr_ksid = nkcr;
489f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
490f48205beScasper 	} else if (newcr->cr_ksid)
491f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4927c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate cred_t *
49945916cd2Sjpk crgetcred(void)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	cred_t *cr;
5027c478bd9Sstevel@tonic-gate 	proc_t *p;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
5057c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
5067c478bd9Sstevel@tonic-gate 	crhold(cr = p->p_cred);
5077c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
5087c478bd9Sstevel@tonic-gate 	return (cr);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * Backward compatibility check for suser().
5137c478bd9Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
5147c478bd9Sstevel@tonic-gate  * done through use of privilege in the audit trail.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate int
5177c478bd9Sstevel@tonic-gate suser(cred_t *cr)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
5207c478bd9Sstevel@tonic-gate 	    == 0);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
5257c478bd9Sstevel@tonic-gate  * described by the supplied credentials.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate int
5287c478bd9Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	if (gid == cr->cr_gid)
5317c478bd9Sstevel@tonic-gate 		return (1);
5327c478bd9Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
5377c478bd9Sstevel@tonic-gate  */
5387c478bd9Sstevel@tonic-gate int
5397c478bd9Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
5407c478bd9Sstevel@tonic-gate {
54167dbe2beSCasper H.S. Dik 	int hi, lo;
54267dbe2beSCasper H.S. Dik 	credgrp_t *grps = cr->cr_grps;
5437c478bd9Sstevel@tonic-gate 	const gid_t *gp, *endgp;
5447c478bd9Sstevel@tonic-gate 
54567dbe2beSCasper H.S. Dik 	if (grps == NULL)
54667dbe2beSCasper H.S. Dik 		return (0);
54767dbe2beSCasper H.S. Dik 
54867dbe2beSCasper H.S. Dik 	/* For a small number of groups, use sequentials search. */
54967dbe2beSCasper H.S. Dik 	if (grps->crg_ngroups <= BIN_GROUP_SEARCH_CUTOFF) {
55067dbe2beSCasper H.S. Dik 		endgp = &grps->crg_groups[grps->crg_ngroups];
55167dbe2beSCasper H.S. Dik 		for (gp = grps->crg_groups; gp < endgp; gp++)
5527c478bd9Sstevel@tonic-gate 			if (*gp == gid)
5537c478bd9Sstevel@tonic-gate 				return (1);
5547c478bd9Sstevel@tonic-gate 		return (0);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
55767dbe2beSCasper H.S. Dik 	/* We use binary search when we have many groups. */
55867dbe2beSCasper H.S. Dik 	lo = 0;
55967dbe2beSCasper H.S. Dik 	hi = grps->crg_ngroups - 1;
56067dbe2beSCasper H.S. Dik 	gp = grps->crg_groups;
56167dbe2beSCasper H.S. Dik 
56267dbe2beSCasper H.S. Dik 	do {
56367dbe2beSCasper H.S. Dik 		int m = (lo + hi) / 2;
56467dbe2beSCasper H.S. Dik 
56567dbe2beSCasper H.S. Dik 		if (gid > gp[m])
56667dbe2beSCasper H.S. Dik 			lo = m + 1;
56767dbe2beSCasper H.S. Dik 		else if (gid < gp[m])
56867dbe2beSCasper H.S. Dik 			hi = m - 1;
56967dbe2beSCasper H.S. Dik 		else
57067dbe2beSCasper H.S. Dik 			return (1);
57167dbe2beSCasper H.S. Dik 	} while (lo <= hi);
57267dbe2beSCasper H.S. Dik 
57367dbe2beSCasper H.S. Dik 	return (0);
57467dbe2beSCasper H.S. Dik }
57567dbe2beSCasper H.S. Dik 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * This function is called to check whether the credentials set
5787c478bd9Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
5797c478bd9Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
5807c478bd9Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
5817c478bd9Sstevel@tonic-gate  *
5827c478bd9Sstevel@tonic-gate  * The rules are:
5837c478bd9Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
5847c478bd9Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
5857c478bd9Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
5867c478bd9Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
5877c478bd9Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
5887c478bd9Sstevel@tonic-gate  *     succeeds
5897c478bd9Sstevel@tonic-gate  * (4) otherwise, the check fails
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate int
5927c478bd9Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	if (scrp == tcrp)
5957c478bd9Sstevel@tonic-gate 		return (1);
5967c478bd9Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
5977c478bd9Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
5987c478bd9Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
5997c478bd9Sstevel@tonic-gate 		return (0);
6007c478bd9Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
6017c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
6027c478bd9Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
6037c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
6047c478bd9Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
6057c478bd9Sstevel@tonic-gate 		return (1);
6067c478bd9Sstevel@tonic-gate 	return (0);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
6117c478bd9Sstevel@tonic-gate  * additionally returns success if the proc_t's match
6127c478bd9Sstevel@tonic-gate  * It is the preferred interface for most uses.
613ddf7fe95Scasper  * And it will acquire p_crlock itself, so it assert's that it shouldn't
6147c478bd9Sstevel@tonic-gate  * be held.
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate int
6177c478bd9Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	int rets;
6207c478bd9Sstevel@tonic-gate 	cred_t *tcrp;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (tp == sp)
6257c478bd9Sstevel@tonic-gate 		return (1);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
6287c478bd9Sstevel@tonic-gate 		return (0);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
631ddf7fe95Scasper 	crhold(tcrp = tp->p_cred);
6327c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
633ddf7fe95Scasper 	rets = hasprocperm(tcrp, scrp);
634ddf7fe95Scasper 	crfree(tcrp);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	return (rets);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
6417c478bd9Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
6427c478bd9Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
6437c478bd9Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
6467c478bd9Sstevel@tonic-gate  * 1 if they do not.
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate int
6497c478bd9Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
6507c478bd9Sstevel@tonic-gate {
65167dbe2beSCasper H.S. Dik 	credgrp_t *grp1, *grp2;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	if (cr1 == cr2)
6547c478bd9Sstevel@tonic-gate 		return (0);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
6577c478bd9Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
6587c478bd9Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
6597c478bd9Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
6607c478bd9Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
66167dbe2beSCasper H.S. Dik 	    ((grp1 = cr1->cr_grps) == (grp2 = cr2->cr_grps) ||
66267dbe2beSCasper H.S. Dik 	    (grp1 != NULL && grp2 != NULL &&
66367dbe2beSCasper H.S. Dik 	    grp1->crg_ngroups == grp2->crg_ngroups &&
66467dbe2beSCasper H.S. Dik 	    bcmp(grp1->crg_groups, grp2->crg_groups,
66567dbe2beSCasper H.S. Dik 	    grp1->crg_ngroups * sizeof (gid_t)) == 0))) {
6667c478bd9Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 	return (1);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  * Read access functions to cred_t.
6737c478bd9Sstevel@tonic-gate  */
6747c478bd9Sstevel@tonic-gate uid_t
6757c478bd9Sstevel@tonic-gate crgetuid(const cred_t *cr)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	return (cr->cr_uid);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate uid_t
6817c478bd9Sstevel@tonic-gate crgetruid(const cred_t *cr)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	return (cr->cr_ruid);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate uid_t
6877c478bd9Sstevel@tonic-gate crgetsuid(const cred_t *cr)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	return (cr->cr_suid);
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate gid_t
6937c478bd9Sstevel@tonic-gate crgetgid(const cred_t *cr)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	return (cr->cr_gid);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate gid_t
6997c478bd9Sstevel@tonic-gate crgetrgid(const cred_t *cr)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	return (cr->cr_rgid);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate gid_t
7057c478bd9Sstevel@tonic-gate crgetsgid(const cred_t *cr)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	return (cr->cr_sgid);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate const auditinfo_addr_t *
7117c478bd9Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate auditinfo_addr_t *
7177c478bd9Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	return (CR_AUINFO(cr));
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate zoneid_t
7237c478bd9Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
7247c478bd9Sstevel@tonic-gate {
72545916cd2Sjpk 	return (cr->cr_zone == NULL ?
72645916cd2Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
72745916cd2Sjpk 	    cr->cr_zone->zone_id);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate projid_t
7317c478bd9Sstevel@tonic-gate crgetprojid(const cred_t *cr)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	return (cr->cr_projid);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
73645916cd2Sjpk zone_t *
73745916cd2Sjpk crgetzone(const cred_t *cr)
73845916cd2Sjpk {
73945916cd2Sjpk 	return (cr->cr_zone);
74045916cd2Sjpk }
74145916cd2Sjpk 
74245916cd2Sjpk struct ts_label_s *
74345916cd2Sjpk crgetlabel(const cred_t *cr)
74445916cd2Sjpk {
74545916cd2Sjpk 	return (cr->cr_label ?
74645916cd2Sjpk 	    cr->cr_label :
74745916cd2Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
74845916cd2Sjpk }
74945916cd2Sjpk 
75045916cd2Sjpk boolean_t
75145916cd2Sjpk crisremote(const cred_t *cr)
75245916cd2Sjpk {
75345916cd2Sjpk 	return (REMOTE_PEER_CRED(cr));
75445916cd2Sjpk }
75545916cd2Sjpk 
756bda89588Sjp151216 #define	BADUID(x, zn)	((x) != -1 && !VALID_UID((x), (zn)))
757bda89588Sjp151216 #define	BADGID(x, zn)	((x) != -1 && !VALID_GID((x), (zn)))
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate int
7607c478bd9Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
7617c478bd9Sstevel@tonic-gate {
762bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
763bda89588Sjp151216 
7647c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7657c478bd9Sstevel@tonic-gate 
766bda89588Sjp151216 	if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone))
7677c478bd9Sstevel@tonic-gate 		return (-1);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if (r != -1)
7707c478bd9Sstevel@tonic-gate 		cr->cr_ruid = r;
7717c478bd9Sstevel@tonic-gate 	if (e != -1)
7727c478bd9Sstevel@tonic-gate 		cr->cr_uid = e;
7737c478bd9Sstevel@tonic-gate 	if (s != -1)
7747c478bd9Sstevel@tonic-gate 		cr->cr_suid = s;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (0);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate int
7807c478bd9Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
7817c478bd9Sstevel@tonic-gate {
782bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
783bda89588Sjp151216 
7847c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7857c478bd9Sstevel@tonic-gate 
786bda89588Sjp151216 	if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone))
7877c478bd9Sstevel@tonic-gate 		return (-1);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (r != -1)
7907c478bd9Sstevel@tonic-gate 		cr->cr_rgid = r;
7917c478bd9Sstevel@tonic-gate 	if (e != -1)
7927c478bd9Sstevel@tonic-gate 		cr->cr_gid = e;
7937c478bd9Sstevel@tonic-gate 	if (s != -1)
7947c478bd9Sstevel@tonic-gate 		cr->cr_sgid = s;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	return (0);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate int
8007c478bd9Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
8017c478bd9Sstevel@tonic-gate {
802bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
803bda89588Sjp151216 
8047c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
8057c478bd9Sstevel@tonic-gate 
806bda89588Sjp151216 	if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone))
8077c478bd9Sstevel@tonic-gate 		return (-1);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
8107c478bd9Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	return (0);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
81567dbe2beSCasper H.S. Dik static int
81667dbe2beSCasper H.S. Dik gidcmp(const void *v1, const void *v2)
81767dbe2beSCasper H.S. Dik {
81867dbe2beSCasper H.S. Dik 	gid_t g1 = *(gid_t *)v1;
81967dbe2beSCasper H.S. Dik 	gid_t g2 = *(gid_t *)v2;
82067dbe2beSCasper H.S. Dik 
82167dbe2beSCasper H.S. Dik 	if (g1 < g2)
82267dbe2beSCasper H.S. Dik 		return (-1);
82367dbe2beSCasper H.S. Dik 	else if (g1 > g2)
82467dbe2beSCasper H.S. Dik 		return (1);
82567dbe2beSCasper H.S. Dik 	else
82667dbe2beSCasper H.S. Dik 		return (0);
82767dbe2beSCasper H.S. Dik }
82867dbe2beSCasper H.S. Dik 
8297c478bd9Sstevel@tonic-gate int
8307c478bd9Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
8357c478bd9Sstevel@tonic-gate 		return (-1);
8367c478bd9Sstevel@tonic-gate 
83767dbe2beSCasper H.S. Dik 	if (cr->cr_grps != NULL)
83867dbe2beSCasper H.S. Dik 		crgrprele(cr->cr_grps);
8397c478bd9Sstevel@tonic-gate 
84067dbe2beSCasper H.S. Dik 	if (n > 0) {
84167dbe2beSCasper H.S. Dik 		cr->cr_grps = kmem_alloc(CREDGRPSZ(n), KM_SLEEP);
84267dbe2beSCasper H.S. Dik 		bcopy(grp, cr->cr_grps->crg_groups, n * sizeof (gid_t));
84367dbe2beSCasper H.S. Dik 		cr->cr_grps->crg_ref = 1;
84467dbe2beSCasper H.S. Dik 		cr->cr_grps->crg_ngroups = n;
84567dbe2beSCasper H.S. Dik 		qsort(cr->cr_grps->crg_groups, n, sizeof (gid_t), gidcmp);
84667dbe2beSCasper H.S. Dik 	} else {
84767dbe2beSCasper H.S. Dik 		cr->cr_grps = NULL;
84867dbe2beSCasper H.S. Dik 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	return (0);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate void
8547c478bd9Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
8577c478bd9Sstevel@tonic-gate 	cr->cr_projid = projid;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*
86167dbe2beSCasper H.S. Dik  * This routine returns the pointer to the first element of the crg_groups
8627c478bd9Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
86367dbe2beSCasper H.S. Dik  * Note that when we have no grouplist, we return one element but the
86467dbe2beSCasper H.S. Dik  * caller should never reference it.
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate const gid_t *
8677c478bd9Sstevel@tonic-gate crgetgroups(const cred_t *cr)
8687c478bd9Sstevel@tonic-gate {
86967dbe2beSCasper H.S. Dik 	return (cr->cr_grps == NULL ? &cr->cr_gid : cr->cr_grps->crg_groups);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate int
8737c478bd9Sstevel@tonic-gate crgetngroups(const cred_t *cr)
8747c478bd9Sstevel@tonic-gate {
87567dbe2beSCasper H.S. Dik 	return (cr->cr_grps == NULL ? 0 : cr->cr_grps->crg_ngroups);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate void
8797c478bd9Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
8827c478bd9Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
8837c478bd9Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
8847c478bd9Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
8857c478bd9Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
8867c478bd9Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
8877c478bd9Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0; /* in case ngroups == 0 */
88867dbe2beSCasper H.S. Dik 	pcrp->pr_ngroups = cr->cr_grps == NULL ? 0 : cr->cr_grps->crg_ngroups;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
89167dbe2beSCasper H.S. Dik 		bcopy(cr->cr_grps->crg_groups, pcrp->pr_groups,
89267dbe2beSCasper H.S. Dik 		    sizeof (gid_t) * pcrp->pr_ngroups);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate static int
89645916cd2Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
8997c478bd9Sstevel@tonic-gate 	au_tid_addr_t	tid;
9007c478bd9Sstevel@tonic-gate 
901134a1f4eSCasper H.S. Dik 	if (secpolicy_audit_getattr(rcr, B_TRUE) != 0)
9027c478bd9Sstevel@tonic-gate 		return (-1);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
9057c478bd9Sstevel@tonic-gate 	tid = ai->ai_termid;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
9087c478bd9Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
9097c478bd9Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
9127c478bd9Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
9157c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	return (0);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
92045916cd2Sjpk void
92145916cd2Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
92245916cd2Sjpk {
92345916cd2Sjpk 	ts_label_t	*tslp;
92445916cd2Sjpk 
92545916cd2Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
92645916cd2Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
92745916cd2Sjpk }
92845916cd2Sjpk 
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
9317c478bd9Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
9327c478bd9Sstevel@tonic-gate  * memory and copy it twice.
93345916cd2Sjpk  *
93445916cd2Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
93545916cd2Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
93645916cd2Sjpk  * to determine whether audit info should be included.
9377c478bd9Sstevel@tonic-gate  */
9387c478bd9Sstevel@tonic-gate struct ucred_s *
93945916cd2Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
94267dbe2beSCasper H.S. Dik 	uint32_t realsz = ucredminsize(cr);
94367dbe2beSCasper H.S. Dik 	ts_label_t *tslp = is_system_labeled() ? crgetlabel(cr) : NULL;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
9467c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
94767dbe2beSCasper H.S. Dik 		uc = kmem_zalloc(realsz, KM_SLEEP);
9487c478bd9Sstevel@tonic-gate 	} else {
94967dbe2beSCasper H.S. Dik 		bzero(buf, realsz);
9507c478bd9Sstevel@tonic-gate 		uc = buf;
9517c478bd9Sstevel@tonic-gate 	}
95267dbe2beSCasper H.S. Dik 	uc->uc_size = realsz;
9537c478bd9Sstevel@tonic-gate 	uc->uc_pid = pid;
9547c478bd9Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
9557c478bd9Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
9567c478bd9Sstevel@tonic-gate 
95745916cd2Sjpk 	if (REMOTE_PEER_CRED(cr)) {
95845916cd2Sjpk 		/*
95967dbe2beSCasper H.S. Dik 		 * Other than label, the rest of cred info about a
96067dbe2beSCasper H.S. Dik 		 * remote peer isn't available. Copy the label directly
96167dbe2beSCasper H.S. Dik 		 * after the header where we generally copy the prcred.
96267dbe2beSCasper H.S. Dik 		 * That's why we use sizeof (struct ucred_s).  The other
96367dbe2beSCasper H.S. Dik 		 * offset fields are initialized to 0.
96445916cd2Sjpk 		 */
96567dbe2beSCasper H.S. Dik 		uc->uc_labeloff = tslp == NULL ? 0 : sizeof (struct ucred_s);
96645916cd2Sjpk 	} else {
96767dbe2beSCasper H.S. Dik 		uc->uc_credoff = UCRED_CRED_OFF;
96867dbe2beSCasper H.S. Dik 		uc->uc_privoff = UCRED_PRIV_OFF;
96967dbe2beSCasper H.S. Dik 		uc->uc_audoff = UCRED_AUD_OFF;
97067dbe2beSCasper H.S. Dik 		uc->uc_labeloff = tslp == NULL ? 0 : UCRED_LABEL_OFF;
97167dbe2beSCasper H.S. Dik 
9727c478bd9Sstevel@tonic-gate 		cred2prcred(cr, UCCRED(uc));
9737c478bd9Sstevel@tonic-gate 		cred2prpriv(cr, UCPRIV(uc));
97467dbe2beSCasper H.S. Dik 
97545916cd2Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
9767c478bd9Sstevel@tonic-gate 			uc->uc_audoff = 0;
97745916cd2Sjpk 	}
97867dbe2beSCasper H.S. Dik 	if (tslp != NULL)
97967dbe2beSCasper H.S. Dik 		bcopy(&tslp->tsl_label, UCLABEL(uc), sizeof (bslabel_t));
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	return (uc);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /*
98567dbe2beSCasper H.S. Dik  * Don't allocate the non-needed group entries.  Note: this function
98667dbe2beSCasper H.S. Dik  * must match the code in cred2ucred; they must agree about the
98767dbe2beSCasper H.S. Dik  * minimal size of the ucred.
98867dbe2beSCasper H.S. Dik  */
98967dbe2beSCasper H.S. Dik uint32_t
99067dbe2beSCasper H.S. Dik ucredminsize(const cred_t *cr)
99167dbe2beSCasper H.S. Dik {
99267dbe2beSCasper H.S. Dik 	int ndiff;
99367dbe2beSCasper H.S. Dik 
99467dbe2beSCasper H.S. Dik 	if (cr == NULL)
99567dbe2beSCasper H.S. Dik 		return (ucredsize);
99667dbe2beSCasper H.S. Dik 
99767dbe2beSCasper H.S. Dik 	if (REMOTE_PEER_CRED(cr)) {
99867dbe2beSCasper H.S. Dik 		if (is_system_labeled())
99967dbe2beSCasper H.S. Dik 			return (sizeof (struct ucred_s) + sizeof (bslabel_t));
100067dbe2beSCasper H.S. Dik 		else
100167dbe2beSCasper H.S. Dik 			return (sizeof (struct ucred_s));
100267dbe2beSCasper H.S. Dik 	}
100367dbe2beSCasper H.S. Dik 
100467dbe2beSCasper H.S. Dik 	if (cr->cr_grps == NULL)
100567dbe2beSCasper H.S. Dik 		ndiff = ngroups_max - 1;	/* Needs one for prcred_t */
100667dbe2beSCasper H.S. Dik 	else
100767dbe2beSCasper H.S. Dik 		ndiff = ngroups_max - cr->cr_grps->crg_ngroups;
100867dbe2beSCasper H.S. Dik 
100967dbe2beSCasper H.S. Dik 	return (ucredsize - ndiff * sizeof (gid_t));
101067dbe2beSCasper H.S. Dik }
101167dbe2beSCasper H.S. Dik 
101267dbe2beSCasper H.S. Dik /*
10137c478bd9Sstevel@tonic-gate  * Get the "ucred" of a process.
10147c478bd9Sstevel@tonic-gate  */
10157c478bd9Sstevel@tonic-gate struct ucred_s *
10167c478bd9Sstevel@tonic-gate pgetucred(proc_t *p)
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate 	cred_t *cr;
10197c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
10227c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
10237c478bd9Sstevel@tonic-gate 	crhold(cr);
10247c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
10257c478bd9Sstevel@tonic-gate 
102645916cd2Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
10277c478bd9Sstevel@tonic-gate 	crfree(cr);
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	return (uc);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
10347c478bd9Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
10357c478bd9Sstevel@tonic-gate  * make that the uid instead and retry the call.
10367c478bd9Sstevel@tonic-gate  * Private interface for NFS.
10377c478bd9Sstevel@tonic-gate  */
10387c478bd9Sstevel@tonic-gate cred_t *
10397c478bd9Sstevel@tonic-gate crnetadjust(cred_t *cr)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
10427c478bd9Sstevel@tonic-gate 		cr = crdup(cr);
10437c478bd9Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
10447c478bd9Sstevel@tonic-gate 		return (cr);
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 	return (NULL);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate  * The reference count is of interest when you want to check
10517c478bd9Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate uint_t
10547c478bd9Sstevel@tonic-gate crgetref(const cred_t *cr)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	return (cr->cr_ref);
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate static int
10607c478bd9Sstevel@tonic-gate get_c2audit_load(void)
10617c478bd9Sstevel@tonic-gate {
10627c478bd9Sstevel@tonic-gate 	static int	gotit = 0;
10637c478bd9Sstevel@tonic-gate 	static int	c2audit_load;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (gotit)
10667c478bd9Sstevel@tonic-gate 		return (c2audit_load);
1067005d3febSMarek Pospisil 	c2audit_load = 1;		/* set default value once */
1068005d3febSMarek Pospisil 	if (mod_sysctl(SYS_CHECK_EXCLUDE, "c2audit") != 0)
1069005d3febSMarek Pospisil 		c2audit_load = 0;
10707c478bd9Sstevel@tonic-gate 	gotit++;
1071005d3febSMarek Pospisil 
10727c478bd9Sstevel@tonic-gate 	return (c2audit_load);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate int
10767c478bd9Sstevel@tonic-gate get_audit_ucrsize(void)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
10837c478bd9Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
10847c478bd9Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
10857c478bd9Sstevel@tonic-gate  * same.
10867c478bd9Sstevel@tonic-gate  */
10877c478bd9Sstevel@tonic-gate void
10887c478bd9Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	ASSERT(cr != kcred);
10937c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
10947c478bd9Sstevel@tonic-gate 	cr->cr_zone = zptr;
10957c478bd9Sstevel@tonic-gate 	zone_cred_hold(zptr);
10967c478bd9Sstevel@tonic-gate 	if (oldzptr)
10977c478bd9Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
10987c478bd9Sstevel@tonic-gate }
109945916cd2Sjpk 
110045916cd2Sjpk /*
110145916cd2Sjpk  * Create a new cred based on the supplied label
110245916cd2Sjpk  */
110345916cd2Sjpk cred_t *
110445916cd2Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
110545916cd2Sjpk {
110645916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
110745916cd2Sjpk 	cred_t *cr = NULL;
110845916cd2Sjpk 
110945916cd2Sjpk 	if (lbl != NULL) {
1110ddf7fe95Scasper 		if ((cr = crdup_flags(dummycr, flags)) != NULL) {
111145916cd2Sjpk 			cr->cr_label = lbl;
111245916cd2Sjpk 		} else {
111345916cd2Sjpk 			label_rele(lbl);
111445916cd2Sjpk 		}
111545916cd2Sjpk 	}
111645916cd2Sjpk 
111745916cd2Sjpk 	return (cr);
111845916cd2Sjpk }
111945916cd2Sjpk 
112045916cd2Sjpk /*
112145916cd2Sjpk  * Derive a new cred from the existing cred, but with a different label.
112245916cd2Sjpk  * To be used when a cred is being shared, but the label needs to be changed
112345916cd2Sjpk  * by a caller without affecting other users
112445916cd2Sjpk  */
112545916cd2Sjpk cred_t *
11265f9878b0Sken Powell - Sun Microsystem copycred_from_tslabel(const cred_t *cr, ts_label_t *label, int flags)
11275f9878b0Sken Powell - Sun Microsystem {
11285f9878b0Sken Powell - Sun Microsystem 	cred_t *newcr = NULL;
11295f9878b0Sken Powell - Sun Microsystem 
11305f9878b0Sken Powell - Sun Microsystem 	if ((newcr = crdup_flags(cr, flags)) != NULL) {
11315f9878b0Sken Powell - Sun Microsystem 		if (newcr->cr_label != NULL)
11325f9878b0Sken Powell - Sun Microsystem 			label_rele(newcr->cr_label);
11335f9878b0Sken Powell - Sun Microsystem 		label_hold(label);
11345f9878b0Sken Powell - Sun Microsystem 		newcr->cr_label = label;
11355f9878b0Sken Powell - Sun Microsystem 	}
11365f9878b0Sken Powell - Sun Microsystem 
11375f9878b0Sken Powell - Sun Microsystem 	return (newcr);
11385f9878b0Sken Powell - Sun Microsystem }
11395f9878b0Sken Powell - Sun Microsystem 
11405f9878b0Sken Powell - Sun Microsystem /*
11415f9878b0Sken Powell - Sun Microsystem  * Derive a new cred from the existing cred, but with a different label.
11425f9878b0Sken Powell - Sun Microsystem  */
11435f9878b0Sken Powell - Sun Microsystem cred_t *
11445f9878b0Sken Powell - Sun Microsystem copycred_from_bslabel(const cred_t *cr, bslabel_t *blabel,
11455f9878b0Sken Powell - Sun Microsystem     uint32_t doi, int flags)
114645916cd2Sjpk {
114745916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
114845916cd2Sjpk 	cred_t  *newcr = NULL;
114945916cd2Sjpk 
115045916cd2Sjpk 	if (lbl != NULL) {
11515f9878b0Sken Powell - Sun Microsystem 		newcr = copycred_from_tslabel(cr, lbl, flags);
115245916cd2Sjpk 		label_rele(lbl);
115345916cd2Sjpk 	}
115445916cd2Sjpk 
115545916cd2Sjpk 	return (newcr);
115645916cd2Sjpk }
115745916cd2Sjpk 
115845916cd2Sjpk /*
115945916cd2Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
116045916cd2Sjpk  */
116145916cd2Sjpk cred_t *
116245916cd2Sjpk zone_kcred(void)
116345916cd2Sjpk {
116445916cd2Sjpk 	zone_t *zone;
116545916cd2Sjpk 
116645916cd2Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
116745916cd2Sjpk 		return (zone->zone_kcred);
116845916cd2Sjpk 	else
116945916cd2Sjpk 		return (kcred);
117045916cd2Sjpk }
1171f48205beScasper 
1172f48205beScasper boolean_t
1173bda89588Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id)
1174f48205beScasper {
1175bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
11764edd44c5Sjp151216 	if (id <= IDMAP_WK__MAX_UID)
1177bda89588Sjp151216 		return (B_TRUE);
1178bda89588Sjp151216 
1179bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1180bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1181f48205beScasper 	membar_consumer();
1182bda89588Sjp151216 	return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid);
1183f48205beScasper }
1184f48205beScasper 
1185f48205beScasper boolean_t
1186bda89588Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id)
1187f48205beScasper {
1188bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
11894edd44c5Sjp151216 	if (id <= IDMAP_WK__MAX_GID)
1190bda89588Sjp151216 		return (B_TRUE);
1191bda89588Sjp151216 
1192bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1193bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1194f48205beScasper 	membar_consumer();
1195bda89588Sjp151216 	return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid);
1196f48205beScasper }
1197f48205beScasper 
1198f48205beScasper int
1199bda89588Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count)
1200f48205beScasper {
1201bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1202bda89588Sjp151216 
1203bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1204bda89588Sjp151216 
1205bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1206f48205beScasper 
1207f48205beScasper 	/* Test for unsigned integer wrap around */
1208bda89588Sjp151216 	if (eph_zsd->last_uid + count < eph_zsd->last_uid) {
1209bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1210f48205beScasper 		return (-1);
1211f48205beScasper 	}
1212f48205beScasper 
1213f48205beScasper 	/* first call or idmap crashed and state corrupted */
1214f48205beScasper 	if (flags != 0)
1215bda89588Sjp151216 		eph_zsd->min_uid = eph_zsd->last_uid;
1216f48205beScasper 
1217f48205beScasper 	hasephids = B_TRUE;
1218bda89588Sjp151216 	*start = eph_zsd->last_uid + 1;
1219bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_uid, count);
1220bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1221f48205beScasper 	return (0);
1222f48205beScasper }
1223f48205beScasper 
1224f48205beScasper int
1225bda89588Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count)
1226f48205beScasper {
1227bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1228bda89588Sjp151216 
1229bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1230bda89588Sjp151216 
1231bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1232f48205beScasper 
1233f48205beScasper 	/* Test for unsigned integer wrap around */
1234bda89588Sjp151216 	if (eph_zsd->last_gid + count < eph_zsd->last_gid) {
1235bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1236f48205beScasper 		return (-1);
1237f48205beScasper 	}
1238f48205beScasper 
1239f48205beScasper 	/* first call or idmap crashed and state corrupted */
1240f48205beScasper 	if (flags != 0)
1241bda89588Sjp151216 		eph_zsd->min_gid = eph_zsd->last_gid;
1242f48205beScasper 
1243f48205beScasper 	hasephids = B_TRUE;
1244bda89588Sjp151216 	*start = eph_zsd->last_gid + 1;
1245bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_gid, count);
1246bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1247f48205beScasper 	return (0);
1248f48205beScasper }
1249f48205beScasper 
1250f48205beScasper /*
1251bda89588Sjp151216  * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data()
1252bda89588Sjp151216  * are project private functions that are for use of the test system only and
1253bda89588Sjp151216  * are not to be used for other purposes.
1254bda89588Sjp151216  */
1255bda89588Sjp151216 
1256bda89588Sjp151216 void
1257bda89588Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid,
1258bda89588Sjp151216 	gid_t *min_gid, gid_t *last_gid)
1259bda89588Sjp151216 {
1260bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1261bda89588Sjp151216 
1262bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1263bda89588Sjp151216 
1264bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1265bda89588Sjp151216 
1266bda89588Sjp151216 	*min_uid = eph_zsd->min_uid;
1267bda89588Sjp151216 	*last_uid = eph_zsd->last_uid;
1268bda89588Sjp151216 	*min_gid = eph_zsd->min_gid;
1269bda89588Sjp151216 	*last_gid = eph_zsd->last_gid;
1270bda89588Sjp151216 
1271bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1272bda89588Sjp151216 }
1273bda89588Sjp151216 
1274bda89588Sjp151216 
1275bda89588Sjp151216 void
1276bda89588Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid,
1277bda89588Sjp151216 	gid_t min_gid, gid_t last_gid)
1278bda89588Sjp151216 {
1279bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1280bda89588Sjp151216 
1281bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1282bda89588Sjp151216 
1283bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1284bda89588Sjp151216 
1285bda89588Sjp151216 	if (min_uid != 0)
1286bda89588Sjp151216 		eph_zsd->min_uid = min_uid;
1287bda89588Sjp151216 	if (last_uid != 0)
1288bda89588Sjp151216 		eph_zsd->last_uid = last_uid;
1289bda89588Sjp151216 	if (min_gid != 0)
1290bda89588Sjp151216 		eph_zsd->min_gid = min_gid;
1291bda89588Sjp151216 	if (last_gid != 0)
1292bda89588Sjp151216 		eph_zsd->last_gid = last_gid;
1293bda89588Sjp151216 
1294bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1295bda89588Sjp151216 }
1296bda89588Sjp151216 
1297bda89588Sjp151216 /*
1298da6c28aaSamw  * If the credential user SID or group SID is mapped to an ephemeral
1299da6c28aaSamw  * ID, map the credential to nobody.
1300f48205beScasper  */
1301f48205beScasper cred_t *
1302f48205beScasper crgetmapped(const cred_t *cr)
1303f48205beScasper {
1304bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
130590c3d472Scasper 	/*
130690c3d472Scasper 	 * Someone incorrectly passed a NULL cred to a vnode operation
130790c3d472Scasper 	 * either on purpose or by calling CRED() in interrupt context.
130890c3d472Scasper 	 */
130990c3d472Scasper 	if (cr == NULL)
131090c3d472Scasper 		return (NULL);
131190c3d472Scasper 
1312f48205beScasper 	if (cr->cr_ksid != NULL) {
1313bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) {
1314bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1315bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1316bda89588Sjp151216 		}
1317f48205beScasper 
1318bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) {
1319bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1320bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1321bda89588Sjp151216 		}
1322f48205beScasper 	}
1323f48205beScasper 
1324f48205beScasper 	return ((cred_t *)cr);
1325f48205beScasper }
1326f48205beScasper 
1327f48205beScasper /* index should be in range for a ksidindex_t */
1328f48205beScasper void
1329f48205beScasper crsetsid(cred_t *cr, ksid_t *ksp, int index)
1330f48205beScasper {
1331f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1332f48205beScasper 	ASSERT(index >= 0 && index < KSID_COUNT);
1333f48205beScasper 	if (cr->cr_ksid == NULL && ksp == NULL)
1334f48205beScasper 		return;
1335f48205beScasper 	cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index);
1336f48205beScasper }
1337f48205beScasper 
1338f48205beScasper void
1339f48205beScasper crsetsidlist(cred_t *cr, ksidlist_t *ksl)
1340f48205beScasper {
1341f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1342f48205beScasper 	if (cr->cr_ksid == NULL && ksl == NULL)
1343f48205beScasper 		return;
1344f48205beScasper 	cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl);
1345f48205beScasper }
1346f48205beScasper 
1347f48205beScasper ksid_t *
1348f48205beScasper crgetsid(const cred_t *cr, int i)
1349f48205beScasper {
1350f48205beScasper 	ASSERT(i >= 0 && i < KSID_COUNT);
1351f48205beScasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain)
1352f48205beScasper 		return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]);
1353f48205beScasper 	return (NULL);
1354f48205beScasper }
1355f48205beScasper 
1356f48205beScasper ksidlist_t *
1357f48205beScasper crgetsidlist(const cred_t *cr)
1358f48205beScasper {
1359da6c28aaSamw 	if (cr->cr_ksid != NULL)
136028a151a0Scasper 		return (cr->cr_ksid->kr_sidlist);
1361f48205beScasper 	return (NULL);
1362f48205beScasper }
1363da6c28aaSamw 
1364da6c28aaSamw /*
1365da6c28aaSamw  * Interface to set the effective and permitted privileges for
1366da6c28aaSamw  * a credential; this interface does no security checks and is
1367da6c28aaSamw  * intended for kernel (file)servers creating credentials with
1368da6c28aaSamw  * specific privileges.
1369da6c28aaSamw  */
1370da6c28aaSamw int
1371da6c28aaSamw crsetpriv(cred_t *cr, ...)
1372da6c28aaSamw {
1373da6c28aaSamw 	va_list ap;
1374da6c28aaSamw 	const char *privnm;
1375da6c28aaSamw 
1376da6c28aaSamw 	ASSERT(cr->cr_ref <= 2);
1377da6c28aaSamw 
1378da6c28aaSamw 	priv_set_PA(cr);
1379da6c28aaSamw 
1380da6c28aaSamw 	va_start(ap, cr);
1381da6c28aaSamw 
1382da6c28aaSamw 	while ((privnm = va_arg(ap, const char *)) != NULL) {
1383da6c28aaSamw 		int priv = priv_getbyname(privnm, 0);
1384da6c28aaSamw 		if (priv < 0)
1385da6c28aaSamw 			return (-1);
1386da6c28aaSamw 
1387da6c28aaSamw 		priv_addset(&CR_PPRIV(cr), priv);
1388da6c28aaSamw 		priv_addset(&CR_EPRIV(cr), priv);
1389da6c28aaSamw 	}
1390da6c28aaSamw 	priv_adjust_PA(cr);
1391da6c28aaSamw 	va_end(ap);
1392da6c28aaSamw 	return (0);
1393da6c28aaSamw }
1394ddf7fe95Scasper 
1395546a3997SThomas Haynes /*
1396546a3997SThomas Haynes  * Interface to effectively set the PRIV_ALL for
1397546a3997SThomas Haynes  * a credential; this interface does no security checks and is
1398546a3997SThomas Haynes  * intended for kernel (file)servers to extend the user credentials
1399546a3997SThomas Haynes  * to be ALL, like either kcred or zcred.
1400546a3997SThomas Haynes  */
1401546a3997SThomas Haynes void
1402546a3997SThomas Haynes crset_zone_privall(cred_t *cr)
1403546a3997SThomas Haynes {
1404546a3997SThomas Haynes 	zone_t	*zone = crgetzone(cr);
1405546a3997SThomas Haynes 
1406546a3997SThomas Haynes 	priv_fillset(&CR_LPRIV(cr));
1407546a3997SThomas Haynes 	CR_EPRIV(cr) = CR_PPRIV(cr) = CR_IPRIV(cr) = CR_LPRIV(cr);
1408546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_LPRIV(cr));
1409546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_EPRIV(cr));
1410546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_IPRIV(cr));
1411546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_PPRIV(cr));
1412546a3997SThomas Haynes }
1413546a3997SThomas Haynes 
1414ddf7fe95Scasper struct credklpd *
1415ddf7fe95Scasper crgetcrklpd(const cred_t *cr)
1416ddf7fe95Scasper {
1417ddf7fe95Scasper 	return (cr->cr_klpd);
1418ddf7fe95Scasper }
1419ddf7fe95Scasper 
1420ddf7fe95Scasper void
1421ddf7fe95Scasper crsetcrklpd(cred_t *cr, struct credklpd *crklpd)
1422ddf7fe95Scasper {
1423ddf7fe95Scasper 	ASSERT(cr->cr_ref <= 2);
1424ddf7fe95Scasper 
1425ddf7fe95Scasper 	if (cr->cr_klpd != NULL)
1426ddf7fe95Scasper 		crklpd_rele(cr->cr_klpd);
1427ddf7fe95Scasper 	cr->cr_klpd = crklpd;
1428ddf7fe95Scasper }
142967dbe2beSCasper H.S. Dik 
143067dbe2beSCasper H.S. Dik credgrp_t *
143167dbe2beSCasper H.S. Dik crgrpcopyin(int n, gid_t *gidset)
143267dbe2beSCasper H.S. Dik {
143367dbe2beSCasper H.S. Dik 	credgrp_t *mem;
143467dbe2beSCasper H.S. Dik 	size_t sz = CREDGRPSZ(n);
143567dbe2beSCasper H.S. Dik 
143667dbe2beSCasper H.S. Dik 	ASSERT(n > 0);
143767dbe2beSCasper H.S. Dik 
143867dbe2beSCasper H.S. Dik 	mem = kmem_alloc(sz, KM_SLEEP);
143967dbe2beSCasper H.S. Dik 
144067dbe2beSCasper H.S. Dik 	if (copyin(gidset, mem->crg_groups, sizeof (gid_t) * n)) {
144167dbe2beSCasper H.S. Dik 		kmem_free(mem, sz);
144267dbe2beSCasper H.S. Dik 		return (NULL);
144367dbe2beSCasper H.S. Dik 	}
144467dbe2beSCasper H.S. Dik 	mem->crg_ref = 1;
144567dbe2beSCasper H.S. Dik 	mem->crg_ngroups = n;
1446b77dfcc8SIra Cooper 	qsort(mem->crg_groups, n, sizeof (gid_t), gidcmp);
144767dbe2beSCasper H.S. Dik 	return (mem);
144867dbe2beSCasper H.S. Dik }
144967dbe2beSCasper H.S. Dik 
145067dbe2beSCasper H.S. Dik const gid_t *
145167dbe2beSCasper H.S. Dik crgetggroups(const credgrp_t *grps)
145267dbe2beSCasper H.S. Dik {
145367dbe2beSCasper H.S. Dik 	return (grps->crg_groups);
145467dbe2beSCasper H.S. Dik }
145567dbe2beSCasper H.S. Dik 
145667dbe2beSCasper H.S. Dik void
145767dbe2beSCasper H.S. Dik crsetcredgrp(cred_t *cr, credgrp_t *grps)
145867dbe2beSCasper H.S. Dik {
145967dbe2beSCasper H.S. Dik 	ASSERT(cr->cr_ref <= 2);
146067dbe2beSCasper H.S. Dik 
146167dbe2beSCasper H.S. Dik 	if (cr->cr_grps != NULL)
146267dbe2beSCasper H.S. Dik 		crgrprele(cr->cr_grps);
146367dbe2beSCasper H.S. Dik 
146467dbe2beSCasper H.S. Dik 	cr->cr_grps = grps;
146567dbe2beSCasper H.S. Dik }
146667dbe2beSCasper H.S. Dik 
146767dbe2beSCasper H.S. Dik void
146867dbe2beSCasper H.S. Dik crgrprele(credgrp_t *grps)
146967dbe2beSCasper H.S. Dik {
1470*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&grps->crg_ref) == 0)
147167dbe2beSCasper H.S. Dik 		kmem_free(grps, CREDGRPSZ(grps->crg_ngroups));
147267dbe2beSCasper H.S. Dik }
147367dbe2beSCasper H.S. Dik 
147467dbe2beSCasper H.S. Dik static void
147567dbe2beSCasper H.S. Dik crgrphold(credgrp_t *grps)
147667dbe2beSCasper H.S. Dik {
1477*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&grps->crg_ref);
147867dbe2beSCasper H.S. Dik }
1479