forked from golang/vuln
/
binary.go
149 lines (132 loc) · 4.2 KB
/
binary.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
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.18
// +build go1.18
package vulncheck
import (
"context"
"fmt"
"io"
"runtime/debug"
"strings"
"github.com/hyangah/legacyvuln/internal/derrors"
"github.com/hyangah/legacyvuln/internal/semver"
"github.com/hyangah/legacyvuln/vulncheck/internal/binscan"
"golang.org/x/tools/go/packages"
)
// Binary detects presence of vulnerable symbols in exe.
// The Calls, Imports, and Requires fields on Result will be empty.
func Binary(ctx context.Context, exe io.ReaderAt, cfg *Config) (_ *Result, err error) {
defer derrors.Wrap(&err, "vulncheck.Binary")
mods, packageSymbols, bi, err := binscan.ExtractPackagesAndSymbols(exe)
if err != nil {
return nil, err
}
cmods := convertModules(mods)
// set the stdlib version for detection of vulns in the standard library
// TODO(#53740): what if Go version is not in semver format?
stdlibModule.Version = semver.GoTagToSemver(bi.GoVersion)
// Add "stdlib" module.
cmods = append(cmods, stdlibModule)
modVulns, err := fetchVulnerabilities(ctx, cfg.Client, cmods)
if err != nil {
return nil, err
}
goos := findSetting("GOOS", bi)
goarch := findSetting("GOARCH", bi)
if goos == "" || goarch == "" {
fmt.Printf("warning: failed to extract build system specification GOOS: %s GOARCH: %s\n", goos, goarch)
}
modVulns = modVulns.filter(goos, goarch)
result := &Result{}
for pkg, symbols := range packageSymbols {
mod := findPackageModule(pkg, cmods)
if cfg.ImportsOnly {
addImportsOnlyVulns(pkg, mod, symbols, result, modVulns)
} else {
addSymbolVulns(pkg, mod, symbols, result, modVulns)
}
}
setModules(result, cmods)
return result, nil
}
// addImportsOnlyVulns adds Vuln entries to result in imports only mode, i.e., for each vulnerable symbol
// of pkg.
func addImportsOnlyVulns(pkg, mod string, symbols []string, result *Result, modVulns moduleVulnerabilities) {
for _, osv := range modVulns.vulnsForPackage(pkg) {
for _, affected := range osv.Affected {
for _, p := range affected.EcosystemSpecific.Imports {
if p.Path != pkg {
continue
}
syms := p.Symbols
if len(syms) == 0 {
// If every symbol of pkg is vulnerable, we would ideally
// compute every symbol mentioned in the pkg and then add
// Vuln entry for it, just as we do in Source. However,
// we don't have code of pkg here so we have to do best
// we can, which is the symbols of pkg actually appearing
// in the binary.
syms = symbols
}
for _, symbol := range syms {
vuln := &Vuln{
OSV: osv,
Symbol: symbol,
PkgPath: pkg,
ModPath: mod,
}
result.Vulns = append(result.Vulns, vuln)
}
}
}
}
}
// addSymbolVulns adds Vuln entries to result for every symbol of pkg in the binary that is vulnerable.
func addSymbolVulns(pkg, mod string, symbols []string, result *Result, modVulns moduleVulnerabilities) {
for _, symbol := range symbols {
for _, osv := range modVulns.vulnsForSymbol(pkg, symbol) {
vuln := &Vuln{
OSV: osv,
Symbol: symbol,
PkgPath: pkg,
ModPath: mod,
}
result.Vulns = append(result.Vulns, vuln)
}
}
}
func convertModules(mods []*packages.Module) []*Module {
vmods := make([]*Module, len(mods))
convertMod := newModuleConverter()
for i, mod := range mods {
vmods[i] = convertMod(mod)
}
return vmods
}
// findPackageModule returns the path of a module that could contain the import
// path pkg. It uses paths only. It is possible but unlikely for a package path
// to match two or more different module paths. We just take the first one.
// If no module path matches, findPackageModule returns the empty string.
func findPackageModule(pkg string, mods []*Module) string {
if isStdPackage(pkg) {
return stdlibModule.Path
}
for _, m := range mods {
if pkg == m.Path || strings.HasPrefix(pkg, m.Path+"/") {
return m.Path
}
}
return ""
}
// findSetting returns value of setting from bi if present.
// Otherwise, returns "".
func findSetting(setting string, bi *debug.BuildInfo) string {
for _, s := range bi.Settings {
if s.Key == setting {
return s.Value
}
}
return ""
}