cve-check.bbclass 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # This class is used to check recipes against public CVEs.
  7. #
  8. # In order to use this class just inherit the class in the
  9. # local.conf file and it will add the cve_check task for
  10. # every recipe. The task can be used per recipe, per image,
  11. # or using the special cases "world" and "universe". The
  12. # cve_check task will print a warning for every unpatched
  13. # CVE found and generate a file in the recipe WORKDIR/cve
  14. # directory. If an image is build it will generate a report
  15. # in DEPLOY_DIR_IMAGE for all the packages used.
  16. #
  17. # Example:
  18. # bitbake -c cve_check openssl
  19. # bitbake core-image-sato
  20. # bitbake -k -c cve_check universe
  21. #
  22. # DISCLAIMER
  23. #
  24. # This class/tool is meant to be used as support and not
  25. # the only method to check against CVEs. Running this tool
  26. # doesn't guarantee your packages are free of CVEs.
  27. # The product name that the CVE database uses defaults to BPN, but may need to
  28. # be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff).
  29. CVE_PRODUCT ??= "${BPN}"
  30. CVE_VERSION ??= "${PV}"
  31. CVE_CHECK_DB_FILENAME ?= "nvdcve_2-2.db"
  32. CVE_CHECK_DB_DIR ?= "${STAGING_DIR}/CVE_CHECK"
  33. CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/${CVE_CHECK_DB_FILENAME}"
  34. CVE_CHECK_DB_FILE_LOCK ?= "${CVE_CHECK_DB_FILE}.lock"
  35. CVE_CHECK_SUMMARY_DIR ?= "${LOG_DIR}/cve"
  36. CVE_CHECK_SUMMARY_FILE_NAME ?= "cve-summary"
  37. CVE_CHECK_SUMMARY_FILE_NAME_JSON = "cve-summary.json"
  38. CVE_CHECK_SUMMARY_INDEX_PATH = "${CVE_CHECK_SUMMARY_DIR}/cve-summary-index.txt"
  39. CVE_CHECK_LOG_JSON ?= "${T}/cve.json"
  40. CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve"
  41. CVE_CHECK_RECIPE_FILE_JSON ?= "${CVE_CHECK_DIR}/${PN}_cve.json"
  42. CVE_CHECK_MANIFEST_JSON_SUFFIX ?= "json"
  43. CVE_CHECK_MANIFEST_JSON ?= "${IMGDEPLOYDIR}/${IMAGE_NAME}.${CVE_CHECK_MANIFEST_JSON_SUFFIX}"
  44. CVE_CHECK_COPY_FILES ??= "1"
  45. CVE_CHECK_CREATE_MANIFEST ??= "1"
  46. # Report Patched or Ignored CVEs
  47. CVE_CHECK_REPORT_PATCHED ??= "1"
  48. CVE_CHECK_SHOW_WARNINGS ??= "1"
  49. # Provide JSON output
  50. CVE_CHECK_FORMAT_JSON ??= "1"
  51. # Check for packages without CVEs (no issues or missing product name)
  52. CVE_CHECK_COVERAGE ??= "1"
  53. # Skip CVE Check for packages (PN)
  54. CVE_CHECK_SKIP_RECIPE ?= ""
  55. # Replace NVD DB check status for a given CVE. Each of CVE has to be mentioned
  56. # separately with optional detail and description for this status.
  57. #
  58. # CVE_STATUS[CVE-1234-0001] = "not-applicable-platform: Issue only applies on Windows"
  59. # CVE_STATUS[CVE-1234-0002] = "fixed-version: Fixed externally"
  60. #
  61. # Settings the same status and reason for multiple CVEs is possible
  62. # via CVE_STATUS_GROUPS variable.
  63. #
  64. # CVE_STATUS_GROUPS = "CVE_STATUS_WIN CVE_STATUS_PATCHED"
  65. #
  66. # CVE_STATUS_WIN = "CVE-1234-0001 CVE-1234-0003"
  67. # CVE_STATUS_WIN[status] = "not-applicable-platform: Issue only applies on Windows"
  68. # CVE_STATUS_PATCHED = "CVE-1234-0002 CVE-1234-0004"
  69. # CVE_STATUS_PATCHED[status] = "fixed-version: Fixed externally"
  70. #
  71. # All possible CVE statuses could be found in cve-check-map.conf
  72. # CVE_CHECK_STATUSMAP[not-applicable-platform] = "Ignored"
  73. # CVE_CHECK_STATUSMAP[fixed-version] = "Patched"
  74. #
  75. # CVE_CHECK_IGNORE is deprecated and CVE_STATUS has to be used instead.
  76. # Keep CVE_CHECK_IGNORE until other layers migrate to new variables
  77. CVE_CHECK_IGNORE ?= ""
  78. # Layers to be excluded
  79. CVE_CHECK_LAYER_EXCLUDELIST ??= ""
  80. # Layers to be included
  81. CVE_CHECK_LAYER_INCLUDELIST ??= ""
  82. # set to "alphabetical" for version using single alphabetical character as increment release
  83. CVE_VERSION_SUFFIX ??= ""
  84. python () {
  85. # Fallback all CVEs from CVE_CHECK_IGNORE to CVE_STATUS
  86. cve_check_ignore = d.getVar("CVE_CHECK_IGNORE")
  87. if cve_check_ignore:
  88. bb.warn("CVE_CHECK_IGNORE is deprecated in favor of CVE_STATUS")
  89. for cve in (d.getVar("CVE_CHECK_IGNORE") or "").split():
  90. d.setVarFlag("CVE_STATUS", cve, "ignored")
  91. # Process CVE_STATUS_GROUPS to set multiple statuses and optional detail or description at once
  92. for cve_status_group in (d.getVar("CVE_STATUS_GROUPS") or "").split():
  93. cve_group = d.getVar(cve_status_group)
  94. if cve_group is not None:
  95. for cve in cve_group.split():
  96. d.setVarFlag("CVE_STATUS", cve, d.getVarFlag(cve_status_group, "status"))
  97. else:
  98. bb.warn("CVE_STATUS_GROUPS contains undefined variable %s" % cve_status_group)
  99. }
  100. def generate_json_report(d, out_path, link_path):
  101. if os.path.exists(d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")):
  102. import json
  103. from oe.cve_check import cve_check_merge_jsons, update_symlinks
  104. bb.note("Generating JSON CVE summary")
  105. index_file = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
  106. summary = {"version":"1", "package": []}
  107. with open(index_file) as f:
  108. filename = f.readline()
  109. while filename:
  110. with open(filename.rstrip()) as j:
  111. data = json.load(j)
  112. cve_check_merge_jsons(summary, data)
  113. filename = f.readline()
  114. summary["package"].sort(key=lambda d: d['name'])
  115. with open(out_path, "w") as f:
  116. json.dump(summary, f, indent=2)
  117. update_symlinks(out_path, link_path)
  118. python cve_save_summary_handler () {
  119. import shutil
  120. import datetime
  121. from oe.cve_check import update_symlinks
  122. cve_summary_name = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME")
  123. cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
  124. bb.utils.mkdirhier(cvelogpath)
  125. timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
  126. if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
  127. json_summary_link_name = os.path.join(cvelogpath, d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON"))
  128. json_summary_name = os.path.join(cvelogpath, "%s-%s.json" % (cve_summary_name, timestamp))
  129. generate_json_report(d, json_summary_name, json_summary_link_name)
  130. bb.plain("Complete CVE JSON report summary created at: %s" % json_summary_link_name)
  131. }
  132. addhandler cve_save_summary_handler
  133. cve_save_summary_handler[eventmask] = "bb.event.BuildCompleted"
  134. python do_cve_check () {
  135. """
  136. Check recipe for patched and unpatched CVEs
  137. """
  138. from oe.cve_check import get_patched_cves
  139. with bb.utils.fileslocked([d.getVar("CVE_CHECK_DB_FILE_LOCK")], shared=True):
  140. if os.path.exists(d.getVar("CVE_CHECK_DB_FILE")):
  141. try:
  142. patched_cves = get_patched_cves(d)
  143. except FileNotFoundError:
  144. bb.fatal("Failure in searching patches")
  145. cve_data, status = check_cves(d, patched_cves)
  146. if len(cve_data) or (d.getVar("CVE_CHECK_COVERAGE") == "1" and status):
  147. get_cve_info(d, cve_data)
  148. cve_write_data(d, cve_data, status)
  149. else:
  150. bb.note("No CVE database found, skipping CVE check")
  151. }
  152. addtask cve_check before do_build
  153. do_cve_check[depends] = "cve-update-nvd2-native:do_unpack"
  154. do_cve_check[nostamp] = "1"
  155. python cve_check_cleanup () {
  156. """
  157. Delete the file used to gather all the CVE information.
  158. """
  159. bb.utils.remove(e.data.getVar("CVE_CHECK_SUMMARY_INDEX_PATH"))
  160. }
  161. addhandler cve_check_cleanup
  162. cve_check_cleanup[eventmask] = "bb.event.BuildCompleted"
  163. python cve_check_write_rootfs_manifest () {
  164. """
  165. Create CVE manifest when building an image
  166. """
  167. import shutil
  168. import json
  169. from oe.rootfs import image_list_installed_packages
  170. from oe.cve_check import cve_check_merge_jsons, update_symlinks
  171. if d.getVar("CVE_CHECK_COPY_FILES") == "1":
  172. deploy_file_json = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
  173. if os.path.exists(deploy_file_json):
  174. bb.utils.remove(deploy_file_json)
  175. # Create a list of relevant recipies
  176. recipies = set()
  177. for pkg in list(image_list_installed_packages(d)):
  178. pkg_info = os.path.join(d.getVar('PKGDATA_DIR'),
  179. 'runtime-reverse', pkg)
  180. pkg_data = oe.packagedata.read_pkgdatafile(pkg_info)
  181. recipies.add(pkg_data["PN"])
  182. bb.note("Writing rootfs CVE manifest")
  183. deploy_dir = d.getVar("IMGDEPLOYDIR")
  184. link_name = d.getVar("IMAGE_LINK_NAME")
  185. json_data = {"version":"1", "package": []}
  186. text_data = ""
  187. enable_json = d.getVar("CVE_CHECK_FORMAT_JSON") == "1"
  188. save_pn = d.getVar("PN")
  189. for pkg in recipies:
  190. # To be able to use the CVE_CHECK_RECIPE_FILE_JSON variable we have to evaluate
  191. # it with the different PN names set each time.
  192. d.setVar("PN", pkg)
  193. if enable_json:
  194. pkgfilepath = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
  195. if os.path.exists(pkgfilepath):
  196. with open(pkgfilepath) as j:
  197. data = json.load(j)
  198. cve_check_merge_jsons(json_data, data)
  199. d.setVar("PN", save_pn)
  200. if enable_json:
  201. manifest_name_suffix = d.getVar("CVE_CHECK_MANIFEST_JSON_SUFFIX")
  202. link_path = os.path.join(deploy_dir, "%s.%s" % (link_name, manifest_name_suffix))
  203. manifest_name = d.getVar("CVE_CHECK_MANIFEST_JSON")
  204. with open(manifest_name, "w") as f:
  205. json.dump(json_data, f, indent=2)
  206. update_symlinks(manifest_name, link_path)
  207. bb.plain("Image CVE JSON report stored in: %s" % manifest_name)
  208. }
  209. ROOTFS_POSTPROCESS_COMMAND:prepend = "${@'cve_check_write_rootfs_manifest ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
  210. do_rootfs[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
  211. do_populate_sdk[recrdeptask] += "${@'do_cve_check' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
  212. def cve_is_ignored(d, cve_data, cve):
  213. if cve not in cve_data:
  214. return False
  215. if cve_data[cve]['abbrev-status'] == "Ignored":
  216. return True
  217. return False
  218. def cve_is_patched(d, cve_data, cve):
  219. if cve not in cve_data:
  220. return False
  221. if cve_data[cve]['abbrev-status'] == "Patched":
  222. return True
  223. return False
  224. def cve_update(d, cve_data, cve, entry):
  225. # If no entry, just add it
  226. if cve not in cve_data:
  227. cve_data[cve] = entry
  228. return
  229. # If we are updating, there might be change in the status
  230. bb.debug(1, "Trying CVE entry update for %s from %s to %s" % (cve, cve_data[cve]['abbrev-status'], entry['abbrev-status']))
  231. if cve_data[cve]['abbrev-status'] == "Unknown":
  232. cve_data[cve] = entry
  233. return
  234. if cve_data[cve]['abbrev-status'] == entry['abbrev-status']:
  235. return
  236. # Update like in {'abbrev-status': 'Patched', 'status': 'version-not-in-range'} to {'abbrev-status': 'Unpatched', 'status': 'version-in-range'}
  237. if entry['abbrev-status'] == "Unpatched" and cve_data[cve]['abbrev-status'] == "Patched":
  238. if entry['status'] == "version-in-range" and cve_data[cve]['status'] == "version-not-in-range":
  239. # New result from the scan, vulnerable
  240. cve_data[cve] = entry
  241. bb.debug(1, "CVE entry %s update from Patched to Unpatched from the scan result" % cve)
  242. return
  243. if entry['abbrev-status'] == "Patched" and cve_data[cve]['abbrev-status'] == "Unpatched":
  244. if entry['status'] == "version-not-in-range" and cve_data[cve]['status'] == "version-in-range":
  245. # Range does not match the scan, but we already have a vulnerable match, ignore
  246. bb.debug(1, "CVE entry %s update from Patched to Unpatched from the scan result - not applying" % cve)
  247. return
  248. # If we have an "Ignored", it has a priority
  249. if cve_data[cve]['abbrev-status'] == "Ignored":
  250. bb.debug(1, "CVE %s not updating because Ignored" % cve)
  251. return
  252. bb.warn("Unhandled CVE entry update for %s from %s to %s" % (cve, cve_data[cve], entry))
  253. def check_cves(d, cve_data):
  254. """
  255. Connect to the NVD database and find unpatched cves.
  256. """
  257. from oe.cve_check import Version, convert_cve_version, decode_cve_status
  258. pn = d.getVar("PN")
  259. real_pv = d.getVar("PV")
  260. suffix = d.getVar("CVE_VERSION_SUFFIX")
  261. cves_status = []
  262. cves_in_recipe = False
  263. # CVE_PRODUCT can contain more than one product (eg. curl/libcurl)
  264. products = d.getVar("CVE_PRODUCT").split()
  265. # If this has been unset then we're not scanning for CVEs here (for example, image recipes)
  266. if not products:
  267. return ([], [])
  268. pv = d.getVar("CVE_VERSION").split("+git")[0]
  269. # If the recipe has been skipped/ignored we return empty lists
  270. if pn in d.getVar("CVE_CHECK_SKIP_RECIPE").split():
  271. bb.note("Recipe has been skipped by cve-check")
  272. return ([], [])
  273. import sqlite3
  274. db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
  275. conn = sqlite3.connect(db_file, uri=True)
  276. # For each of the known product names (e.g. curl has CPEs using curl and libcurl)...
  277. for product in products:
  278. cves_in_product = False
  279. if ":" in product:
  280. vendor, product = product.split(":", 1)
  281. else:
  282. vendor = "%"
  283. # Find all relevant CVE IDs.
  284. cve_cursor = conn.execute("SELECT DISTINCT ID FROM PRODUCTS WHERE PRODUCT IS ? AND VENDOR LIKE ?", (product, vendor))
  285. for cverow in cve_cursor:
  286. cve = cverow[0]
  287. # Write status once only for each product
  288. if not cves_in_product:
  289. cves_status.append([product, True])
  290. cves_in_product = True
  291. cves_in_recipe = True
  292. if cve_is_ignored(d, cve_data, cve):
  293. bb.note("%s-%s ignores %s" % (product, pv, cve))
  294. continue
  295. elif cve_is_patched(d, cve_data, cve):
  296. bb.note("%s has been patched" % (cve))
  297. continue
  298. vulnerable = False
  299. ignored = False
  300. product_cursor = conn.execute("SELECT * FROM PRODUCTS WHERE ID IS ? AND PRODUCT IS ? AND VENDOR LIKE ?", (cve, product, vendor))
  301. for row in product_cursor:
  302. (_, _, _, version_start, operator_start, version_end, operator_end) = row
  303. #bb.debug(2, "Evaluating row " + str(row))
  304. if cve_is_ignored(d, cve_data, cve):
  305. ignored = True
  306. version_start = convert_cve_version(version_start)
  307. version_end = convert_cve_version(version_end)
  308. if (operator_start == '=' and pv == version_start) or version_start == '-':
  309. vulnerable = True
  310. else:
  311. if operator_start:
  312. try:
  313. vulnerable_start = (operator_start == '>=' and Version(pv,suffix) >= Version(version_start,suffix))
  314. vulnerable_start |= (operator_start == '>' and Version(pv,suffix) > Version(version_start,suffix))
  315. except:
  316. bb.warn("%s: Failed to compare %s %s %s for %s" %
  317. (product, pv, operator_start, version_start, cve))
  318. vulnerable_start = False
  319. else:
  320. vulnerable_start = False
  321. if operator_end:
  322. try:
  323. vulnerable_end = (operator_end == '<=' and Version(pv,suffix) <= Version(version_end,suffix) )
  324. vulnerable_end |= (operator_end == '<' and Version(pv,suffix) < Version(version_end,suffix) )
  325. except:
  326. bb.warn("%s: Failed to compare %s %s %s for %s" %
  327. (product, pv, operator_end, version_end, cve))
  328. vulnerable_end = False
  329. else:
  330. vulnerable_end = False
  331. if operator_start and operator_end:
  332. vulnerable = vulnerable_start and vulnerable_end
  333. else:
  334. vulnerable = vulnerable_start or vulnerable_end
  335. if vulnerable:
  336. if ignored:
  337. bb.note("%s is ignored in %s-%s" % (cve, pn, real_pv))
  338. cve_update(d, cve_data, cve, {"abbrev-status": "Ignored"})
  339. else:
  340. bb.note("%s-%s is vulnerable to %s" % (pn, real_pv, cve))
  341. cve_update(d, cve_data, cve, {"abbrev-status": "Unpatched", "status": "version-in-range"})
  342. break
  343. product_cursor.close()
  344. if not vulnerable:
  345. bb.note("%s-%s is not vulnerable to %s" % (pn, real_pv, cve))
  346. cve_update(d, cve_data, cve, {"abbrev-status": "Patched", "status": "version-not-in-range"})
  347. cve_cursor.close()
  348. if not cves_in_product:
  349. bb.note("No CVE records found for product %s, pn %s" % (product, pn))
  350. cves_status.append([product, False])
  351. conn.close()
  352. if not cves_in_recipe:
  353. bb.note("No CVE records for products in recipe %s" % (pn))
  354. if d.getVar("CVE_CHECK_SHOW_WARNINGS") == "1":
  355. unpatched_cves = [cve for cve in cve_data if cve_data[cve]["abbrev-status"] == "Unpatched"]
  356. if unpatched_cves:
  357. bb.warn("Found unpatched CVE (%s)" % " ".join(unpatched_cves))
  358. return (cve_data, cves_status)
  359. def get_cve_info(d, cve_data):
  360. """
  361. Get CVE information from the database.
  362. """
  363. import sqlite3
  364. db_file = d.expand("file:${CVE_CHECK_DB_FILE}?mode=ro")
  365. conn = sqlite3.connect(db_file, uri=True)
  366. for cve in cve_data:
  367. cursor = conn.execute("SELECT * FROM NVD WHERE ID IS ?", (cve,))
  368. for row in cursor:
  369. # The CVE itdelf has been added already
  370. if row[0] not in cve_data:
  371. bb.note("CVE record %s not present" % row[0])
  372. continue
  373. #cve_data[row[0]] = {}
  374. cve_data[row[0]]["NVD-summary"] = row[1]
  375. cve_data[row[0]]["NVD-scorev2"] = row[2]
  376. cve_data[row[0]]["NVD-scorev3"] = row[3]
  377. cve_data[row[0]]["NVD-scorev4"] = row[4]
  378. cve_data[row[0]]["NVD-modified"] = row[5]
  379. cve_data[row[0]]["NVD-vector"] = row[6]
  380. cve_data[row[0]]["NVD-vectorString"] = row[7]
  381. cursor.close()
  382. conn.close()
  383. def cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file):
  384. """
  385. Write CVE information in the JSON format: to WORKDIR; and to
  386. CVE_CHECK_DIR, if CVE manifest if enabled, write fragment
  387. files that will be assembled at the end in cve_check_write_rootfs_manifest.
  388. """
  389. import json
  390. write_string = json.dumps(output, indent=2)
  391. with open(direct_file, "w") as f:
  392. bb.note("Writing file %s with CVE information" % direct_file)
  393. f.write(write_string)
  394. if d.getVar("CVE_CHECK_COPY_FILES") == "1":
  395. bb.utils.mkdirhier(os.path.dirname(deploy_file))
  396. with open(deploy_file, "w") as f:
  397. f.write(write_string)
  398. if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
  399. cvelogpath = d.getVar("CVE_CHECK_SUMMARY_DIR")
  400. index_path = d.getVar("CVE_CHECK_SUMMARY_INDEX_PATH")
  401. bb.utils.mkdirhier(cvelogpath)
  402. fragment_file = os.path.basename(deploy_file)
  403. fragment_path = os.path.join(cvelogpath, fragment_file)
  404. with open(fragment_path, "w") as f:
  405. f.write(write_string)
  406. with open(index_path, "a+") as f:
  407. f.write("%s\n" % fragment_path)
  408. def cve_write_data_json(d, cve_data, cve_status):
  409. """
  410. Prepare CVE data for the JSON format, then write it.
  411. """
  412. output = {"version":"1", "package": []}
  413. nvd_link = "https://nvd.nist.gov/vuln/detail/"
  414. fdir_name = d.getVar("FILE_DIRNAME")
  415. layer = fdir_name.split("/")[-3]
  416. include_layers = d.getVar("CVE_CHECK_LAYER_INCLUDELIST").split()
  417. exclude_layers = d.getVar("CVE_CHECK_LAYER_EXCLUDELIST").split()
  418. report_all = d.getVar("CVE_CHECK_REPORT_PATCHED") == "1"
  419. if exclude_layers and layer in exclude_layers:
  420. return
  421. if include_layers and layer not in include_layers:
  422. return
  423. product_data = []
  424. for s in cve_status:
  425. p = {"product": s[0], "cvesInRecord": "Yes"}
  426. if s[1] == False:
  427. p["cvesInRecord"] = "No"
  428. product_data.append(p)
  429. package_version = "%s%s" % (d.getVar("EXTENDPE"), d.getVar("PV"))
  430. package_data = {
  431. "name" : d.getVar("PN"),
  432. "layer" : layer,
  433. "version" : package_version,
  434. "products": product_data
  435. }
  436. cve_list = []
  437. for cve in sorted(cve_data):
  438. if not report_all and (cve_data[cve]["abbrev-status"] == "Patched" or cve_data[cve]["abbrev-status"] == "Ignored"):
  439. continue
  440. issue_link = "%s%s" % (nvd_link, cve)
  441. cve_item = {
  442. "id" : cve,
  443. "status" : cve_data[cve]["abbrev-status"],
  444. "link": issue_link,
  445. }
  446. if 'NVD-summary' in cve_data[cve]:
  447. cve_item["summary"] = cve_data[cve]["NVD-summary"]
  448. cve_item["scorev2"] = cve_data[cve]["NVD-scorev2"]
  449. cve_item["scorev3"] = cve_data[cve]["NVD-scorev3"]
  450. cve_item["scorev4"] = cve_data[cve]["NVD-scorev4"]
  451. cve_item["modified"] = cve_data[cve]["NVD-modified"]
  452. cve_item["vector"] = cve_data[cve]["NVD-vector"]
  453. cve_item["vectorString"] = cve_data[cve]["NVD-vectorString"]
  454. if 'status' in cve_data[cve]:
  455. cve_item["detail"] = cve_data[cve]["status"]
  456. if 'justification' in cve_data[cve]:
  457. cve_item["description"] = cve_data[cve]["justification"]
  458. if 'resource' in cve_data[cve]:
  459. cve_item["patch-file"] = cve_data[cve]["resource"]
  460. cve_list.append(cve_item)
  461. package_data["issue"] = cve_list
  462. output["package"].append(package_data)
  463. direct_file = d.getVar("CVE_CHECK_LOG_JSON")
  464. deploy_file = d.getVar("CVE_CHECK_RECIPE_FILE_JSON")
  465. manifest_file = d.getVar("CVE_CHECK_SUMMARY_FILE_NAME_JSON")
  466. cve_check_write_json_output(d, output, direct_file, deploy_file, manifest_file)
  467. def cve_write_data(d, cve_data, status):
  468. """
  469. Write CVE data in each enabled format.
  470. """
  471. if d.getVar("CVE_CHECK_FORMAT_JSON") == "1":
  472. cve_write_data_json(d, cve_data, status)