2015-11-26 21:52:25 +01:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2017-09-19 21:24:31 +02:00
|
|
|
;;; Copyright © 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
|
2015-11-26 21:52:25 +01:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix cve)
|
|
|
|
#:use-module (guix utils)
|
|
|
|
#:use-module (guix http-client)
|
2016-05-23 17:42:32 +02:00
|
|
|
#:use-module ((guix build utils) #:select (mkdir-p))
|
2015-11-26 21:52:25 +01:00
|
|
|
#:use-module (sxml ssax)
|
|
|
|
#:use-module (web uri)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-9)
|
2016-05-28 00:44:36 +02:00
|
|
|
#:use-module (srfi srfi-11)
|
2015-11-26 21:52:25 +01:00
|
|
|
#:use-module (srfi srfi-19)
|
2016-05-26 23:00:08 +02:00
|
|
|
#:use-module (srfi srfi-26)
|
2015-11-26 21:52:25 +01:00
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (ice-9 regex)
|
|
|
|
#:use-module (ice-9 vlist)
|
|
|
|
#:export (vulnerability?
|
|
|
|
vulnerability-id
|
|
|
|
vulnerability-packages
|
|
|
|
|
|
|
|
xml->vulnerabilities
|
|
|
|
current-vulnerabilities
|
|
|
|
vulnerabilities->lookup-proc))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This modules provides the tools to fetch, parse, and digest part of the
|
|
|
|
;;; Common Vulnerabilities and Exposures (CVE) feeds provided by the US NIST
|
|
|
|
;;; at <https://nvd.nist.gov/download.cfm#CVE_FEED>.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define-record-type <vulnerability>
|
|
|
|
(vulnerability id packages)
|
|
|
|
vulnerability?
|
2016-05-28 00:44:36 +02:00
|
|
|
(id vulnerability-id) ;string
|
|
|
|
(packages vulnerability-packages)) ;((p1 v1 v2 v3) (p2 v1) ...)
|
2015-11-26 21:52:25 +01:00
|
|
|
|
2016-03-11 15:55:57 +01:00
|
|
|
(define %now
|
|
|
|
(current-date))
|
|
|
|
(define %current-year
|
|
|
|
(date-year %now))
|
|
|
|
(define %past-year
|
|
|
|
(- %current-year 1))
|
|
|
|
|
|
|
|
(define (yearly-feed-uri year)
|
|
|
|
"Return the URI for the CVE feed for YEAR."
|
2015-11-26 21:52:25 +01:00
|
|
|
(string->uri
|
2016-03-11 15:55:57 +01:00
|
|
|
(string-append "https://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-"
|
|
|
|
(number->string year) ".xml.gz")))
|
2015-11-26 21:52:25 +01:00
|
|
|
|
2016-03-11 15:55:57 +01:00
|
|
|
(define %current-year-ttl
|
2015-11-26 21:52:25 +01:00
|
|
|
;; According to <https://nvd.nist.gov/download.cfm#CVE_FEED>, feeds are
|
|
|
|
;; updated "approximately every two hours."
|
|
|
|
(* 3600 3))
|
|
|
|
|
2016-03-11 15:55:57 +01:00
|
|
|
(define %past-year-ttl
|
|
|
|
;; Update the previous year's database more and more infrequently.
|
|
|
|
(* 3600 24 2 (date-month %now)))
|
|
|
|
|
|
|
|
(define (call-with-cve-port uri ttl proc)
|
2015-11-26 21:52:25 +01:00
|
|
|
"Pass PROC an input port from which to read the CVE stream."
|
2016-05-23 17:46:59 +02:00
|
|
|
(let ((port (http-fetch uri)))
|
2015-11-26 21:52:25 +01:00
|
|
|
(dynamic-wind
|
|
|
|
(const #t)
|
|
|
|
(lambda ()
|
|
|
|
(call-with-decompressed-port 'gzip port
|
2016-03-11 15:55:57 +01:00
|
|
|
(lambda (port)
|
|
|
|
(setvbuf port _IOFBF 65536)
|
|
|
|
(proc port))))
|
2015-11-26 21:52:25 +01:00
|
|
|
(lambda ()
|
|
|
|
(close-port port)))))
|
|
|
|
|
|
|
|
(define %cpe-package-rx
|
2016-03-11 10:21:58 +01:00
|
|
|
;; For applications: "cpe:/a:VENDOR:PACKAGE:VERSION", or sometimes
|
|
|
|
;; "cpe/a:VENDOR:PACKAGE:VERSION:PATCH-LEVEL".
|
|
|
|
(make-regexp "^cpe:/a:([^:]+):([^:]+):([^:]+)((:.+)?)"))
|
2015-11-26 21:52:25 +01:00
|
|
|
|
|
|
|
(define (cpe->package-name cpe)
|
|
|
|
"Converts the Common Platform Enumeration (CPE) string CPE to a package
|
2016-05-28 00:44:36 +02:00
|
|
|
name, in a very naive way. Return two values: the package name, and its
|
|
|
|
version string. Return #f and #f if CPE does not look like an application CPE
|
|
|
|
string."
|
|
|
|
(cond ((regexp-exec %cpe-package-rx (string-trim-both cpe))
|
|
|
|
=>
|
2015-11-26 21:52:25 +01:00
|
|
|
(lambda (matches)
|
2016-05-28 00:44:36 +02:00
|
|
|
(values (match:substring matches 2)
|
|
|
|
(string-append (match:substring matches 3)
|
|
|
|
(match (match:substring matches 4)
|
|
|
|
("" "")
|
|
|
|
(patch-level
|
|
|
|
;; Drop the colon from things like
|
|
|
|
;; "cpe:/a:openbsd:openssh:6.8:p1".
|
|
|
|
(string-drop patch-level 1)))))))
|
|
|
|
(else
|
|
|
|
(values #f #f))))
|
|
|
|
|
|
|
|
(define (cpe->product-alist products)
|
|
|
|
"Given PRODUCTS, a list of CPE names, return the subset limited to the
|
|
|
|
applications listed in PRODUCTS, with names converted to package names:
|
|
|
|
|
|
|
|
(cpe->product-alist
|
|
|
|
'(\"cpe:/a:gnu:libtasn1:4.7\" \"cpe:/a:gnu:libtasn1:4.6\" \"cpe:/a:gnu:cpio:2.11\"))
|
|
|
|
=> ((\"libtasn1\" \"4.7\" \"4.6\") (\"cpio\" \"2.11\"))
|
|
|
|
"
|
|
|
|
(fold (lambda (product result)
|
|
|
|
(let-values (((name version) (cpe->package-name product)))
|
|
|
|
(if name
|
|
|
|
(match result
|
|
|
|
(((previous . versions) . tail)
|
|
|
|
;; Attempt to coalesce NAME and PREVIOUS.
|
|
|
|
(if (string=? name previous)
|
|
|
|
(alist-cons name (cons version versions) tail)
|
|
|
|
(alist-cons name (list version) result)))
|
|
|
|
(()
|
|
|
|
(alist-cons name (list version) result)))
|
|
|
|
result)))
|
|
|
|
'()
|
|
|
|
(sort products string<?)))
|
2015-11-26 21:52:25 +01:00
|
|
|
|
|
|
|
(define %parse-vulnerability-feed
|
|
|
|
;; Parse the XML vulnerability feed from
|
|
|
|
;; <https://nvd.nist.gov/download.cfm#CVE_FEED> and return a list of
|
|
|
|
;; vulnerability objects.
|
|
|
|
(ssax:make-parser NEW-LEVEL-SEED
|
|
|
|
(lambda (elem-gi attributes namespaces expected-content
|
|
|
|
seed)
|
|
|
|
(match elem-gi
|
|
|
|
((name-space . 'entry)
|
|
|
|
(cons (assoc-ref attributes 'id) seed))
|
|
|
|
((name-space . 'vulnerable-software-list)
|
|
|
|
(cons '() seed))
|
|
|
|
((name-space . 'product)
|
|
|
|
(cons 'product seed))
|
|
|
|
(x seed)))
|
|
|
|
|
|
|
|
FINISH-ELEMENT
|
|
|
|
(lambda (elem-gi attributes namespaces parent-seed
|
|
|
|
seed)
|
|
|
|
(match elem-gi
|
|
|
|
((name-space . 'entry)
|
|
|
|
(match seed
|
|
|
|
(((? string? id) . rest)
|
|
|
|
;; Some entries have no vulnerable-software-list.
|
|
|
|
rest)
|
|
|
|
((products id . rest)
|
2016-05-28 00:44:36 +02:00
|
|
|
(match (cpe->product-alist products)
|
2015-11-26 21:52:25 +01:00
|
|
|
(()
|
|
|
|
;; No application among PRODUCTS.
|
|
|
|
rest)
|
|
|
|
(packages
|
2016-05-28 00:44:36 +02:00
|
|
|
(cons (vulnerability id packages)
|
2015-11-26 21:52:25 +01:00
|
|
|
rest))))))
|
|
|
|
(x
|
|
|
|
seed)))
|
|
|
|
|
|
|
|
CHAR-DATA-HANDLER
|
|
|
|
(lambda (str _ seed)
|
|
|
|
(match seed
|
|
|
|
(('product software-list . rest)
|
|
|
|
;; Add STR to the vulnerable software list this
|
|
|
|
;; <product> tag is part of.
|
|
|
|
(cons (cons str software-list) rest))
|
|
|
|
(x x)))))
|
|
|
|
|
|
|
|
(define (xml->vulnerabilities port)
|
|
|
|
"Read from PORT an XML feed of vulnerabilities and return a list of
|
|
|
|
vulnerability objects."
|
|
|
|
(reverse (%parse-vulnerability-feed port '())))
|
|
|
|
|
2016-05-23 17:42:32 +02:00
|
|
|
(define vulnerability->sexp
|
|
|
|
(match-lambda
|
|
|
|
(($ <vulnerability> id packages)
|
|
|
|
`(v ,id ,packages))))
|
|
|
|
|
|
|
|
(define sexp->vulnerability
|
|
|
|
(match-lambda
|
|
|
|
(('v id (packages ...))
|
|
|
|
(vulnerability id packages))))
|
|
|
|
|
|
|
|
(define (fetch-vulnerabilities year ttl)
|
|
|
|
"Return the list of <vulnerability> for YEAR, assuming the on-disk cache has
|
|
|
|
the given TTL (fetch from the NIST web site when TTL has expired)."
|
|
|
|
;; Note: We used to keep the original XML files in cache but parsing it
|
|
|
|
;; would take typically ~15s for a year of data. Thus, we instead store a
|
|
|
|
;; summarized version thereof as an sexp, which can be parsed in 1s or so.
|
|
|
|
(define cache
|
|
|
|
(string-append (cache-directory) "/cve/" (number->string year)))
|
|
|
|
|
|
|
|
(define (do-fetch)
|
|
|
|
(call-with-cve-port (yearly-feed-uri year) ttl
|
2016-03-11 15:55:57 +01:00
|
|
|
(lambda (port)
|
|
|
|
;; XXX: The SSAX "error port" is used to send pointless warnings such as
|
|
|
|
;; "warning: Skipping PI". Turn that off.
|
2016-05-26 23:00:08 +02:00
|
|
|
(format (current-error-port) "fetching CVE database for ~a...~%" year)
|
2016-03-11 15:55:57 +01:00
|
|
|
(parameterize ((current-ssax-error-port (%make-void-port "w")))
|
|
|
|
(xml->vulnerabilities port)))))
|
|
|
|
|
2016-05-23 17:42:32 +02:00
|
|
|
(define (update-cache)
|
|
|
|
(mkdir-p (dirname cache))
|
|
|
|
(let ((vulns (do-fetch)))
|
|
|
|
(with-atomic-file-output cache
|
|
|
|
(lambda (port)
|
|
|
|
(write `(vulnerabilities
|
2016-05-28 00:44:36 +02:00
|
|
|
1 ;format version
|
2016-05-23 17:42:32 +02:00
|
|
|
,(map vulnerability->sexp vulns))
|
|
|
|
port)))
|
|
|
|
vulns))
|
|
|
|
|
|
|
|
(define (old? file)
|
|
|
|
;; Return true if PORT has passed TTL.
|
|
|
|
(let* ((s (stat file))
|
|
|
|
(now (current-time time-utc)))
|
|
|
|
(< (+ (stat:mtime s) ttl) (time-second now))))
|
|
|
|
|
2017-09-19 21:24:31 +02:00
|
|
|
(define (read* port)
|
|
|
|
;; Disable read options to avoid populating the source property weak
|
|
|
|
;; table, which speeds things up, saves memory, and works around
|
|
|
|
;; <https://lists.gnu.org/archive/html/guile-devel/2017-09/msg00031.html>.
|
|
|
|
(let ((options (read-options)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda ()
|
|
|
|
(read-disable 'positions))
|
|
|
|
(lambda ()
|
|
|
|
(read port))
|
|
|
|
(lambda ()
|
|
|
|
(read-options options)))))
|
|
|
|
|
2016-05-23 17:42:32 +02:00
|
|
|
(catch 'system-error
|
|
|
|
(lambda ()
|
|
|
|
(if (old? cache)
|
|
|
|
(update-cache)
|
2017-09-19 21:24:31 +02:00
|
|
|
(match (call-with-input-file cache read*)
|
2016-05-28 00:44:36 +02:00
|
|
|
(('vulnerabilities 1 vulns)
|
2016-05-23 17:42:32 +02:00
|
|
|
(map sexp->vulnerability vulns))
|
|
|
|
(x
|
|
|
|
(update-cache)))))
|
|
|
|
(lambda args
|
|
|
|
(update-cache))))
|
|
|
|
|
|
|
|
(define (current-vulnerabilities)
|
|
|
|
"Return the current list of Common Vulnerabilities and Exposures (CVE) as
|
|
|
|
published by the US NIST."
|
2016-05-26 23:00:08 +02:00
|
|
|
(let ((past-years (unfold (cut > <> 3)
|
|
|
|
(lambda (n)
|
|
|
|
(- %current-year n))
|
|
|
|
1+
|
|
|
|
1))
|
|
|
|
(past-ttls (unfold (cut > <> 3)
|
|
|
|
(lambda (n)
|
|
|
|
(* n %past-year-ttl))
|
|
|
|
1+
|
|
|
|
1)))
|
|
|
|
(append-map fetch-vulnerabilities
|
|
|
|
(cons %current-year past-years)
|
|
|
|
(cons %current-year-ttl past-ttls))))
|
2015-11-26 21:52:25 +01:00
|
|
|
|
|
|
|
(define (vulnerabilities->lookup-proc vulnerabilities)
|
|
|
|
"Return a lookup procedure built from VULNERABILITIES that takes a package
|
|
|
|
name and optionally a version number. When the version is omitted, the lookup
|
2016-05-28 00:44:36 +02:00
|
|
|
procedure returns a list of vulnerabilities; otherwise, it returns a list of
|
|
|
|
vulnerabilities affecting the given package version."
|
2015-11-26 21:52:25 +01:00
|
|
|
(define table
|
|
|
|
;; Map package names to lists of version/vulnerability pairs.
|
|
|
|
(fold (lambda (vuln table)
|
|
|
|
(match vuln
|
|
|
|
(($ <vulnerability> id packages)
|
|
|
|
(fold (lambda (package table)
|
|
|
|
(match package
|
2016-05-28 00:44:36 +02:00
|
|
|
((name . versions)
|
|
|
|
(vhash-cons name (cons vuln versions)
|
2015-11-26 21:52:25 +01:00
|
|
|
table))))
|
|
|
|
table
|
|
|
|
packages))))
|
|
|
|
vlist-null
|
|
|
|
vulnerabilities))
|
|
|
|
|
|
|
|
(lambda* (package #:optional version)
|
|
|
|
(vhash-fold* (if version
|
|
|
|
(lambda (pair result)
|
|
|
|
(match pair
|
2016-05-28 00:44:36 +02:00
|
|
|
((vuln . versions)
|
|
|
|
(if (member version versions)
|
2015-11-26 21:52:25 +01:00
|
|
|
(cons vuln result)
|
|
|
|
result))))
|
2016-05-28 00:44:36 +02:00
|
|
|
(lambda (pair result)
|
|
|
|
(match pair
|
|
|
|
((vuln . _)
|
|
|
|
(cons vuln result)))))
|
2015-11-26 21:52:25 +01:00
|
|
|
'()
|
|
|
|
package table)))
|
|
|
|
|
2016-03-11 15:55:57 +01:00
|
|
|
|
|
|
|
;;; Local Variables:
|
|
|
|
;;; eval: (put 'call-with-cve-port 'scheme-indent-function 2)
|
|
|
|
;;; End:
|
|
|
|
|
2015-11-26 21:52:25 +01:00
|
|
|
;;; cve.scm ends here
|