forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgrconfig.go
284 lines (256 loc) · 8.65 KB
/
mgrconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2015 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package mgrconfig
import (
"encoding/json"
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/google/syzkaller/pkg/config"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/prog"
_ "github.com/google/syzkaller/sys" // most mgrconfig users want targets too
"github.com/google/syzkaller/vm"
)
type Config struct {
Name string // Instance name (used for identification and as GCE instance prefix)
Target string // Target OS/arch, e.g. "linux/arm64" or "linux/amd64/386" (amd64 OS with 386 test process)
HTTP string // TCP address to serve HTTP stats page (e.g. "localhost:50000")
RPC string // TCP address to serve RPC for fuzzer processes (optional)
Workdir string
Vmlinux string
Kernel_Src string // kernel source directory
Tag string // arbitrary optional tag that is saved along with crash reports (e.g. branch/commit)
Image string // linux image for VMs
SSHKey string // ssh key for the image (may be empty for some VM types)
SSH_User string // ssh user ("root" by default)
Hub_Client string
Hub_Addr string
Hub_Key string
Email_Addrs []string // syz-manager will send crash emails to this list of emails using mailx (optional)
Dashboard_Client string
Dashboard_Addr string
Dashboard_Key string
Syzkaller string // path to syzkaller checkout (syz-manager will look for binaries in bin subdir)
Procs int // number of parallel processes inside of every VM
Sandbox string // type of sandbox to use during fuzzing:
// "none": don't do anything special (has false positives, e.g. due to killing init)
// "setuid": impersonate into user nobody (65534), default
// "namespace": create a new namespace for fuzzer using CLONE_NEWNS/CLONE_NEWNET/CLONE_NEWPID/etc,
// requires building kernel with CONFIG_NAMESPACES, CONFIG_UTS_NS, CONFIG_USER_NS, CONFIG_PID_NS and CONFIG_NET_NS.
Cover bool // use kcov coverage (default: true)
Leak bool // do memory leak checking
Reproduce bool // reproduce, localize and minimize crashers (on by default)
Enable_Syscalls []string
Disable_Syscalls []string
Suppressions []string // don't save reports matching these regexps, but reboot VM after them
Ignores []string // completely ignore reports matching these regexps (don't save nor reboot)
Type string // VM type (qemu, kvm, local)
VM json.RawMessage // VM-type-specific config
// Implementation details beyond this point.
ParsedSuppressions []*regexp.Regexp `json:"-"`
ParsedIgnores []*regexp.Regexp `json:"-"`
// Parsed Target:
TargetOS string `json:"-"`
TargetArch string `json:"-"`
TargetVMArch string `json:"-"`
// Syzkaller binaries that we are going to use:
SyzFuzzerBin string `json:"-"`
SyzExecprogBin string `json:"-"`
SyzExecutorBin string `json:"-"`
}
func LoadData(data []byte) (*Config, error) {
return load(data, "")
}
func LoadFile(filename string) (*Config, error) {
return load(nil, filename)
}
func DefaultValues() *Config {
return &Config{
SSH_User: "root",
Cover: true,
Reproduce: true,
Sandbox: "setuid",
RPC: ":0",
Procs: 1,
}
}
func load(data []byte, filename string) (*Config, error) {
cfg := DefaultValues()
if data != nil {
if err := config.LoadData(data, cfg); err != nil {
return nil, err
}
} else {
if err := config.LoadFile(filename, cfg); err != nil {
return nil, err
}
}
var err error
cfg.TargetOS, cfg.TargetVMArch, cfg.TargetArch, err = SplitTarget(cfg.Target)
if err != nil {
return nil, err
}
targetBin := func(name, arch string) string {
exe := ""
if cfg.TargetOS == "windows" {
exe = ".exe"
}
return filepath.Join(cfg.Syzkaller, "bin", cfg.TargetOS+"_"+arch, name+exe)
}
cfg.SyzFuzzerBin = targetBin("syz-fuzzer", cfg.TargetVMArch)
cfg.SyzExecprogBin = targetBin("syz-execprog", cfg.TargetVMArch)
cfg.SyzExecutorBin = targetBin("syz-executor", cfg.TargetArch)
if !osutil.IsExist(cfg.SyzFuzzerBin) {
return nil, fmt.Errorf("bad config syzkaller param: can't find %v", cfg.SyzFuzzerBin)
}
if !osutil.IsExist(cfg.SyzExecprogBin) {
return nil, fmt.Errorf("bad config syzkaller param: can't find %v", cfg.SyzExecprogBin)
}
if !osutil.IsExist(cfg.SyzExecutorBin) {
return nil, fmt.Errorf("bad config syzkaller param: can't find %v", cfg.SyzExecutorBin)
}
if cfg.HTTP == "" {
return nil, fmt.Errorf("config param http is empty")
}
if cfg.Workdir == "" {
return nil, fmt.Errorf("config param workdir is empty")
}
if cfg.Type == "" {
return nil, fmt.Errorf("config param type is empty")
}
if cfg.Procs < 1 || cfg.Procs > 32 {
return nil, fmt.Errorf("bad config param procs: '%v', want [1, 32]", cfg.Procs)
}
switch cfg.Sandbox {
case "none", "setuid", "namespace":
default:
return nil, fmt.Errorf("config param sandbox must contain one of none/setuid/namespace")
}
cfg.Workdir = osutil.Abs(cfg.Workdir)
cfg.Vmlinux = osutil.Abs(cfg.Vmlinux)
cfg.Syzkaller = osutil.Abs(cfg.Syzkaller)
if cfg.Kernel_Src == "" {
cfg.Kernel_Src = filepath.Dir(cfg.Vmlinux) // assume in-tree build by default
}
if err := parseSuppressions(cfg); err != nil {
return nil, err
}
if cfg.Hub_Client != "" && (cfg.Name == "" || cfg.Hub_Addr == "" || cfg.Hub_Key == "") {
return nil, fmt.Errorf("hub_client is set, but name/hub_addr/hub_key is empty")
}
if cfg.Dashboard_Client != "" && (cfg.Name == "" ||
cfg.Dashboard_Addr == "" ||
cfg.Dashboard_Key == "") {
return nil, fmt.Errorf("dashboard_client is set, but name/dashboard_addr/dashboard_key is empty")
}
return cfg, nil
}
func SplitTarget(target string) (string, string, string, error) {
if target == "" {
return "", "", "", fmt.Errorf("target is empty")
}
targetParts := strings.Split(target, "/")
if len(targetParts) != 2 && len(targetParts) != 3 {
return "", "", "", fmt.Errorf("bad config param target")
}
os := targetParts[0]
vmarch := targetParts[1]
arch := targetParts[1]
if len(targetParts) == 3 {
arch = targetParts[2]
}
return os, vmarch, arch, nil
}
func ParseEnabledSyscalls(target *prog.Target, enabled, disabled []string) (map[int]bool, error) {
match := func(call *prog.Syscall, str string) bool {
if str == call.CallName || str == call.Name {
return true
}
if len(str) > 1 && str[len(str)-1] == '*' && strings.HasPrefix(call.Name, str[:len(str)-1]) {
return true
}
return false
}
syscalls := make(map[int]bool)
if len(enabled) != 0 {
for _, c := range enabled {
n := 0
for _, call := range target.Syscalls {
if match(call, c) {
syscalls[call.ID] = true
n++
}
}
if n == 0 {
return nil, fmt.Errorf("unknown enabled syscall: %v", c)
}
}
} else {
for _, call := range target.Syscalls {
syscalls[call.ID] = true
}
}
for _, c := range disabled {
n := 0
for _, call := range target.Syscalls {
if match(call, c) {
delete(syscalls, call.ID)
n++
}
}
if n == 0 {
return nil, fmt.Errorf("unknown disabled syscall: %v", c)
}
}
return syscalls, nil
}
func parseSuppressions(cfg *Config) error {
// Add some builtin suppressions.
// TODO(dvyukov): this should be moved to pkg/report.
supp := append(cfg.Suppressions, []string{
"panic: failed to start executor binary",
"panic: executor failed: pthread_create failed",
"panic: failed to create temp dir",
"fatal error: runtime: out of memory",
"fatal error: runtime: cannot allocate memory",
"fatal error: unexpected signal during runtime execution", // presubmably OOM turned into SIGBUS
"signal SIGBUS: bus error", // presubmably OOM turned into SIGBUS
"Out of memory: Kill process .* \\(syz-fuzzer\\)",
"Out of memory: Kill process .* \\(sshd\\)",
"Killed process .* \\(syz-fuzzer\\)",
"Killed process .* \\(sshd\\)",
"lowmemorykiller: Killing 'syz-fuzzer'",
"lowmemorykiller: Killing 'sshd'",
"INIT: PANIC: segmentation violation!",
}...)
for _, s := range supp {
re, err := regexp.Compile(s)
if err != nil {
return fmt.Errorf("failed to compile suppression '%v': %v", s, err)
}
cfg.ParsedSuppressions = append(cfg.ParsedSuppressions, re)
}
for _, ignore := range cfg.Ignores {
re, err := regexp.Compile(ignore)
if err != nil {
return fmt.Errorf("failed to compile ignore '%v': %v", ignore, err)
}
cfg.ParsedIgnores = append(cfg.ParsedIgnores, re)
}
return nil
}
func CreateVMEnv(cfg *Config, debug bool) *vm.Env {
return &vm.Env{
Name: cfg.Name,
OS: cfg.TargetOS,
Arch: cfg.TargetVMArch,
Workdir: cfg.Workdir,
Image: cfg.Image,
SSHKey: cfg.SSHKey,
SSHUser: cfg.SSH_User,
Debug: debug,
Config: cfg.VM,
}
}