ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
b9a53a
From e7077e3a551a3faedfcc3d007de6a72fb5e1df62 Mon Sep 17 00:00:00 2001
b9a53a
From: Evgeny Vereshchagin <evvers@ya.ru>
b9a53a
Date: Tue, 20 Nov 2018 01:20:32 +0100
b9a53a
Subject: [PATCH] tests: add a fuzzer for journald streams
b9a53a
b9a53a
(cherry picked from commit 9541f5ff5c637bb1b3e3c69706cb73e68ff06813)
b9a53a
b9a53a
Resolves: #1764560
b9a53a
---
b9a53a
 src/fuzz/fuzz-journald-stream.c      | 35 ++++++++++++++++++++++++++++
b9a53a
 src/fuzz/fuzz-journald.c             | 10 +++++---
b9a53a
 src/fuzz/meson.build                 |  6 +++++
b9a53a
 src/journal/journald-stream.c        |  4 ++--
b9a53a
 src/journal/journald-stream.h        |  2 ++
b9a53a
 test/fuzz/fuzz-journald-stream/basic |  8 +++++++
b9a53a
 6 files changed, 60 insertions(+), 5 deletions(-)
b9a53a
 create mode 100644 src/fuzz/fuzz-journald-stream.c
b9a53a
 create mode 100644 test/fuzz/fuzz-journald-stream/basic
b9a53a
b9a53a
diff --git a/src/fuzz/fuzz-journald-stream.c b/src/fuzz/fuzz-journald-stream.c
b9a53a
new file mode 100644
b9a53a
index 0000000000..247c0889bc
b9a53a
--- /dev/null
b9a53a
+++ b/src/fuzz/fuzz-journald-stream.c
b9a53a
@@ -0,0 +1,35 @@
b9a53a
+/* SPDX-License-Identifier: LGPL-2.1+ */
b9a53a
+
b9a53a
+#include <linux/sockios.h>
b9a53a
+
b9a53a
+#include "fd-util.h"
b9a53a
+#include "fuzz.h"
b9a53a
+#include "fuzz-journald.h"
b9a53a
+#include "journald-stream.h"
b9a53a
+
b9a53a
+static int stream_fds[2] = { -1, -1 };
b9a53a
+
b9a53a
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
b9a53a
+        Server s;
b9a53a
+        StdoutStream *stream;
b9a53a
+        int v;
b9a53a
+
b9a53a
+        if (size == 0)
b9a53a
+                return 0;
b9a53a
+
b9a53a
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
b9a53a
+                log_set_max_level(LOG_CRIT);
b9a53a
+
b9a53a
+        assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0, stream_fds) >= 0);
b9a53a
+        dummy_server_init(&s, NULL, 0);
b9a53a
+        assert_se(stdout_stream_install(&s, stream_fds[0], &stream) >= 0);
b9a53a
+        assert_se(write(stream_fds[1], data, size) == (ssize_t) size);
b9a53a
+        while (ioctl(stream_fds[0], SIOCINQ, &v) == 0 && v)
b9a53a
+                sd_event_run(s.event, (uint64_t) -1);
b9a53a
+        if (s.n_stdout_streams)
b9a53a
+                stdout_stream_destroy(stream);
b9a53a
+        server_done(&s);
b9a53a
+        stream_fds[1] = safe_close(stream_fds[1]);
b9a53a
+
b9a53a
+        return 0;
b9a53a
+}
b9a53a
diff --git a/src/fuzz/fuzz-journald.c b/src/fuzz/fuzz-journald.c
b9a53a
index 0659b92ba3..950e885cae 100644
b9a53a
--- a/src/fuzz/fuzz-journald.c
b9a53a
+++ b/src/fuzz/fuzz-journald.c
b9a53a
@@ -15,11 +15,15 @@ void dummy_server_init(Server *s, const uint8_t *buffer, size_t size) {
b9a53a
                 .hostname_fd = -1,
b9a53a
                 .notify_fd = -1,
b9a53a
                 .storage = STORAGE_NONE,
b9a53a
+                .line_max = 64,
b9a53a
         };
