1101099Srwatson/*- 2166905Srwatson * Copyright (c) 1999-2002, 2007 Robert N. M. Watson 3126097Srwatson * Copyright (c) 2001-2002 Networks Associates Technology, Inc. 4172930Srwatson * Copyright (c) 2006 SPARTA, Inc. 5101099Srwatson * All rights reserved. 6101099Srwatson * 7101099Srwatson * This software was developed by Robert Watson for the TrustedBSD Project. 8101099Srwatson * 9106393Srwatson * This software was developed for the FreeBSD Project in part by Network 10106393Srwatson * Associates Laboratories, the Security Research Division of Network 11106393Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 12106393Srwatson * as part of the DARPA CHATS research program. 13101099Srwatson * 14172930Srwatson * This software was enhanced by SPARTA ISSO under SPAWAR contract 15172930Srwatson * N66001-04-C-6019 ("SEFOS"). 16172930Srwatson * 17101099Srwatson * Redistribution and use in source and binary forms, with or without 18101099Srwatson * modification, are permitted provided that the following conditions 19101099Srwatson * are met: 20101099Srwatson * 1. Redistributions of source code must retain the above copyright 21101099Srwatson * notice, this list of conditions and the following disclaimer. 22101099Srwatson * 2. Redistributions in binary form must reproduce the above copyright 23101099Srwatson * notice, this list of conditions and the following disclaimer in the 24101099Srwatson * documentation and/or other materials provided with the distribution. 25101099Srwatson * 26101099Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27101099Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28101099Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29101099Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30101099Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31101099Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32101099Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34101099Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35101099Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36101099Srwatson * SUCH DAMAGE. 37101099Srwatson * 38101099Srwatson * $FreeBSD: releng/10.3/sys/security/mac_ifoff/mac_ifoff.c 227309 2011-11-07 15:43:11Z ed $ 39101099Srwatson */ 40101099Srwatson 41101099Srwatson/* 42101099Srwatson * Developed by the TrustedBSD Project. 43172955Srwatson * 44101099Srwatson * Limit access to interfaces until they are specifically administratively 45101099Srwatson * enabled. Prevents protocol stack-driven packet leakage in unsafe 46101099Srwatson * environments. 47101099Srwatson */ 48101099Srwatson 49101099Srwatson#include <sys/param.h> 50101099Srwatson#include <sys/kernel.h> 51166905Srwatson#include <sys/module.h> 52101099Srwatson#include <sys/socket.h> 53101099Srwatson#include <sys/sysctl.h> 54101099Srwatson 55101099Srwatson#include <net/bpfdesc.h> 56101099Srwatson#include <net/if_types.h> 57101099Srwatson 58165469Srwatson#include <security/mac/mac_policy.h> 59101099Srwatson 60101099SrwatsonSYSCTL_DECL(_security_mac); 61101099Srwatson 62227309Sedstatic SYSCTL_NODE(_security_mac, OID_AUTO, ifoff, CTLFLAG_RW, 0, 63101099Srwatson "TrustedBSD mac_ifoff policy controls"); 64101099Srwatson 65172955Srwatsonstatic int ifoff_enabled = 1; 66101099SrwatsonSYSCTL_INT(_security_mac_ifoff, OID_AUTO, enabled, CTLFLAG_RW, 67172955Srwatson &ifoff_enabled, 0, "Enforce ifoff policy"); 68172955SrwatsonTUNABLE_INT("security.mac.ifoff.enabled", &ifoff_enabled); 69101099Srwatson 70172955Srwatsonstatic int ifoff_lo_enabled = 1; 71101099SrwatsonSYSCTL_INT(_security_mac_ifoff, OID_AUTO, lo_enabled, CTLFLAG_RW, 72172955Srwatson &ifoff_lo_enabled, 0, "Enable loopback interfaces"); 73172955SrwatsonTUNABLE_INT("security.mac.ifoff.lo_enabled", &ifoff_lo_enabled); 74101099Srwatson 75172955Srwatsonstatic int ifoff_other_enabled = 0; 76101099SrwatsonSYSCTL_INT(_security_mac_ifoff, OID_AUTO, other_enabled, CTLFLAG_RW, 77172955Srwatson &ifoff_other_enabled, 0, "Enable other interfaces"); 78172955SrwatsonTUNABLE_INT("security.mac.ifoff.other_enabled", &ifoff_other_enabled); 79101099Srwatson 80172955Srwatsonstatic int ifoff_bpfrecv_enabled = 0; 81101099SrwatsonSYSCTL_INT(_security_mac_ifoff, OID_AUTO, bpfrecv_enabled, CTLFLAG_RW, 82172955Srwatson &ifoff_bpfrecv_enabled, 0, "Enable BPF reception even when interface " 83101099Srwatson "is disabled"); 84172955SrwatsonTUNABLE_INT("security.mac.ifoff.bpfrecv.enabled", &ifoff_bpfrecv_enabled); 85101099Srwatson 86101099Srwatsonstatic int 87172930Srwatsonifnet_check_outgoing(struct ifnet *ifp) 88101099Srwatson{ 89101099Srwatson 90172955Srwatson if (!ifoff_enabled) 91101099Srwatson return (0); 92101099Srwatson 93172955Srwatson if (ifoff_lo_enabled && ifp->if_type == IFT_LOOP) 94101099Srwatson return (0); 95101099Srwatson 96172955Srwatson if (ifoff_other_enabled && ifp->if_type != IFT_LOOP) 97101099Srwatson return (0); 98101099Srwatson 99101099Srwatson return (EPERM); 100101099Srwatson} 101101099Srwatson 102101099Srwatsonstatic int 103172930Srwatsonifnet_check_incoming(struct ifnet *ifp, int viabpf) 104101099Srwatson{ 105172955Srwatson if (!ifoff_enabled) 106101099Srwatson return (0); 107101099Srwatson 108172955Srwatson if (ifoff_lo_enabled && ifp->if_type == IFT_LOOP) 109101099Srwatson return (0); 110101099Srwatson 111172955Srwatson if (ifoff_other_enabled && ifp->if_type != IFT_LOOP) 112101099Srwatson return (0); 113101099Srwatson 114172955Srwatson if (viabpf && ifoff_bpfrecv_enabled) 115101099Srwatson return (0); 116101099Srwatson 117101099Srwatson return (EPERM); 118101099Srwatson} 119101099Srwatson 120173138Srwatson/* 121173138Srwatson * Object-specific entry point implementations are sorted alphabetically by 122173138Srwatson * object type and then by operation. 123173138Srwatson */ 124101099Srwatsonstatic int 125172955Srwatsonifoff_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel, 126168976Srwatson struct ifnet *ifp, struct label *ifplabel) 127101099Srwatson{ 128101099Srwatson 129172930Srwatson return (ifnet_check_incoming(ifp, 1)); 130101099Srwatson} 131101099Srwatson 132101099Srwatsonstatic int 133172955Srwatsonifoff_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel, 134168976Srwatson struct mbuf *m, struct label *mlabel) 135101099Srwatson{ 136101099Srwatson 137172930Srwatson return (ifnet_check_outgoing(ifp)); 138101099Srwatson} 139101099Srwatson 140101099Srwatsonstatic int 141172955Srwatsonifoff_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel, 142122875Srwatson struct mbuf *m, struct label *mlabel) 143122875Srwatson{ 144122875Srwatson 145122875Srwatson M_ASSERTPKTHDR(m); 146122875Srwatson if (m->m_pkthdr.rcvif != NULL) 147172930Srwatson return (ifnet_check_incoming(m->m_pkthdr.rcvif, 0)); 148122875Srwatson 149122875Srwatson return (0); 150122875Srwatson} 151122875Srwatson 152122875Srwatsonstatic int 153172955Srwatsonifoff_socket_check_deliver(struct socket *so, struct label *solabel, 154168976Srwatson struct mbuf *m, struct label *mlabel) 155101099Srwatson{ 156101099Srwatson 157113687Srwatson M_ASSERTPKTHDR(m); 158113687Srwatson if (m->m_pkthdr.rcvif != NULL) 159172930Srwatson return (ifnet_check_incoming(m->m_pkthdr.rcvif, 0)); 160101099Srwatson 161101099Srwatson return (0); 162101099Srwatson} 163101099Srwatson 164172955Srwatsonstatic struct mac_policy_ops ifoff_ops = 165101099Srwatson{ 166172955Srwatson .mpo_bpfdesc_check_receive = ifoff_bpfdesc_check_receive, 167172955Srwatson .mpo_ifnet_check_transmit = ifoff_ifnet_check_transmit, 168172955Srwatson .mpo_inpcb_check_deliver = ifoff_inpcb_check_deliver, 169172955Srwatson .mpo_socket_check_deliver = ifoff_socket_check_deliver, 170101099Srwatson}; 171101099Srwatson 172172955SrwatsonMAC_POLICY_SET(&ifoff_ops, mac_ifoff, "TrustedBSD MAC/ifoff", 173187016Srwatson MPC_LOADTIME_FLAG_UNLOADOK, NULL); 174