cve-check.bbclass 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # This class is used to check recipes against public CVEs.
  2. #
  3. # In order to use this class just inherit the class in the
  4. # local.conf file and it will add the cve_check task for
  5. # every recipe. The task can be used per recipe, per image,
  6. # or using the special cases "world" and "universe". The
  7. # cve_check task will print a warning for every unpatched
  8. # CVE found and generate a file in the recipe WORKDIR/cve
  9. # directory. If an image is build it will generate a report
  10. # in DEPLOY_DIR_IMAGE for all the packages used.
  11. #
  12. # Example:
  13. # bitbake -c cve_check openssl
  14. # bitbake core-image-sato
  15. # bitbake -k -c cve_check universe
  16. #
  17. # DISCLAIMER
  18. #
  19. # This class/tool is meant to be used as support and not
  20. # the only method to check against CVEs. Running this tool
  21. # doesn't guarantee your packages are free of CVEs.
  22. # The product name that the CVE database uses. Defaults to BPN, but may need to
  23. # be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff).
  24. CVE_PRODUCT ?= "${BPN}"
  25. CVE_CHECK_DB_DIR ?= "${DL_DIR}/CVE_CHECK"
  26. CVE_CHECK_DB_FILE ?= "${CVE_CHECK_DB_DIR}/nvd.db"
  27. CVE_CHECK_LOCAL_DIR ?= "${WORKDIR}/cve"
  28. CVE_CHECK_LOCAL_FILE ?= "${CVE_CHECK_LOCAL_DIR}/cve.log"
  29. CVE_CHECK_TMP_FILE ?= "${TMPDIR}/cve_check"
  30. CVE_CHECK_DIR ??= "${DEPLOY_DIR}/cve"
  31. CVE_CHECK_MANIFEST ?= "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.cve"
  32. CVE_CHECK_COPY_FILES ??= "1"
  33. CVE_CHECK_CREATE_MANIFEST ??= "1"
  34. # Whitelist for packages (PN)
  35. CVE_CHECK_PN_WHITELIST = "\
  36. glibc-locale \
  37. "
  38. # Whitelist for CVE and version of package
  39. CVE_CHECK_CVE_WHITELIST = "{\
  40. 'CVE-2014-2524': ('6.3','5.2',), \
  41. }"
  42. python do_cve_check () {
  43. """
  44. Check recipe for patched and unpatched CVEs
  45. """
  46. if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")):
  47. patched_cves = get_patches_cves(d)
  48. patched, unpatched = check_cves(d, patched_cves)
  49. if patched or unpatched:
  50. cve_data = get_cve_info(d, patched + unpatched)
  51. cve_write_data(d, patched, unpatched, cve_data)
  52. else:
  53. bb.note("Failed to update CVE database, skipping CVE check")
  54. }
  55. addtask cve_check after do_unpack before do_build
  56. do_cve_check[depends] = "cve-check-tool-native:do_populate_sysroot cve-check-tool-native:do_populate_cve_db"
  57. do_cve_check[nostamp] = "1"
  58. python cve_check_cleanup () {
  59. """
  60. Delete the file used to gather all the CVE information.
  61. """
  62. bb.utils.remove(e.data.getVar("CVE_CHECK_TMP_FILE"))
  63. }
  64. addhandler cve_check_cleanup
  65. cve_check_cleanup[eventmask] = "bb.cooker.CookerExit"
  66. python cve_check_write_rootfs_manifest () {
  67. """
  68. Create CVE manifest when building an image
  69. """
  70. import shutil
  71. if os.path.exists(d.getVar("CVE_CHECK_TMP_FILE")):
  72. bb.note("Writing rootfs CVE manifest")
  73. deploy_dir = d.getVar("DEPLOY_DIR_IMAGE")
  74. link_name = d.getVar("IMAGE_LINK_NAME")
  75. manifest_name = d.getVar("CVE_CHECK_MANIFEST")
  76. cve_tmp_file = d.getVar("CVE_CHECK_TMP_FILE")
  77. shutil.copyfile(cve_tmp_file, manifest_name)
  78. if manifest_name and os.path.exists(manifest_name):
  79. manifest_link = os.path.join(deploy_dir, "%s.cve" % link_name)
  80. # If we already have another manifest, update symlinks
  81. if os.path.exists(os.path.realpath(manifest_link)):
  82. os.remove(manifest_link)
  83. os.symlink(os.path.basename(manifest_name), manifest_link)
  84. bb.plain("Image CVE report stored in: %s" % manifest_name)
  85. }
  86. ROOTFS_POSTPROCESS_COMMAND_prepend = "${@'cve_check_write_rootfs_manifest; ' if d.getVar('CVE_CHECK_CREATE_MANIFEST') == '1' else ''}"
  87. def get_patches_cves(d):
  88. """
  89. Get patches that solve CVEs using the "CVE: " tag.
  90. """
  91. import re
  92. pn = d.getVar("PN")
  93. cve_match = re.compile("CVE:( CVE\-\d{4}\-\d+)+")
  94. patched_cves = set()
  95. bb.debug(2, "Looking for patches that solves CVEs for %s" % pn)
  96. for url in src_patches(d):
  97. patch_file = bb.fetch.decodeurl(url)[2]
  98. with open(patch_file, "r", encoding="utf-8") as f:
  99. try:
  100. patch_text = f.read()
  101. except UnicodeDecodeError:
  102. bb.debug(1, "Failed to read patch %s using UTF-8 encoding"
  103. " trying with iso8859-1" % patch_file)
  104. f.close()
  105. with open(patch_file, "r", encoding="iso8859-1") as f:
  106. patch_text = f.read()
  107. # Search for the "CVE: " line
  108. match = cve_match.search(patch_text)
  109. if match:
  110. # Get only the CVEs without the "CVE: " tag
  111. cves = patch_text[match.start()+5:match.end()]
  112. for cve in cves.split():
  113. bb.debug(2, "Patch %s solves %s" % (patch_file, cve))
  114. patched_cves.add(cve)
  115. else:
  116. bb.debug(2, "Patch %s doesn't solve CVEs" % patch_file)
  117. return patched_cves
  118. def check_cves(d, patched_cves):
  119. """
  120. Run cve-check-tool looking for patched and unpatched CVEs.
  121. """
  122. import ast, csv, tempfile, subprocess, io
  123. cves_patched = []
  124. cves_unpatched = []
  125. bpn = d.getVar("CVE_PRODUCT")
  126. pv = d.getVar("PV").split("git+")[0]
  127. cves = " ".join(patched_cves)
  128. cve_db_dir = d.getVar("CVE_CHECK_DB_DIR")
  129. cve_whitelist = ast.literal_eval(d.getVar("CVE_CHECK_CVE_WHITELIST"))
  130. cve_cmd = "cve-check-tool"
  131. cmd = [cve_cmd, "--no-html", "--csv", "--not-affected", "-t", "faux", "-d", cve_db_dir]
  132. # If the recipe has been whitlisted we return empty lists
  133. if d.getVar("PN") in d.getVar("CVE_CHECK_PN_WHITELIST").split():
  134. bb.note("Recipe has been whitelisted, skipping check")
  135. return ([], [])
  136. # It is needed to export the proxies to download the database using HTTP
  137. bb.utils.export_proxies(d)
  138. try:
  139. # Write the faux CSV file to be used with cve-check-tool
  140. fd, faux = tempfile.mkstemp(prefix="cve-faux-")
  141. with os.fdopen(fd, "w") as f:
  142. f.write("%s,%s,%s," % (bpn, pv, cves))
  143. cmd.append(faux)
  144. output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode("utf-8")
  145. bb.debug(2, "Output of command %s:\n%s" % ("\n".join(cmd), output))
  146. except subprocess.CalledProcessError as e:
  147. bb.warn("Couldn't check for CVEs: %s (output %s)" % (e, e.output))
  148. finally:
  149. os.remove(faux)
  150. for row in csv.reader(io.StringIO(output)):
  151. # Third row has the unpatched CVEs
  152. if row[2]:
  153. for cve in row[2].split():
  154. # Skip if the CVE has been whitlisted for the current version
  155. if pv in cve_whitelist.get(cve,[]):
  156. bb.note("%s-%s has been whitelisted for %s" % (bpn, pv, cve))
  157. else:
  158. cves_unpatched.append(cve)
  159. bb.debug(2, "%s-%s is not patched for %s" % (bpn, pv, cve))
  160. # Fourth row has patched CVEs
  161. if row[3]:
  162. for cve in row[3].split():
  163. cves_patched.append(cve)
  164. bb.debug(2, "%s-%s is patched for %s" % (bpn, pv, cve))
  165. return (cves_patched, cves_unpatched)
  166. def get_cve_info(d, cves):
  167. """
  168. Get CVE information from the database used by cve-check-tool.
  169. Unfortunately the only way to get CVE info is set the output to
  170. html (hard to parse) or query directly the database.
  171. """
  172. try:
  173. import sqlite3
  174. except ImportError:
  175. from pysqlite2 import dbapi2 as sqlite3
  176. cve_data = {}
  177. db_file = d.getVar("CVE_CHECK_DB_FILE")
  178. placeholder = ",".join("?" * len(cves))
  179. query = "SELECT * FROM NVD WHERE id IN (%s)" % placeholder
  180. conn = sqlite3.connect(db_file)
  181. cur = conn.cursor()
  182. for row in cur.execute(query, tuple(cves)):
  183. cve_data[row[0]] = {}
  184. cve_data[row[0]]["summary"] = row[1]
  185. cve_data[row[0]]["score"] = row[2]
  186. cve_data[row[0]]["modified"] = row[3]
  187. cve_data[row[0]]["vector"] = row[4]
  188. conn.close()
  189. return cve_data
  190. def cve_write_data(d, patched, unpatched, cve_data):
  191. """
  192. Write CVE information in WORKDIR; and to CVE_CHECK_DIR, and
  193. CVE manifest if enabled.
  194. """
  195. cve_file = d.getVar("CVE_CHECK_LOCAL_FILE")
  196. nvd_link = "https://web.nvd.nist.gov/view/vuln/detail?vulnId="
  197. write_string = ""
  198. unpatched_cves = []
  199. bb.utils.mkdirhier(d.getVar("CVE_CHECK_LOCAL_DIR"))
  200. for cve in sorted(cve_data):
  201. write_string += "PACKAGE NAME: %s\n" % d.getVar("PN")
  202. write_string += "PACKAGE VERSION: %s\n" % d.getVar("PV")
  203. write_string += "CVE: %s\n" % cve
  204. if cve in patched:
  205. write_string += "CVE STATUS: Patched\n"
  206. else:
  207. unpatched_cves.append(cve)
  208. write_string += "CVE STATUS: Unpatched\n"
  209. write_string += "CVE SUMMARY: %s\n" % cve_data[cve]["summary"]
  210. write_string += "CVSS v2 BASE SCORE: %s\n" % cve_data[cve]["score"]
  211. write_string += "VECTOR: %s\n" % cve_data[cve]["vector"]
  212. write_string += "MORE INFORMATION: %s%s\n\n" % (nvd_link, cve)
  213. if unpatched_cves:
  214. bb.warn("Found unpatched CVE (%s), for more information check %s" % (" ".join(unpatched_cves),cve_file))
  215. with open(cve_file, "w") as f:
  216. bb.note("Writing file %s with CVE information" % cve_file)
  217. f.write(write_string)
  218. if d.getVar("CVE_CHECK_COPY_FILES") == "1":
  219. cve_dir = d.getVar("CVE_CHECK_DIR")
  220. bb.utils.mkdirhier(cve_dir)
  221. deploy_file = os.path.join(cve_dir, d.getVar("PN"))
  222. with open(deploy_file, "w") as f:
  223. f.write(write_string)
  224. if d.getVar("CVE_CHECK_CREATE_MANIFEST") == "1":
  225. with open(d.getVar("CVE_CHECK_TMP_FILE"), "a") as f:
  226. f.write("%s" % write_string)