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.
7 * This software was developed by Robert Watson and Ilmar Habibulin for the
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.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
48 #include <sys/systm.h>
49 #include <sys/mount.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>
58 #include <net/if_var.h>
60 #include <netinet/in.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
64 #include <security/mac/mac_framework.h>
65 #include <security/mac/mac_internal.h>
66 #include <security/mac/mac_policy.h>
69 mac_inpcb_label_alloc(int flag)
74 label = mac_labelzone_alloc(flag);
77 MAC_CHECK(init_inpcb_label, label, flag);
79 MAC_PERFORM(destroy_inpcb_label, label);
80 mac_labelzone_free(label);
87 mac_init_inpcb(struct inpcb *inp, int flag)
90 inp->inp_label = mac_inpcb_label_alloc(flag);
91 if (inp->inp_label == NULL)
97 mac_ipq_label_alloc(int flag)
102 label = mac_labelzone_alloc(flag);
106 MAC_CHECK(init_ipq_label, label, flag);
108 MAC_PERFORM(destroy_ipq_label, label);
109 mac_labelzone_free(label);
116 mac_init_ipq(struct ipq *q, int flag)
119 q->ipq_label = mac_ipq_label_alloc(flag);
120 if (q->ipq_label == NULL)
126 mac_inpcb_label_free(struct label *label)
129 MAC_PERFORM(destroy_inpcb_label, label);
130 mac_labelzone_free(label);
134 mac_destroy_inpcb(struct inpcb *inp)
137 mac_inpcb_label_free(inp->inp_label);
138 inp->inp_label = NULL;
142 mac_ipq_label_free(struct label *label)
145 MAC_PERFORM(destroy_ipq_label, label);
146 mac_labelzone_free(label);
150 mac_destroy_ipq(struct ipq *q)
153 mac_ipq_label_free(q->ipq_label);
158 mac_create_inpcb_from_socket(struct socket *so, struct inpcb *inp)
161 MAC_PERFORM(create_inpcb_from_socket, so, so->so_label, inp,
166 mac_create_datagram_from_ipq(struct ipq *q, struct mbuf *m)
170 label = mac_mbuf_to_label(m);
172 MAC_PERFORM(create_datagram_from_ipq, q, q->ipq_label, m, label);
176 mac_create_fragment(struct mbuf *m, struct mbuf *frag)
178 struct label *mlabel, *fraglabel;
180 mlabel = mac_mbuf_to_label(m);
181 fraglabel = mac_mbuf_to_label(frag);
183 MAC_PERFORM(create_fragment, m, mlabel, frag, fraglabel);
187 mac_create_ipq(struct mbuf *m, struct ipq *q)
191 label = mac_mbuf_to_label(m);
193 MAC_PERFORM(create_ipq, m, label, q, q->ipq_label);
197 mac_create_mbuf_from_inpcb(struct inpcb *inp, struct mbuf *m)
199 struct label *mlabel;
201 INP_LOCK_ASSERT(inp);
202 mlabel = mac_mbuf_to_label(m);
204 MAC_PERFORM(create_mbuf_from_inpcb, inp, inp->inp_label, m, mlabel);
208 mac_fragment_match(struct mbuf *m, struct ipq *q)
213 label = mac_mbuf_to_label(m);
216 MAC_BOOLEAN(fragment_match, &&, m, label, q, q->ipq_label);
222 mac_reflect_mbuf_icmp(struct mbuf *m)
226 label = mac_mbuf_to_label(m);
228 MAC_PERFORM(reflect_mbuf_icmp, m, label);
232 mac_reflect_mbuf_tcp(struct mbuf *m)
236 label = mac_mbuf_to_label(m);
238 MAC_PERFORM(reflect_mbuf_tcp, m, label);
242 mac_update_ipq(struct mbuf *m, struct ipq *q)
246 label = mac_mbuf_to_label(m);
248 MAC_PERFORM(update_ipq, m, label, q, q->ipq_label);
252 mac_check_inpcb_deliver(struct inpcb *inp, struct mbuf *m)
259 label = mac_mbuf_to_label(m);
261 MAC_CHECK(check_inpcb_deliver, inp, inp->inp_label, m, label);
267 mac_check_inpcb_visible(struct ucred *cred, struct inpcb *inp)
271 INP_LOCK_ASSERT(inp);
273 MAC_CHECK(check_inpcb_visible, cred, inp, inp->inp_label);
279 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
282 INP_WLOCK_ASSERT(inp);
283 SOCK_LOCK_ASSERT(so);
284 MAC_PERFORM(inpcb_sosetlabel, so, so->so_label, inp, inp->inp_label);
288 mac_create_mbuf_from_firewall(struct mbuf *m)
293 label = mac_mbuf_to_label(m);
294 MAC_PERFORM(create_mbuf_from_firewall, m, label);
298 * These functions really should be referencing the syncache structure
299 * instead of the label. However, due to some of the complexities associated
300 * with exposing this syncache structure we operate directly on it's label
301 * pointer. This should be OK since we aren't making any access control
302 * decisions within this code directly, we are merely allocating and copying
303 * label storage so we can properly initialize mbuf labels for any packets
304 * the syncache code might create.
307 mac_destroy_syncache(struct label **label)
310 MAC_PERFORM(destroy_syncache_label, *label);
311 mac_labelzone_free(*label);
316 mac_init_syncache(struct label **label)
320 *label = mac_labelzone_alloc(M_NOWAIT);
324 * Since we are holding the inpcb locks the policy can not allocate
325 * policy specific label storage using M_WAITOK. So we need to do a
326 * MAC_CHECK instead of the typical MAC_PERFORM so we can propagate
327 * allocation failures back to the syncache code.
329 MAC_CHECK(init_syncache_label, *label, M_NOWAIT);
331 MAC_PERFORM(destroy_syncache_label, *label);
332 mac_labelzone_free(*label);
338 mac_init_syncache_from_inpcb(struct label *label, struct inpcb *inp)
341 INP_WLOCK_ASSERT(inp);
342 MAC_PERFORM(init_syncache_from_inpcb, label, inp);
346 mac_create_mbuf_from_syncache(struct label *sc_label, struct mbuf *m)
348 struct label *mlabel;
351 mlabel = mac_mbuf_to_label(m);
352 MAC_PERFORM(create_mbuf_from_syncache, sc_label, m, mlabel);