Skip to content

Commit f41fb25

Browse files
committed
[#3953] Fix a static initialization race in fuzzers. Turn KEA_FUZZ_DIR into a function
1 parent 9b8b7f2 commit f41fb25

10 files changed

+32
-30
lines changed

fuzz/fuzz.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -21,25 +21,25 @@ using namespace isc::util::encode;
2121
using namespace isc::util::file;
2222
using namespace std;
2323

24-
extern "C" {
24+
string KEA_FUZZ_DIR() {
25+
static TemporaryDirectory TEMP_DIR = TemporaryDirectory();
26+
return TEMP_DIR.dirName();
27+
}
2528

26-
string KEA_LFC = isFile(KEA_LFC_INSTALLATION) ? KEA_LFC_INSTALLATION : KEA_LFC_SOURCES;
27-
// string KEA_FUZZ_DIR = isFile(KEA_FUZZ_DIR_INSTALLATION) ? KEA_FUZZ_DIR_INSTALLATION : KEA_FUZZ_DIR_SOURCES;
28-
TemporaryDirectory TEMP_DIR = TemporaryDirectory();
29-
string KEA_FUZZ_DIR = TEMP_DIR.dirName();
29+
extern "C" {
3030

3131
bool
3232
DoInitialization() {
3333
LLVMFuzzerTearDown();
3434

3535
// Spoof the logger just enough to not get LoggingNotInitialized thrown.
3636
// We explicitly don't want any logging during fuzzing for performance reasons.
37-
setenv("KEA_LOCKFILE_DIR", KEA_FUZZ_DIR.c_str(), 0);
37+
setenv("KEA_LOCKFILE_DIR", KEA_FUZZ_DIR().c_str(), 0);
3838
setenv("KEA_LFC_EXECUTABLE", "/bin/true", 0);
3939
if (!getenv("DEBUG")) {
4040
setenv("KEA_LOGGER_DESTINATION", "/dev/null", 0);
4141
}
42-
setenv("KEA_PIDFILE_DIR", KEA_FUZZ_DIR.c_str(), 0);
42+
setenv("KEA_PIDFILE_DIR", KEA_FUZZ_DIR().c_str(), 0);
4343
if (!isc::log::isLoggingInitialized()) {
4444
isc::log::initLogger("fuzzer");
4545
Daemon::loggerInit("fuzzer", /* verbose = */ false);

fuzz/fuzz.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -9,10 +9,9 @@
99
#include <string>
1010
#include <vector>
1111

12-
extern "C" {
12+
std::string KEA_FUZZ_DIR();
1313

14-
extern std::string KEA_FUZZ_DIR;
15-
extern std::string KEA_LFC;
14+
extern "C" {
1615

1716
bool
1817
DoInitialization();

fuzz/fuzz_config_kea_dhcp4.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -29,7 +29,7 @@ namespace {
2929

3030
static pid_t const PID(getpid());
3131
static string const PID_STR(to_string(PID));
32-
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".conf");
32+
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR() + "/kea-dhcp4-" + PID_STR + ".conf");
3333

3434
} // namespace
3535

fuzz/fuzz_config_kea_dhcp6.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -29,7 +29,7 @@ namespace {
2929

3030
static pid_t const PID(getpid());
3131
static string const PID_STR(to_string(PID));
32-
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".conf");
32+
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR() + "/kea-dhcp6-" + PID_STR + ".conf");
3333

3434
} // namespace
3535

fuzz/fuzz_http_endpoint_kea_dhcp4.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -44,8 +44,8 @@ namespace {
4444
static pid_t const PID(getpid());
4545
static string const PID_STR(to_string(PID));
4646
static string const ADDRESS("127.0.0.1");
47-
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".conf");
48-
static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".csv");
47+
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR() + "/kea-dhcp4-" + PID_STR + ".conf");
48+
static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR() + "/kea-dhcp4-" + PID_STR + ".csv");
4949

