Proper isolation of a Linux bridge

Vincent Bernat

TL;DR

When con­fig­ur­ing a Linux bridge, use the fol­low­ing com­mands to en­force isolation:

# bridge vlan del dev br0 vid 1 self
# ip link set dev br0 type bridge vlan_filtering 1

A net­work bridge (also com­monly called a “switch”) brings sev­eral Eth­er­net seg­ments to­gether. It is a com­mon el­e­ment in most in­fra­struc­tures. Linux pro­vides its own im­ple­mentation.

A typ­i­cal use of a Linux bridge is shown be­low. The hy­per­vi­sor is run­ning three vir­tual hosts. Each vir­tual host is at­tached to the br0 bridge (rep­re­sented by the hor­i­zon­tal seg­ment). The hy­per­vi­sor has two phys­i­cal net­work in­terfaces:

  • eth0 is at­tached to a pub­lic net­work pro­vid­ing var­i­ous ser­vices for the vir­tual hosts (DHCP, DNS, NTP, routers to In­ter­net, …). It is also part of the br0 bridge.
  • eth1 is at­tached to an in­fra­struc­ture net­work pro­vid­ing var­i­ous ser­vices to the hy­per­vi­sor (DNS, NTP, con­fig­u­ra­tion man­age­ment, routers to In­ter­net, …). It is not part of the br0 bridge.
Typical use of Linux bridging with virtual machines
Typical use of Linux bridging on a hypervisor with virtual hosts

The main ex­pec­ta­tion of such a setup is that while the vir­tual hosts should be able to use re­sources from the pub­lic net­work, they should not be able to ac­cess re­sources from the in­fra­struc­ture net­work (in­clud­ing re­sources hosted on the hy­per­vi­sor it­self, like a SSH server). In other words, we ex­pect to­tal iso­la­tion be­tween the green do­main and the purple one.

That’s not the case. From any vir­tual host:

# ip route add 192.168.14.3/32 dev eth0
# ping -c 3 192.168.14.3
PING 192.168.14.3 (192.168.14.3) 56(84) bytes of data.
64 bytes from 192.168.14.3: icmp_seq=1 ttl=59 time=0.644 ms
64 bytes from 192.168.14.3: icmp_seq=2 ttl=59 time=0.829 ms
64 bytes from 192.168.14.3: icmp_seq=3 ttl=59 time=0.894 ms

--- 192.168.14.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2033ms
rtt min/avg/max/mdev = 0.644/0.789/0.894/0.105 ms

Why?#

There are two main fac­tors be­hind this behavior:

  1. A bridge can ac­cept IP traf­fic. This is a use­ful fea­ture if you want Linux to act as a bridge and pro­vide some IP ser­vices to bridge users (a DHCP re­lay or a de­fault gate­way). This is usu­ally done by con­fig­ur­ing the IP ad­dress on the bridge de­vice: ip addr add 192.0.2.2/25 dev br0.
  2. An in­ter­face does not need an IP ad­dress to process in­com­ing IP traf­fic. Ad­di­tion­ally, by de­fault, Linux ac­cepts to an­swer ARP re­quests in­de­pen­dently from the in­com­ing in­terface.

Bridge processing#

Af­ter turn­ing an in­com­ing Eth­er­net frame into a socket buf­fer, the net­work driver trans­fers the buf­fer to the netif_​receive_​skb() func­tion. The fol­low­ing ac­tions are executed:

  1. eval­u­ate the XDP re­ceive hooks,
  2. copy the frame to any reg­is­tered global or per-de­vice taps (e.g. tcpdump),
  3. eval­u­ate the ingress pol­icy (con­fig­ured with tc),
  4. hand over the frame to the de­vice-spe­cific re­ceive han­dler, if any,
  5. hand over the frame to a global or de­vice-spe­cific pro­to­col han­dler (e.g. IPv4, ARP, IPv6).

For a bridged in­ter­face, the ker­nel has con­fig­ured a de­vice-spe­cific re­ceive han­dler, br_​handle_​frame(). This func­tion won’t al­low any ad­di­tional pro­cess­ing in the con­text of the in­com­ing in­ter­face, ex­cept for STP and LLDP frames, or if “brout­ing” is en­abled.1 There­fore, the pro­to­col han­dlers are never ex­e­cuted in this case.

Af­ter a few ad­di­tional checks, Linux will de­cide if the frame has to be lo­cally de­livered:

  • the en­try for the tar­get MAC in the FDB is marked for lo­cal de­livery; or
  • the tar­get MAC is a broad­cast or a mul­ti­cast address.

