blob: eb9535069a474c8a0e2becd7c2ce7c601f87a0b2 [file] [log] [blame]
Andrew Geissler5199d832021-09-24 16:47:35 -05001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
5DEPLOY_DIR_SPDX ??= "${DEPLOY_DIR}/spdx/${MACHINE}"
6
7# The product name that the CVE database uses. Defaults to BPN, but may need to
8# be overriden per recipe (for example tiff.bb sets CVE_PRODUCT=libtiff).
9CVE_PRODUCT ??= "${BPN}"
10CVE_VERSION ??= "${PV}"
11
12SPDXDIR ??= "${WORKDIR}/spdx"
13SPDXDEPLOY = "${SPDXDIR}/deploy"
14SPDXWORK = "${SPDXDIR}/work"
15
Patrick Williams93c203f2021-10-06 16:15:23 -050016SPDX_TOOL_NAME ??= "oe-spdx-creator"
17SPDX_TOOL_VERSION ??= "1.0"
18
Andrew Geissler5199d832021-09-24 16:47:35 -050019SPDXRUNTIMEDEPLOY = "${SPDXDIR}/runtime-deploy"
20
21SPDX_INCLUDE_SOURCES ??= "0"
22SPDX_INCLUDE_PACKAGED ??= "0"
23SPDX_ARCHIVE_SOURCES ??= "0"
24SPDX_ARCHIVE_PACKAGED ??= "0"
25
26SPDX_UUID_NAMESPACE ??= "sbom.openembedded.org"
27SPDX_NAMESPACE_PREFIX ??= "http://spdx.org/spdxdoc"
28
29SPDX_LICENSES ??= "${COREBASE}/meta/files/spdx-licenses.json"
30
Andrew Geissler595f6302022-01-24 19:11:47 +000031SPDX_ORG ??= "OpenEmbedded ()"
32
Andrew Geissler5199d832021-09-24 16:47:35 -050033do_image_complete[depends] = "virtual/kernel:do_create_spdx"
34
35def get_doc_namespace(d, doc):
36 import uuid
37 namespace_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, d.getVar("SPDX_UUID_NAMESPACE"))
38 return "%s/%s-%s" % (d.getVar("SPDX_NAMESPACE_PREFIX"), doc.name, str(uuid.uuid5(namespace_uuid, doc.name)))
39
Andrew Geisslereff27472021-10-29 15:35:00 -050040def create_annotation(d, comment):
41 from datetime import datetime, timezone
42
43 creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
44 annotation = oe.spdx.SPDXAnnotation()
45 annotation.annotationDate = creation_time
46 annotation.annotationType = "OTHER"
47 annotation.annotator = "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION"))
48 annotation.comment = comment
49 return annotation
50
Patrick Williams93c203f2021-10-06 16:15:23 -050051def recipe_spdx_is_native(d, recipe):
52 return any(a.annotationType == "OTHER" and
53 a.annotator == "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION")) and
54 a.comment == "isNative" for a in recipe.annotations)
Andrew Geissler5199d832021-09-24 16:47:35 -050055
Andrew Geissler595f6302022-01-24 19:11:47 +000056def is_work_shared_spdx(d):
57 return bb.data.inherits_class('kernel', d) or ('work-shared' in d.getVar('WORKDIR'))
Andrew Geissler5199d832021-09-24 16:47:35 -050058
59python() {
60 import json
61 if d.getVar("SPDX_LICENSE_DATA"):
62 return
63
64 with open(d.getVar("SPDX_LICENSES"), "r") as f:
65 data = json.load(f)
66 # Transform the license array to a dictionary
67 data["licenses"] = {l["licenseId"]: l for l in data["licenses"]}
68 d.setVar("SPDX_LICENSE_DATA", data)
69}
70
71def convert_license_to_spdx(lic, document, d, existing={}):
72 from pathlib import Path
73 import oe.spdx
74
75 available_licenses = d.getVar("AVAILABLE_LICENSES").split()
76 license_data = d.getVar("SPDX_LICENSE_DATA")
77 extracted = {}
78
79 def add_extracted_license(ident, name):
80 nonlocal document
81
82 if name in extracted:
83 return
84
85 extracted_info = oe.spdx.SPDXExtractedLicensingInfo()
86 extracted_info.name = name
87 extracted_info.licenseId = ident
88 extracted_info.extractedText = None
89
90 if name == "PD":
91 # Special-case this.
92 extracted_info.extractedText = "Software released to the public domain"
93 elif name in available_licenses:
94 # This license can be found in COMMON_LICENSE_DIR or LICENSE_PATH
Andrew Geissler595f6302022-01-24 19:11:47 +000095 for directory in [d.getVar('COMMON_LICENSE_DIR')] + (d.getVar('LICENSE_PATH') or '').split():
Andrew Geissler5199d832021-09-24 16:47:35 -050096 try:
97 with (Path(directory) / name).open(errors="replace") as f:
98 extracted_info.extractedText = f.read()
99 break
100 except FileNotFoundError:
101 pass
102 if extracted_info.extractedText is None:
103 # Error out, as the license was in available_licenses so should
104 # be on disk somewhere.
105 bb.error("Cannot find text for license %s" % name)
106 else:
107 # If it's not SPDX, or PD, or in available licenses, then NO_GENERIC_LICENSE must be set
108 filename = d.getVarFlag('NO_GENERIC_LICENSE', name)
109 if filename:
110 filename = d.expand("${S}/" + filename)
111 with open(filename, errors="replace") as f:
112 extracted_info.extractedText = f.read()
113 else:
114 bb.error("Cannot find any text for license %s" % name)
115
116 extracted[name] = extracted_info
117 document.hasExtractedLicensingInfos.append(extracted_info)
118
119 def convert(l):
120 if l == "(" or l == ")":
121 return l
122
123 if l == "&":
124 return "AND"
125
126 if l == "|":
127 return "OR"
128
129 if l == "CLOSED":
130 return "NONE"
131
132 spdx_license = d.getVarFlag("SPDXLICENSEMAP", l) or l
133 if spdx_license in license_data["licenses"]:
134 return spdx_license
135
136 try:
137 spdx_license = existing[l]
138 except KeyError:
139 spdx_license = "LicenseRef-" + l
140 add_extracted_license(spdx_license, l)
141
142 return spdx_license
143
144 lic_split = lic.replace("(", " ( ").replace(")", " ) ").split()
145
146 return ' '.join(convert(l) for l in lic_split)
147
Andrew Geissler5199d832021-09-24 16:47:35 -0500148def process_sources(d):
149 pn = d.getVar('PN')
150 assume_provided = (d.getVar("ASSUME_PROVIDED") or "").split()
151 if pn in assume_provided:
152 for p in d.getVar("PROVIDES").split():
153 if p != pn:
154 pn = p
155 break
156
157 # glibc-locale: do_fetch, do_unpack and do_patch tasks have been deleted,
158 # so avoid archiving source here.
159 if pn.startswith('glibc-locale'):
160 return False
161 if d.getVar('PN') == "libtool-cross":
162 return False
163 if d.getVar('PN') == "libgcc-initial":
164 return False
165 if d.getVar('PN') == "shadow-sysroot":
166 return False
167
168 # We just archive gcc-source for all the gcc related recipes
169 if d.getVar('BPN') in ['gcc', 'libgcc']:
170 bb.debug(1, 'spdx: There is bug in scan of %s is, do nothing' % pn)
171 return False
172
173 return True
174
175
176def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archive=None, ignore_dirs=[], ignore_top_level_dirs=[]):
177 from pathlib import Path
178 import oe.spdx
179 import hashlib
180
181 source_date_epoch = d.getVar("SOURCE_DATE_EPOCH")
182 if source_date_epoch:
183 source_date_epoch = int(source_date_epoch)
184
185 sha1s = []
186 spdx_files = []
187
188 file_counter = 1
189 for subdir, dirs, files in os.walk(topdir):
190 dirs[:] = [d for d in dirs if d not in ignore_dirs]
191 if subdir == str(topdir):
192 dirs[:] = [d for d in dirs if d not in ignore_top_level_dirs]
193
194 for file in files:
195 filepath = Path(subdir) / file
196 filename = str(filepath.relative_to(topdir))
197
198 if filepath.is_file() and not filepath.is_symlink():
199 spdx_file = oe.spdx.SPDXFile()
200 spdx_file.SPDXID = get_spdxid(file_counter)
201 for t in get_types(filepath):
202 spdx_file.fileTypes.append(t)
203 spdx_file.fileName = filename
204
205 if archive is not None:
206 with filepath.open("rb") as f:
207 info = archive.gettarinfo(fileobj=f)
208 info.name = filename
209 info.uid = 0
210 info.gid = 0
211 info.uname = "root"
212 info.gname = "root"
213
214 if source_date_epoch is not None and info.mtime > source_date_epoch:
215 info.mtime = source_date_epoch
216
217 archive.addfile(info, f)
218
219 sha1 = bb.utils.sha1_file(filepath)
220 sha1s.append(sha1)
221 spdx_file.checksums.append(oe.spdx.SPDXChecksum(
222 algorithm="SHA1",
223 checksumValue=sha1,
224 ))
225 spdx_file.checksums.append(oe.spdx.SPDXChecksum(
226 algorithm="SHA256",
227 checksumValue=bb.utils.sha256_file(filepath),
228 ))
229
230 doc.files.append(spdx_file)
231 doc.add_relationship(spdx_pkg, "CONTAINS", spdx_file)
232 spdx_pkg.hasFiles.append(spdx_file.SPDXID)
233
234 spdx_files.append(spdx_file)
235
236 file_counter += 1
237
238 sha1s.sort()
239 verifier = hashlib.sha1()
240 for v in sha1s:
241 verifier.update(v.encode("utf-8"))
242 spdx_pkg.packageVerificationCode.packageVerificationCodeValue = verifier.hexdigest()
243
244 return spdx_files
245
246
247def add_package_sources_from_debug(d, package_doc, spdx_package, package, package_files, sources):
248 from pathlib import Path
249 import hashlib
250 import oe.packagedata
251 import oe.spdx
252
253 debug_search_paths = [
254 Path(d.getVar('PKGD')),
255 Path(d.getVar('STAGING_DIR_TARGET')),
256 Path(d.getVar('STAGING_DIR_NATIVE')),
Andrew Geissler595f6302022-01-24 19:11:47 +0000257 Path(d.getVar('STAGING_KERNEL_DIR')),
Andrew Geissler5199d832021-09-24 16:47:35 -0500258 ]
259
260 pkg_data = oe.packagedata.read_subpkgdata_extended(package, d)
261
262 if pkg_data is None:
263 return
264
265 for file_path, file_data in pkg_data["files_info"].items():
266 if not "debugsrc" in file_data:
267 continue
268
269 for pkg_file in package_files:
270 if file_path.lstrip("/") == pkg_file.fileName.lstrip("/"):
271 break
272 else:
273 bb.fatal("No package file found for %s" % str(file_path))
274 continue
275
276 for debugsrc in file_data["debugsrc"]:
277 ref_id = "NOASSERTION"
278 for search in debug_search_paths:
Andrew Geissler595f6302022-01-24 19:11:47 +0000279 if debugsrc.startswith("/usr/src/kernel"):
280 debugsrc_path = search / debugsrc.replace('/usr/src/kernel/', '')
281 else:
282 debugsrc_path = search / debugsrc.lstrip("/")
Andrew Geissler5199d832021-09-24 16:47:35 -0500283 if not debugsrc_path.exists():
284 continue
285
286 file_sha256 = bb.utils.sha256_file(debugsrc_path)
287
288 if file_sha256 in sources:
289 source_file = sources[file_sha256]
290
291 doc_ref = package_doc.find_external_document_ref(source_file.doc.documentNamespace)
292 if doc_ref is None:
293 doc_ref = oe.spdx.SPDXExternalDocumentRef()
294 doc_ref.externalDocumentId = "DocumentRef-dependency-" + source_file.doc.name
295 doc_ref.spdxDocument = source_file.doc.documentNamespace
296 doc_ref.checksum.algorithm = "SHA1"
297 doc_ref.checksum.checksumValue = source_file.doc_sha1
298 package_doc.externalDocumentRefs.append(doc_ref)
299
300 ref_id = "%s:%s" % (doc_ref.externalDocumentId, source_file.file.SPDXID)
301 else:
302 bb.debug(1, "Debug source %s with SHA256 %s not found in any dependency" % (str(debugsrc_path), file_sha256))
303 break
304 else:
305 bb.debug(1, "Debug source %s not found" % debugsrc)
306
307 package_doc.add_relationship(pkg_file, "GENERATED_FROM", ref_id, comment=debugsrc)
308
309def collect_dep_recipes(d, doc, spdx_recipe):
310 from pathlib import Path
311 import oe.sbom
312 import oe.spdx
313
314 deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
315
316 dep_recipes = []
317 taskdepdata = d.getVar("BB_TASKDEPDATA", False)
318 deps = sorted(set(
319 dep[0] for dep in taskdepdata.values() if
320 dep[1] == "do_create_spdx" and dep[0] != d.getVar("PN")
321 ))
322 for dep_pn in deps:
323 dep_recipe_path = deploy_dir_spdx / "recipes" / ("recipe-%s.spdx.json" % dep_pn)
324
325 spdx_dep_doc, spdx_dep_sha1 = oe.sbom.read_doc(dep_recipe_path)
326
327 for pkg in spdx_dep_doc.packages:
328 if pkg.name == dep_pn:
329 spdx_dep_recipe = pkg
330 break
331 else:
332 continue
333
334 dep_recipes.append(oe.sbom.DepRecipe(spdx_dep_doc, spdx_dep_sha1, spdx_dep_recipe))
335
336 dep_recipe_ref = oe.spdx.SPDXExternalDocumentRef()
337 dep_recipe_ref.externalDocumentId = "DocumentRef-dependency-" + spdx_dep_doc.name
338 dep_recipe_ref.spdxDocument = spdx_dep_doc.documentNamespace
339 dep_recipe_ref.checksum.algorithm = "SHA1"
340 dep_recipe_ref.checksum.checksumValue = spdx_dep_sha1
341
342 doc.externalDocumentRefs.append(dep_recipe_ref)
343
344 doc.add_relationship(
345 "%s:%s" % (dep_recipe_ref.externalDocumentId, spdx_dep_recipe.SPDXID),
346 "BUILD_DEPENDENCY_OF",
347 spdx_recipe
348 )
349
350 return dep_recipes
351
352collect_dep_recipes[vardepsexclude] += "BB_TASKDEPDATA"
353
354
355def collect_dep_sources(d, dep_recipes):
356 import oe.sbom
357
358 sources = {}
359 for dep in dep_recipes:
Patrick Williams93c203f2021-10-06 16:15:23 -0500360 # Don't collect sources from native recipes as they
361 # match non-native sources also.
362 if recipe_spdx_is_native(d, dep.recipe):
363 continue
Andrew Geissler5199d832021-09-24 16:47:35 -0500364 recipe_files = set(dep.recipe.hasFiles)
365
366 for spdx_file in dep.doc.files:
367 if spdx_file.SPDXID not in recipe_files:
368 continue
369
370 if "SOURCE" in spdx_file.fileTypes:
371 for checksum in spdx_file.checksums:
372 if checksum.algorithm == "SHA256":
373 sources[checksum.checksumValue] = oe.sbom.DepSource(dep.doc, dep.doc_sha1, dep.recipe, spdx_file)
374 break
375
376 return sources
377
378
379python do_create_spdx() {
380 from datetime import datetime, timezone
381 import oe.sbom
382 import oe.spdx
383 import uuid
384 from pathlib import Path
385 from contextlib import contextmanager
386 import oe.cve_check
387
388 @contextmanager
389 def optional_tarfile(name, guard, mode="w"):
390 import tarfile
391 import bb.compress.zstd
392
393 num_threads = int(d.getVar("BB_NUMBER_THREADS"))
394
395 if guard:
396 name.parent.mkdir(parents=True, exist_ok=True)
397 with bb.compress.zstd.open(name, mode=mode + "b", num_threads=num_threads) as f:
398 with tarfile.open(fileobj=f, mode=mode + "|") as tf:
399 yield tf
400 else:
401 yield None
402
403
404 deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
405 spdx_workdir = Path(d.getVar("SPDXWORK"))
406 include_packaged = d.getVar("SPDX_INCLUDE_PACKAGED") == "1"
407 include_sources = d.getVar("SPDX_INCLUDE_SOURCES") == "1"
408 archive_sources = d.getVar("SPDX_ARCHIVE_SOURCES") == "1"
409 archive_packaged = d.getVar("SPDX_ARCHIVE_PACKAGED") == "1"
Andrew Geissler5199d832021-09-24 16:47:35 -0500410
411 creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
412
413 doc = oe.spdx.SPDXDocument()
414
415 doc.name = "recipe-" + d.getVar("PN")
416 doc.documentNamespace = get_doc_namespace(d, doc)
417 doc.creationInfo.created = creation_time
418 doc.creationInfo.comment = "This document was created by analyzing recipe files during the build."
419 doc.creationInfo.licenseListVersion = d.getVar("SPDX_LICENSE_DATA")["licenseListVersion"]
420 doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
Andrew Geissler595f6302022-01-24 19:11:47 +0000421 doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500422 doc.creationInfo.creators.append("Person: N/A ()")
423
424 recipe = oe.spdx.SPDXPackage()
425 recipe.name = d.getVar("PN")
426 recipe.versionInfo = d.getVar("PV")
427 recipe.SPDXID = oe.sbom.get_recipe_spdxid(d)
Andrew Geisslereff27472021-10-29 15:35:00 -0500428 if bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d):
429 recipe.annotations.append(create_annotation(d, "isNative"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500430
431 for s in d.getVar('SRC_URI').split():
432 if not s.startswith("file://"):
433 recipe.downloadLocation = s
434 break
435 else:
436 recipe.downloadLocation = "NOASSERTION"
437
438 homepage = d.getVar("HOMEPAGE")
439 if homepage:
440 recipe.homepage = homepage
441
442 license = d.getVar("LICENSE")
443 if license:
444 recipe.licenseDeclared = convert_license_to_spdx(license, doc, d)
445
446 summary = d.getVar("SUMMARY")
447 if summary:
448 recipe.summary = summary
449
450 description = d.getVar("DESCRIPTION")
451 if description:
452 recipe.description = description
453
454 # Some CVEs may be patched during the build process without incrementing the version number,
455 # so querying for CVEs based on the CPE id can lead to false positives. To account for this,
456 # save the CVEs fixed by patches to source information field in the SPDX.
457 patched_cves = oe.cve_check.get_patched_cves(d)
458 patched_cves = list(patched_cves)
459 patched_cves = ' '.join(patched_cves)
460 if patched_cves:
461 recipe.sourceInfo = "CVEs fixed: " + patched_cves
462
463 cpe_ids = oe.cve_check.get_cpe_ids(d.getVar("CVE_PRODUCT"), d.getVar("CVE_VERSION"))
464 if cpe_ids:
465 for cpe_id in cpe_ids:
466 cpe = oe.spdx.SPDXExternalReference()
467 cpe.referenceCategory = "SECURITY"
468 cpe.referenceType = "http://spdx.org/rdf/references/cpe23Type"
469 cpe.referenceLocator = cpe_id
470 recipe.externalRefs.append(cpe)
471
472 doc.packages.append(recipe)
473 doc.add_relationship(doc, "DESCRIBES", recipe)
474
475 if process_sources(d) and include_sources:
476 recipe_archive = deploy_dir_spdx / "recipes" / (doc.name + ".tar.zst")
477 with optional_tarfile(recipe_archive, archive_sources) as archive:
478 spdx_get_src(d)
479
480 add_package_files(
481 d,
482 doc,
483 recipe,
484 spdx_workdir,
485 lambda file_counter: "SPDXRef-SourceFile-%s-%d" % (d.getVar("PN"), file_counter),
486 lambda filepath: ["SOURCE"],
487 ignore_dirs=[".git"],
488 ignore_top_level_dirs=["temp"],
489 archive=archive,
490 )
491
492 if archive is not None:
493 recipe.packageFileName = str(recipe_archive.name)
494
495 dep_recipes = collect_dep_recipes(d, doc, recipe)
496
497 doc_sha1 = oe.sbom.write_doc(d, doc, "recipes")
498 dep_recipes.append(oe.sbom.DepRecipe(doc, doc_sha1, recipe))
499
500 recipe_ref = oe.spdx.SPDXExternalDocumentRef()
501 recipe_ref.externalDocumentId = "DocumentRef-recipe-" + recipe.name
502 recipe_ref.spdxDocument = doc.documentNamespace
503 recipe_ref.checksum.algorithm = "SHA1"
504 recipe_ref.checksum.checksumValue = doc_sha1
505
506 sources = collect_dep_sources(d, dep_recipes)
507 found_licenses = {license.name:recipe_ref.externalDocumentId + ":" + license.licenseId for license in doc.hasExtractedLicensingInfos}
508
Patrick Williams93c203f2021-10-06 16:15:23 -0500509 if not recipe_spdx_is_native(d, recipe):
Andrew Geissler5199d832021-09-24 16:47:35 -0500510 bb.build.exec_func("read_subpackage_metadata", d)
511
512 pkgdest = Path(d.getVar("PKGDEST"))
513 for package in d.getVar("PACKAGES").split():
514 if not oe.packagedata.packaged(package, d):
515 continue
516
517 package_doc = oe.spdx.SPDXDocument()
518 pkg_name = d.getVar("PKG:%s" % package) or package
519 package_doc.name = pkg_name
520 package_doc.documentNamespace = get_doc_namespace(d, package_doc)
521 package_doc.creationInfo.created = creation_time
522 package_doc.creationInfo.comment = "This document was created by analyzing packages created during the build."
523 package_doc.creationInfo.licenseListVersion = d.getVar("SPDX_LICENSE_DATA")["licenseListVersion"]
524 package_doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
Andrew Geissler595f6302022-01-24 19:11:47 +0000525 package_doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500526 package_doc.creationInfo.creators.append("Person: N/A ()")
527 package_doc.externalDocumentRefs.append(recipe_ref)
528
529 package_license = d.getVar("LICENSE:%s" % package) or d.getVar("LICENSE")
530
531 spdx_package = oe.spdx.SPDXPackage()
532
533 spdx_package.SPDXID = oe.sbom.get_package_spdxid(pkg_name)
534 spdx_package.name = pkg_name
535 spdx_package.versionInfo = d.getVar("PV")
536 spdx_package.licenseDeclared = convert_license_to_spdx(package_license, package_doc, d, found_licenses)
537
538 package_doc.packages.append(spdx_package)
539
540 package_doc.add_relationship(spdx_package, "GENERATED_FROM", "%s:%s" % (recipe_ref.externalDocumentId, recipe.SPDXID))
541 package_doc.add_relationship(package_doc, "DESCRIBES", spdx_package)
542
543 package_archive = deploy_dir_spdx / "packages" / (package_doc.name + ".tar.zst")
544 with optional_tarfile(package_archive, archive_packaged) as archive:
545 package_files = add_package_files(
546 d,
547 package_doc,
548 spdx_package,
549 pkgdest / package,
550 lambda file_counter: oe.sbom.get_packaged_file_spdxid(pkg_name, file_counter),
551 lambda filepath: ["BINARY"],
552 archive=archive,
553 )
554
555 if archive is not None:
556 spdx_package.packageFileName = str(package_archive.name)
557
558 add_package_sources_from_debug(d, package_doc, spdx_package, package, package_files, sources)
559
560 oe.sbom.write_doc(d, package_doc, "packages")
561}
562# NOTE: depending on do_unpack is a hack that is necessary to get it's dependencies for archive the source
563addtask do_create_spdx after do_package do_packagedata do_unpack before do_build do_rm_work
564
565SSTATETASKS += "do_create_spdx"
566do_create_spdx[sstate-inputdirs] = "${SPDXDEPLOY}"
567do_create_spdx[sstate-outputdirs] = "${DEPLOY_DIR_SPDX}"
568
569python do_create_spdx_setscene () {
570 sstate_setscene(d)
571}
572addtask do_create_spdx_setscene
573
574do_create_spdx[dirs] = "${SPDXDEPLOY} ${SPDXWORK}"
575do_create_spdx[cleandirs] = "${SPDXDEPLOY} ${SPDXWORK}"
576do_create_spdx[depends] += "${PATCHDEPENDENCY}"
577do_create_spdx[deptask] = "do_create_spdx"
578
579def collect_package_providers(d):
580 from pathlib import Path
581 import oe.sbom
582 import oe.spdx
583 import json
584
585 deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
586
587 providers = {}
588
589 taskdepdata = d.getVar("BB_TASKDEPDATA", False)
590 deps = sorted(set(
591 dep[0] for dep in taskdepdata.values() if dep[0] != d.getVar("PN")
592 ))
593 deps.append(d.getVar("PN"))
594
595 for dep_pn in deps:
596 recipe_data = oe.packagedata.read_pkgdata(dep_pn, d)
597
598 for pkg in recipe_data.get("PACKAGES", "").split():
599
600 pkg_data = oe.packagedata.read_subpkgdata_dict(pkg, d)
601 rprovides = set(n for n, _ in bb.utils.explode_dep_versions2(pkg_data.get("RPROVIDES", "")).items())
602 rprovides.add(pkg)
603
604 for r in rprovides:
605 providers[r] = pkg
606
607 return providers
608
609collect_package_providers[vardepsexclude] += "BB_TASKDEPDATA"
610
611python do_create_runtime_spdx() {
612 from datetime import datetime, timezone
613 import oe.sbom
614 import oe.spdx
615 import oe.packagedata
616 from pathlib import Path
617
618 deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
619 spdx_deploy = Path(d.getVar("SPDXRUNTIMEDEPLOY"))
Andrew Geisslereff27472021-10-29 15:35:00 -0500620 is_native = bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d)
Andrew Geissler5199d832021-09-24 16:47:35 -0500621
622 creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
623
624 providers = collect_package_providers(d)
625
626 if not is_native:
627 bb.build.exec_func("read_subpackage_metadata", d)
628
629 dep_package_cache = {}
630
631 pkgdest = Path(d.getVar("PKGDEST"))
632 for package in d.getVar("PACKAGES").split():
633 localdata = bb.data.createCopy(d)
634 pkg_name = d.getVar("PKG:%s" % package) or package
635 localdata.setVar("PKG", pkg_name)
636 localdata.setVar('OVERRIDES', d.getVar("OVERRIDES", False) + ":" + package)
637
638 if not oe.packagedata.packaged(package, localdata):
639 continue
640
641 pkg_spdx_path = deploy_dir_spdx / "packages" / (pkg_name + ".spdx.json")
642
643 package_doc, package_doc_sha1 = oe.sbom.read_doc(pkg_spdx_path)
644
645 for p in package_doc.packages:
646 if p.name == pkg_name:
647 spdx_package = p
648 break
649 else:
650 bb.fatal("Package '%s' not found in %s" % (pkg_name, pkg_spdx_path))
651
652 runtime_doc = oe.spdx.SPDXDocument()
653 runtime_doc.name = "runtime-" + pkg_name
654 runtime_doc.documentNamespace = get_doc_namespace(localdata, runtime_doc)
655 runtime_doc.creationInfo.created = creation_time
656 runtime_doc.creationInfo.comment = "This document was created by analyzing package runtime dependencies."
657 runtime_doc.creationInfo.licenseListVersion = d.getVar("SPDX_LICENSE_DATA")["licenseListVersion"]
658 runtime_doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
Andrew Geissler595f6302022-01-24 19:11:47 +0000659 runtime_doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500660 runtime_doc.creationInfo.creators.append("Person: N/A ()")
661
662 package_ref = oe.spdx.SPDXExternalDocumentRef()
663 package_ref.externalDocumentId = "DocumentRef-package-" + package
664 package_ref.spdxDocument = package_doc.documentNamespace
665 package_ref.checksum.algorithm = "SHA1"
666 package_ref.checksum.checksumValue = package_doc_sha1
667
668 runtime_doc.externalDocumentRefs.append(package_ref)
669
670 runtime_doc.add_relationship(
671 runtime_doc.SPDXID,
672 "AMENDS",
673 "%s:%s" % (package_ref.externalDocumentId, package_doc.SPDXID)
674 )
675
676 deps = bb.utils.explode_dep_versions2(localdata.getVar("RDEPENDS") or "")
677 seen_deps = set()
678 for dep, _ in deps.items():
679 if dep in seen_deps:
680 continue
681
Andrew Geissler595f6302022-01-24 19:11:47 +0000682 if dep not in providers:
683 continue
684
Andrew Geissler5199d832021-09-24 16:47:35 -0500685 dep = providers[dep]
686
687 if not oe.packagedata.packaged(dep, localdata):
688 continue
689
690 dep_pkg_data = oe.packagedata.read_subpkgdata_dict(dep, d)
691 dep_pkg = dep_pkg_data["PKG"]
692
693 if dep in dep_package_cache:
694 (dep_spdx_package, dep_package_ref) = dep_package_cache[dep]
695 else:
696 dep_path = deploy_dir_spdx / "packages" / ("%s.spdx.json" % dep_pkg)
697
698 spdx_dep_doc, spdx_dep_sha1 = oe.sbom.read_doc(dep_path)
699
700 for pkg in spdx_dep_doc.packages:
701 if pkg.name == dep_pkg:
702 dep_spdx_package = pkg
703 break
704 else:
705 bb.fatal("Package '%s' not found in %s" % (dep_pkg, dep_path))
706
707 dep_package_ref = oe.spdx.SPDXExternalDocumentRef()
708 dep_package_ref.externalDocumentId = "DocumentRef-runtime-dependency-" + spdx_dep_doc.name
709 dep_package_ref.spdxDocument = spdx_dep_doc.documentNamespace
710 dep_package_ref.checksum.algorithm = "SHA1"
711 dep_package_ref.checksum.checksumValue = spdx_dep_sha1
712
713 dep_package_cache[dep] = (dep_spdx_package, dep_package_ref)
714
715 runtime_doc.externalDocumentRefs.append(dep_package_ref)
716
717 runtime_doc.add_relationship(
718 "%s:%s" % (dep_package_ref.externalDocumentId, dep_spdx_package.SPDXID),
719 "RUNTIME_DEPENDENCY_OF",
720 "%s:%s" % (package_ref.externalDocumentId, spdx_package.SPDXID)
721 )
722 seen_deps.add(dep)
723
724 oe.sbom.write_doc(d, runtime_doc, "runtime", spdx_deploy)
725}
726
727addtask do_create_runtime_spdx after do_create_spdx before do_build do_rm_work
728SSTATETASKS += "do_create_runtime_spdx"
729do_create_runtime_spdx[sstate-inputdirs] = "${SPDXRUNTIMEDEPLOY}"
730do_create_runtime_spdx[sstate-outputdirs] = "${DEPLOY_DIR_SPDX}"
731
732python do_create_runtime_spdx_setscene () {
733 sstate_setscene(d)
734}
735addtask do_create_runtime_spdx_setscene
736
737do_create_runtime_spdx[dirs] = "${SPDXRUNTIMEDEPLOY}"
738do_create_runtime_spdx[cleandirs] = "${SPDXRUNTIMEDEPLOY}"
739do_create_runtime_spdx[rdeptask] = "do_create_spdx"
740
741def spdx_get_src(d):
742 """
743 save patched source of the recipe in SPDX_WORKDIR.
744 """
745 import shutil
746 spdx_workdir = d.getVar('SPDXWORK')
747 spdx_sysroot_native = d.getVar('STAGING_DIR_NATIVE')
748 pn = d.getVar('PN')
749
750 workdir = d.getVar("WORKDIR")
751
752 try:
753 # The kernel class functions require it to be on work-shared, so we dont change WORKDIR
Andrew Geissler595f6302022-01-24 19:11:47 +0000754 if not is_work_shared_spdx(d):
Andrew Geissler5199d832021-09-24 16:47:35 -0500755 # Change the WORKDIR to make do_unpack do_patch run in another dir.
756 d.setVar('WORKDIR', spdx_workdir)
757 # Restore the original path to recipe's native sysroot (it's relative to WORKDIR).
758 d.setVar('STAGING_DIR_NATIVE', spdx_sysroot_native)
759
760 # The changed 'WORKDIR' also caused 'B' changed, create dir 'B' for the
761 # possibly requiring of the following tasks (such as some recipes's
762 # do_patch required 'B' existed).
763 bb.utils.mkdirhier(d.getVar('B'))
764
765 bb.build.exec_func('do_unpack', d)
766 # Copy source of kernel to spdx_workdir
Andrew Geissler595f6302022-01-24 19:11:47 +0000767 if is_work_shared_spdx(d):
Andrew Geissler5199d832021-09-24 16:47:35 -0500768 d.setVar('WORKDIR', spdx_workdir)
769 d.setVar('STAGING_DIR_NATIVE', spdx_sysroot_native)
770 src_dir = spdx_workdir + "/" + d.getVar('PN')+ "-" + d.getVar('PV') + "-" + d.getVar('PR')
771 bb.utils.mkdirhier(src_dir)
772 if bb.data.inherits_class('kernel',d):
773 share_src = d.getVar('STAGING_KERNEL_DIR')
774 cmd_copy_share = "cp -rf " + share_src + "/* " + src_dir + "/"
775 cmd_copy_kernel_result = os.popen(cmd_copy_share).read()
776 bb.note("cmd_copy_kernel_result = " + cmd_copy_kernel_result)
777
778 git_path = src_dir + "/.git"
779 if os.path.exists(git_path):
780 shutils.rmtree(git_path)
781
782 # Make sure gcc and kernel sources are patched only once
Andrew Geissler595f6302022-01-24 19:11:47 +0000783 if not (d.getVar('SRC_URI') == "" or is_work_shared_spdx(d)):
Andrew Geissler5199d832021-09-24 16:47:35 -0500784 bb.build.exec_func('do_patch', d)
785
786 # Some userland has no source.
787 if not os.path.exists( spdx_workdir ):
788 bb.utils.mkdirhier(spdx_workdir)
789 finally:
790 d.setVar("WORKDIR", workdir)
791
792do_rootfs[recrdeptask] += "do_create_spdx do_create_runtime_spdx"
793
794ROOTFS_POSTUNINSTALL_COMMAND =+ "image_combine_spdx ; "
795python image_combine_spdx() {
796 import os
797 import oe.spdx
798 import oe.sbom
799 import io
800 import json
801 from oe.rootfs import image_list_installed_packages
802 from datetime import timezone, datetime
803 from pathlib import Path
804 import tarfile
805 import bb.compress.zstd
806
807 creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
808 image_name = d.getVar("IMAGE_NAME")
809 image_link_name = d.getVar("IMAGE_LINK_NAME")
810
811 deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
812 imgdeploydir = Path(d.getVar("IMGDEPLOYDIR"))
813 source_date_epoch = d.getVar("SOURCE_DATE_EPOCH")
814
815 doc = oe.spdx.SPDXDocument()
816 doc.name = image_name
817 doc.documentNamespace = get_doc_namespace(d, doc)
818 doc.creationInfo.created = creation_time
819 doc.creationInfo.comment = "This document was created by analyzing the source of the Yocto recipe during the build."
820 doc.creationInfo.licenseListVersion = d.getVar("SPDX_LICENSE_DATA")["licenseListVersion"]
821 doc.creationInfo.creators.append("Tool: OpenEmbedded Core create-spdx.bbclass")
Andrew Geissler595f6302022-01-24 19:11:47 +0000822 doc.creationInfo.creators.append("Organization: %s" % d.getVar("SPDX_ORG"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500823 doc.creationInfo.creators.append("Person: N/A ()")
824
825 image = oe.spdx.SPDXPackage()
826 image.name = d.getVar("PN")
827 image.versionInfo = d.getVar("PV")
828 image.SPDXID = oe.sbom.get_image_spdxid(image_name)
829
830 doc.packages.append(image)
831
832 spdx_package = oe.spdx.SPDXPackage()
833
834 packages = image_list_installed_packages(d)
835
836 for name in sorted(packages.keys()):
837 pkg_spdx_path = deploy_dir_spdx / "packages" / (name + ".spdx.json")
838 pkg_doc, pkg_doc_sha1 = oe.sbom.read_doc(pkg_spdx_path)
839
840 for p in pkg_doc.packages:
841 if p.name == name:
842 pkg_ref = oe.spdx.SPDXExternalDocumentRef()
843 pkg_ref.externalDocumentId = "DocumentRef-%s" % pkg_doc.name
844 pkg_ref.spdxDocument = pkg_doc.documentNamespace
845 pkg_ref.checksum.algorithm = "SHA1"
846 pkg_ref.checksum.checksumValue = pkg_doc_sha1
847
848 doc.externalDocumentRefs.append(pkg_ref)
849 doc.add_relationship(image, "CONTAINS", "%s:%s" % (pkg_ref.externalDocumentId, p.SPDXID))
850 break
851 else:
852 bb.fatal("Unable to find package with name '%s' in SPDX file %s" % (name, pkg_spdx_path))
853
854 runtime_spdx_path = deploy_dir_spdx / "runtime" / ("runtime-" + name + ".spdx.json")
855 runtime_doc, runtime_doc_sha1 = oe.sbom.read_doc(runtime_spdx_path)
856
857 runtime_ref = oe.spdx.SPDXExternalDocumentRef()
858 runtime_ref.externalDocumentId = "DocumentRef-%s" % runtime_doc.name
859 runtime_ref.spdxDocument = runtime_doc.documentNamespace
860 runtime_ref.checksum.algorithm = "SHA1"
861 runtime_ref.checksum.checksumValue = runtime_doc_sha1
862
863 # "OTHER" isn't ideal here, but I can't find a relationship that makes sense
864 doc.externalDocumentRefs.append(runtime_ref)
865 doc.add_relationship(
866 image,
867 "OTHER",
868 "%s:%s" % (runtime_ref.externalDocumentId, runtime_doc.SPDXID),
869 comment="Runtime dependencies for %s" % name
870 )
871
872 image_spdx_path = imgdeploydir / (image_name + ".spdx.json")
873
874 with image_spdx_path.open("wb") as f:
875 doc.to_json(f, sort_keys=True)
876
Andrew Geissler595f6302022-01-24 19:11:47 +0000877 if image_link_name:
878 image_spdx_link = imgdeploydir / (image_link_name + ".spdx.json")
879 image_spdx_link.symlink_to(os.path.relpath(image_spdx_path, image_spdx_link.parent))
Andrew Geissler5199d832021-09-24 16:47:35 -0500880
881 num_threads = int(d.getVar("BB_NUMBER_THREADS"))
882
883 visited_docs = set()
884
885 index = {"documents": []}
886
887 spdx_tar_path = imgdeploydir / (image_name + ".spdx.tar.zst")
888 with bb.compress.zstd.open(spdx_tar_path, "w", num_threads=num_threads) as f:
889 with tarfile.open(fileobj=f, mode="w|") as tar:
890 def collect_spdx_document(path):
891 nonlocal tar
892 nonlocal deploy_dir_spdx
893 nonlocal source_date_epoch
894 nonlocal index
895
896 if path in visited_docs:
897 return
898
899 visited_docs.add(path)
900
901 with path.open("rb") as f:
902 doc, sha1 = oe.sbom.read_doc(f)
903 f.seek(0)
904
905 if doc.documentNamespace in visited_docs:
906 return
907
908 bb.note("Adding SPDX document %s" % path)
909 visited_docs.add(doc.documentNamespace)
910 info = tar.gettarinfo(fileobj=f)
911
912 info.name = doc.name + ".spdx.json"
913 info.uid = 0
914 info.gid = 0
915 info.uname = "root"
916 info.gname = "root"
917
918 if source_date_epoch is not None and info.mtime > int(source_date_epoch):
919 info.mtime = int(source_date_epoch)
920
921 tar.addfile(info, f)
922
923 index["documents"].append({
924 "filename": info.name,
925 "documentNamespace": doc.documentNamespace,
926 "sha1": sha1,
927 })
928
929 for ref in doc.externalDocumentRefs:
930 ref_path = deploy_dir_spdx / "by-namespace" / ref.spdxDocument.replace("/", "_")
931 collect_spdx_document(ref_path)
932
933 collect_spdx_document(image_spdx_path)
934
935 index["documents"].sort(key=lambda x: x["filename"])
936
937 index_str = io.BytesIO(json.dumps(index, sort_keys=True).encode("utf-8"))
938
939 info = tarfile.TarInfo()
940 info.name = "index.json"
941 info.size = len(index_str.getvalue())
942 info.uid = 0
943 info.gid = 0
944 info.uname = "root"
945 info.gname = "root"
946
947 tar.addfile(info, fileobj=index_str)
948
949 def make_image_link(target_path, suffix):
Andrew Geissler595f6302022-01-24 19:11:47 +0000950 if image_link_name:
951 link = imgdeploydir / (image_link_name + suffix)
952 link.symlink_to(os.path.relpath(target_path, link.parent))
Andrew Geissler5199d832021-09-24 16:47:35 -0500953
954 make_image_link(spdx_tar_path, ".spdx.tar.zst")
955
956 spdx_index_path = imgdeploydir / (image_name + ".spdx.index.json")
957 with spdx_index_path.open("w") as f:
958 json.dump(index, f, sort_keys=True)
959
960 make_image_link(spdx_index_path, ".spdx.index.json")
961}
962