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