xref: /freebsd/sys/security/mac/mac_inet.c (revision c96ae1968a6ab7056427a739bce81bf07447c2d4)
1 /*-
2  * Copyright (c) 1999-2002 Robert N. M. Watson
3  * Copyright (c) 2001 Ilmar S. Habibulin
4  * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed by Robert Watson and Ilmar Habibulin for the
8  * TrustedBSD Project.
9  *
10  * This software was developed for the FreeBSD Project in part by Network
11  * Associates Laboratories, the Security Research Division of Network
12  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
13  * as part of the DARPA CHATS research program.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_mac.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/sbuf.h>
48 #include <sys/systm.h>
49 #include <sys/mount.h>
50 #include <sys/file.h>
51 #include <sys/namei.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 
57 #include <net/if.h>
58 #include <net/if_var.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 
64 #include <security/mac/mac_framework.h>
65 #include <security/mac/mac_internal.h>
66 #include <security/mac/mac_policy.h>
67 
68 static struct label *
69 mac_inpcb_label_alloc(int flag)
70 {
71 	struct label *label;
72 	int error;
73 
74 	label = mac_labelzone_alloc(flag);
75 	if (label == NULL)
76 		return (NULL);
77 	MAC_CHECK(init_inpcb_label, label, flag);
78 	if (error) {
79 		MAC_PERFORM(destroy_inpcb_label, label);
80 		mac_labelzone_free(label);
81 		return (NULL);
82 	}
83 	return (label);
84 }
85 
86 int
87 mac_init_inpcb(struct inpcb *inp, int flag)
88 {
89 
90 	inp->inp_label = mac_inpcb_label_alloc(flag);
91 	if (inp->inp_label == NULL)
92 		return (ENOMEM);
93 	return (0);
94 }
95 
96 static struct label *
97 mac_ipq_label_alloc(int flag)
98 {
99 	struct label *label;
100 	int error;
101 
102 	label = mac_labelzone_alloc(flag);
103 	if (label == NULL)
104 		return (NULL);
105 
106 	MAC_CHECK(init_ipq_label, label, flag);
107 	if (error) {
108 		MAC_PERFORM(destroy_ipq_label, label);
109 		mac_labelzone_free(label);
110 		return (NULL);
111 	}
112 	return (label);
113 }
114 
115 int
116 mac_init_ipq(struct ipq *ipq, int flag)
117 {
118 
119 	ipq->ipq_label = mac_ipq_label_alloc(flag);
120 	if (ipq->ipq_label == NULL)
121 		return (ENOMEM);
122 	return (0);
123 }
124 
125 static void
126 mac_inpcb_label_free(struct label *label)
127 {
128 
129 	MAC_PERFORM(destroy_inpcb_label, label);
130 	mac_labelzone_free(label);
131 }
132 
133 void
134 mac_destroy_inpcb(struct inpcb *inp)
135 {
136 
137 	mac_inpcb_label_free(inp->inp_label);
138 	inp->inp_label = NULL;
139 }
140 
141 static void
142 mac_ipq_label_free(struct label *label)
143 {
144 
145 	MAC_PERFORM(destroy_ipq_label, label);
146 	mac_labelzone_free(label);
147 }
148 
149 void
150 mac_destroy_ipq(struct ipq *ipq)
151 {
152 
153 	mac_ipq_label_free(ipq->ipq_label);
154 	ipq->ipq_label = NULL;
155 }
156 
157 void
158 mac_create_inpcb_from_socket(struct socket *so, struct inpcb *inp)
159 {
160 
161 	MAC_PERFORM(create_inpcb_from_socket, so, so->so_label, inp,
162 	    inp->inp_label);
163 }
164 
165 void
166 mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
167 {
168 	struct label *label;
169 
170 	label = mac_mbuf_to_label(datagram);
171 
172 	MAC_PERFORM(create_datagram_from_ipq, ipq, ipq->ipq_label,
173 	    datagram, label);
174 }
175 
176 void
177 mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
178 {
179 	struct label *datagramlabel, *fragmentlabel;
180 
181 	datagramlabel = mac_mbuf_to_label(datagram);
182 	fragmentlabel = mac_mbuf_to_label(fragment);
183 
184 	MAC_PERFORM(create_fragment, datagram, datagramlabel, fragment,
185 	    fragmentlabel);
186 }
187 
188 void
189 mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
190 {
191 	struct label *label;
192 
193 	label = mac_mbuf_to_label(fragment);
194 
195 	MAC_PERFORM(create_ipq, fragment, label, ipq, ipq->ipq_label);
196 }
197 
198 void
199 mac_create_mbuf_from_inpcb(struct inpcb *inp, struct mbuf *m)
200 {
201 	struct label *mlabel;
202 
203 	INP_LOCK_ASSERT(inp);
204 	mlabel = mac_mbuf_to_label(m);
205 
206 	MAC_PERFORM(create_mbuf_from_inpcb, inp, inp->inp_label, m, mlabel);
207 }
208 
209 int
210 mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
211 {
212 	struct label *label;
213 	int result;
214 
215 	label = mac_mbuf_to_label(fragment);
216 
217 	result = 1;
218 	MAC_BOOLEAN(fragment_match, &&, fragment, label, ipq,
219 	    ipq->ipq_label);
220 
221 	return (result);
222 }
223 
224 void
225 mac_reflect_mbuf_icmp(struct mbuf *m)
226 {
227 	struct label *label;
228 
229 	label = mac_mbuf_to_label(m);
230 
231 	MAC_PERFORM(reflect_mbuf_icmp, m, label);
232 }
233 void
234 mac_reflect_mbuf_tcp(struct mbuf *m)
235 {
236 	struct label *label;
237 
238 	label = mac_mbuf_to_label(m);
239 
240 	MAC_PERFORM(reflect_mbuf_tcp, m, label);
241 }
242 
243 void
244 mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
245 {
246 	struct label *label;
247 
248 	label = mac_mbuf_to_label(fragment);
249 
250 	MAC_PERFORM(update_ipq, fragment, label, ipq, ipq->ipq_label);
251 }
252 
253 int
254 mac_check_inpcb_deliver(struct inpcb *inp, struct mbuf *m)
255 {
256 	struct label *label;
257 	int error;
258 
259 	M_ASSERTPKTHDR(m);
260 
261 	label = mac_mbuf_to_label(m);
262 
263 	MAC_CHECK(check_inpcb_deliver, inp, inp->inp_label, m, label);
264 
265 	return (error);
266 }
267 
268 void
269 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
270 {
271 
272 	INP_LOCK_ASSERT(inp);
273 	SOCK_LOCK_ASSERT(so);
274 	MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label);
275 }
276 
277 void
278 mac_create_mbuf_from_firewall(struct mbuf *m)
279 {
280 	struct label *label;
281 
282 	M_ASSERTPKTHDR(m);
283 	label = mac_mbuf_to_label(m);
284 	MAC_PERFORM(create_mbuf_from_firewall, m, label);
285 }
286 
287 /*
288  * These functions really should be referencing the syncache structure
289  * instead of the label.  However, due to some of the complexities associated
290  * with exposing this syncache structure we operate directly on it's label
291  * pointer.  This should be OK since we aren't making any access control
292  * decisions within this code directly, we are merely allocating and copying
293  * label storage so we can properly initialize mbuf labels for any packets
294  * the syncache code might create.
295  */
296 void
297 mac_destroy_syncache(struct label **label)
298 {
299 
300 	MAC_PERFORM(destroy_syncache_label, *label);
301 	mac_labelzone_free(*label);
302 	*label = NULL;
303 }
304 
305 int
306 mac_init_syncache(struct label **label)
307 {
308 	int error;
309 
310 	*label = mac_labelzone_alloc(M_NOWAIT);
311 	if (*label == NULL)
312 		return (ENOMEM);
313 	/*
314 	 * Since we are holding the inpcb locks the policy can not allocate
315 	 * policy specific label storage using M_WAITOK.  So we need to do a
316 	 * MAC_CHECK instead of the typical MAC_PERFORM so we can propagate
317 	 * allocation failures back to the syncache code.
318 	 */
319 	MAC_CHECK(init_syncache_label, *label, M_NOWAIT);
320 	return (error);
321 }
322 
323 void
324 mac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp)
325 {
326 
327 	INP_LOCK_ASSERT(inp);
328 	MAC_PERFORM(init_syncache_from_inpcb, label, inp);
329 }
330 
331 void
332 mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m)
333 {
334 	struct label *mbuf_label;
335 
336 	M_ASSERTPKTHDR(m);
337 	mbuf_label = mac_mbuf_to_label(m);
338 	MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mbuf_label);
339 }
340