forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goversion.go
250 lines (215 loc) · 6.11 KB
/
goversion.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
// 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"
"regexp"
"strings"
"golang.org/x/arch/x86/x86asm"
"golang.org/x/mod/semver"
)
var goVersionMatcher = regexp.MustCompile(`(go[\d+\.]*(beta|rc)?[\d*])`)
// GoVersion holds information about the compiler version.
type GoVersion struct {
// Name is a string representation of the version.
Name string
// SHA is a digest of the git commit for the release.
SHA string
// Timestamp is a string of the timestamp when the commit was created.
Timestamp string
}
// ResolveGoVersion tries to return the GoVersion for the given tag.
// For example the tag: go1 will return a GoVersion struct representing version 1.0 of the compiler.
// If no goversion for the given tag is found, nil is returned.
func ResolveGoVersion(tag string) *GoVersion {
v, ok := goversions[tag]
if !ok {
return nil
}
return v
}
// GoVersionCompare compares two version strings.
// If a < b, -1 is returned.
// If a == b, 0 is returned.
// If a > b, 1 is returned.
func GoVersionCompare(a, b string) int {
if a == b {
return 0
}
return semver.Compare(buildSemVerString(a), buildSemVerString(b))
}
func buildSemVerString(v string) string {
// First remove the go prefix
tmp := strings.TrimPrefix(v, "go")
// If it has a pre-release, we need to add a dash and patch version of 0.
if strings.Contains(tmp, "beta") {
tmp = strings.ReplaceAll(tmp, "beta", ".0-beta")
}
if strings.Contains(tmp, "rc") {
tmp = strings.ReplaceAll(tmp, "rc", ".0-rc")
}
return "v" + tmp
}
func findGoCompilerVersion(f *GoFile) (*GoVersion, error) {
// Try to determine the version based on the schedinit function.
if v := tryFromSchedInit(f); v != nil {
return v, nil
}
// If no version was found, search the sections for the
// version string.
data, err := f.fh.getRData()
// If read only data section does not exist, try text.
if err == ErrSectionDoesNotExist {
data, err = f.fh.getCodeSection()
}
if err != nil {
return nil, err
}
notfound := false
for !notfound {
version := matchGoVersionString(data)
if version == "" {
return nil, ErrNoGoVersionFound
}
ver := ResolveGoVersion(version)
// Go before 1.4 does not have the version string so if we have found
// a version string below 1.4beta1 it is a false positive.
if ver == nil || GoVersionCompare(ver.Name, "go1.4beta1") < 0 {
off := bytes.Index(data, []byte(version))
// No match
if off == -1 {
break
}
data = data[off+2:]
continue
}
return ver, nil
}
return nil, nil
}
// tryFromSchedInit tries to identify the version of the Go compiler that compiled the code.
// The function "schedinit" in the "runtime" package has the only reference to this string
// used to identify the version.
// The function returns nil if no version is found.
func tryFromSchedInit(f *GoFile) *GoVersion {
// 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 shedinit 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 != "schedinit" {
continue
}
fcn = vv
break pkgLoop
}
}
// Check if the functions was found
if fcn == nil {
// If we can't find the function there is nothing to do.
return nil
}
// Get the raw hex.
buf, err := f.Bytes(fcn.Offset, fcn.End-fcn.Offset)
if err != nil {
return nil
}
/*
Disassemble the function until the loading of the Go version is found.
*/
// Counter for how many bytes has been read.
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 "lea" instruction.
if inst.Op != x86asm.LEA {
continue
}
// Check what it's loading and if it's pointing to the compiler version used.
// First assume that the address is a direct addressing.
arg := inst.Args[1].(x86asm.Mem)
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)
}
// 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
}
if !bytes.HasPrefix(bstr, []byte("go1.")) {
continue
}
// Likely the version string.
ver := string(bstr)
gover := ResolveGoVersion(ver)
if gover != nil {
return gover
}
// An unknown version.
return &GoVersion{Name: ver}
}
return nil
}
func matchGoVersionString(data []byte) string {
return string(goVersionMatcher.Find(data))
}