dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
ff6046
From aec984020cd22ac8a199bcd067047fba50850889 Mon Sep 17 00:00:00 2001
ff6046
From: Evgeny Vereshchagin <evvers@ya.ru>
ff6046
Date: Wed, 26 Sep 2018 15:04:26 +0000
ff6046
Subject: [PATCH] tests: add a fuzzer for sd-ndisc
ff6046
ff6046
(cherry picked from commit 0f0a1dad7d69802a7e6c7fc9aba350f0e87c1952)
ff6046
ff6046
Resolves: #1696224
ff6046
---
ff6046
 src/fuzz/fuzz-ndisc-rs.c | 57 ++++++++++++++++++++++++++++++++++++++++
ff6046
 src/fuzz/meson.build     | 10 +++++++
ff6046
 2 files changed, 67 insertions(+)
ff6046
 create mode 100644 src/fuzz/fuzz-ndisc-rs.c
ff6046
ff6046
diff --git a/src/fuzz/fuzz-ndisc-rs.c b/src/fuzz/fuzz-ndisc-rs.c
ff6046
new file mode 100644
ff6046
index 0000000000..7f2d8f8649
ff6046
--- /dev/null
ff6046
+++ b/src/fuzz/fuzz-ndisc-rs.c
ff6046
@@ -0,0 +1,57 @@
ff6046
+/* SPDX-License-Identifier: LGPL-2.1+ */
ff6046
+
ff6046
+#include <netinet/icmp6.h>
ff6046
+#include <arpa/inet.h>
ff6046
+
ff6046
+#include "alloc-util.h"
ff6046
+#include "icmp6-util.h"
ff6046
+#include "fuzz.h"
ff6046
+#include "sd-ndisc.h"
ff6046
+#include "socket-util.h"
ff6046
+#include "ndisc-internal.h"
ff6046
+
ff6046
+static int test_fd[2];
ff6046
+
ff6046
+int icmp6_bind_router_solicitation(int index) {
ff6046
+        assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, test_fd) >= 0);
ff6046
+        return test_fd[0];
ff6046
+}
ff6046
+
ff6046
+int icmp6_bind_router_advertisement(int index) {
ff6046
+        return -ENOSYS;
ff6046
+}
ff6046
+
ff6046
+int icmp6_receive(int fd, void *iov_base, size_t iov_len,
ff6046
+                  struct in6_addr *dst, triple_timestamp *timestamp) {
ff6046
+        assert_se(read(fd, iov_base, iov_len) == (ssize_t) iov_len);
ff6046
+
ff6046
+        if (timestamp)
ff6046
+                triple_timestamp_get(timestamp);
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
ff6046
+        return 0;
ff6046
+}
ff6046
+
ff6046
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
ff6046
+        struct ether_addr mac_addr = {
ff6046
+                .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
ff6046
+        };
ff6046
+        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
ff6046
+        _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
ff6046
+
ff6046
+        assert_se(sd_event_new(&e) >= 0);
ff6046
+        assert_se(sd_ndisc_new(&nd) >= 0);
ff6046
+        assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
ff6046
+        assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
ff6046
+        assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
ff6046
+        assert_se(sd_ndisc_start(nd) >= 0);
ff6046
+        assert_se(write(test_fd[1], data, size) == (ssize_t) size);
ff6046
+        (void) sd_event_run(e, (uint64_t) -1);
ff6046
+        assert_se(sd_ndisc_stop(nd) >= 0);
ff6046
+        close(test_fd[1]);
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build
ff6046
index 5a97ef5091..5c81ac0c5b 100644
ff6046
--- a/src/fuzz/meson.build
ff6046
+++ b/src/fuzz/meson.build
ff6046
@@ -14,6 +14,16 @@ fuzzers += [
ff6046
           libshared],
ff6046
          []],
ff6046
 
ff6046
+        [['src/fuzz/fuzz-ndisc-rs.c',
ff6046
+          'src/libsystemd-network/dhcp-identifier.h',
ff6046
+          'src/libsystemd-network/dhcp-identifier.c',
ff6046
+          'src/libsystemd-network/icmp6-util.h',
ff6046
+          'src/systemd/sd-dhcp6-client.h',
ff6046
+          'src/systemd/sd-ndisc.h'],
ff6046
+         [libshared,
ff6046
+          libsystemd_network],
ff6046
+         []],
ff6046
+
ff6046
         [['src/fuzz/fuzz-unit-file.c'],
ff6046
          [libcore,
ff6046
           libshared],