|
| 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 | + |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import dateparser |
| 13 | +from fetchcode.vcs import fetch_via_vcs |
| 14 | +from packageurl import PackageURL |
| 15 | +from pytz import UTC |
| 16 | +from univers.version_range import GenericVersionRange |
| 17 | +from univers.versions import InvalidVersion |
| 18 | + |
| 19 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 20 | +from vulnerabilities.importer import AffectedPackageV2 |
| 21 | +from vulnerabilities.importer import PackageCommitPatchData |
| 22 | +from vulnerabilities.importer import logger |
| 23 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 24 | +from vulnerabilities.utils import build_description |
| 25 | + |
| 26 | + |
| 27 | +class GlibcImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 28 | + """ |
| 29 | + Pipeline to collect glibc advisories: |
| 30 | + """ |
| 31 | + |
| 32 | + pipeline_id = "glibc_importer_v2" |
| 33 | + spdx_license_expression = "LGPL-2.1-only" |
| 34 | + license_url = "https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=LICENSES" |
| 35 | + repo_url = "git+https://sourceware.org/git/glibc.git" |
| 36 | + |
| 37 | + precedence = 200 |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def steps(cls): |
| 41 | + return ( |
| 42 | + cls.clone, |
| 43 | + cls.collect_and_store_advisories, |
| 44 | + cls.clean_downloads, |
| 45 | + ) |
| 46 | + |
| 47 | + def clone(self): |
| 48 | + self.log(f"Cloning `{self.repo_url}`") |
| 49 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 50 | + |
| 51 | + def advisories_count(self): |
| 52 | + root = Path(self.vcs_response.dest_dir) / "advisories" |
| 53 | + return sum(1 for _ in root.rglob("*")) |
| 54 | + |
| 55 | + def collect_advisories(self): |
| 56 | + base_path = Path(self.vcs_response.dest_dir) / "advisories" |
| 57 | + for file_path in base_path.rglob("*"): |
| 58 | + if not file_path.name.startswith("GLIBC-SA"): |
| 59 | + continue |
| 60 | + |
| 61 | + with open(file_path) as f: |
| 62 | + vulnerability_data = f.read() |
| 63 | + |
| 64 | + parsed_items = self.parse_advisory_text(vulnerability_data) |
| 65 | + fixed_commits, fixed_versions, affected_commits, affected_versions = [], [], [], [] |
| 66 | + advisory_id = file_path.name |
| 67 | + cve_id = None |
| 68 | + summary = None |
| 69 | + description = None |
| 70 | + date_published = None |
| 71 | + for item in parsed_items: |
| 72 | + name = item.get("name") |
| 73 | + if name == "summary": |
| 74 | + summary = item.get("value") |
| 75 | + elif name == "description": |
| 76 | + description = item.get("value") |
| 77 | + elif name == "CVE-Id": |
| 78 | + cve_id = item.get("value") |
| 79 | + elif name == "Public-Date": |
| 80 | + date_published_value = item.get("value") |
| 81 | + date_published = dateparser.parse(date_published_value).replace(tzinfo=UTC) |
| 82 | + elif name == "Vulnerable-Commit": |
| 83 | + fix_commit = item.get("commit") |
| 84 | + affected_commits.append(fix_commit) |
| 85 | + |
| 86 | + fixed_version = item.get("version") |
| 87 | + affected_versions.append(fixed_version) |
| 88 | + elif name == "Fix-Commit": |
| 89 | + fix_commit = item.get("commit") |
| 90 | + fixed_commits.append(fix_commit) |
| 91 | + |
| 92 | + fixed_version = item.get("version") |
| 93 | + fixed_versions.append(fixed_version) |
| 94 | + |
| 95 | + affected_packages = [] |
| 96 | + purl = PackageURL( |
| 97 | + type="sid", |
| 98 | + namespace="gnu.org", |
| 99 | + name="glibc", |
| 100 | + ) |
| 101 | + |
| 102 | + affected_version_range = None |
| 103 | + try: |
| 104 | + affected_version_range = GenericVersionRange.from_versions(affected_versions) |
| 105 | + except InvalidVersion as e: |
| 106 | + logger.error( |
| 107 | + f"InvalidVersion while parsing affected_version_range: {affected_versions} error: {e}" |
| 108 | + ) |
| 109 | + |
| 110 | + fixed_version_range = None |
| 111 | + try: |
| 112 | + fixed_version_range = GenericVersionRange.from_versions(fixed_versions) |
| 113 | + except InvalidVersion as e: |
| 114 | + logger.error( |
| 115 | + f"InvalidVersion while parsing fixed_version_range: {fixed_versions} error: {e}" |
| 116 | + ) |
| 117 | + |
| 118 | + fixed_by_commit_patches = [ |
| 119 | + PackageCommitPatchData( |
| 120 | + vcs_url="https://sourceware.org/git/glibc.git", commit_hash=fixed_commit |
| 121 | + ) |
| 122 | + for fixed_commit in fixed_commits |
| 123 | + ] |
| 124 | + introduced_by_commit_patches = [ |
| 125 | + PackageCommitPatchData( |
| 126 | + vcs_url="https://sourceware.org/git/glibc.git", commit_hash=affected_commit |
| 127 | + ) |
| 128 | + for affected_commit in affected_commits |
| 129 | + ] |
| 130 | + |
| 131 | + if ( |
| 132 | + affected_version_range |
| 133 | + or fixed_version_range |
| 134 | + or introduced_by_commit_patches |
| 135 | + or fixed_by_commit_patches |
| 136 | + ): |
| 137 | + affected_packages.append( |
| 138 | + AffectedPackageV2( |
| 139 | + package=purl, |
| 140 | + affected_version_range=affected_version_range, |
| 141 | + fixed_version_range=fixed_version_range, |
| 142 | + introduced_by_commit_patches=introduced_by_commit_patches, |
| 143 | + fixed_by_commit_patches=fixed_by_commit_patches, |
| 144 | + ) |
| 145 | + ) |
| 146 | + |
| 147 | + yield AdvisoryDataV2( |
| 148 | + advisory_id=advisory_id, |
| 149 | + aliases=[cve_id] if cve_id else [], |
| 150 | + summary=build_description(summary, description), |
| 151 | + affected_packages=affected_packages, |
| 152 | + date_published=date_published, |
| 153 | + url="https://sourceware.org/git/glibc.git", |
| 154 | + ) |
| 155 | + |
| 156 | + def parse_advisory_text(self, text): |
| 157 | + summary, _, tail = text.partition("\n\n") |
| 158 | + description, _, metadata = tail.partition("\n\n") |
| 159 | + |
| 160 | + parsed = [ |
| 161 | + {"name": "summary", "value": summary}, |
| 162 | + {"name": "description", "value": description}, |
| 163 | + ] |
| 164 | + |
| 165 | + for line in metadata.splitlines(): |
| 166 | + name, _, value = line.partition(": ") |
| 167 | + if name.endswith( |
| 168 | + ( |
| 169 | + "Commit", |
| 170 | + "Backport", |
| 171 | + ) |
| 172 | + ): |
| 173 | + commit, _, version = value.partition(" ") |
| 174 | + parsed.append({"name": name, "commit": commit, "version": version.strip(")(")}) |
| 175 | + else: |
| 176 | + parsed.append({"name": name, "value": value}) |
| 177 | + return parsed |
| 178 | + |
| 179 | + def clean_downloads(self): |
| 180 | + """Cleanup any temporary repository data.""" |
| 181 | + if self.vcs_response: |
| 182 | + self.log(f"Removing cloned repository") |
| 183 | + self.vcs_response.delete() |
| 184 | + |
| 185 | + def on_failure(self): |
| 186 | + """Ensure cleanup is always performed on failure.""" |
| 187 | + self.clean_downloads() |
0 commit comments