/src/kea/fuzz/fuzz_unix_socket_kea_dhcp6.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 <dhcp6/ctrl_dhcp6_srv.h> |
16 | | #include <dhcpsrv/cfgmgr.h> |
17 | | #include <testutils/unix_control_client.h> |
18 | | |
19 | | #include <util/filesystem.h> |
20 | | |
21 | | #include <cassert> |
22 | | |
23 | | using namespace isc::asiolink; |
24 | | using namespace isc::config; |
25 | | using namespace isc::data; |
26 | | using namespace isc::dhcp; |
27 | | using namespace isc::dhcp::test; |
28 | | using namespace isc::util; |
29 | | using namespace isc::util::file; |
30 | | using namespace std; |
31 | | |
32 | | namespace { |
33 | | |
34 | | static pid_t const PID(getpid()); |
35 | | 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"); |
39 | | |
40 | | static UnixControlClient TEST_CLIENT; |
41 | | |
42 | | } // namespace |
43 | | |
44 | | extern "C" { |
45 | | |
46 | | int |
47 | 4 | LLVMFuzzerInitialize() { |
48 | 4 | static bool initialized(DoInitialization()); |
49 | 4 | assert(initialized); |
50 | | |
51 | | // "control-socket" is of explicit interest, but we also specify the memfile |
52 | | // CSV location and the server-id to make sure that we don't get an error |
53 | | // caused by an invalid file path. |
54 | 4 | writeToFile(KEA_DHCP6_CONF, R"( |
55 | 4 | { |
56 | 4 | "Dhcp6": { |
57 | 4 | "control-socket": { |
58 | 4 | "socket-name": ")" + SOCKET + R"(", |
59 | 4 | "socket-type": "unix" |
60 | 4 | }, |
61 | 4 | "lease-database": { |
62 | 4 | "name": ")" + KEA_DHCP6_CSV + R"(", |
63 | 4 | "persist": false, |
64 | 4 | "type": "memfile" |
65 | 4 | }, |
66 | 4 | "server-id": { |
67 | 4 | "type": "EN", |
68 | 4 | "enterprise-id": 2495, |
69 | 4 | "identifier": "0123456789", |
70 | 4 | "persist": false |
71 | 4 | } |
72 | 4 | } |
73 | 4 | } |
74 | 4 | )"); |
75 | | |
76 | | // Iterate through the interfaces and expect no errors. |
77 | 8 | for (IfacePtr const& interface : IfaceMgr::instance().getIfaces()) { |
78 | 8 | for (string const& error : interface->getErrors()) { |
79 | 0 | cout << error << endl; |
80 | 0 | } |
81 | 8 | assert(interface->getErrors().empty()); |
82 | 8 | } |
83 | | |
84 | 4 | return 0; |
85 | 4 | } |
86 | | |
87 | | int |
88 | 4 | LLVMFuzzerTearDown() { |
89 | 4 | try { |
90 | 4 | remove(KEA_DHCP6_CONF.c_str()); |
91 | 4 | } catch (...) { |
92 | 0 | } |
93 | 4 | try { |
94 | 0 | remove(KEA_DHCP6_CSV.c_str()); |
95 | 0 | } catch (...) { |
96 | 0 | } |
97 | 0 | try { |
98 | 0 | remove(SOCKET.c_str()); |
99 | 0 | } catch (...) { |
100 | 0 | } |
101 | 0 | try { |
102 | 0 | remove((SOCKET + ".lock").c_str()); |
103 | 0 | } catch (...) { |
104 | 0 | } |
105 | |
|
106 | 4 | return 0; |
107 | 0 | } |
108 | | |
109 | | int |
110 | 6.68k | LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { |
111 | 6.68k | CfgMgr::instance().clear(); |
112 | 6.68k | ControlledDhcpv6Srv server; |
113 | 6.68k | Path socket_path(SOCKET); |
114 | 6.68k | auto dir = socket_path.parentDirectory(); |
115 | 6.68k | auto path = UnixCommandConfig::getSocketPath(true, dir); |
116 | 6.68k | UnixCommandConfig::setSocketPathPerms(file::getPermissions(path)); |
117 | | |
118 | 6.68k | server.init(KEA_DHCP6_CONF); |
119 | 6.68k | assert(isSocket(SOCKET)); |
120 | | |
121 | 6.68k | string const command(reinterpret_cast<char const*>(data), size); |
122 | 6.68k | TEST_CLIENT.connectToServer(SOCKET); |
123 | 6.68k | TEST_CLIENT.sendCommand(command); |
124 | 6.68k | ControlledDhcpv6Srv::getInstance()->getIOService()->poll(); |
125 | 6.68k | string response; |
126 | 6.68k | TEST_CLIENT.getResponse(response); |
127 | 6.68k | ControlledDhcpv6Srv::getInstance()->getIOService()->poll(); |
128 | 6.68k | TEST_CLIENT.disconnectFromServer(); |
129 | 6.68k | ControlledDhcpv6Srv::getInstance()->getIOService()->poll(); |
130 | | |
131 | 6.68k | return 0; |
132 | 6.68k | } |
133 | | |
134 | | } // extern "C" |