blob: b55f4299da3a6c3eaa5e42ab2db61f7cc9352d27 [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"
Andrew Geissler220dafd2023-10-04 10:18:08 -050035CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_2-1.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 Geissler8f840682023-07-21 09:09:43 -050051CVE_CHECK_MANIFEST ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}.cve"
52CVE_CHECK_MANIFEST_JSON ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}.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 Geissler8f840682023-07-21 09:09:43 -050073# Replace NVD DB check status for a given CVE. Each of CVE has to be mentioned
74# separately with optional detail and description for this status.
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050075#
Andrew Geissler8f840682023-07-21 09:09:43 -050076# CVE_STATUS[CVE-1234-0001] = "not-applicable-platform: Issue only applies on Windows"
77# CVE_STATUS[CVE-1234-0002] = "fixed-version: Fixed externally"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050078#
Andrew Geissler8f840682023-07-21 09:09:43 -050079# Settings the same status and reason for multiple CVEs is possible
80# via CVE_STATUS_GROUPS variable.
81#
82# CVE_STATUS_GROUPS = "CVE_STATUS_WIN CVE_STATUS_PATCHED"
83#
84# CVE_STATUS_WIN = "CVE-1234-0001 CVE-1234-0003"
85# CVE_STATUS_WIN[status] = "not-applicable-platform: Issue only applies on Windows"
86# CVE_STATUS_PATCHED = "CVE-1234-0002 CVE-1234-0004"
87# CVE_STATUS_PATCHED[status] = "fixed-version: Fixed externally"
88#
89# All possible CVE statuses could be found in cve-check-map.conf
90# CVE_CHECK_STATUSMAP[not-applicable-platform] = "Ignored"
91# CVE_CHECK_STATUSMAP[fixed-version] = "Patched"
92#
93# CVE_CHECK_IGNORE is deprecated and CVE_STATUS has to be used instead.
94# Keep CVE_CHECK_IGNORE until other layers migrate to new variables
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000095CVE_CHECK_IGNORE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060096
Andrew Geisslerd1e89492021-02-12 15:35:20 -060097# Layers to be excluded
98CVE_CHECK_LAYER_EXCLUDELIST ??= ""
99
Patrick Williams213cb262021-08-07 19:21:33 -0500100# Layers to be included
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600101CVE_CHECK_LAYER_INCLUDELIST ??= ""
102
103
Patrick Williams213cb262021-08-07 19:21:33 -0500104# set to "alphabetical" for version using single alphabetical character as increment release
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600105CVE_VERSION_SUFFIX ??= ""
106
Andrew Geissler8f840682023-07-21 09:09:43 -0500107python () {
108 # Fallback all CVEs from CVE_CHECK_IGNORE to CVE_STATUS
109 cve_check_ignore = d.getVar("CVE_CHECK_IGNORE")
110 if cve_check_ignore:
111 bb.warn("CVE_CHECK_IGNORE is deprecated in favor of CVE_STATUS")
112 for cve in (d.getVar("CVE_CHECK_IGNORE") or "").split():
113 d.setVarFlag("CVE_STATUS", cve, "ignored")
114
115 # Process CVE_STATUS_GROUPS to set multiple statuses and optional detail or description at once
116 for cve_status_group in (d.getVar("CVE_STATUS_GROUPS") or "").split():
117 cve_group = d.getVar(cve_status_group)
118 if cve_group is not None:
119 for cve in cve_group.split():
120 d.setVarFlag("CVE_STATUS", cve, d.getVarFlag(cve_status_group, "status"))
121 else:
122 bb.warn("CVE_STATUS_GROUPS contains undefined variable %s" % cve_status_group)
123}
124
Andrew Geisslerd5838332022-05-27 11:33:10 -0500125def generate_json_report(d, out_path, link_path):
126 if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")):
127 import json
Andrew Geissler78b72792022-06-14 06:47:25 -0500128 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Andrew Geisslerd5838332022-05-27 11:33:10 -0500129
130 bb.note("Generating JSON CVE summary")
131 index_file = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
132 summary = {"version":"1", "package": []}
133 with open(index_file) as f:
134 filename = f.readline()
135 while filename:
136 with open(filename.rstrip()) as j:
137 data = json.load(j)
138 cve_check_merge_jsons(summary, data)
139 filename = f.readline()
140
141 with open(out_path, "w") as f:
142 json.dump(summary, f, indent=2)
143
Andrew Geissler78b72792022-06-14 06:47:25 -0500144 update_symlinks(out_path, link_path)
Andrew Geisslerd5838332022-05-27 11:33:10 -0500145
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500146python cve_save_summary_handler () {
147 import shutil
148 import datetime
Andrew Geissler78b72792022-06-14 06:47:25 -0500149 from oe.cve_check import update_symlinks
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500150
151 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
152
153 cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME")
154 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
155 bb.utils.mkdirhier(cvelogpath)
156
157 timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
158 cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp))
159
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500160 if os.path.exists(cve_tmp_file):
161 shutil.copyfile(cve_tmp_file, cve_summary_file)
Andrew Geissler78b72792022-06-14 06:47:25 -0500162 cvefile_link = os.path.join(cvelogpath, cve_summary_name)
163 update_symlinks(cve_summary_file, cvefile_link)
164 bb.plain("Complete CVE report summary created at: %s" % cvefile_link)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500165
Andrew Geisslerd5838332022-05-27 11:33:10 -0500166 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
167 json_summary_link_name = os.path.join(cvelogpath, d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON"))
168 json_summary_name = os.path.join(cvelogpath, "%s-%s.json" % (cve_summary_name, timestamp))
169 generate_json_report(d, json_summary_name, json_summary_link_name)
170 bb.plain("Complete CVE JSON report summary created at: %s" % json_summary_link_name)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500171}
172
173addhandler cve_save_summary_handler
174cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted"
175
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600176python do_cve_check () {
177 """
178 Check recipe for patched and unpatched CVEs
179 """
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500180 from oe.cve_check import get_patched_cves
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181
Patrick Williams92b42cb2022-09-03 06:53:57 -0500182 with bb.utils.fileslocked([d.getVar("CVE_CHECK_DB_FILE_LOCK")], shared=True):
183 if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
184 try:
185 patched_cves = get_patched_cves(d)
186 except FileNotFoundError:
187 bb.fatal("Failure in searching patches")
188 ignored, patched, unpatched, status = check_cves(d, patched_cves)
189 if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status):
190 cve_data = get_cve_info(d, patched + unpatched + ignored)
191 cve_write_data(d, patched, unpatched, ignored, cve_data, status)
192 else:
193 bb.note("No CVE database found, skipping CVE check")
Brad Bishop96ff1982019-08-19 13:50:42 -0400194
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600195}
196
Patrick Williams03907ee2022-05-01 06:28:52 -0500197addtask cve_check before do_build
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500198do_cve_check[depends] = "cve-update-nvd2-native:do_fetch"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600199do_cve_check[nostamp] = "1"
200
201python cve_check_cleanup () {
202 """
203 Delete the file used to gather all the CVE information.
204 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
Andrew Geissler9aee5002022-03-30 16:27:02 +0000206 bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207}
208
209addhandler cve_check_cleanup
Andrew Geissler615f2f12022-07-15 14:00:58 -0500210cve_check_cleanup[eventmask] = "bb.event.BuildCompleted"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600211
212python cve_check_write_rootfs_manifest () {
213 """
214 Create CVE manifest when building an image
215 """
216
217 import shutil
Andrew Geissler78b72792022-06-14 06:47:25 -0500218 import json
219 from oe.rootfs import image_list_installed_packages
220 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600221
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500222 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500223 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500224 if os.path.exists(deploy_file):
225 bb.utils.remove(deploy_file)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000226 deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
227 if os.path.exists(deploy_file_json):
228 bb.utils.remove(deploy_file_json)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500229
Andrew Geissler78b72792022-06-14 06:47:25 -0500230 # Create a list of relevant recipies
231 recipies = set()
232 for pkg in list(image_list_installed_packages(d)):
233 pkg_info = os.path.join(d.getVar('PKGDATA_DIR'),
234 'runtime-reverse', pkg)
235 pkg_data = oe.packagedata.read_pkgdatafile(pkg_info)
236 recipies.add(pkg_data["PN"])
237
238 bb.note("Writing rootfs CVE manifest")
Andrew Geisslerc5535c92023-01-27 16:10:19 -0600239 deploy_dir = d.getVar("IMGDEPLOYDIR")
Andrew Geissler78b72792022-06-14 06:47:25 -0500240 link_name = d.getVar("IMAGE_LINK_NAME")
241
242 json_data = {"version":"1", "package": []}
243 text_data = ""
244 enable_json = d.getVar("CVE_CHECK_FORMAT_JSON") == "1"
245 enable_text = d.getVar("CVE_CHECK_FORMAT_TEXT") == "1"
246
247 save_pn = d.getVar("PN")
248
249 for pkg in recipies:
250 # To be able to use the CVE_CHECK_RECIPE_FILE variable we have to evaluate
251 # it with the different PN names set each time.
252 d.setVar("PN", pkg)
253 if enable_text:
254 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE")
255 if os.path.exists(pkgfilepath):
256 with open(pkgfilepath) as pfile:
257 text_data += pfile.read()
258
259 if enable_json:
260 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
261 if os.path.exists(pkgfilepath):
262 with open(pkgfilepath) as j:
263 data = json.load(j)
264 cve_check_merge_jsons(json_data, data)
265
266 d.setVar("PN", save_pn)
267
268 if enable_text:
269 link_path = os.path.join(deploy_dir, "%s.cve" % link_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 manifest_name = d.getVar("CVE_CHECK_MANIFEST")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600271
Andrew Geissler78b72792022-06-14 06:47:25 -0500272 with open(manifest_name, "w") as f:
273 f.write(text_data)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600274
Andrew Geissler78b72792022-06-14 06:47:25 -0500275 update_symlinks(manifest_name, link_path)
276 bb.plain("Image CVE report stored in: %s" % manifest_name)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000277
Andrew Geissler78b72792022-06-14 06:47:25 -0500278 if enable_json:
279 link_path = os.path.join(deploy_dir, "%s.json" % link_name)
280 manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON")
281
282 with open(manifest_name, "w") as f:
283 json.dump(json_data, f, indent=2)
284
285 update_symlinks(manifest_name, link_path)
286 bb.plain("Image CVE JSON report stored in: %s" % manifest_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600287}
288
Andrew Geissler5082cc72023-09-11 08:41:39 -0400289ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Andrew Geisslerd5838332022-05-27 11:33:10 -0500291do_populate_sdk[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600292
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600293def check_cves(d, patched_cves):
294 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400295 Connect to the NVD database and find unpatched cves.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600296 """
Andrew Geissler8f840682023-07-21 09:09:43 -0500297 from oe.cve_check import Version, convert_cve_version, decode_cve_status
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600298
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600299 pn = d.getVar("PN")
300 real_pv = d.getVar("PV")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600301 suffix = d.getVar("CVE_VERSION_SUFFIX")
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600302
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600303 cves_unpatched = []
Andrew Geissler615f2f12022-07-15 14:00:58 -0500304 cves_ignored = []
Andrew Geissler9aee5002022-03-30 16:27:02 +0000305 cves_status = []
306 cves_in_recipe = False
Brad Bishopf3fd2882019-06-21 08:06:37 -0400307 # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
Brad Bishop96ff1982019-08-19 13:50:42 -0400308 products = d.getVar("CVE_PRODUCT").split()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400309 # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
Brad Bishop96ff1982019-08-19 13:50:42 -0400310 if not products:
Andrew Geissler78b72792022-06-14 06:47:25 -0500311 return ([], [], [], [])
Brad Bishop316dfdd2018-06-25 12:45:53 -0400312 pv = d.getVar("CVE_VERSION").split("+git")[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600313
Andrew Geissler9aee5002022-03-30 16:27:02 +0000314 # If the recipe has been skipped/ignored we return empty lists
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000315 if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split():
Andrew Geissler9aee5002022-03-30 16:27:02 +0000316 bb.note("Recipe has been skipped by cve-check")
317 return ([], [], [], [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600318
Andrew Geissler8f840682023-07-21 09:09:43 -0500319 # Convert CVE_STATUS into ignored CVEs and check validity
320 cve_ignore = []
321 for cve in (d.getVarFlags("CVE_STATUS") or {}):
322 decoded_status, _, _ = decode_cve_status(d, cve)
323 if decoded_status == "Ignored":
324 cve_ignore.append(cve)
Brad Bishop96ff1982019-08-19 13:50:42 -0400325
Brad Bishopf3fd2882019-06-21 08:06:37 -0400326 import sqlite3
Brad Bishop6dbb3162019-11-25 09:41:34 -0500327 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
328 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600329
Brad Bishop6dbb3162019-11-25 09:41:34 -0500330 # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
Brad Bishop96ff1982019-08-19 13:50:42 -0400331 for product in products:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000332 cves_in_product = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400333 if ":" in product:
334 vendor, product = product.split(":", 1)
Brad Bishop96ff1982019-08-19 13:50:42 -0400335 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500336 vendor = "%"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600337
Brad Bishop6dbb3162019-11-25 09:41:34 -0500338 # Find all relevant CVE IDs.
Patrick Williams92b42cb2022-09-03 06:53:57 -0500339 cve_cursor = conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor))
340 for cverow in cve_cursor:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500341 cve = cverow[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600342
Andrew Geissler9aee5002022-03-30 16:27:02 +0000343 if cve in cve_ignore:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500344 bb.note("%s-%s ignores %s" % (product, pv, cve))
345 cves_ignored.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500346 continue
Brad Bishopf3fd2882019-06-21 08:06:37 -0400347 elif cve in patched_cves:
348 bb.note("%s has been patched" % (cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500349 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000350 # Write status once only for each product
351 if not cves_in_product:
352 cves_status.append([product, True])
353 cves_in_product = True
354 cves_in_recipe = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500355
356 vulnerable = False
Andrew Geissler615f2f12022-07-15 14:00:58 -0500357 ignored = False
358
Patrick Williams92b42cb2022-09-03 06:53:57 -0500359 product_cursor = conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor))
360 for row in product_cursor:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500361 (_, _, _, version_start, operator_start, version_end, operator_end) = row
362 #bb.debug(2, "Evaluating row " + str(row))
Andrew Geissler615f2f12022-07-15 14:00:58 -0500363 if cve in cve_ignore:
364 ignored = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500365
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500366 version_start = convert_cve_version(version_start)
367 version_end = convert_cve_version(version_end)
368
Andrew Geissler82c905d2020-04-13 13:39:40 -0500369 if (operator_start == '=' and pv == version_start) or version_start == '-':
Brad Bishop6dbb3162019-11-25 09:41:34 -0500370 vulnerable = True
Brad Bishop96ff1982019-08-19 13:50:42 -0400371 else:
372 if operator_start:
373 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600374 vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix))
375 vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix))
Brad Bishop96ff1982019-08-19 13:50:42 -0400376 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500377 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400378 (product, pv, operator_start, version_start, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500379 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400380 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500381 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400382
383 if operator_end:
384 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600385 vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) )
386 vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) )
Brad Bishop96ff1982019-08-19 13:50:42 -0400387 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500388 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400389 (product, pv, operator_end, version_end, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500390 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400391 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500392 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400393
394 if operator_start and operator_end:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500395 vulnerable = vulnerable_start and vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400396 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500397 vulnerable = vulnerable_start or vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400398
Brad Bishop6dbb3162019-11-25 09:41:34 -0500399 if vulnerable:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500400 if ignored:
401 bb.note("%s is ignored in %s-%s" % (cve, pn, real_pv))
402 cves_ignored.append(cve)
403 else:
404 bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve))
405 cves_unpatched.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500406 break
Patrick Williams92b42cb2022-09-03 06:53:57 -0500407 product_cursor.close()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500408
409 if not vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600410 bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500411 patched_cves.add(cve)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500412 cve_cursor.close()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500413
Andrew Geissler9aee5002022-03-30 16:27:02 +0000414 if not cves_in_product:
415 bb.note("No CVE records found for product %s, pn %s" % (product, pn))
416 cves_status.append([product, False])
417
Brad Bishopf3fd2882019-06-21 08:06:37 -0400418 conn.close()
419
Andrew Geissler9aee5002022-03-30 16:27:02 +0000420 if not cves_in_recipe:
421 bb.note("No CVE records for products in recipe %s" % (pn))
422
Andrew Geissler615f2f12022-07-15 14:00:58 -0500423 return (list(cves_ignored), list(patched_cves), cves_unpatched, cves_status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600424
425def get_cve_info(d, cves):
426 """
Brad Bishop96ff1982019-08-19 13:50:42 -0400427 Get CVE information from the database.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600428 """
429
Brad Bishop6dbb3162019-11-25 09:41:34 -0500430 import sqlite3
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600431
432 cve_data = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000433 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
434 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600435
Brad Bishop6dbb3162019-11-25 09:41:34 -0500436 for cve in cves:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500437 cursor = conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,))
438 for row in cursor:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500439 cve_data[row[0]] = {}
440 cve_data[row[0]]["summary"] = row[1]
441 cve_data[row[0]]["scorev2"] = row[2]
442 cve_data[row[0]]["scorev3"] = row[3]
443 cve_data[row[0]]["modified"] = row[4]
444 cve_data[row[0]]["vector"] = row[5]
Andrew Geissler220dafd2023-10-04 10:18:08 -0500445 cve_data[row[0]]["vectorString"] = row[6]
Patrick Williams92b42cb2022-09-03 06:53:57 -0500446 cursor.close()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500447 conn.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600448 return cve_data
449
Andrew Geissler9aee5002022-03-30 16:27:02 +0000450def cve_write_data_text(d, patched, unpatched, ignored, cve_data):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600451 """
452 Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
453 CVE manifest if enabled.
454 """
455
Andrew Geissler8f840682023-07-21 09:09:43 -0500456 from oe.cve_check import decode_cve_status
457
Brad Bishop316dfdd2018-06-25 12:45:53 -0400458 cve_file = d.getVar("CVE_CHECK_LOG")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600459 fdir_name = d.getVar("FILE_DIRNAME")
460 layer = fdir_name.split("/")[-3]
461
462 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
463 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
464
Andrew Geissler615f2f12022-07-15 14:00:58 -0500465 report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
466
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600467 if exclude_layers and layer in exclude_layers:
468 return
469
470 if include_layers and layer not in include_layers:
471 return
472
Andrew Geissler9aee5002022-03-30 16:27:02 +0000473 # Early exit, the text format does not report packages without CVEs
Andrew Geissler615f2f12022-07-15 14:00:58 -0500474 if not patched+unpatched+ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000475 return
476
Patrick Williams213cb262021-08-07 19:21:33 -0500477 nvd_link = "https://nvd.nist.gov/vuln/detail/"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600478 write_string = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500479 unpatched_cves = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400480 bb.utils.mkdirhier(os.path.dirname(cve_file))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600481
482 for cve in sorted(cve_data):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500483 is_patched = cve in patched
Andrew Geissler615f2f12022-07-15 14:00:58 -0500484 is_ignored = cve in ignored
485
Andrew Geissler8f840682023-07-21 09:09:43 -0500486 status = "Unpatched"
Andrew Geissler615f2f12022-07-15 14:00:58 -0500487 if (is_patched or is_ignored) and not report_all:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500488 continue
Andrew Geissler8f840682023-07-21 09:09:43 -0500489 if is_ignored:
490 status = "Ignored"
491 elif is_patched:
492 status = "Patched"
493 else:
494 # default value of status is Unpatched
495 unpatched_cves.append(cve)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500496
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600497 write_string += "LAYER: %s\n" % layer
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500498 write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500499 write_string += "PACKAGE VERSION: %s%s\n" % (d.getVar("EXTENDPE"), d.getVar("PV"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600500 write_string += "CVE: %s\n" % cve
Andrew Geissler8f840682023-07-21 09:09:43 -0500501 write_string += "CVE STATUS: %s\n" % status
502 _, detail, description = decode_cve_status(d, cve)
503 if detail:
504 write_string += "CVE DETAIL: %s\n" % detail
505 if description:
506 write_string += "CVE DESCRIPTION: %s\n" % description
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600507 write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
Brad Bishopf3fd2882019-06-21 08:06:37 -0400508 write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
509 write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600510 write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
Andrew Geissler220dafd2023-10-04 10:18:08 -0500511 write_string += "VECTORSTRING: %s\n" % cve_data[cve]["vectorString"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600512 write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
513
Andrew Geissler78b72792022-06-14 06:47:25 -0500514 if unpatched_cves and d.getVar("CVE_CHECK_SHOW_WARNINGS") == "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500515 bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
516
Andrew Geissler78b72792022-06-14 06:47:25 -0500517 with open(cve_file, "w") as f:
518 bb.note("Writing file %s with CVE information" % cve_file)
519 f.write(write_string)
520
521 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
522 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
523 bb.utils.mkdirhier(os.path.dirname(deploy_file))
524 with open(deploy_file, "w") as f:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600525 f.write(write_string)
526
Andrew Geissler78b72792022-06-14 06:47:25 -0500527 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
528 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
529 bb.utils.mkdirhier(cvelogpath)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500530
Andrew Geissler78b72792022-06-14 06:47:25 -0500531 with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
532 f.write("%s" % write_string)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000533
534def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file):
535 """
536 Write CVE information in the JSON format: to WORKDIR; and to
537 CVE_CHECK_DIR, if CVE manifest if enabled, write fragment
538 files that will be assembled at the end in cve_check_write_rootfs_manifest.
539 """
540
541 import json
542
543 write_string = json.dumps(output, indent=2)
544 with open(direct_file, "w") as f:
545 bb.note("Writing file %s with CVE information" % direct_file)
546 f.write(write_string)
547
548 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
549 bb.utils.mkdirhier(os.path.dirname(deploy_file))
550 with open(deploy_file, "w") as f:
551 f.write(write_string)
552
553 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
554 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
555 index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
556 bb.utils.mkdirhier(cvelogpath)
557 fragment_file = os.path.basename(deploy_file)
558 fragment_path = os.path.join(cvelogpath, fragment_file)
559 with open(fragment_path, "w") as f:
560 f.write(write_string)
561 with open(index_path, "a+") as f:
562 f.write("%s\n" % fragment_path)
563
564def cve_write_data_json(d, patched, unpatched, ignored, cve_data, cve_status):
565 """
566 Prepare CVE data for the JSON format, then write it.
567 """
568
Andrew Geissler8f840682023-07-21 09:09:43 -0500569 from oe.cve_check import decode_cve_status
570
Andrew Geissler9aee5002022-03-30 16:27:02 +0000571 output = {"version":"1", "package": []}
572 nvd_link = "https://nvd.nist.gov/vuln/detail/"
573
574 fdir_name = d.getVar("FILE_DIRNAME")
575 layer = fdir_name.split("/")[-3]
576
577 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
578 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
579
Andrew Geissler615f2f12022-07-15 14:00:58 -0500580 report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
581
Andrew Geissler9aee5002022-03-30 16:27:02 +0000582 if exclude_layers and layer in exclude_layers:
583 return
584
585 if include_layers and layer not in include_layers:
586 return
587
588 unpatched_cves = []
589
590 product_data = []
591 for s in cve_status:
592 p = {"product": s[0], "cvesInRecord": "Yes"}
593 if s[1] == False:
594 p["cvesInRecord"] = "No"
595 product_data.append(p)
596
597 package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV"))
598 package_data = {
599 "name" : d.getVar("PN"),
600 "layer" : layer,
601 "version" : package_version,
602 "products": product_data
603 }
604 cve_list = []
605
606 for cve in sorted(cve_data):
607 is_patched = cve in patched
Andrew Geissler615f2f12022-07-15 14:00:58 -0500608 is_ignored = cve in ignored
Andrew Geissler9aee5002022-03-30 16:27:02 +0000609 status = "Unpatched"
Andrew Geissler615f2f12022-07-15 14:00:58 -0500610 if (is_patched or is_ignored) and not report_all:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000611 continue
Andrew Geissler615f2f12022-07-15 14:00:58 -0500612 if is_ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000613 status = "Ignored"
614 elif is_patched:
615 status = "Patched"
616 else:
617 # default value of status is Unpatched
618 unpatched_cves.append(cve)
619
620 issue_link = "%s%s" % (nvd_link, cve)
621
622 cve_item = {
623 "id" : cve,
624 "summary" : cve_data[cve]["summary"],
625 "scorev2" : cve_data[cve]["scorev2"],
626 "scorev3" : cve_data[cve]["scorev3"],
627 "vector" : cve_data[cve]["vector"],
Andrew Geissler220dafd2023-10-04 10:18:08 -0500628 "vectorString" : cve_data[cve]["vectorString"],
Andrew Geissler9aee5002022-03-30 16:27:02 +0000629 "status" : status,
630 "link": issue_link
631 }
Andrew Geissler8f840682023-07-21 09:09:43 -0500632 _, detail, description = decode_cve_status(d, cve)
633 if detail:
634 cve_item["detail"] = detail
635 if description:
636 cve_item["description"] = description
Andrew Geissler9aee5002022-03-30 16:27:02 +0000637 cve_list.append(cve_item)
638
639 package_data["issue"] = cve_list
640 output["package"].append(package_data)
641
642 direct_file = d.getVar("CVE_CHECK_LOG_JSON")
643 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
644 manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")
645
646 cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file)
647
648def cve_write_data(d, patched, unpatched, ignored, cve_data, status):
649 """
650 Write CVE data in each enabled format.
651 """
652
653 if d.getVar("CVE_CHECK_FORMAT_TEXT") == "1":
654 cve_write_data_text(d, patched, unpatched, ignored, cve_data)
655 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
656 cve_write_data_json(d, patched, unpatched, ignored, cve_data, status)