| 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 | @@ -24,6 +24,7 @@ type upstreamResolver interface { |
| 13 | } |
| 14 | |
| 15 | type dnsHandler struct { |
| 16 | + exchange func(context.Context, *dns.Msg) (*dns.Msg, error) |
| 17 | zones []types.Zone |
| 18 | zonesLock sync.RWMutex |
| 19 | upstream upstreamResolver |
| 20 | @@ -36,7 +37,16 @@ func (h *dnsHandler) handle(w dns.ResponseWriter, r *dns.Msg, responseMessageSiz |
| 21 | m := new(dns.Msg) |
| 22 | m.SetReply(r) |
| 23 | m.RecursionAvailable = true |
| 24 | - h.addAnswers(m) |
| 25 | + if h.exchange != nil { |
| 26 | + answer, err := h.exchange(context.Background(), r) |
| 27 | + if err != nil { |
| 28 | + m.SetRcode(r, dns.RcodeServerFailure) |
| 29 | + } else { |
| 30 | + m = answer |
| 31 | + } |
| 32 | + } else { |
| 33 | + h.addAnswers(m) |
| 34 | + } |
| 35 | edns0 := r.IsEdns0() |
| 36 | if edns0 != nil { |
| 37 | responseMessageSize = int(edns0.UDPSize()) |
| 38 | @@ -332,3 +342,8 @@ func (s *Server) addZone(req types.Zone) { |
| 39 | // No existing zone for req.Name, add new one |
| 40 | s.handler.zones = append(s.handler.zones, req) |
| 41 | } |
| 42 | + |
| 43 | +// SetExchange replaces all upstream DNS resolution; errors never fall back. |
| 44 | +func (s *Server) SetExchange(exchange func(context.Context, *dns.Msg) (*dns.Msg, error)) { |
| 45 | + s.handler.exchange = exchange |
| 46 | +} |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | @@ -19,7 +19,7 @@ import ( |
| 52 | |
| 53 | const linkLocalSubnet = "169.254.0.0/16" |
| 54 | |
| 55 | -func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool, outbound types.Outbound) *tcp.Forwarder { |
| 56 | +func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool, outbound types.Outbound, dialers ...types.DialContext) *tcp.Forwarder { |
| 57 | return tcp.NewForwarder(s, 0, 10, func(r *tcp.ForwarderRequest) { |
| 58 | localAddress := r.ID().LocalAddress |
| 59 | |
| 60 | @@ -37,7 +37,11 @@ func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mute |
| 61 | r.Complete(true) |
| 62 | return |
| 63 | } |
| 64 | - outbound, err := net.Dial("tcp", net.JoinHostPort(localAddress.String(), fmt.Sprint(r.ID().LocalPort))) |
| 65 | + dial := (&net.Dialer{}).DialContext |
| 66 | + if len(dialers) > 0 && dialers[0] != nil { |
| 67 | + dial = dialers[0] |
| 68 | + } |
| 69 | + outbound, err := dial(context.Background(), "tcp", net.JoinHostPort(localAddress.String(), fmt.Sprint(r.ID().LocalPort))) |
| 70 | if err != nil { |
| 71 | log.Tracef("net.Dial() = %v", err) |
| 72 | r.Complete(true) |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | @@ -1,6 +1,8 @@ |
| 78 | package types |
| 79 | |
| 80 | import ( |
| 81 | + "context" |
| 82 | + "github.com/miekg/dns" |
| 83 | "net" |
| 84 | "net/netip" |
| 85 | "regexp" |
| 86 | @@ -15,7 +17,11 @@ func (o Outbound) Allows(network string, dst netip.AddrPort) bool { |
| 87 | return o == nil || o(network, dst) |
| 88 | } |
| 89 | |
| 90 | +type DialContext func(context.Context, string, string) (net.Conn, error) |
| 91 | + |
| 92 | type Configuration struct { |
| 93 | + DialContext DialContext `yaml:"-"` |
| 94 | + DNSExchange func(context.Context, *dns.Msg) (*dns.Msg, error) `yaml:"-"` |
| 95 | // Print packets on stderr |
| 96 | Debug bool `yaml:"debug,omitempty"` |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | @@ -24,7 +24,7 @@ func addServices(configuration *types.Configuration, s *stack.Stack, ipPool *tap |
| 103 | var natLock sync.Mutex |
| 104 | translation := parseNATTable(configuration) |
| 105 | |
| 106 | - tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound) |
| 107 | + tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound, configuration.DialContext) |
| 108 | s.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket) |
| 109 | udpForwarder := forwarder.UDP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound) |
| 110 | s.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket) |
| 111 | @@ -83,6 +83,7 @@ func dnsServer(configuration *types.Configuration, s *stack.Stack) (http.Handler |
| 112 | } |
| 113 | |
| 114 | server.SetPolicy(configuration.DNSNames, configuration.DNSAnswered) |
| 115 | + server.SetExchange(configuration.DNSExchange) |
| 116 | |
| 117 | go func() { |
| 118 | if err := server.Serve(); err != nil { |
| 119 | |
| 120 | new file mode 100644 |
| 121 | |
| 122 | |
| 123 | |
| 124 | @@ -0,0 +1,29 @@ |
| 125 | +// SPDX-License-Identifier: Apache-2.0 |
| 126 | +package dns |
| 127 | + |
| 128 | +import ( |
| 129 | + "context" |
| 130 | + "errors" |
| 131 | + "testing" |
| 132 | + |
| 133 | + "github.com/miekg/dns" |
| 134 | +) |
| 135 | + |
| 136 | +type cageWriter struct { |
| 137 | + dns.ResponseWriter |
| 138 | + msg *dns.Msg |
| 139 | +} |
| 140 | + |
| 141 | +func (w *cageWriter) WriteMsg(m *dns.Msg) error { w.msg = m; return nil } |
| 142 | + |
| 143 | +func TestCageDNSFailureDoesNotUseHostResolver(t *testing.T) { |
| 144 | + q := new(dns.Msg) |
| 145 | + q.SetQuestion("example.com.", dns.TypeA) |
| 146 | + w := &cageWriter{} |
| 147 | + // A nil upstream would panic if the ordinary resolver path were used. |
| 148 | + h := &dnsHandler{exchange: func(context.Context, *dns.Msg) (*dns.Msg, error) { return nil, errors.New("proxy down") }} |
| 149 | + h.handle(w, q, dns.MaxMsgSize) |
| 150 | + if w.msg == nil || w.msg.Rcode != dns.RcodeServerFailure { |
| 151 | + t.Fatal("expected fail-closed SERVFAIL") |
| 152 | + } |
| 153 | +} |