In this case, the frame is passed to the br_​pass_​frame_​up() func­tion. A VLAN-re­lated check is op­tion­ally per­formed. The socket buf­fer is at­tached to the bridge in­ter­face (br0) in­stead of the phys­i­cal in­ter­face (eth0). It is eval­u­ated by Net­fil­ter and sent back to netif_​receive_​skb(). It will go through the four steps a sec­ond time.

IPv4 processing#

When a de­vice doesn’t have a pro­to­col-in­de­pen­dent re­ceive han­dler, a pro­to­col-spe­cific han­dler will be used:

# cat /proc/net/ptype
Type Device      Function
0800          ip_rcv
0011          llc_rcv [llc]
0004          llc_rcv [llc]
0806          arp_rcv
86dd          ipv6_rcv

There­fore, if the Eth­er­net type of the in­com­ing frame is 0x800, the socket buf­fer is han­dled by ip_​rcv(). Among other things, the three fol­low­ing steps will happen:

  • If the frame des­ti­na­tion ad­dress is not the MAC ad­dress of the in­com­ing in­ter­face, not a mul­ti­cast one, and not a broad­cast one, the frame is dropped (“not for us”).
  • Net­fil­ter gets a chance to eval­u­ate the packet (in a PREROUTING chain).
  • The rout­ing sub­sys­tem will de­cide the des­ti­na­tion of the packet in ip_​route_​input_​slow(): is it a lo­cal packet, should it be for­warded, should it be dropped, should it be en­cap­su­lated? No­tably, the re­verse-path fil­ter­ing is done dur­ing this eval­u­a­tion in fib_​validate_​source().

Re­verse-path fil­ter­ing (also known as uRPF, or uni­cast re­verse-path for­ward­ing, RFC 3704) en­ables Linux to re­ject traf­fic on in­ter­faces which it should never have orig­i­nated: the source ad­dress is looked up in the rout­ing ta­bles and if the out­go­ing in­ter­face is dif­fer­ent from the cur­rent in­com­ing one, the packet is rejected.

ARP processing#

When the Eth­er­net type of the in­com­ing frame is 0x806, the socket buf­fer is han­dled by arp_rcv().

  • As with IPv4, if the frame is not for us, it is dropped.
  • If the in­com­ing de­vice has the NOARP flag, the frame is dropped.
  • Net­fil­ter gets a chance to eval­u­ate the packet (con­fig­u­ra­tion is done with arptables).
  • For an ARP re­quest, the val­ues of arp_​ignore and arp_​filter may trig­ger a drop of the packet.

IPv6 processing#

When the Eth­er­net type of the in­com­ing frame is 0x86dd, the socket buf­fer is han­dled by ipv6_rcv().

  • As with IPv4, if the frame is not for us, it is dropped.
  • If IPv6 is dis­abled on the in­ter­face, the packet is dropped.
  • Net­fil­ter gets a chance to eval­u­ate the packet (in a PREROUTING chain).
  • The rout­ing sub­sys­tem will de­cide the des­ti­na­tion of the packet. How­ever, un­like IPv4, there is no re­verse-path fil­tering.2

Workarounds#

There are var­i­ous meth­ods to fix the situation.

We can com­pletely ig­nore the bridged in­ter­faces: as long as they are at­tached to the bridge, they can­not process any up­per layer pro­to­col (IPv4, IPv6, ARP). There­fore, we can fo­cus on fil­ter­ing in­com­ing traf­fic from br0.

It should be noted that for IPv4, IPv6, and ARP pro­to­cols, the MAC ad­dress check can be cir­cum­vented by us­ing the broad­cast MAC address.

Protocol-independent workarounds#

The four fol­low­ing fixes will in­dis­tinctly drop IPv4, ARP, and IPv6 packets.

Using VLAN-aware bridge#

Linux 3.9 in­tro­duced the abil­ity to use VLAN fil­ter­ing on bridge ports. This can be used to pre­vent any lo­cal traffic:3

# ip link set dev br0 type bridge vlan_filtering 1
# bridge vlan del dev br0 vid 1 self
# bridge vlan show
port    vlan ids
eth0     1 PVID Egress Untagged
eth2     1 PVID Egress Untagged
eth3     1 PVID Egress Untagged
eth4     1 PVID Egress Untagged
br0     None

This is the most ef­fi­cient method since the frame is dropped di­rectly in br_​pass_​frame_up().

Using XDP receive hooks#

It’s also pos­si­ble to drop the bridged frame early af­ter it has been re-de­liv­ered to netif_​receive_​skb() by br_​pass_​frame_​up(). The XDP hooks at­tached to an in­ter­face are eval­u­ated be­fore taps, ingress pol­icy, and han­dlers. A very sim­ple xdp_​drop_​all.c pro­gram would look like this:

