-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSyzbotGenerator.go
213 lines (186 loc) · 4.73 KB
/
SyzbotGenerator.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
package generators
import (
"encoding/json"
"fmt"
"github.com/andybalholm/cascadia"
"github.com/gorilla/feeds"
"golang.org/x/net/html"
"io/ioutil"
url2 "net/url"
"os"
"sort"
"strings"
"time"
)
type SyzbotCrash struct {
Id string `json:"id"`
Title string `json:"title"`
Repro string `json:"repro"`
BisectStatus string `json:"bisect_status"`
FirstSeen time.Time `json:"first_seen"`
}
type syzbotState struct {
Crashes map[string]SyzbotCrash
}
type SyzbotGenerator struct {
filterFunc ItemFilterFunc
itemModFunc ItemModifierFunc
workdir string
}
func (g *SyzbotGenerator) WorkDir(path string) error {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(path, 0644); err != nil {
return err
}
} else {
return err
}
}
g.workdir = path
return nil
}
func (g *SyzbotGenerator) RegisterItemFilter(callback ItemFilterFunc) {
g.filterFunc = callback
}
func (g *SyzbotGenerator) RegisterItemModifier(callback ItemModifierFunc) {
g.itemModFunc = callback
}
func (g *SyzbotGenerator) saveState(state *syzbotState) error {
fp, err := os.OpenFile(g.workdir+"/syzbot.state.json", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer fp.Close()
jsBytes, err := json.Marshal(state)
if err != nil {
return err
}
fp.Write(jsBytes)
return nil
}
func (g *SyzbotGenerator) loadState() (syzbotState, error) {
content, err := ioutil.ReadFile(g.workdir + "/syzbot.state.json")
if err != nil {
if os.IsNotExist(err) {
return syzbotState{
make(map[string]SyzbotCrash),
}, nil
}
return syzbotState{}, err
}
var out syzbotState
err = json.Unmarshal(content, &out)
return out, err
}
func (g *SyzbotGenerator) CompileSelector(selector string) (*cascadia.Selector, error) {
sel, err := cascadia.Compile(selector)
return &sel, err
}
func (g *SyzbotGenerator) Feed() (*feeds.Feed, error) {
url := "https://syzkaller.appspot.com/upstream"
cssg := CssGenerator{}
contentSelector, err := g.CompileSelector("table.list_table:nth-of-type(2) tbody tr")
if err != nil {
return nil, err
}
titleSelector, err := g.CompileSelector("td.title")
if err != nil {
return nil, err
}
linkSelector, err := g.CompileSelector("td.title a")
if err != nil {
return nil, err
}
reproSelector, err := g.CompileSelector("td.stat")
if err != nil {
return nil, err
}
bisectStatusSelector, err := g.CompileSelector("td.bisect_status")
if err != nil {
return nil, err
}
state, err := g.loadState()
if err != nil {
return nil, err
}
res, err := httpGet(url)
if err != nil {
return nil, err
}
node, err := html.Parse(res.Body)
if err != nil {
return nil, err
}
var crashes []*SyzbotCrash
for _, itemNode := range contentSelector.MatchAll(node) {
crash := SyzbotCrash{}
if titleNode := titleSelector.MatchFirst(itemNode); titleNode != nil {
crash.Title, _ = cssg.GetText(titleNode)
}
if linkNode := linkSelector.MatchFirst(itemNode); linkNode != nil {
link, err := cssg.GetLink(linkNode)
if err != nil {
panic(err)
}
if strings.Contains(link, "id=") {
parsedLink, _ := url2.Parse(link)
q := parsedLink.Query()
crash.Id = q.Get("id")
}
}
if reproNode := reproSelector.MatchFirst(itemNode); reproNode != nil {
crash.Repro, _ = cssg.GetText(reproNode)
}
if bisectNode := bisectStatusSelector.MatchFirst(itemNode); bisectNode != nil {
crash.BisectStatus, _ = cssg.GetText(bisectNode)
}
crashes = append(crashes, &crash)
}
hasUpdate := false
for i, crash := range crashes {
if _, found := state.Crashes[crash.Id]; !found {
hasUpdate = true
crashes[i].FirstSeen = time.Now()
state.Crashes[crash.Id] = *crashes[i]
} else {
crashes[i].FirstSeen = state.Crashes[crash.Id].FirstSeen
}
}
if hasUpdate {
g.saveState(&state)
}
sort.Slice(crashes, func(i, j int) bool {
// Since we want these is descending order return i > j
return crashes[i].FirstSeen.After(crashes[j].FirstSeen)
})
out := feeds.Feed{}
out.Title = "Syzbot - Upstream Crashes"
out.Link = &feeds.Link{Href: "https://syzkaller.appspot.com/upstream"}
out.Updated = time.Now()
for _, crash := range crashes {
newItem := &feeds.Item{
Title: crash.Title,
Link: &feeds.Link{
Href: fmt.Sprintf("https://syzkaller.appspot.com/bug?id=%s", crash.Id),
},
Author: &feeds.Author{
Name: "Syzbot",
},
Id: crash.Id,
Created: crash.FirstSeen,
Updated: crash.FirstSeen,
}
if g.itemModFunc != nil {
g.itemModFunc(newItem)
}
if g.filterFunc == nil || g.filterFunc(newItem) {
out.Items = append(out.Items, newItem)
}
if len(out.Items) >= 20 {
// Break here instead of just iterating over the first 20 to account for the filter removing some
break
}
}
return &out, nil
}