Skip to content

Commit 9f8bc7c

Browse files
committed
added a structured inputs example
1 parent 3e1bca5 commit 9f8bc7c

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

src/explore_me/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ target_link_libraries(explore_me
1818
foreach(TestType IN ITEMS
1919
simple_checks
2020
complex_checks
21+
structured_input_checks
2122
)
2223

2324
add_executable(${TestType}_test

src/explore_me/explore_me.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
static long insecureEncrypt(long input);
77
static void trigger_global_buffer_overflow(const std::string &c);
88
static void trigger_use_after_free();
9+
static void trigger_double_free();
10+
static void trigger_memory_leak();
911

1012
void ExploreSimpleChecks(int a, int b, std::string c) {
1113
if (a >= 20000) {
@@ -29,6 +31,16 @@ void ExploreComplexChecks(long a, long b, std::string c) {
2931
}
3032
}
3133

34+
void ExploreStructuredInputChecks(InputStrut inputStrut){
35+
if (EncodeBase64(inputStrut.c) == "SGV5LCB3ZWw=") {
36+
if (insecureEncrypt(inputStrut.a) == 0x4e9e91e6677cfff3L) {
37+
if (insecureEncrypt(inputStrut.b) == 0x4f8b9fb34431d9d3L) {
38+
trigger_double_free();
39+
}
40+
}
41+
}
42+
}
43+
3244
static long insecureEncrypt(long input) {
3345
long key = 0xefe4eb93215cb6b0L;
3446
return input ^ key;
@@ -47,4 +59,19 @@ static void trigger_use_after_free() {
4759
buffer[5] = '\0';
4860
free(buffer);
4961
printf("%s\n", buffer);
50-
}
62+
}
63+
64+
static void trigger_double_free(){
65+
auto *buffer = static_cast<char *>(malloc(6));
66+
memcpy(buffer, "hello", 5);
67+
buffer[5] = '\0';
68+
for (int i = 0; i < 2; i++) {
69+
free(buffer);
70+
}
71+
}
72+
73+
static void trigger_memory_leak(){
74+
auto *buffer = static_cast<char *>(malloc(6));
75+
memcpy(buffer, "hello", 5);
76+
buffer[5] = '\0';
77+
}

src/explore_me/explore_me.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
#include <string>
99

10+
struct InputStrut {
11+
long a;
12+
long b;
13+
std::string c;
14+
};
15+
1016
void ExploreSimpleChecks(int a, int b, std::string c);
1117

12-
void ExploreComplexChecks(long a, long b, std::string c);
18+
void ExploreComplexChecks(long a, long b, std::string c);
19+
20+
void ExploreStructuredInputChecks(InputStrut inputStrut);
21+
22+
void ExploreCustomMutatorExampleChecks(long a, long b, std::string c);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <cifuzz/cifuzz.h>
2+
#include <fuzzer/FuzzedDataProvider.h>
3+
4+
#include "explore_me.h"
5+
6+
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
7+
#include <gtest/gtest.h>
8+
9+
TEST(ExploreStructuredInputChecks, DeveloperTest) {
10+
InputStrut inputStrut = (InputStrut) {.a=0, .b= 10, .c="Developer"};
11+
EXPECT_NO_THROW(ExploreStructuredInputChecks(inputStrut));
12+
}
13+
14+
TEST(ExploreStructuredInputChecks, MaintainerTest) {
15+
InputStrut inputStrut = (InputStrut) {.a=20, .b= -10, .c="Maintainer"};
16+
EXPECT_NO_THROW(ExploreStructuredInputChecks(inputStrut));
17+
}
18+
19+
#endif
20+
21+
FUZZ_TEST(const uint8_t *data, size_t size) {
22+
FuzzedDataProvider fdp(data, size);
23+
int a = fdp.ConsumeIntegral<int>();
24+
int b = fdp.ConsumeIntegral<int>();
25+
std::string c = fdp.ConsumeRemainingBytesAsString();
26+
InputStrut inputStrut = (InputStrut) {.a=a, .b= b, .c=c};
27+
28+
ExploreStructuredInputChecks(inputStrut);
29+
}

0 commit comments

Comments
 (0)