Skip to content

Commit b958d28

Browse files
committed
moved advanced examples out to a new directory
1 parent 8ab143a commit b958d28

16 files changed

+123
-36
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ include(googletest)
1717
find_package(cifuzz NO_SYSTEM_ENVIRONMENT_PATH)
1818
enable_fuzz_testing()
1919

20-
add_subdirectory(src/explore_me)
20+
add_subdirectory(src/simple_examples)
2121
add_subdirectory(src/automotive)
22+
add_subdirectory(src/advanced_examples)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ issues in large code bases such as Google Chrome.
1616
In this example, we demonstrate how you can use CI Fuzz to integrate fuzz testing into your
1717
C/C++ projects. The example project uses [CMake](https://cmake.org/) as the build system and contains
1818
the following three use cases:
19-
* [Simple Checks Example](src/explore_me/explore_me.cpp#L10):
19+
* [Simple Checks Example](src/simple_examples/explore_me.cpp#L10):
2020
A simple example that triggers a buffer over when the input parameters satisfy certain criteria.
2121
We show that CI Fuzz can quickly generate a test case that trigger this bug.
22-
* [Complex Checks Example](src/explore_me/explore_me.cpp#L22):
22+
* [Complex Checks Example](src/simple_examples/explore_me.cpp#L22):
2323
A more complex example that triggers a use-after-free bug when the input parameters satisfy
2424
certain criteria. In this example, the checks are more complex and involve Base64 encoding
2525
and XORing with constant value, making it more challenging to find the correct combination of
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
set(OPENSSL_USE_STATIC_LIBS TRUE)
2+
find_package(OpenSSL REQUIRED)
3+
4+
add_library(explore_me_advanced
5+
explore_me.cpp
6+
)
7+
8+
target_include_directories(explore_me_advanced PRIVATE
9+
${CMAKE_CURRENT_SOURCE_DIR}
10+
${OpenSSL_INCLUDE_DIR}
11+
)
12+
13+
target_link_libraries(explore_me_advanced
14+
OpenSSL::Crypto
15+
)
16+
17+
foreach(TestType IN ITEMS
18+
structured_input_checks
19+
custom_mutator_example_checks
20+
)
21+
22+
add_executable(${TestType}_test
23+
${TestType}_test.cpp
24+
)
25+
26+
target_include_directories(${TestType}_test PRIVATE
27+
${CIFUZZ_INCLUDE_DIR}
28+
)
29+
30+
target_link_libraries(${TestType}_test
31+
explore_me_advanced
32+
${GTEST_BOTH_LIBRARIES}
33+
)
34+
35+
add_test(explore_me.${TestType} ${TestType}_test)
36+
37+
add_fuzz_test(${TestType}_fuzz_test
38+
${TestType}_test.cpp
39+
)
40+
41+
target_link_libraries(${TestType}_fuzz_test
42+
explore_me_advanced
43+
${GTEST_BOTH_LIBRARIES}
44+
)
45+
endforeach(TestType )

src/explore_me/custom_mutator_example_checks_test.cpp renamed to src/advanced_examples/custom_mutator_example_checks_test.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,28 @@ FUZZ_TEST(const uint8_t *data, size_t size) {
3131
}
3232

3333

34-
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *data, size_t size,
35-
size_t maxSize, unsigned int seed) {
34+
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
35+
size_t MaxSize, unsigned int Seed) {
3636
std::cout << "In custom mutator.\n";
3737

38-
FuzzedDataProvider fdp(data, size);
38+
FuzzedDataProvider fdp(Data, Size);
3939
long a = fdp.ConsumeIntegral<long>();
4040
long b = fdp.ConsumeIntegral<long>();
4141
std::string tempC = fdp.ConsumeRemainingBytesAsString();
42-
size_t c_size= strlen(tempC.c_str()) +1;
42+
size_t c_size = strlen(tempC.c_str()) +1;
4343
char* c = (char*) malloc(c_size);
4444
strncpy(c, tempC.c_str(), c_size);
4545
SpecialRequirementsStruct specialRequirementsStruct = (SpecialRequirementsStruct) {
4646
.a= a, .b=b, .c_size=c_size, .c= c
4747
};
4848
size_t size1 = sizeof(specialRequirementsStruct);
4949

50-
if (maxSize >= size1) {
51-
free(data);
52-
data = (uint8_t*) malloc (size1);
53-
std::memcpy(data, &specialRequirementsStruct, size1);
50+
if (MaxSize >= size1) {
51+
free(Data);
52+
Data = (uint8_t*) malloc (size1);
53+
std::memcpy(Data, &specialRequirementsStruct, size1);
5454
return sizeof(specialRequirementsStruct);
5555
} else {
56-
return maxSize;
56+
return MaxSize;
5757
}
58-
59-
60-
61-
62-
63-
6458
}
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <cstring>
22

33
#include "explore_me.h"
4-
#include "utils.h"
54

65
static long insecureEncrypt(long input);
76
static void trigger_global_buffer_overflow(const std::string &c);
@@ -21,18 +20,8 @@ void ExploreSimpleChecks(int a, int b, std::string c) {
2120
}
2221
}
2322

24-
void ExploreComplexChecks(long a, long b, std::string c) {
25-
if (EncodeBase64(c) == "SGV5LCB3ZWw=") {
26-
if (insecureEncrypt(a) == 0x4e9e91e6677cfff3L) {
27-
if (insecureEncrypt(b) == 0x4f8b9fb34431d9d3L) {
28-
trigger_use_after_free();
29-
}
30-
}
31-
}
32-
}
33-
3423
void ExploreStructuredInputChecks(InputStruct inputStruct){
35-
if (EncodeBase64(inputStruct.c) == "SGV5LCB3ZWw=") {
24+
if (inputStruct.c == "Attacker") {
3625
if (insecureEncrypt(inputStruct.a) == 0x4e9e91e6677cfff3L) {
3726
if (insecureEncrypt(inputStruct.b) == 0x4f8b9fb34431d9d3L) {
3827
trigger_double_free();
@@ -42,6 +31,7 @@ void ExploreStructuredInputChecks(InputStruct inputStruct){
4231
}
4332

4433
void ExploreCustomMutatorExampleChecks(SpecialRequirementsStruct* specialRequirementsStruct){
34+
printf("Hello!\n");
4535
strncpy(specialRequirementsStruct->c, "Hello\0", specialRequirementsStruct->c_size);
4636

4737
if (insecureEncrypt(specialRequirementsStruct->a) == 0x4e9e91e6677cfff3L) {
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ struct SpecialRequirementsStruct {
2020
char* c;
2121
};
2222

23-
void ExploreSimpleChecks(int a, int b, std::string c);
24-
25-
void ExploreComplexChecks(long a, long b, std::string c);
26-
2723
void ExploreStructuredInputChecks(InputStruct inputStrut);
2824

2925
void ExploreCustomMutatorExampleChecks(SpecialRequirementsStruct* specialRequirementsStruct);
File renamed without changes.

src/explore_me/custom_mutator_example_checks_fuzz_test_inputs/brilliant_caterpillar-crash-da39a3ee5e6b4b0d3255bfef95601890afd80709

Whitespace-only changes.

src/explore_me/custom_mutator_example_checks_fuzz_test_inputs/tough_pigeon-crash-da39a3ee5e6b4b0d3255bfef95601890afd80709

Whitespace-only changes.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ target_link_libraries(explore_me
1818
foreach(TestType IN ITEMS
1919
simple_checks
2020
complex_checks
21-
structured_input_checks
22-
custom_mutator_example_checks
2321
)
2422

2523
add_executable(${TestType}_test

0 commit comments

Comments
 (0)