xref: /titanic_53/usr/src/uts/common/os/cred.c (revision bda89588bd7667394a834e8a9a34612cce2ae9c3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*bda89588Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/policy.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/user.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
557c478bd9Sstevel@tonic-gate #include <sys/ucred.h>
567c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
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>
65da6c28aaSamw #include <sys/varargs.h>
66f48205beScasper 
67*bda89588Sjp151216 
68*bda89588Sjp151216 /* Ephemeral IDs Zones specific data */
69*bda89588Sjp151216 typedef struct ephemeral_zsd {
70*bda89588Sjp151216 	uid_t		min_uid;
71*bda89588Sjp151216 	uid_t		last_uid;
72*bda89588Sjp151216 	gid_t		min_gid;
73*bda89588Sjp151216 	gid_t		last_gid;
74f48205beScasper 	kmutex_t	eph_lock;
75*bda89588Sjp151216 	cred_t		*eph_nobody;
76*bda89588Sjp151216 } ephemeral_zsd_t;
77*bda89588Sjp151216 
78*bda89588Sjp151216 
79*bda89588Sjp151216 static kmutex_t		ephemeral_zone_mutex;
80*bda89588Sjp151216 static zone_key_t	ephemeral_zone_key;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static struct kmem_cache *cred_cache;
837c478bd9Sstevel@tonic-gate static size_t		crsize = 0;
847c478bd9Sstevel@tonic-gate static int		audoff = 0;
857c478bd9Sstevel@tonic-gate uint32_t		ucredsize;
867c478bd9Sstevel@tonic-gate cred_t			*kcred;
8745916cd2Sjpk static cred_t		*dummycr;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate int rstlink;		/* link(2) restricted to files owned by user? */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int get_c2audit_load(void);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	CR_AUINFO(c)	(auditinfo_addr_t *)((audoff == 0) ? NULL : \
947c478bd9Sstevel@tonic-gate 			    ((char *)(c)) + audoff)
957c478bd9Sstevel@tonic-gate 
9645916cd2Sjpk #define	REMOTE_PEER_CRED(c)	((c)->cr_gid == -1)
977c478bd9Sstevel@tonic-gate 
98f48205beScasper 
99f48205beScasper static boolean_t hasephids = B_FALSE;
100f48205beScasper 
101*bda89588Sjp151216 static ephemeral_zsd_t *
102*bda89588Sjp151216 get_ephemeral_zsd(zone_t *zone)
103*bda89588Sjp151216 {
104*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
105*bda89588Sjp151216 
106*bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
107*bda89588Sjp151216 	if (eph_zsd != NULL) {
108*bda89588Sjp151216 		return (eph_zsd);
109*bda89588Sjp151216 	}
110*bda89588Sjp151216 
111*bda89588Sjp151216 	mutex_enter(&ephemeral_zone_mutex);
112*bda89588Sjp151216 	eph_zsd = zone_getspecific(ephemeral_zone_key, zone);
113*bda89588Sjp151216 	if (eph_zsd == NULL) {
114*bda89588Sjp151216 		eph_zsd = kmem_zalloc(sizeof (ephemeral_zsd_t), KM_SLEEP);
115*bda89588Sjp151216 		eph_zsd->min_uid = MAXUID;
116*bda89588Sjp151216 		eph_zsd->last_uid = IDMAP_WK__MAX_UID;
117*bda89588Sjp151216 		eph_zsd->min_gid = MAXUID;
118*bda89588Sjp151216 		eph_zsd->last_gid = IDMAP_WK__MAX_GID;
119*bda89588Sjp151216 		mutex_init(&eph_zsd->eph_lock, NULL, MUTEX_DEFAULT, NULL);
120*bda89588Sjp151216 
121*bda89588Sjp151216 		/*
122*bda89588Sjp151216 		 * nobody is used to map SID containing CRs.
123*bda89588Sjp151216 		 */
124*bda89588Sjp151216 		eph_zsd->eph_nobody = crdup(zone->zone_kcred);
125*bda89588Sjp151216 		(void) crsetugid(eph_zsd->eph_nobody, UID_NOBODY, GID_NOBODY);
126*bda89588Sjp151216 		CR_FLAGS(eph_zsd->eph_nobody) = 0;
127*bda89588Sjp151216 		eph_zsd->eph_nobody->cr_zone = zone;
128*bda89588Sjp151216 
129*bda89588Sjp151216 		(void) zone_setspecific(ephemeral_zone_key, zone, eph_zsd);
130*bda89588Sjp151216 	}
131*bda89588Sjp151216 	mutex_exit(&ephemeral_zone_mutex);
132*bda89588Sjp151216 	return (eph_zsd);
133*bda89588Sjp151216 }
134*bda89588Sjp151216 
135*bda89588Sjp151216 /*
136*bda89588Sjp151216  * This function is called when a zone is destroyed
137*bda89588Sjp151216  */
138*bda89588Sjp151216 static void
139*bda89588Sjp151216 /* ARGSUSED */
140*bda89588Sjp151216 destroy_ephemeral_zsd(zoneid_t zone_id, void *arg)
141*bda89588Sjp151216 {
142*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = arg;
143*bda89588Sjp151216 	if (eph_zsd != NULL) {
144*bda89588Sjp151216 		mutex_destroy(&eph_zsd->eph_lock);
145*bda89588Sjp151216 		crfree(eph_zsd->eph_nobody);
146*bda89588Sjp151216 		kmem_free(eph_zsd, sizeof (ephemeral_zsd_t));
147*bda89588Sjp151216 	}
148*bda89588Sjp151216 }
149*bda89588Sjp151216 
150*bda89588Sjp151216 
151*bda89588Sjp151216 
152f48205beScasper /*
1537c478bd9Sstevel@tonic-gate  * Initialize credentials data structures.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate void
1577c478bd9Sstevel@tonic-gate cred_init(void)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	priv_init();
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	crsize = sizeof (cred_t) + sizeof (gid_t) * (ngroups_max - 1);
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * Make sure it's word-aligned.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (get_c2audit_load() > 0) {
1687c478bd9Sstevel@tonic-gate #ifdef _LP64
1697c478bd9Sstevel@tonic-gate 		/* assure audit context is 64-bit aligned */
1707c478bd9Sstevel@tonic-gate 		audoff = (crsize +
1717c478bd9Sstevel@tonic-gate 		    sizeof (int64_t) - 1) & ~(sizeof (int64_t) - 1);
1727c478bd9Sstevel@tonic-gate #else	/* _LP64 */
1737c478bd9Sstevel@tonic-gate 		audoff = crsize;
1747c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
1757c478bd9Sstevel@tonic-gate 		crsize = audoff + sizeof (auditinfo_addr_t);
1767c478bd9Sstevel@tonic-gate 		crsize = (crsize + sizeof (int) - 1) & ~(sizeof (int) - 1);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	cred_cache = kmem_cache_create("cred_cache", crsize, 0,
1807c478bd9Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
18345916cd2Sjpk 	 * dummycr is used to copy initial state for creds.
18445916cd2Sjpk 	 */
18545916cd2Sjpk 	dummycr = cralloc();
18645916cd2Sjpk 	bzero(dummycr, crsize);
18745916cd2Sjpk 	dummycr->cr_ref = 1;
188f48205beScasper 	dummycr->cr_uid = (uid_t)-1;
189f48205beScasper 	dummycr->cr_gid = (gid_t)-1;
190f48205beScasper 	dummycr->cr_ruid = (uid_t)-1;
191f48205beScasper 	dummycr->cr_rgid = (gid_t)-1;
192f48205beScasper 	dummycr->cr_suid = (uid_t)-1;
193f48205beScasper 	dummycr->cr_sgid = (gid_t)-1;
194f48205beScasper 
19545916cd2Sjpk 
19645916cd2Sjpk 	/*
1977c478bd9Sstevel@tonic-gate 	 * kcred is used by anything that needs all privileges; it's
1987c478bd9Sstevel@tonic-gate 	 * also the template used for crget as it has all the compatible
1997c478bd9Sstevel@tonic-gate 	 * sets filled in.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	kcred = cralloc();
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	bzero(kcred, crsize);
2047c478bd9Sstevel@tonic-gate 	kcred->cr_ref = 1;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/* kcred is never freed, so we don't need zone_cred_hold here */
2077c478bd9Sstevel@tonic-gate 	kcred->cr_zone = &zone0;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	priv_fillset(&CR_LPRIV(kcred));
2107c478bd9Sstevel@tonic-gate 	CR_IPRIV(kcred) = *priv_basic;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* Not a basic privilege, if chown is not restricted add it to I0 */
2137c478bd9Sstevel@tonic-gate 	if (!rstchown)
2147c478bd9Sstevel@tonic-gate 		priv_addset(&CR_IPRIV(kcred), PRIV_FILE_CHOWN_SELF);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* Basic privilege, if link is restricted remove it from I0 */
2177c478bd9Sstevel@tonic-gate 	if (rstlink)
2187c478bd9Sstevel@tonic-gate 		priv_delset(&CR_IPRIV(kcred), PRIV_FILE_LINK_ANY);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	CR_EPRIV(kcred) = CR_PPRIV(kcred) = CR_IPRIV(kcred);
22145916cd2Sjpk 
22245916cd2Sjpk 	CR_FLAGS(kcred) = NET_MAC_AWARE;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Set up credentials of p0.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	ttoproc(curthread)->p_cred = kcred;
2287c478bd9Sstevel@tonic-gate 	curthread->t_cred = kcred;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	ucredsize = UCRED_SIZE;
231*bda89588Sjp151216 
232*bda89588Sjp151216 	mutex_init(&ephemeral_zone_mutex, NULL, MUTEX_DEFAULT, NULL);
233*bda89588Sjp151216 	zone_key_create(&ephemeral_zone_key, NULL, NULL, destroy_ephemeral_zsd);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * Allocate (nearly) uninitialized cred_t.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate cred_t *
2407c478bd9Sstevel@tonic-gate cralloc(void)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2437c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;		/* So we can crfree() */
2447c478bd9Sstevel@tonic-gate 	cr->cr_zone = NULL;
24545916cd2Sjpk 	cr->cr_label = NULL;
246f48205beScasper 	cr->cr_ksid = NULL;
247f48205beScasper 	return (cr);
248f48205beScasper }
249f48205beScasper 
250f48205beScasper /*
251f48205beScasper  * As cralloc but prepared for ksid change (if appropriate).
252f48205beScasper  */
253f48205beScasper cred_t *
254f48205beScasper cralloc_ksid(void)
255f48205beScasper {
256f48205beScasper 	cred_t *cr = cralloc();
257f48205beScasper 	if (hasephids)
258f48205beScasper 		cr->cr_ksid = kcrsid_alloc();
2597c478bd9Sstevel@tonic-gate 	return (cr);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * Allocate a initialized cred structure and crhold() it.
2647c478bd9Sstevel@tonic-gate  * Initialized means: all ids 0, group count 0, L=Full, E=P=I=I0
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate cred_t *
2677c478bd9Sstevel@tonic-gate crget(void)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	cred_t *cr = kmem_cache_alloc(cred_cache, KM_SLEEP);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	bcopy(kcred, cr, crsize);
2727c478bd9Sstevel@tonic-gate 	cr->cr_ref = 1;
2737c478bd9Sstevel@tonic-gate 	zone_cred_hold(cr->cr_zone);
27445916cd2Sjpk 	if (cr->cr_label)
27545916cd2Sjpk 		label_hold(cr->cr_label);
2767c478bd9Sstevel@tonic-gate 	return (cr);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Broadcast the cred to all the threads in the process.
2817c478bd9Sstevel@tonic-gate  * The current thread's credentials can be set right away, but other
2827c478bd9Sstevel@tonic-gate  * threads must wait until the start of the next system call or trap.
2837c478bd9Sstevel@tonic-gate  * This avoids changing the cred in the middle of a system call.
2847c478bd9Sstevel@tonic-gate  *
2857c478bd9Sstevel@tonic-gate  * The cred has already been held for the process and the thread (2 holds),
2867c478bd9Sstevel@tonic-gate  * and p->p_cred set.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * p->p_crlock shouldn't be held here, since p_lock must be acquired.
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate void
2917c478bd9Sstevel@tonic-gate crset(proc_t *p, cred_t *cr)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	kthread_id_t	t;
2947c478bd9Sstevel@tonic-gate 	kthread_id_t	first;
2957c478bd9Sstevel@tonic-gate 	cred_t *oldcr;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);	/* assumes p_lwpcnt can't change */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * DTrace accesses t_cred in probe context.  t_cred must always be
3017c478bd9Sstevel@tonic-gate 	 * either NULL, or point to a valid, allocated cred structure.
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	t = curthread;
3047c478bd9Sstevel@tonic-gate 	oldcr = t->t_cred;
3057c478bd9Sstevel@tonic-gate 	t->t_cred = cr;		/* the cred is held by caller for this thread */
3067c478bd9Sstevel@tonic-gate 	crfree(oldcr);		/* free the old cred for the thread */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * Broadcast to other threads, if any.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (p->p_lwpcnt > 1) {
3127c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* to keep thread list safe */
3137c478bd9Sstevel@tonic-gate 		first = curthread;
3147c478bd9Sstevel@tonic-gate 		for (t = first->t_forw; t != first; t = t->t_forw)
3157c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
3167c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Put a hold on a cred structure.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate void
3247c478bd9Sstevel@tonic-gate crhold(cred_t *cr)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	atomic_add_32(&cr->cr_ref, 1);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * Release previous hold on a cred structure.  Free it if refcnt == 0.
33145916cd2Sjpk  * If cred uses label different from zone label, free it.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate void
3347c478bd9Sstevel@tonic-gate crfree(cred_t *cr)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	if (atomic_add_32_nv(&cr->cr_ref, -1) == 0) {
3377c478bd9Sstevel@tonic-gate 		ASSERT(cr != kcred);
33845916cd2Sjpk 		if (cr->cr_label)
33945916cd2Sjpk 			label_rele(cr->cr_label);
3407c478bd9Sstevel@tonic-gate 		if (cr->cr_zone)
3417c478bd9Sstevel@tonic-gate 			zone_cred_rele(cr->cr_zone);
342f48205beScasper 		if (cr->cr_ksid)
343f48205beScasper 			kcrsid_rele(cr->cr_ksid);
3447c478bd9Sstevel@tonic-gate 		kmem_cache_free(cred_cache, cr);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3507c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3517c478bd9Sstevel@tonic-gate  * 	and one for the thread.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate cred_t *
3547c478bd9Sstevel@tonic-gate crcopy(cred_t *cr)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	cred_t *newcr;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	newcr = cralloc();
3597c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
3607c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3617c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
36245916cd2Sjpk 	if (newcr->cr_label)
36345916cd2Sjpk 		label_hold(cr->cr_label);
364f48205beScasper 	if (newcr->cr_ksid)
365f48205beScasper 		kcrsid_hold(cr->cr_ksid);
3667c478bd9Sstevel@tonic-gate 	crfree(cr);
3677c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3687c478bd9Sstevel@tonic-gate 	return (newcr);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * Copy a cred structure to a new one and free the old one.
3737c478bd9Sstevel@tonic-gate  *	The new cred will have two references.  One for the calling process,
3747c478bd9Sstevel@tonic-gate  * 	and one for the thread.
3757c478bd9Sstevel@tonic-gate  * This variation on crcopy uses a pre-allocated structure for the
3767c478bd9Sstevel@tonic-gate  * "new" cred.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate void
3797c478bd9Sstevel@tonic-gate crcopy_to(cred_t *oldcr, cred_t *newcr)
3807c478bd9Sstevel@tonic-gate {
381f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
382f48205beScasper 
3837c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
3847c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
3857c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
38645916cd2Sjpk 	if (newcr->cr_label)
38745916cd2Sjpk 		label_hold(newcr->cr_label);
388f48205beScasper 	if (nkcr) {
389f48205beScasper 		newcr->cr_ksid = nkcr;
390f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
391f48205beScasper 	} else if (newcr->cr_ksid)
392f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
3937c478bd9Sstevel@tonic-gate 	crfree(oldcr);
3947c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 2;		/* caller gets two references */
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
3997c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate cred_t *
4027c478bd9Sstevel@tonic-gate crdup(cred_t *cr)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	cred_t *newcr;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	newcr = cralloc();
4077c478bd9Sstevel@tonic-gate 	bcopy(cr, newcr, crsize);
4087c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4097c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
41045916cd2Sjpk 	if (newcr->cr_label)
41145916cd2Sjpk 		label_hold(newcr->cr_label);
412f48205beScasper 	if (newcr->cr_ksid)
413f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4147c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4157c478bd9Sstevel@tonic-gate 	return (newcr);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Dup a cred struct to a new held one.
4207c478bd9Sstevel@tonic-gate  *	The old cred is not freed.
4217c478bd9Sstevel@tonic-gate  * This variation on crdup uses a pre-allocated structure for the
4227c478bd9Sstevel@tonic-gate  * "new" cred.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate void
4257c478bd9Sstevel@tonic-gate crdup_to(cred_t *oldcr, cred_t *newcr)
4267c478bd9Sstevel@tonic-gate {
427f48205beScasper 	credsid_t *nkcr = newcr->cr_ksid;
428f48205beScasper 
4297c478bd9Sstevel@tonic-gate 	bcopy(oldcr, newcr, crsize);
4307c478bd9Sstevel@tonic-gate 	if (newcr->cr_zone)
4317c478bd9Sstevel@tonic-gate 		zone_cred_hold(newcr->cr_zone);
43245916cd2Sjpk 	if (newcr->cr_label)
43345916cd2Sjpk 		label_hold(newcr->cr_label);
434f48205beScasper 	if (nkcr) {
435f48205beScasper 		newcr->cr_ksid = nkcr;
436f48205beScasper 		kcrsidcopy_to(oldcr->cr_ksid, newcr->cr_ksid);
437f48205beScasper 	} else if (newcr->cr_ksid)
438f48205beScasper 		kcrsid_hold(newcr->cr_ksid);
4397c478bd9Sstevel@tonic-gate 	newcr->cr_ref = 1;
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Return the (held) credentials for the current running process.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate cred_t *
44645916cd2Sjpk crgetcred(void)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	cred_t *cr;
4497c478bd9Sstevel@tonic-gate 	proc_t *p;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
4527c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
4537c478bd9Sstevel@tonic-gate 	crhold(cr = p->p_cred);
4547c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
4557c478bd9Sstevel@tonic-gate 	return (cr);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  * Backward compatibility check for suser().
4607c478bd9Sstevel@tonic-gate  * Accounting flag is now set in the policy functions; auditing is
4617c478bd9Sstevel@tonic-gate  * done through use of privilege in the audit trail.
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate int
4647c478bd9Sstevel@tonic-gate suser(cred_t *cr)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_SUSER_COMPAT, B_FALSE, EPERM, NULL)
4677c478bd9Sstevel@tonic-gate 	    == 0);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * Determine whether the supplied group id is a member of the group
4727c478bd9Sstevel@tonic-gate  * described by the supplied credentials.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate int
4757c478bd9Sstevel@tonic-gate groupmember(gid_t gid, const cred_t *cr)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	if (gid == cr->cr_gid)
4787c478bd9Sstevel@tonic-gate 		return (1);
4797c478bd9Sstevel@tonic-gate 	return (supgroupmember(gid, cr));
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * As groupmember but only check against the supplemental groups.
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate int
4867c478bd9Sstevel@tonic-gate supgroupmember(gid_t gid, const cred_t *cr)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	const gid_t *gp, *endgp;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	endgp = &cr->cr_groups[cr->cr_ngroups];
4917c478bd9Sstevel@tonic-gate 	for (gp = cr->cr_groups; gp < endgp; gp++)
4927c478bd9Sstevel@tonic-gate 		if (*gp == gid)
4937c478bd9Sstevel@tonic-gate 			return (1);
4947c478bd9Sstevel@tonic-gate 	return (0);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * This function is called to check whether the credentials set
4997c478bd9Sstevel@tonic-gate  * "scrp" has permission to act on credentials set "tcrp".  It enforces the
5007c478bd9Sstevel@tonic-gate  * permission requirements needed to send a signal to a process.
5017c478bd9Sstevel@tonic-gate  * The same requirements are imposed by other system calls, however.
5027c478bd9Sstevel@tonic-gate  *
5037c478bd9Sstevel@tonic-gate  * The rules are:
5047c478bd9Sstevel@tonic-gate  * (1) if the credentials are the same, the check succeeds
5057c478bd9Sstevel@tonic-gate  * (2) if the zone ids don't match, and scrp is not in the global zone or
5067c478bd9Sstevel@tonic-gate  *     does not have the PRIV_PROC_ZONE privilege, the check fails
5077c478bd9Sstevel@tonic-gate  * (3) if the real or effective user id of scrp matches the real or saved
5087c478bd9Sstevel@tonic-gate  *     user id of tcrp or scrp has the PRIV_PROC_OWNER privilege, the check
5097c478bd9Sstevel@tonic-gate  *     succeeds
5107c478bd9Sstevel@tonic-gate  * (4) otherwise, the check fails
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate int
5137c478bd9Sstevel@tonic-gate hasprocperm(const cred_t *tcrp, const cred_t *scrp)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	if (scrp == tcrp)
5167c478bd9Sstevel@tonic-gate 		return (1);
5177c478bd9Sstevel@tonic-gate 	if (scrp->cr_zone != tcrp->cr_zone &&
5187c478bd9Sstevel@tonic-gate 	    (scrp->cr_zone != global_zone ||
5197c478bd9Sstevel@tonic-gate 	    secpolicy_proc_zone(scrp) != 0))
5207c478bd9Sstevel@tonic-gate 		return (0);
5217c478bd9Sstevel@tonic-gate 	if (scrp->cr_uid == tcrp->cr_ruid ||
5227c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_ruid ||
5237c478bd9Sstevel@tonic-gate 	    scrp->cr_uid  == tcrp->cr_suid ||
5247c478bd9Sstevel@tonic-gate 	    scrp->cr_ruid == tcrp->cr_suid ||
5257c478bd9Sstevel@tonic-gate 	    !PRIV_POLICY(scrp, PRIV_PROC_OWNER, B_FALSE, EPERM, "hasprocperm"))
5267c478bd9Sstevel@tonic-gate 		return (1);
5277c478bd9Sstevel@tonic-gate 	return (0);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * This interface replaces hasprocperm; it works like hasprocperm but
5327c478bd9Sstevel@tonic-gate  * additionally returns success if the proc_t's match
5337c478bd9Sstevel@tonic-gate  * It is the preferred interface for most uses.
5347c478bd9Sstevel@tonic-gate  * And it will acquire pcrlock itself, so it assert's that it shouldn't
5357c478bd9Sstevel@tonic-gate  * be held.
5367c478bd9Sstevel@tonic-gate  */
5377c478bd9Sstevel@tonic-gate int
5387c478bd9Sstevel@tonic-gate prochasprocperm(proc_t *tp, proc_t *sp, const cred_t *scrp)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	int rets;
5417c478bd9Sstevel@tonic-gate 	cred_t *tcrp;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&tp->p_crlock));
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (tp == sp)
5467c478bd9Sstevel@tonic-gate 		return (1);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (tp->p_sessp != sp->p_sessp && secpolicy_basic_proc(scrp) != 0)
5497c478bd9Sstevel@tonic-gate 		return (0);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
5527c478bd9Sstevel@tonic-gate 	tcrp = tp->p_cred;
5537c478bd9Sstevel@tonic-gate 	rets = hasprocperm(tcrp, scrp);
5547c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	return (rets);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * This routine is used to compare two credentials to determine if
5617c478bd9Sstevel@tonic-gate  * they refer to the same "user".  If the pointers are equal, then
5627c478bd9Sstevel@tonic-gate  * they must refer to the same user.  Otherwise, the contents of
5637c478bd9Sstevel@tonic-gate  * the credentials are compared to see whether they are equivalent.
5647c478bd9Sstevel@tonic-gate  *
5657c478bd9Sstevel@tonic-gate  * This routine returns 0 if the credentials refer to the same user,
5667c478bd9Sstevel@tonic-gate  * 1 if they do not.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate int
5697c478bd9Sstevel@tonic-gate crcmp(const cred_t *cr1, const cred_t *cr2)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (cr1 == cr2)
5737c478bd9Sstevel@tonic-gate 		return (0);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (cr1->cr_uid == cr2->cr_uid &&
5767c478bd9Sstevel@tonic-gate 	    cr1->cr_gid == cr2->cr_gid &&
5777c478bd9Sstevel@tonic-gate 	    cr1->cr_ruid == cr2->cr_ruid &&
5787c478bd9Sstevel@tonic-gate 	    cr1->cr_rgid == cr2->cr_rgid &&
5797c478bd9Sstevel@tonic-gate 	    cr1->cr_ngroups == cr2->cr_ngroups &&
5807c478bd9Sstevel@tonic-gate 	    cr1->cr_zone == cr2->cr_zone &&
5817c478bd9Sstevel@tonic-gate 	    bcmp(cr1->cr_groups, cr2->cr_groups,
5827c478bd9Sstevel@tonic-gate 	    cr1->cr_ngroups * sizeof (gid_t)) == 0) {
5837c478bd9Sstevel@tonic-gate 		return (!priv_isequalset(&CR_OEPRIV(cr1), &CR_OEPRIV(cr2)));
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 	return (1);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  * Read access functions to cred_t.
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate uid_t
5927c478bd9Sstevel@tonic-gate crgetuid(const cred_t *cr)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	return (cr->cr_uid);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate uid_t
5987c478bd9Sstevel@tonic-gate crgetruid(const cred_t *cr)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	return (cr->cr_ruid);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate uid_t
6047c478bd9Sstevel@tonic-gate crgetsuid(const cred_t *cr)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	return (cr->cr_suid);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate gid_t
6107c478bd9Sstevel@tonic-gate crgetgid(const cred_t *cr)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	return (cr->cr_gid);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate gid_t
6167c478bd9Sstevel@tonic-gate crgetrgid(const cred_t *cr)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	return (cr->cr_rgid);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate gid_t
6227c478bd9Sstevel@tonic-gate crgetsgid(const cred_t *cr)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	return (cr->cr_sgid);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate const auditinfo_addr_t *
6287c478bd9Sstevel@tonic-gate crgetauinfo(const cred_t *cr)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	return ((const auditinfo_addr_t *)CR_AUINFO(cr));
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate auditinfo_addr_t *
6347c478bd9Sstevel@tonic-gate crgetauinfo_modifiable(cred_t *cr)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	return (CR_AUINFO(cr));
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate zoneid_t
6407c478bd9Sstevel@tonic-gate crgetzoneid(const cred_t *cr)
6417c478bd9Sstevel@tonic-gate {
64245916cd2Sjpk 	return (cr->cr_zone == NULL ?
64345916cd2Sjpk 	    (cr->cr_uid == -1 ? (zoneid_t)-1 : GLOBAL_ZONEID) :
64445916cd2Sjpk 	    cr->cr_zone->zone_id);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate projid_t
6487c478bd9Sstevel@tonic-gate crgetprojid(const cred_t *cr)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	return (cr->cr_projid);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
65345916cd2Sjpk zone_t *
65445916cd2Sjpk crgetzone(const cred_t *cr)
65545916cd2Sjpk {
65645916cd2Sjpk 	return (cr->cr_zone);
65745916cd2Sjpk }
65845916cd2Sjpk 
65945916cd2Sjpk struct ts_label_s *
66045916cd2Sjpk crgetlabel(const cred_t *cr)
66145916cd2Sjpk {
66245916cd2Sjpk 	return (cr->cr_label ?
66345916cd2Sjpk 	    cr->cr_label :
66445916cd2Sjpk 	    (cr->cr_zone ? cr->cr_zone->zone_slabel : NULL));
66545916cd2Sjpk }
66645916cd2Sjpk 
66745916cd2Sjpk boolean_t
66845916cd2Sjpk crisremote(const cred_t *cr)
66945916cd2Sjpk {
67045916cd2Sjpk 	return (REMOTE_PEER_CRED(cr));
67145916cd2Sjpk }
67245916cd2Sjpk 
673*bda89588Sjp151216 #define	BADUID(x, zn)	((x) != -1 && !VALID_UID((x), (zn)))
674*bda89588Sjp151216 #define	BADGID(x, zn)	((x) != -1 && !VALID_GID((x), (zn)))
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate int
6777c478bd9Sstevel@tonic-gate crsetresuid(cred_t *cr, uid_t r, uid_t e, uid_t s)
6787c478bd9Sstevel@tonic-gate {
679*bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
680*bda89588Sjp151216 
6817c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6827c478bd9Sstevel@tonic-gate 
683*bda89588Sjp151216 	if (BADUID(r, zone) || BADUID(e, zone) || BADUID(s, zone))
6847c478bd9Sstevel@tonic-gate 		return (-1);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (r != -1)
6877c478bd9Sstevel@tonic-gate 		cr->cr_ruid = r;
6887c478bd9Sstevel@tonic-gate 	if (e != -1)
6897c478bd9Sstevel@tonic-gate 		cr->cr_uid = e;
6907c478bd9Sstevel@tonic-gate 	if (s != -1)
6917c478bd9Sstevel@tonic-gate 		cr->cr_suid = s;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	return (0);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate int
6977c478bd9Sstevel@tonic-gate crsetresgid(cred_t *cr, gid_t r, gid_t e, gid_t s)
6987c478bd9Sstevel@tonic-gate {
699*bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
700*bda89588Sjp151216 
7017c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7027c478bd9Sstevel@tonic-gate 
703*bda89588Sjp151216 	if (BADGID(r, zone) || BADGID(e, zone) || BADGID(s, zone))
7047c478bd9Sstevel@tonic-gate 		return (-1);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (r != -1)
7077c478bd9Sstevel@tonic-gate 		cr->cr_rgid = r;
7087c478bd9Sstevel@tonic-gate 	if (e != -1)
7097c478bd9Sstevel@tonic-gate 		cr->cr_gid = e;
7107c478bd9Sstevel@tonic-gate 	if (s != -1)
7117c478bd9Sstevel@tonic-gate 		cr->cr_sgid = s;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	return (0);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate int
7177c478bd9Sstevel@tonic-gate crsetugid(cred_t *cr, uid_t uid, gid_t gid)
7187c478bd9Sstevel@tonic-gate {
719*bda89588Sjp151216 	zone_t	*zone = crgetzone(cr);
720*bda89588Sjp151216 
7217c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7227c478bd9Sstevel@tonic-gate 
723*bda89588Sjp151216 	if (!VALID_UID(uid, zone) || !VALID_GID(gid, zone))
7247c478bd9Sstevel@tonic-gate 		return (-1);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	cr->cr_uid = cr->cr_ruid = cr->cr_suid = uid;
7277c478bd9Sstevel@tonic-gate 	cr->cr_gid = cr->cr_rgid = cr->cr_sgid = gid;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	return (0);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate int
7337c478bd9Sstevel@tonic-gate crsetgroups(cred_t *cr, int n, gid_t *grp)
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (n > ngroups_max || n < 0)
7387c478bd9Sstevel@tonic-gate 		return (-1);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	cr->cr_ngroups = n;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (n > 0)
7437c478bd9Sstevel@tonic-gate 		bcopy(grp, cr->cr_groups, n * sizeof (gid_t));
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	return (0);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate void
7497c478bd9Sstevel@tonic-gate crsetprojid(cred_t *cr, projid_t projid)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	ASSERT(projid >= 0 && projid <= MAXPROJID);
7527c478bd9Sstevel@tonic-gate 	cr->cr_projid = projid;
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * This routine returns the pointer to the first element of the cr_groups
7577c478bd9Sstevel@tonic-gate  * array.  It can move around in an implementation defined way.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate const gid_t *
7607c478bd9Sstevel@tonic-gate crgetgroups(const cred_t *cr)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	return (cr->cr_groups);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate int
7667c478bd9Sstevel@tonic-gate crgetngroups(const cred_t *cr)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	return (cr->cr_ngroups);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate void
7727c478bd9Sstevel@tonic-gate cred2prcred(const cred_t *cr, prcred_t *pcrp)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	pcrp->pr_euid = cr->cr_uid;
7757c478bd9Sstevel@tonic-gate 	pcrp->pr_ruid = cr->cr_ruid;
7767c478bd9Sstevel@tonic-gate 	pcrp->pr_suid = cr->cr_suid;
7777c478bd9Sstevel@tonic-gate 	pcrp->pr_egid = cr->cr_gid;
7787c478bd9Sstevel@tonic-gate 	pcrp->pr_rgid = cr->cr_rgid;
7797c478bd9Sstevel@tonic-gate 	pcrp->pr_sgid = cr->cr_sgid;
7807c478bd9Sstevel@tonic-gate 	pcrp->pr_ngroups = MIN(cr->cr_ngroups, (uint_t)ngroups_max);
7817c478bd9Sstevel@tonic-gate 	pcrp->pr_groups[0] = 0;	/* in case ngroups == 0 */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	if (pcrp->pr_ngroups != 0)
7847c478bd9Sstevel@tonic-gate 		bcopy(cr->cr_groups, pcrp->pr_groups,
7857c478bd9Sstevel@tonic-gate 		    sizeof (gid_t) * cr->cr_ngroups);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static int
78945916cd2Sjpk cred2ucaud(const cred_t *cr, auditinfo64_addr_t *ainfo, const cred_t *rcr)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	auditinfo_addr_t	*ai;
7927c478bd9Sstevel@tonic-gate 	au_tid_addr_t	tid;
7937c478bd9Sstevel@tonic-gate 
79445916cd2Sjpk 	if (secpolicy_audit_getattr(rcr) != 0)
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	ai = CR_AUINFO(cr);	/* caller makes sure this is non-NULL */
7987c478bd9Sstevel@tonic-gate 	tid = ai->ai_termid;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	ainfo->ai_auid = ai->ai_auid;
8017c478bd9Sstevel@tonic-gate 	ainfo->ai_mask = ai->ai_mask;
8027c478bd9Sstevel@tonic-gate 	ainfo->ai_asid = ai->ai_asid;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_type = tid.at_type;
8057c478bd9Sstevel@tonic-gate 	bcopy(&tid.at_addr, &ainfo->ai_termid.at_addr, 4 * sizeof (uint_t));
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_major = (uint32_t)getmajor(tid.at_port);
8087c478bd9Sstevel@tonic-gate 	ainfo->ai_termid.at_port.at_minor = (uint32_t)getminor(tid.at_port);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	return (0);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
81345916cd2Sjpk void
81445916cd2Sjpk cred2uclabel(const cred_t *cr, bslabel_t *labelp)
81545916cd2Sjpk {
81645916cd2Sjpk 	ts_label_t	*tslp;
81745916cd2Sjpk 
81845916cd2Sjpk 	if ((tslp = crgetlabel(cr)) != NULL)
81945916cd2Sjpk 		bcopy(&tslp->tsl_label, labelp, sizeof (bslabel_t));
82045916cd2Sjpk }
82145916cd2Sjpk 
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate  * Convert a credential into a "ucred".  Allow the caller to specify
8247c478bd9Sstevel@tonic-gate  * and aligned buffer, e.g., in an mblk, so we don't have to allocate
8257c478bd9Sstevel@tonic-gate  * memory and copy it twice.
82645916cd2Sjpk  *
82745916cd2Sjpk  * This function may call cred2ucaud(), which calls CRED(). Since this
82845916cd2Sjpk  * can be called from an interrupt thread, receiver's cred (rcr) is needed
82945916cd2Sjpk  * to determine whether audit info should be included.
8307c478bd9Sstevel@tonic-gate  */
8317c478bd9Sstevel@tonic-gate struct ucred_s *
83245916cd2Sjpk cred2ucred(const cred_t *cr, pid_t pid, void *buf, const cred_t *rcr)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/* The structure isn't always completely filled in, so zero it */
8377c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
8387c478bd9Sstevel@tonic-gate 		uc = kmem_zalloc(ucredsize, KM_SLEEP);
8397c478bd9Sstevel@tonic-gate 	} else {
8407c478bd9Sstevel@tonic-gate 		bzero(buf, ucredsize);
8417c478bd9Sstevel@tonic-gate 		uc = buf;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	uc->uc_size = ucredsize;
8447c478bd9Sstevel@tonic-gate 	uc->uc_credoff = UCRED_CRED_OFF;
8457c478bd9Sstevel@tonic-gate 	uc->uc_privoff = UCRED_PRIV_OFF;
8467c478bd9Sstevel@tonic-gate 	uc->uc_audoff = UCRED_AUD_OFF;
84745916cd2Sjpk 	uc->uc_labeloff = UCRED_LABEL_OFF;
8487c478bd9Sstevel@tonic-gate 	uc->uc_pid = pid;
8497c478bd9Sstevel@tonic-gate 	uc->uc_projid = cr->cr_projid;
8507c478bd9Sstevel@tonic-gate 	uc->uc_zoneid = crgetzoneid(cr);
8517c478bd9Sstevel@tonic-gate 
85245916cd2Sjpk 	/*
85345916cd2Sjpk 	 * Note that cred2uclabel() call should not be factored out
85445916cd2Sjpk 	 * to the bottom of the if-else. UCXXX() macros depend on
85545916cd2Sjpk 	 * uc_xxxoff values to work correctly.
85645916cd2Sjpk 	 */
85745916cd2Sjpk 	if (REMOTE_PEER_CRED(cr)) {
85845916cd2Sjpk 		/*
85945916cd2Sjpk 		 * other than label, the rest of cred info about a
86045916cd2Sjpk 		 * remote peer isn't available.
86145916cd2Sjpk 		 */
86245916cd2Sjpk 		cred2uclabel(cr, UCLABEL(uc));
86345916cd2Sjpk 		uc->uc_credoff = 0;
86445916cd2Sjpk 		uc->uc_privoff = 0;
86545916cd2Sjpk 		uc->uc_audoff = 0;
86645916cd2Sjpk 	} else {
8677c478bd9Sstevel@tonic-gate 		cred2prcred(cr, UCCRED(uc));
8687c478bd9Sstevel@tonic-gate 		cred2prpriv(cr, UCPRIV(uc));
86945916cd2Sjpk 		if (audoff == 0 || cred2ucaud(cr, UCAUD(uc), rcr) != 0)
8707c478bd9Sstevel@tonic-gate 			uc->uc_audoff = 0;
87145916cd2Sjpk 		cred2uclabel(cr, UCLABEL(uc));
87245916cd2Sjpk 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	return (uc);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate  * Get the "ucred" of a process.
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate struct ucred_s *
8817c478bd9Sstevel@tonic-gate pgetucred(proc_t *p)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	cred_t *cr;
8847c478bd9Sstevel@tonic-gate 	struct ucred_s *uc;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
8877c478bd9Sstevel@tonic-gate 	cr = p->p_cred;
8887c478bd9Sstevel@tonic-gate 	crhold(cr);
8897c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
8907c478bd9Sstevel@tonic-gate 
89145916cd2Sjpk 	uc = cred2ucred(cr, p->p_pid, NULL, CRED());
8927c478bd9Sstevel@tonic-gate 	crfree(cr);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	return (uc);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  * If the reply status is NFSERR_EACCES, it may be because we are
8997c478bd9Sstevel@tonic-gate  * root (no root net access).  Check the real uid, if it isn't root
9007c478bd9Sstevel@tonic-gate  * make that the uid instead and retry the call.
9017c478bd9Sstevel@tonic-gate  * Private interface for NFS.
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate cred_t *
9047c478bd9Sstevel@tonic-gate crnetadjust(cred_t *cr)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 && cr->cr_ruid != 0) {
9077c478bd9Sstevel@tonic-gate 		cr = crdup(cr);
9087c478bd9Sstevel@tonic-gate 		cr->cr_uid = cr->cr_ruid;
9097c478bd9Sstevel@tonic-gate 		return (cr);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	return (NULL);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate  * The reference count is of interest when you want to check
9167c478bd9Sstevel@tonic-gate  * whether it is ok to modify the credential in place.
9177c478bd9Sstevel@tonic-gate  */
9187c478bd9Sstevel@tonic-gate uint_t
9197c478bd9Sstevel@tonic-gate crgetref(const cred_t *cr)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	return (cr->cr_ref);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate static int
9257c478bd9Sstevel@tonic-gate get_c2audit_load(void)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	static int	gotit = 0;
9287c478bd9Sstevel@tonic-gate 	static int	c2audit_load;
9297c478bd9Sstevel@tonic-gate 	u_longlong_t	audit_load_val;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (gotit)
9327c478bd9Sstevel@tonic-gate 		return (c2audit_load);
9337c478bd9Sstevel@tonic-gate 	audit_load_val = 0;		/* set default value once */
9347c478bd9Sstevel@tonic-gate 	(void) mod_sysvar("c2audit", "audit_load", &audit_load_val);
9357c478bd9Sstevel@tonic-gate 	c2audit_load = (int)audit_load_val;
9367c478bd9Sstevel@tonic-gate 	gotit++;
9377c478bd9Sstevel@tonic-gate 	return (c2audit_load);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate int
9417c478bd9Sstevel@tonic-gate get_audit_ucrsize(void)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	return (get_c2audit_load() ? sizeof (auditinfo64_addr_t) : 0);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * Set zone pointer in credential to indicated value.  First adds a
9487c478bd9Sstevel@tonic-gate  * hold for the new zone, then drops the hold on previous zone (if any).
9497c478bd9Sstevel@tonic-gate  * This is done in this order in case the old and new zones are the
9507c478bd9Sstevel@tonic-gate  * same.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate void
9537c478bd9Sstevel@tonic-gate crsetzone(cred_t *cr, zone_t *zptr)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	zone_t *oldzptr = cr->cr_zone;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	ASSERT(cr != kcred);
9587c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
9597c478bd9Sstevel@tonic-gate 	cr->cr_zone = zptr;
9607c478bd9Sstevel@tonic-gate 	zone_cred_hold(zptr);
9617c478bd9Sstevel@tonic-gate 	if (oldzptr)
9627c478bd9Sstevel@tonic-gate 		zone_cred_rele(oldzptr);
9637c478bd9Sstevel@tonic-gate }
96445916cd2Sjpk 
96545916cd2Sjpk /*
96645916cd2Sjpk  * Create a new cred based on the supplied label
96745916cd2Sjpk  */
96845916cd2Sjpk cred_t *
96945916cd2Sjpk newcred_from_bslabel(bslabel_t *blabel, uint32_t doi, int flags)
97045916cd2Sjpk {
97145916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
97245916cd2Sjpk 	cred_t *cr = NULL;
97345916cd2Sjpk 
97445916cd2Sjpk 	if (lbl != NULL) {
97545916cd2Sjpk 		if ((cr = kmem_cache_alloc(cred_cache, flags)) != NULL) {
97645916cd2Sjpk 			bcopy(dummycr, cr, crsize);
97745916cd2Sjpk 			cr->cr_label = lbl;
97845916cd2Sjpk 		} else {
97945916cd2Sjpk 			label_rele(lbl);
98045916cd2Sjpk 		}
98145916cd2Sjpk 	}
98245916cd2Sjpk 
98345916cd2Sjpk 	return (cr);
98445916cd2Sjpk }
98545916cd2Sjpk 
98645916cd2Sjpk /*
98745916cd2Sjpk  * Derive a new cred from the existing cred, but with a different label.
98845916cd2Sjpk  * To be used when a cred is being shared, but the label needs to be changed
98945916cd2Sjpk  * by a caller without affecting other users
99045916cd2Sjpk  */
99145916cd2Sjpk cred_t *
99245916cd2Sjpk copycred_from_bslabel(cred_t *cr, bslabel_t *blabel, uint32_t doi, int flags)
99345916cd2Sjpk {
99445916cd2Sjpk 	ts_label_t *lbl = labelalloc(blabel, doi, flags);
99545916cd2Sjpk 	cred_t *newcr = NULL;
99645916cd2Sjpk 
99745916cd2Sjpk 	if (lbl != NULL) {
99845916cd2Sjpk 		if ((newcr = kmem_cache_alloc(cred_cache, flags)) != NULL) {
99945916cd2Sjpk 			bcopy(cr, newcr, crsize);
100045916cd2Sjpk 			if (newcr->cr_zone)
100145916cd2Sjpk 				zone_cred_hold(newcr->cr_zone);
100245916cd2Sjpk 			newcr->cr_label = lbl;
100345916cd2Sjpk 			newcr->cr_ref = 1;
100445916cd2Sjpk 		} else {
100545916cd2Sjpk 			label_rele(lbl);
100645916cd2Sjpk 		}
100745916cd2Sjpk 	}
100845916cd2Sjpk 
100945916cd2Sjpk 	return (newcr);
101045916cd2Sjpk }
101145916cd2Sjpk 
101245916cd2Sjpk /*
101345916cd2Sjpk  * This function returns a pointer to the kcred-equivalent in the current zone.
101445916cd2Sjpk  */
101545916cd2Sjpk cred_t *
101645916cd2Sjpk zone_kcred(void)
101745916cd2Sjpk {
101845916cd2Sjpk 	zone_t *zone;
101945916cd2Sjpk 
102045916cd2Sjpk 	if ((zone = CRED()->cr_zone) != NULL)
102145916cd2Sjpk 		return (zone->zone_kcred);
102245916cd2Sjpk 	else
102345916cd2Sjpk 		return (kcred);
102445916cd2Sjpk }
1025f48205beScasper 
1026f48205beScasper boolean_t
1027*bda89588Sjp151216 valid_ephemeral_uid(zone_t *zone, uid_t id)
1028f48205beScasper {
1029*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
1030*bda89588Sjp151216 	if (id < IDMAP_WK__MAX_UID)
1031*bda89588Sjp151216 		return (B_TRUE);
1032*bda89588Sjp151216 
1033*bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1034*bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1035f48205beScasper 	membar_consumer();
1036*bda89588Sjp151216 	return (id > eph_zsd->min_uid && id <= eph_zsd->last_uid);
1037f48205beScasper }
1038f48205beScasper 
1039f48205beScasper boolean_t
1040*bda89588Sjp151216 valid_ephemeral_gid(zone_t *zone, gid_t id)
1041f48205beScasper {
1042*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
1043*bda89588Sjp151216 	if (id < IDMAP_WK__MAX_GID)
1044*bda89588Sjp151216 		return (B_TRUE);
1045*bda89588Sjp151216 
1046*bda89588Sjp151216 	eph_zsd = get_ephemeral_zsd(zone);
1047*bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1048f48205beScasper 	membar_consumer();
1049*bda89588Sjp151216 	return (id > eph_zsd->min_gid && id <= eph_zsd->last_gid);
1050f48205beScasper }
1051f48205beScasper 
1052f48205beScasper int
1053*bda89588Sjp151216 eph_uid_alloc(zone_t *zone, int flags, uid_t *start, int count)
1054f48205beScasper {
1055*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1056*bda89588Sjp151216 
1057*bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1058*bda89588Sjp151216 
1059*bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1060f48205beScasper 
1061f48205beScasper 	/* Test for unsigned integer wrap around */
1062*bda89588Sjp151216 	if (eph_zsd->last_uid + count < eph_zsd->last_uid) {
1063*bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1064f48205beScasper 		return (-1);
1065f48205beScasper 	}
1066f48205beScasper 
1067f48205beScasper 	/* first call or idmap crashed and state corrupted */
1068f48205beScasper 	if (flags != 0)
1069*bda89588Sjp151216 		eph_zsd->min_uid = eph_zsd->last_uid;
1070f48205beScasper 
1071f48205beScasper 	hasephids = B_TRUE;
1072*bda89588Sjp151216 	*start = eph_zsd->last_uid + 1;
1073*bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_uid, count);
1074*bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1075f48205beScasper 	return (0);
1076f48205beScasper }
1077f48205beScasper 
1078f48205beScasper int
1079*bda89588Sjp151216 eph_gid_alloc(zone_t *zone, int flags, gid_t *start, int count)
1080f48205beScasper {
1081*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1082*bda89588Sjp151216 
1083*bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1084*bda89588Sjp151216 
1085*bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1086f48205beScasper 
1087f48205beScasper 	/* Test for unsigned integer wrap around */
1088*bda89588Sjp151216 	if (eph_zsd->last_gid + count < eph_zsd->last_gid) {
1089*bda89588Sjp151216 		mutex_exit(&eph_zsd->eph_lock);
1090f48205beScasper 		return (-1);
1091f48205beScasper 	}
1092f48205beScasper 
1093f48205beScasper 	/* first call or idmap crashed and state corrupted */
1094f48205beScasper 	if (flags != 0)
1095*bda89588Sjp151216 		eph_zsd->min_gid = eph_zsd->last_gid;
1096f48205beScasper 
1097f48205beScasper 	hasephids = B_TRUE;
1098*bda89588Sjp151216 	*start = eph_zsd->last_gid + 1;
1099*bda89588Sjp151216 	atomic_add_32(&eph_zsd->last_gid, count);
1100*bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1101f48205beScasper 	return (0);
1102f48205beScasper }
1103f48205beScasper 
1104f48205beScasper /*
1105*bda89588Sjp151216  * IMPORTANT.The two functions get_ephemeral_data() and set_ephemeral_data()
1106*bda89588Sjp151216  * are project private functions that are for use of the test system only and
1107*bda89588Sjp151216  * are not to be used for other purposes.
1108*bda89588Sjp151216  */
1109*bda89588Sjp151216 
1110*bda89588Sjp151216 void
1111*bda89588Sjp151216 get_ephemeral_data(zone_t *zone, uid_t *min_uid, uid_t *last_uid,
1112*bda89588Sjp151216 	gid_t *min_gid, gid_t *last_gid)
1113*bda89588Sjp151216 {
1114*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1115*bda89588Sjp151216 
1116*bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1117*bda89588Sjp151216 
1118*bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1119*bda89588Sjp151216 
1120*bda89588Sjp151216 	*min_uid = eph_zsd->min_uid;
1121*bda89588Sjp151216 	*last_uid = eph_zsd->last_uid;
1122*bda89588Sjp151216 	*min_gid = eph_zsd->min_gid;
1123*bda89588Sjp151216 	*last_gid = eph_zsd->last_gid;
1124*bda89588Sjp151216 
1125*bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1126*bda89588Sjp151216 }
1127*bda89588Sjp151216 
1128*bda89588Sjp151216 
1129*bda89588Sjp151216 void
1130*bda89588Sjp151216 set_ephemeral_data(zone_t *zone, uid_t min_uid, uid_t last_uid,
1131*bda89588Sjp151216 	gid_t min_gid, gid_t last_gid)
1132*bda89588Sjp151216 {
1133*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd = get_ephemeral_zsd(zone);
1134*bda89588Sjp151216 
1135*bda89588Sjp151216 	ASSERT(eph_zsd != NULL);
1136*bda89588Sjp151216 
1137*bda89588Sjp151216 	mutex_enter(&eph_zsd->eph_lock);
1138*bda89588Sjp151216 
1139*bda89588Sjp151216 	if (min_uid != 0)
1140*bda89588Sjp151216 		eph_zsd->min_uid = min_uid;
1141*bda89588Sjp151216 	if (last_uid != 0)
1142*bda89588Sjp151216 		eph_zsd->last_uid = last_uid;
1143*bda89588Sjp151216 	if (min_gid != 0)
1144*bda89588Sjp151216 		eph_zsd->min_gid = min_gid;
1145*bda89588Sjp151216 	if (last_gid != 0)
1146*bda89588Sjp151216 		eph_zsd->last_gid = last_gid;
1147*bda89588Sjp151216 
1148*bda89588Sjp151216 	mutex_exit(&eph_zsd->eph_lock);
1149*bda89588Sjp151216 }
1150*bda89588Sjp151216 
1151*bda89588Sjp151216 /*
1152da6c28aaSamw  * If the credential user SID or group SID is mapped to an ephemeral
1153da6c28aaSamw  * ID, map the credential to nobody.
1154f48205beScasper  */
1155f48205beScasper cred_t *
1156f48205beScasper crgetmapped(const cred_t *cr)
1157f48205beScasper {
1158*bda89588Sjp151216 	ephemeral_zsd_t *eph_zsd;
115990c3d472Scasper 	/*
116090c3d472Scasper 	 * Someone incorrectly passed a NULL cred to a vnode operation
116190c3d472Scasper 	 * either on purpose or by calling CRED() in interrupt context.
116290c3d472Scasper 	 */
116390c3d472Scasper 	if (cr == NULL)
116490c3d472Scasper 		return (NULL);
116590c3d472Scasper 
1166f48205beScasper 	if (cr->cr_ksid != NULL) {
1167*bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_USER].ks_id > MAXUID) {
1168*bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1169*bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1170*bda89588Sjp151216 		}
1171f48205beScasper 
1172*bda89588Sjp151216 		if (cr->cr_ksid->kr_sidx[KSID_GROUP].ks_id > MAXUID) {
1173*bda89588Sjp151216 			eph_zsd = get_ephemeral_zsd(crgetzone(cr));
1174*bda89588Sjp151216 			return (eph_zsd->eph_nobody);
1175*bda89588Sjp151216 		}
1176f48205beScasper 	}
1177f48205beScasper 
1178f48205beScasper 	return ((cred_t *)cr);
1179f48205beScasper }
1180f48205beScasper 
1181f48205beScasper /* index should be in range for a ksidindex_t */
1182f48205beScasper void
1183f48205beScasper crsetsid(cred_t *cr, ksid_t *ksp, int index)
1184f48205beScasper {
1185f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1186f48205beScasper 	ASSERT(index >= 0 && index < KSID_COUNT);
1187f48205beScasper 	if (cr->cr_ksid == NULL && ksp == NULL)
1188f48205beScasper 		return;
1189f48205beScasper 	cr->cr_ksid = kcrsid_setsid(cr->cr_ksid, ksp, index);
1190f48205beScasper }
1191f48205beScasper 
1192f48205beScasper void
1193f48205beScasper crsetsidlist(cred_t *cr, ksidlist_t *ksl)
1194f48205beScasper {
1195f48205beScasper 	ASSERT(cr->cr_ref <= 2);
1196f48205beScasper 	if (cr->cr_ksid == NULL && ksl == NULL)
1197f48205beScasper 		return;
1198f48205beScasper 	cr->cr_ksid = kcrsid_setsidlist(cr->cr_ksid, ksl);
1199f48205beScasper }
1200f48205beScasper 
1201f48205beScasper ksid_t *
1202f48205beScasper crgetsid(const cred_t *cr, int i)
1203f48205beScasper {
1204f48205beScasper 	ASSERT(i >= 0 && i < KSID_COUNT);
1205f48205beScasper 	if (cr->cr_ksid != NULL && cr->cr_ksid->kr_sidx[i].ks_domain)
1206f48205beScasper 		return ((ksid_t *)&cr->cr_ksid->kr_sidx[i]);
1207f48205beScasper 	return (NULL);
1208f48205beScasper }
1209f48205beScasper 
1210f48205beScasper ksidlist_t *
1211f48205beScasper crgetsidlist(const cred_t *cr)
1212f48205beScasper {
1213da6c28aaSamw 	if (cr->cr_ksid != NULL)
121428a151a0Scasper 		return (cr->cr_ksid->kr_sidlist);
1215f48205beScasper 	return (NULL);
1216f48205beScasper }
1217da6c28aaSamw 
1218da6c28aaSamw /*
1219da6c28aaSamw  * Interface to set the effective and permitted privileges for
1220da6c28aaSamw  * a credential; this interface does no security checks and is
1221da6c28aaSamw  * intended for kernel (file)servers creating credentials with
1222da6c28aaSamw  * specific privileges.
1223da6c28aaSamw  */
1224da6c28aaSamw int
1225da6c28aaSamw crsetpriv(cred_t *cr, ...)
1226da6c28aaSamw {
1227da6c28aaSamw 	va_list ap;
1228da6c28aaSamw 	const char *privnm;
1229da6c28aaSamw 
1230da6c28aaSamw 	ASSERT(cr->cr_ref <= 2);
1231da6c28aaSamw 
1232da6c28aaSamw 	priv_set_PA(cr);
1233da6c28aaSamw 
1234da6c28aaSamw 	va_start(ap, cr);
1235da6c28aaSamw 
1236da6c28aaSamw 	while ((privnm = va_arg(ap, const char *)) != NULL) {
1237da6c28aaSamw 		int priv = priv_getbyname(privnm, 0);
1238da6c28aaSamw 		if (priv < 0)
1239da6c28aaSamw 			return (-1);
1240da6c28aaSamw 
1241da6c28aaSamw 		priv_addset(&CR_PPRIV(cr), priv);
1242da6c28aaSamw 		priv_addset(&CR_EPRIV(cr), priv);
1243da6c28aaSamw 	}
1244da6c28aaSamw 	priv_adjust_PA(cr);
1245da6c28aaSamw 	va_end(ap);
1246da6c28aaSamw 	return (0);
1247da6c28aaSamw }
1248