5050
static int PORT;
5151
static string PORT_STR;

fuzz/fuzz_http_endpoint_kea_dhcp6.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -44,8 +44,8 @@ namespace {
4444
static pid_t const PID(getpid());
4545
static string const PID_STR(to_string(PID));
4646
static string const ADDRESS("::1");
47-
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".conf");
48-
static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".csv");
47+
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR() + "/kea-dhcp6-" + PID_STR + ".conf");
48+
static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR() + "/kea-dhcp6-" + PID_STR + ".csv");
4949

5050
static int PORT;
5151
static string PORT_STR;

fuzz/fuzz_packets_kea_dhcp4.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using namespace std;
2828

2929
namespace {
3030

31-
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4.conf");
31+
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR() + "/kea-dhcp4.conf");
3232
static string KEA_DHCP4_FUZZING_INTERFACE;
3333
static string KEA_DHCP4_FUZZING_ADDRESS;
3434

fuzz/fuzz_packets_kea_dhcp6.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using namespace std;
2828

2929
namespace {
3030

31-
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6.conf");
31+
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR() + "/kea-dhcp6.conf");
3232
static string KEA_DHCP6_FUZZING_INTERFACE;
3333
static string KEA_DHCP6_FUZZING_ADDRESS;
3434

fuzz/fuzz_unix_socket_kea_dhcp4.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
#include <config/unix_command_config.h>
1515
#include <dhcp4/ctrl_dhcp4_srv.h>
1616
#include <dhcpsrv/cfgmgr.h>
17+
#include <testutils/env_var_wrapper.h>
1718
#include <testutils/unix_control_client.h>
1819

1920
#include <util/filesystem.h>
2021

2122
#include <cassert>
23+
#include <cstdlib>
2224

2325
using namespace isc::asiolink;
2426
using namespace isc::config;
2527
using namespace isc::data;
2628
using namespace isc::dhcp;
2729
using namespace isc::dhcp::test;
30+
using namespace isc::test;
2831
using namespace isc::util;
2932
using namespace isc::util::file;
3033
using namespace std;
@@ -33,9 +36,9 @@ namespace {
3336

3437
static pid_t const PID(getpid());
3538
static string const PID_STR(to_string(PID));
36-
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".conf");
37-
static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR + "/kea-dhcp4-" + PID_STR + ".csv");
38-
static string const SOCKET(KEA_FUZZ_DIR + "/kea-dhcp4-ctrl-" + PID_STR + ".sock");
39+
static string const KEA_DHCP4_CONF(KEA_FUZZ_DIR() + "/kea-dhcp4-" + PID_STR + ".conf");
40+
static string const KEA_DHCP4_CSV(KEA_FUZZ_DIR() + "/kea-dhcp4-" + PID_STR + ".csv");
41+
static string const SOCKET(KEA_FUZZ_DIR() + "/kea-dhcp4-ctrl-" + PID_STR + ".sock");
3942

4043
static UnixControlClient TEST_CLIENT;
4144

fuzz/fuzz_unix_socket_kea_dhcp6.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ namespace {
3333

3434
static pid_t const PID(getpid());
3535
static string const PID_STR(to_string(PID));
36-
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".conf");
37-
static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR + "/kea-dhcp6-" + PID_STR + ".csv");
38-
static string const SOCKET(KEA_FUZZ_DIR + "/kea-dhcp6-ctrl-" + PID_STR + ".sock");
36+
static string const KEA_DHCP6_CONF(KEA_FUZZ_DIR() + "/kea-dhcp6-" + PID_STR + ".conf");
37+
static string const KEA_DHCP6_CSV(KEA_FUZZ_DIR() + "/kea-dhcp6-" + PID_STR + ".csv");
38+
static string const SOCKET(KEA_FUZZ_DIR() + "/kea-dhcp6-ctrl-" + PID_STR + ".sock");
3939

4040
static UnixControlClient TEST_CLIENT;
4141

0 commit comments

Comments
 (0)