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 ZONE_DEFINE_ID(ZONE_ID_MAC_LABEL, "MAC Labels", struct label,
41 ZC_READONLY | ZC_ZFREE_CLEARMEM);
42
43 #define MAC_LABEL_NULL_SLOT (void *)~0ULL
44
45 /*
46 * Number of initial values matches logic in security/_label.h
47 */
48 static const struct label empty_label = {
49 .l_perpolicy = {
50 /* at least 3 */
51 MAC_LABEL_NULL_SLOT,
52 MAC_LABEL_NULL_SLOT,
53 MAC_LABEL_NULL_SLOT,
54 #if defined(XNU_TARGET_OS_OSX)
55 /* +4 for macOS = 7 */
56 MAC_LABEL_NULL_SLOT,
57 MAC_LABEL_NULL_SLOT,
58 MAC_LABEL_NULL_SLOT,
59 MAC_LABEL_NULL_SLOT,
60 #elif CONFIG_VNGUARD
61 /* otherwise, +1 for vnguard = 4 */
62 MAC_LABEL_NULL_SLOT,
63 #endif
64 }
65 };
66
67 static struct label *
label_alloc_noinit(int flags)68 label_alloc_noinit(int flags)
69 {
70 static_assert(MAC_NOWAIT == Z_NOWAIT);
71 return zalloc_ro(ZONE_ID_MAC_LABEL, Z_ZERO | (flags & MAC_NOWAIT));
72 }
73
74 struct label *
mac_labelzone_alloc(int flags)75 mac_labelzone_alloc(int flags)
76 {
77 struct label *label;
78
79 label = label_alloc_noinit(flags);
80 if (label) {
81 zalloc_ro_update_elem(ZONE_ID_MAC_LABEL, label, &empty_label);
82 }
83
84 return label;
85 }
86
87 struct label *
88 mac_labelzone_alloc_for_owner(struct label **labelp, int flags,
89 void (^extra_setup)(struct label *))
90 {
91 struct label *label;
92
93 if (labelp) {
94 struct label tmp_label = empty_label;
95
96 label = zalloc_ro(ZONE_ID_MAC_LABEL, Z_ZERO | (flags & MAC_NOWAIT));
97
98 tmp_label.l_owner = labelp;
99 zalloc_ro_update_elem(ZONE_ID_MAC_LABEL, label, &tmp_label);
100 } else {
101 label = mac_labelzone_alloc(flags);
102 }
103
104 if (label && extra_setup) {
105 extra_setup(label);
106 }
107
108 return label;
109 }
110
111 struct label *
112 mac_labelzone_alloc_owned(struct label **labelp, int flags,
113 void (^extra_setup)(struct label *))
114 {
115 struct label *label;
116
117 label = mac_labelzone_alloc_for_owner(labelp, flags, extra_setup);
118
119 if (labelp) {
120 *labelp = label;
121 }
122
123 return label;
124 }
125
126 void
mac_labelzone_free(struct label * label)127 mac_labelzone_free(struct label *label)
128 {
129 if (label == NULL) {
130 panic("Free of NULL MAC label");
131 }
132
133 zfree_ro(ZONE_ID_MAC_LABEL, label);
134 }
135
136 void
137 mac_labelzone_free_owned(struct label **labelp,
138 void (^extra_deinit)(struct label *))
139 {
140 struct label *label;
141
142 label = mac_label_verify(labelp);
143 if (label) {
144 if (extra_deinit) {
145 extra_deinit(label);
146 }
147
148 *labelp = NULL;
149 mac_labelzone_free(label);
150 }
151 }
152
153 __abortlike
154 static void
mac_label_verify_panic(struct label ** labelp)155 mac_label_verify_panic(struct label **labelp)
156 {
157 panic("label backref mismatch: labelp:%p label:%p l_owner:%p", labelp,
158 *labelp, (*labelp)->l_owner);
159 }
160
161 struct label *
mac_label_verify(struct label ** labelp)162 mac_label_verify(struct label **labelp)
163 {
164 struct label *label = *labelp;
165
166 if (label != NULL) {
167 zone_require_ro(ZONE_ID_MAC_LABEL, sizeof(struct label), label);
168
169 if (__improbable(label->l_owner != labelp)) {
170 mac_label_verify_panic(labelp);
171 }
172 }
173
174 return label;
175 }
176
177 static intptr_t
mac_label_slot_encode(intptr_t p)178 mac_label_slot_encode(intptr_t p)
179 {
180 return (p == 0) ? (intptr_t)MAC_LABEL_NULL_SLOT : p;
181 }
182
183 static intptr_t
mac_label_slot_decode(intptr_t p)184 mac_label_slot_decode(intptr_t p)
185 {
186 return (p == (intptr_t)MAC_LABEL_NULL_SLOT) ? 0 : p;
187 }
188
189 /*
190 * Functions used by policy modules to get and set label values.
191 */
192 intptr_t
mac_label_get(struct label * label,int slot)193 mac_label_get(struct label *label, int slot)
194 {
195 KASSERT(label != NULL, ("mac_label_get: NULL label"));
196
197 zone_require_ro(ZONE_ID_MAC_LABEL, sizeof(struct label), label);
198 return mac_label_slot_decode((intptr_t) label->l_perpolicy[slot]);
199 }
200
201 __abortlike
202 static void
panic_label_set_sentinel(void)203 panic_label_set_sentinel(void)
204 {
205 panic("cannot set mac label to ~0");
206 }
207
208 void
mac_label_set(struct label * label,int slot,intptr_t v)209 mac_label_set(struct label *label, int slot, intptr_t v)
210 {
211 KASSERT(label != NULL, ("mac_label_set: NULL label"));
212
213 if (v == (intptr_t)MAC_LABEL_NULL_SLOT) {
214 panic_label_set_sentinel();
215 }
216
217 v = mac_label_slot_encode(v);
218 zalloc_ro_update_field(ZONE_ID_MAC_LABEL, label, l_perpolicy[slot],
219 (void **)&v);
220 }
221