|
| 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 collections import defaultdict |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from fetchcode.vcs import fetch_via_vcs |
| 13 | +from univers.version_range import GenericVersionRange |
| 14 | + |
| 15 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 16 | +from vulnerabilities.importer import AffectedPackageV2 |
| 17 | +from vulnerabilities.importer import PackageCommitPatchData |
| 18 | +from vulnerabilities.importer import PatchData |
| 19 | +from vulnerabilities.importer import ReferenceV2 |
| 20 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 21 | +from vulnerabilities.pipes.advisory import classify_patch_source |
| 22 | +from vulnerabilities.utils import commit_regex |
| 23 | +from vulnerabilities.utils import cve_regex |
| 24 | +from vulnerabilities.utils import is_commit |
| 25 | + |
| 26 | + |
| 27 | +class LinuxKernelPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 28 | + """ |
| 29 | + Pipeline to collect Linux Kernel Pipeline: |
| 30 | + """ |
| 31 | + |
| 32 | + pipeline_id = "linux_kernel_cves_fix_commits" |
| 33 | + spdx_license_expression = "Apache-2.0" |
| 34 | + license_url = "https://github.com/nluedtke/linux_kernel_cves/blob/master/LICENSE" |
| 35 | + run_once = True |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def steps(cls): |
| 39 | + return ( |
| 40 | + cls.clone, |
| 41 | + cls.extract_kernel_cve_fix_commits, |
| 42 | + cls.collect_and_store_advisories, |
| 43 | + cls.clean_downloads, |
| 44 | + ) |
| 45 | + |
| 46 | + def advisories_count(self): |
| 47 | + root = Path(self.vcs_response.dest_dir) |
| 48 | + return sum(1 for _ in root.rglob("data/*.txt")) |
| 49 | + |
| 50 | + def clone(self): |
| 51 | + self.repo_url = "git+https://github.com/nluedtke/linux_kernel_cves" |
| 52 | + self.log(f"Cloning `{self.repo_url}`") |
| 53 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 54 | + |
| 55 | + def extract_kernel_cve_fix_commits(self): |
| 56 | + self.log(f"Processing linux kernel fix commits.") |
| 57 | + base_path = Path(self.vcs_response.dest_dir) / "data" |
| 58 | + |
| 59 | + for file_path in base_path.rglob("*.txt"): |
| 60 | + if "_CVEs.txt" in file_path.name: |
| 61 | + continue |
| 62 | + |
| 63 | + if "_security.txt" in file_path.name: |
| 64 | + self.parse_commits_file(file_path) |
| 65 | + |
| 66 | + def collect_advisories(self): |
| 67 | + for ( |
| 68 | + vulnerability_id, |
| 69 | + fixed_versions_commits, |
| 70 | + ) in self.cve_to_fixed_versions_and_commits.items(): |
| 71 | + references = [] |
| 72 | + patches = [] |
| 73 | + affected_packages = [] |
| 74 | + |
| 75 | + for fixed_version, commit_hash in fixed_versions_commits: |
| 76 | + patch_url = f"https://github.com/torvalds/linux/commit/{commit_hash}" |
| 77 | + if not commit_hash: |
| 78 | + continue |
| 79 | + |
| 80 | + base_purl, patch_objs = classify_patch_source( |
| 81 | + url=patch_url, |
| 82 | + commit_hash=commit_hash, |
| 83 | + patch_text=None, |
| 84 | + ) |
| 85 | + |
| 86 | + for patch_obj in patch_objs: |
| 87 | + fixed_version_range = GenericVersionRange.from_versions([fixed_version]) |
| 88 | + if isinstance(patch_obj, PackageCommitPatchData): |
| 89 | + fixed_commit = patch_obj |
| 90 | + affected_package = AffectedPackageV2( |
| 91 | + package=base_purl, |
| 92 | + fixed_by_commit_patches=[fixed_commit], |
| 93 | + fixed_version_range=fixed_version_range, |
| 94 | + ) |
| 95 | + affected_packages.append(affected_package) |
| 96 | + elif isinstance(patch_obj, PatchData): |
| 97 | + patches.append(patch_obj) |
| 98 | + elif isinstance(patch_obj, ReferenceV2): |
| 99 | + references.append(patch_obj) |
| 100 | + |
| 101 | + yield AdvisoryDataV2( |
| 102 | + advisory_id=vulnerability_id, |
| 103 | + references=references, |
| 104 | + affected_packages=affected_packages, |
| 105 | + patches=patches, |
| 106 | + url="https://github.com/nluedtke/linux_kernel_cves", |
| 107 | + ) |
| 108 | + |
| 109 | + def parse_commits_file(self, file_path): |
| 110 | + """Extract CVE-ID and commit hashes from a text file""" |
| 111 | + self.cve_to_fixed_versions_and_commits = defaultdict(set) |
| 112 | + fixed_version = None |
| 113 | + with open(file_path, "r", encoding="utf-8") as f: |
| 114 | + for line in f: |
| 115 | + line = line.strip() |
| 116 | + |
| 117 | + if not line: |
| 118 | + continue |
| 119 | + |
| 120 | + if line.startswith("CVEs fixed in"): |
| 121 | + fixed_version = line.replace("CVEs fixed in", "").strip().rstrip(":") |
| 122 | + continue |
| 123 | + |
| 124 | + parts = line.split(":", 2) |
| 125 | + |
| 126 | + if len(parts) < 2: |
| 127 | + continue |
| 128 | + |
| 129 | + cve_part = parts[0] |
| 130 | + commit_part = parts[1] |
| 131 | + |
| 132 | + cve_match = cve_regex.search(cve_part) |
| 133 | + if not cve_match: |
| 134 | + continue |
| 135 | + |
| 136 | + cve = cve_match.group(0) |
| 137 | + |
| 138 | + sha1_match = commit_regex.search(commit_part) |
| 139 | + commit_hash = sha1_match.group(0) if sha1_match else None |
| 140 | + |
| 141 | + if not commit_hash or not is_commit(commit_hash): |
| 142 | + continue |
| 143 | + |
| 144 | + self.cve_to_fixed_versions_and_commits[cve].add((fixed_version, commit_hash)) |
| 145 | + |
| 146 | + def clean_downloads(self): |
| 147 | + """Cleanup any temporary repository data.""" |
| 148 | + if self.vcs_response: |
| 149 | + self.log("Removing cloned repository") |
| 150 | + self.vcs_response.delete() |
| 151 | + |
| 152 | + def on_failure(self): |
| 153 | + """Ensure cleanup is always performed on failure.""" |
| 154 | + self.clean_downloads() |
0 commit comments