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