From 2e8f381c2e1872b8b59e4b46cbd84786f3ecc689 Mon Sep 17 00:00:00 2001 From: Mazdak Nasab Date: Thu, 19 Nov 2026 15:11:35 -0700 Subject: [PATCH] feature/conn25: guard against an unusable Transit IP (#22513) ClientPrepareNewFlowForMagicIP folded resolving a Magic IP and counting the flow into one call, and the datapath dropped its own IsValid check on the resolved Transit IP in favor of trusting the new ok return. That left the validity requirement stated only in the Conn25Datapath doc comment, and an implementation that violates it panics the packet path two different ways. A zero Transit IP is neither Is4 nor Is6, so it slips past both of checksum.UpdateDstAddr's family-mismatch panics and reaches As4, which panics in turn; a valid Transit IP in the other family hits one of those family-mismatch panics outright. Check for both, releasing the counted reference since no flow is installed, so the packet passes through unmodified as before. Updates https://github.com/tailscale/corp/issues/44273 Change-Id: Ib0c0eaa92bc4f0470299da08d48c865e1d101849 Signed-off-by: Mazdak Nasab --- feature/conn25/datapath.go | 9 +++++ feature/conn25/datapath_test.go | 70 ++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) 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