blob: 78516d0bb63a59df3e42c83be267453fae15c19d [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001# This class is used to check recipes against public CVEs.
2#
3# In order to use this class just inherit the class in the
4# local.conf file and it will add the cve_check task for
5# every recipe. The task can be used per recipe, per image,
6# or using the special cases "world" and "universe". The
7# cve_check task will print a warning for every unpatched
8# CVE found and generate a file in the recipe WORKDIR/cve
9# directory. If an image is build it will generate a report
10# in DEPLOY_DIR_IMAGE for all the packages used.
11#
12# Example:
13# bitbake -c cve_check openssl
14# bitbake core-image-sato
15# bitbake -k -c cve_check universe
16#
17# DISCLAIMER
18#
19# This class/tool is meant to be used as support and not
20# the only method to check against CVEs. Running this tool
21# doesn't guarantee your packages are free of CVEs.
22
Patrick Williams213cb262021-08-07 19:21:33 -050023# The product name that the CVE database uses defaults to BPN, but may need to
Brad Bishop37a0e4d2017-12-04 01:01:44 -050024# be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff).
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025CVE_PRODUCT ??= "${BPN}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040026CVE_VERSION ??= "${PV}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -050027
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK"
Andrew Geissler82c905d2020-04-13 13:39:40 -050029CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_1.1.db"
Andrew Geisslerc9f78652020-09-18 14:11:35 -050030CVE_CHECK_DB_FILE_LOCK ?= "${CVE_CHECK_DB_FILE}.lock"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031
Brad Bishop316dfdd2018-06-25 12:45:53 -040032CVE_CHECK_LOG ?= "${T}/cve.log"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060033CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check"
Andrew Geisslerb7d28612020-07-24 16:15:54 -050034CVE_CHECK_SUMMARY_DIR ?= "${LOG_DIR}/cve"
35CVE_CHECK_SUMMARY_FILE_NAME ?= "cve-summary"
36CVE_CHECK_SUMMARY_FILE ?= "${CVE_CHECK_SUMMARY_DIR}/${CVE_CHECK_SUMMARY_FILE_NAME}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000037CVE_CHECK_SUMMARY_FILE_NAME_JSON = "cve-summary.json"
38CVE_CHECK_SUMMARY_INDEX_PATH = "${CVE_CHECK_SUMMARY_DIR}/cve-summary-index.txt"
39
40CVE_CHECK_LOG_JSON ?= "${T}/cve.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060041
42CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050043CVE_CHECK_RECIPE_FILE ?= "${CVE_CHECK_DIR}/${PN}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000044CVE_CHECK_RECIPE_FILE_JSON ?= "${CVE_CHECK_DIR}/${PN}_cve.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060045CVE_CHECK_MANIFEST ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve"
Andrew Geissler9aee5002022-03-30 16:27:02 +000046CVE_CHECK_MANIFEST_JSON ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.json"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047CVE_CHECK_COPY_FILES ??= "1"
48CVE_CHECK_CREATE_MANIFEST ??= "1"
49
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050050CVE_CHECK_REPORT_PATCHED ??= "1"
51
Andrew Geissler9aee5002022-03-30 16:27:02 +000052# Provide text output
53CVE_CHECK_FORMAT_TEXT ??= "1"
54
55# Provide JSON output
56CVE_CHECK_FORMAT_JSON ??= "1"
57
58# Check for packages without CVEs (no issues or missing product name)
59CVE_CHECK_COVERAGE ??= "1"
60
61# Skip CVE Check for packages (PN)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000062CVE_CHECK_SKIP_RECIPE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063
Andrew Geissler9aee5002022-03-30 16:27:02 +000064# Ingore the check for a given list of CVEs. If a CVE is found,
65# then it is considered patched. The value is a string containing
66# space separated CVE values:
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050067#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000068# CVE_CHECK_IGNORE = 'CVE-2014-2524 CVE-2018-1234'
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050069#
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000070CVE_CHECK_IGNORE ?= ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060071
Andrew Geisslerd1e89492021-02-12 15:35:20 -060072# Layers to be excluded
73CVE_CHECK_LAYER_EXCLUDELIST ??= ""
74
Patrick Williams213cb262021-08-07 19:21:33 -050075# Layers to be included
Andrew Geisslerd1e89492021-02-12 15:35:20 -060076CVE_CHECK_LAYER_INCLUDELIST ??= ""
77
78
Patrick Williams213cb262021-08-07 19:21:33 -050079# set to "alphabetical" for version using single alphabetical character as increment release
Andrew Geisslerd1e89492021-02-12 15:35:20 -060080CVE_VERSION_SUFFIX ??= ""
81
Andrew Geisslerb7d28612020-07-24 16:15:54 -050082python cve_save_summary_handler () {
83 import shutil
84 import datetime
85
86 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
87
88 cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME")
89 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
90 bb.utils.mkdirhier(cvelogpath)
91
92 timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
93 cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp))
94
Andrew Geisslerc9f78652020-09-18 14:11:35 -050095 if os.path.exists(cve_tmp_file):
96 shutil.copyfile(cve_tmp_file, cve_summary_file)
Andrew Geisslerb7d28612020-07-24 16:15:54 -050097
Andrew Geisslerc9f78652020-09-18 14:11:35 -050098 if cve_summary_file and os.path.exists(cve_summary_file):
99 cvefile_link = os.path.join(cvelogpath, cve_summary_name)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500100
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500101 if os.path.exists(os.path.realpath(cvefile_link)):
102 os.remove(cvefile_link)
103 os.symlink(os.path.basename(cve_summary_file), cvefile_link)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500104}
105
106addhandler cve_save_summary_handler
107cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted"
108
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600109python do_cve_check () {
110 """
111 Check recipe for patched and unpatched CVEs
112 """
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500113 from oe.cve_check import get_patched_cves
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600114
Brad Bishop96ff1982019-08-19 13:50:42 -0400115 if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500116 try:
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500117 patched_cves = get_patched_cves(d)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500118 except FileNotFoundError:
119 bb.fatal("Failure in searching patches")
Andrew Geissler9aee5002022-03-30 16:27:02 +0000120 ignored, patched, unpatched, status = check_cves(d, patched_cves)
121 if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600122 cve_data = get_cve_info(d, patched + unpatched)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000123 cve_write_data(d, patched, unpatched, ignored, cve_data, status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600124 else:
Brad Bishop96ff1982019-08-19 13:50:42 -0400125 bb.note("No CVE database found, skipping CVE check")
126
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600127}
128
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500129addtask cve_check before do_build after do_fetch
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500130do_cve_check[depends] = "cve-update-db-native:do_fetch"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131do_cve_check[nostamp] = "1"
132
133python cve_check_cleanup () {
134 """
135 Delete the file used to gather all the CVE information.
136 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
Andrew Geissler9aee5002022-03-30 16:27:02 +0000138 bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139}
140
141addhandler cve_check_cleanup
142cve_check_cleanup[eventmask] = "bb.cooker.CookerExit"
143
144python cve_check_write_rootfs_manifest () {
145 """
146 Create CVE manifest when building an image
147 """
148
149 import shutil
Andrew Geissler9aee5002022-03-30 16:27:02 +0000150 from oe.cve_check import cve_check_merge_jsons
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600151
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500152 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500153 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500154 if os.path.exists(deploy_file):
155 bb.utils.remove(deploy_file)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000156 deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
157 if os.path.exists(deploy_file_json):
158 bb.utils.remove(deploy_file_json)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500159
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600161 bb.note("Writing rootfs CVE manifest")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 deploy_dir = d.getVar("DEPLOY_DIR_IMAGE")
163 link_name = d.getVar("IMAGE_LINK_NAME")
164 manifest_name = d.getVar("CVE_CHECK_MANIFEST")
165 cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000167 bb.utils.mkdirhier(os.path.dirname(manifest_name))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600168 shutil.copyfile(cve_tmp_file, manifest_name)
169
170 if manifest_name and os.path.exists(manifest_name):
171 manifest_link = os.path.join(deploy_dir, "%s.cve" % link_name)
172 # If we already have another manifest, update symlinks
173 if os.path.exists(os.path.realpath(manifest_link)):
174 os.remove(manifest_link)
175 os.symlink(os.path.basename(manifest_name), manifest_link)
176 bb.plain("Image CVE report stored in: %s" % manifest_name)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000177
178 if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")):
179 import json
180 bb.note("Generating JSON CVE manifest")
181 deploy_dir = d.getVar("DEPLOY_DIR_IMAGE")
182 link_name = d.getVar("IMAGE_LINK_NAME")
183 manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON")
184 index_file = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
185 manifest = {"version":"1", "package": []}
186 with open(index_file) as f:
187 filename = f.readline()
188 while filename:
189 with open(filename.rstrip()) as j:
190 data = json.load(j)
191 cve_check_merge_jsons(manifest, data)
192 filename = f.readline()
193
194 with open(manifest_name, "w") as f:
195 json.dump(manifest, f, indent=2)
196 bb.plain("Image CVE report stored in: %s" % manifest_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600197}
198
Patrick Williams213cb262021-08-07 19:21:33 -0500199ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600201
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202def check_cves(d, patched_cves):
203 """
Brad Bishopf3fd2882019-06-21 08:06:37 -0400204 Connect to the NVD database and find unpatched cves.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600205 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600206 from oe.cve_check import Version
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600207
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600208 pn = d.getVar("PN")
209 real_pv = d.getVar("PV")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600210 suffix = d.getVar("CVE_VERSION_SUFFIX")
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600211
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600212 cves_unpatched = []
Andrew Geissler9aee5002022-03-30 16:27:02 +0000213 cves_status = []
214 cves_in_recipe = False
Brad Bishopf3fd2882019-06-21 08:06:37 -0400215 # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
Brad Bishop96ff1982019-08-19 13:50:42 -0400216 products = d.getVar("CVE_PRODUCT").split()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400217 # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
Brad Bishop96ff1982019-08-19 13:50:42 -0400218 if not products:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000219 return ([], [], [], {})
Brad Bishop316dfdd2018-06-25 12:45:53 -0400220 pv = d.getVar("CVE_VERSION").split("+git")[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600221
Andrew Geissler9aee5002022-03-30 16:27:02 +0000222 # If the recipe has been skipped/ignored we return empty lists
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000223 if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split():
Andrew Geissler9aee5002022-03-30 16:27:02 +0000224 bb.note("Recipe has been skipped by cve-check")
225 return ([], [], [], [])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600226
Andrew Geissler9aee5002022-03-30 16:27:02 +0000227 cve_ignore = d.getVar("CVE_CHECK_IGNORE").split()
Brad Bishop96ff1982019-08-19 13:50:42 -0400228
Brad Bishopf3fd2882019-06-21 08:06:37 -0400229 import sqlite3
Brad Bishop6dbb3162019-11-25 09:41:34 -0500230 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
231 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600232
Brad Bishop6dbb3162019-11-25 09:41:34 -0500233 # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
Brad Bishop96ff1982019-08-19 13:50:42 -0400234 for product in products:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000235 cves_in_product = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400236 if ":" in product:
237 vendor, product = product.split(":", 1)
Brad Bishop96ff1982019-08-19 13:50:42 -0400238 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500239 vendor = "%"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600240
Brad Bishop6dbb3162019-11-25 09:41:34 -0500241 # Find all relevant CVE IDs.
242 for cverow in conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor)):
243 cve = cverow[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600244
Andrew Geissler9aee5002022-03-30 16:27:02 +0000245 if cve in cve_ignore:
246 bb.note("%s-%s has been ignored for %s" % (product, pv, cve))
247 # TODO: this should be in the report as 'ignored'
Brad Bishop64c979e2019-11-04 13:55:29 -0500248 patched_cves.add(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500249 continue
Brad Bishopf3fd2882019-06-21 08:06:37 -0400250 elif cve in patched_cves:
251 bb.note("%s has been patched" % (cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500252 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000253 # Write status once only for each product
254 if not cves_in_product:
255 cves_status.append([product, True])
256 cves_in_product = True
257 cves_in_recipe = True
Brad Bishop6dbb3162019-11-25 09:41:34 -0500258
259 vulnerable = False
260 for row in conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor)):
261 (_, _, _, version_start, operator_start, version_end, operator_end) = row
262 #bb.debug(2, "Evaluating row " + str(row))
263
Andrew Geissler82c905d2020-04-13 13:39:40 -0500264 if (operator_start == '=' and pv == version_start) or version_start == '-':
Brad Bishop6dbb3162019-11-25 09:41:34 -0500265 vulnerable = True
Brad Bishop96ff1982019-08-19 13:50:42 -0400266 else:
267 if operator_start:
268 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600269 vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix))
270 vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix))
Brad Bishop96ff1982019-08-19 13:50:42 -0400271 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500272 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400273 (product, pv, operator_start, version_start, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500274 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400275 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500276 vulnerable_start = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400277
278 if operator_end:
279 try:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600280 vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) )
281 vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) )
Brad Bishop96ff1982019-08-19 13:50:42 -0400282 except:
Brad Bishop64c979e2019-11-04 13:55:29 -0500283 bb.warn("%s: Failed to compare %s %s %s for %s" %
Brad Bishop96ff1982019-08-19 13:50:42 -0400284 (product, pv, operator_end, version_end, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500285 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400286 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500287 vulnerable_end = False
Brad Bishop96ff1982019-08-19 13:50:42 -0400288
289 if operator_start and operator_end:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500290 vulnerable = vulnerable_start and vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400291 else:
Brad Bishop6dbb3162019-11-25 09:41:34 -0500292 vulnerable = vulnerable_start or vulnerable_end
Brad Bishop96ff1982019-08-19 13:50:42 -0400293
Brad Bishop6dbb3162019-11-25 09:41:34 -0500294 if vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600295 bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop96ff1982019-08-19 13:50:42 -0400296 cves_unpatched.append(cve)
Brad Bishop6dbb3162019-11-25 09:41:34 -0500297 break
298
299 if not vulnerable:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600300 bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve))
Brad Bishop6dbb3162019-11-25 09:41:34 -0500301 # TODO: not patched but not vulnerable
302 patched_cves.add(cve)
303
Andrew Geissler9aee5002022-03-30 16:27:02 +0000304 if not cves_in_product:
305 bb.note("No CVE records found for product %s, pn %s" % (product, pn))
306 cves_status.append([product, False])
307
Brad Bishopf3fd2882019-06-21 08:06:37 -0400308 conn.close()
309
Andrew Geissler9aee5002022-03-30 16:27:02 +0000310 if not cves_in_recipe:
311 bb.note("No CVE records for products in recipe %s" % (pn))
312
313 return (list(cve_ignore), list(patched_cves), cves_unpatched, cves_status)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600314
315def get_cve_info(d, cves):
316 """
Brad Bishop96ff1982019-08-19 13:50:42 -0400317 Get CVE information from the database.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600318 """
319
Brad Bishop6dbb3162019-11-25 09:41:34 -0500320 import sqlite3
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600321
322 cve_data = {}
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000323 db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
324 conn = sqlite3.connect(db_file, uri=True)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600325
Brad Bishop6dbb3162019-11-25 09:41:34 -0500326 for cve in cves:
327 for row in conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,)):
328 cve_data[row[0]] = {}
329 cve_data[row[0]]["summary"] = row[1]
330 cve_data[row[0]]["scorev2"] = row[2]
331 cve_data[row[0]]["scorev3"] = row[3]
332 cve_data[row[0]]["modified"] = row[4]
333 cve_data[row[0]]["vector"] = row[5]
334
335 conn.close()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600336 return cve_data
337
Andrew Geissler9aee5002022-03-30 16:27:02 +0000338def cve_write_data_text(d, patched, unpatched, ignored, cve_data):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600339 """
340 Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
341 CVE manifest if enabled.
342 """
343
Brad Bishop316dfdd2018-06-25 12:45:53 -0400344 cve_file = d.getVar("CVE_CHECK_LOG")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600345 fdir_name = d.getVar("FILE_DIRNAME")
346 layer = fdir_name.split("/")[-3]
347
348 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
349 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
350
351 if exclude_layers and layer in exclude_layers:
352 return
353
354 if include_layers and layer not in include_layers:
355 return
356
Andrew Geissler9aee5002022-03-30 16:27:02 +0000357 # Early exit, the text format does not report packages without CVEs
358 if not patched+unpatched:
359 return
360
Patrick Williams213cb262021-08-07 19:21:33 -0500361 nvd_link = "https://nvd.nist.gov/vuln/detail/"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600362 write_string = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500363 unpatched_cves = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400364 bb.utils.mkdirhier(os.path.dirname(cve_file))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600365
366 for cve in sorted(cve_data):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500367 is_patched = cve in patched
368 if is_patched and (d.getVar("CVE_CHECK_REPORT_PATCHED") != "1"):
369 continue
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600370 write_string += "LAYER: %s\n" % layer
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500371 write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500372 write_string += "PACKAGE VERSION: %s%s\n" % (d.getVar("EXTENDPE"), d.getVar("PV"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600373 write_string += "CVE: %s\n" % cve
Andrew Geissler9aee5002022-03-30 16:27:02 +0000374 if cve in ignored:
375 write_string += "CVE STATUS: Ignored\n"
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500376 elif is_patched:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600377 write_string += "CVE STATUS: Patched\n"
378 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500379 unpatched_cves.append(cve)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600380 write_string += "CVE STATUS: Unpatched\n"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600381 write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
Brad Bishopf3fd2882019-06-21 08:06:37 -0400382 write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"]
383 write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600384 write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
385 write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
386
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500387 if unpatched_cves:
388 bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
389
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500390 if write_string:
391 with open(cve_file, "w") as f:
392 bb.note("Writing file %s with CVE information" % cve_file)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600393 f.write(write_string)
394
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500395 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
396 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE")
397 bb.utils.mkdirhier(os.path.dirname(deploy_file))
398 with open(deploy_file, "w") as f:
399 f.write(write_string)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500400
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500401 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
402 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
403 bb.utils.mkdirhier(cvelogpath)
404
405 with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
406 f.write("%s" % write_string)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000407
408def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file):
409 """
410 Write CVE information in the JSON format: to WORKDIR; and to
411 CVE_CHECK_DIR, if CVE manifest if enabled, write fragment
412 files that will be assembled at the end in cve_check_write_rootfs_manifest.
413 """
414
415 import json
416
417 write_string = json.dumps(output, indent=2)
418 with open(direct_file, "w") as f:
419 bb.note("Writing file %s with CVE information" % direct_file)
420 f.write(write_string)
421
422 if d.getVar("CVE_CHECK_COPY_FILES") == "1":
423 bb.utils.mkdirhier(os.path.dirname(deploy_file))
424 with open(deploy_file, "w") as f:
425 f.write(write_string)
426
427 if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
428 cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
429 index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
430 bb.utils.mkdirhier(cvelogpath)
431 fragment_file = os.path.basename(deploy_file)
432 fragment_path = os.path.join(cvelogpath, fragment_file)
433 with open(fragment_path, "w") as f:
434 f.write(write_string)
435 with open(index_path, "a+") as f:
436 f.write("%s\n" % fragment_path)
437
438def cve_write_data_json(d, patched, unpatched, ignored, cve_data, cve_status):
439 """
440 Prepare CVE data for the JSON format, then write it.
441 """
442
443 output = {"version":"1", "package": []}
444 nvd_link = "https://nvd.nist.gov/vuln/detail/"
445
446 fdir_name = d.getVar("FILE_DIRNAME")
447 layer = fdir_name.split("/")[-3]
448
449 include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
450 exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
451
452 if exclude_layers and layer in exclude_layers:
453 return
454
455 if include_layers and layer not in include_layers:
456 return
457
458 unpatched_cves = []
459
460 product_data = []
461 for s in cve_status:
462 p = {"product": s[0], "cvesInRecord": "Yes"}
463 if s[1] == False:
464 p["cvesInRecord"] = "No"
465 product_data.append(p)
466
467 package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV"))
468 package_data = {
469 "name" : d.getVar("PN"),
470 "layer" : layer,
471 "version" : package_version,
472 "products": product_data
473 }
474 cve_list = []
475
476 for cve in sorted(cve_data):
477 is_patched = cve in patched
478 status = "Unpatched"
479 if is_patched and (d.getVar("CVE_CHECK_REPORT_PATCHED") != "1"):
480 continue
481 if cve in ignored:
482 status = "Ignored"
483 elif is_patched:
484 status = "Patched"
485 else:
486 # default value of status is Unpatched
487 unpatched_cves.append(cve)
488
489 issue_link = "%s%s" % (nvd_link, cve)
490
491 cve_item = {
492 "id" : cve,
493 "summary" : cve_data[cve]["summary"],
494 "scorev2" : cve_data[cve]["scorev2"],
495 "scorev3" : cve_data[cve]["scorev3"],
496 "vector" : cve_data[cve]["vector"],
497 "status" : status,
498 "link": issue_link
499 }
500 cve_list.append(cve_item)
501
502 package_data["issue"] = cve_list
503 output["package"].append(package_data)
504
505 direct_file = d.getVar("CVE_CHECK_LOG_JSON")
506 deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
507 manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")
508
509 cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file)
510
511def cve_write_data(d, patched, unpatched, ignored, cve_data, status):
512 """
513 Write CVE data in each enabled format.
514 """
515
516 if d.getVar("CVE_CHECK_FORMAT_TEXT") == "1":
517 cve_write_data_text(d, patched, unpatched, ignored, cve_data)
518 if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
519 cve_write_data_json(d, patched, unpatched, ignored, cve_data, status)