blob: 19ed5548b3ad551656141f3c9882bc90c67a9172 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001# This class is used to check recipes against public CVEs.
2#
3# In order to use this class just inherit the class in the
4# local.conf file and it will add the cve_check task for
5# every recipe. The task can be used per recipe, per image,
6# or using the special cases "world" and "universe". The
7# cve_check task will print a warning for every unpatched
8# CVE found and generate a file in the recipe WORKDIR/cve
9# directory. If an image is build it will generate a report
10# in DEPLOY_DIR_IMAGE for all the packages used.
11#
12# Example:
13# bitbake -c cve_check openssl
14# bitbake core-image-sato
15# bitbake -k -c cve_check universe
16#
17# DISCLAIMER
18#
19# This class/tool is meant to be used as support and not
20# the only method to check against CVEs. Running this tool
21# doesn't guarantee your packages are free of CVEs.
22
Brad Bishop37a0e4d2017-12-04 01:01:44 -050023# The product name that the CVE database uses. Defaults to BPN, but may need to
24# be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff).
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025CVE_PRODUCT ??= "${BPN}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040026CVE_VERSION ??= "${PV}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050027
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK"
Brad Bishop96ff1982019-08-19 13:50:42 -040029CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_1.0.db"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060030
Brad Bishop316dfdd2018-06-25 12:45:53 -040031CVE_CHECK_LOG ?= "${T}/cve.log"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060032CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check"
33
34CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve"
35CVE_CHECK_MANIFEST ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve"
36CVE_CHECK_COPY_FILES ??= "1"
37CVE_CHECK_CREATE_MANIFEST ??= "1"
38
39# Whitelist for packages (PN)
Brad Bishop96ff1982019-08-19 13:50:42 -040040CVE_CHECK_PN_WHITELIST ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060041
Brad Bishop96ff1982019-08-19 13:50:42 -040042# Whitelist for CVE. If a CVE is found, then it is considered patched.
43# The value is a string containing space separated CVE values:
44#
45# CVE_CHECK_WHITELIST = 'CVE-2014-2524 CVE-2018-1234'
46#
47CVE_CHECK_WHITELIST ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048
49python do_cve_check () {
50 """
51 Check recipe for patched and unpatched CVEs
52 """
53
Brad Bishop96ff1982019-08-19 13:50:42 -040054 if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060055 patched_cves = get_patches_cves(d)
56 patched, unpatched = check_cves(d, patched_cves)
57 if patched or unpatched:
58 cve_data = get_cve_info(d, patched + unpatched)
59 cve_write_data(d, patched, unpatched, cve_data)
60 else:
Brad Bishop96ff1982019-08-19 13:50:42 -040061 bb.note("No CVE database found, skipping CVE check")
62
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063}
64
Brad Bishop1d80a2e2019-11-15 16:35:03 -050065addtask cve_check before do_build
Brad Bishop96ff1982019-08-19 13:50:42 -040066do_cve_check[depends] = "cve-update-db-native:do_populate_cve_db"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067do_cve_check[nostamp] = "1"
68
69python cve_check_cleanup () {
70 """
71 Delete the file used to gather all the CVE information.
72 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074}
75
76addhandler cve_check_cleanup
77cve_check_cleanup[eventmask] = "bb.cooker.CookerExit"
78
79python cve_check_write_rootfs_manifest () {
80 """
81 Create CVE manifest when building an image
82 """
83
84 import shutil
85
Brad Bishopd7bf8c12018-02-25 22:55:05 -050086 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
87 deploy_file = os.path.join(d.getVar("CVE_CHECK_DIR"), d.getVar("PN"))
88 if os.path.exists(deploy_file):
89 bb.utils.remove(deploy_file)
90
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060092 bb.note("Writing rootfs CVE manifest")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 deploy_dir = d.getVar("DEPLOY_DIR_IMAGE")
94 link_name = d.getVar("IMAGE_LINK_NAME")
95 manifest_name = d.getVar("CVE_CHECK_MANIFEST")
96 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060097
98 shutil.copyfile(cve_tmp_file, manifest_name)
99
100 if manifest_name and os.path.exists(manifest_name):
101 manifest_link = os.path.join(deploy_dir, "%s.cve" % link_name)
102 # If we already have another manifest, update symlinks
103 if os.path.exists(os.path.realpath(manifest_link)):
104 os.remove(manifest_link)
105 os.symlink(os.path.basename(manifest_name), manifest_link)
106 bb.plain("Image CVE report stored in: %s" % manifest_name)
107}
108
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109ROOTFS_POSTPROCESS_COMMAND_prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600111
112def get_patches_cves(d):
113 """
114 Get patches that solve CVEs using the "CVE: " tag.
115 """
116
117 import re
118
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 pn = d.getVar("PN")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 cve_match = re.compile("CVE:( CVE\-\d{4}\-\d+)+")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121
122 # Matches last CVE-1234-211432 in the file name, also if written
123 # with small letters. Not supporting multiple CVE id's in a single
124 # file name.
125 cve_file_name_match = re.compile(".*([Cc][Vv][Ee]\-\d{4}\-\d+)")
126
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600127 patched_cves = set()
128 bb.debug(2, "Looking for patches that solves CVEs for %s" % pn)
129 for url in src_patches(d):
130 patch_file = bb.fetch.decodeurl(url)[2]
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131
132 # Check patch file name for CVE ID
133 fname_match = cve_file_name_match.search(patch_file)
134 if fname_match:
135 cve = fname_match.group(1).upper()
136 patched_cves.add(cve)
137 bb.debug(2, "Found CVE %s from patch file name %s" % (cve, patch_file))
138
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139 with open(patch_file, "r", encoding="utf-8") as f:
140 try:
141 patch_text = f.read()
142 except UnicodeDecodeError:
143 bb.debug(1, "Failed to read patch %s using UTF-8 encoding"
144 " trying with iso8859-1" % patch_file)
145 f.close()
146 with open(patch_file, "r", encoding="iso8859-1") as f:
147 patch_text = f.read()
148
Brad Bishopbba38f32018-08-23 16:11:46 +0800149 # Search for one or more "CVE: " lines
150 text_match = False
151 for match in cve_match.finditer(patch_text):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600152 # Get only the CVEs without the "CVE: " tag
153 cves = patch_text[match.start()+5:match.end()]
154 for cve in cves.split():
155 bb.debug(2, "Patch %s solves %s" % (patch_file, cve))
156 patched_cves.add(cve)
Brad Bishopbba38f32018-08-23 16:11:46 +0800157 text_match = True
158
159 if not fname_match and not text_match:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600160 bb.debug(2, "Patch %s doesn't solve CVEs" % patch_file)
161
162 return patched_cves
163
164def check_cves(d, patched_cves):
165 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400166 Connect to the NVD database and find unpatched cves.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400168 from distutils.version import LooseVersion
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600169
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170 cves_unpatched = []
Brad Bishopf3fd2882019-06-21 08:06:37 -0400171 # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
Brad Bishop96ff1982019-08-19 13:50:42 -0400172 products = d.getVar("CVE_PRODUCT").split()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400173 # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
Brad Bishop96ff1982019-08-19 13:50:42 -0400174 if not products:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400175 return ([], [])
176 pv = d.getVar("CVE_VERSION").split("+git")[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600177
178 # If the recipe has been whitlisted we return empty lists
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 if d.getVar("PN") in d.getVar("CVE_CHECK_PN_WHITELIST").split():
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600180 bb.note("Recipe has been whitelisted, skipping check")
181 return ([], [])
182
Brad Bishop96ff1982019-08-19 13:50:42 -0400183 old_cve_whitelist = d.getVar("CVE_CHECK_CVE_WHITELIST")
184 if old_cve_whitelist:
185 bb.warn("CVE_CHECK_CVE_WHITELIST is deprecated, please use CVE_CHECK_WHITELIST.")
186 cve_whitelist = d.getVar("CVE_CHECK_WHITELIST").split()
187
Brad Bishopf3fd2882019-06-21 08:06:37 -0400188 import sqlite3
Brad Bishop6dbb3162019-11-25 09:41:34 -0500189 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
190 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600191
Brad Bishop6dbb3162019-11-25 09:41:34 -0500192 # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
Brad Bishop96ff1982019-08-19 13:50:42 -0400193 for product in products:
Brad Bishop96ff1982019-08-19 13:50:42 -0400194 if ":" in product:
195 vendor, product = product.split(":", 1)
Brad Bishop96ff1982019-08-19 13:50:42 -0400196 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500197 vendor = "%"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600198
Brad Bishop6dbb3162019-11-25 09:41:34 -0500199 # Find all relevant CVE IDs.
200 for cverow in conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor)):
201 cve = cverow[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202
Brad Bishop96ff1982019-08-19 13:50:42 -0400203 if cve in cve_whitelist:
204 bb.note("%s-%s has been whitelisted for %s" % (product, pv, cve))
Brad Bishop64c979e2019-11-04 13:55:29 -0500205 # TODO: this should be in the report as 'whitelisted'
206 patched_cves.add(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500207 continue
Brad Bishopf3fd2882019-06-21 08:06:37 -0400208 elif cve in patched_cves:
209 bb.note("%s has been patched" % (cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500210 continue
211
212 vulnerable = False
213 for row in conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor)):
214 (_, _, _, version_start, operator_start, version_end, operator_end) = row
215 #bb.debug(2, "Evaluating row " + str(row))
216
Brad Bishop96ff1982019-08-19 13:50:42 -0400217 if (operator_start == '=' and pv == version_start):
Brad Bishop6dbb3162019-11-25 09:41:34 -0500218 vulnerable = True
Brad Bishop96ff1982019-08-19 13:50:42 -0400219 else:
220 if operator_start:
221 try:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500222 vulnerable_start = (operator_start == '>=' and LooseVersion(pv) >= LooseVersion(version_start))
223 vulnerable_start |= (operator_start == '>' and LooseVersion(pv) > LooseVersion(version_start))
Brad Bishop96ff1982019-08-19 13:50:42 -0400224 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500225 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400226 (product, pv, operator_start, version_start, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500227 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400228 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500229 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400230
231 if operator_end:
232 try:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500233 vulnerable_end = (operator_end == '<=' and LooseVersion(pv) <= LooseVersion(version_end))
234 vulnerable_end |= (operator_end == '<' and LooseVersion(pv) < LooseVersion(version_end))
Brad Bishop96ff1982019-08-19 13:50:42 -0400235 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500236 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400237 (product, pv, operator_end, version_end, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500238 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400239 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500240 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400241
242 if operator_start and operator_end:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500243 vulnerable = vulnerable_start and vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400244 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500245 vulnerable = vulnerable_start or vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400246
Brad Bishop6dbb3162019-11-25 09:41:34 -0500247 if vulnerable:
Brad Bishop64c979e2019-11-04 13:55:29 -0500248 bb.note("%s-%s is vulnerable to %s" % (product, pv, cve))
Brad Bishop96ff1982019-08-19 13:50:42 -0400249 cves_unpatched.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500250 break
251
252 if not vulnerable:
253 bb.note("%s-%s is not vulnerable to %s" % (product, pv, cve))
254 # TODO: not patched but not vulnerable
255 patched_cves.add(cve)
256
Brad Bishopf3fd2882019-06-21 08:06:37 -0400257 conn.close()
258
259 return (list(patched_cves), cves_unpatched)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260
261def get_cve_info(d, cves):
262 """
Brad Bishop96ff1982019-08-19 13:50:42 -0400263 Get CVE information from the database.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600264 """
265
Brad Bishop6dbb3162019-11-25 09:41:34 -0500266 import sqlite3
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600267
268 cve_data = {}
Brad Bishop6dbb3162019-11-25 09:41:34 -0500269 conn = sqlite3.connect(d.getVar("CVE_CHECK_DB_FILE"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600270
Brad Bishop6dbb3162019-11-25 09:41:34 -0500271 for cve in cves:
272 for row in conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,)):
273 cve_data[row[0]] = {}
274 cve_data[row[0]]["summary"] = row[1]
275 cve_data[row[0]]["scorev2"] = row[2]
276 cve_data[row[0]]["scorev3"] = row[3]
277 cve_data[row[0]]["modified"] = row[4]
278 cve_data[row[0]]["vector"] = row[5]
279
280 conn.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600281 return cve_data
282
283def cve_write_data(d, patched, unpatched, cve_data):
284 """
285 Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
286 CVE manifest if enabled.
287 """
288
Brad Bishop316dfdd2018-06-25 12:45:53 -0400289 cve_file = d.getVar("CVE_CHECK_LOG")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600290 nvd_link = "https://web.nvd.nist.gov/view/vuln/detail?vulnId="
291 write_string = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500292 unpatched_cves = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400293 bb.utils.mkdirhier(os.path.dirname(cve_file))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600294
295 for cve in sorted(cve_data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500296 write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
297 write_string += "PACKAGE VERSION: %s\n" % d.getVar("PV")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600298 write_string += "CVE: %s\n" % cve
299 if cve in patched:
300 write_string += "CVE STATUS: Patched\n"
301 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500302 unpatched_cves.append(cve)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600303 write_string += "CVE STATUS: Unpatched\n"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600304 write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
Brad Bishopf3fd2882019-06-21 08:06:37 -0400305 write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
306 write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600307 write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
308 write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
309
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500310 if unpatched_cves:
311 bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
312
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600313 with open(cve_file, "w") as f:
314 bb.note("Writing file %s with CVE information" % cve_file)
315 f.write(write_string)
316
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500317 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
318 cve_dir = d.getVar("CVE_CHECK_DIR")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600319 bb.utils.mkdirhier(cve_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500320 deploy_file = os.path.join(cve_dir, d.getVar("PN"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600321 with open(deploy_file, "w") as f:
322 f.write(write_string)
323
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500324 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
325 with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600326 f.write("%s" % write_string)