]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-dhcp-server.c
Merge pull request #10694 from evverx/udev-test-in-container
[thirdparty/systemd.git] / src / fuzz / fuzz-dhcp-server.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "fuzz.h"
4
5 #include "sd-dhcp-server.c"
6
7 /* stub out network so that the server doesn't send */
8 ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) {
9 return len;
10 }
11
12 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) {
13 return 0;
14 }
15
16 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
17 _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
18 struct in_addr address = {.s_addr = htobe32(UINT32_C(10) << 24 | UINT32_C(1))};
19 static const uint8_t chaddr[] = {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
20 uint8_t *client_id;
21 DHCPLease *lease;
22 int pool_offset;
23
24 if (size < sizeof(DHCPMessage))
25 return 0;
26
27 assert_se(sd_dhcp_server_new(&server, 1) >= 0);
28 server->fd = open("/dev/null", O_RDWR|O_CLOEXEC|O_NOCTTY);
29 assert_se(server->fd >= 0);
30 assert_se(sd_dhcp_server_configure_pool(server, &address, 24, 0, 0) >= 0);
31
32 /* add a lease to the pool to expose additional code paths */
33 client_id = malloc(2);
34 assert_se(client_id);
35 client_id[0] = 2;
36 client_id[1] = 2;
37 lease = new0(DHCPLease, 1);
38 assert_se(lease);
39 lease->client_id.length = 2;
40 lease->client_id.data = client_id;
41 lease->address = htobe32(UINT32_C(10) << 24 | UINT32_C(2));
42 lease->gateway = htobe32(UINT32_C(10) << 24 | UINT32_C(1));
43 lease->expiration = UINT64_MAX;
44 memcpy(lease->chaddr, chaddr, 16);
45 pool_offset = get_pool_offset(server, lease->address);
46 server->bound_leases[pool_offset] = lease;
47 assert_se(hashmap_put(server->leases_by_client_id, &lease->client_id, lease) >= 0);
48
49 (void) dhcp_server_handle_message(server, (DHCPMessage*)data, size);
50
51 return 0;
52 }