diff --git a/feature/conn25/datapath.go b/feature/conn25/datapath.go index 2b5174bb8..526ee5803 100644 --- a/feature/conn25/datapath.go +++ b/feature/conn25/datapath.go @@ -273,6 +273,15 @@ func (dh *datapathHandler) HandlePacketFromTunDevice(p *packet.Parsed, tun *tstu return filter.Accept } transitIP := ref.TransitIP() + if !transitIP.IsValid() || transitIP.Is4() != magicIP.Is4() { + // Defense in depth: implementations must not report ok with a Transit IP + // that is invalid or in a different family than the Magic IP it replaces. + dh.logf("[Unexpected] counted flow for magic IP %v has an unusable transit IP %v, dropping packet", + magicIP, transitIP) + ref.Release() + dh.sendICMPHostUnreachable(p, tun) + return filter.Drop + } // This is a new outbound client flow. Install a DNAT MagicIP-to-TransitIP action // for the outgoing direction, and an SNAT TransitIP-to-MagicIP action for the diff --git a/feature/conn25/datapath_test.go b/feature/conn25/datapath_test.go index 1bf184b1f..aee3cc2d7 100644 --- a/feature/conn25/datapath_test.go +++ b/feature/conn25/datapath_test.go @@ -27,15 +27,24 @@ import ( type testConn25 struct { clientPrepareNewFlowForMagicIPFn func(netip.Addr) (netip.Addr, error) connectorRealIPForTransitIPConnectionFn func(netip.Addr, netip.Addr) (netip.Addr, error) + + // countsInvalidTransitIP makes ClientPrepareNewFlowForMagicIP model an + // implementation that violates the [Conn25Datapath] contract by reporting + // ok with a zero Transit IP, so tests can exercise the datapath's guard + // against it. + countsInvalidTransitIP bool } // ClientPrepareNewFlowForMagicIP counts a flow, and so reports ok, exactly when // clientPrepareNewFlowForMagicIPFn resolves magicIP to a Transit IP. func (tc *testConn25) ClientPrepareNewFlowForMagicIP(magicIP netip.Addr) (ClientFlowRef, bool, error) { transitIP, err := tc.clientPrepareNewFlowForMagicIPFn(magicIP) - if err != nil || !transitIP.IsValid() { + if err != nil { return ClientFlowRef{}, false, err } + if !transitIP.IsValid() && !tc.countsInvalidTransitIP { + return ClientFlowRef{}, false, nil + } return ClientFlowRef{transit: transitIP}, true, nil } @@ -58,6 +67,11 @@ type testNet struct { unroutableMagicIP netip.Addr // a Magic IP whose Transit IP isn't installed in WireGuard yet transitIP netip.Addr realIP netip.Addr // the internet-facing address behind transitIP + + // otherFamilyTransitIP is the other testNet's transitIP, so that tests can + // hand the datapath a Transit IP that is valid but can't be written into a + // packet of this family. + otherFamilyTransitIP netip.Addr } var testNets = []testNet{ @@ -72,6 +86,8 @@ var testNets = []testNet{ unroutableMagicIP: netip.MustParseAddr("10.64.0.3"), transitIP: netip.MustParseAddr("169.254.0.1"), realIP: netip.MustParseAddr("240.64.0.1"), + + otherFamilyTransitIP: netip.MustParseAddr("fd7a:115c:a1e0:a99c:200::1"), }, { name: "ipv6", @@ -84,6 +100,8 @@ var testNets = []testNet{ unroutableMagicIP: netip.MustParseAddr("fd7a:115c:a1e0:a99c:100::3"), transitIP: netip.MustParseAddr("fd7a:115c:a1e0:a99c:200::1"), realIP: netip.MustParseAddr("2606:4700::6812:1a78"), + + otherFamilyTransitIP: netip.MustParseAddr("169.254.0.1"), }, } @@ -488,6 +506,56 @@ func TestClientFlowCache(t *testing.T) { } } +// TestClientFlowUnusableTransitIPDropped verifies the defensive measure against +// a flow with a Transit IP that can't be written into the packet. +func TestClientFlowUnusableTransitIPDropped(t *testing.T) { + const clientPort, serverPort = 1234, 80 + + for _, tn := range testNets { + t.Run(tn.name, func(t *testing.T) { + for _, tt := range []struct { + name string + transitIP netip.Addr + }{ + {"invalid", netip.Addr{}}, + {"wrong-family", tn.otherFamilyTransitIP}, + } { + t.Run(tt.name, func(t *testing.T) { + mock := &testConn25{countsInvalidTransitIP: true} + mock.clientPrepareNewFlowForMagicIPFn = func(netip.Addr) (netip.Addr, error) { + return tt.transitIP, nil + } + dph := newDatapathHandler(mock, t.Logf) + chtun, tun := newChannelTUN(t) + + // HandlePacketFromTunDevice blocks until the injected packet is + // read, so drain the channel TUN concurrently. + gotInboundPacketChan := make(chan []byte, 1) + go func() { gotInboundPacketChan <- <-chtun.Inbound }() + + src := netip.AddrPortFrom(tn.clientSrcIP, clientPort) + dst := netip.AddrPortFrom(tn.magicIP, serverPort) + p := tn.udpPacket(src, dst) + if got, want := dph.HandlePacketFromTunDevice(p, tun), filter.Drop; got != want { + t.Fatalf("unexpected filter response: got %v, want %v", got, want) + } + // The dropped packet itself must be left untouched, since the + // injected error quotes it. + tn.checkPacket(t, p, src, dst) + + var injected []byte + select { + case injected = <-gotInboundPacketChan: + case <-time.After(1 * time.Second): + t.Fatal("timed out waiting for injected ICMP packet") + } + tn.checkICMPUnreachable(t, injected, tn.magicIP, tn.clientSrcIP, p) + }) + } + }) + } +} + func TestConnectorFlowCache(t *testing.T) { const clientPort, serverPort = 1234, 80