mac_ifoff.c revision 166905
1193323Sed/*-
2193323Sed * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3193323Sed * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4193323Sed * All rights reserved.
5193323Sed *
6193323Sed * This software was developed by Robert Watson for the TrustedBSD Project.
7193323Sed *
8193323Sed * This software was developed for the FreeBSD Project in part by Network
9193323Sed * Associates Laboratories, the Security Research Division of Network
10193323Sed * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11193323Sed * as part of the DARPA CHATS research program.
12193323Sed *
13193323Sed * Redistribution and use in source and binary forms, with or without
14193323Sed * modification, are permitted provided that the following conditions
15193323Sed * are met:
16193323Sed * 1. Redistributions of source code must retain the above copyright
17193323Sed *    notice, this list of conditions and the following disclaimer.
18193323Sed * 2. Redistributions in binary form must reproduce the above copyright
19193323Sed *    notice, this list of conditions and the following disclaimer in the
20193323Sed *    documentation and/or other materials provided with the distribution.
21193323Sed *
22193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23198090Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24198090Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32193323Sed * SUCH DAMAGE.
33193323Sed *
34193323Sed * $FreeBSD: head/sys/security/mac_ifoff/mac_ifoff.c 166905 2007-02-23 14:39:04Z rwatson $
35193323Sed */
36193323Sed
37207618Srdivacky/*
38193323Sed * Developed by the TrustedBSD Project.
39193323Sed * Limit access to interfaces until they are specifically administratively
40193323Sed * enabled.  Prevents protocol stack-driven packet leakage in unsafe
41193323Sed * environments.
42193323Sed */
43193323Sed
44193323Sed#include <sys/param.h>
45193323Sed#include <sys/kernel.h>
46193323Sed#include <sys/module.h>
47193323Sed#include <sys/socket.h>
48193323Sed#include <sys/sysctl.h>
49193323Sed
50193323Sed#include <net/bpfdesc.h>
51193323Sed#include <net/if_types.h>
52193323Sed
53193323Sed#include <security/mac/mac_policy.h>
54193323Sed
55193323SedSYSCTL_DECL(_security_mac);
56193323Sed
57193323SedSYSCTL_NODE(_security_mac, OID_AUTO, ifoff, CTLFLAG_RW, 0,
58193323Sed    "TrustedBSD mac_ifoff policy controls");
59193323Sed
60193323Sedstatic int	mac_ifoff_enabled = 1;
61193323SedSYSCTL_INT(_security_mac_ifoff, OID_AUTO, enabled, CTLFLAG_RW,
62193323Sed    &mac_ifoff_enabled, 0, "Enforce ifoff policy");
63193323SedTUNABLE_INT("security.mac.ifoff.enabled", &mac_ifoff_enabled);
64193323Sed
65193323Sedstatic int	mac_ifoff_lo_enabled = 1;
66193323SedSYSCTL_INT(_security_mac_ifoff, OID_AUTO, lo_enabled, CTLFLAG_RW,
67193323Sed    &mac_ifoff_lo_enabled, 0, "Enable loopback interfaces");
68193323SedTUNABLE_INT("security.mac.ifoff.lo_enabled", &mac_ifoff_lo_enabled);
69193323Sed
70193323Sedstatic int	mac_ifoff_other_enabled = 0;
71193323SedSYSCTL_INT(_security_mac_ifoff, OID_AUTO, other_enabled, CTLFLAG_RW,
72193323Sed    &mac_ifoff_other_enabled, 0, "Enable other interfaces");
73193323SedTUNABLE_INT("security.mac.ifoff.other_enabled", &mac_ifoff_other_enabled);
74193323Sed
75193323Sedstatic int	mac_ifoff_bpfrecv_enabled = 0;
76193323SedSYSCTL_INT(_security_mac_ifoff, OID_AUTO, bpfrecv_enabled, CTLFLAG_RW,
77193323Sed    &mac_ifoff_bpfrecv_enabled, 0, "Enable BPF reception even when interface "
78193323Sed    "is disabled");
79193323SedTUNABLE_INT("security.mac.ifoff.bpfrecv.enabled", &mac_ifoff_bpfrecv_enabled);
80193323Sed
81193323Sedstatic int
82200581Srdivackycheck_ifnet_outgoing(struct ifnet *ifnet)
83193323Sed{
84193323Sed
85193323Sed	if (!mac_ifoff_enabled)
86193323Sed		return (0);
87193323Sed
88193323Sed	if (mac_ifoff_lo_enabled && ifnet->if_type == IFT_LOOP)
89193323Sed		return (0);
90193323Sed
91193323Sed	if (mac_ifoff_other_enabled && ifnet->if_type != IFT_LOOP)
92193323Sed		return (0);
93193323Sed
94193323Sed	return (EPERM);
95193323Sed}
96193323Sed
97193323Sedstatic int
98193323Sedcheck_ifnet_incoming(struct ifnet *ifnet, int viabpf)
99193323Sed{
100193323Sed	if (!mac_ifoff_enabled)
101193323Sed		return (0);
102193323Sed
103193323Sed	if (mac_ifoff_lo_enabled && ifnet->if_type == IFT_LOOP)
104193323Sed		return (0);
105193323Sed
106193323Sed	if (mac_ifoff_other_enabled && ifnet->if_type != IFT_LOOP)
107193323Sed		return (0);
108193323Sed
109193323Sed	if (viabpf && mac_ifoff_bpfrecv_enabled)
110193323Sed		return (0);
111193323Sed
112193323Sed	return (EPERM);
113193323Sed}
114193323Sed
115193323Sedstatic int
116193323Sedmac_ifoff_check_bpfdesc_receive(struct bpf_d *bpf_d, struct label *bpflabel,
117193323Sed    struct ifnet *ifnet, struct label *ifnetlabel)
118193323Sed{
119210299Sed
120193323Sed	return (check_ifnet_incoming(ifnet, 1));
121193323Sed}
122193323Sed
123193323Sedstatic int
124193323Sedmac_ifoff_check_ifnet_transmit(struct ifnet *ifnet, struct label *ifnetlabel,
125193323Sed    struct mbuf *m, struct label *mbuflabel)
126193323Sed{
127193323Sed
128193323Sed	return (check_ifnet_outgoing(ifnet));
129198090Srdivacky}
130193323Sed
131193323Sedstatic int
132193323Sedmac_ifoff_check_inpcb_deliver(struct inpcb *inp, struct label *inplabel,
133193323Sed    struct mbuf *m, struct label *mlabel)
134193323Sed{
135193323Sed
136193323Sed	M_ASSERTPKTHDR(m);
137193323Sed	if (m->m_pkthdr.rcvif != NULL)
138193323Sed		return (check_ifnet_incoming(m->m_pkthdr.rcvif, 0));
139193323Sed
140193323Sed	return (0);
141193323Sed}
142193323Sed
143193323Sedstatic int
144193323Sedmac_ifoff_check_socket_deliver(struct socket *so, struct label *socketlabel,
145193323Sed    struct mbuf *m, struct label *mbuflabel)
146193323Sed{
147193323Sed
148193323Sed	M_ASSERTPKTHDR(m);
149193323Sed	if (m->m_pkthdr.rcvif != NULL)
150193323Sed		return (check_ifnet_incoming(m->m_pkthdr.rcvif, 0));
151193323Sed
152193323Sed	return (0);
153193323Sed}
154193323Sed
155193323Sedstatic struct mac_policy_ops mac_ifoff_ops =
156193323Sed{
157193323Sed	.mpo_check_bpfdesc_receive = mac_ifoff_check_bpfdesc_receive,
158193323Sed	.mpo_check_ifnet_transmit = mac_ifoff_check_ifnet_transmit,
159193323Sed	.mpo_check_inpcb_deliver = mac_ifoff_check_inpcb_deliver,
160193323Sed	.mpo_check_socket_deliver = mac_ifoff_check_socket_deliver,
161193323Sed};
162193323Sed
163193323SedMAC_POLICY_SET(&mac_ifoff_ops, mac_ifoff, "TrustedBSD MAC/ifoff",
164193323Sed    MPC_LOADTIME_FLAG_UNLOADOK, NULL);
165193323Sed