/src/kea/fuzz/fuzz_unix_socket_kea_dhcp4.cc
Line | Count | Source |
1 | | // Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC") |
2 | | // |
3 | | // This Source Code Form is subject to the terms of the Mozilla Public |
4 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | | |
7 | | #include <config.h> |
8 | | |
9 | | #include <fuzz.h> |
10 | | |
11 | | #include <asiolink/io_service.h> |
12 | | #include <cc/data.h> |
13 | | #include <config/command_mgr.h> |
14 | | #include <config/unix_command_config.h> |
15 | | #include <dhcp4/ctrl_dhcp4_srv.h> |
16 | | #include <dhcpsrv/cfgmgr.h> |
17 | | #include <testutils/env_var_wrapper.h> |
18 | | #include <testutils/unix_control_client.h> |
19 | | |
20 | | #include <util/filesystem.h> |
21 | | |
22 | | #include <cassert> |
23 | | #include <cstdlib> |
24 | | |
25 | | using namespace isc::asiolink; |
26 | | using namespace isc::config; |
27 | | using namespace isc::data; |
28 | | using namespace isc::dhcp; |
29 | | using namespace isc::dhcp::test; |
30 | | using namespace isc::test; |
31 | | using namespace isc::util; |
32 | | using namespace isc::util::file; |
33 | | using namespace std; |
34 | | |
35 | | namespace { |
36 | | |
37 | | static pid_t const PID(getpid()); |
38 | | static string const PID_STR(to_string(PID)); |
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"); |
42 | | |
43 | | static UnixControlClient TEST_CLIENT; |
44 | | |
45 | | } // namespace |
46 | | |
47 | | extern "C" { |
48 | | |
49 | | int |
50 | 4 | LLVMFuzzerInitialize() { |
51 | 4 | static bool initialized(DoInitialization()); |
52 | 4 | assert(initialized); |
53 | | |
54 | | // "control-socket" is of explicit interest, but we also specify the memfile |
55 | | // CSV location to make sure that we don't get an error caused by an invalid |
56 | | // file path. |
57 | 4 | writeToFile(KEA_DHCP4_CONF, R"( |
58 | 4 | { |
59 | 4 | "Dhcp4": { |
60 | 4 | "control-socket": { |
61 | 4 | "socket-name": ")" + SOCKET + R"(", |
62 | 4 | "socket-type": "unix" |
63 | 4 | }, |
64 | 4 | "lease-database": { |
65 | 4 | "name": ")" + KEA_DHCP4_CSV + R"(", |
66 | 4 | "persist": false, |
67 | 4 | "type": "memfile" |
68 | 4 | } |
69 | 4 | } |
70 | 4 | } |
71 | 4 | )"); |
72 | | |
73 | | // Iterate through the interfaces and expect no errors. |
74 | 8 | for (IfacePtr const& interface : IfaceMgr::instance().getIfaces()) { |
75 | 8 | for (string const& error : interface->getErrors()) { |
76 | 0 | cout << error << endl; |
77 | 0 | } |
78 | 8 | assert(interface->getErrors().empty()); |
79 | 8 | } |
80 | | |
81 | 4 | return 0; |
82 | 4 | } |
83 | | |
84 | | int |
85 | 4 | LLVMFuzzerTearDown() { |
86 | 4 | try { |
87 | 4 | remove(KEA_DHCP4_CONF.c_str()); |
88 | 4 | } catch (...) { |
89 | 0 | } |
90 | 4 | try { |
91 | 0 | remove(KEA_DHCP4_CSV.c_str()); |
92 | 0 | } catch (...) { |
93 | 0 | } |
94 | 0 | try { |
95 | 0 | remove(SOCKET.c_str()); |
96 | 0 | } catch (...) { |
97 | 0 | } |
98 | 0 | try { |
99 | 0 | remove((SOCKET + ".lock").c_str()); |
100 | 0 | } catch (...) { |
101 | 0 | } |
102 | |
|
103 | 4 | return 0; |
104 | 0 | } |
105 | | |
106 | | int |
107 | 6.84k | LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { |
108 | 6.84k | CfgMgr::instance().clear(); |
109 | 6.84k | ControlledDhcpv4Srv server; |
110 | 6.84k | Path socket_path(SOCKET); |
111 | 6.84k | auto dir = socket_path.parentDirectory(); |
112 | 6.84k | auto path = UnixCommandConfig::getSocketPath(true, dir); |
113 | 6.84k | UnixCommandConfig::setSocketPathPerms(file::getPermissions(path)); |
114 | | |
115 | 6.84k | server.init(KEA_DHCP4_CONF); |
116 | 6.84k | assert(isSocket(SOCKET)); |
117 | | |
118 | 6.84k | string const command(reinterpret_cast<char const*>(data), size); |
119 | 6.84k | TEST_CLIENT.connectToServer(SOCKET); |
120 | 6.84k | TEST_CLIENT.sendCommand(command); |
121 | 6.84k | ControlledDhcpv4Srv::getInstance()->getIOService()->poll(); |
122 | 6.84k | string response; |
123 | 6.84k | TEST_CLIENT.getResponse(response); |
124 | 6.84k | ControlledDhcpv4Srv::getInstance()->getIOService()->poll(); |
125 | 6.84k | TEST_CLIENT.disconnectFromServer(); |
126 | 6.84k | ControlledDhcpv4Srv::getInstance()->getIOService()->poll(); |
127 | | |
128 | 6.84k | return 0; |
129 | 6.84k | } |
130 | | |
131 | | } // extern "C" |