diff --git a/cmd/k8s-operator/depaware.txt b/cmd/k8s-operator/depaware.txt index de8c50fc1..7ed8e04f0 100644 --- a/cmd/k8s-operator/depaware.txt +++ b/cmd/k8s-operator/depaware.txt @@ -805,6 +805,7 @@ tailscale.com/cmd/k8s-operator dependencies: (generated by github.com/tailscale/ tailscale.com/net/dns/resolver from tailscale.com/net/dns+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnsfallback from tailscale.com/control/controlclient+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/logpolicy+ tailscale.com/net/flowtrack from tailscale.com/wgengine+ tailscale.com/net/ipset from tailscale.com/ipn/ipnlocal+ tailscale.com/net/memnet from tailscale.com/tsnet diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 1b9a93ab9..7140805ac 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -240,6 +240,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/net/captivedetection from tailscale.com/feature/captiveportal/netcheckhook tailscale.com/net/dnscache from tailscale.com/control/controlhttp+ tailscale.com/net/dnsfallback from tailscale.com/control/controlhttp+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/net/dnsfallback tailscale.com/net/netaddr from tailscale.com/ipn+ tailscale.com/net/netcheck from tailscale.com/cmd/tailscale/cli+ tailscale.com/net/neterror from tailscale.com/net/netcheck+ diff --git a/cmd/tailscaled/depaware-min.txt b/cmd/tailscaled/depaware-min.txt index 65d235fca..539e6896b 100644 --- a/cmd/tailscaled/depaware-min.txt +++ b/cmd/tailscaled/depaware-min.txt @@ -90,6 +90,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/net/dns/resolver from tailscale.com/net/dns+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnsfallback from tailscale.com/cmd/tailscaled+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/logpolicy+ tailscale.com/net/flowtrack from tailscale.com/wgengine/filter tailscale.com/net/ipset from tailscale.com/ipn/ipnlocal+ tailscale.com/net/netaddr from tailscale.com/ipn+ diff --git a/cmd/tailscaled/depaware-minbox.txt b/cmd/tailscaled/depaware-minbox.txt index 495291937..08ee2036f 100644 --- a/cmd/tailscaled/depaware-minbox.txt +++ b/cmd/tailscaled/depaware-minbox.txt @@ -107,6 +107,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/net/dns/resolver from tailscale.com/net/dns+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnsfallback from tailscale.com/cmd/tailscaled+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/logpolicy+ tailscale.com/net/flowtrack from tailscale.com/wgengine/filter tailscale.com/net/ipset from tailscale.com/ipn/ipnlocal+ tailscale.com/net/netaddr from tailscale.com/ipn+ diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index cf050ebc0..1a087d87d 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -382,6 +382,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/net/dns/resolver from tailscale.com/net/dns+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnsfallback from tailscale.com/cmd/tailscaled+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/logpolicy+ tailscale.com/net/flowtrack from tailscale.com/wgengine+ tailscale.com/net/ipset from tailscale.com/ipn/ipnlocal+ tailscale.com/net/netaddr from tailscale.com/ipn+ diff --git a/cmd/tsidp/depaware.txt b/cmd/tsidp/depaware.txt index 9c3c9c1f8..53400074e 100644 --- a/cmd/tsidp/depaware.txt +++ b/cmd/tsidp/depaware.txt @@ -190,6 +190,7 @@ tailscale.com/cmd/tsidp dependencies: (generated by github.com/tailscale/depawar tailscale.com/net/dns/resolver from tailscale.com/net/dns+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnsfallback from tailscale.com/control/controlclient+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/logpolicy+ tailscale.com/net/flowtrack from tailscale.com/wgengine+ tailscale.com/net/ipset from tailscale.com/ipn/ipnlocal+ tailscale.com/net/memnet from tailscale.com/tsnet diff --git a/logpolicy/logpolicy.go b/logpolicy/logpolicy.go index 542e4607e..871f4743f 100644 --- a/logpolicy/logpolicy.go +++ b/logpolicy/logpolicy.go @@ -40,6 +40,7 @@ import ( "tailscale.com/logtail/filch" "tailscale.com/net/dnscache" "tailscale.com/net/dnsfallback" + "tailscale.com/net/dnsfallback/fallbacknames" "tailscale.com/net/netknob" "tailscale.com/net/netmon" "tailscale.com/net/netns" @@ -862,12 +863,17 @@ func dialContext(ctx context.Context, netw, addr string, dial, cachedDial netx.D } // Either regular DNS (via logDNSCache) failed or the IPs it gave us - // didn't work, so try again with IPs from bootstrap DNS. - logf("logtail: dial %q failed: %v (in %v), trying bootstrap...", addr, err, d) - host, _, err := net.SplitHostPort(addr) - if err != nil { + // didn't work, so try again with IPs from bootstrap DNS, but only for + // Tailscale's own log servers. See + // https://github.com/tailscale/tailscale/issues/21028. + host, _, splitErr := net.SplitHostPort(addr) + if splitErr != nil { + return nil, splitErr + } + if !fallbacknames.IsTailscaleHost(host) { return nil, err } + logf("logtail: dial %q failed: %v (in %v), trying bootstrap...", addr, err, d) ips, err := dnsfallback.MakeLookupFunc(logf, netMon)(ctx, host) if err != nil { return nil, err diff --git a/net/dnscache/dnscache.go b/net/dnscache/dnscache.go index 126d862ec..067c37777 100644 --- a/net/dnscache/dnscache.go +++ b/net/dnscache/dnscache.go @@ -72,6 +72,16 @@ const ( srcFallback = "fallback" // Resolver.LookupIPFallback (DERP-based bootstrap DNS) ) +// ErrNoFallback is returned (possibly wrapped) by a +// [Resolver.LookupIPFallback] func to say that it doesn't handle the +// requested host, so the Resolver should return its original error +// without logging or counting a fallback attempt. +// +// net/dnsfallback returns it for any host that isn't one of Tailscale's +// own, as part of slowly retiring the DERP-based fallback DNS entirely. +// See https://github.com/tailscale/tailscale/issues/21028. +var ErrNoFallback = errors.New("no fallback DNS for host") + var ( metricDiskFallbackHit = clientmetric.NewCounter("dnscache_disk_fallback_hit") metricDiskFallbackMiss = clientmetric.NewCounter("dnscache_disk_fallback_miss") @@ -122,6 +132,10 @@ type Resolver struct { // LookupIPFallback optionally provides a backup DNS mechanism // to use if Forward returns an error or no results. + // + // If it returns an error wrapping [ErrNoFallback], the Resolver + // behaves as if LookupIPFallback were nil for that host and + // returns the original error. LookupIPFallback func(ctx context.Context, host string) ([]netip.Addr, error) // TTL is how long to keep entries cached @@ -385,15 +399,18 @@ func (r *Resolver) lookupIP(ctx context.Context, host string) (ip, ip6 netip.Add if (err != nil || len(ips) == 0) && r.LookupIPFallback != nil { lookupCtx, lookupCancel := context.WithTimeout(ctx, 30*time.Second) defer lookupCancel() - if err != nil { - r.dlogf("resolving %q using fallback resolver due to error", host) - } else { - r.dlogf("resolving %q using fallback resolver due to no returned IPs", host) - } - src = srcFallback - ips, err = r.LookupIPFallback(lookupCtx, host) - if err == nil && len(ips) > 0 { - metricDERPFallbackOK.Add(1) + fbIPs, fbErr := r.LookupIPFallback(lookupCtx, host) + if !errors.Is(fbErr, ErrNoFallback) { + if err != nil { + r.dlogf("tried fallback resolver for %q due to error", host) + } else { + r.dlogf("tried fallback resolver for %q due to no returned IPs", host) + } + src = srcFallback + ips, err = fbIPs, fbErr + if err == nil && len(ips) > 0 { + metricDERPFallbackOK.Add(1) + } } } if err != nil { diff --git a/net/dnscache/dnscache_test.go b/net/dnscache/dnscache_test.go index 23edce755..6a4ec5284 100644 --- a/net/dnscache/dnscache_test.go +++ b/net/dnscache/dnscache_test.go @@ -414,6 +414,29 @@ func TestDiskCacheHooks(t *testing.T) { t.Errorf("persist calls = %+v; want %+v", persisted, want) } }) + + t.Run("no-fallback-keeps-original-error", func(t *testing.T) { + defer HookLookupDiskCache.SetForTest(func(host string) ([]netip.Addr, bool) { + return nil, false + })() + r := &Resolver{ + Logf: t.Logf, + LookupIPForTest: func(ctx context.Context, host string) ([]netip.Addr, error) { + return nil, errFailed + }, + LookupIPFallback: func(ctx context.Context, host string) ([]netip.Addr, error) { + return nil, ErrNoFallback + }, + } + derp0 := metricDERPFallbackOK.Value() + _, _, _, err := r.LookupIP(t.Context(), "other.example.com") + if !errors.Is(err, errFailed) { + t.Errorf("err = %v; want original error %v", err, errFailed) + } + if d := metricDERPFallbackOK.Value() - derp0; d != 0 { + t.Errorf("DERP fallback ok metric delta = %d; want 0", d) + } + }) } func newTestCert(t *testing.T, dnsName string) (tls.Certificate, *x509.Certificate) { diff --git a/net/dnsfallback/dnsfallback.go b/net/dnsfallback/dnsfallback.go index a2297b681..6f069cabc 100644 --- a/net/dnsfallback/dnsfallback.go +++ b/net/dnsfallback/dnsfallback.go @@ -4,6 +4,10 @@ // Package dnsfallback contains a DNS fallback mechanism // for starting up Tailscale when the system DNS is broken or otherwise unavailable. // +// It resolves Tailscale's own hostnames by asking DERP servers over HTTPS. +// We're slowly removing it in favor of caching earlier DNS results on disk +// (feature/dnsresolvecache); see https://github.com/tailscale/tailscale/issues/21028. +// // The data is backed by a JSON file `dns-fallback-servers.json` that is updated // by `update-dns-fallbacks.go`: // @@ -28,6 +32,8 @@ import ( "tailscale.com/atomicfile" "tailscale.com/feature" "tailscale.com/health" + "tailscale.com/net/dnscache" + "tailscale.com/net/dnsfallback/fallbacknames" "tailscale.com/net/netmon" "tailscale.com/net/netns" "tailscale.com/net/netutil" @@ -39,7 +45,13 @@ import ( // MakeLookupFunc creates a function that can be used to resolve hostnames // (e.g. as a LookupIPFallback from dnscache.Resolver). -// The netMon parameter is optional; if non-nil it's used to do faster interface lookups. +// The netMon parameter is optional; if non-nil it's used to do faster interface lookups +// and to fail fast when no network interface is up. +// +// The returned func only resolves Tailscale's own hostnames (see +// [fallbacknames.IsTailscaleHost]). For any other name it returns an error wrapping +// [dnscache.ErrNoFallback], so a [dnscache.Resolver] returns its original +// error instead. func MakeLookupFunc(logf logger.Logf, netMon *netmon.Monitor) func(ctx context.Context, host string) ([]netip.Addr, error) { fr := &fallbackResolver{ logf: logf, @@ -60,14 +72,24 @@ type fallbackResolver struct { } func (fr *fallbackResolver) Lookup(ctx context.Context, host string) ([]netip.Addr, error) { - return lookup(ctx, host, fr.logf, fr.healthTracker, fr.netMon) -} - -func lookup(ctx context.Context, host string, logf logger.Logf, ht *health.Tracker, netMon *netmon.Monitor) ([]netip.Addr, error) { if ip, err := netip.ParseAddr(host); err == nil && ip.IsValid() { return []netip.Addr{ip}, nil } + // DERP servers only answer for Tailscale's own names, so asking about + // any other name can't work. See + // https://github.com/tailscale/tailscale/issues/21028. + if !fallbacknames.IsTailscaleHost(host) { + return nil, dnscache.ErrNoFallback + } + if fr.netMon != nil && !fr.netMon.InterfaceState().AnyInterfaceUp() { + return nil, errNetworkDown + } + return lookup(ctx, host, fr.logf, fr.healthTracker, fr.netMon) +} +var errNetworkDown = errors.New("dnsfallback: network is down") + +func lookup(ctx context.Context, host string, logf logger.Logf, ht *health.Tracker, netMon *netmon.Monitor) ([]netip.Addr, error) { type nameIP struct { dnsName string ip netip.Addr diff --git a/net/dnsfallback/dnsfallback_test.go b/net/dnsfallback/dnsfallback_test.go index 828f547cc..9dc784da2 100644 --- a/net/dnsfallback/dnsfallback_test.go +++ b/net/dnsfallback/dnsfallback_test.go @@ -6,13 +6,16 @@ package dnsfallback import ( "context" "encoding/json" + "errors" "flag" "net/netip" "os" "path/filepath" "reflect" + "strings" "testing" + "tailscale.com/net/dnscache" "tailscale.com/net/netmon" "tailscale.com/tailcfg" "tailscale.com/types/logger" @@ -220,3 +223,34 @@ func TestBootstrapDNSMapNilNetMon(t *testing.T) { t.Fatal("expected error from canceled context") } } + +// TestLookupSkips verifies that [fallbackResolver.Lookup] fails without +// asking any DERP server for names that aren't Tailscale's and when the +// network is down. +func TestLookupSkips(t *testing.T) { + // The context is already canceled, so if Lookup got as far as dialing + // a DERP server, it would fail with a context error instead of the + // errors checked for below. + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + fr := &fallbackResolver{logf: t.Logf} + _, err := fr.Lookup(ctx, "headscale.example.com") + if !errors.Is(err, dnscache.ErrNoFallback) { + t.Errorf("Lookup of non-Tailscale host: err = %v; want %v", err, dnscache.ErrNoFallback) + } + if err != nil && strings.Contains(err.Error(), "headscale") { + t.Errorf("error %q mentions the non-Tailscale host", err) + } + + // A Monitor with no interface state reports no interface as up. + fr.netMon = new(netmon.Monitor) + if _, err := fr.Lookup(ctx, "controlplane.tailscale.com"); !errors.Is(err, errNetworkDown) { + t.Errorf("Lookup with network down: err = %v; want %v", err, errNetworkDown) + } + + addrs, err := fr.Lookup(ctx, "192.0.2.1") + if err != nil || len(addrs) != 1 || addrs[0] != netip.MustParseAddr("192.0.2.1") { + t.Errorf("Lookup of IP literal = %v, %v; want [192.0.2.1], nil", addrs, err) + } +} diff --git a/net/dnsfallback/fallbacknames/fallbacknames.go b/net/dnsfallback/fallbacknames/fallbacknames.go new file mode 100644 index 000000000..a8f696448 --- /dev/null +++ b/net/dnsfallback/fallbacknames/fallbacknames.go @@ -0,0 +1,48 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +// Package fallbacknames defines which hostnames the DERP-based fallback +// DNS mechanism (net/dnsfallback and the DERP server's /bootstrap-dns +// handler) is allowed to resolve. +// +// It's a leaf package so that both clients and cmd/derper can share the +// policy without depending on each other. +// +// We're slowly removing the DERP-based fallback DNS mechanism entirely in +// favor of caching earlier DNS results on disk (feature/dnsresolvecache). +// This set of names may shrink over time but must not grow. +// See https://github.com/tailscale/tailscale/issues/21028. +package fallbacknames + +import "strings" + +// IsTailscaleHost reports whether host is one of Tailscale's own hostnames, +// the only names the DERP-based fallback DNS resolves. +func IsTailscaleHost(host string) bool { + host = strings.ToLower(strings.TrimSuffix(host, ".")) + if !validHostname(host) { + return false + } + return host == "tailscale.com" || + strings.HasSuffix(host, ".tailscale.com") || + strings.HasSuffix(host, ".tailscale.io") +} + +// validHostname reports whether host is made of non-empty dot-separated +// labels of lowercase letters, digits, and hyphens. +func validHostname(host string) bool { + if host == "" || len(host) > 253 { + return false + } + for label := range strings.SplitSeq(host, ".") { + if label == "" { + return false + } + for _, c := range label { + if !('a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-') { + return false + } + } + } + return true +} diff --git a/net/dnsfallback/fallbacknames/fallbacknames_test.go b/net/dnsfallback/fallbacknames/fallbacknames_test.go new file mode 100644 index 000000000..b4ef27227 --- /dev/null +++ b/net/dnsfallback/fallbacknames/fallbacknames_test.go @@ -0,0 +1,33 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package fallbacknames + +import "testing" + +func TestIsTailscaleHost(t *testing.T) { + tests := []struct { + host string + want bool + }{ + {"controlplane.tailscale.com", true}, + {"log.tailscale.com", true}, + {"log.tailscale.io", true}, + {"login.us.tailscale.com", true}, + {"Controlplane.Tailscale.COM.", true}, + {"tailscale.com", true}, + {"headscale.example.com", false}, + {"faketailscale.com", false}, + {"tailscale.com.example.net", false}, + {"log.tailscale.com/x.tailscale.com", false}, + {"foo bar.tailscale.com", false}, + {"foo..tailscale.com", false}, + {".tailscale.com", false}, + {"", false}, + } + for _, tt := range tests { + if got := IsTailscaleHost(tt.host); got != tt.want { + t.Errorf("IsTailscaleHost(%q) = %v; want %v", tt.host, got, tt.want) + } + } +} diff --git a/tsnet/depaware.txt b/tsnet/depaware.txt index e902e257b..d58a3407d 100644 --- a/tsnet/depaware.txt +++ b/tsnet/depaware.txt @@ -186,6 +186,7 @@ tailscale.com/tsnet dependencies: (generated by github.com/tailscale/depaware) tailscale.com/net/dns/resolver from tailscale.com/net/dns+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnsfallback from tailscale.com/control/controlclient+ + tailscale.com/net/dnsfallback/fallbacknames from tailscale.com/logpolicy+ tailscale.com/net/flowtrack from tailscale.com/wgengine+ tailscale.com/net/ipset from tailscale.com/ipn/ipnlocal+ tailscale.com/net/memnet from tailscale.com/tsnet