From 887d43ea67d65f083b8592534f14d55be0cfacc1 Mon Sep 17 00:00:00 2001 From: ziadhany Date: Thu, 7 Sep 2023 22:04:37 +0300 Subject: [PATCH] Rename expected files Add OSSFuzzImprover to IMPROVERS_REGISTRY Fix oss-fuzz test ( add weakness in expected test file ) Add oss-fuzz tests Import data from oss_fuzz using osv format Resolve merge conflicts Signed-off-by: ziadhany --- vulnerabilities/importers/__init__.py | 2 + vulnerabilities/importers/oss_fuzz.py | 37 +++++++++++++++++ vulnerabilities/improvers/__init__.py | 1 + vulnerabilities/improvers/valid_versions.py | 6 +++ .../test_data/oss_fuzz/oss-fuzz-data1.yaml | 41 +++++++++++++++++++ .../oss-fuzz-data1.yaml-expected.json | 20 +++++++++ .../test_data/oss_fuzz/oss-fuzz-data2.yaml | 33 +++++++++++++++ .../oss-fuzz-data2.yaml-expected.json | 20 +++++++++ vulnerabilities/tests/test_oss_fuzz.py | 36 ++++++++++++++++ 9 files changed, 196 insertions(+) create mode 100644 vulnerabilities/importers/oss_fuzz.py create mode 100644 vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml create mode 100644 vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml-expected.json create mode 100644 vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml create mode 100644 vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml-expected.json create mode 100644 vulnerabilities/tests/test_oss_fuzz.py diff --git a/vulnerabilities/importers/__init__.py b/vulnerabilities/importers/__init__.py index 80809be62..add6967f8 100644 --- a/vulnerabilities/importers/__init__.py +++ b/vulnerabilities/importers/__init__.py @@ -25,6 +25,7 @@ from vulnerabilities.importers import npm from vulnerabilities.importers import nvd from vulnerabilities.importers import openssl +from vulnerabilities.importers import oss_fuzz from vulnerabilities.importers import postgresql from vulnerabilities.importers import project_kb_msr2019 from vulnerabilities.importers import pypa @@ -65,6 +66,7 @@ ubuntu_usn.UbuntuUSNImporter, fireeye.FireyeImporter, apache_kafka.ApacheKafkaImporter, + oss_fuzz.OSSFuzzImporter, ] IMPORTERS_REGISTRY = {x.qualified_name: x for x in IMPORTERS_REGISTRY} diff --git a/vulnerabilities/importers/oss_fuzz.py b/vulnerabilities/importers/oss_fuzz.py new file mode 100644 index 000000000..6e4d3fef4 --- /dev/null +++ b/vulnerabilities/importers/oss_fuzz.py @@ -0,0 +1,37 @@ +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# VulnerableCode is a trademark of nexB Inc. +# SPDX-License-Identifier: Apache-2.0 +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +# See https://github.com/nexB/vulnerablecode for support or download. +# See https://aboutcode.org for more information about nexB OSS projects. +# +import logging +from pathlib import Path +from typing import Iterable + +import saneyaml + +from vulnerabilities.importer import AdvisoryData +from vulnerabilities.importer import Importer +from vulnerabilities.importers.osv import parse_advisory_data + +logger = logging.getLogger(__name__) + + +class OSSFuzzImporter(Importer): + license_url = "https://github.com/google/oss-fuzz-vulns/blob/main/LICENSE" + spdx_license_expression = "CC-BY-4.0" + url = "git+https://github.com/google/oss-fuzz-vulns" + + def advisory_data(self) -> Iterable[AdvisoryData]: + try: + self.clone(repo_url=self.url) + path = Path(self.vcs_response.dest_dir) / "vulns" + for file in path.glob("**/*.yaml"): + with open(file) as f: + yaml_data = saneyaml.load(f.read()) + yield parse_advisory_data(yaml_data, supported_ecosystem="oss-fuzz") + finally: + if self.vcs_response: + self.vcs_response.delete() diff --git a/vulnerabilities/improvers/__init__.py b/vulnerabilities/improvers/__init__.py index efbf9d43b..b49bb931d 100644 --- a/vulnerabilities/improvers/__init__.py +++ b/vulnerabilities/improvers/__init__.py @@ -22,6 +22,7 @@ valid_versions.IstioImprover, valid_versions.DebianOvalImprover, valid_versions.UbuntuOvalImprover, + valid_versions.OSSFuzzImprover, ] IMPROVERS_REGISTRY = {x.qualified_name: x for x in IMPROVERS_REGISTRY} diff --git a/vulnerabilities/improvers/valid_versions.py b/vulnerabilities/improvers/valid_versions.py index 5b1796339..61f62d5a7 100644 --- a/vulnerabilities/improvers/valid_versions.py +++ b/vulnerabilities/improvers/valid_versions.py @@ -35,6 +35,7 @@ from vulnerabilities.importers.istio import IstioImporter from vulnerabilities.importers.nginx import NginxImporter from vulnerabilities.importers.npm import NpmImporter +from vulnerabilities.importers.oss_fuzz import OSSFuzzImporter from vulnerabilities.importers.ubuntu import UbuntuImporter from vulnerabilities.improver import MAX_CONFIDENCE from vulnerabilities.improver import Improver @@ -477,3 +478,8 @@ class DebianOvalImprover(ValidVersionImprover): class UbuntuOvalImprover(ValidVersionImprover): importer = UbuntuImporter ignorable_versions = [] + + +class OSSFuzzImprover(ValidVersionImprover): + importer = OSSFuzzImporter + ignorable_versions = [] diff --git a/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml new file mode 100644 index 000000000..dbe1a8a08 --- /dev/null +++ b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml @@ -0,0 +1,41 @@ +id: OSV-2021-933 +summary: Heap-buffer-overflow in print_mac +details: | + OSS-Fuzz report: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35887 + + ``` + Crash type: Heap-buffer-overflow WRITE 4 + Crash state: + print_mac + log_packet + dhcp_reply + ``` +modified: '2022-04-13T03:04:31.143462Z' +published: '2021-07-08T00:01:26.369555Z' +references: +- type: REPORT + url: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35887 +affected: +- package: + name: dnsmasq + ecosystem: OSS-Fuzz + ranges: + - type: GIT + repo: git://thekelleys.org.uk/dnsmasq.git + events: + - introduced: 96f6444958c29a670f4254722d787f328153605c + - fixed: d242cbffa4f20c9f7472f79b3a9e47008b6fe77c + versions: + - v2.86 + - v2.86rc1 + - v2.86rc2 + - v2.86rc3 + - v2.86test5 + - v2.86test6 + - v2.86test7 + - v2.87test1 + - v2.87test2 + - v2.87test3 + - v2.87test4 + ecosystem_specific: + severity: HIGH diff --git a/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml-expected.json b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml-expected.json new file mode 100644 index 000000000..4b7914971 --- /dev/null +++ b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data1.yaml-expected.json @@ -0,0 +1,20 @@ +{ + "aliases": [ + "OSV-2021-933" + ], + "summary": "Heap-buffer-overflow in print_mac\nOSS-Fuzz report: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35887\n\n```\nCrash type: Heap-buffer-overflow WRITE 4\nCrash state:\nprint_mac\nlog_packet\ndhcp_reply\n```", + "affected_packages": [ + + ], + "references": [ + { + "reference_id": "", + "url": "https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35887", + "severities": [ + + ] + } + ], + "date_published": "2021-07-08T00:01:26.369555+00:00", + "weaknesses": [] +} \ No newline at end of file diff --git a/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml new file mode 100644 index 000000000..cff7c1fec --- /dev/null +++ b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml @@ -0,0 +1,33 @@ +id: OSV-2022-145 +summary: Heap-buffer-overflow in print_mac +details: | + OSS-Fuzz report: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=44581 + + ``` + Crash type: Heap-buffer-overflow WRITE 4 + Crash state: + print_mac + log_packet + dhcp_reply + ``` +modified: '2022-04-13T03:04:31.179893Z' +published: '2022-02-13T00:01:27.883603Z' +references: +- type: REPORT + url: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=44581 +affected: +- package: + name: dnsmasq + ecosystem: OSS-Fuzz + purl: pkg:generic/dnsmasq + ranges: + - type: GIT + repo: git://thekelleys.org.uk/dnsmasq.git + events: + - introduced: e426c2d3bc182d790f83039b77a09d55230ca71f + - fixed: 03345ecefeb0d82e3c3a4c28f27c3554f0611b39 + versions: + - v2.87test8 + ecosystem_specific: + severity: HIGH +schema_version: 1.2.0 diff --git a/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml-expected.json b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml-expected.json new file mode 100644 index 000000000..a197ab4d4 --- /dev/null +++ b/vulnerabilities/tests/test_data/oss_fuzz/oss-fuzz-data2.yaml-expected.json @@ -0,0 +1,20 @@ +{ + "aliases": [ + "OSV-2022-145" + ], + "summary": "Heap-buffer-overflow in print_mac\nOSS-Fuzz report: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=44581\n\n```\nCrash type: Heap-buffer-overflow WRITE 4\nCrash state:\nprint_mac\nlog_packet\ndhcp_reply\n```", + "affected_packages": [ + + ], + "references": [ + { + "reference_id": "", + "url": "https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=44581", + "severities": [ + + ] + } + ], + "date_published": "2022-02-13T00:01:27.883603+00:00", + "weaknesses": [] +} \ No newline at end of file diff --git a/vulnerabilities/tests/test_oss_fuzz.py b/vulnerabilities/tests/test_oss_fuzz.py new file mode 100644 index 000000000..c6dcb501e --- /dev/null +++ b/vulnerabilities/tests/test_oss_fuzz.py @@ -0,0 +1,36 @@ +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# VulnerableCode is a trademark of nexB Inc. +# SPDX-License-Identifier: Apache-2.0 +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +# See https://github.com/nexB/vulnerablecode for support or download. +# See https://aboutcode.org for more information about nexB OSS projects. +# +import os +from unittest import TestCase + +import saneyaml + +from vulnerabilities.importers.osv import parse_advisory_data +from vulnerabilities.tests import util_tests + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +TEST_DATA = os.path.join(BASE_DIR, "test_data/oss_fuzz") + + +class TestOSSFuzzImporter(TestCase): + def test_to_advisories1(self): + with open(os.path.join(TEST_DATA, "oss-fuzz-data1.yaml")) as f: + mock_response = saneyaml.load(f) + expected_file = os.path.join(TEST_DATA, "oss-fuzz-data1.yaml-expected.json") + imported_data = parse_advisory_data(mock_response, "oss-fuzz") + result = imported_data.to_dict() + util_tests.check_results_against_json(result, expected_file) + + def test_to_advisorie2(self): + with open(os.path.join(TEST_DATA, "oss-fuzz-data2.yaml")) as f: + mock_response = saneyaml.load(f) + expected_file = os.path.join(TEST_DATA, "oss-fuzz-data2.yaml-expected.json") + imported_data = parse_advisory_data(mock_response, "oss-fuzz") + result = imported_data.to_dict() + util_tests.check_results_against_json(result, expected_file)