mac_ifoff.c revision 166531
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 166531 2007-02-06 10:59:23Z 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/mount.h> 49#include <sys/proc.h> 50#include <sys/systm.h> 51#include <sys/sysproto.h> 52#include <sys/sysent.h> 53#include <sys/vnode.h> 54#include <sys/file.h> 55#include <sys/socket.h> 56#include <sys/socketvar.h> 57#include <sys/sysctl.h> 58 59#include <net/bpfdesc.h> 60#include <net/if.h> 61#include <net/if_types.h> 62#include <net/if_var.h> 63 64#include <vm/vm.h> 65 66#include <security/mac/mac_policy.h> 67 68SYSCTL_DECL(_security_mac); 69 70SYSCTL_NODE(_security_mac, OID_AUTO, ifoff, CTLFLAG_RW, 0, 71 "TrustedBSD mac_ifoff policy controls"); 72 73static int mac_ifoff_enabled = 1; 74SYSCTL_INT(_security_mac_ifoff, OID_AUTO, enabled, CTLFLAG_RW, 75 &mac_ifoff_enabled, 0, "Enforce ifoff policy"); 76TUNABLE_INT("security.mac.ifoff.enabled", &mac_ifoff_enabled); 77 78static int mac_ifoff_lo_enabled = 1; 79SYSCTL_INT(_security_mac_ifoff, OID_AUTO, lo_enabled, CTLFLAG_RW, 80 &mac_ifoff_lo_enabled, 0, "Enable loopback interfaces"); 81TUNABLE_INT("security.mac.ifoff.lo_enabled", &mac_ifoff_lo_enabled); 82 83static int mac_ifoff_other_enabled = 0; 84SYSCTL_INT(_security_mac_ifoff, OID_AUTO, other_enabled, CTLFLAG_RW, 85 &mac_ifoff_other_enabled, 0, "Enable other interfaces"); 86TUNABLE_INT("security.mac.ifoff.other_enabled", &mac_ifoff_other_enabled); 87 88static int mac_ifoff_bpfrecv_enabled = 0; 89SYSCTL_INT(_security_mac_ifoff, OID_AUTO, bpfrecv_enabled, CTLFLAG_RW, 90 &mac_ifoff_bpfrecv_enabled, 0, "Enable BPF reception even when interface " 91 "is disabled"); 92TUNABLE_INT("security.mac.ifoff.bpfrecv.enabled", &mac_ifoff_bpfrecv_enabled); 93 94static int 95check_ifnet_outgoing(struct ifnet *ifnet) 96{ 97 98 if (!mac_ifoff_enabled) 99 return (0); 100 101 if (mac_ifoff_lo_enabled && ifnet->if_type == IFT_LOOP) 102 return (0); 103 104 if (mac_ifoff_other_enabled && ifnet->if_type != IFT_LOOP) 105 return (0); 106 107 return (EPERM); 108} 109 110static int 111check_ifnet_incoming(struct ifnet *ifnet, int viabpf) 112{ 113 if (!mac_ifoff_enabled) 114 return (0); 115 116 if (mac_ifoff_lo_enabled && ifnet->if_type == IFT_LOOP) 117 return (0); 118 119 if (mac_ifoff_other_enabled && ifnet->if_type != IFT_LOOP) 120 return (0); 121 122 if (viabpf && mac_ifoff_bpfrecv_enabled) 123 return (0); 124 125 return (EPERM); 126} 127 128static int 129mac_ifoff_check_bpfdesc_receive(struct bpf_d *bpf_d, struct label *bpflabel, 130 struct ifnet *ifnet, struct label *ifnetlabel) 131{ 132 133 return (check_ifnet_incoming(ifnet, 1)); 134} 135 136static int 137mac_ifoff_check_ifnet_transmit(struct ifnet *ifnet, struct label *ifnetlabel, 138 struct mbuf *m, struct label *mbuflabel) 139{ 140 141 return (check_ifnet_outgoing(ifnet)); 142} 143 144static int 145mac_ifoff_check_inpcb_deliver(struct inpcb *inp, struct label *inplabel, 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 156static int 157mac_ifoff_check_socket_deliver(struct socket *so, struct label *socketlabel, 158 struct mbuf *m, struct label *mbuflabel) 159{ 160 161 M_ASSERTPKTHDR(m); 162 if (m->m_pkthdr.rcvif != NULL) 163 return (check_ifnet_incoming(m->m_pkthdr.rcvif, 0)); 164 165 return (0); 166} 167 168static struct mac_policy_ops mac_ifoff_ops = 169{ 170 .mpo_check_bpfdesc_receive = mac_ifoff_check_bpfdesc_receive, 171 .mpo_check_ifnet_transmit = mac_ifoff_check_ifnet_transmit, 172 .mpo_check_inpcb_deliver = mac_ifoff_check_inpcb_deliver, 173 .mpo_check_socket_deliver = mac_ifoff_check_socket_deliver, 174}; 175 176MAC_POLICY_SET(&mac_ifoff_ops, mac_ifoff, "TrustedBSD MAC/ifoff", 177 MPC_LOADTIME_FLAG_UNLOADOK, NULL); 178