b9a53a
         assert_se(sd_event_default(&s->event) >= 0);
b9a53a
-        s->buffer = memdup_suffix0(buffer, size);
b9a53a
-        assert_se(s->buffer);
b9a53a
-        s->buffer_size = size + 1;
b9a53a
+
b9a53a
+        if (buffer) {
b9a53a
+                s->buffer = memdup_suffix0(buffer, size);
b9a53a
+                assert_se(s->buffer);
b9a53a
+                s->buffer_size = size + 1;
b9a53a
+        }
b9a53a
 }
b9a53a
 
b9a53a
 void fuzz_journald_processing_function(
b9a53a
diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build
b9a53a
index 897c02e4ae..eea9117360 100644
b9a53a
--- a/src/fuzz/meson.build
b9a53a
+++ b/src/fuzz/meson.build
b9a53a
@@ -51,6 +51,12 @@ fuzzers += [
b9a53a
           libshared],
b9a53a
          [libselinux]],
b9a53a
 
b9a53a
+        [['src/fuzz/fuzz-journald-stream.c',
b9a53a
+          'src/fuzz/fuzz-journald.c'],
b9a53a
+         [libjournal_core,
b9a53a
+          libshared],
b9a53a
+         [libselinux]],
b9a53a
+
b9a53a
         [['src/fuzz/fuzz-journald-syslog.c',
b9a53a
           'src/fuzz/fuzz-journald.c'],
b9a53a
          [libjournal_core,
b9a53a
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
b9a53a
index dbf3503a82..6f8a4011ff 100644
b9a53a
--- a/src/journal/journald-stream.c
b9a53a
+++ b/src/journal/journald-stream.c
b9a53a
@@ -125,7 +125,7 @@ void stdout_stream_free(StdoutStream *s) {
b9a53a
 
b9a53a
 DEFINE_TRIVIAL_CLEANUP_FUNC(StdoutStream*, stdout_stream_free);
b9a53a
 
b9a53a
-static void stdout_stream_destroy(StdoutStream *s) {
b9a53a
+void stdout_stream_destroy(StdoutStream *s) {
b9a53a
         if (!s)
b9a53a
                 return;
b9a53a
 
b9a53a
@@ -534,7 +534,7 @@ terminate:
b9a53a
         return 0;
b9a53a
 }
b9a53a
 
b9a53a
-static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
b9a53a
+int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
b9a53a
         _cleanup_(stdout_stream_freep) StdoutStream *stream = NULL;
b9a53a
         sd_id128_t id;
b9a53a
         int r;
b9a53a
diff --git a/src/journal/journald-stream.h b/src/journal/journald-stream.h
b9a53a
index bc5622ab3b..487376e763 100644
b9a53a
--- a/src/journal/journald-stream.h
b9a53a
+++ b/src/journal/journald-stream.h
b9a53a
@@ -10,4 +10,6 @@ int server_open_stdout_socket(Server *s);
b9a53a
 int server_restore_streams(Server *s, FDSet *fds);
b9a53a
 
b9a53a
 void stdout_stream_free(StdoutStream *s);
b9a53a
+int stdout_stream_install(Server *s, int fd, StdoutStream **ret);
b9a53a
+void stdout_stream_destroy(StdoutStream *s);
b9a53a
 void stdout_stream_send_notify(StdoutStream *s);
b9a53a
diff --git a/test/fuzz/fuzz-journald-stream/basic b/test/fuzz/fuzz-journald-stream/basic
b9a53a
new file mode 100644
b9a53a
index 0000000000..a088f1a539
b9a53a
--- /dev/null
b9a53a
+++ b/test/fuzz/fuzz-journald-stream/basic
b9a53a
@@ -0,0 +1,8 @@
b9a53a
+
b9a53a
+
b9a53a
+6
b9a53a
+1
b9a53a
+0
b9a53a
+0
b9a53a
+0
b9a53a
+hey
b9a53a
\ No newline at end of file