Skip to content

Commit 53c09c8

Browse files
committed
Add initial support for collecting linux kernel commits.
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent dcb0511 commit 53c09c8

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

vulnerabilities/improvers/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
from vulnerabilities.pipelines import flag_ghost_packages
2020
from vulnerabilities.pipelines import populate_vulnerability_summary_pipeline
2121
from vulnerabilities.pipelines import remove_duplicate_advisories
22+
from vulnerabilities.pipelines.v2_improvers import (
23+
collect_linux_kernel_cves_commits as collect_linux_kernel_cves_commits_v2,
24+
)
2225
from vulnerabilities.pipelines.v2_improvers import compute_advisory_todo as compute_advisory_todo_v2
2326
from vulnerabilities.pipelines.v2_improvers import compute_package_risk as compute_package_risk_v2
2427
from vulnerabilities.pipelines.v2_improvers import (
@@ -68,5 +71,6 @@
6871
compute_version_rank_v2.ComputeVersionRankPipeline,
6972
compute_advisory_todo_v2.ComputeToDo,
7073
compute_advisory_todo.ComputeToDo,
74+
collect_linux_kernel_cves_commits_v2.CollectFixCommitLinuxKernelPipeline,
7175
]
7276
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
import re
10+
from pathlib import Path
11+
12+
from fetchcode.vcs import fetch_via_vcs
13+
14+
from vulnerabilities.models import AdvisoryV2
15+
from vulnerabilities.models import CodeFixV2
16+
from vulnerabilities.pipelines import VulnerableCodePipeline
17+
from vulnerabilities.utils import cve_regex
18+
19+
20+
class CollectFixCommitLinuxKernelPipeline(VulnerableCodePipeline):
21+
"""
22+
Pipeline to collect fix commits from Linux Kernel:
23+
"""
24+
25+
pipeline_id = "linux_kernel_cves_fix_commits"
26+
spdx_license_expression = "Apache-2.0"
27+
license_url = "https://github.com/quarkslab/aosp_dataset/blob/master/LICENSE"
28+
importer_name = "linux_kernel_cves_fix_commits"
29+
qualified_name = "linux_kernel_cves_fix_commits"
30+
repo_url = "git+https://github.com/nluedtke/linux_kernel_cves"
31+
32+
@classmethod
33+
def steps(cls):
34+
return (
35+
cls.clone,
36+
cls.collect_fix_commits,
37+
)
38+
39+
def clone(self):
40+
self.log(f"Cloning `{self.repo_url}`")
41+
self.vcs_response = fetch_via_vcs(self.repo_url)
42+
43+
def collect_fix_commits(self):
44+
self.log(f"Processing aosp_dataset fix commits.")
45+
base_path = Path(self.vcs_response.dest_dir) / "data"
46+
for file_path in base_path.rglob("*.txt"):
47+
if "_CVEs.txt" in file_path.name:
48+
continue
49+
50+
if "_security.txt" in file_path.name:
51+
for vulnerability_id, commit_hash in self.parse_commits_file(file_path):
52+
53+
kernel_urls = [
54+
f"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/{commit_hash}",
55+
f"https://github.com/torvalds/linux/commit/{commit_hash}",
56+
]
57+
58+
if not (vulnerability_id and commit_hash):
59+
continue
60+
61+
try:
62+
advisories = AdvisoryV2.objects.filter(
63+
advisory_id__iendswith=vulnerability_id
64+
)
65+
except AdvisoryV2.DoesNotExist:
66+
self.log(f"Can't find vulnerability_id: {vulnerability_id}")
67+
continue
68+
69+
for advisory in advisories:
70+
for impact in advisory.impacted_packages.all():
71+
for package in impact.affecting_packages.all():
72+
code_fix, created = CodeFixV2.objects.get_or_create(
73+
commits=[kernel_urls],
74+
advisory=advisory,
75+
affected_package=package,
76+
)
77+
78+
if created:
79+
self.log(
80+
f"Created CodeFix entry for vulnerability_id: {vulnerability_id} with VCS URL {kernel_urls}"
81+
)
82+
83+
def parse_commits_file(self, file_path):
84+
sha1_pattern = re.compile(r"\b[a-f0-9]{40}\b")
85+
86+
with open(file_path, "r", encoding="utf-8") as f:
87+
for line in f:
88+
line = line.strip()
89+
if not line:
90+
continue
91+
92+
cve_match = cve_regex.search(line)
93+
cve = cve_match.group(1) if cve_match else None
94+
95+
sha1_match = sha1_pattern.search(line)
96+
commit_hash = sha1_match.group(0) if sha1_match else None
97+
yield cve, commit_hash
98+
99+
def clean_downloads(self):
100+
if self.vcs_response:
101+
self.log(f"Removing cloned repository")
102+
self.vcs_response.delete()
103+
104+
def on_failure(self):
105+
self.clean_downloads()

0 commit comments

Comments
 (0)