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

feat(go): parse main module of go binary files #6530

Merged
merged 11 commits into from
Apr 22, 2024
9 changes: 9 additions & 0 deletions docs/docs/coverage/language/golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ $ trivy rootfs ./your_binary
!!! note
It doesn't work with UPX-compressed binaries.

#### (devel) versions
There are times when Go uses the `(devel)` version for modules/dependencies and Trivy can't resolve them:

- Only Go binaries installed using the `go install` command contain correct (semver) version for the main module.
In other cases, Go uses the `(devel)` version[^3].
- Dependencies replaced with local ones use the `(devel)` versions.


[^1]: It doesn't require the Internet access.
[^2]: Need to download modules to local cache beforehand
[^3]: See https://github.com/aquasecurity/trivy/issues/1837#issuecomment-1832523477

[dependency-graph]: ../../configuration/reporting.md#show-origins-of-vulnerable-dependencies
21 changes: 17 additions & 4 deletions pkg/dependency/parser/golang/binary/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binary

import (
"debug/buildinfo"
"sort"
"strings"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -43,10 +44,21 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

libs := make([]types.Library, 0, len(info.Deps)+1)
DmitriyLewen marked this conversation as resolved.
Show resolved Hide resolved
libs = append(libs, types.Library{
Name: "stdlib",
Version: strings.TrimPrefix(info.GoVersion, "go"),
})
libs = append(libs, []types.Library{
{
// Add the Go version used to build this binary.
Name: "stdlib",
Version: strings.TrimPrefix(info.GoVersion, "go"),
},
{
// Add main module
Name: info.Main.Path,
// Only binaries installed with `go install` contain semver version of the main module.
// Other binaries use the `(devel)` version.
// See https://github.com/aquasecurity/trivy/issues/1837#issuecomment-1832523477.
Version: info.Main.Version,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to use an empty string rather than (devel). What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it.
I am concerned about user questions:
(devel) indicates that we have determined version. It's not correct version, but it's version that the Go binary contains.
If we use empty version - some users may say: this is bug, because some dependencies don't have versions (for example #6456 - the problem here is different, but even in such situations questions arise).

But we can write about this in the documentation.


So I checked a bit:
looks like it work good. e.g. purl simply doesn't contain version.
CycloneDX and SPDX work well:

{
      "bom-ref": "pkg:golang/github.com/aquasecurity/trivy",
      "type": "library",
      "name": "github.com/aquasecurity/trivy",
      "purl": "pkg:golang/github.com/aquasecurity/trivy",
      "properties": [
        {
          "name": "aquasecurity:trivy:PkgType",
          "value": "gobinary"
        }
      ]
    },
"name": "github.com/aquasecurity/trivy",
      "SPDXID": "SPDXRef-Package-5290f6d6347a1886",
      "supplier": "NOASSERTION",
      "downloadLocation": "NONE",
      "filesAnalyzed": false,
      "sourceInfo": "package found in: trivy",
      "licenseConcluded": "NONE",
      "licenseDeclared": "NONE",
      "externalRefs": [
        {
          "referenceCategory": "PACKAGE-MANAGER",
          "referenceType": "purl",
          "referenceLocator": "pkg:golang/github.com/aquasecurity/trivy"
        }
      ],
      "attributionTexts": [
        "PkgType: gobinary"
      ],
      "primaryPackagePurpose": "LIBRARY"

So I like your solution. We will get more correct purl. The (devel) version will not force users to look at the docs to understand what it is.

But then we also need to overwrite (devel) for dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used empty string instead of (devel) - 35f6401

},
}...)

for _, dep := range info.Deps {
// binaries with old go version may incorrectly add module in Deps
Expand All @@ -67,5 +79,6 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
})
}

