forked from google/syzkaller
/
preprocess_hooks.go
258 lines (228 loc) · 7.75 KB
/
preprocess_hooks.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
package parser
import (
"github.com/google/syzkaller/tools/moonshine/strace_types"
"github.com/google/syzkaller/prog"
. "github.com/google/syzkaller/tools/moonshine/logging"
"fmt"
)
type PreprocessHook func(ctx *Context)
func Preprocess(ctx *Context) {
call := ctx.CurrentStraceCall.CallName
if procFunc, ok := PreprocessMap[call]; ok {
procFunc(ctx)
}
return
}
var PreprocessMap = map[string]PreprocessHook {
"accept": Preprocess_Accept,
"accept4": Preprocess_Accept,
"bind": Preprocess_Bind,
"connect": Preprocess_Connect,
"fcntl": Preprocess_Fcntl,
"getsockname": Preprocess_Getsockname,
"getsockopt": Preprocess_Getsockopt,
"ioctl": Preprocess_Ioctl,
"open": Preprocess_Open,
"prctl": Preprocess_Prctl,
"recvfrom": Preprocess_Recvfrom,
"mknod": Preprocess_Mknod,
"modify_ldt": Preprocess_ModifyLdt,
"openat": Preprocess_Openat,
"sendto": Preprocess_Sendto,
"setsockopt": Preprocess_Setsockopt,
"shmctl": Preprocess_Shmctl,
"socket": Preprocess_Socket,
}
func Preprocess_Accept(ctx *Context) {
/*
Accept can take on many subforms such as
accept$inet
accept$inet6
In order to determine the proper form we need to look at the file descriptor to determine
the proper socket type. We refer to the $inet as a suffix to the name
*/
suffix := ""
straceFd := ctx.CurrentStraceCall.Args[0] //File descriptor of Accept
syzFd := ctx.CurrentSyzCall.Meta.Args[0]
if arg := ctx.Cache.Get(syzFd, straceFd); arg != nil {
switch a := arg.Type().(type) {
case *prog.ResourceType:
if suffix = strace_types.Accept_labels[a.TypeName]; suffix != "" {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
}
}
func Preprocess_Bind(ctx *Context) {
suffix := ""
straceFd := ctx.CurrentStraceCall.Args[0]
syzFd := ctx.CurrentSyzCall.Meta.Args[0]
if arg := ctx.Cache.Get(syzFd, straceFd); arg != nil {
switch a := arg.Type().(type) {
case *prog.ResourceType:
if suffix = strace_types.Bind_labels[a.TypeName]; suffix != "" {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
}
}
func Preprocess_Connect(ctx *Context) {
suffix := ""
straceFd := ctx.CurrentStraceCall.Args[0]
syzFd := ctx.CurrentSyzCall.Meta.Args[0]
if arg := ctx.Cache.Get(syzFd, straceFd); arg != nil {
switch a := arg.Type().(type) {
case *prog.ResourceType:
if suffix = strace_types.Connect_labels[a.TypeName]; suffix != "" {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
}
}
func Preprocess_Getsockname(ctx *Context) {
suffix := ""
straceFd := ctx.CurrentStraceCall.Args[0]
syzFd := ctx.CurrentSyzCall.Meta.Args[0]
if arg := ctx.Cache.Get(syzFd, straceFd); arg != nil {
switch a := arg.Type().(type) {
case *prog.ResourceType:
if suffix = strace_types.Getsockname_labels[a.TypeName]; suffix != "" {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
}
}
func Preprocess_Socket(ctx *Context) {
straceFd := ctx.CurrentStraceCall.Args[0]
if suffix, ok := strace_types.Socket_labels[straceFd.String()]; ok {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
func Preprocess_Setsockopt(ctx *Context) {
sockLevel := ctx.CurrentStraceCall.Args[1]
optName := ctx.CurrentStraceCall.Args[2]
pair := strace_types.Pair {
A: sockLevel.String(),
B: optName.String(),
}
if suffix, ok := strace_types.Setsockopt_labels[pair]; ok {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
func Preprocess_Getsockopt(ctx *Context) {
sockLevel := ctx.CurrentStraceCall.Args[1]
optName := ctx.CurrentStraceCall.Args[2]
pair := strace_types.Pair {
A: sockLevel.String(),
B: optName.String(),
}
if suffix, ok := strace_types.Getsockopt_labels[pair]; ok {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
func Preprocess_Recvfrom(ctx *Context) {
suffix := ""
straceFd := ctx.CurrentStraceCall.Args[0]
syzFd := ctx.CurrentSyzCall.Meta.Args[0]
if arg := ctx.Cache.Get(syzFd, straceFd); arg != nil {
switch a := arg.Type().(type) {
case *prog.ResourceType:
if suffix = strace_types.Recvfrom_labels[a.TypeName]; suffix != "" {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
}
}
func Preprocess_Open(ctx *Context) {
if len(ctx.CurrentStraceCall.Args) < 3 {
ctx.CurrentStraceCall.Args = append(ctx.CurrentStraceCall.Args,
strace_types.NewExpression(strace_types.NewIntType(int64(0))))
}
}
func Preprocess_Mknod(ctx *Context) {
if len(ctx.CurrentStraceCall.Args) < 3 {
ctx.CurrentStraceCall.Args = append(ctx.CurrentStraceCall.Args,
strace_types.NewExpression(strace_types.NewIntType(int64(0))))
}
}
func Preprocess_Openat(ctx *Context) {
if len(ctx.CurrentSyzCall.Args) < 4 {
ctx.CurrentStraceCall.Args = append(ctx.CurrentStraceCall.Args,
strace_types.NewExpression(strace_types.NewIntType(int64(0))))
}
}
func Preprocess_Ioctl(ctx *Context) {
ioctlCmd := ctx.CurrentStraceCall.Args[1].String()
if suffix, ok := strace_types.Ioctl_map[ioctlCmd]; ok {
ctx.CurrentStraceCall.CallName += suffix
} else if _, ok := ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName + "$" + ioctlCmd]; ok {
ctx.CurrentStraceCall.CallName += "$"+ioctlCmd
}
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
func Preprocess_Fcntl(ctx *Context) {
fcntlCmd := ctx.CurrentStraceCall.Args[1].String()
if suffix, ok := strace_types.Fcntl_labels[fcntlCmd]; ok {
ctx.CurrentStraceCall.CallName += suffix
} else if _, ok := ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName + "$" + fcntlCmd]; ok {
ctx.CurrentStraceCall.CallName += "$"+fcntlCmd
}
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
func Preprocess_Prctl(ctx *Context) {
prctlCmd := ctx.CurrentStraceCall.Args[0].String()
if _, ok := ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName + "$" + prctlCmd]; ok {
ctx.CurrentStraceCall.CallName += "$"+prctlCmd
}
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
func Preprocess_Shmctl(ctx *Context) {
shmctlCmd := ctx.CurrentStraceCall.Args[1].String()
if _, ok := ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName + "$" + shmctlCmd]; ok {
ctx.CurrentStraceCall.CallName += "$"+shmctlCmd
}
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
func Preprocess_Sendto(ctx *Context) {
suffix := ""
straceFd := ctx.CurrentStraceCall.Args[0] //File descriptor of Accept
syzFd := ctx.CurrentSyzCall.Meta.Args[0]
if arg := ctx.Cache.Get(syzFd, straceFd); arg != nil {
switch a := arg.Type().(type) {
case *prog.ResourceType:
if suffix = strace_types.Sendto_labels[a.TypeName]; suffix != "" {
ctx.CurrentStraceCall.CallName += suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}
}
}
}
func Preprocess_ModifyLdt(ctx *Context) {
suffix := ""
fmt.Printf("PREPROCESS MODIFY LDT\n")
switch a := ctx.CurrentStraceCall.Args[0].(type) {
case *strace_types.Expression:
switch a.Eval(ctx.Target) {
case 0:
suffix = "$read"
case 1:
suffix = "$write"
case 2:
suffix = "$read_default"
case 17:
suffix = "$write2"
}
default:
Failf("Preprocess modifyldt received unexpected strace type: %s\n", a.Name())
}
ctx.CurrentStraceCall.CallName = ctx.CurrentStraceCall.CallName + suffix
ctx.CurrentSyzCall.Meta = ctx.Target.SyscallMap[ctx.CurrentStraceCall.CallName]
}