pkg-stats 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. #!/usr/bin/env python3
  2. # Copyright (C) 2009 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. import aiohttp
  18. import argparse
  19. import asyncio
  20. import datetime
  21. import fnmatch
  22. import os
  23. from collections import defaultdict
  24. import re
  25. import subprocess
  26. import requests # NVD database download
  27. import json
  28. import ijson
  29. import distutils.version
  30. import time
  31. import gzip
  32. import sys
  33. sys.path.append('utils/')
  34. from getdeveloperlib import parse_developers # noqa: E402
  35. NVD_START_YEAR = 2002
  36. NVD_JSON_VERSION = "1.0"
  37. NVD_BASE_URL = "https://nvd.nist.gov/feeds/json/cve/" + NVD_JSON_VERSION
  38. INFRA_RE = re.compile(r"\$\(eval \$\(([a-z-]*)-package\)\)")
  39. URL_RE = re.compile(r"\s*https?://\S*\s*$")
  40. RM_API_STATUS_ERROR = 1
  41. RM_API_STATUS_FOUND_BY_DISTRO = 2
  42. RM_API_STATUS_FOUND_BY_PATTERN = 3
  43. RM_API_STATUS_NOT_FOUND = 4
  44. CVE_AFFECTS = 1
  45. CVE_DOESNT_AFFECT = 2
  46. CVE_UNKNOWN = 3
  47. class Defconfig:
  48. def __init__(self, name, path):
  49. self.name = name
  50. self.path = path
  51. self.developers = None
  52. def set_developers(self, developers):
  53. """
  54. Fills in the .developers field
  55. """
  56. self.developers = [
  57. developer.name
  58. for developer in developers
  59. if developer.hasfile(self.path)
  60. ]
  61. def get_defconfig_list():
  62. """
  63. Builds the list of Buildroot defconfigs, returning a list of Defconfig
  64. objects.
  65. """
  66. return [
  67. Defconfig(name[:-len('_defconfig')], os.path.join('configs', name))
  68. for name in os.listdir('configs')
  69. if name.endswith('_defconfig')
  70. ]
  71. class Package:
  72. all_licenses = dict()
  73. all_license_files = list()
  74. all_versions = dict()
  75. all_ignored_cves = dict()
  76. # This is the list of all possible checks. Add new checks to this list so
  77. # a tool that post-processeds the json output knows the checks before
  78. # iterating over the packages.
  79. status_checks = ['cve', 'developers', 'hash', 'license',
  80. 'license-files', 'patches', 'pkg-check', 'url', 'version']
  81. def __init__(self, name, path):
  82. self.name = name
  83. self.path = path
  84. self.pkg_path = os.path.dirname(path)
  85. self.infras = None
  86. self.license = None
  87. self.has_license = False
  88. self.has_license_files = False
  89. self.has_hash = False
  90. self.patch_files = []
  91. self.warnings = 0
  92. self.current_version = None
  93. self.url = None
  94. self.url_worker = None
  95. self.cves = list()
  96. self.latest_version = {'status': RM_API_STATUS_ERROR, 'version': None, 'id': None}
  97. self.status = {}
  98. def pkgvar(self):
  99. return self.name.upper().replace("-", "_")
  100. def set_url(self):
  101. """
  102. Fills in the .url field
  103. """
  104. self.status['url'] = ("warning", "no Config.in")
  105. for filename in os.listdir(os.path.dirname(self.path)):
  106. if fnmatch.fnmatch(filename, 'Config.*'):
  107. fp = open(os.path.join(os.path.dirname(self.path), filename), "r")
  108. for config_line in fp:
  109. if URL_RE.match(config_line):
  110. self.url = config_line.strip()
  111. self.status['url'] = ("ok", "found")
  112. fp.close()
  113. return
  114. self.status['url'] = ("error", "missing")
  115. fp.close()
  116. @property
  117. def patch_count(self):
  118. return len(self.patch_files)
  119. @property
  120. def has_valid_infra(self):
  121. try:
  122. if self.infras[0][1] == 'virtual':
  123. return False
  124. except IndexError:
  125. return False
  126. return True
  127. def set_infra(self):
  128. """
  129. Fills in the .infras field
  130. """
  131. self.infras = list()
  132. with open(self.path, 'r') as f:
  133. lines = f.readlines()
  134. for l in lines:
  135. match = INFRA_RE.match(l)
  136. if not match:
  137. continue
  138. infra = match.group(1)
  139. if infra.startswith("host-"):
  140. self.infras.append(("host", infra[5:]))
  141. else:
  142. self.infras.append(("target", infra))
  143. def set_license(self):
  144. """
  145. Fills in the .status['license'] and .status['license-files'] fields
  146. """
  147. if not self.has_valid_infra:
  148. self.status['license'] = ("na", "no valid package infra")
  149. self.status['license-files'] = ("na", "no valid package infra")
  150. return
  151. var = self.pkgvar()
  152. self.status['license'] = ("error", "missing")
  153. self.status['license-files'] = ("error", "missing")
  154. if var in self.all_licenses:
  155. self.license = self.all_licenses[var]
  156. self.status['license'] = ("ok", "found")
  157. if var in self.all_license_files:
  158. self.status['license-files'] = ("ok", "found")
  159. def set_hash_info(self):
  160. """
  161. Fills in the .status['hash'] field
  162. """
  163. if not self.has_valid_infra:
  164. self.status['hash'] = ("na", "no valid package infra")
  165. self.status['hash-license'] = ("na", "no valid package infra")
  166. return
  167. hashpath = self.path.replace(".mk", ".hash")
  168. if os.path.exists(hashpath):
  169. self.status['hash'] = ("ok", "found")
  170. else:
  171. self.status['hash'] = ("error", "missing")
  172. def set_patch_count(self):
  173. """
  174. Fills in the .patch_count, .patch_files and .status['patches'] fields
  175. """
  176. if not self.has_valid_infra:
  177. self.status['patches'] = ("na", "no valid package infra")
  178. return
  179. pkgdir = os.path.dirname(self.path)
  180. for subdir, _, _ in os.walk(pkgdir):
  181. self.patch_files = fnmatch.filter(os.listdir(subdir), '*.patch')
  182. if self.patch_count == 0:
  183. self.status['patches'] = ("ok", "no patches")
  184. elif self.patch_count < 5:
  185. self.status['patches'] = ("warning", "some patches")
  186. else:
  187. self.status['patches'] = ("error", "lots of patches")
  188. def set_current_version(self):
  189. """
  190. Fills in the .current_version field
  191. """
  192. var = self.pkgvar()
  193. if var in self.all_versions:
  194. self.current_version = self.all_versions[var]
  195. def set_check_package_warnings(self):
  196. """
  197. Fills in the .warnings and .status['pkg-check'] fields
  198. """
  199. cmd = ["./utils/check-package"]
  200. pkgdir = os.path.dirname(self.path)
  201. self.status['pkg-check'] = ("error", "Missing")
  202. for root, dirs, files in os.walk(pkgdir):
  203. for f in files:
  204. if f.endswith(".mk") or f.endswith(".hash") or f == "Config.in" or f == "Config.in.host":
  205. cmd.append(os.path.join(root, f))
  206. o = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[1]
  207. lines = o.splitlines()
  208. for line in lines:
  209. m = re.match("^([0-9]*) warnings generated", line.decode())
  210. if m:
  211. self.warnings = int(m.group(1))
  212. if self.warnings == 0:
  213. self.status['pkg-check'] = ("ok", "no warnings")
  214. else:
  215. self.status['pkg-check'] = ("error", "{} warnings".format(self.warnings))
  216. return
  217. def is_cve_ignored(self, cve):
  218. """
  219. Tells if the CVE is ignored by the package
  220. """
  221. return cve in self.all_ignored_cves.get(self.pkgvar(), [])
  222. def set_developers(self, developers):
  223. """
  224. Fills in the .developers and .status['developers'] field
  225. """
  226. self.developers = [
  227. dev.name
  228. for dev in developers
  229. if dev.hasfile(self.path)
  230. ]
  231. if self.developers:
  232. self.status['developers'] = ("ok", "{} developers".format(len(self.developers)))
  233. else:
  234. self.status['developers'] = ("warning", "no developers")
  235. def is_status_ok(self, name):
  236. return self.status[name][0] == 'ok'
  237. def __eq__(self, other):
  238. return self.path == other.path
  239. def __lt__(self, other):
  240. return self.path < other.path
  241. def __str__(self):
  242. return "%s (path='%s', license='%s', license_files='%s', hash='%s', patches=%d)" % \
  243. (self.name, self.path, self.is_status_ok('license'),
  244. self.is_status_ok('license-files'), self.status['hash'], self.patch_count)
  245. class CVE:
  246. """An accessor class for CVE Items in NVD files"""
  247. def __init__(self, nvd_cve):
  248. """Initialize a CVE from its NVD JSON representation"""
  249. self.nvd_cve = nvd_cve
  250. @staticmethod
  251. def download_nvd_year(nvd_path, year):
  252. metaf = "nvdcve-%s-%s.meta" % (NVD_JSON_VERSION, year)
  253. path_metaf = os.path.join(nvd_path, metaf)
  254. jsonf_gz = "nvdcve-%s-%s.json.gz" % (NVD_JSON_VERSION, year)
  255. path_jsonf_gz = os.path.join(nvd_path, jsonf_gz)
  256. # If the database file is less than a day old, we assume the NVD data
  257. # locally available is recent enough.
  258. if os.path.exists(path_jsonf_gz) and os.stat(path_jsonf_gz).st_mtime >= time.time() - 86400:
  259. return path_jsonf_gz
  260. # If not, we download the meta file
  261. url = "%s/%s" % (NVD_BASE_URL, metaf)
  262. print("Getting %s" % url)
  263. page_meta = requests.get(url)
  264. page_meta.raise_for_status()
  265. # If the meta file already existed, we compare the existing
  266. # one with the data newly downloaded. If they are different,
  267. # we need to re-download the database.
  268. # If the database does not exist locally, we need to redownload it in
  269. # any case.
  270. if os.path.exists(path_metaf) and os.path.exists(path_jsonf_gz):
  271. meta_known = open(path_metaf, "r").read()
  272. if page_meta.text == meta_known:
  273. return path_jsonf_gz
  274. # Grab the compressed JSON NVD, and write files to disk
  275. url = "%s/%s" % (NVD_BASE_URL, jsonf_gz)
  276. print("Getting %s" % url)
  277. page_json = requests.get(url)
  278. page_json.raise_for_status()
  279. open(path_jsonf_gz, "wb").write(page_json.content)
  280. open(path_metaf, "w").write(page_meta.text)
  281. return path_jsonf_gz
  282. @classmethod
  283. def read_nvd_dir(cls, nvd_dir):
  284. """
  285. Iterate over all the CVEs contained in NIST Vulnerability Database
  286. feeds since NVD_START_YEAR. If the files are missing or outdated in
  287. nvd_dir, a fresh copy will be downloaded, and kept in .json.gz
  288. """
  289. for year in range(NVD_START_YEAR, datetime.datetime.now().year + 1):
  290. filename = CVE.download_nvd_year(nvd_dir, year)
  291. try:
  292. content = ijson.items(gzip.GzipFile(filename), 'CVE_Items.item')
  293. except: # noqa: E722
  294. print("ERROR: cannot read %s. Please remove the file then rerun this script" % filename)
  295. raise
  296. for cve in content:
  297. yield cls(cve['cve'])
  298. def each_product(self):
  299. """Iterate over each product section of this cve"""
  300. for vendor in self.nvd_cve['affects']['vendor']['vendor_data']:
  301. for product in vendor['product']['product_data']:
  302. yield product
  303. @property
  304. def identifier(self):
  305. """The CVE unique identifier"""
  306. return self.nvd_cve['CVE_data_meta']['ID']
  307. @property
  308. def pkg_names(self):
  309. """The set of package names referred by this CVE definition"""
  310. return set(p['product_name'] for p in self.each_product())
  311. def affects(self, br_pkg):
  312. """
  313. True if the Buildroot Package object passed as argument is affected
  314. by this CVE.
  315. """
  316. if br_pkg.is_cve_ignored(self.identifier):
  317. return CVE_DOESNT_AFFECT
  318. for product in self.each_product():
  319. if product['product_name'] != br_pkg.name:
  320. continue
  321. for v in product['version']['version_data']:
  322. if v["version_affected"] == "=":
  323. if br_pkg.current_version == v["version_value"]:
  324. return CVE_AFFECTS
  325. elif v["version_affected"] == "<=":
  326. pkg_version = distutils.version.LooseVersion(br_pkg.current_version)
  327. if not hasattr(pkg_version, "version"):
  328. print("Cannot parse package '%s' version '%s'" % (br_pkg.name, br_pkg.current_version))
  329. continue
  330. cve_affected_version = distutils.version.LooseVersion(v["version_value"])
  331. if not hasattr(cve_affected_version, "version"):
  332. print("Cannot parse CVE affected version '%s'" % v["version_value"])
  333. continue
  334. try:
  335. affected = pkg_version <= cve_affected_version
  336. except TypeError:
  337. return CVE_UNKNOWN
  338. if affected:
  339. return CVE_AFFECTS
  340. else:
  341. return CVE_DOESNT_AFFECT
  342. else:
  343. print("version_affected: %s" % v['version_affected'])
  344. return CVE_DOESNT_AFFECT
  345. def get_pkglist(npackages, package_list):
  346. """
  347. Builds the list of Buildroot packages, returning a list of Package
  348. objects. Only the .name and .path fields of the Package object are
  349. initialized.
  350. npackages: limit to N packages
  351. package_list: limit to those packages in this list
  352. """
  353. WALK_USEFUL_SUBDIRS = ["boot", "linux", "package", "toolchain"]
  354. WALK_EXCLUDES = ["boot/common.mk",
  355. "linux/linux-ext-.*.mk",
  356. "package/freescale-imx/freescale-imx.mk",
  357. "package/gcc/gcc.mk",
  358. "package/gstreamer/gstreamer.mk",
  359. "package/gstreamer1/gstreamer1.mk",
  360. "package/gtk2-themes/gtk2-themes.mk",
  361. "package/matchbox/matchbox.mk",
  362. "package/opengl/opengl.mk",
  363. "package/qt5/qt5.mk",
  364. "package/x11r7/x11r7.mk",
  365. "package/doc-asciidoc.mk",
  366. "package/pkg-.*.mk",
  367. "package/nvidia-tegra23/nvidia-tegra23.mk",
  368. "toolchain/toolchain-external/pkg-toolchain-external.mk",
  369. "toolchain/toolchain-external/toolchain-external.mk",
  370. "toolchain/toolchain.mk",
  371. "toolchain/helpers.mk",
  372. "toolchain/toolchain-wrapper.mk"]
  373. packages = list()
  374. count = 0
  375. for root, dirs, files in os.walk("."):
  376. rootdir = root.split("/")
  377. if len(rootdir) < 2:
  378. continue
  379. if rootdir[1] not in WALK_USEFUL_SUBDIRS:
  380. continue
  381. for f in files:
  382. if not f.endswith(".mk"):
  383. continue
  384. # Strip ending ".mk"
  385. pkgname = f[:-3]
  386. if package_list and pkgname not in package_list:
  387. continue
  388. pkgpath = os.path.join(root, f)
  389. skip = False
  390. for exclude in WALK_EXCLUDES:
  391. # pkgpath[2:] strips the initial './'
  392. if re.match(exclude, pkgpath[2:]):
  393. skip = True
  394. continue
  395. if skip:
  396. continue
  397. p = Package(pkgname, pkgpath)
  398. packages.append(p)
  399. count += 1
  400. if npackages and count == npackages:
  401. return packages
  402. return packages
  403. def package_init_make_info():
  404. # Fetch all variables at once
  405. variables = subprocess.check_output(["make", "BR2_HAVE_DOT_CONFIG=y", "-s", "printvars",
  406. "VARS=%_LICENSE %_LICENSE_FILES %_VERSION %_IGNORE_CVES"])
  407. variable_list = variables.decode().splitlines()
  408. # We process first the host package VERSION, and then the target
  409. # package VERSION. This means that if a package exists in both
  410. # target and host variants, with different values (eg. version
  411. # numbers (unlikely)), we'll report the target one.
  412. variable_list = [x[5:] for x in variable_list if x.startswith("HOST_")] + \
  413. [x for x in variable_list if not x.startswith("HOST_")]
  414. for l in variable_list:
  415. # Get variable name and value
  416. pkgvar, value = l.split("=")
  417. # Strip the suffix according to the variable
  418. if pkgvar.endswith("_LICENSE"):
  419. # If value is "unknown", no license details available
  420. if value == "unknown":
  421. continue
  422. pkgvar = pkgvar[:-8]
  423. Package.all_licenses[pkgvar] = value
  424. elif pkgvar.endswith("_LICENSE_FILES"):
  425. if pkgvar.endswith("_MANIFEST_LICENSE_FILES"):
  426. continue
  427. pkgvar = pkgvar[:-14]
  428. Package.all_license_files.append(pkgvar)
  429. elif pkgvar.endswith("_VERSION"):
  430. if pkgvar.endswith("_DL_VERSION"):
  431. continue
  432. pkgvar = pkgvar[:-8]
  433. Package.all_versions[pkgvar] = value
  434. elif pkgvar.endswith("_IGNORE_CVES"):
  435. pkgvar = pkgvar[:-12]
  436. Package.all_ignored_cves[pkgvar] = value.split()
  437. check_url_count = 0
  438. async def check_url_status(session, pkg, npkgs, retry=True):
  439. global check_url_count
  440. try:
  441. async with session.get(pkg.url) as resp:
  442. if resp.status >= 400:
  443. pkg.status['url'] = ("error", "invalid {}".format(resp.status))
  444. check_url_count += 1
  445. print("[%04d/%04d] %s" % (check_url_count, npkgs, pkg.name))
  446. return
  447. except (aiohttp.ClientError, asyncio.TimeoutError):
  448. if retry:
  449. return await check_url_status(session, pkg, npkgs, retry=False)
  450. else:
  451. pkg.status['url'] = ("error", "invalid (err)")
  452. check_url_count += 1
  453. print("[%04d/%04d] %s" % (check_url_count, npkgs, pkg.name))
  454. return
  455. pkg.status['url'] = ("ok", "valid")
  456. check_url_count += 1
  457. print("[%04d/%04d] %s" % (check_url_count, npkgs, pkg.name))
  458. async def check_package_urls(packages):
  459. tasks = []
  460. connector = aiohttp.TCPConnector(limit_per_host=5)
  461. async with aiohttp.ClientSession(connector=connector, trust_env=True) as sess:
  462. packages = [p for p in packages if p.status['url'][0] == 'ok']
  463. for pkg in packages:
  464. tasks.append(asyncio.ensure_future(check_url_status(sess, pkg, len(packages))))
  465. await asyncio.wait(tasks)
  466. def check_package_latest_version_set_status(pkg, status, version, identifier):
  467. pkg.latest_version = {
  468. "status": status,
  469. "version": version,
  470. "id": identifier,
  471. }
  472. if pkg.latest_version['status'] == RM_API_STATUS_ERROR:
  473. pkg.status['version'] = ('warning', "Release Monitoring API error")
  474. elif pkg.latest_version['status'] == RM_API_STATUS_NOT_FOUND:
  475. pkg.status['version'] = ('warning', "Package not found on Release Monitoring")
  476. if pkg.latest_version['version'] is None:
  477. pkg.status['version'] = ('warning', "No upstream version available on Release Monitoring")
  478. elif pkg.latest_version['version'] != pkg.current_version:
  479. pkg.status['version'] = ('error', "The newer version {} is available upstream".format(pkg.latest_version['version']))
  480. else:
  481. pkg.status['version'] = ('ok', 'up-to-date')
  482. async def check_package_get_latest_version_by_distro(session, pkg, retry=True):
  483. url = "https://release-monitoring.org//api/project/Buildroot/%s" % pkg.name
  484. try:
  485. async with session.get(url) as resp:
  486. if resp.status != 200:
  487. return False
  488. data = await resp.json()
  489. version = data['version'] if 'version' in data else None
  490. check_package_latest_version_set_status(pkg,
  491. RM_API_STATUS_FOUND_BY_DISTRO,
  492. version,
  493. data['id'])
  494. return True
  495. except (aiohttp.ClientError, asyncio.TimeoutError):
  496. if retry:
  497. return await check_package_get_latest_version_by_distro(session, pkg, retry=False)
  498. else:
  499. return False
  500. async def check_package_get_latest_version_by_guess(session, pkg, retry=True):
  501. url = "https://release-monitoring.org/api/projects/?pattern=%s" % pkg.name
  502. try:
  503. async with session.get(url) as resp:
  504. if resp.status != 200:
  505. return False
  506. data = await resp.json()
  507. # filter projects that have the right name and a version defined
  508. projects = [p for p in data['projects'] if p['name'] == pkg.name and 'version' in p]
  509. projects.sort(key=lambda x: x['id'])
  510. if len(projects) > 0:
  511. check_package_latest_version_set_status(pkg,
  512. RM_API_STATUS_FOUND_BY_DISTRO,
  513. projects[0]['version'],
  514. projects[0]['id'])
  515. return True
  516. except (aiohttp.ClientError, asyncio.TimeoutError):
  517. if retry:
  518. return await check_package_get_latest_version_by_guess(session, pkg, retry=False)
  519. else:
  520. return False
  521. check_latest_count = 0
  522. async def check_package_latest_version_get(session, pkg, npkgs):
  523. global check_latest_count
  524. if await check_package_get_latest_version_by_distro(session, pkg):
  525. check_latest_count += 1
  526. print("[%04d/%04d] %s" % (check_latest_count, npkgs, pkg.name))
  527. return
  528. if await check_package_get_latest_version_by_guess(session, pkg):
  529. check_latest_count += 1
  530. print("[%04d/%04d] %s" % (check_latest_count, npkgs, pkg.name))
  531. return
  532. check_package_latest_version_set_status(pkg,
  533. RM_API_STATUS_NOT_FOUND,
  534. None, None)
  535. check_latest_count += 1
  536. print("[%04d/%04d] %s" % (check_latest_count, npkgs, pkg.name))
  537. async def check_package_latest_version(packages):
  538. """
  539. Fills in the .latest_version field of all Package objects
  540. This field is a dict and has the following keys:
  541. - status: one of RM_API_STATUS_ERROR,
  542. RM_API_STATUS_FOUND_BY_DISTRO, RM_API_STATUS_FOUND_BY_PATTERN,
  543. RM_API_STATUS_NOT_FOUND
  544. - version: string containing the latest version known by
  545. release-monitoring.org for this package
  546. - id: string containing the id of the project corresponding to this
  547. package, as known by release-monitoring.org
  548. """
  549. for pkg in [p for p in packages if not p.has_valid_infra]:
  550. pkg.status['version'] = ("na", "no valid package infra")
  551. tasks = []
  552. connector = aiohttp.TCPConnector(limit_per_host=5)
  553. async with aiohttp.ClientSession(connector=connector, trust_env=True) as sess:
  554. packages = [p for p in packages if p.has_valid_infra]
  555. for pkg in packages:
  556. tasks.append(asyncio.ensure_future(check_package_latest_version_get(sess, pkg, len(packages))))
  557. await asyncio.wait(tasks)
  558. def check_package_cves(nvd_path, packages):
  559. if not os.path.isdir(nvd_path):
  560. os.makedirs(nvd_path)
  561. for cve in CVE.read_nvd_dir(nvd_path):
  562. for pkg_name in cve.pkg_names:
  563. if pkg_name in packages and cve.affects(packages[pkg_name]) == CVE_AFFECTS:
  564. packages[pkg_name].cves.append(cve.identifier)
  565. def calculate_stats(packages):
  566. stats = defaultdict(int)
  567. stats['packages'] = len(packages)
  568. for pkg in packages:
  569. # If packages have multiple infra, take the first one. For the
  570. # vast majority of packages, the target and host infra are the
  571. # same. There are very few packages that use a different infra
  572. # for the host and target variants.
  573. if len(pkg.infras) > 0:
  574. infra = pkg.infras[0][1]
  575. stats["infra-%s" % infra] += 1
  576. else:
  577. stats["infra-unknown"] += 1
  578. if pkg.is_status_ok('license'):
  579. stats["license"] += 1
  580. else:
  581. stats["no-license"] += 1
  582. if pkg.is_status_ok('license-files'):
  583. stats["license-files"] += 1
  584. else:
  585. stats["no-license-files"] += 1
  586. if pkg.is_status_ok('hash'):
  587. stats["hash"] += 1
  588. else:
  589. stats["no-hash"] += 1
  590. if pkg.latest_version['status'] == RM_API_STATUS_FOUND_BY_DISTRO:
  591. stats["rmo-mapping"] += 1
  592. else:
  593. stats["rmo-no-mapping"] += 1
  594. if not pkg.latest_version['version']:
  595. stats["version-unknown"] += 1
  596. elif pkg.latest_version['version'] == pkg.current_version:
  597. stats["version-uptodate"] += 1
  598. else:
  599. stats["version-not-uptodate"] += 1
  600. stats["patches"] += pkg.patch_count
  601. stats["total-cves"] += len(pkg.cves)
  602. if len(pkg.cves) != 0:
  603. stats["pkg-cves"] += 1
  604. return stats
  605. html_header = """
  606. <head>
  607. <script src=\"https://www.kryogenix.org/code/browser/sorttable/sorttable.js\"></script>
  608. <style type=\"text/css\">
  609. table {
  610. width: 100%;
  611. }
  612. td {
  613. border: 1px solid black;
  614. }
  615. td.centered {
  616. text-align: center;
  617. }
  618. td.wrong {
  619. background: #ff9a69;
  620. }
  621. td.correct {
  622. background: #d2ffc4;
  623. }
  624. td.nopatches {
  625. background: #d2ffc4;
  626. }
  627. td.somepatches {
  628. background: #ffd870;
  629. }
  630. td.lotsofpatches {
  631. background: #ff9a69;
  632. }
  633. td.good_url {
  634. background: #d2ffc4;
  635. }
  636. td.missing_url {
  637. background: #ffd870;
  638. }
  639. td.invalid_url {
  640. background: #ff9a69;
  641. }
  642. td.version-good {
  643. background: #d2ffc4;
  644. }
  645. td.version-needs-update {
  646. background: #ff9a69;
  647. }
  648. td.version-unknown {
  649. background: #ffd870;
  650. }
  651. td.version-error {
  652. background: #ccc;
  653. }
  654. </style>
  655. <title>Statistics of Buildroot packages</title>
  656. </head>
  657. <a href=\"#results\">Results</a><br/>
  658. <p id=\"sortable_hint\"></p>
  659. """
  660. html_footer = """
  661. </body>
  662. <script>
  663. if (typeof sorttable === \"object\") {
  664. document.getElementById(\"sortable_hint\").innerHTML =
  665. \"hint: the table can be sorted by clicking the column headers\"
  666. }
  667. </script>
  668. </html>
  669. """
  670. def infra_str(infra_list):
  671. if not infra_list:
  672. return "Unknown"
  673. elif len(infra_list) == 1:
  674. return "<b>%s</b><br/>%s" % (infra_list[0][1], infra_list[0][0])
  675. elif infra_list[0][1] == infra_list[1][1]:
  676. return "<b>%s</b><br/>%s + %s" % \
  677. (infra_list[0][1], infra_list[0][0], infra_list[1][0])
  678. else:
  679. return "<b>%s</b> (%s)<br/><b>%s</b> (%s)" % \
  680. (infra_list[0][1], infra_list[0][0],
  681. infra_list[1][1], infra_list[1][0])
  682. def boolean_str(b):
  683. if b:
  684. return "Yes"
  685. else:
  686. return "No"
  687. def dump_html_pkg(f, pkg):
  688. f.write(" <tr>\n")
  689. f.write(" <td>%s</td>\n" % pkg.path[2:])
  690. # Patch count
  691. td_class = ["centered"]
  692. if pkg.patch_count == 0:
  693. td_class.append("nopatches")
  694. elif pkg.patch_count < 5:
  695. td_class.append("somepatches")
  696. else:
  697. td_class.append("lotsofpatches")
  698. f.write(" <td class=\"%s\">%s</td>\n" %
  699. (" ".join(td_class), str(pkg.patch_count)))
  700. # Infrastructure
  701. infra = infra_str(pkg.infras)
  702. td_class = ["centered"]
  703. if infra == "Unknown":
  704. td_class.append("wrong")
  705. else:
  706. td_class.append("correct")
  707. f.write(" <td class=\"%s\">%s</td>\n" %
  708. (" ".join(td_class), infra_str(pkg.infras)))
  709. # License
  710. td_class = ["centered"]
  711. if pkg.is_status_ok('license'):
  712. td_class.append("correct")
  713. else:
  714. td_class.append("wrong")
  715. f.write(" <td class=\"%s\">%s</td>\n" %
  716. (" ".join(td_class), boolean_str(pkg.is_status_ok('license'))))
  717. # License files
  718. td_class = ["centered"]
  719. if pkg.is_status_ok('license-files'):
  720. td_class.append("correct")
  721. else:
  722. td_class.append("wrong")
  723. f.write(" <td class=\"%s\">%s</td>\n" %
  724. (" ".join(td_class), boolean_str(pkg.is_status_ok('license-files'))))
  725. # Hash
  726. td_class = ["centered"]
  727. if pkg.is_status_ok('hash'):
  728. td_class.append("correct")
  729. else:
  730. td_class.append("wrong")
  731. f.write(" <td class=\"%s\">%s</td>\n" %
  732. (" ".join(td_class), boolean_str(pkg.is_status_ok('hash'))))
  733. # Current version
  734. if len(pkg.current_version) > 20:
  735. current_version = pkg.current_version[:20] + "..."
  736. else:
  737. current_version = pkg.current_version
  738. f.write(" <td class=\"centered\">%s</td>\n" % current_version)
  739. # Latest version
  740. if pkg.latest_version['status'] == RM_API_STATUS_ERROR:
  741. td_class.append("version-error")
  742. if pkg.latest_version['version'] is None:
  743. td_class.append("version-unknown")
  744. elif pkg.latest_version['version'] != pkg.current_version:
  745. td_class.append("version-needs-update")
  746. else:
  747. td_class.append("version-good")
  748. if pkg.latest_version['status'] == RM_API_STATUS_ERROR:
  749. latest_version_text = "<b>Error</b>"
  750. elif pkg.latest_version['status'] == RM_API_STATUS_NOT_FOUND:
  751. latest_version_text = "<b>Not found</b>"
  752. else:
  753. if pkg.latest_version['version'] is None:
  754. latest_version_text = "<b>Found, but no version</b>"
  755. else:
  756. latest_version_text = "<a href=\"https://release-monitoring.org/project/%s\"><b>%s</b></a>" % \
  757. (pkg.latest_version['id'], str(pkg.latest_version['version']))
  758. latest_version_text += "<br/>"
  759. if pkg.latest_version['status'] == RM_API_STATUS_FOUND_BY_DISTRO:
  760. latest_version_text += "found by <a href=\"https://release-monitoring.org/distro/Buildroot/\">distro</a>"
  761. else:
  762. latest_version_text += "found by guess"
  763. f.write(" <td class=\"%s\">%s</td>\n" %
  764. (" ".join(td_class), latest_version_text))
  765. # Warnings
  766. td_class = ["centered"]
  767. if pkg.warnings == 0:
  768. td_class.append("correct")
  769. else:
  770. td_class.append("wrong")
  771. f.write(" <td class=\"%s\">%d</td>\n" %
  772. (" ".join(td_class), pkg.warnings))
  773. # URL status
  774. td_class = ["centered"]
  775. url_str = pkg.status['url'][1]
  776. if pkg.status['url'][0] in ("error", "warning"):
  777. td_class.append("missing_url")
  778. if pkg.status['url'][0] == "error":
  779. td_class.append("invalid_url")
  780. url_str = "<a href=%s>%s</a>" % (pkg.url, pkg.status['url'][1])
  781. else:
  782. td_class.append("good_url")
  783. url_str = "<a href=%s>Link</a>" % pkg.url
  784. f.write(" <td class=\"%s\">%s</td>\n" %
  785. (" ".join(td_class), url_str))
  786. # CVEs
  787. td_class = ["centered"]
  788. if len(pkg.cves) == 0:
  789. td_class.append("correct")
  790. else:
  791. td_class.append("wrong")
  792. f.write(" <td class=\"%s\">\n" % " ".join(td_class))
  793. for cve in pkg.cves:
  794. f.write(" <a href=\"https://security-tracker.debian.org/tracker/%s\">%s<br/>\n" % (cve, cve))
  795. f.write(" </td>\n")
  796. f.write(" </tr>\n")
  797. def dump_html_all_pkgs(f, packages):
  798. f.write("""
  799. <table class=\"sortable\">
  800. <tr>
  801. <td>Package</td>
  802. <td class=\"centered\">Patch count</td>
  803. <td class=\"centered\">Infrastructure</td>
  804. <td class=\"centered\">License</td>
  805. <td class=\"centered\">License files</td>
  806. <td class=\"centered\">Hash file</td>
  807. <td class=\"centered\">Current version</td>
  808. <td class=\"centered\">Latest version</td>
  809. <td class=\"centered\">Warnings</td>
  810. <td class=\"centered\">Upstream URL</td>
  811. <td class=\"centered\">CVEs</td>
  812. </tr>
  813. """)
  814. for pkg in sorted(packages):
  815. dump_html_pkg(f, pkg)
  816. f.write("</table>")
  817. def dump_html_stats(f, stats):
  818. f.write("<a id=\"results\"></a>\n")
  819. f.write("<table>\n")
  820. infras = [infra[6:] for infra in stats.keys() if infra.startswith("infra-")]
  821. for infra in infras:
  822. f.write(" <tr><td>Packages using the <i>%s</i> infrastructure</td><td>%s</td></tr>\n" %
  823. (infra, stats["infra-%s" % infra]))
  824. f.write(" <tr><td>Packages having license information</td><td>%s</td></tr>\n" %
  825. stats["license"])
  826. f.write(" <tr><td>Packages not having license information</td><td>%s</td></tr>\n" %
  827. stats["no-license"])
  828. f.write(" <tr><td>Packages having license files information</td><td>%s</td></tr>\n" %
  829. stats["license-files"])
  830. f.write(" <tr><td>Packages not having license files information</td><td>%s</td></tr>\n" %
  831. stats["no-license-files"])
  832. f.write(" <tr><td>Packages having a hash file</td><td>%s</td></tr>\n" %
  833. stats["hash"])
  834. f.write(" <tr><td>Packages not having a hash file</td><td>%s</td></tr>\n" %
  835. stats["no-hash"])
  836. f.write(" <tr><td>Total number of patches</td><td>%s</td></tr>\n" %
  837. stats["patches"])
  838. f.write("<tr><td>Packages having a mapping on <i>release-monitoring.org</i></td><td>%s</td></tr>\n" %
  839. stats["rmo-mapping"])
  840. f.write("<tr><td>Packages lacking a mapping on <i>release-monitoring.org</i></td><td>%s</td></tr>\n" %
  841. stats["rmo-no-mapping"])
  842. f.write("<tr><td>Packages that are up-to-date</td><td>%s</td></tr>\n" %
  843. stats["version-uptodate"])
  844. f.write("<tr><td>Packages that are not up-to-date</td><td>%s</td></tr>\n" %
  845. stats["version-not-uptodate"])
  846. f.write("<tr><td>Packages with no known upstream version</td><td>%s</td></tr>\n" %
  847. stats["version-unknown"])
  848. f.write("<tr><td>Packages affected by CVEs</td><td>%s</td></tr>\n" %
  849. stats["pkg-cves"])
  850. f.write("<tr><td>Total number of CVEs affecting all packages</td><td>%s</td></tr>\n" %
  851. stats["total-cves"])
  852. f.write("</table>\n")
  853. def dump_html_gen_info(f, date, commit):
  854. # Updated on Mon Feb 19 08:12:08 CET 2018, Git commit aa77030b8f5e41f1c53eb1c1ad664b8c814ba032
  855. f.write("<p><i>Updated on %s, git commit %s</i></p>\n" % (str(date), commit))
  856. def dump_html(packages, stats, date, commit, output):
  857. with open(output, 'w') as f:
  858. f.write(html_header)
  859. dump_html_all_pkgs(f, packages)
  860. dump_html_stats(f, stats)
  861. dump_html_gen_info(f, date, commit)
  862. f.write(html_footer)
  863. def dump_json(packages, defconfigs, stats, date, commit, output):
  864. # Format packages as a dictionnary instead of a list
  865. # Exclude local field that does not contains real date
  866. excluded_fields = ['url_worker', 'name']
  867. pkgs = {
  868. pkg.name: {
  869. k: v
  870. for k, v in pkg.__dict__.items()
  871. if k not in excluded_fields
  872. } for pkg in packages
  873. }
  874. defconfigs = {
  875. d.name: {
  876. k: v
  877. for k, v in d.__dict__.items()
  878. } for d in defconfigs
  879. }
  880. # Aggregate infrastructures into a single dict entry
  881. statistics = {
  882. k: v
  883. for k, v in stats.items()
  884. if not k.startswith('infra-')
  885. }
  886. statistics['infra'] = {k[6:]: v for k, v in stats.items() if k.startswith('infra-')}
  887. # The actual structure to dump, add commit and date to it
  888. final = {'packages': pkgs,
  889. 'stats': statistics,
  890. 'defconfigs': defconfigs,
  891. 'package_status_checks': Package.status_checks,
  892. 'commit': commit,
  893. 'date': str(date)}
  894. with open(output, 'w') as f:
  895. json.dump(final, f, indent=2, separators=(',', ': '))
  896. f.write('\n')
  897. def resolvepath(path):
  898. return os.path.abspath(os.path.expanduser(path))
  899. def parse_args():
  900. parser = argparse.ArgumentParser()
  901. output = parser.add_argument_group('output', 'Output file(s)')
  902. output.add_argument('--html', dest='html', type=resolvepath,
  903. help='HTML output file')
  904. output.add_argument('--json', dest='json', type=resolvepath,
  905. help='JSON output file')
  906. packages = parser.add_mutually_exclusive_group()
  907. packages.add_argument('-n', dest='npackages', type=int, action='store',
  908. help='Number of packages')
  909. packages.add_argument('-p', dest='packages', action='store',
  910. help='List of packages (comma separated)')
  911. parser.add_argument('--nvd-path', dest='nvd_path',
  912. help='Path to the local NVD database', type=resolvepath)
  913. args = parser.parse_args()
  914. if not args.html and not args.json:
  915. parser.error('at least one of --html or --json (or both) is required')
  916. return args
  917. def __main__():
  918. global cvecheck
  919. args = parse_args()
  920. if args.nvd_path:
  921. import cve as cvecheck
  922. if args.packages:
  923. package_list = args.packages.split(",")
  924. else:
  925. package_list = None
  926. date = datetime.datetime.utcnow()
  927. commit = subprocess.check_output(['git', 'rev-parse',
  928. 'HEAD']).splitlines()[0].decode()
  929. print("Build package list ...")
  930. packages = get_pkglist(args.npackages, package_list)
  931. print("Getting developers ...")
  932. developers = parse_developers()
  933. print("Build defconfig list ...")
  934. defconfigs = get_defconfig_list()
  935. for d in defconfigs:
  936. d.set_developers(developers)
  937. print("Getting package make info ...")
  938. package_init_make_info()
  939. print("Getting package details ...")
  940. for pkg in packages:
  941. pkg.set_infra()
  942. pkg.set_license()
  943. pkg.set_hash_info()
  944. pkg.set_patch_count()
  945. pkg.set_check_package_warnings()
  946. pkg.set_current_version()
  947. pkg.set_url()
  948. pkg.set_developers(developers)
  949. print("Checking URL status")
  950. loop = asyncio.get_event_loop()
  951. loop.run_until_complete(check_package_urls(packages))
  952. print("Getting latest versions ...")
  953. loop = asyncio.get_event_loop()
  954. loop.run_until_complete(check_package_latest_version(packages))
  955. if args.nvd_path:
  956. print("Checking packages CVEs")
  957. check_package_cves(args.nvd_path, {p.name: p for p in packages})
  958. print("Calculate stats")
  959. stats = calculate_stats(packages)
  960. if args.html:
  961. print("Write HTML")
  962. dump_html(packages, stats, date, commit, args.html)
  963. if args.json:
  964. print("Write JSON")
  965. dump_json(packages, defconfigs, stats, date, commit, args.json)
  966. __main__()