From 8305be139b6aea7df3207e65b268d7595de29212 Mon Sep 17 00:00:00 2001 From: Chet Burgess Date: Tue, 4 Aug 2015 13:10:04 -0700 Subject: [PATCH 1/1] Add ARP spoofing protection for LinuxBridge agent This is a backport for the fix that went into master to address this bug. This patch adds ARP spoofing protection for the Linux Bridge agent based on ebtables. This code was written to be minimally invasive with the intent of back-porting to Kilo. The protection is enabled and disabled with the same 'prevent_arp_spoofing' agent config flag added for the OVS agent in I7c079b779245a0af6bc793564fa8a560e4226afe. The protection works by setting up an ebtables chain for each port and jumping all ARP traffic to that chain. The port-specific chains have a default DROP policy and then have allow rules installed that only allow ARP traffic with a source CIDR that matches one of the port's fixed IPs or an allowed address pair. Closes-Bug: #1274034 --- neutron/plugins/linuxbridge/agent/arp_protect.py | 130 ++++++++++ .../linuxbridge/agent/linuxbridge_neutron_agent.py | 13 + neutron/plugins/linuxbridge/common/config.py | 10 + neutron/plugins/ml2/rpc.py | 3 + neutron/tests/unit/linuxbridge/test_arp_protect.py | 279 +++++++++++++++++++++ .../unit/linuxbridge/test_lb_neutron_agent.py | 1 + 6 files changed, 436 insertions(+) create mode 100644 neutron/plugins/linuxbridge/agent/arp_protect.py create mode 100644 neutron/tests/unit/linuxbridge/test_arp_protect.py diff --git a/neutron/plugins/linuxbridge/agent/arp_protect.py b/neutron/plugins/linuxbridge/agent/arp_protect.py new file mode 100644 index 0000000..ce728e1 --- /dev/null +++ b/neutron/plugins/linuxbridge/agent/arp_protect.py @@ -0,0 +1,130 @@ +# Copyright (c) 2015 Mirantis, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import netaddr + +from oslo.config import cfg + +from neutron.agent.linux import ip_lib +from neutron.openstack.common import lockutils +from neutron.openstack.common import log as logging + +LOG = logging.getLogger(__name__) +SPOOF_CHAIN_PREFIX = 'neutronARP-' + + +def setup_arp_spoofing_protection(vif, port_details): + current_rules = ebtables(['-L']).splitlines() + if not port_details.get('port_security_enabled', True): + # clear any previous entries related to this port + delete_arp_spoofing_protection([vif], current_rules) + LOG.info(_("Skipping ARP spoofing rules for port '%s' because " + "it has port security disabled"), vif) + return + # collect all of the addresses and cidrs that belong to the port + addresses = {f['ip_address'] for f in port_details['fixed_ips']} + if port_details.get('allowed_address_pairs'): + addresses |= {p['ip_address'] + for p in port_details['allowed_address_pairs']} + + addresses = {ip for ip in addresses + if netaddr.IPNetwork(ip).version == 4} + if any(netaddr.IPNetwork(ip).prefixlen == 0 for ip in addresses): + # don't try to install protection because a /0 prefix allows any + # address anyway and the ARP_SPA can only match on /1 or more. + return + + install_arp_spoofing_protection(vif, addresses, current_rules) + + +def chain_name(vif): + # start each chain with a common identifer for cleanup to find + return '%s%s' % (SPOOF_CHAIN_PREFIX, vif) + + +@lockutils.synchronized('ebtables') +def delete_arp_spoofing_protection(vifs, current_rules=None): + if not current_rules: + current_rules = ebtables(['-L']).splitlines() + # delete the jump rule and then delete the whole chain + jumps = [vif for vif in vifs if vif_jump_present(vif, current_rules)] + for vif in jumps: + ebtables(['-D', 'FORWARD', '-i', vif, '-j', + chain_name(vif), '-p', 'ARP']) + for vif in vifs: + if chain_exists(chain_name(vif), current_rules): + ebtables(['-X', chain_name(vif)]) + + +def delete_unreferenced_arp_protection(current_vifs): + # deletes all jump rules and chains that aren't in current_vifs but match + # the spoof prefix + output = ebtables(['-L']).splitlines() + to_delete = [] + for line in output: + # we're looking to find and turn the following: + # Bridge chain: SPOOF_CHAIN_PREFIXtap199, entries: 0, policy: DROP + # into 'tap199' + if line.startswith('Bridge chain: %s' % SPOOF_CHAIN_PREFIX): + devname = line.split(SPOOF_CHAIN_PREFIX, 1)[1].split(',')[0] + if devname not in current_vifs: + to_delete.append(devname) + LOG.info(_("Clearing orphaned ARP spoofing entries for devices %s"), + to_delete) + delete_arp_spoofing_protection(to_delete, output) + + +@lockutils.synchronized('ebtables') +def install_arp_spoofing_protection(vif, addresses, current_rules): + # make a VIF-specific ARP chain so we don't conflict with other rules + vif_chain = chain_name(vif) + if not chain_exists(vif_chain, current_rules): + ebtables(['-N', vif_chain, '-P', 'DROP']) + # flush the chain to clear previous accepts. this will cause dropped ARP + # packets until the allows are installed, but that's better than leaked + # spoofed packets and ARP can handle losses. + ebtables(['-F', vif_chain]) + for addr in addresses: + ebtables(['-A', vif_chain, '-p', 'ARP', '--arp-ip-src', addr, + '-j', 'ACCEPT']) + # check if jump rule already exists, if not, install it + if not vif_jump_present(vif, current_rules): + ebtables(['-A', 'FORWARD', '-i', vif, '-j', + vif_chain, '-p', 'ARP']) + + +def chain_exists(chain, current_rules): + for rule in current_rules: + if rule.startswith('Bridge chain: %s' % chain): + return True + return False + + +def vif_jump_present(vif, current_rules): + searches = (('-i %s' % vif), ('-j %s' % chain_name(vif)), ('-p ARP')) + for line in current_rules: + if all(s in line for s in searches): + return True + return False + + +# Used to scope ebtables commands in testing +NAMESPACE = None + + +def ebtables(comm): + execute = ip_lib.IPWrapper(root_helper=cfg.CONF.AGENT.root_helper, + namespace=NAMESPACE).netns.execute + return execute(['ebtables'] + comm) diff --git a/neutron/plugins/linuxbridge/agent/linuxbridge_neutron_agent.py b/neutron/plugins/linuxbridge/agent/linuxbridge_neutron_agent.py index aa3788e..6b2a482 100755 --- a/neutron/plugins/linuxbridge/agent/linuxbridge_neutron_agent.py +++ b/neutron/plugins/linuxbridge/agent/linuxbridge_neutron_agent.py @@ -44,6 +44,7 @@ from neutron import context from neutron.openstack.common import log as logging from neutron.openstack.common import loopingcall from neutron.plugins.common import constants as p_const +from neutron.plugins.linuxbridge.agent import arp_protect from neutron.plugins.linuxbridge.common import config # noqa from neutron.plugins.linuxbridge.common import constants as lconst @@ -767,6 +768,7 @@ class LinuxBridgeNeutronAgentRPC(sg_rpc.SecurityGroupAgentRpcMixin): root_helper): self.polling_interval = polling_interval self.root_helper = root_helper + self.prevent_arp_spoofing = cfg.CONF.AGENT.prevent_arp_spoofing self.setup_linux_bridge(interface_mappings) configurations = {'interface_mappings': interface_mappings} if self.br_mgr.vxlan_mode != lconst.VXLAN_NONE: @@ -881,6 +883,11 @@ class LinuxBridgeNeutronAgentRPC(sg_rpc.SecurityGroupAgentRpcMixin): if 'port_id' in device_details: LOG.info(_("Port %(device)s updated. Details: %(details)s"), {'device': device, 'details': device_details}) + if self.prevent_arp_spoofing: + port = self.br_mgr.get_tap_device_name( + device_details['port_id']) + arp_protect.setup_arp_spoofing_protection(port, + device_details) if device_details['admin_state_up']: # create the networking for the port network_type = device_details.get('network_type') @@ -935,6 +942,8 @@ class LinuxBridgeNeutronAgentRPC(sg_rpc.SecurityGroupAgentRpcMixin): else: LOG.debug(_("Device %s not defined on plugin"), device) self.br_mgr.remove_empty_bridges() + if self.prevent_arp_spoofing: + arp_protect.delete_arp_spoofing_protection(devices) return resync def scan_devices(self, previous, sync): @@ -955,6 +964,10 @@ class LinuxBridgeNeutronAgentRPC(sg_rpc.SecurityGroupAgentRpcMixin): 'current': set(), 'updated': set(), 'removed': set()} + # clear any orphaned ARP spoofing rules (e.g. interface was + # manually deleted) + if self.prevent_arp_spoofing: + arp_protect.delete_unreferenced_arp_protection(current_devices) if sync: # This is the first iteration, or the previous one had a problem. diff --git a/neutron/plugins/linuxbridge/common/config.py b/neutron/plugins/linuxbridge/common/config.py index a8060a9..b773c60 100644 --- a/neutron/plugins/linuxbridge/common/config.py +++ b/neutron/plugins/linuxbridge/common/config.py @@ -62,6 +62,16 @@ agent_opts = [ "polling for local device changes.")), cfg.BoolOpt('rpc_support_old_agents', default=False, help=_("Enable server RPC compatibility with old agents")), + cfg.BoolOpt('prevent_arp_spoofing', default=True, + help=_("Enable suppression of ARP responses that don't match " + "an IP address that belongs to the port from which " + "they originate. Note: This prevents the VMs attached " + "to this agent from spoofing, it doesn't protect them " + "from other devices which have the capability to spoof " + "(e.g. bare metal or VMs attached to agents without " + "this flag set to True). Spoofing rules will not be " + "added to any ports that have port security disabled. " + "For LinuxBridge, this requires ebtables.")), ] diff --git a/neutron/plugins/ml2/rpc.py b/neutron/plugins/ml2/rpc.py index 09a970d..2fba622 100644 --- a/neutron/plugins/ml2/rpc.py +++ b/neutron/plugins/ml2/rpc.py @@ -23,6 +23,7 @@ from neutron.common import rpc as n_rpc from neutron.common import topics from neutron.common import utils from neutron.extensions import portbindings +from neutron.extensions import portsecurity as psec from neutron import manager from neutron.openstack.common import log from neutron.plugins.common import constants as service_constants @@ -102,6 +103,8 @@ class RpcCallbacks(n_rpc.RpcCallback, 'physical_network': segment[api.PHYSICAL_NETWORK], 'fixed_ips': port['fixed_ips'], 'device_owner': port['device_owner'], + 'allowed_address_pairs': port['allowed_address_pairs'], + 'port_security_enabled': port.get(psec.PORTSECURITY, True), 'profile': port[portbindings.PROFILE]} LOG.debug(_("Returning: %s"), entry) return entry diff --git a/neutron/tests/unit/linuxbridge/test_arp_protect.py b/neutron/tests/unit/linuxbridge/test_arp_protect.py new file mode 100644 index 0000000..516dc9d --- /dev/null +++ b/neutron/tests/unit/linuxbridge/test_arp_protect.py @@ -0,0 +1,279 @@ +# Copyright (c) 2015 Cisco Systems, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import contextlib +import copy +import mock + +from neutron.plugins.linuxbridge.agent import arp_protect +from neutron.tests import base + + +class ArpProtectTestCase(base.BaseTestCase): + + VIF = 'tap3fc5bc14-b1' + FIXED_IP = '1.2.3.4' + ALLOWED_ADDRESS = '5.6.7.8' + CHAIN_NAME = arp_protect.chain_name(VIF) + + EBTABLES_EMPTY_SAMPLE = [ + 'Bridge table: filter', + '', + 'Bridge chain: INPUT, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: FORWARD, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: OUTPUT, entries: 0, policy: ACCEPT', + ] + + EBTABLES_LOADED_SAMPLE = [ + 'Bridge table: filter', + '', + 'Bridge chain: INPUT, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: FORWARD, entries: 2, policy: ACCEPT', + '-p ARP -i %s -j %s' % (VIF, CHAIN_NAME), + '', + 'Bridge chain: OUTPUT, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: %s, entries: 1, policy: DROP' % (CHAIN_NAME), + '-p ARP --arp-ip-src %s -j ACCEPT' % (FIXED_IP), + ] + PORT_DETAILS_SAMPLE = { + 'port_security_enabled': True, + 'fixed_ips': [{ + 'subnet_id': '12345', + 'ip_address': FIXED_IP, + }], + 'allowed_address_pairs': [], + } + + def _do_test_setup_arp_spoofing(self, vif, port_details): + with contextlib.nested( + mock.patch.object( + arp_protect, 'ebtables', + return_value='\n'.join(self.EBTABLES_EMPTY_SAMPLE) + ) + ) as ebtables_fn: # noqa + arp_protect.setup_arp_spoofing_protection(vif, port_details) + + def test_setup_arp_spoofing(self): + port_details = copy.deepcopy(self.PORT_DETAILS_SAMPLE) + with contextlib.nested( + mock.patch.object(arp_protect, 'install_arp_spoofing_protection'), + mock.patch.object(arp_protect, 'delete_arp_spoofing_protection'), + ) as (install_fn, delete_fn): + self._do_test_setup_arp_spoofing(self.VIF, port_details) + self.assertFalse(delete_fn.called) + install_fn.assert_called_once_with(self.VIF, + ({self.FIXED_IP}), + self.EBTABLES_EMPTY_SAMPLE) + + def test_setup_arp_spoofing_with_allowed_address_pairs(self): + port_details = copy.deepcopy(self.PORT_DETAILS_SAMPLE) + port_details['allowed_address_pairs'] = [{ + 'mac_address': 'aa:bb:cc:dd:ee:ff', + 'ip_address': '5.6.7.8', + }] + with contextlib.nested( + mock.patch.object(arp_protect, 'install_arp_spoofing_protection'), + mock.patch.object(arp_protect, 'delete_arp_spoofing_protection'), + ) as (install_fn, delete_fn): + self._do_test_setup_arp_spoofing(self.VIF, port_details) + self.assertFalse(delete_fn.called) + install_fn.assert_called_once_with(self.VIF, + ({self.FIXED_IP, + self.ALLOWED_ADDRESS}), + self.EBTABLES_EMPTY_SAMPLE) + + def test_setup_arp_spoofing_no_pse(self): + port_details = copy.deepcopy(self.PORT_DETAILS_SAMPLE) + port_details['port_security_enabled'] = False + with contextlib.nested( + mock.patch.object(arp_protect, 'install_arp_spoofing_protection'), + mock.patch.object(arp_protect, 'delete_arp_spoofing_protection'), + ) as (install_fn, delete_fn): + self._do_test_setup_arp_spoofing(self.VIF, port_details) + delete_fn.assert_called_once_with([self.VIF], + self.EBTABLES_EMPTY_SAMPLE) + self.assertFalse(install_fn.called) + + def test_setup_arp_spoofing_zero_length_prefix(self): + port_details = copy.deepcopy(self.PORT_DETAILS_SAMPLE) + port_details['allowed_address_pairs'] = [{ + 'mac_address': 'aa:bb:cc:dd:ee:ff', + 'ip_address': '0.0.0.0/0', + }] + with contextlib.nested( + mock.patch.object(arp_protect, 'install_arp_spoofing_protection'), + mock.patch.object(arp_protect, 'delete_arp_spoofing_protection'), + ) as (install_fn, delete_fn): + self._do_test_setup_arp_spoofing(self.VIF, port_details) + self.assertFalse(delete_fn.called) + self.assertFalse(install_fn.called) + + def test_chain_name(self): + name = '%s%s' % (arp_protect.SPOOF_CHAIN_PREFIX, self.VIF) + self.assertEqual(name, arp_protect.chain_name(self.VIF)) + + def test_delete_arp_spoofing(self): + # Note(cfb): We don't call this with contextlib.nested() because + # arp_protect.delete_arp_spoofing_protection() has a decorator + # which is a non-nested context manager and they don't play nice + # together with mock at all. + ebtables_p = mock.patch.object(arp_protect, 'ebtables') + ebtables = ebtables_p.start() + + arp_protect.delete_arp_spoofing_protection( + [self.VIF], current_rules=self.EBTABLES_LOADED_SAMPLE) + expected = [ + mock.call(['-D', 'FORWARD', '-i', self.VIF, '-j', + self.CHAIN_NAME, '-p', 'ARP']), + mock.call(['-X', self.CHAIN_NAME]), + ] + ebtables.assert_has_calls(expected) + + def test_delete_unreferenced_arp(self): + with contextlib.nested( + mock.patch.object( + arp_protect, 'ebtables', + return_value='\n'.join(self.EBTABLES_LOADED_SAMPLE)), + mock.patch.object(arp_protect, 'delete_arp_spoofing_protection'), + ) as (ebtables_fn, delete_fn): + arp_protect.delete_unreferenced_arp_protection([]) + delete_fn.assert_called_once_with([self.VIF], + self.EBTABLES_LOADED_SAMPLE) + + def test_install_arp_spoofing_single_ip(self): + # Note(cfb): We don't call this with contextlib.nested() because + # arp_protect.install.arp_spoofing_protection() has a decorator + # which is a non-nested context manager and they don't play nice + # together with mock at all. + ebtables_p = mock.patch.object(arp_protect, 'ebtables') + ebtables = ebtables_p.start() + + arp_protect.install_arp_spoofing_protection( + self.VIF, [self.FIXED_IP], self.EBTABLES_EMPTY_SAMPLE) + expected = [ + mock.call(['-N', self.CHAIN_NAME, '-P', 'DROP']), + mock.call(['-F', self.CHAIN_NAME]), + mock.call(['-A', self.CHAIN_NAME, '-p', 'ARP', + '--arp-ip-src', self.FIXED_IP, '-j', 'ACCEPT']), + mock.call(['-A', 'FORWARD', '-i', self.VIF, '-j', + self.CHAIN_NAME, '-p', 'ARP']), + ] + ebtables.assert_has_calls(expected) + + def test_install_arp_spoofing_multiple_ip(self): + # Note(cfb): We don't call this with contextlib.nested() because + # arp_protect.install.arp_spoofing_protection() has a decorator + # which is a non-nested context manager and they don't play nice + # together with mock at all. + ebtables_p = mock.patch.object(arp_protect, 'ebtables') + ebtables = ebtables_p.start() + + arp_protect.install_arp_spoofing_protection( + self.VIF, [self.FIXED_IP, self.ALLOWED_ADDRESS], + self.EBTABLES_EMPTY_SAMPLE) + expected = [ + mock.call(['-N', self.CHAIN_NAME, '-P', 'DROP']), + mock.call(['-F', self.CHAIN_NAME]), + mock.call(['-A', self.CHAIN_NAME, '-p', 'ARP', + '--arp-ip-src', self.FIXED_IP, '-j', 'ACCEPT']), + mock.call(['-A', self.CHAIN_NAME, '-p', 'ARP', + '--arp-ip-src', self.ALLOWED_ADDRESS, '-j', 'ACCEPT']), + mock.call(['-A', 'FORWARD', '-i', self.VIF, '-j', + self.CHAIN_NAME, '-p', 'ARP']), + ] + ebtables.assert_has_calls(expected) + + def test_install_arp_spoofing_existing_chain(self): + # Note(cfb): We don't call this with contextlib.nested() because + # arp_protect.install.arp_spoofing_protection() has a decorator + # which is a non-nested context manager and they don't play nice + # together with mock at all. + ebtables_p = mock.patch.object(arp_protect, 'ebtables') + ebtables = ebtables_p.start() + + current_rules = [ + 'Bridge table: filter', + '', + 'Bridge chain: INPUT, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: FORWARD, entries: 2, policy: ACCEPT', + '', + 'Bridge chain: OUTPUT, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: %s, entries: 1, policy: DROP' % (self.CHAIN_NAME), + ] + + arp_protect.install_arp_spoofing_protection(self.VIF, + [self.FIXED_IP], + current_rules) + expected = [ + mock.call(['-F', self.CHAIN_NAME]), + mock.call(['-A', self.CHAIN_NAME, '-p', 'ARP', + '--arp-ip-src', self.FIXED_IP, '-j', 'ACCEPT']), + mock.call(['-A', 'FORWARD', '-i', self.VIF, '-j', + self.CHAIN_NAME, '-p', 'ARP']), + ] + ebtables.assert_has_calls(expected) + + def test_install_arp_spoofing_existing_jump(self): + # Note(cfb): We don't call this with contextlib.nested() because + # arp_protect.install.arp_spoofing_protection() has a decorator + # which is a non-nested context manager and they don't play nice + # together with mock at all. + ebtables_p = mock.patch.object(arp_protect, 'ebtables') + ebtables = ebtables_p.start() + + current_rules = [ + 'Bridge table: filter', + '', + 'Bridge chain: INPUT, entries: 0, policy: ACCEPT', + '', + 'Bridge chain: FORWARD, entries: 2, policy: ACCEPT', + '-p ARP -i %s -j %s' % (self.VIF, self.CHAIN_NAME), + '', + 'Bridge chain: OUTPUT, entries: 0, policy: ACCEPT', + '', + ] + + arp_protect.install_arp_spoofing_protection(self.VIF, + [self.FIXED_IP], + current_rules) + expected = [ + mock.call(['-N', self.CHAIN_NAME, '-P', 'DROP']), + mock.call(['-F', self.CHAIN_NAME]), + mock.call(['-A', self.CHAIN_NAME, '-p', 'ARP', + '--arp-ip-src', self.FIXED_IP, '-j', 'ACCEPT']), + ] + ebtables.assert_has_calls(expected) + + def test_chain_exists(self): + self.assertTrue(arp_protect.chain_exists(self.CHAIN_NAME, + self.EBTABLES_LOADED_SAMPLE)) + + def test_chain_does_not_exist(self): + self.assertFalse(arp_protect.chain_exists('foobarbaz', + self.EBTABLES_LOADED_SAMPLE)) + + def test_vif_jump_present(self): + self.assertTrue(arp_protect.vif_jump_present( + self.VIF, self.EBTABLES_LOADED_SAMPLE)) + + def test_vif_jump_not_present(self): + self.assertFalse(arp_protect.vif_jump_present( + 'foobarbaz', self.EBTABLES_LOADED_SAMPLE)) diff --git a/neutron/tests/unit/linuxbridge/test_lb_neutron_agent.py b/neutron/tests/unit/linuxbridge/test_lb_neutron_agent.py index 579742f..80a9d66 100644 --- a/neutron/tests/unit/linuxbridge/test_lb_neutron_agent.py +++ b/neutron/tests/unit/linuxbridge/test_lb_neutron_agent.py @@ -95,6 +95,7 @@ class TestLinuxBridgeAgent(base.BaseTestCase): super(TestLinuxBridgeAgent, self).setUp() # disable setting up periodic state reporting cfg.CONF.set_override('report_interval', 0, 'AGENT') + cfg.CONF.set_override('prevent_arp_spoofing', False, 'AGENT') cfg.CONF.set_default('firewall_driver', 'neutron.agent.firewall.NoopFirewallDriver', group='SECURITYGROUP') -- 2.3.2 (Apple Git-55)