blob: 1b4910f737468384f701ee40eecc4ba9e634b37b [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
Patrick Williams213cb262021-08-07 19:21:33 -050023# The product name that the CVE database uses defaults to BPN, but may need to
Brad Bishop37a0e4d2017-12-04 01:01:44 -050024# 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"
Andrew Geissler82c905d2020-04-13 13:39:40 -050029CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_1.1.db"
Andrew Geisslerc9f78652020-09-18 14:11:35 -050030CVE_CHECK_DB_FILE_LOCK ?= "${CVE_CHECK_DB_FILE}.lock"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031
Brad Bishop316dfdd2018-06-25 12:45:53 -040032CVE_CHECK_LOG ?= "${T}/cve.log"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060033CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check"
Andrew Geisslerb7d28612020-07-24 16:15:54 -050034CVE_CHECK_SUMMARY_DIR ?= "${LOG_DIR}/cve"
35CVE_CHECK_SUMMARY_FILE_NAME ?= "cve-summary"
36CVE_CHECK_SUMMARY_FILE ?= "${CVE_CHECK_SUMMARY_DIR}/${CVE_CHECK_SUMMARY_FILE_NAME}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000037CVE_CHECK_SUMMARY_FILE_NAME_JSON = "cve-summary.json"
38CVE_CHECK_SUMMARY_INDEX_PATH = "${CVE_CHECK_SUMMARY_DIR}/cve-summary-index.txt"
39
40CVE_CHECK_LOG_JSON ?= "${T}/cve.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060041
42CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050043CVE_CHECK_RECIPE_FILE ?= "${CVE_CHECK_DIR}/${PN}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000044CVE_CHECK_RECIPE_FILE_JSON ?= "${CVE_CHECK_DIR}/${PN}_cve.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060045CVE_CHECK_MANIFEST ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve"
Andrew Geissler9aee5002022-03-30 16:27:02 +000046CVE_CHECK_MANIFEST_JSON ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047CVE_CHECK_COPY_FILES ??= "1"
48CVE_CHECK_CREATE_MANIFEST ??= "1"
49
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050050CVE_CHECK_REPORT_PATCHED ??= "1"
Andrew Geissler78b72792022-06-14 06:47:25 -050051CVE_CHECK_SHOW_WARNINGS ??= "1"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050052
Andrew Geissler9aee5002022-03-30 16:27:02 +000053# Provide text output
54CVE_CHECK_FORMAT_TEXT ??= "1"
55
56# Provide JSON output
57CVE_CHECK_FORMAT_JSON ??= "1"
58
59# Check for packages without CVEs (no issues or missing product name)
60CVE_CHECK_COVERAGE ??= "1"
61
62# Skip CVE Check for packages (PN)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000063CVE_CHECK_SKIP_RECIPE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060064
Andrew Geissler9aee5002022-03-30 16:27:02 +000065# Ingore the check for a given list of CVEs. If a CVE is found,
66# then it is considered patched. The value is a string containing
67# space separated CVE values:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050068#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000069# CVE_CHECK_IGNORE = 'CVE-2014-2524 CVE-2018-1234'
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050070#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000071CVE_CHECK_IGNORE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072
Andrew Geisslerd1e89492021-02-12 15:35:20 -060073# Layers to be excluded
74CVE_CHECK_LAYER_EXCLUDELIST ??= ""
75
Patrick Williams213cb262021-08-07 19:21:33 -050076# Layers to be included
Andrew Geisslerd1e89492021-02-12 15:35:20 -060077CVE_CHECK_LAYER_INCLUDELIST ??= ""
78
79
Patrick Williams213cb262021-08-07 19:21:33 -050080# set to "alphabetical" for version using single alphabetical character as increment release
Andrew Geisslerd1e89492021-02-12 15:35:20 -060081CVE_VERSION_SUFFIX ??= ""
82
Andrew Geisslerd5838332022-05-27 11:33:10 -050083def generate_json_report(d, out_path, link_path):
84 if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")):
85 import json
Andrew Geissler78b72792022-06-14 06:47:25 -050086 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Andrew Geisslerd5838332022-05-27 11:33:10 -050087
88 bb.note("Generating JSON CVE summary")
89 index_file = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
90 summary = {"version":"1", "package": []}
91 with open(index_file) as f:
92 filename = f.readline()
93 while filename:
94 with open(filename.rstrip()) as j:
95 data = json.load(j)
96 cve_check_merge_jsons(summary, data)
97 filename = f.readline()
98
99 with open(out_path, "w") as f:
100 json.dump(summary, f, indent=2)
101
Andrew Geissler78b72792022-06-14 06:47:25 -0500102 update_symlinks(out_path, link_path)
Andrew Geisslerd5838332022-05-27 11:33:10 -0500103
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500104python cve_save_summary_handler () {
105 import shutil
106 import datetime
Andrew Geissler78b72792022-06-14 06:47:25 -0500107 from oe.cve_check import update_symlinks
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500108
109 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
110
111 cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME")
112 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
113 bb.utils.mkdirhier(cvelogpath)
114
115 timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
116 cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp))
117
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500118 if os.path.exists(cve_tmp_file):
119 shutil.copyfile(cve_tmp_file, cve_summary_file)
Andrew Geissler78b72792022-06-14 06:47:25 -0500120 cvefile_link = os.path.join(cvelogpath, cve_summary_name)
121 update_symlinks(cve_summary_file, cvefile_link)
122 bb.plain("Complete CVE report summary created at: %s" % cvefile_link)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500123
Andrew Geisslerd5838332022-05-27 11:33:10 -0500124 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
125 json_summary_link_name = os.path.join(cvelogpath, d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON"))
126 json_summary_name = os.path.join(cvelogpath, "%s-%s.json" % (cve_summary_name, timestamp))
127 generate_json_report(d, json_summary_name, json_summary_link_name)
128 bb.plain("Complete CVE JSON report summary created at: %s" % json_summary_link_name)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500129}
130
131addhandler cve_save_summary_handler
132cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted"
133
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600134python do_cve_check () {
135 """
136 Check recipe for patched and unpatched CVEs
137 """
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500138 from oe.cve_check import get_patched_cves
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139
Brad Bishop96ff1982019-08-19 13:50:42 -0400140 if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500141 try:
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500142 patched_cves = get_patched_cves(d)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500143 except FileNotFoundError:
144 bb.fatal("Failure in searching patches")
Andrew Geissler9aee5002022-03-30 16:27:02 +0000145 ignored, patched, unpatched, status = check_cves(d, patched_cves)
146 if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147 cve_data = get_cve_info(d, patched + unpatched)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000148 cve_write_data(d, patched, unpatched, ignored, cve_data, status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600149 else:
Brad Bishop96ff1982019-08-19 13:50:42 -0400150 bb.note("No CVE database found, skipping CVE check")
151
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600152}
153
Patrick Williams03907ee2022-05-01 06:28:52 -0500154addtask cve_check before do_build
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500155do_cve_check[depends] = "cve-update-db-native:do_fetch"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156do_cve_check[nostamp] = "1"
157
158python cve_check_cleanup () {
159 """
160 Delete the file used to gather all the CVE information.
161 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
Andrew Geissler9aee5002022-03-30 16:27:02 +0000163 bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600164}
165
166addhandler cve_check_cleanup
167cve_check_cleanup[eventmask] = "bb.cooker.CookerExit"
168
169python cve_check_write_rootfs_manifest () {
170 """
171 Create CVE manifest when building an image
172 """
173
174 import shutil
Andrew Geissler78b72792022-06-14 06:47:25 -0500175 import json
176 from oe.rootfs import image_list_installed_packages
177 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600178
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500180 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500181 if os.path.exists(deploy_file):
182 bb.utils.remove(deploy_file)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000183 deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
184 if os.path.exists(deploy_file_json):
185 bb.utils.remove(deploy_file_json)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500186
Andrew Geissler78b72792022-06-14 06:47:25 -0500187 # Create a list of relevant recipies
188 recipies = set()
189 for pkg in list(image_list_installed_packages(d)):
190 pkg_info = os.path.join(d.getVar('PKGDATA_DIR'),
191 'runtime-reverse', pkg)
192 pkg_data = oe.packagedata.read_pkgdatafile(pkg_info)
193 recipies.add(pkg_data["PN"])
194
195 bb.note("Writing rootfs CVE manifest")
196 deploy_dir = d.getVar("DEPLOY_DIR_IMAGE")
197 link_name = d.getVar("IMAGE_LINK_NAME")
198
199 json_data = {"version":"1", "package": []}
200 text_data = ""
201 enable_json = d.getVar("CVE_CHECK_FORMAT_JSON") == "1"
202 enable_text = d.getVar("CVE_CHECK_FORMAT_TEXT") == "1"
203
204 save_pn = d.getVar("PN")
205
206 for pkg in recipies:
207 # To be able to use the CVE_CHECK_RECIPE_FILE variable we have to evaluate
208 # it with the different PN names set each time.
209 d.setVar("PN", pkg)
210 if enable_text:
211 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE")
212 if os.path.exists(pkgfilepath):
213 with open(pkgfilepath) as pfile:
214 text_data += pfile.read()
215
216 if enable_json:
217 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
218 if os.path.exists(pkgfilepath):
219 with open(pkgfilepath) as j:
220 data = json.load(j)
221 cve_check_merge_jsons(json_data, data)
222
223 d.setVar("PN", save_pn)
224
225 if enable_text:
226 link_path = os.path.join(deploy_dir, "%s.cve" % link_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500227 manifest_name = d.getVar("CVE_CHECK_MANIFEST")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600228
Andrew Geissler78b72792022-06-14 06:47:25 -0500229 with open(manifest_name, "w") as f:
230 f.write(text_data)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600231
Andrew Geissler78b72792022-06-14 06:47:25 -0500232 update_symlinks(manifest_name, link_path)
233 bb.plain("Image CVE report stored in: %s" % manifest_name)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000234
Andrew Geissler78b72792022-06-14 06:47:25 -0500235 if enable_json:
236 link_path = os.path.join(deploy_dir, "%s.json" % link_name)
237 manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON")
238
239 with open(manifest_name, "w") as f:
240 json.dump(json_data, f, indent=2)
241
242 update_symlinks(manifest_name, link_path)
243 bb.plain("Image CVE JSON report stored in: %s" % manifest_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600244}
245
Patrick Williams213cb262021-08-07 19:21:33 -0500246ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500247do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Andrew Geisslerd5838332022-05-27 11:33:10 -0500248do_populate_sdk[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600249
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600250def check_cves(d, patched_cves):
251 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400252 Connect to the NVD database and find unpatched cves.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600254 from oe.cve_check import Version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600255
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600256 pn = d.getVar("PN")
257 real_pv = d.getVar("PV")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600258 suffix = d.getVar("CVE_VERSION_SUFFIX")
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600259
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260 cves_unpatched = []
Andrew Geissler9aee5002022-03-30 16:27:02 +0000261 cves_status = []
262 cves_in_recipe = False
Brad Bishopf3fd2882019-06-21 08:06:37 -0400263 # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
Brad Bishop96ff1982019-08-19 13:50:42 -0400264 products = d.getVar("CVE_PRODUCT").split()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400265 # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
Brad Bishop96ff1982019-08-19 13:50:42 -0400266 if not products:
Andrew Geissler78b72792022-06-14 06:47:25 -0500267 return ([], [], [], [])
Brad Bishop316dfdd2018-06-25 12:45:53 -0400268 pv = d.getVar("CVE_VERSION").split("+git")[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600269
Andrew Geissler9aee5002022-03-30 16:27:02 +0000270 # If the recipe has been skipped/ignored we return empty lists
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000271 if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split():
Andrew Geissler9aee5002022-03-30 16:27:02 +0000272 bb.note("Recipe has been skipped by cve-check")
273 return ([], [], [], [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600274
Andrew Geissler9aee5002022-03-30 16:27:02 +0000275 cve_ignore = d.getVar("CVE_CHECK_IGNORE").split()
Brad Bishop96ff1982019-08-19 13:50:42 -0400276
Brad Bishopf3fd2882019-06-21 08:06:37 -0400277 import sqlite3
Brad Bishop6dbb3162019-11-25 09:41:34 -0500278 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
279 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600280
Brad Bishop6dbb3162019-11-25 09:41:34 -0500281 # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
Brad Bishop96ff1982019-08-19 13:50:42 -0400282 for product in products:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000283 cves_in_product = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400284 if ":" in product:
285 vendor, product = product.split(":", 1)
Brad Bishop96ff1982019-08-19 13:50:42 -0400286 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500287 vendor = "%"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600288
Brad Bishop6dbb3162019-11-25 09:41:34 -0500289 # Find all relevant CVE IDs.
290 for cverow in conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor)):
291 cve = cverow[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600292
Andrew Geissler9aee5002022-03-30 16:27:02 +0000293 if cve in cve_ignore:
294 bb.note("%s-%s has been ignored for %s" % (product, pv, cve))
295 # TODO: this should be in the report as 'ignored'
Brad Bishop64c979e2019-11-04 13:55:29 -0500296 patched_cves.add(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500297 continue
Brad Bishopf3fd2882019-06-21 08:06:37 -0400298 elif cve in patched_cves:
299 bb.note("%s has been patched" % (cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500300 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000301 # Write status once only for each product
302 if not cves_in_product:
303 cves_status.append([product, True])
304 cves_in_product = True
305 cves_in_recipe = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500306
307 vulnerable = False
308 for row in conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor)):
309 (_, _, _, version_start, operator_start, version_end, operator_end) = row
310 #bb.debug(2, "Evaluating row " + str(row))
311
Andrew Geissler82c905d2020-04-13 13:39:40 -0500312 if (operator_start == '=' and pv == version_start) or version_start == '-':
Brad Bishop6dbb3162019-11-25 09:41:34 -0500313 vulnerable = True
Brad Bishop96ff1982019-08-19 13:50:42 -0400314 else:
315 if operator_start:
316 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600317 vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix))
318 vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix))
Brad Bishop96ff1982019-08-19 13:50:42 -0400319 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500320 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400321 (product, pv, operator_start, version_start, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500322 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400323 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500324 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400325
326 if operator_end:
327 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600328 vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) )
329 vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) )
Brad Bishop96ff1982019-08-19 13:50:42 -0400330 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500331 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400332 (product, pv, operator_end, version_end, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500333 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400334 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500335 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400336
337 if operator_start and operator_end:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500338 vulnerable = vulnerable_start and vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400339 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500340 vulnerable = vulnerable_start or vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400341
Brad Bishop6dbb3162019-11-25 09:41:34 -0500342 if vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600343 bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop96ff1982019-08-19 13:50:42 -0400344 cves_unpatched.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500345 break
346
347 if not vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600348 bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500349 # TODO: not patched but not vulnerable
350 patched_cves.add(cve)
351
Andrew Geissler9aee5002022-03-30 16:27:02 +0000352 if not cves_in_product:
353 bb.note("No CVE records found for product %s, pn %s" % (product, pn))
354 cves_status.append([product, False])
355
Brad Bishopf3fd2882019-06-21 08:06:37 -0400356 conn.close()
357
Andrew Geissler9aee5002022-03-30 16:27:02 +0000358 if not cves_in_recipe:
359 bb.note("No CVE records for products in recipe %s" % (pn))
360
361 return (list(cve_ignore), list(patched_cves), cves_unpatched, cves_status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600362
363def get_cve_info(d, cves):
364 """
Brad Bishop96ff1982019-08-19 13:50:42 -0400365 Get CVE information from the database.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600366 """
367
Brad Bishop6dbb3162019-11-25 09:41:34 -0500368 import sqlite3
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600369
370 cve_data = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000371 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
372 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600373
Brad Bishop6dbb3162019-11-25 09:41:34 -0500374 for cve in cves:
375 for row in conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,)):
376 cve_data[row[0]] = {}
377 cve_data[row[0]]["summary"] = row[1]
378 cve_data[row[0]]["scorev2"] = row[2]
379 cve_data[row[0]]["scorev3"] = row[3]
380 cve_data[row[0]]["modified"] = row[4]
381 cve_data[row[0]]["vector"] = row[5]
382
383 conn.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600384 return cve_data
385
Andrew Geissler9aee5002022-03-30 16:27:02 +0000386def cve_write_data_text(d, patched, unpatched, ignored, cve_data):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600387 """
388 Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
389 CVE manifest if enabled.
390 """
391
Brad Bishop316dfdd2018-06-25 12:45:53 -0400392 cve_file = d.getVar("CVE_CHECK_LOG")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600393 fdir_name = d.getVar("FILE_DIRNAME")
394 layer = fdir_name.split("/")[-3]
395
396 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
397 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
398
399 if exclude_layers and layer in exclude_layers:
400 return
401
402 if include_layers and layer not in include_layers:
403 return
404
Andrew Geissler9aee5002022-03-30 16:27:02 +0000405 # Early exit, the text format does not report packages without CVEs
406 if not patched+unpatched:
407 return
408
Patrick Williams213cb262021-08-07 19:21:33 -0500409 nvd_link = "https://nvd.nist.gov/vuln/detail/"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600410 write_string = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500411 unpatched_cves = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400412 bb.utils.mkdirhier(os.path.dirname(cve_file))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600413
414 for cve in sorted(cve_data):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500415 is_patched = cve in patched
416 if is_patched and (d.getVar("CVE_CHECK_REPORT_PATCHED") != "1"):
417 continue
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600418 write_string += "LAYER: %s\n" % layer
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500419 write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500420 write_string += "PACKAGE VERSION: %s%s\n" % (d.getVar("EXTENDPE"), d.getVar("PV"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600421 write_string += "CVE: %s\n" % cve
Andrew Geissler9aee5002022-03-30 16:27:02 +0000422 if cve in ignored:
423 write_string += "CVE STATUS: Ignored\n"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500424 elif is_patched:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600425 write_string += "CVE STATUS: Patched\n"
426 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500427 unpatched_cves.append(cve)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600428 write_string += "CVE STATUS: Unpatched\n"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600429 write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
Brad Bishopf3fd2882019-06-21 08:06:37 -0400430 write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
431 write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600432 write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
433 write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
434
Andrew Geissler78b72792022-06-14 06:47:25 -0500435 if unpatched_cves and d.getVar("CVE_CHECK_SHOW_WARNINGS") == "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500436 bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
437
Andrew Geissler78b72792022-06-14 06:47:25 -0500438 with open(cve_file, "w") as f:
439 bb.note("Writing file %s with CVE information" % cve_file)
440 f.write(write_string)
441
442 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
443 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
444 bb.utils.mkdirhier(os.path.dirname(deploy_file))
445 with open(deploy_file, "w") as f:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600446 f.write(write_string)
447
Andrew Geissler78b72792022-06-14 06:47:25 -0500448 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
449 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
450 bb.utils.mkdirhier(cvelogpath)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500451
Andrew Geissler78b72792022-06-14 06:47:25 -0500452 with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
453 f.write("%s" % write_string)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000454
455def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file):
456 """
457 Write CVE information in the JSON format: to WORKDIR; and to
458 CVE_CHECK_DIR, if CVE manifest if enabled, write fragment
459 files that will be assembled at the end in cve_check_write_rootfs_manifest.
460 """
461
462 import json
463
464 write_string = json.dumps(output, indent=2)
465 with open(direct_file, "w") as f:
466 bb.note("Writing file %s with CVE information" % direct_file)
467 f.write(write_string)
468
469 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
470 bb.utils.mkdirhier(os.path.dirname(deploy_file))
471 with open(deploy_file, "w") as f:
472 f.write(write_string)
473
474 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
475 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
476 index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
477 bb.utils.mkdirhier(cvelogpath)
478 fragment_file = os.path.basename(deploy_file)
479 fragment_path = os.path.join(cvelogpath, fragment_file)
480 with open(fragment_path, "w") as f:
481 f.write(write_string)
482 with open(index_path, "a+") as f:
483 f.write("%s\n" % fragment_path)
484
485def cve_write_data_json(d, patched, unpatched, ignored, cve_data, cve_status):
486 """
487 Prepare CVE data for the JSON format, then write it.
488 """
489
490 output = {"version":"1", "package": []}
491 nvd_link = "https://nvd.nist.gov/vuln/detail/"
492
493 fdir_name = d.getVar("FILE_DIRNAME")
494 layer = fdir_name.split("/")[-3]
495
496 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
497 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
498
499 if exclude_layers and layer in exclude_layers:
500 return
501
502 if include_layers and layer not in include_layers:
503 return
504
505 unpatched_cves = []
506
507 product_data = []
508 for s in cve_status:
509 p = {"product": s[0], "cvesInRecord": "Yes"}
510 if s[1] == False:
511 p["cvesInRecord"] = "No"
512 product_data.append(p)
513
514 package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV"))
515 package_data = {
516 "name" : d.getVar("PN"),
517 "layer" : layer,
518 "version" : package_version,
519 "products": product_data
520 }
521 cve_list = []
522
523 for cve in sorted(cve_data):
524 is_patched = cve in patched
525 status = "Unpatched"
526 if is_patched and (d.getVar("CVE_CHECK_REPORT_PATCHED") != "1"):
527 continue
528 if cve in ignored:
529 status = "Ignored"
530 elif is_patched:
531 status = "Patched"
532 else:
533 # default value of status is Unpatched
534 unpatched_cves.append(cve)
535
536 issue_link = "%s%s" % (nvd_link, cve)
537
538 cve_item = {
539 "id" : cve,
540 "summary" : cve_data[cve]["summary"],
541 "scorev2" : cve_data[cve]["scorev2"],
542 "scorev3" : cve_data[cve]["scorev3"],
543 "vector" : cve_data[cve]["vector"],
544 "status" : status,
545 "link": issue_link
546 }
547 cve_list.append(cve_item)
548
549 package_data["issue"] = cve_list
550 output["package"].append(package_data)
551
552 direct_file = d.getVar("CVE_CHECK_LOG_JSON")
553 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
554 manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")
555
556 cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file)
557
558def cve_write_data(d, patched, unpatched, ignored, cve_data, status):
559 """
560 Write CVE data in each enabled format.
561 """
562
563 if d.getVar("CVE_CHECK_FORMAT_TEXT") == "1":
564 cve_write_data_text(d, patched, unpatched, ignored, cve_data)
565 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
566 cve_write_data_json(d, patched, unpatched, ignored, cve_data, status)