4fbe94
From b276c85200786add6c86b6c1fedc888c71ffe5db Mon Sep 17 00:00:00 2001
4fbe94
From: Evgeny Vereshchagin <evvers@ya.ru>
4fbe94
Date: Sat, 17 Nov 2018 13:01:09 +0100
4fbe94
Subject: [PATCH] tests: introduce dummy_server_init and use it in all journald
4fbe94
 fuzzers
4fbe94
4fbe94
(cherry picked from commit ed62712dc6fb236845c489a7f386c7aff0ec31d6)
4fbe94
4fbe94
Resolves: #1764560
4fbe94
---
4fbe94
 src/fuzz/fuzz-journald-audit.c | 18 +++---------------
4fbe94
 src/fuzz/fuzz-journald-kmsg.c  | 20 ++++----------------
4fbe94
 src/fuzz/fuzz-journald.c       | 26 +++++++++++++++++++-------
4fbe94
 src/fuzz/fuzz-journald.h       |  2 ++
4fbe94
 src/fuzz/meson.build           |  6 ++++--
4fbe94
 5 files changed, 32 insertions(+), 40 deletions(-)
4fbe94
4fbe94
diff --git a/src/fuzz/fuzz-journald-audit.c b/src/fuzz/fuzz-journald-audit.c
4fbe94
index fe401c0d98..3f3ce7e8ee 100644
4fbe94
--- a/src/fuzz/fuzz-journald-audit.c
4fbe94
+++ b/src/fuzz/fuzz-journald-audit.c
4fbe94
@@ -1,26 +1,14 @@
4fbe94
 /* SPDX-License-Identifier: LGPL-2.1+ */
4fbe94
 
4fbe94
 #include "fuzz.h"
4fbe94
+#include "fuzz-journald.h"
4fbe94
 #include "journald-audit.h"
4fbe94
 
4fbe94
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
4fbe94
         Server s;
4fbe94
-        _cleanup_free_ char *buffer = NULL;
4fbe94
 
4fbe94
-        s = (Server) {
4fbe94
-                .syslog_fd = -1,
4fbe94
-                .native_fd = -1,
4fbe94
-                .stdout_fd = -1,
4fbe94
-                .dev_kmsg_fd = -1,
4fbe94
-                .audit_fd = -1,
4fbe94
-                .hostname_fd = -1,
4fbe94
-                .notify_fd = -1,
4fbe94
-                .storage = STORAGE_NONE,
4fbe94
-        };
4fbe94
-        assert_se(sd_event_default(&s.event) >= 0);
4fbe94
-        buffer = memdup_suffix0(data, size);
4fbe94
-        assert_se(buffer);
4fbe94
-        process_audit_string(&s, 0, buffer, size);
4fbe94
+        dummy_server_init(&s, data, size);
4fbe94
+        process_audit_string(&s, 0, s.buffer, size);
4fbe94
         server_done(&s);
4fbe94
 
4fbe94
         return 0;
4fbe94
diff --git a/src/fuzz/fuzz-journald-kmsg.c b/src/fuzz/fuzz-journald-kmsg.c
4fbe94
index e2611c6d45..f7426c8400 100644
4fbe94
--- a/src/fuzz/fuzz-journald-kmsg.c
4fbe94
+++ b/src/fuzz/fuzz-journald-kmsg.c
4fbe94
@@ -1,29 +1,17 @@
4fbe94
 /* SPDX-License-Identifier: LGPL-2.1+ */
4fbe94
 
4fbe94
 #include "fuzz.h"
4fbe94
+#include "fuzz-journald.h"
4fbe94
 #include "journald-kmsg.h"
4fbe94
 
4fbe94
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
4fbe94
-        Server s = {};
4fbe94
-        _cleanup_free_ char *buffer = NULL;
4fbe94
+        Server s;
4fbe94
 
4fbe94
         if (size == 0)
4fbe94
                 return 0;
4fbe94
 
4fbe94
-        s = (Server) {
4fbe94
-                .syslog_fd = -1,
4fbe94
-                .native_fd = -1,
4fbe94
-                .stdout_fd = -1,
4fbe94
-                .dev_kmsg_fd = -1,
4fbe94
-                .audit_fd = -1,
4fbe94
-                .hostname_fd = -1,
4fbe94
-                .notify_fd = -1,
4fbe94
-                .storage = STORAGE_NONE,
4fbe94
-        };
4fbe94
-        assert_se(sd_event_default(&s.event) >= 0);
4fbe94
-        buffer = memdup(data, size);
4fbe94
-        assert_se(buffer);
4fbe94
-        dev_kmsg_record(&s, buffer, size);
4fbe94
+        dummy_server_init(&s, data, size);
4fbe94
+        dev_kmsg_record(&s, s.buffer, size);
4fbe94
         server_done(&s);