#include <linux/bpf.h>

int main()
{
    return XDP_DROP;
}

It can be com­piled and in­stalled with:

$ clang -O2 -target bpf -c xdp_drop_all.c -o xdp_drop_all.o
$ sudo ip link set dev br0 xdp obj xdp_drop_all.o sec .text

This re­quires Linux 4.14 and this is the sec­ond most ef­fi­cient method but a bit in­con­ve­nient as it needs a compiler.4

Using ingress policy#

The ingress pol­icy of an in­ter­face is eval­u­ated just a bit later but be­fore any han­dler. There­fore, the fol­low­ing com­mands will en­sure no lo­cal de­liv­ery happens:

# tc qdisc add dev br0 handle ffff: ingress
# tc filter add dev br0 parent ffff: matchall action drop

This is the third most ef­fi­cient method but is more con­ve­nient than the pre­vi­ous one for a very tiny ad­di­tional overhead.

Up­date (2020-01)

Since Linux 4.2, it is pos­si­ble to use Net­fil­ter to setup the ingress pol­icy with the same ef­fi­ciency. This can be done through nftables:

# nft -f - <<EOF
> table netdev firewall {
>   chain ingress {
>     type filter hook ingress device br0 priority 0; policy drop;
>     log
>   }
> }
> EOF

Using ebtables#

Just be­fore re-de­liv­er­ing the frame to netif_​receive_​skb(), Net­fil­ter gets a chance to is­sue a de­ci­sion. It’s easy to con­fig­ure it to drop the frame:

# ebtables -A INPUT --logical-in br0 -j DROP

How­ever, to the best of my knowl­edge, this part of Net­fil­ter is known to be in­ef­ficient.

Using namespaces#

Iso­la­tion can also be ob­tained by mov­ing all the bridged in­ter­faces into a ded­i­cated net­work names­pace and con­fig­ure the bridge in­side this namespace:

# ip netns add bridge0
# ip link set netns bridge0 eth0
# ip link set netns bridge0 eth2
# ip link set netns bridge0 eth3
# ip link set netns bridge0 eth4
# ip link del dev br0
# ip netns exec bridge0 brctl addbr br0
# for i in 0 2 3 4; do
>    ip netns exec bridge0 brctl addif br0 eth$i
>    ip netns exec bridge0 ip link set up dev eth$i
> done
# ip netns exec bridge0 ip link set up dev br0

The frame will still wan­der a bit in­side the IP stack, wast­ing some CPU cy­cles and in­creas­ing the pos­si­ble at­tack sur­face. But ul­ti­mately, it will be dropped.

Up­date (2023-08)

A more light­weight al­ter­na­tive is to move the bridge into a VRF with ip link set br0 master vrf0. The re­sult is sim­i­lar to the so­lu­tion with names­paces, but the con­fig­u­ra­tion is simpler.

Protocol-dependent workarounds#

Un­less you re­quire mul­ti­ple lay­ers of se­cu­rity, if one of the pre­vi­ous work­arounds is al­ready ap­plied, there is no need to ap­ply one of the pro­to­col-de­pen­dent fixes be­low. It’s still in­ter­est­ing to know them be­cause it is not un­com­mon to al­ready have them in place.

ARP#

The eas­i­est way to dis­able ARP pro­cess­ing on a bridge is to set the NOARP flag on the de­vice. The ARP packet will be dropped as the very first step of the ARP handler.

# ip link set arp off dev br0
# ip l l dev br0
8: br0: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 50:54:33:00:00:04 brd ff:ff:ff:ff:ff:ff

arptables can also drop the packet quite early:

# arptables -A INPUT -i br0 -j DROP

An­other way is to set arp_​ignore to 2 for the given in­ter­face. The ker­nel will only an­swer to ARP re­quests whose tar­get IP ad­dress is con­fig­ured on the in­com­ing in­ter­face. Since the bridge in­ter­face doesn’t have an IP ad­dress, no ARP re­quests will be answered.

# sysctl -qw net.ipv4.conf.br0.arp_ignore=2

Dis­abling ARP pro­cess­ing is not a suf­fi­cient work­around for IPv4. A user can still in­sert the ap­pro­pri­ate en­try in its neigh­bor cache:

# ip neigh replace 192.168.14.3 lladdr 50:54:33:00:00:04 dev eth0
# ping -c 1 192.168.14.3
PING 192.168.14.3 (192.168.14.3) 56(84) bytes of data.
64 bytes from 192.168.14.3: icmp_seq=1 ttl=49 time=1.30 ms

