-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqemuer.go
More file actions
381 lines (357 loc) · 9.42 KB
/
qemuer.go
File metadata and controls
381 lines (357 loc) · 9.42 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// A script for fuzzing the kernel in QEMU with Trinity and KASAN.
// Author: Dmitry Vyukov
// go run qemuer.go -ninst=4 -maxcpu=4 --timeout=30m \
// -diskimage=/disk/image/with/linux \
// -id_rsa=/id_rsa/file/for/ssh \
// -bzimage=/kernel/src/arch/x86/boot/bzImage \
// -binary=/src/trinity/trinity \
// -args="--dangerous -q -m -C 32"
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"strconv"
"sync"
"time"
)
var (
diskimage = flag.String("diskimage", "", "path to linux disk image (required)")
bzimage = flag.String("bzimage", "", "path to kernel image, e.g. arch/x86/boot/bzImage (required)")
qemu = flag.String("qemu", "qemu-system-x86_64", "qemu binary")
port = flag.Int("port", 23505, "use ports [port, port+ninst) for ssh")
id_rsa = flag.String("id_rsa", "", "path to id_rsa file to ssh into the image (required)")
ninst = flag.Int("ninst", 1, "number of instances to use (1-100)")
binary = flag.String("binary", "", "path to binary to execute (required)")
args = flag.String("args", "", "arguments for the binary")
maxcpu = flag.Int("maxcpu", 1, "maximum number of cpus (1-128)")
memmb = flag.Int("memmb", 2048, "per-instance memory, in mb (128-8192)")
timeout = flag.Duration("timeout", 60*time.Minute, "timeout for binary execution")
v = flag.Bool("v", false, "dump instance output to stdout")
images = make(chan string)
)
func main() {
flag.Parse()
if *binary == "" || *diskimage == "" || *bzimage == "" || *id_rsa == "" ||
*ninst <= 0 || *ninst > 100 || *maxcpu <= 0 || *maxcpu > 128 ||
*memmb < 128 || *memmb > 8192 {
flag.PrintDefaults()
os.Exit(1)
}
if *v && *ninst > 1 {
// Intermixed output from several intances is unuseful.
*ninst = 1
}
log.Printf("fuzzing on %v instance(s)", *ninst)
go CopyImageLoop()
for i := 0; i < *ninst; i++ {
go FuzzLoop(i)
time.Sleep(time.Second)
}
select {}
}
func CopyImageLoop() {
for i := 0; ; i++ {
t0 := time.Now()
oldf, err := os.Open(*diskimage)
if err != nil {
log.Fatalf("failed to open original image: %v", err)
}
defer oldf.Close()
image := fmt.Sprintf("asanimage%v", i)
newf, err := os.Create(image)
if err != nil {
log.Fatalf("failed to open new image: %v", err)
}
_, err = io.Copy(newf, oldf)
if err != nil {
log.Fatalf("failed to copy image: %v", err)
}
oldf.Close()
newf.Close()
log.Printf("copied image (%v)", time.Now().Sub(t0))
images <- image
}
}
func FuzzLoop(i int) {
ncpu := i%*maxcpu + 1
for run := 0; ; run++ {
logname := fmt.Sprintf("asan%v-%v-%v.log", i, run, time.Now().Unix())
logf, err := os.Create(logname)
if err != nil {
log.Printf("failed to create log file: %v\n", err)
time.Sleep(10 * time.Second)
continue
}
log := io.Writer(logf)
if *v {
log = io.MultiWriter(log, os.Stdout)
}
inst := &Instance{
name: fmt.Sprintf("asan%v", i),
id: i,
runid: run,
logname: logname,
log: log,
ncpu: ncpu,
cmds: make(map[*Command]bool),
}
inst.Run()
inst.Shutdown()
}
}
type Instance struct {
sync.Mutex
name string
image string
id int
runid int
ncpu int
logname string
log io.Writer
cmds map[*Command]bool
qemu *Command
}
type Command struct {
sync.Mutex
cmd *exec.Cmd
done chan struct{}
failed bool
out []byte
outpos int
inw io.Writer
}
var bootSem = make(chan bool, 1)
func (inst *Instance) Run() bool {
// Obtain brand new image.
bootSem <- true
t0 := time.Now()
inst.image = <-images
// Start the instance.
inst.qemu = inst.CreateCommand(*qemu,
"-hda", inst.image,
"-m", strconv.Itoa(*memmb),
"-smp", strconv.Itoa(inst.ncpu),
"-net", fmt.Sprintf("user,hostfwd=tcp::%v-:22", *port+inst.id),
"-net", "nic",
"-nographic",
"-kernel", *bzimage,
"-append", "console=ttyS0 root=/dev/sda debug earlyprintk=serial slub_debug=UZQ",
"-enable-kvm")
if !inst.qemu.WaitForOut("Starting OpenBSD Secure Shell server: sshd", 5*time.Minute) {
<-bootSem
inst.Logf("failed to start qemu")
return false
}
<-bootSem
inst.Logf("started vm (%v)", time.Now().Sub(t0))
// Run the binary.
t0 = time.Now()
nfailfast := 0
reported := true // not very useful (and broken because of command prompt wait)
deadline := time.Now().Add(*timeout)
for try := 0; ; try++ {
start := time.Now()
conoutlen := inst.qemu.OutLen()
lastout := time.Now()
// Copy the binary into the instance.
inst.CreateSCPCommand(*binary, "/tmp/kasaner_binary").Wait(3 * time.Minute)
cmd := inst.CreateSSHCommand("/tmp/kasaner_binary " + *args)
for {
time.Sleep(time.Second)
// Check for AddressSanitizer reports in output.
if !reported && inst.qemu.CheckOut("KASan") {
reported = true
inst.Logf("found KASan report in %v", inst.logname)
}
if time.Now().After(deadline) {
inst.Logf("test %v interrupted", try)
return true
}
if cmd.Exited() {
t := time.Now().Sub(start)
inst.Logf("test %v finished after %v", try, t)
if t < 20*time.Second {
// If the binary exits too quickly often,
// assume the vm is broken.
nfailfast++
if nfailfast > 3 {
inst.Logf("vm is broken (binary can't start)")
return false
}
} else {
nfailfast = 0
}
break
}
if conoutlen != inst.qemu.OutLen() {
conoutlen = inst.qemu.OutLen()
lastout = time.Now()
} else if lastout.Add(5 * time.Minute).Before(time.Now()) {
// If no output for more than 5 minutes,
// assume the vm is broken.
inst.Logf("vm hang after %v (no output)", time.Now().Sub(start))
return false
}
}
}
}
func (inst *Instance) Shutdown() {
for try := 0; try < 10; try++ {
inst.qemu.cmd.Process.Kill()
time.Sleep(time.Second)
inst.Lock()
n := len(inst.cmds)
inst.Unlock()
if n == 0 {
os.Remove(inst.image)
return
}
}
inst.Logf("hanged processes after kill")
inst.Lock()
for cmd := range inst.cmds {
cmd.cmd.Process.Kill()
}
inst.Unlock()
time.Sleep(3 * time.Second)
os.Remove(inst.image)
}
func (inst *Instance) CreateCommand(args ...string) *Command {
fmt.Fprintf(inst.log, "command %v\n", args)
cmd := &Command{}
cmd.done = make(chan struct{})
outr, outw, err := os.Pipe()
if err != nil {
inst.Logf("failed to create pipe: %v", err)
cmd.failed = true
close(cmd.done)
return cmd
}
inr, inw, err := os.Pipe()
if err != nil {
inst.Logf("failed to create pipe: %v", err)
cmd.failed = true
close(cmd.done)
return cmd
}
cmd.cmd = exec.Command(args[0], args[1:]...)
cmd.cmd.Stdout = io.MultiWriter(inst.log, outw)
cmd.cmd.Stderr = io.MultiWriter(inst.log, outw)
cmd.cmd.Stdin = inr
cmd.inw = inw
go func() {
err := cmd.cmd.Start()
if err == nil {
inst.Lock()
inst.cmds[cmd] = true
inst.Unlock()
err = cmd.cmd.Wait()
inst.Lock()
delete(inst.cmds, cmd)
inst.Unlock()
}
fmt.Fprintf(inst.log, "command %v DONE %v\n", args, err)
inw.Close()
inr.Close()
outw.Close()
outr.Close()
cmd.failed = err != nil
close(cmd.done)
}()
go func() {
var buf [4 << 10]byte
for {
n, err := outr.Read(buf[:])
if err != nil {
return
}
cmd.Lock()
cmd.out = append(cmd.out, buf[:n]...)
cmd.Unlock()
}
}()
return cmd
}
func (inst *Instance) CreateSSHCommand(args ...string) *Command {
args1 := []string{"ssh", "-i", *id_rsa, "-p", strconv.Itoa(*port + inst.id),
"-o", "ConnectionAttempts=10", "-o", "ConnectTimeout=60",
"-o", "BatchMode=yes", "-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no", "root@localhost"}
return inst.CreateCommand(append(args1, args...)...)
}
func (inst *Instance) CreateSCPCommand(from, to string) *Command {
return inst.CreateCommand("scp", "-i", *id_rsa, "-P", strconv.Itoa(*port+inst.id),
"-o", "ConnectionAttempts=10", "-o", "ConnectTimeout=60",
"-o", "BatchMode=yes", "-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
from, "root@localhost:"+to)
}
func (inst *Instance) Logf(str string, args ...interface{}) {
fmt.Fprintf(inst.log, str+"\n", args...)
if !*v {
log.Printf("%v-%v: "+str, append([]interface{}{inst.name, inst.runid}, args...)...)
}
}
func (cmd *Command) Wait(max time.Duration) bool {
select {
case <-cmd.done:
return !cmd.failed
case <-time.After(max):
return false
}
}
func (cmd *Command) Exited() bool {
select {
case <-cmd.done:
return true
default:
return false
}
}
func (cmd *Command) OutLen() int {
cmd.Lock()
defer cmd.Unlock()
return len(cmd.out)
}
func (cmd *Command) CheckOut(what string) bool {
cmd.Lock()
defer cmd.Unlock()
idx := bytes.Index(cmd.out[cmd.outpos:], []byte(what))
if idx == -1 {
newpos := len(cmd.out) - len(what)
if newpos > cmd.outpos {
cmd.outpos = newpos
}
} else {
cmd.outpos += idx + len(what)
}
return idx != -1
}
func (cmd *Command) WaitForOut(what string, max time.Duration) bool {
if cmd.CheckOut(what) {
return true
}
ticker := time.NewTicker(time.Second)
timeout := time.NewTimer(max)
defer func() {
ticker.Stop()
timeout.Stop()
}()
for {
select {
case <-cmd.done:
return cmd.CheckOut(what)
case <-ticker.C:
if cmd.CheckOut(what) {
return true
}
case <-timeout.C:
return false
}
}
}