Files
tailscale/feature/androiddns/androiddns_test.go
Brad Fitzpatrick 86b3cd5aa1 feature/androiddns: add dnsproxyd DNS resolution for standalone Android binaries
Android doesn't have /etc/resolv.conf. This causes problems for people
running GOOS=linux binaries (or GOOS=android binaries without cgo, so
they don't use Android's bionic libc) in Termux, adb shell, etc.
(Go binaries built with cgo use bionic on Android: golang/go#10714)

16 years ago when I was on the Android team I added a system-wide DNS
cache (dnsproxyd) and made bionic query that, so each Android app
wasn't doing its own DNS resolution. That interface was never meant to
be stable, and I thought that code would be surely dead by now 16
years later, but apparently it lives on, and is more stable now: both
empirically (time, ossification?), and because of how Android's split system
updates work nowadays, the dnsproxyd lives on the other side of bionic,
so they seem to keep it pretty stable. The old bionic<->dnsproxyd APIs
I added 16 years ago are still there, but 8 years ago it got some additional
APIs to query by a DNS packet instead.

So use it! If we find ourselves on Android and without libc access
(and because we don't want to pull in ebitengine/purego with all its
side effects), just query the DNS server like bionic does.

This can be disabled in Linux binaries with ts_omit_androiddns.

Old links:

  LineageOS/android_system_netd@007e987fee
  https://android.googlesource.com/platform/system/netd/+/007e987fee7e815e0c4bc820f434a632b7a69a9d
  ("DNS proxy thread in netd.")

  aosp-mirror/platform_bionic@a1dbf0b453
  https://android.googlesource.com/platform/bionic/+/a1dbf0b453801620565e5911f354f82706b0200d
  ("DNS proxy: the start.  proxies getaddrinfo calls.")

Back then I found it cleaner to proxy at the getaddrinfo level rather
than speak in terms of DNS packets. The raw-packet resnsend command I
use here came eight years later, added in November 2018 for Android
10's android_res_nsend NDK API:

  LineageOS/android_system_netd@c0c818f448
  https://android.googlesource.com/platform/system/netd/+/c0c818f448efa90ab1f9b1733fb86c5e22fb894c
  ("Add resNetworkSend cmd in DnsProxyListener")

Android 10 (codename Q, API level 29, released September 2019) is
therefore the minimum OS version for this to work.

I verified this against the DnsResolver module on an Android 13
emulator with SELinux enforcing, from the shell UID, with both a pure
Go GOOS=android binary and a static GOOS=linux binary: raw queries,
NXDOMAIN handling, the runtime Android detection, and a tailcat binary
reaching DERP with lookups visible in the daemon's logcat output, some
served from my 2010 DNS cache.

Updates #21129

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I0d63763e255a077e4e5745b3e64ba0d78dab6d69
Signed-off-by: Brad Fitzpatrick <[email protected]>
2026-09-07 18:35:31 -07:00

228 lines
5.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build linux
package androiddns
import (
"context"
"encoding/base64"
"encoding/binary"
"net"
"net/netip"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"golang.org/x/net/dns/dnsmessage"
"tailscale.com/tstest"
)
// startFakeDaemon starts a fake dnsproxyd on a unix socket and points
// the package at it. For each connection it parses the resnsend
// command, decodes the query, and replies with what answer returns.
// If answer returns a negative value, that value is sent as the
// initial big-endian result (the daemon's -errno case).
func startFakeDaemon(t *testing.T, answer func(query []byte) (ans []byte, errno int32)) {
t.Helper()
sock := filepath.Join(t.TempDir(), "dnsproxyd")
ln, err := net.Listen("unix", sock)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
tstest.Replace(t, &socketPath, sock)
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
buf := make([]byte, maxCmdSize)
n, err := c.Read(buf)
if err != nil {
return
}
cmd := string(buf[:n])
nul := strings.IndexByte(cmd, 0)
if nul < 0 {
t.Error("command not NUL terminated in a single read")
return
}
f := strings.Fields(cmd[:nul])
if len(f) != 4 || f[0] != "resnsend" {
t.Errorf("bad command %q", cmd[:nul])
return
}
query, err := base64.StdEncoding.DecodeString(f[3])
if err != nil {
t.Errorf("bad base64 %q: %v", f[3], err)
return
}
ans, errno := answer(query)
var be [4]byte
if errno < 0 {
binary.BigEndian.PutUint32(be[:], uint32(errno))
c.Write(be[:])
return
}
binary.BigEndian.PutUint32(be[:], 0) // rcode
c.Write(be[:])
binary.BigEndian.PutUint32(be[:], uint32(len(ans)))
c.Write(be[:])
c.Write(ans)
}()
}
}()
}
// dnsAnswer builds a wire-format answer to the single-question query,
// answering A questions with 1.2.3.4 and AAAA questions with ::6.
func dnsAnswer(t *testing.T, query []byte) []byte {
var p dnsmessage.Parser
hdr, err := p.Start(query)
if err != nil {
t.Errorf("parsing query: %v", err)
return nil
}
q, err := p.Question()
if err != nil {
t.Errorf("reading question: %v", err)
return nil
}
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{
ID: hdr.ID,
Response: true,
Authoritative: true,
})
b.EnableCompression()
b.StartQuestions()
b.Question(q)
b.StartAnswers()
rh := dnsmessage.ResourceHeader{Name: q.Name, Class: q.Class, TTL: 60}
switch q.Type {
case dnsmessage.TypeA:
rh.Type = dnsmessage.TypeA
b.AResource(rh, dnsmessage.AResource{A: [4]byte{1, 2, 3, 4}})
case dnsmessage.TypeAAAA:
rh.Type = dnsmessage.TypeAAAA
b.AAAAResource(rh, dnsmessage.AAAAResource{AAAA: [16]byte{15: 6}})
}
ans, err := b.Finish()
if err != nil {
t.Errorf("building answer: %v", err)
return nil
}
return ans
}
func makeQuery(t *testing.T, name string, qtype dnsmessage.Type) []byte {
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{ID: 0x1234, RecursionDesired: true})
b.StartQuestions()
if err := b.Question(dnsmessage.Question{
Name: dnsmessage.MustNewName(name),
Type: qtype,
Class: dnsmessage.ClassINET,
}); err != nil {
t.Fatal(err)
}
msg, err := b.Finish()
if err != nil {
t.Fatal(err)
}
return msg
}
func TestQuery(t *testing.T) {
startFakeDaemon(t, func(query []byte) ([]byte, int32) {
return dnsAnswer(t, query), 0
})
query := makeQuery(t, "example.com.", dnsmessage.TypeA)
ans, err := Query(context.Background(), query)
if err != nil {
t.Fatal(err)
}
var p dnsmessage.Parser
hdr, err := p.Start(ans)
if err != nil {
t.Fatal(err)
}
if hdr.ID != 0x1234 {
t.Errorf("answer ID = %#x; want %#x", hdr.ID, 0x1234)
}
if err := p.SkipAllQuestions(); err != nil {
t.Fatal(err)
}
if _, err := p.AnswerHeader(); err != nil {
t.Fatal(err)
}
a, err := p.AResource()
if err != nil {
t.Fatal(err)
}
if got, want := netip.AddrFrom4(a.A), netip.AddrFrom4([4]byte{1, 2, 3, 4}); got != want {
t.Errorf("answer = %v; want %v", got, want)
}
}
func TestQueryDaemonError(t *testing.T) {
startFakeDaemon(t, func(query []byte) ([]byte, int32) {
return nil, -111 // -ECONNREFUSED
})
query := makeQuery(t, "example.com.", dnsmessage.TypeA)
_, err := Query(context.Background(), query)
if err == nil {
t.Fatal("Query succeeded; want error")
}
if !strings.Contains(err.Error(), "connection refused") {
t.Errorf("error %q does not mention connection refused", err)
}
}
func TestQueryTooLarge(t *testing.T) {
_, err := Query(context.Background(), make([]byte, maxCmdSize))
if err == nil || !strings.Contains(err.Error(), "too large") {
t.Fatalf("err = %v; want too-large error", err)
}
}
func TestQueryContextCancel(t *testing.T) {
startFakeDaemon(t, func(query []byte) ([]byte, int32) {
time.Sleep(5 * time.Second)
return dnsAnswer(t, query), 0
})
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
query := makeQuery(t, "example.com.", dnsmessage.TypeA)
if _, err := Query(ctx, query); err == nil {
t.Fatal("Query succeeded; want timeout error")
}
}
// TestResolver exercises the full net.Resolver integration: Go's
// built-in resolver framing queries over our conn and parsing the
// answers the fake daemon returns.
func TestResolver(t *testing.T) {
startFakeDaemon(t, func(query []byte) ([]byte, int32) {
return dnsAnswer(t, query), 0
})
r := NewResolver()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
addrs, err := r.LookupNetIP(ctx, "ip", "example.com.")
if err != nil {
t.Fatal(err)
}
want4 := netip.MustParseAddr("1.2.3.4")
want6 := netip.MustParseAddr("::6")
if !slices.Contains(addrs, want4) || !slices.Contains(addrs, want6) {
t.Errorf("LookupNetIP = %v; want both %v and %v", addrs, want4, want6)
}
}