xref: /freebsd/sys/security/mac/mac_inet.c (revision f4f8f02054f3abb6ceb84aefcdecc78d5c8b462f)
1 /*-
2  * Copyright (c) 1999-2002, 2007, 2009 Robert N. M. Watson
3  * Copyright (c) 2001 Ilmar S. Habibulin
4  * Copyright (c) 2001-2004 Networks Associates Technology, Inc.
5  * Copyright (c) 2006 SPARTA, Inc.
6  * Copyright (c) 2008 Apple Inc.
7  * All rights reserved.
8  *
9  * This software was developed by Robert Watson and Ilmar Habibulin for the
10  * TrustedBSD Project.
11  *
12  * This software was developed for the FreeBSD Project in part by Network
13  * Associates Laboratories, the Security Research Division of Network
14  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
15  * as part of the DARPA CHATS research program.
16  *
17  * This software was enhanced by SPARTA ISSO under SPAWAR contract
18  * N66001-04-C-6019 ("SEFOS").
19  *
20  * This software was developed at the University of Cambridge Computer
21  * Laboratory with support from a grant from Google, Inc.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include "opt_kdtrace.h"
49 #include "opt_mac.h"
50 
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/sbuf.h>
57 #include <sys/sdt.h>
58 #include <sys/systm.h>
59 #include <sys/mount.h>
60 #include <sys/file.h>
61 #include <sys/namei.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66 
67 #include <net/if.h>
68 #include <net/if_var.h>
69 
70 #include <netinet/in.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/ip_var.h>
73 
74 #include <security/mac/mac_framework.h>
75 #include <security/mac/mac_internal.h>
76 #include <security/mac/mac_policy.h>
77 
78 static struct label *
79 mac_inpcb_label_alloc(int flag)
80 {
81 	struct label *label;
82 	int error;
83 
84 	label = mac_labelzone_alloc(flag);
85 	if (label == NULL)
86 		return (NULL);
87 	if (flag & M_WAITOK)
88 		MAC_POLICY_CHECK(inpcb_init_label, label, flag);
89 	else
90 		MAC_POLICY_CHECK_NOSLEEP(inpcb_init_label, label, flag);
91 	if (error) {
92 		MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
93 		mac_labelzone_free(label);
94 		return (NULL);
95 	}
96 	return (label);
97 }
98 
99 int
100 mac_inpcb_init(struct inpcb *inp, int flag)
101 {
102 
103 	if (mac_labeled & MPC_OBJECT_INPCB) {
104 		inp->inp_label = mac_inpcb_label_alloc(flag);
105 		if (inp->inp_label == NULL)
106 			return (ENOMEM);
107 	} else
108 		inp->inp_label = NULL;
109 	return (0);
110 }
111 
112 static struct label *
113 mac_ipq_label_alloc(int flag)
114 {
115 	struct label *label;
116 	int error;
117 
118 	label = mac_labelzone_alloc(flag);
119 	if (label == NULL)
120 		return (NULL);
121 
122 	if (flag & M_WAITOK)
123 		MAC_POLICY_CHECK(ipq_init_label, label, flag);
124 	else
125 		MAC_POLICY_CHECK_NOSLEEP(ipq_init_label, label, flag);
126 	if (error) {
127 		MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
128 		mac_labelzone_free(label);
129 		return (NULL);
130 	}
131 	return (label);
132 }
133 
134 int
135 mac_ipq_init(struct ipq *q, int flag)
136 {
137 
138 	if (mac_labeled & MPC_OBJECT_IPQ) {
139 		q->ipq_label = mac_ipq_label_alloc(flag);
140 		if (q->ipq_label == NULL)
141 			return (ENOMEM);
142 	} else
143 		q->ipq_label = NULL;
144 	return (0);
145 }
146 
147 static void
148 mac_inpcb_label_free(struct label *label)
149 {
150 
151 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
152 	mac_labelzone_free(label);
153 }
154 
155 void
156 mac_inpcb_destroy(struct inpcb *inp)
157 {
158 
159 	if (inp->inp_label != NULL) {
160 		mac_inpcb_label_free(inp->inp_label);
161 		inp->inp_label = NULL;
162 	}
163 }
164 
165 static void
166 mac_ipq_label_free(struct label *label)
167 {
168 
169 	MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
170 	mac_labelzone_free(label);
171 }
172 
173 void
174 mac_ipq_destroy(struct ipq *q)
175 {
176 
177 	if (q->ipq_label != NULL) {
178 		mac_ipq_label_free(q->ipq_label);
179 		q->ipq_label = NULL;
180 	}
181 }
182 
183 void
184 mac_inpcb_create(struct socket *so, struct inpcb *inp)
185 {
186 
187 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp,
188 	    inp->inp_label);
189 }
190 
191 void
192 mac_ipq_reassemble(struct ipq *q, struct mbuf *m)
193 {
194 	struct label *label;
195 
196 	label = mac_mbuf_to_label(m);
197 
198 	MAC_POLICY_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m,
199 	    label);
200 }
201 
202 void
203 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag)
204 {
205 	struct label *mlabel, *fraglabel;
206 
207 	mlabel = mac_mbuf_to_label(m);
208 	fraglabel = mac_mbuf_to_label(frag);
209 
210 	MAC_POLICY_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag,
211 	    fraglabel);
212 }
213 
214 void
215 mac_ipq_create(struct mbuf *m, struct ipq *q)
216 {
217 	struct label *label;
218 
219 	label = mac_mbuf_to_label(m);
220 
221 	MAC_POLICY_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label);
222 }
223 
224 void
225 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m)
226 {
227 	struct label *mlabel;
228 
229 	INP_LOCK_ASSERT(inp);
230 	mlabel = mac_mbuf_to_label(m);
231 
232 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m,
233 	    mlabel);
234 }
235 
236 int
237 mac_ipq_match(struct mbuf *m, struct ipq *q)
238 {
239 	struct label *label;
240 	int result;
241 
242 	label = mac_mbuf_to_label(m);
243 
244 	result = 1;
245 	MAC_POLICY_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label);
246 
247 	return (result);
248 }
249 
250 void
251 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m)
252 {
253 	struct label *mlabel;
254 
255 	mlabel = mac_mbuf_to_label(m);
256 
257 	MAC_IFNET_LOCK(ifp);
258 	MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m,
259 	    mlabel);
260 	MAC_IFNET_UNLOCK(ifp);
261 }
262 
263 void
264 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend)
265 {
266 	struct label *mrecvlabel, *msendlabel;
267 
268 	mrecvlabel = mac_mbuf_to_label(mrecv);
269 	msendlabel = mac_mbuf_to_label(msend);
270 
271 	MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel,
272 	    msend, msendlabel);
273 }
274 
275 void
276 mac_netinet_icmp_replyinplace(struct mbuf *m)
277 {
278 	struct label *label;
279 
280 	label = mac_mbuf_to_label(m);
281 
282 	MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label);
283 }
284 
285 void
286 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m)
287 {
288 	struct label *mlabel;
289 
290 	mlabel = mac_mbuf_to_label(m);
291 
292 	MAC_IFNET_LOCK(ifp);
293 	MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m,
294 	    mlabel);
295 	MAC_IFNET_UNLOCK(ifp);
296 }
297 
298 void
299 mac_netinet_tcp_reply(struct mbuf *m)
300 {
301 	struct label *label;
302 
303 	label = mac_mbuf_to_label(m);
304 
305 	MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
306 }
307 
308 void
309 mac_ipq_update(struct mbuf *m, struct ipq *q)
310 {
311 	struct label *label;
312 
313 	label = mac_mbuf_to_label(m);
314 
315 	MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
316 }
317 
318 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
319     "struct mbuf *");
320 
321 int
322 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
323 {
324 	struct label *label;
325 	int error;
326 
327 	M_ASSERTPKTHDR(m);
328 
329 	label = mac_mbuf_to_label(m);
330 
331 	MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
332 	    label);
333 	MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
334 
335 	return (error);
336 }
337 
338 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
339     "struct inpcb *");
340 
341 int
342 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
343 {
344 	int error;
345 
346 	INP_LOCK_ASSERT(inp);
347 
348 	MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
349 	    inp->inp_label);
350 	MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
351 
352 	return (error);
353 }
354 
355 void
356 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
357 {
358 
359 	INP_WLOCK_ASSERT(inp);
360 	SOCK_LOCK_ASSERT(so);
361 
362 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
363 	    inp->inp_label);
364 }
365 
366 void
367 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
368 {
369 	struct label *mrecvlabel, *msendlabel;
370 
371 	M_ASSERTPKTHDR(mrecv);
372 	M_ASSERTPKTHDR(msend);
373 
374 	mrecvlabel = mac_mbuf_to_label(mrecv);
375 	msendlabel = mac_mbuf_to_label(msend);
376 
377 	MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
378 	    msend, msendlabel);
379 }
380 
381 void
382 mac_netinet_firewall_send(struct mbuf *m)
383 {
384 	struct label *label;
385 
386 	M_ASSERTPKTHDR(m);
387 
388 	label = mac_mbuf_to_label(m);
389 
390 	MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
391 }
392 
393 /*
394  * These functions really should be referencing the syncache structure
395  * instead of the label.  However, due to some of the complexities associated
396  * with exposing this syncache structure we operate directly on it's label
397  * pointer.  This should be OK since we aren't making any access control
398  * decisions within this code directly, we are merely allocating and copying
399  * label storage so we can properly initialize mbuf labels for any packets
400  * the syncache code might create.
401  */
402 void
403 mac_syncache_destroy(struct label **label)
404 {
405 
406 	if (*label != NULL) {
407 		MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
408 		mac_labelzone_free(*label);
409 		*label = NULL;
410 	}
411 }
412 
413 int
414 mac_syncache_init(struct label **label)
415 {
416 	int error;
417 
418 	if (mac_labeled & MPC_OBJECT_SYNCACHE) {
419 		*label = mac_labelzone_alloc(M_NOWAIT);
420 		if (*label == NULL)
421 			return (ENOMEM);
422 		/*
423 		 * Since we are holding the inpcb locks the policy can not
424 		 * allocate policy specific label storage using M_WAITOK.  So
425 		 * we need to do a MAC_CHECK instead of the typical
426 		 * MAC_PERFORM so we can propagate allocation failures back
427 		 * to the syncache code.
428 		 */
429 		MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
430 		    M_NOWAIT);
431 		if (error) {
432 			MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
433 			    *label);
434 			mac_labelzone_free(*label);
435 		}
436 		return (error);
437 	} else
438 		*label = NULL;
439 	return (0);
440 }
441 
442 void
443 mac_syncache_create(struct label *label, struct inpcb *inp)
444 {
445 
446 	INP_WLOCK_ASSERT(inp);
447 
448 	MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
449 }
450 
451 void
452 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
453 {
454 	struct label *mlabel;
455 
456 	M_ASSERTPKTHDR(m);
457 
458 	mlabel = mac_mbuf_to_label(m);
459 
460 	MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,
461 	    mlabel);
462 }
463