From 04e532b257d3b1d2e46a44109bbc76544d2e4dde Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 7 Oct 2020 15:58:14 +0200 Subject: [PATCH] core: fix crash with no plugins (#4184) * core: fix crash with no plugins A Corefile that defines a zone without plugins crashes coredns with the stack trace below. Change this to return a refused. ~~~ corefile example.org { whoami log cache debug } example.net { } ~~~ Asking for anyhing in example.net does this. Add test that tests this. ~~~ panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0xa5e6a4] goroutine 55 [running]: github.com/coredns/coredns/core/dnsserver.(*Server).ServeDNS(0xc000438f60, 0x2059420, 0xc0005a4030, 0x206c0e0, 0xc000522140, 0xc0005ae000) /home/miek/src/github.com/coredns/coredns/core/dnsserver/server.go:247 +0x884 github.com/coredns/coredns/core/dnsserver.(*Server).ServePacket.func1(0x206dd00, 0xc00009e180, 0xc0005ae000) /home/miek/src/github.com/coredns/coredns/core/dnsserver/server.go:126 +0xaf github.com/miekg/dns.HandlerFunc.ServeDNS(0xc000529270, 0x206dd00, 0xc00009e180, 0xc0005ae000) /home/miek/go/pkg/mod/github.com/miekg/dns@v1.1.31/server.go:37 +0x44 github.com/miekg/dns.(*Server).serveDNS(0xc000286c60, 0xc000282400, 0x34, 0x200, 0xc00009e180) /home/miek/go/pkg/mod/github.com/miekg/dns@v1.1.31/server.go:609 +0x2f7 github.com/miekg/dns.(*Server).serveUDPPacket(0xc000286c60, 0xc0003b03b4, 0xc000282400, 0x34, 0x200, 0xc00000e320, 0xc000522080) /home/miek/go/pkg/mod/github.com/miekg/dns@v1.1.31/server.go:549 +0xb2 created by github.com/miekg/dns.(*Server).serveUDP /home/miek/go/pkg/mod/github.com/miekg/dns@v1.1.31/server.go:479 +0x292 ~~~ Also fix single typo in chaos_test.go Signed-off-by: Miek Gieben * Fix naming Signed-off-by: Miek Gieben --- core/dnsserver/server.go | 4 ++++ test/chaos_test.go | 2 +- test/no_plugins_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/no_plugins_test.go diff --git a/core/dnsserver/server.go b/core/dnsserver/server.go index 52a2d6efc..eb23346e0 100644 --- a/core/dnsserver/server.go +++ b/core/dnsserver/server.go @@ -240,6 +240,10 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) for { if h, ok := s.zones[q[off:]]; ok { + if h.pluginChain == nil { // zone defined, but has not got any plugins + errorAndMetricsFunc(s.Addr, w, r, dns.RcodeRefused) + return + } if r.Question[0].Qtype != dns.TypeDS { if h.FilterFunc == nil { rcode, _ := h.pluginChain.ServeDNS(ctx, w, r) diff --git a/test/chaos_test.go b/test/chaos_test.go index 5178e5fc3..eb83a2759 100644 --- a/test/chaos_test.go +++ b/test/chaos_test.go @@ -32,6 +32,6 @@ func TestChaos(t *testing.T) { chTxt := resp.Answer[0].(*dns.TXT).Txt[0] version := caddy.AppName + "-" + caddy.AppVersion if chTxt != version { - t.Fatalf("Expected version to bo %s, got %s", version, chTxt) + t.Fatalf("Expected version to be %s, got %s", version, chTxt) } } diff --git a/test/no_plugins_test.go b/test/no_plugins_test.go new file mode 100644 index 000000000..51d546db3 --- /dev/null +++ b/test/no_plugins_test.go @@ -0,0 +1,28 @@ +package test + +import ( + "testing" + + "github.com/miekg/dns" +) + +func TestNoPlugins(t *testing.T) { + corefile := `example.org:0 { + }` + + i, udp, _, err := CoreDNSServerAndPorts(corefile) + if err != nil { + t.Fatalf("Could not get CoreDNS serving instance: %s", err) + } + defer i.Stop() + + m := new(dns.Msg) + m.SetQuestion("example.org.", dns.TypeA) + resp, err := dns.Exchange(m, udp) + if err != nil { + t.Fatalf("Expected to receive reply, but didn't: %v", err) + } + if resp.Rcode != dns.RcodeRefused { + t.Fatalf("Expected rcode to be %d, got %d", dns.RcodeRefused, resp.Rcode) + } +}