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