xref: /titanic_53/usr/src/uts/common/os/priv.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Privilege implementation.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * This file provides the infrastructure for privilege sets and limits
33*7c478bd9Sstevel@tonic-gate  * the number of files that requires to include <sys/cred_impl.h> and/or
34*7c478bd9Sstevel@tonic-gate  * <sys/priv_impl.h>.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * The Solaris privilege mechanism has been designed in a
37*7c478bd9Sstevel@tonic-gate  * future proof manner.  While the kernel may use fixed size arrays
38*7c478bd9Sstevel@tonic-gate  * and fixed bitmasks and bit values, the representation of those
39*7c478bd9Sstevel@tonic-gate  * is kernel private.  All external interfaces as well as K-to-K interfaces
40*7c478bd9Sstevel@tonic-gate  * have been constructed in a manner to provide the maximum flexibility.
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * There can be X privilege sets each containing Y 32 bit words.
43*7c478bd9Sstevel@tonic-gate  * <X, Y> are constant for a kernel invocation.
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * As a consequence, all privilege set manipulation happens in functions
46*7c478bd9Sstevel@tonic-gate  * below.
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * Privilege name to number mapping table consists in the generated
65*7c478bd9Sstevel@tonic-gate  * priv_const.c file.  This lock protects against updates of the privilege
66*7c478bd9Sstevel@tonic-gate  * names and counts; all other priv_info fields are read-only.
67*7c478bd9Sstevel@tonic-gate  * The actual protected values are:
68*7c478bd9Sstevel@tonic-gate  *	global variable nprivs
69*7c478bd9Sstevel@tonic-gate  *	the priv_max field
70*7c478bd9Sstevel@tonic-gate  *	the priv_names field
71*7c478bd9Sstevel@tonic-gate  *	the priv names info item (cnt/strings)
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate krwlock_t privinfo_lock;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static boolean_t priv_valid(const cred_t *);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate priv_set_t priv_fullset;	/* set of all privileges */
78*7c478bd9Sstevel@tonic-gate priv_set_t priv_unsafe;	/* unsafe to exec set-uid root if these are not in L */
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate  * Privilege initialization functions.
82*7c478bd9Sstevel@tonic-gate  * Called from common/os/cred.c when cred_init is called.
83*7c478bd9Sstevel@tonic-gate  */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate void
86*7c478bd9Sstevel@tonic-gate priv_init(void)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	rw_init(&privinfo_lock, NULL, RW_DRIVER, NULL);
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	PRIV_BASIC_ASSERT(priv_basic);
91*7c478bd9Sstevel@tonic-gate 	PRIV_UNSAFE_ASSERT(&priv_unsafe);
92*7c478bd9Sstevel@tonic-gate 	priv_fillset(&priv_fullset);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	devpolicy_init();
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /* Utility functions: privilege sets as opaque data types */
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Guts of prgetprivsize.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate int
103*7c478bd9Sstevel@tonic-gate priv_prgetprivsize(prpriv_t *tmpl)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	return (sizeof (prpriv_t) +
106*7c478bd9Sstevel@tonic-gate 		PRIV_SETBYTES - sizeof (priv_chunk_t) +
107*7c478bd9Sstevel@tonic-gate 		(tmpl ? tmpl->pr_infosize : priv_info->priv_infosize));
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /*
111*7c478bd9Sstevel@tonic-gate  * Guts of prgetpriv.
112*7c478bd9Sstevel@tonic-gate  */
113*7c478bd9Sstevel@tonic-gate void
114*7c478bd9Sstevel@tonic-gate cred2prpriv(const cred_t *cp, prpriv_t *pr)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	priv_set_t *psa;
117*7c478bd9Sstevel@tonic-gate 	int i;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	pr->pr_nsets = PRIV_NSET;
120*7c478bd9Sstevel@tonic-gate 	pr->pr_setsize = PRIV_SETSIZE;
121*7c478bd9Sstevel@tonic-gate 	pr->pr_infosize = priv_info->priv_infosize;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	psa = (priv_set_t *)pr->pr_sets;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_NSET; i++)
126*7c478bd9Sstevel@tonic-gate 		psa[i] = *priv_getset(cp, i);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	priv_getinfo(cp, (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr));
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * Guts of pr_spriv:
133*7c478bd9Sstevel@tonic-gate  *
134*7c478bd9Sstevel@tonic-gate  * Set the privileges of a process.
135*7c478bd9Sstevel@tonic-gate  *
136*7c478bd9Sstevel@tonic-gate  * In order to set the privileges, the setting process will need to
137*7c478bd9Sstevel@tonic-gate  * have those privileges in its effective set in order to prevent
138*7c478bd9Sstevel@tonic-gate  * specially privileged processes to easily gain additional privileges.
139*7c478bd9Sstevel@tonic-gate  * Pre-existing privileges can be retained.  To change any privileges,
140*7c478bd9Sstevel@tonic-gate  * PRIV_PROC_OWNER needs to be asserted.
141*7c478bd9Sstevel@tonic-gate  *
142*7c478bd9Sstevel@tonic-gate  * In formula:
143*7c478bd9Sstevel@tonic-gate  *
144*7c478bd9Sstevel@tonic-gate  *	S' <= S || S' <= S + Ea
145*7c478bd9Sstevel@tonic-gate  *
146*7c478bd9Sstevel@tonic-gate  * the new set must either be subset of the old set or a subset of
147*7c478bd9Sstevel@tonic-gate  * the oldset merged with the effective set of the acting process; or just:
148*7c478bd9Sstevel@tonic-gate  *
149*7c478bd9Sstevel@tonic-gate  *	S' <= S + Ea
150*7c478bd9Sstevel@tonic-gate  *
151*7c478bd9Sstevel@tonic-gate  * It's not legal to grow the limit set this way.
152*7c478bd9Sstevel@tonic-gate  *
153*7c478bd9Sstevel@tonic-gate  */
154*7c478bd9Sstevel@tonic-gate int
155*7c478bd9Sstevel@tonic-gate priv_pr_spriv(proc_t *p, prpriv_t *prpriv, const cred_t *cr)
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate 	cred_t *oldcred;
158*7c478bd9Sstevel@tonic-gate 	cred_t *newcred;
159*7c478bd9Sstevel@tonic-gate 	int i;
160*7c478bd9Sstevel@tonic-gate 	int err = EPERM;
161*7c478bd9Sstevel@tonic-gate 	cred_priv_t *cp, *ocp;
162*7c478bd9Sstevel@tonic-gate 	priv_set_t eset;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	/*
167*7c478bd9Sstevel@tonic-gate 	 * Set must have proper dimension; infosize must be absent
168*7c478bd9Sstevel@tonic-gate 	 * or properly sized.
169*7c478bd9Sstevel@tonic-gate 	 */
170*7c478bd9Sstevel@tonic-gate 	if (prpriv->pr_nsets != PRIV_NSET ||
171*7c478bd9Sstevel@tonic-gate 	    prpriv->pr_setsize != PRIV_SETSIZE ||
172*7c478bd9Sstevel@tonic-gate 	    (prpriv->pr_infosize & (sizeof (uint32_t) - 1)) != 0 ||
173*7c478bd9Sstevel@tonic-gate 	    prpriv->pr_infosize > priv_info->priv_infosize ||
174*7c478bd9Sstevel@tonic-gate 	    prpriv->pr_infosize < 0)
175*7c478bd9Sstevel@tonic-gate 		    return (EINVAL);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (priv_proc_cred_perm(cr, p, &oldcred, VWRITE) != 0) {
180*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
181*7c478bd9Sstevel@tonic-gate 		return (EPERM);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	newcred = crdup(oldcred);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	/* Copy the privilege sets from prpriv to newcred */
187*7c478bd9Sstevel@tonic-gate 	bcopy(prpriv->pr_sets, CR_PRIVSETS(newcred), PRIV_SETBYTES);
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	cp = &newcred->cr_priv;
190*7c478bd9Sstevel@tonic-gate 	ocp = &oldcred->cr_priv;
191*7c478bd9Sstevel@tonic-gate 	eset = CR_OEPRIV(cr);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	priv_intersect(&CR_LPRIV(oldcred), &eset);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	/*
196*7c478bd9Sstevel@tonic-gate 	 * Verify the constraints laid out:
197*7c478bd9Sstevel@tonic-gate 	 * for the limit set, we require that the new set is a subset
198*7c478bd9Sstevel@tonic-gate 	 * of the old limit set.
199*7c478bd9Sstevel@tonic-gate 	 * for all other sets, we require that the new set is either a
200*7c478bd9Sstevel@tonic-gate 	 * subset of the old set or a subset of the intersection of
201*7c478bd9Sstevel@tonic-gate 	 * the old limit set and the effective set of the acting process.
202*7c478bd9Sstevel@tonic-gate 	 */
203*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_NSET; i++)
204*7c478bd9Sstevel@tonic-gate 		if (!priv_issubset(&cp->crprivs[i], &ocp->crprivs[i]) &&
205*7c478bd9Sstevel@tonic-gate 		    (i == PRIV_LIMIT || !priv_issubset(&cp->crprivs[i], &eset)))
206*7c478bd9Sstevel@tonic-gate 			break;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	crfree(oldcred);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	if (i < PRIV_NSET || !priv_valid(newcred))
211*7c478bd9Sstevel@tonic-gate 		goto err;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	/* Load the settable privilege information */
214*7c478bd9Sstevel@tonic-gate 	if (prpriv->pr_infosize > 0) {
215*7c478bd9Sstevel@tonic-gate 		char *x = (char *)prpriv + PRIV_PRPRIV_INFO_OFFSET(prpriv);
216*7c478bd9Sstevel@tonic-gate 		char *lastx = x + prpriv->pr_infosize;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 		while (x < lastx) {
219*7c478bd9Sstevel@tonic-gate 			priv_info_t *pi = (priv_info_t *)x;
220*7c478bd9Sstevel@tonic-gate 			priv_info_uint_t *pii;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 			switch (pi->priv_info_type) {
223*7c478bd9Sstevel@tonic-gate 			case PRIV_INFO_FLAGS:
224*7c478bd9Sstevel@tonic-gate 				pii = (priv_info_uint_t *)x;
225*7c478bd9Sstevel@tonic-gate 				if (pii->info.priv_info_size != sizeof (*pii)) {
226*7c478bd9Sstevel@tonic-gate 					err = EINVAL;
227*7c478bd9Sstevel@tonic-gate 					goto err;
228*7c478bd9Sstevel@tonic-gate 				}
229*7c478bd9Sstevel@tonic-gate 				CR_FLAGS(newcred) &= ~PRIV_USER;
230*7c478bd9Sstevel@tonic-gate 				CR_FLAGS(newcred) |= (pii->val & PRIV_USER);
231*7c478bd9Sstevel@tonic-gate 				break;
232*7c478bd9Sstevel@tonic-gate 			default:
233*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
234*7c478bd9Sstevel@tonic-gate 				goto err;
235*7c478bd9Sstevel@tonic-gate 			}
236*7c478bd9Sstevel@tonic-gate 			/* Guarantee alignment and forward progress */
237*7c478bd9Sstevel@tonic-gate 			if ((pi->priv_info_size & (sizeof (uint32_t) - 1)) ||
238*7c478bd9Sstevel@tonic-gate 			    pi->priv_info_size < sizeof (*pi) ||
239*7c478bd9Sstevel@tonic-gate 			    lastx - x > pi->priv_info_size) {
240*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
241*7c478bd9Sstevel@tonic-gate 				goto err;
242*7c478bd9Sstevel@tonic-gate 			}
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 			x += pi->priv_info_size;
245*7c478bd9Sstevel@tonic-gate 		}
246*7c478bd9Sstevel@tonic-gate 	}
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	/*
249*7c478bd9Sstevel@tonic-gate 	 * We'll try to copy the privilege aware flag; but since the
250*7c478bd9Sstevel@tonic-gate 	 * privileges sets are all individually set, they are set
251*7c478bd9Sstevel@tonic-gate 	 * as if we're privilege aware.  If PRIV_AWARE wasn't set
252*7c478bd9Sstevel@tonic-gate 	 * or was explicitely unset, we need to set the flag and then
253*7c478bd9Sstevel@tonic-gate 	 * try to get rid of it.
254*7c478bd9Sstevel@tonic-gate 	 */
255*7c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(newcred) & PRIV_AWARE) == 0) {
256*7c478bd9Sstevel@tonic-gate 		CR_FLAGS(newcred) |= PRIV_AWARE;
257*7c478bd9Sstevel@tonic-gate 		priv_adjust_PA(newcred);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
261*7c478bd9Sstevel@tonic-gate 	oldcred = p->p_cred;
262*7c478bd9Sstevel@tonic-gate 	p->p_cred = newcred;
263*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
264*7c478bd9Sstevel@tonic-gate 	crfree(oldcred);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
267*7c478bd9Sstevel@tonic-gate 	return (0);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate err:
270*7c478bd9Sstevel@tonic-gate 	crfree(newcred);
271*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
272*7c478bd9Sstevel@tonic-gate 	return (err);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate priv_impl_info_t
276*7c478bd9Sstevel@tonic-gate *priv_hold_implinfo(void)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	rw_enter(&privinfo_lock, RW_READER);
279*7c478bd9Sstevel@tonic-gate 	return (priv_info);
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate void
283*7c478bd9Sstevel@tonic-gate priv_release_implinfo(void)
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	rw_exit(&privinfo_lock);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate size_t
289*7c478bd9Sstevel@tonic-gate priv_get_implinfo_size(void)
290*7c478bd9Sstevel@tonic-gate {
291*7c478bd9Sstevel@tonic-gate 	return (privinfosize);
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate /*
296*7c478bd9Sstevel@tonic-gate  * Return the nth privilege set
297*7c478bd9Sstevel@tonic-gate  */
298*7c478bd9Sstevel@tonic-gate const priv_set_t *
299*7c478bd9Sstevel@tonic-gate priv_getset(const cred_t *cr, int set)
300*7c478bd9Sstevel@tonic-gate {
301*7c478bd9Sstevel@tonic-gate 	ASSERT(PRIV_VALIDSET(set));
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & PRIV_AWARE) == 0)
304*7c478bd9Sstevel@tonic-gate 		switch (set) {
305*7c478bd9Sstevel@tonic-gate 		case PRIV_EFFECTIVE:
306*7c478bd9Sstevel@tonic-gate 			return (&CR_OEPRIV(cr));
307*7c478bd9Sstevel@tonic-gate 		case PRIV_PERMITTED:
308*7c478bd9Sstevel@tonic-gate 			return (&CR_OPPRIV(cr));
309*7c478bd9Sstevel@tonic-gate 		}
310*7c478bd9Sstevel@tonic-gate 	return (&CR_PRIVS(cr)->crprivs[set]);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate /*
314*7c478bd9Sstevel@tonic-gate  * Buf must be allocated by caller and contain sufficient space to
315*7c478bd9Sstevel@tonic-gate  * contain all additional info structures using priv_info.priv_infosize.
316*7c478bd9Sstevel@tonic-gate  * The buffer must be properly aligned.
317*7c478bd9Sstevel@tonic-gate  */
318*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
319*7c478bd9Sstevel@tonic-gate void
320*7c478bd9Sstevel@tonic-gate priv_getinfo(const cred_t *cr, void *buf)
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	struct priv_info_uint *ii;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	ii = buf;
325*7c478bd9Sstevel@tonic-gate 	ii->val = CR_FLAGS(cr);
326*7c478bd9Sstevel@tonic-gate 	ii->info.priv_info_size = (uint32_t)sizeof (*ii);
327*7c478bd9Sstevel@tonic-gate 	ii->info.priv_info_type = PRIV_INFO_FLAGS;
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate int
331*7c478bd9Sstevel@tonic-gate priv_getbyname(const char *name, uint_t flag)
332*7c478bd9Sstevel@tonic-gate {
333*7c478bd9Sstevel@tonic-gate 	int i;
334*7c478bd9Sstevel@tonic-gate 	int wheld = 0;
335*7c478bd9Sstevel@tonic-gate 	int len;
336*7c478bd9Sstevel@tonic-gate 	char *p;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (flag != 0 && flag != PRIV_ALLOC)
339*7c478bd9Sstevel@tonic-gate 		return (-EINVAL);
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if (strncasecmp(name, "priv_", 5) == 0)
342*7c478bd9Sstevel@tonic-gate 		name += 5;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	rw_enter(&privinfo_lock, RW_READER);
345*7c478bd9Sstevel@tonic-gate rescan:
346*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nprivs; i++)
347*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(priv_names[i], name) == 0) {
348*7c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
349*7c478bd9Sstevel@tonic-gate 			return (i);
350*7c478bd9Sstevel@tonic-gate 		}
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	if (!wheld) {
354*7c478bd9Sstevel@tonic-gate 		if (!(flag & PRIV_ALLOC)) {
355*7c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
356*7c478bd9Sstevel@tonic-gate 			return (-EINVAL);
357*7c478bd9Sstevel@tonic-gate 		}
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 		/* check length, validity and available space */
360*7c478bd9Sstevel@tonic-gate 		len = strlen(name) + 1;
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 		if (len > PRIVNAME_MAX) {
363*7c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
364*7c478bd9Sstevel@tonic-gate 			return (-ENAMETOOLONG);
365*7c478bd9Sstevel@tonic-gate 		}
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 		for (p = (char *)name; *p != '\0'; p++) {
368*7c478bd9Sstevel@tonic-gate 			char c = *p;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 			if (!((c >= 'A' && c <= 'Z') ||
371*7c478bd9Sstevel@tonic-gate 			    (c >= 'a' && c <= 'z') ||
372*7c478bd9Sstevel@tonic-gate 			    (c >= '0' && c <= '9') ||
373*7c478bd9Sstevel@tonic-gate 			    c == '_')) {
374*7c478bd9Sstevel@tonic-gate 				rw_exit(&privinfo_lock);
375*7c478bd9Sstevel@tonic-gate 				return (-EINVAL);
376*7c478bd9Sstevel@tonic-gate 			}
377*7c478bd9Sstevel@tonic-gate 		}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 		if (!rw_tryupgrade(&privinfo_lock)) {
380*7c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
381*7c478bd9Sstevel@tonic-gate 			rw_enter(&privinfo_lock, RW_WRITER);
382*7c478bd9Sstevel@tonic-gate 			wheld = 1;
383*7c478bd9Sstevel@tonic-gate 			/* Someone may have added our privilege */
384*7c478bd9Sstevel@tonic-gate 			goto rescan;
385*7c478bd9Sstevel@tonic-gate 		}
386*7c478bd9Sstevel@tonic-gate 	}
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	if (nprivs == MAX_PRIVILEGE || len + privbytes > maxprivbytes) {
389*7c478bd9Sstevel@tonic-gate 		rw_exit(&privinfo_lock);
390*7c478bd9Sstevel@tonic-gate 		return (-ENOMEM);
391*7c478bd9Sstevel@tonic-gate 	}
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	priv_names[i] = p = priv_str + privbytes;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	bcopy(name, p, len);
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	/* make the priv_names[i] and privilege name globally visible */
398*7c478bd9Sstevel@tonic-gate 	membar_producer();
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	/* adjust priv count and bytes count */
401*7c478bd9Sstevel@tonic-gate 	priv_ninfo->cnt = priv_info->priv_max = ++nprivs;
402*7c478bd9Sstevel@tonic-gate 	privbytes += len;
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	rw_exit(&privinfo_lock);
405*7c478bd9Sstevel@tonic-gate 	return (i);
406*7c478bd9Sstevel@tonic-gate }
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate /*
409*7c478bd9Sstevel@tonic-gate  * We can't afford locking the privileges here because of the locations
410*7c478bd9Sstevel@tonic-gate  * we call this from; so we make sure that the privileges table
411*7c478bd9Sstevel@tonic-gate  * is visible to us; it is made visible before the value of nprivs is
412*7c478bd9Sstevel@tonic-gate  * updated.
413*7c478bd9Sstevel@tonic-gate  */
414*7c478bd9Sstevel@tonic-gate const char *
415*7c478bd9Sstevel@tonic-gate priv_getbynum(int priv)
416*7c478bd9Sstevel@tonic-gate {
417*7c478bd9Sstevel@tonic-gate 	int maxpriv = nprivs;
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	membar_consumer();
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	if (priv >= 0 && priv < maxpriv)
422*7c478bd9Sstevel@tonic-gate 		return (priv_names[priv]);
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	return (NULL);
425*7c478bd9Sstevel@tonic-gate }
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate const char *
428*7c478bd9Sstevel@tonic-gate priv_getsetbynum(int setno)
429*7c478bd9Sstevel@tonic-gate {
430*7c478bd9Sstevel@tonic-gate 	if (!PRIV_VALIDSET(setno))
431*7c478bd9Sstevel@tonic-gate 		return (NULL);
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	return (priv_setnames[setno]);
434*7c478bd9Sstevel@tonic-gate }
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate /*
437*7c478bd9Sstevel@tonic-gate  * Privilege sanity checking when setting: E <= P.
438*7c478bd9Sstevel@tonic-gate  */
439*7c478bd9Sstevel@tonic-gate static boolean_t
440*7c478bd9Sstevel@tonic-gate priv_valid(const cred_t *cr)
441*7c478bd9Sstevel@tonic-gate {
442*7c478bd9Sstevel@tonic-gate 	return (priv_issubset(&CR_EPRIV(cr), &CR_PPRIV(cr)));
443*7c478bd9Sstevel@tonic-gate }
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate /*
446*7c478bd9Sstevel@tonic-gate  * Privilege manipulation functions
447*7c478bd9Sstevel@tonic-gate  *
448*7c478bd9Sstevel@tonic-gate  * Without knowing the details of the privilege set implementation,
449*7c478bd9Sstevel@tonic-gate  * opaque pointers can be used to manipulate sets at will.
450*7c478bd9Sstevel@tonic-gate  */
451*7c478bd9Sstevel@tonic-gate void
452*7c478bd9Sstevel@tonic-gate priv_emptyset(priv_set_t *set)
453*7c478bd9Sstevel@tonic-gate {
454*7c478bd9Sstevel@tonic-gate 	bzero(set, sizeof (*set));
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate void
458*7c478bd9Sstevel@tonic-gate priv_fillset(priv_set_t *set)
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 	int i;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	/* memset? */
463*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++)
464*7c478bd9Sstevel@tonic-gate 		set->pbits[i] = ~(priv_chunk_t)0;
465*7c478bd9Sstevel@tonic-gate }
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate void
468*7c478bd9Sstevel@tonic-gate priv_addset(priv_set_t *set, int priv)
469*7c478bd9Sstevel@tonic-gate {
470*7c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
471*7c478bd9Sstevel@tonic-gate 	__PRIV_ASSERT(set, priv);
472*7c478bd9Sstevel@tonic-gate }
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate void
475*7c478bd9Sstevel@tonic-gate priv_delset(priv_set_t *set, int priv)
476*7c478bd9Sstevel@tonic-gate {
477*7c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
478*7c478bd9Sstevel@tonic-gate 	__PRIV_CLEAR(set, priv);
479*7c478bd9Sstevel@tonic-gate }
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate boolean_t
482*7c478bd9Sstevel@tonic-gate priv_ismember(const priv_set_t *set, int priv)
483*7c478bd9Sstevel@tonic-gate {
484*7c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
485*7c478bd9Sstevel@tonic-gate 	return (__PRIV_ISASSERT(set, priv) ? B_TRUE : B_FALSE);
486*7c478bd9Sstevel@tonic-gate }
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate #define	PRIV_TEST_BODY(test) \
489*7c478bd9Sstevel@tonic-gate 	int i; \
490*7c478bd9Sstevel@tonic-gate \
491*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++) \
492*7c478bd9Sstevel@tonic-gate 		if (!(test)) \
493*7c478bd9Sstevel@tonic-gate 			return (B_FALSE); \
494*7c478bd9Sstevel@tonic-gate \
495*7c478bd9Sstevel@tonic-gate 	return (B_TRUE)
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate boolean_t
498*7c478bd9Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b)
499*7c478bd9Sstevel@tonic-gate {
500*7c478bd9Sstevel@tonic-gate 	return ((boolean_t)(bcmp(a, b, sizeof (*a)) == 0));
501*7c478bd9Sstevel@tonic-gate }
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate boolean_t
504*7c478bd9Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set)
505*7c478bd9Sstevel@tonic-gate {
506*7c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY(set->pbits[i] == 0);
507*7c478bd9Sstevel@tonic-gate }
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate boolean_t
510*7c478bd9Sstevel@tonic-gate priv_isfullset(const priv_set_t *set)
511*7c478bd9Sstevel@tonic-gate {
512*7c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY(set->pbits[i] == ~(priv_chunk_t)0);
513*7c478bd9Sstevel@tonic-gate }
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate /*
516*7c478bd9Sstevel@tonic-gate  * Return true if a is a subset of b
517*7c478bd9Sstevel@tonic-gate  */
518*7c478bd9Sstevel@tonic-gate boolean_t
519*7c478bd9Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b)
520*7c478bd9Sstevel@tonic-gate {
521*7c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY((a->pbits[i] | b->pbits[i]) == b->pbits[i]);
522*7c478bd9Sstevel@tonic-gate }
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate #define	PRIV_CHANGE_BODY(a, op, b) \
525*7c478bd9Sstevel@tonic-gate 	int i; \
526*7c478bd9Sstevel@tonic-gate \
527*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++) \
528*7c478bd9Sstevel@tonic-gate 		a->pbits[i] op b->pbits[i]
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate /* B = A ^ B */
531*7c478bd9Sstevel@tonic-gate void
532*7c478bd9Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b)
533*7c478bd9Sstevel@tonic-gate {
534*7c478bd9Sstevel@tonic-gate 	/* CSTYLED */
535*7c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, &=, a);
536*7c478bd9Sstevel@tonic-gate }
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate /* B = A v B */
539*7c478bd9Sstevel@tonic-gate void
540*7c478bd9Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b)
541*7c478bd9Sstevel@tonic-gate {
542*7c478bd9Sstevel@tonic-gate 	/* CSTYLED */
543*7c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, |=, a);
544*7c478bd9Sstevel@tonic-gate }
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate /* A = ! A */
547*7c478bd9Sstevel@tonic-gate void
548*7c478bd9Sstevel@tonic-gate priv_inverse(priv_set_t *a)
549*7c478bd9Sstevel@tonic-gate {
550*7c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(a, = ~, a);
551*7c478bd9Sstevel@tonic-gate }
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate /*
554*7c478bd9Sstevel@tonic-gate  * Can the source cred act on the target credential?
555*7c478bd9Sstevel@tonic-gate  *
556*7c478bd9Sstevel@tonic-gate  * We will you allow to gain uids this way but not privileges.
557*7c478bd9Sstevel@tonic-gate  */
558*7c478bd9Sstevel@tonic-gate int
559*7c478bd9Sstevel@tonic-gate priv_proc_cred_perm(const cred_t *scr, proc_t *tp, cred_t **pcr, int mode)
560*7c478bd9Sstevel@tonic-gate {
561*7c478bd9Sstevel@tonic-gate 	const priv_set_t *eset;
562*7c478bd9Sstevel@tonic-gate 	int idsmatch;
563*7c478bd9Sstevel@tonic-gate 	cred_t *tcr;
564*7c478bd9Sstevel@tonic-gate 	int res = 0;
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 	/* prevent the cred from going away */
567*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
568*7c478bd9Sstevel@tonic-gate 	crhold(tcr = tp->p_cred);
569*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 	if (scr == tcr)
572*7c478bd9Sstevel@tonic-gate 		goto out;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 	idsmatch = (scr->cr_uid == tcr->cr_uid &&
575*7c478bd9Sstevel@tonic-gate 		    scr->cr_uid == tcr->cr_ruid &&
576*7c478bd9Sstevel@tonic-gate 		    scr->cr_uid == tcr->cr_suid &&
577*7c478bd9Sstevel@tonic-gate 		    scr->cr_gid == tcr->cr_gid &&
578*7c478bd9Sstevel@tonic-gate 		    scr->cr_gid == tcr->cr_rgid &&
579*7c478bd9Sstevel@tonic-gate 		    scr->cr_gid == tcr->cr_sgid &&
580*7c478bd9Sstevel@tonic-gate 		    !(tp->p_flag & SNOCD));
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	/*
583*7c478bd9Sstevel@tonic-gate 	 * Source credential must have the proc_zone privilege if referencing
584*7c478bd9Sstevel@tonic-gate 	 * a process in another zone.
585*7c478bd9Sstevel@tonic-gate 	 */
586*7c478bd9Sstevel@tonic-gate 	if (scr->cr_zone != tcr->cr_zone && secpolicy_proc_zone(scr) != 0) {
587*7c478bd9Sstevel@tonic-gate 		res = EACCES;
588*7c478bd9Sstevel@tonic-gate 		goto out;
589*7c478bd9Sstevel@tonic-gate 	}
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	if (!(mode & VWRITE)) {
592*7c478bd9Sstevel@tonic-gate 		if (!idsmatch && secpolicy_proc_owner(scr, tcr, 0) != 0)
593*7c478bd9Sstevel@tonic-gate 			res = EACCES;
594*7c478bd9Sstevel@tonic-gate 		goto out;
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 	/*
598*7c478bd9Sstevel@tonic-gate 	 * For writing, the effective set of scr must dominate all sets of tcr,
599*7c478bd9Sstevel@tonic-gate 	 * We test Pt <= Es (Et <= Pt so no need to test) and It <= Es
600*7c478bd9Sstevel@tonic-gate 	 * The Limit set of scr must be a superset of the limitset of
601*7c478bd9Sstevel@tonic-gate 	 * tcr.
602*7c478bd9Sstevel@tonic-gate 	 */
603*7c478bd9Sstevel@tonic-gate 	eset = &CR_OEPRIV(scr);
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	if (!priv_issubset(&CR_IPRIV(tcr), eset) ||
606*7c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_OPPRIV(tcr), eset) ||
607*7c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_LPRIV(tcr), &CR_LPRIV(scr)) ||
608*7c478bd9Sstevel@tonic-gate 	    !idsmatch && secpolicy_proc_owner(scr, tcr, mode) != 0)
609*7c478bd9Sstevel@tonic-gate 		res = EACCES;
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate out:
612*7c478bd9Sstevel@tonic-gate 	if (res == 0 && pcr != NULL)
613*7c478bd9Sstevel@tonic-gate 		*pcr = tcr;
614*7c478bd9Sstevel@tonic-gate 	else
615*7c478bd9Sstevel@tonic-gate 		crfree(tcr);
616*7c478bd9Sstevel@tonic-gate 	return (res);
617*7c478bd9Sstevel@tonic-gate }
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate /*
620*7c478bd9Sstevel@tonic-gate  * Set the privilege aware bit, adding L to E/P if
621*7c478bd9Sstevel@tonic-gate  * necessasry.
622*7c478bd9Sstevel@tonic-gate  */
623*7c478bd9Sstevel@tonic-gate void
624*7c478bd9Sstevel@tonic-gate priv_set_PA(cred_t *cr)
625*7c478bd9Sstevel@tonic-gate {
626*7c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_AWARE)
629*7c478bd9Sstevel@tonic-gate 		return;
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 	CR_FLAGS(cr) |= PRIV_AWARE;
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0)
634*7c478bd9Sstevel@tonic-gate 		priv_union(&CR_LPRIV(cr), &CR_EPRIV(cr));
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 || cr->cr_suid == 0 || cr->cr_ruid == 0)
637*7c478bd9Sstevel@tonic-gate 		priv_union(&CR_LPRIV(cr), &CR_PPRIV(cr));
638*7c478bd9Sstevel@tonic-gate }
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate boolean_t
641*7c478bd9Sstevel@tonic-gate priv_can_clear_PA(const cred_t *cr)
642*7c478bd9Sstevel@tonic-gate {
643*7c478bd9Sstevel@tonic-gate 	/*
644*7c478bd9Sstevel@tonic-gate 	 * We can clear PA in the following cases:
645*7c478bd9Sstevel@tonic-gate 	 *
646*7c478bd9Sstevel@tonic-gate 	 * None of the uids are 0.
647*7c478bd9Sstevel@tonic-gate 	 * Any uid == 0 and P == L and (Euid != 0 or E == L)
648*7c478bd9Sstevel@tonic-gate 	 */
649*7c478bd9Sstevel@tonic-gate 	return ((cr->cr_suid != 0 && cr->cr_ruid != 0 && cr->cr_uid != 0) ||
650*7c478bd9Sstevel@tonic-gate 	    priv_isequalset(&CR_PPRIV(cr), &CR_LPRIV(cr)) &&
651*7c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != 0 || priv_isequalset(&CR_EPRIV(cr), &CR_LPRIV(cr))));
652*7c478bd9Sstevel@tonic-gate }
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate /*
655*7c478bd9Sstevel@tonic-gate  * Clear privilege aware bit if it is an idempotent operation and by
656*7c478bd9Sstevel@tonic-gate  * clearing it the process cannot get to uid 0 and all privileges.
657*7c478bd9Sstevel@tonic-gate  *
658*7c478bd9Sstevel@tonic-gate  * This function should be called with caution as it may cause "E" to be
659*7c478bd9Sstevel@tonic-gate  * lost once a processes assumes euid 0 again.
660*7c478bd9Sstevel@tonic-gate  */
661*7c478bd9Sstevel@tonic-gate void
662*7c478bd9Sstevel@tonic-gate priv_adjust_PA(cred_t *cr)
663*7c478bd9Sstevel@tonic-gate {
664*7c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	if (!(CR_FLAGS(cr) & PRIV_AWARE) ||
667*7c478bd9Sstevel@tonic-gate 	    !priv_can_clear_PA(cr))
668*7c478bd9Sstevel@tonic-gate 		return;
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_AWARE_INHERIT)
671*7c478bd9Sstevel@tonic-gate 		return;
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	/*
674*7c478bd9Sstevel@tonic-gate 	 * We now need to adjust P/E in those cases when uids
675*7c478bd9Sstevel@tonic-gate 	 * are zero; the rules are P' = I & L, E' = I & L;
676*7c478bd9Sstevel@tonic-gate 	 * but since P = L and E = L, we can use P &= I, E &= I,
677*7c478bd9Sstevel@tonic-gate 	 * depending on which uids are 0.
678*7c478bd9Sstevel@tonic-gate 	 */
679*7c478bd9Sstevel@tonic-gate 	if (cr->cr_suid == 0 || cr->cr_ruid == 0 || cr->cr_uid == 0) {
680*7c478bd9Sstevel@tonic-gate 		if (cr->cr_uid == 0)
681*7c478bd9Sstevel@tonic-gate 			priv_intersect(&CR_IPRIV(cr), &CR_EPRIV(cr));
682*7c478bd9Sstevel@tonic-gate 		priv_intersect(&CR_IPRIV(cr), &CR_PPRIV(cr));
683*7c478bd9Sstevel@tonic-gate 	}
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	CR_FLAGS(cr) &= ~PRIV_AWARE;
686*7c478bd9Sstevel@tonic-gate }
687