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