Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 7 | # 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 Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 29 | # The product name that the CVE database uses defaults to BPN, but may need to |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 30 | # be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff). |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 31 | CVE_PRODUCT ??= "${BPN}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 32 | CVE_VERSION ??= "${PV}" |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 33 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 34 | CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK" |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 35 | CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_2-1.db" |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 36 | CVE_CHECK_DB_FILE_LOCK ?= "${CVE_CHECK_DB_FILE}.lock" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 38 | CVE_CHECK_LOG ?= "${T}/cve.log" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 39 | CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check" |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 40 | CVE_CHECK_SUMMARY_DIR ?= "${LOG_DIR}/cve" |
| 41 | CVE_CHECK_SUMMARY_FILE_NAME ?= "cve-summary" |
| 42 | CVE_CHECK_SUMMARY_FILE ?= "${CVE_CHECK_SUMMARY_DIR}/${CVE_CHECK_SUMMARY_FILE_NAME}" |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 43 | CVE_CHECK_SUMMARY_FILE_NAME_JSON = "cve-summary.json" |
| 44 | CVE_CHECK_SUMMARY_INDEX_PATH = "${CVE_CHECK_SUMMARY_DIR}/cve-summary-index.txt" |
| 45 | |
| 46 | CVE_CHECK_LOG_JSON ?= "${T}/cve.json" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | |
| 48 | CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 49 | CVE_CHECK_RECIPE_FILE ?= "${CVE_CHECK_DIR}/${PN}" |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 50 | CVE_CHECK_RECIPE_FILE_JSON ?= "${CVE_CHECK_DIR}/${PN}_cve.json" |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 51 | CVE_CHECK_MANIFEST ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}.cve" |
| 52 | CVE_CHECK_MANIFEST_JSON ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}.json" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | CVE_CHECK_COPY_FILES ??= "1" |
| 54 | CVE_CHECK_CREATE_MANIFEST ??= "1" |
| 55 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 56 | # Report Patched or Ignored CVEs |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 57 | CVE_CHECK_REPORT_PATCHED ??= "1" |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 58 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 59 | CVE_CHECK_SHOW_WARNINGS ??= "1" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 60 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 61 | # Provide text output |
| 62 | CVE_CHECK_FORMAT_TEXT ??= "1" |
| 63 | |
| 64 | # Provide JSON output |
| 65 | CVE_CHECK_FORMAT_JSON ??= "1" |
| 66 | |
| 67 | # Check for packages without CVEs (no issues or missing product name) |
| 68 | CVE_CHECK_COVERAGE ??= "1" |
| 69 | |
| 70 | # Skip CVE Check for packages (PN) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 71 | CVE_CHECK_SKIP_RECIPE ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 72 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 73 | # 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 Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 75 | # |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 76 | # CVE_STATUS[CVE-1234-0001] = "not-applicable-platform: Issue only applies on Windows" |
| 77 | # CVE_STATUS[CVE-1234-0002] = "fixed-version: Fixed externally" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 78 | # |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 79 | # 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 Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 95 | CVE_CHECK_IGNORE ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 96 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 97 | # Layers to be excluded |
| 98 | CVE_CHECK_LAYER_EXCLUDELIST ??= "" |
| 99 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 100 | # Layers to be included |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 101 | CVE_CHECK_LAYER_INCLUDELIST ??= "" |
| 102 | |
| 103 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 104 | # set to "alphabetical" for version using single alphabetical character as increment release |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 105 | CVE_VERSION_SUFFIX ??= "" |
| 106 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 107 | python () { |
| 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 Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 125 | def generate_json_report(d, out_path, link_path): |
| 126 | if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")): |
| 127 | import json |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 128 | from oe.cve_check import cve_check_merge_jsons, update_symlinks |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 129 | |
| 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 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 141 | summary["package"].sort(key=lambda d: d['name']) |
| 142 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 143 | with open(out_path, "w") as f: |
| 144 | json.dump(summary, f, indent=2) |
| 145 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 146 | update_symlinks(out_path, link_path) |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 147 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 148 | python cve_save_summary_handler () { |
| 149 | import shutil |
| 150 | import datetime |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 151 | from oe.cve_check import update_symlinks |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 152 | |
| 153 | cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE") |
| 154 | |
| 155 | cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME") |
| 156 | cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR") |
| 157 | bb.utils.mkdirhier(cvelogpath) |
| 158 | |
| 159 | timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
| 160 | cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp)) |
| 161 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 162 | if os.path.exists(cve_tmp_file): |
| 163 | shutil.copyfile(cve_tmp_file, cve_summary_file) |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 164 | cvefile_link = os.path.join(cvelogpath, cve_summary_name) |
| 165 | update_symlinks(cve_summary_file, cvefile_link) |
| 166 | bb.plain("Complete CVE report summary created at: %s" % cvefile_link) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 167 | |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 168 | if d.getVar("CVE_CHECK_FORMAT_JSON") == "1": |
| 169 | json_summary_link_name = os.path.join(cvelogpath, d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")) |
| 170 | json_summary_name = os.path.join(cvelogpath, "%s-%s.json" % (cve_summary_name, timestamp)) |
| 171 | generate_json_report(d, json_summary_name, json_summary_link_name) |
| 172 | bb.plain("Complete CVE JSON report summary created at: %s" % json_summary_link_name) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | addhandler cve_save_summary_handler |
| 176 | cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted" |
| 177 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 178 | python do_cve_check () { |
| 179 | """ |
| 180 | Check recipe for patched and unpatched CVEs |
| 181 | """ |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 182 | from oe.cve_check import get_patched_cves |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 183 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 184 | with bb.utils.fileslocked([d.getVar("CVE_CHECK_DB_FILE_LOCK")], shared=True): |
| 185 | if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")): |
| 186 | try: |
| 187 | patched_cves = get_patched_cves(d) |
| 188 | except FileNotFoundError: |
| 189 | bb.fatal("Failure in searching patches") |
| 190 | ignored, patched, unpatched, status = check_cves(d, patched_cves) |
| 191 | if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status): |
| 192 | cve_data = get_cve_info(d, patched + unpatched + ignored) |
| 193 | cve_write_data(d, patched, unpatched, ignored, cve_data, status) |
| 194 | else: |
| 195 | bb.note("No CVE database found, skipping CVE check") |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 196 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 199 | addtask cve_check before do_build |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 200 | do_cve_check[depends] = "cve-update-nvd2-native:do_fetch" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 201 | do_cve_check[nostamp] = "1" |
| 202 | |
| 203 | python cve_check_cleanup () { |
| 204 | """ |
| 205 | Delete the file used to gather all the CVE information. |
| 206 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 207 | bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE")) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 208 | bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | addhandler cve_check_cleanup |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 212 | cve_check_cleanup[eventmask] = "bb.event.BuildCompleted" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 213 | |
| 214 | python cve_check_write_rootfs_manifest () { |
| 215 | """ |
| 216 | Create CVE manifest when building an image |
| 217 | """ |
| 218 | |
| 219 | import shutil |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 220 | import json |
| 221 | from oe.rootfs import image_list_installed_packages |
| 222 | from oe.cve_check import cve_check_merge_jsons, update_symlinks |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 223 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 224 | if d.getVar("CVE_CHECK_COPY_FILES") == "1": |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 225 | deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 226 | if os.path.exists(deploy_file): |
| 227 | bb.utils.remove(deploy_file) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 228 | deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON") |
| 229 | if os.path.exists(deploy_file_json): |
| 230 | bb.utils.remove(deploy_file_json) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 231 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 232 | # Create a list of relevant recipies |
| 233 | recipies = set() |
| 234 | for pkg in list(image_list_installed_packages(d)): |
| 235 | pkg_info = os.path.join(d.getVar('PKGDATA_DIR'), |
| 236 | 'runtime-reverse', pkg) |
| 237 | pkg_data = oe.packagedata.read_pkgdatafile(pkg_info) |
| 238 | recipies.add(pkg_data["PN"]) |
| 239 | |
| 240 | bb.note("Writing rootfs CVE manifest") |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 241 | deploy_dir = d.getVar("IMGDEPLOYDIR") |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 242 | link_name = d.getVar("IMAGE_LINK_NAME") |
| 243 | |
| 244 | json_data = {"version":"1", "package": []} |
| 245 | text_data = "" |
| 246 | enable_json = d.getVar("CVE_CHECK_FORMAT_JSON") == "1" |
| 247 | enable_text = d.getVar("CVE_CHECK_FORMAT_TEXT") == "1" |
| 248 | |
| 249 | save_pn = d.getVar("PN") |
| 250 | |
| 251 | for pkg in recipies: |
| 252 | # To be able to use the CVE_CHECK_RECIPE_FILE variable we have to evaluate |
| 253 | # it with the different PN names set each time. |
| 254 | d.setVar("PN", pkg) |
| 255 | if enable_text: |
| 256 | pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE") |
| 257 | if os.path.exists(pkgfilepath): |
| 258 | with open(pkgfilepath) as pfile: |
| 259 | text_data += pfile.read() |
| 260 | |
| 261 | if enable_json: |
| 262 | pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE_JSON") |
| 263 | if os.path.exists(pkgfilepath): |
| 264 | with open(pkgfilepath) as j: |
| 265 | data = json.load(j) |
| 266 | cve_check_merge_jsons(json_data, data) |
| 267 | |
| 268 | d.setVar("PN", save_pn) |
| 269 | |
| 270 | if enable_text: |
| 271 | link_path = os.path.join(deploy_dir, "%s.cve" % link_name) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 272 | manifest_name = d.getVar("CVE_CHECK_MANIFEST") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 273 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 274 | with open(manifest_name, "w") as f: |
| 275 | f.write(text_data) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 276 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 277 | update_symlinks(manifest_name, link_path) |
| 278 | bb.plain("Image CVE report stored in: %s" % manifest_name) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 279 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 280 | if enable_json: |
| 281 | link_path = os.path.join(deploy_dir, "%s.json" % link_name) |
| 282 | manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON") |
| 283 | |
| 284 | with open(manifest_name, "w") as f: |
| 285 | json.dump(json_data, f, indent=2) |
| 286 | |
| 287 | update_symlinks(manifest_name, link_path) |
| 288 | bb.plain("Image CVE JSON report stored in: %s" % manifest_name) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 289 | } |
| 290 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 291 | ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 292 | do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 293 | do_populate_sdk[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 294 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 295 | def check_cves(d, patched_cves): |
| 296 | """ |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 297 | Connect to the NVD database and find unpatched cves. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 298 | """ |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 299 | from oe.cve_check import Version, convert_cve_version, decode_cve_status |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 300 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 301 | pn = d.getVar("PN") |
| 302 | real_pv = d.getVar("PV") |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 303 | suffix = d.getVar("CVE_VERSION_SUFFIX") |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 304 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 305 | cves_unpatched = [] |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 306 | cves_ignored = [] |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 307 | cves_status = [] |
| 308 | cves_in_recipe = False |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 309 | # CVE_PRODUCT can contain more than one product (eg. curl/libcurl) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 310 | products = d.getVar("CVE_PRODUCT").split() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 311 | # If this has been unset then we're not scanning for CVEs here (for example, image recipes) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 312 | if not products: |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 313 | return ([], [], [], []) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 314 | pv = d.getVar("CVE_VERSION").split("+git")[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 315 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 316 | # If the recipe has been skipped/ignored we return empty lists |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 317 | if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split(): |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 318 | bb.note("Recipe has been skipped by cve-check") |
| 319 | return ([], [], [], []) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 320 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 321 | # Convert CVE_STATUS into ignored CVEs and check validity |
| 322 | cve_ignore = [] |
| 323 | for cve in (d.getVarFlags("CVE_STATUS") or {}): |
| 324 | decoded_status, _, _ = decode_cve_status(d, cve) |
| 325 | if decoded_status == "Ignored": |
| 326 | cve_ignore.append(cve) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 327 | |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 328 | import sqlite3 |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 329 | db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro") |
| 330 | conn = sqlite3.connect(db_file, uri=True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 331 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 332 | # For each of the known product names (e.g. curl has CPEs using curl and libcurl)... |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 333 | for product in products: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 334 | cves_in_product = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 335 | if ":" in product: |
| 336 | vendor, product = product.split(":", 1) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 337 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 338 | vendor = "%" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 339 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 340 | # Find all relevant CVE IDs. |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 341 | cve_cursor = conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor)) |
| 342 | for cverow in cve_cursor: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 343 | cve = cverow[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 344 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 345 | if cve in cve_ignore: |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 346 | bb.note("%s-%s ignores %s" % (product, pv, cve)) |
| 347 | cves_ignored.append(cve) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 348 | continue |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 349 | elif cve in patched_cves: |
| 350 | bb.note("%s has been patched" % (cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 351 | continue |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 352 | # Write status once only for each product |
| 353 | if not cves_in_product: |
| 354 | cves_status.append([product, True]) |
| 355 | cves_in_product = True |
| 356 | cves_in_recipe = True |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 357 | |
| 358 | vulnerable = False |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 359 | ignored = False |
| 360 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 361 | product_cursor = conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor)) |
| 362 | for row in product_cursor: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 363 | (_, _, _, version_start, operator_start, version_end, operator_end) = row |
| 364 | #bb.debug(2, "Evaluating row " + str(row)) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 365 | if cve in cve_ignore: |
| 366 | ignored = True |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 367 | |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 368 | version_start = convert_cve_version(version_start) |
| 369 | version_end = convert_cve_version(version_end) |
| 370 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 371 | if (operator_start == '=' and pv == version_start) or version_start == '-': |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 372 | vulnerable = True |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 373 | else: |
| 374 | if operator_start: |
| 375 | try: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 376 | vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix)) |
| 377 | vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix)) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 378 | except: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 379 | bb.warn("%s: Failed to compare %s %s %s for %s" % |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 380 | (product, pv, operator_start, version_start, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 381 | vulnerable_start = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 382 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 383 | vulnerable_start = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 384 | |
| 385 | if operator_end: |
| 386 | try: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 387 | vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) ) |
| 388 | vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) ) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 389 | except: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 390 | bb.warn("%s: Failed to compare %s %s %s for %s" % |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 391 | (product, pv, operator_end, version_end, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 392 | vulnerable_end = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 393 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 394 | vulnerable_end = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 395 | |
| 396 | if operator_start and operator_end: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 397 | vulnerable = vulnerable_start and vulnerable_end |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 398 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 399 | vulnerable = vulnerable_start or vulnerable_end |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 400 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 401 | if vulnerable: |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 402 | if ignored: |
| 403 | bb.note("%s is ignored in %s-%s" % (cve, pn, real_pv)) |
| 404 | cves_ignored.append(cve) |
| 405 | else: |
| 406 | bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve)) |
| 407 | cves_unpatched.append(cve) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 408 | break |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 409 | product_cursor.close() |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 410 | |
| 411 | if not vulnerable: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 412 | bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 413 | patched_cves.add(cve) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 414 | cve_cursor.close() |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 415 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 416 | if not cves_in_product: |
| 417 | bb.note("No CVE records found for product %s, pn %s" % (product, pn)) |
| 418 | cves_status.append([product, False]) |
| 419 | |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 420 | conn.close() |
| 421 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 422 | if not cves_in_recipe: |
| 423 | bb.note("No CVE records for products in recipe %s" % (pn)) |
| 424 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 425 | return (list(cves_ignored), list(patched_cves), cves_unpatched, cves_status) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 426 | |
| 427 | def get_cve_info(d, cves): |
| 428 | """ |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 429 | Get CVE information from the database. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 430 | """ |
| 431 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 432 | import sqlite3 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 433 | |
| 434 | cve_data = {} |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 435 | db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro") |
| 436 | conn = sqlite3.connect(db_file, uri=True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 437 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 438 | for cve in cves: |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 439 | cursor = conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,)) |
| 440 | for row in cursor: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 441 | cve_data[row[0]] = {} |
| 442 | cve_data[row[0]]["summary"] = row[1] |
| 443 | cve_data[row[0]]["scorev2"] = row[2] |
| 444 | cve_data[row[0]]["scorev3"] = row[3] |
| 445 | cve_data[row[0]]["modified"] = row[4] |
| 446 | cve_data[row[0]]["vector"] = row[5] |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 447 | cve_data[row[0]]["vectorString"] = row[6] |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 448 | cursor.close() |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 449 | conn.close() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 450 | return cve_data |
| 451 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 452 | def cve_write_data_text(d, patched, unpatched, ignored, cve_data): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 453 | """ |
| 454 | Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and |
| 455 | CVE manifest if enabled. |
| 456 | """ |
| 457 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 458 | from oe.cve_check import decode_cve_status |
| 459 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 460 | cve_file = d.getVar("CVE_CHECK_LOG") |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 461 | fdir_name = d.getVar("FILE_DIRNAME") |
| 462 | layer = fdir_name.split("/")[-3] |
| 463 | |
| 464 | include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split() |
| 465 | exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split() |
| 466 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 467 | report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1" |
| 468 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 469 | if exclude_layers and layer in exclude_layers: |
| 470 | return |
| 471 | |
| 472 | if include_layers and layer not in include_layers: |
| 473 | return |
| 474 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 475 | # Early exit, the text format does not report packages without CVEs |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 476 | if not patched+unpatched+ignored: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 477 | return |
| 478 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 479 | nvd_link = "https://nvd.nist.gov/vuln/detail/" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 480 | write_string = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 481 | unpatched_cves = [] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 482 | bb.utils.mkdirhier(os.path.dirname(cve_file)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 483 | |
| 484 | for cve in sorted(cve_data): |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 485 | is_patched = cve in patched |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 486 | is_ignored = cve in ignored |
| 487 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 488 | status = "Unpatched" |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 489 | if (is_patched or is_ignored) and not report_all: |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 490 | continue |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 491 | if is_ignored: |
| 492 | status = "Ignored" |
| 493 | elif is_patched: |
| 494 | status = "Patched" |
| 495 | else: |
| 496 | # default value of status is Unpatched |
| 497 | unpatched_cves.append(cve) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 498 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 499 | write_string += "LAYER: %s\n" % layer |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 500 | write_string += "PACKAGE NAME: %s\n" % d.getVar("PN") |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 501 | write_string += "PACKAGE VERSION: %s%s\n" % (d.getVar("EXTENDPE"), d.getVar("PV")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 502 | write_string += "CVE: %s\n" % cve |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 503 | write_string += "CVE STATUS: %s\n" % status |
| 504 | _, detail, description = decode_cve_status(d, cve) |
| 505 | if detail: |
| 506 | write_string += "CVE DETAIL: %s\n" % detail |
| 507 | if description: |
| 508 | write_string += "CVE DESCRIPTION: %s\n" % description |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 509 | write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"] |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 510 | write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"] |
| 511 | write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 512 | write_string += "VECTOR: %s\n" % cve_data[cve]["vector"] |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 513 | write_string += "VECTORSTRING: %s\n" % cve_data[cve]["vectorString"] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 514 | write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve) |
| 515 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 516 | if unpatched_cves and d.getVar("CVE_CHECK_SHOW_WARNINGS") == "1": |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 517 | bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file)) |
| 518 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 519 | with open(cve_file, "w") as f: |
| 520 | bb.note("Writing file %s with CVE information" % cve_file) |
| 521 | f.write(write_string) |
| 522 | |
| 523 | if d.getVar("CVE_CHECK_COPY_FILES") == "1": |
| 524 | deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE") |
| 525 | bb.utils.mkdirhier(os.path.dirname(deploy_file)) |
| 526 | with open(deploy_file, "w") as f: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 527 | f.write(write_string) |
| 528 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 529 | if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1": |
| 530 | cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR") |
| 531 | bb.utils.mkdirhier(cvelogpath) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 532 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 533 | with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f: |
| 534 | f.write("%s" % write_string) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 535 | |
| 536 | def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file): |
| 537 | """ |
| 538 | Write CVE information in the JSON format: to WORKDIR; and to |
| 539 | CVE_CHECK_DIR, if CVE manifest if enabled, write fragment |
| 540 | files that will be assembled at the end in cve_check_write_rootfs_manifest. |
| 541 | """ |
| 542 | |
| 543 | import json |
| 544 | |
| 545 | write_string = json.dumps(output, indent=2) |
| 546 | with open(direct_file, "w") as f: |
| 547 | bb.note("Writing file %s with CVE information" % direct_file) |
| 548 | f.write(write_string) |
| 549 | |
| 550 | if d.getVar("CVE_CHECK_COPY_FILES") == "1": |
| 551 | bb.utils.mkdirhier(os.path.dirname(deploy_file)) |
| 552 | with open(deploy_file, "w") as f: |
| 553 | f.write(write_string) |
| 554 | |
| 555 | if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1": |
| 556 | cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR") |
| 557 | index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH") |
| 558 | bb.utils.mkdirhier(cvelogpath) |
| 559 | fragment_file = os.path.basename(deploy_file) |
| 560 | fragment_path = os.path.join(cvelogpath, fragment_file) |
| 561 | with open(fragment_path, "w") as f: |
| 562 | f.write(write_string) |
| 563 | with open(index_path, "a+") as f: |
| 564 | f.write("%s\n" % fragment_path) |
| 565 | |
| 566 | def cve_write_data_json(d, patched, unpatched, ignored, cve_data, cve_status): |
| 567 | """ |
| 568 | Prepare CVE data for the JSON format, then write it. |
| 569 | """ |
| 570 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 571 | from oe.cve_check import decode_cve_status |
| 572 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 573 | output = {"version":"1", "package": []} |
| 574 | nvd_link = "https://nvd.nist.gov/vuln/detail/" |
| 575 | |
| 576 | fdir_name = d.getVar("FILE_DIRNAME") |
| 577 | layer = fdir_name.split("/")[-3] |
| 578 | |
| 579 | include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split() |
| 580 | exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split() |
| 581 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 582 | report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1" |
| 583 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 584 | if exclude_layers and layer in exclude_layers: |
| 585 | return |
| 586 | |
| 587 | if include_layers and layer not in include_layers: |
| 588 | return |
| 589 | |
| 590 | unpatched_cves = [] |
| 591 | |
| 592 | product_data = [] |
| 593 | for s in cve_status: |
| 594 | p = {"product": s[0], "cvesInRecord": "Yes"} |
| 595 | if s[1] == False: |
| 596 | p["cvesInRecord"] = "No" |
| 597 | product_data.append(p) |
| 598 | |
| 599 | package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV")) |
| 600 | package_data = { |
| 601 | "name" : d.getVar("PN"), |
| 602 | "layer" : layer, |
| 603 | "version" : package_version, |
| 604 | "products": product_data |
| 605 | } |
| 606 | cve_list = [] |
| 607 | |
| 608 | for cve in sorted(cve_data): |
| 609 | is_patched = cve in patched |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 610 | is_ignored = cve in ignored |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 611 | status = "Unpatched" |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 612 | if (is_patched or is_ignored) and not report_all: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 613 | continue |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 614 | if is_ignored: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 615 | status = "Ignored" |
| 616 | elif is_patched: |
| 617 | status = "Patched" |
| 618 | else: |
| 619 | # default value of status is Unpatched |
| 620 | unpatched_cves.append(cve) |
| 621 | |
| 622 | issue_link = "%s%s" % (nvd_link, cve) |
| 623 | |
| 624 | cve_item = { |
| 625 | "id" : cve, |
| 626 | "summary" : cve_data[cve]["summary"], |
| 627 | "scorev2" : cve_data[cve]["scorev2"], |
| 628 | "scorev3" : cve_data[cve]["scorev3"], |
| 629 | "vector" : cve_data[cve]["vector"], |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 630 | "vectorString" : cve_data[cve]["vectorString"], |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 631 | "status" : status, |
| 632 | "link": issue_link |
| 633 | } |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 634 | _, detail, description = decode_cve_status(d, cve) |
| 635 | if detail: |
| 636 | cve_item["detail"] = detail |
| 637 | if description: |
| 638 | cve_item["description"] = description |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 639 | cve_list.append(cve_item) |
| 640 | |
| 641 | package_data["issue"] = cve_list |
| 642 | output["package"].append(package_data) |
| 643 | |
| 644 | direct_file = d.getVar("CVE_CHECK_LOG_JSON") |
| 645 | deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON") |
| 646 | manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON") |
| 647 | |
| 648 | cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file) |
| 649 | |
| 650 | def cve_write_data(d, patched, unpatched, ignored, cve_data, status): |
| 651 | """ |
| 652 | Write CVE data in each enabled format. |
| 653 | """ |
| 654 | |
| 655 | if d.getVar("CVE_CHECK_FORMAT_TEXT") == "1": |
| 656 | cve_write_data_text(d, patched, unpatched, ignored, cve_data) |
| 657 | if d.getVar("CVE_CHECK_FORMAT_JSON") == "1": |
| 658 | cve_write_data_json(d, patched, unpatched, ignored, cve_data, status) |