2016-09-01 23:32:06 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2016 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import argparse
|
2018-01-12 20:49:40 +00:00
|
|
|
import datetime
|
2016-11-04 15:04:36 +00:00
|
|
|
import errno
|
2016-09-01 23:32:06 +00:00
|
|
|
import os
|
2016-09-28 19:44:45 +00:00
|
|
|
import pipes
|
2016-10-14 01:07:57 +00:00
|
|
|
import re
|
2016-09-01 23:32:06 +00:00
|
|
|
import shutil
|
2016-11-14 18:17:38 +00:00
|
|
|
import string
|
2016-09-01 23:32:06 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2016-10-14 01:07:57 +00:00
|
|
|
import tempfile
|
2016-09-01 23:32:06 +00:00
|
|
|
import templates
|
2016-10-14 01:07:57 +00:00
|
|
|
import time
|
2016-09-01 23:32:06 +00:00
|
|
|
|
|
|
|
OSSFUZZ_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
BUILD_DIR = os.path.join(OSSFUZZ_DIR, 'build')
|
|
|
|
|
2017-05-09 19:29:38 +00:00
|
|
|
BASE_IMAGES = [
|
|
|
|
'gcr.io/oss-fuzz-base/base-image',
|
|
|
|
'gcr.io/oss-fuzz-base/base-clang',
|
|
|
|
'gcr.io/oss-fuzz-base/base-builder',
|
|
|
|
'gcr.io/oss-fuzz-base/base-runner',
|
|
|
|
'gcr.io/oss-fuzz-base/base-runner-debug',
|
2017-12-08 20:45:59 +00:00
|
|
|
'gcr.io/oss-fuzz-base/msan-builder',
|
2017-05-09 19:29:38 +00:00
|
|
|
]
|
|
|
|
|
2017-05-12 22:58:13 +00:00
|
|
|
VALID_PROJECT_NAME_REGEX = re.compile(r'^[a-zA-Z0-9_-]+$')
|
|
|
|
|
2016-12-28 01:14:33 +00:00
|
|
|
|
2016-09-01 23:32:06 +00:00
|
|
|
def main():
|
|
|
|
os.chdir(OSSFUZZ_DIR)
|
|
|
|
if not os.path.exists(BUILD_DIR):
|
|
|
|
os.mkdir(BUILD_DIR)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser('helper.py', description='oss-fuzz helpers')
|
2017-03-30 20:32:56 +00:00
|
|
|
subparsers = parser.add_subparsers(dest='command')
|
|
|
|
|
|
|
|
generate_parser = subparsers.add_parser(
|
|
|
|
'generate', help='Generate files for new project.')
|
|
|
|
generate_parser.add_argument('project_name')
|
|
|
|
|
|
|
|
build_image_parser = subparsers.add_parser(
|
|
|
|
'build_image', help='Build an image.')
|
|
|
|
build_image_parser.add_argument('project_name')
|
2017-05-09 19:29:38 +00:00
|
|
|
build_image_parser.add_argument('--pull', action='store_true',
|
|
|
|
help='Pull latest base image.')
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
build_fuzzers_parser = subparsers.add_parser(
|
|
|
|
'build_fuzzers', help='Build fuzzers for a project.')
|
|
|
|
_add_engine_args(build_fuzzers_parser)
|
|
|
|
_add_sanitizer_args(build_fuzzers_parser)
|
2017-04-06 01:13:20 +00:00
|
|
|
_add_environment_args(build_fuzzers_parser)
|
2017-03-30 20:32:56 +00:00
|
|
|
build_fuzzers_parser.add_argument('project_name')
|
|
|
|
build_fuzzers_parser.add_argument('source_path', help='path of local source',
|
2017-05-10 21:32:01 +00:00
|
|
|
nargs='?')
|
2018-05-01 04:01:48 +00:00
|
|
|
build_fuzzers_parser.add_argument('--no-clean', action='store_true',
|
|
|
|
help='do not clean existing artifacts.')
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2018-04-30 01:44:22 +00:00
|
|
|
check_build_parser = subparsers.add_parser(
|
|
|
|
'check_build', help='Checks that fuzzers execute without errors.')
|
|
|
|
_add_engine_args(check_build_parser)
|
|
|
|
_add_sanitizer_args(check_build_parser)
|
|
|
|
_add_environment_args(check_build_parser)
|
|
|
|
check_build_parser.add_argument('project_name', help='name of the project')
|
|
|
|
check_build_parser.add_argument('fuzzer_name', help='name of the fuzzer',
|
|
|
|
nargs='?')
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
run_fuzzer_parser = subparsers.add_parser(
|
|
|
|
'run_fuzzer', help='Run a fuzzer.')
|
|
|
|
_add_engine_args(run_fuzzer_parser)
|
2018-01-15 18:42:16 +00:00
|
|
|
_add_environment_args(run_fuzzer_parser)
|
2017-03-30 20:32:56 +00:00
|
|
|
run_fuzzer_parser.add_argument('project_name', help='name of the project')
|
|
|
|
run_fuzzer_parser.add_argument('fuzzer_name', help='name of the fuzzer')
|
|
|
|
run_fuzzer_parser.add_argument('fuzzer_args', help='arguments to pass to the fuzzer',
|
2017-05-10 21:32:01 +00:00
|
|
|
nargs=argparse.REMAINDER)
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
coverage_parser = subparsers.add_parser(
|
|
|
|
'coverage', help='Run a fuzzer for a while and generate coverage.')
|
|
|
|
coverage_parser.add_argument('--run_time', default=60,
|
|
|
|
help='time in seconds to run fuzzer')
|
|
|
|
coverage_parser.add_argument('project_name', help='name of the project')
|
|
|
|
coverage_parser.add_argument('fuzzer_name', help='name of the fuzzer')
|
|
|
|
coverage_parser.add_argument('fuzzer_args', help='arguments to pass to the fuzzer',
|
2017-05-10 21:32:01 +00:00
|
|
|
nargs=argparse.REMAINDER)
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
reproduce_parser = subparsers.add_parser(
|
|
|
|
'reproduce', help='Reproduce a crash.')
|
2017-05-10 21:32:01 +00:00
|
|
|
reproduce_parser.add_argument('--valgrind', action='store_true',
|
|
|
|
help='run with valgrind')
|
2017-03-30 20:32:56 +00:00
|
|
|
reproduce_parser.add_argument('project_name', help='name of the project')
|
|
|
|
reproduce_parser.add_argument('fuzzer_name', help='name of the fuzzer')
|
|
|
|
reproduce_parser.add_argument('testcase_path', help='path of local testcase')
|
|
|
|
reproduce_parser.add_argument('fuzzer_args', help='arguments to pass to the fuzzer',
|
2017-05-10 21:32:01 +00:00
|
|
|
nargs=argparse.REMAINDER)
|
2018-04-11 17:12:13 +00:00
|
|
|
_add_environment_args(reproduce_parser)
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
shell_parser = subparsers.add_parser(
|
|
|
|
'shell', help='Run /bin/bash in an image.')
|
|
|
|
shell_parser.add_argument('project_name', help='name of the project')
|
2017-04-06 01:13:20 +00:00
|
|
|
_add_engine_args(shell_parser)
|
|
|
|
_add_sanitizer_args(shell_parser)
|
|
|
|
_add_environment_args(shell_parser)
|
2017-03-30 20:32:56 +00:00
|
|
|
|
2017-05-10 21:32:01 +00:00
|
|
|
pull_images_parser = subparsers.add_parser('pull_images',
|
|
|
|
help='Pull base images.')
|
2017-05-09 19:29:38 +00:00
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
if args.command == 'generate':
|
|
|
|
return generate(args)
|
|
|
|
elif args.command == 'build_image':
|
|
|
|
return build_image(args)
|
|
|
|
elif args.command == 'build_fuzzers':
|
|
|
|
return build_fuzzers(args)
|
2018-04-30 01:44:22 +00:00
|
|
|
elif args.command == 'check_build':
|
|
|
|
return check_build(args)
|
2017-03-30 20:32:56 +00:00
|
|
|
elif args.command == 'run_fuzzer':
|
|
|
|
return run_fuzzer(args)
|
|
|
|
elif args.command == 'coverage':
|
|
|
|
return coverage(args)
|
|
|
|
elif args.command == 'reproduce':
|
|
|
|
return reproduce(args)
|
|
|
|
elif args.command == 'shell':
|
|
|
|
return shell(args)
|
2017-05-09 19:29:38 +00:00
|
|
|
elif args.command == 'pull_images':
|
|
|
|
return pull_images(args)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2016-11-04 21:20:41 +00:00
|
|
|
def _is_base_image(image_name):
|
|
|
|
"""Checks if the image name is a base image."""
|
|
|
|
return os.path.exists(os.path.join('infra', 'base-images', image_name))
|
|
|
|
|
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
def _check_project_exists(project_name):
|
|
|
|
"""Checks if a project exists."""
|
|
|
|
if not os.path.exists(os.path.join(OSSFUZZ_DIR, 'projects', project_name)):
|
|
|
|
print(project_name, 'does not exist', file=sys.stderr)
|
2016-09-01 23:32:06 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
def _check_fuzzer_exists(project_name, fuzzer_name):
|
2016-11-04 21:20:41 +00:00
|
|
|
"""Checks if a fuzzer exists."""
|
2017-01-21 22:53:09 +00:00
|
|
|
command = ['docker', 'run', '--rm']
|
|
|
|
command.extend(['-v', '%s:/out' % os.path.join(BUILD_DIR, 'out', project_name)])
|
|
|
|
command.append('ubuntu:16.04')
|
|
|
|
|
|
|
|
command.extend(['/bin/bash', '-c', 'test -f /out/%s' % fuzzer_name])
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(command)
|
|
|
|
except subprocess.CalledProcessError:
|
2016-11-04 21:20:41 +00:00
|
|
|
print(fuzzer_name,
|
|
|
|
'does not seem to exist. Please run build_fuzzers first.',
|
|
|
|
file=sys.stderr)
|
|
|
|
return False
|
2016-09-28 19:44:45 +00:00
|
|
|
|
2016-11-04 21:20:41 +00:00
|
|
|
return True
|
2016-09-28 19:44:45 +00:00
|
|
|
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2017-01-03 18:47:05 +00:00
|
|
|
def _get_absolute_path(path):
|
|
|
|
"""Returns absolute path with user expansion."""
|
|
|
|
return os.path.abspath(os.path.expanduser(path))
|
|
|
|
|
|
|
|
|
2016-11-04 21:20:41 +00:00
|
|
|
def _get_command_string(command):
|
|
|
|
"""Returns a shell escaped command string."""
|
|
|
|
return ' '.join(pipes.quote(part) for part in command)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
|
|
|
|
2017-02-16 23:09:37 +00:00
|
|
|
def _add_engine_args(parser):
|
|
|
|
"""Add common engine args."""
|
|
|
|
parser.add_argument('--engine', default='libfuzzer',
|
2017-10-31 04:22:58 +00:00
|
|
|
choices=['libfuzzer', 'afl', 'honggfuzz', 'none'])
|
2017-02-16 23:09:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _add_sanitizer_args(parser):
|
|
|
|
"""Add common sanitizer args."""
|
|
|
|
parser.add_argument('--sanitizer', default='address',
|
2018-01-30 19:35:32 +00:00
|
|
|
choices=['address', 'memory', 'undefined', 'coverage'])
|
2017-02-16 23:09:37 +00:00
|
|
|
|
|
|
|
|
2017-04-06 01:13:20 +00:00
|
|
|
def _add_environment_args(parser):
|
|
|
|
"""Add common environment args."""
|
|
|
|
parser.add_argument('-e', action='append',
|
|
|
|
help="set environment variable e.g. VAR=value")
|
|
|
|
|
|
|
|
|
2017-05-09 19:29:38 +00:00
|
|
|
def _build_image(image_name, no_cache=False, pull=False):
|
2016-11-04 21:20:41 +00:00
|
|
|
"""Build image."""
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
is_base_image = _is_base_image(image_name)
|
|
|
|
if is_base_image:
|
2017-03-28 21:04:34 +00:00
|
|
|
image_project = 'oss-fuzz-base'
|
2016-11-04 21:20:41 +00:00
|
|
|
dockerfile_dir = os.path.join('infra', 'base-images', image_name)
|
|
|
|
else:
|
2017-03-28 21:04:34 +00:00
|
|
|
image_project = 'oss-fuzz'
|
2016-11-29 19:22:48 +00:00
|
|
|
if not _check_project_exists(image_name):
|
2016-11-04 21:20:41 +00:00
|
|
|
return False
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
dockerfile_dir = os.path.join('projects', image_name)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2016-12-28 01:14:33 +00:00
|
|
|
build_args = []
|
2017-03-30 20:47:38 +00:00
|
|
|
if no_cache:
|
|
|
|
build_args.append('--no-cache')
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
build_args += ['-t', 'gcr.io/%s/%s' % (image_project, image_name), dockerfile_dir]
|
|
|
|
|
2017-05-09 19:29:38 +00:00
|
|
|
return docker_build(build_args, pull=pull)
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def docker_run(run_args, print_output=True):
|
|
|
|
"""Call `docker run`."""
|
2017-10-16 22:54:58 +00:00
|
|
|
command = ['docker', 'run', '--rm', '-i', '--privileged']
|
2017-03-30 20:32:56 +00:00
|
|
|
command.extend(run_args)
|
2016-12-28 01:14:33 +00:00
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
print('Running:', _get_command_string(command))
|
|
|
|
stdout = None
|
|
|
|
if not print_output:
|
|
|
|
stdout = open(os.devnull, 'w')
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(command, stdout=stdout, stderr=subprocess.STDOUT)
|
2018-03-15 22:49:36 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
return e.returncode
|
2017-03-30 20:32:56 +00:00
|
|
|
|
2018-03-15 22:49:36 +00:00
|
|
|
return 0
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def docker_build(build_args, pull=False):
|
|
|
|
"""Call `docker build`."""
|
|
|
|
command = ['docker', 'build']
|
|
|
|
if pull:
|
|
|
|
command.append('--pull')
|
|
|
|
|
|
|
|
command.extend(build_args)
|
2016-09-28 19:44:45 +00:00
|
|
|
print('Running:', _get_command_string(command))
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(command)
|
2016-09-01 23:32:06 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print('docker build failed.', file=sys.stderr)
|
2016-11-04 21:20:41 +00:00
|
|
|
return False
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2016-11-04 21:20:41 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-05-09 19:29:38 +00:00
|
|
|
def docker_pull(image, pull=False):
|
|
|
|
"""Call `docker pull`."""
|
|
|
|
command = ['docker', 'pull', image]
|
|
|
|
print('Running:', _get_command_string(command))
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(command)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print('docker pull failed.', file=sys.stderr)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
def build_image(args):
|
2016-11-04 21:20:41 +00:00
|
|
|
"""Build docker image."""
|
2017-05-10 20:49:09 +00:00
|
|
|
pull = args.pull
|
|
|
|
if not pull:
|
|
|
|
y_or_n = raw_input('Pull latest base images (compiler/runtime)? (y/N): ')
|
|
|
|
pull = y_or_n.lower() == 'y'
|
|
|
|
|
|
|
|
if pull:
|
|
|
|
print('Pulling latest base images...')
|
|
|
|
else:
|
|
|
|
print('Using cached base images...')
|
|
|
|
|
2017-03-30 20:47:38 +00:00
|
|
|
# If build_image is called explicitly, don't use cache.
|
2017-05-10 20:49:09 +00:00
|
|
|
if _build_image(args.project_name, no_cache=True, pull=pull):
|
2016-11-04 21:20:41 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
return 1
|
2016-09-01 23:32:06 +00:00
|
|
|
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
def build_fuzzers(args):
|
2016-09-01 23:32:06 +00:00
|
|
|
"""Build fuzzers."""
|
2018-05-01 04:01:48 +00:00
|
|
|
no_clean = args.no_clean
|
|
|
|
if not no_clean:
|
|
|
|
y_or_n = raw_input(
|
|
|
|
'WARNING: Remove existing build artifacts in /out '
|
|
|
|
'(recommended if build config changed) ? (y/N): ')
|
|
|
|
no_clean = y_or_n.lower() != 'y'
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2018-05-01 04:01:48 +00:00
|
|
|
project_name = args.project_name
|
2017-03-30 21:00:22 +00:00
|
|
|
if not _build_image(args.project_name):
|
2016-09-01 23:32:06 +00:00
|
|
|
return 1
|
|
|
|
|
2018-05-01 04:01:48 +00:00
|
|
|
project_out_dir = os.path.join(BUILD_DIR, 'out', project_name)
|
|
|
|
if no_clean:
|
|
|
|
print('Keeping existing build artifacts as-is (if any).')
|
|
|
|
else:
|
|
|
|
print('Cleaning existing build artifacts.')
|
|
|
|
|
|
|
|
# Clean old and possibly conflicting artifacts in project's out directory.
|
|
|
|
docker_run([
|
|
|
|
'-v', '%s:/out' % project_out_dir,
|
|
|
|
'-t', 'gcr.io/oss-fuzz/%s' % project_name,
|
|
|
|
'/bin/bash', '-c', 'rm -rf /out/*'
|
|
|
|
])
|
|
|
|
|
2017-02-16 23:09:37 +00:00
|
|
|
env = [
|
|
|
|
'FUZZING_ENGINE=' + args.engine,
|
|
|
|
'SANITIZER=' + args.sanitizer
|
|
|
|
]
|
|
|
|
|
2016-12-29 21:08:47 +00:00
|
|
|
if args.e:
|
|
|
|
env += args.e
|
|
|
|
|
2017-01-03 18:47:05 +00:00
|
|
|
command = (
|
|
|
|
['docker', 'run', '--rm', '-i', '--cap-add', 'SYS_PTRACE'] +
|
|
|
|
sum([['-e', v] for v in env], [])
|
|
|
|
)
|
|
|
|
if args.source_path:
|
|
|
|
command += [
|
|
|
|
'-v',
|
|
|
|
'%s:/src/%s' % (_get_absolute_path(args.source_path), args.project_name)
|
|
|
|
]
|
|
|
|
command += [
|
2018-05-01 04:01:48 +00:00
|
|
|
'-v', '%s:/out' % project_out_dir,
|
2017-01-03 18:47:05 +00:00
|
|
|
'-v', '%s:/work' % os.path.join(BUILD_DIR, 'work', project_name),
|
2017-03-16 02:10:01 +00:00
|
|
|
'-t', 'gcr.io/oss-fuzz/%s' % project_name
|
2017-01-03 18:47:05 +00:00
|
|
|
]
|
2016-09-28 19:44:45 +00:00
|
|
|
|
|
|
|
print('Running:', _get_command_string(command))
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(command)
|
2016-09-01 23:32:06 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print('fuzzers build failed.', file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-04-30 01:44:22 +00:00
|
|
|
def check_build(args):
|
|
|
|
"""Checks that fuzzers in the container execute without errors."""
|
|
|
|
if not _check_project_exists(args.project_name):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if (args.fuzzer_name and
|
|
|
|
not _check_fuzzer_exists(args.project_name, args.fuzzer_name)):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
env = [
|
|
|
|
'FUZZING_ENGINE=' + args.engine,
|
|
|
|
'SANITIZER=' + args.sanitizer
|
|
|
|
]
|
|
|
|
if args.e:
|
|
|
|
env += args.e
|
|
|
|
|
|
|
|
run_args = sum([['-e', v] for v in env], []) + [
|
|
|
|
'-v', '%s:/out' % os.path.join(BUILD_DIR, 'out', args.project_name),
|
|
|
|
'-t', 'gcr.io/oss-fuzz-base/base-runner'
|
|
|
|
]
|
|
|
|
|
|
|
|
if args.fuzzer_name:
|
|
|
|
run_args += [
|
|
|
|
'bad_build_check',
|
|
|
|
os.path.join('/out', args.fuzzer_name)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
run_args.append('test_all')
|
|
|
|
|
|
|
|
exit_code = docker_run(run_args)
|
|
|
|
if exit_code == 0:
|
|
|
|
print('Check build passed.')
|
|
|
|
else:
|
|
|
|
print('Check build failed.')
|
|
|
|
|
|
|
|
return exit_code
|
|
|
|
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
def run_fuzzer(args):
|
2016-09-01 23:32:06 +00:00
|
|
|
"""Runs a fuzzer in the container."""
|
2016-11-29 19:22:48 +00:00
|
|
|
if not _check_project_exists(args.project_name):
|
2016-09-01 23:32:06 +00:00
|
|
|
return 1
|
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
if not _check_fuzzer_exists(args.project_name, args.fuzzer_name):
|
2016-11-04 21:20:41 +00:00
|
|
|
return 1
|
|
|
|
|
2017-02-16 23:09:37 +00:00
|
|
|
env = ['FUZZING_ENGINE=' + args.engine]
|
2018-01-15 18:42:16 +00:00
|
|
|
if args.e:
|
|
|
|
env += args.e
|
2017-02-16 23:09:37 +00:00
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
run_args = sum([['-e', v] for v in env], []) + [
|
2016-11-29 19:22:48 +00:00
|
|
|
'-v', '%s:/out' % os.path.join(BUILD_DIR, 'out', args.project_name),
|
2017-03-22 19:12:51 +00:00
|
|
|
'-t', 'gcr.io/oss-fuzz-base/base-runner',
|
2016-11-02 23:06:02 +00:00
|
|
|
'run_fuzzer',
|
2017-01-27 02:27:42 +00:00
|
|
|
args.fuzzer_name,
|
2016-09-28 19:44:45 +00:00
|
|
|
] + args.fuzzer_args
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2018-03-15 22:49:36 +00:00
|
|
|
return docker_run(run_args)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2017-01-03 18:47:05 +00:00
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
def coverage(args):
|
2016-10-14 01:07:57 +00:00
|
|
|
"""Runs a fuzzer in the container."""
|
2016-11-29 19:22:48 +00:00
|
|
|
if not _check_project_exists(args.project_name):
|
2016-10-14 01:07:57 +00:00
|
|
|
return 1
|
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
if not _check_fuzzer_exists(args.project_name, args.fuzzer_name):
|
2016-11-04 21:20:41 +00:00
|
|
|
return 1
|
|
|
|
|
2016-10-14 01:07:57 +00:00
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
run_args = [
|
2017-05-31 19:44:25 +00:00
|
|
|
'-e', 'FUZZING_ENGINE=libfuzzer',
|
|
|
|
'-e', 'ASAN_OPTIONS=coverage_dir=/cov',
|
|
|
|
'-e', 'MSAN_OPTIONS=coverage_dir=/cov',
|
|
|
|
'-e', 'UBSAN_OPTIONS=coverage_dir=/cov',
|
2016-11-29 19:22:48 +00:00
|
|
|
'-v', '%s:/out' % os.path.join(BUILD_DIR, 'out', args.project_name),
|
2016-10-14 01:07:57 +00:00
|
|
|
'-v', '%s:/cov' % temp_dir,
|
|
|
|
'-w', '/cov',
|
2017-03-22 19:12:51 +00:00
|
|
|
'-t', 'gcr.io/oss-fuzz-base/base-runner',
|
2017-05-31 19:44:25 +00:00
|
|
|
'run_fuzzer',
|
|
|
|
args.fuzzer_name,
|
2017-01-03 21:28:28 +00:00
|
|
|
'-dump_coverage=1',
|
2016-10-14 01:07:57 +00:00
|
|
|
'-max_total_time=%s' % args.run_time
|
|
|
|
] + args.fuzzer_args
|
|
|
|
|
2017-05-31 19:44:25 +00:00
|
|
|
print('This may take a while (running your fuzzer for %s seconds)...' %
|
2016-11-29 23:10:58 +00:00
|
|
|
args.run_time)
|
2017-03-30 20:32:56 +00:00
|
|
|
docker_run(run_args, print_output=False)
|
2016-10-14 01:07:57 +00:00
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
run_args = [
|
2017-03-30 20:47:38 +00:00
|
|
|
'-v', '%s:/out' % os.path.join(BUILD_DIR, 'out', args.project_name),
|
|
|
|
'-v', '%s:/cov' % temp_dir,
|
|
|
|
'-w', '/cov',
|
|
|
|
'-p', '8001:8001',
|
|
|
|
'-t', 'gcr.io/oss-fuzz/%s' % args.project_name,
|
|
|
|
'coverage_report', '/out/%s' % args.fuzzer_name,
|
2016-10-14 01:07:57 +00:00
|
|
|
]
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
docker_run(run_args)
|
2016-10-14 01:07:57 +00:00
|
|
|
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2017-04-05 15:56:39 +00:00
|
|
|
def reproduce(args):
|
2017-01-03 18:47:05 +00:00
|
|
|
"""Reproduces a testcase in the container."""
|
|
|
|
if not _check_project_exists(args.project_name):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if not _check_fuzzer_exists(args.project_name, args.fuzzer_name):
|
|
|
|
return 1
|
|
|
|
|
2017-05-10 21:32:01 +00:00
|
|
|
debugger = ''
|
|
|
|
env = []
|
|
|
|
image_name = 'base-runner'
|
|
|
|
|
|
|
|
if args.valgrind:
|
|
|
|
debugger = 'valgrind --tool=memcheck --track-origins=yes --leak-check=full'
|
|
|
|
|
|
|
|
if debugger:
|
|
|
|
image_name = 'base-runner-debug'
|
|
|
|
env += ['DEBUGGER=' + debugger]
|
|
|
|
|
2018-04-11 17:12:13 +00:00
|
|
|
if args.e:
|
|
|
|
env += args.e
|
|
|
|
|
2017-05-10 21:32:01 +00:00
|
|
|
run_args = sum([['-e', v] for v in env], []) + [
|
2017-01-03 18:47:05 +00:00
|
|
|
'-v', '%s:/out' % os.path.join(BUILD_DIR, 'out', args.project_name),
|
|
|
|
'-v', '%s:/testcase' % _get_absolute_path(args.testcase_path),
|
2017-05-10 21:32:01 +00:00
|
|
|
'-t', 'gcr.io/oss-fuzz-base/%s' % image_name,
|
2017-01-03 18:47:05 +00:00
|
|
|
'reproduce',
|
2017-01-27 02:27:42 +00:00
|
|
|
args.fuzzer_name,
|
2017-01-03 19:07:58 +00:00
|
|
|
'-runs=100',
|
|
|
|
] + args.fuzzer_args
|
2017-01-03 18:47:05 +00:00
|
|
|
|
2018-03-15 22:49:36 +00:00
|
|
|
return docker_run(run_args)
|
2017-01-03 18:47:05 +00:00
|
|
|
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
def generate(args):
|
2016-11-29 19:22:48 +00:00
|
|
|
"""Generate empty project files."""
|
2017-05-12 22:58:13 +00:00
|
|
|
if not VALID_PROJECT_NAME_REGEX.match(args.project_name):
|
|
|
|
print('Invalid project name.', file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
dir = os.path.join('projects', args.project_name)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
|
|
|
try:
|
2016-10-25 02:39:13 +00:00
|
|
|
os.mkdir(dir)
|
2016-11-04 15:04:36 +00:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2016-10-25 02:39:13 +00:00
|
|
|
print(dir, 'already exists.', file=sys.stderr)
|
2016-09-01 23:32:06 +00:00
|
|
|
return 1
|
|
|
|
|
2016-11-04 21:20:41 +00:00
|
|
|
print('Writing new files to', dir)
|
|
|
|
|
2016-11-17 17:49:34 +00:00
|
|
|
template_args = {
|
2018-01-12 20:49:40 +00:00
|
|
|
'project_name' : args.project_name,
|
|
|
|
'year': datetime.datetime.now().year
|
2016-11-29 23:10:58 +00:00
|
|
|
}
|
2016-11-29 19:25:33 +00:00
|
|
|
with open(os.path.join(dir, 'project.yaml'), 'w') as f:
|
|
|
|
f.write(templates.PROJECT_YAML_TEMPLATE % template_args)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2016-10-25 02:39:13 +00:00
|
|
|
with open(os.path.join(dir, 'Dockerfile'), 'w') as f:
|
2016-11-17 17:49:34 +00:00
|
|
|
f.write(templates.DOCKER_TEMPLATE % template_args)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2016-10-25 02:39:13 +00:00
|
|
|
build_sh_path = os.path.join(dir, 'build.sh')
|
2016-09-02 16:57:31 +00:00
|
|
|
with open(build_sh_path, 'w') as f:
|
2016-11-17 17:49:34 +00:00
|
|
|
f.write(templates.BUILD_TEMPLATE % template_args)
|
2016-09-01 23:32:06 +00:00
|
|
|
|
2016-11-04 15:03:46 +00:00
|
|
|
os.chmod(build_sh_path, 0o755)
|
2016-09-01 23:32:06 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2017-03-30 20:32:56 +00:00
|
|
|
def shell(args):
|
2016-09-28 20:23:30 +00:00
|
|
|
"""Runs a shell within a docker image."""
|
2017-03-30 21:00:22 +00:00
|
|
|
if not _build_image(args.project_name):
|
2016-09-28 20:23:30 +00:00
|
|
|
return 1
|
|
|
|
|
2017-04-06 01:13:20 +00:00
|
|
|
env = [
|
|
|
|
'FUZZING_ENGINE=' + args.engine,
|
|
|
|
'SANITIZER=' + args.sanitizer
|
|
|
|
]
|
|
|
|
|
|
|
|
if args.e:
|
|
|
|
env += args.e
|
|
|
|
|
2017-10-16 22:54:58 +00:00
|
|
|
if _is_base_image(args.project_name):
|
|
|
|
image_project = 'oss-fuzz-base'
|
|
|
|
out_dir = os.path.join(BUILD_DIR, 'out');
|
|
|
|
else:
|
|
|
|
image_project = 'oss-fuzz'
|
|
|
|
out_dir = os.path.join(BUILD_DIR, 'out', args.project_name)
|
|
|
|
|
2017-04-06 01:13:20 +00:00
|
|
|
run_args = sum([['-e', v] for v in env], []) + [
|
2017-10-16 22:54:58 +00:00
|
|
|
'-v', '%s:/out' % out_dir,
|
2017-03-30 20:32:56 +00:00
|
|
|
'-v', '%s:/work' % os.path.join(BUILD_DIR, 'work', args.project_name),
|
2017-10-16 22:54:58 +00:00
|
|
|
'-t', 'gcr.io/%s/%s' % (image_project, args.project_name),
|
2017-03-30 20:32:56 +00:00
|
|
|
'/bin/bash'
|
2016-09-28 20:23:30 +00:00
|
|
|
]
|
2017-03-30 20:32:56 +00:00
|
|
|
|
|
|
|
docker_run(run_args)
|
2017-05-09 19:29:38 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def pull_images(args):
|
|
|
|
"""Pull base images."""
|
|
|
|
for base_image in BASE_IMAGES:
|
|
|
|
if not docker_pull(base_image):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
2016-09-28 20:23:30 +00:00
|
|
|
|
|
|
|
|
2016-09-01 23:32:06 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|