diff options
| author | Ross Burton <ross.burton@intel.com> | 2019-09-25 12:11:02 +0100 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2019-09-30 16:44:42 +0100 |
| commit | 411624fa506a74eba7f1b1e159bd1a2286aa6686 (patch) | |
| tree | 7ce4de536965ab503c2473b916c8b97b20f273ee /meta/recipes-core | |
| parent | 5be20f94d629b8f5ebb34254b98ab6913e54b4e4 (diff) | |
| download | poky-411624fa506a74eba7f1b1e159bd1a2286aa6686.tar.gz | |
cve-check: backport rewrite from master
As detailed at [1] the XML feeds provided by NIST are being discontinued on
October 9th 2019. As cve-check-tool uses these feeds, cve-check.bbclass will be
inoperable after this date.
To ensure that cve-check continues working, backport the following commits from
master to move away from the unmaintained cve-check-tool to our own Python code
that fetches the JSON:
546d14135c5 cve-update-db: New recipe to update CVE database
bc144b028f6 cve-check: Remove dependency to cve-check-tool-native
7f62a20b32a cve-check: Manage CVE_PRODUCT with more than one name
3bf63bc6084 cve-check: Consider CVE that affects versions with less than operator
c0eabd30d7b cve-update-db: Use std library instead of urllib3
27eb839ee65 cve-check: be idiomatic
09be21f4d17 cve-update-db: Manage proxy if needed.
975793e3825 cve-update-db: do_populate_cve_db depends on do_fetch
0325dd72714 cve-update-db: Catch request.urlopen errors.
4078da92b49 cve-check: Depends on cve-update-db-native
f7676e9a38d cve-update-db: Use NVD CPE data to populate PRODUCTS table
bc0195be1b1 cve-check: Update unpatched CVE matching
c807c2a6409 cve-update-db-native: Skip recipe when cve-check class is not loaded.
07bb8b25e17 cve-check: remove redundant readline CVE whitelisting
5388ed6d137 cve-check-tool: remove
270ac00cb43 cve-check.bbclass: initialize to_append
e6bf9000987 cve-check: allow comparison of Vendor as well as Product
91770338f76 cve-update-db-native: use SQL placeholders instead of format strings
7069302a4cc cve-check: Replace CVE_CHECK_CVE_WHITELIST by CVE_CHECK_WHITELIST
78de2cb39d7 cve-update-db-native: Remove hash column from database.
4b301030cf9 cve-update-db-native: use os.path.join instead of +
f0d822fad2a cve-update-db: actually inherit native
b309840b6aa cve-update-db-native: use executemany() to optimise CPE insertion
bb4e53af33d cve-update-db-native: improve metadata parsing
94227459792 cve-update-db-native: clean up JSON fetching
95438d52b73 cve-update-db-native: fix https proxy issues
1f9a963b9ff glibc: exclude child recipes from CVE scanning
[1] https://nvd.nist.gov/General/News/XML-Vulnerability-Feed-Retirement
(From OE-Core rev: 8c87e78547c598cada1bce92e7b25d85b994e2eb)
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Armin Kuster <akuster808@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/recipes-core')
| -rw-r--r-- | meta/recipes-core/glibc/glibc-locale.inc | 3 | ||||
| -rw-r--r-- | meta/recipes-core/glibc/glibc-mtrace.inc | 3 | ||||
| -rw-r--r-- | meta/recipes-core/glibc/glibc-scripts.inc | 3 | ||||
| -rw-r--r-- | meta/recipes-core/meta/cve-update-db-native.bb | 195 |
4 files changed, 204 insertions, 0 deletions
diff --git a/meta/recipes-core/glibc/glibc-locale.inc b/meta/recipes-core/glibc/glibc-locale.inc index 17f5b781f4..3f77221ec3 100644 --- a/meta/recipes-core/glibc/glibc-locale.inc +++ b/meta/recipes-core/glibc/glibc-locale.inc | |||
| @@ -100,3 +100,6 @@ do_install() { | |||
| 100 | inherit libc-package | 100 | inherit libc-package |
| 101 | 101 | ||
| 102 | BBCLASSEXTEND = "nativesdk" | 102 | BBCLASSEXTEND = "nativesdk" |
| 103 | |||
| 104 | # Don't scan for CVEs as glibc will be scanned | ||
| 105 | CVE_PRODUCT = "" | ||
diff --git a/meta/recipes-core/glibc/glibc-mtrace.inc b/meta/recipes-core/glibc/glibc-mtrace.inc index d703c14bdc..ef9d60ec23 100644 --- a/meta/recipes-core/glibc/glibc-mtrace.inc +++ b/meta/recipes-core/glibc/glibc-mtrace.inc | |||
| @@ -11,3 +11,6 @@ do_install() { | |||
| 11 | install -d -m 0755 ${D}${bindir} | 11 | install -d -m 0755 ${D}${bindir} |
| 12 | install -m 0755 ${SRC}/mtrace ${D}${bindir}/ | 12 | install -m 0755 ${SRC}/mtrace ${D}${bindir}/ |
| 13 | } | 13 | } |
| 14 | |||
| 15 | # Don't scan for CVEs as glibc will be scanned | ||
| 16 | CVE_PRODUCT = "" | ||
diff --git a/meta/recipes-core/glibc/glibc-scripts.inc b/meta/recipes-core/glibc/glibc-scripts.inc index 2a2b41507e..14a14e4512 100644 --- a/meta/recipes-core/glibc/glibc-scripts.inc +++ b/meta/recipes-core/glibc/glibc-scripts.inc | |||
| @@ -18,3 +18,6 @@ do_install() { | |||
| 18 | # sotruss script requires sotruss-lib.so (given by libsotruss package), | 18 | # sotruss script requires sotruss-lib.so (given by libsotruss package), |
| 19 | # to produce trace of the library calls. | 19 | # to produce trace of the library calls. |
| 20 | RDEPENDS_${PN} += "libsotruss" | 20 | RDEPENDS_${PN} += "libsotruss" |
| 21 | |||
| 22 | # Don't scan for CVEs as glibc will be scanned | ||
| 23 | CVE_PRODUCT = "" | ||
diff --git a/meta/recipes-core/meta/cve-update-db-native.bb b/meta/recipes-core/meta/cve-update-db-native.bb new file mode 100644 index 0000000000..2c427a5884 --- /dev/null +++ b/meta/recipes-core/meta/cve-update-db-native.bb | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | SUMMARY = "Updates the NVD CVE database" | ||
| 2 | LICENSE = "MIT" | ||
| 3 | |||
| 4 | INHIBIT_DEFAULT_DEPS = "1" | ||
| 5 | |||
| 6 | inherit native | ||
| 7 | |||
| 8 | deltask do_unpack | ||
| 9 | deltask do_patch | ||
| 10 | deltask do_configure | ||
| 11 | deltask do_compile | ||
| 12 | deltask do_install | ||
| 13 | deltask do_populate_sysroot | ||
| 14 | |||
| 15 | python () { | ||
| 16 | if not d.getVar("CVE_CHECK_DB_FILE"): | ||
| 17 | raise bb.parse.SkipRecipe("Skip recipe when cve-check class is not loaded.") | ||
| 18 | } | ||
| 19 | |||
| 20 | python do_populate_cve_db() { | ||
| 21 | """ | ||
| 22 | Update NVD database with json data feed | ||
| 23 | """ | ||
| 24 | |||
| 25 | import sqlite3, urllib, urllib.parse, shutil, gzip | ||
| 26 | from datetime import date | ||
| 27 | |||
| 28 | BASE_URL = "https://nvd.nist.gov/feeds/json/cve/1.0/nvdcve-1.0-" | ||
| 29 | YEAR_START = 2002 | ||
| 30 | |||
| 31 | db_dir = os.path.join(d.getVar("DL_DIR"), 'CVE_CHECK') | ||
| 32 | db_file = os.path.join(db_dir, 'nvdcve_1.0.db') | ||
| 33 | json_tmpfile = os.path.join(db_dir, 'nvd.json.gz') | ||
| 34 | proxy = d.getVar("https_proxy") | ||
| 35 | |||
| 36 | if proxy: | ||
| 37 | # instantiate an opener but do not install it as the global | ||
| 38 | # opener unless if we're really sure it's applicable for all | ||
| 39 | # urllib requests | ||
| 40 | proxy_handler = urllib.request.ProxyHandler({'https': proxy}) | ||
| 41 | proxy_opener = urllib.request.build_opener(proxy_handler) | ||
| 42 | else: | ||
| 43 | proxy_opener = None | ||
| 44 | |||
| 45 | cve_f = open(os.path.join(d.getVar("TMPDIR"), 'cve_check'), 'a') | ||
| 46 | |||
| 47 | if not os.path.isdir(db_dir): | ||
| 48 | os.mkdir(db_dir) | ||
| 49 | |||
| 50 | # Connect to database | ||
| 51 | conn = sqlite3.connect(db_file) | ||
| 52 | c = conn.cursor() | ||
| 53 | |||
| 54 | initialize_db(c) | ||
| 55 | |||
| 56 | for year in range(YEAR_START, date.today().year + 1): | ||
| 57 | year_url = BASE_URL + str(year) | ||
| 58 | meta_url = year_url + ".meta" | ||
| 59 | json_url = year_url + ".json.gz" | ||
| 60 | |||
| 61 | # Retrieve meta last modified date | ||
| 62 | |||
| 63 | response = None | ||
| 64 | |||
| 65 | if proxy_opener: | ||
| 66 | response = proxy_opener.open(meta_url) | ||
| 67 | else: | ||
| 68 | req = urllib.request.Request(meta_url) | ||
| 69 | response = urllib.request.urlopen(req) | ||
| 70 | |||
| 71 | if response: | ||
| 72 | for l in response.read().decode("utf-8").splitlines(): | ||
| 73 | key, value = l.split(":", 1) | ||
| 74 | if key == "lastModifiedDate": | ||
| 75 | last_modified = value | ||
| 76 | break | ||
| 77 | else: | ||
| 78 | bb.warn("Cannot parse CVE metadata, update failed") | ||
| 79 | return | ||
| 80 | |||
| 81 | # Compare with current db last modified date | ||
| 82 | c.execute("select DATE from META where YEAR = ?", (year,)) | ||
| 83 | meta = c.fetchone() | ||
| 84 | if not meta or meta[0] != last_modified: | ||
| 85 | # Clear products table entries corresponding to current year | ||
| 86 | c.execute("delete from PRODUCTS where ID like ?", ('CVE-%d%%' % year,)) | ||
| 87 | |||
| 88 | # Update db with current year json file | ||
| 89 | try: | ||
| 90 | if proxy_opener: | ||
| 91 | response = proxy_opener.open(json_url) | ||
| 92 | else: | ||
| 93 | req = urllib.request.Request(json_url) | ||
| 94 | response = urllib.request.urlopen(req) | ||
| 95 | |||
| 96 | if response: | ||
| 97 | update_db(c, gzip.decompress(response.read()).decode('utf-8')) | ||
| 98 | c.execute("insert or replace into META values (?, ?)", [year, last_modified]) | ||
| 99 | except urllib.error.URLError as e: | ||
| 100 | cve_f.write('Warning: CVE db update error, CVE data is outdated.\n\n') | ||
| 101 | bb.warn("Cannot parse CVE data (%s), update failed" % e.reason) | ||
| 102 | return | ||
| 103 | |||
| 104 | # Update success, set the date to cve_check file. | ||
| 105 | if year == date.today().year: | ||
| 106 | cve_f.write('CVE database update : %s\n\n' % date.today()) | ||
| 107 | |||
| 108 | cve_f.close() | ||
| 109 | conn.commit() | ||
| 110 | conn.close() | ||
| 111 | } | ||
| 112 | |||
| 113 | def initialize_db(c): | ||
| 114 | c.execute("CREATE TABLE IF NOT EXISTS META (YEAR INTEGER UNIQUE, DATE TEXT)") | ||
| 115 | c.execute("CREATE TABLE IF NOT EXISTS NVD (ID TEXT UNIQUE, SUMMARY TEXT, \ | ||
| 116 | SCOREV2 TEXT, SCOREV3 TEXT, MODIFIED INTEGER, VECTOR TEXT)") | ||
| 117 | c.execute("CREATE TABLE IF NOT EXISTS PRODUCTS (ID TEXT, \ | ||
| 118 | VENDOR TEXT, PRODUCT TEXT, VERSION_START TEXT, OPERATOR_START TEXT, \ | ||
| 119 | VERSION_END TEXT, OPERATOR_END TEXT)") | ||
| 120 | |||
| 121 | def parse_node_and_insert(c, node, cveId): | ||
| 122 | # Parse children node if needed | ||
| 123 | for child in node.get('children', ()): | ||
| 124 | parse_node_and_insert(c, child, cveId) | ||
| 125 | |||
| 126 | def cpe_generator(): | ||
| 127 | for cpe in node.get('cpe_match', ()): | ||
| 128 | if not cpe['vulnerable']: | ||
| 129 | return | ||
| 130 | cpe23 = cpe['cpe23Uri'].split(':') | ||
| 131 | vendor = cpe23[3] | ||
| 132 | product = cpe23[4] | ||
| 133 | version = cpe23[5] | ||
| 134 | |||
| 135 | if version != '*': | ||
| 136 | # Version is defined, this is a '=' match | ||
| 137 | yield [cveId, vendor, product, version, '=', '', ''] | ||
| 138 | else: | ||
| 139 | # Parse start version, end version and operators | ||
| 140 | op_start = '' | ||
| 141 | op_end = '' | ||
| 142 | v_start = '' | ||
| 143 | v_end = '' | ||
| 144 | |||
| 145 | if 'versionStartIncluding' in cpe: | ||
| 146 | op_start = '>=' | ||
| 147 | v_start = cpe['versionStartIncluding'] | ||
| 148 | |||
| 149 | if 'versionStartExcluding' in cpe: | ||
| 150 | op_start = '>' | ||
| 151 | v_start = cpe['versionStartExcluding'] | ||
| 152 | |||
| 153 | if 'versionEndIncluding' in cpe: | ||
| 154 | op_end = '<=' | ||
| 155 | v_end = cpe['versionEndIncluding'] | ||
| 156 | |||
| 157 | if 'versionEndExcluding' in cpe: | ||
| 158 | op_end = '<' | ||
| 159 | v_end = cpe['versionEndExcluding'] | ||
| 160 | |||
| 161 | yield [cveId, vendor, product, v_start, op_start, v_end, op_end] | ||
| 162 | |||
| 163 | c.executemany("insert into PRODUCTS values (?, ?, ?, ?, ?, ?, ?)", cpe_generator()) | ||
| 164 | |||
| 165 | def update_db(c, jsondata): | ||
| 166 | import json | ||
| 167 | root = json.loads(jsondata) | ||
| 168 | |||
| 169 | for elt in root['CVE_Items']: | ||
| 170 | if not elt['impact']: | ||
| 171 | continue | ||
| 172 | |||
| 173 | cveId = elt['cve']['CVE_data_meta']['ID'] | ||
| 174 | cveDesc = elt['cve']['description']['description_data'][0]['value'] | ||
| 175 | date = elt['lastModifiedDate'] | ||
| 176 | accessVector = elt['impact']['baseMetricV2']['cvssV2']['accessVector'] | ||
| 177 | cvssv2 = elt['impact']['baseMetricV2']['cvssV2']['baseScore'] | ||
| 178 | |||
| 179 | try: | ||
| 180 | cvssv3 = elt['impact']['baseMetricV3']['cvssV3']['baseScore'] | ||
| 181 | except: | ||
| 182 | cvssv3 = 0.0 | ||
| 183 | |||
| 184 | c.execute("insert or replace into NVD values (?, ?, ?, ?, ?, ?)", | ||
| 185 | [cveId, cveDesc, cvssv2, cvssv3, date, accessVector]) | ||
| 186 | |||
| 187 | configurations = elt['configurations']['nodes'] | ||
| 188 | for config in configurations: | ||
| 189 | parse_node_and_insert(c, config, cveId) | ||
| 190 | |||
| 191 | |||
| 192 | addtask do_populate_cve_db before do_fetch | ||
| 193 | do_populate_cve_db[nostamp] = "1" | ||
| 194 | |||
| 195 | EXCLUDE_FROM_WORLD = "1" | ||
