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