blob: 079f062f79b2fe3f932338660a61f2e059908b8e [file] [log] [blame]
Brad Bishop96ff1982019-08-19 13:50:42 -04001SUMMARY = "Updates the NVD CVE database"
2LICENSE = "MIT"
3
4INHIBIT_DEFAULT_DEPS = "1"
5
6inherit native
7
8deltask do_unpack
9deltask do_patch
10deltask do_configure
11deltask do_compile
12deltask do_install
13deltask do_populate_sysroot
14
Andrew Geissler95ac1b82021-03-31 14:34:31 -050015NVDCVE_URL ?= "https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-"
Andrew Geisslerd5838332022-05-27 11:33:10 -050016# CVE database update interval, in seconds. By default: once a day (24*60*60).
17# Use 0 to force the update
Andrew Geissler78b72792022-06-14 06:47:25 -050018# Use a negative value to skip the update
Andrew Geisslerd5838332022-05-27 11:33:10 -050019CVE_DB_UPDATE_INTERVAL ?= "86400"
Andrew Geissler95ac1b82021-03-31 14:34:31 -050020
Patrick Williams2390b1b2022-11-03 13:47:49 -050021# Timeout for blocking socket operations, such as the connection attempt.
22CVE_SOCKET_TIMEOUT ?= "60"
23
Andrew Geissler517393d2023-01-13 08:55:19 -060024CVE_DB_TEMP_FILE ?= "${CVE_CHECK_DB_DIR}/temp_nvdcve_1.1.db"
25
Brad Bishop96ff1982019-08-19 13:50:42 -040026python () {
Andrew Geisslerc9f78652020-09-18 14:11:35 -050027 if not bb.data.inherits_class("cve-check", d):
Brad Bishop96ff1982019-08-19 13:50:42 -040028 raise bb.parse.SkipRecipe("Skip recipe when cve-check class is not loaded.")
29}
30
Andrew Geisslerc9f78652020-09-18 14:11:35 -050031python do_fetch() {
Brad Bishop96ff1982019-08-19 13:50:42 -040032 """
33 Update NVD database with json data feed
34 """
Brad Bishop6dbb3162019-11-25 09:41:34 -050035 import bb.utils
Andrew Geisslerc9f78652020-09-18 14:11:35 -050036 import bb.progress
Andrew Geissler517393d2023-01-13 08:55:19 -060037 import shutil
Brad Bishop96ff1982019-08-19 13:50:42 -040038
Brad Bishop6dbb3162019-11-25 09:41:34 -050039 bb.utils.export_proxies(d)
40
Brad Bishop6dbb3162019-11-25 09:41:34 -050041 db_file = d.getVar("CVE_CHECK_DB_FILE")
42 db_dir = os.path.dirname(db_file)
Andrew Geissler517393d2023-01-13 08:55:19 -060043 db_tmp_file = d.getVar("CVE_DB_TEMP_FILE")
Andrew Geisslerc9f78652020-09-18 14:11:35 -050044
Andrew Geissler517393d2023-01-13 08:55:19 -060045 cleanup_db_download(db_file, db_tmp_file)
Brad Bishop08902b02019-08-20 09:16:51 -040046
Andrew Geisslerd5838332022-05-27 11:33:10 -050047 # The NVD database changes once a day, so no need to update more frequently
48 # Allow the user to force-update
Brad Bishop1d80a2e2019-11-15 16:35:03 -050049 try:
50 import time
Andrew Geisslerd5838332022-05-27 11:33:10 -050051 update_interval = int(d.getVar("CVE_DB_UPDATE_INTERVAL"))
Andrew Geissler78b72792022-06-14 06:47:25 -050052 if update_interval < 0:
53 bb.note("CVE database update skipped")
54 return
Andrew Geisslerd5838332022-05-27 11:33:10 -050055 if time.time() - os.path.getmtime(db_file) < update_interval:
Andrew Geisslerc9f78652020-09-18 14:11:35 -050056 bb.debug(2, "Recently updated, skipping")
Brad Bishop1d80a2e2019-11-15 16:35:03 -050057 return
Andrew Geisslerd5838332022-05-27 11:33:10 -050058
Brad Bishop1d80a2e2019-11-15 16:35:03 -050059 except OSError:
60 pass
61
Andrew Geisslerc9f78652020-09-18 14:11:35 -050062 bb.utils.mkdirhier(db_dir)
Andrew Geissler517393d2023-01-13 08:55:19 -060063 if os.path.exists(db_file):
64 shutil.copy2(db_file, db_tmp_file)
65
66 if update_db_file(db_tmp_file, d) == True:
67 # Update downloaded correctly, can swap files
68 shutil.move(db_tmp_file, db_file)
69 else:
70 # Update failed, do not modify the database
71 bb.note("CVE database update failed")
72 os.remove(db_tmp_file)
73}
74
75do_fetch[lockfiles] += "${CVE_CHECK_DB_FILE_LOCK}"
76do_fetch[file-checksums] = ""
77do_fetch[vardeps] = ""
78
79def cleanup_db_download(db_file, db_tmp_file):
80 """
81 Cleanup the download space from possible failed downloads
82 """
83
84 # Clean up the updates done on the main file
85 # Remove it only if a journal file exists - it means a complete re-download
86 if os.path.exists("{0}-journal".format(db_file)):
87 # If a journal is present the last update might have been interrupted. In that case,
88 # just wipe any leftovers and force the DB to be recreated.
89 os.remove("{0}-journal".format(db_file))
90
91 if os.path.exists(db_file):
92 os.remove(db_file)
93
94 # Clean-up the temporary file downloads, we can remove both journal
95 # and the temporary database
96 if os.path.exists("{0}-journal".format(db_tmp_file)):
97 # If a journal is present the last update might have been interrupted. In that case,
98 # just wipe any leftovers and force the DB to be recreated.
99 os.remove("{0}-journal".format(db_tmp_file))
100
101 if os.path.exists(db_tmp_file):
102 os.remove(db_tmp_file)
103
104def update_db_file(db_tmp_file, d):
105 """
106 Update the given database file
107 """
108 import bb.utils, bb.progress
109 from datetime import date
110 import urllib, gzip, sqlite3
111
112 YEAR_START = 2002
113 cve_socket_timeout = int(d.getVar("CVE_SOCKET_TIMEOUT"))
Brad Bishop96ff1982019-08-19 13:50:42 -0400114
115 # Connect to database
Andrew Geissler517393d2023-01-13 08:55:19 -0600116 conn = sqlite3.connect(db_tmp_file)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500117 initialize_db(conn)
Brad Bishop96ff1982019-08-19 13:50:42 -0400118
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500119 with bb.progress.ProgressHandler(d) as ph, open(os.path.join(d.getVar("TMPDIR"), 'cve_check'), 'a') as cve_f:
120 total_years = date.today().year + 1 - YEAR_START
121 for i, year in enumerate(range(YEAR_START, date.today().year + 1)):
122 bb.debug(2, "Updating %d" % year)
123 ph.update((float(i + 1) / total_years) * 100)
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500124 year_url = (d.getVar('NVDCVE_URL')) + str(year)
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500125 meta_url = year_url + ".meta"
126 json_url = year_url + ".json.gz"
Brad Bishop96ff1982019-08-19 13:50:42 -0400127
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500128 # Retrieve meta last modified date
Brad Bishop96ff1982019-08-19 13:50:42 -0400129 try:
Patrick Williams2390b1b2022-11-03 13:47:49 -0500130 response = urllib.request.urlopen(meta_url, timeout=cve_socket_timeout)
Brad Bishop96ff1982019-08-19 13:50:42 -0400131 except urllib.error.URLError as e:
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500132 cve_f.write('Warning: CVE db update error, Unable to fetch CVE data.\n\n')
Patrick Williamsb9af8752023-01-30 13:28:01 -0600133 bb.warn("Failed to fetch CVE data (%s)" % e.reason)
Andrew Geissler517393d2023-01-13 08:55:19 -0600134 return False
Brad Bishop96ff1982019-08-19 13:50:42 -0400135
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500136 if response:
137 for l in response.read().decode("utf-8").splitlines():
138 key, value = l.split(":", 1)
139 if key == "lastModifiedDate":
140 last_modified = value
141 break
142 else:
143 bb.warn("Cannot parse CVE metadata, update failed")
Andrew Geissler517393d2023-01-13 08:55:19 -0600144 return False
Brad Bishop96ff1982019-08-19 13:50:42 -0400145
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500146 # Compare with current db last modified date
Patrick Williams92b42cb2022-09-03 06:53:57 -0500147 cursor = conn.execute("select DATE from META where YEAR = ?", (year,))
148 meta = cursor.fetchone()
149 cursor.close()
150
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500151 if not meta or meta[0] != last_modified:
152 bb.debug(2, "Updating entries")
153 # Clear products table entries corresponding to current year
Patrick Williams92b42cb2022-09-03 06:53:57 -0500154 conn.execute("delete from PRODUCTS where ID like ?", ('CVE-%d%%' % year,)).close()
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500155
156 # Update db with current year json file
157 try:
Patrick Williams2390b1b2022-11-03 13:47:49 -0500158 response = urllib.request.urlopen(json_url, timeout=cve_socket_timeout)
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500159 if response:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500160 update_db(conn, gzip.decompress(response.read()).decode('utf-8'))
161 conn.execute("insert or replace into META values (?, ?)", [year, last_modified]).close()
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500162 except urllib.error.URLError as e:
163 cve_f.write('Warning: CVE db update error, CVE data is outdated.\n\n')
164 bb.warn("Cannot parse CVE data (%s), update failed" % e.reason)
Andrew Geissler517393d2023-01-13 08:55:19 -0600165 return False
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500166 else:
167 bb.debug(2, "Already up to date (last modified %s)" % last_modified)
168 # Update success, set the date to cve_check file.
169 if year == date.today().year:
170 cve_f.write('CVE database update : %s\n\n' % date.today())
171
172 conn.commit()
173 conn.close()
Andrew Geissler517393d2023-01-13 08:55:19 -0600174 return True
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500175
Patrick Williams92b42cb2022-09-03 06:53:57 -0500176def initialize_db(conn):
177 with conn:
178 c = conn.cursor()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500179
Patrick Williams92b42cb2022-09-03 06:53:57 -0500180 c.execute("CREATE TABLE IF NOT EXISTS META (YEAR INTEGER UNIQUE, DATE TEXT)")
Brad Bishop6dbb3162019-11-25 09:41:34 -0500181
Patrick Williams92b42cb2022-09-03 06:53:57 -0500182 c.execute("CREATE TABLE IF NOT EXISTS NVD (ID TEXT UNIQUE, SUMMARY TEXT, \
183 SCOREV2 TEXT, SCOREV3 TEXT, MODIFIED INTEGER, VECTOR TEXT)")
Brad Bishop96ff1982019-08-19 13:50:42 -0400184
Patrick Williams92b42cb2022-09-03 06:53:57 -0500185 c.execute("CREATE TABLE IF NOT EXISTS PRODUCTS (ID TEXT, \
186 VENDOR TEXT, PRODUCT TEXT, VERSION_START TEXT, OPERATOR_START TEXT, \
187 VERSION_END TEXT, OPERATOR_END TEXT)")
188 c.execute("CREATE INDEX IF NOT EXISTS PRODUCT_ID_IDX on PRODUCTS(ID);")
189
190 c.close()
191
192def parse_node_and_insert(conn, node, cveId):
Brad Bishop96ff1982019-08-19 13:50:42 -0400193 # Parse children node if needed
194 for child in node.get('children', ()):
Patrick Williams92b42cb2022-09-03 06:53:57 -0500195 parse_node_and_insert(conn, child, cveId)
Brad Bishop96ff1982019-08-19 13:50:42 -0400196
197 def cpe_generator():
198 for cpe in node.get('cpe_match', ()):
199 if not cpe['vulnerable']:
200 return
Andrew Geisslerc926e172021-05-07 16:11:35 -0500201 cpe23 = cpe.get('cpe23Uri')
202 if not cpe23:
203 return
204 cpe23 = cpe23.split(':')
205 if len(cpe23) < 6:
206 return
Brad Bishop96ff1982019-08-19 13:50:42 -0400207 vendor = cpe23[3]
208 product = cpe23[4]
209 version = cpe23[5]
210
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500211 if cpe23[6] == '*' or cpe23[6] == '-':
212 version_suffix = ""
213 else:
214 version_suffix = "_" + cpe23[6]
215
Andrew Geissler82c905d2020-04-13 13:39:40 -0500216 if version != '*' and version != '-':
Brad Bishop96ff1982019-08-19 13:50:42 -0400217 # Version is defined, this is a '=' match
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500218 yield [cveId, vendor, product, version + version_suffix, '=', '', '']
Andrew Geissler82c905d2020-04-13 13:39:40 -0500219 elif version == '-':
220 # no version information is available
221 yield [cveId, vendor, product, version, '', '', '']
Brad Bishop96ff1982019-08-19 13:50:42 -0400222 else:
223 # Parse start version, end version and operators
224 op_start = ''
225 op_end = ''
226 v_start = ''
227 v_end = ''
228
229 if 'versionStartIncluding' in cpe:
230 op_start = '>='
231 v_start = cpe['versionStartIncluding']
232
233 if 'versionStartExcluding' in cpe:
234 op_start = '>'
235 v_start = cpe['versionStartExcluding']
236
237 if 'versionEndIncluding' in cpe:
238 op_end = '<='
239 v_end = cpe['versionEndIncluding']
240
241 if 'versionEndExcluding' in cpe:
242 op_end = '<'
243 v_end = cpe['versionEndExcluding']
244
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600245 if op_start or op_end or v_start or v_end:
246 yield [cveId, vendor, product, v_start, op_start, v_end, op_end]
247 else:
248 # This is no version information, expressed differently.
249 # Save processing by representing as -.
250 yield [cveId, vendor, product, '-', '', '', '']
Brad Bishop96ff1982019-08-19 13:50:42 -0400251
Patrick Williams92b42cb2022-09-03 06:53:57 -0500252 conn.executemany("insert into PRODUCTS values (?, ?, ?, ?, ?, ?, ?)", cpe_generator()).close()
Brad Bishop96ff1982019-08-19 13:50:42 -0400253
Patrick Williams92b42cb2022-09-03 06:53:57 -0500254def update_db(conn, jsondata):
Brad Bishop96ff1982019-08-19 13:50:42 -0400255 import json
256 root = json.loads(jsondata)
257
258 for elt in root['CVE_Items']:
259 if not elt['impact']:
260 continue
261
Andrew Geissler635e0e42020-08-21 15:58:33 -0500262 accessVector = None
Brad Bishop96ff1982019-08-19 13:50:42 -0400263 cveId = elt['cve']['CVE_data_meta']['ID']
264 cveDesc = elt['cve']['description']['description_data'][0]['value']
265 date = elt['lastModifiedDate']
Brad Bishop96ff1982019-08-19 13:50:42 -0400266 try:
Andrew Geissler635e0e42020-08-21 15:58:33 -0500267 accessVector = elt['impact']['baseMetricV2']['cvssV2']['accessVector']
268 cvssv2 = elt['impact']['baseMetricV2']['cvssV2']['baseScore']
269 except KeyError:
270 cvssv2 = 0.0
271 try:
272 accessVector = accessVector or elt['impact']['baseMetricV3']['cvssV3']['attackVector']
Brad Bishop96ff1982019-08-19 13:50:42 -0400273 cvssv3 = elt['impact']['baseMetricV3']['cvssV3']['baseScore']
Andrew Geissler635e0e42020-08-21 15:58:33 -0500274 except KeyError:
275 accessVector = accessVector or "UNKNOWN"
Brad Bishop96ff1982019-08-19 13:50:42 -0400276 cvssv3 = 0.0
277
Patrick Williams92b42cb2022-09-03 06:53:57 -0500278 conn.execute("insert or replace into NVD values (?, ?, ?, ?, ?, ?)",
279 [cveId, cveDesc, cvssv2, cvssv3, date, accessVector]).close()
Brad Bishop96ff1982019-08-19 13:50:42 -0400280
281 configurations = elt['configurations']['nodes']
282 for config in configurations:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500283 parse_node_and_insert(conn, config, cveId)
Brad Bishop96ff1982019-08-19 13:50:42 -0400284
285
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500286do_fetch[nostamp] = "1"
Brad Bishop96ff1982019-08-19 13:50:42 -0400287
288EXCLUDE_FROM_WORLD = "1"