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$");
50 #include <sys/param.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
57 #include <sys/systm.h>
58 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/protosw.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysctl.h>
67 #include <net/if_var.h>
69 #include <netinet/in.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/ip_var.h>
73 #include <security/mac/mac_framework.h>
74 #include <security/mac/mac_internal.h>
75 #include <security/mac/mac_policy.h>
78 mac_inpcb_label_alloc(int flag)
83 label = mac_labelzone_alloc(flag);
87 MAC_POLICY_CHECK(inpcb_init_label, label, flag);
89 MAC_POLICY_CHECK_NOSLEEP(inpcb_init_label, label, flag);
91 MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
92 mac_labelzone_free(label);
99 mac_inpcb_init(struct inpcb *inp, int flag)
102 if (mac_labeled & MPC_OBJECT_INPCB) {
103 inp->inp_label = mac_inpcb_label_alloc(flag);
104 if (inp->inp_label == NULL)
107 inp->inp_label = NULL;
111 static struct label *
112 mac_ipq_label_alloc(int flag)
117 label = mac_labelzone_alloc(flag);
122 MAC_POLICY_CHECK(ipq_init_label, label, flag);
124 MAC_POLICY_CHECK_NOSLEEP(ipq_init_label, label, flag);
126 MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
127 mac_labelzone_free(label);
134 mac_ipq_init(struct ipq *q, int flag)
137 if (mac_labeled & MPC_OBJECT_IPQ) {
138 q->ipq_label = mac_ipq_label_alloc(flag);
139 if (q->ipq_label == NULL)
147 mac_inpcb_label_free(struct label *label)
150 MAC_POLICY_PERFORM_NOSLEEP(inpcb_destroy_label, label);
151 mac_labelzone_free(label);
155 mac_inpcb_destroy(struct inpcb *inp)
158 if (inp->inp_label != NULL) {
159 mac_inpcb_label_free(inp->inp_label);
160 inp->inp_label = NULL;
165 mac_ipq_label_free(struct label *label)
168 MAC_POLICY_PERFORM_NOSLEEP(ipq_destroy_label, label);
169 mac_labelzone_free(label);
173 mac_ipq_destroy(struct ipq *q)
176 if (q->ipq_label != NULL) {
177 mac_ipq_label_free(q->ipq_label);
183 mac_inpcb_create(struct socket *so, struct inpcb *inp)
186 MAC_POLICY_PERFORM_NOSLEEP(inpcb_create, so, so->so_label, inp,
191 mac_ipq_reassemble(struct ipq *q, struct mbuf *m)
195 if (mac_policy_count == 0)
198 label = mac_mbuf_to_label(m);
200 MAC_POLICY_PERFORM_NOSLEEP(ipq_reassemble, q, q->ipq_label, m,
205 mac_netinet_fragment(struct mbuf *m, struct mbuf *frag)
207 struct label *mlabel, *fraglabel;
209 if (mac_policy_count == 0)
212 mlabel = mac_mbuf_to_label(m);
213 fraglabel = mac_mbuf_to_label(frag);
215 MAC_POLICY_PERFORM_NOSLEEP(netinet_fragment, m, mlabel, frag,
220 mac_ipq_create(struct mbuf *m, struct ipq *q)
224 if (mac_policy_count == 0)
227 label = mac_mbuf_to_label(m);
229 MAC_POLICY_PERFORM_NOSLEEP(ipq_create, m, label, q, q->ipq_label);
233 mac_inpcb_create_mbuf(struct inpcb *inp, struct mbuf *m)
235 struct label *mlabel;
237 INP_LOCK_ASSERT(inp);
239 if (mac_policy_count == 0)
242 mlabel = mac_mbuf_to_label(m);
244 MAC_POLICY_PERFORM_NOSLEEP(inpcb_create_mbuf, inp, inp->inp_label, m,
249 mac_ipq_match(struct mbuf *m, struct ipq *q)
254 if (mac_policy_count == 0)
257 label = mac_mbuf_to_label(m);
260 MAC_POLICY_BOOLEAN_NOSLEEP(ipq_match, &&, m, label, q, q->ipq_label);
266 mac_netinet_arp_send(struct ifnet *ifp, struct mbuf *m)
268 struct label *mlabel;
270 if (mac_policy_count == 0)
273 mlabel = mac_mbuf_to_label(m);
276 MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m,
278 MAC_IFNET_UNLOCK(ifp);
282 mac_netinet_icmp_reply(struct mbuf *mrecv, struct mbuf *msend)
284 struct label *mrecvlabel, *msendlabel;
286 if (mac_policy_count == 0)
289 mrecvlabel = mac_mbuf_to_label(mrecv);
290 msendlabel = mac_mbuf_to_label(msend);
292 MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_reply, mrecv, mrecvlabel,
297 mac_netinet_icmp_replyinplace(struct mbuf *m)
301 if (mac_policy_count == 0)
304 label = mac_mbuf_to_label(m);
306 MAC_POLICY_PERFORM_NOSLEEP(netinet_icmp_replyinplace, m, label);
310 mac_netinet_igmp_send(struct ifnet *ifp, struct mbuf *m)
312 struct label *mlabel;
314 if (mac_policy_count == 0)
317 mlabel = mac_mbuf_to_label(m);
320 MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m,
322 MAC_IFNET_UNLOCK(ifp);
326 mac_netinet_tcp_reply(struct mbuf *m)
330 if (mac_policy_count == 0)
333 label = mac_mbuf_to_label(m);
335 MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
339 mac_ipq_update(struct mbuf *m, struct ipq *q)
343 if (mac_policy_count == 0)
346 label = mac_mbuf_to_label(m);
348 MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
351 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
355 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
362 if (mac_policy_count == 0)
365 label = mac_mbuf_to_label(m);
367 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
369 MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
374 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
378 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
382 INP_LOCK_ASSERT(inp);
384 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
386 MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
392 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
395 INP_WLOCK_ASSERT(inp);
396 SOCK_LOCK_ASSERT(so);
398 MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
403 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
405 struct label *mrecvlabel, *msendlabel;
407 M_ASSERTPKTHDR(mrecv);
408 M_ASSERTPKTHDR(msend);
410 if (mac_policy_count == 0)
413 mrecvlabel = mac_mbuf_to_label(mrecv);
414 msendlabel = mac_mbuf_to_label(msend);
416 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
421 mac_netinet_firewall_send(struct mbuf *m)
427 if (mac_policy_count == 0)
430 label = mac_mbuf_to_label(m);
432 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
436 * These functions really should be referencing the syncache structure
437 * instead of the label. However, due to some of the complexities associated
438 * with exposing this syncache structure we operate directly on its label
439 * pointer. This should be OK since we aren't making any access control
440 * decisions within this code directly, we are merely allocating and copying
441 * label storage so we can properly initialize mbuf labels for any packets
442 * the syncache code might create.
445 mac_syncache_destroy(struct label **label)
448 if (*label != NULL) {
449 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
450 mac_labelzone_free(*label);
456 mac_syncache_init(struct label **label)
460 if (mac_labeled & MPC_OBJECT_SYNCACHE) {
461 *label = mac_labelzone_alloc(M_NOWAIT);
465 * Since we are holding the inpcb locks the policy can not
466 * allocate policy specific label storage using M_WAITOK. So
467 * we need to do a MAC_CHECK instead of the typical
468 * MAC_PERFORM so we can propagate allocation failures back
469 * to the syncache code.
471 MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
474 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
476 mac_labelzone_free(*label);
485 mac_syncache_create(struct label *label, struct inpcb *inp)
488 INP_WLOCK_ASSERT(inp);
490 MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
494 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
496 struct label *mlabel;
500 if (mac_policy_count == 0)
503 mlabel = mac_mbuf_to_label(m);
505 MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,