| 1 | Subject: [PATCH] Route cage TCP and DNS through host-owned transports |
| 2 | |
| 3 | Optional transport hooks retain the upstream default. DNS exchange failures |
| 4 | return SERVFAIL without invoking the host resolver. Cage supplies a proxy-only |
| 5 | TCP dialer and DoH exchange outside the guest. |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | @@ -150,7 +150,20 @@ func NewHandler(opts HandlerOptions) (dns.Handler, error) { |
| 13 | return h, nil |
| 14 | } |
| 15 | |
| 16 | +// Exchange overrides upstream resolution; a failed exchange returns SERVFAIL. |
| 17 | +var Exchange func(context.Context, *dns.Msg) (*dns.Msg, error) |
| 18 | + |
| 19 | func (h *Handler) handleQuery(ctx context.Context, w dns.ResponseWriter, req *dns.Msg) { |
| 20 | + if Exchange != nil { |
| 21 | + defer w.Close() |
| 22 | + answer, err := Exchange(ctx, req) |
| 23 | + if err != nil { |
| 24 | + answer = new(dns.Msg) |
| 25 | + answer.SetRcode(req, dns.RcodeServerFailure) |
| 26 | + } |
| 27 | + _ = w.WriteMsg(answer) |
| 28 | + return |
| 29 | + } |
| 30 | var ( |
| 31 | reply dns.Msg |
| 32 | handled bool |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | @@ -22,6 +22,7 @@ import ( |
| 38 | "github.com/containers/gvisor-tap-vsock/pkg/transport" |
| 39 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 40 | "github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork" |
| 41 | + "github.com/miekg/dns" |
| 42 | "github.com/sirupsen/logrus" |
| 43 | "golang.org/x/sync/errgroup" |
| 44 | ) |
| 45 | @@ -54,6 +55,23 @@ const gatewayMacAddr = "5a:94:ef:e4:0c:dd" |
| 46 | // The host services the guest needs (AllowHostService) are reached past it. |
| 47 | var Outbound types.Outbound |
| 48 | |
| 49 | +// GuestDial and DNSExchange are host-owned transport hooks. |
| 50 | +var GuestDial types.DialContext |
| 51 | +var DNSExchange func(context.Context, *dns.Msg) (*dns.Msg, error) |
| 52 | + |
| 53 | +func dialGuest(ctx context.Context, network, addr string) (net.Conn, error) { |
| 54 | + if GuestDial == nil { |
| 55 | + return (&net.Dialer{}).DialContext(ctx, network, addr) |
| 56 | + } |
| 57 | + dst, err := netip.ParseAddrPort(addr) |
| 58 | + if err == nil && dst.Addr().Unmap() == netip.MustParseAddr("127.0.0.1") { |
| 59 | + if _, ok := hostServices.Load(hostService{network, dst.Port()}); ok { |
| 60 | + return (&net.Dialer{}).DialContext(ctx, network, addr) |
| 61 | + } |
| 62 | + } |
| 63 | + return GuestDial(ctx, network, addr) |
| 64 | +} |
| 65 | + |
| 66 | // Names and Answered, when set by the program running the hostagent, are |
| 67 | // applied by the gateway's DNS server of the per-instance netstacks to every |
| 68 | // query it gets (types.Configuration.DNSNames and DNSAnswered). |
| 69 | @@ -125,6 +143,8 @@ func StartGVisorNetstack(ctx context.Context, gVisorOpts *GVisorNetstackOpts) er |
| 70 | }, |
| 71 | GatewayVirtualIPs: []string{gatewayIP}, |
| 72 | Outbound: outbound, |
| 73 | + DialContext: dialGuest, |
| 74 | + DNSExchange: DNSExchange, |
| 75 | DNSNames: Names, |
| 76 | DNSAnswered: Answered, |
| 77 | } |
| 78 | |
| 79 | new file mode 100644 |
| 80 | |
| 81 | |
| 82 | |
| 83 | @@ -0,0 +1,26 @@ |
| 84 | +// SPDX-License-Identifier: Apache-2.0 |
| 85 | +package dns |
| 86 | + |
| 87 | +import ( |
| 88 | + "context" |
| 89 | + "errors" |
| 90 | + "testing" |
| 91 | + |
| 92 | + "github.com/miekg/dns" |
| 93 | +) |
| 94 | + |
| 95 | +func TestCageDNSFailureDoesNotUseStaticOrHostResolver(t *testing.T) { |
| 96 | + previous := Exchange |
| 97 | + t.Cleanup(func() { Exchange = previous }) |
| 98 | + Exchange = func(context.Context, *dns.Msg) (*dns.Msg, error) { return nil, errors.New("proxy down") } |
| 99 | + h, err := NewHandler(HandlerOptions{StaticHosts: map[string]string{"example.com": "1.1.1.1"}}) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + q := new(dns.Msg) |
| 104 | + q.SetQuestion("example.com.", dns.TypeA) |
| 105 | + h.ServeDNS(TestResponseWriter{}, q) |
| 106 | + if dnsResult.Rcode != dns.RcodeServerFailure || len(dnsResult.Answer) != 0 { |
| 107 | + t.Fatal("expected SERVFAIL without static/host fallback") |
| 108 | + } |
| 109 | +} |