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 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 23 | # The product name that the CVE database uses. Defaults to BPN, but may need to |
| 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}" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | |
| 38 | CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 39 | CVE_CHECK_RECIPE_FILE ?= "${CVE_CHECK_DIR}/${PN}" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 40 | CVE_CHECK_MANIFEST ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve" |
| 41 | CVE_CHECK_COPY_FILES ??= "1" |
| 42 | CVE_CHECK_CREATE_MANIFEST ??= "1" |
| 43 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 44 | CVE_CHECK_REPORT_PATCHED ??= "1" |
| 45 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 46 | # Whitelist for packages (PN) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 47 | CVE_CHECK_PN_WHITELIST ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 48 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 49 | # Whitelist for CVE. If a CVE is found, then it is considered patched. |
| 50 | # The value is a string containing space separated CVE values: |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 51 | # |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 52 | # CVE_CHECK_WHITELIST = 'CVE-2014-2524 CVE-2018-1234' |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 53 | # |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 54 | CVE_CHECK_WHITELIST ?= "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 55 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 56 | python cve_save_summary_handler () { |
| 57 | import shutil |
| 58 | import datetime |
| 59 | |
| 60 | cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE") |
| 61 | |
| 62 | cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME") |
| 63 | cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR") |
| 64 | bb.utils.mkdirhier(cvelogpath) |
| 65 | |
| 66 | timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
| 67 | cve_summary_file = os.path.join(cvelogpath, "%s-%s.txt" % (cve_summary_name, timestamp)) |
| 68 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 69 | if os.path.exists(cve_tmp_file): |
| 70 | shutil.copyfile(cve_tmp_file, cve_summary_file) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 71 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 72 | if cve_summary_file and os.path.exists(cve_summary_file): |
| 73 | cvefile_link = os.path.join(cvelogpath, cve_summary_name) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 74 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 75 | if os.path.exists(os.path.realpath(cvefile_link)): |
| 76 | os.remove(cvefile_link) |
| 77 | os.symlink(os.path.basename(cve_summary_file), cvefile_link) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | addhandler cve_save_summary_handler |
| 81 | cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted" |
| 82 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 83 | python do_cve_check () { |
| 84 | """ |
| 85 | Check recipe for patched and unpatched CVEs |
| 86 | """ |
| 87 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 88 | if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 89 | try: |
| 90 | patched_cves = get_patches_cves(d) |
| 91 | except FileNotFoundError: |
| 92 | bb.fatal("Failure in searching patches") |
| 93 | whitelisted, patched, unpatched = check_cves(d, patched_cves) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 94 | if patched or unpatched: |
| 95 | cve_data = get_cve_info(d, patched + unpatched) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 96 | cve_write_data(d, patched, unpatched, whitelisted, cve_data) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 97 | else: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 98 | bb.note("No CVE database found, skipping CVE check") |
| 99 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | } |
| 101 | |
Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 102 | addtask cve_check before do_build after do_fetch |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 103 | do_cve_check[depends] = "cve-update-db-native:do_fetch" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 104 | do_cve_check[nostamp] = "1" |
| 105 | |
| 106 | python cve_check_cleanup () { |
| 107 | """ |
| 108 | Delete the file used to gather all the CVE information. |
| 109 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | addhandler cve_check_cleanup |
| 114 | cve_check_cleanup[eventmask] = "bb.cooker.CookerExit" |
| 115 | |
| 116 | python cve_check_write_rootfs_manifest () { |
| 117 | """ |
| 118 | Create CVE manifest when building an image |
| 119 | """ |
| 120 | |
| 121 | import shutil |
| 122 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 123 | if d.getVar("CVE_CHECK_COPY_FILES") == "1": |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 124 | deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 125 | if os.path.exists(deploy_file): |
| 126 | bb.utils.remove(deploy_file) |
| 127 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 128 | if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 129 | bb.note("Writing rootfs CVE manifest") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 130 | deploy_dir = d.getVar("DEPLOY_DIR_IMAGE") |
| 131 | link_name = d.getVar("IMAGE_LINK_NAME") |
| 132 | manifest_name = d.getVar("CVE_CHECK_MANIFEST") |
| 133 | cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 134 | |
| 135 | shutil.copyfile(cve_tmp_file, manifest_name) |
| 136 | |
| 137 | if manifest_name and os.path.exists(manifest_name): |
| 138 | manifest_link = os.path.join(deploy_dir, "%s.cve" % link_name) |
| 139 | # If we already have another manifest, update symlinks |
| 140 | if os.path.exists(os.path.realpath(manifest_link)): |
| 141 | os.remove(manifest_link) |
| 142 | os.symlink(os.path.basename(manifest_name), manifest_link) |
| 143 | bb.plain("Image CVE report stored in: %s" % manifest_name) |
| 144 | } |
| 145 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 146 | 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] | 147 | 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] | 148 | |
| 149 | def get_patches_cves(d): |
| 150 | """ |
| 151 | Get patches that solve CVEs using the "CVE: " tag. |
| 152 | """ |
| 153 | |
| 154 | import re |
| 155 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 156 | pn = d.getVar("PN") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 157 | cve_match = re.compile("CVE:( CVE\-\d{4}\-\d+)+") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 158 | |
| 159 | # Matches last CVE-1234-211432 in the file name, also if written |
| 160 | # with small letters. Not supporting multiple CVE id's in a single |
| 161 | # file name. |
| 162 | cve_file_name_match = re.compile(".*([Cc][Vv][Ee]\-\d{4}\-\d+)") |
| 163 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 164 | patched_cves = set() |
| 165 | bb.debug(2, "Looking for patches that solves CVEs for %s" % pn) |
| 166 | for url in src_patches(d): |
| 167 | patch_file = bb.fetch.decodeurl(url)[2] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 168 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 169 | if not os.path.isfile(patch_file): |
| 170 | bb.error("File Not found: %s" % patch_file) |
| 171 | raise FileNotFoundError |
| 172 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 173 | # Check patch file name for CVE ID |
| 174 | fname_match = cve_file_name_match.search(patch_file) |
| 175 | if fname_match: |
| 176 | cve = fname_match.group(1).upper() |
| 177 | patched_cves.add(cve) |
| 178 | bb.debug(2, "Found CVE %s from patch file name %s" % (cve, patch_file)) |
| 179 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 180 | with open(patch_file, "r", encoding="utf-8") as f: |
| 181 | try: |
| 182 | patch_text = f.read() |
| 183 | except UnicodeDecodeError: |
| 184 | bb.debug(1, "Failed to read patch %s using UTF-8 encoding" |
| 185 | " trying with iso8859-1" % patch_file) |
| 186 | f.close() |
| 187 | with open(patch_file, "r", encoding="iso8859-1") as f: |
| 188 | patch_text = f.read() |
| 189 | |
Brad Bishop | bba38f3 | 2018-08-23 16:11:46 +0800 | [diff] [blame] | 190 | # Search for one or more "CVE: " lines |
| 191 | text_match = False |
| 192 | for match in cve_match.finditer(patch_text): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 193 | # Get only the CVEs without the "CVE: " tag |
| 194 | cves = patch_text[match.start()+5:match.end()] |
| 195 | for cve in cves.split(): |
| 196 | bb.debug(2, "Patch %s solves %s" % (patch_file, cve)) |
| 197 | patched_cves.add(cve) |
Brad Bishop | bba38f3 | 2018-08-23 16:11:46 +0800 | [diff] [blame] | 198 | text_match = True |
| 199 | |
| 200 | if not fname_match and not text_match: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 201 | bb.debug(2, "Patch %s doesn't solve CVEs" % patch_file) |
| 202 | |
| 203 | return patched_cves |
| 204 | |
| 205 | def check_cves(d, patched_cves): |
| 206 | """ |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 207 | Connect to the NVD database and find unpatched cves. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 208 | """ |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 209 | from distutils.version import LooseVersion |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 210 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 211 | cves_unpatched = [] |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 212 | # CVE_PRODUCT can contain more than one product (eg. curl/libcurl) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 213 | products = d.getVar("CVE_PRODUCT").split() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 214 | # 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] | 215 | if not products: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 216 | return ([], [], []) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 217 | pv = d.getVar("CVE_VERSION").split("+git")[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 218 | |
| 219 | # If the recipe has been whitlisted we return empty lists |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 220 | if d.getVar("PN") in d.getVar("CVE_CHECK_PN_WHITELIST").split(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 221 | bb.note("Recipe has been whitelisted, skipping check") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 222 | return ([], [], []) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 223 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 224 | old_cve_whitelist = d.getVar("CVE_CHECK_CVE_WHITELIST") |
| 225 | if old_cve_whitelist: |
| 226 | bb.warn("CVE_CHECK_CVE_WHITELIST is deprecated, please use CVE_CHECK_WHITELIST.") |
| 227 | cve_whitelist = d.getVar("CVE_CHECK_WHITELIST").split() |
| 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: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 235 | if ":" in product: |
| 236 | vendor, product = product.split(":", 1) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 237 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 238 | vendor = "%" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 239 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 240 | # Find all relevant CVE IDs. |
| 241 | for cverow in conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor)): |
| 242 | cve = cverow[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 243 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 244 | if cve in cve_whitelist: |
| 245 | bb.note("%s-%s has been whitelisted for %s" % (product, pv, cve)) |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 246 | # TODO: this should be in the report as 'whitelisted' |
| 247 | patched_cves.add(cve) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 248 | continue |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 249 | elif cve in patched_cves: |
| 250 | bb.note("%s has been patched" % (cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 251 | continue |
| 252 | |
| 253 | vulnerable = False |
| 254 | for row in conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor)): |
| 255 | (_, _, _, version_start, operator_start, version_end, operator_end) = row |
| 256 | #bb.debug(2, "Evaluating row " + str(row)) |
| 257 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 258 | if (operator_start == '=' and pv == version_start) or version_start == '-': |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 259 | vulnerable = True |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 260 | else: |
| 261 | if operator_start: |
| 262 | try: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 263 | vulnerable_start = (operator_start == '>=' and LooseVersion(pv) >= LooseVersion(version_start)) |
| 264 | vulnerable_start |= (operator_start == '>' and LooseVersion(pv) > LooseVersion(version_start)) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 265 | except: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 266 | bb.warn("%s: Failed to compare %s %s %s for %s" % |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 267 | (product, pv, operator_start, version_start, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 268 | vulnerable_start = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 269 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 270 | vulnerable_start = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 271 | |
| 272 | if operator_end: |
| 273 | try: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 274 | vulnerable_end = (operator_end == '<=' and LooseVersion(pv) <= LooseVersion(version_end)) |
| 275 | vulnerable_end |= (operator_end == '<' and LooseVersion(pv) < LooseVersion(version_end)) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 276 | except: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 277 | bb.warn("%s: Failed to compare %s %s %s for %s" % |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 278 | (product, pv, operator_end, version_end, cve)) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 279 | vulnerable_end = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 280 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 281 | vulnerable_end = False |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 282 | |
| 283 | if operator_start and operator_end: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 284 | vulnerable = vulnerable_start and vulnerable_end |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 285 | else: |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 286 | vulnerable = vulnerable_start or vulnerable_end |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 287 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 288 | if vulnerable: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 289 | bb.note("%s-%s is vulnerable to %s" % (product, pv, cve)) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 290 | cves_unpatched.append(cve) |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 291 | break |
| 292 | |
| 293 | if not vulnerable: |
| 294 | bb.note("%s-%s is not vulnerable to %s" % (product, pv, cve)) |
| 295 | # TODO: not patched but not vulnerable |
| 296 | patched_cves.add(cve) |
| 297 | |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 298 | conn.close() |
| 299 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 300 | return (list(cve_whitelist), list(patched_cves), cves_unpatched) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 301 | |
| 302 | def get_cve_info(d, cves): |
| 303 | """ |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 304 | Get CVE information from the database. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 305 | """ |
| 306 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 307 | import sqlite3 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 308 | |
| 309 | cve_data = {} |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 310 | conn = sqlite3.connect(d.getVar("CVE_CHECK_DB_FILE")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 311 | |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 312 | for cve in cves: |
| 313 | for row in conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,)): |
| 314 | cve_data[row[0]] = {} |
| 315 | cve_data[row[0]]["summary"] = row[1] |
| 316 | cve_data[row[0]]["scorev2"] = row[2] |
| 317 | cve_data[row[0]]["scorev3"] = row[3] |
| 318 | cve_data[row[0]]["modified"] = row[4] |
| 319 | cve_data[row[0]]["vector"] = row[5] |
| 320 | |
| 321 | conn.close() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 322 | return cve_data |
| 323 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 324 | def cve_write_data(d, patched, unpatched, whitelisted, cve_data): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 325 | """ |
| 326 | Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and |
| 327 | CVE manifest if enabled. |
| 328 | """ |
| 329 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 330 | cve_file = d.getVar("CVE_CHECK_LOG") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 331 | nvd_link = "https://web.nvd.nist.gov/view/vuln/detail?vulnId=" |
| 332 | write_string = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 333 | unpatched_cves = [] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 334 | bb.utils.mkdirhier(os.path.dirname(cve_file)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 335 | |
| 336 | for cve in sorted(cve_data): |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 337 | is_patched = cve in patched |
| 338 | if is_patched and (d.getVar("CVE_CHECK_REPORT_PATCHED") != "1"): |
| 339 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 340 | write_string += "PACKAGE NAME: %s\n" % d.getVar("PN") |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 341 | 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] | 342 | write_string += "CVE: %s\n" % cve |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 343 | if cve in whitelisted: |
| 344 | write_string += "CVE STATUS: Whitelisted\n" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 345 | elif is_patched: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 346 | write_string += "CVE STATUS: Patched\n" |
| 347 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 348 | unpatched_cves.append(cve) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 349 | write_string += "CVE STATUS: Unpatched\n" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 350 | write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"] |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 351 | write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["scorev2"] |
| 352 | write_string += "CVSS v3 BASE SCORE: %s\n" % cve_data[cve]["scorev3"] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 353 | write_string += "VECTOR: %s\n" % cve_data[cve]["vector"] |
| 354 | write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve) |
| 355 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 356 | if unpatched_cves: |
| 357 | bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file)) |
| 358 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 359 | if write_string: |
| 360 | with open(cve_file, "w") as f: |
| 361 | bb.note("Writing file %s with CVE information" % cve_file) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 362 | f.write(write_string) |
| 363 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 364 | if d.getVar("CVE_CHECK_COPY_FILES") == "1": |
| 365 | deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE") |
| 366 | bb.utils.mkdirhier(os.path.dirname(deploy_file)) |
| 367 | with open(deploy_file, "w") as f: |
| 368 | f.write(write_string) |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 369 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 370 | if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1": |
| 371 | cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR") |
| 372 | bb.utils.mkdirhier(cvelogpath) |
| 373 | |
| 374 | with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f: |
| 375 | f.write("%s" % write_string) |