blob: bd9e7e7445c1e7ec86595a4b0bce4cd85789b544 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
Patrick Williamsc0f7c042017-02-23 20:41:17 -06007# This class is used to check recipes against public CVEs.
8#
9# In order to use this class just inherit the class in the
10# local.conf file and it will add the cve_check task for
11# every recipe. The task can be used per recipe, per image,
12# or using the special cases "world" and "universe". The
13# cve_check task will print a warning for every unpatched
14# CVE found and generate a file in the recipe WORKDIR/cve
15# directory. If an image is build it will generate a report
16# in DEPLOY_DIR_IMAGE for all the packages used.
17#
18# Example:
19# bitbake -c cve_check openssl
20# bitbake core-image-sato
21# bitbake -k -c cve_check universe
22#
23# DISCLAIMER
24#
25# This class/tool is meant to be used as support and not
26# the only method to check against CVEs. Running this tool
27# doesn't guarantee your packages are free of CVEs.
28
Patrick Williams213cb262021-08-07 19:21:33 -050029# The product name that the CVE database uses defaults to BPN, but may need to
Brad Bishop37a0e4d2017-12-04 01:01:44 -050030# be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff).
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031CVE_PRODUCT ??= "${BPN}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040032CVE_VERSION ??= "${PV}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050033
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK"
Patrick Williams8e7b46e2023-05-01 14:19:06 -050035CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_2.db"
Andrew Geisslerc9f78652020-09-18 14:11:35 -050036CVE_CHECK_DB_FILE_LOCK ?= "${CVE_CHECK_DB_FILE}.lock"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037
Brad Bishop316dfdd2018-06-25 12:45:53 -040038CVE_CHECK_LOG ?= "${T}/cve.log"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060039CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check"
Andrew Geisslerb7d28612020-07-24 16:15:54 -050040CVE_CHECK_SUMMARY_DIR ?= "${LOG_DIR}/cve"
41CVE_CHECK_SUMMARY_FILE_NAME ?= "cve-summary"
42CVE_CHECK_SUMMARY_FILE ?= "${CVE_CHECK_SUMMARY_DIR}/${CVE_CHECK_SUMMARY_FILE_NAME}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000043CVE_CHECK_SUMMARY_FILE_NAME_JSON = "cve-summary.json"
44CVE_CHECK_SUMMARY_INDEX_PATH = "${CVE_CHECK_SUMMARY_DIR}/cve-summary-index.txt"
45
46CVE_CHECK_LOG_JSON ?= "${T}/cve.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047
48CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050049CVE_CHECK_RECIPE_FILE ?= "${CVE_CHECK_DIR}/${PN}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000050CVE_CHECK_RECIPE_FILE_JSON ?= "${CVE_CHECK_DIR}/${PN}_cve.json"
Andrew Geisslerc5535c92023-01-27 16:10:19 -060051CVE_CHECK_MANIFEST ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve"
52CVE_CHECK_MANIFEST_JSON ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053CVE_CHECK_COPY_FILES ??= "1"
54CVE_CHECK_CREATE_MANIFEST ??= "1"
55
Andrew Geissler615f2f12022-07-15 14:00:58 -050056# Report Patched or Ignored CVEs
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050057CVE_CHECK_REPORT_PATCHED ??= "1"
Andrew Geissler615f2f12022-07-15 14:00:58 -050058
Andrew Geissler78b72792022-06-14 06:47:25 -050059CVE_CHECK_SHOW_WARNINGS ??= "1"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050060
Andrew Geissler9aee5002022-03-30 16:27:02 +000061# Provide text output
62CVE_CHECK_FORMAT_TEXT ??= "1"
63
64# Provide JSON output
65CVE_CHECK_FORMAT_JSON ??= "1"
66
67# Check for packages without CVEs (no issues or missing product name)
68CVE_CHECK_COVERAGE ??= "1"
69
70# Skip CVE Check for packages (PN)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000071CVE_CHECK_SKIP_RECIPE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072
Andrew Geissler9aee5002022-03-30 16:27:02 +000073# Ingore the check for a given list of CVEs. If a CVE is found,
74# then it is considered patched. The value is a string containing
75# space separated CVE values:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050076#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000077# CVE_CHECK_IGNORE = 'CVE-2014-2524 CVE-2018-1234'
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050078#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000079CVE_CHECK_IGNORE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060080
Andrew Geisslerd1e89492021-02-12 15:35:20 -060081# Layers to be excluded
82CVE_CHECK_LAYER_EXCLUDELIST ??= ""
83
Patrick Williams213cb262021-08-07 19:21:33 -050084# Layers to be included
Andrew Geisslerd1e89492021-02-12 15:35:20 -060085CVE_CHECK_LAYER_INCLUDELIST ??= ""
86
87
Patrick Williams213cb262021-08-07 19:21:33 -050088# set to "alphabetical" for version using single alphabetical character as increment release
Andrew Geisslerd1e89492021-02-12 15:35:20 -060089CVE_VERSION_SUFFIX ??= ""
90
Andrew Geisslerd5838332022-05-27 11:33:10 -050091def generate_json_report(d, out_path, link_path):
92 if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")):
93 import json
Andrew Geissler78b72792022-06-14 06:47:25 -050094 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Andrew Geisslerd5838332022-05-27 11:33:10 -050095
96 bb.note("Generating JSON CVE summary")
97 index_file = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
98 summary = {"version":"1", "package": []}
99 with open(index_file) as f:
100 filename = f.readline()
101 while filename:
102 with open(filename.rstrip()) as j:
103 data = json.load(j)
104 cve_check_merge_jsons(summary, data)
105 filename = f.readline()
106
107 with open(out_path, "w") as f:
108 json.dump(summary, f, indent=2)
109
Andrew Geissler78b72792022-06-14 06:47:25 -0500110 update_symlinks(out_path, link_path)
Andrew Geisslerd5838332022-05-27 11:33:10 -0500111
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500112python cve_save_summary_handler () {
113 import shutil
114 import datetime
Andrew Geissler78b72792022-06-14 06:47:25 -0500115 from oe.cve_check import update_symlinks
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500116
117 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
118
119 cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME")
120 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
121 bb.utils.mkdirhier(cvelogpath)
122
123 timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
124 cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp))
125
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500126 if os.path.exists(cve_tmp_file):
127 shutil.copyfile(cve_tmp_file, cve_summary_file)
Andrew Geissler78b72792022-06-14 06:47:25 -0500128 cvefile_link = os.path.join(cvelogpath, cve_summary_name)
129 update_symlinks(cve_summary_file, cvefile_link)
130 bb.plain("Complete CVE report summary created at: %s" % cvefile_link)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500131
Andrew Geisslerd5838332022-05-27 11:33:10 -0500132 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
133 json_summary_link_name = os.path.join(cvelogpath, d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON"))
134 json_summary_name = os.path.join(cvelogpath, "%s-%s.json" % (cve_summary_name, timestamp))
135 generate_json_report(d, json_summary_name, json_summary_link_name)
136 bb.plain("Complete CVE JSON report summary created at: %s" % json_summary_link_name)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500137}
138
139addhandler cve_save_summary_handler
140cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted"
141
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600142python do_cve_check () {
143 """
144 Check recipe for patched and unpatched CVEs
145 """
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500146 from oe.cve_check import get_patched_cves
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147
Patrick Williams92b42cb2022-09-03 06:53:57 -0500148 with bb.utils.fileslocked([d.getVar("CVE_CHECK_DB_FILE_LOCK")], shared=True):
149 if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
150 try:
151 patched_cves = get_patched_cves(d)
152 except FileNotFoundError:
153 bb.fatal("Failure in searching patches")
154 ignored, patched, unpatched, status = check_cves(d, patched_cves)
155 if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status):
156 cve_data = get_cve_info(d, patched + unpatched + ignored)
157 cve_write_data(d, patched, unpatched, ignored, cve_data, status)
158 else:
159 bb.note("No CVE database found, skipping CVE check")
Brad Bishop96ff1982019-08-19 13:50:42 -0400160
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600161}
162
Patrick Williams03907ee2022-05-01 06:28:52 -0500163addtask cve_check before do_build
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500164do_cve_check[depends] = "cve-update-nvd2-native:do_fetch"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600165do_cve_check[nostamp] = "1"
166
167python cve_check_cleanup () {
168 """
169 Delete the file used to gather all the CVE information.
170 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500171 bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
Andrew Geissler9aee5002022-03-30 16:27:02 +0000172 bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600173}
174
175addhandler cve_check_cleanup
Andrew Geissler615f2f12022-07-15 14:00:58 -0500176cve_check_cleanup[eventmask] = "bb.event.BuildCompleted"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600177
178python cve_check_write_rootfs_manifest () {
179 """
180 Create CVE manifest when building an image
181 """
182
183 import shutil
Andrew Geissler78b72792022-06-14 06:47:25 -0500184 import json
185 from oe.rootfs import image_list_installed_packages
186 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600187
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500188 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500189 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500190 if os.path.exists(deploy_file):
191 bb.utils.remove(deploy_file)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000192 deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
193 if os.path.exists(deploy_file_json):
194 bb.utils.remove(deploy_file_json)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195
Andrew Geissler78b72792022-06-14 06:47:25 -0500196 # Create a list of relevant recipies
197 recipies = set()
198 for pkg in list(image_list_installed_packages(d)):
199 pkg_info = os.path.join(d.getVar('PKGDATA_DIR'),
200 'runtime-reverse', pkg)
201 pkg_data = oe.packagedata.read_pkgdatafile(pkg_info)
202 recipies.add(pkg_data["PN"])
203
204 bb.note("Writing rootfs CVE manifest")
Andrew Geisslerc5535c92023-01-27 16:10:19 -0600205 deploy_dir = d.getVar("IMGDEPLOYDIR")
Andrew Geissler78b72792022-06-14 06:47:25 -0500206 link_name = d.getVar("IMAGE_LINK_NAME")
207
208 json_data = {"version":"1", "package": []}
209 text_data = ""
210 enable_json = d.getVar("CVE_CHECK_FORMAT_JSON") == "1"
211 enable_text = d.getVar("CVE_CHECK_FORMAT_TEXT") == "1"
212
213 save_pn = d.getVar("PN")
214
215 for pkg in recipies:
216 # To be able to use the CVE_CHECK_RECIPE_FILE variable we have to evaluate
217 # it with the different PN names set each time.
218 d.setVar("PN", pkg)
219 if enable_text:
220 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE")
221 if os.path.exists(pkgfilepath):
222 with open(pkgfilepath) as pfile:
223 text_data += pfile.read()
224
225 if enable_json:
226 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
227 if os.path.exists(pkgfilepath):
228 with open(pkgfilepath) as j:
229 data = json.load(j)
230 cve_check_merge_jsons(json_data, data)
231
232 d.setVar("PN", save_pn)
233
234 if enable_text:
235 link_path = os.path.join(deploy_dir, "%s.cve" % link_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500236 manifest_name = d.getVar("CVE_CHECK_MANIFEST")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600237
Andrew Geissler78b72792022-06-14 06:47:25 -0500238 with open(manifest_name, "w") as f:
239 f.write(text_data)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600240
Andrew Geissler78b72792022-06-14 06:47:25 -0500241 update_symlinks(manifest_name, link_path)
242 bb.plain("Image CVE report stored in: %s" % manifest_name)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000243
Andrew Geissler78b72792022-06-14 06:47:25 -0500244 if enable_json:
245 link_path = os.path.join(deploy_dir, "%s.json" % link_name)
246 manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON")
247
248 with open(manifest_name, "w") as f:
249 json.dump(json_data, f, indent=2)
250
251 update_symlinks(manifest_name, link_path)
252 bb.plain("Image CVE JSON report stored in: %s" % manifest_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253}
254
Patrick Williams213cb262021-08-07 19:21:33 -0500255ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500256do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Andrew Geisslerd5838332022-05-27 11:33:10 -0500257do_populate_sdk[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600258
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600259def check_cves(d, patched_cves):
260 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400261 Connect to the NVD database and find unpatched cves.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600262 """
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500263 from oe.cve_check import Version, convert_cve_version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600264
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600265 pn = d.getVar("PN")
266 real_pv = d.getVar("PV")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600267 suffix = d.getVar("CVE_VERSION_SUFFIX")
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600268
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600269 cves_unpatched = []
Andrew Geissler615f2f12022-07-15 14:00:58 -0500270 cves_ignored = []
Andrew Geissler9aee5002022-03-30 16:27:02 +0000271 cves_status = []
272 cves_in_recipe = False
Brad Bishopf3fd2882019-06-21 08:06:37 -0400273 # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
Brad Bishop96ff1982019-08-19 13:50:42 -0400274 products = d.getVar("CVE_PRODUCT").split()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400275 # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
Brad Bishop96ff1982019-08-19 13:50:42 -0400276 if not products:
Andrew Geissler78b72792022-06-14 06:47:25 -0500277 return ([], [], [], [])
Brad Bishop316dfdd2018-06-25 12:45:53 -0400278 pv = d.getVar("CVE_VERSION").split("+git")[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600279
Andrew Geissler9aee5002022-03-30 16:27:02 +0000280 # If the recipe has been skipped/ignored we return empty lists
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000281 if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split():
Andrew Geissler9aee5002022-03-30 16:27:02 +0000282 bb.note("Recipe has been skipped by cve-check")
283 return ([], [], [], [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600284
Andrew Geissler9aee5002022-03-30 16:27:02 +0000285 cve_ignore = d.getVar("CVE_CHECK_IGNORE").split()
Brad Bishop96ff1982019-08-19 13:50:42 -0400286
Brad Bishopf3fd2882019-06-21 08:06:37 -0400287 import sqlite3
Brad Bishop6dbb3162019-11-25 09:41:34 -0500288 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
289 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600290
Brad Bishop6dbb3162019-11-25 09:41:34 -0500291 # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
Brad Bishop96ff1982019-08-19 13:50:42 -0400292 for product in products:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000293 cves_in_product = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400294 if ":" in product:
295 vendor, product = product.split(":", 1)
Brad Bishop96ff1982019-08-19 13:50:42 -0400296 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500297 vendor = "%"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600298
Brad Bishop6dbb3162019-11-25 09:41:34 -0500299 # Find all relevant CVE IDs.
Patrick Williams92b42cb2022-09-03 06:53:57 -0500300 cve_cursor = conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor))
301 for cverow in cve_cursor:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500302 cve = cverow[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600303
Andrew Geissler9aee5002022-03-30 16:27:02 +0000304 if cve in cve_ignore:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500305 bb.note("%s-%s ignores %s" % (product, pv, cve))
306 cves_ignored.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500307 continue
Brad Bishopf3fd2882019-06-21 08:06:37 -0400308 elif cve in patched_cves:
309 bb.note("%s has been patched" % (cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500310 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000311 # Write status once only for each product
312 if not cves_in_product:
313 cves_status.append([product, True])
314 cves_in_product = True
315 cves_in_recipe = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500316
317 vulnerable = False
Andrew Geissler615f2f12022-07-15 14:00:58 -0500318 ignored = False
319
Patrick Williams92b42cb2022-09-03 06:53:57 -0500320 product_cursor = conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor))
321 for row in product_cursor:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500322 (_, _, _, version_start, operator_start, version_end, operator_end) = row
323 #bb.debug(2, "Evaluating row " + str(row))
Andrew Geissler615f2f12022-07-15 14:00:58 -0500324 if cve in cve_ignore:
325 ignored = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500326
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500327 version_start = convert_cve_version(version_start)
328 version_end = convert_cve_version(version_end)
329
Andrew Geissler82c905d2020-04-13 13:39:40 -0500330 if (operator_start == '=' and pv == version_start) or version_start == '-':
Brad Bishop6dbb3162019-11-25 09:41:34 -0500331 vulnerable = True
Brad Bishop96ff1982019-08-19 13:50:42 -0400332 else:
333 if operator_start:
334 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600335 vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix))
336 vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix))
Brad Bishop96ff1982019-08-19 13:50:42 -0400337 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500338 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400339 (product, pv, operator_start, version_start, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500340 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400341 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500342 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400343
344 if operator_end:
345 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600346 vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) )
347 vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) )
Brad Bishop96ff1982019-08-19 13:50:42 -0400348 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500349 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400350 (product, pv, operator_end, version_end, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500351 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400352 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500353 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400354
355 if operator_start and operator_end:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500356 vulnerable = vulnerable_start and vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400357 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500358 vulnerable = vulnerable_start or vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400359
Brad Bishop6dbb3162019-11-25 09:41:34 -0500360 if vulnerable:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500361 if ignored:
362 bb.note("%s is ignored in %s-%s" % (cve, pn, real_pv))
363 cves_ignored.append(cve)
364 else:
365 bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve))
366 cves_unpatched.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500367 break
Patrick Williams92b42cb2022-09-03 06:53:57 -0500368 product_cursor.close()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500369
370 if not vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600371 bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500372 patched_cves.add(cve)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500373 cve_cursor.close()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500374
Andrew Geissler9aee5002022-03-30 16:27:02 +0000375 if not cves_in_product:
376 bb.note("No CVE records found for product %s, pn %s" % (product, pn))
377 cves_status.append([product, False])
378
Brad Bishopf3fd2882019-06-21 08:06:37 -0400379 conn.close()
380
Andrew Geissler9aee5002022-03-30 16:27:02 +0000381 if not cves_in_recipe:
382 bb.note("No CVE records for products in recipe %s" % (pn))
383
Andrew Geissler615f2f12022-07-15 14:00:58 -0500384 return (list(cves_ignored), list(patched_cves), cves_unpatched, cves_status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600385
386def get_cve_info(d, cves):
387 """
Brad Bishop96ff1982019-08-19 13:50:42 -0400388 Get CVE information from the database.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600389 """
390
Brad Bishop6dbb3162019-11-25 09:41:34 -0500391 import sqlite3
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600392
393 cve_data = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000394 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
395 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600396
Brad Bishop6dbb3162019-11-25 09:41:34 -0500397 for cve in cves:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500398 cursor = conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,))
399 for row in cursor:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500400 cve_data[row[0]] = {}
401 cve_data[row[0]]["summary"] = row[1]
402 cve_data[row[0]]["scorev2"] = row[2]
403 cve_data[row[0]]["scorev3"] = row[3]
404 cve_data[row[0]]["modified"] = row[4]
405 cve_data[row[0]]["vector"] = row[5]
Patrick Williams92b42cb2022-09-03 06:53:57 -0500406 cursor.close()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500407 conn.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600408 return cve_data
409
Andrew Geissler9aee5002022-03-30 16:27:02 +0000410def cve_write_data_text(d, patched, unpatched, ignored, cve_data):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600411 """
412 Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
413 CVE manifest if enabled.
414 """
415
Brad Bishop316dfdd2018-06-25 12:45:53 -0400416 cve_file = d.getVar("CVE_CHECK_LOG")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600417 fdir_name = d.getVar("FILE_DIRNAME")
418 layer = fdir_name.split("/")[-3]
419
420 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
421 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
422
Andrew Geissler615f2f12022-07-15 14:00:58 -0500423 report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
424
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600425 if exclude_layers and layer in exclude_layers:
426 return
427
428 if include_layers and layer not in include_layers:
429 return
430
Andrew Geissler9aee5002022-03-30 16:27:02 +0000431 # Early exit, the text format does not report packages without CVEs
Andrew Geissler615f2f12022-07-15 14:00:58 -0500432 if not patched+unpatched+ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000433 return
434
Patrick Williams213cb262021-08-07 19:21:33 -0500435 nvd_link = "https://nvd.nist.gov/vuln/detail/"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600436 write_string = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500437 unpatched_cves = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400438 bb.utils.mkdirhier(os.path.dirname(cve_file))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600439
440 for cve in sorted(cve_data):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500441 is_patched = cve in patched
Andrew Geissler615f2f12022-07-15 14:00:58 -0500442 is_ignored = cve in ignored
443
444 if (is_patched or is_ignored) and not report_all:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500445 continue
Andrew Geissler615f2f12022-07-15 14:00:58 -0500446
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600447 write_string += "LAYER: %s\n" % layer
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500448 write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500449 write_string += "PACKAGE VERSION: %s%s\n" % (d.getVar("EXTENDPE"), d.getVar("PV"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600450 write_string += "CVE: %s\n" % cve
Andrew Geissler615f2f12022-07-15 14:00:58 -0500451 if is_ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000452 write_string += "CVE STATUS: Ignored\n"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500453 elif is_patched:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600454 write_string += "CVE STATUS: Patched\n"
455 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500456 unpatched_cves.append(cve)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600457 write_string += "CVE STATUS: Unpatched\n"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600458 write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
Brad Bishopf3fd2882019-06-21 08:06:37 -0400459 write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
460 write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600461 write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
462 write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
463
Andrew Geissler78b72792022-06-14 06:47:25 -0500464 if unpatched_cves and d.getVar("CVE_CHECK_SHOW_WARNINGS") == "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500465 bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
466
Andrew Geissler78b72792022-06-14 06:47:25 -0500467 with open(cve_file, "w") as f:
468 bb.note("Writing file %s with CVE information" % cve_file)
469 f.write(write_string)
470
471 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
472 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
473 bb.utils.mkdirhier(os.path.dirname(deploy_file))
474 with open(deploy_file, "w") as f:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600475 f.write(write_string)
476
Andrew Geissler78b72792022-06-14 06:47:25 -0500477 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
478 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
479 bb.utils.mkdirhier(cvelogpath)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500480
Andrew Geissler78b72792022-06-14 06:47:25 -0500481 with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
482 f.write("%s" % write_string)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000483
484def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file):
485 """
486 Write CVE information in the JSON format: to WORKDIR; and to
487 CVE_CHECK_DIR, if CVE manifest if enabled, write fragment
488 files that will be assembled at the end in cve_check_write_rootfs_manifest.
489 """
490
491 import json
492
493 write_string = json.dumps(output, indent=2)
494 with open(direct_file, "w") as f:
495 bb.note("Writing file %s with CVE information" % direct_file)
496 f.write(write_string)
497
498 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
499 bb.utils.mkdirhier(os.path.dirname(deploy_file))
500 with open(deploy_file, "w") as f:
501 f.write(write_string)
502
503 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
504 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
505 index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
506 bb.utils.mkdirhier(cvelogpath)
507 fragment_file = os.path.basename(deploy_file)
508 fragment_path = os.path.join(cvelogpath, fragment_file)
509 with open(fragment_path, "w") as f:
510 f.write(write_string)
511 with open(index_path, "a+") as f:
512 f.write("%s\n" % fragment_path)
513
514def cve_write_data_json(d, patched, unpatched, ignored, cve_data, cve_status):
515 """
516 Prepare CVE data for the JSON format, then write it.
517 """
518
519 output = {"version":"1", "package": []}
520 nvd_link = "https://nvd.nist.gov/vuln/detail/"
521
522 fdir_name = d.getVar("FILE_DIRNAME")
523 layer = fdir_name.split("/")[-3]
524
525 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
526 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
527
Andrew Geissler615f2f12022-07-15 14:00:58 -0500528 report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
529
Andrew Geissler9aee5002022-03-30 16:27:02 +0000530 if exclude_layers and layer in exclude_layers:
531 return
532
533 if include_layers and layer not in include_layers:
534 return
535
536 unpatched_cves = []
537
538 product_data = []
539 for s in cve_status:
540 p = {"product": s[0], "cvesInRecord": "Yes"}
541 if s[1] == False:
542 p["cvesInRecord"] = "No"
543 product_data.append(p)
544
545 package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV"))
546 package_data = {
547 "name" : d.getVar("PN"),
548 "layer" : layer,
549 "version" : package_version,
550 "products": product_data
551 }
552 cve_list = []
553
554 for cve in sorted(cve_data):
555 is_patched = cve in patched
Andrew Geissler615f2f12022-07-15 14:00:58 -0500556 is_ignored = cve in ignored
Andrew Geissler9aee5002022-03-30 16:27:02 +0000557 status = "Unpatched"
Andrew Geissler615f2f12022-07-15 14:00:58 -0500558 if (is_patched or is_ignored) and not report_all:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000559 continue
Andrew Geissler615f2f12022-07-15 14:00:58 -0500560 if is_ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000561 status = "Ignored"
562 elif is_patched:
563 status = "Patched"
564 else:
565 # default value of status is Unpatched
566 unpatched_cves.append(cve)
567
568 issue_link = "%s%s" % (nvd_link, cve)
569
570 cve_item = {
571 "id" : cve,
572 "summary" : cve_data[cve]["summary"],
573 "scorev2" : cve_data[cve]["scorev2"],
574 "scorev3" : cve_data[cve]["scorev3"],
575 "vector" : cve_data[cve]["vector"],
576 "status" : status,
577 "link": issue_link
578 }
579 cve_list.append(cve_item)
580
581 package_data["issue"] = cve_list
582 output["package"].append(package_data)
583
584 direct_file = d.getVar("CVE_CHECK_LOG_JSON")
585 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
586 manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")
587
588 cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file)
589
590def cve_write_data(d, patched, unpatched, ignored, cve_data, status):
591 """
592 Write CVE data in each enabled format.
593 """
594
595 if d.getVar("CVE_CHECK_FORMAT_TEXT") == "1":
596 cve_write_data_text(d, patched, unpatched, ignored, cve_data)
597 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
598 cve_write_data_json(d, patched, unpatched, ignored, cve_data, status)