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: use fix info from secDB in APK matcher even if NVD fix info present #2162

Merged
merged 4 commits into from
Oct 7, 2024
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
13 changes: 12 additions & 1 deletion grype/matcher/apk/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"

grypeDB "github.com/anchore/grype/grype/db/v5"
"github.com/anchore/grype/grype/distro"
"github.com/anchore/grype/grype/match"
"github.com/anchore/grype/grype/pkg"
Expand Down Expand Up @@ -45,6 +46,7 @@ func (m *Matcher) Match(store vulnerability.Provider, d *distro.Distro, p pkg.Pa
return matches, nil
}

//nolint:funlen
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, d, p, m.Type())
Expand Down Expand Up @@ -88,7 +90,16 @@ cveLoop:
secDBVulnerabilitiesForID, exists := secDBVulnerabilitiesByID[id]
if !exists {
// does not exist in secdb, so the CPE record(s) should be added to the final results
finalCpeMatches = append(finalCpeMatches, cpeMatchesForID...)

// remove fixed-in versions, since NVD doesn't know when Alpine will fix things
for _, nvdOnlyMatch := range cpeMatchesForID {
if len(nvdOnlyMatch.Vulnerability.Fix.Versions) > 0 {
nvdOnlyMatch.Vulnerability.Fix = vulnerability.Fix{
State: grypeDB.UnknownFixState,
}
}
finalCpeMatches = append(finalCpeMatches, nvdOnlyMatch)
}
continue
}

Expand Down
179 changes: 179 additions & 0 deletions grype/matcher/apk/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,106 @@ func TestBothSecdbAndNvdMatches(t *testing.T) {
assertMatches(t, expected, actual)
}

func TestBothSecdbAndNvdMatches_DifferentFixInfo(t *testing.T) {
// NVD and Alpine's secDB both have the same CVE ID for the package
nvdVuln := grypeDB.Vulnerability{
ID: "CVE-2020-1",
VersionConstraint: "< 1.0.0",
VersionFormat: "unknown",
CPEs: []string{`cpe:2.3:a:lib_vnc_project-\(server\):libvncserver:*:*:*:*:*:*:*:*`},
Namespace: "nvd:cpe",
Fix: grypeDB.Fix{
Versions: []string{"1.0.0"},
State: grypeDB.FixedState,
},
}

secDbVuln := grypeDB.Vulnerability{
// ID *does* match - this is the key for comparison in the matcher
ID: "CVE-2020-1",
VersionConstraint: "< 0.9.12",
VersionFormat: "apk",
Namespace: "secdb:distro:alpine:3.12",
// SecDB indicates Alpine have backported a fix to v0.9...
Fix: grypeDB.Fix{
Versions: []string{"0.9.12"},
State: grypeDB.FixedState,
},
}
store := mockStore{
backend: map[string]map[string][]grypeDB.Vulnerability{
"nvd:cpe": {
"libvncserver": []grypeDB.Vulnerability{nvdVuln},
},
"secdb:distro:alpine:3.12": {
"libvncserver": []grypeDB.Vulnerability{secDbVuln},
},
},
}

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.9",
Type: syftPkg.ApkPkg,
CPEs: []cpe.CPE{
cpe.Must("cpe:2.3:a:*:libvncserver:0.9.9:*:*:*:*:*:*:*", ""),
},
}

// ensure the SECDB record is preferred over the NVD record
vulnFound, err := vulnerability.NewVulnerability(secDbVuln)
assert.NoError(t, err)
vulnFound.Fix = vulnerability.Fix{
Versions: secDbVuln.Fix.Versions,
State: secDbVuln.Fix.State,
}

expected := []match.Match{
{

Vulnerability: *vulnFound,
Package: p,
Details: []match.Detail{
{
Type: match.ExactDirectMatch,
Confidence: 1.0,
SearchedBy: map[string]interface{}{
"distro": map[string]string{
"type": d.Type.String(),
"version": d.RawVersion,
},
"package": map[string]string{
"name": "libvncserver",
"version": "0.9.9",
},
"namespace": "secdb:distro:alpine:3.12",
},
Found: map[string]interface{}{
"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 TestBothSecdbAndNvdMatches_DifferentPackageName(t *testing.T) {
// NVD and Alpine's secDB both have the same CVE ID for the package
nvdVuln := grypeDB.Vulnerability{
Expand Down Expand Up @@ -372,6 +472,85 @@ func TestNvdOnlyMatches(t *testing.T) {
assertMatches(t, expected, actual)
}

func TestNvdOnlyMatches_FixInNvd(t *testing.T) {
nvdVuln := 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",
Fix: grypeDB.Fix{
Versions: []string{"0.9.12"},
State: grypeDB.FixedState,
},
}
store := mockStore{
backend: map[string]map[string][]grypeDB.Vulnerability{
"nvd:cpe": {
"libvncserver": []grypeDB.Vulnerability{nvdVuln},
},
},
}

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.9",
Type: syftPkg.ApkPkg,
CPEs: []cpe.CPE{
cpe.Must("cpe:2.3:a:*:libvncserver:0.9.9:*:*:*:*:*:*:*", ""),
},
}

vulnFound, err := vulnerability.NewVulnerability(nvdVuln)
assert.NoError(t, err)
vulnFound.CPEs = []cpe.CPE{cpe.Must(nvdVuln.CPEs[0], "")}
// Important: for alpine matcher, fix version can come from secDB but _not_ from
// NVD data.
vulnFound.Fix = vulnerability.Fix{State: grypeDB.UnknownFixState}

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.9:*:*:*:*:*:*:*"},
Namespace: "nvd:cpe",
Package: search.CPEPackageParameter{
Name: "libvncserver",
Version: "0.9.9",
},
},
Found: search.CPEResult{
CPEs: []string{vulnFound.CPEs[0].Attributes.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 TestNvdMatchesProperVersionFiltering(t *testing.T) {
nvdVulnMatch := grypeDB.Vulnerability{
ID: "CVE-2020-1",
Expand Down
Loading