forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: stefan <stefan_paksa@icloud.com>
- Loading branch information
Showing
10 changed files
with
256 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package alt | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
dbTypes "github.com/aquasecurity/trivy-db/pkg/types" | ||
ustrings "github.com/aquasecurity/trivy-db/pkg/utils/strings" | ||
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/alt" | ||
"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" | ||
"github.com/cheggaaa/pb/v3" | ||
version "github.com/knqyf263/go-rpm-version" | ||
"golang.org/x/exp/maps" | ||
"golang.org/x/exp/slices" | ||
"golang.org/x/xerrors" | ||
"k8s.io/utils/clock" | ||
) | ||
|
||
var ( | ||
eolDates = map[string]time.Time{ | ||
"p9": time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC), | ||
"p10": time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC), | ||
"c10f1": time.Date(2024, 6, 30, 23, 59, 59, 0, time.UTC), | ||
} | ||
) | ||
|
||
type options struct { | ||
clock clock.Clock | ||
} | ||
|
||
type option func(*options) | ||
|
||
func WithClock(clock clock.Clock) option { | ||
return func(opts *options) { | ||
opts.clock = clock | ||
} | ||
} | ||
|
||
// Scanner implements the ALT scanner with ALT` vuln source | ||
type Scanner struct { | ||
vs alt.VulnSrc | ||
*options | ||
} | ||
|
||
// NewScanner is the factory method for Scanner | ||
func NewScanner(opts ...option) *Scanner { | ||
o := &options{ | ||
clock: clock.RealClock{}, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
return &Scanner{ | ||
vs: alt.NewVulnSrc(), | ||
options: o, | ||
} | ||
} | ||
|
||
// IsSupportedVersion checks the OSFamily can be scanned using ALT scanner | ||
func (s *Scanner) IsSupportedVersion(ctx context.Context, osFamily ftypes.OSType, osVer string) bool { | ||
return osver.Supported(ctx, eolDates, osFamily, osVer) | ||
} | ||
|
||
func (s *Scanner) Detect(cpe string, _ *ftypes.Repository, pkgs []ftypes.Package) ([]types.DetectedVulnerability, error) { | ||
log.Logger.Info("Detecting ALT vulnerabilities...") | ||
log.Logger.Debugf("ALT: os version: %s", fromCPE(cpe)) | ||
log.Logger.Debugf("ALT: the number of packages: %d", len(pkgs)) | ||
|
||
var vulns []types.DetectedVulnerability | ||
p := pb.New(len(pkgs)) | ||
p.Start() | ||
for _, pkg := range pkgs { | ||
detectedVulns, err := s.detect(cpe, pkg) | ||
if err != nil { | ||
return nil, xerrors.Errorf("ALT vulnerability detection error: %w", err) | ||
} | ||
vulns = append(vulns, detectedVulns...) | ||
p.Increment() | ||
} | ||
p.Finish() | ||
return vulns, nil | ||
} | ||
|
||
func (s *Scanner) detect(cpe string, pkg ftypes.Package) ([]types.DetectedVulnerability, error) { | ||
advisories, err := s.vs.Get(pkg.Name, cpe) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to get ALT advisories: %w", err) | ||
} | ||
|
||
installed := utils.FormatVersion(pkg) | ||
installedVersion := version.NewVersion(installed) | ||
|
||
uniqVulns := map[string]types.DetectedVulnerability{} | ||
for _, adv := range advisories { | ||
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, | ||
Layer: pkg.Layer, | ||
SeveritySource: vulnerability.ALT, | ||
Vulnerability: dbTypes.Vulnerability{ | ||
Severity: adv.Severity.String(), | ||
}, | ||
Custom: adv.Custom, | ||
} | ||
|
||
if adv.FixedVersion == "" { | ||
if _, ok := uniqVulns[vulnID]; !ok { | ||
uniqVulns[vulnID] = vuln | ||
} | ||
continue | ||
} | ||
|
||
fixedVersion := version.NewVersion(adv.FixedVersion) | ||
if installedVersion.LessThan(fixedVersion) { | ||
vuln.VendorIDs = adv.VendorIDs | ||
vuln.FixedVersion = fixedVersion.String() | ||
|
||
if v, ok := uniqVulns[vulnID]; ok { | ||
v.VendorIDs = ustrings.Unique(append(v.VendorIDs, vuln.VendorIDs...)) | ||
|
||
if version.NewVersion(v.FixedVersion).LessThan(fixedVersion) { | ||
v.FixedVersion = vuln.FixedVersion | ||
} | ||
uniqVulns[vulnID] = v | ||
} else { | ||
uniqVulns[vulnID] = vuln | ||
} | ||
} | ||
} | ||
|
||
vulns := maps.Values(uniqVulns) | ||
sort.Slice(vulns, func(i, j int) bool { | ||
return vulns[i].VulnerabilityID < vulns[j].VulnerabilityID | ||
}) | ||
|
||
return vulns, nil | ||
} | ||
|
||
func fromCPE(cpe string) string { | ||
if strings.Contains(cpe, "sp") && strings.Contains(cpe, "10") { | ||
return "c10f1" | ||
} | ||
if !strings.Contains(cpe, "sp") && strings.Contains(cpe, "10") { | ||
return "p10" | ||
} | ||
if !strings.Contains(cpe, "sp") && strings.Contains(cpe, "9") { | ||
return "p9" | ||
} | ||
return "undefined" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package alt | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/aquasecurity/trivy/pkg/fanal/analyzer" | ||
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os" | ||
"github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"golang.org/x/exp/slices" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterAnalyzer(&altOSAnalyzer{}) | ||
} | ||
|
||
const altAnalyzerVersion = 1 | ||
|
||
var requiredFiles = []string{"etc/os-release"} | ||
|
||
type altOSAnalyzer struct{} | ||
|
||
func (a altOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) { | ||
scanner := bufio.NewScanner(input.Content) | ||
var cpe string | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
ss := strings.SplitN(line, "=", 2) | ||
if len(ss) != 2 { | ||
continue | ||
} | ||
key, value := strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1]) | ||
|
||
switch key { | ||
case "ID": | ||
id := strings.Trim(value, `"'`) | ||
if !strings.Contains(id, "altlinux") { | ||
return nil, nil | ||
} | ||
continue | ||
case "CPE_NAME": | ||
cpe = strings.Trim(value, `"'`) | ||
default: | ||
continue | ||
} | ||
return &analyzer.AnalysisResult{ | ||
OS: types.OS{Family: types.ALT, Name: cpe}, | ||
}, nil | ||
} | ||
return nil, xerrors.Errorf("alt: %w", fos.AnalyzeOSError) | ||
} | ||
|
||
func (a altOSAnalyzer) Required(filePath string, _ os.FileInfo) bool { | ||
return slices.Contains(requiredFiles, filePath) | ||
} | ||
|
||
func (a altOSAnalyzer) Type() analyzer.Type { | ||
return analyzer.TypeALT | ||
} | ||
|
||
func (a altOSAnalyzer) Version() int { | ||
return altAnalyzerVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters