Skip to content

Commit

Permalink
[#3605] Rotate ports on HTTP fuzzers as well
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-pavel committed Oct 23, 2024
1 parent f340ef1 commit a740d0b
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 58 deletions.
49 changes: 28 additions & 21 deletions fuzz/fuzz_http_endpoint_kea_dhcp4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ using namespace std;
namespace {

static pid_t const PID(getpid());
static int const PORT(getpid() % 1000 + 2000);
static string const PID_STR(to_string(PID));
static string const PORT_STR(to_string(PORT));
static string const ADDRESS("0.0.0.0");
static string const ADDRESS("127.0.0.1");
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".conf");
static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".csv");

static int PORT;
static string PORT_STR;

/// @brief Represents HTTP POST request with JSON body.
///
/// In addition to the requirements specified by the @ref PostHttpRequest
Expand Down Expand Up @@ -102,24 +103,7 @@ LLVMFuzzerInitialize() {
static bool initialized(DoInitialization());
assert(initialized);

writeToFile(KEA_DHCP4_CONF, R"(
{
"Dhcp4": {
"control-sockets": [
{
"socket-address": ")" + ADDRESS + R"(",
"socket-port": )" + PORT_STR + R"(,
"socket-type": "http"
}
],
"lease-database": {
"name": ")" + KEA_DHCP4_CSV + R"(",
"persist": false,
"type": "memfile"
}
}
}
)");
setenv("KEA_DHCP4_FUZZING_ROTATE_PORT", "true", 0);

return 0;
}
Expand All @@ -141,6 +125,29 @@ int
LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) {
CfgMgr::instance().clear();
ControlledDhcpv4Srv server;

PORT = ControlledDhcpv4Srv::getInstance()->getServerPort();
PORT_STR = to_string(PORT);

writeToFile(KEA_DHCP4_CONF, R"(
{
"Dhcp4": {
"control-sockets": [
{
"socket-address": ")" + ADDRESS + R"(",
"socket-port": )" + PORT_STR + R"(,
"socket-type": "http"
}
],
"lease-database": {
"name": ")" + KEA_DHCP4_CSV + R"(",
"persist": false,
"type": "memfile"
}
}
}
)");

server.init(KEA_DHCP4_CONF);

HttpClient client(ControlledDhcpv4Srv::getInstance()->getIOService(), false);
Expand Down
53 changes: 30 additions & 23 deletions fuzz/fuzz_http_endpoint_kea_dhcp6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ using namespace std;
namespace {

static pid_t const PID(getpid());
static int const PORT(getpid() % 1000 + 2000);
static string const PID_STR(to_string(PID));
static string const PORT_STR(to_string(PORT));
static string const ADDRESS("::");
static string const ADDRESS("::1");
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".conf");
static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".csv");

static int PORT;
static string PORT_STR;

/// @brief Represents HTTP POST request with JSON body.
///
/// In addition to the requirements specified by the @ref PostHttpRequest
Expand Down Expand Up @@ -102,6 +103,32 @@ LLVMFuzzerInitialize() {
static bool initialized(DoInitialization());
assert(initialized);

setenv("KEA_DHCP6_FUZZING_ROTATE_PORT", "true", 0);

return 0;
}

int
LLVMFuzzerTearDown() {
try {
remove(KEA_DHCP6_CONF.c_str());
} catch (...) {
}
try {
remove(KEA_DHCP6_CSV.c_str());
} catch (...) {
}
return 0;
}

int
LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) {
CfgMgr::instance().clear();
ControlledDhcpv6Srv server;

PORT = ControlledDhcpv6Srv::getInstance()->getServerPort();
PORT_STR = to_string(PORT);

writeToFile(KEA_DHCP6_CONF, R"(
{
"Dhcp6": {
Expand All @@ -127,26 +154,6 @@ LLVMFuzzerInitialize() {
}
)");

return 0;
}

int
LLVMFuzzerTearDown() {
try {
remove(KEA_DHCP6_CONF.c_str());
} catch (...) {
}
try {
remove(KEA_DHCP6_CSV.c_str());
} catch (...) {
}
return 0;
}

int
LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) {
CfgMgr::instance().clear();
ControlledDhcpv6Srv server;
server.init(KEA_DHCP6_CONF);

HttpClient client(ControlledDhcpv6Srv::getInstance()->getIOService(), false);
Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_config_kea_dhcp4.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_config_kea_dhcp6.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_http_endpoint_kea_dhcp4.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_http_endpoint_kea_dhcp6.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_packets_kea_dhcp4.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_packets_kea_dhcp6.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
2 changes: 1 addition & 1 deletion fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if ! sudo -n true; then exec sudo -E -- "${0}" "${@}"; fi
if test "$(id -u)" != 0; then exec sudo -E -- "${0}" "${@}"; fi

script_path=$(cd "$(dirname "${0}")" && pwd)

Expand Down
6 changes: 3 additions & 3 deletions src/bin/dhcp4/dhcp4_srv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5181,9 +5181,9 @@ void Dhcpv4Srv::discardPackets() {

uint16_t Dhcpv4Srv::getServerPort() const {
#ifdef FUZZING
char const* const randomize(getenv("KEA_DHCP4_FUZZING_ROTATE_PORT"));
if (randomize) {
InterprocessSyncFile file("kea-dhcp4-fuzzing-randomize-port");
char const* const rotate(getenv("KEA_DHCP4_FUZZING_ROTATE_PORT"));
if (rotate) {
InterprocessSyncFile file("kea-dhcp4-fuzzing-rotate-port");
InterprocessSyncLocker locker(file);
while (!locker.lock()) {
this_thread::sleep_for(1s);
Expand Down
6 changes: 3 additions & 3 deletions src/bin/dhcp6/dhcp6_srv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4921,9 +4921,9 @@ void Dhcpv6Srv::discardPackets() {

uint16_t Dhcpv6Srv::getServerPort() const {
#ifdef FUZZING
char const* const randomize(getenv("KEA_DHCP6_FUZZING_ROTATE_PORT"));
if (randomize) {
InterprocessSyncFile file("kea-dhcp6-fuzzing-randomize-port");
char const* const rotate(getenv("KEA_DHCP6_FUZZING_ROTATE_PORT"));
if (rotate) {
InterprocessSyncFile file("kea-dhcp6-fuzzing-rotate-port");
InterprocessSyncLocker locker(file);
while (!locker.lock()) {
this_thread::sleep_for(1s);
Expand Down

0 comments on commit a740d0b

Please sign in to comment.