|
| 1 | +package alt |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + dbTypes "github.com/aquasecurity/trivy-db/pkg/types" |
| 10 | + ustrings "github.com/aquasecurity/trivy-db/pkg/utils/strings" |
| 11 | + "github.com/aquasecurity/trivy-db/pkg/vulnsrc/alt" |
| 12 | + "github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability" |
| 13 | + osver "github.com/aquasecurity/trivy/pkg/detector/ospkg/version" |
| 14 | + ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" |
| 15 | + "github.com/aquasecurity/trivy/pkg/log" |
| 16 | + "github.com/aquasecurity/trivy/pkg/scanner/utils" |
| 17 | + "github.com/aquasecurity/trivy/pkg/types" |
| 18 | + "github.com/cheggaaa/pb/v3" |
| 19 | + version "github.com/knqyf263/go-rpm-version" |
| 20 | + "golang.org/x/exp/maps" |
| 21 | + "golang.org/x/exp/slices" |
| 22 | + "golang.org/x/xerrors" |
| 23 | + "k8s.io/utils/clock" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + eolDates = map[string]time.Time{ |
| 28 | + "p9": time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC), |
| 29 | + "p10": time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC), |
| 30 | + "c10f1": time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC), |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +type options struct { |
| 35 | + clock clock.Clock |
| 36 | +} |
| 37 | + |
| 38 | +type option func(*options) |
| 39 | + |
| 40 | +func WithClock(clock clock.Clock) option { |
| 41 | + return func(opts *options) { |
| 42 | + opts.clock = clock |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Scanner implements the ALT scanner with ALT` vuln source |
| 47 | +type Scanner struct { |
| 48 | + vs alt.VulnSrc |
| 49 | + *options |
| 50 | +} |
| 51 | + |
| 52 | +// NewScanner is the factory method for Scanner |
| 53 | +func NewScanner(opts ...option) *Scanner { |
| 54 | + o := &options{ |
| 55 | + clock: clock.RealClock{}, |
| 56 | + } |
| 57 | + |
| 58 | + for _, opt := range opts { |
| 59 | + opt(o) |
| 60 | + } |
| 61 | + return &Scanner{ |
| 62 | + vs: alt.NewVulnSrc(), |
| 63 | + options: o, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// IsSupportedVersion checks the OSFamily can be scanned using ALT scanner |
| 68 | +func (s *Scanner) IsSupportedVersion(ctx context.Context, osFamily ftypes.OSType, osVer string) bool { |
| 69 | + return osver.Supported(ctx, eolDates, osFamily, osVer) |
| 70 | +} |
| 71 | + |
| 72 | +func (s *Scanner) Detect(cpe string, _ *ftypes.Repository, pkgs []ftypes.Package) ([]types.DetectedVulnerability, error) { |
| 73 | + log.Logger.Info("Detecting ALT vulnerabilities...") |
| 74 | + log.Logger.Debugf("ALT: os version: %s", fromCPE(cpe)) |
| 75 | + log.Logger.Debugf("ALT: the number of packages: %d", len(pkgs)) |
| 76 | + |
| 77 | + var vulns []types.DetectedVulnerability |
| 78 | + p := pb.New(len(pkgs)) |
| 79 | + p.Start() |
| 80 | + for _, pkg := range pkgs { |
| 81 | + detectedVulns, err := s.detect(cpe, pkg) |
| 82 | + if err != nil { |
| 83 | + return nil, xerrors.Errorf("ALT vulnerability detection error: %w", err) |
| 84 | + } |
| 85 | + vulns = append(vulns, detectedVulns...) |
| 86 | + p.Increment() |
| 87 | + } |
| 88 | + p.Finish() |
| 89 | + return vulns, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (s *Scanner) detect(cpe string, pkg ftypes.Package) ([]types.DetectedVulnerability, error) { |
| 93 | + advisories, err := s.vs.Get(pkg.Name, cpe) |
| 94 | + if err != nil { |
| 95 | + return nil, xerrors.Errorf("failed to get ALT advisories: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + installed := utils.FormatVersion(pkg) |
| 99 | + installedVersion := version.NewVersion(installed) |
| 100 | + |
| 101 | + uniqVulns := map[string]types.DetectedVulnerability{} |
| 102 | + for _, adv := range advisories { |
| 103 | + if len(adv.Arches) != 0 && pkg.Arch != "noarch" { |
| 104 | + if !slices.Contains(adv.Arches, pkg.Arch) { |
| 105 | + continue |
| 106 | + } |
| 107 | + } |
| 108 | + vulnID := adv.VulnerabilityID |
| 109 | + vuln := types.DetectedVulnerability{ |
| 110 | + VulnerabilityID: vulnID, |
| 111 | + PkgID: pkg.ID, |
| 112 | + PkgName: pkg.Name, |
| 113 | + InstalledVersion: utils.FormatVersion(pkg), |
| 114 | + PkgIdentifier: pkg.Identifier, |
| 115 | + Layer: pkg.Layer, |
| 116 | + SeveritySource: vulnerability.ALT, |
| 117 | + Vulnerability: dbTypes.Vulnerability{ |
| 118 | + Severity: adv.Severity.String(), |
| 119 | + }, |
| 120 | + Custom: adv.Custom, |
| 121 | + } |
| 122 | + |
| 123 | + if adv.FixedVersion == "" { |
| 124 | + if _, ok := uniqVulns[vulnID]; !ok { |
| 125 | + uniqVulns[vulnID] = vuln |
| 126 | + } |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + fixedVersion := version.NewVersion(adv.FixedVersion) |
| 131 | + if installedVersion.LessThan(fixedVersion) { |
| 132 | + vuln.VendorIDs = adv.VendorIDs |
| 133 | + vuln.FixedVersion = fixedVersion.String() |
| 134 | + |
| 135 | + if v, ok := uniqVulns[vulnID]; ok { |
| 136 | + v.VendorIDs = ustrings.Unique(append(v.VendorIDs, vuln.VendorIDs...)) |
| 137 | + |
| 138 | + if version.NewVersion(v.FixedVersion).LessThan(fixedVersion) { |
| 139 | + v.FixedVersion = vuln.FixedVersion |
| 140 | + } |
| 141 | + uniqVulns[vulnID] = v |
| 142 | + } else { |
| 143 | + uniqVulns[vulnID] = vuln |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + vulns := maps.Values(uniqVulns) |
| 149 | + sort.Slice(vulns, func(i, j int) bool { |
| 150 | + return vulns[i].VulnerabilityID < vulns[j].VulnerabilityID |
| 151 | + }) |
| 152 | + |
| 153 | + return vulns, nil |
| 154 | +} |
| 155 | + |
| 156 | +func fromCPE(cpe string) string { |
| 157 | + if strings.Contains(cpe, "sp") && strings.Contains(cpe, "10") { |
| 158 | + return "c10f1" |
| 159 | + } |
| 160 | + if !strings.Contains(cpe, "sp") && strings.Contains(cpe, "10") { |
| 161 | + return "p10" |
| 162 | + } |
| 163 | + if !strings.Contains(cpe, "sp") && strings.Contains(cpe, "9") { |
| 164 | + return "p9" |
| 165 | + } |
| 166 | + return "undefined" |
| 167 | +} |
0 commit comments