]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3605] Rotate ports on HTTP fuzzers as well
authorAndrei Pavel <andrei@isc.org>
Wed, 23 Oct 2024 07:59:31 +0000 (10:59 +0300)
committerAndrei Pavel <andrei@isc.org>
Wed, 23 Oct 2024 13:40:37 +0000 (16:40 +0300)
12 files changed:
fuzz/fuzz_http_endpoint_kea_dhcp4.cc
fuzz/fuzz_http_endpoint_kea_dhcp6.cc
fuzz/tests/test_fuzz_config_kea_dhcp4.sh
fuzz/tests/test_fuzz_config_kea_dhcp6.sh
fuzz/tests/test_fuzz_http_endpoint_kea_dhcp4.sh
fuzz/tests/test_fuzz_http_endpoint_kea_dhcp6.sh
fuzz/tests/test_fuzz_packets_kea_dhcp4.sh
fuzz/tests/test_fuzz_packets_kea_dhcp6.sh
fuzz/tests/test_fuzz_unix_socket_kea_dhcp4.sh
fuzz/tests/test_fuzz_unix_socket_kea_dhcp6.sh
src/bin/dhcp4/dhcp4_srv.cc
src/bin/dhcp6/dhcp6_srv.cc

index d17107e392f0074de6ad7694e16775d539b1a1a7..68521b10f695f534d0aeb798b92636142ad91a1c 100644 (file)
@@ -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);
index 08f4f7e79614b0f28eca8b98290135b6ff92ce9e..48165d7c23f3336ddc416fd43ae4ca2041b4777c 100644 (file)
@@ -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);
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index 416b72a656801414dda73c4d977f776418bfd564..f9e0a8f2c55bf44cdd305a4182582b3ced1851fe 100755 (executable)
@@ -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)
 
index af373f79e4d21309071098984f2ea4bc0ec9fa4a..c5e65061c62b784397256874fd3c3d644d3c39d1 100644 (file)
@@ -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);
index 2e59bbf35eff658fc8c85449feea566a43ae6a84..0383225d1f2d566bbad14518bec65e4471916780 100644 (file)
@@ -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);