-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
311 lines (252 loc) · 8.81 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"github.com/opencontainers/runtime-spec/specs-go"
)
// TODO add a verbose flag and do a proper verbose mode
var verbose = false
// need to save the previous instructions to go back and look for the syscall ID
// have found MOVs to 0(SP) as far as 10 instructions behind, so 15 seems like a safe number
const previousInstructionsBufferSize = 15
// wrapper for each findSyscallID by arch
func findSyscallID(arch specs.Arch, previouInstructions []string, curPos int) (int64, error) {
var i int64
var err error
switch arch {
case specs.ArchX86_64:
i, err = findSyscallIDx86_64(previouInstructions, curPos)
case specs.ArchX86:
i, err = findSyscallIDx86(previouInstructions, curPos)
case specs.ArchARM:
i, err = findSyscallIDARM(previouInstructions, curPos)
default:
log.Fatalln(arch, "is not supported")
}
return i, err
}
func findRuntimeSyscallID(arch specs.Arch, previouInstructions []string, curPos int) (int64, error) {
var i int64
var err error
switch arch {
case specs.ArchX86_64:
i, err = findRuntimeSyscallIDx86_64(previouInstructions, curPos)
case specs.ArchX86:
i, err = findRuntimeSyscallIDx86_64(previouInstructions, curPos) // Same as x86_64 ?
case specs.ArchARM:
i, err = findRuntimeSyscallIDARM(previouInstructions, curPos)
default:
log.Fatalln(arch, "is not supported")
}
return i, err
}
func findRuntimeSyscallIDx86_64(previouInstructions []string, curPos int) (int64, error) {
i := 0
for i < previousInstructionsBufferSize {
instruction := previouInstructions[curPos%previousInstructionsBufferSize]
isMOV := strings.Index(instruction, "MOV") != -1
isAXRegister := strings.Index(instruction, ", AX") != -1
// runtime·read on syscall/sys_linux_amd64.s has the following for calling the read syscall:
// MOVL $0, AX
// SYSCALL
// However, some compiler optmization changes it to:
// XORL AX, AX
// which must be faster to zero the register than using a MOV, so we need to account for this
isRead := strings.Index(instruction, "XOR") != -1 && strings.Index(instruction, " AX, AX") != -1
if isRead {
return 0, nil
}
if isMOV && isAXRegister {
syscallIDBeginning := strings.Index(instruction, "$")
if syscallIDBeginning == -1 {
return -1, fmt.Errorf("Failed to find syscall ID on line: %v", instruction)
}
syscallIDEnd := strings.Index(instruction, ", AX")
hex := instruction[syscallIDBeginning+1 : syscallIDEnd]
id, err := strconv.ParseInt(hex, 0, 64)
if err != nil {
return -1, fmt.Errorf("Error parsing hex id: %v", err)
}
return id, nil
}
i++
curPos--
}
return -1, fmt.Errorf("Failed to find syscall ID")
}
func findRuntimeSyscallIDARM(previouInstructions []string, curPos int) (int64, error) {
i := 0
for i < previousInstructionsBufferSize {
instruction := previouInstructions[curPos%previousInstructionsBufferSize]
isR7 := strings.Index(instruction, ", R7") != -1
isNotReg := strings.Index(instruction, "),") == -1 // get the "(R15)," ending in MOVW 0x2c(R15), R7
if isR7 && isNotReg {
syscallIDBeginning := strings.Index(instruction, "$")
if syscallIDBeginning == -1 {
return -1, fmt.Errorf("Failed to find syscall ID on line: %v", instruction)
}
syscallIDEnd := strings.Index(instruction, ", R7")
hex := instruction[syscallIDBeginning+1 : syscallIDEnd]
id, err := strconv.ParseInt(hex, 0, 64)
if err != nil {
return -1, fmt.Errorf("Error parsing hex id: %v", err)
}
return id, nil
}
i++
curPos--
}
return -1, fmt.Errorf("Failed to find syscall ID")
}
// findSyscallIDx86_64 goes back from the call until it finds an instruction with the format
// MOVQ $ID, 0(SP), which is the one that pushes the syscall ID onto the base address
// at the SP register
func findSyscallIDx86_64(previouInstructions []string, curPos int) (int64, error) {
i := 0
for i < previousInstructionsBufferSize {
instruction := previouInstructions[curPos%previousInstructionsBufferSize]
isMOVQ := strings.Index(instruction, "MOVQ") != -1
isBaseSPAddress := strings.Index(instruction, ", 0(SP)") != -1
if isMOVQ && isBaseSPAddress {
syscallIDBeginning := strings.Index(instruction, "$")
if syscallIDBeginning == -1 {
return -1, fmt.Errorf("Failed to find syscall ID on line: %v", instruction)
}
syscallIDEnd := strings.Index(instruction, ", 0(SP)")
hex := instruction[syscallIDBeginning+1 : syscallIDEnd]
id, err := strconv.ParseInt(hex, 0, 64)
if err != nil {
return -1, fmt.Errorf("Error parsing hex id: %v", err)
}
return id, nil
}
i++
curPos--
}
return -1, fmt.Errorf("Failed to find syscall ID")
}
// findSyscallIDx86 goes back from the call until it finds an instruction with the format
// MOVL $ID, 0(SP), which is the one that pushes the syscall ID onto the base address
// at the SP register
func findSyscallIDx86(previouInstructions []string, curPos int) (int64, error) {
i := 0
for i < previousInstructionsBufferSize {
instruction := previouInstructions[curPos%previousInstructionsBufferSize]
isMOVL := strings.Index(instruction, "MOVL") != -1
isBaseSPAddress := strings.Index(instruction, ", 0(SP)") != -1
if isMOVL && isBaseSPAddress {
syscallIDBeginning := strings.Index(instruction, "$")
if syscallIDBeginning == -1 {
return -1, fmt.Errorf("Failed to find syscall ID on line: %v", instruction)
}
syscallIDEnd := strings.Index(instruction, ", 0(SP)")
hex := instruction[syscallIDBeginning+1 : syscallIDEnd]
id, err := strconv.ParseInt(hex, 0, 64)
if err != nil {
return -1, fmt.Errorf("Error parsing hex id: %v", err)
}
return id, nil
}
i++
curPos--
}
return -1, fmt.Errorf("Failed to find syscall ID")
}
func findSyscallIDARM(previouInstructions []string, curPos int) (int64, error) {
i := 0
for i < previousInstructionsBufferSize {
instruction := previouInstructions[curPos%previousInstructionsBufferSize]
isMOVW := strings.Index(instruction, "MOVW") != -1
isBaseSPAddress := strings.Index(instruction, ", R0") != -1
syscallIDBeginning := strings.Index(instruction, "$")
if isMOVW && isBaseSPAddress && (syscallIDBeginning != -1) {
syscallIDEnd := strings.Index(instruction, ", R0")
hex := instruction[syscallIDBeginning+1 : syscallIDEnd]
id, err := strconv.ParseInt(hex, 0, 64)
if err != nil {
return -1, fmt.Errorf("Error parsing hex id: %v", err)
}
return id, nil
}
i++
curPos--
}
return -1, fmt.Errorf("Failed to find syscall ID")
}
func getSyscallList(disassambled *os.File, arch specs.Arch) []string {
scanner := bufio.NewScanner(disassambled)
// keep a few of the past instructions in a buffer so we can look back and find the syscall ID
previousInstructions := make([]string, previousInstructionsBufferSize)
lineCount := 0
syscalls := getDefaultSyscalls(arch)
fmt.Println("Scanning disassembled binary for syscall IDs")
currentFunction := ""
for scanner.Scan() {
instruction := scanner.Text()
previousInstructions[lineCount%previousInstructionsBufferSize] = instruction
if len(instruction) > 5 && instruction[0:4] == "TEXT" {
currentFunction = parseFunctionName(instruction)
}
// function call to one of the 5 functions from the syscall package
if isSyscallPkgCall(arch, instruction) {
id, err := findSyscallID(arch, previousInstructions, lineCount)
if err != nil {
log.Printf("Failed to find syscall ID for line %v: %v, reason: %v\n", lineCount+1, instruction, err)
lineCount++
continue
}
syscalls[id] = true
}
// the runtime package doesn't use the functions on the syscall package, instead it uses SYSCALL directly
if isRuntimeSyscall(arch, instruction, currentFunction) {
id, err := findRuntimeSyscallID(arch, previousInstructions, lineCount)
if err != nil {
log.Printf("Failed to find syscall ID for line %v: \n\t%v\n\treason: %v\n", lineCount+1, instruction, err)
lineCount++
continue
}
syscalls[id] = true
}
lineCount++
}
syscallsList := make([]string, len(syscalls))
i := 0
for id := range syscalls {
name, ok := syscallIDtoName[arch][id]
if !ok {
fmt.Printf("Sycall ID %v not available on the ID->name map\n", id)
} else {
syscallsList[i] = name
i++
}
}
sort.Strings(syscallsList)
return syscallsList
}
func main() {
flag.Parse()
if len(flag.Args()) < 2 {
fmt.Println("Usage: go2seccomp /path/to/binary /path/to/profile.json")
os.Exit(1)
}
binaryPath := flag.Args()[0]
profilePath := flag.Args()[1]
f := openElf(binaryPath)
if !isGoBinary(f) {
fmt.Println(binaryPath, "doesn't seems to be a Go binary")
os.Exit(1)
}
arch := getArch(f)
disassambled := disassamble(binaryPath)
defer disassambled.Close()
defer os.Remove("disassembled.asm")
syscallsList := getSyscallList(disassambled, arch)
fmt.Printf("Syscalls detected (total: %v): %v\n", len(syscallsList), syscallsList)
writeProfile(syscallsList, arch, profilePath)
}