2018-09-14 21:25:12 +00:00
|
|
|
// Copyright 2018 clair authors
|
2016-11-18 21:24:54 +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 alpine implements a vulnerability source updater using the
|
|
|
|
// alpine-secdb git repository.
|
2016-11-18 21:24:54 +00:00
|
|
|
package alpine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2017-02-07 19:48:42 +00:00
|
|
|
"path/filepath"
|
2016-11-18 21:24:54 +00:00
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-11-18 21:24:54 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
"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"
|
2018-10-19 20:38:54 +00:00
|
|
|
"github.com/coreos/clair/pkg/fsutil"
|
2018-09-14 21:25:12 +00:00
|
|
|
"github.com/coreos/clair/pkg/gitutil"
|
2016-11-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-19 20:38:54 +00:00
|
|
|
// This Alpine vulnerability database affects origin packages, which has
|
|
|
|
// `origin` field of itself.
|
2018-08-23 05:39:26 +00:00
|
|
|
secdbGitURL = "https://github.com/alpinelinux/alpine-secdb"
|
2016-11-18 21:24:54 +00:00
|
|
|
updaterFlag = "alpine-secdbUpdater"
|
|
|
|
nvdURLPrefix = "https://cve.mitre.org/cgi-bin/cvename.cgi?name="
|
2018-10-18 18:52:50 +00:00
|
|
|
// affected type indicates if the affected feature hint is for binary or
|
|
|
|
// source package.
|
|
|
|
affectedType = database.AffectBinaryPackage
|
2016-11-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-01-04 02:44:32 +00:00
|
|
|
vulnsrc.RegisterUpdater("alpine", &updater{})
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
type updater struct {
|
2016-11-18 21:24:54 +00:00
|
|
|
repositoryLocalPath string
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
func (u *updater) Update(db database.Datastore) (resp vulnsrc.UpdateResponse, err error) {
|
2018-10-19 20:38:54 +00:00
|
|
|
log.WithField("package", "Alpine").Info("start fetching vulnerabilities")
|
2016-11-18 21:24:54 +00:00
|
|
|
// Pull the master branch.
|
2018-10-19 20:38:54 +00:00
|
|
|
var (
|
|
|
|
commit string
|
|
|
|
existingCommit string
|
|
|
|
foundCommit bool
|
|
|
|
namespaces []string
|
|
|
|
vulns []database.VulnerabilityWithAffected
|
|
|
|
)
|
|
|
|
|
|
|
|
if u.repositoryLocalPath, commit, err = gitutil.CloneOrPull(secdbGitURL, u.repositoryLocalPath, updaterFlag); err != nil {
|
2016-11-18 21:24:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the updaterFlag to equal the commit processed.
|
|
|
|
resp.FlagName = updaterFlag
|
|
|
|
resp.FlagValue = commit
|
2018-10-19 20:38:54 +00:00
|
|
|
if existingCommit, foundCommit, err = database.FindKeyValueAndRollback(db, updaterFlag); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-11-18 21:24:54 +00:00
|
|
|
|
|
|
|
// Short-circuit if there have been no updates.
|
2018-10-19 20:38:54 +00:00
|
|
|
if foundCommit && commit == existingCommit {
|
|
|
|
log.WithField("package", "alpine").Debug("no update, skip")
|
2016-11-18 21:24:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
// Get the list of namespaces from the repository.
|
2018-10-19 20:38:54 +00:00
|
|
|
if namespaces, err = fsutil.Readdir(u.repositoryLocalPath, fsutil.DirectoriesOnly); err != nil {
|
2017-02-07 19:48:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:24:54 +00:00
|
|
|
// Append any changed vulnerabilities to the response.
|
2016-12-16 21:45:14 +00:00
|
|
|
for _, namespace := range namespaces {
|
2018-10-19 20:38:54 +00:00
|
|
|
if vulns, err = parseVulnsFromNamespace(u.repositoryLocalPath, namespace); err != nil {
|
2016-11-18 21:24:54 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-19 20:38:54 +00:00
|
|
|
|
2016-11-18 21:24:54 +00:00
|
|
|
resp.Vulnerabilities = append(resp.Vulnerabilities, vulns...)
|
2016-12-15 21:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
func (u *updater) Clean() {
|
|
|
|
if u.repositoryLocalPath != "" {
|
|
|
|
os.RemoveAll(u.repositoryLocalPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
func parseVulnsFromNamespace(repositoryPath, namespace string) (vulns []database.VulnerabilityWithAffected, err error) {
|
2017-02-07 19:48:42 +00:00
|
|
|
nsDir := filepath.Join(repositoryPath, namespace)
|
|
|
|
var dbFilenames []string
|
2018-10-19 20:38:54 +00:00
|
|
|
if dbFilenames, err = fsutil.Readdir(nsDir, fsutil.FilesOnly); err != nil {
|
2016-12-15 21:36:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
for _, filename := range dbFilenames {
|
2018-10-19 20:38:54 +00:00
|
|
|
var db *secDB
|
|
|
|
if db, err = newSecDB(filepath.Join(nsDir, filename)); err != nil {
|
2017-02-07 19:48:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
vulns = append(vulns, db.Vulnerabilities()...)
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
type secDB struct {
|
2016-12-14 22:01:38 +00:00
|
|
|
Distro string `yaml:"distroversion"`
|
|
|
|
Packages []struct {
|
|
|
|
Pkg struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Fixes map[string][]string `yaml:"secfixes"`
|
|
|
|
} `yaml:"pkg"`
|
|
|
|
} `yaml:"packages"`
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
func newSecDB(filePath string) (file *secDB, err error) {
|
|
|
|
var f io.ReadCloser
|
|
|
|
f, err = os.Open(filePath)
|
2016-12-14 22:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
defer f.Close()
|
|
|
|
file = &secDB{}
|
|
|
|
err = yaml.NewDecoder(f).Decode(file)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *secDB) Vulnerabilities() (vulns []database.VulnerabilityWithAffected) {
|
|
|
|
if file == nil {
|
2016-12-14 22:01:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
namespace := database.Namespace{Name: "alpine:" + file.Distro, VersionFormat: dpkg.ParserName}
|
|
|
|
for _, pkg := range file.Packages {
|
|
|
|
for version, cveNames := range pkg.Pkg.Fixes {
|
|
|
|
if err := versionfmt.Valid(dpkg.ParserName, version); err != nil {
|
|
|
|
log.WithError(err).WithFields(log.Fields{
|
|
|
|
"version": version,
|
|
|
|
"package name": pkg.Pkg.Name,
|
|
|
|
}).Warning("could not parse package version, skipping")
|
2016-12-14 22:01:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:38:54 +00:00
|
|
|
for _, cve := range cveNames {
|
|
|
|
vuln := database.VulnerabilityWithAffected{
|
|
|
|
Vulnerability: database.Vulnerability{
|
|
|
|
Name: cve,
|
|
|
|
Link: nvdURLPrefix + cve,
|
|
|
|
Severity: database.UnknownSeverity,
|
|
|
|
Namespace: namespace,
|
|
|
|
},
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
var fixedInVersion string
|
|
|
|
if version != versionfmt.MaxVersion {
|
|
|
|
fixedInVersion = version
|
|
|
|
}
|
2018-10-19 20:38:54 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
vuln.Affected = []database.AffectedFeature{
|
2016-12-14 22:01:38 +00:00
|
|
|
{
|
2018-10-18 18:52:50 +00:00
|
|
|
AffectedType: affectedType,
|
2018-10-19 20:38:54 +00:00
|
|
|
FeatureName: pkg.Pkg.Name,
|
2017-07-26 23:22:29 +00:00
|
|
|
AffectedVersion: version,
|
|
|
|
FixedInVersion: fixedInVersion,
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "alpine:" + file.Distro,
|
|
|
|
VersionFormat: dpkg.ParserName,
|
2016-12-14 22:01:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
vulns = append(vulns, vuln)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|