-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathredhat-oval.go
501 lines (426 loc) · 12.9 KB
/
redhat-oval.go
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package redhatoval
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
bolt "go.etcd.io/bbolt"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy-db/pkg/utils"
"github.com/aquasecurity/trivy-db/pkg/utils/ints"
ustrings "github.com/aquasecurity/trivy-db/pkg/utils/strings"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
)
const (
rootBucket = "Red Hat"
)
var (
ovalDir = "oval"
cpeDir = "cpe"
vulnListDir = "vuln-list-redhat"
moduleRegexp = regexp.MustCompile(`Module\s+(.*)\s+is enabled`)
source = types.DataSource{
ID: vulnerability.RedHatOVAL,
Name: "Red Hat OVAL v2",
URL: "https://www.redhat.com/security/data/oval/v2/",
}
)
type VulnSrc struct {
dbc db.Operation
}
func NewVulnSrc() VulnSrc {
return VulnSrc{
dbc: db.Config{},
}
}
func (vs VulnSrc) Name() types.SourceID {
return vulnerability.RedHatOVAL
}
func (vs VulnSrc) Update(dir string) error {
uniqCPEs := CPEMap{}
repoToCPE, err := vs.parseRepositoryCpeMapping(dir, uniqCPEs)
if err != nil {
return xerrors.Errorf("unable to store the mapping between repositories and CPE names: %w", err)
}
nvrToCPE, err := vs.parseNvrCpeMapping(dir, uniqCPEs)
if err != nil {
return xerrors.Errorf("unable to store the mapping between NVR and CPE names: %w", err)
}
// List version directories
rootDir := filepath.Join(dir, vulnListDir, ovalDir)
versions, err := os.ReadDir(rootDir)
if err != nil {
return xerrors.Errorf("unable to list directory entries (%s): %w", rootDir, err)
}
advisories := map[bucket]Advisory{}
for _, ver := range versions {
versionDir := filepath.Join(rootDir, ver.Name())
streams, err := os.ReadDir(versionDir)
if err != nil {
return xerrors.Errorf("unable to get a list of directory entries (%s): %w", versionDir, err)
}
for _, f := range streams {
if !f.IsDir() {
continue
}
definitions, err := parseOVALStream(filepath.Join(versionDir, f.Name()), uniqCPEs)
if err != nil {
return xerrors.Errorf("failed to parse OVAL stream: %w", err)
}
advisories = vs.mergeAdvisories(advisories, definitions)
}
}
if err = vs.save(repoToCPE, nvrToCPE, advisories, uniqCPEs); err != nil {
return xerrors.Errorf("save error: %w", err)
}
return nil
}
func (vs VulnSrc) parseRepositoryCpeMapping(dir string, uniqCPEs CPEMap) (map[string][]string, error) {
filePath := filepath.Join(dir, vulnListDir, cpeDir, "repository-to-cpe.json")
f, err := os.Open(filePath)
if err != nil {
return nil, xerrors.Errorf("file open error: %w", err)
}
defer f.Close()
var repoToCPE map[string][]string
if err = json.NewDecoder(f).Decode(&repoToCPE); err != nil {
return nil, xerrors.Errorf("JSON parse error: %w", err)
}
for _, cpes := range repoToCPE {
updateCPEs(cpes, uniqCPEs)
}
return repoToCPE, nil
}
func (vs VulnSrc) parseNvrCpeMapping(dir string, uniqCPEs CPEMap) (map[string][]string, error) {
filePath := filepath.Join(dir, vulnListDir, cpeDir, "nvr-to-cpe.json")
f, err := os.Open(filePath)
if err != nil {
return nil, xerrors.Errorf("file open error: %w", err)
}
defer f.Close()
nvrToCpe := map[string][]string{}
if err = json.NewDecoder(f).Decode(&nvrToCpe); err != nil {
return nil, xerrors.Errorf("JSON parse error: %w", err)
}
for _, cpes := range nvrToCpe {
updateCPEs(cpes, uniqCPEs)
}
return nvrToCpe, nil
}
func (vs VulnSrc) mergeAdvisories(advisories map[bucket]Advisory, defs map[bucket]Definition) map[bucket]Advisory {
for bkt, def := range defs {
if old, ok := advisories[bkt]; ok {
found := false
for i := range old.Entries {
// New advisory should contain a single fixed version and list of arches.
if old.Entries[i].FixedVersion == def.Entry.FixedVersion && old.Entries[i].Status == def.Entry.Status &&
slices.Equal(old.Entries[i].Arches, def.Entry.Arches) && slices.Equal(old.Entries[i].Cves, def.Entry.Cves) {
found = true
old.Entries[i].AffectedCPEList = ustrings.Merge(old.Entries[i].AffectedCPEList, def.Entry.AffectedCPEList)
}
}
if !found {
old.Entries = append(old.Entries, def.Entry)
}
advisories[bkt] = old
} else {
advisories[bkt] = Advisory{
Entries: []Entry{def.Entry},
}
}
}
return advisories
}
func (vs VulnSrc) save(repoToCpe, nvrToCpe map[string][]string, advisories map[bucket]Advisory, uniqCPEs CPEMap) error {
cpeList := uniqCPEs.List()
err := vs.dbc.BatchUpdate(func(tx *bolt.Tx) error {
if err := vs.dbc.PutDataSource(tx, rootBucket, source); err != nil {
return xerrors.Errorf("failed to put data source: %w", err)
}
// Store the mapping between repository and CPE names
for repo, cpes := range repoToCpe {
if err := vs.dbc.PutRedHatRepositories(tx, repo, cpeList.Indices(cpes)); err != nil {
return xerrors.Errorf("repository put error: %w", err)
}
}
// Store the mapping between NVR and CPE names
for nvr, cpes := range nvrToCpe {
if err := vs.dbc.PutRedHatNVRs(tx, nvr, cpeList.Indices(cpes)); err != nil {
return xerrors.Errorf("NVR put error: %w", err)
}
}
// Store advisories
for bkt, advisory := range advisories {
for i := range advisory.Entries {
// Convert CPE names to indices.
advisory.Entries[i].AffectedCPEIndices = cpeList.Indices(advisory.Entries[i].AffectedCPEList)
}
if err := vs.dbc.PutAdvisoryDetail(tx, bkt.vulnID, bkt.pkgName, []string{rootBucket}, advisory); err != nil {
return xerrors.Errorf("failed to save Red Hat OVAL advisory: %w", err)
}
if err := vs.dbc.PutVulnerabilityID(tx, bkt.vulnID); err != nil {
return xerrors.Errorf("failed to put severity: %w", err)
}
}
// Store CPE indices for debug information
for i, cpe := range cpeList {
if err := vs.dbc.PutRedHatCPEs(tx, i, cpe); err != nil {
return xerrors.Errorf("CPE put error: %w", err)
}
}
return nil
})
if err != nil {
return xerrors.Errorf("batch update error: %w", err)
}
return nil
}
func (vs VulnSrc) cpeIndices(repositories, nvrs []string) ([]int, error) {
var cpeIndices []int
for _, repo := range repositories {
results, err := vs.dbc.RedHatRepoToCPEs(repo)
if err != nil {
return nil, xerrors.Errorf("unable to convert repositories to CPEs: %w", err)
}
cpeIndices = append(cpeIndices, results...)
}
for _, nvr := range nvrs {
results, err := vs.dbc.RedHatNVRToCPEs(nvr)
if err != nil {
return nil, xerrors.Errorf("unable to convert repositories to CPEs: %w", err)
}
cpeIndices = append(cpeIndices, results...)
}
return ints.Unique(cpeIndices), nil
}
func (vs VulnSrc) Get(pkgName string, repositories, nvrs []string) ([]types.Advisory, error) {
cpeIndices, err := vs.cpeIndices(repositories, nvrs)
if err != nil {
return nil, xerrors.Errorf("CPE convert error: %w", err)
}
if len(cpeIndices) == 0 {
return nil, xerrors.Errorf("unable to find CPE indices. See https://github.com/aquasecurity/trivy-db/issues/435 for details")
}
rawAdvisories, err := vs.dbc.ForEachAdvisory([]string{rootBucket}, pkgName)
if err != nil {
return nil, xerrors.Errorf("unable to iterate advisories: %w", err)
}
var advisories []types.Advisory
for vulnID, v := range rawAdvisories {
var adv Advisory
if err = json.Unmarshal(v.Content, &adv); err != nil {
return nil, xerrors.Errorf("failed to unmarshal advisory JSON: %w", err)
}
for _, entry := range adv.Entries {
if !ints.HasIntersection(cpeIndices, entry.AffectedCPEIndices) {
continue
}
for _, cve := range entry.Cves {
advisory := types.Advisory{
Severity: cve.Severity,
FixedVersion: entry.FixedVersion,
Arches: entry.Arches,
Status: entry.Status,
DataSource: &v.Source,
}
if strings.HasPrefix(vulnID, "CVE-") {
advisory.VulnerabilityID = vulnID
} else {
advisory.VulnerabilityID = cve.ID
advisory.VendorIDs = []string{vulnID}
}
advisories = append(advisories, advisory)
}
}
}
return advisories, nil
}
func parseOVALStream(dir string, uniqCPEs CPEMap) (map[bucket]Definition, error) {
log.Printf(" Parsing %s", dir)
// Parse tests
tests, err := parseTests(dir)
if err != nil {
return nil, xerrors.Errorf("failed to parse ovalTests: %w", err)
}
var advisories []redhatOVAL
definitionsDir := filepath.Join(dir, "definitions")
if exists, _ := utils.Exists(definitionsDir); !exists {
return nil, nil
}
err = utils.FileWalk(definitionsDir, func(r io.Reader, path string) error {
var definition redhatOVAL
if err := json.NewDecoder(r).Decode(&definition); err != nil {
return xerrors.Errorf("failed to decode %s: %w", path, err)
}
advisories = append(advisories, definition)
return nil
})
if err != nil {
return nil, xerrors.Errorf("Red Hat OVAL walk error: %w", err)
}
return parseDefinitions(advisories, tests, uniqCPEs), nil
}
func parseDefinitions(advisories []redhatOVAL, tests map[string]rpmInfoTest, uniqCPEs CPEMap) map[bucket]Definition {
defs := map[bucket]Definition{}
for _, advisory := range advisories {
// Skip unaffected vulnerabilities
if strings.Contains(advisory.ID, "unaffected") {
continue
}
// Parse criteria
moduleName, affectedPkgs := walkCriterion(advisory.Criteria, tests)
for _, affectedPkg := range affectedPkgs {
pkgName := affectedPkg.Name
if moduleName != "" {
// Add modular namespace
// e.g. nodejs:12::npm
pkgName = fmt.Sprintf("%s::%s", moduleName, pkgName)
}
rhsaID := vendorID(advisory.Metadata.References)
var cveEntries []CveEntry
for _, cve := range advisory.Metadata.Advisory.Cves {
cveEntries = append(cveEntries, CveEntry{
ID: cve.CveID,
Severity: severityFromImpact(cve.Impact),
})
}
sort.Slice(cveEntries, func(i, j int) bool {
return cveEntries[i].ID < cveEntries[j].ID
})
if rhsaID != "" { // For patched vulnerabilities
bkt := bucket{
pkgName: pkgName,
vulnID: rhsaID,
}
defs[bkt] = Definition{
Entry: Entry{
Cves: cveEntries,
FixedVersion: affectedPkg.FixedVersion,
AffectedCPEList: advisory.Metadata.Advisory.AffectedCpeList,
Arches: affectedPkg.Arches,
// The status is obviously "fixed" when there is a patch.
// To keep the database size small, we don't store the status for patched vulns.
// Status: StatusFixed,
},
}
} else { // For unpatched vulnerabilities
for _, cve := range cveEntries {
bkt := bucket{
pkgName: pkgName,
vulnID: cve.ID,
}
defs[bkt] = Definition{
Entry: Entry{
Cves: []CveEntry{
{
Severity: cve.Severity,
},
},
FixedVersion: affectedPkg.FixedVersion,
AffectedCPEList: advisory.Metadata.Advisory.AffectedCpeList,
Arches: affectedPkg.Arches,
Status: newStatus(advisory.Metadata.Advisory.Affected.Resolution.State),
},
}
}
}
}
updateCPEs(advisory.Metadata.Advisory.AffectedCpeList, uniqCPEs)
}
return defs
}
func walkCriterion(cri criteria, tests map[string]rpmInfoTest) (string, []pkg) {
var moduleName string
var packages []pkg
for _, c := range cri.Criterions {
// Parse module name
m := moduleRegexp.FindStringSubmatch(c.Comment)
if len(m) > 1 && m[1] != "" {
moduleName = m[1]
continue
}
t, ok := tests[c.TestRef]
if !ok {
continue
}
// Skip red-def:signature_keyid
if t.SignatureKeyID.Text != "" {
continue
}
var arches []string
if t.Arch != "" {
arches = strings.Split(t.Arch, "|") // affected arches are merged with '|'(e.g. 'aarch64|ppc64le|x86_64')
sort.Strings(arches)
}
packages = append(packages, pkg{
Name: t.Name,
FixedVersion: t.FixedVersion,
Arches: arches,
})
}
if len(cri.Criterias) == 0 {
return moduleName, packages
}
for _, c := range cri.Criterias {
m, pkgs := walkCriterion(c, tests)
if m != "" {
moduleName = m
}
if len(pkgs) != 0 {
packages = append(packages, pkgs...)
}
}
return moduleName, packages
}
func updateCPEs(cpes []string, uniqCPEs CPEMap) {
for _, cpe := range cpes {
cpe = strings.TrimSpace(cpe)
if cpe == "" {
continue
}
uniqCPEs.Add(cpe)
}
}
func vendorID(refs []reference) string {
for _, ref := range refs {
switch ref.Source {
case "RHSA", "RHBA":
return ref.RefID
}
}
return ""
}
func severityFromImpact(sev string) types.Severity {
switch strings.ToLower(sev) {
case "low":
return types.SeverityLow
case "moderate":
return types.SeverityMedium
case "important":
return types.SeverityHigh
case "critical":
return types.SeverityCritical
}
return types.SeverityUnknown
}
func newStatus(s string) types.Status {
switch strings.ToLower(s) {
case "affected", "fix deferred":
return types.StatusAffected
case "under investigation":
return types.StatusUnderInvestigation
case "will not fix":
return types.StatusWillNotFix
case "out of support scope":
return types.StatusEndOfLife
}
return types.StatusUnknown
}