-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing.go
209 lines (169 loc) · 4.34 KB
/
parsing.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
package soaap
import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
//
// Parse a JSON file emitted by SOAAP.
//
// The `progress` callback will be notified when major events occur
// (top-level JSON parsing begins/ends, traces are parsed, etc.).
//
func ParseJSON(f *os.File, progress func(string)) (Results, error) {
decoder := json.NewDecoder(f)
var top map[string]map[string]json.RawMessage
go progress(fmt.Sprintf("Loading %s", f.Name()))
err := decoder.Decode(&top)
if err != nil {
return Results{}, err
}
raw := top["soaap"]
var soaap Results
maxTraceSize := len(raw)
soaap.Traces = make([]CallTrace, maxTraceSize)
parsed := 0
// Once SOAAP issue #28 is resolved, we should be able to replace this
// loop (as well as the `parseTrace` function) with a single call to
// decoder.Decode().
for k, v := range raw {
switch k {
case "access_origin_warning":
// TODO
case "cap_rights_warning":
// TODO
case "classified_warning":
// TODO
case "global_access_warning":
// TODO
case "global_lost_update":
// TODO
case "private_access":
json.Unmarshal(v, &soaap.PrivateAccess)
for i, vuln := range soaap.PrivateAccess {
num, err := traceNumber(vuln.TraceName)
if err != nil {
return Results{}, err
}
soaap.PrivateAccess[i].Trace = num
// Build a slice of *useful* sources (i.e., those with traces)
sources := make([]DataSource, 0)
for _, source := range vuln.Sources {
if source.TraceRef != "" {
num, err := traceNumber(source.TraceRef)
if err != nil {
return Results{}, err
}
source.Trace = num
sources = append(sources, source)
}
}
soaap.PrivateAccess[i].Sources = sources
}
case "private_leak":
// TODO
case "privileged_call":
// TODO
case "sandboxed_func":
// TODO
case "syscall_warning":
// TODO
case "vulnerability_warning":
json.Unmarshal(v, &soaap.Vulnerabilities)
for i, vuln := range soaap.Vulnerabilities {
num, err := traceNumber(vuln.TraceName)
if err != nil {
return Results{}, err
}
soaap.Vulnerabilities[i].Trace = num
}
default:
index, err := traceNumber(k)
if err != nil {
return soaap, errors.New(k + " is not a trace")
}
err = parseTrace(v, soaap.Traces, index)
if err != nil {
return soaap, err
}
}
parsed += 1
if parsed%10000 == 0 {
go progress(fmt.Sprintf("Parsed %d traces", parsed))
}
}
progress(fmt.Sprintf("Finished parsing %s.", f.Name()))
return soaap, nil
}
//
// Unwrap the SOAAP encoding of a call trace.
//
// This should be simplified once SOAAP issue #28 is addressed: we won't
// need this function any more.
//
func parseTrace(j json.RawMessage, traces []CallTrace, index int) error {
if index >= len(traces) {
return fmt.Errorf("index (%d) too large for traces (len: %d)",
index, len(traces))
}
var x map[string]json.RawMessage
err := json.Unmarshal(j, &x)
if err != nil {
return err
}
var rawCallSites []json.RawMessage
err = json.Unmarshal(x["trace"], &rawCallSites)
if err != nil {
return err
}
// The elements in a SOAAP call trace include at least n-1 functions:
// the final element may be a reference to another trace.
count := len(rawCallSites)
t := &traces[index]
t.CallSites = make([]CallSite, 0, count)
t.Next = -1
for i, v := range rawCallSites {
var tmp CallSite
err = json.Unmarshal(v, &tmp)
if err != nil {
return err
}
// If we managed to parse a real function name, this is
// a CallSite and not a reference to a sub-trace.
if tmp.Function != "" {
t.CallSites = append(t.CallSites, tmp)
} else {
if i != count-1 {
return fmt.Errorf("unable to convert call site %s", v)
}
var tmp map[string]string
err = json.Unmarshal(v, &tmp)
if err != nil {
return err
}
ref, err := traceNumber(tmp["trace_ref"])
if err != nil {
return err
}
if ref > len(traces) {
return errors.New(
fmt.Sprintf("trace %d references %d "+
" but we only have %d traces",
index, ref, len(traces)))
}
t.Next = ref
}
}
return err
}
// Extract a trace number from a string like '!trace42'.
func traceNumber(name string) (int, error) {
if !strings.HasPrefix(name, "!trace") {
return -1, errors.New("'" + name + "' is not a trace name")
}
i, err := strconv.ParseInt(name[6:], 10, 32)
return int(i), err
}