pkg-stats 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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 json
  27. import sys
  28. import time
  29. import gzip
  30. import xml.etree.ElementTree
  31. import requests
  32. brpath = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
  33. sys.path.append(os.path.join(brpath, "utils"))
  34. from getdeveloperlib import parse_developers # noqa: E402
  35. from cpedb import CPEDB_URL # noqa: E402
  36. INFRA_RE = re.compile(r"\$\(eval \$\(([a-z-]*)-package\)\)")
  37. URL_RE = re.compile(r"\s*https?://\S*\s*$")
  38. RM_API_STATUS_ERROR = 1
  39. RM_API_STATUS_FOUND_BY_DISTRO = 2
  40. RM_API_STATUS_FOUND_BY_PATTERN = 3
  41. RM_API_STATUS_NOT_FOUND = 4
  42. class Defconfig:
  43. def __init__(self, name, path):
  44. self.name = name
  45. self.path = path
  46. self.developers = None
  47. def set_developers(self, developers):
  48. """
  49. Fills in the .developers field
  50. """
  51. self.developers = [
  52. developer.name
  53. for developer in developers
  54. if developer.hasfile(self.path)
  55. ]
  56. def get_defconfig_list():
  57. """
  58. Builds the list of Buildroot defconfigs, returning a list of Defconfig
  59. objects.
  60. """
  61. return [
  62. Defconfig(name[:-len('_defconfig')], os.path.join('configs', name))
  63. for name in os.listdir(os.path.join(brpath, 'configs'))
  64. if name.endswith('_defconfig')
  65. ]
  66. class Package:
  67. all_licenses = dict()
  68. all_license_files = list()
  69. all_versions = dict()
  70. all_ignored_cves = dict()
  71. all_cpeids = dict()
  72. # This is the list of all possible checks. Add new checks to this list so
  73. # a tool that post-processeds the json output knows the checks before
  74. # iterating over the packages.
  75. status_checks = ['cve', 'developers', 'hash', 'license',
  76. 'license-files', 'patches', 'pkg-check', 'url', 'version']
  77. def __init__(self, name, path):
  78. self.name = name
  79. self.path = path
  80. self.pkg_path = os.path.dirname(path)
  81. self.infras = None
  82. self.license = None
  83. self.has_license = False
  84. self.has_license_files = False
  85. self.has_hash = False
  86. self.patch_files = []
  87. self.warnings = 0
  88. self.current_version = None
  89. self.url = None
  90. self.url_worker = None
  91. self.cpeid = None
  92. self.cves = list()
  93. self.ignored_cves = list()
  94. self.unsure_cves = list()
  95. self.latest_version = {'status': RM_API_STATUS_ERROR, 'version': None, 'id': None}
  96. self.status = {}
  97. def pkgvar(self):
  98. return self.name.upper().replace("-", "_")
  99. def set_url(self):
  100. """
  101. Fills in the .url field
  102. """
  103. self.status['url'] = ("warning", "no Config.in")
  104. pkgdir = os.path.dirname(os.path.join(brpath, self.path))
  105. for filename in os.listdir(pkgdir):
  106. if fnmatch.fnmatch(filename, 'Config.*'):
  107. fp = open(os.path.join(pkgdir, 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. if self.infras is None:
  122. return False
  123. return len(self.infras) > 0
  124. @property
  125. def is_actual_package(self):
  126. try:
  127. if not self.has_valid_infra:
  128. return False
  129. if self.infras[0][1] == 'virtual':
  130. return False
  131. except IndexError:
  132. return False
  133. return True
  134. def set_infra(self):
  135. """
  136. Fills in the .infras field
  137. """
  138. self.infras = list()
  139. with open(os.path.join(brpath, self.path), 'r') as f:
  140. lines = f.readlines()
  141. for line in lines:
  142. match = INFRA_RE.match(line)
  143. if not match:
  144. continue
  145. infra = match.group(1)
  146. if infra.startswith("host-"):
  147. self.infras.append(("host", infra[5:]))
  148. else:
  149. self.infras.append(("target", infra))
  150. def set_license(self):
  151. """
  152. Fills in the .status['license'] and .status['license-files'] fields
  153. """
  154. if not self.is_actual_package:
  155. self.status['license'] = ("na", "no valid package infra")
  156. self.status['license-files'] = ("na", "no valid package infra")
  157. return
  158. var = self.pkgvar()
  159. self.status['license'] = ("error", "missing")
  160. self.status['license-files'] = ("error", "missing")
  161. if var in self.all_licenses:
  162. self.license = self.all_licenses[var]
  163. self.status['license'] = ("ok", "found")
  164. if var in self.all_license_files:
  165. self.status['license-files'] = ("ok", "found")
  166. def set_hash_info(self):
  167. """
  168. Fills in the .status['hash'] field
  169. """
  170. if not self.is_actual_package:
  171. self.status['hash'] = ("na", "no valid package infra")
  172. self.status['hash-license'] = ("na", "no valid package infra")
  173. return
  174. hashpath = self.path.replace(".mk", ".hash")
  175. if os.path.exists(os.path.join(brpath, hashpath)):
  176. self.status['hash'] = ("ok", "found")
  177. else:
  178. self.status['hash'] = ("error", "missing")
  179. def set_patch_count(self):
  180. """
  181. Fills in the .patch_count, .patch_files and .status['patches'] fields
  182. """
  183. if not self.is_actual_package:
  184. self.status['patches'] = ("na", "no valid package infra")
  185. return
  186. pkgdir = os.path.dirname(os.path.join(brpath, self.path))
  187. for subdir, _, _ in os.walk(pkgdir):
  188. self.patch_files = fnmatch.filter(os.listdir(subdir), '*.patch')
  189. if self.patch_count == 0:
  190. self.status['patches'] = ("ok", "no patches")
  191. elif self.patch_count < 5:
  192. self.status['patches'] = ("warning", "some patches")
  193. else:
  194. self.status['patches'] = ("error", "lots of patches")
  195. def set_current_version(self):
  196. """
  197. Fills in the .current_version field
  198. """
  199. var = self.pkgvar()
  200. if var in self.all_versions:
  201. self.current_version = self.all_versions[var]
  202. def set_cpeid(self):
  203. """
  204. Fills in the .cpeid field
  205. """
  206. var = self.pkgvar()
  207. if not self.is_actual_package:
  208. self.status['cpe'] = ("na", "N/A - virtual pkg")
  209. return
  210. if not self.current_version:
  211. self.status['cpe'] = ("na", "no version information available")
  212. return
  213. if var in self.all_cpeids:
  214. self.cpeid = self.all_cpeids[var]
  215. # Set a preliminary status, it might be overridden by check_package_cpes()
  216. self.status['cpe'] = ("warning", "not checked against CPE dictionnary")
  217. else:
  218. self.status['cpe'] = ("error", "no verified CPE identifier")
  219. def set_check_package_warnings(self):
  220. """
  221. Fills in the .warnings and .status['pkg-check'] fields
  222. """
  223. cmd = [os.path.join(brpath, "utils/check-package")]
  224. pkgdir = os.path.dirname(os.path.join(brpath, self.path))
  225. self.status['pkg-check'] = ("error", "Missing")
  226. for root, dirs, files in os.walk(pkgdir):
  227. for f in files:
  228. if f.endswith(".mk") or f.endswith(".hash") or f == "Config.in" or f == "Config.in.host":
  229. cmd.append(os.path.join(root, f))
  230. o = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[1]
  231. lines = o.splitlines()
  232. for line in lines:
  233. m = re.match("^([0-9]*) warnings generated", line.decode())
  234. if m:
  235. self.warnings = int(m.group(1))
  236. if self.warnings == 0:
  237. self.status['pkg-check'] = ("ok", "no warnings")
  238. else:
  239. self.status['pkg-check'] = ("error", "{} warnings".format(self.warnings))
  240. return
  241. def set_ignored_cves(self):
  242. """
  243. Give the list of CVEs ignored by the package
  244. """
  245. self.ignored_cves = list(self.all_ignored_cves.get(self.pkgvar(), []))
  246. def set_developers(self, developers):
  247. """
  248. Fills in the .developers and .status['developers'] field
  249. """
  250. self.developers = [
  251. dev.name
  252. for dev in developers
  253. if dev.hasfile(self.path)
  254. ]
  255. if self.developers:
  256. self.status['developers'] = ("ok", "{} developers".format(len(self.developers)))
  257. else:
  258. self.status['developers'] = ("warning", "no developers")
  259. def is_status_ok(self, name):
  260. return name in self.status and self.status[name][0] == 'ok'
  261. def is_status_error(self, name):
  262. return name in self.status and self.status[name][0] == 'error'
  263. def is_status_na(self, name):
  264. return name in self.status and self.status[name][0] == 'na'
  265. def __eq__(self, other):
  266. return self.path == other.path
  267. def __lt__(self, other):
  268. return self.path < other.path
  269. def __str__(self):
  270. return "%s (path='%s', license='%s', license_files='%s', hash='%s', patches=%d)" % \
  271. (self.name, self.path, self.is_status_ok('license'),
  272. self.is_status_ok('license-files'), self.status['hash'], self.patch_count)
  273. def get_pkglist(npackages, package_list):
  274. """
  275. Builds the list of Buildroot packages, returning a list of Package
  276. objects. Only the .name and .path fields of the Package object are
  277. initialized.
  278. npackages: limit to N packages
  279. package_list: limit to those packages in this list
  280. """
  281. WALK_USEFUL_SUBDIRS = ["boot", "linux", "package", "toolchain"]
  282. WALK_EXCLUDES = ["boot/common.mk",
  283. "linux/linux-ext-.*.mk",
  284. "package/freescale-imx/freescale-imx.mk",
  285. "package/gcc/gcc.mk",
  286. "package/gstreamer/gstreamer.mk",
  287. "package/gstreamer1/gstreamer1.mk",
  288. "package/gtk2-themes/gtk2-themes.mk",
  289. "package/matchbox/matchbox.mk",
  290. "package/opengl/opengl.mk",
  291. "package/qt5/qt5.mk",
  292. "package/x11r7/x11r7.mk",
  293. "package/doc-asciidoc.mk",
  294. "package/pkg-.*.mk",
  295. "toolchain/toolchain-external/pkg-toolchain-external.mk",
  296. "toolchain/toolchain-external/toolchain-external.mk",
  297. "toolchain/toolchain.mk",
  298. "toolchain/helpers.mk",
  299. "toolchain/toolchain-wrapper.mk"]
  300. packages = list()
  301. count = 0
  302. for root, dirs, files in os.walk(brpath):
  303. root = os.path.relpath(root, brpath)
  304. rootdir = root.split("/")
  305. if len(rootdir) < 1:
  306. continue
  307. if rootdir[0] not in WALK_USEFUL_SUBDIRS:
  308. continue
  309. for f in files:
  310. if not f.endswith(".mk"):
  311. continue
  312. # Strip ending ".mk"
  313. pkgname = f[:-3]
  314. if package_list and pkgname not in package_list:
  315. continue
  316. pkgpath = os.path.join(root, f)
  317. skip = False
  318. for exclude in WALK_EXCLUDES:
  319. if re.match(exclude, pkgpath):
  320. skip = True
  321. continue
  322. if skip:
  323. continue
  324. p = Package(pkgname, pkgpath)
  325. packages.append(p)
  326. count += 1
  327. if npackages and count == npackages:
  328. return packages
  329. return packages
  330. def get_config_packages():
  331. cmd = ["make", "--no-print-directory", "show-info"]
  332. js = json.loads(subprocess.check_output(cmd))
  333. return set([v["name"] for v in js.values() if 'name' in v])
  334. def package_init_make_info():
  335. # Fetch all variables at once
  336. variables = subprocess.check_output(["make", "--no-print-directory", "-s",
  337. "BR2_HAVE_DOT_CONFIG=y", "printvars",
  338. "VARS=%_LICENSE %_LICENSE_FILES %_VERSION %_IGNORE_CVES %_CPE_ID"])
  339. variable_list = variables.decode().splitlines()
  340. # We process first the host package VERSION, and then the target
  341. # package VERSION. This means that if a package exists in both
  342. # target and host variants, with different values (eg. version
  343. # numbers (unlikely)), we'll report the target one.
  344. variable_list = [x[5:] for x in variable_list if x.startswith("HOST_")] + \
  345. [x for x in variable_list if not x.startswith("HOST_")]
  346. for item in variable_list:
  347. # Get variable name and value
  348. pkgvar, value = item.split("=", maxsplit=1)
  349. # Strip the suffix according to the variable
  350. if pkgvar.endswith("_LICENSE"):
  351. # If value is "unknown", no license details available
  352. if value == "unknown":
  353. continue
  354. pkgvar = pkgvar[:-8]
  355. Package.all_licenses[pkgvar] = value
  356. elif pkgvar.endswith("_LICENSE_FILES"):
  357. if pkgvar.endswith("_MANIFEST_LICENSE_FILES"):
  358. continue
  359. pkgvar = pkgvar[:-14]
  360. Package.all_license_files.append(pkgvar)
  361. elif pkgvar.endswith("_VERSION"):
  362. if pkgvar.endswith("_DL_VERSION"):
  363. continue
  364. pkgvar = pkgvar[:-8]
  365. Package.all_versions[pkgvar] = value
  366. elif pkgvar.endswith("_IGNORE_CVES"):
  367. pkgvar = pkgvar[:-12]
  368. Package.all_ignored_cves[pkgvar] = value.split()
  369. elif pkgvar.endswith("_CPE_ID"):
  370. pkgvar = pkgvar[:-7]
  371. Package.all_cpeids[pkgvar] = value
  372. check_url_count = 0
  373. async def check_url_status(session, pkg, npkgs, retry=True):
  374. global check_url_count
  375. try:
  376. async with session.get(pkg.url) as resp:
  377. if resp.status >= 400:
  378. pkg.status['url'] = ("error", "invalid {}".format(resp.status))
  379. check_url_count += 1
  380. print("[%04d/%04d] %s" % (check_url_count, npkgs, pkg.name))
  381. return
  382. except (aiohttp.ClientError, asyncio.TimeoutError):
  383. if retry:
  384. return await check_url_status(session, pkg, npkgs, retry=False)
  385. else:
  386. pkg.status['url'] = ("error", "invalid (err)")
  387. check_url_count += 1
  388. print("[%04d/%04d] %s" % (check_url_count, npkgs, pkg.name))
  389. return
  390. pkg.status['url'] = ("ok", "valid")
  391. check_url_count += 1
  392. print("[%04d/%04d] %s" % (check_url_count, npkgs, pkg.name))
  393. async def check_package_urls(packages):
  394. tasks = []
  395. connector = aiohttp.TCPConnector(limit_per_host=5)
  396. async with aiohttp.ClientSession(connector=connector, trust_env=True,
  397. timeout=aiohttp.ClientTimeout(total=15)) as sess:
  398. packages = [p for p in packages if p.status['url'][0] == 'ok']
  399. for pkg in packages:
  400. tasks.append(asyncio.ensure_future(check_url_status(sess, pkg, len(packages))))
  401. await asyncio.wait(tasks)
  402. def check_package_latest_version_set_status(pkg, status, version, identifier):
  403. pkg.latest_version = {
  404. "status": status,
  405. "version": version,
  406. "id": identifier,
  407. }
  408. if pkg.latest_version['status'] == RM_API_STATUS_ERROR:
  409. pkg.status['version'] = ('warning', "Release Monitoring API error")
  410. elif pkg.latest_version['status'] == RM_API_STATUS_NOT_FOUND:
  411. pkg.status['version'] = ('warning', "Package not found on Release Monitoring")
  412. if pkg.latest_version['version'] is None:
  413. pkg.status['version'] = ('warning', "No upstream version available on Release Monitoring")
  414. elif pkg.latest_version['version'] != pkg.current_version:
  415. pkg.status['version'] = ('error', "The newer version {} is available upstream".format(pkg.latest_version['version']))
  416. else:
  417. pkg.status['version'] = ('ok', 'up-to-date')
  418. async def check_package_get_latest_version_by_distro(session, pkg, retry=True):
  419. url = "https://release-monitoring.org//api/project/Buildroot/%s" % pkg.name
  420. try:
  421. async with session.get(url) as resp:
  422. if resp.status != 200:
  423. return False
  424. data = await resp.json()
  425. if 'stable_versions' in data and data['stable_versions']:
  426. version = data['stable_versions'][0]
  427. elif 'version' in data:
  428. version = data['version']
  429. else:
  430. version = None
  431. check_package_latest_version_set_status(pkg,
  432. RM_API_STATUS_FOUND_BY_DISTRO,
  433. version,
  434. data['id'])
  435. return True
  436. except (aiohttp.ClientError, asyncio.TimeoutError):
  437. if retry:
  438. return await check_package_get_latest_version_by_distro(session, pkg, retry=False)
  439. else:
  440. return False
  441. async def check_package_get_latest_version_by_guess(session, pkg, retry=True):
  442. url = "https://release-monitoring.org/api/projects/?pattern=%s" % pkg.name
  443. try:
  444. async with session.get(url) as resp:
  445. if resp.status != 200:
  446. return False
  447. data = await resp.json()
  448. # filter projects that have the right name and a version defined
  449. projects = [p for p in data['projects'] if p['name'] == pkg.name and 'stable_versions' in p]
  450. projects.sort(key=lambda x: x['id'])
  451. if len(projects) > 0:
  452. check_package_latest_version_set_status(pkg,
  453. RM_API_STATUS_FOUND_BY_PATTERN,
  454. projects[0]['stable_versions'][0],
  455. projects[0]['id'])
  456. return True
  457. except (aiohttp.ClientError, asyncio.TimeoutError):
  458. if retry:
  459. return await check_package_get_latest_version_by_guess(session, pkg, retry=False)
  460. else:
  461. return False
  462. check_latest_count = 0
  463. async def check_package_latest_version_get(session, pkg, npkgs):
  464. global check_latest_count
  465. if await check_package_get_latest_version_by_distro(session, pkg):
  466. check_latest_count += 1
  467. print("[%04d/%04d] %s" % (check_latest_count, npkgs, pkg.name))
  468. return
  469. if await check_package_get_latest_version_by_guess(session, pkg):
  470. check_latest_count += 1
  471. print("[%04d/%04d] %s" % (check_latest_count, npkgs, pkg.name))
  472. return
  473. check_package_latest_version_set_status(pkg,
  474. RM_API_STATUS_NOT_FOUND,
  475. None, None)
  476. check_latest_count += 1
  477. print("[%04d/%04d] %s" % (check_latest_count, npkgs, pkg.name))
  478. async def check_package_latest_version(packages):
  479. """
  480. Fills in the .latest_version field of all Package objects
  481. This field is a dict and has the following keys:
  482. - status: one of RM_API_STATUS_ERROR,
  483. RM_API_STATUS_FOUND_BY_DISTRO, RM_API_STATUS_FOUND_BY_PATTERN,
  484. RM_API_STATUS_NOT_FOUND
  485. - version: string containing the latest version known by
  486. release-monitoring.org for this package
  487. - id: string containing the id of the project corresponding to this
  488. package, as known by release-monitoring.org
  489. """
  490. for pkg in [p for p in packages if not p.is_actual_package]:
  491. pkg.status['version'] = ("na", "no valid package infra")
  492. tasks = []
  493. connector = aiohttp.TCPConnector(limit_per_host=5)
  494. async with aiohttp.ClientSession(connector=connector, trust_env=True) as sess:
  495. packages = [p for p in packages if p.is_actual_package]
  496. for pkg in packages:
  497. tasks.append(asyncio.ensure_future(check_package_latest_version_get(sess, pkg, len(packages))))
  498. await asyncio.wait(tasks)
  499. def check_package_cve_affects(cve, cpe_product_pkgs):
  500. for product in cve.affected_products:
  501. if product not in cpe_product_pkgs:
  502. continue
  503. for pkg in cpe_product_pkgs[product]:
  504. cve_status = cve.affects(pkg.name, pkg.current_version, pkg.ignored_cves, pkg.cpeid)
  505. if cve_status == cve.CVE_AFFECTS:
  506. pkg.cves.append(cve.identifier)
  507. elif cve_status == cve.CVE_UNKNOWN:
  508. pkg.unsure_cves.append(cve.identifier)
  509. def check_package_cves(nvd_path, packages):
  510. if not os.path.isdir(nvd_path):
  511. os.makedirs(nvd_path)
  512. cpe_product_pkgs = defaultdict(list)
  513. for pkg in packages:
  514. if not pkg.is_actual_package:
  515. pkg.status['cve'] = ("na", "N/A")
  516. continue
  517. if not pkg.current_version:
  518. pkg.status['cve'] = ("na", "no version information available")
  519. continue
  520. if pkg.cpeid:
  521. cpe_product = cvecheck.cpe_product(pkg.cpeid)
  522. cpe_product_pkgs[cpe_product].append(pkg)
  523. else:
  524. cpe_product_pkgs[pkg.name].append(pkg)
  525. for cve in cvecheck.CVE.read_nvd_dir(nvd_path):
  526. check_package_cve_affects(cve, cpe_product_pkgs)
  527. for pkg in packages:
  528. if 'cve' not in pkg.status:
  529. if pkg.cves or pkg.unsure_cves:
  530. pkg.status['cve'] = ("error", "affected by CVEs")
  531. else:
  532. pkg.status['cve'] = ("ok", "not affected by CVEs")
  533. def check_package_cpes(nvd_path, packages):
  534. class CpeXmlParser:
  535. cpes = []
  536. def start(self, tag, attrib):
  537. if tag == "{http://scap.nist.gov/schema/cpe-extension/2.3}cpe23-item":
  538. self.cpes.append(attrib['name'])
  539. def close(self):
  540. return self.cpes
  541. print("CPE: Setting up NIST dictionary")
  542. if not os.path.exists(os.path.join(nvd_path, "cpe")):
  543. os.makedirs(os.path.join(nvd_path, "cpe"))
  544. cpe_dict_local = os.path.join(nvd_path, "cpe", os.path.basename(CPEDB_URL))
  545. if not os.path.exists(cpe_dict_local) or os.stat(cpe_dict_local).st_mtime < time.time() - 86400:
  546. print("CPE: Fetching xml manifest from [" + CPEDB_URL + "]")
  547. cpe_dict = requests.get(CPEDB_URL)
  548. open(cpe_dict_local, "wb").write(cpe_dict.content)
  549. print("CPE: Unzipping xml manifest...")
  550. nist_cpe_file = gzip.GzipFile(fileobj=open(cpe_dict_local, 'rb'))
  551. parser = xml.etree.ElementTree.XMLParser(target=CpeXmlParser())
  552. while True:
  553. c = nist_cpe_file.read(1024*1024)
  554. if not c:
  555. break
  556. parser.feed(c)
  557. cpes = parser.close()
  558. for p in packages:
  559. if not p.cpeid:
  560. continue
  561. if p.cpeid in cpes:
  562. p.status['cpe'] = ("ok", "verified CPE identifier")
  563. else:
  564. p.status['cpe'] = ("error", "CPE version unknown in CPE database")
  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. stats["total-unsure-cves"] += len(pkg.unsure_cves)
  603. if len(pkg.cves) != 0:
  604. stats["pkg-cves"] += 1
  605. if len(pkg.unsure_cves) != 0:
  606. stats["pkg-unsure-cves"] += 1
  607. if pkg.cpeid:
  608. stats["cpe-id"] += 1
  609. else:
  610. stats["no-cpe-id"] += 1
  611. return stats
  612. html_header = """
  613. <head>
  614. <script src=\"https://www.kryogenix.org/code/browser/sorttable/sorttable.js\"></script>
  615. <style type=\"text/css\">
  616. table {
  617. width: 100%;
  618. }
  619. td {
  620. border: 1px solid black;
  621. }
  622. td.centered {
  623. text-align: center;
  624. }
  625. td.wrong {
  626. background: #ff9a69;
  627. }
  628. td.correct {
  629. background: #d2ffc4;
  630. }
  631. td.nopatches {
  632. background: #d2ffc4;
  633. }
  634. td.somepatches {
  635. background: #ffd870;
  636. }
  637. td.lotsofpatches {
  638. background: #ff9a69;
  639. }
  640. td.good_url {
  641. background: #d2ffc4;
  642. }
  643. td.missing_url {
  644. background: #ffd870;
  645. }
  646. td.invalid_url {
  647. background: #ff9a69;
  648. }
  649. td.version-good {
  650. background: #d2ffc4;
  651. }
  652. td.version-needs-update {
  653. background: #ff9a69;
  654. }
  655. td.version-unknown {
  656. background: #ffd870;
  657. }
  658. td.version-error {
  659. background: #ccc;
  660. }
  661. td.cpe-ok {
  662. background: #d2ffc4;
  663. }
  664. td.cpe-nok {
  665. background: #ff9a69;
  666. }
  667. td.cpe-unknown {
  668. background: #ffd870;
  669. }
  670. td.cve-ok {
  671. background: #d2ffc4;
  672. }
  673. td.cve-nok {
  674. background: #ff9a69;
  675. }
  676. td.cve-unknown {
  677. background: #ffd870;
  678. }
  679. td.cve_ignored {
  680. background: #ccc;
  681. }
  682. </style>
  683. <title>Statistics of Buildroot packages</title>
  684. </head>
  685. <a href=\"#results\">Results</a><br/>
  686. <p id=\"sortable_hint\"></p>
  687. """
  688. html_footer = """
  689. </body>
  690. <script>
  691. if (typeof sorttable === \"object\") {
  692. document.getElementById(\"sortable_hint\").innerHTML =
  693. \"hint: the table can be sorted by clicking the column headers\"
  694. }
  695. </script>
  696. </html>
  697. """
  698. def infra_str(infra_list):
  699. if not infra_list:
  700. return "Unknown"
  701. elif len(infra_list) == 1:
  702. return "<b>%s</b><br/>%s" % (infra_list[0][1], infra_list[0][0])
  703. elif infra_list[0][1] == infra_list[1][1]:
  704. return "<b>%s</b><br/>%s + %s" % \
  705. (infra_list[0][1], infra_list[0][0], infra_list[1][0])
  706. else:
  707. return "<b>%s</b> (%s)<br/><b>%s</b> (%s)" % \
  708. (infra_list[0][1], infra_list[0][0],
  709. infra_list[1][1], infra_list[1][0])
  710. def boolean_str(b):
  711. if b:
  712. return "Yes"
  713. else:
  714. return "No"
  715. def dump_html_pkg(f, pkg):
  716. f.write(" <tr>\n")
  717. f.write(" <td>%s</td>\n" % pkg.path)
  718. # Patch count
  719. td_class = ["centered"]
  720. if pkg.patch_count == 0:
  721. td_class.append("nopatches")
  722. elif pkg.patch_count < 5:
  723. td_class.append("somepatches")
  724. else:
  725. td_class.append("lotsofpatches")
  726. f.write(" <td class=\"%s\">%s</td>\n" %
  727. (" ".join(td_class), str(pkg.patch_count)))
  728. # Infrastructure
  729. infra = infra_str(pkg.infras)
  730. td_class = ["centered"]
  731. if infra == "Unknown":
  732. td_class.append("wrong")
  733. else:
  734. td_class.append("correct")
  735. f.write(" <td class=\"%s\">%s</td>\n" %
  736. (" ".join(td_class), infra_str(pkg.infras)))
  737. # License
  738. td_class = ["centered"]
  739. if pkg.is_status_ok('license'):
  740. td_class.append("correct")
  741. else:
  742. td_class.append("wrong")
  743. f.write(" <td class=\"%s\">%s</td>\n" %
  744. (" ".join(td_class), boolean_str(pkg.is_status_ok('license'))))
  745. # License files
  746. td_class = ["centered"]
  747. if pkg.is_status_ok('license-files'):
  748. td_class.append("correct")
  749. else:
  750. td_class.append("wrong")
  751. f.write(" <td class=\"%s\">%s</td>\n" %
  752. (" ".join(td_class), boolean_str(pkg.is_status_ok('license-files'))))
  753. # Hash
  754. td_class = ["centered"]
  755. if pkg.is_status_ok('hash'):
  756. td_class.append("correct")
  757. else:
  758. td_class.append("wrong")
  759. f.write(" <td class=\"%s\">%s</td>\n" %
  760. (" ".join(td_class), boolean_str(pkg.is_status_ok('hash'))))
  761. # Current version
  762. if len(pkg.current_version) > 20:
  763. current_version = pkg.current_version[:20] + "..."
  764. else:
  765. current_version = pkg.current_version
  766. f.write(" <td class=\"centered\">%s</td>\n" % current_version)
  767. # Latest version
  768. if pkg.latest_version['status'] == RM_API_STATUS_ERROR:
  769. td_class.append("version-error")
  770. if pkg.latest_version['version'] is None:
  771. td_class.append("version-unknown")
  772. elif pkg.latest_version['version'] != pkg.current_version:
  773. td_class.append("version-needs-update")
  774. else:
  775. td_class.append("version-good")
  776. if pkg.latest_version['status'] == RM_API_STATUS_ERROR:
  777. latest_version_text = "<b>Error</b>"
  778. elif pkg.latest_version['status'] == RM_API_STATUS_NOT_FOUND:
  779. latest_version_text = "<b>Not found</b>"
  780. else:
  781. if pkg.latest_version['version'] is None:
  782. latest_version_text = "<b>Found, but no version</b>"
  783. else:
  784. latest_version_text = "<a href=\"https://release-monitoring.org/project/%s\"><b>%s</b></a>" % \
  785. (pkg.latest_version['id'], str(pkg.latest_version['version']))
  786. latest_version_text += "<br/>"
  787. if pkg.latest_version['status'] == RM_API_STATUS_FOUND_BY_DISTRO:
  788. latest_version_text += "found by <a href=\"https://release-monitoring.org/distro/Buildroot/\">distro</a>"
  789. else:
  790. latest_version_text += "found by guess"
  791. f.write(" <td class=\"%s\">%s</td>\n" %
  792. (" ".join(td_class), latest_version_text))
  793. # Warnings
  794. td_class = ["centered"]
  795. if pkg.warnings == 0:
  796. td_class.append("correct")
  797. else:
  798. td_class.append("wrong")
  799. f.write(" <td class=\"%s\">%d</td>\n" %
  800. (" ".join(td_class), pkg.warnings))
  801. # URL status
  802. td_class = ["centered"]
  803. url_str = pkg.status['url'][1]
  804. if pkg.status['url'][0] in ("error", "warning"):
  805. td_class.append("missing_url")
  806. if pkg.status['url'][0] == "error":
  807. td_class.append("invalid_url")
  808. url_str = "<a href=%s>%s</a>" % (pkg.url, pkg.status['url'][1])
  809. else:
  810. td_class.append("good_url")
  811. url_str = "<a href=%s>Link</a>" % pkg.url
  812. f.write(" <td class=\"%s\">%s</td>\n" %
  813. (" ".join(td_class), url_str))
  814. # CVEs
  815. td_class = ["centered"]
  816. if pkg.is_status_ok("cve"):
  817. td_class.append("cve-ok")
  818. elif pkg.is_status_error("cve"):
  819. td_class.append("cve-nok")
  820. elif pkg.is_status_na("cve") and not pkg.is_actual_package:
  821. td_class.append("cve-ok")
  822. else:
  823. td_class.append("cve-unknown")
  824. f.write(" <td class=\"%s\">\n" % " ".join(td_class))
  825. if pkg.is_status_error("cve"):
  826. for cve in pkg.cves:
  827. f.write(" <a href=\"https://security-tracker.debian.org/tracker/%s\">%s<br/>\n" % (cve, cve))
  828. for cve in pkg.unsure_cves:
  829. f.write(" <a href=\"https://security-tracker.debian.org/tracker/%s\">%s <i>(unsure)</i><br/>\n" % (cve, cve))
  830. elif pkg.is_status_na("cve"):
  831. f.write(" %s" % pkg.status['cve'][1])
  832. else:
  833. f.write(" N/A\n")
  834. f.write(" </td>\n")
  835. # CVEs Ignored
  836. td_class = ["centered"]
  837. if pkg.ignored_cves:
  838. td_class.append("cve_ignored")
  839. f.write(" <td class=\"%s\">\n" % " ".join(td_class))
  840. for ignored_cve in pkg.ignored_cves:
  841. f.write(" <a href=\"https://security-tracker.debian.org/tracker/%s\">%s<br/>\n" % (ignored_cve, ignored_cve))
  842. f.write(" </td>\n")
  843. # CPE ID
  844. td_class = ["left"]
  845. if pkg.is_status_ok("cpe"):
  846. td_class.append("cpe-ok")
  847. elif pkg.is_status_error("cpe"):
  848. td_class.append("cpe-nok")
  849. elif pkg.is_status_na("cpe") and not pkg.is_actual_package:
  850. td_class.append("cpe-ok")
  851. else:
  852. td_class.append("cpe-unknown")
  853. f.write(" <td class=\"%s\">\n" % " ".join(td_class))
  854. if pkg.cpeid:
  855. f.write(" <code>%s</code>\n" % pkg.cpeid)
  856. if not pkg.is_status_ok("cpe"):
  857. if pkg.is_actual_package and pkg.current_version:
  858. if pkg.cpeid:
  859. f.write(" <br/>%s <a href=\"https://nvd.nist.gov/products/cpe/search/results?namingFormat=2.3&keyword=%s\">(Search)</a>\n" % # noqa: E501
  860. (pkg.status['cpe'][1], ":".join(pkg.cpeid.split(":")[0:5])))
  861. else:
  862. f.write(" %s <a href=\"https://nvd.nist.gov/products/cpe/search/results?namingFormat=2.3&keyword=%s\">(Search)</a>\n" % # noqa: E501
  863. (pkg.status['cpe'][1], pkg.name))
  864. else:
  865. f.write(" %s\n" % pkg.status['cpe'][1])
  866. f.write(" </td>\n")
  867. f.write(" </tr>\n")
  868. def dump_html_all_pkgs(f, packages):
  869. f.write("""
  870. <table class=\"sortable\">
  871. <tr>
  872. <td>Package</td>
  873. <td class=\"centered\">Patch count</td>
  874. <td class=\"centered\">Infrastructure</td>
  875. <td class=\"centered\">License</td>
  876. <td class=\"centered\">License files</td>
  877. <td class=\"centered\">Hash file</td>
  878. <td class=\"centered\">Current version</td>
  879. <td class=\"centered\">Latest version</td>
  880. <td class=\"centered\">Warnings</td>
  881. <td class=\"centered\">Upstream URL</td>
  882. <td class=\"centered\">CVEs</td>
  883. <td class=\"centered\">CVEs Ignored</td>
  884. <td class=\"centered\">CPE ID</td>
  885. </tr>
  886. """)
  887. for pkg in sorted(packages):
  888. dump_html_pkg(f, pkg)
  889. f.write("</table>")
  890. def dump_html_stats(f, stats):
  891. f.write("<a id=\"results\"></a>\n")
  892. f.write("<table>\n")
  893. infras = [infra[6:] for infra in stats.keys() if infra.startswith("infra-")]
  894. for infra in infras:
  895. f.write(" <tr><td>Packages using the <i>%s</i> infrastructure</td><td>%s</td></tr>\n" %
  896. (infra, stats["infra-%s" % infra]))
  897. f.write(" <tr><td>Packages having license information</td><td>%s</td></tr>\n" %
  898. stats["license"])
  899. f.write(" <tr><td>Packages not having license information</td><td>%s</td></tr>\n" %
  900. stats["no-license"])
  901. f.write(" <tr><td>Packages having license files information</td><td>%s</td></tr>\n" %
  902. stats["license-files"])
  903. f.write(" <tr><td>Packages not having license files information</td><td>%s</td></tr>\n" %
  904. stats["no-license-files"])
  905. f.write(" <tr><td>Packages having a hash file</td><td>%s</td></tr>\n" %
  906. stats["hash"])
  907. f.write(" <tr><td>Packages not having a hash file</td><td>%s</td></tr>\n" %
  908. stats["no-hash"])
  909. f.write(" <tr><td>Total number of patches</td><td>%s</td></tr>\n" %
  910. stats["patches"])
  911. f.write("<tr><td>Packages having a mapping on <i>release-monitoring.org</i></td><td>%s</td></tr>\n" %
  912. stats["rmo-mapping"])
  913. f.write("<tr><td>Packages lacking a mapping on <i>release-monitoring.org</i></td><td>%s</td></tr>\n" %
  914. stats["rmo-no-mapping"])
  915. f.write("<tr><td>Packages that are up-to-date</td><td>%s</td></tr>\n" %
  916. stats["version-uptodate"])
  917. f.write("<tr><td>Packages that are not up-to-date</td><td>%s</td></tr>\n" %
  918. stats["version-not-uptodate"])
  919. f.write("<tr><td>Packages with no known upstream version</td><td>%s</td></tr>\n" %
  920. stats["version-unknown"])
  921. f.write("<tr><td>Packages affected by CVEs</td><td>%s</td></tr>\n" %
  922. stats["pkg-cves"])
  923. f.write("<tr><td>Total number of CVEs affecting all packages</td><td>%s</td></tr>\n" %
  924. stats["total-cves"])
  925. f.write("<tr><td>Packages affected by unsure CVEs</td><td>%s</td></tr>\n" %
  926. stats["pkg-unsure-cves"])
  927. f.write("<tr><td>Total number of unsure CVEs affecting all packages</td><td>%s</td></tr>\n" %
  928. stats["total-unsure-cves"])
  929. f.write("<tr><td>Packages with CPE ID</td><td>%s</td></tr>\n" %
  930. stats["cpe-id"])
  931. f.write("<tr><td>Packages without CPE ID</td><td>%s</td></tr>\n" %
  932. stats["no-cpe-id"])
  933. f.write("</table>\n")
  934. def dump_html_gen_info(f, date, commit):
  935. # Updated on Mon Feb 19 08:12:08 CET 2018, Git commit aa77030b8f5e41f1c53eb1c1ad664b8c814ba032
  936. f.write("<p><i>Updated on %s, git commit %s</i></p>\n" % (str(date), commit))
  937. def dump_html(packages, stats, date, commit, output):
  938. with open(output, 'w') as f:
  939. f.write(html_header)
  940. dump_html_all_pkgs(f, packages)
  941. dump_html_stats(f, stats)
  942. dump_html_gen_info(f, date, commit)
  943. f.write(html_footer)
  944. def dump_json(packages, defconfigs, stats, date, commit, output):
  945. # Format packages as a dictionnary instead of a list
  946. # Exclude local field that does not contains real date
  947. excluded_fields = ['url_worker', 'name']
  948. pkgs = {
  949. pkg.name: {
  950. k: v
  951. for k, v in pkg.__dict__.items()
  952. if k not in excluded_fields
  953. } for pkg in packages
  954. }
  955. defconfigs = {
  956. d.name: {
  957. k: v
  958. for k, v in d.__dict__.items()
  959. } for d in defconfigs
  960. }
  961. # Aggregate infrastructures into a single dict entry
  962. statistics = {
  963. k: v
  964. for k, v in stats.items()
  965. if not k.startswith('infra-')
  966. }
  967. statistics['infra'] = {k[6:]: v for k, v in stats.items() if k.startswith('infra-')}
  968. # The actual structure to dump, add commit and date to it
  969. final = {'packages': pkgs,
  970. 'stats': statistics,
  971. 'defconfigs': defconfigs,
  972. 'package_status_checks': Package.status_checks,
  973. 'commit': commit,
  974. 'date': str(date)}
  975. with open(output, 'w') as f:
  976. json.dump(final, f, indent=2, separators=(',', ': '))
  977. f.write('\n')
  978. def resolvepath(path):
  979. return os.path.abspath(os.path.expanduser(path))
  980. def list_str(values):
  981. return values.split(',')
  982. def parse_args():
  983. parser = argparse.ArgumentParser()
  984. output = parser.add_argument_group('output', 'Output file(s)')
  985. output.add_argument('--html', dest='html', type=resolvepath,
  986. help='HTML output file')
  987. output.add_argument('--json', dest='json', type=resolvepath,
  988. help='JSON output file')
  989. packages = parser.add_mutually_exclusive_group()
  990. packages.add_argument('-c', dest='configpackages', action='store_true',
  991. help='Apply to packages enabled in current configuration')
  992. packages.add_argument('-n', dest='npackages', type=int, action='store',
  993. help='Number of packages')
  994. packages.add_argument('-p', dest='packages', action='store',
  995. help='List of packages (comma separated)')
  996. parser.add_argument('--nvd-path', dest='nvd_path',
  997. help='Path to the local NVD database', type=resolvepath)
  998. parser.add_argument('--disable', type=list_str,
  999. help='Features to disable, comma-separated (cve, upstream, url, cpe, warning)',
  1000. default=[])
  1001. args = parser.parse_args()
  1002. if not args.html and not args.json:
  1003. parser.error('at least one of --html or --json (or both) is required')
  1004. return args
  1005. def __main__():
  1006. global cvecheck
  1007. args = parse_args()
  1008. if args.nvd_path:
  1009. import cve as cvecheck
  1010. if args.packages:
  1011. package_list = args.packages.split(",")
  1012. elif args.configpackages:
  1013. package_list = get_config_packages()
  1014. else:
  1015. package_list = None
  1016. date = datetime.datetime.utcnow()
  1017. commit = subprocess.check_output(['git', '-C', brpath,
  1018. 'rev-parse',
  1019. 'HEAD']).splitlines()[0].decode()
  1020. print("Build package list ...")
  1021. packages = get_pkglist(args.npackages, package_list)
  1022. print("Getting developers ...")
  1023. developers = parse_developers()
  1024. print("Build defconfig list ...")
  1025. defconfigs = get_defconfig_list()
  1026. for d in defconfigs:
  1027. d.set_developers(developers)
  1028. print("Getting package make info ...")
  1029. package_init_make_info()
  1030. print("Getting package details ...")
  1031. for pkg in packages:
  1032. pkg.set_infra()
  1033. pkg.set_license()
  1034. pkg.set_hash_info()
  1035. pkg.set_patch_count()
  1036. if "warnings" not in args.disable:
  1037. pkg.set_check_package_warnings()
  1038. pkg.set_current_version()
  1039. pkg.set_cpeid()
  1040. pkg.set_url()
  1041. pkg.set_ignored_cves()
  1042. pkg.set_developers(developers)
  1043. if "url" not in args.disable:
  1044. print("Checking URL status")
  1045. loop = asyncio.get_event_loop()
  1046. loop.run_until_complete(check_package_urls(packages))
  1047. if "upstream" not in args.disable:
  1048. print("Getting latest versions ...")
  1049. loop = asyncio.get_event_loop()
  1050. loop.run_until_complete(check_package_latest_version(packages))
  1051. if "cve" not in args.disable and args.nvd_path:
  1052. print("Checking packages CVEs")
  1053. check_package_cves(args.nvd_path, packages)
  1054. if "cpe" not in args.disable and args.nvd_path:
  1055. print("Checking packages CPEs")
  1056. check_package_cpes(args.nvd_path, packages)
  1057. print("Calculate stats")
  1058. stats = calculate_stats(packages)
  1059. if args.html:
  1060. print("Write HTML")
  1061. dump_html(packages, stats, date, commit, args.html)
  1062. if args.json:
  1063. print("Write JSON")
  1064. dump_json(packages, defconfigs, stats, date, commit, args.json)
  1065. __main__()