-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathapk.go
More file actions
440 lines (365 loc) · 13.1 KB
/
apk.go
File metadata and controls
440 lines (365 loc) · 13.1 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package scan
import (
"context"
"fmt"
"io/fs"
"os"
"path"
"regexp"
"slices"
"strings"
"time"
"github.com/adrg/xdg"
"github.com/anchore/grype/grype"
v6 "github.com/anchore/grype/grype/db/v6"
"github.com/anchore/grype/grype/db/v6/distribution"
"github.com/anchore/grype/grype/db/v6/installation"
"github.com/anchore/grype/grype/match"
"github.com/anchore/grype/grype/matcher"
"github.com/anchore/grype/grype/matcher/dotnet"
"github.com/anchore/grype/grype/matcher/golang"
"github.com/anchore/grype/grype/matcher/java"
"github.com/anchore/grype/grype/matcher/javascript"
"github.com/anchore/grype/grype/matcher/python"
"github.com/anchore/grype/grype/matcher/ruby"
"github.com/anchore/grype/grype/matcher/rust"
"github.com/anchore/grype/grype/matcher/stock"
grypePkg "github.com/anchore/grype/grype/pkg"
"github.com/anchore/grype/grype/vulnerability"
"github.com/anchore/syft/syft/cpe"
"github.com/anchore/syft/syft/pkg"
sbomSyft "github.com/anchore/syft/syft/sbom"
"github.com/chainguard-dev/clog"
"github.com/charmbracelet/log"
anchorelogger "github.com/wolfi-dev/wolfictl/pkg/anchorelog"
"github.com/wolfi-dev/wolfictl/pkg/sbom"
)
const (
mavenSearchBaseURL = "https://search.maven.org/solrsearch/select"
)
var DefaultGrypeDBDir = path.Join(xdg.CacheHome, "wolfictl", "grype", "db")
type Result struct {
TargetAPK TargetAPK
Findings []Finding
GrypeDBStatus *v6.Status
}
type TargetAPK struct {
Name string
Version string
OriginPackageName string
}
// Origin returns the name of the origin package, if the package's metadata
// indicates an origin package. Otherwise, it returns the package name.
func (t TargetAPK) Origin() string {
if t.OriginPackageName != "" {
return t.OriginPackageName
}
return t.Name
}
func newTargetAPK(s *sbomSyft.SBOM) (TargetAPK, error) {
// There should be exactly one APK package in the SBOM, and it should be the APK
// we intended to scan.
pkgs := s.Artifacts.Packages.Sorted(pkg.ApkPkg)
if len(pkgs) != 1 {
return TargetAPK{}, fmt.Errorf("expected exactly one APK package, found %d", len(pkgs))
}
p := pkgs[0]
metadata, ok := p.Metadata.(pkg.ApkDBEntry)
if !ok {
return TargetAPK{}, fmt.Errorf("expected APK metadata, found %T", p.Metadata)
}
return TargetAPK{
Name: p.Name,
Version: p.Version,
OriginPackageName: metadata.OriginPackage,
}, nil
}
type Scanner struct {
vulnProvider vulnerability.Provider
dbStatus *v6.Status
vulnerabilityMatcher *grype.VulnerabilityMatcher
disableSBOMCache bool
}
// Options determine the configuration for a new Scanner. The zero-value of this
// struct is a valid configuration.
type Options struct {
// PathOfDatabaseArchiveToImport, if set, is the path to a Grype vulnerability
// database archive (.tar.gz file) from which a database will be loaded by
// Grype.
//
// If empty, the default Grype database loading behavior will be used (e.g.
// downloading the database from the Internet).
PathOfDatabaseArchiveToImport string
// PathOfDatabaseDestinationDirectory is the directory to which the Grype
// database will be extracted, and where the database will be loaded from at
// runtime. If empty, the value of DefaultGrypeDBDir will be used.
PathOfDatabaseDestinationDirectory string
// UseCPEs controls whether the scanner will use CPEs to match vulnerabilities
// for matcher types that default to not using CPE matching. Most consumers will
// probably want this set to false in order to avoid excessive noise from
// matching.
UseCPEs bool
// DisableDatabaseAgeValidation controls whether the scanner will validate the
// age of the vulnerability database before using it. If true, the scanner will
// not validate the age of the database. This bool should always be set to false
// except for testing purposes.
DisableDatabaseAgeValidation bool
// DisableSBOMCache controls whether the scanner will cache SBOMs generated from
// APKs. If true, the scanner will not cache SBOMs or use existing cached SBOMs.
DisableSBOMCache bool
}
// DefaultOptions is the recommended default configuration for a new Scanner.
// These options are suitable for most use scanning cases.
var DefaultOptions = Options{}
// NewScanner initializes the grype DB for reuse across multiple scans.
func NewScanner(opts Options) (*Scanner, error) {
dbDestDir := opts.PathOfDatabaseDestinationDirectory
if dbDestDir == "" {
dbDestDir = DefaultGrypeDBDir
}
// Default to 24 hours if GRYPE_DB_MAX_ALLOWED_BUILT_AGE is unset
maxAllowedBuiltAge := 24 * time.Hour
grypeMaxAllowedBuiltAge := os.Getenv("GRYPE_DB_MAX_ALLOWED_BUILT_AGE")
if grypeMaxAllowedBuiltAge != "" {
parseMaxAllowedBuiltAge, err := time.ParseDuration(grypeMaxAllowedBuiltAge)
if err != nil {
return nil, fmt.Errorf("could not parse GRYPE_DB_MAX_ALLOWED_BUILT_AGE: %w", err)
}
maxAllowedBuiltAge = parseMaxAllowedBuiltAge
}
installCfg := installation.Config{
DBRootDir: dbDestDir,
ValidateChecksum: true,
ValidateAge: !opts.DisableDatabaseAgeValidation,
MaxAllowedBuiltAge: maxAllowedBuiltAge,
UpdateCheckMaxFrequency: 1 * time.Hour,
}
distCfg := distribution.DefaultConfig()
distClient, err := distribution.NewClient(distCfg)
if err != nil {
return nil, fmt.Errorf("creating distribution client: %w", err)
}
updateDB := true
if dbArchivePath := opts.PathOfDatabaseArchiveToImport; dbArchivePath != "" {
fmt.Fprintf(os.Stderr, "using local grype DB archive %q...\n", dbArchivePath)
dbCurator, err := installation.NewCurator(installCfg, distClient)
if err != nil {
return nil, fmt.Errorf("unable to create the grype db import config: %w", err)
}
if err := dbCurator.Import(dbArchivePath); err != nil {
return nil, fmt.Errorf("unable to import vulnerability database: %w", err)
}
updateDB = false
}
vulnProvider, dbStatus, err := grype.LoadVulnerabilityDB(distCfg, installCfg, updateDB)
if err != nil {
return nil, fmt.Errorf("failed to load vulnerability database: %w", err)
}
vulnerabilityMatcher := NewGrypeVulnerabilityMatcher(vulnProvider, opts.UseCPEs)
return &Scanner{
vulnProvider: vulnProvider,
dbStatus: dbStatus,
vulnerabilityMatcher: vulnerabilityMatcher,
disableSBOMCache: opts.DisableSBOMCache,
}, nil
}
// ScanAPK scans an APK file for vulnerabilities.
func (s *Scanner) ScanAPK(ctx context.Context, apk fs.File, distroID string) (*Result, error) {
logger := clog.FromContext(ctx)
stat, err := apk.Stat()
if err != nil {
return nil, fmt.Errorf("failed to stat APK file: %w", err)
}
logger.Info("scanning APK for vulnerabilities", "path", stat.Name())
var ssbom *sbomSyft.SBOM
if s.disableSBOMCache {
ssbom, err = sbom.Generate(ctx, stat.Name(), apk, distroID)
} else {
ssbom, err = sbom.CachedGenerate(ctx, stat.Name(), apk, distroID)
}
if err != nil {
return nil, fmt.Errorf("failed to generate SBOM from APK: %w", err)
}
return s.APKSBOM(ctx, ssbom)
}
// APKSBOM scans an SBOM of an APK for vulnerabilities.
func (s *Scanner) APKSBOM(ctx context.Context, ssbom *sbomSyft.SBOM) (*Result, error) {
logger := clog.FromContext(ctx)
logger.Debug("scanning APK SBOM for vulnerabilities", "packageCount", ssbom.Artifacts.Packages.PackageCount())
apk, err := newTargetAPK(ssbom)
if err != nil {
return nil, err
}
grype.SetLogger(anchorelogger.NewSlogAdapter(logger.Base()))
syftPkgs := ssbom.Artifacts.Packages.Sorted()
grypePkgs := grypePkg.FromPackages(syftPkgs, grypePkg.SynthesisConfig{GenerateMissingCPEs: false})
logger.Info("converted packages to grype packages", "packageCount", len(grypePkgs))
// Find vulnerability matches
matchesCollection, _, err := s.vulnerabilityMatcher.FindMatches(grypePkgs, grypePkg.Context{
Source: &ssbom.Source,
Distro: ssbom.Artifacts.LinuxDistribution,
})
if err != nil {
return nil, fmt.Errorf("failed to find vulnerability matches: %w", err)
}
logger.Debug("grype matching finished", "matchCount", matchesCollection.Count())
matches := matchesCollection.Sorted()
var findings []Finding
for i := range matches {
m := matches[i]
if allow, reason := shouldAllowMatch(m); !allow {
log.Info(
"match deemed invalid, dropping from results",
"vulnerabilityID",
m.Vulnerability.ID,
"componentName",
m.Package.Name,
"componentVersion",
m.Package.Version,
"componentType",
m.Package.Type,
"reason",
reason,
)
continue
}
finding, err := mapMatchToFinding(m, s.vulnProvider)
if err != nil {
return nil, fmt.Errorf("failed to map match to finding: %w", err)
}
if finding == nil {
return nil, fmt.Errorf("failed to map match to finding: nil")
}
findings = append(findings, *finding)
}
result := &Result{
TargetAPK: apk,
Findings: findings,
GrypeDBStatus: s.dbStatus,
}
return result, nil
}
// shouldAllowMatch is a point where we can optionally filter out matches from
// the scan based on criteria we define. This function will return true unless
// it determines that the match should be dropped from the final result set. In
// this latter case, it also returns a string explanation of the reason for
// dropping the match.
func shouldAllowMatch(m match.Match) (allow bool, reason string) {
// For now, since our new changes are centered on Go, allow all non-Go matches
// to minimize unexpected disruption in scanning. We can widen the scope of this
// filtering to other ecosystems when we're comfortable with it.
if m.Package.Type != pkg.GoModulePkg {
return true, ""
}
// Also exempt the Go stdlib.
if m.Package.Name == "stdlib" {
return true, ""
}
for _, d := range m.Details {
if d.Type != match.CPEMatch {
continue
}
// Be especially critical of CPE-based results...
r, ok := d.Found.(match.CPEResult)
if !ok {
continue
}
p, ok := d.SearchedBy.(match.CPEParameters)
if !ok {
continue
}
// Drop matches where the version constraint is totally nonexistent, to reduce
// false positives.
if strings.HasPrefix(r.VersionConstraint, "none") {
return false, "CPE has no version constraint"
}
// Drop matches where there's no fix.
if m.Vulnerability.Fix.State != vulnerability.FixStateFixed {
return false, "CPE-based match with no fix available"
}
// Older golang.org/x repositories have versions like "2019-03-20". This will
// create false positives when versions are sorted, so let's drop these.
if len(m.Vulnerability.Fix.Versions) != 1 {
continue
}
// Only use CPEs from a trusted source.
if !isMatchFromTrustedCPESource(p.CPEs, m.Package.CPEs) {
return false, "CPE-based match from untrusted CPE source"
}
f := m.Vulnerability.Fix.Versions[0]
if regexGolangDateVersion.MatchString(f) {
return false, "CPE-based match unexpected version format ('XXXX-YY-ZZ')"
}
}
return true, ""
}
func isMatchFromTrustedCPESource(searchCPEs []string, packageCPEs []cpe.CPE) bool {
// Basically allow everything except "syft-generated".
// First we'll build a lookup table of CPEs to sources, and then use that to
// determine if the CPE(s) we matched on came from at least one trusted source.
sourcesByCPE := make(map[string][]cpe.Source)
for i := range packageCPEs {
c := packageCPEs[i]
cpeStr := c.Attributes.BindToFmtString()
sourcesByCPE[cpeStr] = append(sourcesByCPE[cpeStr], c.Source)
}
for i := range searchCPEs {
c := searchCPEs[i]
srcs := sourcesByCPE[c]
if len(srcs) == 0 {
continue
}
for _, src := range srcs {
if slices.Contains(trustedCPESources, src) {
return true
}
}
}
return false
}
var trustedCPESources = []cpe.Source{
sbom.CPESourceWolfictl,
sbom.CPESourceMelangeConfiguration,
cpe.NVDDictionaryLookupSource,
}
var regexGolangDateVersion = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
// Close closes the scanner's database connection.
func (s *Scanner) Close() {
if s.vulnProvider == nil {
return
}
if err := s.vulnProvider.Close(); err != nil {
clog.FromContext(context.Background()).Warnf("failed to close grype database: %v", err)
}
}
func NewGrypeVulnerabilityMatcher(vulnProvider vulnerability.Provider, useCPEs bool) *grype.VulnerabilityMatcher {
return &grype.VulnerabilityMatcher{
VulnerabilityProvider: vulnProvider,
Matchers: createMatchers(useCPEs),
}
}
func createMatchers(useCPEs bool) []match.Matcher {
return matcher.NewDefaultMatchers(
matcher.Config{
Dotnet: dotnet.MatcherConfig{UseCPEs: useCPEs},
Golang: golang.MatcherConfig{
UseCPEs: true, // note: disregarding --use-cpes flag value
AlwaysUseCPEForStdlib: true,
AllowMainModulePseudoVersionComparison: false,
},
Java: java.MatcherConfig{
ExternalSearchConfig: java.ExternalSearchConfig{
SearchMavenUpstream: true,
MavenBaseURL: mavenSearchBaseURL,
MavenRateLimit: 400 * time.Millisecond, // increased from the default of 300ms to avoid rate limiting with extremely large set of java packages such as druid
},
UseCPEs: useCPEs,
},
Javascript: javascript.MatcherConfig{UseCPEs: useCPEs},
Python: python.MatcherConfig{UseCPEs: useCPEs},
Ruby: ruby.MatcherConfig{UseCPEs: useCPEs},
Rust: rust.MatcherConfig{UseCPEs: useCPEs},
Stock: stock.MatcherConfig{UseCPEs: true},
},
)
}