mirror of
https://git.yoctoproject.org/poky
synced 2026-06-01 13:09:50 +00:00
cve-check: backport rewrite from master
As detailed at [1] the XML feeds provided by NIST are being discontinued on October 9th 2019. As cve-check-tool uses these feeds, cve-check.bbclass will be inoperable after this date. To ensure that cve-check continues working, backport the following commits from master to move away from the unmaintained cve-check-tool to our own Python code that fetches the JSON: 546d14135c5 cve-update-db: New recipe to update CVE database bc144b028f6 cve-check: Remove dependency to cve-check-tool-native 7f62a20b32a cve-check: Manage CVE_PRODUCT with more than one name 3bf63bc6084 cve-check: Consider CVE that affects versions with less than operator c0eabd30d7b cve-update-db: Use std library instead of urllib3 27eb839ee65 cve-check: be idiomatic 09be21f4d17 cve-update-db: Manage proxy if needed. 975793e3825 cve-update-db: do_populate_cve_db depends on do_fetch 0325dd72714 cve-update-db: Catch request.urlopen errors. 4078da92b49 cve-check: Depends on cve-update-db-native f7676e9a38d cve-update-db: Use NVD CPE data to populate PRODUCTS table bc0195be1b1 cve-check: Update unpatched CVE matching c807c2a6409 cve-update-db-native: Skip recipe when cve-check class is not loaded. 07bb8b25e17 cve-check: remove redundant readline CVE whitelisting 5388ed6d137 cve-check-tool: remove 270ac00cb43 cve-check.bbclass: initialize to_append e6bf9000987 cve-check: allow comparison of Vendor as well as Product 91770338f76 cve-update-db-native: use SQL placeholders instead of format strings 7069302a4cc cve-check: Replace CVE_CHECK_CVE_WHITELIST by CVE_CHECK_WHITELIST 78de2cb39d7 cve-update-db-native: Remove hash column from database. 4b301030cf9 cve-update-db-native: use os.path.join instead of + f0d822fad2a cve-update-db: actually inherit native b309840b6aa cve-update-db-native: use executemany() to optimise CPE insertion bb4e53af33d cve-update-db-native: improve metadata parsing 94227459792 cve-update-db-native: clean up JSON fetching 95438d52b73 cve-update-db-native: fix https proxy issues 1f9a963b9ff glibc: exclude child recipes from CVE scanning [1] https://nvd.nist.gov/General/News/XML-Vulnerability-Feed-Retirement (From OE-Core rev: 8c87e78547c598cada1bce92e7b25d85b994e2eb) Signed-off-by: Ross Burton <ross.burton@intel.com> Signed-off-by: Armin Kuster <akuster808@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
committed by
Richard Purdie
parent
5be20f94d6
commit
411624fa50
@@ -26,7 +26,7 @@ CVE_PRODUCT ??= "${BPN}"
|
||||
CVE_VERSION ??= "${PV}"
|
||||
|
||||
CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK"
|
||||
CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvd.db"
|
||||
CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_1.0.db"
|
||||
|
||||
CVE_CHECK_LOG ?= "${T}/cve.log"
|
||||
CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check"
|
||||
@@ -37,32 +37,33 @@ CVE_CHECK_COPY_FILES ??= "1"
|
||||
CVE_CHECK_CREATE_MANIFEST ??= "1"
|
||||
|
||||
# Whitelist for packages (PN)
|
||||
CVE_CHECK_PN_WHITELIST = "\
|
||||
glibc-locale \
|
||||
"
|
||||
CVE_CHECK_PN_WHITELIST ?= ""
|
||||
|
||||
# Whitelist for CVE and version of package
|
||||
CVE_CHECK_CVE_WHITELIST = "{\
|
||||
'CVE-2014-2524': ('6.3','5.2',), \
|
||||
}"
|
||||
# Whitelist for CVE. If a CVE is found, then it is considered patched.
|
||||
# The value is a string containing space separated CVE values:
|
||||
#
|
||||
# CVE_CHECK_WHITELIST = 'CVE-2014-2524 CVE-2018-1234'
|
||||
#
|
||||
CVE_CHECK_WHITELIST ?= ""
|
||||
|
||||
python do_cve_check () {
|
||||
"""
|
||||
Check recipe for patched and unpatched CVEs
|
||||
"""
|
||||
|
||||
if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")):
|
||||
if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
|
||||
patched_cves = get_patches_cves(d)
|
||||
patched, unpatched = check_cves(d, patched_cves)
|
||||
if patched or unpatched:
|
||||
cve_data = get_cve_info(d, patched + unpatched)
|
||||
cve_write_data(d, patched, unpatched, cve_data)
|
||||
else:
|
||||
bb.note("Failed to update CVE database, skipping CVE check")
|
||||
bb.note("No CVE database found, skipping CVE check")
|
||||
|
||||
}
|
||||
|
||||
addtask cve_check after do_unpack before do_build
|
||||
do_cve_check[depends] = "cve-check-tool-native:do_populate_sysroot cve-check-tool-native:do_populate_cve_db"
|
||||
do_cve_check[depends] = "cve-update-db-native:do_populate_cve_db"
|
||||
do_cve_check[nostamp] = "1"
|
||||
|
||||
python cve_check_cleanup () {
|
||||
@@ -163,65 +164,94 @@ def get_patches_cves(d):
|
||||
|
||||
def check_cves(d, patched_cves):
|
||||
"""
|
||||
Run cve-check-tool looking for patched and unpatched CVEs.
|
||||
Connect to the NVD database and find unpatched cves.
|
||||
"""
|
||||
|
||||
import ast, csv, tempfile, subprocess, io
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
cves_patched = []
|
||||
cves_unpatched = []
|
||||
bpn = d.getVar("CVE_PRODUCT")
|
||||
# CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
|
||||
products = d.getVar("CVE_PRODUCT").split()
|
||||
# If this has been unset then we're not scanning for CVEs here (for example, image recipes)
|
||||
if not bpn:
|
||||
if not products:
|
||||
return ([], [])
|
||||
pv = d.getVar("CVE_VERSION").split("+git")[0]
|
||||
cves = " ".join(patched_cves)
|
||||
cve_db_dir = d.getVar("CVE_CHECK_DB_DIR")
|
||||
cve_whitelist = ast.literal_eval(d.getVar("CVE_CHECK_CVE_WHITELIST"))
|
||||
cve_cmd = "cve-check-tool"
|
||||
cmd = [cve_cmd, "--no-html", "--skip-update", "--csv", "--not-affected", "-t", "faux", "-d", cve_db_dir]
|
||||
|
||||
# If the recipe has been whitlisted we return empty lists
|
||||
if d.getVar("PN") in d.getVar("CVE_CHECK_PN_WHITELIST").split():
|
||||
bb.note("Recipe has been whitelisted, skipping check")
|
||||
return ([], [])
|
||||
|
||||
try:
|
||||
# Write the faux CSV file to be used with cve-check-tool
|
||||
fd, faux = tempfile.mkstemp(prefix="cve-faux-")
|
||||
with os.fdopen(fd, "w") as f:
|
||||
for pn in bpn.split():
|
||||
f.write("%s,%s,%s,\n" % (pn, pv, cves))
|
||||
cmd.append(faux)
|
||||
old_cve_whitelist = d.getVar("CVE_CHECK_CVE_WHITELIST")
|
||||
if old_cve_whitelist:
|
||||
bb.warn("CVE_CHECK_CVE_WHITELIST is deprecated, please use CVE_CHECK_WHITELIST.")
|
||||
cve_whitelist = d.getVar("CVE_CHECK_WHITELIST").split()
|
||||
|
||||
output = subprocess.check_output(cmd).decode("utf-8")
|
||||
bb.debug(2, "Output of command %s:\n%s" % ("\n".join(cmd), output))
|
||||
except subprocess.CalledProcessError as e:
|
||||
bb.warn("Couldn't check for CVEs: %s (output %s)" % (e, e.output))
|
||||
finally:
|
||||
os.remove(faux)
|
||||
import sqlite3
|
||||
db_file = d.getVar("CVE_CHECK_DB_FILE")
|
||||
conn = sqlite3.connect(db_file)
|
||||
|
||||
for row in csv.reader(io.StringIO(output)):
|
||||
# Third row has the unpatched CVEs
|
||||
if row[2]:
|
||||
for cve in row[2].split():
|
||||
# Skip if the CVE has been whitlisted for the current version
|
||||
if pv in cve_whitelist.get(cve,[]):
|
||||
bb.note("%s-%s has been whitelisted for %s" % (bpn, pv, cve))
|
||||
else:
|
||||
for product in products:
|
||||
c = conn.cursor()
|
||||
if ":" in product:
|
||||
vendor, product = product.split(":", 1)
|
||||
c.execute("SELECT * FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR IS ?", (product, vendor))
|
||||
else:
|
||||
c.execute("SELECT * FROM PRODUCTS WHERE PRODUCT IS ?", (product,))
|
||||
|
||||
for row in c:
|
||||
cve = row[0]
|
||||
version_start = row[3]
|
||||
operator_start = row[4]
|
||||
version_end = row[5]
|
||||
operator_end = row[6]
|
||||
|
||||
if cve in cve_whitelist:
|
||||
bb.note("%s-%s has been whitelisted for %s" % (product, pv, cve))
|
||||
elif cve in patched_cves:
|
||||
bb.note("%s has been patched" % (cve))
|
||||
else:
|
||||
to_append = False
|
||||
if (operator_start == '=' and pv == version_start):
|
||||
cves_unpatched.append(cve)
|
||||
bb.debug(2, "%s-%s is not patched for %s" % (bpn, pv, cve))
|
||||
# Fourth row has patched CVEs
|
||||
if row[3]:
|
||||
for cve in row[3].split():
|
||||
cves_patched.append(cve)
|
||||
bb.debug(2, "%s-%s is patched for %s" % (bpn, pv, cve))
|
||||
else:
|
||||
if operator_start:
|
||||
try:
|
||||
to_append_start = (operator_start == '>=' and LooseVersion(pv) >= LooseVersion(version_start))
|
||||
to_append_start |= (operator_start == '>' and LooseVersion(pv) > LooseVersion(version_start))
|
||||
except:
|
||||
bb.note("%s: Failed to compare %s %s %s for %s" %
|
||||
(product, pv, operator_start, version_start, cve))
|
||||
to_append_start = False
|
||||
else:
|
||||
to_append_start = False
|
||||
|
||||
return (cves_patched, cves_unpatched)
|
||||
if operator_end:
|
||||
try:
|
||||
to_append_end = (operator_end == '<=' and LooseVersion(pv) <= LooseVersion(version_end))
|
||||
to_append_end |= (operator_end == '<' and LooseVersion(pv) < LooseVersion(version_end))
|
||||
except:
|
||||
bb.note("%s: Failed to compare %s %s %s for %s" %
|
||||
(product, pv, operator_end, version_end, cve))
|
||||
to_append_end = False
|
||||
else:
|
||||
to_append_end = False
|
||||
|
||||
if operator_start and operator_end:
|
||||
to_append = to_append_start and to_append_end
|
||||
else:
|
||||
to_append = to_append_start or to_append_end
|
||||
|
||||
if to_append:
|
||||
cves_unpatched.append(cve)
|
||||
bb.debug(2, "%s-%s is not patched for %s" % (product, pv, cve))
|
||||
conn.close()
|
||||
|
||||
return (list(patched_cves), cves_unpatched)
|
||||
|
||||
def get_cve_info(d, cves):
|
||||
"""
|
||||
Get CVE information from the database used by cve-check-tool.
|
||||
Get CVE information from the database.
|
||||
|
||||
Unfortunately the only way to get CVE info is set the output to
|
||||
html (hard to parse) or query directly the database.
|
||||
@@ -241,9 +271,10 @@ def get_cve_info(d, cves):
|
||||
for row in cur.execute(query, tuple(cves)):
|
||||
cve_data[row[0]] = {}
|
||||
cve_data[row[0]]["summary"] = row[1]
|
||||
cve_data[row[0]]["score"] = row[2]
|
||||
cve_data[row[0]]["modified"] = row[3]
|
||||
cve_data[row[0]]["vector"] = row[4]
|
||||
cve_data[row[0]]["scorev2"] = row[2]
|
||||
cve_data[row[0]]["scorev3"] = row[3]
|
||||
cve_data[row[0]]["modified"] = row[4]
|
||||
cve_data[row[0]]["vector"] = row[5]
|
||||
conn.close()
|
||||
|
||||
return cve_data
|
||||
@@ -270,7 +301,8 @@ def cve_write_data(d, patched, unpatched, cve_data):
|
||||
unpatched_cves.append(cve)
|
||||
write_string += "CVE STATUS: Unpatched\n"
|
||||
write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
|
||||
write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["score"]
|
||||
write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
|
||||
write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
|
||||
write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
|
||||
write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user