xref: /titanic_50/usr/src/uts/common/os/priv.c (revision d93c0b4cf0eaea69e1c297b8812a7474feb926b7)
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
530cbf0d2SCasper H.S. Dik  * Common Development and Distribution License (the "License").
630cbf0d2SCasper H.S. Dik  * 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 /*
22634e26ecSCasper H.S. Dik  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Privilege implementation.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * This file provides the infrastructure for privilege sets and limits
307c478bd9Sstevel@tonic-gate  * the number of files that requires to include <sys/cred_impl.h> and/or
317c478bd9Sstevel@tonic-gate  * <sys/priv_impl.h>.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * The Solaris privilege mechanism has been designed in a
347c478bd9Sstevel@tonic-gate  * future proof manner.  While the kernel may use fixed size arrays
357c478bd9Sstevel@tonic-gate  * and fixed bitmasks and bit values, the representation of those
367c478bd9Sstevel@tonic-gate  * is kernel private.  All external interfaces as well as K-to-K interfaces
377c478bd9Sstevel@tonic-gate  * have been constructed in a manner to provide the maximum flexibility.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * There can be X privilege sets each containing Y 32 bit words.
407c478bd9Sstevel@tonic-gate  * <X, Y> are constant for a kernel invocation.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * As a consequence, all privilege set manipulation happens in functions
437c478bd9Sstevel@tonic-gate  * below.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/systm.h>
487c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
547c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
557c478bd9Sstevel@tonic-gate #include <sys/policy.h>
567c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
577c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
587c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Privilege name to number mapping table consists in the generated
627c478bd9Sstevel@tonic-gate  * priv_const.c file.  This lock protects against updates of the privilege
637c478bd9Sstevel@tonic-gate  * names and counts; all other priv_info fields are read-only.
647c478bd9Sstevel@tonic-gate  * The actual protected values are:
657c478bd9Sstevel@tonic-gate  *	global variable nprivs
667c478bd9Sstevel@tonic-gate  *	the priv_max field
677c478bd9Sstevel@tonic-gate  *	the priv_names field
687c478bd9Sstevel@tonic-gate  *	the priv names info item (cnt/strings)
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate krwlock_t privinfo_lock;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static boolean_t priv_valid(const cred_t *);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate priv_set_t priv_fullset;	/* set of all privileges */
757c478bd9Sstevel@tonic-gate priv_set_t priv_unsafe;	/* unsafe to exec set-uid root if these are not in L */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Privilege initialization functions.
797c478bd9Sstevel@tonic-gate  * Called from common/os/cred.c when cred_init is called.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
priv_init(void)837c478bd9Sstevel@tonic-gate priv_init(void)
847c478bd9Sstevel@tonic-gate {
85*d93c0b4cSCasper H.S. Dik #ifdef DEBUG
86*d93c0b4cSCasper H.S. Dik 	int alloc_test_priv = 1;
87*d93c0b4cSCasper H.S. Dik #else
88*d93c0b4cSCasper H.S. Dik 	int alloc_test_priv = priv_debug;
89*d93c0b4cSCasper H.S. Dik #endif
907c478bd9Sstevel@tonic-gate 	rw_init(&privinfo_lock, NULL, RW_DRIVER, NULL);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	PRIV_BASIC_ASSERT(priv_basic);
937c478bd9Sstevel@tonic-gate 	PRIV_UNSAFE_ASSERT(&priv_unsafe);
947c478bd9Sstevel@tonic-gate 	priv_fillset(&priv_fullset);
957c478bd9Sstevel@tonic-gate 
96634e26ecSCasper H.S. Dik 	/*
97*d93c0b4cSCasper H.S. Dik 	 * When booting with priv_debug set or in a DEBUG kernel, then we'll
98*d93c0b4cSCasper H.S. Dik 	 * add an additional basic privilege and we verify that it is always
99*d93c0b4cSCasper H.S. Dik 	 * present in E.
100634e26ecSCasper H.S. Dik 	 */
101*d93c0b4cSCasper H.S. Dik 	if (alloc_test_priv != 0 &&
102634e26ecSCasper H.S. Dik 	    (priv_basic_test = priv_getbyname("basic_test", PRIV_ALLOC)) >= 0) {
103634e26ecSCasper H.S. Dik 		priv_addset(priv_basic, priv_basic_test);
104634e26ecSCasper H.S. Dik 	}
105634e26ecSCasper H.S. Dik 
1067c478bd9Sstevel@tonic-gate 	devpolicy_init();
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* Utility functions: privilege sets as opaque data types */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Guts of prgetprivsize.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate int
priv_prgetprivsize(prpriv_t * tmpl)1157c478bd9Sstevel@tonic-gate priv_prgetprivsize(prpriv_t *tmpl)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	return (sizeof (prpriv_t) +
1187c478bd9Sstevel@tonic-gate 	    PRIV_SETBYTES - sizeof (priv_chunk_t) +
1197c478bd9Sstevel@tonic-gate 	    (tmpl ? tmpl->pr_infosize : priv_info->priv_infosize));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Guts of prgetpriv.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate void
cred2prpriv(const cred_t * cp,prpriv_t * pr)1267c478bd9Sstevel@tonic-gate cred2prpriv(const cred_t *cp, prpriv_t *pr)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	priv_set_t *psa;
1297c478bd9Sstevel@tonic-gate 	int i;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	pr->pr_nsets = PRIV_NSET;
1327c478bd9Sstevel@tonic-gate 	pr->pr_setsize = PRIV_SETSIZE;
1337c478bd9Sstevel@tonic-gate 	pr->pr_infosize = priv_info->priv_infosize;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	psa = (priv_set_t *)pr->pr_sets;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_NSET; i++)
1387c478bd9Sstevel@tonic-gate 		psa[i] = *priv_getset(cp, i);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	priv_getinfo(cp, (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * Guts of pr_spriv:
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * Set the privileges of a process.
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  * In order to set the privileges, the setting process will need to
1497c478bd9Sstevel@tonic-gate  * have those privileges in its effective set in order to prevent
1507c478bd9Sstevel@tonic-gate  * specially privileged processes to easily gain additional privileges.
1517c478bd9Sstevel@tonic-gate  * Pre-existing privileges can be retained.  To change any privileges,
1527c478bd9Sstevel@tonic-gate  * PRIV_PROC_OWNER needs to be asserted.
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * In formula:
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  *	S' <= S || S' <= S + Ea
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * the new set must either be subset of the old set or a subset of
1597c478bd9Sstevel@tonic-gate  * the oldset merged with the effective set of the acting process; or just:
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  *	S' <= S + Ea
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * It's not legal to grow the limit set this way.
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate int
priv_pr_spriv(proc_t * p,prpriv_t * prpriv,const cred_t * cr)1677c478bd9Sstevel@tonic-gate priv_pr_spriv(proc_t *p, prpriv_t *prpriv, const cred_t *cr)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	cred_t *oldcred;
1707c478bd9Sstevel@tonic-gate 	cred_t *newcred;
1717c478bd9Sstevel@tonic-gate 	int i;
1727c478bd9Sstevel@tonic-gate 	int err = EPERM;
1737c478bd9Sstevel@tonic-gate 	cred_priv_t *cp, *ocp;
1747c478bd9Sstevel@tonic-gate 	priv_set_t eset;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Set must have proper dimension; infosize must be absent
1807c478bd9Sstevel@tonic-gate 	 * or properly sized.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	if (prpriv->pr_nsets != PRIV_NSET ||
1837c478bd9Sstevel@tonic-gate 	    prpriv->pr_setsize != PRIV_SETSIZE ||
1847c478bd9Sstevel@tonic-gate 	    (prpriv->pr_infosize & (sizeof (uint32_t) - 1)) != 0 ||
1857c478bd9Sstevel@tonic-gate 	    prpriv->pr_infosize > priv_info->priv_infosize ||
1867c478bd9Sstevel@tonic-gate 	    prpriv->pr_infosize < 0)
1877c478bd9Sstevel@tonic-gate 		return (EINVAL);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (priv_proc_cred_perm(cr, p, &oldcred, VWRITE) != 0) {
1927c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1937c478bd9Sstevel@tonic-gate 		return (EPERM);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	newcred = crdup(oldcred);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* Copy the privilege sets from prpriv to newcred */
1997c478bd9Sstevel@tonic-gate 	bcopy(prpriv->pr_sets, CR_PRIVSETS(newcred), PRIV_SETBYTES);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	cp = &newcred->cr_priv;
2027c478bd9Sstevel@tonic-gate 	ocp = &oldcred->cr_priv;
2037c478bd9Sstevel@tonic-gate 	eset = CR_OEPRIV(cr);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	priv_intersect(&CR_LPRIV(oldcred), &eset);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Verify the constraints laid out:
2097c478bd9Sstevel@tonic-gate 	 * for the limit set, we require that the new set is a subset
2107c478bd9Sstevel@tonic-gate 	 * of the old limit set.
2117c478bd9Sstevel@tonic-gate 	 * for all other sets, we require that the new set is either a
2127c478bd9Sstevel@tonic-gate 	 * subset of the old set or a subset of the intersection of
2137c478bd9Sstevel@tonic-gate 	 * the old limit set and the effective set of the acting process.
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_NSET; i++)
2167c478bd9Sstevel@tonic-gate 		if (!priv_issubset(&cp->crprivs[i], &ocp->crprivs[i]) &&
2177c478bd9Sstevel@tonic-gate 		    (i == PRIV_LIMIT || !priv_issubset(&cp->crprivs[i], &eset)))
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	crfree(oldcred);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (i < PRIV_NSET || !priv_valid(newcred))
2237c478bd9Sstevel@tonic-gate 		goto err;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* Load the settable privilege information */
2267c478bd9Sstevel@tonic-gate 	if (prpriv->pr_infosize > 0) {
2277c478bd9Sstevel@tonic-gate 		char *x = (char *)prpriv + PRIV_PRPRIV_INFO_OFFSET(prpriv);
2287c478bd9Sstevel@tonic-gate 		char *lastx = x + prpriv->pr_infosize;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		while (x < lastx) {
2317c478bd9Sstevel@tonic-gate 			priv_info_t *pi = (priv_info_t *)x;
2327c478bd9Sstevel@tonic-gate 			priv_info_uint_t *pii;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 			switch (pi->priv_info_type) {
2357c478bd9Sstevel@tonic-gate 			case PRIV_INFO_FLAGS:
2367c478bd9Sstevel@tonic-gate 				pii = (priv_info_uint_t *)x;
2377c478bd9Sstevel@tonic-gate 				if (pii->info.priv_info_size != sizeof (*pii)) {
2387c478bd9Sstevel@tonic-gate 					err = EINVAL;
2397c478bd9Sstevel@tonic-gate 					goto err;
2407c478bd9Sstevel@tonic-gate 				}
2417c478bd9Sstevel@tonic-gate 				CR_FLAGS(newcred) &= ~PRIV_USER;
2427c478bd9Sstevel@tonic-gate 				CR_FLAGS(newcred) |= (pii->val & PRIV_USER);
2437c478bd9Sstevel@tonic-gate 				break;
2447c478bd9Sstevel@tonic-gate 			default:
2457c478bd9Sstevel@tonic-gate 				err = EINVAL;
2467c478bd9Sstevel@tonic-gate 				goto err;
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 			/* Guarantee alignment and forward progress */
2497c478bd9Sstevel@tonic-gate 			if ((pi->priv_info_size & (sizeof (uint32_t) - 1)) ||
2507c478bd9Sstevel@tonic-gate 			    pi->priv_info_size < sizeof (*pi) ||
2517c478bd9Sstevel@tonic-gate 			    lastx - x > pi->priv_info_size) {
2527c478bd9Sstevel@tonic-gate 				err = EINVAL;
2537c478bd9Sstevel@tonic-gate 				goto err;
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			x += pi->priv_info_size;
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * We'll try to copy the privilege aware flag; but since the
2627c478bd9Sstevel@tonic-gate 	 * privileges sets are all individually set, they are set
2637c478bd9Sstevel@tonic-gate 	 * as if we're privilege aware.  If PRIV_AWARE wasn't set
2647c478bd9Sstevel@tonic-gate 	 * or was explicitely unset, we need to set the flag and then
2657c478bd9Sstevel@tonic-gate 	 * try to get rid of it.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(newcred) & PRIV_AWARE) == 0) {
2687c478bd9Sstevel@tonic-gate 		CR_FLAGS(newcred) |= PRIV_AWARE;
2697c478bd9Sstevel@tonic-gate 		priv_adjust_PA(newcred);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
2737c478bd9Sstevel@tonic-gate 	oldcred = p->p_cred;
2747c478bd9Sstevel@tonic-gate 	p->p_cred = newcred;
2757c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
2767c478bd9Sstevel@tonic-gate 	crfree(oldcred);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2797c478bd9Sstevel@tonic-gate 	return (0);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate err:
2827c478bd9Sstevel@tonic-gate 	crfree(newcred);
2837c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2847c478bd9Sstevel@tonic-gate 	return (err);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate priv_impl_info_t
priv_hold_implinfo(void)2887c478bd9Sstevel@tonic-gate *priv_hold_implinfo(void)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	rw_enter(&privinfo_lock, RW_READER);
2917c478bd9Sstevel@tonic-gate 	return (priv_info);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate void
priv_release_implinfo(void)2957c478bd9Sstevel@tonic-gate priv_release_implinfo(void)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	rw_exit(&privinfo_lock);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate size_t
priv_get_implinfo_size(void)3017c478bd9Sstevel@tonic-gate priv_get_implinfo_size(void)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	return (privinfosize);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * Return the nth privilege set
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate const priv_set_t *
priv_getset(const cred_t * cr,int set)3117c478bd9Sstevel@tonic-gate priv_getset(const cred_t *cr, int set)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	ASSERT(PRIV_VALIDSET(set));
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & PRIV_AWARE) == 0)
3167c478bd9Sstevel@tonic-gate 		switch (set) {
3177c478bd9Sstevel@tonic-gate 		case PRIV_EFFECTIVE:
3187c478bd9Sstevel@tonic-gate 			return (&CR_OEPRIV(cr));
3197c478bd9Sstevel@tonic-gate 		case PRIV_PERMITTED:
3207c478bd9Sstevel@tonic-gate 			return (&CR_OPPRIV(cr));
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	return (&CR_PRIVS(cr)->crprivs[set]);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Buf must be allocated by caller and contain sufficient space to
3277c478bd9Sstevel@tonic-gate  * contain all additional info structures using priv_info.priv_infosize.
3287c478bd9Sstevel@tonic-gate  * The buffer must be properly aligned.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3317c478bd9Sstevel@tonic-gate void
priv_getinfo(const cred_t * cr,void * buf)3327c478bd9Sstevel@tonic-gate priv_getinfo(const cred_t *cr, void *buf)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	struct priv_info_uint *ii;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	ii = buf;
3377c478bd9Sstevel@tonic-gate 	ii->val = CR_FLAGS(cr);
3387c478bd9Sstevel@tonic-gate 	ii->info.priv_info_size = (uint32_t)sizeof (*ii);
3397c478bd9Sstevel@tonic-gate 	ii->info.priv_info_type = PRIV_INFO_FLAGS;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate int
priv_getbyname(const char * name,uint_t flag)3437c478bd9Sstevel@tonic-gate priv_getbyname(const char *name, uint_t flag)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	int i;
3467c478bd9Sstevel@tonic-gate 	int wheld = 0;
3477c478bd9Sstevel@tonic-gate 	int len;
3487c478bd9Sstevel@tonic-gate 	char *p;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (flag != 0 && flag != PRIV_ALLOC)
3517c478bd9Sstevel@tonic-gate 		return (-EINVAL);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (strncasecmp(name, "priv_", 5) == 0)
3547c478bd9Sstevel@tonic-gate 		name += 5;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	rw_enter(&privinfo_lock, RW_READER);
3577c478bd9Sstevel@tonic-gate rescan:
3587c478bd9Sstevel@tonic-gate 	for (i = 0; i < nprivs; i++)
3597c478bd9Sstevel@tonic-gate 		if (strcasecmp(priv_names[i], name) == 0) {
3607c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3617c478bd9Sstevel@tonic-gate 			return (i);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (!wheld) {
3667c478bd9Sstevel@tonic-gate 		if (!(flag & PRIV_ALLOC)) {
3677c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3687c478bd9Sstevel@tonic-gate 			return (-EINVAL);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		/* check length, validity and available space */
3727c478bd9Sstevel@tonic-gate 		len = strlen(name) + 1;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if (len > PRIVNAME_MAX) {
3757c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3767c478bd9Sstevel@tonic-gate 			return (-ENAMETOOLONG);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		for (p = (char *)name; *p != '\0'; p++) {
3807c478bd9Sstevel@tonic-gate 			char c = *p;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 			if (!((c >= 'A' && c <= 'Z') ||
3837c478bd9Sstevel@tonic-gate 			    (c >= 'a' && c <= 'z') ||
3847c478bd9Sstevel@tonic-gate 			    (c >= '0' && c <= '9') ||
3857c478bd9Sstevel@tonic-gate 			    c == '_')) {
3867c478bd9Sstevel@tonic-gate 				rw_exit(&privinfo_lock);
3877c478bd9Sstevel@tonic-gate 				return (-EINVAL);
3887c478bd9Sstevel@tonic-gate 			}
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		if (!rw_tryupgrade(&privinfo_lock)) {
3927c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3937c478bd9Sstevel@tonic-gate 			rw_enter(&privinfo_lock, RW_WRITER);
3947c478bd9Sstevel@tonic-gate 			wheld = 1;
3957c478bd9Sstevel@tonic-gate 			/* Someone may have added our privilege */
3967c478bd9Sstevel@tonic-gate 			goto rescan;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (nprivs == MAX_PRIVILEGE || len + privbytes > maxprivbytes) {
4017c478bd9Sstevel@tonic-gate 		rw_exit(&privinfo_lock);
4027c478bd9Sstevel@tonic-gate 		return (-ENOMEM);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	priv_names[i] = p = priv_str + privbytes;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	bcopy(name, p, len);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/* make the priv_names[i] and privilege name globally visible */
4107c478bd9Sstevel@tonic-gate 	membar_producer();
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/* adjust priv count and bytes count */
4137c478bd9Sstevel@tonic-gate 	priv_ninfo->cnt = priv_info->priv_max = ++nprivs;
4147c478bd9Sstevel@tonic-gate 	privbytes += len;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	rw_exit(&privinfo_lock);
4177c478bd9Sstevel@tonic-gate 	return (i);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * We can't afford locking the privileges here because of the locations
4227c478bd9Sstevel@tonic-gate  * we call this from; so we make sure that the privileges table
4237c478bd9Sstevel@tonic-gate  * is visible to us; it is made visible before the value of nprivs is
4247c478bd9Sstevel@tonic-gate  * updated.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate const char *
priv_getbynum(int priv)4277c478bd9Sstevel@tonic-gate priv_getbynum(int priv)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	int maxpriv = nprivs;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	membar_consumer();
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (priv >= 0 && priv < maxpriv)
4347c478bd9Sstevel@tonic-gate 		return (priv_names[priv]);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	return (NULL);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate const char *
priv_getsetbynum(int setno)4407c478bd9Sstevel@tonic-gate priv_getsetbynum(int setno)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	if (!PRIV_VALIDSET(setno))
4437c478bd9Sstevel@tonic-gate 		return (NULL);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	return (priv_setnames[setno]);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate  * Privilege sanity checking when setting: E <= P.
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate static boolean_t
priv_valid(const cred_t * cr)4527c478bd9Sstevel@tonic-gate priv_valid(const cred_t *cr)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	return (priv_issubset(&CR_EPRIV(cr), &CR_PPRIV(cr)));
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Privilege manipulation functions
4597c478bd9Sstevel@tonic-gate  *
4607c478bd9Sstevel@tonic-gate  * Without knowing the details of the privilege set implementation,
4617c478bd9Sstevel@tonic-gate  * opaque pointers can be used to manipulate sets at will.
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate void
priv_emptyset(priv_set_t * set)4647c478bd9Sstevel@tonic-gate priv_emptyset(priv_set_t *set)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	bzero(set, sizeof (*set));
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate void
priv_fillset(priv_set_t * set)4707c478bd9Sstevel@tonic-gate priv_fillset(priv_set_t *set)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	int i;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/* memset? */
4757c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++)
4767c478bd9Sstevel@tonic-gate 		set->pbits[i] = ~(priv_chunk_t)0;
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate void
priv_addset(priv_set_t * set,int priv)4807c478bd9Sstevel@tonic-gate priv_addset(priv_set_t *set, int priv)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
4837c478bd9Sstevel@tonic-gate 	__PRIV_ASSERT(set, priv);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate void
priv_delset(priv_set_t * set,int priv)4877c478bd9Sstevel@tonic-gate priv_delset(priv_set_t *set, int priv)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
4907c478bd9Sstevel@tonic-gate 	__PRIV_CLEAR(set, priv);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate boolean_t
priv_ismember(const priv_set_t * set,int priv)4947c478bd9Sstevel@tonic-gate priv_ismember(const priv_set_t *set, int priv)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
4977c478bd9Sstevel@tonic-gate 	return (__PRIV_ISASSERT(set, priv) ? B_TRUE : B_FALSE);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate #define	PRIV_TEST_BODY(test) \
5017c478bd9Sstevel@tonic-gate 	int i; \
5027c478bd9Sstevel@tonic-gate \
5037c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++) \
5047c478bd9Sstevel@tonic-gate 		if (!(test)) \
5057c478bd9Sstevel@tonic-gate 			return (B_FALSE); \
5067c478bd9Sstevel@tonic-gate \
5077c478bd9Sstevel@tonic-gate 	return (B_TRUE)
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate boolean_t
priv_isequalset(const priv_set_t * a,const priv_set_t * b)5107c478bd9Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	return ((boolean_t)(bcmp(a, b, sizeof (*a)) == 0));
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate boolean_t
priv_isemptyset(const priv_set_t * set)5167c478bd9Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY(set->pbits[i] == 0);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate boolean_t
priv_isfullset(const priv_set_t * set)5227c478bd9Sstevel@tonic-gate priv_isfullset(const priv_set_t *set)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY(set->pbits[i] == ~(priv_chunk_t)0);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * Return true if a is a subset of b
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate boolean_t
priv_issubset(const priv_set_t * a,const priv_set_t * b)5317c478bd9Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY((a->pbits[i] | b->pbits[i]) == b->pbits[i]);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate #define	PRIV_CHANGE_BODY(a, op, b) \
5377c478bd9Sstevel@tonic-gate 	int i; \
5387c478bd9Sstevel@tonic-gate \
5397c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++) \
5407c478bd9Sstevel@tonic-gate 		a->pbits[i] op b->pbits[i]
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /* B = A ^ B */
5437c478bd9Sstevel@tonic-gate void
priv_intersect(const priv_set_t * a,priv_set_t * b)5447c478bd9Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	/* CSTYLED */
5477c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, &=, a);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /* B = A v B */
5517c478bd9Sstevel@tonic-gate void
priv_union(const priv_set_t * a,priv_set_t * b)5527c478bd9Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	/* CSTYLED */
5557c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, |=, a);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /* A = ! A */
5597c478bd9Sstevel@tonic-gate void
priv_inverse(priv_set_t * a)5607c478bd9Sstevel@tonic-gate priv_inverse(priv_set_t *a)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(a, = ~, a);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Can the source cred act on the target credential?
5677c478bd9Sstevel@tonic-gate  *
5687c478bd9Sstevel@tonic-gate  * We will you allow to gain uids this way but not privileges.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate int
priv_proc_cred_perm(const cred_t * scr,proc_t * tp,cred_t ** pcr,int mode)5717c478bd9Sstevel@tonic-gate priv_proc_cred_perm(const cred_t *scr, proc_t *tp, cred_t **pcr, int mode)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	const priv_set_t *eset;
5747c478bd9Sstevel@tonic-gate 	int idsmatch;
5757c478bd9Sstevel@tonic-gate 	cred_t *tcr;
5767c478bd9Sstevel@tonic-gate 	int res = 0;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/* prevent the cred from going away */
5797c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
5807c478bd9Sstevel@tonic-gate 	crhold(tcr = tp->p_cred);
5817c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
5827c478bd9Sstevel@tonic-gate 
58330cbf0d2SCasper H.S. Dik 	if (scr == tcr && !(tp->p_flag & SNOCD))
5847c478bd9Sstevel@tonic-gate 		goto out;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	idsmatch = (scr->cr_uid == tcr->cr_uid &&
5877c478bd9Sstevel@tonic-gate 	    scr->cr_uid == tcr->cr_ruid &&
5887c478bd9Sstevel@tonic-gate 	    scr->cr_uid == tcr->cr_suid &&
5897c478bd9Sstevel@tonic-gate 	    scr->cr_gid == tcr->cr_gid &&
5907c478bd9Sstevel@tonic-gate 	    scr->cr_gid == tcr->cr_rgid &&
5917c478bd9Sstevel@tonic-gate 	    scr->cr_gid == tcr->cr_sgid &&
5927c478bd9Sstevel@tonic-gate 	    !(tp->p_flag & SNOCD));
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * Source credential must have the proc_zone privilege if referencing
5967c478bd9Sstevel@tonic-gate 	 * a process in another zone.
5977c478bd9Sstevel@tonic-gate 	 */
5987c478bd9Sstevel@tonic-gate 	if (scr->cr_zone != tcr->cr_zone && secpolicy_proc_zone(scr) != 0) {
5997c478bd9Sstevel@tonic-gate 		res = EACCES;
6007c478bd9Sstevel@tonic-gate 		goto out;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (!(mode & VWRITE)) {
6047c478bd9Sstevel@tonic-gate 		if (!idsmatch && secpolicy_proc_owner(scr, tcr, 0) != 0)
6057c478bd9Sstevel@tonic-gate 			res = EACCES;
6067c478bd9Sstevel@tonic-gate 		goto out;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * For writing, the effective set of scr must dominate all sets of tcr,
6117c478bd9Sstevel@tonic-gate 	 * We test Pt <= Es (Et <= Pt so no need to test) and It <= Es
6127c478bd9Sstevel@tonic-gate 	 * The Limit set of scr must be a superset of the limitset of
6137c478bd9Sstevel@tonic-gate 	 * tcr.
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	eset = &CR_OEPRIV(scr);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (!priv_issubset(&CR_IPRIV(tcr), eset) ||
6187c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_OPPRIV(tcr), eset) ||
6197c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_LPRIV(tcr), &CR_LPRIV(scr)) ||
6207c478bd9Sstevel@tonic-gate 	    !idsmatch && secpolicy_proc_owner(scr, tcr, mode) != 0)
6217c478bd9Sstevel@tonic-gate 		res = EACCES;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate out:
6247c478bd9Sstevel@tonic-gate 	if (res == 0 && pcr != NULL)
6257c478bd9Sstevel@tonic-gate 		*pcr = tcr;
6267c478bd9Sstevel@tonic-gate 	else
6277c478bd9Sstevel@tonic-gate 		crfree(tcr);
6287c478bd9Sstevel@tonic-gate 	return (res);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*
632982b4ad2SCasper H.S. Dik  * Set the privilege aware bit, adding L to E/P if necessary.
633982b4ad2SCasper H.S. Dik  * Each time we set it, we also clear PRIV_AWARE_RESET.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate void
priv_set_PA(cred_t * cr)6367c478bd9Sstevel@tonic-gate priv_set_PA(cred_t *cr)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6397c478bd9Sstevel@tonic-gate 
640982b4ad2SCasper H.S. Dik 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) == PRIV_AWARE)
6417c478bd9Sstevel@tonic-gate 		return;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	CR_FLAGS(cr) |= PRIV_AWARE;
644982b4ad2SCasper H.S. Dik 	CR_FLAGS(cr) &= ~PRIV_AWARE_RESET;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0)
6477c478bd9Sstevel@tonic-gate 		priv_union(&CR_LPRIV(cr), &CR_EPRIV(cr));
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 || cr->cr_suid == 0 || cr->cr_ruid == 0)
6507c478bd9Sstevel@tonic-gate 		priv_union(&CR_LPRIV(cr), &CR_PPRIV(cr));
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate boolean_t
priv_can_clear_PA(const cred_t * cr)6547c478bd9Sstevel@tonic-gate priv_can_clear_PA(const cred_t *cr)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * We can clear PA in the following cases:
6587c478bd9Sstevel@tonic-gate 	 *
6597c478bd9Sstevel@tonic-gate 	 * None of the uids are 0.
6607c478bd9Sstevel@tonic-gate 	 * Any uid == 0 and P == L and (Euid != 0 or E == L)
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	return ((cr->cr_suid != 0 && cr->cr_ruid != 0 && cr->cr_uid != 0) ||
6637c478bd9Sstevel@tonic-gate 	    priv_isequalset(&CR_PPRIV(cr), &CR_LPRIV(cr)) &&
6647c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != 0 || priv_isequalset(&CR_EPRIV(cr), &CR_LPRIV(cr))));
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * Clear privilege aware bit if it is an idempotent operation and by
6697c478bd9Sstevel@tonic-gate  * clearing it the process cannot get to uid 0 and all privileges.
6707c478bd9Sstevel@tonic-gate  *
6717c478bd9Sstevel@tonic-gate  * This function should be called with caution as it may cause "E" to be
6727c478bd9Sstevel@tonic-gate  * lost once a processes assumes euid 0 again.
6737c478bd9Sstevel@tonic-gate  */
6747c478bd9Sstevel@tonic-gate void
priv_adjust_PA(cred_t * cr)6757c478bd9Sstevel@tonic-gate priv_adjust_PA(cred_t *cr)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (!(CR_FLAGS(cr) & PRIV_AWARE) ||
680982b4ad2SCasper H.S. Dik 	    !priv_can_clear_PA(cr)) {
681982b4ad2SCasper H.S. Dik 		CR_FLAGS(cr) &= ~PRIV_AWARE_RESET;
6827c478bd9Sstevel@tonic-gate 		return;
683982b4ad2SCasper H.S. Dik 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_AWARE_INHERIT)
6867c478bd9Sstevel@tonic-gate 		return;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * We now need to adjust P/E in those cases when uids
6907c478bd9Sstevel@tonic-gate 	 * are zero; the rules are P' = I & L, E' = I & L;
6917c478bd9Sstevel@tonic-gate 	 * but since P = L and E = L, we can use P &= I, E &= I,
6927c478bd9Sstevel@tonic-gate 	 * depending on which uids are 0.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if (cr->cr_suid == 0 || cr->cr_ruid == 0 || cr->cr_uid == 0) {
6957c478bd9Sstevel@tonic-gate 		if (cr->cr_uid == 0)
6967c478bd9Sstevel@tonic-gate 			priv_intersect(&CR_IPRIV(cr), &CR_EPRIV(cr));
6977c478bd9Sstevel@tonic-gate 		priv_intersect(&CR_IPRIV(cr), &CR_PPRIV(cr));
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
700982b4ad2SCasper H.S. Dik 	CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET);
701982b4ad2SCasper H.S. Dik }
702982b4ad2SCasper H.S. Dik 
703982b4ad2SCasper H.S. Dik /*
704982b4ad2SCasper H.S. Dik  * Reset privilege aware bit if so requested by setting the PRIV_AWARE_RESET
705982b4ad2SCasper H.S. Dik  * flag.
706982b4ad2SCasper H.S. Dik  */
707982b4ad2SCasper H.S. Dik void
priv_reset_PA(cred_t * cr,boolean_t finalize)708982b4ad2SCasper H.S. Dik priv_reset_PA(cred_t *cr, boolean_t finalize)
709982b4ad2SCasper H.S. Dik {
710982b4ad2SCasper H.S. Dik 	ASSERT(cr->cr_ref <= 2);
711982b4ad2SCasper H.S. Dik 
712982b4ad2SCasper H.S. Dik 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_RESET)) !=
713982b4ad2SCasper H.S. Dik 	    (PRIV_AWARE|PRIV_AWARE_RESET)) {
714982b4ad2SCasper H.S. Dik 		CR_FLAGS(cr) &= ~PRIV_AWARE_RESET;
715982b4ad2SCasper H.S. Dik 		return;
716982b4ad2SCasper H.S. Dik 	}
717982b4ad2SCasper H.S. Dik 
718982b4ad2SCasper H.S. Dik 	/*
719982b4ad2SCasper H.S. Dik 	 * When PRIV_AWARE_RESET is enabled, any change of uids causes
720982b4ad2SCasper H.S. Dik 	 * a change to the P and E sets.  Bracketing with
721982b4ad2SCasper H.S. Dik 	 * seteuid(0) ... seteuid(uid)/setreuid(-1, 0) .. setreuid(-1, uid)
722982b4ad2SCasper H.S. Dik 	 * will cause the privilege sets "do the right thing.".
723982b4ad2SCasper H.S. Dik 	 * When the change of the uid is "final", e.g., by using setuid(uid),
724982b4ad2SCasper H.S. Dik 	 * or setreuid(uid, uid) or when the last set*uid() call causes all
725982b4ad2SCasper H.S. Dik 	 * uids to be the same, we set P and E to I & L, like when you exec.
726982b4ad2SCasper H.S. Dik 	 * We make an exception when all the uids are 0; this is required
727982b4ad2SCasper H.S. Dik 	 * when we login as root as in that particular case we cannot
728982b4ad2SCasper H.S. Dik 	 * make a distinction between seteuid(0) and seteuid(uid).
729982b4ad2SCasper H.S. Dik 	 * We rely on seteuid/setreuid/setuid to tell us with the
730982b4ad2SCasper H.S. Dik 	 * "finalize" argument that we no longer expect new uid changes,
731982b4ad2SCasper H.S. Dik 	 * cf. setreuid(uid, uid) and setuid(uid).
732982b4ad2SCasper H.S. Dik 	 */
733982b4ad2SCasper H.S. Dik 	if (cr->cr_suid == cr->cr_ruid && cr->cr_suid == cr->cr_uid) {
734982b4ad2SCasper H.S. Dik 		if (finalize || cr->cr_uid != 0) {
735982b4ad2SCasper H.S. Dik 			CR_EPRIV(cr) = CR_IPRIV(cr);
736982b4ad2SCasper H.S. Dik 			priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr));
737982b4ad2SCasper H.S. Dik 			CR_PPRIV(cr) = CR_EPRIV(cr);
738982b4ad2SCasper H.S. Dik 			CR_FLAGS(cr) &= ~(PRIV_AWARE|PRIV_AWARE_RESET);
739982b4ad2SCasper H.S. Dik 		} else {
740982b4ad2SCasper H.S. Dik 			CR_EPRIV(cr) = CR_PPRIV(cr);
741982b4ad2SCasper H.S. Dik 		}
742982b4ad2SCasper H.S. Dik 	} else if (cr->cr_uid != 0 && (cr->cr_ruid == 0 || cr->cr_suid == 0)) {
743982b4ad2SCasper H.S. Dik 		CR_EPRIV(cr) = CR_IPRIV(cr);
744982b4ad2SCasper H.S. Dik 		priv_intersect(&CR_LPRIV(cr), &CR_EPRIV(cr));
745982b4ad2SCasper H.S. Dik 	}
7467c478bd9Sstevel@tonic-gate }
747