Add files using upload-large-folder tool
Browse files- local-test-tika-delta-05/fuzz-tooling/infra/base-images/README.md +6 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/affected_fuzz_targets.py +113 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/base_runner_utils.py +33 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/build_fuzzers.py +217 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/cifuzz_end_to_end_test.py +48 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/config_utils_test.py +200 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/continuous_integration_test.py +88 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/docker_test.py +119 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/generate_coverage_report_test.py +71 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/http_utils.py +118 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/requirements.txt +4 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/run_cifuzz.py +89 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/run_fuzzers.py +321 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/run_fuzzers_entrypoint.py +97 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/sarif_utils.py +251 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/sarif_utils_test.py +128 -0
- local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/test_helpers.py +117 -0
- local-test-tika-delta-05/fuzz-tooling/infra/experimental/SystemSan/SystemSan.cpp +493 -0
- local-test-tika-delta-05/fuzz-tooling/infra/experimental/chronos/build_all.sh +33 -0
- local-test-tika-delta-05/fuzz-tooling/tools/vscode-extension/src/commands/cmdBuildFuzzerFromWorkspaceCFLite.ts +49 -0
local-test-tika-delta-05/fuzz-tooling/infra/base-images/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Building all infra images:
|
| 2 |
+
|
| 3 |
+
```bash
|
| 4 |
+
# run from project root
|
| 5 |
+
infra/base-images/all.sh
|
| 6 |
+
```
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/affected_fuzz_targets.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for dealing with fuzz targets affected by the change-under-test
|
| 15 |
+
(CUT)."""
|
| 16 |
+
import logging
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
|
| 20 |
+
# pylint: disable=wrong-import-position,import-error
|
| 21 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 22 |
+
import utils
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def remove_unaffected_fuzz_targets(clusterfuzz_deployment, out_dir,
|
| 26 |
+
files_changed, repo_path):
|
| 27 |
+
"""Removes all non affected fuzz targets in the out directory.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
clusterfuzz_deployment: The ClusterFuzz deployment object.
|
| 31 |
+
out_dir: The location of the fuzz target binaries.
|
| 32 |
+
files_changed: A list of files changed compared to HEAD.
|
| 33 |
+
repo_path: The location of the OSS-Fuzz repo in the docker image.
|
| 34 |
+
|
| 35 |
+
This function will not delete fuzz targets unless it knows that the fuzz
|
| 36 |
+
targets are unaffected. For example, this means that fuzz targets which don't
|
| 37 |
+
have coverage data on will not be deleted.
|
| 38 |
+
"""
|
| 39 |
+
if not files_changed:
|
| 40 |
+
# Don't remove any fuzz targets if there is no difference from HEAD.
|
| 41 |
+
logging.info('No files changed compared to HEAD.')
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
+
logging.info('Files changed in PR: %s', files_changed)
|
| 45 |
+
|
| 46 |
+
fuzz_target_paths = utils.get_fuzz_targets(out_dir)
|
| 47 |
+
if not fuzz_target_paths:
|
| 48 |
+
# Nothing to remove.
|
| 49 |
+
logging.error('No fuzz targets found in out dir.')
|
| 50 |
+
return
|
| 51 |
+
|
| 52 |
+
coverage = clusterfuzz_deployment.get_coverage(repo_path)
|
| 53 |
+
if not coverage:
|
| 54 |
+
# Don't remove any fuzz targets unless we have data.
|
| 55 |
+
logging.error('Could not find latest coverage report.')
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
affected_fuzz_targets = get_affected_fuzz_targets(coverage, fuzz_target_paths,
|
| 59 |
+
files_changed)
|
| 60 |
+
|
| 61 |
+
if not affected_fuzz_targets:
|
| 62 |
+
logging.info('No affected fuzz targets detected, keeping all as fallback.')
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
logging.info('Using affected fuzz targets: %s.', affected_fuzz_targets)
|
| 66 |
+
unaffected_fuzz_targets = set(fuzz_target_paths) - affected_fuzz_targets
|
| 67 |
+
logging.info('Removing unaffected fuzz targets: %s.', unaffected_fuzz_targets)
|
| 68 |
+
|
| 69 |
+
# Remove all the targets that are not affected.
|
| 70 |
+
for fuzz_target_path in unaffected_fuzz_targets:
|
| 71 |
+
try:
|
| 72 |
+
os.remove(fuzz_target_path)
|
| 73 |
+
except OSError as error:
|
| 74 |
+
logging.error('%s occurred while removing file %s', error,
|
| 75 |
+
fuzz_target_path)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def is_fuzz_target_affected(coverage, fuzz_target_path, files_changed):
|
| 79 |
+
"""Returns True if a fuzz target (|fuzz_target_path|) is affected by
|
| 80 |
+
|files_changed|."""
|
| 81 |
+
fuzz_target = os.path.basename(fuzz_target_path)
|
| 82 |
+
covered_files = coverage.get_files_covered_by_target(fuzz_target)
|
| 83 |
+
if not covered_files:
|
| 84 |
+
# Assume a fuzz target is affected if we can't get its coverage from
|
| 85 |
+
# OSS-Fuzz.
|
| 86 |
+
# TODO(metzman): Figure out what we should do if covered_files is [].
|
| 87 |
+
# Should we act as if we couldn't get the coverage?
|
| 88 |
+
logging.info('Could not get coverage for %s. Treating as affected.',
|
| 89 |
+
fuzz_target)
|
| 90 |
+
return True
|
| 91 |
+
|
| 92 |
+
covered_files = [
|
| 93 |
+
os.path.normpath(covered_file) for covered_file in covered_files
|
| 94 |
+
]
|
| 95 |
+
logging.info('Fuzz target %s is affected by: %s', fuzz_target, covered_files)
|
| 96 |
+
for filename in files_changed:
|
| 97 |
+
if filename in covered_files:
|
| 98 |
+
logging.info('Fuzz target %s is affected by changed file: %s',
|
| 99 |
+
fuzz_target, filename)
|
| 100 |
+
return True
|
| 101 |
+
|
| 102 |
+
logging.info('Fuzz target %s is not affected.', fuzz_target)
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def get_affected_fuzz_targets(coverage, fuzz_target_paths, files_changed):
|
| 107 |
+
"""Returns a list of paths of affected targets."""
|
| 108 |
+
affected_fuzz_targets = set()
|
| 109 |
+
for fuzz_target_path in fuzz_target_paths:
|
| 110 |
+
if is_fuzz_target_affected(coverage, fuzz_target_path, files_changed):
|
| 111 |
+
affected_fuzz_targets.add(fuzz_target_path)
|
| 112 |
+
|
| 113 |
+
return affected_fuzz_targets
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/base_runner_utils.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Utilities for scripts from ghcr.io/aixcc-finals/base-runner."""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
|
| 18 |
+
import config_utils
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_env(config, workspace):
|
| 22 |
+
"""Returns a dictionary containing the current environment with additional env
|
| 23 |
+
vars set to values needed to run a fuzzer."""
|
| 24 |
+
env = os.environ.copy()
|
| 25 |
+
env['SANITIZER'] = config.sanitizer
|
| 26 |
+
env['FUZZING_LANGUAGE'] = config.language
|
| 27 |
+
env['OUT'] = workspace.out
|
| 28 |
+
env['CIFUZZ'] = 'True'
|
| 29 |
+
env['FUZZING_ENGINE'] = config_utils.DEFAULT_ENGINE
|
| 30 |
+
env['ARCHITECTURE'] = config.architecture
|
| 31 |
+
# Do this so we don't fail in tests.
|
| 32 |
+
env['FUZZER_ARGS'] = '-rss_limit_mb=2560 -timeout=25'
|
| 33 |
+
return env
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/build_fuzzers.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module used by CI tools in order to interact with fuzzers. This module helps
|
| 15 |
+
CI tools to build fuzzers."""
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
import os
|
| 19 |
+
import sys
|
| 20 |
+
|
| 21 |
+
import affected_fuzz_targets
|
| 22 |
+
import base_runner_utils
|
| 23 |
+
import clusterfuzz_deployment
|
| 24 |
+
import continuous_integration
|
| 25 |
+
import docker
|
| 26 |
+
import logs
|
| 27 |
+
import workspace_utils
|
| 28 |
+
|
| 29 |
+
# pylint: disable=wrong-import-position,import-error
|
| 30 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 31 |
+
import helper
|
| 32 |
+
import utils
|
| 33 |
+
|
| 34 |
+
logs.init()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def check_project_src_path(project_src_path):
|
| 38 |
+
"""Returns True if |project_src_path| exists."""
|
| 39 |
+
if not os.path.exists(project_src_path):
|
| 40 |
+
logging.error(
|
| 41 |
+
'PROJECT_SRC_PATH: %s does not exist. '
|
| 42 |
+
'Are you mounting it correctly?', project_src_path)
|
| 43 |
+
return False
|
| 44 |
+
return True
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# pylint: disable=too-many-arguments
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class Builder: # pylint: disable=too-many-instance-attributes
|
| 51 |
+
"""Class for fuzzer builders."""
|
| 52 |
+
|
| 53 |
+
def __init__(self, config, ci_system):
|
| 54 |
+
self.config = config
|
| 55 |
+
self.ci_system = ci_system
|
| 56 |
+
self.workspace = workspace_utils.Workspace(config)
|
| 57 |
+
self.workspace.initialize_dir(self.workspace.out)
|
| 58 |
+
self.workspace.initialize_dir(self.workspace.work)
|
| 59 |
+
self.clusterfuzz_deployment = (
|
| 60 |
+
clusterfuzz_deployment.get_clusterfuzz_deployment(
|
| 61 |
+
self.config, self.workspace))
|
| 62 |
+
self.image_repo_path = None
|
| 63 |
+
self.host_repo_path = None
|
| 64 |
+
self.repo_manager = None
|
| 65 |
+
|
| 66 |
+
def build_image_and_checkout_src(self):
|
| 67 |
+
"""Builds the project builder image and checkout source code for the patch
|
| 68 |
+
we want to fuzz (if necessary). Returns True on success."""
|
| 69 |
+
result = self.ci_system.prepare_for_fuzzer_build()
|
| 70 |
+
if not result.success:
|
| 71 |
+
return False
|
| 72 |
+
self.image_repo_path = result.image_repo_path
|
| 73 |
+
self.repo_manager = result.repo_manager
|
| 74 |
+
if self.config.output_sarif:
|
| 75 |
+
self.workspace.make_repo_for_sarif(self.repo_manager)
|
| 76 |
+
logging.info('repo_dir: %s.', self.repo_manager.repo_dir)
|
| 77 |
+
self.host_repo_path = self.repo_manager.repo_dir
|
| 78 |
+
return True
|
| 79 |
+
|
| 80 |
+
def build_fuzzers(self):
|
| 81 |
+
"""Moves the source code we want to fuzz into the project builder and builds
|
| 82 |
+
the fuzzers from that source code. Returns True on success."""
|
| 83 |
+
docker_args, docker_container = docker.get_base_docker_run_args(
|
| 84 |
+
self.workspace, self.config.sanitizer, self.config.language,
|
| 85 |
+
self.config.architecture, self.config.docker_in_docker)
|
| 86 |
+
if not docker_container:
|
| 87 |
+
docker_args.extend(
|
| 88 |
+
_get_docker_build_fuzzers_args_not_container(self.host_repo_path))
|
| 89 |
+
|
| 90 |
+
build_command = self.ci_system.get_build_command(self.host_repo_path,
|
| 91 |
+
self.image_repo_path)
|
| 92 |
+
|
| 93 |
+
# Set extra environment variables so that they are visible to the build.
|
| 94 |
+
for key in self.config.extra_environment_variables:
|
| 95 |
+
# Don't specify their value in case they get echoed.
|
| 96 |
+
docker_args.extend(['-e', key])
|
| 97 |
+
|
| 98 |
+
docker_args.extend([
|
| 99 |
+
docker.get_project_image_name(self.config.oss_fuzz_project_name),
|
| 100 |
+
'/bin/bash',
|
| 101 |
+
'-c',
|
| 102 |
+
build_command,
|
| 103 |
+
])
|
| 104 |
+
logging.info('Building with %s sanitizer.', self.config.sanitizer)
|
| 105 |
+
|
| 106 |
+
# TODO(metzman): Stop using helper.docker_run so we can get rid of
|
| 107 |
+
# docker.get_base_docker_run_args and merge its contents into
|
| 108 |
+
# docker.get_base_docker_run_command.
|
| 109 |
+
if not helper.docker_run(docker_args):
|
| 110 |
+
logging.error('Building fuzzers failed.')
|
| 111 |
+
return False
|
| 112 |
+
|
| 113 |
+
return True
|
| 114 |
+
|
| 115 |
+
def upload_build(self):
|
| 116 |
+
"""Upload build."""
|
| 117 |
+
if self.config.upload_build:
|
| 118 |
+
self.clusterfuzz_deployment.upload_build(
|
| 119 |
+
self.repo_manager.get_current_commit())
|
| 120 |
+
|
| 121 |
+
return True
|
| 122 |
+
|
| 123 |
+
def check_fuzzer_build(self):
|
| 124 |
+
"""Checks the fuzzer build. Returns True on success or if config specifies
|
| 125 |
+
to skip check."""
|
| 126 |
+
if not self.config.bad_build_check:
|
| 127 |
+
return True
|
| 128 |
+
|
| 129 |
+
return check_fuzzer_build(self.config)
|
| 130 |
+
|
| 131 |
+
def build(self):
|
| 132 |
+
"""Builds the image, checkouts the source (if needed), builds the fuzzers
|
| 133 |
+
and then removes the unaffectted fuzzers. Returns True on success."""
|
| 134 |
+
methods = [
|
| 135 |
+
self.build_image_and_checkout_src,
|
| 136 |
+
self.build_fuzzers,
|
| 137 |
+
self.remove_unaffected_fuzz_targets,
|
| 138 |
+
self.upload_build,
|
| 139 |
+
self.check_fuzzer_build,
|
| 140 |
+
]
|
| 141 |
+
for method in methods:
|
| 142 |
+
if not method():
|
| 143 |
+
return False
|
| 144 |
+
return True
|
| 145 |
+
|
| 146 |
+
def remove_unaffected_fuzz_targets(self):
|
| 147 |
+
"""Removes the fuzzers unaffected by the patch."""
|
| 148 |
+
if self.config.keep_unaffected_fuzz_targets:
|
| 149 |
+
logging.info('Not removing unaffected fuzz targets.')
|
| 150 |
+
return True
|
| 151 |
+
|
| 152 |
+
logging.info('Removing unaffected fuzz targets.')
|
| 153 |
+
changed_files = self.ci_system.get_changed_code_under_test(
|
| 154 |
+
self.repo_manager)
|
| 155 |
+
affected_fuzz_targets.remove_unaffected_fuzz_targets(
|
| 156 |
+
self.clusterfuzz_deployment, self.workspace.out, changed_files,
|
| 157 |
+
self.image_repo_path)
|
| 158 |
+
return True
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def build_fuzzers(config):
|
| 162 |
+
"""Builds all of the fuzzers for a specific OSS-Fuzz project.
|
| 163 |
+
|
| 164 |
+
Args:
|
| 165 |
+
config: The configuration object for building fuzzers.
|
| 166 |
+
|
| 167 |
+
Returns:
|
| 168 |
+
True if build succeeded.
|
| 169 |
+
"""
|
| 170 |
+
# Do some quick validation.
|
| 171 |
+
if config.project_src_path and not check_project_src_path(
|
| 172 |
+
config.project_src_path):
|
| 173 |
+
return False
|
| 174 |
+
|
| 175 |
+
# Get the builder and then build the fuzzers.
|
| 176 |
+
ci_system = continuous_integration.get_ci(config)
|
| 177 |
+
logging.info('ci_system: %s.', ci_system)
|
| 178 |
+
builder = Builder(config, ci_system)
|
| 179 |
+
return builder.build()
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def check_fuzzer_build(config):
|
| 183 |
+
"""Checks the integrity of the built fuzzers.
|
| 184 |
+
|
| 185 |
+
Args:
|
| 186 |
+
config: The config object.
|
| 187 |
+
|
| 188 |
+
Returns:
|
| 189 |
+
True if fuzzers pass OSS-Fuzz's build check.
|
| 190 |
+
"""
|
| 191 |
+
workspace = workspace_utils.Workspace(config)
|
| 192 |
+
if not os.path.exists(workspace.out):
|
| 193 |
+
logging.error('Invalid out directory: %s.', workspace.out)
|
| 194 |
+
return False
|
| 195 |
+
if not os.listdir(workspace.out):
|
| 196 |
+
logging.error('No fuzzers found in out directory: %s.', workspace.out)
|
| 197 |
+
return False
|
| 198 |
+
|
| 199 |
+
env = base_runner_utils.get_env(config, workspace)
|
| 200 |
+
if config.allowed_broken_targets_percentage is not None:
|
| 201 |
+
env['ALLOWED_BROKEN_TARGETS_PERCENTAGE'] = (
|
| 202 |
+
config.allowed_broken_targets_percentage)
|
| 203 |
+
|
| 204 |
+
stdout, stderr, retcode = utils.execute('test_all.py', env=env)
|
| 205 |
+
print(f'Build check: stdout: {stdout}\nstderr: {stderr}')
|
| 206 |
+
if retcode == 0:
|
| 207 |
+
logging.info('Build check passed.')
|
| 208 |
+
return True
|
| 209 |
+
logging.error('Build check failed.')
|
| 210 |
+
return False
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def _get_docker_build_fuzzers_args_not_container(host_repo_path):
|
| 214 |
+
"""Returns arguments to the docker build arguments that are needed to use
|
| 215 |
+
|host_repo_path| when the host of the OSS-Fuzz builder container is not
|
| 216 |
+
another container."""
|
| 217 |
+
return ['-v', f'{host_repo_path}:{host_repo_path}']
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/cifuzz_end_to_end_test.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""End-to-End tests for CIFuzz."""
|
| 15 |
+
import os
|
| 16 |
+
import unittest
|
| 17 |
+
|
| 18 |
+
import run_cifuzz
|
| 19 |
+
import test_helpers
|
| 20 |
+
|
| 21 |
+
CIFUZZ_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 22 |
+
EXTERNAL_PROJECT_PATH = os.path.join(CIFUZZ_DIR, 'test_data',
|
| 23 |
+
'external-project')
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# This test will fail if not run as root because the fuzzer build process
|
| 27 |
+
# creates binaries that only root can write to.
|
| 28 |
+
# Use a seperate env var to keep this seperate from integration tests which
|
| 29 |
+
# don't have this annoying property.
|
| 30 |
+
@unittest.skipIf(not os.getenv('END_TO_END_TESTS'),
|
| 31 |
+
'END_TO_END_TESTS=1 not set')
|
| 32 |
+
class EndToEndTest(unittest.TestCase):
|
| 33 |
+
"""End-to-End tests for CIFuzz."""
|
| 34 |
+
|
| 35 |
+
def setUp(self):
|
| 36 |
+
test_helpers.patch_environ(self, runner=True)
|
| 37 |
+
|
| 38 |
+
def test_simple(self):
|
| 39 |
+
"""Simple end-to-end test using run_cifuzz.main()."""
|
| 40 |
+
os.environ['REPOSITORY'] = 'external-project'
|
| 41 |
+
os.environ['PROJECT_SRC_PATH'] = EXTERNAL_PROJECT_PATH
|
| 42 |
+
os.environ['FILESTORE'] = 'no_filestore'
|
| 43 |
+
os.environ['NO_CLUSTERFUZZ_DEPLOYMENT'] = 'True'
|
| 44 |
+
|
| 45 |
+
with test_helpers.docker_temp_dir() as temp_dir:
|
| 46 |
+
os.environ['WORKSPACE'] = temp_dir
|
| 47 |
+
# TODO(metzman): Verify the crash, affected fuzzers, and other things.
|
| 48 |
+
self.assertEqual(run_cifuzz.main(), 1)
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/config_utils_test.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for config_utils."""
|
| 15 |
+
import os
|
| 16 |
+
import unittest
|
| 17 |
+
from unittest import mock
|
| 18 |
+
|
| 19 |
+
import config_utils
|
| 20 |
+
import constants
|
| 21 |
+
import test_helpers
|
| 22 |
+
|
| 23 |
+
# pylint: disable=no-self-use,protected-access
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class BaseConfigTest(unittest.TestCase):
|
| 27 |
+
"""Tests for BaseConfig."""
|
| 28 |
+
|
| 29 |
+
def setUp(self):
|
| 30 |
+
test_helpers.patch_environ(self)
|
| 31 |
+
|
| 32 |
+
def _create_config(self):
|
| 33 |
+
return config_utils.BuildFuzzersConfig()
|
| 34 |
+
|
| 35 |
+
def test_language_default(self):
|
| 36 |
+
"""Tests that the correct default language is set."""
|
| 37 |
+
config = self._create_config()
|
| 38 |
+
self.assertEqual(config.language, 'c++')
|
| 39 |
+
|
| 40 |
+
def test_language(self):
|
| 41 |
+
"""Tests that the correct language is set."""
|
| 42 |
+
language = 'python'
|
| 43 |
+
os.environ['LANGUAGE'] = language
|
| 44 |
+
config = self._create_config()
|
| 45 |
+
self.assertEqual(config.language, language)
|
| 46 |
+
|
| 47 |
+
def test_is_coverage(self):
|
| 48 |
+
"""Tests that is_coverage is set correctly."""
|
| 49 |
+
# Test it is set when it is supposed to be.
|
| 50 |
+
os.environ['SANITIZER'] = 'coverage'
|
| 51 |
+
config = self._create_config()
|
| 52 |
+
self.assertTrue(config.is_coverage)
|
| 53 |
+
|
| 54 |
+
# Test it is not set when it is not supposed to be.
|
| 55 |
+
os.environ['SANITIZER'] = 'address'
|
| 56 |
+
config = self._create_config()
|
| 57 |
+
self.assertFalse(config.is_coverage)
|
| 58 |
+
|
| 59 |
+
@mock.patch('logging.error')
|
| 60 |
+
def test_validate_no_workspace(self, mock_error):
|
| 61 |
+
"""Tests that validate returns False if GITHUB_WORKSPACE isn't set."""
|
| 62 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 63 |
+
config = self._create_config()
|
| 64 |
+
self.assertFalse(config.validate())
|
| 65 |
+
mock_error.assert_called_with('Must set WORKSPACE.')
|
| 66 |
+
|
| 67 |
+
@mock.patch('logging.error')
|
| 68 |
+
def test_validate_invalid_language(self, mock_error):
|
| 69 |
+
"""Tests that validate returns False if GITHUB_WORKSPACE isn't set."""
|
| 70 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 71 |
+
os.environ['WORKSPACE'] = '/workspace'
|
| 72 |
+
os.environ['LANGUAGE'] = 'invalid-language'
|
| 73 |
+
config = self._create_config()
|
| 74 |
+
self.assertFalse(config.validate())
|
| 75 |
+
mock_error.assert_called_with('Invalid LANGUAGE: %s. Must be one of: %s.',
|
| 76 |
+
os.environ['LANGUAGE'], constants.LANGUAGES)
|
| 77 |
+
|
| 78 |
+
@mock.patch('logging.error')
|
| 79 |
+
def test_validate_invalid_sanitizer(self, mock_error):
|
| 80 |
+
"""Tests that validate returns False if GITHUB_WORKSPACE isn't set."""
|
| 81 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 82 |
+
os.environ['WORKSPACE'] = '/workspace'
|
| 83 |
+
os.environ['SANITIZER'] = 'invalid-sanitizer'
|
| 84 |
+
config = self._create_config()
|
| 85 |
+
self.assertFalse(config.validate())
|
| 86 |
+
mock_error.assert_called_with('Invalid SANITIZER: %s. Must be one of: %s.',
|
| 87 |
+
os.environ['SANITIZER'],
|
| 88 |
+
config_utils.SANITIZERS)
|
| 89 |
+
|
| 90 |
+
def test_validate(self):
|
| 91 |
+
"""Tests that validate returns True if config is valid."""
|
| 92 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 93 |
+
os.environ['WORKSPACE'] = '/workspace'
|
| 94 |
+
os.environ['REPOSITORY'] = 'repo'
|
| 95 |
+
config = self._create_config()
|
| 96 |
+
self.assertTrue(config.validate())
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class BuildFuzzersConfigTest(unittest.TestCase):
|
| 100 |
+
"""Tests for BuildFuzzersConfig."""
|
| 101 |
+
|
| 102 |
+
def setUp(self):
|
| 103 |
+
test_helpers.patch_environ(self)
|
| 104 |
+
|
| 105 |
+
def _create_config(self):
|
| 106 |
+
return config_utils.BuildFuzzersConfig()
|
| 107 |
+
|
| 108 |
+
@mock.patch('platform_config.github._get_event_data', return_value={})
|
| 109 |
+
def test_github_base_ref(self, _):
|
| 110 |
+
"""Tests that base_ref is set properly."""
|
| 111 |
+
expected_base_ref = 'expected_base_ref'
|
| 112 |
+
os.environ['GITHUB_BASE_REF'] = expected_base_ref
|
| 113 |
+
os.environ['CFL_PLATFORM'] = 'github'
|
| 114 |
+
os.environ['GITHUB_REPOSITORY'] = 'owner/repo'
|
| 115 |
+
config = self._create_config()
|
| 116 |
+
self.assertEqual(config.base_ref, expected_base_ref)
|
| 117 |
+
|
| 118 |
+
def test_base_ref(self):
|
| 119 |
+
"""Tests that base_ref is set properly."""
|
| 120 |
+
expected_base_ref = 'expected_base_ref'
|
| 121 |
+
os.environ['GIT_BASE_REF'] = expected_base_ref
|
| 122 |
+
config = self._create_config()
|
| 123 |
+
self.assertEqual(config.base_ref, expected_base_ref)
|
| 124 |
+
|
| 125 |
+
def test_keep_unaffected_defaults_to_true(self):
|
| 126 |
+
"""Tests that keep_unaffected_fuzz_targets defaults to true."""
|
| 127 |
+
config = self._create_config()
|
| 128 |
+
self.assertTrue(config.keep_unaffected_fuzz_targets)
|
| 129 |
+
|
| 130 |
+
def test_keep_unaffected_defaults_to_false_when_pr(self):
|
| 131 |
+
"""Tests that keep_unaffected_fuzz_targets defaults to false when from a
|
| 132 |
+
pr."""
|
| 133 |
+
os.environ['GIT_BASE_REF'] = 'base-ref'
|
| 134 |
+
config = self._create_config()
|
| 135 |
+
self.assertFalse(config.keep_unaffected_fuzz_targets)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class RunFuzzersConfigTest(unittest.TestCase):
|
| 139 |
+
"""Tests for RunFuzzersConfig."""
|
| 140 |
+
|
| 141 |
+
def setUp(self):
|
| 142 |
+
test_helpers.patch_environ(self)
|
| 143 |
+
|
| 144 |
+
def _create_config(self):
|
| 145 |
+
return config_utils.RunFuzzersConfig()
|
| 146 |
+
|
| 147 |
+
def test_coverage(self):
|
| 148 |
+
"""Tests that mode is overriden properly based on is_coverage."""
|
| 149 |
+
# Test that it is overriden when it is supposed to be.
|
| 150 |
+
os.environ['SANITIZER'] = 'coverage'
|
| 151 |
+
os.environ['MODE'] = 'code-change'
|
| 152 |
+
config = self._create_config()
|
| 153 |
+
self.assertEqual(config.mode, 'coverage')
|
| 154 |
+
|
| 155 |
+
# Test that it isn't overriden when it isn't supposed to be.
|
| 156 |
+
os.environ['SANITIZER'] = 'address'
|
| 157 |
+
mode = 'code-change'
|
| 158 |
+
os.environ['MODE'] = mode
|
| 159 |
+
config = self._create_config()
|
| 160 |
+
self.assertEqual(config.mode, mode)
|
| 161 |
+
|
| 162 |
+
def test_run_config_validate(self):
|
| 163 |
+
"""Tests that _run_config_validate returns True when the config is valid."""
|
| 164 |
+
self.assertTrue(self._create_config()._run_config_validate())
|
| 165 |
+
|
| 166 |
+
@mock.patch('logging.error')
|
| 167 |
+
def test_run_config_invalid_mode(self, mock_error):
|
| 168 |
+
"""Tests that _run_config_validate returns False when mode is invalid."""
|
| 169 |
+
fake_mode = 'fake-mode'
|
| 170 |
+
os.environ['MODE'] = fake_mode
|
| 171 |
+
self.assertFalse(self._create_config()._run_config_validate())
|
| 172 |
+
mock_error.assert_called_with('Invalid MODE: %s. Must be one of %s.',
|
| 173 |
+
fake_mode,
|
| 174 |
+
config_utils.RunFuzzersConfig.MODES)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
class GetSanitizerTest(unittest.TestCase):
|
| 178 |
+
"""Tests for _get_sanitizer."""
|
| 179 |
+
|
| 180 |
+
def setUp(self):
|
| 181 |
+
test_helpers.patch_environ(self)
|
| 182 |
+
self.sanitizer = 'memory'
|
| 183 |
+
|
| 184 |
+
def test_default_value(self):
|
| 185 |
+
"""Tests that the default value returned by _get_sanitizer is correct."""
|
| 186 |
+
self.assertEqual(config_utils._get_sanitizer(), 'address')
|
| 187 |
+
|
| 188 |
+
def test_normal_case(self):
|
| 189 |
+
"""Tests that _get_sanitizer returns the correct value in normal cases."""
|
| 190 |
+
os.environ['SANITIZER'] = self.sanitizer
|
| 191 |
+
self.assertEqual(config_utils._get_sanitizer(), self.sanitizer)
|
| 192 |
+
|
| 193 |
+
def test_capitalization(self):
|
| 194 |
+
"""Tests that that _get_sanitizer handles capitalization properly."""
|
| 195 |
+
os.environ['SANITIZER'] = self.sanitizer.upper()
|
| 196 |
+
self.assertEqual(config_utils._get_sanitizer(), self.sanitizer)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
if __name__ == '__main__':
|
| 200 |
+
unittest.main()
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/continuous_integration_test.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for continuous_integration_module."""
|
| 15 |
+
import os
|
| 16 |
+
import sys
|
| 17 |
+
import unittest
|
| 18 |
+
from unittest import mock
|
| 19 |
+
|
| 20 |
+
import continuous_integration
|
| 21 |
+
import docker
|
| 22 |
+
|
| 23 |
+
# pylint: disable=wrong-import-position,import-error
|
| 24 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 25 |
+
|
| 26 |
+
import repo_manager
|
| 27 |
+
|
| 28 |
+
# pylint: disable=no-self-use
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class FixGitRepoForDiffTest(unittest.TestCase):
|
| 32 |
+
"""Tests for fix_git_repo_for_diff."""
|
| 33 |
+
|
| 34 |
+
@mock.patch('utils.execute')
|
| 35 |
+
def test_fix_git_repo_for_diff(self, mock_execute):
|
| 36 |
+
"""Tests that fix_git_repo_for_diff works as intended."""
|
| 37 |
+
repo_dir = '/dir'
|
| 38 |
+
repo_manager_obj = repo_manager.RepoManager(repo_dir)
|
| 39 |
+
continuous_integration.fix_git_repo_for_diff(repo_manager_obj)
|
| 40 |
+
expected_command = [
|
| 41 |
+
'git', 'symbolic-ref', 'refs/remotes/origin/HEAD',
|
| 42 |
+
'refs/remotes/origin/master'
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
mock_execute.assert_called_with(expected_command, location=repo_dir)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class GetBuildCommand(unittest.TestCase):
|
| 49 |
+
"""Tests for get_build_command."""
|
| 50 |
+
|
| 51 |
+
def test_build_command(self):
|
| 52 |
+
"""Tests that get_build_command works as intended."""
|
| 53 |
+
self.assertEqual(continuous_integration.get_build_command(), 'compile')
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class GetReplaceRepoAndBuildCommand(unittest.TestCase):
|
| 57 |
+
"""Tests for get_replace_repo_and_build_command."""
|
| 58 |
+
|
| 59 |
+
def test_get_replace_repo_and_build_command(self):
|
| 60 |
+
"""Tests that get_replace_repo_and_build_command works as intended."""
|
| 61 |
+
host_repo_path = '/path/on/host/to/repo'
|
| 62 |
+
image_repo_path = '/src/repo'
|
| 63 |
+
command = continuous_integration.get_replace_repo_and_build_command(
|
| 64 |
+
host_repo_path, image_repo_path)
|
| 65 |
+
expected_command = ('cd / && rm -rf /src/repo/* && '
|
| 66 |
+
'cp -r /path/on/host/to/repo /src && cd - '
|
| 67 |
+
'&& compile')
|
| 68 |
+
self.assertEqual(command, expected_command)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class BuildExternalProjetDockerImage(unittest.TestCase):
|
| 72 |
+
"""Tests for build_external_project_docker_image."""
|
| 73 |
+
|
| 74 |
+
@mock.patch('helper.docker_build')
|
| 75 |
+
def test_build_external_project_docker_image(self, mock_docker_build):
|
| 76 |
+
"""Tests that build_external_project_docker_image works as intended."""
|
| 77 |
+
build_integration_path = '.clusterfuzzlite'
|
| 78 |
+
project_src = '/path/to/project/src'
|
| 79 |
+
continuous_integration.build_external_project_docker_image(
|
| 80 |
+
project_src, build_integration_path)
|
| 81 |
+
|
| 82 |
+
mock_docker_build.assert_called_with([
|
| 83 |
+
'-t', docker.EXTERNAL_PROJECT_IMAGE, '-f',
|
| 84 |
+
os.path.join('.clusterfuzzlite', 'Dockerfile'), project_src
|
| 85 |
+
])
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# TODO(metzman): Write tests for the rest of continuous_integration.py.
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/docker_test.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests the functionality of the docker module."""
|
| 15 |
+
import unittest
|
| 16 |
+
from unittest import mock
|
| 17 |
+
|
| 18 |
+
import docker
|
| 19 |
+
import test_helpers
|
| 20 |
+
import workspace_utils
|
| 21 |
+
|
| 22 |
+
CONTAINER_NAME = 'example-container'
|
| 23 |
+
config = test_helpers.create_run_config(oss_fuzz_project_name='project',
|
| 24 |
+
workspace='/workspace')
|
| 25 |
+
config.workspace = '/workspace'
|
| 26 |
+
WORKSPACE = workspace_utils.Workspace(config)
|
| 27 |
+
SANITIZER = 'example-sanitizer'
|
| 28 |
+
LANGUAGE = 'example-language'
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class GetProjectImageTest(unittest.TestCase):
|
| 32 |
+
"""Tests for get_project_image."""
|
| 33 |
+
|
| 34 |
+
def test_get_project_image(self):
|
| 35 |
+
"""Tests that get_project_image_name works as intended."""
|
| 36 |
+
project = 'my-project'
|
| 37 |
+
self.assertEqual(docker.get_project_image_name(project),
|
| 38 |
+
'gcr.io/oss-fuzz/my-project')
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class GetDeleteImagesTest(unittest.TestCase):
|
| 42 |
+
"""Tests for delete_images."""
|
| 43 |
+
|
| 44 |
+
@mock.patch('utils.execute')
|
| 45 |
+
def test_delete_images(self, mock_execute): # pylint: disable=no-self-use
|
| 46 |
+
"""Tests that get_project_image_name works as intended."""
|
| 47 |
+
images = ['image']
|
| 48 |
+
docker.delete_images(images)
|
| 49 |
+
expected_calls = [
|
| 50 |
+
mock.call(['docker', 'rmi', '-f'] + images),
|
| 51 |
+
mock.call(['docker', 'builder', 'prune', '-f'])
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
mock_execute.assert_has_calls(expected_calls)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class GetBaseDockerRunArgsTest(unittest.TestCase):
|
| 58 |
+
"""Tests get_base_docker_run_args."""
|
| 59 |
+
|
| 60 |
+
@mock.patch('utils.get_container_name', return_value=CONTAINER_NAME)
|
| 61 |
+
def test_get_base_docker_run_args_container(self, _):
|
| 62 |
+
"""Tests that get_base_docker_run_args works as intended when inside a
|
| 63 |
+
container."""
|
| 64 |
+
docker_args, docker_container = docker.get_base_docker_run_args(
|
| 65 |
+
WORKSPACE, SANITIZER, LANGUAGE)
|
| 66 |
+
self.assertEqual(docker_container, CONTAINER_NAME)
|
| 67 |
+
expected_docker_args = []
|
| 68 |
+
expected_docker_args = [
|
| 69 |
+
'-e',
|
| 70 |
+
'FUZZING_ENGINE=libfuzzer',
|
| 71 |
+
'-e',
|
| 72 |
+
'CIFUZZ=True',
|
| 73 |
+
'-e',
|
| 74 |
+
f'SANITIZER={SANITIZER}',
|
| 75 |
+
'-e',
|
| 76 |
+
'ARCHITECTURE=x86_64',
|
| 77 |
+
'-e',
|
| 78 |
+
f'FUZZING_LANGUAGE={LANGUAGE}',
|
| 79 |
+
'-e',
|
| 80 |
+
f'OUT={WORKSPACE.out}',
|
| 81 |
+
'--volumes-from',
|
| 82 |
+
CONTAINER_NAME,
|
| 83 |
+
]
|
| 84 |
+
self.assertEqual(docker_args, expected_docker_args)
|
| 85 |
+
|
| 86 |
+
@mock.patch('utils.get_container_name', return_value=None)
|
| 87 |
+
def test_get_base_docker_run_args_no_container(self, _):
|
| 88 |
+
"""Tests that get_base_docker_run_args works as intended when not inside a
|
| 89 |
+
container."""
|
| 90 |
+
docker_args, docker_container = docker.get_base_docker_run_args(
|
| 91 |
+
WORKSPACE, SANITIZER, LANGUAGE)
|
| 92 |
+
self.assertEqual(docker_container, None)
|
| 93 |
+
expected_docker_args = [
|
| 94 |
+
'-e', 'FUZZING_ENGINE=libfuzzer', '-e', 'CIFUZZ=True', '-e',
|
| 95 |
+
f'SANITIZER={SANITIZER}', '-e', 'ARCHITECTURE=x86_64', '-e',
|
| 96 |
+
f'FUZZING_LANGUAGE={LANGUAGE}', '-e', f'OUT={WORKSPACE.out}', '-v',
|
| 97 |
+
f'{WORKSPACE.workspace}:{WORKSPACE.workspace}'
|
| 98 |
+
]
|
| 99 |
+
self.assertEqual(docker_args, expected_docker_args)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class GetBaseDockerRunCommandTest(unittest.TestCase):
|
| 103 |
+
"""Tests get_base_docker_run_args."""
|
| 104 |
+
|
| 105 |
+
@mock.patch('utils.get_container_name', return_value=None)
|
| 106 |
+
def test_get_base_docker_run_command_no_container(self, _):
|
| 107 |
+
"""Tests that get_base_docker_run_args works as intended when not inside a
|
| 108 |
+
container."""
|
| 109 |
+
docker_args, docker_container = docker.get_base_docker_run_command(
|
| 110 |
+
WORKSPACE, SANITIZER, LANGUAGE)
|
| 111 |
+
self.assertEqual(docker_container, None)
|
| 112 |
+
expected_docker_command = [
|
| 113 |
+
'docker', 'run', '--rm', '--privileged', '-e',
|
| 114 |
+
'FUZZING_ENGINE=libfuzzer', '-e', 'CIFUZZ=True', '-e',
|
| 115 |
+
f'SANITIZER={SANITIZER}', '-e', 'ARCHITECTURE=x86_64', '-e',
|
| 116 |
+
f'FUZZING_LANGUAGE={LANGUAGE}', '-e', f'OUT={WORKSPACE.out}', '-v',
|
| 117 |
+
f'{WORKSPACE.workspace}:{WORKSPACE.workspace}'
|
| 118 |
+
]
|
| 119 |
+
self.assertEqual(docker_args, expected_docker_command)
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/generate_coverage_report_test.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for generate_coverage_report."""
|
| 15 |
+
|
| 16 |
+
import unittest
|
| 17 |
+
from unittest import mock
|
| 18 |
+
|
| 19 |
+
import generate_coverage_report
|
| 20 |
+
import test_helpers
|
| 21 |
+
|
| 22 |
+
OUT_DIR = '/outdir'
|
| 23 |
+
PROJECT = 'example-project'
|
| 24 |
+
SANITIZER = 'coverage'
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TestRunCoverageCommand(unittest.TestCase):
|
| 28 |
+
"""Tests run_coverage_command"""
|
| 29 |
+
|
| 30 |
+
def setUp(self):
|
| 31 |
+
test_helpers.patch_environ(self, empty=True)
|
| 32 |
+
|
| 33 |
+
@mock.patch('utils.execute')
|
| 34 |
+
def test_run_coverage_command(self, mock_execute): # pylint: disable=no-self-use
|
| 35 |
+
"""Tests that run_coverage_command works as intended."""
|
| 36 |
+
config = test_helpers.create_run_config(oss_fuzz_project_name=PROJECT,
|
| 37 |
+
sanitizer=SANITIZER)
|
| 38 |
+
workspace = test_helpers.create_workspace()
|
| 39 |
+
generate_coverage_report.run_coverage_command(config, workspace)
|
| 40 |
+
expected_command = 'coverage'
|
| 41 |
+
expected_env = {
|
| 42 |
+
'SANITIZER': config.sanitizer,
|
| 43 |
+
'FUZZING_LANGUAGE': config.language,
|
| 44 |
+
'OUT': workspace.out,
|
| 45 |
+
'CIFUZZ': 'True',
|
| 46 |
+
'FUZZING_ENGINE': 'libfuzzer',
|
| 47 |
+
'ARCHITECTURE': 'x86_64',
|
| 48 |
+
'FUZZER_ARGS': '-rss_limit_mb=2560 -timeout=25',
|
| 49 |
+
'HTTP_PORT': '',
|
| 50 |
+
'COVERAGE_EXTRA_ARGS': '',
|
| 51 |
+
'CORPUS_DIR': workspace.corpora,
|
| 52 |
+
'COVERAGE_OUTPUT_DIR': workspace.coverage_report
|
| 53 |
+
}
|
| 54 |
+
mock_execute.assert_called_with(expected_command, env=expected_env)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class DownloadCorporaTest(unittest.TestCase):
|
| 58 |
+
"""Tests for download_corpora."""
|
| 59 |
+
|
| 60 |
+
def test_download_corpora(self): # pylint: disable=no-self-use
|
| 61 |
+
"""Tests that download_corpora works as intended."""
|
| 62 |
+
clusterfuzz_deployment = mock.Mock()
|
| 63 |
+
clusterfuzz_deployment.workspace = test_helpers.create_workspace()
|
| 64 |
+
fuzz_target_paths = ['/path/to/fuzzer1', '/path/to/fuzzer2']
|
| 65 |
+
expected_calls = [
|
| 66 |
+
mock.call('fuzzer1', '/workspace/cifuzz-corpus/fuzzer1'),
|
| 67 |
+
mock.call('fuzzer2', '/workspace/cifuzz-corpus/fuzzer2')
|
| 68 |
+
]
|
| 69 |
+
generate_coverage_report.download_corpora(fuzz_target_paths,
|
| 70 |
+
clusterfuzz_deployment)
|
| 71 |
+
clusterfuzz_deployment.download_corpus.assert_has_calls(expected_calls)
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/http_utils.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Utility module for HTTP."""
|
| 15 |
+
import json
|
| 16 |
+
import logging
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
import tempfile
|
| 20 |
+
import zipfile
|
| 21 |
+
|
| 22 |
+
import requests
|
| 23 |
+
|
| 24 |
+
# pylint: disable=wrong-import-position,import-error
|
| 25 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 26 |
+
import retry
|
| 27 |
+
|
| 28 |
+
_DOWNLOAD_URL_RETRIES = 3
|
| 29 |
+
_DOWNLOAD_URL_BACKOFF = 1
|
| 30 |
+
_HTTP_REQUEST_TIMEOUT = 10
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def download_and_unpack_zip(url, extract_directory, headers=None):
|
| 34 |
+
"""Downloads and unpacks a zip file from an HTTP URL.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
url: A url to the zip file to be downloaded and unpacked.
|
| 38 |
+
extract_directory: The path where the zip file should be extracted to.
|
| 39 |
+
headers: (Optional) HTTP headers to send with the download request.
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
True on success.
|
| 43 |
+
"""
|
| 44 |
+
if headers is None:
|
| 45 |
+
headers = {}
|
| 46 |
+
|
| 47 |
+
if not os.path.exists(extract_directory):
|
| 48 |
+
logging.error('Extract directory: %s does not exist.', extract_directory)
|
| 49 |
+
return False
|
| 50 |
+
|
| 51 |
+
# Gives the temporary zip file a unique identifier in the case that
|
| 52 |
+
# that download_and_unpack_zip is done in parallel.
|
| 53 |
+
with tempfile.NamedTemporaryFile(suffix='.zip') as tmp_file:
|
| 54 |
+
if not download_url(url, tmp_file.name, headers=headers):
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
with zipfile.ZipFile(tmp_file.name, 'r') as zip_file:
|
| 59 |
+
zip_file.extractall(extract_directory)
|
| 60 |
+
except zipfile.BadZipFile:
|
| 61 |
+
logging.error('Error unpacking zip from %s. Bad Zipfile.', url)
|
| 62 |
+
return False
|
| 63 |
+
|
| 64 |
+
return True
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def download_url(*args, **kwargs):
|
| 68 |
+
"""Wrapper around _download_url that returns False if _download_url
|
| 69 |
+
exceptions."""
|
| 70 |
+
try:
|
| 71 |
+
return _download_url(*args, **kwargs)
|
| 72 |
+
except Exception: # pylint: disable=broad-except
|
| 73 |
+
return False
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def get_json_from_url(url):
|
| 77 |
+
"""Gets a json object from a specified HTTP URL.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
url: The url of the json to be downloaded.
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
A dictionary deserialized from JSON or None on failure.
|
| 84 |
+
"""
|
| 85 |
+
try:
|
| 86 |
+
return requests.get(url, timeout=_HTTP_REQUEST_TIMEOUT).json()
|
| 87 |
+
except (ValueError, TypeError, json.JSONDecodeError,
|
| 88 |
+
requests.exceptions.ReadTimeout) as err:
|
| 89 |
+
logging.error('Loading json from url %s failed with: %s.', url, str(err))
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@retry.wrap(_DOWNLOAD_URL_RETRIES, _DOWNLOAD_URL_BACKOFF)
|
| 94 |
+
def _download_url(url, filename, headers=None):
|
| 95 |
+
"""Downloads the file located at |url|, using HTTP to |filename|.
|
| 96 |
+
|
| 97 |
+
Args:
|
| 98 |
+
url: A url to a file to download.
|
| 99 |
+
filename: The path the file should be downloaded to.
|
| 100 |
+
headers: (Optional) HTTP headers to send with the download request.
|
| 101 |
+
|
| 102 |
+
Returns:
|
| 103 |
+
True on success.
|
| 104 |
+
"""
|
| 105 |
+
if headers is None:
|
| 106 |
+
headers = {}
|
| 107 |
+
|
| 108 |
+
response = requests.get(url, headers=headers)
|
| 109 |
+
|
| 110 |
+
if response.status_code != 200:
|
| 111 |
+
logging.error('Unable to download from: %s. Code: %d. Content: %s.', url,
|
| 112 |
+
response.status_code, response.content)
|
| 113 |
+
return False
|
| 114 |
+
|
| 115 |
+
with open(filename, 'wb') as file_handle:
|
| 116 |
+
file_handle.write(response.content)
|
| 117 |
+
|
| 118 |
+
return True
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
clusterfuzz==2.5.9
|
| 2 |
+
requests==2.28.0
|
| 3 |
+
protobuf==3.20.2
|
| 4 |
+
gsutil==5.20
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/run_cifuzz.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Script for running CIFuzz end-to-end. This is meant to work outside any
|
| 15 |
+
docker image. This cannot depend on any CIFuzz code or third party packages."""
|
| 16 |
+
import os
|
| 17 |
+
import subprocess
|
| 18 |
+
import sys
|
| 19 |
+
import tempfile
|
| 20 |
+
import logging
|
| 21 |
+
|
| 22 |
+
INFRA_DIR = os.path.dirname(os.path.dirname(__file__))
|
| 23 |
+
DEFAULT_ENVS = [('DRY_RUN', '0'), ('SANITIZER', 'address')]
|
| 24 |
+
BASE_CIFUZZ_DOCKER_TAG = 'ghcr.io/aixcc-finals'
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def set_default_env_var_if_unset(env_var, default_value):
|
| 28 |
+
"""Sets the value of |env_var| in the environment to |default_value| if it was
|
| 29 |
+
not already set."""
|
| 30 |
+
if env_var not in os.environ:
|
| 31 |
+
os.environ[env_var] = default_value
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def docker_run(name, workspace, project_src_path):
|
| 35 |
+
"""Runs a CIFuzz docker container with |name|."""
|
| 36 |
+
command = [
|
| 37 |
+
'docker', 'run', '--name', name, '--rm', '-e', 'PROJECT_SRC_PATH', '-e',
|
| 38 |
+
'OSS_FUZZ_PROJECT_NAME', '-e', 'WORKSPACE', '-e', 'REPOSITORY', '-e',
|
| 39 |
+
'DRY_RUN', '-e', 'CI', '-e', 'SANITIZER', '-e', 'GIT_SHA', '-e',
|
| 40 |
+
'FILESTORE', '-e', 'NO_CLUSTERFUZZ_DEPLOYMENT'
|
| 41 |
+
]
|
| 42 |
+
if project_src_path:
|
| 43 |
+
command += ['-v', f'{project_src_path}:{project_src_path}']
|
| 44 |
+
command += [
|
| 45 |
+
'-v', '/var/run/docker.sock:/var/run/docker.sock', '-v',
|
| 46 |
+
f'{workspace}:{workspace}', f'{BASE_CIFUZZ_DOCKER_TAG}/{name}'
|
| 47 |
+
]
|
| 48 |
+
print('Running docker command:', command)
|
| 49 |
+
subprocess.run(command, check=True)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def docker_build(image):
|
| 53 |
+
"""Builds the CIFuzz |image|. Only suitable for building CIFuzz images."""
|
| 54 |
+
command = [
|
| 55 |
+
'docker', 'build', '-t', f'{BASE_CIFUZZ_DOCKER_TAG}/{image}', '--file',
|
| 56 |
+
f'{image}.Dockerfile', '.'
|
| 57 |
+
]
|
| 58 |
+
subprocess.run(command, check=True, cwd=INFRA_DIR)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def main():
|
| 62 |
+
"""Builds and runs fuzzers using CIFuzz."""
|
| 63 |
+
for env_var, default_value in DEFAULT_ENVS:
|
| 64 |
+
set_default_env_var_if_unset(env_var, default_value)
|
| 65 |
+
|
| 66 |
+
repository = os.getenv('REPOSITORY')
|
| 67 |
+
assert repository
|
| 68 |
+
|
| 69 |
+
project_src_path = os.getenv('PROJECT_SRC_PATH')
|
| 70 |
+
|
| 71 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 72 |
+
if 'WORKSPACE' not in os.environ:
|
| 73 |
+
os.environ['WORKSPACE'] = temp_dir
|
| 74 |
+
|
| 75 |
+
workspace = os.environ['WORKSPACE']
|
| 76 |
+
|
| 77 |
+
docker_build('build_fuzzers')
|
| 78 |
+
docker_run('build_fuzzers', workspace, project_src_path)
|
| 79 |
+
docker_build('run_fuzzers')
|
| 80 |
+
try:
|
| 81 |
+
docker_run('run_fuzzers', workspace, project_src_path)
|
| 82 |
+
except subprocess.CalledProcessError:
|
| 83 |
+
logging.error('run_fuzzers failed.')
|
| 84 |
+
return 1
|
| 85 |
+
return 0
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == '__main__':
|
| 89 |
+
sys.exit(main())
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/run_fuzzers.py
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for running fuzzers."""
|
| 15 |
+
import enum
|
| 16 |
+
import logging
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
import time
|
| 20 |
+
|
| 21 |
+
import clusterfuzz_deployment
|
| 22 |
+
import fuzz_target
|
| 23 |
+
import generate_coverage_report
|
| 24 |
+
import workspace_utils
|
| 25 |
+
import sarif_utils
|
| 26 |
+
|
| 27 |
+
# pylint: disable=wrong-import-position,import-error
|
| 28 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 29 |
+
|
| 30 |
+
import utils
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class RunFuzzersResult(enum.Enum):
|
| 34 |
+
"""Enum result from running fuzzers."""
|
| 35 |
+
ERROR = 0
|
| 36 |
+
BUG_FOUND = 1
|
| 37 |
+
NO_BUG_FOUND = 2
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class BaseFuzzTargetRunner:
|
| 41 |
+
"""Base class for fuzzer runners."""
|
| 42 |
+
|
| 43 |
+
def __init__(self, config):
|
| 44 |
+
self.config = config
|
| 45 |
+
self.workspace = workspace_utils.Workspace(config)
|
| 46 |
+
self.clusterfuzz_deployment = (
|
| 47 |
+
clusterfuzz_deployment.get_clusterfuzz_deployment(
|
| 48 |
+
self.config, self.workspace))
|
| 49 |
+
|
| 50 |
+
# Set by the initialize method.
|
| 51 |
+
self.fuzz_target_paths = None
|
| 52 |
+
|
| 53 |
+
def get_fuzz_targets(self):
|
| 54 |
+
"""Returns fuzz targets in out directory."""
|
| 55 |
+
return utils.get_fuzz_targets(self.workspace.out)
|
| 56 |
+
|
| 57 |
+
def initialize(self):
|
| 58 |
+
"""Initialization method. Must be called before calling run_fuzz_targets.
|
| 59 |
+
Returns True on success."""
|
| 60 |
+
# Use a separate initialization function so we can return False on failure
|
| 61 |
+
# instead of exceptioning like we need to do if this were done in the
|
| 62 |
+
# __init__ method.
|
| 63 |
+
|
| 64 |
+
logging.info('Using %s sanitizer.', self.config.sanitizer)
|
| 65 |
+
|
| 66 |
+
# TODO(metzman) Add a check to ensure we aren't over time limit.
|
| 67 |
+
if not self.config.fuzz_seconds or self.config.fuzz_seconds < 1:
|
| 68 |
+
logging.error(
|
| 69 |
+
'Fuzz_seconds argument must be greater than 1, but was: %s.',
|
| 70 |
+
self.config.fuzz_seconds)
|
| 71 |
+
return False
|
| 72 |
+
|
| 73 |
+
if not os.path.exists(self.workspace.out):
|
| 74 |
+
logging.error('Out directory: %s does not exist.', self.workspace.out)
|
| 75 |
+
return False
|
| 76 |
+
|
| 77 |
+
if not os.path.exists(self.workspace.artifacts):
|
| 78 |
+
os.makedirs(self.workspace.artifacts)
|
| 79 |
+
elif (not os.path.isdir(self.workspace.artifacts) or
|
| 80 |
+
os.listdir(self.workspace.artifacts)):
|
| 81 |
+
logging.error('Artifacts path: %s exists and is not an empty directory.',
|
| 82 |
+
self.workspace.artifacts)
|
| 83 |
+
return False
|
| 84 |
+
|
| 85 |
+
self.fuzz_target_paths = self.get_fuzz_targets()
|
| 86 |
+
logging.info('Fuzz targets: %s', self.fuzz_target_paths)
|
| 87 |
+
if not self.fuzz_target_paths:
|
| 88 |
+
logging.error('No fuzz targets were found in out directory: %s.',
|
| 89 |
+
self.workspace.out)
|
| 90 |
+
return False
|
| 91 |
+
|
| 92 |
+
return True
|
| 93 |
+
|
| 94 |
+
def cleanup_after_fuzz_target_run(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 95 |
+
"""Cleans up after running |fuzz_target_obj|."""
|
| 96 |
+
raise NotImplementedError('Child class must implement method.')
|
| 97 |
+
|
| 98 |
+
def run_fuzz_target(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 99 |
+
"""Fuzzes with |fuzz_target_obj| and returns the result."""
|
| 100 |
+
raise NotImplementedError('Child class must implement method.')
|
| 101 |
+
|
| 102 |
+
@property
|
| 103 |
+
def quit_on_bug_found(self):
|
| 104 |
+
"""Property that is checked to determine if fuzzing should quit after first
|
| 105 |
+
bug is found."""
|
| 106 |
+
raise NotImplementedError('Child class must implement method.')
|
| 107 |
+
|
| 108 |
+
def create_fuzz_target_obj(self, target_path, run_seconds):
|
| 109 |
+
"""Returns a fuzz target object."""
|
| 110 |
+
return fuzz_target.FuzzTarget(target_path, run_seconds, self.workspace,
|
| 111 |
+
self.clusterfuzz_deployment, self.config)
|
| 112 |
+
|
| 113 |
+
def run_fuzz_targets(self):
|
| 114 |
+
"""Runs fuzz targets. Returns True if a bug was found."""
|
| 115 |
+
fuzzers_left_to_run = len(self.fuzz_target_paths)
|
| 116 |
+
|
| 117 |
+
# Make a copy since we will mutate it.
|
| 118 |
+
fuzz_seconds = self.config.fuzz_seconds
|
| 119 |
+
|
| 120 |
+
min_seconds_per_fuzzer = fuzz_seconds // fuzzers_left_to_run
|
| 121 |
+
bug_found = False
|
| 122 |
+
for target_path in self.fuzz_target_paths:
|
| 123 |
+
# By doing this, we can ensure that every fuzz target runs for at least
|
| 124 |
+
# min_seconds_per_fuzzer, but that other fuzzers will have longer to run
|
| 125 |
+
# if one ends early.
|
| 126 |
+
run_seconds = max(fuzz_seconds // fuzzers_left_to_run,
|
| 127 |
+
min_seconds_per_fuzzer)
|
| 128 |
+
|
| 129 |
+
target = self.create_fuzz_target_obj(target_path, run_seconds)
|
| 130 |
+
start_time = time.time()
|
| 131 |
+
result = self.run_fuzz_target(target)
|
| 132 |
+
self.cleanup_after_fuzz_target_run(target)
|
| 133 |
+
|
| 134 |
+
# It's OK if this goes negative since we take max when determining
|
| 135 |
+
# run_seconds.
|
| 136 |
+
fuzz_seconds -= time.time() - start_time
|
| 137 |
+
|
| 138 |
+
fuzzers_left_to_run -= 1
|
| 139 |
+
if not result.testcase or not result.stacktrace:
|
| 140 |
+
logging.info('Fuzzer %s finished running without reportable crashes.',
|
| 141 |
+
target.target_name)
|
| 142 |
+
continue
|
| 143 |
+
|
| 144 |
+
bug_found = True
|
| 145 |
+
if self.quit_on_bug_found:
|
| 146 |
+
logging.info('Bug found. Stopping fuzzing.')
|
| 147 |
+
break
|
| 148 |
+
|
| 149 |
+
# pylint: disable=undefined-loop-variable
|
| 150 |
+
if not target_path:
|
| 151 |
+
logging.error('Ran no fuzz targets.')
|
| 152 |
+
elif self.config.output_sarif:
|
| 153 |
+
# TODO(metzman): Handle multiple crashes.
|
| 154 |
+
write_fuzz_result_to_sarif(result, target_path, self.workspace)
|
| 155 |
+
self.clusterfuzz_deployment.upload_crashes()
|
| 156 |
+
return bug_found
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def write_fuzz_result_to_sarif(fuzz_result, target_path, workspace):
|
| 160 |
+
"""Write results of fuzzing to SARIF."""
|
| 161 |
+
logging.info('Writing sarif results.')
|
| 162 |
+
sarif_utils.write_stacktrace_to_sarif(fuzz_result.stacktrace, target_path,
|
| 163 |
+
workspace)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
class PruneTargetRunner(BaseFuzzTargetRunner):
|
| 167 |
+
"""Runner that prunes corpora."""
|
| 168 |
+
|
| 169 |
+
@property
|
| 170 |
+
def quit_on_bug_found(self):
|
| 171 |
+
return False
|
| 172 |
+
|
| 173 |
+
def run_fuzz_target(self, fuzz_target_obj):
|
| 174 |
+
"""Prunes with |fuzz_target_obj| and returns the result."""
|
| 175 |
+
result = fuzz_target_obj.prune()
|
| 176 |
+
logging.debug('Corpus path contents: %s.', os.listdir(result.corpus_path))
|
| 177 |
+
self.clusterfuzz_deployment.upload_corpus(fuzz_target_obj.target_name,
|
| 178 |
+
result.corpus_path,
|
| 179 |
+
replace=True)
|
| 180 |
+
return result
|
| 181 |
+
|
| 182 |
+
def cleanup_after_fuzz_target_run(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 183 |
+
"""Cleans up after pruning with |fuzz_target_obj|."""
|
| 184 |
+
fuzz_target_obj.free_disk_if_needed()
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
NON_FUZZ_TARGETS_FOR_COVERAGE = {
|
| 188 |
+
'llvm-symbolizer',
|
| 189 |
+
'jazzer_agent_deploy.jar',
|
| 190 |
+
'jazzer_driver',
|
| 191 |
+
'jazzer_driver_with_sanitizer',
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def is_coverage_fuzz_target(file_path):
|
| 196 |
+
"""Returns whether |file_path| is a fuzz target binary for the purposes of a
|
| 197 |
+
coverage report. Inspired by infra/base-images/base-runner/coverage."""
|
| 198 |
+
if not os.path.isfile(file_path):
|
| 199 |
+
return False
|
| 200 |
+
if not utils.is_executable(file_path):
|
| 201 |
+
return False
|
| 202 |
+
filename = os.path.basename(file_path)
|
| 203 |
+
return filename not in NON_FUZZ_TARGETS_FOR_COVERAGE
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def get_coverage_fuzz_targets(out):
|
| 207 |
+
"""Returns a list of fuzz targets in |out| for coverage."""
|
| 208 |
+
# We only want fuzz targets from the root because during the coverage build,
|
| 209 |
+
# a lot of the image's filesystem is copied into /out for the purpose of
|
| 210 |
+
# generating coverage reports.
|
| 211 |
+
fuzz_targets = []
|
| 212 |
+
for filename in os.listdir(out):
|
| 213 |
+
file_path = os.path.join(out, filename)
|
| 214 |
+
if is_coverage_fuzz_target(file_path):
|
| 215 |
+
fuzz_targets.append(file_path)
|
| 216 |
+
return fuzz_targets
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
class CoverageTargetRunner(BaseFuzzTargetRunner):
|
| 220 |
+
"""Runner that runs the 'coverage' command."""
|
| 221 |
+
|
| 222 |
+
@property
|
| 223 |
+
def quit_on_bug_found(self):
|
| 224 |
+
raise NotImplementedError('Not implemented for CoverageTargetRunner.')
|
| 225 |
+
|
| 226 |
+
def get_fuzz_targets(self):
|
| 227 |
+
"""Returns fuzz targets in out directory."""
|
| 228 |
+
return get_coverage_fuzz_targets(self.workspace.out)
|
| 229 |
+
|
| 230 |
+
def run_fuzz_targets(self):
|
| 231 |
+
"""Generates a coverage report. Always returns False since it never finds
|
| 232 |
+
any bugs."""
|
| 233 |
+
generate_coverage_report.generate_coverage_report(
|
| 234 |
+
self.fuzz_target_paths, self.workspace, self.clusterfuzz_deployment,
|
| 235 |
+
self.config)
|
| 236 |
+
return False
|
| 237 |
+
|
| 238 |
+
def run_fuzz_target(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 239 |
+
"""Fuzzes with |fuzz_target_obj| and returns the result."""
|
| 240 |
+
raise NotImplementedError('Not implemented for CoverageTargetRunner.')
|
| 241 |
+
|
| 242 |
+
def cleanup_after_fuzz_target_run(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 243 |
+
"""Cleans up after running |fuzz_target_obj|."""
|
| 244 |
+
raise NotImplementedError('Not implemented for CoverageTargetRunner.')
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
class CiFuzzTargetRunner(BaseFuzzTargetRunner):
|
| 248 |
+
"""Runner for fuzz targets used in CI (patch-fuzzing) context."""
|
| 249 |
+
|
| 250 |
+
@property
|
| 251 |
+
def quit_on_bug_found(self):
|
| 252 |
+
return True
|
| 253 |
+
|
| 254 |
+
def cleanup_after_fuzz_target_run(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 255 |
+
"""Cleans up after running |fuzz_target_obj|."""
|
| 256 |
+
fuzz_target_obj.free_disk_if_needed()
|
| 257 |
+
|
| 258 |
+
def run_fuzz_target(self, fuzz_target_obj): # pylint: disable=no-self-use
|
| 259 |
+
return fuzz_target_obj.fuzz()
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
class BatchFuzzTargetRunner(BaseFuzzTargetRunner):
|
| 263 |
+
"""Runner for fuzz targets used in batch fuzzing context."""
|
| 264 |
+
|
| 265 |
+
@property
|
| 266 |
+
def quit_on_bug_found(self):
|
| 267 |
+
return False
|
| 268 |
+
|
| 269 |
+
def run_fuzz_target(self, fuzz_target_obj):
|
| 270 |
+
"""Fuzzes with |fuzz_target_obj| and returns the result."""
|
| 271 |
+
result = fuzz_target_obj.fuzz(batch=True)
|
| 272 |
+
logging.debug('Corpus path contents: %s.', os.listdir(result.corpus_path))
|
| 273 |
+
self.clusterfuzz_deployment.upload_corpus(fuzz_target_obj.target_name,
|
| 274 |
+
result.corpus_path)
|
| 275 |
+
return result
|
| 276 |
+
|
| 277 |
+
def cleanup_after_fuzz_target_run(self, fuzz_target_obj):
|
| 278 |
+
"""Cleans up after running |fuzz_target_obj|."""
|
| 279 |
+
# This must be done after we upload the corpus, otherwise it will be deleted
|
| 280 |
+
# before we get a chance to upload it. We can't delete the fuzz target
|
| 281 |
+
# because it is needed when we upload the build.
|
| 282 |
+
fuzz_target_obj.free_disk_if_needed(delete_fuzz_target=False)
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
_MODE_RUNNER_MAPPING = {
|
| 286 |
+
'batch': BatchFuzzTargetRunner,
|
| 287 |
+
'coverage': CoverageTargetRunner,
|
| 288 |
+
'prune': PruneTargetRunner,
|
| 289 |
+
'code-change': CiFuzzTargetRunner,
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def get_fuzz_target_runner(config):
|
| 294 |
+
"""Returns a fuzz target runner object based on the mode of
|
| 295 |
+
|config|."""
|
| 296 |
+
runner = _MODE_RUNNER_MAPPING[config.mode](config)
|
| 297 |
+
logging.info('run fuzzers MODE is: %s. Runner: %s.', config.mode, runner)
|
| 298 |
+
return runner
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def run_fuzzers(config): # pylint: disable=too-many-locals
|
| 302 |
+
"""Runs fuzzers for a specific OSS-Fuzz project.
|
| 303 |
+
|
| 304 |
+
Args:
|
| 305 |
+
config: A RunFuzzTargetsConfig.
|
| 306 |
+
|
| 307 |
+
Returns:
|
| 308 |
+
A RunFuzzersResult enum value indicating what happened during fuzzing.
|
| 309 |
+
"""
|
| 310 |
+
fuzz_target_runner = get_fuzz_target_runner(config)
|
| 311 |
+
if not fuzz_target_runner.initialize():
|
| 312 |
+
# We didn't fuzz at all because of internal (CIFuzz) errors. And we didn't
|
| 313 |
+
# find any bugs.
|
| 314 |
+
return RunFuzzersResult.ERROR
|
| 315 |
+
|
| 316 |
+
if not fuzz_target_runner.run_fuzz_targets():
|
| 317 |
+
# We fuzzed successfully, but didn't find any bugs (in the fuzz target).
|
| 318 |
+
return RunFuzzersResult.NO_BUG_FOUND
|
| 319 |
+
|
| 320 |
+
# We fuzzed successfully and found bug(s) in the fuzz targets.
|
| 321 |
+
return RunFuzzersResult.BUG_FOUND
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/run_fuzzers_entrypoint.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Runs a specific OSS-Fuzz project's fuzzers for CI tools."""
|
| 15 |
+
import logging
|
| 16 |
+
import sys
|
| 17 |
+
|
| 18 |
+
import config_utils
|
| 19 |
+
import docker
|
| 20 |
+
import logs
|
| 21 |
+
import run_fuzzers
|
| 22 |
+
|
| 23 |
+
# pylint: disable=c-extension-no-member
|
| 24 |
+
# pylint gets confused because of the relative import of cifuzz.
|
| 25 |
+
|
| 26 |
+
logs.init()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def delete_unneeded_docker_images(config):
|
| 30 |
+
"""Deletes unneeded docker images if running in an environment with low
|
| 31 |
+
disk space."""
|
| 32 |
+
if not config.low_disk_space:
|
| 33 |
+
return
|
| 34 |
+
logging.info('Deleting builder docker images to save disk space.')
|
| 35 |
+
project_image = docker.get_project_image_name(config.oss_fuzz_project_name)
|
| 36 |
+
images = [
|
| 37 |
+
project_image,
|
| 38 |
+
docker.BASE_BUILDER_TAG,
|
| 39 |
+
docker.BASE_BUILDER_TAG + '-go',
|
| 40 |
+
docker.BASE_BUILDER_TAG + '-javascript',
|
| 41 |
+
docker.BASE_BUILDER_TAG + '-jvm',
|
| 42 |
+
docker.BASE_BUILDER_TAG + '-python',
|
| 43 |
+
docker.BASE_BUILDER_TAG + '-rust',
|
| 44 |
+
docker.BASE_BUILDER_TAG + '-ruby',
|
| 45 |
+
docker.BASE_BUILDER_TAG + '-swift',
|
| 46 |
+
]
|
| 47 |
+
docker.delete_images(images)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def run_fuzzers_entrypoint():
|
| 51 |
+
"""This is the entrypoint for the run_fuzzers github action.
|
| 52 |
+
This action can be added to any OSS-Fuzz project's workflow that uses
|
| 53 |
+
Github."""
|
| 54 |
+
config = config_utils.RunFuzzersConfig()
|
| 55 |
+
# The default return code when an error occurs.
|
| 56 |
+
returncode = 1
|
| 57 |
+
if config.dry_run:
|
| 58 |
+
# Sets the default return code on error to success.
|
| 59 |
+
returncode = 0
|
| 60 |
+
|
| 61 |
+
delete_unneeded_docker_images(config)
|
| 62 |
+
# Run the specified project's fuzzers from the build.
|
| 63 |
+
result = run_fuzzers.run_fuzzers(config)
|
| 64 |
+
if result == run_fuzzers.RunFuzzersResult.ERROR:
|
| 65 |
+
logging.error('Error occurred while running in workspace %s.',
|
| 66 |
+
config.workspace)
|
| 67 |
+
return returncode
|
| 68 |
+
if result == run_fuzzers.RunFuzzersResult.BUG_FOUND:
|
| 69 |
+
logging.info('Bug found.')
|
| 70 |
+
if not config.dry_run:
|
| 71 |
+
# Return 2 when a bug was found by a fuzzer causing the CI to fail.
|
| 72 |
+
return 2
|
| 73 |
+
return 0
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def main():
|
| 77 |
+
"""Runs project's fuzzers for CI tools.
|
| 78 |
+
This is the entrypoint for the run_fuzzers github action.
|
| 79 |
+
|
| 80 |
+
NOTE: libFuzzer binaries must be located in the $WORKSPACE/build-out
|
| 81 |
+
directory in order for this action to be used. This action will only fuzz the
|
| 82 |
+
binaries that are located in that directory. It is recommended that you add
|
| 83 |
+
the build_fuzzers action preceding this one.
|
| 84 |
+
|
| 85 |
+
NOTE: Any crash report will be in the filepath:
|
| 86 |
+
${GITHUB_WORKSPACE}/out/testcase
|
| 87 |
+
This can be used in parallel with the upload-artifact action to surface the
|
| 88 |
+
logs.
|
| 89 |
+
|
| 90 |
+
Returns:
|
| 91 |
+
0 on success or nonzero on failure.
|
| 92 |
+
"""
|
| 93 |
+
return run_fuzzers_entrypoint()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
if __name__ == '__main__':
|
| 97 |
+
sys.exit(main())
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/sarif_utils.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for outputting SARIF data."""
|
| 15 |
+
import copy
|
| 16 |
+
import json
|
| 17 |
+
import logging
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
from clusterfuzz import stacktraces
|
| 21 |
+
|
| 22 |
+
SARIF_RULES = [
|
| 23 |
+
{
|
| 24 |
+
'id': 'no-crashes',
|
| 25 |
+
'shortDescription': {
|
| 26 |
+
'text': 'Don\'t crash'
|
| 27 |
+
},
|
| 28 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/416.html',
|
| 29 |
+
'properties': {
|
| 30 |
+
'category': 'Crashes'
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
'id': 'heap-use-after-free',
|
| 35 |
+
'shortDescription': {
|
| 36 |
+
'text': 'Use of a heap-object after it has been freed.'
|
| 37 |
+
},
|
| 38 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/416.html',
|
| 39 |
+
'properties': {
|
| 40 |
+
'category': 'Crashes'
|
| 41 |
+
}
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
'id': 'heap-buffer-overflow',
|
| 45 |
+
'shortDescription': {
|
| 46 |
+
'text': 'A read or write past the end of a heap buffer.'
|
| 47 |
+
},
|
| 48 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/122.html',
|
| 49 |
+
'properties': {
|
| 50 |
+
'category': 'Crashes'
|
| 51 |
+
}
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
'id': 'stack-buffer-overflow',
|
| 55 |
+
'shortDescription': {
|
| 56 |
+
'text': 'A read or write past the end of a stack buffer.'
|
| 57 |
+
},
|
| 58 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/121.html',
|
| 59 |
+
'properties': {
|
| 60 |
+
'category': 'Crashes'
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
'id': 'global-buffer-overflow',
|
| 65 |
+
'shortDescription': {
|
| 66 |
+
'text': 'A read or write past the end of a global buffer.'
|
| 67 |
+
},
|
| 68 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/121.html',
|
| 69 |
+
'properties': {
|
| 70 |
+
'category': 'Crashes'
|
| 71 |
+
}
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
'id': 'stack-use-after-return',
|
| 75 |
+
'shortDescription': {
|
| 76 |
+
'text':
|
| 77 |
+
'A stack-based variable has been used after the function returned.'
|
| 78 |
+
},
|
| 79 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/562.html',
|
| 80 |
+
'properties': {
|
| 81 |
+
'category': 'Crashes'
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
'id': 'stack-use-after-scope',
|
| 86 |
+
'shortDescription': {
|
| 87 |
+
'text':
|
| 88 |
+
'A stack-based variable has been used outside of the scope in which it exists.'
|
| 89 |
+
},
|
| 90 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/562.html',
|
| 91 |
+
'properties': {
|
| 92 |
+
'category': 'Crashes'
|
| 93 |
+
}
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
'id': 'initialization-order-fiasco',
|
| 97 |
+
'shortDescription': {
|
| 98 |
+
'text': 'Problem with order of initialization of global objects.'
|
| 99 |
+
},
|
| 100 |
+
'helpUri': 'https://isocpp.org/wiki/faq/ctors#static-init-order',
|
| 101 |
+
'properties': {
|
| 102 |
+
'category': 'Crashes'
|
| 103 |
+
}
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
'id':
|
| 107 |
+
'direct-leak',
|
| 108 |
+
'shortDescription': {
|
| 109 |
+
'text': 'Memory is leaked.'
|
| 110 |
+
},
|
| 111 |
+
'helpUri':
|
| 112 |
+
'https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer',
|
| 113 |
+
'properties': {
|
| 114 |
+
'category': 'Crashes'
|
| 115 |
+
}
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
'id':
|
| 119 |
+
'indirect-leak',
|
| 120 |
+
'shortDescription': {
|
| 121 |
+
'text': 'Memory is leaked.'
|
| 122 |
+
},
|
| 123 |
+
'helpUri':
|
| 124 |
+
'https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer',
|
| 125 |
+
'properties': {
|
| 126 |
+
'category': 'Crashes'
|
| 127 |
+
}
|
| 128 |
+
},
|
| 129 |
+
]
|
| 130 |
+
SARIF_DATA = {
|
| 131 |
+
'version':
|
| 132 |
+
'2.1.0',
|
| 133 |
+
'$schema':
|
| 134 |
+
'http://json.schemastore.org/sarif-2.1.0-rtm.4',
|
| 135 |
+
'runs': [{
|
| 136 |
+
'tool': {
|
| 137 |
+
'driver': {
|
| 138 |
+
'name': 'ClusterFuzzLite/CIFuzz',
|
| 139 |
+
'informationUri': 'https://google.github.io/clusterfuzzlite/',
|
| 140 |
+
'rules': SARIF_RULES,
|
| 141 |
+
}
|
| 142 |
+
},
|
| 143 |
+
'results': []
|
| 144 |
+
}]
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
SRC_ROOT = '/src/'
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def redact_src_path(src_path):
|
| 151 |
+
"""Redact the src path so that it can be reported to users."""
|
| 152 |
+
src_path = os.path.normpath(src_path)
|
| 153 |
+
if src_path.startswith(SRC_ROOT):
|
| 154 |
+
src_path = src_path[len(SRC_ROOT):]
|
| 155 |
+
|
| 156 |
+
src_path = os.sep.join(src_path.split(os.sep)[1:])
|
| 157 |
+
return src_path
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def get_error_frame(crash_info):
|
| 161 |
+
"""Returns the stackframe where the error occurred."""
|
| 162 |
+
if not crash_info.crash_state:
|
| 163 |
+
return None
|
| 164 |
+
state = crash_info.crash_state.split('\n')[0]
|
| 165 |
+
logging.info('state: %s frames %s, %s', state, crash_info.frames,
|
| 166 |
+
[f.function_name for f in crash_info.frames[0]])
|
| 167 |
+
|
| 168 |
+
for crash_frames in crash_info.frames:
|
| 169 |
+
for frame in crash_frames:
|
| 170 |
+
# TODO(metzman): Do something less fragile here.
|
| 171 |
+
if frame.function_name is None:
|
| 172 |
+
continue
|
| 173 |
+
if state in frame.function_name:
|
| 174 |
+
return frame
|
| 175 |
+
return None
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def get_error_source_info(crash_info):
|
| 179 |
+
"""Returns the filename and the line where the bug occurred."""
|
| 180 |
+
frame = get_error_frame(crash_info)
|
| 181 |
+
if not frame:
|
| 182 |
+
return (None, 1)
|
| 183 |
+
try:
|
| 184 |
+
return redact_src_path(frame.filename), int(frame.fileline or 1)
|
| 185 |
+
except TypeError:
|
| 186 |
+
return (None, 1)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def get_rule_index(crash_type):
|
| 190 |
+
"""Returns the rule index describe the rule that |crash_type| ran afoul of."""
|
| 191 |
+
# Don't include "READ" or "WRITE" or number of bytes.
|
| 192 |
+
crash_type = crash_type.replace('\n', ' ').split(' ')[0].lower()
|
| 193 |
+
logging.info('crash_type: %s.', crash_type)
|
| 194 |
+
for idx, rule in enumerate(SARIF_RULES):
|
| 195 |
+
if rule['id'] == crash_type:
|
| 196 |
+
logging.info('Rule index: %d.', idx)
|
| 197 |
+
return idx
|
| 198 |
+
|
| 199 |
+
return get_rule_index('no-crashes')
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def get_sarif_data(stacktrace, target_path):
|
| 203 |
+
"""Returns a description of the crash in SARIF."""
|
| 204 |
+
data = copy.deepcopy(SARIF_DATA)
|
| 205 |
+
if stacktrace is None:
|
| 206 |
+
return data
|
| 207 |
+
|
| 208 |
+
fuzz_target = os.path.basename(target_path)
|
| 209 |
+
stack_parser = stacktraces.StackParser(fuzz_target=fuzz_target,
|
| 210 |
+
symbolized=True,
|
| 211 |
+
detect_ooms_and_hangs=True,
|
| 212 |
+
include_ubsan=True)
|
| 213 |
+
crash_info = stack_parser.parse(stacktrace)
|
| 214 |
+
error_source_info = get_error_source_info(crash_info)
|
| 215 |
+
rule_idx = get_rule_index(crash_info.crash_type)
|
| 216 |
+
rule_id = SARIF_RULES[rule_idx]['id']
|
| 217 |
+
uri = error_source_info[0]
|
| 218 |
+
|
| 219 |
+
result = {
|
| 220 |
+
'level': 'error',
|
| 221 |
+
'message': {
|
| 222 |
+
'text': crash_info.crash_type
|
| 223 |
+
},
|
| 224 |
+
'locations': [{
|
| 225 |
+
'physicalLocation': {
|
| 226 |
+
'artifactLocation': {
|
| 227 |
+
'uri': uri,
|
| 228 |
+
'index': 0
|
| 229 |
+
},
|
| 230 |
+
'region': {
|
| 231 |
+
'startLine': error_source_info[1],
|
| 232 |
+
# We don't have this granualarity fuzzing.
|
| 233 |
+
'startColumn': 1,
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
}],
|
| 237 |
+
'ruleId': rule_id,
|
| 238 |
+
'ruleIndex': rule_idx
|
| 239 |
+
}
|
| 240 |
+
if uri:
|
| 241 |
+
data['runs'][0]['results'].append(result)
|
| 242 |
+
return data
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def write_stacktrace_to_sarif(stacktrace, target_path, workspace):
|
| 246 |
+
"""Writes a description of the crash in stacktrace to a SARIF file."""
|
| 247 |
+
data = get_sarif_data(stacktrace, target_path)
|
| 248 |
+
if not os.path.exists(workspace.sarif):
|
| 249 |
+
os.makedirs(workspace.sarif)
|
| 250 |
+
with open(os.path.join(workspace.sarif, 'results.sarif'), 'w') as file_handle:
|
| 251 |
+
file_handle.write(json.dumps(data))
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/sarif_utils_test.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for sarif_utils.py"""
|
| 15 |
+
import os
|
| 16 |
+
import unittest
|
| 17 |
+
from unittest import mock
|
| 18 |
+
|
| 19 |
+
import sarif_utils
|
| 20 |
+
|
| 21 |
+
CRASH_INFO_FILELINE = 403
|
| 22 |
+
|
| 23 |
+
TEST_DATA = os.path.join(os.path.dirname(__file__), 'test_data')
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class GetSarifDataTest(unittest.TestCase):
|
| 27 |
+
"""Tests for get_sarif_data."""
|
| 28 |
+
|
| 29 |
+
def setUp(self):
|
| 30 |
+
self.maxDiff = None # pylint: disable=invalid-name
|
| 31 |
+
|
| 32 |
+
def test_get_sarif_data_none(self):
|
| 33 |
+
"""Tests get_sarif_data when there was no crash."""
|
| 34 |
+
self.assertEqual(sarif_utils.get_sarif_data(None, '/root/target'),
|
| 35 |
+
sarif_utils.SARIF_DATA)
|
| 36 |
+
|
| 37 |
+
def test_ordinary_case(self):
|
| 38 |
+
stacktrace_filename = os.path.join(TEST_DATA,
|
| 39 |
+
'sarif_utils_systemd_stack.txt')
|
| 40 |
+
with open(stacktrace_filename, 'r') as fp:
|
| 41 |
+
stacktrace = fp.read()
|
| 42 |
+
expected_result = {
|
| 43 |
+
'level': 'error',
|
| 44 |
+
'message': {
|
| 45 |
+
'text': 'Heap-buffer-overflow\nREAD 4'
|
| 46 |
+
},
|
| 47 |
+
'locations': [{
|
| 48 |
+
'physicalLocation': {
|
| 49 |
+
'artifactLocation': {
|
| 50 |
+
'uri': 'src/core/fuzz-unit-file.c',
|
| 51 |
+
'index': 0
|
| 52 |
+
},
|
| 53 |
+
'region': {
|
| 54 |
+
'startLine': 30,
|
| 55 |
+
# We don't have this granualarity fuzzing.
|
| 56 |
+
'startColumn': 1,
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
}],
|
| 60 |
+
'ruleId': 'heap-buffer-overflow',
|
| 61 |
+
'ruleIndex': 2
|
| 62 |
+
}
|
| 63 |
+
actual_result = sarif_utils.get_sarif_data(
|
| 64 |
+
stacktrace, '/root/target')['runs'][0]['results'][0]
|
| 65 |
+
self.assertEqual(actual_result, expected_result)
|
| 66 |
+
|
| 67 |
+
def test_llvmfuzzertestoneinput_case(self):
|
| 68 |
+
stacktrace_filename = os.path.join(TEST_DATA,
|
| 69 |
+
'sarif_utils_only_llvmfuzzer_stack.txt')
|
| 70 |
+
with open(stacktrace_filename, 'r') as fp:
|
| 71 |
+
stacktrace = fp.read()
|
| 72 |
+
actual_result = sarif_utils.get_sarif_data(
|
| 73 |
+
stacktrace, '/root/target')['runs'][0]['results']
|
| 74 |
+
self.assertEqual(actual_result, [])
|
| 75 |
+
|
| 76 |
+
def test_msan(self):
|
| 77 |
+
"""Tests that MSAN stacktraces don't exception."""
|
| 78 |
+
stacktrace_filename = os.path.join(TEST_DATA, 'sarif_utils_msan_stack.txt')
|
| 79 |
+
with open(stacktrace_filename, 'r') as fp:
|
| 80 |
+
stacktrace = fp.read()
|
| 81 |
+
|
| 82 |
+
actual_result = sarif_utils.get_sarif_data(stacktrace, '/root/target')
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class RedactSrcPathTest(unittest.TestCase):
|
| 86 |
+
"""Tests for redact_src_path."""
|
| 87 |
+
|
| 88 |
+
def test_redact_src_path(self):
|
| 89 |
+
"""Tests redact_src_path."""
|
| 90 |
+
path = '/src/src-repo/subdir/file'
|
| 91 |
+
self.assertEqual(sarif_utils.redact_src_path(path), 'subdir/file')
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _get_mock_crash_info():
|
| 95 |
+
"""Returns a mock crash_info to be used in tests."""
|
| 96 |
+
stack_frame = mock.MagicMock()
|
| 97 |
+
stack_frame.filename = '/src/repo-dir/sub/vuln.cc'
|
| 98 |
+
stack_frame.function_name = 'vuln_func'
|
| 99 |
+
stack_frame.fileline = CRASH_INFO_FILELINE
|
| 100 |
+
crash1_frames = [stack_frame, stack_frame]
|
| 101 |
+
frames = [crash1_frames]
|
| 102 |
+
crash_info = mock.MagicMock()
|
| 103 |
+
crash_info.frames = frames
|
| 104 |
+
crash_info.crash_state = 'vuln_func\nvuln_func0\nvuln_func1'
|
| 105 |
+
return crash_info
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
class GetErrorSourceInfoTest(unittest.TestCase):
|
| 109 |
+
"""Tests for get_error_source_info."""
|
| 110 |
+
|
| 111 |
+
def test_redact_src_path(self):
|
| 112 |
+
"""Tests that get_error_source_info finds the right source info."""
|
| 113 |
+
crash_info = _get_mock_crash_info()
|
| 114 |
+
source_info = sarif_utils.get_error_source_info(crash_info)
|
| 115 |
+
expected_source_info = ('sub/vuln.cc', CRASH_INFO_FILELINE)
|
| 116 |
+
self.assertEqual(source_info, expected_source_info)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class GetRuleIndexTest(unittest.TestCase):
|
| 120 |
+
"""Tests for get_rule_index."""
|
| 121 |
+
CRASH_INFO_CRASH_TYPE = 'Heap-use-after-free READ 8'
|
| 122 |
+
|
| 123 |
+
def test_get_rule_index(self):
|
| 124 |
+
"""Tests that get_rule_index finds the right rule index."""
|
| 125 |
+
index = sarif_utils.get_rule_index(self.CRASH_INFO_CRASH_TYPE)
|
| 126 |
+
self.assertEqual(sarif_utils.SARIF_RULES[index]['id'],
|
| 127 |
+
'heap-use-after-free')
|
| 128 |
+
self.assertEqual(sarif_utils.get_rule_index('no-crashes'), 0)
|
local-test-tika-delta-05/fuzz-tooling/infra/cifuzz/test_helpers.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Contains convenient helpers for writing tests."""
|
| 15 |
+
|
| 16 |
+
import contextlib
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
import shutil
|
| 20 |
+
import tempfile
|
| 21 |
+
from unittest import mock
|
| 22 |
+
|
| 23 |
+
import config_utils
|
| 24 |
+
import docker
|
| 25 |
+
import workspace_utils
|
| 26 |
+
|
| 27 |
+
INFRA_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 28 |
+
# pylint: disable=wrong-import-position,import-error
|
| 29 |
+
sys.path.append(INFRA_DIR)
|
| 30 |
+
|
| 31 |
+
import helper
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# TODO(metzman): Get rid of these decorators.
|
| 35 |
+
@mock.patch('config_utils._is_dry_run', return_value=True)
|
| 36 |
+
@mock.patch('platform_config.BasePlatformConfig.project_src_path',
|
| 37 |
+
return_value=None)
|
| 38 |
+
@mock.patch('os.path.basename', return_value=None)
|
| 39 |
+
def _create_config(config_cls, _, __, ___, **kwargs):
|
| 40 |
+
"""Creates a config object from |config_cls| and then sets every attribute
|
| 41 |
+
that is a key in |kwargs| to the corresponding value. Asserts that each key in
|
| 42 |
+
|kwargs| is an attribute of config."""
|
| 43 |
+
with mock.patch('config_utils.BaseConfig.validate', return_value=True):
|
| 44 |
+
config = config_cls()
|
| 45 |
+
for key, value in kwargs.items():
|
| 46 |
+
assert hasattr(config, key), 'Config doesn\'t have attribute: ' + key
|
| 47 |
+
setattr(config, key, value)
|
| 48 |
+
|
| 49 |
+
return config
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def create_build_config(**kwargs):
|
| 53 |
+
"""Wrapper around _create_config for build configs."""
|
| 54 |
+
return _create_config(config_utils.BuildFuzzersConfig, **kwargs)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def create_run_config(**kwargs):
|
| 58 |
+
"""Wrapper around _create_config for run configs."""
|
| 59 |
+
return _create_config(config_utils.RunFuzzersConfig, **kwargs)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def create_workspace(workspace_path='/workspace'):
|
| 63 |
+
"""Returns a workspace located at |workspace_path| ('/workspace' by
|
| 64 |
+
default)."""
|
| 65 |
+
config = create_run_config(workspace=workspace_path)
|
| 66 |
+
return workspace_utils.Workspace(config)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def patch_environ(testcase_obj, env=None, empty=False, runner=False):
|
| 70 |
+
"""Patch environment. |testcase_obj| is the unittest.TestCase that contains
|
| 71 |
+
tests. |env|, if specified, is a dictionary of environment variables to start
|
| 72 |
+
from. If |empty| is True then the new patched environment will be empty. If
|
| 73 |
+
|runner| is True then the necessary environment variables will be set to run
|
| 74 |
+
the scripts from base-runner."""
|
| 75 |
+
if env is None:
|
| 76 |
+
env = {}
|
| 77 |
+
|
| 78 |
+
patcher = mock.patch.dict(os.environ, env)
|
| 79 |
+
testcase_obj.addCleanup(patcher.stop)
|
| 80 |
+
patcher.start()
|
| 81 |
+
if empty:
|
| 82 |
+
for key in os.environ.copy():
|
| 83 |
+
del os.environ[key]
|
| 84 |
+
|
| 85 |
+
if runner:
|
| 86 |
+
# Add the scripts for base-runner to the path since the wont be in
|
| 87 |
+
# /usr/local/bin on host machines during testing.
|
| 88 |
+
base_runner_dir = os.path.join(INFRA_DIR, 'base-images', 'base-runner')
|
| 89 |
+
os.environ['PATH'] = (os.environ.get('PATH', '') + os.pathsep +
|
| 90 |
+
base_runner_dir)
|
| 91 |
+
if 'GOPATH' not in os.environ:
|
| 92 |
+
# A GOPATH must be set or else the coverage script fails, even for getting
|
| 93 |
+
# the coverage of non-Go programs.
|
| 94 |
+
os.environ['GOPATH'] = '/root/go'
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@contextlib.contextmanager
|
| 98 |
+
def temp_dir_copy(directory):
|
| 99 |
+
"""Context manager that yields a temporary copy of |directory|."""
|
| 100 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 101 |
+
temp_copy_path = os.path.join(temp_dir, os.path.basename(directory))
|
| 102 |
+
shutil.copytree(directory, temp_copy_path)
|
| 103 |
+
yield temp_copy_path
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@contextlib.contextmanager
|
| 107 |
+
def docker_temp_dir():
|
| 108 |
+
"""Returns a temporary a directory that is useful for use with docker. On
|
| 109 |
+
cleanup this contextmanager uses docker to delete the directory's contents so
|
| 110 |
+
that if anything is owned by root it can be deleted (which
|
| 111 |
+
tempfile.TemporaryDirectory() cannot do) by non-root users."""
|
| 112 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 113 |
+
yield temp_dir
|
| 114 |
+
helper.docker_run([
|
| 115 |
+
'-v', f'{temp_dir}:/temp_dir', '-t', docker.BASE_BUILDER_TAG,
|
| 116 |
+
'/bin/bash', '-c', 'rm -rf /temp_dir/*'
|
| 117 |
+
])
|
local-test-tika-delta-05/fuzz-tooling/infra/experimental/SystemSan/SystemSan.cpp
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* Copyright 2022 Google LLC
|
| 3 |
+
|
| 4 |
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
* you may not use this file except in compliance with the License.
|
| 6 |
+
* You may obtain a copy of the License at
|
| 7 |
+
|
| 8 |
+
* http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
|
| 10 |
+
* Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
* See the License for the specific language governing permissions and
|
| 14 |
+
* limitations under the License.
|
| 15 |
+
*/
|
| 16 |
+
/* A detector that uses ptrace to identify shell injection vulnerabilities. */
|
| 17 |
+
|
| 18 |
+
/* C standard library */
|
| 19 |
+
#include <errno.h>
|
| 20 |
+
#include <signal.h>
|
| 21 |
+
#include <stdio.h>
|
| 22 |
+
#include <string.h>
|
| 23 |
+
|
| 24 |
+
/* POSIX */
|
| 25 |
+
#include <sys/stat.h>
|
| 26 |
+
#include <sys/user.h>
|
| 27 |
+
#include <sys/wait.h>
|
| 28 |
+
#include <unistd.h>
|
| 29 |
+
|
| 30 |
+
/* Linux */
|
| 31 |
+
#include <sys/ptrace.h>
|
| 32 |
+
#include <syscall.h>
|
| 33 |
+
#include <fcntl.h>
|
| 34 |
+
|
| 35 |
+
#include <fstream>
|
| 36 |
+
#include <iostream>
|
| 37 |
+
#include <map>
|
| 38 |
+
#include <set>
|
| 39 |
+
#include <sstream>
|
| 40 |
+
#include <string>
|
| 41 |
+
#include <vector>
|
| 42 |
+
|
| 43 |
+
#include "inspect_utils.h"
|
| 44 |
+
#include "inspect_dns.h"
|
| 45 |
+
|
| 46 |
+
#define DEBUG_LOGS 0
|
| 47 |
+
|
| 48 |
+
#if DEBUG_LOGS
|
| 49 |
+
#define debug_log(...) \
|
| 50 |
+
do { \
|
| 51 |
+
fprintf(stderr, __VA_ARGS__); \
|
| 52 |
+
fflush(stdout); \
|
| 53 |
+
fputc('\n', stderr); \
|
| 54 |
+
} while (0)
|
| 55 |
+
#else
|
| 56 |
+
#define debug_log(...)
|
| 57 |
+
#endif
|
| 58 |
+
|
| 59 |
+
#define fatal_log(...) \
|
| 60 |
+
do { \
|
| 61 |
+
fprintf(stderr, __VA_ARGS__); \
|
| 62 |
+
fputc('\n', stderr); \
|
| 63 |
+
exit(EXIT_FAILURE); \
|
| 64 |
+
} while (0)
|
| 65 |
+
|
| 66 |
+
// The magic string that we'll use to detect full control over the command
|
| 67 |
+
// executed.
|
| 68 |
+
const std::string kTripWire = "/tmp/tripwire";
|
| 69 |
+
// Shell injection bug confirmed with /tmp/tripwire.
|
| 70 |
+
const std::string kInjectionError = "Shell injection";
|
| 71 |
+
// Shell corruption bug detected based on syntax error.
|
| 72 |
+
const std::string kCorruptionError = "Shell corruption";
|
| 73 |
+
// The magic string that we'll use to detect arbitrary file open
|
| 74 |
+
const std::string kFzAbsoluteDirectory = "/fz/";
|
| 75 |
+
// Arbitrary file open in /fz/
|
| 76 |
+
const std::string kArbitraryFileOpenError = "Arbitrary file open";
|
| 77 |
+
// Assuming only shorter (than this constant) top dir are legitly used.
|
| 78 |
+
constexpr int kRootDirMaxLength = 16;
|
| 79 |
+
|
| 80 |
+
// The PID of the root process we're fuzzing.
|
| 81 |
+
pid_t g_root_pid;
|
| 82 |
+
|
| 83 |
+
// Map of a PID/TID its PID/TID creator and wether it ran exec.
|
| 84 |
+
std::map<pid_t, ThreadParent> root_pids;
|
| 85 |
+
|
| 86 |
+
// Assuming the longest pathname is "/bin/bash".
|
| 87 |
+
constexpr int kShellPathnameLength = 20;
|
| 88 |
+
|
| 89 |
+
// Syntax error messages of each shell.
|
| 90 |
+
const std::map<std::string, std::set<std::string>> kShellSyntaxErrors = {
|
| 91 |
+
{"bash",
|
| 92 |
+
{
|
| 93 |
+
": command not found", // General
|
| 94 |
+
": syntax error", // Unfinished " or ' or ` or if, leading | or ;
|
| 95 |
+
": missing `]'", // Unfinished [
|
| 96 |
+
": event not found", // ! leads large numbers
|
| 97 |
+
": No such file or directory", // Leading < or /
|
| 98 |
+
}},
|
| 99 |
+
{"csh",
|
| 100 |
+
{
|
| 101 |
+
": Command not found.", // General
|
| 102 |
+
": Missing }.", // Unfinished {
|
| 103 |
+
"Too many ('s.", // Unfinished (
|
| 104 |
+
"Invalid null command.", // Leading | or < or >
|
| 105 |
+
"Missing name for redirect.", // Single < or >
|
| 106 |
+
": No match.", // Leading ? or [ or *
|
| 107 |
+
"Modifier failed.", // Leading ^
|
| 108 |
+
"No previous left hand side.", // A ^
|
| 109 |
+
": No such job.", // Leading %
|
| 110 |
+
": No current job.", // A %
|
| 111 |
+
": Undefined variable.", // Containing $
|
| 112 |
+
": Event not found.", // ! leads large numbers
|
| 113 |
+
// TODO: Make this more specific.
|
| 114 |
+
"Unmatched", // Unfinished " or ' or `, leading ;
|
| 115 |
+
}},
|
| 116 |
+
{"dash",
|
| 117 |
+
{
|
| 118 |
+
"not found", // General
|
| 119 |
+
"Syntax error", // Unfinished " or ' or ` or if, leading | or ; or &
|
| 120 |
+
"missing ]", // Unfinished [
|
| 121 |
+
"No such file", // Leading <
|
| 122 |
+
}},
|
| 123 |
+
{"zsh",
|
| 124 |
+
{
|
| 125 |
+
": command not found", // General
|
| 126 |
+
": syntax error", // Unfinished " or ' or `
|
| 127 |
+
": ']' expected", // Unfinished [
|
| 128 |
+
": no such file or directory", // Leading < or /
|
| 129 |
+
": parse error near", // Leading |, or &
|
| 130 |
+
": no such user or named directory", // Leading ~
|
| 131 |
+
}},
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
// Shells used by Processes.
|
| 135 |
+
std::map<pid_t, std::string> g_shell_pids;
|
| 136 |
+
|
| 137 |
+
struct Tracee {
|
| 138 |
+
pid_t pid;
|
| 139 |
+
bool syscall_enter = true;
|
| 140 |
+
|
| 141 |
+
Tracee(pid_t pid) : pid(pid) {}
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
pid_t run_child(char **argv) {
|
| 145 |
+
// Run the program under test with its args as a child process
|
| 146 |
+
pid_t pid = fork();
|
| 147 |
+
switch (pid) {
|
| 148 |
+
case -1:
|
| 149 |
+
fatal_log("Fork failed: %s", strerror(errno));
|
| 150 |
+
case 0:
|
| 151 |
+
raise(SIGSTOP);
|
| 152 |
+
execvp(argv[0], argv);
|
| 153 |
+
fatal_log("execvp: %s", strerror(errno));
|
| 154 |
+
}
|
| 155 |
+
return pid;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
// Construct a string with the memory specified in a register.
|
| 159 |
+
std::string read_string(pid_t pid, unsigned long reg, unsigned long length) {
|
| 160 |
+
auto memory = read_memory(pid, reg, length);
|
| 161 |
+
if (!memory.size()) {
|
| 162 |
+
return "";
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
std::string content(reinterpret_cast<char *>(memory.data()),
|
| 166 |
+
std::min(memory.size(), length));
|
| 167 |
+
return content;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
void inspect_for_injection(pid_t pid, const user_regs_struct ®s) {
|
| 171 |
+
// Inspect a PID's registers for the sign of shell injection.
|
| 172 |
+
std::string path = read_string(pid, regs.rdi, kTripWire.length());
|
| 173 |
+
if (!path.length()) {
|
| 174 |
+
return;
|
| 175 |
+
}
|
| 176 |
+
debug_log("inspecting");
|
| 177 |
+
if (path == kTripWire) {
|
| 178 |
+
report_bug(kInjectionError, pid);
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
std::string get_pathname(pid_t pid, const user_regs_struct ®s) {
|
| 183 |
+
// Parse the pathname from the memory specified in the RDI register.
|
| 184 |
+
std::string pathname = read_string(pid, regs.rdi, kShellPathnameLength);
|
| 185 |
+
debug_log("Pathname is %s (len %lu)\n", pathname.c_str(), pathname.length());
|
| 186 |
+
return pathname;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
std::string match_shell(std::string binary_pathname);
|
| 190 |
+
|
| 191 |
+
// Identify the exact shell behind sh
|
| 192 |
+
std::string identify_sh(std::string path) {
|
| 193 |
+
char shell_pathname[kShellPathnameLength];
|
| 194 |
+
auto written = readlink(path.c_str(), shell_pathname, kShellPathnameLength - 1);
|
| 195 |
+
if (written == -1) {
|
| 196 |
+
std::cerr << "Cannot query which shell is behind sh: readlink failed on "
|
| 197 |
+
<< path << ": "
|
| 198 |
+
<< strerror(errno) << "\n";
|
| 199 |
+
std::cerr << "Assuming the shell is dash\n";
|
| 200 |
+
return "dash";
|
| 201 |
+
}
|
| 202 |
+
shell_pathname[written] = '\0';
|
| 203 |
+
|
| 204 |
+
debug_log("sh links to %s\n", shell_pathname);
|
| 205 |
+
std::string shell_pathname_str(shell_pathname);
|
| 206 |
+
|
| 207 |
+
return match_shell(shell_pathname_str);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
std::string match_shell(std::string binary_pathname) {
|
| 211 |
+
// Identify the name of the shell used in the pathname.
|
| 212 |
+
if (!binary_pathname.length()) {
|
| 213 |
+
return "";
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// We use c_str() to accept only the null terminated string.
|
| 217 |
+
std::string binary_name = binary_pathname.substr(
|
| 218 |
+
binary_pathname.find_last_of("/") + 1).c_str();
|
| 219 |
+
|
| 220 |
+
debug_log("Binary is %s (%lu)\n", binary_name.c_str(),
|
| 221 |
+
binary_name.length());
|
| 222 |
+
|
| 223 |
+
for (const auto &item : kShellSyntaxErrors) {
|
| 224 |
+
std::string known_shell = item.first;
|
| 225 |
+
if (binary_name == "sh") {
|
| 226 |
+
debug_log("Matched sh: Needs to identify which specific shell it is.\n");
|
| 227 |
+
return identify_sh(binary_pathname);
|
| 228 |
+
}
|
| 229 |
+
if (binary_name == known_shell) {
|
| 230 |
+
debug_log("Matched %s\n", binary_name.c_str());
|
| 231 |
+
return known_shell;
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
return "";
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
std::string get_shell(pid_t pid, const user_regs_struct ®s) {
|
| 238 |
+
// Get shell name used in a process.
|
| 239 |
+
std::string binary_pathname = get_pathname(pid, regs);
|
| 240 |
+
return match_shell(binary_pathname);
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
void match_error_pattern(std::string buffer, std::string shell, pid_t pid) {
|
| 244 |
+
auto error_patterns = kShellSyntaxErrors.at(shell);
|
| 245 |
+
for (const auto &pattern : error_patterns) {
|
| 246 |
+
if (buffer.find(pattern) != std::string::npos) {
|
| 247 |
+
std::cerr << "--- Found a sign of shell corruption ---\n"
|
| 248 |
+
<< buffer.c_str()
|
| 249 |
+
<< "\n----------------------------------------\n";
|
| 250 |
+
// If a shell corruption error happens, kill its parent.
|
| 251 |
+
auto parent = root_pids[pid];
|
| 252 |
+
while (!parent.ran_exec) {
|
| 253 |
+
if (parent.parent_tid == g_root_pid) {
|
| 254 |
+
break;
|
| 255 |
+
}
|
| 256 |
+
parent = root_pids[parent.parent_tid];
|
| 257 |
+
}
|
| 258 |
+
report_bug(kCorruptionError, parent.parent_tid);
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
void inspect_for_corruption(pid_t pid, const user_regs_struct ®s) {
|
| 264 |
+
// Inspect a PID's registers for shell corruption.
|
| 265 |
+
std::string buffer = read_string(pid, regs.rsi, regs.rdx);
|
| 266 |
+
debug_log("Write buffer: %s\n", buffer.c_str());
|
| 267 |
+
match_error_pattern(buffer, g_shell_pids[pid], pid);
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
void log_file_open(std::string path, int flags, pid_t pid) {
|
| 271 |
+
report_bug(kArbitraryFileOpenError, pid);
|
| 272 |
+
std::cerr << "===File opened: " << path.c_str() << ", flags = " << flags << ",";
|
| 273 |
+
switch (flags & 3) {
|
| 274 |
+
case O_RDONLY:
|
| 275 |
+
std::cerr << "O_RDONLY";
|
| 276 |
+
break;
|
| 277 |
+
case O_WRONLY:
|
| 278 |
+
std::cerr << "O_WRONLY";
|
| 279 |
+
break;
|
| 280 |
+
case O_RDWR:
|
| 281 |
+
std::cerr << "O_RDWR";
|
| 282 |
+
break;
|
| 283 |
+
default:
|
| 284 |
+
std::cerr << "unknown";
|
| 285 |
+
}
|
| 286 |
+
std::cerr << "===\n";
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
bool has_unprintable(const std::string &value) {
|
| 290 |
+
for (size_t i = 0; i < value.length(); i++) {
|
| 291 |
+
if (value[i] & 0x80) {
|
| 292 |
+
return true;
|
| 293 |
+
}
|
| 294 |
+
}
|
| 295 |
+
return false;
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
void inspect_for_arbitrary_file_open(pid_t pid, const user_regs_struct ®s) {
|
| 299 |
+
// Inspect a PID's register for the sign of arbitrary file open.
|
| 300 |
+
std::string path = read_string(pid, regs.rsi, kRootDirMaxLength);
|
| 301 |
+
if (!path.length()) {
|
| 302 |
+
return;
|
| 303 |
+
}
|
| 304 |
+
if (path.substr(0, kFzAbsoluteDirectory.length()) == kFzAbsoluteDirectory) {
|
| 305 |
+
log_file_open(path, regs.rdx, pid);
|
| 306 |
+
return;
|
| 307 |
+
}
|
| 308 |
+
if (path[0] == '/' && path.length() > 1) {
|
| 309 |
+
std::string path_absolute_topdir = path;
|
| 310 |
+
size_t root_dir_end = path.find('/', 1);
|
| 311 |
+
if (root_dir_end != std::string::npos) {
|
| 312 |
+
path_absolute_topdir = path.substr(0, root_dir_end);
|
| 313 |
+
}
|
| 314 |
+
if (has_unprintable(path_absolute_topdir)) {
|
| 315 |
+
struct stat dirstat;
|
| 316 |
+
if (stat(path_absolute_topdir.c_str(), &dirstat) != 0) {
|
| 317 |
+
log_file_open(path, regs.rdx, pid);
|
| 318 |
+
}
|
| 319 |
+
}
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
int trace(std::map<pid_t, Tracee> pids) {
|
| 324 |
+
unsigned long exit_status = 0;
|
| 325 |
+
while (!pids.empty()) {
|
| 326 |
+
std::vector<pid_t> new_pids;
|
| 327 |
+
|
| 328 |
+
auto it = pids.begin();
|
| 329 |
+
|
| 330 |
+
while (it != pids.end()) {
|
| 331 |
+
auto pid = it->first;
|
| 332 |
+
auto &tracee = it->second;
|
| 333 |
+
int status = 0;
|
| 334 |
+
|
| 335 |
+
int result = waitpid(pid, &status, __WALL | WNOHANG);
|
| 336 |
+
if (result == -1) {
|
| 337 |
+
it = pids.erase(it);
|
| 338 |
+
continue;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
if (result == 0) {
|
| 342 |
+
// Nothing to report yet.
|
| 343 |
+
++it;
|
| 344 |
+
continue;
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
if (WIFEXITED(status) || WIFSIGNALED(status)) {
|
| 348 |
+
debug_log("%d exited", pid);
|
| 349 |
+
it = pids.erase(it);
|
| 350 |
+
// Remove pid from the watchlist when it exits
|
| 351 |
+
g_shell_pids.erase(pid);
|
| 352 |
+
root_pids.erase(pid);
|
| 353 |
+
continue;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
// ptrace sets 0x80 for syscalls (with PTRACE_O_TRACESYSGOOD set).
|
| 357 |
+
bool is_syscall =
|
| 358 |
+
WIFSTOPPED(status) && WSTOPSIG(status) == (SIGTRAP | 0x80);
|
| 359 |
+
int sig = 0;
|
| 360 |
+
if (!is_syscall) {
|
| 361 |
+
// Handle generic signal.
|
| 362 |
+
siginfo_t siginfo;
|
| 363 |
+
if (ptrace(PTRACE_GETSIGINFO, pid, nullptr, &siginfo) == -1) {
|
| 364 |
+
debug_log("ptrace(PTRACE_GETSIGINFO, %d): %s", pid, strerror(errno));
|
| 365 |
+
continue;
|
| 366 |
+
}
|
| 367 |
+
sig = siginfo.si_signo;
|
| 368 |
+
debug_log("forwarding signal %d to %d", sig, pid);
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
if ((status >> 8 == (SIGTRAP | (PTRACE_EVENT_EXIT << 8)))) {
|
| 372 |
+
debug_log("%d exiting", pid);
|
| 373 |
+
if (pid == g_root_pid) {
|
| 374 |
+
if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &exit_status) == -1) {
|
| 375 |
+
debug_log("ptrace(PTRACE_GETEVENTMSG, %d): %s", pid, strerror(errno));
|
| 376 |
+
}
|
| 377 |
+
debug_log("got exit status from root process: %lu", exit_status);
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
if (ptrace(PTRACE_DETACH, pid, 0, 0) == -1) {
|
| 381 |
+
debug_log("ptrace(PTRACE_DETACH, %d): %s", pid, strerror(errno));
|
| 382 |
+
}
|
| 383 |
+
continue;
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
if (WIFSTOPPED(status) &&
|
| 387 |
+
(status >> 8 == (SIGTRAP | (PTRACE_EVENT_CLONE << 8)) ||
|
| 388 |
+
status >> 8 == (SIGTRAP | (PTRACE_EVENT_FORK << 8)) ||
|
| 389 |
+
status >> 8 == (SIGTRAP | (PTRACE_EVENT_VFORK << 8)))) {
|
| 390 |
+
long new_pid;
|
| 391 |
+
if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &new_pid) == -1) {
|
| 392 |
+
debug_log("ptrace(PTRACE_GETEVENTMSG, %d): %s", pid, strerror(errno));
|
| 393 |
+
continue;
|
| 394 |
+
}
|
| 395 |
+
debug_log("forked %ld", new_pid);
|
| 396 |
+
new_pids.push_back(new_pid);
|
| 397 |
+
root_pids.emplace(new_pid, ThreadParent(pid));
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
if (is_syscall) {
|
| 401 |
+
user_regs_struct regs;
|
| 402 |
+
if (ptrace(PTRACE_GETREGS, pid, 0, ®s) == -1) {
|
| 403 |
+
debug_log("ptrace(PTRACE_GETREGS, %d): %s", pid, strerror(errno));
|
| 404 |
+
continue;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
if (tracee.syscall_enter) {
|
| 408 |
+
if (regs.orig_rax == __NR_execve) {
|
| 409 |
+
// This is a new process.
|
| 410 |
+
auto parent = root_pids[pid];
|
| 411 |
+
parent.ran_exec = true;
|
| 412 |
+
root_pids[pid] = parent;
|
| 413 |
+
inspect_for_injection(pid, regs);
|
| 414 |
+
std::string shell = get_shell(pid, regs);
|
| 415 |
+
if (shell != "") {
|
| 416 |
+
debug_log("Shell parsed: %s", shell.c_str());
|
| 417 |
+
g_shell_pids.insert(std::make_pair(pid, shell));
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
inspect_dns_syscalls(pid, regs);
|
| 422 |
+
|
| 423 |
+
if (regs.orig_rax == __NR_openat) {
|
| 424 |
+
// TODO(metzman): Re-enable this once we have config/flag support.
|
| 425 |
+
// inspect_for_arbitrary_file_open(pid, regs);
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
if (regs.orig_rax == __NR_write &&
|
| 429 |
+
g_shell_pids.find(pid) != g_shell_pids.end()) {
|
| 430 |
+
debug_log("Inspecting the `write` buffer of shell process %d.",
|
| 431 |
+
pid);
|
| 432 |
+
inspect_for_corruption(pid, regs);
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
// TODO: Check for commands with invalid syntax passed to /bin/sh and
|
| 437 |
+
// other shells.
|
| 438 |
+
// TODO: It's possible the process we're fuzzing can communicate with
|
| 439 |
+
// another process to execute code. Our check wouldn't catch this
|
| 440 |
+
// currently.
|
| 441 |
+
tracee.syscall_enter = !tracee.syscall_enter;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
if (ptrace(PTRACE_SYSCALL, pid, nullptr, sig) == -1) {
|
| 445 |
+
debug_log("ptrace(PTRACE_SYSCALL, %d): %s", pid, strerror(errno));
|
| 446 |
+
continue;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
++it;
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
for (const auto &pid : new_pids) {
|
| 453 |
+
pids.emplace(pid, Tracee(pid));
|
| 454 |
+
}
|
| 455 |
+
}
|
| 456 |
+
return static_cast<int>(exit_status >> 8);
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
int main(int argc, char **argv) {
|
| 460 |
+
if (argc <= 1) {
|
| 461 |
+
fatal_log("Expecting at least one arguments, received %d", argc - 1);
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
// Create an executable tripwire file, as programs may check for existence
|
| 465 |
+
// before actually calling exec.
|
| 466 |
+
std::ofstream tripwire(kTripWire);
|
| 467 |
+
tripwire.close();
|
| 468 |
+
chmod(kTripWire.c_str(), 0755);
|
| 469 |
+
|
| 470 |
+
pid_t pid = run_child(argv + 1);
|
| 471 |
+
|
| 472 |
+
long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
|
| 473 |
+
PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
|
| 474 |
+
PTRACE_O_TRACEEXIT;
|
| 475 |
+
|
| 476 |
+
if (ptrace(PTRACE_SEIZE, pid, nullptr, options) == -1) {
|
| 477 |
+
fatal_log("ptrace(PTRACE_SEIZE): %s", strerror(errno));
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
if (waitpid(pid, nullptr, __WALL) == -1) {
|
| 481 |
+
fatal_log("waitpid: %s", strerror(errno));
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) == -1) {
|
| 485 |
+
fatal_log("ptrace(PTRACE_SYSCALL): %s", strerror(errno));
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
g_root_pid = pid;
|
| 489 |
+
std::map<pid_t, Tracee> pids;
|
| 490 |
+
pids.emplace(pid, Tracee(pid));
|
| 491 |
+
root_pids.emplace(pid, ThreadParent(pid));
|
| 492 |
+
return trace(pids);
|
| 493 |
+
}
|
local-test-tika-delta-05/fuzz-tooling/infra/experimental/chronos/build_all.sh
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Copyright 2024 Google LLC
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
#
|
| 16 |
+
################################################################################
|
| 17 |
+
|
| 18 |
+
# Build all C/C++ projects.
|
| 19 |
+
c_project_yaml=$(find projects/ -name project.yaml -exec grep -l 'language: c' {} \;)
|
| 20 |
+
projs=$(echo $c_project_yaml | xargs dirname | xargs basename -a | sort)
|
| 21 |
+
|
| 22 |
+
cd infra/experimental/chronos
|
| 23 |
+
|
| 24 |
+
for proj in $projs; do
|
| 25 |
+
if [ ! -f ../../../projects/$proj/Dockerfile ]; then
|
| 26 |
+
# Incomplete integration.
|
| 27 |
+
echo "Skipping $proj as it's incomplete."
|
| 28 |
+
continue
|
| 29 |
+
fi
|
| 30 |
+
|
| 31 |
+
echo ./build_on_cloudbuild.sh $proj c
|
| 32 |
+
./build_on_cloudbuild.sh $proj c
|
| 33 |
+
done
|
local-test-tika-delta-05/fuzz-tooling/tools/vscode-extension/src/commands/cmdBuildFuzzerFromWorkspaceCFLite.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
| 2 |
+
// Copyright 2023 Google LLC
|
| 3 |
+
//
|
| 4 |
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
// you may not use this file except in compliance with the License.
|
| 6 |
+
// You may obtain a copy of the License at
|
| 7 |
+
//
|
| 8 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
//
|
| 10 |
+
// Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
// See the License for the specific language governing permissions and
|
| 14 |
+
// limitations under the License.
|
| 15 |
+
//
|
| 16 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 17 |
+
|
| 18 |
+
import {commandHistory} from '../commandUtils';
|
| 19 |
+
import {setStatusText} from '../utils';
|
| 20 |
+
import {buildFuzzersFromWorkspaceClusterfuzzLite} from '../ossfuzzWrappers';
|
| 21 |
+
|
| 22 |
+
export async function cmdInputCollectorBuildFuzzersFromWorkspaceCFLite() {
|
| 23 |
+
// Create an history object
|
| 24 |
+
const args = new Object({
|
| 25 |
+
toClean: false,
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
const commandObject = new Object({
|
| 29 |
+
commandType: 'oss-fuzz.WSBuildFuzzers',
|
| 30 |
+
Arguments: args,
|
| 31 |
+
dispatcherFunc: cmdDispatchbuildFuzzersFromWorkspaceClusterfuzzLite,
|
| 32 |
+
});
|
| 33 |
+
console.log('L1: ' + commandHistory.length);
|
| 34 |
+
commandHistory.push(commandObject);
|
| 35 |
+
|
| 36 |
+
await cmdDispatchbuildFuzzersFromWorkspaceClusterfuzzLite(args);
|
| 37 |
+
return true;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
| 41 |
+
async function cmdDispatchbuildFuzzersFromWorkspaceClusterfuzzLite(_args: any) {
|
| 42 |
+
await setStatusText('[CFLite] Building fuzzers: starting');
|
| 43 |
+
const res = await buildFuzzersFromWorkspaceClusterfuzzLite();
|
| 44 |
+
if (res) {
|
| 45 |
+
await setStatusText('[CFLite] Building fuzzers: finished');
|
| 46 |
+
} else {
|
| 47 |
+
await setStatusText('[CFLite] Building fuzzers: failed');
|
| 48 |
+
}
|
| 49 |
+
}
|