This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
libfuzzer.go
336 lines (297 loc) · 7.62 KB
/
libfuzzer.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
334
335
336
package client
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
func libFuzzerExitCodeToStatus(exitCode int) string {
status := "pass"
switch exitCode {
case libFuzzerTimeoutExitCode:
status = "timeout"
case libFuzzerCrashExitCode:
status = "crash"
case libFuzzerLeakExitCode:
status = "crash"
case libFuzzerOOMExitCode:
status = "oom"
case libFuzzerSuccessExitCode:
status = "pass"
default:
status = "failed"
}
return status
}
func (c *FuzzitClient) runlibFuzzerMerge() error {
isEmpty, err := IsDirEmpty("corpus")
if err != nil {
return err
}
if isEmpty {
log.Println("nothing to merge. skipping...")
return nil
}
// this directory should not exist but do to some old bugs it might exist in the corpus, so we delete it
if _, err := os.Stat("merge"); err == nil {
if err = os.RemoveAll("merge"); err != nil {
return err
}
}
if err := os.Mkdir("merge", 0644); err != nil {
return err
}
if _, err := os.Stat("/tmp/merge_control.txt"); err == nil {
if err = os.Remove("/tmp/merge_control.txt"); err != nil {
return err
}
}
args := []string{
"-print_final_stats=1",
"-exact_artifact_path=./artifact",
fmt.Sprintf("-error_exitcode=%d", libFuzzerTimeoutExitCode),
"-merge_control_file=/tmp/merge_control.txt",
"-merge=1",
"merge",
"corpus",
}
log.Println("Running merge with: ./fuzzer " + strings.Join(args, " "))
cmd := exec.Command("./fuzzer",
args...)
if err := appendPrefixToCmd(cmd); err != nil {
return err
}
if err := cmd.Run(); err != nil {
return err
}
if err := c.refreshToken(); err != nil {
return err
}
if err := os.RemoveAll("corpus"); err != nil {
return err
}
if err := os.Rename("merge", "corpus"); err != nil {
return err
}
if err := c.archiveAndUpload("corpus",
fmt.Sprintf("orgs/%s/targets/%s/corpus.tar.gz", c.Org, c.currentJob.TargetId),
"corpus.tar.gz"); err != nil {
return err
}
return nil
}
func (c *FuzzitClient) uploadCrash(exitCode int) error {
ctx := context.Background()
if !c.updateDB {
return nil
}
if _, err := os.Stat("artifact"); err == nil {
colRef := c.firestoreClient.Collection(fmt.Sprintf("orgs/%s/targets/%s/jobs/%s/crashes", c.Org, c.currentJob.TargetId, c.jobId))
crashRef := colRef.NewDoc()
log.Printf("uploading crash...")
if err = c.uploadFile("artifact",
fmt.Sprintf("orgs/%s/targets/%s/jobs/%s/crashes/%s", c.Org, c.currentJob.TargetId, c.jobId, crashRef.ID),
fmt.Sprintf("crash-%s", c.currentJob.TargetId)); err != nil {
return err
}
_, err = crashRef.Set(ctx, crash{
TargetName: c.currentJob.TargetId,
JobId: c.jobId,
TargetId: c.currentJob.TargetId,
OrgId: c.Org,
ExitCode: uint32(exitCode),
Type: "crash",
LastLines: strings.Join(lastLines, "\n"),
V2: true,
})
if err != nil {
return err
}
}
return nil
}
func (c *FuzzitClient) runLibFuzzerFuzzing() error {
ctx := context.Background()
args := append(
[]string{
"-print_final_stats=1",
"-exact_artifact_path=./artifact",
"-error_exitcode=76",
"-max_total_time=3600",
"corpus",
"additional-corpus",
"seed",
},
)
if c.currentJob.Args != "" {
args = append(args, splitAndRemoveEmpty(c.currentJob.Args, " ")...)
}
var err error
err = nil
var exitCode int
for err == nil {
log.Println("Running fuzzing with: ./fuzzer " + strings.Join(args, " "))
cmd := exec.Command("./fuzzer",
args...)
if err := appendPrefixToCmd(cmd); err != nil {
return err
}
err = cmd.Start()
// Use a channel to signal completion so we can use a select statement
done := make(chan error)
go func() { done <- cmd.Wait() }()
timeout := time.After(60 * time.Second)
stopSession := false
for !stopSession {
select {
case <-timeout:
var fuzzingJob Job
if err := c.refreshToken(); err != nil {
return err
}
docRef := c.firestoreClient.Doc(fmt.Sprintf("orgs/%s/targets/%s/jobs/%s", c.Org, c.currentJob.TargetId, c.jobId))
if docRef == nil {
return fmt.Errorf("invalid resource")
}
docsnap, err := docRef.Get(ctx)
if err != nil {
return err
}
err = docsnap.DataTo(&fuzzingJob)
if err != nil {
return err
}
if fuzzingJob.Status == "in progress" {
timeout = time.After(60 * time.Second)
} else {
log.Println("job was cancel by user. exiting...")
cmd.Process.Kill()
return nil
}
case err = <-done:
stopSession = true
if err != nil {
log.Printf("process finished with error = %v\n", err)
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitCode = status.ExitStatus()
log.Printf("Exit Status: %d", status.ExitStatus())
}
} else {
return err
}
} else {
if err = c.runlibFuzzerMerge(); err != nil {
return err
}
log.Print("process finished successfully")
}
}
}
}
if err := c.refreshToken(); err != nil {
return err
}
err = c.uploadCrash(exitCode)
if err != nil {
return err
}
err = c.transitionStatus(libFuzzerExitCodeToStatus(exitCode))
if err != nil {
return err
}
return nil
}
func (c *FuzzitClient) runLibFuzzerRegression() error {
var corpusFiles []string
var seedFiles []string
corpusFiles, err := listFiles("corpus")
if err != nil {
return err
}
additionalFiles, err := listFiles("additional-corpus")
if err != nil {
return err
}
seedFiles, err = listFiles("seed")
if err != nil {
return err
}
regressionFiles := append(corpusFiles, seedFiles...)
regressionFiles = append(regressionFiles, additionalFiles...)
if len(regressionFiles) == 0 {
log.Println("no files in corpus and seed. skipping run")
c.transitionStatus("pass")
return nil
}
args := append(
[]string{
"-print_final_stats=1",
"-exact_artifact_path=./artifact",
"-error_exitcode=76",
},
regressionFiles...,
)
if c.currentJob.Args != "" {
args = append(args, splitAndRemoveEmpty(c.currentJob.Args, " ")...)
}
log.Println("Running regression...")
cmd := exec.Command("./fuzzer",
args...)
if err := appendPrefixToCmd(cmd); err != nil {
return err
}
exitCode := 0
if err := cmd.Run(); err != nil {
if !c.updateDB {
// if this is local regression we want to exit with error code so the ci can fail
return err
}
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitCode = status.ExitStatus()
log.Printf("Exit Status: %d", exitCode)
}
} else {
return err
}
}
if err := c.uploadCrash(exitCode); err != nil {
return err
}
err = c.transitionStatus(libFuzzerExitCodeToStatus(exitCode))
if err != nil {
return err
}
return nil
}
func (c *FuzzitClient) runLibFuzzer() error {
if _, err := os.Stat("fuzzer"); os.IsNotExist(err) {
c.transitionStatus("failed")
return fmt.Errorf("fuzzer executable doesnt exist")
}
if err := os.Chmod("./fuzzer", 0770); err != nil {
return err
}
var err error
if c.currentJob.Type == "regression" {
err = c.runLibFuzzerRegression()
} else {
err = c.runLibFuzzerFuzzing()
}
return err
}