xref: /xnu-10002.61.3/security/mac_label.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions /*-
2*0f4c859eSApple OSS Distributions  * Copyright (c) 2004 Networks Associates Technology, Inc.
3*0f4c859eSApple OSS Distributions  * Copyright (c) 2005 SPARTA, Inc.
4*0f4c859eSApple OSS Distributions  * All rights reserved.
5*0f4c859eSApple OSS Distributions  *
6*0f4c859eSApple OSS Distributions  * This software was developed for the FreeBSD Project in part by Network
7*0f4c859eSApple OSS Distributions  * Associates Laboratories, the Security Research Division of Network
8*0f4c859eSApple OSS Distributions  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
9*0f4c859eSApple OSS Distributions  * as part of the DARPA CHATS research program.
10*0f4c859eSApple OSS Distributions  *
11*0f4c859eSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
12*0f4c859eSApple OSS Distributions  * modification, are permitted provided that the following conditions
13*0f4c859eSApple OSS Distributions  * are met:
14*0f4c859eSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
15*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
16*0f4c859eSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
17*0f4c859eSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
18*0f4c859eSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
19*0f4c859eSApple OSS Distributions  *
20*0f4c859eSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*0f4c859eSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*0f4c859eSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*0f4c859eSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*0f4c859eSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*0f4c859eSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*0f4c859eSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*0f4c859eSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*0f4c859eSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*0f4c859eSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*0f4c859eSApple OSS Distributions  * SUCH DAMAGE.
31*0f4c859eSApple OSS Distributions  */
32*0f4c859eSApple OSS Distributions 
33*0f4c859eSApple OSS Distributions #include <kern/zalloc.h>
34*0f4c859eSApple OSS Distributions #include <security/_label.h>
35*0f4c859eSApple OSS Distributions #include <sys/param.h>
36*0f4c859eSApple OSS Distributions #include <sys/queue.h>
37*0f4c859eSApple OSS Distributions #include <sys/systm.h>
38*0f4c859eSApple OSS Distributions #include <security/mac_internal.h>
39*0f4c859eSApple OSS Distributions 
40*0f4c859eSApple OSS Distributions ZONE_DEFINE_ID(ZONE_ID_MAC_LABEL, "MAC Labels", struct label,
41*0f4c859eSApple OSS Distributions     ZC_READONLY | ZC_ZFREE_CLEARMEM);
42*0f4c859eSApple OSS Distributions 
43*0f4c859eSApple OSS Distributions /*
44*0f4c859eSApple OSS Distributions  * Number of initial values matches logic in security/_label.h
45*0f4c859eSApple OSS Distributions  */
46*0f4c859eSApple OSS Distributions const struct label empty_label = {
47*0f4c859eSApple OSS Distributions 	.l_perpolicy[0 ... MAC_MAX_SLOTS - 1] = MAC_LABEL_NULL_SLOT,
48*0f4c859eSApple OSS Distributions };
49*0f4c859eSApple OSS Distributions 
50*0f4c859eSApple OSS Distributions static struct label *
label_alloc_noinit(int flags)51*0f4c859eSApple OSS Distributions label_alloc_noinit(int flags)
52*0f4c859eSApple OSS Distributions {
53*0f4c859eSApple OSS Distributions 	static_assert(MAC_NOWAIT == Z_NOWAIT);
54*0f4c859eSApple OSS Distributions 	return zalloc_ro(ZONE_ID_MAC_LABEL, Z_ZERO | (flags & MAC_NOWAIT));
55*0f4c859eSApple OSS Distributions }
56*0f4c859eSApple OSS Distributions 
57*0f4c859eSApple OSS Distributions struct label *
mac_labelzone_alloc(int flags)58*0f4c859eSApple OSS Distributions mac_labelzone_alloc(int flags)
59*0f4c859eSApple OSS Distributions {
60*0f4c859eSApple OSS Distributions 	struct label *label;
61*0f4c859eSApple OSS Distributions 
62*0f4c859eSApple OSS Distributions 	label = label_alloc_noinit(flags);
63*0f4c859eSApple OSS Distributions 	if (label) {
64*0f4c859eSApple OSS Distributions 		zalloc_ro_update_elem(ZONE_ID_MAC_LABEL, label, &empty_label);
65*0f4c859eSApple OSS Distributions 	}
66*0f4c859eSApple OSS Distributions 
67*0f4c859eSApple OSS Distributions 	return label;
68*0f4c859eSApple OSS Distributions }
69*0f4c859eSApple OSS Distributions 
70*0f4c859eSApple OSS Distributions struct label *
71*0f4c859eSApple OSS Distributions mac_labelzone_alloc_for_owner(struct label **labelp, int flags,
72*0f4c859eSApple OSS Distributions     void (^extra_setup)(struct label *))
73*0f4c859eSApple OSS Distributions {
74*0f4c859eSApple OSS Distributions 	struct label *label;
75*0f4c859eSApple OSS Distributions 
76*0f4c859eSApple OSS Distributions 	if (labelp) {
77*0f4c859eSApple OSS Distributions 		struct label tmp_label = empty_label;
78*0f4c859eSApple OSS Distributions 
79*0f4c859eSApple OSS Distributions 		label = zalloc_ro(ZONE_ID_MAC_LABEL, Z_ZERO | (flags & MAC_NOWAIT));
80*0f4c859eSApple OSS Distributions 
81*0f4c859eSApple OSS Distributions 		tmp_label.l_owner = labelp;
82*0f4c859eSApple OSS Distributions 		zalloc_ro_update_elem(ZONE_ID_MAC_LABEL, label, &tmp_label);
83*0f4c859eSApple OSS Distributions 	} else {
84*0f4c859eSApple OSS Distributions 		label = mac_labelzone_alloc(flags);
85*0f4c859eSApple OSS Distributions 	}
86*0f4c859eSApple OSS Distributions 
87*0f4c859eSApple OSS Distributions 	if (label && extra_setup) {
88*0f4c859eSApple OSS Distributions 		extra_setup(label);
89*0f4c859eSApple OSS Distributions 	}
90*0f4c859eSApple OSS Distributions 
91*0f4c859eSApple OSS Distributions 	return label;
92*0f4c859eSApple OSS Distributions }
93*0f4c859eSApple OSS Distributions 
94*0f4c859eSApple OSS Distributions struct label *
95*0f4c859eSApple OSS Distributions mac_labelzone_alloc_owned(struct label **labelp, int flags,
96*0f4c859eSApple OSS Distributions     void (^extra_setup)(struct label *))
97*0f4c859eSApple OSS Distributions {
98*0f4c859eSApple OSS Distributions 	struct label *label;
99*0f4c859eSApple OSS Distributions 
100*0f4c859eSApple OSS Distributions 	label = mac_labelzone_alloc_for_owner(labelp, flags, extra_setup);
101*0f4c859eSApple OSS Distributions 
102*0f4c859eSApple OSS Distributions 	if (labelp) {
103*0f4c859eSApple OSS Distributions 		*labelp = label;
104*0f4c859eSApple OSS Distributions 	}
105*0f4c859eSApple OSS Distributions 
106*0f4c859eSApple OSS Distributions 	return label;
107*0f4c859eSApple OSS Distributions }
108*0f4c859eSApple OSS Distributions 
109*0f4c859eSApple OSS Distributions void
mac_labelzone_free(struct label * label)110*0f4c859eSApple OSS Distributions mac_labelzone_free(struct label *label)
111*0f4c859eSApple OSS Distributions {
112*0f4c859eSApple OSS Distributions 	if (label == NULL) {
113*0f4c859eSApple OSS Distributions 		panic("Free of NULL MAC label");
114*0f4c859eSApple OSS Distributions 	}
115*0f4c859eSApple OSS Distributions 
116*0f4c859eSApple OSS Distributions 	zfree_ro(ZONE_ID_MAC_LABEL, label);
117*0f4c859eSApple OSS Distributions }
118*0f4c859eSApple OSS Distributions 
119*0f4c859eSApple OSS Distributions void
120*0f4c859eSApple OSS Distributions mac_labelzone_free_owned(struct label **labelp,
121*0f4c859eSApple OSS Distributions     void (^extra_deinit)(struct label *))
122*0f4c859eSApple OSS Distributions {
123*0f4c859eSApple OSS Distributions 	struct label *label;
124*0f4c859eSApple OSS Distributions 
125*0f4c859eSApple OSS Distributions 	label = mac_label_verify(labelp);
126*0f4c859eSApple OSS Distributions 	if (label) {
127*0f4c859eSApple OSS Distributions 		if (extra_deinit) {
128*0f4c859eSApple OSS Distributions 			extra_deinit(label);
129*0f4c859eSApple OSS Distributions 		}
130*0f4c859eSApple OSS Distributions 
131*0f4c859eSApple OSS Distributions 		*labelp = NULL;
132*0f4c859eSApple OSS Distributions 		mac_labelzone_free(label);
133*0f4c859eSApple OSS Distributions 	}
134*0f4c859eSApple OSS Distributions }
135*0f4c859eSApple OSS Distributions 
136*0f4c859eSApple OSS Distributions __abortlike
137*0f4c859eSApple OSS Distributions static void
mac_label_verify_panic(struct label ** labelp)138*0f4c859eSApple OSS Distributions mac_label_verify_panic(struct label **labelp)
139*0f4c859eSApple OSS Distributions {
140*0f4c859eSApple OSS Distributions 	panic("label backref mismatch: labelp:%p label:%p l_owner:%p", labelp,
141*0f4c859eSApple OSS Distributions 	    *labelp, (*labelp)->l_owner);
142*0f4c859eSApple OSS Distributions }
143*0f4c859eSApple OSS Distributions 
144*0f4c859eSApple OSS Distributions struct label *
mac_label_verify(struct label ** labelp)145*0f4c859eSApple OSS Distributions mac_label_verify(struct label **labelp)
146*0f4c859eSApple OSS Distributions {
147*0f4c859eSApple OSS Distributions 	struct label *label = *labelp;
148*0f4c859eSApple OSS Distributions 
149*0f4c859eSApple OSS Distributions 	if (label != NULL) {
150*0f4c859eSApple OSS Distributions 		zone_require_ro(ZONE_ID_MAC_LABEL, sizeof(struct label), label);
151*0f4c859eSApple OSS Distributions 
152*0f4c859eSApple OSS Distributions 		if (__improbable(label->l_owner != labelp)) {
153*0f4c859eSApple OSS Distributions 			mac_label_verify_panic(labelp);
154*0f4c859eSApple OSS Distributions 		}
155*0f4c859eSApple OSS Distributions 	}
156*0f4c859eSApple OSS Distributions 
157*0f4c859eSApple OSS Distributions 	return label;
158*0f4c859eSApple OSS Distributions }
159*0f4c859eSApple OSS Distributions 
160*0f4c859eSApple OSS Distributions static intptr_t
mac_label_slot_encode(intptr_t p)161*0f4c859eSApple OSS Distributions mac_label_slot_encode(intptr_t p)
162*0f4c859eSApple OSS Distributions {
163*0f4c859eSApple OSS Distributions 	return p ?: MAC_LABEL_NULL_SLOT;
164*0f4c859eSApple OSS Distributions }
165*0f4c859eSApple OSS Distributions 
166*0f4c859eSApple OSS Distributions static intptr_t
mac_label_slot_decode(intptr_t p)167*0f4c859eSApple OSS Distributions mac_label_slot_decode(intptr_t p)
168*0f4c859eSApple OSS Distributions {
169*0f4c859eSApple OSS Distributions 	switch (p) {
170*0f4c859eSApple OSS Distributions 	case 0:
171*0f4c859eSApple OSS Distributions 		/* make sure 0 doesn't mean NULL and causes crashes */
172*0f4c859eSApple OSS Distributions 		return MAC_LABEL_NULL_SLOT;
173*0f4c859eSApple OSS Distributions 	case MAC_LABEL_NULL_SLOT:
174*0f4c859eSApple OSS Distributions 		return 0l;
175*0f4c859eSApple OSS Distributions 	default:
176*0f4c859eSApple OSS Distributions 		return p;
177*0f4c859eSApple OSS Distributions 	}
178*0f4c859eSApple OSS Distributions }
179*0f4c859eSApple OSS Distributions 
180*0f4c859eSApple OSS Distributions /*
181*0f4c859eSApple OSS Distributions  * Functions used by policy modules to get and set label values.
182*0f4c859eSApple OSS Distributions  */
183*0f4c859eSApple OSS Distributions intptr_t
mac_label_get(struct label * label,int slot)184*0f4c859eSApple OSS Distributions mac_label_get(struct label *label, int slot)
185*0f4c859eSApple OSS Distributions {
186*0f4c859eSApple OSS Distributions 	KASSERT(label != NULL, ("mac_label_get: NULL label"));
187*0f4c859eSApple OSS Distributions 
188*0f4c859eSApple OSS Distributions 	zone_require_ro(ZONE_ID_MAC_LABEL, sizeof(struct label), label);
189*0f4c859eSApple OSS Distributions 	return mac_label_slot_decode((intptr_t) label->l_perpolicy[slot]);
190*0f4c859eSApple OSS Distributions }
191*0f4c859eSApple OSS Distributions 
192*0f4c859eSApple OSS Distributions __abortlike
193*0f4c859eSApple OSS Distributions static void
panic_label_set_sentinel(void)194*0f4c859eSApple OSS Distributions panic_label_set_sentinel(void)
195*0f4c859eSApple OSS Distributions {
196*0f4c859eSApple OSS Distributions 	panic("cannot set mac label to ~0");
197*0f4c859eSApple OSS Distributions }
198*0f4c859eSApple OSS Distributions 
199*0f4c859eSApple OSS Distributions void
mac_label_set(struct label * label,int slot,intptr_t v)200*0f4c859eSApple OSS Distributions mac_label_set(struct label *label, int slot, intptr_t v)
201*0f4c859eSApple OSS Distributions {
202*0f4c859eSApple OSS Distributions 	KASSERT(label != NULL, ("mac_label_set: NULL label"));
203*0f4c859eSApple OSS Distributions 
204*0f4c859eSApple OSS Distributions #if DEVELOPMENT || DEBUG
205*0f4c859eSApple OSS Distributions 	/* can't modify a sealed label, see mac_cred_label_seal() */
206*0f4c859eSApple OSS Distributions 	assertf(label->l_owner != (struct label **)-1,
207*0f4c859eSApple OSS Distributions 	    "mac_label_set(%p, %d, 0x%lx) is sealed",
208*0f4c859eSApple OSS Distributions 	    label, slot, v);
209*0f4c859eSApple OSS Distributions #endif
210*0f4c859eSApple OSS Distributions 	if (v == MAC_LABEL_NULL_SLOT) {
211*0f4c859eSApple OSS Distributions 		panic_label_set_sentinel();
212*0f4c859eSApple OSS Distributions 	}
213*0f4c859eSApple OSS Distributions 	v = mac_label_slot_encode(v);
214*0f4c859eSApple OSS Distributions 	zalloc_ro_update_field(ZONE_ID_MAC_LABEL, label, l_perpolicy[slot], &v);
215*0f4c859eSApple OSS Distributions }
216