sort.Sort(types.Libraries(libs))
return libs, nil, nil
}
62 changes: 46 additions & 16 deletions pkg/dependency/parser/golang/binary/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ func TestParse(t *testing.T) {
name: "ELF",
inputFile: "testdata/test.elf",
want: []types.Library{
{
Name: "stdlib",
Version: "1.15.2",
},
{
Name: "github.com/aquasecurity/go-pep440-version",
Version: "v0.0.0-20210121094942-22b2f8951d46",
Expand All @@ -34,20 +30,24 @@ func TestParse(t *testing.T) {
Name: "github.com/aquasecurity/go-version",
Version: "v0.0.0-20210121072130-637058cfe492",
},
{
Name: "github.com/aquasecurity/test",
Version: "(devel)",
},
{
Name: "golang.org/x/xerrors",
Version: "v0.0.0-20200804184101-5ec99f83aff1",
},
{
Name: "stdlib",
Version: "1.15.2",
},
},
},
{
name: "PE",
inputFile: "testdata/test.exe",
want: []types.Library{
{
Name: "stdlib",
Version: "1.15.2",
},
{
Name: "github.com/aquasecurity/go-pep440-version",
Version: "v0.0.0-20210121094942-22b2f8951d46",
Expand All @@ -56,20 +56,24 @@ func TestParse(t *testing.T) {
Name: "github.com/aquasecurity/go-version",
Version: "v0.0.0-20210121072130-637058cfe492",
},
{
Name: "github.com/aquasecurity/test",
Version: "(devel)",
},
{
Name: "golang.org/x/xerrors",
Version: "v0.0.0-20200804184101-5ec99f83aff1",
},
{
Name: "stdlib",
Version: "1.15.2",
},
},
},
{
name: "Mach-O",
inputFile: "testdata/test.macho",
want: []types.Library{
{
Name: "stdlib",
Version: "1.15.2",
},
{
Name: "github.com/aquasecurity/go-pep440-version",
Version: "v0.0.0-20210121094942-22b2f8951d46",
Expand All @@ -78,28 +82,54 @@ func TestParse(t *testing.T) {
Name: "github.com/aquasecurity/go-version",
Version: "v0.0.0-20210121072130-637058cfe492",
},
{
Name: "github.com/aquasecurity/test",
Version: "(devel)",
},
{
Name: "golang.org/x/xerrors",
Version: "v0.0.0-20200804184101-5ec99f83aff1",
},
{
Name: "stdlib",
Version: "1.15.2",
},
},
},
{
name: "with replace directive",
inputFile: "testdata/replace.elf",
want: []types.Library{
{
Name: "stdlib",
Version: "1.16.4",
},
{
Name: "github.com/davecgh/go-spew",
Version: "v1.1.1",
},
{
Name: "github.com/ebati/trivy-mod-parse",
Version: "(devel)",
},
{
Name: "github.com/go-sql-driver/mysql",
Version: "v1.5.0",
},
{
Name: "stdlib",
Version: "1.16.4",
},
},
},
{
name: "with semver main module version",
inputFile: "testdata/semver-main-module-version.macho",
want: []types.Library{
{
Name: "go.etcd.io/bbolt",
Version: "v1.3.5",
},
{
Name: "stdlib",
Version: "1.20.6",
},
},
},
{
Expand Down
Binary file not shown.
12 changes: 8 additions & 4 deletions pkg/fanal/analyzer/language/golang/binary/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func Test_gobinaryLibraryAnalyzer_Analyze(t *testing.T) {
Type: types.GoBinary,
FilePath: "testdata/executable_gobinary",
Libraries: types.Packages{
{
Name: "stdlib",
Version: "1.15.2",
},
{
Name: "github.com/aquasecurity/go-pep440-version",
Version: "v0.0.0-20210121094942-22b2f8951d46",
Expand All @@ -41,10 +37,18 @@ func Test_gobinaryLibraryAnalyzer_Analyze(t *testing.T) {
Name: "github.com/aquasecurity/go-version",
Version: "v0.0.0-20210121072130-637058cfe492",
},
{
Name: "github.com/aquasecurity/test",
Version: "(devel)",
},
{
Name: "golang.org/x/xerrors",
Version: "v0.0.0-20200804184101-5ec99f83aff1",
},
{
Name: "stdlib",
Version: "1.15.2",
},
},
},
},
Expand Down