2017-01-04 02:44:32 +00:00
|
|
|
// Copyright 2017 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"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-01-18 02:22:20 +00:00
|
|
|
"os/exec"
|
2017-02-07 19:48:42 +00:00
|
|
|
"path/filepath"
|
2016-11-18 21:24:54 +00:00
|
|
|
"strings"
|
|
|
|
|
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"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2016-11-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
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="
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("package", "Alpine").Info("Start fetching vulnerabilities")
|
2016-11-18 21:24:54 +00:00
|
|
|
|
|
|
|
// Pull the master branch.
|
|
|
|
var commit string
|
2017-01-04 02:44:32 +00:00
|
|
|
commit, err = u.pullRepository()
|
2016-11-18 21:24:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:09:45 +00:00
|
|
|
// Open a database transaction.
|
2017-07-26 23:22:29 +00:00
|
|
|
tx, err := db.Begin()
|
2016-11-18 21:24:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
defer tx.Rollback()
|
|
|
|
|
2018-07-05 23:09:45 +00:00
|
|
|
// Ask the database for the latest commit we successfully applied.
|
|
|
|
var dbCommit string
|
|
|
|
var ok bool
|
|
|
|
dbCommit, ok, err = tx.FindKeyValue(updaterFlag)
|
2017-07-26 23:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
dbCommit = ""
|
|
|
|
}
|
2016-11-18 21:24:54 +00:00
|
|
|
|
|
|
|
// Set the updaterFlag to equal the commit processed.
|
|
|
|
resp.FlagName = updaterFlag
|
|
|
|
resp.FlagValue = commit
|
|
|
|
|
|
|
|
// Short-circuit if there have been no updates.
|
|
|
|
if commit == dbCommit {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithField("package", "alpine").Debug("no update")
|
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.
|
2016-12-16 21:45:14 +00:00
|
|
|
var namespaces []string
|
2017-02-07 19:48:42 +00:00
|
|
|
namespaces, err = ls(u.repositoryLocalPath, directoriesOnly)
|
|
|
|
if err != nil {
|
|
|
|
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 {
|
2017-07-26 23:22:29 +00:00
|
|
|
var vulns []database.VulnerabilityWithAffected
|
2016-12-16 22:42:06 +00:00
|
|
|
var note string
|
2017-01-04 02:44:32 +00:00
|
|
|
vulns, note, err = parseVulnsFromNamespace(u.repositoryLocalPath, namespace)
|
2016-11-18 21:24:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-12-16 22:42:06 +00:00
|
|
|
if note != "" {
|
|
|
|
resp.Notes = append(resp.Notes, note)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
type lsFilter int
|
|
|
|
|
|
|
|
const (
|
|
|
|
filesOnly lsFilter = iota
|
|
|
|
directoriesOnly
|
|
|
|
)
|
|
|
|
|
|
|
|
func ls(path string, filter lsFilter) ([]string, error) {
|
2016-12-16 21:45:14 +00:00
|
|
|
dir, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
2016-12-30 13:36:09 +00:00
|
|
|
finfos, err := dir.Readdir(0)
|
2016-12-16 21:45:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
var files []string
|
2016-12-30 13:36:09 +00:00
|
|
|
for _, info := range finfos {
|
2017-02-07 19:48:42 +00:00
|
|
|
if filter == directoriesOnly && !info.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter == filesOnly && info.IsDir() {
|
2016-12-30 13:36:09 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-02-07 19:48:42 +00:00
|
|
|
|
2016-12-30 13:36:09 +00:00
|
|
|
if strings.HasPrefix(info.Name(), ".") {
|
2016-12-16 21:45:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
files = append(files, info.Name())
|
2016-12-16 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
return files, nil
|
2016-12-16 21:45:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func parseVulnsFromNamespace(repositoryPath, namespace string) (vulns []database.VulnerabilityWithAffected, note string, err error) {
|
2017-02-07 19:48:42 +00:00
|
|
|
nsDir := filepath.Join(repositoryPath, namespace)
|
|
|
|
var dbFilenames []string
|
|
|
|
dbFilenames, err = ls(nsDir, filesOnly)
|
2016-12-15 21:36:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
for _, filename := range dbFilenames {
|
|
|
|
var file io.ReadCloser
|
|
|
|
file, err = os.Open(filepath.Join(nsDir, filename))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
var fileVulns []database.VulnerabilityWithAffected
|
2017-02-07 19:48:42 +00:00
|
|
|
fileVulns, err = parseYAML(file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vulns = append(vulns, fileVulns...)
|
|
|
|
file.Close()
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:44:32 +00:00
|
|
|
func (u *updater) pullRepository() (commit string, err error) {
|
2016-12-15 21:36:41 +00:00
|
|
|
// If the repository doesn't exist, clone it.
|
2017-01-04 02:44:32 +00:00
|
|
|
if _, pathExists := os.Stat(u.repositoryLocalPath); u.repositoryLocalPath == "" || os.IsNotExist(pathExists) {
|
|
|
|
if u.repositoryLocalPath, err = ioutil.TempDir(os.TempDir(), "alpine-secdb"); err != nil {
|
|
|
|
return "", vulnsrc.ErrFilesystem
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 02:22:20 +00:00
|
|
|
cmd := exec.Command("git", "clone", secdbGitURL, ".")
|
|
|
|
cmd.Dir = u.repositoryLocalPath
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2017-01-04 02:44:32 +00:00
|
|
|
u.Clean()
|
2018-07-05 23:11:30 +00:00
|
|
|
log.WithError(err).WithField("output", string(out)).Error("could not clone alpine-secdb repository")
|
2017-01-13 07:08:52 +00:00
|
|
|
return "", commonerr.ErrCouldNotDownload
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
2016-12-15 21:36:41 +00:00
|
|
|
} else {
|
2017-01-18 02:22:20 +00:00
|
|
|
// The repository already exists and it needs to be refreshed via a pull.
|
|
|
|
cmd := exec.Command("git", "pull")
|
|
|
|
cmd.Dir = u.repositoryLocalPath
|
|
|
|
if _, err := cmd.CombinedOutput(); err != nil {
|
2017-01-04 02:44:32 +00:00
|
|
|
return "", vulnsrc.ErrGitFailure
|
2016-12-15 21:36:41 +00:00
|
|
|
}
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 02:22:20 +00:00
|
|
|
cmd := exec.Command("git", "rev-parse", "HEAD")
|
|
|
|
cmd.Dir = u.repositoryLocalPath
|
|
|
|
out, err := cmd.CombinedOutput()
|
2016-11-18 21:24:54 +00:00
|
|
|
if err != nil {
|
2017-01-04 02:44:32 +00:00
|
|
|
return "", vulnsrc.ErrGitFailure
|
2016-11-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commit = strings.TrimSpace(string(out))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
type secDBFile 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"`
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
func parseYAML(r io.Reader) (vulns []database.VulnerabilityWithAffected, err error) {
|
2016-12-14 22:01:38 +00:00
|
|
|
var rBytes []byte
|
|
|
|
rBytes, err = ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:48:42 +00:00
|
|
|
var file secDBFile
|
2016-12-14 22:01:38 +00:00
|
|
|
err = yaml.Unmarshal(rBytes, &file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pack := range file.Packages {
|
|
|
|
pkg := pack.Pkg
|
2016-12-28 01:45:11 +00:00
|
|
|
for version, vulnStrs := range pkg.Fixes {
|
2017-01-03 21:00:20 +00:00
|
|
|
err := versionfmt.Valid(dpkg.ParserName, version)
|
2016-12-14 22:01:38 +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")
|
2016-12-14 22:01:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vulnStr := range vulnStrs {
|
2017-07-26 23:22:29 +00:00
|
|
|
var vuln database.VulnerabilityWithAffected
|
2017-01-19 18:42:37 +00:00
|
|
|
vuln.Severity = database.UnknownSeverity
|
2016-12-14 22:01:38 +00:00
|
|
|
vuln.Name = vulnStr
|
|
|
|
vuln.Link = nvdURLPrefix + vulnStr
|
2017-07-26 23:22:29 +00:00
|
|
|
|
|
|
|
var fixedInVersion string
|
|
|
|
if version != versionfmt.MaxVersion {
|
|
|
|
fixedInVersion = version
|
|
|
|
}
|
|
|
|
vuln.Affected = []database.AffectedFeature{
|
2016-12-14 22:01:38 +00:00
|
|
|
{
|
2017-07-26 23:22:29 +00:00
|
|
|
FeatureName: pkg.Name,
|
|
|
|
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
|
|
|
|
}
|