2021-03-12 15:27:07 +00:00
|
|
|
# Copyright 2021 Google LLC
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
"""Module for dealing with docker."""
|
2021-07-27 19:11:27 +00:00
|
|
|
import logging
|
2021-03-12 15:27:07 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2022-07-12 13:50:10 +00:00
|
|
|
import uuid
|
2021-03-12 15:27:07 +00:00
|
|
|
|
|
|
|
# pylint: disable=wrong-import-position,import-error
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
2021-08-05 00:04:55 +00:00
|
|
|
import constants
|
2021-03-12 15:27:07 +00:00
|
|
|
import utils
|
2022-01-13 19:27:53 +00:00
|
|
|
import environment
|
2021-03-12 15:27:07 +00:00
|
|
|
|
|
|
|
BASE_BUILDER_TAG = 'gcr.io/oss-fuzz-base/base-builder'
|
|
|
|
PROJECT_TAG_PREFIX = 'gcr.io/oss-fuzz/'
|
|
|
|
|
2021-08-05 20:27:24 +00:00
|
|
|
# Default fuzz configuration.
|
2021-06-23 14:30:11 +00:00
|
|
|
_DEFAULT_DOCKER_RUN_ARGS = [
|
2022-06-08 19:15:27 +00:00
|
|
|
'-e', 'FUZZING_ENGINE=' + constants.DEFAULT_ENGINE, '-e', 'CIFUZZ=True'
|
2021-06-23 14:30:11 +00:00
|
|
|
]
|
|
|
|
|
2022-07-12 13:50:10 +00:00
|
|
|
UNIQUE_ID_SUFFIX = '-' + uuid.uuid4().hex
|
|
|
|
|
|
|
|
# TODO(metzman): Make run_fuzzers able to delete this image.
|
|
|
|
EXTERNAL_PROJECT_IMAGE = 'external-cfl-project' + UNIQUE_ID_SUFFIX
|
2021-07-21 05:32:32 +00:00
|
|
|
|
2021-06-23 14:30:11 +00:00
|
|
|
_DEFAULT_DOCKER_RUN_COMMAND = [
|
|
|
|
'docker',
|
|
|
|
'run',
|
|
|
|
'--rm',
|
|
|
|
'--privileged',
|
|
|
|
]
|
|
|
|
|
2021-03-12 15:27:07 +00:00
|
|
|
|
2021-08-04 21:19:22 +00:00
|
|
|
def get_docker_env_vars(env_mapping):
|
|
|
|
"""Returns a list of docker arguments that sets each key in |env_mapping| as
|
|
|
|
an env var and the value of that key in |env_mapping| as the value."""
|
|
|
|
env_var_args = []
|
|
|
|
for env_var, env_var_val in env_mapping.items():
|
|
|
|
env_var_args.extend(['-e', f'{env_var}={env_var_val}'])
|
|
|
|
return env_var_args
|
|
|
|
|
|
|
|
|
2021-03-12 15:27:07 +00:00
|
|
|
def get_project_image_name(project):
|
|
|
|
"""Returns the name of the project builder image for |project_name|."""
|
2022-07-12 13:50:10 +00:00
|
|
|
# TODO(jonathanmetzman): We may need unique names to support parallel fuzzing
|
|
|
|
# for CIFuzz (like CFL supports). Don't do this for now because no one has
|
|
|
|
# asked for it and build_specified_commit would need to be modified to support
|
|
|
|
# this.
|
2021-07-21 05:32:32 +00:00
|
|
|
if project:
|
|
|
|
return PROJECT_TAG_PREFIX + project
|
|
|
|
|
|
|
|
return EXTERNAL_PROJECT_IMAGE
|
2021-03-12 15:27:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def delete_images(images):
|
|
|
|
"""Deletes |images|."""
|
|
|
|
command = ['docker', 'rmi', '-f'] + images
|
|
|
|
utils.execute(command)
|
|
|
|
utils.execute(['docker', 'builder', 'prune', '-f'])
|
2021-06-18 17:26:36 +00:00
|
|
|
|
|
|
|
|
2021-08-04 00:59:17 +00:00
|
|
|
def get_base_docker_run_args(workspace,
|
2021-08-05 00:04:55 +00:00
|
|
|
sanitizer=constants.DEFAULT_SANITIZER,
|
2021-10-05 13:01:38 +00:00
|
|
|
language=constants.DEFAULT_LANGUAGE,
|
2022-06-08 19:15:27 +00:00
|
|
|
architecture=constants.DEFAULT_ARCHITECTURE,
|
2021-10-05 13:01:38 +00:00
|
|
|
docker_in_docker=False):
|
2021-06-23 14:30:11 +00:00
|
|
|
"""Returns arguments that should be passed to every invocation of 'docker
|
|
|
|
run'."""
|
|
|
|
docker_args = _DEFAULT_DOCKER_RUN_ARGS.copy()
|
2021-08-04 21:19:22 +00:00
|
|
|
env_mapping = {
|
|
|
|
'SANITIZER': sanitizer,
|
2022-06-08 19:15:27 +00:00
|
|
|
'ARCHITECTURE': architecture,
|
2021-08-04 21:19:22 +00:00
|
|
|
'FUZZING_LANGUAGE': language,
|
|
|
|
'OUT': workspace.out
|
|
|
|
}
|
|
|
|
docker_args += get_docker_env_vars(env_mapping)
|
2022-01-13 19:27:53 +00:00
|
|
|
docker_container = environment.get('CFL_CONTAINER_ID',
|
|
|
|
utils.get_container_name())
|
2021-07-27 19:11:27 +00:00
|
|
|
logging.info('Docker container: %s.', docker_container)
|
2021-10-05 13:01:38 +00:00
|
|
|
if docker_container and not docker_in_docker:
|
2021-06-30 14:34:42 +00:00
|
|
|
# Don't map specific volumes if in a docker container, it breaks when
|
|
|
|
# running a sibling container.
|
|
|
|
docker_args += ['--volumes-from', docker_container]
|
2021-06-23 14:30:11 +00:00
|
|
|
else:
|
2021-06-30 14:34:42 +00:00
|
|
|
docker_args += _get_args_mapping_host_path_to_container(workspace.workspace)
|
2021-06-23 14:30:11 +00:00
|
|
|
return docker_args, docker_container
|
|
|
|
|
|
|
|
|
2021-08-04 00:59:17 +00:00
|
|
|
def get_base_docker_run_command(workspace,
|
2021-08-05 00:04:55 +00:00
|
|
|
sanitizer=constants.DEFAULT_SANITIZER,
|
2021-10-05 13:01:38 +00:00
|
|
|
language=constants.DEFAULT_LANGUAGE,
|
2022-06-08 19:15:27 +00:00
|
|
|
architecture=constants.DEFAULT_ARCHITECTURE,
|
2021-10-05 13:01:38 +00:00
|
|
|
docker_in_docker=False):
|
2021-06-23 14:30:11 +00:00
|
|
|
"""Returns part of the command that should be used everytime 'docker run' is
|
|
|
|
invoked."""
|
|
|
|
docker_args, docker_container = get_base_docker_run_args(
|
2022-06-08 19:15:27 +00:00
|
|
|
workspace,
|
|
|
|
sanitizer,
|
|
|
|
language,
|
|
|
|
architecture,
|
|
|
|
docker_in_docker=docker_in_docker)
|
2021-06-23 14:30:11 +00:00
|
|
|
command = _DEFAULT_DOCKER_RUN_COMMAND.copy() + docker_args
|
|
|
|
return command, docker_container
|
|
|
|
|
|
|
|
|
2021-06-30 14:34:42 +00:00
|
|
|
def _get_args_mapping_host_path_to_container(host_path, container_path=None):
|
2021-06-23 14:30:11 +00:00
|
|
|
"""Get arguments to docker run that will map |host_path| a path on the host to
|
|
|
|
a path in the container. If |container_path| is specified, that path is mapped
|
|
|
|
to. If not, then |host_path| is mapped to itself in the container."""
|
2021-06-30 14:34:42 +00:00
|
|
|
# WARNING: Do not use this function when running in production (and
|
|
|
|
# --volumes-from) is used for mapping volumes. It will break production.
|
2021-06-23 14:30:11 +00:00
|
|
|
container_path = host_path if container_path is None else container_path
|
|
|
|
return ['-v', f'{host_path}:{container_path}']
|