From 2f43be745389822bac7f8fb745cf8f9829fd3604 Mon Sep 17 00:00:00 2001 From: Luis Augenstein Date: Tue, 7 Oct 2025 08:44:40 +0200 Subject: [PATCH 1/2] updated setup to work with src tree outside of the repo Signed-off-by: Luis Augenstein --- .vscode/kernelsbom.code-workspace | 20 +++++++++++ .vscode/launch.json | 2 +- .vscode/settings.json | 1 - README.md | 33 +++++++++++-------- .../cmd_graph_based_kernel_build.py | 4 +-- .../cmd_graph_visualization.py | 4 +-- 6 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 .vscode/kernelsbom.code-workspace diff --git a/.vscode/kernelsbom.code-workspace b/.vscode/kernelsbom.code-workspace new file mode 100644 index 00000000..a8020412 --- /dev/null +++ b/.vscode/kernelsbom.code-workspace @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2025 TNG Technology Consulting GmbH +// +// SPDX-License-Identifier: GPL-2.0-only + +{ + "folders": [ + { + "path": ".." + }, + { + "path": "../../linux" + }, + { + "path": "../../linux_cmd" + } + ], + "settings": { + "search.useIgnoreFiles": false, + } +} diff --git a/.vscode/launch.json b/.vscode/launch.json index d372d052..27c016e4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "console": "integratedTerminal", "args": [ "--src-tree", "../linux", - "--output-tree", "../linux/kernel-build", + "--output-tree", "../linux/kernel_build", "--root-output-in-tree", "vmlinux", "--debug" ] diff --git a/.vscode/settings.json b/.vscode/settings.json index 6c3d1a12..aaa102ef 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,7 +13,6 @@ "python.testing.unittestEnabled": true, "ruff.lineLength": 120, "editor.formatOnSave": true, - "search.useIgnoreFiles": false, "files.exclude": { "**/__pycache__": true }, diff --git a/README.md b/README.md index b16eb5f0..27ed3cd2 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,7 @@ A script to generate an SPDX-format Software Bill of Materials (SBOM) for the `v The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/` directory in the official [linux](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/) kernel source tree. ## Getting Started -1. Clone the repository -2. Activate the venv and install build dependencies - ```bash - python3 -m venv .venv - source .venv/bin/activate - pip install pre-commit reuse ruff - pre-commit install - ``` -3. provide a linux src and output tree, e.g., by downloading precompiled testdata from [KernelSbom-TestData](https://fileshare.tngtech.com/library/98e7e6f8-bffe-4a55-a8d2-817d4f3e51e8/KernelSbom-TestData/) +1. Provide a linux src and output tree, e.g., by downloading precompiled testdata from [KernelSbom-TestData](https://fileshare.tngtech.com/library/98e7e6f8-bffe-4a55-a8d2-817d4f3e51e8/KernelSbom-TestData/) ```bash test_archive="linux-defconfig.tar.gz" curl -L -o "$test_archive" "https://fileshare.tngtech.com/d/e69946da808b41f88047/files/?p=%2F$test_archive&dl=1" @@ -31,16 +23,21 @@ The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/ make O=kernel_build make -j$(nproc) O=kernel_build ``` -4. Run the [sbom.py](sbom/sbom.py) script +2. Clone the repository + ``` + git clone git@github.com:TNG/KernelSbom.git + cd KernelSbom + ``` +3. Run the [sbom.py](sbom/sbom.py) script ```bash python3 sbom/sbom.py \ - --src-tree linux \ - --output-tree linux/kernel_build \ + --src-tree ../linux \ + --output-tree ../linux/kernel_build \ --root-output-in-tree vmlinux \ --spdx sbom.spdx.json \ --used-files sbom.used_files.txt ``` - Starting from `vmlinux` the script builds the **cmd graph**, a directed acyclic graph (DAG) where nodes are filenames and edges represent build dependencies extracted from `..cmd` files. Based on the cmd graph, the final `sbom.spdx.json`, `sbom.used_files.txt` files are created and saved in this repository’s root directory. + Starting from `vmlinux` the script builds the **cmd graph**, a directed acyclic graph (DAG) where nodes are filenames and edges represent build dependencies extracted from `..cmd` files. Based on the cmd graph, the final `sbom.spdx.json`, and `sbom.used_files.txt` files are created and saved in this repository’s root directory. ## Directory Structure @@ -55,7 +52,15 @@ The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/ The main contribution is the content of the `sbom` directory which eventually should be moved into the `linux/scripts/` directory in the official [linux](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/) kernel source tree. -## Reuse +## Development + +Activate the venv and install build dependencies +```bash +python3 -m venv .venv +source .venv/bin/activate +pip install pre-commit reuse ruff +pre-commit install +``` when commiting `reuse lint` is executed as a pre-commit hook to check if all files have compliant License headers. If any file is missing a license header add it via ``` diff --git a/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py b/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py index dbc5255f..d22b1a61 100644 --- a/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py +++ b/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py @@ -54,8 +54,8 @@ def _remove_files(base_path: Path, patterns_to_remove: list[re.Pattern[str]], ig script_path = Path(__file__).parent # Paths to the original source and build directories cmd_graph_path = script_path / "../cmd_graph.pickle" - src_tree = (script_path / "../../linux").resolve() - output_tree = (script_path / "../../linux/kernel_build").resolve() + src_tree = (script_path / "../../../linux").resolve() + output_tree = (script_path / "../../../linux/kernel_build").resolve() root_output_in_tree = Path("vmlinux") # Configure logging diff --git a/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py b/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py index d577feee..bfb0865d 100644 --- a/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py +++ b/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py @@ -71,8 +71,8 @@ def traverse(node: CmdGraphNode, depth: int = 0): if __name__ == "__main__": script_path = Path(__file__).parent cmd_graph_path = script_path / "../cmd_graph.pickle" - src_tree = (script_path / "../../linux").resolve() - output_tree = (script_path / "../../linux/kernel_build").resolve() + src_tree = (script_path / "../../../linux").resolve() + output_tree = (script_path / "../../../linux/kernel_build").resolve() root_output_in_tree = Path("vmlinux") cmd_graph_json_gz_path = script_path / "web/cmd_graph.json.gz" max_visualization_depth: int | None = None From 108b08f8ea3b582343800a0689d37f9005051586 Mon Sep 17 00:00:00 2001 From: Luis Augenstein Date: Tue, 7 Oct 2025 10:47:56 +0200 Subject: [PATCH 2/2] make src and output tree in analysis scripts variable Signed-off-by: Luis Augenstein --- .vscode/kernelsbom.code-workspace | 20 ------------------- .../cmd_graph_based_kernel_build.py | 18 ++++++++++++----- .../cmd_graph_visualization.py | 15 +++++++++++--- 3 files changed, 25 insertions(+), 28 deletions(-) delete mode 100644 .vscode/kernelsbom.code-workspace diff --git a/.vscode/kernelsbom.code-workspace b/.vscode/kernelsbom.code-workspace deleted file mode 100644 index a8020412..00000000 --- a/.vscode/kernelsbom.code-workspace +++ /dev/null @@ -1,20 +0,0 @@ -// SPDX-FileCopyrightText: 2025 TNG Technology Consulting GmbH -// -// SPDX-License-Identifier: GPL-2.0-only - -{ - "folders": [ - { - "path": ".." - }, - { - "path": "../../linux" - }, - { - "path": "../../linux_cmd" - } - ], - "settings": { - "search.useIgnoreFiles": false, - } -} diff --git a/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py b/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py index d22b1a61..51c77e53 100644 --- a/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py +++ b/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py @@ -51,18 +51,26 @@ def _remove_files(base_path: Path, patterns_to_remove: list[re.Pattern[str]], ig if __name__ == "__main__": + """ + cmd_graph_based_kernel_build.py + """ script_path = Path(__file__).parent - # Paths to the original source and build directories - cmd_graph_path = script_path / "../cmd_graph.pickle" - src_tree = (script_path / "../../../linux").resolve() - output_tree = (script_path / "../../../linux/kernel_build").resolve() + src_tree = ( + Path(sys.argv[1]).resolve() + if len(sys.argv) >= 2 and sys.argv[1] + else (script_path / "../../../linux").resolve() + ) + output_tree = ( + Path(sys.argv[1]).resolve() if len(sys.argv) >= 3 and sys.argv[2] else (src_tree / "kernel_build").resolve() + ) root_output_in_tree = Path("vmlinux") + cmd_graph_path = script_path / "../cmd_graph.pickle" + cmd_src_tree = src_tree.parent / f"{src_tree.name}_cmd" # Configure logging logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s") # Copy the original source tree - cmd_src_tree = (script_path / "../../linux_cmd").resolve() if cmd_src_tree.exists(): shutil.rmtree(cmd_src_tree) logging.info(f"Copy {src_tree} into {cmd_src_tree}") diff --git a/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py b/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py index bfb0865d..bd7c73f8 100644 --- a/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py +++ b/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py @@ -69,11 +69,20 @@ def traverse(node: CmdGraphNode, depth: int = 0): if __name__ == "__main__": + """ + cmd_graph_visualization.py + """ script_path = Path(__file__).parent - cmd_graph_path = script_path / "../cmd_graph.pickle" - src_tree = (script_path / "../../../linux").resolve() - output_tree = (script_path / "../../../linux/kernel_build").resolve() + src_tree = ( + Path(sys.argv[1]).resolve() + if len(sys.argv) >= 2 and sys.argv[1] + else (script_path / "../../../linux").resolve() + ) + output_tree = ( + Path(sys.argv[1]).resolve() if len(sys.argv) >= 3 and sys.argv[2] else (src_tree / "kernel_build").resolve() + ) root_output_in_tree = Path("vmlinux") + cmd_graph_path = script_path / "../cmd_graph.pickle" cmd_graph_json_gz_path = script_path / "web/cmd_graph.json.gz" max_visualization_depth: int | None = None