blob: 944243fce9790456054b7243a320463396523ed1 [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
Brad Bishop96ff1982019-08-19 13:50:42 -040021python () {
Andrew Geisslerc9f78652020-09-18 14:11:35 -050022 if not bb.data.inherits_class("cve-check", d):
Brad Bishop96ff1982019-08-19 13:50:42 -040023 raise bb.parse.SkipRecipe("Skip recipe when cve-check class is not loaded.")
24}
25
Andrew Geisslerc9f78652020-09-18 14:11:35 -050026python do_fetch() {
Brad Bishop96ff1982019-08-19 13:50:42 -040027 """
28 Update NVD database with json data feed
29 """
Brad Bishop6dbb3162019-11-25 09:41:34 -050030 import bb.utils
Andrew Geisslerc9f78652020-09-18 14:11:35 -050031 import bb.progress
32 import sqlite3, urllib, urllib.parse, gzip
Brad Bishop96ff1982019-08-19 13:50:42 -040033 from datetime import date
34
Brad Bishop6dbb3162019-11-25 09:41:34 -050035 bb.utils.export_proxies(d)
36
Brad Bishop96ff1982019-08-19 13:50:42 -040037 YEAR_START = 2002
38
Brad Bishop6dbb3162019-11-25 09:41:34 -050039 db_file = d.getVar("CVE_CHECK_DB_FILE")
40 db_dir = os.path.dirname(db_file)
Andrew Geisslerc9f78652020-09-18 14:11:35 -050041
42 if os.path.exists("{0}-journal".format(db_file)):
43 # If a journal is present the last update might have been interrupted. In that case,
44 # just wipe any leftovers and force the DB to be recreated.
45 os.remove("{0}-journal".format(db_file))
46
47 if os.path.exists(db_file):
48 os.remove(db_file)
Brad Bishop08902b02019-08-20 09:16:51 -040049
Andrew Geisslerd5838332022-05-27 11:33:10 -050050 # The NVD database changes once a day, so no need to update more frequently
51 # Allow the user to force-update
Brad Bishop1d80a2e2019-11-15 16:35:03 -050052 try:
53 import time
Andrew Geisslerd5838332022-05-27 11:33:10 -050054 update_interval = int(d.getVar("CVE_DB_UPDATE_INTERVAL"))
Andrew Geissler78b72792022-06-14 06:47:25 -050055 if update_interval < 0:
56 bb.note("CVE database update skipped")
57 return
Andrew Geisslerd5838332022-05-27 11:33:10 -050058 if time.time() - os.path.getmtime(db_file) < update_interval:
Andrew Geisslerc9f78652020-09-18 14:11:35 -050059 bb.debug(2, "Recently updated, skipping")
Brad Bishop1d80a2e2019-11-15 16:35:03 -050060 return
Andrew Geisslerd5838332022-05-27 11:33:10 -050061
Brad Bishop1d80a2e2019-11-15 16:35:03 -050062 except OSError:
63 pass
64
Andrew Geisslerc9f78652020-09-18 14:11:35 -050065 bb.utils.mkdirhier(db_dir)
Brad Bishop96ff1982019-08-19 13:50:42 -040066
67 # Connect to database
68 conn = sqlite3.connect(db_file)
Patrick Williams92b42cb2022-09-03 06:53:57 -050069 initialize_db(conn)
Brad Bishop96ff1982019-08-19 13:50:42 -040070
Andrew Geisslerc9f78652020-09-18 14:11:35 -050071 with bb.progress.ProgressHandler(d) as ph, open(os.path.join(d.getVar("TMPDIR"), 'cve_check'), 'a') as cve_f:
72 total_years = date.today().year + 1 - YEAR_START
73 for i, year in enumerate(range(YEAR_START, date.today().year + 1)):
74 bb.debug(2, "Updating %d" % year)
75 ph.update((float(i + 1) / total_years) * 100)
Andrew Geissler95ac1b82021-03-31 14:34:31 -050076 year_url = (d.getVar('NVDCVE_URL')) + str(year)
Andrew Geisslerc9f78652020-09-18 14:11:35 -050077 meta_url = year_url + ".meta"
78 json_url = year_url + ".json.gz"
Brad Bishop96ff1982019-08-19 13:50:42 -040079
Andrew Geisslerc9f78652020-09-18 14:11:35 -050080 # Retrieve meta last modified date
Brad Bishop96ff1982019-08-19 13:50:42 -040081 try:
Andrew Geisslerc9f78652020-09-18 14:11:35 -050082 response = urllib.request.urlopen(meta_url)
Brad Bishop96ff1982019-08-19 13:50:42 -040083 except urllib.error.URLError as e:
Andrew Geisslerc9f78652020-09-18 14:11:35 -050084 cve_f.write('Warning: CVE db update error, Unable to fetch CVE data.\n\n')
85 bb.warn("Failed to fetch CVE data (%s)" % e.reason)
Brad Bishop96ff1982019-08-19 13:50:42 -040086 return
87
Andrew Geisslerc9f78652020-09-18 14:11:35 -050088 if response:
89 for l in response.read().decode("utf-8").splitlines():
90 key, value = l.split(":", 1)
91 if key == "lastModifiedDate":
92 last_modified = value
93 break
94 else:
95 bb.warn("Cannot parse CVE metadata, update failed")
96 return
Brad Bishop96ff1982019-08-19 13:50:42 -040097
Andrew Geisslerc9f78652020-09-18 14:11:35 -050098 # Compare with current db last modified date
Patrick Williams92b42cb2022-09-03 06:53:57 -050099 cursor = conn.execute("select DATE from META where YEAR = ?", (year,))
100 meta = cursor.fetchone()
101 cursor.close()
102
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500103 if not meta or meta[0] != last_modified:
104 bb.debug(2, "Updating entries")
105 # Clear products table entries corresponding to current year
Patrick Williams92b42cb2022-09-03 06:53:57 -0500106 conn.execute("delete from PRODUCTS where ID like ?", ('CVE-%d%%' % year,)).close()
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500107
108 # Update db with current year json file
109 try:
110 response = urllib.request.urlopen(json_url)
111 if response:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500112 update_db(conn, gzip.decompress(response.read()).decode('utf-8'))
113 conn.execute("insert or replace into META values (?, ?)", [year, last_modified]).close()
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500114 except urllib.error.URLError as e:
115 cve_f.write('Warning: CVE db update error, CVE data is outdated.\n\n')
116 bb.warn("Cannot parse CVE data (%s), update failed" % e.reason)
117 return
118 else:
119 bb.debug(2, "Already up to date (last modified %s)" % last_modified)
120 # Update success, set the date to cve_check file.
121 if year == date.today().year:
122 cve_f.write('CVE database update : %s\n\n' % date.today())
123
124 conn.commit()
125 conn.close()
Brad Bishop96ff1982019-08-19 13:50:42 -0400126}
127
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500128do_fetch[lockfiles] += "${CVE_CHECK_DB_FILE_LOCK}"
129do_fetch[file-checksums] = ""
130do_fetch[vardeps] = ""
131
Patrick Williams92b42cb2022-09-03 06:53:57 -0500132def initialize_db(conn):
133 with conn:
134 c = conn.cursor()
Brad Bishop6dbb3162019-11-25 09:41:34 -0500135
Patrick Williams92b42cb2022-09-03 06:53:57 -0500136 c.execute("CREATE TABLE IF NOT EXISTS META (YEAR INTEGER UNIQUE, DATE TEXT)")
Brad Bishop6dbb3162019-11-25 09:41:34 -0500137
Patrick Williams92b42cb2022-09-03 06:53:57 -0500138 c.execute("CREATE TABLE IF NOT EXISTS NVD (ID TEXT UNIQUE, SUMMARY TEXT, \
139 SCOREV2 TEXT, SCOREV3 TEXT, MODIFIED INTEGER, VECTOR TEXT)")
Brad Bishop96ff1982019-08-19 13:50:42 -0400140
Patrick Williams92b42cb2022-09-03 06:53:57 -0500141 c.execute("CREATE TABLE IF NOT EXISTS PRODUCTS (ID TEXT, \
142 VENDOR TEXT, PRODUCT TEXT, VERSION_START TEXT, OPERATOR_START TEXT, \
143 VERSION_END TEXT, OPERATOR_END TEXT)")
144 c.execute("CREATE INDEX IF NOT EXISTS PRODUCT_ID_IDX on PRODUCTS(ID);")
145
146 c.close()
147
148def parse_node_and_insert(conn, node, cveId):
Brad Bishop96ff1982019-08-19 13:50:42 -0400149 # Parse children node if needed
150 for child in node.get('children', ()):
Patrick Williams92b42cb2022-09-03 06:53:57 -0500151 parse_node_and_insert(conn, child, cveId)
Brad Bishop96ff1982019-08-19 13:50:42 -0400152
153 def cpe_generator():
154 for cpe in node.get('cpe_match', ()):
155 if not cpe['vulnerable']:
156 return
Andrew Geisslerc926e172021-05-07 16:11:35 -0500157 cpe23 = cpe.get('cpe23Uri')
158 if not cpe23:
159 return
160 cpe23 = cpe23.split(':')
161 if len(cpe23) < 6:
162 return
Brad Bishop96ff1982019-08-19 13:50:42 -0400163 vendor = cpe23[3]
164 product = cpe23[4]
165 version = cpe23[5]
166
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500167 if cpe23[6] == '*' or cpe23[6] == '-':
168 version_suffix = ""
169 else:
170 version_suffix = "_" + cpe23[6]
171
Andrew Geissler82c905d2020-04-13 13:39:40 -0500172 if version != '*' and version != '-':
Brad Bishop96ff1982019-08-19 13:50:42 -0400173 # Version is defined, this is a '=' match
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500174 yield [cveId, vendor, product, version + version_suffix, '=', '', '']
Andrew Geissler82c905d2020-04-13 13:39:40 -0500175 elif version == '-':
176 # no version information is available
177 yield [cveId, vendor, product, version, '', '', '']
Brad Bishop96ff1982019-08-19 13:50:42 -0400178 else:
179 # Parse start version, end version and operators
180 op_start = ''
181 op_end = ''
182 v_start = ''
183 v_end = ''
184
185 if 'versionStartIncluding' in cpe:
186 op_start = '>='
187 v_start = cpe['versionStartIncluding']
188
189 if 'versionStartExcluding' in cpe:
190 op_start = '>'
191 v_start = cpe['versionStartExcluding']
192
193 if 'versionEndIncluding' in cpe:
194 op_end = '<='
195 v_end = cpe['versionEndIncluding']
196
197 if 'versionEndExcluding' in cpe:
198 op_end = '<'
199 v_end = cpe['versionEndExcluding']
200
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600201 if op_start or op_end or v_start or v_end:
202 yield [cveId, vendor, product, v_start, op_start, v_end, op_end]
203 else:
204 # This is no version information, expressed differently.
205 # Save processing by representing as -.
206 yield [cveId, vendor, product, '-', '', '', '']
Brad Bishop96ff1982019-08-19 13:50:42 -0400207
Patrick Williams92b42cb2022-09-03 06:53:57 -0500208 conn.executemany("insert into PRODUCTS values (?, ?, ?, ?, ?, ?, ?)", cpe_generator()).close()
Brad Bishop96ff1982019-08-19 13:50:42 -0400209
Patrick Williams92b42cb2022-09-03 06:53:57 -0500210def update_db(conn, jsondata):
Brad Bishop96ff1982019-08-19 13:50:42 -0400211 import json
212 root = json.loads(jsondata)
213
214 for elt in root['CVE_Items']:
215 if not elt['impact']:
216 continue
217
Andrew Geissler635e0e42020-08-21 15:58:33 -0500218 accessVector = None
Brad Bishop96ff1982019-08-19 13:50:42 -0400219 cveId = elt['cve']['CVE_data_meta']['ID']
220 cveDesc = elt['cve']['description']['description_data'][0]['value']
221 date = elt['lastModifiedDate']
Brad Bishop96ff1982019-08-19 13:50:42 -0400222 try:
Andrew Geissler635e0e42020-08-21 15:58:33 -0500223 accessVector = elt['impact']['baseMetricV2']['cvssV2']['accessVector']
224 cvssv2 = elt['impact']['baseMetricV2']['cvssV2']['baseScore']
225 except KeyError:
226 cvssv2 = 0.0
227 try:
228 accessVector = accessVector or elt['impact']['baseMetricV3']['cvssV3']['attackVector']
Brad Bishop96ff1982019-08-19 13:50:42 -0400229 cvssv3 = elt['impact']['baseMetricV3']['cvssV3']['baseScore']
Andrew Geissler635e0e42020-08-21 15:58:33 -0500230 except KeyError:
231 accessVector = accessVector or "UNKNOWN"
Brad Bishop96ff1982019-08-19 13:50:42 -0400232 cvssv3 = 0.0
233
Patrick Williams92b42cb2022-09-03 06:53:57 -0500234 conn.execute("insert or replace into NVD values (?, ?, ?, ?, ?, ?)",
235 [cveId, cveDesc, cvssv2, cvssv3, date, accessVector]).close()
Brad Bishop96ff1982019-08-19 13:50:42 -0400236
237 configurations = elt['configurations']['nodes']
238 for config in configurations:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500239 parse_node_and_insert(conn, config, cveId)
Brad Bishop96ff1982019-08-19 13:50:42 -0400240
241
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500242do_fetch[nostamp] = "1"
Brad Bishop96ff1982019-08-19 13:50:42 -0400243
244EXCLUDE_FROM_WORLD = "1"