create-spdx-2.2.bbclass 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: GPL-2.0-only
  5. #
  6. inherit spdx-common
  7. SPDX_VERSION = "2.2"
  8. SPDX_ORG ??= "OpenEmbedded ()"
  9. SPDX_SUPPLIER ??= "Organization: ${SPDX_ORG}"
  10. SPDX_SUPPLIER[doc] = "The SPDX PackageSupplier field for SPDX packages created from \
  11. this recipe. For SPDX documents create using this class during the build, this \
  12. is the contact information for the person or organization who is doing the \
  13. build."
  14. SPDX_ARCHIVE_SOURCES ??= "0"
  15. SPDX_ARCHIVE_PACKAGED ??= "0"
  16. def get_namespace(d, name):
  17. import uuid
  18. namespace_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, d.getVar("SPDX_UUID_NAMESPACE"))
  19. return "%s/%s-%s" % (d.getVar("SPDX_NAMESPACE_PREFIX"), name, str(uuid.uuid5(namespace_uuid, name)))
  20. def create_annotation(d, comment):
  21. from datetime import datetime, timezone
  22. creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
  23. annotation = oe.spdx.SPDXAnnotation()
  24. annotation.annotationDate = creation_time
  25. annotation.annotationType = "OTHER"
  26. annotation.annotator = "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION"))
  27. annotation.comment = comment
  28. return annotation
  29. def recipe_spdx_is_native(d, recipe):
  30. return any(a.annotationType == "OTHER" and
  31. a.annotator == "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION")) and
  32. a.comment == "isNative" for a in recipe.annotations)
  33. def get_json_indent(d):
  34. if d.getVar("SPDX_PRETTY") == "1":
  35. return 2
  36. return None
  37. def convert_license_to_spdx(lic, license_data, document, d, existing={}):
  38. from pathlib import Path
  39. import oe.spdx
  40. extracted = {}
  41. def add_extracted_license(ident, name):
  42. nonlocal document
  43. if name in extracted:
  44. return
  45. extracted_info = oe.spdx.SPDXExtractedLicensingInfo()
  46. extracted_info.name = name
  47. extracted_info.licenseId = ident
  48. extracted_info.extractedText = None
  49. if name == "PD":
  50. # Special-case this.
  51. extracted_info.extractedText = "Software released to the public domain"
  52. else:
  53. # Seach for the license in COMMON_LICENSE_DIR and LICENSE_PATH
  54. for directory in [d.getVar('COMMON_LICENSE_DIR')] + (d.getVar('LICENSE_PATH') or '').split():
  55. try:
  56. with (Path(directory) / name).open(errors="replace") as f:
  57. extracted_info.extractedText = f.read()
  58. break
  59. except FileNotFoundError:
  60. pass
  61. if extracted_info.extractedText is None:
  62. # If it's not SPDX or PD, then NO_GENERIC_LICENSE must be set
  63. entry = d.getVarFlag('NO_GENERIC_LICENSE', name).split(';')
  64. filename = entry[0]
  65. params = {i.split('=')[0]: i.split('=')[1] for i in entry[1:] if '=' in i}
  66. beginline = int(params.get('beginline', 1))
  67. endline = params.get('endline', None)
  68. if endline:
  69. endline = int(endline)
  70. if filename:
  71. filename = d.expand("${S}/" + filename)
  72. with open(filename, errors="replace") as f:
  73. extracted_info.extractedText = "".join(line for idx, line in enumerate(f, 1) if beginline <= idx and idx <= (endline or idx))
  74. else:
  75. bb.fatal("Cannot find any text for license %s" % name)
  76. extracted[name] = extracted_info
  77. document.hasExtractedLicensingInfos.append(extracted_info)
  78. def convert(l):
  79. if l == "(" or l == ")":
  80. return l
  81. if l == "&":
  82. return "AND"
  83. if l == "|":
  84. return "OR"
  85. if l == "CLOSED":
  86. return "NONE"
  87. spdx_license = d.getVarFlag("SPDXLICENSEMAP", l) or l
  88. if spdx_license in license_data["licenses"]:
  89. return spdx_license
  90. try:
  91. spdx_license = existing[l]
  92. except KeyError:
  93. spdx_license = "LicenseRef-" + l
  94. add_extracted_license(spdx_license, l)
  95. return spdx_license
  96. lic_split = lic.replace("(", " ( ").replace(")", " ) ").replace("|", " | ").replace("&", " & ").split()
  97. return ' '.join(convert(l) for l in lic_split)
  98. def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archive=None, ignore_dirs=[], ignore_top_level_dirs=[]):
  99. from pathlib import Path
  100. import oe.spdx
  101. import oe.spdx_common
  102. import hashlib
  103. source_date_epoch = d.getVar("SOURCE_DATE_EPOCH")
  104. if source_date_epoch:
  105. source_date_epoch = int(source_date_epoch)
  106. sha1s = []
  107. spdx_files = []
  108. file_counter = 1
  109. for subdir, dirs, files in os.walk(topdir):
  110. dirs[:] = [d for d in dirs if d not in ignore_dirs]
  111. if subdir == str(topdir):
  112. dirs[:] = [d for d in dirs if d not in ignore_top_level_dirs]
  113. for file in files:
  114. filepath = Path(subdir) / file
  115. filename = str(filepath.relative_to(topdir))
  116. if not filepath.is_symlink() and filepath.is_file():
  117. spdx_file = oe.spdx.SPDXFile()
  118. spdx_file.SPDXID = get_spdxid(file_counter)
  119. for t in get_types(filepath):
  120. spdx_file.fileTypes.append(t)
  121. spdx_file.fileName = filename
  122. if archive is not None:
  123. with filepath.open("rb") as f:
  124. info = archive.gettarinfo(fileobj=f)
  125. info.name = filename
  126. info.uid = 0
  127. info.gid = 0
  128. info.uname = "root"
  129. info.gname = "root"
  130. if source_date_epoch is not None and info.mtime > source_date_epoch:
  131. info.mtime = source_date_epoch
  132. archive.addfile(info, f)
  133. sha1 = bb.utils.sha1_file(filepath)
  134. sha1s.append(sha1)
  135. spdx_file.checksums.append(oe.spdx.SPDXChecksum(
  136. algorithm="SHA1",
  137. checksumValue=sha1,
  138. ))
  139. spdx_file.checksums.append(oe.spdx.SPDXChecksum(
  140. algorithm="SHA256",
  141. checksumValue=bb.utils.sha256_file(filepath),
  142. ))
  143. if "SOURCE" in spdx_file.fileTypes:
  144. extracted_lics = oe.spdx_common.extract_licenses(filepath)
  145. if extracted_lics:
  146. spdx_file.licenseInfoInFiles = extracted_lics
  147. doc.files.append(spdx_file)
  148. doc.add_relationship(spdx_pkg, "CONTAINS", spdx_file)
  149. spdx_pkg.hasFiles.append(spdx_file.SPDXID)
  150. spdx_files.append(spdx_file)
  151. file_counter += 1
  152. sha1s.sort()
  153. verifier = hashlib.sha1()
  154. for v in sha1s:
  155. verifier.update(v.encode("utf-8"))
  156. spdx_pkg.packageVerificationCode.packageVerificationCodeValue = verifier.hexdigest()
  157. return spdx_files
  158. def add_package_sources_from_debug(d, package_doc, spdx_package, package, package_files, sources):
  159. from pathlib import Path
  160. import hashlib
  161. import oe.packagedata
  162. import oe.spdx
  163. debug_search_paths = [
  164. Path(d.getVar('PKGD')),
  165. Path(d.getVar('STAGING_DIR_TARGET')),
  166. Path(d.getVar('STAGING_DIR_NATIVE')),
  167. Path(d.getVar('STAGING_KERNEL_DIR')),
  168. ]
  169. pkg_data = oe.packagedata.read_subpkgdata_extended(package, d)
  170. if pkg_data is None:
  171. return
  172. for file_path, file_data in pkg_data["files_info"].items():
  173. if not "debugsrc" in file_data:
  174. continue
  175. for pkg_file in package_files:
  176. if file_path.lstrip("/") == pkg_file.fileName.lstrip("/"):
  177. break
  178. else:
  179. bb.fatal("No package file found for %s in %s; SPDX found: %s" % (str(file_path), package,
  180. " ".join(p.fileName for p in package_files)))
  181. continue
  182. for debugsrc in file_data["debugsrc"]:
  183. ref_id = "NOASSERTION"
  184. for search in debug_search_paths:
  185. if debugsrc.startswith("/usr/src/kernel"):
  186. debugsrc_path = search / debugsrc.replace('/usr/src/kernel/', '')
  187. else:
  188. debugsrc_path = search / debugsrc.lstrip("/")
  189. # We can only hash files below, skip directories, links, etc.
  190. if not os.path.isfile(debugsrc_path):
  191. continue
  192. file_sha256 = bb.utils.sha256_file(debugsrc_path)
  193. if file_sha256 in sources:
  194. source_file = sources[file_sha256]
  195. doc_ref = package_doc.find_external_document_ref(source_file.doc.documentNamespace)
  196. if doc_ref is None:
  197. doc_ref = oe.spdx.SPDXExternalDocumentRef()
  198. doc_ref.externalDocumentId = "DocumentRef-dependency-" + source_file.doc.name
  199. doc_ref.spdxDocument = source_file.doc.documentNamespace
  200. doc_ref.checksum.algorithm = "SHA1"
  201. doc_ref.checksum.checksumValue = source_file.doc_sha1
  202. package_doc.externalDocumentRefs.append(doc_ref)
  203. ref_id = "%s:%s" % (doc_ref.externalDocumentId, source_file.file.SPDXID)
  204. else:
  205. bb.debug(1, "Debug source %s with SHA256 %s not found in any dependency" % (str(debugsrc_path), file_sha256))
  206. break
  207. else:
  208. bb.debug(1, "Debug source %s not found" % debugsrc)
  209. package_doc.add_relationship(pkg_file, "GENERATED_FROM", ref_id, comment=debugsrc)
  210. add_package_sources_from_debug[vardepsexclude] += "STAGING_KERNEL_DIR"
  211. def collect_dep_recipes(d, doc, spdx_recipe):
  212. import json
  213. from pathlib import Path
  214. import oe.sbom
  215. import oe.spdx
  216. import oe.spdx_common
  217. deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
  218. package_archs = d.getVar("SPDX_MULTILIB_SSTATE_ARCHS").split()
  219. package_archs.reverse()
  220. dep_recipes = []
  221. deps = oe.spdx_common.get_spdx_deps(d)
  222. for dep in deps:
  223. # If this dependency is not calculated in the taskhash skip it.
  224. # Otherwise, it can result in broken links since this task won't
  225. # rebuild and see the new SPDX ID if the dependency changes
  226. if not dep.in_taskhash:
  227. continue
  228. dep_recipe_path = oe.sbom.doc_find_by_hashfn(deploy_dir_spdx, package_archs, "recipe-" + dep.pn, dep.hashfn)
  229. if not dep_recipe_path:
  230. bb.fatal("Cannot find any SPDX file for recipe %s, %s" % (dep.pn, dep.hashfn))
  231. spdx_dep_doc, spdx_dep_sha1 = oe.sbom.read_doc(dep_recipe_path)
  232. for pkg in spdx_dep_doc.packages:
  233. if pkg.name == dep.pn:
  234. spdx_dep_recipe = pkg
  235. break
  236. else:
  237. continue
  238. dep_recipes.append(oe.sbom.DepRecipe(spdx_dep_doc, spdx_dep_sha1, spdx_dep_recipe))
  239. dep_recipe_ref = oe.spdx.SPDXExternalDocumentRef()
  240. dep_recipe_ref.externalDocumentId = "DocumentRef-dependency-" + spdx_dep_doc.name
  241. dep_recipe_ref.spdxDocument = spdx_dep_doc.documentNamespace
  242. dep_recipe_ref.checksum.algorithm = "SHA1"
  243. dep_recipe_ref.checksum.checksumValue = spdx_dep_sha1
  244. doc.externalDocumentRefs.append(dep_recipe_ref)
  245. doc.add_relationship(
  246. "%s:%s" % (dep_recipe_ref.externalDocumentId, spdx_dep_recipe.SPDXID),
  247. "BUILD_DEPENDENCY_OF",
  248. spdx_recipe
  249. )
  250. return dep_recipes
  251. collect_dep_recipes[vardepsexclude] = "SPDX_MULTILIB_SSTATE_ARCHS"
  252. def collect_dep_sources(d, dep_recipes):
  253. import oe.sbom
  254. sources = {}
  255. for dep in dep_recipes:
  256. # Don't collect sources from native recipes as they
  257. # match non-native sources also.
  258. if recipe_spdx_is_native(d, dep.recipe):
  259. continue
  260. recipe_files = set(dep.recipe.hasFiles)
  261. for spdx_file in dep.doc.files:
  262. if spdx_file.SPDXID not in recipe_files:
  263. continue
  264. if "SOURCE" in spdx_file.fileTypes:
  265. for checksum in spdx_file.checksums:
  266. if checksum.algorithm == "SHA256":
  267. sources[checksum.checksumValue] = oe.sbom.DepSource(dep.doc, dep.doc_sha1, dep.recipe, spdx_file)
  268. break
  269. return sources
  270. def add_download_packages(d, doc, recipe):
  271. import os.path
  272. from bb.fetch2 import decodeurl, CHECKSUM_LIST
  273. import bb.process
  274. import oe.spdx
  275. import oe.sbom
  276. for download_idx, src_uri in enumerate(d.getVar('SRC_URI').split()):
  277. f = bb.fetch2.FetchData(src_uri, d)
  278. package = oe.spdx.SPDXPackage()
  279. package.name = "%s-source-%d" % (d.getVar("PN"), download_idx + 1)
  280. package.SPDXID = oe.sbom.get_download_spdxid(d, download_idx + 1)
  281. if f.type == "file":
  282. continue
  283. if f.method.supports_checksum(f):
  284. for checksum_id in CHECKSUM_LIST:
  285. if checksum_id.upper() not in oe.spdx.SPDXPackage.ALLOWED_CHECKSUMS:
  286. continue
  287. expected_checksum = getattr(f, "%s_expected" % checksum_id)
  288. if expected_checksum is None:
  289. continue
  290. c = oe.spdx.SPDXChecksum()
  291. c.algorithm = checksum_id.upper()
  292. c.checksumValue = expected_checksum
  293. package.checksums.append(c)
  294. package.downloadLocation = oe.spdx_common.fetch_data_to_uri(f, f.name)
  295. doc.packages.append(package)
  296. doc.add_relationship(doc, "DESCRIBES", package)
  297. # In the future, we might be able to do more fancy dependencies,
  298. # but this should be sufficient for now
  299. doc.add_relationship(package, "BUILD_DEPENDENCY_OF", recipe)
  300. def get_license_list_version(license_data, d):
  301. # Newer versions of the SPDX license list are SemVer ("MAJOR.MINOR.MICRO"),
  302. # but SPDX 2 only uses "MAJOR.MINOR".
  303. return ".".join(license_data["licenseListVersion"].split(".")[:2])
  304. python do_create_spdx() {
  305. from datetime import datetime, timezone
  306. import oe.sbom
  307. import oe.spdx
  308. import oe.spdx_common
  309. import uuid
  310. from pathlib import Path
  311. from contextlib import contextmanager
  312. import oe.cve_check
  313. license_data = oe.spdx_common.load_spdx_license_data(d)
  314. @contextmanager
  315. def optional_tarfile(name, guard, mode="w"):
  316. import tarfile
  317. import bb.compress.zstd
  318. num_threads = int(d.getVar("BB_NUMBER_THREADS"))
  319. if guard:
  320. name.parent.mkdir(parents=True, exist_ok=True)
  321. with bb.compress.zstd.open(name, mode=mode + "b", num_threads=num_threads) as f:
  322. with tarfile.open(fileobj=f, mode=mode + "|") as tf:
  323. yield tf
  324. else:
  325. yield None
  326. deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
  327. spdx_workdir = Path(d.getVar("SPDXWORK"))
  328. include_sources = d.getVar("SPDX_INCLUDE_SOURCES") == "1"
  329. archive_sources = d.getVar("SPDX_ARCHIVE_SOURCES") == "1"
  330. archive_packaged = d.getVar("SPDX_ARCHIVE_PACKAGED") == "1"
  331. pkg_arch = d.getVar("SSTATE_PKGARCH")
  332. creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
  333. doc = oe.spdx.SPDXDocument()
  334. doc.name = "recipe-" + d.getVar("PN")
  335. doc.documentNamespace = get_namespace(d, doc.name)
  336. doc.creationInfo.created = creation_time
  337. doc.creationInfo.comment = "This document was created by analyzing recipe files during the build."
  338. doc.creationInfo.licenseListVersion = get_license_list_version(license_data, d)
  339. doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
  340. doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
  341. doc.creationInfo.creators.append("Person: N/A ()")
  342. recipe = oe.spdx.SPDXPackage()
  343. recipe.name = d.getVar("PN")
  344. recipe.versionInfo = d.getVar("PV")
  345. recipe.SPDXID = oe.sbom.get_recipe_spdxid(d)
  346. recipe.supplier = d.getVar("SPDX_SUPPLIER")
  347. if bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d):
  348. recipe.annotations.append(create_annotation(d, "isNative"))
  349. homepage = d.getVar("HOMEPAGE")
  350. if homepage:
  351. recipe.homepage = homepage
  352. license = d.getVar("LICENSE")
  353. if license:
  354. recipe.licenseDeclared = convert_license_to_spdx(license, license_data, doc, d)
  355. summary = d.getVar("SUMMARY")
  356. if summary:
  357. recipe.summary = summary
  358. description = d.getVar("DESCRIPTION")
  359. if description:
  360. recipe.description = description
  361. if d.getVar("SPDX_CUSTOM_ANNOTATION_VARS"):
  362. for var in d.getVar('SPDX_CUSTOM_ANNOTATION_VARS').split():
  363. recipe.annotations.append(create_annotation(d, var + "=" + d.getVar(var)))
  364. # Some CVEs may be patched during the build process without incrementing the version number,
  365. # so querying for CVEs based on the CPE id can lead to false positives. To account for this,
  366. # save the CVEs fixed by patches to source information field in the SPDX.
  367. patched_cves = oe.cve_check.get_patched_cves(d)
  368. patched_cves = list(patched_cves)
  369. patched_cves = ' '.join(patched_cves)
  370. if patched_cves:
  371. recipe.sourceInfo = "CVEs fixed: " + patched_cves
  372. cpe_ids = oe.cve_check.get_cpe_ids(d.getVar("CVE_PRODUCT"), d.getVar("CVE_VERSION"))
  373. if cpe_ids:
  374. for cpe_id in cpe_ids:
  375. cpe = oe.spdx.SPDXExternalReference()
  376. cpe.referenceCategory = "SECURITY"
  377. cpe.referenceType = "http://spdx.org/rdf/references/cpe23Type"
  378. cpe.referenceLocator = cpe_id
  379. recipe.externalRefs.append(cpe)
  380. doc.packages.append(recipe)
  381. doc.add_relationship(doc, "DESCRIBES", recipe)
  382. add_download_packages(d, doc, recipe)
  383. if oe.spdx_common.process_sources(d) and include_sources:
  384. recipe_archive = deploy_dir_spdx / "recipes" / (doc.name + ".tar.zst")
  385. with optional_tarfile(recipe_archive, archive_sources) as archive:
  386. oe.spdx_common.get_patched_src(d)
  387. add_package_files(
  388. d,
  389. doc,
  390. recipe,
  391. spdx_workdir,
  392. lambda file_counter: "SPDXRef-SourceFile-%s-%d" % (d.getVar("PN"), file_counter),
  393. lambda filepath: ["SOURCE"],
  394. ignore_dirs=[".git"],
  395. ignore_top_level_dirs=["temp"],
  396. archive=archive,
  397. )
  398. if archive is not None:
  399. recipe.packageFileName = str(recipe_archive.name)
  400. dep_recipes = collect_dep_recipes(d, doc, recipe)
  401. doc_sha1 = oe.sbom.write_doc(d, doc, pkg_arch, "recipes", indent=get_json_indent(d))
  402. dep_recipes.append(oe.sbom.DepRecipe(doc, doc_sha1, recipe))
  403. recipe_ref = oe.spdx.SPDXExternalDocumentRef()
  404. recipe_ref.externalDocumentId = "DocumentRef-recipe-" + recipe.name
  405. recipe_ref.spdxDocument = doc.documentNamespace
  406. recipe_ref.checksum.algorithm = "SHA1"
  407. recipe_ref.checksum.checksumValue = doc_sha1
  408. sources = collect_dep_sources(d, dep_recipes)
  409. found_licenses = {license.name:recipe_ref.externalDocumentId + ":" + license.licenseId for license in doc.hasExtractedLicensingInfos}
  410. if not recipe_spdx_is_native(d, recipe):
  411. bb.build.exec_func("read_subpackage_metadata", d)
  412. pkgdest = Path(d.getVar("PKGDEST"))
  413. for package in d.getVar("PACKAGES").split():
  414. if not oe.packagedata.packaged(package, d):
  415. continue
  416. package_doc = oe.spdx.SPDXDocument()
  417. pkg_name = d.getVar("PKG:%s" % package) or package
  418. package_doc.name = pkg_name
  419. package_doc.documentNamespace = get_namespace(d, package_doc.name)
  420. package_doc.creationInfo.created = creation_time
  421. package_doc.creationInfo.comment = "This document was created by analyzing packages created during the build."
  422. package_doc.creationInfo.licenseListVersion = get_license_list_version(license_data, d)
  423. package_doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
  424. package_doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
  425. package_doc.creationInfo.creators.append("Person: N/A ()")
  426. package_doc.externalDocumentRefs.append(recipe_ref)
  427. package_license = d.getVar("LICENSE:%s" % package) or d.getVar("LICENSE")
  428. spdx_package = oe.spdx.SPDXPackage()
  429. spdx_package.SPDXID = oe.sbom.get_package_spdxid(pkg_name)
  430. spdx_package.name = pkg_name
  431. spdx_package.versionInfo = d.getVar("PV")
  432. spdx_package.licenseDeclared = convert_license_to_spdx(package_license, license_data, package_doc, d, found_licenses)
  433. spdx_package.supplier = d.getVar("SPDX_SUPPLIER")
  434. package_doc.packages.append(spdx_package)
  435. package_doc.add_relationship(spdx_package, "GENERATED_FROM", "%s:%s" % (recipe_ref.externalDocumentId, recipe.SPDXID))
  436. package_doc.add_relationship(package_doc, "DESCRIBES", spdx_package)
  437. package_archive = deploy_dir_spdx / "packages" / (package_doc.name + ".tar.zst")
  438. with optional_tarfile(package_archive, archive_packaged) as archive:
  439. package_files = add_package_files(
  440. d,
  441. package_doc,
  442. spdx_package,
  443. pkgdest / package,
  444. lambda file_counter: oe.sbom.get_packaged_file_spdxid(pkg_name, file_counter),
  445. lambda filepath: ["BINARY"],
  446. ignore_top_level_dirs=['CONTROL', 'DEBIAN'],
  447. archive=archive,
  448. )
  449. if archive is not None:
  450. spdx_package.packageFileName = str(package_archive.name)
  451. add_package_sources_from_debug(d, package_doc, spdx_package, package, package_files, sources)
  452. oe.sbom.write_doc(d, package_doc, pkg_arch, "packages", indent=get_json_indent(d))
  453. }
  454. do_create_spdx[vardepsexclude] += "BB_NUMBER_THREADS"
  455. # NOTE: depending on do_unpack is a hack that is necessary to get it's dependencies for archive the source
  456. addtask do_create_spdx after do_package do_packagedata do_unpack do_collect_spdx_deps before do_populate_sdk do_build do_rm_work
  457. SSTATETASKS += "do_create_spdx"
  458. do_create_spdx[sstate-inputdirs] = "${SPDXDEPLOY}"
  459. do_create_spdx[sstate-outputdirs] = "${DEPLOY_DIR_SPDX}"
  460. python do_create_spdx_setscene () {
  461. sstate_setscene(d)
  462. }
  463. addtask do_create_spdx_setscene
  464. do_create_spdx[dirs] = "${SPDXWORK}"
  465. do_create_spdx[cleandirs] = "${SPDXDEPLOY} ${SPDXWORK}"
  466. do_create_spdx[depends] += " \
  467. ${PATCHDEPENDENCY} \
  468. ${@create_spdx_source_deps(d)} \
  469. "
  470. python do_create_runtime_spdx() {
  471. from datetime import datetime, timezone
  472. import oe.sbom
  473. import oe.spdx
  474. import oe.spdx_common
  475. import oe.packagedata
  476. from pathlib import Path
  477. deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
  478. spdx_deploy = Path(d.getVar("SPDXRUNTIMEDEPLOY"))
  479. is_native = bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d)
  480. creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
  481. license_data = oe.spdx_common.load_spdx_license_data(d)
  482. providers = oe.spdx_common.collect_package_providers(d)
  483. pkg_arch = d.getVar("SSTATE_PKGARCH")
  484. package_archs = d.getVar("SPDX_MULTILIB_SSTATE_ARCHS").split()
  485. package_archs.reverse()
  486. if not is_native:
  487. bb.build.exec_func("read_subpackage_metadata", d)
  488. dep_package_cache = {}
  489. pkgdest = Path(d.getVar("PKGDEST"))
  490. for package in d.getVar("PACKAGES").split():
  491. localdata = bb.data.createCopy(d)
  492. pkg_name = d.getVar("PKG:%s" % package) or package
  493. localdata.setVar("PKG", pkg_name)
  494. localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + package)
  495. if not oe.packagedata.packaged(package, localdata):
  496. continue
  497. pkg_spdx_path = oe.sbom.doc_path(deploy_dir_spdx, pkg_name, pkg_arch, "packages")
  498. package_doc, package_doc_sha1 = oe.sbom.read_doc(pkg_spdx_path)
  499. for p in package_doc.packages:
  500. if p.name == pkg_name:
  501. spdx_package = p
  502. break
  503. else:
  504. bb.fatal("Package '%s' not found in %s" % (pkg_name, pkg_spdx_path))
  505. runtime_doc = oe.spdx.SPDXDocument()
  506. runtime_doc.name = "runtime-" + pkg_name
  507. runtime_doc.documentNamespace = get_namespace(localdata, runtime_doc.name)
  508. runtime_doc.creationInfo.created = creation_time
  509. runtime_doc.creationInfo.comment = "This document was created by analyzing package runtime dependencies."
  510. runtime_doc.creationInfo.licenseListVersion = get_license_list_version(license_data, d)
  511. runtime_doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
  512. runtime_doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
  513. runtime_doc.creationInfo.creators.append("Person: N/A ()")
  514. package_ref = oe.spdx.SPDXExternalDocumentRef()
  515. package_ref.externalDocumentId = "DocumentRef-package-" + package
  516. package_ref.spdxDocument = package_doc.documentNamespace
  517. package_ref.checksum.algorithm = "SHA1"
  518. package_ref.checksum.checksumValue = package_doc_sha1
  519. runtime_doc.externalDocumentRefs.append(package_ref)
  520. runtime_doc.add_relationship(
  521. runtime_doc.SPDXID,
  522. "AMENDS",
  523. "%s:%s" % (package_ref.externalDocumentId, package_doc.SPDXID)
  524. )
  525. deps = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
  526. seen_deps = set()
  527. for dep, _ in deps.items():
  528. if dep in seen_deps:
  529. continue
  530. if dep not in providers:
  531. continue
  532. (dep, dep_hashfn) = providers[dep]
  533. if not oe.packagedata.packaged(dep, localdata):
  534. continue
  535. dep_pkg_data = oe.packagedata.read_subpkgdata_dict(dep, d)
  536. dep_pkg = dep_pkg_data["PKG"]
  537. if dep in dep_package_cache:
  538. (dep_spdx_package, dep_package_ref) = dep_package_cache[dep]
  539. else:
  540. dep_path = oe.sbom.doc_find_by_hashfn(deploy_dir_spdx, package_archs, dep_pkg, dep_hashfn)
  541. if not dep_path:
  542. bb.fatal("No SPDX file found for package %s, %s" % (dep_pkg, dep_hashfn))
  543. spdx_dep_doc, spdx_dep_sha1 = oe.sbom.read_doc(dep_path)
  544. for pkg in spdx_dep_doc.packages:
  545. if pkg.name == dep_pkg:
  546. dep_spdx_package = pkg
  547. break
  548. else:
  549. bb.fatal("Package '%s' not found in %s" % (dep_pkg, dep_path))
  550. dep_package_ref = oe.spdx.SPDXExternalDocumentRef()
  551. dep_package_ref.externalDocumentId = "DocumentRef-runtime-dependency-" + spdx_dep_doc.name
  552. dep_package_ref.spdxDocument = spdx_dep_doc.documentNamespace
  553. dep_package_ref.checksum.algorithm = "SHA1"
  554. dep_package_ref.checksum.checksumValue = spdx_dep_sha1
  555. dep_package_cache[dep] = (dep_spdx_package, dep_package_ref)
  556. runtime_doc.externalDocumentRefs.append(dep_package_ref)
  557. runtime_doc.add_relationship(
  558. "%s:%s" % (dep_package_ref.externalDocumentId, dep_spdx_package.SPDXID),
  559. "RUNTIME_DEPENDENCY_OF",
  560. "%s:%s" % (package_ref.externalDocumentId, spdx_package.SPDXID)
  561. )
  562. seen_deps.add(dep)
  563. oe.sbom.write_doc(d, runtime_doc, pkg_arch, "runtime", spdx_deploy, indent=get_json_indent(d))
  564. }
  565. do_create_runtime_spdx[vardepsexclude] += "OVERRIDES SPDX_MULTILIB_SSTATE_ARCHS"
  566. addtask do_create_runtime_spdx after do_create_spdx before do_build do_rm_work
  567. SSTATETASKS += "do_create_runtime_spdx"
  568. do_create_runtime_spdx[sstate-inputdirs] = "${SPDXRUNTIMEDEPLOY}"
  569. do_create_runtime_spdx[sstate-outputdirs] = "${DEPLOY_DIR_SPDX}"
  570. python do_create_runtime_spdx_setscene () {
  571. sstate_setscene(d)
  572. }
  573. addtask do_create_runtime_spdx_setscene
  574. do_create_runtime_spdx[dirs] = "${SPDXRUNTIMEDEPLOY}"
  575. do_create_runtime_spdx[cleandirs] = "${SPDXRUNTIMEDEPLOY}"
  576. do_create_runtime_spdx[rdeptask] = "do_create_spdx"
  577. do_rootfs[recrdeptask] += "do_create_spdx do_create_runtime_spdx"
  578. do_rootfs[cleandirs] += "${SPDXIMAGEWORK}"
  579. ROOTFS_POSTUNINSTALL_COMMAND =+ "image_combine_spdx"
  580. do_populate_sdk[recrdeptask] += "do_create_spdx do_create_runtime_spdx"
  581. do_populate_sdk[cleandirs] += "${SPDXSDKWORK}"
  582. POPULATE_SDK_POST_HOST_COMMAND:append:task-populate-sdk = " sdk_host_combine_spdx"
  583. POPULATE_SDK_POST_TARGET_COMMAND:append:task-populate-sdk = " sdk_target_combine_spdx"
  584. python image_combine_spdx() {
  585. import os
  586. import oe.sbom
  587. from pathlib import Path
  588. from oe.rootfs import image_list_installed_packages
  589. image_name = d.getVar("IMAGE_NAME")
  590. image_link_name = d.getVar("IMAGE_LINK_NAME")
  591. imgdeploydir = Path(d.getVar("IMGDEPLOYDIR"))
  592. img_spdxid = oe.sbom.get_image_spdxid(image_name)
  593. packages = image_list_installed_packages(d)
  594. combine_spdx(d, image_name, imgdeploydir, img_spdxid, packages, Path(d.getVar("SPDXIMAGEWORK")))
  595. def make_image_link(target_path, suffix):
  596. if image_link_name:
  597. link = imgdeploydir / (image_link_name + suffix)
  598. if link != target_path:
  599. link.symlink_to(os.path.relpath(target_path, link.parent))
  600. spdx_tar_path = imgdeploydir / (image_name + ".spdx.tar.zst")
  601. make_image_link(spdx_tar_path, ".spdx.tar.zst")
  602. }
  603. python sdk_host_combine_spdx() {
  604. sdk_combine_spdx(d, "host")
  605. }
  606. python sdk_target_combine_spdx() {
  607. sdk_combine_spdx(d, "target")
  608. }
  609. def sdk_combine_spdx(d, sdk_type):
  610. import oe.sbom
  611. from pathlib import Path
  612. from oe.sdk import sdk_list_installed_packages
  613. sdk_name = d.getVar("TOOLCHAIN_OUTPUTNAME") + "-" + sdk_type
  614. sdk_deploydir = Path(d.getVar("SDKDEPLOYDIR"))
  615. sdk_spdxid = oe.sbom.get_sdk_spdxid(sdk_name)
  616. sdk_packages = sdk_list_installed_packages(d, sdk_type == "target")
  617. combine_spdx(d, sdk_name, sdk_deploydir, sdk_spdxid, sdk_packages, Path(d.getVar('SPDXSDKWORK')))
  618. def combine_spdx(d, rootfs_name, rootfs_deploydir, rootfs_spdxid, packages, spdx_workdir):
  619. import os
  620. import oe.spdx
  621. import oe.sbom
  622. import oe.spdx_common
  623. import io
  624. import json
  625. from datetime import timezone, datetime
  626. from pathlib import Path
  627. import tarfile
  628. import bb.compress.zstd
  629. license_data = oe.spdx_common.load_spdx_license_data(d)
  630. providers = oe.spdx_common.collect_package_providers(d)
  631. package_archs = d.getVar("SPDX_MULTILIB_SSTATE_ARCHS").split()
  632. package_archs.reverse()
  633. creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
  634. deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
  635. source_date_epoch = d.getVar("SOURCE_DATE_EPOCH")
  636. doc = oe.spdx.SPDXDocument()
  637. doc.name = rootfs_name
  638. doc.documentNamespace = get_namespace(d, doc.name)
  639. doc.creationInfo.created = creation_time
  640. doc.creationInfo.comment = "This document was created by analyzing the source of the Yocto recipe during the build."
  641. doc.creationInfo.licenseListVersion = get_license_list_version(license_data, d)
  642. doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
  643. doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
  644. doc.creationInfo.creators.append("Person: N/A ()")
  645. image = oe.spdx.SPDXPackage()
  646. image.name = d.getVar("PN")
  647. image.versionInfo = d.getVar("PV")
  648. image.SPDXID = rootfs_spdxid
  649. image.supplier = d.getVar("SPDX_SUPPLIER")
  650. doc.packages.append(image)
  651. if packages:
  652. for name in sorted(packages.keys()):
  653. if name not in providers:
  654. bb.fatal("Unable to find SPDX provider for '%s'" % name)
  655. pkg_name, pkg_hashfn = providers[name]
  656. pkg_spdx_path = oe.sbom.doc_find_by_hashfn(deploy_dir_spdx, package_archs, pkg_name, pkg_hashfn)
  657. if not pkg_spdx_path:
  658. bb.fatal("No SPDX file found for package %s, %s" % (pkg_name, pkg_hashfn))
  659. pkg_doc, pkg_doc_sha1 = oe.sbom.read_doc(pkg_spdx_path)
  660. for p in pkg_doc.packages:
  661. if p.name == name:
  662. pkg_ref = oe.spdx.SPDXExternalDocumentRef()
  663. pkg_ref.externalDocumentId = "DocumentRef-%s" % pkg_doc.name
  664. pkg_ref.spdxDocument = pkg_doc.documentNamespace
  665. pkg_ref.checksum.algorithm = "SHA1"
  666. pkg_ref.checksum.checksumValue = pkg_doc_sha1
  667. doc.externalDocumentRefs.append(pkg_ref)
  668. doc.add_relationship(image, "CONTAINS", "%s:%s" % (pkg_ref.externalDocumentId, p.SPDXID))
  669. break
  670. else:
  671. bb.fatal("Unable to find package with name '%s' in SPDX file %s" % (name, pkg_spdx_path))
  672. runtime_spdx_path = oe.sbom.doc_find_by_hashfn(deploy_dir_spdx, package_archs, "runtime-" + name, pkg_hashfn)
  673. if not runtime_spdx_path:
  674. bb.fatal("No runtime SPDX document found for %s, %s" % (name, pkg_hashfn))
  675. runtime_doc, runtime_doc_sha1 = oe.sbom.read_doc(runtime_spdx_path)
  676. runtime_ref = oe.spdx.SPDXExternalDocumentRef()
  677. runtime_ref.externalDocumentId = "DocumentRef-%s" % runtime_doc.name
  678. runtime_ref.spdxDocument = runtime_doc.documentNamespace
  679. runtime_ref.checksum.algorithm = "SHA1"
  680. runtime_ref.checksum.checksumValue = runtime_doc_sha1
  681. # "OTHER" isn't ideal here, but I can't find a relationship that makes sense
  682. doc.externalDocumentRefs.append(runtime_ref)
  683. doc.add_relationship(
  684. image,
  685. "OTHER",
  686. "%s:%s" % (runtime_ref.externalDocumentId, runtime_doc.SPDXID),
  687. comment="Runtime dependencies for %s" % name
  688. )
  689. bb.utils.mkdirhier(spdx_workdir)
  690. image_spdx_path = spdx_workdir / (rootfs_name + ".spdx.json")
  691. with image_spdx_path.open("wb") as f:
  692. doc.to_json(f, sort_keys=True, indent=get_json_indent(d))
  693. num_threads = int(d.getVar("BB_NUMBER_THREADS"))
  694. visited_docs = set()
  695. index = {"documents": []}
  696. spdx_tar_path = rootfs_deploydir / (rootfs_name + ".spdx.tar.zst")
  697. with bb.compress.zstd.open(spdx_tar_path, "w", num_threads=num_threads) as f:
  698. with tarfile.open(fileobj=f, mode="w|") as tar:
  699. def collect_spdx_document(path):
  700. nonlocal tar
  701. nonlocal deploy_dir_spdx
  702. nonlocal source_date_epoch
  703. nonlocal index
  704. if path in visited_docs:
  705. return
  706. visited_docs.add(path)
  707. with path.open("rb") as f:
  708. doc, sha1 = oe.sbom.read_doc(f)
  709. f.seek(0)
  710. if doc.documentNamespace in visited_docs:
  711. return
  712. bb.note("Adding SPDX document %s" % path)
  713. visited_docs.add(doc.documentNamespace)
  714. info = tar.gettarinfo(fileobj=f)
  715. info.name = doc.name + ".spdx.json"
  716. info.uid = 0
  717. info.gid = 0
  718. info.uname = "root"
  719. info.gname = "root"
  720. if source_date_epoch is not None and info.mtime > int(source_date_epoch):
  721. info.mtime = int(source_date_epoch)
  722. tar.addfile(info, f)
  723. index["documents"].append({
  724. "filename": info.name,
  725. "documentNamespace": doc.documentNamespace,
  726. "sha1": sha1,
  727. })
  728. for ref in doc.externalDocumentRefs:
  729. ref_path = oe.sbom.doc_find_by_namespace(deploy_dir_spdx, package_archs, ref.spdxDocument)
  730. if not ref_path:
  731. bb.fatal("Cannot find any SPDX file for document %s" % ref.spdxDocument)
  732. collect_spdx_document(ref_path)
  733. collect_spdx_document(image_spdx_path)
  734. index["documents"].sort(key=lambda x: x["filename"])
  735. index_str = io.BytesIO(json.dumps(
  736. index,
  737. sort_keys=True,
  738. indent=get_json_indent(d),
  739. ).encode("utf-8"))
  740. info = tarfile.TarInfo()
  741. info.name = "index.json"
  742. info.size = len(index_str.getvalue())
  743. info.uid = 0
  744. info.gid = 0
  745. info.uname = "root"
  746. info.gname = "root"
  747. tar.addfile(info, fileobj=index_str)
  748. combine_spdx[vardepsexclude] += "BB_NUMBER_THREADS SPDX_MULTILIB_SSTATE_ARCHS"