1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <netinet/icmp6.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "icmp6-util.h"
9 #include "fuzz.h"
10 #include "sd-ndisc.h"
11 #include "socket-util.h"
12 #include "ndisc-internal.h"
13
14 static int test_fd[2] = { -1, -1 };
15
icmp6_bind_router_solicitation(int index)16 int icmp6_bind_router_solicitation(int index) {
17 assert_se(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) >= 0);
18 return test_fd[0];
19 }
20
icmp6_bind_router_advertisement(int index)21 int icmp6_bind_router_advertisement(int index) {
22 return -ENOSYS;
23 }
24
icmp6_receive(int fd,void * iov_base,size_t iov_len,struct in6_addr * dst,triple_timestamp * timestamp)25 int icmp6_receive(int fd, void *iov_base, size_t iov_len,
26 struct in6_addr *dst, triple_timestamp *timestamp) {
27 assert_se(read(fd, iov_base, iov_len) == (ssize_t) iov_len);
28
29 if (timestamp)
30 triple_timestamp_get(timestamp);
31
32 return 0;
33 }
34
icmp6_send_router_solicitation(int s,const struct ether_addr * ether_addr)35 int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
36 return 0;
37 }
38
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)39 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
40 struct ether_addr mac_addr = {
41 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
42 };
43 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
44 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
45
46 if (outside_size_range(size, 0, 2048))
47 return 0;
48
49 assert_se(sd_event_new(&e) >= 0);
50 assert_se(sd_ndisc_new(&nd) >= 0);
51 assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
52 assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
53 assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
54 assert_se(sd_ndisc_start(nd) >= 0);
55 assert_se(write(test_fd[1], data, size) == (ssize_t) size);
56 (void) sd_event_run(e, UINT64_MAX);
57 assert_se(sd_ndisc_stop(nd) >= 0);
58 close(test_fd[1]);
59
60 return 0;
61 }
62