Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | # 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 Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 23 | # 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] | 24 | # be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff). |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | CVE_PRODUCT ??= "${BPN}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 26 | CVE_VERSION ??= "${PV}" |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 27 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 29 | CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvdcve_1.1.db" |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 30 | CVE_CHECK_DB_FILE_LOCK ?= "${CVE_CHECK_DB_FILE}.lock" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 32 | CVE_CHECK_LOG ?= "${T}/cve.log" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 33 | CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check" |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 34 | CVE_CHECK_SUMMARY_DIR ?= "${LOG_DIR}/cve" |
| 35 | CVE_CHECK_SUMMARY_FILE_NAME ?= "cve-summary" |
| 36 | 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] | 37 | CVE_CHECK_SUMMARY_FILE_NAME_JSON = "cve-summary.json" |
| 38 | CVE_CHECK_SUMMARY_INDEX_PATH = "${CVE_CHECK_SUMMARY_DIR}/cve-summary-index.txt" |
| 39 | |
| 40 | CVE_CHECK_LOG_JSON ?= "${T}/cve.json" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 41 | |
| 42 | CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 43 | CVE_CHECK_RECIPE_FILE ?= "${CVE_CHECK_DIR}/${PN}" |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 44 | CVE_CHECK_RECIPE_FILE_JSON ?= "${CVE_CHECK_DIR}/${PN}_cve.json" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | CVE_CHECK_MANIFEST ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve" |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 46 | CVE_CHECK_MANIFEST_JSON ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.json" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | CVE_CHECK_COPY_FILES ??= "1" |
| 48 | CVE_CHECK_CREATE_MANIFEST ??= "1" |
| 49 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 50 | CVE_CHECK_REPORT_PATCHED ??= "1" |
| 51 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 52 | # Provide text output |
| 53 | CVE_CHECK_FORMAT_TEXT ??= "1" |
| 54 | |
| 55 | # Provide JSON output |
| 56 | CVE_CHECK_FORMAT_JSON ??= "1" |
| 57 | |
| 58 | # Check for packages without CVEs (no issues or missing product name) |
| 59 | CVE_CHECK_COVERAGE ??= "1" |
| 60 | |
| 61 | # Skip CVE Check for packages (PN) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 62 | CVE_CHECK_SKIP_RECIPE ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 64 | # 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 Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 67 | # |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 68 | # CVE_CHECK_IGNORE = 'CVE-2014-2524 CVE-2018-1234' |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 69 | # |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 70 | CVE_CHECK_IGNORE ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 72 | # Layers to be excluded |
| 73 | CVE_CHECK_LAYER_EXCLUDELIST ??= "" |
| 74 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 75 | # Layers to be included |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 76 | CVE_CHECK_LAYER_INCLUDELIST ??= "" |
| 77 | |
| 78 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 79 | # set to "alphabetical" for version using single alphabetical character as increment release |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 80 | CVE_VERSION_SUFFIX ??= "" |
| 81 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 82 | python 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 Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 95 | if os.path.exists(cve_tmp_file): |
| 96 | shutil.copyfile(cve_tmp_file, cve_summary_file) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 97 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 98 | if cve_summary_file and os.path.exists(cve_summary_file): |
| 99 | cvefile_link = os.path.join(cvelogpath, cve_summary_name) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 100 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 101 | 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 Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | addhandler cve_save_summary_handler |
| 107 | cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted" |
| 108 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | python do_cve_check () { |
| 110 | """ |
| 111 | Check recipe for patched and unpatched CVEs |
| 112 | """ |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 113 | from oe.cve_check import get_patched_cves |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 114 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 115 | if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 116 | try: |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 117 | patched_cves = get_patched_cves(d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 118 | except FileNotFoundError: |
| 119 | bb.fatal("Failure in searching patches") |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 120 | ignored, patched, unpatched, status = check_cves(d, patched_cves) |
| 121 | if patched or unpatched or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | cve_data = get_cve_info(d, patched + unpatched) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 123 | cve_write_data(d, patched, unpatched, ignored, cve_data, status) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 124 | else: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 125 | bb.note("No CVE database found, skipping CVE check") |
| 126 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 129 | addtask cve_check before do_build |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 130 | do_cve_check[depends] = "cve-update-db-native:do_fetch" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 131 | do_cve_check[nostamp] = "1" |
| 132 | |
| 133 | python cve_check_cleanup () { |
| 134 | """ |
| 135 | Delete the file used to gather all the CVE information. |
| 136 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE")) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 138 | bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | addhandler cve_check_cleanup |
| 142 | cve_check_cleanup[eventmask] = "bb.cooker.CookerExit" |
| 143 | |
| 144 | python cve_check_write_rootfs_manifest () { |
| 145 | """ |
| 146 | Create CVE manifest when building an image |
| 147 | """ |
| 148 | |
| 149 | import shutil |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 150 | from oe.cve_check import cve_check_merge_jsons |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 151 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 152 | if d.getVar("CVE_CHECK_COPY_FILES") == "1": |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 153 | deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 154 | if os.path.exists(deploy_file): |
| 155 | bb.utils.remove(deploy_file) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 156 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 159 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 160 | if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 161 | bb.note("Writing rootfs CVE manifest") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 162 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 166 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 167 | bb.utils.mkdirhier(os.path.dirname(manifest_name)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 168 | 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 177 | |
| 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 199 | 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] | 200 | do_rootfs[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] | 201 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 202 | def check_cves(d, patched_cves): |
| 203 | """ |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 204 | Connect to the NVD database and find unpatched cves. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 205 | """ |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 206 | from oe.cve_check import Version |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 207 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 208 | pn = d.getVar("PN") |
| 209 | real_pv = d.getVar("PV") |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 210 | suffix = d.getVar("CVE_VERSION_SUFFIX") |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 211 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 212 | cves_unpatched = [] |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 213 | cves_status = [] |
| 214 | cves_in_recipe = False |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 215 | # CVE_PRODUCT can contain more than one product (eg. curl/libcurl) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 216 | products = d.getVar("CVE_PRODUCT").split() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 217 | # 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] | 218 | if not products: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 219 | return ([], [], [], {}) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 220 | pv = d.getVar("CVE_VERSION").split("+git")[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 221 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 222 | # If the recipe has been skipped/ignored we return empty lists |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 223 | if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split(): |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 224 | bb.note("Recipe has been skipped by cve-check") |
| 225 | return ([], [], [], []) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 226 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 227 | cve_ignore = d.getVar("CVE_CHECK_IGNORE").split() |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 228 | |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 229 | import sqlite3 |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 230 | db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro") |
| 231 | conn = sqlite3.connect(db_file, uri=True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 232 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 233 | # 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] | 234 | for product in products: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 235 | cves_in_product = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 236 | if ":" in product: |
| 237 | vendor, product = product.split(":", 1) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 238 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 239 | vendor = "%" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 240 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 241 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 244 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 245 | 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 Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 248 | patched_cves.add(cve) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 249 | continue |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 250 | elif cve in patched_cves: |
| 251 | bb.note("%s has been patched" % (cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 252 | continue |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 253 | # 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 Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 258 | |
| 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 264 | if (operator_start == '=' and pv == version_start) or version_start == '-': |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 265 | vulnerable = True |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 266 | else: |
| 267 | if operator_start: |
| 268 | try: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 269 | 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 271 | except: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 272 | bb.warn("%s: Failed to compare %s %s %s for %s" % |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 273 | (product, pv, operator_start, version_start, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 274 | vulnerable_start = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 275 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 276 | vulnerable_start = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 277 | |
| 278 | if operator_end: |
| 279 | try: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 280 | 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 282 | except: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 283 | bb.warn("%s: Failed to compare %s %s %s for %s" % |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 284 | (product, pv, operator_end, version_end, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 285 | vulnerable_end = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 286 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 287 | vulnerable_end = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 288 | |
| 289 | if operator_start and operator_end: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 290 | vulnerable = vulnerable_start and vulnerable_end |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 291 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 292 | vulnerable = vulnerable_start or vulnerable_end |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 293 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 294 | if vulnerable: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 295 | bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve)) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 296 | cves_unpatched.append(cve) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 297 | break |
| 298 | |
| 299 | if not vulnerable: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 300 | 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] | 301 | # TODO: not patched but not vulnerable |
| 302 | patched_cves.add(cve) |
| 303 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 304 | 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 Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 308 | conn.close() |
| 309 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 310 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 314 | |
| 315 | def get_cve_info(d, cves): |
| 316 | """ |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 317 | Get CVE information from the database. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 318 | """ |
| 319 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 320 | import sqlite3 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 321 | |
| 322 | cve_data = {} |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 323 | db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro") |
| 324 | conn = sqlite3.connect(db_file, uri=True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 325 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 326 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 336 | return cve_data |
| 337 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 338 | def cve_write_data_text(d, patched, unpatched, ignored, cve_data): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 339 | """ |
| 340 | Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and |
| 341 | CVE manifest if enabled. |
| 342 | """ |
| 343 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 344 | cve_file = d.getVar("CVE_CHECK_LOG") |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 345 | 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 357 | # Early exit, the text format does not report packages without CVEs |
| 358 | if not patched+unpatched: |
| 359 | return |
| 360 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 361 | nvd_link = "https://nvd.nist.gov/vuln/detail/" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 362 | write_string = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 363 | unpatched_cves = [] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 364 | bb.utils.mkdirhier(os.path.dirname(cve_file)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 365 | |
| 366 | for cve in sorted(cve_data): |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 367 | is_patched = cve in patched |
| 368 | if is_patched and (d.getVar("CVE_CHECK_REPORT_PATCHED") != "1"): |
| 369 | continue |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 370 | write_string += "LAYER: %s\n" % layer |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 371 | write_string += "PACKAGE NAME: %s\n" % d.getVar("PN") |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 372 | 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] | 373 | write_string += "CVE: %s\n" % cve |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 374 | if cve in ignored: |
| 375 | write_string += "CVE STATUS: Ignored\n" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 376 | elif is_patched: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 377 | write_string += "CVE STATUS: Patched\n" |
| 378 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 379 | unpatched_cves.append(cve) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 380 | write_string += "CVE STATUS: Unpatched\n" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 381 | write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"] |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 382 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 384 | write_string += "VECTOR: %s\n" % cve_data[cve]["vector"] |
| 385 | write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve) |
| 386 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 387 | if unpatched_cves: |
| 388 | bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file)) |
| 389 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 390 | if write_string: |
| 391 | with open(cve_file, "w") as f: |
| 392 | bb.note("Writing file %s with CVE information" % cve_file) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 393 | f.write(write_string) |
| 394 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 395 | 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 Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 400 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 401 | 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 407 | |
| 408 | def 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 | |
| 438 | def 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 | |
| 511 | def 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) |