-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting up Fuzz Testing with LibFuzzer and initial Server Fuzz Test #263
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2b4fe1d
Adding "fuzz" target to run Fuzz Tests with LibFuzzer
alexw91 8e3787b
Allowing install_prlimit.sh to print error message
alexw91 f83fbfb
Add workaround for https://github.com/travis-ci/travis-ci/issues/2607
alexw91 1e93c42
Increasing timeout for individual fuzz tests since Travis Build Hardw…
alexw91 f00c50e
Skipping Fuzzing on OS X for now. I can't get it working, lots of Cla…
alexw91 6143722
Add pipe to /dev/null back now that prlimit install script works again
alexw91 35970a8
Rewriting Fuzz Tests to use LD_PRELOAD function overrides instead of …
alexw91 a9755f6
clean up LD_PRELOAD Makefile
alexw91 3394899
moving opening curly brace to its own line, and using snake_case inst…
alexw91 869d7bf
Adding Clean up logic for negative fuzz tests
alexw91 60c7aaf
Updating .gitignore
alexw91 f0f5105
Fixing Comment style and spacing
alexw91 1cc787c
Passing PATH env variable to sudo
alexw91 881177f
adding missing GUARD to global_overrides LD_PRELOAD and fixing C++ st…
alexw91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/bin/bash | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
# | ||
|
||
set -e | ||
|
||
usage() { | ||
echo "install_clang.sh download_dir install_dir travis_platform" | ||
exit 1 | ||
} | ||
|
||
if [ "$#" -ne "3" ]; then | ||
usage | ||
fi | ||
|
||
CLANG_DOWNLOAD_DIR=$1 | ||
CLANG_INSTALL_DIR=$2 | ||
PLATFORM=$3 | ||
|
||
mkdir -p $CLANG_DOWNLOAD_DIR | ||
cd $CLANG_DOWNLOAD_DIR | ||
|
||
if [ "$PLATFORM" == "linux" ]; then | ||
# The Certificate used by chromium.googlesource.com is not in the default CA | ||
# list supported by git/curl on Ubuntu, but the certificate is in the | ||
# ca-certificates.crt file in Ubuntu, so set this env variable so that it is | ||
# picked up by git. | ||
export SSL_CERT_FILE=/usr/lib/ssl/certs/ca-certificates.crt | ||
fi | ||
|
||
GIT_CURL_VERBOSE=1 | ||
echo "Downloading Clang..." | ||
git clone https://chromium.googlesource.com/chromium/src/tools/clang | ||
|
||
echo "Updating Clang..." | ||
$CLANG_DOWNLOAD_DIR/clang/scripts/update.py | ||
|
||
# "third_party" directory is created above $CLANG_DOWNLOAD_DIR after running | ||
# update, move it into $CLANG_DOWNLOAD_DIR once update is complete. | ||
mv ../third_party $CLANG_DOWNLOAD_DIR | ||
|
||
echo "Installed Clang Version: " | ||
$CLANG_DOWNLOAD_DIR/third_party/llvm-build/Release+Asserts/bin/clang --version | ||
|
||
ln -s $CLANG_DOWNLOAD_DIR/third_party/llvm-build/Release+Asserts/ $CLANG_INSTALL_DIR | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/bin/bash | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
# | ||
set -e | ||
|
||
usage() { | ||
echo "install_libFuzzer.sh download_dir install_dir travis_platform" | ||
exit 1 | ||
} | ||
|
||
if [ "$#" -ne "3" ]; then | ||
usage | ||
fi | ||
|
||
LIBFUZZER_DOWNLOAD_DIR=$1 | ||
LIBFUZZER_INSTALL_DIR=$2 | ||
PLATFORM=$3 | ||
|
||
mkdir -p $LIBFUZZER_DOWNLOAD_DIR | ||
cd $LIBFUZZER_DOWNLOAD_DIR | ||
|
||
git clone https://chromium.googlesource.com/chromium/llvm-project/llvm/lib/Fuzzer | ||
|
||
echo "Compiling LibFuzzer..." | ||
clang++ -c -g -v -O2 -lstdc++ -std=c++11 Fuzzer/*.cpp -IFuzzer | ||
ar ruv libFuzzer.a Fuzzer*.o | ||
|
||
echo "Copying libFuzzer.a to $LIBFUZZER_INSTALL_DIR" | ||
cp libFuzzer.a $LIBFUZZER_INSTALL_DIR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file 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. | ||
# | ||
|
||
SRCS=$(wildcard *.c) | ||
OVERRIDES=$(SRCS:.c=) | ||
|
||
.PHONY : all | ||
all : $(OVERRIDES) | ||
|
||
include ../../../s2n.mk | ||
|
||
CRUFT += $(wildcard *.so) | ||
|
||
LD_PRELOAD_CFLAGS = -Wno-unreachable-code -O0 -I$(LIBCRYPTO_ROOT)/include/ -I../../../ -I../../../api/ | ||
|
||
$(OVERRIDES):: | ||
# Don't include Sanitizer/Fuzz compiler flags since when the LD_PRELOAD shared object is Preloaded, the Sanitizer init | ||
# functions won't have been loaded yet, causing undefined symbol errors. | ||
${CC} ${DEFAULT_CFLAGS} ${DEBUG_CFLAGS} ${LD_PRELOAD_CFLAGS} -shared -fPIC [email protected] -o [email protected] -ldl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
|
||
#define _GNU_SOURCE | ||
|
||
#include <stdio.h> | ||
#include <dlfcn.h> | ||
|
||
#include "crypto/s2n_drbg.h" | ||
|
||
#include "stuffer/s2n_stuffer.h" | ||
|
||
#include "utils/s2n_safety.h" | ||
#include "utils/s2n_random.h" | ||
|
||
int s2n_drbg_generate(struct s2n_drbg *drbg, struct s2n_blob *blob) { | ||
|
||
/* If fuzzing, only generate "fake" random numbers in order to ensure that fuzz tests are deterministic and repeatable. | ||
* This function should generate non-zero values since this function may be called repeatedly at startup until a | ||
* non-zero value is generated. | ||
*/ | ||
GUARD(s2n_get_urandom_data(blob)); | ||
drbg->bytes_used += blob->size; | ||
return 0; | ||
} | ||
|
||
int s2n_stuffer_send_to_fd(struct s2n_stuffer *stuffer, int wfd, uint32_t len) | ||
{ | ||
/* Override the original s2n_stuffer_send_to_fd to check if the write file descriptor is -1, and if so, skip | ||
* writing anything. This is to speed up fuzz tests that write unnecessary data that is never actually read. | ||
*/ | ||
if(wfd == -1){ | ||
stuffer->read_cursor += len; | ||
return len; | ||
} | ||
|
||
/* Otherwise, call the original s2n_stuffer_send_to_fd() */ | ||
typedef int (*orig_s2n_stuffer_send_to_fd_func_type)(struct s2n_stuffer *stuffer, int wfd, uint32_t len); | ||
orig_s2n_stuffer_send_to_fd_func_type orig_s2n_stuffer_send_to_fd; | ||
orig_s2n_stuffer_send_to_fd = (orig_s2n_stuffer_send_to_fd_func_type) dlsym(RTLD_NEXT, "s2n_stuffer_send_to_fd"); | ||
return orig_s2n_stuffer_send_to_fd(stuffer, wfd, len); | ||
} | ||
|
||
int s2n_get_urandom_data(struct s2n_blob *blob){ | ||
|
||
/* If fuzzing, only generate "fake" random numbers in order to ensure that fuzz tests are deterministic and repeatable. | ||
* This function should generate non-zero values since this function may be called repeatedly at startup until a | ||
* non-zero value is generated. | ||
*/ | ||
for(int i=0; i < blob->size; i++){ | ||
blob->data[i] = 4; /* Fake RNG. Chosen by fair dice roll. https://xkcd.com/221/ */ | ||
} | ||
return 0; | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/fuzz/LD_PRELOAD/s2n_memory_leak_negative_test_overrides.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
|
||
#include "utils/s2n_blob.h" | ||
|
||
int s2n_free(struct s2n_blob *b) | ||
{ | ||
/* This will cause large amounts of memory leaks. This should be caught by LibFuzzer as a negative fuzz test to | ||
* ensure that LibFuzzer will catch these memory leaks. | ||
*/ | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd separate each section, s2n-style.