1 /*-
2 * Copyright (c) 1999-2002, 2007 Robert N. M. Watson
3 * Copyright (c) 2001-2002 Networks Associates Technology, Inc.
4 * Copyright (c) 2006 SPARTA, Inc.
5 * All rights reserved.
6 *
7 * This software was developed by Robert Watson for the TrustedBSD Project.
8 *
9 * This software was developed for the FreeBSD Project in part by Network
10 * Associates Laboratories, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
12 * as part of the DARPA CHATS research program.
13 *
14 * This software was enhanced by SPARTA ISSO under SPAWAR contract
15 * N66001-04-C-6019 ("SEFOS").
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 /*
40 * Developed by the TrustedBSD Project.
41 *
42 * Limit access to interfaces until they are specifically administratively
43 * enabled. Prevents protocol stack-driven packet leakage in unsafe
44 * environments.
45 */
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_types.h>
57 #include <net/bpfdesc.h>
58
59 #include <security/mac/mac_policy.h>
60
61 static SYSCTL_NODE(_security_mac, OID_AUTO, ifoff,
62 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
63 "TrustedBSD mac_ifoff policy controls");
64
65 static int ifoff_enabled = 1;
66 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, enabled, CTLFLAG_RWTUN,
67 &ifoff_enabled, 0, "Enforce ifoff policy");
68
69 static int ifoff_lo_enabled = 1;
70 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, lo_enabled, CTLFLAG_RWTUN,
71 &ifoff_lo_enabled, 0, "Enable loopback interfaces");
72
73 static int ifoff_other_enabled = 0;
74 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, other_enabled, CTLFLAG_RWTUN,
75 &ifoff_other_enabled, 0, "Enable other interfaces");
76
77 static int ifoff_bpfrecv_enabled = 0;
78 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, bpfrecv_enabled, CTLFLAG_RWTUN,
79 &ifoff_bpfrecv_enabled, 0, "Enable BPF reception even when interface "
80 "is disabled");
81
82 static int
ifnet_check_outgoing(struct ifnet * ifp)83 ifnet_check_outgoing(struct ifnet *ifp)
84 {
85
86 if (!ifoff_enabled)
87 return (0);
88
89 if (ifoff_lo_enabled && if_gettype(ifp) == IFT_LOOP)
90 return (0);
91
92 if (ifoff_other_enabled && if_gettype(ifp) != IFT_LOOP)
93 return (0);
94
95 return (EPERM);
96 }
97
98 static int
ifnet_check_incoming(struct ifnet * ifp,int viabpf)99 ifnet_check_incoming(struct ifnet *ifp, int viabpf)
100 {
101 if (!ifoff_enabled)
102 return (0);
103
104 if (ifoff_lo_enabled && if_gettype(ifp) == IFT_LOOP)
105 return (0);
106
107 if (ifoff_other_enabled && if_gettype(ifp) != IFT_LOOP)
108 return (0);
109
110 if (viabpf && ifoff_bpfrecv_enabled)
111 return (0);
112
113 return (EPERM);
114 }
115
116 /*
117 * Object-specific entry point implementations are sorted alphabetically by
118 * object type and then by operation.
119 */
120 static int
ifoff_bpfdesc_check_receive(struct bpf_d * d,struct label * dlabel,struct ifnet * ifp,struct label * ifplabel)121 ifoff_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel,
122 struct ifnet *ifp, struct label *ifplabel)
123 {
124
125 return (ifnet_check_incoming(ifp, 1));
126 }
127
128 static int
ifoff_ifnet_check_transmit(struct ifnet * ifp,struct label * ifplabel,struct mbuf * m,struct label * mlabel)129 ifoff_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel,
130 struct mbuf *m, struct label *mlabel)
131 {
132
133 return (ifnet_check_outgoing(ifp));
134 }
135
136 static int
ifoff_inpcb_check_deliver(struct inpcb * inp,struct label * inplabel,struct mbuf * m,struct label * mlabel)137 ifoff_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel,
138 struct mbuf *m, struct label *mlabel)
139 {
140
141 M_ASSERTPKTHDR(m);
142 if (m->m_pkthdr.rcvif != NULL)
143 return (ifnet_check_incoming(m->m_pkthdr.rcvif, 0));
144
145 return (0);
146 }
147
148 static int
ifoff_socket_check_deliver(struct socket * so,struct label * solabel,struct mbuf * m,struct label * mlabel)149 ifoff_socket_check_deliver(struct socket *so, struct label *solabel,
150 struct mbuf *m, struct label *mlabel)
151 {
152
153 M_ASSERTPKTHDR(m);
154 if (m->m_pkthdr.rcvif != NULL)
155 return (ifnet_check_incoming(m->m_pkthdr.rcvif, 0));
156
157 return (0);
158 }
159
160 static struct mac_policy_ops ifoff_ops =
161 {
162 .mpo_bpfdesc_check_receive = ifoff_bpfdesc_check_receive,
163 .mpo_ifnet_check_transmit = ifoff_ifnet_check_transmit,
164 .mpo_inpcb_check_deliver = ifoff_inpcb_check_deliver,
165 .mpo_socket_check_deliver = ifoff_socket_check_deliver,
166 };
167
168 MAC_POLICY_SET(&ifoff_ops, mac_ifoff, "TrustedBSD MAC/ifoff",
169 MPC_LOADTIME_FLAG_UNLOADOK, NULL);
170