blob: da7f93371c0ec9a11530be6420acc11344cf8686 [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 Geissler615f2f12022-07-15 14:00:58 -050050# Report Patched or Ignored CVEs
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050051CVE_CHECK_REPORT_PATCHED ??= "1"
Andrew Geissler615f2f12022-07-15 14:00:58 -050052
Andrew Geissler78b72792022-06-14 06:47:25 -050053CVE_CHECK_SHOW_WARNINGS ??= "1"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050054
Andrew Geissler9aee5002022-03-30 16:27:02 +000055# Provide text output
56CVE_CHECK_FORMAT_TEXT ??= "1"
57
58# Provide JSON output
59CVE_CHECK_FORMAT_JSON ??= "1"
60
61# Check for packages without CVEs (no issues or missing product name)
62CVE_CHECK_COVERAGE ??= "1"
63
64# Skip CVE Check for packages (PN)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000065CVE_CHECK_SKIP_RECIPE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060066
Andrew Geissler9aee5002022-03-30 16:27:02 +000067# Ingore the check for a given list of CVEs. If a CVE is found,
68# then it is considered patched. The value is a string containing
69# space separated CVE values:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050070#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000071# CVE_CHECK_IGNORE = 'CVE-2014-2524 CVE-2018-1234'
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050072#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000073CVE_CHECK_IGNORE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074
Andrew Geisslerd1e89492021-02-12 15:35:20 -060075# Layers to be excluded
76CVE_CHECK_LAYER_EXCLUDELIST ??= ""
77
Patrick Williams213cb262021-08-07 19:21:33 -050078# Layers to be included
Andrew Geisslerd1e89492021-02-12 15:35:20 -060079CVE_CHECK_LAYER_INCLUDELIST ??= ""
80
81
Patrick Williams213cb262021-08-07 19:21:33 -050082# set to "alphabetical" for version using single alphabetical character as increment release
Andrew Geisslerd1e89492021-02-12 15:35:20 -060083CVE_VERSION_SUFFIX ??= ""
84
Andrew Geisslerd5838332022-05-27 11:33:10 -050085def generate_json_report(d, out_path, link_path):
86 if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")):
87 import json
Andrew Geissler78b72792022-06-14 06:47:25 -050088 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Andrew Geisslerd5838332022-05-27 11:33:10 -050089
90 bb.note("Generating JSON CVE summary")
91 index_file = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
92 summary = {"version":"1", "package": []}
93 with open(index_file) as f:
94 filename = f.readline()
95 while filename:
96 with open(filename.rstrip()) as j:
97 data = json.load(j)
98 cve_check_merge_jsons(summary, data)
99 filename = f.readline()
100
101 with open(out_path, "w") as f:
102 json.dump(summary, f, indent=2)
103
Andrew Geissler78b72792022-06-14 06:47:25 -0500104 update_symlinks(out_path, link_path)
Andrew Geisslerd5838332022-05-27 11:33:10 -0500105
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500106python cve_save_summary_handler () {
107 import shutil
108 import datetime
Andrew Geissler78b72792022-06-14 06:47:25 -0500109 from oe.cve_check import update_symlinks
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500110
111 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
112
113 cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME")
114 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
115 bb.utils.mkdirhier(cvelogpath)
116
117 timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
118 cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp))
119
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500120 if os.path.exists(cve_tmp_file):
121 shutil.copyfile(cve_tmp_file, cve_summary_file)
Andrew Geissler78b72792022-06-14 06:47:25 -0500122 cvefile_link = os.path.join(cvelogpath, cve_summary_name)
123 update_symlinks(cve_summary_file, cvefile_link)
124 bb.plain("Complete CVE report summary created at: %s" % cvefile_link)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500125
Andrew Geisslerd5838332022-05-27 11:33:10 -0500126 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
127 json_summary_link_name = os.path.join(cvelogpath, d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON"))
128 json_summary_name = os.path.join(cvelogpath, "%s-%s.json" % (cve_summary_name, timestamp))
129 generate_json_report(d, json_summary_name, json_summary_link_name)
130 bb.plain("Complete CVE JSON report summary created at: %s" % json_summary_link_name)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500131}
132
133addhandler cve_save_summary_handler
134cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted"
135
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600136python do_cve_check () {
137 """
138 Check recipe for patched and unpatched CVEs
139 """
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500140 from oe.cve_check import get_patched_cves
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600141
Brad Bishop96ff1982019-08-19 13:50:42 -0400142 if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500143 try:
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500144 patched_cves = get_patched_cves(d)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500145 except FileNotFoundError:
146 bb.fatal("Failure in searching patches")
Andrew Geissler9aee5002022-03-30 16:27:02 +0000147 ignored, patched, unpatched, status = check_cves(d, patched_cves)
148 if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status):
Andrew Geissler615f2f12022-07-15 14:00:58 -0500149 cve_data = get_cve_info(d, patched + unpatched + ignored)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000150 cve_write_data(d, patched, unpatched, ignored, cve_data, status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600151 else:
Brad Bishop96ff1982019-08-19 13:50:42 -0400152 bb.note("No CVE database found, skipping CVE check")
153
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600154}
155
Patrick Williams03907ee2022-05-01 06:28:52 -0500156addtask cve_check before do_build
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500157do_cve_check[depends] = "cve-update-db-native:do_fetch"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600158do_cve_check[nostamp] = "1"
159
160python cve_check_cleanup () {
161 """
162 Delete the file used to gather all the CVE information.
163 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
Andrew Geissler9aee5002022-03-30 16:27:02 +0000165 bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166}
167
168addhandler cve_check_cleanup
Andrew Geissler615f2f12022-07-15 14:00:58 -0500169cve_check_cleanup[eventmask] = "bb.event.BuildCompleted"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170
171python cve_check_write_rootfs_manifest () {
172 """
173 Create CVE manifest when building an image
174 """
175
176 import shutil
Andrew Geissler78b72792022-06-14 06:47:25 -0500177 import json
178 from oe.rootfs import image_list_installed_packages
179 from oe.cve_check import cve_check_merge_jsons, update_symlinks
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600180
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500181 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500182 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500183 if os.path.exists(deploy_file):
184 bb.utils.remove(deploy_file)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000185 deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
186 if os.path.exists(deploy_file_json):
187 bb.utils.remove(deploy_file_json)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500188
Andrew Geissler78b72792022-06-14 06:47:25 -0500189 # Create a list of relevant recipies
190 recipies = set()
191 for pkg in list(image_list_installed_packages(d)):
192 pkg_info = os.path.join(d.getVar('PKGDATA_DIR'),
193 'runtime-reverse', pkg)
194 pkg_data = oe.packagedata.read_pkgdatafile(pkg_info)
195 recipies.add(pkg_data["PN"])
196
197 bb.note("Writing rootfs CVE manifest")
198 deploy_dir = d.getVar("DEPLOY_DIR_IMAGE")
199 link_name = d.getVar("IMAGE_LINK_NAME")
200
201 json_data = {"version":"1", "package": []}
202 text_data = ""
203 enable_json = d.getVar("CVE_CHECK_FORMAT_JSON") == "1"
204 enable_text = d.getVar("CVE_CHECK_FORMAT_TEXT") == "1"
205
206 save_pn = d.getVar("PN")
207
208 for pkg in recipies:
209 # To be able to use the CVE_CHECK_RECIPE_FILE variable we have to evaluate
210 # it with the different PN names set each time.
211 d.setVar("PN", pkg)
212 if enable_text:
213 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE")
214 if os.path.exists(pkgfilepath):
215 with open(pkgfilepath) as pfile:
216 text_data += pfile.read()
217
218 if enable_json:
219 pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
220 if os.path.exists(pkgfilepath):
221 with open(pkgfilepath) as j:
222 data = json.load(j)
223 cve_check_merge_jsons(json_data, data)
224
225 d.setVar("PN", save_pn)
226
227 if enable_text:
228 link_path = os.path.join(deploy_dir, "%s.cve" % link_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500229 manifest_name = d.getVar("CVE_CHECK_MANIFEST")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230
Andrew Geissler78b72792022-06-14 06:47:25 -0500231 with open(manifest_name, "w") as f:
232 f.write(text_data)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233
Andrew Geissler78b72792022-06-14 06:47:25 -0500234 update_symlinks(manifest_name, link_path)
235 bb.plain("Image CVE report stored in: %s" % manifest_name)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000236
Andrew Geissler78b72792022-06-14 06:47:25 -0500237 if enable_json:
238 link_path = os.path.join(deploy_dir, "%s.json" % link_name)
239 manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON")
240
241 with open(manifest_name, "w") as f:
242 json.dump(json_data, f, indent=2)
243
244 update_symlinks(manifest_name, link_path)
245 bb.plain("Image CVE JSON report stored in: %s" % manifest_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600246}
247
Patrick Williams213cb262021-08-07 19:21:33 -0500248ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500249do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Andrew Geisslerd5838332022-05-27 11:33:10 -0500250do_populate_sdk[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600252def check_cves(d, patched_cves):
253 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400254 Connect to the NVD database and find unpatched cves.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600255 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600256 from oe.cve_check import Version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600258 pn = d.getVar("PN")
259 real_pv = d.getVar("PV")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600260 suffix = d.getVar("CVE_VERSION_SUFFIX")
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600261
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600262 cves_unpatched = []
Andrew Geissler615f2f12022-07-15 14:00:58 -0500263 cves_ignored = []
Andrew Geissler9aee5002022-03-30 16:27:02 +0000264 cves_status = []
265 cves_in_recipe = False
Brad Bishopf3fd2882019-06-21 08:06:37 -0400266 # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
Brad Bishop96ff1982019-08-19 13:50:42 -0400267 products = d.getVar("CVE_PRODUCT").split()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400268 # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
Brad Bishop96ff1982019-08-19 13:50:42 -0400269 if not products:
Andrew Geissler78b72792022-06-14 06:47:25 -0500270 return ([], [], [], [])
Brad Bishop316dfdd2018-06-25 12:45:53 -0400271 pv = d.getVar("CVE_VERSION").split("+git")[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600272
Andrew Geissler9aee5002022-03-30 16:27:02 +0000273 # If the recipe has been skipped/ignored we return empty lists
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000274 if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split():
Andrew Geissler9aee5002022-03-30 16:27:02 +0000275 bb.note("Recipe has been skipped by cve-check")
276 return ([], [], [], [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600277
Andrew Geissler9aee5002022-03-30 16:27:02 +0000278 cve_ignore = d.getVar("CVE_CHECK_IGNORE").split()
Brad Bishop96ff1982019-08-19 13:50:42 -0400279
Brad Bishopf3fd2882019-06-21 08:06:37 -0400280 import sqlite3
Brad Bishop6dbb3162019-11-25 09:41:34 -0500281 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
282 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600283
Brad Bishop6dbb3162019-11-25 09:41:34 -0500284 # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
Brad Bishop96ff1982019-08-19 13:50:42 -0400285 for product in products:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000286 cves_in_product = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400287 if ":" in product:
288 vendor, product = product.split(":", 1)
Brad Bishop96ff1982019-08-19 13:50:42 -0400289 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500290 vendor = "%"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600291
Brad Bishop6dbb3162019-11-25 09:41:34 -0500292 # Find all relevant CVE IDs.
293 for cverow in conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor)):
294 cve = cverow[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600295
Andrew Geissler9aee5002022-03-30 16:27:02 +0000296 if cve in cve_ignore:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500297 bb.note("%s-%s ignores %s" % (product, pv, cve))
298 cves_ignored.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500299 continue
Brad Bishopf3fd2882019-06-21 08:06:37 -0400300 elif cve in patched_cves:
301 bb.note("%s has been patched" % (cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500302 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000303 # Write status once only for each product
304 if not cves_in_product:
305 cves_status.append([product, True])
306 cves_in_product = True
307 cves_in_recipe = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500308
309 vulnerable = False
Andrew Geissler615f2f12022-07-15 14:00:58 -0500310 ignored = False
311
Brad Bishop6dbb3162019-11-25 09:41:34 -0500312 for row in conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor)):
313 (_, _, _, version_start, operator_start, version_end, operator_end) = row
314 #bb.debug(2, "Evaluating row " + str(row))
Andrew Geissler615f2f12022-07-15 14:00:58 -0500315 if cve in cve_ignore:
316 ignored = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500317
Andrew Geissler82c905d2020-04-13 13:39:40 -0500318 if (operator_start == '=' and pv == version_start) or version_start == '-':
Brad Bishop6dbb3162019-11-25 09:41:34 -0500319 vulnerable = True
Brad Bishop96ff1982019-08-19 13:50:42 -0400320 else:
321 if operator_start:
322 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600323 vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix))
324 vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix))
Brad Bishop96ff1982019-08-19 13:50:42 -0400325 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500326 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400327 (product, pv, operator_start, version_start, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500328 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400329 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500330 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400331
332 if operator_end:
333 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600334 vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) )
335 vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) )
Brad Bishop96ff1982019-08-19 13:50:42 -0400336 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500337 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400338 (product, pv, operator_end, version_end, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500339 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400340 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500341 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400342
343 if operator_start and operator_end:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500344 vulnerable = vulnerable_start and vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400345 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500346 vulnerable = vulnerable_start or vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400347
Brad Bishop6dbb3162019-11-25 09:41:34 -0500348 if vulnerable:
Andrew Geissler615f2f12022-07-15 14:00:58 -0500349 if ignored:
350 bb.note("%s is ignored in %s-%s" % (cve, pn, real_pv))
351 cves_ignored.append(cve)
352 else:
353 bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve))
354 cves_unpatched.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500355 break
356
357 if not vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600358 bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500359 patched_cves.add(cve)
360
Andrew Geissler9aee5002022-03-30 16:27:02 +0000361 if not cves_in_product:
362 bb.note("No CVE records found for product %s, pn %s" % (product, pn))
363 cves_status.append([product, False])
364
Brad Bishopf3fd2882019-06-21 08:06:37 -0400365 conn.close()
366
Andrew Geissler9aee5002022-03-30 16:27:02 +0000367 if not cves_in_recipe:
368 bb.note("No CVE records for products in recipe %s" % (pn))
369
Andrew Geissler615f2f12022-07-15 14:00:58 -0500370 return (list(cves_ignored), list(patched_cves), cves_unpatched, cves_status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600371
372def get_cve_info(d, cves):
373 """
Brad Bishop96ff1982019-08-19 13:50:42 -0400374 Get CVE information from the database.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600375 """
376
Brad Bishop6dbb3162019-11-25 09:41:34 -0500377 import sqlite3
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600378
379 cve_data = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000380 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
381 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600382
Brad Bishop6dbb3162019-11-25 09:41:34 -0500383 for cve in cves:
384 for row in conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,)):
385 cve_data[row[0]] = {}
386 cve_data[row[0]]["summary"] = row[1]
387 cve_data[row[0]]["scorev2"] = row[2]
388 cve_data[row[0]]["scorev3"] = row[3]
389 cve_data[row[0]]["modified"] = row[4]
390 cve_data[row[0]]["vector"] = row[5]
391
392 conn.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600393 return cve_data
394
Andrew Geissler9aee5002022-03-30 16:27:02 +0000395def cve_write_data_text(d, patched, unpatched, ignored, cve_data):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600396 """
397 Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
398 CVE manifest if enabled.
399 """
400
Brad Bishop316dfdd2018-06-25 12:45:53 -0400401 cve_file = d.getVar("CVE_CHECK_LOG")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600402 fdir_name = d.getVar("FILE_DIRNAME")
403 layer = fdir_name.split("/")[-3]
404
405 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
406 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
407
Andrew Geissler615f2f12022-07-15 14:00:58 -0500408 report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
409
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600410 if exclude_layers and layer in exclude_layers:
411 return
412
413 if include_layers and layer not in include_layers:
414 return
415
Andrew Geissler9aee5002022-03-30 16:27:02 +0000416 # Early exit, the text format does not report packages without CVEs
Andrew Geissler615f2f12022-07-15 14:00:58 -0500417 if not patched+unpatched+ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000418 return
419
Patrick Williams213cb262021-08-07 19:21:33 -0500420 nvd_link = "https://nvd.nist.gov/vuln/detail/"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600421 write_string = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500422 unpatched_cves = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400423 bb.utils.mkdirhier(os.path.dirname(cve_file))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600424
425 for cve in sorted(cve_data):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500426 is_patched = cve in patched
Andrew Geissler615f2f12022-07-15 14:00:58 -0500427 is_ignored = cve in ignored
428
429 if (is_patched or is_ignored) and not report_all:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500430 continue
Andrew Geissler615f2f12022-07-15 14:00:58 -0500431
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600432 write_string += "LAYER: %s\n" % layer
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500433 write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500434 write_string += "PACKAGE VERSION: %s%s\n" % (d.getVar("EXTENDPE"), d.getVar("PV"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600435 write_string += "CVE: %s\n" % cve
Andrew Geissler615f2f12022-07-15 14:00:58 -0500436 if is_ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000437 write_string += "CVE STATUS: Ignored\n"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500438 elif is_patched:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600439 write_string += "CVE STATUS: Patched\n"
440 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500441 unpatched_cves.append(cve)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600442 write_string += "CVE STATUS: Unpatched\n"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600443 write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
Brad Bishopf3fd2882019-06-21 08:06:37 -0400444 write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
445 write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600446 write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
447 write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
448
Andrew Geissler78b72792022-06-14 06:47:25 -0500449 if unpatched_cves and d.getVar("CVE_CHECK_SHOW_WARNINGS") == "1":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500450 bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
451
Andrew Geissler78b72792022-06-14 06:47:25 -0500452 with open(cve_file, "w") as f:
453 bb.note("Writing file %s with CVE information" % cve_file)
454 f.write(write_string)
455
456 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
457 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
458 bb.utils.mkdirhier(os.path.dirname(deploy_file))
459 with open(deploy_file, "w") as f:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600460 f.write(write_string)
461
Andrew Geissler78b72792022-06-14 06:47:25 -0500462 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
463 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
464 bb.utils.mkdirhier(cvelogpath)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500465
Andrew Geissler78b72792022-06-14 06:47:25 -0500466 with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
467 f.write("%s" % write_string)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000468
469def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file):
470 """
471 Write CVE information in the JSON format: to WORKDIR; and to
472 CVE_CHECK_DIR, if CVE manifest if enabled, write fragment
473 files that will be assembled at the end in cve_check_write_rootfs_manifest.
474 """
475
476 import json
477
478 write_string = json.dumps(output, indent=2)
479 with open(direct_file, "w") as f:
480 bb.note("Writing file %s with CVE information" % direct_file)
481 f.write(write_string)
482
483 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
484 bb.utils.mkdirhier(os.path.dirname(deploy_file))
485 with open(deploy_file, "w") as f:
486 f.write(write_string)
487
488 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
489 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
490 index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
491 bb.utils.mkdirhier(cvelogpath)
492 fragment_file = os.path.basename(deploy_file)
493 fragment_path = os.path.join(cvelogpath, fragment_file)
494 with open(fragment_path, "w") as f:
495 f.write(write_string)
496 with open(index_path, "a+") as f:
497 f.write("%s\n" % fragment_path)
498
499def cve_write_data_json(d, patched, unpatched, ignored, cve_data, cve_status):
500 """
501 Prepare CVE data for the JSON format, then write it.
502 """
503
504 output = {"version":"1", "package": []}
505 nvd_link = "https://nvd.nist.gov/vuln/detail/"
506
507 fdir_name = d.getVar("FILE_DIRNAME")
508 layer = fdir_name.split("/")[-3]
509
510 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
511 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
512
Andrew Geissler615f2f12022-07-15 14:00:58 -0500513 report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
514
Andrew Geissler9aee5002022-03-30 16:27:02 +0000515 if exclude_layers and layer in exclude_layers:
516 return
517
518 if include_layers and layer not in include_layers:
519 return
520
521 unpatched_cves = []
522
523 product_data = []
524 for s in cve_status:
525 p = {"product": s[0], "cvesInRecord": "Yes"}
526 if s[1] == False:
527 p["cvesInRecord"] = "No"
528 product_data.append(p)
529
530 package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV"))
531 package_data = {
532 "name" : d.getVar("PN"),
533 "layer" : layer,
534 "version" : package_version,
535 "products": product_data
536 }
537 cve_list = []
538
539 for cve in sorted(cve_data):
540 is_patched = cve in patched
Andrew Geissler615f2f12022-07-15 14:00:58 -0500541 is_ignored = cve in ignored
Andrew Geissler9aee5002022-03-30 16:27:02 +0000542 status = "Unpatched"
Andrew Geissler615f2f12022-07-15 14:00:58 -0500543 if (is_patched or is_ignored) and not report_all:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000544 continue
Andrew Geissler615f2f12022-07-15 14:00:58 -0500545 if is_ignored:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000546 status = "Ignored"
547 elif is_patched:
548 status = "Patched"
549 else:
550 # default value of status is Unpatched
551 unpatched_cves.append(cve)
552
553 issue_link = "%s%s" % (nvd_link, cve)
554
555 cve_item = {
556 "id" : cve,
557 "summary" : cve_data[cve]["summary"],
558 "scorev2" : cve_data[cve]["scorev2"],
559 "scorev3" : cve_data[cve]["scorev3"],
560 "vector" : cve_data[cve]["vector"],
561 "status" : status,
562 "link": issue_link
563 }
564 cve_list.append(cve_item)
565
566 package_data["issue"] = cve_list
567 output["package"].append(package_data)
568
569 direct_file = d.getVar("CVE_CHECK_LOG_JSON")
570 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
571 manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")
572
573 cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file)
574
575def cve_write_data(d, patched, unpatched, ignored, cve_data, status):
576 """
577 Write CVE data in each enabled format.
578 """
579
580 if d.getVar("CVE_CHECK_FORMAT_TEXT") == "1":
581 cve_write_data_text(d, patched, unpatched, ignored, cve_data)
582 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
583 cve_write_data_json(d, patched, unpatched, ignored, cve_data, status)