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