xref: /titanic_51/usr/src/uts/common/os/cred.c (revision 5f9878b0212a5bc5924a85d227160bf7f43712f1)
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 /*
22546a3997SThomas Haynes  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
447c478bd9Sstevel@tonic-gate #include <sys/policy.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/user.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
537c478bd9Sstevel@tonic-gate #include <sys/ucred.h>
547c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
56f48205beScasper #include <sys/avl.h>
57da6c28aaSamw #include <sys/door.h>
587c478bd9Sstevel@tonic-gate #include <c2/audit.h>
597c478bd9Sstevel@tonic-gate #include <sys/zone.h>
6045916cd2Sjpk #include <sys/tsol/label.h>
61f48205beScasper #include <sys/sid.h>
62c5c4113dSnw141292 #include <sys/idmap.h>
63ddf7fe95Scasper #include <sys/klpd.h>
64da6c28aaSamw #include <sys/varargs.h>
65f48205beScasper 
66bda89588Sjp151216 
67bda89588Sjp151216 /* Ephemeral IDs Zones specific data */
68bda89588Sjp151216 typedef struct ephemeral_zsd {
69bda89588Sjp151216 	uid_t		min_uid;
70bda89588Sjp151216 	uid_t		last_uid;
71bda89588Sjp151216 	gid_t		min_gid;
72bda89588Sjp151216 	gid_t		last_gid;
73f48205beScasper 	kmutex_t	eph_lock;
74bda89588Sjp151216 	cred_t		*eph_nobody;
75bda89588Sjp151216 } ephemeral_zsd_t;
76bda89588Sjp151216 
77bda89588Sjp151216 
78bda89588Sjp151216 static kmutex_t		ephemeral_zone_mutex;
79bda89588Sjp151216 static zone_key_t	ephemeral_zone_key;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static struct kmem_cache *cred_cache;
827c478bd9Sstevel@tonic-gate static size_t		crsize = 0;
837c478bd9Sstevel@tonic-gate static int		audoff = 0;
847c478bd9Sstevel@tonic-gate uint32_t		ucredsize;
857c478bd9Sstevel@tonic-gate cred_t			*kcred;
8645916cd2Sjpk static cred_t		*dummycr;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int get_c2audit_load(void);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
937c478bd9Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
947c478bd9Sstevel@tonic-gate 
9545916cd2Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
967c478bd9Sstevel@tonic-gate 
97f48205beScasper 
98f48205beScasper static boolean_t hasephids = B_FALSE;
99f48205beScasper 
100bda89588Sjp151216 static ephemeral_zsd_t *
101bda89588Sjp151216 get_ephemeral_zsd(zone_t *zone)
102bda89588Sjp151216 {
103bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
104bda89588Sjp151216 
105bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
106bda89588Sjp151216 	if (eph_zsd != NULL) {
107bda89588Sjp151216 		return (eph_zsd);
108bda89588Sjp151216 	}
109bda89588Sjp151216 
110bda89588Sjp151216 	mutex_enter(&ephemeral_zone_mutex);
111bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
112bda89588Sjp151216 	if (eph_zsd == NULL) {
113bda89588Sjp151216 		eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP);
114bda89588Sjp151216 		eph_zsd->min_uid = MAXUID;
115bda89588Sjp151216 		eph_zsd->last_uid = IDMAP_WK__MAX_UID;
116bda89588Sjp151216 		eph_zsd->min_gid = MAXUID;
117bda89588Sjp151216 		eph_zsd->last_gid = IDMAP_WK__MAX_GID;
118bda89588Sjp151216 		mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL);
119bda89588Sjp151216 
120bda89588Sjp151216 		/*
121bda89588Sjp151216 		 * nobody is used to map SID containing CRs.
122bda89588Sjp151216 		 */
123bda89588Sjp151216 		eph_zsd->eph_nobody = crdup(zone->zone_kcred);
124bda89588Sjp151216 		(void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY);
125bda89588Sjp151216 		CR_FLAGS(eph_zsd->eph_nobody) = 0;
126bda89588Sjp151216 		eph_zsd->eph_nobody->cr_zone = zone;
127bda89588Sjp151216 
128bda89588Sjp151216 		(void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd);
129bda89588Sjp151216 	}
130bda89588Sjp151216 	mutex_exit(&ephemeral_zone_mutex);
131bda89588Sjp151216 	return (eph_zsd);
132bda89588Sjp151216 }
133bda89588Sjp151216 
134*5f9878b0Sken Powell - Sun Microsystem static cred_t *crdup_flags(const cred_t *, int);
135ddf7fe95Scasper static cred_t *cralloc_flags(int);
136ddf7fe95Scasper 
137bda89588Sjp151216 /*
138bda89588Sjp151216  * This function is called when a zone is destroyed
139bda89588Sjp151216  */
140bda89588Sjp151216 static void
141bda89588Sjp151216 /* ARGSUSED */
142bda89588Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg)
143bda89588Sjp151216 {
144bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = arg;
145bda89588Sjp151216 	if (eph_zsd != NULL) {
146bda89588Sjp151216 		mutex_destroy(&eph_zsd->eph_lock);
147bda89588Sjp151216 		crfree(eph_zsd->eph_nobody);
148bda89588Sjp151216 		kmem_free(eph_zsd, sizeof (ephemeral_zsd_t));
149bda89588Sjp151216 	}
150bda89588Sjp151216 }
151bda89588Sjp151216 
152bda89588Sjp151216 
153bda89588Sjp151216 
154f48205beScasper /*
1557c478bd9Sstevel@tonic-gate  * Initialize credentials data structures.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate void
1597c478bd9Sstevel@tonic-gate cred_init(void)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	priv_init();
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1);
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Make sure it's word-aligned.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
1707c478bd9Sstevel@tonic-gate #ifdef _LP64
1717c478bd9Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
1727c478bd9Sstevel@tonic-gate 		audoff = (crsize +
1737c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
1747c478bd9Sstevel@tonic-gate #else	/* _LP64 */
1757c478bd9Sstevel@tonic-gate 		audoff = crsize;
1767c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
1777c478bd9Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1787c478bd9Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1827c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
18545916cd2Sjpk 	 * dummycr is used to copy initial state for creds.
18645916cd2Sjpk 	 */
18745916cd2Sjpk 	dummycr = cralloc();
18845916cd2Sjpk 	bzero(dummycr, crsize);
18945916cd2Sjpk 	dummycr->cr_ref = 1;
190f48205beScasper 	dummycr->cr_uid = (uid_t)-1;
191f48205beScasper 	dummycr->cr_gid = (gid_t)-1;
192f48205beScasper 	dummycr->cr_ruid = (uid_t)-1;
193f48205beScasper 	dummycr->cr_rgid = (gid_t)-1;
194f48205beScasper 	dummycr->cr_suid = (uid_t)-1;
195f48205beScasper 	dummycr->cr_sgid = (gid_t)-1;
196f48205beScasper 
19745916cd2Sjpk 
19845916cd2Sjpk 	/*
1997c478bd9Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
2007c478bd9Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
2017c478bd9Sstevel@tonic-gate 	 * sets filled in.
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	kcred = cralloc();
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	bzero(kcred, crsize);
2067c478bd9Sstevel@tonic-gate 	kcred->cr_ref = 1;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
2097c478bd9Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
2127c478bd9Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
2157c478bd9Sstevel@tonic-gate 	if (!rstchown)
2167c478bd9Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
2197c478bd9Sstevel@tonic-gate 	if (rstlink)
2207c478bd9Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
22345916cd2Sjpk 
22445916cd2Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Set up credentials of p0.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
2307c478bd9Sstevel@tonic-gate 	curthread->t_cred = kcred;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	ucredsize = UCRED_SIZE;
233bda89588Sjp151216 
234bda89588Sjp151216 	mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL);
235bda89588Sjp151216 	zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
2407c478bd9Sstevel@tonic-gate  */
241ddf7fe95Scasper static cred_t *
242ddf7fe95Scasper cralloc_flags(int flgs)
2437c478bd9Sstevel@tonic-gate {
244ddf7fe95Scasper 	cred_t *cr = kmem_cache_alloc(cred_cache, flgs);
245ddf7fe95Scasper 
246ddf7fe95Scasper 	if (cr == NULL)
247ddf7fe95Scasper 		return (NULL);
248ddf7fe95Scasper 
2497c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
2507c478bd9Sstevel@tonic-gate 	cr->cr_zone = NULL;
25145916cd2Sjpk 	cr->cr_label = NULL;
252f48205beScasper 	cr->cr_ksid = NULL;
253ddf7fe95Scasper 	cr->cr_klpd = NULL;
254f48205beScasper 	return (cr);
255f48205beScasper }
256f48205beScasper 
257ddf7fe95Scasper cred_t *
258ddf7fe95Scasper cralloc(void)
259ddf7fe95Scasper {
260ddf7fe95Scasper 	return (cralloc_flags(KM_SLEEP));
261ddf7fe95Scasper }
262ddf7fe95Scasper 
263f48205beScasper /*
264f48205beScasper  * As cralloc but prepared for ksid change (if appropriate).
265f48205beScasper  */
266f48205beScasper cred_t *
267f48205beScasper cralloc_ksid(void)
268f48205beScasper {
269f48205beScasper 	cred_t *cr = cralloc();
270f48205beScasper 	if (hasephids)
271f48205beScasper 		cr->cr_ksid = kcrsid_alloc();
2727c478bd9Sstevel@tonic-gate 	return (cr);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
2777c478bd9Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate cred_t *
2807c478bd9Sstevel@tonic-gate crget(void)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
2857c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;
2867c478bd9Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
28745916cd2Sjpk 	if (cr->cr_label)
28845916cd2Sjpk 		label_hold(cr->cr_label);
289ddf7fe95Scasper 	ASSERT(cr->cr_klpd == NULL);
2907c478bd9Sstevel@tonic-gate 	return (cr);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
2957c478bd9Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
2967c478bd9Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
2977c478bd9Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
3007c478bd9Sstevel@tonic-gate  * and p->p_cred set.
3017c478bd9Sstevel@tonic-gate  *
3027c478bd9Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate void
3057c478bd9Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
3087c478bd9Sstevel@tonic-gate 	kthread_id_t	first;
3097c478bd9Sstevel@tonic-gate 	cred_t *oldcr;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
3157c478bd9Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	t = curthread;
3187c478bd9Sstevel@tonic-gate 	oldcr = t->t_cred;
3197c478bd9Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
3207c478bd9Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
3267c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
3277c478bd9Sstevel@tonic-gate 		first = curthread;
3287c478bd9Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
3297c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
3307c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * Put a hold on a cred structure.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate void
3387c478bd9Sstevel@tonic-gate crhold(cred_t *cr)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	atomic_add_32(&cr->cr_ref, 1);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
34545916cd2Sjpk  * If cred uses label different from zone label, free it.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate void
3487c478bd9Sstevel@tonic-gate crfree(cred_t *cr)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) {
3517c478bd9Sstevel@tonic-gate 		ASSERT(cr != kcred);
35245916cd2Sjpk 		if (cr->cr_label)
35345916cd2Sjpk 			label_rele(cr->cr_label);
354ddf7fe95Scasper 		if (cr->cr_klpd)
355ddf7fe95Scasper 			crklpd_rele(cr->cr_klpd);
3567c478bd9Sstevel@tonic-gate 		if (cr->cr_zone)
3577c478bd9Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
358f48205beScasper 		if (cr->cr_ksid)
359f48205beScasper 			kcrsid_rele(cr->cr_ksid);
3607c478bd9Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3667c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3677c478bd9Sstevel@tonic-gate  * 	and one for the thread.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate cred_t *
3707c478bd9Sstevel@tonic-gate crcopy(cred_t *cr)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	cred_t *newcr;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	newcr = cralloc();
3757c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3767c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3777c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
37845916cd2Sjpk 	if (newcr->cr_label)
379ddf7fe95Scasper 		label_hold(newcr->cr_label);
380f48205beScasper 	if (newcr->cr_ksid)
381ddf7fe95Scasper 		kcrsid_hold(newcr->cr_ksid);
382ddf7fe95Scasper 	if (newcr->cr_klpd)
383ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
3847c478bd9Sstevel@tonic-gate 	crfree(cr);
3857c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3867c478bd9Sstevel@tonic-gate 	return (newcr);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3917c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3927c478bd9Sstevel@tonic-gate  * 	and one for the thread.
3937c478bd9Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
3947c478bd9Sstevel@tonic-gate  * "new" cred.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate void
3977c478bd9Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
3987c478bd9Sstevel@tonic-gate {
399f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
400f48205beScasper 
4017c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4027c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4037c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
40445916cd2Sjpk 	if (newcr->cr_label)
40545916cd2Sjpk 		label_hold(newcr->cr_label);
406ddf7fe95Scasper 	if (newcr->cr_klpd)
407ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
408f48205beScasper 	if (nkcr) {
409f48205beScasper 		newcr->cr_ksid = nkcr;
410f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
411f48205beScasper 	} else if (newcr->cr_ksid)
412f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4137c478bd9Sstevel@tonic-gate 	crfree(oldcr);
4147c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4197c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4207c478bd9Sstevel@tonic-gate  */
421ddf7fe95Scasper static cred_t *
422*5f9878b0Sken Powell - Sun Microsystem crdup_flags(const cred_t *cr, int flgs)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	cred_t *newcr;
4257c478bd9Sstevel@tonic-gate 
426ddf7fe95Scasper 	newcr = cralloc_flags(flgs);
427ddf7fe95Scasper 
428ddf7fe95Scasper 	if (newcr == NULL)
429ddf7fe95Scasper 		return (NULL);
430ddf7fe95Scasper 
4317c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
4327c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4337c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
43445916cd2Sjpk 	if (newcr->cr_label)
43545916cd2Sjpk 		label_hold(newcr->cr_label);
436ddf7fe95Scasper 	if (newcr->cr_klpd)
437ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
438f48205beScasper 	if (newcr->cr_ksid)
439f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4407c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4417c478bd9Sstevel@tonic-gate 	return (newcr);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
444ddf7fe95Scasper cred_t *
445ddf7fe95Scasper crdup(cred_t *cr)
446ddf7fe95Scasper {
447ddf7fe95Scasper 	return (crdup_flags(cr, KM_SLEEP));
448ddf7fe95Scasper }
449ddf7fe95Scasper 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4527c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4537c478bd9Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
4547c478bd9Sstevel@tonic-gate  * "new" cred.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate void
4577c478bd9Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
4587c478bd9Sstevel@tonic-gate {
459f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
460f48205beScasper 
4617c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4627c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4637c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
46445916cd2Sjpk 	if (newcr->cr_label)
46545916cd2Sjpk 		label_hold(newcr->cr_label);
466ddf7fe95Scasper 	if (newcr->cr_klpd)
467ddf7fe95Scasper 		crklpd_hold(newcr->cr_klpd);
468f48205beScasper 	if (nkcr) {
469f48205beScasper 		newcr->cr_ksid = nkcr;
470f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
471f48205beScasper 	} else if (newcr->cr_ksid)
472f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4737c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
4787c478bd9Sstevel@tonic-gate  */
4797c478bd9Sstevel@tonic-gate cred_t *
48045916cd2Sjpk crgetcred(void)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	cred_t *cr;
4837c478bd9Sstevel@tonic-gate 	proc_t *p;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
4867c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
4877c478bd9Sstevel@tonic-gate 	crhold(cr = p->p_cred);
4887c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
4897c478bd9Sstevel@tonic-gate 	return (cr);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * Backward compatibility check for suser().
4947c478bd9Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
4957c478bd9Sstevel@tonic-gate  * done through use of privilege in the audit trail.
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate int
4987c478bd9Sstevel@tonic-gate suser(cred_t *cr)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
5017c478bd9Sstevel@tonic-gate 	    == 0);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
5067c478bd9Sstevel@tonic-gate  * described by the supplied credentials.
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate int
5097c478bd9Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	if (gid == cr->cr_gid)
5127c478bd9Sstevel@tonic-gate 		return (1);
5137c478bd9Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
5187c478bd9Sstevel@tonic-gate  */
5197c478bd9Sstevel@tonic-gate int
5207c478bd9Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	const gid_t *gp, *endgp;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	endgp = &cr->cr_groups[cr->cr_ngroups];
5257c478bd9Sstevel@tonic-gate 	for (gp = cr->cr_groups; gp < endgp; gp++)
5267c478bd9Sstevel@tonic-gate 		if (*gp == gid)
5277c478bd9Sstevel@tonic-gate 			return (1);
5287c478bd9Sstevel@tonic-gate 	return (0);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * This function is called to check whether the credentials set
5337c478bd9Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
5347c478bd9Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
5357c478bd9Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
5367c478bd9Sstevel@tonic-gate  *
5377c478bd9Sstevel@tonic-gate  * The rules are:
5387c478bd9Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
5397c478bd9Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
5407c478bd9Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
5417c478bd9Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
5427c478bd9Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
5437c478bd9Sstevel@tonic-gate  *     succeeds
5447c478bd9Sstevel@tonic-gate  * (4) otherwise, the check fails
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate int
5477c478bd9Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	if (scrp == tcrp)
5507c478bd9Sstevel@tonic-gate 		return (1);
5517c478bd9Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
5527c478bd9Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
5537c478bd9Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
5547c478bd9Sstevel@tonic-gate 		return (0);
5557c478bd9Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
5567c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
5577c478bd9Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
5587c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
5597c478bd9Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
5607c478bd9Sstevel@tonic-gate 		return (1);
5617c478bd9Sstevel@tonic-gate 	return (0);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
5667c478bd9Sstevel@tonic-gate  * additionally returns success if the proc_t's match
5677c478bd9Sstevel@tonic-gate  * It is the preferred interface for most uses.
568ddf7fe95Scasper  * And it will acquire p_crlock itself, so it assert's that it shouldn't
5697c478bd9Sstevel@tonic-gate  * be held.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate int
5727c478bd9Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	int rets;
5757c478bd9Sstevel@tonic-gate 	cred_t *tcrp;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (tp == sp)
5807c478bd9Sstevel@tonic-gate 		return (1);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
5837c478bd9Sstevel@tonic-gate 		return (0);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
586ddf7fe95Scasper 	crhold(tcrp = tp->p_cred);
5877c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
588ddf7fe95Scasper 	rets = hasprocperm(tcrp, scrp);
589ddf7fe95Scasper 	crfree(tcrp);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	return (rets);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
5967c478bd9Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
5977c478bd9Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
5987c478bd9Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
5997c478bd9Sstevel@tonic-gate  *
6007c478bd9Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
6017c478bd9Sstevel@tonic-gate  * 1 if they do not.
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate int
6047c478bd9Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (cr1 == cr2)
6087c478bd9Sstevel@tonic-gate 		return (0);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
6117c478bd9Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
6127c478bd9Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
6137c478bd9Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
6147c478bd9Sstevel@tonic-gate 	    cr1->cr_ngroups == cr2->cr_ngroups &&
6157c478bd9Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
6167c478bd9Sstevel@tonic-gate 	    bcmp(cr1->cr_groups, cr2->cr_groups,
6177c478bd9Sstevel@tonic-gate 	    cr1->cr_ngroups * sizeof (gid_t)) == 0) {
6187c478bd9Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	return (1);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * Read access functions to cred_t.
6257c478bd9Sstevel@tonic-gate  */
6267c478bd9Sstevel@tonic-gate uid_t
6277c478bd9Sstevel@tonic-gate crgetuid(const cred_t *cr)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	return (cr->cr_uid);
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate uid_t
6337c478bd9Sstevel@tonic-gate crgetruid(const cred_t *cr)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	return (cr->cr_ruid);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate uid_t
6397c478bd9Sstevel@tonic-gate crgetsuid(const cred_t *cr)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	return (cr->cr_suid);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate gid_t
6457c478bd9Sstevel@tonic-gate crgetgid(const cred_t *cr)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	return (cr->cr_gid);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate gid_t
6517c478bd9Sstevel@tonic-gate crgetrgid(const cred_t *cr)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	return (cr->cr_rgid);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate gid_t
6577c478bd9Sstevel@tonic-gate crgetsgid(const cred_t *cr)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	return (cr->cr_sgid);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate const auditinfo_addr_t *
6637c478bd9Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate auditinfo_addr_t *
6697c478bd9Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	return (CR_AUINFO(cr));
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate zoneid_t
6757c478bd9Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
6767c478bd9Sstevel@tonic-gate {
67745916cd2Sjpk 	return (cr->cr_zone == NULL ?
67845916cd2Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
67945916cd2Sjpk 	    cr->cr_zone->zone_id);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate projid_t
6837c478bd9Sstevel@tonic-gate crgetprojid(const cred_t *cr)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	return (cr->cr_projid);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
68845916cd2Sjpk zone_t *
68945916cd2Sjpk crgetzone(const cred_t *cr)
69045916cd2Sjpk {
69145916cd2Sjpk 	return (cr->cr_zone);
69245916cd2Sjpk }
69345916cd2Sjpk 
69445916cd2Sjpk struct ts_label_s *
69545916cd2Sjpk crgetlabel(const cred_t *cr)
69645916cd2Sjpk {
69745916cd2Sjpk 	return (cr->cr_label ?
69845916cd2Sjpk 	    cr->cr_label :
69945916cd2Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
70045916cd2Sjpk }
70145916cd2Sjpk 
70245916cd2Sjpk boolean_t
70345916cd2Sjpk crisremote(const cred_t *cr)
70445916cd2Sjpk {
70545916cd2Sjpk 	return (REMOTE_PEER_CRED(cr));
70645916cd2Sjpk }
70745916cd2Sjpk 
708bda89588Sjp151216 #define	BADUID(x, zn)	((x) != -1 && !VALID_UID((x), (zn)))
709bda89588Sjp151216 #define	BADGID(x, zn)	((x) != -1 && !VALID_GID((x), (zn)))
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate int
7127c478bd9Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
7137c478bd9Sstevel@tonic-gate {
714bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
715bda89588Sjp151216 
7167c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7177c478bd9Sstevel@tonic-gate 
718bda89588Sjp151216 	if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone))
7197c478bd9Sstevel@tonic-gate 		return (-1);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (r != -1)
7227c478bd9Sstevel@tonic-gate 		cr->cr_ruid = r;
7237c478bd9Sstevel@tonic-gate 	if (e != -1)
7247c478bd9Sstevel@tonic-gate 		cr->cr_uid = e;
7257c478bd9Sstevel@tonic-gate 	if (s != -1)
7267c478bd9Sstevel@tonic-gate 		cr->cr_suid = s;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	return (0);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate int
7327c478bd9Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
7337c478bd9Sstevel@tonic-gate {
734bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
735bda89588Sjp151216 
7367c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7377c478bd9Sstevel@tonic-gate 
738bda89588Sjp151216 	if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone))
7397c478bd9Sstevel@tonic-gate 		return (-1);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (r != -1)
7427c478bd9Sstevel@tonic-gate 		cr->cr_rgid = r;
7437c478bd9Sstevel@tonic-gate 	if (e != -1)
7447c478bd9Sstevel@tonic-gate 		cr->cr_gid = e;
7457c478bd9Sstevel@tonic-gate 	if (s != -1)
7467c478bd9Sstevel@tonic-gate 		cr->cr_sgid = s;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	return (0);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate int
7527c478bd9Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
7537c478bd9Sstevel@tonic-gate {
754bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
755bda89588Sjp151216 
7567c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7577c478bd9Sstevel@tonic-gate 
758bda89588Sjp151216 	if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone))
7597c478bd9Sstevel@tonic-gate 		return (-1);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
7627c478bd9Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	return (0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate int
7687c478bd9Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
7737c478bd9Sstevel@tonic-gate 		return (-1);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	cr->cr_ngroups = n;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	if (n > 0)
7787c478bd9Sstevel@tonic-gate 		bcopy(grp, cr->cr_groups, n * sizeof (gid_t));
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate void
7847c478bd9Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
7877c478bd9Sstevel@tonic-gate 	cr->cr_projid = projid;
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * This routine returns the pointer to the first element of the cr_groups
7927c478bd9Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate const gid_t *
7957c478bd9Sstevel@tonic-gate crgetgroups(const cred_t *cr)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	return (cr->cr_groups);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate int
8017c478bd9Sstevel@tonic-gate crgetngroups(const cred_t *cr)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	return (cr->cr_ngroups);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate void
8077c478bd9Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
8107c478bd9Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
8117c478bd9Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
8127c478bd9Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
8137c478bd9Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
8147c478bd9Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
8157c478bd9Sstevel@tonic-gate 	pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max);
8167c478bd9Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0;	/* in case ngroups == 0 */
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
8197c478bd9Sstevel@tonic-gate 		bcopy(cr->cr_groups, pcrp->pr_groups,
8207c478bd9Sstevel@tonic-gate 		    sizeof (gid_t) * cr->cr_ngroups);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate static int
82445916cd2Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
8277c478bd9Sstevel@tonic-gate 	au_tid_addr_t	tid;
8287c478bd9Sstevel@tonic-gate 
82945916cd2Sjpk 	if (secpolicy_audit_getattr(rcr) != 0)
8307c478bd9Sstevel@tonic-gate 		return (-1);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
8337c478bd9Sstevel@tonic-gate 	tid = ai->ai_termid;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
8367c478bd9Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
8377c478bd9Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
8407c478bd9Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
8437c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	return (0);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
84845916cd2Sjpk void
84945916cd2Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
85045916cd2Sjpk {
85145916cd2Sjpk 	ts_label_t	*tslp;
85245916cd2Sjpk 
85345916cd2Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
85445916cd2Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
85545916cd2Sjpk }
85645916cd2Sjpk 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
8597c478bd9Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
8607c478bd9Sstevel@tonic-gate  * memory and copy it twice.
86145916cd2Sjpk  *
86245916cd2Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
86345916cd2Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
86445916cd2Sjpk  * to determine whether audit info should be included.
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate struct ucred_s *
86745916cd2Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
8727c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
8737c478bd9Sstevel@tonic-gate 		uc = kmem_zalloc(ucredsize, KM_SLEEP);
8747c478bd9Sstevel@tonic-gate 	} else {
8757c478bd9Sstevel@tonic-gate 		bzero(buf, ucredsize);
8767c478bd9Sstevel@tonic-gate 		uc = buf;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	uc->uc_size = ucredsize;
8797c478bd9Sstevel@tonic-gate 	uc->uc_credoff = UCRED_CRED_OFF;
8807c478bd9Sstevel@tonic-gate 	uc->uc_privoff = UCRED_PRIV_OFF;
8817c478bd9Sstevel@tonic-gate 	uc->uc_audoff = UCRED_AUD_OFF;
88245916cd2Sjpk 	uc->uc_labeloff = UCRED_LABEL_OFF;
8837c478bd9Sstevel@tonic-gate 	uc->uc_pid = pid;
8847c478bd9Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
8857c478bd9Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
8867c478bd9Sstevel@tonic-gate 
88745916cd2Sjpk 	/*
88845916cd2Sjpk 	 * Note that cred2uclabel() call should not be factored out
88945916cd2Sjpk 	 * to the bottom of the if-else. UCXXX() macros depend on
89045916cd2Sjpk 	 * uc_xxxoff values to work correctly.
89145916cd2Sjpk 	 */
89245916cd2Sjpk 	if (REMOTE_PEER_CRED(cr)) {
89345916cd2Sjpk 		/*
89445916cd2Sjpk 		 * other than label, the rest of cred info about a
89545916cd2Sjpk 		 * remote peer isn't available.
89645916cd2Sjpk 		 */
89745916cd2Sjpk 		cred2uclabel(cr, UCLABEL(uc));
89845916cd2Sjpk 		uc->uc_credoff = 0;
89945916cd2Sjpk 		uc->uc_privoff = 0;
90045916cd2Sjpk 		uc->uc_audoff = 0;
90145916cd2Sjpk 	} else {
9027c478bd9Sstevel@tonic-gate 		cred2prcred(cr, UCCRED(uc));
9037c478bd9Sstevel@tonic-gate 		cred2prpriv(cr, UCPRIV(uc));
90445916cd2Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
9057c478bd9Sstevel@tonic-gate 			uc->uc_audoff = 0;
90645916cd2Sjpk 		cred2uclabel(cr, UCLABEL(uc));
90745916cd2Sjpk 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	return (uc);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate  * Get the "ucred" of a process.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate struct ucred_s *
9167c478bd9Sstevel@tonic-gate pgetucred(proc_t *p)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	cred_t *cr;
9197c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
9227c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
9237c478bd9Sstevel@tonic-gate 	crhold(cr);
9247c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
9257c478bd9Sstevel@tonic-gate 
92645916cd2Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
9277c478bd9Sstevel@tonic-gate 	crfree(cr);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	return (uc);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
9347c478bd9Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
9357c478bd9Sstevel@tonic-gate  * make that the uid instead and retry the call.
9367c478bd9Sstevel@tonic-gate  * Private interface for NFS.
9377c478bd9Sstevel@tonic-gate  */
9387c478bd9Sstevel@tonic-gate cred_t *
9397c478bd9Sstevel@tonic-gate crnetadjust(cred_t *cr)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
9427c478bd9Sstevel@tonic-gate 		cr = crdup(cr);
9437c478bd9Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
9447c478bd9Sstevel@tonic-gate 		return (cr);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 	return (NULL);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * The reference count is of interest when you want to check
9517c478bd9Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate uint_t
9547c478bd9Sstevel@tonic-gate crgetref(const cred_t *cr)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	return (cr->cr_ref);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate static int
9607c478bd9Sstevel@tonic-gate get_c2audit_load(void)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	static int	gotit = 0;
9637c478bd9Sstevel@tonic-gate 	static int	c2audit_load;
9647c478bd9Sstevel@tonic-gate 	u_longlong_t	audit_load_val;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (gotit)
9677c478bd9Sstevel@tonic-gate 		return (c2audit_load);
9687c478bd9Sstevel@tonic-gate 	audit_load_val = 0;		/* set default value once */
9697c478bd9Sstevel@tonic-gate 	(void) mod_sysvar("c2audit", "audit_load", &audit_load_val);
9707c478bd9Sstevel@tonic-gate 	c2audit_load = (int)audit_load_val;
9717c478bd9Sstevel@tonic-gate 	gotit++;
9727c478bd9Sstevel@tonic-gate 	return (c2audit_load);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate int
9767c478bd9Sstevel@tonic-gate get_audit_ucrsize(void)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
9837c478bd9Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
9847c478bd9Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
9857c478bd9Sstevel@tonic-gate  * same.
9867c478bd9Sstevel@tonic-gate  */
9877c478bd9Sstevel@tonic-gate void
9887c478bd9Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	ASSERT(cr != kcred);
9937c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
9947c478bd9Sstevel@tonic-gate 	cr->cr_zone = zptr;
9957c478bd9Sstevel@tonic-gate 	zone_cred_hold(zptr);
9967c478bd9Sstevel@tonic-gate 	if (oldzptr)
9977c478bd9Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
9987c478bd9Sstevel@tonic-gate }
99945916cd2Sjpk 
100045916cd2Sjpk /*
100145916cd2Sjpk  * Create a new cred based on the supplied label
100245916cd2Sjpk  */
100345916cd2Sjpk cred_t *
100445916cd2Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
100545916cd2Sjpk {
100645916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
100745916cd2Sjpk 	cred_t *cr = NULL;
100845916cd2Sjpk 
100945916cd2Sjpk 	if (lbl != NULL) {
1010ddf7fe95Scasper 		if ((cr = crdup_flags(dummycr, flags)) != NULL) {
101145916cd2Sjpk 			cr->cr_label = lbl;
101245916cd2Sjpk 		} else {
101345916cd2Sjpk 			label_rele(lbl);
101445916cd2Sjpk 		}
101545916cd2Sjpk 	}
101645916cd2Sjpk 
101745916cd2Sjpk 	return (cr);
101845916cd2Sjpk }
101945916cd2Sjpk 
102045916cd2Sjpk /*
102145916cd2Sjpk  * Derive a new cred from the existing cred, but with a different label.
102245916cd2Sjpk  * To be used when a cred is being shared, but the label needs to be changed
102345916cd2Sjpk  * by a caller without affecting other users
102445916cd2Sjpk  */
102545916cd2Sjpk cred_t *
1026*5f9878b0Sken Powell - Sun Microsystem copycred_from_tslabel(const cred_t *cr, ts_label_t *label, int flags)
1027*5f9878b0Sken Powell - Sun Microsystem {
1028*5f9878b0Sken Powell - Sun Microsystem 	cred_t *newcr = NULL;
1029*5f9878b0Sken Powell - Sun Microsystem 
1030*5f9878b0Sken Powell - Sun Microsystem 	if ((newcr = crdup_flags(cr, flags)) != NULL) {
1031*5f9878b0Sken Powell - Sun Microsystem 		if (newcr->cr_label != NULL)
1032*5f9878b0Sken Powell - Sun Microsystem 			label_rele(newcr->cr_label);
1033*5f9878b0Sken Powell - Sun Microsystem 		label_hold(label);
1034*5f9878b0Sken Powell - Sun Microsystem 		newcr->cr_label = label;
1035*5f9878b0Sken Powell - Sun Microsystem 	}
1036*5f9878b0Sken Powell - Sun Microsystem 
1037*5f9878b0Sken Powell - Sun Microsystem 	return (newcr);
1038*5f9878b0Sken Powell - Sun Microsystem }
1039*5f9878b0Sken Powell - Sun Microsystem 
1040*5f9878b0Sken Powell - Sun Microsystem /*
1041*5f9878b0Sken Powell - Sun Microsystem  * Derive a new cred from the existing cred, but with a different label.
1042*5f9878b0Sken Powell - Sun Microsystem  */
1043*5f9878b0Sken Powell - Sun Microsystem cred_t *
1044*5f9878b0Sken Powell - Sun Microsystem copycred_from_bslabel(const cred_t *cr, bslabel_t *blabel,
1045*5f9878b0Sken Powell - Sun Microsystem     uint32_t doi, int flags)
104645916cd2Sjpk {
104745916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
104845916cd2Sjpk 	cred_t  *newcr = NULL;
104945916cd2Sjpk 
105045916cd2Sjpk 	if (lbl != NULL) {
1051*5f9878b0Sken Powell - Sun Microsystem 		newcr = copycred_from_tslabel(cr, lbl, flags);
105245916cd2Sjpk 		label_rele(lbl);
105345916cd2Sjpk 	}
105445916cd2Sjpk 
105545916cd2Sjpk 	return (newcr);
105645916cd2Sjpk }
105745916cd2Sjpk 
105845916cd2Sjpk /*
105945916cd2Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
106045916cd2Sjpk  */
106145916cd2Sjpk cred_t *
106245916cd2Sjpk zone_kcred(void)
106345916cd2Sjpk {
106445916cd2Sjpk 	zone_t *zone;
106545916cd2Sjpk 
106645916cd2Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
106745916cd2Sjpk 		return (zone->zone_kcred);
106845916cd2Sjpk 	else
106945916cd2Sjpk 		return (kcred);
107045916cd2Sjpk }
1071f48205beScasper 
1072f48205beScasper boolean_t
1073bda89588Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id)
1074f48205beScasper {
1075bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
10764edd44c5Sjp151216 	if (id <= IDMAP_WK__MAX_UID)
1077bda89588Sjp151216 		return (B_TRUE);
1078bda89588Sjp151216 
1079bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1080bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1081f48205beScasper 	membar_consumer();
1082bda89588Sjp151216 	return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid);
1083f48205beScasper }
1084f48205beScasper 
1085f48205beScasper boolean_t
1086bda89588Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id)
1087f48205beScasper {
1088bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
10894edd44c5Sjp151216 	if (id <= IDMAP_WK__MAX_GID)
1090bda89588Sjp151216 		return (B_TRUE);
1091bda89588Sjp151216 
1092bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1093bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1094f48205beScasper 	membar_consumer();
1095bda89588Sjp151216 	return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid);
1096f48205beScasper }
1097f48205beScasper 
1098f48205beScasper int
1099bda89588Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count)
1100f48205beScasper {
1101bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1102bda89588Sjp151216 
1103bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1104bda89588Sjp151216 
1105bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1106f48205beScasper 
1107f48205beScasper 	/* Test for unsigned integer wrap around */
1108bda89588Sjp151216 	if (eph_zsd->last_uid + count < eph_zsd->last_uid) {
1109bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1110f48205beScasper 		return (-1);
1111f48205beScasper 	}
1112f48205beScasper 
1113f48205beScasper 	/* first call or idmap crashed and state corrupted */
1114f48205beScasper 	if (flags != 0)
1115bda89588Sjp151216 		eph_zsd->min_uid = eph_zsd->last_uid;
1116f48205beScasper 
1117f48205beScasper 	hasephids = B_TRUE;
1118bda89588Sjp151216 	*start = eph_zsd->last_uid + 1;
1119bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_uid, count);
1120bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1121f48205beScasper 	return (0);
1122f48205beScasper }
1123f48205beScasper 
1124f48205beScasper int
1125bda89588Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count)
1126f48205beScasper {
1127bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1128bda89588Sjp151216 
1129bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1130bda89588Sjp151216 
1131bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1132f48205beScasper 
1133f48205beScasper 	/* Test for unsigned integer wrap around */
1134bda89588Sjp151216 	if (eph_zsd->last_gid + count < eph_zsd->last_gid) {
1135bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1136f48205beScasper 		return (-1);
1137f48205beScasper 	}
1138f48205beScasper 
1139f48205beScasper 	/* first call or idmap crashed and state corrupted */
1140f48205beScasper 	if (flags != 0)
1141bda89588Sjp151216 		eph_zsd->min_gid = eph_zsd->last_gid;
1142f48205beScasper 
1143f48205beScasper 	hasephids = B_TRUE;
1144bda89588Sjp151216 	*start = eph_zsd->last_gid + 1;
1145bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_gid, count);
1146bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1147f48205beScasper 	return (0);
1148f48205beScasper }
1149f48205beScasper 
1150f48205beScasper /*
1151bda89588Sjp151216  * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data()
1152bda89588Sjp151216  * are project private functions that are for use of the test system only and
1153bda89588Sjp151216  * are not to be used for other purposes.
1154bda89588Sjp151216  */
1155bda89588Sjp151216 
1156bda89588Sjp151216 void
1157bda89588Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid,
1158bda89588Sjp151216 	gid_t *min_gid, gid_t *last_gid)
1159bda89588Sjp151216 {
1160bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1161bda89588Sjp151216 
1162bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1163bda89588Sjp151216 
1164bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1165bda89588Sjp151216 
1166bda89588Sjp151216 	*min_uid = eph_zsd->min_uid;
1167bda89588Sjp151216 	*last_uid = eph_zsd->last_uid;
1168bda89588Sjp151216 	*min_gid = eph_zsd->min_gid;
1169bda89588Sjp151216 	*last_gid = eph_zsd->last_gid;
1170bda89588Sjp151216 
1171bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1172bda89588Sjp151216 }
1173bda89588Sjp151216 
1174bda89588Sjp151216 
1175bda89588Sjp151216 void
1176bda89588Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid,
1177bda89588Sjp151216 	gid_t min_gid, gid_t last_gid)
1178bda89588Sjp151216 {
1179bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1180bda89588Sjp151216 
1181bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1182bda89588Sjp151216 
1183bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1184bda89588Sjp151216 
1185bda89588Sjp151216 	if (min_uid != 0)
1186bda89588Sjp151216 		eph_zsd->min_uid = min_uid;
1187bda89588Sjp151216 	if (last_uid != 0)
1188bda89588Sjp151216 		eph_zsd->last_uid = last_uid;
1189bda89588Sjp151216 	if (min_gid != 0)
1190bda89588Sjp151216 		eph_zsd->min_gid = min_gid;
1191bda89588Sjp151216 	if (last_gid != 0)
1192bda89588Sjp151216 		eph_zsd->last_gid = last_gid;
1193bda89588Sjp151216 
1194bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1195bda89588Sjp151216 }
1196bda89588Sjp151216 
1197bda89588Sjp151216 /*
1198da6c28aaSamw  * If the credential user SID or group SID is mapped to an ephemeral
1199da6c28aaSamw  * ID, map the credential to nobody.
1200f48205beScasper  */
1201f48205beScasper cred_t *
1202f48205beScasper crgetmapped(const cred_t *cr)
1203f48205beScasper {
1204bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
120590c3d472Scasper 	/*
120690c3d472Scasper 	 * Someone incorrectly passed a NULL cred to a vnode operation
120790c3d472Scasper 	 * either on purpose or by calling CRED() in interrupt context.
120890c3d472Scasper 	 */
120990c3d472Scasper 	if (cr == NULL)
121090c3d472Scasper 		return (NULL);
121190c3d472Scasper 
1212f48205beScasper 	if (cr->cr_ksid != NULL) {
1213bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) {
1214bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1215bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1216bda89588Sjp151216 		}
1217f48205beScasper 
1218bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) {
1219bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1220bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1221bda89588Sjp151216 		}
1222f48205beScasper 	}
1223f48205beScasper 
1224f48205beScasper 	return ((cred_t *)cr);
1225f48205beScasper }
1226f48205beScasper 
1227f48205beScasper /* index should be in range for a ksidindex_t */
1228f48205beScasper void
1229f48205beScasper crsetsid(cred_t *cr, ksid_t *ksp, int index)
1230f48205beScasper {
1231f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1232f48205beScasper 	ASSERT(index >= 0 && index < KSID_COUNT);
1233f48205beScasper 	if (cr->cr_ksid == NULL && ksp == NULL)
1234f48205beScasper 		return;
1235f48205beScasper 	cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index);
1236f48205beScasper }
1237f48205beScasper 
1238f48205beScasper void
1239f48205beScasper crsetsidlist(cred_t *cr, ksidlist_t *ksl)
1240f48205beScasper {
1241f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1242f48205beScasper 	if (cr->cr_ksid == NULL && ksl == NULL)
1243f48205beScasper 		return;
1244f48205beScasper 	cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl);
1245f48205beScasper }
1246f48205beScasper 
1247f48205beScasper ksid_t *
1248f48205beScasper crgetsid(const cred_t *cr, int i)
1249f48205beScasper {
1250f48205beScasper 	ASSERT(i >= 0 && i < KSID_COUNT);
1251f48205beScasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain)
1252f48205beScasper 		return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]);
1253f48205beScasper 	return (NULL);
1254f48205beScasper }
1255f48205beScasper 
1256f48205beScasper ksidlist_t *
1257f48205beScasper crgetsidlist(const cred_t *cr)
1258f48205beScasper {
1259da6c28aaSamw 	if (cr->cr_ksid != NULL)
126028a151a0Scasper 		return (cr->cr_ksid->kr_sidlist);
1261f48205beScasper 	return (NULL);
1262f48205beScasper }
1263da6c28aaSamw 
1264da6c28aaSamw /*
1265da6c28aaSamw  * Interface to set the effective and permitted privileges for
1266da6c28aaSamw  * a credential; this interface does no security checks and is
1267da6c28aaSamw  * intended for kernel (file)servers creating credentials with
1268da6c28aaSamw  * specific privileges.
1269da6c28aaSamw  */
1270da6c28aaSamw int
1271da6c28aaSamw crsetpriv(cred_t *cr, ...)
1272da6c28aaSamw {
1273da6c28aaSamw 	va_list ap;
1274da6c28aaSamw 	const char *privnm;
1275da6c28aaSamw 
1276da6c28aaSamw 	ASSERT(cr->cr_ref <= 2);
1277da6c28aaSamw 
1278da6c28aaSamw 	priv_set_PA(cr);
1279da6c28aaSamw 
1280da6c28aaSamw 	va_start(ap, cr);
1281da6c28aaSamw 
1282da6c28aaSamw 	while ((privnm = va_arg(ap, const char *)) != NULL) {
1283da6c28aaSamw 		int priv = priv_getbyname(privnm, 0);
1284da6c28aaSamw 		if (priv < 0)
1285da6c28aaSamw 			return (-1);
1286da6c28aaSamw 
1287da6c28aaSamw 		priv_addset(&CR_PPRIV(cr), priv);
1288da6c28aaSamw 		priv_addset(&CR_EPRIV(cr), priv);
1289da6c28aaSamw 	}
1290da6c28aaSamw 	priv_adjust_PA(cr);
1291da6c28aaSamw 	va_end(ap);
1292da6c28aaSamw 	return (0);
1293da6c28aaSamw }
1294ddf7fe95Scasper 
1295546a3997SThomas Haynes /*
1296546a3997SThomas Haynes  * Interface to effectively set the PRIV_ALL for
1297546a3997SThomas Haynes  * a credential; this interface does no security checks and is
1298546a3997SThomas Haynes  * intended for kernel (file)servers to extend the user credentials
1299546a3997SThomas Haynes  * to be ALL, like either kcred or zcred.
1300546a3997SThomas Haynes  */
1301546a3997SThomas Haynes void
1302546a3997SThomas Haynes crset_zone_privall(cred_t *cr)
1303546a3997SThomas Haynes {
1304546a3997SThomas Haynes 	zone_t	*zone = crgetzone(cr);
1305546a3997SThomas Haynes 
1306546a3997SThomas Haynes 	priv_fillset(&CR_LPRIV(cr));
1307546a3997SThomas Haynes 	CR_EPRIV(cr) = CR_PPRIV(cr) = CR_IPRIV(cr) = CR_LPRIV(cr);
1308546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_LPRIV(cr));
1309546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_EPRIV(cr));
1310546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_IPRIV(cr));
1311546a3997SThomas Haynes 	priv_intersect(zone->zone_privset, &CR_PPRIV(cr));
1312546a3997SThomas Haynes }
1313546a3997SThomas Haynes 
1314ddf7fe95Scasper struct credklpd *
1315ddf7fe95Scasper crgetcrklpd(const cred_t *cr)
1316ddf7fe95Scasper {
1317ddf7fe95Scasper 	return (cr->cr_klpd);
1318ddf7fe95Scasper }
1319ddf7fe95Scasper 
1320ddf7fe95Scasper void
1321ddf7fe95Scasper crsetcrklpd(cred_t *cr, struct credklpd *crklpd)
1322ddf7fe95Scasper {
1323ddf7fe95Scasper 	ASSERT(cr->cr_ref <= 2);
1324ddf7fe95Scasper 
1325ddf7fe95Scasper 	if (cr->cr_klpd != NULL)
1326ddf7fe95Scasper 		crklpd_rele(cr->cr_klpd);
1327ddf7fe95Scasper 	cr->cr_klpd = crklpd;
1328ddf7fe95Scasper }
1329