Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't panic on universal go binaries #2078

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 3 additions & 40 deletions syft/pkg/cataloger/golang/parse_go_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"golang.org/x/mod/module"

"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
Expand Down Expand Up @@ -57,11 +56,11 @@ func (c *goBinaryCataloger) parseGoBinary(resolver file.Resolver, _ *generic.Env
return nil, nil, err
}

mods, archs := scanFile(unionReader, reader.RealPath)
mods := scanFile(unionReader, reader.RealPath)
internal.CloseAndLogError(reader.ReadCloser, reader.RealPath)

for i, mod := range mods {
pkgs = append(pkgs, c.buildGoPkgInfo(resolver, reader.Location, mod, archs[i])...)
for _, mod := range mods {
pkgs = append(pkgs, c.buildGoPkgInfo(resolver, reader.Location, mod, mod.arch)...)
}
return pkgs, nil, nil
}
Expand Down Expand Up @@ -151,42 +150,6 @@ func extractVersionFromLDFlags(ldflags string) (majorVersion string, fullVersion
return "", ""
}

// getArchs finds a binary architecture by two ways:
// 1) reading build info from binaries compiled by go1.18+
// 2) reading file headers from binaries compiled by < go1.18
func getArchs(readers []io.ReaderAt, builds []*extendedBuildInfo) []string {
if len(readers) != len(builds) {
log.Trace("golang cataloger: bin parsing: number of builds and readers doesn't match")
return nil
}

if len(readers) == 0 || len(builds) == 0 {
log.Tracef("golang cataloger: bin parsing: %d readers and %d build info items", len(readers), len(builds))
return nil
}

archs := make([]string, len(builds))
for i, build := range builds {
archs[i] = getGOARCH(build.Settings)
}

// if architecture was found via build settings return
if archs[0] != "" {
return archs
}

for i, r := range readers {
a, err := getGOARCHFromBin(r)
if err != nil {
log.Tracef("golang cataloger: bin parsing: getting arch from binary: %v", err)
continue
}

archs[i] = a
}
return archs
}

func getGOARCH(settings []debug.BuildSetting) string {
for _, s := range settings {
if s.Key == GOARCH {
Expand Down
93 changes: 50 additions & 43 deletions syft/pkg/cataloger/golang/parse_go_binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,12 @@ func TestBuildGoPkgInfo(t *testing.T) {
tests := []struct {
name string
mod *extendedBuildInfo
arch string
expected []pkg.Package
}{
{
name: "parse an empty mod",
mod: nil,
expected: []pkg.Package(nil),
},
{
name: "package without name",
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
Deps: []*debug.Module{
{
Path: "github.com/adrg/xdg",
Expand All @@ -177,7 +171,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
Version: "v0.2.1",
},
},
}, nil,
},
cryptoSettings: nil,
arch: "",
},
expected: []pkg.Package{
{
Expand All @@ -200,14 +196,13 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "buildGoPkgInfo parses a blank mod and returns no packages",
mod: &extendedBuildInfo{&debug.BuildInfo{}, nil},
mod: &extendedBuildInfo{&debug.BuildInfo{}, nil, ""},
expected: []pkg.Package(nil),
},
{
name: "parse a mod without main module",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Settings: []debug.BuildSetting{
{Key: "GOARCH", Value: archDetails},
Expand All @@ -221,7 +216,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
Sum: "h1:VSVdnH7cQ7V+B33qSJHTCRlNgra1607Q8PzEmnvb2Ic=",
},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -249,17 +246,18 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse a mod with path but no main module",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Settings: []debug.BuildSetting{
{Key: "GOARCH", Value: archDetails},
{Key: "GOOS", Value: "darwin"},
{Key: "GOAMD64", Value: "v1"},
},
Path: "github.com/a/b/c",
}, []string{"boringcrypto + fips"},
},
cryptoSettings: []string{"boringcrypto + fips"},
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -294,25 +292,25 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse a mod without packages",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
{Key: "GOARCH", Value: archDetails},
{Key: "GOOS", Value: "darwin"},
{Key: "GOAMD64", Value: "v1"},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{unmodifiedMain},
},
{
name: "parse main mod and replace devel pseudo version and ldflags exists (but contains no version)",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -323,7 +321,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
{Key: "vcs.time", Value: "2022-10-14T19:54:57Z"},
{Key: "-ldflags", Value: `build -ldflags="-w -s -extldflags '-static' -X blah=foobar`},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -359,9 +359,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse main mod and replace devel version with one from ldflags with vcs. build settings",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -372,7 +371,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
{Key: "vcs.time", Value: "2022-10-14T19:54:57Z"},
{Key: "-ldflags", Value: `build -ldflags="-w -s -extldflags '-static' -X github.com/anchore/syft/internal/version.version=0.79.0`},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -408,9 +409,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse main mod and replace devel version with one from ldflags without any vcs. build settings",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -419,7 +419,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
{Key: "GOAMD64", Value: "v1"},
{Key: "-ldflags", Value: `build -ldflags="-w -s -extldflags '-static' -X github.com/anchore/syft/internal/version.version=0.79.0`},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -453,9 +455,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse main mod and replace devel version with one from ldflags main.version without any vcs. build settings",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -464,7 +465,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
{Key: "GOAMD64", Value: "v1"},
{Key: "-ldflags", Value: `build -ldflags="-w -s -extldflags '-static' -X main.version=0.79.0`},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -498,9 +501,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse main mod and replace devel version with one from ldflags main.Version without any vcs. build settings",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -509,7 +511,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
{Key: "GOAMD64", Value: "v1"},
{Key: "-ldflags", Value: `build -ldflags="-w -s -extldflags '-static' -X main.Version=0.79.0`},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -543,9 +547,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse main mod and replace devel version with a pseudo version",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -555,7 +558,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
{Key: "vcs.revision", Value: "41bc6bb410352845f22766e27dd48ba93aa825a4"},
{Key: "vcs.time", Value: "2022-10-14T19:54:57Z"},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -590,9 +595,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse a populated mod string and returns packages but no source info",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -612,7 +616,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
Sum: "h1:DYssiUV1pBmKqzKsm4mqXx8artqC0Q8HgZsVI3lMsAg=",
},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -664,9 +670,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
{
name: "parse a populated mod string and returns packages when a replace directive exists",
arch: archDetails,
mod: &extendedBuildInfo{
&debug.BuildInfo{
BuildInfo: &debug.BuildInfo{
GoVersion: goCompiledVersion,
Main: debug.Module{Path: "github.com/anchore/syft", Version: "(devel)"},
Settings: []debug.BuildSetting{
Expand All @@ -691,7 +696,9 @@ func TestBuildGoPkgInfo(t *testing.T) {
},
},
},
}, nil,
},
cryptoSettings: nil,
arch: archDetails,
},
expected: []pkg.Package{
{
Expand Down Expand Up @@ -756,7 +763,7 @@ func TestBuildGoPkgInfo(t *testing.T) {
)

c := goBinaryCataloger{}
pkgs := c.buildGoPkgInfo(fileresolver.Empty{}, location, test.mod, test.arch)
pkgs := c.buildGoPkgInfo(fileresolver.Empty{}, location, test.mod, test.mod.arch)
assert.Equal(t, test.expected, pkgs)
})
}
Expand Down
Loading