-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc.go
333 lines (301 loc) · 9.99 KB
/
rpc.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2018 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 main
import (
"fmt"
"math/rand"
"net"
"sync"
"time"
"github.com/google/syzkaller/pkg/cover"
"github.com/google/syzkaller/pkg/log"
"github.com/google/syzkaller/pkg/rpctype"
"github.com/google/syzkaller/pkg/signal"
"github.com/google/syzkaller/prog"
"github.com/google/syzkaller/sys"
)
type RPCServer struct {
mgr RPCManagerView
target *prog.Target
enabledSyscalls []int
stats *Stats
sandbox string
batchSize int
mu sync.Mutex
fuzzers map[string]*Fuzzer
checkResult *rpctype.CheckArgs
maxSignal signal.Signal
corpusSignal signal.Signal
corpusCover cover.Cover
rotator *prog.Rotator
rnd *rand.Rand
}
type Fuzzer struct {
name string
inputs []rpctype.RPCInput
newMaxSignal signal.Signal
rotatedSignal signal.Signal
}
type BugFrames struct {
memoryLeaks []string
dataRaces []string
}
// RPCManagerView restricts interface between RPCServer and Manager.
type RPCManagerView interface {
fuzzerConnect() ([]rpctype.RPCInput, BugFrames)
machineChecked(result *rpctype.CheckArgs)
newInput(inp rpctype.RPCInput, sign signal.Signal) bool
candidateBatch(size int) []rpctype.RPCCandidate
rotateCorpus() bool
saveSnapshot(vmName string)
loadSnapshot(vmName string)
checkIsSaveSnapDone(vmName string) bool
checkEnableKasper(vmName string) bool
getFuzzingTime() uint64
}
func startRPCServer(mgr *Manager) (int, error) {
serv := &RPCServer{
mgr: mgr,
target: mgr.target,
enabledSyscalls: mgr.enabledSyscalls,
stats: mgr.stats,
sandbox: mgr.cfg.Sandbox,
fuzzers: make(map[string]*Fuzzer),
rnd: rand.New(rand.NewSource(time.Now().UnixNano())),
}
serv.batchSize = 5
if serv.batchSize < mgr.cfg.Procs {
serv.batchSize = mgr.cfg.Procs
}
s, err := rpctype.NewRPCServer(mgr.cfg.RPC, "Manager", serv)
if err != nil {
return 0, err
}
log.Logf(0, "serving rpc on tcp://%v", s.Addr())
port := s.Addr().(*net.TCPAddr).Port
go s.Serve()
return port, nil
}
func (serv *RPCServer) Connect(a *rpctype.ConnectArgs, r *rpctype.ConnectRes) error {
log.Logf(1, "fuzzer %v connected", a.Name)
serv.stats.vmRestarts.inc()
corpus, bugFrames := serv.mgr.fuzzerConnect()
serv.mu.Lock()
defer serv.mu.Unlock()
f := &Fuzzer{
name: a.Name,
}
serv.fuzzers[a.Name] = f
r.MemoryLeakFrames = bugFrames.memoryLeaks
r.DataRaceFrames = bugFrames.dataRaces
r.EnabledCalls = serv.enabledSyscalls
r.GitRevision = sys.GitRevision
r.TargetRevision = serv.target.Revision
// TODO: temporary disabled b/c we suspect this negatively affects fuzzing.
if false && serv.mgr.rotateCorpus() && serv.rnd.Intn(3) != 0 {
// We do rotation every other time because there are no objective
// proofs regarding its efficiency either way.
// Also, rotation gives significantly skewed syscall selection
// (run prog.TestRotationCoverage), it may or may not be OK.
r.CheckResult = serv.rotateCorpus(f, corpus)
} else {
r.CheckResult = serv.checkResult
f.inputs = corpus
f.newMaxSignal = serv.maxSignal.Copy()
}
return nil
}
func (serv *RPCServer) rotateCorpus(f *Fuzzer, corpus []rpctype.RPCInput) *rpctype.CheckArgs {
// Fuzzing tends to stuck in some local optimum and then it fails to cover
// other state space points since code coverage is only a very approximate
// measure of logic coverage. To overcome this we introduce some variation
// into the process which should cause steady corpus rotation over time
// (the same coverage is achieved in different ways).
//
// First, we select a subset of all syscalls for each VM run (result.EnabledCalls).
// This serves 2 goals: (1) target fuzzer at a particular area of state space,
// (2) disable syscalls that cause frequent crashes at least in some runs
// to allow it to do actual fuzzing.
//
// Then, we remove programs that contain disabled syscalls from corpus
// that will be sent to the VM (f.inputs). We also remove 10% of remaining
// programs at random to allow to rediscover different variations of these programs.
//
// Then, we drop signal provided by the removed programs and also 10%
// of the remaining signal at random (f.newMaxSignal). This again allows
// rediscovery of this signal by different programs.
//
// Finally, we adjust criteria for accepting new programs from this VM (f.rotatedSignal).
// This allows to accept rediscovered varied programs even if they don't
// increase overall coverage. As the result we have multiple programs
// providing the same duplicate coverage, these are removed during periodic
// corpus minimization process. The minimization process is specifically
// non-deterministic to allow the corpus rotation.
//
// Note: at no point we drop anything globally and permanently.
// Everything we remove during this process is temporal and specific to a single VM.
calls := serv.rotator.Select()
var callIDs []int
callNames := make(map[string]bool)
for call := range calls {
callNames[call.Name] = true
callIDs = append(callIDs, call.ID)
}
f.inputs, f.newMaxSignal = serv.selectInputs(callNames, corpus, serv.maxSignal)
// Remove the corresponding signal from rotatedSignal which will
// be used to accept new inputs from this manager.
f.rotatedSignal = serv.corpusSignal.Intersection(f.newMaxSignal)
result := *serv.checkResult
result.EnabledCalls = map[string][]int{serv.sandbox: callIDs}
return &result
}
func (serv *RPCServer) selectInputs(enabled map[string]bool, inputs0 []rpctype.RPCInput, signal0 signal.Signal) (
inputs []rpctype.RPCInput, signal signal.Signal) {
signal = signal0.Copy()
for _, inp := range inputs0 {
calls, err := prog.CallSet(inp.Prog)
if err != nil {
panic(fmt.Sprintf("rotateInputs: CallSet failed: %v\n%s", err, inp.Prog))
}
for call := range calls {
if !enabled[call] {
goto drop
}
}
if serv.rnd.Float64() > 0.9 {
goto drop
}
inputs = append(inputs, inp)
continue
drop:
for _, sig := range inp.Signal.Elems {
delete(signal, sig)
}
}
signal.Split(len(signal) / 10)
return inputs, signal
}
func (serv *RPCServer) Check(a *rpctype.CheckArgs, r *int) error {
serv.mu.Lock()
defer serv.mu.Unlock()
if serv.checkResult != nil {
return nil
}
serv.mgr.machineChecked(a)
a.DisabledCalls = nil
serv.checkResult = a
calls := make(map[*prog.Syscall]bool)
for _, call := range a.EnabledCalls[serv.sandbox] {
calls[serv.target.Syscalls[call]] = true
}
serv.rotator = prog.MakeRotator(serv.target, calls, serv.rnd)
return nil
}
func (serv *RPCServer) SaveSnapshot(a *rpctype.NameArg, r *int) error {
serv.mgr.saveSnapshot(a.Name)
return nil
}
func (serv *RPCServer) LoadSnapshot(a *rpctype.NameArg, r *int) error {
serv.mgr.loadSnapshot(a.Name)
return nil
}
func (serv *RPCServer) CheckIsSaveSnapDone(a *rpctype.NameArg, r *rpctype.BoolRes) error {
r.B = serv.mgr.checkIsSaveSnapDone(a.Name)
return nil
}
func (serv *RPCServer) CheckEnableKasper(a *rpctype.NameArg, r *rpctype.BoolRes) error {
r.B = serv.mgr.checkEnableKasper(a.Name)
return nil
}
func (serv *RPCServer) NewInput(a *rpctype.NewInputArgs, r *int) error {
inputSignal := a.Signal.Deserialize()
log.Logf(4, "new input from %v for syscall %v (signal=%v, cover=%v)",
a.Name, a.Call, inputSignal.Len(), len(a.Cover))
if _, err := serv.target.Deserialize(a.RPCInput.Prog, prog.NonStrict); err != nil {
// This should not happen, but we see such cases episodically, reason unknown.
log.Logf(0, "failed to deserialize program from fuzzer: %v\n%s", err, a.RPCInput.Prog)
return nil
}
serv.mu.Lock()
defer serv.mu.Unlock()
f := serv.fuzzers[a.Name]
genuine := !serv.corpusSignal.Diff(inputSignal).Empty()
rotated := false
if !genuine && f.rotatedSignal != nil {
rotated = !f.rotatedSignal.Diff(inputSignal).Empty()
}
if !genuine && !rotated {
return nil
}
if !serv.mgr.newInput(a.RPCInput, inputSignal) {
return nil
}
if f.rotatedSignal != nil {
f.rotatedSignal.Merge(inputSignal)
}
serv.corpusCover.Merge(a.Cover)
serv.stats.corpusCover.set(len(serv.corpusCover))
serv.stats.newInputs.inc()
if rotated {
serv.stats.rotatedInputs.inc()
}
if genuine {
serv.corpusSignal.Merge(inputSignal)
serv.stats.corpusSignal.set(serv.corpusSignal.Len())
a.RPCInput.Cover = nil // Don't send coverage back to all fuzzers.
for _, other := range serv.fuzzers {
if other == f {
continue
}
other.inputs = append(other.inputs, a.RPCInput)
}
}
return nil
}
func (serv *RPCServer) Poll(a *rpctype.PollArgs, r *rpctype.PollRes) error {
serv.stats.mergeNamed(a.Stats)
serv.mu.Lock()
defer serv.mu.Unlock()
f := serv.fuzzers[a.Name]
if f == nil {
log.Fatalf("fuzzer %v is not connected", a.Name)
}
newMaxSignal := serv.maxSignal.Diff(a.MaxSignal.Deserialize())
if !newMaxSignal.Empty() {
serv.maxSignal.Merge(newMaxSignal)
for _, f1 := range serv.fuzzers {
if f1 == f {
continue
}
f1.newMaxSignal.Merge(newMaxSignal)
}
}
r.MaxSignal = f.newMaxSignal.Split(500).Serialize()
if a.NeedCandidates {
r.Candidates = serv.mgr.candidateBatch(serv.batchSize)
}
if len(r.Candidates) == 0 {
batchSize := serv.batchSize
// When the fuzzer starts, it pumps the whole corpus.
// If we do it using the final batchSize, it can be very slow
// (batch of size 6 can take more than 10 mins for 50K corpus and slow kernel).
// So use a larger batch initially (we use no stats as approximation of initial pump).
const initialBatch = 30
if len(a.Stats) == 0 && batchSize < initialBatch {
batchSize = initialBatch
}
for i := 0; i < batchSize && len(f.inputs) > 0; i++ {
last := len(f.inputs) - 1
r.NewInputs = append(r.NewInputs, f.inputs[last])
f.inputs[last] = rpctype.RPCInput{}
f.inputs = f.inputs[:last]
}
if len(f.inputs) == 0 {
f.inputs = nil
}
}
r.Fuzzing = serv.mgr.getFuzzingTime()
log.Logf(4, "poll from %v: candidates=%v inputs=%v maxsignal=%v",
a.Name, len(r.Candidates), len(r.NewInputs), len(r.MaxSignal.Elems))
return nil
}