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