2017-01-04 02:44:32 +00:00
|
|
|
// Copyright 2017 clair authors
|
2015-11-13 19:11:28 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
// Package debian implements a vulnerability source updater using the Debian
|
|
|
|
// Security Tracker.
|
2016-01-13 21:41:00 +00:00
|
|
|
package debian
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
import (
|
2018-10-12 19:57:41 +00:00
|
|
|
"crypto/sha256"
|
2015-11-13 19:11:28 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-28 01:45:11 +00:00
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2016-12-28 01:45:11 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-04 02:44:32 +00:00
|
|
|
"github.com/coreos/clair/ext/vulnsrc"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2018-09-04 20:51:42 +00:00
|
|
|
"github.com/coreos/clair/pkg/httputil"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-08 18:37:52 +00:00
|
|
|
url = "https://security-tracker.debian.org/tracker/data/json"
|
|
|
|
cveURLPrefix = "https://security-tracker.debian.org/tracker"
|
|
|
|
updaterFlag = "debianUpdater"
|
2019-02-19 21:36:31 +00:00
|
|
|
affectedType = database.SourcePackage
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type jsonData map[string]map[string]jsonVuln
|
|
|
|
|
|
|
|
type jsonVuln struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Releases map[string]jsonRel `json:"releases"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonRel struct {
|
|
|
|
FixedVersion string `json:"fixed_version"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Urgency string `json:"urgency"`
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
type updater struct{}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-01-04 02:44:32 +00:00
|
|
|
vulnsrc.RegisterUpdater("debian", &updater{})
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("package", "Debian").Info("Start fetching vulnerabilities")
|
2018-10-19 20:38:54 +00:00
|
|
|
latestHash, ok, err := database.FindKeyValueAndRollback(datastore, updaterFlag)
|
2015-11-13 19:11:28 +00:00
|
|
|
if err != nil {
|
2018-10-19 20:38:54 +00:00
|
|
|
return
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
latestHash = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download JSON.
|
2018-09-04 20:51:42 +00:00
|
|
|
r, err := httputil.GetWithUserAgent(url)
|
2017-07-26 23:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("could not download Debian's update")
|
|
|
|
return resp, commonerr.ErrCouldNotDownload
|
|
|
|
}
|
|
|
|
|
2018-09-04 20:51:42 +00:00
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if !httputil.Status2xx(r) {
|
|
|
|
log.WithField("StatusCode", r.StatusCode).Error("Failed to update Debian")
|
|
|
|
return resp, commonerr.ErrCouldNotDownload
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
// Parse the JSON.
|
|
|
|
resp, err = buildResponse(r.Body, latestHash)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
func (u *updater) Clean() {}
|
|
|
|
|
|
|
|
func buildResponse(jsonReader io.Reader, latestKnownHash string) (resp vulnsrc.UpdateResponse, err error) {
|
2015-11-13 19:11:28 +00:00
|
|
|
hash := latestKnownHash
|
|
|
|
|
|
|
|
// Defer the addition of flag information to the response.
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
2016-02-08 18:37:52 +00:00
|
|
|
resp.FlagName = updaterFlag
|
2015-11-13 19:11:28 +00:00
|
|
|
resp.FlagValue = hash
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-10-12 19:57:41 +00:00
|
|
|
// Create a TeeReader so that we can unmarshal into JSON and write to a hash
|
2015-11-13 19:11:28 +00:00
|
|
|
// digest at the same time.
|
2018-10-12 19:57:41 +00:00
|
|
|
jsonSHA := sha256.New()
|
2015-11-13 19:11:28 +00:00
|
|
|
teedJSONReader := io.TeeReader(jsonReader, jsonSHA)
|
|
|
|
|
|
|
|
// Unmarshal JSON.
|
|
|
|
var data jsonData
|
|
|
|
err = json.NewDecoder(teedJSONReader).Decode(&data)
|
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).Error("could not unmarshal Debian's JSON")
|
2017-01-13 07:08:52 +00:00
|
|
|
return resp, commonerr.ErrCouldNotParse
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the hash and skip updating if the hash has been seen before.
|
|
|
|
hash = hex.EncodeToString(jsonSHA.Sum(nil))
|
|
|
|
if latestKnownHash == hash {
|
2018-10-19 20:38:54 +00:00
|
|
|
log.WithField("package", "Debian").Debug("no update, skip")
|
2015-11-13 19:11:28 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract vulnerability data from Debian's JSON schema.
|
2015-12-01 19:58:17 +00:00
|
|
|
var unknownReleases map[string]struct{}
|
2016-01-20 00:17:08 +00:00
|
|
|
resp.Vulnerabilities, unknownReleases = parseDebianJSON(&data)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
// Log unknown releases
|
|
|
|
for k := range unknownReleases {
|
|
|
|
note := fmt.Sprintf("Debian %s is not mapped to any version number (eg. Jessie->8). Please update me.", k)
|
|
|
|
resp.Notes = append(resp.Notes, note)
|
|
|
|
log.Warning(note)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func parseDebianJSON(data *jsonData) (vulnerabilities []database.VulnerabilityWithAffected, unknownReleases map[string]struct{}) {
|
|
|
|
mvulnerabilities := make(map[string]*database.VulnerabilityWithAffected)
|
2015-11-13 19:11:28 +00:00
|
|
|
unknownReleases = make(map[string]struct{})
|
|
|
|
|
|
|
|
for pkgName, pkgNode := range *data {
|
|
|
|
for vulnName, vulnNode := range pkgNode {
|
|
|
|
for releaseName, releaseNode := range vulnNode.Releases {
|
|
|
|
// Attempt to detect the release number.
|
|
|
|
if _, isReleaseKnown := database.DebianReleasesMapping[releaseName]; !isReleaseKnown {
|
|
|
|
unknownReleases[releaseName] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:58:33 +00:00
|
|
|
// Skip if the status is not determined or the vulnerability is a temporary one.
|
2017-07-26 23:22:29 +00:00
|
|
|
// TODO: maybe add "undetermined" as Unknown severity.
|
2016-01-14 20:58:33 +00:00
|
|
|
if !strings.HasPrefix(vulnName, "CVE-") || releaseNode.Status == "undetermined" {
|
2015-11-13 19:11:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get or create the vulnerability.
|
2016-01-25 19:50:48 +00:00
|
|
|
vulnerability, vulnerabilityAlreadyExists := mvulnerabilities[vulnName]
|
2015-11-13 19:11:28 +00:00
|
|
|
if !vulnerabilityAlreadyExists {
|
2017-07-26 23:22:29 +00:00
|
|
|
vulnerability = &database.VulnerabilityWithAffected{
|
|
|
|
Vulnerability: database.Vulnerability{
|
|
|
|
Name: vulnName,
|
|
|
|
Link: strings.Join([]string{cveURLPrefix, "/", vulnName}, ""),
|
|
|
|
Severity: database.UnknownSeverity,
|
|
|
|
Description: vulnNode.Description,
|
|
|
|
},
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the priority of the vulnerability.
|
|
|
|
// In the JSON, a vulnerability has one urgency per package it affects.
|
2017-01-15 15:52:13 +00:00
|
|
|
severity := SeverityFromUrgency(releaseNode.Urgency)
|
|
|
|
if severity.Compare(vulnerability.Severity) > 0 {
|
|
|
|
// The highest urgency should be the one set.
|
|
|
|
vulnerability.Severity = severity
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the version of the package the vulnerability affects.
|
2016-12-28 01:45:11 +00:00
|
|
|
var version string
|
2015-11-13 19:11:28 +00:00
|
|
|
var err error
|
2017-07-26 23:22:29 +00:00
|
|
|
if releaseNode.Status == "open" {
|
2015-11-13 19:11:28 +00:00
|
|
|
// Open means that the package is currently vulnerable in the latest
|
|
|
|
// version of this Debian release.
|
2016-12-28 01:45:11 +00:00
|
|
|
version = versionfmt.MaxVersion
|
2015-11-13 19:11:28 +00:00
|
|
|
} else if releaseNode.Status == "resolved" {
|
|
|
|
// Resolved means that the vulnerability has been fixed in
|
|
|
|
// "fixed_version" (if affected).
|
2017-01-03 21:00:20 +00:00
|
|
|
err = versionfmt.Valid(dpkg.ParserName, releaseNode.FixedVersion)
|
2015-11-13 19:11:28 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping")
|
2015-11-13 19:11:28 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
// FixedVersion = "0" means that the vulnerability affecting
|
|
|
|
// current feature is not that important
|
|
|
|
if releaseNode.FixedVersion != "0" {
|
|
|
|
version = releaseNode.FixedVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if version == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var fixedInVersion string
|
|
|
|
if version != versionfmt.MaxVersion {
|
|
|
|
fixedInVersion = version
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:17:08 +00:00
|
|
|
// Create and add the feature version.
|
2017-07-26 23:22:29 +00:00
|
|
|
pkg := database.AffectedFeature{
|
2019-02-19 21:36:31 +00:00
|
|
|
FeatureType: affectedType,
|
2017-07-26 23:22:29 +00:00
|
|
|
FeatureName: pkgName,
|
|
|
|
AffectedVersion: version,
|
|
|
|
FixedInVersion: fixedInVersion,
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "debian:" + database.DebianReleasesMapping[releaseName],
|
|
|
|
VersionFormat: dpkg.ParserName,
|
2016-01-29 16:22:54 +00:00
|
|
|
},
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
vulnerability.Affected = append(vulnerability.Affected, pkg)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
// Store the vulnerability.
|
2016-01-25 19:50:48 +00:00
|
|
|
mvulnerabilities[vulnName] = vulnerability
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:58:17 +00:00
|
|
|
// Convert the vulnerabilities map to a slice
|
|
|
|
for _, v := range mvulnerabilities {
|
2016-01-20 00:17:08 +00:00
|
|
|
vulnerabilities = append(vulnerabilities, *v)
|
2015-12-01 19:58:17 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-15 15:52:13 +00:00
|
|
|
// SeverityFromUrgency converts the urgency scale used by the Debian Security
|
2017-01-19 18:42:37 +00:00
|
|
|
// Bug Tracker into a database.Severity.
|
|
|
|
func SeverityFromUrgency(urgency string) database.Severity {
|
2015-11-13 19:11:28 +00:00
|
|
|
switch urgency {
|
|
|
|
case "not yet assigned":
|
2017-01-19 18:42:37 +00:00
|
|
|
return database.UnknownSeverity
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
case "end-of-life", "unimportant":
|
2017-01-19 18:42:37 +00:00
|
|
|
return database.NegligibleSeverity
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
case "low", "low*", "low**":
|
2017-01-19 18:42:37 +00:00
|
|
|
return database.LowSeverity
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
case "medium", "medium*", "medium**":
|
2017-01-19 18:42:37 +00:00
|
|
|
return database.MediumSeverity
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
case "high", "high*", "high**":
|
2017-01-19 18:42:37 +00:00
|
|
|
return database.HighSeverity
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
default:
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("urgency", urgency).Warning("could not determine vulnerability severity from urgency")
|
2017-01-19 18:42:37 +00:00
|
|
|
return database.UnknownSeverity
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
}
|