aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2017-09-29 22:28:13 +0100
committerGravatar GitHub <noreply@github.com> 2017-09-29 22:28:13 +0100
commit4276d29b81575992bf0c7525b4cb5716d493e913 (patch)
tree152994cf7cb251f9f46cb8c3b914cb413eda33de
parent23526aec1d933b6362c80eaa084749a794d43aca (diff)
downloadcoredns-4276d29b81575992bf0c7525b4cb5716d493e913.tar.gz
coredns-4276d29b81575992bf0c7525b4cb5716d493e913.tar.zst
coredns-4276d29b81575992bf0c7525b4cb5716d493e913.zip
Add fuzzing infrastructure (#1118)
Fix file/fuzz.go build and docs in Makefile.fuzz Each plugin can add a fuzz.go to join the fuzzing craze. pkg/fuzz/do.go could be made a lot smarter, but is probably good enough for starters. $ make -f Makefile.fuzz <plugin> will build with go-fuzz-build and then execute a go-fuzz run. Each plugin's fuzz run uses a per-plugin directory to store the fuzz data.
-rw-r--r--Makefile.fuzz33
-rw-r--r--plugin/cache/fuzz.go14
-rw-r--r--plugin/file/fuzz.go48
-rw-r--r--plugin/pkg/fuzz/do.go25
-rw-r--r--plugin/proxy/fuzz.go19
-rw-r--r--plugin/rewrite/fuzz.go19
6 files changed, 158 insertions, 0 deletions
diff --git a/Makefile.fuzz b/Makefile.fuzz
new file mode 100644
index 000000000..46358246b
--- /dev/null
+++ b/Makefile.fuzz
@@ -0,0 +1,33 @@
+# Makefile for fuzzing
+#
+# Use go-fuzz and needs the tools installed. For each fuzz.go in a plugin's directory
+# you can start the fuzzing with: make -f Makefile.fuzz <plugin>
+# e.g.
+#
+# make -f Makefile.fuzz proxy
+#
+# Each plugin that wants to join the fuzzing fray only needs to add a fuzz.go that calls
+# the plugins's ServeDNS and used the plugin/pkg/fuzz for the Do function.
+#
+# Installing go-fuzz
+#$ go get github.com/dvyukov/go-fuzz/go-fuzz
+#$ go get github.com/dvyukov/go-fuzz/go-fuzz-build
+
+REPO:="github.com/coredns/coredns/plugin"
+
+FUZZ:=$(dir $(wildcard plugin/*/fuzz.go)) # plugin/cache/
+PLUGINS:=$(foreach f,$(FUZZ),$(subst plugin, ,$(f:/=))) # > /cache
+PLUGINS:=$(foreach f,$(PLUGINS),$(subst /, ,$(f))) # > cache
+
+.PHONY: echo
+echo:
+ @echo fuzz targets: $(PLUGINS)
+
+.PHONY: $(PLUGINS)
+$(PLUGINS): echo
+ go-fuzz-build $(REPO)/$(@)
+ go-fuzz -bin=./$(@)-fuzz.zip -workdir=fuzz/$(@)
+
+.PHONY: clean
+clean:
+ rm *-fuzz.zip
diff --git a/plugin/cache/fuzz.go b/plugin/cache/fuzz.go
new file mode 100644
index 000000000..f989019a7
--- /dev/null
+++ b/plugin/cache/fuzz.go
@@ -0,0 +1,14 @@
+package cache
+
+import (
+ "time"
+
+ "github.com/coredns/coredns/plugin/pkg/fuzz"
+)
+
+// Fuzz fuzzes cache.
+func Fuzz(data []byte) int {
+ c := &Cache{pcap: defaultCap, ncap: defaultCap, pttl: maxTTL, nttl: maxNTTL, prefetch: 0, duration: 1 * time.Minute}
+
+ return fuzz.Do(c, data)
+}
diff --git a/plugin/file/fuzz.go b/plugin/file/fuzz.go
new file mode 100644
index 000000000..6be92fb70
--- /dev/null
+++ b/plugin/file/fuzz.go
@@ -0,0 +1,48 @@
+package file
+
+import (
+ "strings"
+
+ "github.com/coredns/coredns/plugin/pkg/fuzz"
+ "github.com/coredns/coredns/plugin/test"
+)
+
+// Fuzz fuzzes file.
+func Fuzz(data []byte) int {
+ name := "miek.nl."
+ zone, _ := Parse(strings.NewReader(fuzzMiekNL), name, "stdin", 0)
+ f := File{Next: test.ErrorHandler(), Zones: Zones{Z: map[string]*Zone{name: zone}, Names: []string{name}}}
+
+ return fuzz.Do(f, data)
+}
+
+const fuzzMiekNL = `
+$TTL 30M
+$ORIGIN miek.nl.
+@ IN SOA linode.atoom.net. miek.miek.nl. (
+ 1282630057 ; Serial
+ 4H ; Refresh
+ 1H ; Retry
+ 7D ; Expire
+ 4H ) ; Negative Cache TTL
+ IN NS linode.atoom.net.
+ IN NS ns-ext.nlnetlabs.nl.
+ IN NS omval.tednet.nl.
+ IN NS ext.ns.whyscream.net.
+
+ IN MX 1 aspmx.l.google.com.
+ IN MX 5 alt1.aspmx.l.google.com.
+ IN MX 5 alt2.aspmx.l.google.com.
+ IN MX 10 aspmx2.googlemail.com.
+ IN MX 10 aspmx3.googlemail.com.
+
+ IN A 139.162.196.78
+ IN AAAA 2a01:7e00::f03c:91ff:fef1:6735
+
+a IN A 139.162.196.78
+ IN AAAA 2a01:7e00::f03c:91ff:fef1:6735
+www IN CNAME a
+archive IN CNAME a
+
+srv IN SRV 10 10 8080 a.miek.nl.
+mx IN MX 10 a.miek.nl.`
diff --git a/plugin/pkg/fuzz/do.go b/plugin/pkg/fuzz/do.go
new file mode 100644
index 000000000..920e8feb7
--- /dev/null
+++ b/plugin/pkg/fuzz/do.go
@@ -0,0 +1,25 @@
+package fuzz
+
+import (
+ "github.com/coredns/coredns/plugin"
+ "github.com/coredns/coredns/plugin/test"
+
+ "github.com/miekg/dns"
+ "golang.org/x/net/context"
+)
+
+// Do will fuzz p - used by gofuzz. See Maefile.fuzz for comments and context.
+func Do(p plugin.Handler, data []byte) int {
+ ctx := context.TODO()
+ ret := 1
+ r := new(dns.Msg)
+ if err := r.Unpack(data); err != nil {
+ ret = 0
+ }
+
+ if _, err := p.ServeDNS(ctx, &test.ResponseWriter{}, r); err != nil {
+ ret = 1
+ }
+
+ return ret
+}
diff --git a/plugin/proxy/fuzz.go b/plugin/proxy/fuzz.go
new file mode 100644
index 000000000..f13ed75bc
--- /dev/null
+++ b/plugin/proxy/fuzz.go
@@ -0,0 +1,19 @@
+package proxy
+
+import (
+ "github.com/coredns/coredns/plugin/pkg/fuzz"
+
+ "github.com/mholt/caddy"
+)
+
+// Fuzz fuzzes proxy.
+func Fuzz(data []byte) int {
+ c := caddy.NewTestController("dns", "proxy . 8.8.8.8:53")
+ up, err := NewStaticUpstreams(&c.Dispenser)
+ if err != nil {
+ return 0
+ }
+ p := &Proxy{Upstreams: &up}
+
+ return fuzz.Do(p, data)
+}
diff --git a/plugin/rewrite/fuzz.go b/plugin/rewrite/fuzz.go
new file mode 100644
index 000000000..c268964e3
--- /dev/null
+++ b/plugin/rewrite/fuzz.go
@@ -0,0 +1,19 @@
+package rewrite
+
+import (
+ "github.com/coredns/coredns/plugin/pkg/fuzz"
+
+ "github.com/mholt/caddy"
+)
+
+// Fuzz fuzzes rewrite.
+func Fuzz(data []byte) int {
+ c := caddy.NewTestController("dns", "rewrite edns0 subnet set 24 56")
+ rules, err := rewriteParse(c)
+ if err != nil {
+ return 0
+ }
+ r := Rewrite{Rules: rules}
+
+ return fuzz.Do(r, data)
+}