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