|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from fetchcode.vcs import fetch_via_vcs |
| 12 | + |
| 13 | +from vulnerabilities.importer import AdvisoryData |
| 14 | +from vulnerabilities.importer import AffectedPackageV2 |
| 15 | +from vulnerabilities.importer import PackageCommitPatchData |
| 16 | +from vulnerabilities.importer import PatchData |
| 17 | +from vulnerabilities.importer import ReferenceV2 |
| 18 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 19 | +from vulnerabilities.pipes.advisory import classify_patch_source |
| 20 | +from vulnerabilities.utils import commit_regex |
| 21 | +from vulnerabilities.utils import cve_regex |
| 22 | +from vulnerabilities.utils import get_advisory_url |
| 23 | +from vulnerabilities.utils import is_commit |
| 24 | + |
| 25 | + |
| 26 | +class LinuxKernelPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 27 | + """ |
| 28 | + Pipeline to collect Linux Kernel Pipeline: |
| 29 | + """ |
| 30 | + |
| 31 | + pipeline_id = "linux_kernel_cves_fix_commits" |
| 32 | + spdx_license_expression = "Apache-2.0" |
| 33 | + license_url = "https://github.com/nluedtke/linux_kernel_cves/blob/master/LICENSE" |
| 34 | + importer_name = "linux_kernel_cves_fix_commits" |
| 35 | + qualified_name = "linux_kernel_cves_fix_commits" |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def steps(cls): |
| 39 | + return ( |
| 40 | + cls.clone, |
| 41 | + cls.collect_and_store_advisories, |
| 42 | + cls.clean_downloads, |
| 43 | + ) |
| 44 | + |
| 45 | + def advisories_count(self): |
| 46 | + root = Path(self.vcs_response.dest_dir) |
| 47 | + return sum(1 for _ in root.rglob("data/*.txt")) |
| 48 | + |
| 49 | + def clone(self): |
| 50 | + self.repo_url = "git+https://github.com/nluedtke/linux_kernel_cves" |
| 51 | + self.log(f"Cloning `{self.repo_url}`") |
| 52 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 53 | + |
| 54 | + def collect_advisories(self): |
| 55 | + self.log(f"Processing linux kernel fix commits.") |
| 56 | + base_path = Path(self.vcs_response.dest_dir) / "data" |
| 57 | + for file_path in base_path.rglob("*.txt"): |
| 58 | + if "_CVEs.txt" in file_path.name: |
| 59 | + continue |
| 60 | + |
| 61 | + if "_security.txt" in file_path.name: |
| 62 | + patches = [] |
| 63 | + affected_packages = [] |
| 64 | + references = [] |
| 65 | + for vulnerability_id, commit_hash in self.parse_commits_file(file_path): |
| 66 | + patch_url = f"https://github.com/torvalds/linux/commit/{commit_hash}" |
| 67 | + if not commit_hash: |
| 68 | + continue |
| 69 | + |
| 70 | + base_purl, patch_objs = classify_patch_source( |
| 71 | + url=patch_url, |
| 72 | + commit_hash=commit_hash, |
| 73 | + patch_text=None, |
| 74 | + ) |
| 75 | + |
| 76 | + for patch_obj in patch_objs: |
| 77 | + if isinstance(patch_obj, PackageCommitPatchData): |
| 78 | + fixed_commit = patch_obj |
| 79 | + affected_package = AffectedPackageV2( |
| 80 | + package=base_purl, |
| 81 | + fixed_by_commit_patches=[fixed_commit], |
| 82 | + ) |
| 83 | + affected_packages.append(affected_package) |
| 84 | + elif isinstance(patch_obj, PatchData): |
| 85 | + patches.append(patch_obj) |
| 86 | + elif isinstance(patch_obj, ReferenceV2): |
| 87 | + references.append(patch_obj) |
| 88 | + |
| 89 | + advisory_url = get_advisory_url( |
| 90 | + file=file_path, |
| 91 | + base_path=self.vcs_response.dest_dir, |
| 92 | + url="https://github.com/nluedtke/linux_kernel_cves/blob/master/", |
| 93 | + ) |
| 94 | + |
| 95 | + yield AdvisoryData( |
| 96 | + advisory_id=vulnerability_id, |
| 97 | + references_v2=references, |
| 98 | + affected_packages=affected_packages, |
| 99 | + patches=patches, |
| 100 | + url=advisory_url, |
| 101 | + ) |
| 102 | + |
| 103 | + def parse_commits_file(self, file_path): |
| 104 | + """Extract CVE-ID and commit hashes from a text file""" |
| 105 | + with open(file_path, "r", encoding="utf-8") as f: |
| 106 | + for line in f: |
| 107 | + parts = line.strip().split(":", 2) |
| 108 | + |
| 109 | + if len(parts) < 2: |
| 110 | + continue |
| 111 | + |
| 112 | + cve_part = parts[0] |
| 113 | + commit_part = parts[1] |
| 114 | + |
| 115 | + cve_match = cve_regex.search(cve_part) |
| 116 | + if not cve_match: |
| 117 | + continue |
| 118 | + |
| 119 | + cve = cve_match.group(0) |
| 120 | + |
| 121 | + sha1_match = commit_regex.search(commit_part) |
| 122 | + commit_hash = sha1_match.group(0) if sha1_match else None |
| 123 | + |
| 124 | + if not commit_hash or not is_commit(commit_hash): |
| 125 | + continue |
| 126 | + |
| 127 | + yield cve, commit_hash |
| 128 | + |
| 129 | + def clean_downloads(self): |
| 130 | + """Cleanup any temporary repository data.""" |
| 131 | + if self.vcs_response: |
| 132 | + self.log("Removing cloned repository") |
| 133 | + self.vcs_response.delete() |
| 134 | + |
| 135 | + def on_failure(self): |
| 136 | + """Ensure cleanup is always performed on failure.""" |
| 137 | + self.clean_downloads() |
0 commit comments