From a740d0bfe58515913cb5839ecb00b3400e5d20cf Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Wed, 23 Oct 2024 10:59:31 +0300 Subject: [PATCH] [#3605] Rotate ports on HTTP fuzzers as well --- fuzz/fuzz_http_endpoint_kea_dhcp4.cc | 49 +++++++++-------- fuzz/fuzz_http_endpoint_kea_dhcp6.cc | 53 +++++++++++-------- fuzz/tests/test_fuzz_config_kea_dhcp4.sh | 2 +- fuzz/tests/test_fuzz_config_kea_dhcp6.sh | 2 +- .../test_fuzz_http_endpoint_kea_dhcp4.sh | 2 +- .../test_fuzz_http_endpoint_kea_dhcp6.sh | 2 +- fuzz/tests/test_fuzz_packets_kea_dhcp4.sh | 2 +- fuzz/tests/test_fuzz_packets_kea_dhcp6.sh | 2 +- fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh | 2 +- fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh | 2 +- src/bin/dhcp4/dhcp4_srv.cc | 6 +-- src/bin/dhcp6/dhcp6_srv.cc | 6 +-- 12 files changed, 72 insertions(+), 58 deletions(-) diff --git a/fuzz/fuzz_http_endpoint_kea_dhcp4.cc b/fuzz/fuzz_http_endpoint_kea_dhcp4.cc index d17107e392..68521b10f6 100644 --- a/fuzz/fuzz_http_endpoint_kea_dhcp4.cc +++ b/fuzz/fuzz_http_endpoint_kea_dhcp4.cc @@ -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 @@ -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; } @@ -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); diff --git a/fuzz/fuzz_http_endpoint_kea_dhcp6.cc b/fuzz/fuzz_http_endpoint_kea_dhcp6.cc index 08f4f7e796..48165d7c23 100644 --- a/fuzz/fuzz_http_endpoint_kea_dhcp6.cc +++ b/fuzz/fuzz_http_endpoint_kea_dhcp6.cc @@ -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 @@ -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": { @@ -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); diff --git a/fuzz/tests/test_fuzz_config_kea_dhcp4.sh b/fuzz/tests/test_fuzz_config_kea_dhcp4.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_config_kea_dhcp4.sh +++ b/fuzz/tests/test_fuzz_config_kea_dhcp4.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_config_kea_dhcp6.sh b/fuzz/tests/test_fuzz_config_kea_dhcp6.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_config_kea_dhcp6.sh +++ b/fuzz/tests/test_fuzz_config_kea_dhcp6.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp4.sh b/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp4.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp4.sh +++ b/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp4.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp6.sh b/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp6.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp6.sh +++ b/fuzz/tests/test_fuzz_http_endpoint_kea_dhcp6.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_packets_kea_dhcp4.sh b/fuzz/tests/test_fuzz_packets_kea_dhcp4.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_packets_kea_dhcp4.sh +++ b/fuzz/tests/test_fuzz_packets_kea_dhcp4.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_packets_kea_dhcp6.sh b/fuzz/tests/test_fuzz_packets_kea_dhcp6.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_packets_kea_dhcp6.sh +++ b/fuzz/tests/test_fuzz_packets_kea_dhcp6.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh b/fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh +++ b/fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh @@ -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) diff --git a/fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh b/fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh index 416b72a656..f9e0a8f2c5 100755 --- a/fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh +++ b/fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh @@ -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) diff --git a/src/bin/dhcp4/dhcp4_srv.cc b/src/bin/dhcp4/dhcp4_srv.cc index af373f79e4..c5e65061c6 100644 --- a/src/bin/dhcp4/dhcp4_srv.cc +++ b/src/bin/dhcp4/dhcp4_srv.cc @@ -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); diff --git a/src/bin/dhcp6/dhcp6_srv.cc b/src/bin/dhcp6/dhcp6_srv.cc index 2e59bbf35e..0383225d1f 100644 --- a/src/bin/dhcp6/dhcp6_srv.cc +++ b/src/bin/dhcp6/dhcp6_srv.cc @@ -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);