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.
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;
271 if (mac_policy_count == 0)
274 mlabel = mac_mbuf_to_label(m);
276 MAC_IFNET_LOCK(ifp, locked);
277 MAC_POLICY_PERFORM_NOSLEEP(netinet_arp_send, ifp, ifp->if_label, m,
279 MAC_IFNET_UNLOCK(ifp, locked);
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;
316 if (mac_policy_count == 0)
319 mlabel = mac_mbuf_to_label(m);
321 MAC_IFNET_LOCK(ifp, locked);
322 MAC_POLICY_PERFORM_NOSLEEP(netinet_igmp_send, ifp, ifp->if_label, m,
324 MAC_IFNET_UNLOCK(ifp, locked);
328 mac_netinet_tcp_reply(struct mbuf *m)
332 if (mac_policy_count == 0)
335 label = mac_mbuf_to_label(m);
337 MAC_POLICY_PERFORM_NOSLEEP(netinet_tcp_reply, m, label);
341 mac_ipq_update(struct mbuf *m, struct ipq *q)
345 if (mac_policy_count == 0)
348 label = mac_mbuf_to_label(m);
350 MAC_POLICY_PERFORM_NOSLEEP(ipq_update, m, label, q, q->ipq_label);
353 MAC_CHECK_PROBE_DEFINE2(inpcb_check_deliver, "struct inpcb *",
357 mac_inpcb_check_deliver(struct inpcb *inp, struct mbuf *m)
364 if (mac_policy_count == 0)
367 label = mac_mbuf_to_label(m);
369 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_deliver, inp, inp->inp_label, m,
371 MAC_CHECK_PROBE2(inpcb_check_deliver, error, inp, m);
376 MAC_CHECK_PROBE_DEFINE2(inpcb_check_visible, "struct ucred *",
380 mac_inpcb_check_visible(struct ucred *cred, struct inpcb *inp)
384 INP_LOCK_ASSERT(inp);
386 MAC_POLICY_CHECK_NOSLEEP(inpcb_check_visible, cred, inp,
388 MAC_CHECK_PROBE2(inpcb_check_visible, error, cred, inp);
394 mac_inpcb_sosetlabel(struct socket *so, struct inpcb *inp)
397 INP_WLOCK_ASSERT(inp);
398 SOCK_LOCK_ASSERT(so);
400 MAC_POLICY_PERFORM_NOSLEEP(inpcb_sosetlabel, so, so->so_label, inp,
405 mac_netinet_firewall_reply(struct mbuf *mrecv, struct mbuf *msend)
407 struct label *mrecvlabel, *msendlabel;
409 M_ASSERTPKTHDR(mrecv);
410 M_ASSERTPKTHDR(msend);
412 if (mac_policy_count == 0)
415 mrecvlabel = mac_mbuf_to_label(mrecv);
416 msendlabel = mac_mbuf_to_label(msend);
418 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_reply, mrecv, mrecvlabel,
423 mac_netinet_firewall_send(struct mbuf *m)
429 if (mac_policy_count == 0)
432 label = mac_mbuf_to_label(m);
434 MAC_POLICY_PERFORM_NOSLEEP(netinet_firewall_send, m, label);
438 * These functions really should be referencing the syncache structure
439 * instead of the label. However, due to some of the complexities associated
440 * with exposing this syncache structure we operate directly on its label
441 * pointer. This should be OK since we aren't making any access control
442 * decisions within this code directly, we are merely allocating and copying
443 * label storage so we can properly initialize mbuf labels for any packets
444 * the syncache code might create.
447 mac_syncache_destroy(struct label **label)
450 if (*label != NULL) {
451 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label, *label);
452 mac_labelzone_free(*label);
458 mac_syncache_init(struct label **label)
462 if (mac_labeled & MPC_OBJECT_SYNCACHE) {
463 *label = mac_labelzone_alloc(M_NOWAIT);
467 * Since we are holding the inpcb locks the policy can not
468 * allocate policy specific label storage using M_WAITOK. So
469 * we need to do a MAC_CHECK instead of the typical
470 * MAC_PERFORM so we can propagate allocation failures back
471 * to the syncache code.
473 MAC_POLICY_CHECK_NOSLEEP(syncache_init_label, *label,
476 MAC_POLICY_PERFORM_NOSLEEP(syncache_destroy_label,
478 mac_labelzone_free(*label);
487 mac_syncache_create(struct label *label, struct inpcb *inp)
490 INP_WLOCK_ASSERT(inp);
492 MAC_POLICY_PERFORM_NOSLEEP(syncache_create, label, inp);
496 mac_syncache_create_mbuf(struct label *sc_label, struct mbuf *m)
498 struct label *mlabel;
502 if (mac_policy_count == 0)
505 mlabel = mac_mbuf_to_label(m);
507 MAC_POLICY_PERFORM_NOSLEEP(syncache_create_mbuf, sc_label, m,