Debian Codegraph @

src/libsystemd-network/fuzz-dhcp-relay.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/socket.h>
4
5 #include "sd-event.h"
6
7 #include "dhcp-relay-internal.h"
8 #include "fd-util.h"
9 #include "fuzz.h"
10 #include "in-addr-util.h"
11 #include "iovec-util.h"
12 #include "tests.h"
13
14 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
15 union in_addr_union a;
16
17 fuzz_setup_logging();
18
19 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
20 ASSERT_OK(sd_event_new(&e));
21
22 _cleanup_(sd_dhcp_relay_unrefp) sd_dhcp_relay *relay = NULL;
23 ASSERT_OK(sd_dhcp_relay_new(&relay));
24 ASSERT_OK(sd_dhcp_relay_attach_event(relay, e, SD_EVENT_PRIORITY_NORMAL));
25 ASSERT_OK(in_addr_from_string(AF_INET, "198.51.100.1", &a));
26 ASSERT_OK(sd_dhcp_relay_set_server_address(relay, &a.in));
27 ASSERT_OK(sd_dhcp_relay_set_remote_id(relay, &IOVEC_MAKE_STRING("test-remote-id")));
28 ASSERT_OK(sd_dhcp_relay_set_server_identifier_override(relay, true));
29
30 _cleanup_(sd_dhcp_relay_interface_unrefp) sd_dhcp_relay_interface *upstream = NULL;
31 ASSERT_OK(sd_dhcp_relay_add_interface(relay, 4242, /* is_upstream= */ true, &upstream));
32 ASSERT_OK(sd_dhcp_relay_interface_set_ifname(upstream, "test-upstream"));
33 ASSERT_OK(in_addr_from_string(AF_INET, "198.51.100.2", &a));
34 ASSERT_OK(sd_dhcp_relay_interface_set_address(upstream, &a.in, 24));
35
36 _cleanup_close_pair_ int upstream_fd[2] = EBADF_PAIR;
37 ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, upstream_fd));
38 upstream->socket_fd = TAKE_FD(upstream_fd[0]);
39 ASSERT_OK(sd_dhcp_relay_interface_start(upstream));
40
41 _cleanup_(sd_dhcp_relay_interface_unrefp) sd_dhcp_relay_interface *downstream = NULL;
42 ASSERT_OK(sd_dhcp_relay_add_interface(relay, 4343, /* is_upstream= */ false, &downstream));
43 ASSERT_OK(sd_dhcp_relay_interface_set_ifname(downstream, "test-downstream"));
44 ASSERT_OK(in_addr_from_string(AF_INET, "192.0.2.1", &a));
45 ASSERT_OK(sd_dhcp_relay_interface_set_address(downstream, &a.in, 24));
46
47 ASSERT_OK(in_addr_from_string(AF_INET, "203.0.113.1", &a));
48 ASSERT_OK(sd_dhcp_relay_downstream_set_gateway_address(downstream, &a.in));
49 ASSERT_OK(sd_dhcp_relay_downstream_set_circuit_id(downstream, &IOVEC_MAKE_STRING("test-circuit-id")));
50 ASSERT_OK(sd_dhcp_relay_downstream_set_virtual_subnet_selection(downstream, &IOVEC_MAKE_STRING("test-virtual-net")));
51
52 _cleanup_close_pair_ int downstream_fd[2] = EBADF_PAIR;
53 ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, downstream_fd));
54 downstream->socket_fd = TAKE_FD(downstream_fd[0]);
55 ASSERT_OK(sd_dhcp_relay_interface_start(downstream));
56
57 (void) downstream_process_message(downstream, &IOVEC_MAKE(data, size), /* pktinfo= */ NULL);
58 (void) upstream_process_message(upstream, &IOVEC_MAKE(data, size), /* pktinfo= */ NULL);
59
60 return 0;
61 }