From f41fb2541fc6cf8804a08a75152cdded3359e427 Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Tue, 17 Jun 2025 12:04:00 +0300 Subject: [PATCH] [#3953] Fix a static initialization race in fuzzers. Turn KEA_FUZZ_DIR into a function --- fuzz/fuzz.cc | 16 ++++++++-------- fuzz/fuzz.h | 7 +++---- fuzz/fuzz_config_kea_dhcp4.cc | 4 ++-- fuzz/fuzz_config_kea_dhcp6.cc | 4 ++-- fuzz/fuzz_http_endpoint_kea_dhcp4.cc | 6 +++--- fuzz/fuzz_http_endpoint_kea_dhcp6.cc | 6 +++--- fuzz/fuzz_packets_kea_dhcp4.cc | 2 +- fuzz/fuzz_packets_kea_dhcp6.cc | 2 +- fuzz/fuzz_unix_socket_kea_dhcp4.cc | 9 ++++++--- fuzz/fuzz_unix_socket_kea_dhcp6.cc | 6 +++--- 10 files changed, 32 insertions(+), 30 deletions(-) diff --git a/fuzz/fuzz.cc b/fuzz/fuzz.cc index f77354f44e..85467e553c 100644 --- a/fuzz/fuzz.cc +++ b/fuzz/fuzz.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -21,12 +21,12 @@ using namespace isc::util::encode; using namespace isc::util::file; using namespace std; -extern "C" { +string KEA_FUZZ_DIR() { + static TemporaryDirectory TEMP_DIR = TemporaryDirectory(); + return TEMP_DIR.dirName(); +} -string KEA_LFC = isFile(KEA_LFC_INSTALLATION) ? KEA_LFC_INSTALLATION : KEA_LFC_SOURCES; -// string KEA_FUZZ_DIR = isFile(KEA_FUZZ_DIR_INSTALLATION) ? KEA_FUZZ_DIR_INSTALLATION : KEA_FUZZ_DIR_SOURCES; -TemporaryDirectory TEMP_DIR = TemporaryDirectory(); -string KEA_FUZZ_DIR = TEMP_DIR.dirName(); +extern "C" { bool DoInitialization() { @@ -34,12 +34,12 @@ DoInitialization() { // Spoof the logger just enough to not get LoggingNotInitialized thrown. // We explicitly don't want any logging during fuzzing for performance reasons. - setenv("KEA_LOCKFILE_DIR", KEA_FUZZ_DIR.c_str(), 0); + setenv("KEA_LOCKFILE_DIR", KEA_FUZZ_DIR().c_str(), 0); setenv("KEA_LFC_EXECUTABLE", "/bin/true", 0); if (!getenv("DEBUG")) { setenv("KEA_LOGGER_DESTINATION", "/dev/null", 0); } - setenv("KEA_PIDFILE_DIR", KEA_FUZZ_DIR.c_str(), 0); + setenv("KEA_PIDFILE_DIR", KEA_FUZZ_DIR().c_str(), 0); if (!isc::log::isLoggingInitialized()) { isc::log::initLogger("fuzzer"); Daemon::loggerInit("fuzzer", /* verbose = */ false); diff --git a/fuzz/fuzz.h b/fuzz/fuzz.h index f92d73a155..e94aa200ec 100644 --- a/fuzz/fuzz.h +++ b/fuzz/fuzz.h @@ -1,4 +1,4 @@ -// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -9,10 +9,9 @@ #include #include -extern "C" { +std::string KEA_FUZZ_DIR(); -extern std::string KEA_FUZZ_DIR; -extern std::string KEA_LFC; +extern "C" { bool DoInitialization(); diff --git a/fuzz/fuzz_config_kea_dhcp4.cc b/fuzz/fuzz_config_kea_dhcp4.cc index 7cf779ff9a..47bc795577 100644 --- a/fuzz/fuzz_config_kea_dhcp4.cc +++ b/fuzz/fuzz_config_kea_dhcp4.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -29,7 +29,7 @@ namespace { static pid_t const PID(getpid()); static string const PID_STR(to_string(PID)); -static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".conf"); +static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR() + "/kea-dhcp4-" + PID_STR + ".conf"); } // namespace diff --git a/fuzz/fuzz_config_kea_dhcp6.cc b/fuzz/fuzz_config_kea_dhcp6.cc index 9e11552c8f..ee24bf0ef0 100644 --- a/fuzz/fuzz_config_kea_dhcp6.cc +++ b/fuzz/fuzz_config_kea_dhcp6.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -29,7 +29,7 @@ namespace { static pid_t const PID(getpid()); static string const PID_STR(to_string(PID)); -static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".conf"); +static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR() + "/kea-dhcp6-" + PID_STR + ".conf"); } // namespace diff --git a/fuzz/fuzz_http_endpoint_kea_dhcp4.cc b/fuzz/fuzz_http_endpoint_kea_dhcp4.cc index 68521b10f6..e76f91d21a 100644 --- a/fuzz/fuzz_http_endpoint_kea_dhcp4.cc +++ b/fuzz/fuzz_http_endpoint_kea_dhcp4.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -44,8 +44,8 @@ namespace { static pid_t const PID(getpid()); static string const PID_STR(to_string(PID)); 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 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; diff --git a/fuzz/fuzz_http_endpoint_kea_dhcp6.cc b/fuzz/fuzz_http_endpoint_kea_dhcp6.cc index 48165d7c23..95230f3a4a 100644 --- a/fuzz/fuzz_http_endpoint_kea_dhcp6.cc +++ b/fuzz/fuzz_http_endpoint_kea_dhcp6.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -44,8 +44,8 @@ namespace { static pid_t const PID(getpid()); static string const PID_STR(to_string(PID)); 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 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; diff --git a/fuzz/fuzz_packets_kea_dhcp4.cc b/fuzz/fuzz_packets_kea_dhcp4.cc index 6427d65c9a..ade190c8a6 100644 --- a/fuzz/fuzz_packets_kea_dhcp4.cc +++ b/fuzz/fuzz_packets_kea_dhcp4.cc @@ -28,7 +28,7 @@ using namespace std; namespace { -static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4.conf"); +static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR() + "/kea-dhcp4.conf"); static string KEA_DHCP4_FUZZING_INTERFACE; static string KEA_DHCP4_FUZZING_ADDRESS; diff --git a/fuzz/fuzz_packets_kea_dhcp6.cc b/fuzz/fuzz_packets_kea_dhcp6.cc index 38d0b35c85..ed96c99cd0 100644 --- a/fuzz/fuzz_packets_kea_dhcp6.cc +++ b/fuzz/fuzz_packets_kea_dhcp6.cc @@ -28,7 +28,7 @@ using namespace std; namespace { -static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6.conf"); +static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR() + "/kea-dhcp6.conf"); static string KEA_DHCP6_FUZZING_INTERFACE; static string KEA_DHCP6_FUZZING_ADDRESS; diff --git a/fuzz/fuzz_unix_socket_kea_dhcp4.cc b/fuzz/fuzz_unix_socket_kea_dhcp4.cc index 1537f10120..1053cf0f83 100644 --- a/fuzz/fuzz_unix_socket_kea_dhcp4.cc +++ b/fuzz/fuzz_unix_socket_kea_dhcp4.cc @@ -14,17 +14,20 @@ #include #include #include +#include #include #include #include +#include using namespace isc::asiolink; using namespace isc::config; using namespace isc::data; using namespace isc::dhcp; using namespace isc::dhcp::test; +using namespace isc::test; using namespace isc::util; using namespace isc::util::file; using namespace std; @@ -33,9 +36,9 @@ namespace { static pid_t const PID(getpid()); static string const PID_STR(to_string(PID)); -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 string const SOCKET(KEA_FUZZ_DIR + "/kea-dhcp4-ctrl-" + PID_STR + ".sock"); +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 string const SOCKET(KEA_FUZZ_DIR() + "/kea-dhcp4-ctrl-" + PID_STR + ".sock"); static UnixControlClient TEST_CLIENT; diff --git a/fuzz/fuzz_unix_socket_kea_dhcp6.cc b/fuzz/fuzz_unix_socket_kea_dhcp6.cc index e545fbd4d2..f14490a539 100644 --- a/fuzz/fuzz_unix_socket_kea_dhcp6.cc +++ b/fuzz/fuzz_unix_socket_kea_dhcp6.cc @@ -33,9 +33,9 @@ namespace { static pid_t const PID(getpid()); static string const PID_STR(to_string(PID)); -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 string const SOCKET(KEA_FUZZ_DIR + "/kea-dhcp6-ctrl-" + PID_STR + ".sock"); +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 string const SOCKET(KEA_FUZZ_DIR() + "/kea-dhcp6-ctrl-" + PID_STR + ".sock"); static UnixControlClient TEST_CLIENT;