--- 192.168.14.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.309/1.309/1.309/0.000 ms

Be­cause the check on the tar­get MAC ad­dress is quite loose, they don’t even need to guess the MAC address:

# ip neigh replace 192.168.14.3 lladdr ff:ff:ff:ff:ff:ff dev eth0
# ping -c 1 192.168.14.3
PING 192.168.14.3 (192.168.14.3) 56(84) bytes of data.
64 bytes from 192.168.14.3: icmp_seq=1 ttl=49 time=1.12 ms

--- 192.168.14.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.129/1.129/1.129/0.000 ms

IPv4#

The ear­li­est place to drop an IPv4 packet is with Net­filter:5

# iptables -t raw -I PREROUTING -i br0 -j DROP

If Net­fil­ter is dis­abled, an­other pos­si­bil­ity is to en­able strict re­verse-path fil­ter­ing for the in­ter­face. In this case, since there is no IP ad­dress con­fig­ured on the in­ter­face, the packet will be dropped dur­ing the route lookup:

# sysctl -qw net.ipv4.conf.br0.rp_filter=1

An­other op­tion is the use of a ded­i­cated rout­ing rule. Com­pared to the re­verse-path fil­ter­ing op­tion, the packet will be dropped a bit ear­lier, still dur­ing the route lookup.

# ip rule add iif br0 blackhole

IPv6#

Linux pro­vides a way to com­pletely dis­able IPv6 on a given in­ter­face. The packet will be dropped as the very first step of the IPv6 handler:

# sysctl -qw net.ipv6.conf.br0.disable_ipv6=1

As with IPv4, it’s pos­si­ble to use Net­fil­ter or a ded­i­cated rout­ing rule.

About the example#

In the above ex­am­ple, the vir­tual host gets ICMP replies be­cause they are routed through the in­fra­struc­ture net­work to In­ter­net (e.g. the hy­per­vi­sor has a de­fault gate­way which also acts as a NAT router to In­ter­net). This may not be the case.

If you want to check if you are “vul­ner­a­ble” de­spite not get­ting an ICMP re­ply, look at the guest neigh­bor ta­ble to check if you got an ARP re­ply from the host:

# ip route add 192.168.14.3/32 dev eth0
# ip neigh show dev eth0
192.168.14.3 lladdr 50:54:33:00:00:04 REACHABLE

If you didn’t get a re­ply, you could still have is­sues with IP pro­cess­ing. Add a static neigh­bor en­try be­fore check­ing the next step:

# ip neigh replace 192.168.14.3 lladdr ff:ff:ff:ff:ff:ff dev eth0

To check if IP pro­cess­ing is en­abled, check the bridge host’s net­work sta­tistics:

# netstat -s | grep "ICMP messages"
    15 ICMP messages received
    15 ICMP messages sent
    0 ICMP messages failed

If the coun­ters are in­creas­ing, it is pro­cess­ing in­com­ing IP packets.

One-way com­mu­ni­ca­tion still al­lows a lot of bad things, like DoS at­tacks. Ad­di­tion­ally, if the hy­per­vi­sor hap­pens to also act as a router, the reach is ex­tended to the whole in­fra­struc­ture net­work, po­ten­tially ex­pos­ing weak de­vices (e.g. PDU) ex­pos­ing an SNMP agent. If one-way com­mu­ni­ca­tion is all that’s needed, the at­tacker can also spoof its source IP ad­dress, by­pass­ing IP-based au­then­tication.

About MACVLAN interfaces#

When source-ad­dress learn­ing is not needed, bridges can be re­placed by MACVLAN or MACVTAP in­ter­faces. Have a look at the fol­low­ing ar­ti­cle for more de­tails: “Bridge vs Macvlan.”

In this case, the packet pro­cess­ing is dif­fer­ent and asym­met­ric. When a frame ex­its the MACVLAN in­ter­face, it is di­rectly trans­mit­ted to the ap­pro­pri­ate in­ter­face (ei­ther an­other MACVLAN in­ter­face or the lower de­vice). In this di­rec­tion, com­mu­ni­ca­tion with the host is not possible.

How­ever, when a frame en­ters the lower de­vice, if the des­ti­na­tion MAC is not one of the sub-in­ter­faces, the frame is pro­cessed by the host us­ing the ap­pro­pri­ate pro­to­col han­dler (ARP, IPv4, IPv6). There­fore, iso­la­tion can only be ob­tained by us­ing the pro­to­col-de­pen­dent methods.