mac_ifoff.c revision 165469
1/*-
2 * Copyright (c) 1999-2002 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: head/sys/security/mac_ifoff/mac_ifoff.c 165469 2006-12-22 23:34:47Z rwatson $
35 */
36
37/*
38 * Developed by the TrustedBSD Project.
39 * Limit access to interfaces until they are specifically administratively
40 * enabled.  Prevents protocol stack-driven packet leakage in unsafe
41 * environments.
42 */
43
44#include <sys/types.h>
45#include <sys/param.h>
46#include <sys/conf.h>
47#include <sys/kernel.h>
48#include <sys/mac.h>
49#include <sys/mount.h>
50#include <sys/proc.h>
51#include <sys/systm.h>
52#include <sys/sysproto.h>
53#include <sys/sysent.h>
54#include <sys/vnode.h>
55#include <sys/file.h>
56#include <sys/socket.h>
57#include <sys/socketvar.h>
58#include <sys/sysctl.h>
59
60#include <net/bpfdesc.h>
61#include <net/if.h>
62#include <net/if_types.h>
63#include <net/if_var.h>
64
65#include <vm/vm.h>
66
67#include <security/mac/mac_policy.h>
68
69SYSCTL_DECL(_security_mac);
70
71SYSCTL_NODE(_security_mac, OID_AUTO, ifoff, CTLFLAG_RW, 0,
72    "TrustedBSD mac_ifoff policy controls");
73
74static int	mac_ifoff_enabled = 1;
75SYSCTL_INT(_security_mac_ifoff, OID_AUTO, enabled, CTLFLAG_RW,
76    &mac_ifoff_enabled, 0, "Enforce ifoff policy");
77TUNABLE_INT("security.mac.ifoff.enabled", &mac_ifoff_enabled);
78
79static int	mac_ifoff_lo_enabled = 1;
80SYSCTL_INT(_security_mac_ifoff, OID_AUTO, lo_enabled, CTLFLAG_RW,
81    &mac_ifoff_lo_enabled, 0, "Enable loopback interfaces");
82TUNABLE_INT("security.mac.ifoff.lo_enabled", &mac_ifoff_lo_enabled);
83
84static int	mac_ifoff_other_enabled = 0;
85SYSCTL_INT(_security_mac_ifoff, OID_AUTO, other_enabled, CTLFLAG_RW,
86    &mac_ifoff_other_enabled, 0, "Enable other interfaces");
87TUNABLE_INT("security.mac.ifoff.other_enabled", &mac_ifoff_other_enabled);
88
89static int	mac_ifoff_bpfrecv_enabled = 0;
90SYSCTL_INT(_security_mac_ifoff, OID_AUTO, bpfrecv_enabled, CTLFLAG_RW,
91    &mac_ifoff_bpfrecv_enabled, 0, "Enable BPF reception even when interface "
92    "is disabled");
93TUNABLE_INT("security.mac.ifoff.bpfrecv.enabled", &mac_ifoff_bpfrecv_enabled);
94
95static int
96check_ifnet_outgoing(struct ifnet *ifnet)
97{
98
99	if (!mac_ifoff_enabled)
100		return (0);
101
102	if (mac_ifoff_lo_enabled && ifnet->if_type == IFT_LOOP)
103		return (0);
104
105	if (mac_ifoff_other_enabled && ifnet->if_type != IFT_LOOP)
106		return (0);
107
108	return (EPERM);
109}
110
111static int
112check_ifnet_incoming(struct ifnet *ifnet, int viabpf)
113{
114	if (!mac_ifoff_enabled)
115		return (0);
116
117	if (mac_ifoff_lo_enabled && ifnet->if_type == IFT_LOOP)
118		return (0);
119
120	if (mac_ifoff_other_enabled && ifnet->if_type != IFT_LOOP)
121		return (0);
122
123	if (viabpf && mac_ifoff_bpfrecv_enabled)
124		return (0);
125
126	return (EPERM);
127}
128
129static int
130mac_ifoff_check_bpfdesc_receive(struct bpf_d *bpf_d, struct label *bpflabel,
131    struct ifnet *ifnet, struct label *ifnetlabel)
132{
133
134	return (check_ifnet_incoming(ifnet, 1));
135}
136
137static int
138mac_ifoff_check_ifnet_transmit(struct ifnet *ifnet, struct label *ifnetlabel,
139    struct mbuf *m, struct label *mbuflabel)
140{
141
142	return (check_ifnet_outgoing(ifnet));
143}
144
145static int
146mac_ifoff_check_inpcb_deliver(struct inpcb *inp, struct label *inplabel,
147    struct mbuf *m, struct label *mlabel)
148{
149
150	M_ASSERTPKTHDR(m);
151	if (m->m_pkthdr.rcvif != NULL)
152		return (check_ifnet_incoming(m->m_pkthdr.rcvif, 0));
153
154	return (0);
155}
156
157static int
158mac_ifoff_check_socket_deliver(struct socket *so, struct label *socketlabel,
159    struct mbuf *m, struct label *mbuflabel)
160{
161
162	M_ASSERTPKTHDR(m);
163	if (m->m_pkthdr.rcvif != NULL)
164		return (check_ifnet_incoming(m->m_pkthdr.rcvif, 0));
165
166	return (0);
167}
168
169static struct mac_policy_ops mac_ifoff_ops =
170{
171	.mpo_check_bpfdesc_receive = mac_ifoff_check_bpfdesc_receive,
172	.mpo_check_ifnet_transmit = mac_ifoff_check_ifnet_transmit,
173	.mpo_check_inpcb_deliver = mac_ifoff_check_inpcb_deliver,
174	.mpo_check_socket_deliver = mac_ifoff_check_socket_deliver,
175};
176
177MAC_POLICY_SET(&mac_ifoff_ops, mac_ifoff, "TrustedBSD MAC/ifoff",
178    MPC_LOADTIME_FLAG_UNLOADOK, NULL);
179