xref: /freebsd/sys/security/mac/mac_inet.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 1999-2002, 2007, 2009, 2019 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 #include "opt_mac.h"
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mutex.h>
53 #include <sys/sbuf.h>
54 #include <sys/sdt.h>
55 #include <sys/systm.h>
56 #include <sys/mount.h>
57 #include <sys/file.h>
58 #include <sys/namei.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 
64 #include <net/if.h>
65 #include <net/if_var.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip_var.h>
70 
71 #include <security/mac/mac_framework.h>
72 #include <security/mac/mac_internal.h>
73 #include <security/mac/mac_policy.h>
74 
75 static struct label *
76 mac_inpcb_label_alloc(int flag)
77 {
78 	struct label *label;
79 	int error;
80 
81 	label = mac_labelzone_alloc(flag);
82 	if (label == NULL)
83 		return (NULL);
84 	if (flag & M_WAITOK)
85 		MAC_POLICY_CHECK(inpcb_init_label, label, flag);
86 	else
87 		MAC_POLICY_CHECK_NOSLEEP(inpcb_init_label, label, flag);
88 	if (error) {
89 		MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
90 		mac_labelzone_free(label);
91 		return (NULL);
92 	}
93 	return (label);
94 }
95 
96 int
97 mac_inpcb_init(struct inpcb *inp, int flag)
98 {
99 
100 	if (mac_labeled & MPC_OBJECT_INPCB) {
101 		inp->inp_label = mac_inpcb_label_alloc(flag);
102 		if (inp->inp_label == NULL)
103 			return (ENOMEM);
104 	} else
105 		inp->inp_label = NULL;
106 	return (0);
107 }
108 
109 /* Check with rules in module if the IPv4 address is allowed. */
110 int
111 mac_inet_check_add_addr(struct ucred *cred, const struct in_addr *ia,
112     struct ifnet *ifp)
113 {
114 	int error;
115 
116 	MAC_POLICY_CHECK(ip4_check_jail, cred, ia, ifp);
117 	return (error);
118 }
119 
120 static struct label *
121 mac_ipq_label_alloc(int flag)
122 {
123 	struct label *label;
124 	int error;
125 
126 	label = mac_labelzone_alloc(flag);
127 	if (label == NULL)
128 		return (NULL);
129 
130 	if (flag & M_WAITOK)
131 		MAC_POLICY_CHECK(ipq_init_label, label, flag);
132 	else
133 		MAC_POLICY_CHECK_NOSLEEP(ipq_init_label, label, flag);
134 	if (error) {
135 		MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
136 		mac_labelzone_free(label);
137 		return (NULL);
138 	}
139 	return (label);
140 }
141 
142 int
143 mac_ipq_init(struct ipq *q, int flag)
144 {
145 
146 	if (mac_labeled & MPC_OBJECT_IPQ) {
147 		q->ipq_label = mac_ipq_label_alloc(flag);
148 		if (q->ipq_label == NULL)
149 			return (ENOMEM);
150 	} else
151 		q->ipq_label = NULL;
152 	return (0);
153 }
154 
155 static void
156 mac_inpcb_label_free(struct label *label)
157 {
158 
159 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
160 	mac_labelzone_free(label);
161 }
162 
163 void
164 mac_inpcb_destroy(struct inpcb *inp)
165 {
166 
167 	if (inp->inp_label != NULL) {
168 		mac_inpcb_label_free(inp->inp_label);
169 		inp->inp_label = NULL;
170 	}
171 }
172 
173 static void
174 mac_ipq_label_free(struct label *label)
175 {
176 
177 	MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
178 	mac_labelzone_free(label);
179 }
180 
181 void
182 mac_ipq_destroy(struct ipq *q)
183 {
184 
185 	if (q->ipq_label != NULL) {
186 		mac_ipq_label_free(q->ipq_label);
187 		q->ipq_label = NULL;
188 	}
189 }
190 
191 void
192 mac_inpcb_create(struct socket *so, struct inpcb *inp)
193 {
194 
195 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp,
196 	    inp->inp_label);
197 }
198 
199 void
200 mac_ipq_reassemble(struct ipq *q, struct mbuf *m)
201 {
202 	struct label *label;
203 
204 	if (mac_policy_count == 0)
205 		return;
206 
207 	label = mac_mbuf_to_label(m);
208 
209 	MAC_POLICY_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m,
210 	    label);
211 }
212 
213 void
214 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag)
215 {
216 	struct label *mlabel, *fraglabel;
217 
218 	if (mac_policy_count == 0)
219 		return;
220 
221 	mlabel = mac_mbuf_to_label(m);
222 	fraglabel = mac_mbuf_to_label(frag);
223 
224 	MAC_POLICY_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag,
225 	    fraglabel);
226 }
227 
228 void
229 mac_ipq_create(struct mbuf *m, struct ipq *q)
230 {
231 	struct label *label;
232 
233 	if (mac_policy_count == 0)
234 		return;
235 
236 	label = mac_mbuf_to_label(m);
237 
238 	MAC_POLICY_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label);
239 }
240 
241 void
242 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m)
243 {
244 	struct label *mlabel;
245 
246 	INP_LOCK_ASSERT(inp);
247 
248 	if (mac_policy_count == 0)
249 		return;
250 
251 	mlabel = mac_mbuf_to_label(m);
252 
253 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m,
254 	    mlabel);
255 }
256 
257 int
258 mac_ipq_match(struct mbuf *m, struct ipq *q)
259 {
260 	struct label *label;
261 	int result;
262 
263 	if (mac_policy_count == 0)
264 		return (1);
265 
266 	label = mac_mbuf_to_label(m);
267 
268 	result = 1;
269 	MAC_POLICY_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label);
270 
271 	return (result);
272 }
273 
274 void
275 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m)
276 {
277 	struct label *mlabel;
278 	int locked;
279 
280 	if (mac_policy_count == 0)
281 		return;
282 
283 	mlabel = mac_mbuf_to_label(m);
284 
285 	MAC_IFNET_LOCK(ifp, locked);
286 	MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, if_getmaclabel(ifp),
287 	    m, mlabel);
288 	MAC_IFNET_UNLOCK(ifp, locked);
289 }
290 
291 void
292 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend)
293 {
294 	struct label *mrecvlabel, *msendlabel;
295 
296 	if (mac_policy_count == 0)
297 		return;
298 
299 	mrecvlabel = mac_mbuf_to_label(mrecv);
300 	msendlabel = mac_mbuf_to_label(msend);
301 
302 	MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel,
303 	    msend, msendlabel);
304 }
305 
306 void
307 mac_netinet_icmp_replyinplace(struct mbuf *m)
308 {
309 	struct label *label;
310 
311 	if (mac_policy_count == 0)
312 		return;
313 
314 	label = mac_mbuf_to_label(m);
315 
316 	MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label);
317 }
318 
319 void
320 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m)
321 {
322 	struct label *mlabel;
323 	int locked;
324 
325 	if (mac_policy_count == 0)
326 		return;
327 
328 	mlabel = mac_mbuf_to_label(m);
329 
330 	MAC_IFNET_LOCK(ifp, locked);
331 	MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, if_getmaclabel(ifp),
332 	    m, mlabel);
333 	MAC_IFNET_UNLOCK(ifp, locked);
334 }
335 
336 void
337 mac_netinet_tcp_reply(struct mbuf *m)
338 {
339 	struct label *label;
340 
341 	if (mac_policy_count == 0)
342 		return;
343 
344 	label = mac_mbuf_to_label(m);
345 
346 	MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
347 }
348 
349 void
350 mac_ipq_update(struct mbuf *m, struct ipq *q)
351 {
352 	struct label *label;
353 
354 	if (mac_policy_count == 0)
355 		return;
356 
357 	label = mac_mbuf_to_label(m);
358 
359 	MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
360 }
361 
362 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
363     "struct mbuf *");
364 
365 int
366 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
367 {
368 	struct label *label;
369 	int error;
370 
371 	M_ASSERTPKTHDR(m);
372 
373 	if (mac_policy_count == 0)
374 		return (0);
375 
376 	label = mac_mbuf_to_label(m);
377 
378 	MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
379 	    label);
380 	MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
381 
382 	return (error);
383 }
384 
385 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
386     "struct inpcb *");
387 
388 int
389 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
390 {
391 	int error;
392 
393 	INP_LOCK_ASSERT(inp);
394 
395 	MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
396 	    inp->inp_label);
397 	MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
398 
399 	return (error);
400 }
401 
402 void
403 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
404 {
405 
406 	INP_WLOCK_ASSERT(inp);
407 	SOCK_LOCK_ASSERT(so);
408 
409 	MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
410 	    inp->inp_label);
411 }
412 
413 void
414 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
415 {
416 	struct label *mrecvlabel, *msendlabel;
417 
418 	M_ASSERTPKTHDR(mrecv);
419 	M_ASSERTPKTHDR(msend);
420 
421 	if (mac_policy_count == 0)
422 		return;
423 
424 	mrecvlabel = mac_mbuf_to_label(mrecv);
425 	msendlabel = mac_mbuf_to_label(msend);
426 
427 	MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
428 	    msend, msendlabel);
429 }
430 
431 void
432 mac_netinet_firewall_send(struct mbuf *m)
433 {
434 	struct label *label;
435 
436 	M_ASSERTPKTHDR(m);
437 
438 	if (mac_policy_count == 0)
439 		return;
440 
441 	label = mac_mbuf_to_label(m);
442 
443 	MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
444 }
445 
446 /*
447  * These functions really should be referencing the syncache structure
448  * instead of the label.  However, due to some of the complexities associated
449  * with exposing this syncache structure we operate directly on its label
450  * pointer.  This should be OK since we aren't making any access control
451  * decisions within this code directly, we are merely allocating and copying
452  * label storage so we can properly initialize mbuf labels for any packets
453  * the syncache code might create.
454  */
455 void
456 mac_syncache_destroy(struct label **label)
457 {
458 
459 	if (*label != NULL) {
460 		MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
461 		mac_labelzone_free(*label);
462 		*label = NULL;
463 	}
464 }
465 
466 int
467 mac_syncache_init(struct label **label)
468 {
469 	int error;
470 
471 	if (mac_labeled & MPC_OBJECT_SYNCACHE) {
472 		*label = mac_labelzone_alloc(M_NOWAIT);
473 		if (*label == NULL)
474 			return (ENOMEM);
475 		/*
476 		 * Since we are holding the inpcb locks the policy can not
477 		 * allocate policy specific label storage using M_WAITOK.  So
478 		 * we need to do a MAC_CHECK instead of the typical
479 		 * MAC_PERFORM so we can propagate allocation failures back
480 		 * to the syncache code.
481 		 */
482 		MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
483 		    M_NOWAIT);
484 		if (error) {
485 			MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
486 			    *label);
487 			mac_labelzone_free(*label);
488 		}
489 		return (error);
490 	} else
491 		*label = NULL;
492 	return (0);
493 }
494 
495 void
496 mac_syncache_create(struct label *label, struct inpcb *inp)
497 {
498 
499 	INP_LOCK_ASSERT(inp);
500 
501 	MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
502 }
503 
504 void
505 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
506 {
507 	struct label *mlabel;
508 
509 	M_ASSERTPKTHDR(m);
510 
511 	if (mac_policy_count == 0)
512 		return;
513 
514 	mlabel = mac_mbuf_to_label(m);
515 
516 	MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,
517 	    mlabel);
518 }
519