switching to plugins

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2018-06-06 10:42:39 -04:00
parent 339b6145f9
commit 0f0ce2af37
Signed by: vbatts
GPG key ID: 10937E57733F1362
5 changed files with 70 additions and 42 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
fuzz-walker
plugin-*.so
*~

View file

@ -1,10 +1,19 @@
GO := go
GO ?= go
SOURCE_FILES := \
$(wildcard *.go) \
$(wildcard walker/*.go) \
$(wildcard walker/walkers/*/*.go)
PLUGINS := \
plugin-readFuzz.so
default: fuzz-walker
default: fuzz-walker $(PLUGINS)
fuzz-walker: $(wildcard *.go)
$(GO) build -o $@ .
plugin-%.so:
$(GO) build -o $@ -buildmode=plugin ./walker/walkers/$*/
clean:
rm -f fuzz-walker
rm -f fuzz-walker *.so

37
main.go
View file

@ -2,8 +2,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
@ -14,13 +12,7 @@ import (
)
var (
dirs = []string{"/sys", "/proc"}
fuzzes = map[string]walker.FuzzFunc{
"chmod": chmodFuzz,
"read": readFuzz,
"writeBytes": writeBytesFuzz,
"writeNum": writeNumFuzz,
}
dirs = []string{"/sys", "/proc"}
fuzzTimeout = 500 * time.Millisecond
walkerPlugins []walker.Fuzzer
)
@ -62,9 +54,9 @@ func main() {
if !info.Mode().IsRegular() {
return nil
}
for fname, fuzz := range fuzzes {
if err := fuzz(path, info, fuzzTimeout); err != nil {
logrus.Warnf("%s: %q fuzz failed with: %v", path, fname, err)
for _, wp := range walkerPlugins {
if err := wp.Func(path, info, fuzzTimeout); err != nil {
logrus.Warnf("%s: %q fuzz failed with: %v", path, wp.Name(), err)
}
}
return nil
@ -83,27 +75,6 @@ func main() {
app.Run(os.Args)
}
func readFuzz(path string, info os.FileInfo, timeout time.Duration) error {
c1 := make(chan error, 1)
go func() {
fd, err := os.Open(path)
if err != nil {
c1 <- err
}
defer fd.Close()
_, err = io.Copy(ioutil.Discard, fd)
c1 <- err
}()
select {
case err := <-c1:
return err
case <-time.After(timeout):
return fmt.Errorf("timeout reached")
}
return nil
}
func writeNumFuzz(path string, info os.FileInfo, timeout time.Duration) error {
return nil
}

View file

@ -13,21 +13,24 @@ func LoadPlugin(path string) (Fuzzer, error) {
if err != nil {
return nil, err
}
sym, err := p.Lookup("MyFuzzerFunc")
sym, err := p.Lookup("NewFuzzer")
if err != nil {
return nil, err
return nil, fmt.Errorf("LoadPlugin: %q", err)
}
mff, ok := sym.(Fuzzer)
nf, ok := sym.(func() (Fuzzer, error))
if !ok {
return nil, fmt.Errorf("plugin at %q did not provide a FuzzFunc named MyFuzzerFunc", path)
return nil, fmt.Errorf("LoadPlugin: plugin at %q did not provide a NewFuzzer function", path)
}
return mff, nil
return nf()
}
// NewFuzzer initializes a new Fuzzer
type NewFuzzer func() (Fuzzer, error)
// Fuzzer is a struct that produces a FuzzFunc
type Fuzzer interface {
Name() string
Func() FuzzFunc
Func(path string, info os.FileInfo, timeout time.Duration) error
}
// FuzzFunc is like filepath.WalkerFunc, but for poking at `path`

View file

@ -0,0 +1,42 @@
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"time"
"git.thisco.de/vbatts/fuzz-walker/walker"
)
func NewFuzzer() (walker.Fuzzer, error) {
return &myFuzzer{}, nil
}
type myFuzzer struct{}
func (mf myFuzzer) Name() string {
return "readFuzz"
}
func (mf myFuzzer) Func(path string, info os.FileInfo, timeout time.Duration) error {
c1 := make(chan error, 1)
go func() {
fd, err := os.Open(path)
if err != nil {
c1 <- err
}
defer fd.Close()
_, err = io.Copy(ioutil.Discard, fd)
c1 <- err
}()
select {
case err := <-c1:
return err
case <-time.After(timeout):
return fmt.Errorf("timeout reached")
}
return nil
}