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