-
Notifications
You must be signed in to change notification settings - Fork 52
/
goroot.go
373 lines (337 loc) · 8.53 KB
/
goroot.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gore
import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"unicode/utf8"
"golang.org/x/arch/x86/x86asm"
)
func tryFromGOROOT(f *GoFile) (string, error) {
// Check for non-supported architectures.
if f.FileInfo.Arch != Arch386 && f.FileInfo.Arch != ArchAMD64 {
return "", nil
}
is32 := false
if f.FileInfo.Arch == Arch386 {
is32 = true
}
// Find runtime.GOROOT function.
var fcn *Function
std, err := f.GetSTDLib()
if err != nil {
return "", nil
}
pkgLoop:
for _, v := range std {
if v.Name != "runtime" {
continue
}
for _, vv := range v.Functions {
if vv.Name != "GOROOT" {
continue
}
fcn = vv
break pkgLoop
}
}
// Check if the functions were found
if fcn == nil {
// If we can't find the function, there is nothing to do.
return "", ErrNoGoRootFound
}
// Get the raw hex.
buf, err := f.Bytes(fcn.Offset, fcn.End-fcn.Offset)
if err != nil {
return "", nil
}
s := 0
mode := f.FileInfo.WordSize * 8
for s < len(buf) {
inst, err := x86asm.Decode(buf[s:], mode)
if err != nil {
// If we fail to decode the instruction, something is wrong so
// bailout.
return "", nil
}
// Update next instruction location.
s = s + inst.Len
// Check if it's a "mov" instruction.
if inst.Op != x86asm.MOV {
continue
}
if inst.Args[0] != x86asm.RAX && inst.Args[0] != x86asm.EAX {
continue
}
arg := inst.Args[1].(x86asm.Mem)
// First, assume that the address is a direct addressing.
addr := arg.Disp
if arg.Base == x86asm.EIP || arg.Base == x86asm.RIP {
// If the addressing is based on the instruction pointer, fix the address.
addr = addr + int64(fcn.Offset) + int64(s)
} else if arg.Base == 0 && arg.Disp > 0 {
// To support x32 direct addressing
} else {
continue
}
// If the addressing is based on the stack pointer, this is not the right
// instruction.
if arg.Base == x86asm.ESP || arg.Base == x86asm.RSP {
continue
}
// Resolve the pointer to the string. If we get no data, this is not the
// right instruction.
b, _ := f.Bytes(uint64(addr), uint64(0x20))
if b == nil {
continue
}
r := bytes.NewReader(b)
ptr, err := readUIntTo64(r, f.FileInfo.ByteOrder, is32)
if err != nil {
// Probably not the right instruction, so go to next.
continue
}
l, err := readUIntTo64(r, f.FileInfo.ByteOrder, is32)
if err != nil {
// Probably not the right instruction, so go to next.
continue
}
bstr, _ := f.Bytes(ptr, l)
if bstr == nil {
continue
}
ver := string(bstr)
if !utf8.ValidString(ver) {
return "", ErrNoGoRootFound
}
return ver, nil
}
// for go version vary from 1.5 to 1.9
s = 0
var insts []x86asm.Inst
for s < len(buf) {
inst, err := x86asm.Decode(buf[s:], mode)
if err != nil {
return "", nil
}
s = s + inst.Len
insts = append(insts, inst)
}
var length, addr uint64
// Look up the address from the end of the assembly instruction
for i := len(insts) - 1; i >= 0; i-- {
inst := insts[i]
if inst.Op == x86asm.MOV && length == 0 {
switch v := inst.Args[1].(type) {
case x86asm.Imm:
length = uint64(v)
continue
default:
continue
}
}
if (inst.Op == x86asm.LEA || inst.Op == x86asm.MOV) && addr == 0 {
switch v := inst.Args[1].(type) {
case x86asm.Mem:
arg := v
if arg.Base == x86asm.ESP || arg.Base == x86asm.RSP {
continue
}
addr = uint64(arg.Disp)
if arg.Base == x86asm.EIP || arg.Base == x86asm.RIP {
// If the addressing is based on the instruction pointer, fix the address.
s = 0
for i2, inst2 := range insts {
if i2 > i {
break
}
s += inst2.Len
}
addr = addr + fcn.Offset + uint64(s)
} else if arg.Base == 0 && arg.Disp > 0 {
// To support x32 direct addressing
} else {
continue
}
case x86asm.Imm:
addr = uint64(v)
default:
continue
}
}
if length > 0 && addr > 0 {
bstr, _ := f.Bytes(addr, length)
if bstr == nil {
continue
}
ver := string(bstr)
if !utf8.ValidString(ver) {
return "", ErrNoGoRootFound
}
return ver, nil
}
}
return "", ErrNoGoRootFound
}
func tryFromTimeInit(f *GoFile) (string, error) {
// Check for non-supported architectures.
if f.FileInfo.Arch != Arch386 && f.FileInfo.Arch != ArchAMD64 {
return "", nil
}
is32 := false
if f.FileInfo.Arch == Arch386 {
is32 = true
}
// Find time.initPackages function.
var fcn *Function
std, err := f.GetSTDLib()
if err != nil {
return "", nil
}
pkgLoop:
for _, v := range std {
if v.Name != "time" {
continue
}
for _, vv := range v.Functions {
if vv.Name != "initPackages" {
continue
}
fcn = vv
break pkgLoop
}
}
// Check if the functions were found
if fcn == nil {
// If we can't find the function there is nothing to do.
return "", ErrNoGoRootFound
}
// Get the raw hex.
buf, err := f.Bytes(fcn.Offset, fcn.End-fcn.Offset)
if err != nil {
return "", nil
}
s := 0
mode := f.FileInfo.WordSize * 8
for s < len(buf) {
inst, err := x86asm.Decode(buf[s:], mode)
if err != nil {
// If we fail to decode the instruction, something is wrong so
// bailout.
return "", nil
}
// Update next instruction location.
s = s + inst.Len
// Check if it's a "mov" instruction.
if inst.Op != x86asm.MOV {
continue
}
if inst.Args[0] != x86asm.RAX && inst.Args[0] != x86asm.ECX {
continue
}
kindof := reflect.TypeOf(inst.Args[1])
if kindof.String() != "x86asm.Mem" {
continue
}
arg := inst.Args[1].(x86asm.Mem)
// First, assume that the address is a direct addressing.
addr := arg.Disp
if arg.Base == x86asm.EIP || arg.Base == x86asm.RIP {
// If the addressing is based on the instruction pointer, fix the address.
addr = addr + int64(fcn.Offset) + int64(s)
} else if arg.Base == 0 && arg.Disp > 0 {
// To support x32 direct addressing
} else {
continue
}
// Resolve the pointer to the string. If we get no data, this is not the
// right instruction.
b, _ := f.Bytes(uint64(addr), uint64(0x20))
if b == nil {
continue
}
r := bytes.NewReader(b)
ptr, err := readUIntTo64(r, f.FileInfo.ByteOrder, is32)
if err != nil {
// Probably not the right instruction, so go to next.
continue
}
// If the pointer is nil, it's not the right instruction
if ptr == 0 {
continue
}
l, err := readUIntTo64(r, f.FileInfo.ByteOrder, is32)
if err != nil {
// Probably not the right instruction, so go to next.
continue
}
bstr, _ := f.Bytes(ptr, l)
if bstr == nil {
continue
}
ver := string(bstr)
if !utf8.ValidString(ver) {
return "", ErrNoGoRootFound
}
return ver, nil
}
return "", ErrNoGoRootFound
}
func findGoRootPath(f *GoFile) (string, error) {
// if DWARF debug info exists, then this can simply be obtained from there
if goroot, ok := getGoRootFromDwarf(f.fh); ok {
return goroot, nil
}
// There is no GOROOT function may be inlined (after go1.16)
// at this time GOROOT is obtained through time_init function
goroot, err := tryFromGOROOT(f)
if goroot != "" {
return goroot, nil
}
if err != nil && !errors.Is(err, ErrNoGoRootFound) {
return "", err
}
goroot, err = tryFromTimeInit(f)
if goroot != "" {
return goroot, nil
}
if err != nil && !errors.Is(err, ErrNoGoRootFound) {
return "", err
}
// Try to determine from std lib package paths.
pkg, err := f.GetSTDLib()
if err != nil {
return "", fmt.Errorf("error when getting standard library packages: %w", err)
}
if len(pkg) == 0 {
return "", fmt.Errorf("no standard library packages found")
}
for _, v := range pkg {
subpath := fmt.Sprintf("/src/%s", v.Name)
if strings.HasSuffix(v.Filepath, subpath) {
return strings.TrimSuffix(v.Filepath, subpath), nil
}
subpathWin := fmt.Sprintf("\\src\\%s", v.Name)
if strings.HasSuffix(v.Filepath, subpathWin) {
return strings.TrimSuffix(v.Filepath, subpathWin), nil
}
}
return "", ErrNoGoRootFound
}