-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
redhat.go
218 lines (193 loc) · 6.24 KB
/
redhat.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
package redhat
import (
"context"
"fmt"
"slices"
"sort"
"strings"
"time"
version "github.com/knqyf263/go-rpm-version"
"github.com/samber/lo"
"golang.org/x/xerrors"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
ustrings "github.com/aquasecurity/trivy-db/pkg/utils/strings"
redhat "github.com/aquasecurity/trivy-db/pkg/vulnsrc/redhat-oval"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
osver "github.com/aquasecurity/trivy/pkg/detector/ospkg/version"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/scanner/utils"
"github.com/aquasecurity/trivy/pkg/types"
)
var (
defaultContentSets = map[string][]string{
"6": {
"rhel-6-server-rpms",
"rhel-6-server-extras-rpms",
},
"7": {
"rhel-7-server-rpms",
"rhel-7-server-extras-rpms",
},
"8": {
"rhel-8-for-x86_64-baseos-rpms",
"rhel-8-for-x86_64-appstream-rpms",
},
"9": {
"rhel-9-for-x86_64-baseos-rpms",
"rhel-9-for-x86_64-appstream-rpms",
},
}
redhatEOLDates = map[string]time.Time{
"4": time.Date(2017, 5, 31, 23, 59, 59, 0, time.UTC),
"5": time.Date(2020, 11, 30, 23, 59, 59, 0, time.UTC),
"6": time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC),
// N/A
"7": time.Date(3000, 1, 1, 23, 59, 59, 0, time.UTC),
"8": time.Date(3000, 1, 1, 23, 59, 59, 0, time.UTC),
"9": time.Date(3000, 1, 1, 23, 59, 59, 0, time.UTC),
}
centosEOLDates = map[string]time.Time{
"3": time.Date(2010, 10, 31, 23, 59, 59, 0, time.UTC),
"4": time.Date(2012, 2, 29, 23, 59, 59, 0, time.UTC),
"5": time.Date(2017, 3, 31, 23, 59, 59, 0, time.UTC),
"6": time.Date(2020, 11, 30, 23, 59, 59, 0, time.UTC),
"7": time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC),
"8": time.Date(2021, 12, 31, 23, 59, 59, 0, time.UTC),
}
excludedVendorsSuffix = []string{
".remi",
}
)
// Scanner implements the RedHat scanner
type Scanner struct {
vs redhat.VulnSrc
}
// NewScanner is the factory method for Scanner
func NewScanner() *Scanner {
return &Scanner{
vs: redhat.NewVulnSrc(),
}
}
// Detect scans and returns redhat vulnerabilities
func (s *Scanner) Detect(ctx context.Context, osVer string, _ *ftypes.Repository, pkgs []ftypes.Package) ([]types.DetectedVulnerability, error) {
osVer = osver.Major(osVer)
log.InfoContext(ctx, "Detecting RHEL/CentOS vulnerabilities...", log.String("os_version", osVer),
log.Int("pkg_num", len(pkgs)))
var vulns []types.DetectedVulnerability
for _, pkg := range pkgs {
if !isFromSupportedVendor(pkg) {
log.DebugContext(ctx, "Skipping the package with unsupported vendor", log.String("package", pkg.Name))
continue
}
detectedVulns, err := s.detect(osVer, pkg)
if err != nil {
return nil, xerrors.Errorf("redhat vulnerability detection error: %w", err)
}
vulns = append(vulns, detectedVulns...)
}
return vulns, nil
}
func (s *Scanner) detect(osVer string, pkg ftypes.Package) ([]types.DetectedVulnerability, error) {
// For Red Hat OVAL v2 containing only binary package names
pkgName := addModularNamespace(pkg.Name, pkg.Modularitylabel)
var contentSets []string
var nvr string
if pkg.BuildInfo == nil {
contentSets = defaultContentSets[osVer]
} else {
contentSets = pkg.BuildInfo.ContentSets
nvr = fmt.Sprintf("%s-%s", pkg.BuildInfo.Nvr, pkg.BuildInfo.Arch)
}
advisories, err := s.vs.Get(pkgName, contentSets, []string{nvr})
if err != nil {
return nil, xerrors.Errorf("failed to get Red Hat advisories: %w", err)
}
installed := utils.FormatVersion(pkg)
installedVersion := version.NewVersion(installed)
uniqVulns := make(map[string]types.DetectedVulnerability)
for _, adv := range advisories {
// if Arches for advisory is empty or pkg.Arch is "noarch", then any Arches are affected
if len(adv.Arches) != 0 && pkg.Arch != "noarch" {
if !slices.Contains(adv.Arches, pkg.Arch) {
continue
}
}
vulnID := adv.VulnerabilityID
vuln := types.DetectedVulnerability{
VulnerabilityID: vulnID,
PkgID: pkg.ID,
PkgName: pkg.Name,
InstalledVersion: utils.FormatVersion(pkg),
PkgIdentifier: pkg.Identifier,
Status: adv.Status,
Layer: pkg.Layer,
SeveritySource: vulnerability.RedHat,
Vulnerability: dbTypes.Vulnerability{
Severity: adv.Severity.String(),
},
Custom: adv.Custom,
}
// unpatched vulnerabilities
if adv.FixedVersion == "" {
// Red Hat may contain several advisories for the same vulnerability (RHSA advisories).
// To avoid overwriting the fixed version by mistake, we should skip unpatched vulnerabilities if they were added earlier
if _, ok := uniqVulns[vulnID]; !ok {
uniqVulns[vulnID] = vuln
}
continue
}
// patched vulnerabilities
fixedVersion := version.NewVersion(adv.FixedVersion)
if installedVersion.LessThan(fixedVersion) {
vuln.VendorIDs = adv.VendorIDs
vuln.FixedVersion = fixedVersion.String()
if v, ok := uniqVulns[vulnID]; ok {
// In case two advisories resolve the same CVE-ID.
// e.g. The first fix might be incomplete.
v.VendorIDs = ustrings.Unique(append(v.VendorIDs, vuln.VendorIDs...))
// The newer fixed version should be taken.
if version.NewVersion(v.FixedVersion).LessThan(fixedVersion) {
v.FixedVersion = vuln.FixedVersion
}
uniqVulns[vulnID] = v
} else {
uniqVulns[vulnID] = vuln
}
}
}
vulns := lo.Values(uniqVulns)
sort.Slice(vulns, func(i, j int) bool {
return vulns[i].VulnerabilityID < vulns[j].VulnerabilityID
})
return vulns, nil
}
// IsSupportedVersion checks is OSFamily can be scanned with Redhat scanner
func (s *Scanner) IsSupportedVersion(ctx context.Context, osFamily ftypes.OSType, osVer string) bool {
osVer = osver.Major(osVer)
if osFamily == ftypes.CentOS {
return osver.Supported(ctx, centosEOLDates, osFamily, osVer)
}
return osver.Supported(ctx, redhatEOLDates, osFamily, osVer)
}
func isFromSupportedVendor(pkg ftypes.Package) bool {
for _, suffix := range excludedVendorsSuffix {
if strings.HasSuffix(pkg.Release, suffix) {
return false
}
}
return true
}
func addModularNamespace(name, label string) string {
// e.g. npm, nodejs:12:8030020201124152102:229f0a1c => nodejs:12::npm
var count int
for i, r := range label {
if r == ':' {
count++
}
if count == 2 {
return label[:i] + "::" + name
}
}
return name
}