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 * $FreeBSD$ 39 */ 40 41 /* 42 * Developed by the TrustedBSD Project. 43 * 44 * Limit access to interfaces until they are specifically administratively 45 * enabled. Prevents protocol stack-driven packet leakage in unsafe 46 * environments. 47 */ 48 49 #include <sys/param.h> 50 #include <sys/kernel.h> 51 #include <sys/module.h> 52 #include <sys/mbuf.h> 53 #include <sys/socket.h> 54 #include <sys/sysctl.h> 55 56 #include <net/if.h> 57 #include <net/if_var.h> 58 #include <net/if_types.h> 59 #include <net/bpfdesc.h> 60 61 #include <security/mac/mac_policy.h> 62 63 SYSCTL_DECL(_security_mac); 64 65 static SYSCTL_NODE(_security_mac, OID_AUTO, ifoff, CTLFLAG_RW, 0, 66 "TrustedBSD mac_ifoff policy controls"); 67 68 static int ifoff_enabled = 1; 69 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, enabled, CTLFLAG_RW, 70 &ifoff_enabled, 0, "Enforce ifoff policy"); 71 TUNABLE_INT("security.mac.ifoff.enabled", &ifoff_enabled); 72 73 static int ifoff_lo_enabled = 1; 74 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, lo_enabled, CTLFLAG_RW, 75 &ifoff_lo_enabled, 0, "Enable loopback interfaces"); 76 TUNABLE_INT("security.mac.ifoff.lo_enabled", &ifoff_lo_enabled); 77 78 static int ifoff_other_enabled = 0; 79 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, other_enabled, CTLFLAG_RW, 80 &ifoff_other_enabled, 0, "Enable other interfaces"); 81 TUNABLE_INT("security.mac.ifoff.other_enabled", &ifoff_other_enabled); 82 83 static int ifoff_bpfrecv_enabled = 0; 84 SYSCTL_INT(_security_mac_ifoff, OID_AUTO, bpfrecv_enabled, CTLFLAG_RW, 85 &ifoff_bpfrecv_enabled, 0, "Enable BPF reception even when interface " 86 "is disabled"); 87 TUNABLE_INT("security.mac.ifoff.bpfrecv.enabled", &ifoff_bpfrecv_enabled); 88 89 static int 90 ifnet_check_outgoing(struct ifnet *ifp) 91 { 92 93 if (!ifoff_enabled) 94 return (0); 95 96 if (ifoff_lo_enabled && ifp->if_type == IFT_LOOP) 97 return (0); 98 99 if (ifoff_other_enabled && ifp->if_type != IFT_LOOP) 100 return (0); 101 102 return (EPERM); 103 } 104 105 static int 106 ifnet_check_incoming(struct ifnet *ifp, int viabpf) 107 { 108 if (!ifoff_enabled) 109 return (0); 110 111 if (ifoff_lo_enabled && ifp->if_type == IFT_LOOP) 112 return (0); 113 114 if (ifoff_other_enabled && ifp->if_type != IFT_LOOP) 115 return (0); 116 117 if (viabpf && ifoff_bpfrecv_enabled) 118 return (0); 119 120 return (EPERM); 121 } 122 123 /* 124 * Object-specific entry point implementations are sorted alphabetically by 125 * object type and then by operation. 126 */ 127 static int 128 ifoff_bpfdesc_check_receive(struct bpf_d *d, struct label *dlabel, 129 struct ifnet *ifp, struct label *ifplabel) 130 { 131 132 return (ifnet_check_incoming(ifp, 1)); 133 } 134 135 static int 136 ifoff_ifnet_check_transmit(struct ifnet *ifp, struct label *ifplabel, 137 struct mbuf *m, struct label *mlabel) 138 { 139 140 return (ifnet_check_outgoing(ifp)); 141 } 142 143 static int 144 ifoff_inpcb_check_deliver(struct inpcb *inp, struct label *inplabel, 145 struct mbuf *m, struct label *mlabel) 146 { 147 148 M_ASSERTPKTHDR(m); 149 if (m->m_pkthdr.rcvif != NULL) 150 return (ifnet_check_incoming(m->m_pkthdr.rcvif, 0)); 151 152 return (0); 153 } 154 155 static int 156 ifoff_socket_check_deliver(struct socket *so, struct label *solabel, 157 struct mbuf *m, struct label *mlabel) 158 { 159 160 M_ASSERTPKTHDR(m); 161 if (m->m_pkthdr.rcvif != NULL) 162 return (ifnet_check_incoming(m->m_pkthdr.rcvif, 0)); 163 164 return (0); 165 } 166 167 static struct mac_policy_ops ifoff_ops = 168 { 169 .mpo_bpfdesc_check_receive = ifoff_bpfdesc_check_receive, 170 .mpo_ifnet_check_transmit = ifoff_ifnet_check_transmit, 171 .mpo_inpcb_check_deliver = ifoff_inpcb_check_deliver, 172 .mpo_socket_check_deliver = ifoff_socket_check_deliver, 173 }; 174 175 MAC_POLICY_SET(&ifoff_ops, mac_ifoff, "TrustedBSD MAC/ifoff", 176 MPC_LOADTIME_FLAG_UNLOADOK, NULL); 177