]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/fuzz-dhcp-client.c
shutdown: Make all mounts private
[thirdparty/systemd.git] / src / libsystemd-network / fuzz-dhcp-client.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/socket.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "fuzz.h"
9 #include "sd-event.h"
10
11 #include "sd-dhcp-client.c"
12
13 int dhcp_network_bind_raw_socket(
14 int ifindex,
15 union sockaddr_union *link,
16 uint32_t id,
17 const struct hw_addr_data *hw_addr,
18 const struct hw_addr_data *bcast_addr,
19 uint16_t arp_type, uint16_t port) {
20
21 int fd;
22 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
23 if (fd < 0)
24 return -errno;
25
26 return fd;
27 }
28
29 int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, const void *packet, size_t len) {
30 return len;
31 }
32
33 int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port, int ip_service_type) {
34 int fd;
35
36 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
37 if (fd < 0)
38 return -errno;
39
40 return fd;
41 }
42
43 int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port, const void *packet, size_t len) {
44 return len;
45 }
46
47 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
48 uint8_t mac_addr[] = {'A', 'B', 'C', '1', '2', '3'};
49 uint8_t bcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
50 _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL;
51 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
52 int res, r;
53
54 if (!getenv("SYSTEMD_LOG_LEVEL"))
55 log_set_max_level(LOG_CRIT);
56
57 r = sd_dhcp_client_new(&client, false);
58 assert_se(r >= 0);
59 assert_se(client);
60
61 assert_se(sd_event_new(&e) >= 0);
62
63 r = sd_dhcp_client_attach_event(client, e, 0);
64 assert_se(r >= 0);
65
66 assert_se(sd_dhcp_client_set_ifindex(client, 42) >= 0);
67 assert_se(sd_dhcp_client_set_mac(client, mac_addr, bcast_addr, ETH_ALEN, ARPHRD_ETHER) >= 0);
68 dhcp_client_set_test_mode(client, true);
69
70 res = sd_dhcp_client_start(client);
71 assert_se(IN_SET(res, 0, -EINPROGRESS));
72 client->xid = 2;
73
74 (void) client_handle_offer(client, (DHCPMessage*) data, size);
75
76 assert_se(sd_dhcp_client_stop(client) >= 0);
77
78 return 0;
79 }