4fbe94
 
4fbe94
         return 0;
4fbe94
diff --git a/src/fuzz/fuzz-journald.c b/src/fuzz/fuzz-journald.c
4fbe94
index f271d7f2fe..0659b92ba3 100644
4fbe94
--- a/src/fuzz/fuzz-journald.c
4fbe94
+++ b/src/fuzz/fuzz-journald.c
4fbe94
@@ -5,12 +5,29 @@
4fbe94
 #include "journald-server.h"
4fbe94
 #include "sd-event.h"
4fbe94
 
4fbe94
+void dummy_server_init(Server *s, const uint8_t *buffer, size_t size) {
4fbe94
+        *s = (Server) {
4fbe94
+                .syslog_fd = -1,
4fbe94
+                .native_fd = -1,
4fbe94
+                .stdout_fd = -1,
4fbe94
+                .dev_kmsg_fd = -1,
4fbe94
+                .audit_fd = -1,
4fbe94
+                .hostname_fd = -1,
4fbe94
+                .notify_fd = -1,
4fbe94
+                .storage = STORAGE_NONE,
4fbe94
+        };
4fbe94
+        assert_se(sd_event_default(&s->event) >= 0);
4fbe94
+        s->buffer = memdup_suffix0(buffer, size);
4fbe94
+        assert_se(s->buffer);
4fbe94
+        s->buffer_size = size + 1;
4fbe94
+}
4fbe94
+
4fbe94
 void fuzz_journald_processing_function(
4fbe94
                 const uint8_t *data,
4fbe94
                 size_t size,
4fbe94
                 void (*f)(Server *s, const char *buf, size_t raw_len, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len)
4fbe94
         ) {
4fbe94
-        Server s = {};
4fbe94
+        Server s;
4fbe94
         char *label = NULL;
4fbe94
         size_t label_len = 0;
4fbe94
         struct ucred *ucred = NULL;
4fbe94
@@ -19,12 +36,7 @@ void fuzz_journald_processing_function(
4fbe94
         if (size == 0)
4fbe94
                 return;
4fbe94
 
4fbe94
-        assert_se(sd_event_default(&s.event) >= 0);
4fbe94
-        s.syslog_fd = s.native_fd = s.stdout_fd = s.dev_kmsg_fd = s.audit_fd = s.hostname_fd = s.notify_fd = -1;
4fbe94
-        s.buffer = memdup_suffix0(data, size);
4fbe94
-        assert_se(s.buffer);
4fbe94
-        s.buffer_size = size + 1;
4fbe94
-        s.storage = STORAGE_NONE;
4fbe94
+        dummy_server_init(&s, data, size);
4fbe94
         (*f)(&s, s.buffer, size, ucred, tv, label, label_len);
4fbe94
         server_done(&s);
4fbe94
 }
4fbe94
diff --git a/src/fuzz/fuzz-journald.h b/src/fuzz/fuzz-journald.h
4fbe94
index e9d32a74aa..77e3b0c064 100644
4fbe94
--- a/src/fuzz/fuzz-journald.h
4fbe94
+++ b/src/fuzz/fuzz-journald.h
4fbe94
@@ -3,6 +3,8 @@
4fbe94
 
4fbe94
 #include "journald-server.h"
4fbe94
 
4fbe94
+void dummy_server_init(Server *s, const uint8_t *buffer, size_t size);
4fbe94
+
4fbe94
 void fuzz_journald_processing_function(
4fbe94
                 const uint8_t *data,
4fbe94
                 size_t size,
4fbe94
diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build
4fbe94
index 5548da3822..897c02e4ae 100644
4fbe94
--- a/src/fuzz/meson.build
4fbe94
+++ b/src/fuzz/meson.build
4fbe94
@@ -33,12 +33,14 @@ fuzzers += [
4fbe94
           libshared],
4fbe94
          [libmount]],
4fbe94
 
4fbe94
-        [['src/fuzz/fuzz-journald-audit.c'],
4fbe94
+        [['src/fuzz/fuzz-journald-audit.c',
4fbe94
+          'src/fuzz/fuzz-journald.c'],
4fbe94
          [libjournal_core,
4fbe94
           libshared],
4fbe94
          [libselinux]],
4fbe94
 
4fbe94
-        [['src/fuzz/fuzz-journald-kmsg.c'],
4fbe94
+        [['src/fuzz/fuzz-journald-kmsg.c',
4fbe94
+          'src/fuzz/fuzz-journald.c'],
4fbe94
          [libjournal_core,
4fbe94
           libshared],
4fbe94
          [libselinux]],