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: correct APK CPE version comparison logic #1165

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 34 additions & 1 deletion grype/matcher/apk/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apk

import (
"fmt"
"strings"

"github.com/anchore/grype/grype/distro"
"github.com/anchore/grype/grype/match"
Expand Down Expand Up @@ -45,7 +46,8 @@ func (m *Matcher) Match(store vulnerability.Provider, d *distro.Distro, p pkg.Pa

func (m *Matcher) cpeMatchesWithoutSecDBFixes(store vulnerability.Provider, d *distro.Distro, p pkg.Package) ([]match.Match, error) {
// find CPE-indexed vulnerability matches specific to the given package name and version
cpeMatches, err := search.ByPackageCPE(store, p, m.Type())
cpePackage := cpeComparablePackage(p)
cpeMatches, err := search.ByPackageCPE(store, cpePackage, m.Type())
Copy link
Contributor

Choose a reason for hiding this comment

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

we should be surfacing matches with the original package, the match details should have the necessary (mutated) details. This way we surface the vuln, the original package [from the SBOM as we understand it], and the match details showing what values were used in the search.

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -101,6 +103,37 @@ cveLoop:
return finalCpeMatches, nil
}

func cpeComparablePackage(p pkg.Package) pkg.Package {
// clean the alpine package version so that it compares correctly with the CPE version comparison logic
// alpine versions are suffixed with -r{buildindex}; however, if left intact CPE comparison logic will
// incorrectly treat these as a pre-release. In actuality, we just want to treat 1.2.3-r21 as equivalent to
// 1.2.3 for purposes of CPE-based matching since the alpine fix should filter out any cases where a later
// build fixes something that was vulnerable in 1.2.3
components := strings.Split(p.Version, "-r")
cpeComparableVersion := p.Version

if len(components) == 2 {
cpeComparableVersion = components[0]
}

if cpeComparableVersion == p.Version {
return p
}

t := p
t.CPEs = nil
t.CPEs = append(t.CPEs, p.CPEs...)

for index, cpe := range t.CPEs {
if cpe.Version == p.Version {
cpe.Version = cpeComparableVersion
t.CPEs[index] = cpe
}
}

return t
}

func deduplicateMatches(secDBMatches, cpeMatches []match.Match) (matches []match.Match) {
// add additional unique matches from CPE source that is unique from the SecDB matches
secDBMatchesByID := matchesByID(secDBMatches)
Expand Down
75 changes: 75 additions & 0 deletions grype/matcher/apk/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,81 @@ func TestNvdOnlyMatches(t *testing.T) {
assertMatches(t, expected, actual)
}

func TestNvdMatchesProperVersionFiltering(t *testing.T) {
nvdVulnMatch := grypeDB.Vulnerability{
ID: "CVE-2020-1",
VersionConstraint: "<= 0.9.11",
VersionFormat: "unknown",
CPEs: []string{`cpe:2.3:a:lib_vnc_project-\(server\):libvncserver:*:*:*:*:*:*:*:*`},
Namespace: "nvd:cpe",
}
nvdVulnNoMatch := grypeDB.Vulnerability{
ID: "CVE-2020-2",
VersionConstraint: "< 0.9.11",
VersionFormat: "unknown",
CPEs: []string{`cpe:2.3:a:lib_vnc_project-\(server\):libvncserver:*:*:*:*:*:*:*:*`},
Namespace: "nvd:cpe",
}
store := mockStore{
backend: map[string]map[string][]grypeDB.Vulnerability{
"nvd:cpe": {
"libvncserver": []grypeDB.Vulnerability{nvdVulnMatch, nvdVulnNoMatch},
},
},
}

provider, err := db.NewVulnerabilityProvider(&store)
require.NoError(t, err)

m := Matcher{}
d, err := distro.New(distro.Alpine, "3.12.0", "")
if err != nil {
t.Fatalf("failed to create a new distro: %+v", err)
}
p := pkg.Package{
ID: pkg.ID(uuid.NewString()),
Name: "libvncserver",
Version: "0.9.11-r10",
Type: syftPkg.ApkPkg,
CPEs: []cpe.CPE{
cpe.Must("cpe:2.3:a:*:libvncserver:0.9.11:*:*:*:*:*:*:*"),
},
}

vulnFound, err := vulnerability.NewVulnerability(nvdVulnMatch)
assert.NoError(t, err)
vulnFound.CPEs = []cpe.CPE{cpe.Must(nvdVulnMatch.CPEs[0])}

expected := []match.Match{
{

Vulnerability: *vulnFound,
Package: p,
Details: []match.Detail{
{
Type: match.CPEMatch,
Confidence: 0.9,
SearchedBy: search.CPEParameters{
CPEs: []string{"cpe:2.3:a:*:libvncserver:0.9.11:*:*:*:*:*:*:*"},
Namespace: "nvd:cpe",
},
Found: search.CPEResult{
CPEs: []string{vulnFound.CPEs[0].BindToFmtString()},
VersionConstraint: vulnFound.Constraint.String(),
VulnerabilityID: "CVE-2020-1",
},
Matcher: match.ApkMatcher,
},
},
},
}

actual, err := m.Match(provider, d, p)
assert.NoError(t, err)

assertMatches(t, expected, actual)
}

func TestNvdMatchesWithSecDBFix(t *testing.T) {
nvdVuln := grypeDB.Vulnerability{
ID: "CVE-2020-1",
Expand Down
2 changes: 1 addition & 1 deletion test/quality/vulnerability-match-labels
Submodule vulnerability-match-labels updated 37 files
+1 −1 .yardstick.yaml
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/52616137-a980-4a3d-8201-18d3d0c1b357.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/552e5171-93d4-41bb-9b2b-c8cc50fb78af.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/be838420-aa57-44e6-ab3c-1b8e31fef60c.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/bfc18374-6aa2-4320-97d8-b0548a2264f3.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/c3444b9c-9ab6-47fe-a9d2-9036a7c09e8f.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/dc7960ec-9828-412c-80f6-3c8c45957eee.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/e057d36c-87c0-4f56-9f1c-faa61874a10f.json
+1 −0 ...:ddac200f3ebc9902fb8cfcd599f41feb2151f1118929da21bcef57dc276975f9/e214b93f-8cb6-4b86-9e72-8cdbfa4f3ddd.json
+0 −1 ...:0825acea611c7c5cc792bc7cc20de44d7413fd287dc5afc4aab9c1891d037b4f/8a8f9b4e-602b-4af8-a886-bbf12a2a53d2.json
+1 −0 ...:0825acea611c7c5cc792bc7cc20de44d7413fd287dc5afc4aab9c1891d037b4f/ad4ef1af-3e45-4652-9668-9fc159961e1a.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/154ad902-f791-4d65-a3e6-300fe083e8eb.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/1669c6ce-7b9c-4982-b49d-57f05f931f03.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/19a6f061-01fd-49d3-9793-818610a74f67.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/30be62ba-f2ef-40df-a46c-23fb7f4f455f.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/38f59a6c-3a61-4452-80df-925ffa845914.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/56564a20-c9e4-48d8-a885-5ab609e14194.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/590f3226-a269-4098-a171-ad01d4a062c6.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/5e0b016c-661c-42f3-bdba-a96af02db99a.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/6d47b739-15b3-43dc-84e7-971d2a42ffe5.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/80852dfd-6ecb-421d-9a8a-5697ae0cd524.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/9a6c99f7-065e-4ab3-97a6-b9a15872db2a.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/a4fe1ac2-e5dd-4564-b781-b53e1cf65fcc.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/ab61ac6f-b160-41ed-b455-9fca09d450af.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/d61897e8-4a74-41a2-88d5-aacca75092ea.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/da94f99a-638b-4d56-841f-d2f5541a7ae4.json
+1 −0 ...:58637f273108e3e9eb4df4d73f7b6b1da303cbbf64f65e65fb7762482f2de63d/f7d4ca52-3d6d-4999-876c-39dfec6d7b20.json
+1 −0 ...:a287a0ff98ac343aa710f4f4258d7198e240e9d416d5c7274663564202f832fb/79b40660-16d5-4ca9-8d69-d87c3ba256e6.json
+1 −0 ...:a287a0ff98ac343aa710f4f4258d7198e240e9d416d5c7274663564202f832fb/8c992fa9-de5e-4722-8ebe-a562e5ab584d.json
+0 −1 ...:a287a0ff98ac343aa710f4f4258d7198e240e9d416d5c7274663564202f832fb/d3878a73-531b-4295-b269-9fbc9ede3fbd.json
+1 −0 ...:a287a0ff98ac343aa710f4f4258d7198e240e9d416d5c7274663564202f832fb/e539fae2-a6ee-42c4-8362-e0b297290828.json
+0 −1 ...:ff80ae5de3446939639f3fbb58f66f641e2da1d881bdab704d9237424f64417f/03cc9a75-a562-4a03-a18c-802c9b1a0670.json
+1 −0 ...:ff80ae5de3446939639f3fbb58f66f641e2da1d881bdab704d9237424f64417f/2038059d-5e3f-4bec-8809-41bd10b6a5ee.json
+0 −1 ...:ff80ae5de3446939639f3fbb58f66f641e2da1d881bdab704d9237424f64417f/56079834-d63e-4cc6-9f30-7a95abf3ac22.json
+1 −0 ...:ff80ae5de3446939639f3fbb58f66f641e2da1d881bdab704d9237424f64417f/90e4a6c6-b669-40db-b053-df06821f7212.json
+1 −0 ...:ff80ae5de3446939639f3fbb58f66f641e2da1d881bdab704d9237424f64417f/dafe5ac8-8851-4891-92f6-30495b1ef4ab.json
+1 −1 requirements.txt