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.
9 * This software was developed by Robert Watson and Ilmar Habibulin for the
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.
17 * This software was enhanced by SPARTA ISSO under SPAWAR contract
18 * N66001-04-C-6019 ("SEFOS").
20 * This software was developed at the University of Cambridge Computer
21 * Laboratory with support from a grant from Google, Inc.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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.
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
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include "opt_kdtrace.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
58 #include <sys/systm.h>
59 #include <sys/mount.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>
68 #include <net/if_var.h>
70 #include <netinet/in.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/ip_var.h>
74 #include <security/mac/mac_framework.h>
75 #include <security/mac/mac_internal.h>
76 #include <security/mac/mac_policy.h>
79 mac_inpcb_label_alloc(int flag)
84 label = mac_labelzone_alloc(flag);
88 MAC_POLICY_CHECK(inpcb_init_label, label, flag);
90 MAC_POLICY_CHECK_NOSLEEP(inpcb_init_label, label, flag);
92 MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
93 mac_labelzone_free(label);
100 mac_inpcb_init(struct inpcb *inp, int flag)
103 if (mac_labeled & MPC_OBJECT_INPCB) {
104 inp->inp_label = mac_inpcb_label_alloc(flag);
105 if (inp->inp_label == NULL)
108 inp->inp_label = NULL;
112 static struct label *
113 mac_ipq_label_alloc(int flag)
118 label = mac_labelzone_alloc(flag);
123 MAC_POLICY_CHECK(ipq_init_label, label, flag);
125 MAC_POLICY_CHECK_NOSLEEP(ipq_init_label, label, flag);
127 MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
128 mac_labelzone_free(label);
135 mac_ipq_init(struct ipq *q, int flag)
138 if (mac_labeled & MPC_OBJECT_IPQ) {
139 q->ipq_label = mac_ipq_label_alloc(flag);
140 if (q->ipq_label == NULL)
148 mac_inpcb_label_free(struct label *label)
151 MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
152 mac_labelzone_free(label);
156 mac_inpcb_destroy(struct inpcb *inp)
159 if (inp->inp_label != NULL) {
160 mac_inpcb_label_free(inp->inp_label);
161 inp->inp_label = NULL;
166 mac_ipq_label_free(struct label *label)
169 MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
170 mac_labelzone_free(label);
174 mac_ipq_destroy(struct ipq *q)
177 if (q->ipq_label != NULL) {
178 mac_ipq_label_free(q->ipq_label);
184 mac_inpcb_create(struct socket *so, struct inpcb *inp)
187 MAC_POLICY_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp,
192 mac_ipq_reassemble(struct ipq *q, struct mbuf *m)
196 if (mac_policy_count == 0)
199 label = mac_mbuf_to_label(m);
201 MAC_POLICY_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m,
206 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag)
208 struct label *mlabel, *fraglabel;
210 if (mac_policy_count == 0)
213 mlabel = mac_mbuf_to_label(m);
214 fraglabel = mac_mbuf_to_label(frag);
216 MAC_POLICY_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag,
221 mac_ipq_create(struct mbuf *m, struct ipq *q)
225 if (mac_policy_count == 0)
228 label = mac_mbuf_to_label(m);
230 MAC_POLICY_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label);
234 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m)
236 struct label *mlabel;
238 INP_LOCK_ASSERT(inp);
240 if (mac_policy_count == 0)
243 mlabel = mac_mbuf_to_label(m);
245 MAC_POLICY_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m,
250 mac_ipq_match(struct mbuf *m, struct ipq *q)
255 if (mac_policy_count == 0)
258 label = mac_mbuf_to_label(m);
261 MAC_POLICY_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label);
267 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m)
269 struct label *mlabel;
271 if (mac_policy_count == 0)
274 mlabel = mac_mbuf_to_label(m);
277 MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m,
279 MAC_IFNET_UNLOCK(ifp);
283 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend)
285 struct label *mrecvlabel, *msendlabel;
287 if (mac_policy_count == 0)
290 mrecvlabel = mac_mbuf_to_label(mrecv);
291 msendlabel = mac_mbuf_to_label(msend);
293 MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel,
298 mac_netinet_icmp_replyinplace(struct mbuf *m)
302 if (mac_policy_count == 0)
305 label = mac_mbuf_to_label(m);
307 MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label);
311 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m)
313 struct label *mlabel;
315 if (mac_policy_count == 0)
318 mlabel = mac_mbuf_to_label(m);
321 MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m,
323 MAC_IFNET_UNLOCK(ifp);
327 mac_netinet_tcp_reply(struct mbuf *m)
331 if (mac_policy_count == 0)
334 label = mac_mbuf_to_label(m);
336 MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
340 mac_ipq_update(struct mbuf *m, struct ipq *q)
344 if (mac_policy_count == 0)
347 label = mac_mbuf_to_label(m);
349 MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
352 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
356 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
363 if (mac_policy_count == 0)
366 label = mac_mbuf_to_label(m);
368 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
370 MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
375 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
379 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
383 INP_LOCK_ASSERT(inp);
385 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
387 MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
393 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
396 INP_WLOCK_ASSERT(inp);
397 SOCK_LOCK_ASSERT(so);
399 MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
404 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
406 struct label *mrecvlabel, *msendlabel;
408 M_ASSERTPKTHDR(mrecv);
409 M_ASSERTPKTHDR(msend);
411 if (mac_policy_count == 0)
414 mrecvlabel = mac_mbuf_to_label(mrecv);
415 msendlabel = mac_mbuf_to_label(msend);
417 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
422 mac_netinet_firewall_send(struct mbuf *m)
428 if (mac_policy_count == 0)
431 label = mac_mbuf_to_label(m);
433 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
437 * These functions really should be referencing the syncache structure
438 * instead of the label. However, due to some of the complexities associated
439 * with exposing this syncache structure we operate directly on it's label
440 * pointer. This should be OK since we aren't making any access control
441 * decisions within this code directly, we are merely allocating and copying
442 * label storage so we can properly initialize mbuf labels for any packets
443 * the syncache code might create.
446 mac_syncache_destroy(struct label **label)
449 if (*label != NULL) {
450 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
451 mac_labelzone_free(*label);
457 mac_syncache_init(struct label **label)
461 if (mac_labeled & MPC_OBJECT_SYNCACHE) {
462 *label = mac_labelzone_alloc(M_NOWAIT);
466 * Since we are holding the inpcb locks the policy can not
467 * allocate policy specific label storage using M_WAITOK. So
468 * we need to do a MAC_CHECK instead of the typical
469 * MAC_PERFORM so we can propagate allocation failures back
470 * to the syncache code.
472 MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
475 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
477 mac_labelzone_free(*label);
486 mac_syncache_create(struct label *label, struct inpcb *inp)
489 INP_WLOCK_ASSERT(inp);
491 MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
495 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
497 struct label *mlabel;
501 if (mac_policy_count == 0)
504 mlabel = mac_mbuf_to_label(m);
506 MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,