forked from anchore/grype-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.go
More file actions
408 lines (356 loc) · 10.9 KB
/
transform.go
File metadata and controls
408 lines (356 loc) · 10.9 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
package os
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/scylladb/go-set/strset"
"github.com/anchore/grype-db/internal/log"
"github.com/anchore/grype-db/pkg/data"
"github.com/anchore/grype-db/pkg/process/internal/codename"
"github.com/anchore/grype-db/pkg/process/internal/common"
"github.com/anchore/grype-db/pkg/process/v6/transformers"
"github.com/anchore/grype-db/pkg/process/v6/transformers/internal"
"github.com/anchore/grype-db/pkg/provider"
"github.com/anchore/grype-db/pkg/provider/unmarshal"
grypeDB "github.com/anchore/grype/grype/db/v6"
"github.com/anchore/grype/grype/db/v6/name"
"github.com/anchore/grype/grype/distro"
"github.com/anchore/syft/syft/pkg"
)
// advisoryKey is an internal struct used for sorting and deduplicating advisories
// that have both a link and ID from the vunnel results data
type advisoryKey struct {
id string
link string
}
func Transform(vulnerability unmarshal.OSVulnerability, state provider.State) ([]data.Entry, error) {
in := []any{
grypeDB.VulnerabilityHandle{
Name: vulnerability.Vulnerability.Name,
ProviderID: state.Provider,
Provider: internal.ProviderModel(state),
Status: grypeDB.VulnerabilityActive,
ModifiedDate: internal.ParseTime(vulnerability.Vulnerability.Metadata.Updated),
PublishedDate: internal.ParseTime(vulnerability.Vulnerability.Metadata.Issued),
BlobValue: &grypeDB.VulnerabilityBlob{
ID: vulnerability.Vulnerability.Name,
Assigners: nil,
Description: strings.TrimSpace(vulnerability.Vulnerability.Description),
References: getReferences(vulnerability),
Aliases: getAliases(vulnerability),
Severities: getSeverities(vulnerability),
},
},
}
for _, a := range getAffectedPackages(vulnerability) {
in = append(in, a)
}
return transformers.NewEntries(in...), nil
}
func getAffectedPackages(vuln unmarshal.OSVulnerability) []grypeDB.AffectedPackageHandle {
var afs []grypeDB.AffectedPackageHandle
groups := groupFixedIns(vuln)
for group, fixedIns := range groups {
// we only care about a single qualifier: rpm modules. The important thing to note about this is that
// a package with no module vs a package with a module should be detectable in the DB.
var qualifiers *grypeDB.PackageQualifiers
if group.format == "rpm" {
module := "" // means the target package must have no module (where as nil means the module has no sway on matching)
if group.hasModule {
module = group.module
}
qualifiers = &grypeDB.PackageQualifiers{
RpmModularity: &module,
}
}
aph := grypeDB.AffectedPackageHandle{
OperatingSystem: getOperatingSystem(group.osName, group.id, group.osVersion, group.osChannel),
Package: getPackage(group),
BlobValue: &grypeDB.PackageBlob{
CVEs: getAliases(vuln),
Qualifiers: qualifiers,
Ranges: nil,
},
}
var ranges []grypeDB.Range
for _, fixedInEntry := range fixedIns {
ranges = append(ranges, grypeDB.Range{
Version: grypeDB.Version{
Type: fixedInEntry.VersionFormat,
Constraint: enforceConstraint(fixedInEntry.Version, fixedInEntry.VulnerableRange, fixedInEntry.VersionFormat, vuln.Vulnerability.Name),
},
Fix: getFix(fixedInEntry),
})
}
aph.BlobValue.Ranges = ranges
afs = append(afs, aph)
}
// stable ordering
sort.Sort(internal.ByAffectedPackage(afs))
return afs
}
func getFix(fixedInEntry unmarshal.OSFixedIn) *grypeDB.Fix {
fixedInVersion := common.CleanFixedInVersion(fixedInEntry.Version)
fixState := grypeDB.NotFixedStatus
if len(fixedInVersion) > 0 {
fixState = grypeDB.FixedStatus
} else if fixedInEntry.VendorAdvisory.NoAdvisory {
fixState = grypeDB.WontFixStatus
}
var advisoryOrder []advisoryKey
advisorySet := strset.New()
for _, a := range fixedInEntry.VendorAdvisory.AdvisorySummary {
if a.Link != "" && !advisorySet.Has(a.Link) {
advisoryOrder = append(advisoryOrder, advisoryKey{id: a.ID, link: a.Link})
advisorySet.Add(a.Link)
}
}
var refs []grypeDB.Reference
for _, adv := range advisoryOrder {
refs = append(refs, grypeDB.Reference{
ID: adv.id,
URL: adv.link,
Tags: []string{grypeDB.AdvisoryReferenceTag},
})
}
var detail *grypeDB.FixDetail
availability := getFixAvailability(fixedInEntry)
if len(refs) > 0 || availability != nil {
detail = &grypeDB.FixDetail{
Available: availability,
References: refs,
}
}
return &grypeDB.Fix{
Version: fixedInVersion,
State: fixState,
Detail: detail,
}
}
func getFixAvailability(fixedInEntry unmarshal.OSFixedIn) *grypeDB.FixAvailability {
if fixedInEntry.Available.Date == "" {
return nil
}
t := internal.ParseTime(fixedInEntry.Available.Date)
if t == nil {
log.WithFields("date", fixedInEntry.Available.Date).Warn("unable to parse fix availability date")
return nil
}
return &grypeDB.FixAvailability{
Date: t,
Kind: fixedInEntry.Available.Kind,
}
}
func enforceConstraint(fixedVersion, vulnerableRange, format, vulnerabilityID string) string {
if len(vulnerableRange) > 0 {
return vulnerableRange
}
fixedVersion = common.CleanConstraint(fixedVersion)
if len(fixedVersion) == 0 {
return ""
}
switch strings.ToLower(format) {
case "semver":
return common.EnforceSemVerConstraint(fixedVersion)
default:
// the passed constraint is a fixed version
return deriveConstraintFromFix(fixedVersion, vulnerabilityID)
}
}
func deriveConstraintFromFix(fixVersion, vulnerabilityID string) string {
constraint := fmt.Sprintf("< %s", fixVersion)
if strings.HasPrefix(vulnerabilityID, "ALASKERNEL-") {
// Amazon advisories of the form ALASKERNEL-5.4-2023-048 should be interpreted as only applying to
// the 5.4.x kernel line since Amazon issue a separate advisory per affected line, thus the constraint
// should be >= 5.4, < {fix version}. In the future the vunnel schema for OS vulns should be enhanced
// to emit actual constraints rather than fixed-in entries (tracked in https://github.com/anchore/vunnel/issues/266)
// at which point this workaround in grype-db can be removed.
components := strings.Split(vulnerabilityID, "-")
if len(components) == 4 {
base := components[1]
constraint = fmt.Sprintf(">= %s, < %s", base, fixVersion)
}
}
return constraint
}
type groupIndex struct {
name string
id string
osName string
osVersion string
osChannel string
hasModule bool
module string
format string
}
func groupFixedIns(vuln unmarshal.OSVulnerability) map[groupIndex][]unmarshal.OSFixedIn {
grouped := make(map[groupIndex][]unmarshal.OSFixedIn)
oi := getOSInfo(vuln.Vulnerability.NamespaceName)
for _, fixedIn := range vuln.Vulnerability.FixedIn {
var mod string
if fixedIn.Module != nil {
mod = *fixedIn.Module
}
g := groupIndex{
name: fixedIn.Name,
id: oi.id,
osName: oi.name,
osVersion: oi.version,
osChannel: oi.channel,
hasModule: fixedIn.Module != nil,
module: mod,
format: fixedIn.VersionFormat,
}
grouped[g] = append(grouped[g], fixedIn)
}
return grouped
}
func getPackageType(osName string) pkg.Type {
switch osName {
case "redhat", "amazonlinux", "oraclelinux", "sles", "mariner", "azurelinux":
return pkg.RpmPkg
case "ubuntu", "debian", "echo":
return pkg.DebPkg
case "alpine", "chainguard", "wolfi", "minimos", "secureos":
return pkg.ApkPkg
case "windows":
return pkg.KbPkg
}
return ""
}
func getPackage(group groupIndex) *grypeDB.Package {
t := getPackageType(group.osName)
return &grypeDB.Package{
Ecosystem: string(t),
Name: name.Normalize(group.name, t),
}
}
type osInfo struct {
name string
id string
version string
channel string
}
func getOSInfo(group string) osInfo {
// derived from enterprise feed groups, expected to be of the form {distro release ID}:{version}
feedGroupComponents := strings.Split(group, ":")
id := feedGroupComponents[0]
version := feedGroupComponents[1]
channel := ""
if strings.Contains(feedGroupComponents[1], "+") {
versionParts := strings.Split(feedGroupComponents[1], "+")
channel = versionParts[1]
version = versionParts[0]
}
if strings.ToLower(id) == "mariner" {
verFields := strings.Split(version, ".")
majorVersionStr := verFields[0]
majorVer, err := strconv.Atoi(majorVersionStr)
if err == nil {
if majorVer >= 3 {
id = string(distro.Azure)
}
}
}
return osInfo{
name: normalizeOsName(id),
id: id,
version: version,
channel: channel,
}
}
func normalizeOsName(id string) string {
d, ok := distro.IDMapping[id]
if !ok {
log.WithFields("distro", id).Warn("unknown distro name")
return id
}
return d.String()
}
func getOperatingSystem(osName, osID, osVersion, channel string) *grypeDB.OperatingSystem {
if osName == "" || osVersion == "" {
return nil
}
versionFields := strings.Split(osVersion, ".")
var majorVersion, minorVersion, labelVersion string
majorVersion = versionFields[0]
if len(majorVersion) > 0 {
// is the first field a number?
_, err := strconv.Atoi(majorVersion[0:1])
if err != nil {
labelVersion = majorVersion
majorVersion = ""
} else if len(versionFields) > 1 {
minorVersion = versionFields[1]
}
}
return &grypeDB.OperatingSystem{
Name: osName,
ReleaseID: osID,
MajorVersion: majorVersion,
MinorVersion: minorVersion,
LabelVersion: labelVersion,
Channel: channel,
Codename: codename.LookupOS(osName, majorVersion, minorVersion),
}
}
func getReferences(vuln unmarshal.OSVulnerability) []grypeDB.Reference {
clean := strings.TrimSpace(vuln.Vulnerability.Link)
if clean == "" {
return nil
}
var linkOrder []string
linkSet := strset.New()
if vuln.Vulnerability.Link != "" {
linkSet.Add(vuln.Vulnerability.Link)
linkOrder = append(linkOrder, vuln.Vulnerability.Link)
}
for _, a := range vuln.Vulnerability.Metadata.CVE {
if a.Link != "" && !linkSet.Has(a.Link) {
linkOrder = append(linkOrder, a.Link)
}
}
var refs []grypeDB.Reference
for _, l := range linkOrder {
refs = append(refs,
grypeDB.Reference{
URL: l,
},
)
}
return refs
}
func getAliases(vuln unmarshal.OSVulnerability) []string {
var aliases []string
for _, cve := range vuln.Vulnerability.Metadata.CVE {
aliases = append(aliases,
cve.Name,
)
}
return aliases
}
func getSeverities(vuln unmarshal.OSVulnerability) []grypeDB.Severity {
var severities []grypeDB.Severity
// TODO: should we clean this here or not?
if vuln.Vulnerability.Severity != "" && strings.ToLower(vuln.Vulnerability.Severity) != "unknown" {
severities = append(severities, grypeDB.Severity{
Scheme: grypeDB.SeveritySchemeCHMLN,
Value: strings.ToLower(vuln.Vulnerability.Severity),
Rank: 1, // TODO: enum this
// TODO Source?
})
}
for _, vendorSeverity := range vuln.Vulnerability.CVSS {
severities = append(severities, grypeDB.Severity{
Scheme: grypeDB.SeveritySchemeCVSS,
Value: grypeDB.CVSSSeverity{
Vector: vendorSeverity.VectorString,
Version: vendorSeverity.Version,
},
Rank: 2,
// TODO: source?
})
}
return severities
}