Brian Stinson 2593d8
From e7e70f575840cd021f6429f264911ae0cbff9741 Mon Sep 17 00:00:00 2001
Brian Stinson 2593d8
From: Evgeny Vereshchagin <evvers@ya.ru>
Brian Stinson 2593d8
Date: Thu, 15 Nov 2018 17:52:57 +0100
Brian Stinson 2593d8
Subject: [PATCH] tests: add a fuzzer for dev_kmsg_record
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit 8857fb9beb9dcb95a6ce1be14dc94c4dc4cd3ea3)
Brian Stinson 2593d8
Brian Stinson 2593d8
Resolves: #1764560
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/fuzz/fuzz-journald-kmsg.c                 | 29 +++++++++++++++++++
Brian Stinson 2593d8
 src/fuzz/meson.build                          |  5 ++++
Brian Stinson 2593d8
 src/journal/journald-kmsg.c                   |  2 +-
Brian Stinson 2593d8
 src/journal/journald-kmsg.h                   |  2 ++
Brian Stinson 2593d8
 test/fuzz/fuzz-journald-kmsg/basic            |  1 +
Brian Stinson 2593d8
 test/fuzz/fuzz-journald-kmsg/dev-null         |  2 ++
Brian Stinson 2593d8
 test/fuzz/fuzz-journald-kmsg/loopback         |  2 ++
Brian Stinson 2593d8
 .../fuzz-journald-kmsg/subsystem-loopback     |  2 ++
Brian Stinson 2593d8
 8 files changed, 44 insertions(+), 1 deletion(-)
Brian Stinson 2593d8
 create mode 100644 src/fuzz/fuzz-journald-kmsg.c
Brian Stinson 2593d8
 create mode 100644 test/fuzz/fuzz-journald-kmsg/basic
Brian Stinson 2593d8
 create mode 100644 test/fuzz/fuzz-journald-kmsg/dev-null
Brian Stinson 2593d8
 create mode 100644 test/fuzz/fuzz-journald-kmsg/loopback
Brian Stinson 2593d8
 create mode 100644 test/fuzz/fuzz-journald-kmsg/subsystem-loopback
Brian Stinson 2593d8
Brian Stinson 2593d8
diff --git a/src/fuzz/fuzz-journald-kmsg.c b/src/fuzz/fuzz-journald-kmsg.c
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..5d99d244b5
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/src/fuzz/fuzz-journald-kmsg.c
Brian Stinson 2593d8
@@ -0,0 +1,29 @@
Brian Stinson 2593d8
+/* SPDX-License-Identifier: LGPL-2.1+ */
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+#include "fuzz.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
+
Brian Stinson 2593d8
+        if (size == 0)
Brian Stinson 2593d8
+                return 0;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        s = (Server) {
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
+        server_done(&s);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        return 0;
Brian Stinson 2593d8
+}
Brian Stinson 2593d8
diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build
Brian Stinson 2593d8
index 1f8631bcc0..0520e448a9 100644
Brian Stinson 2593d8
--- a/src/fuzz/meson.build
Brian Stinson 2593d8
+++ b/src/fuzz/meson.build
Brian Stinson 2593d8
@@ -33,6 +33,11 @@ fuzzers += [
Brian Stinson 2593d8
           libshared],
Brian Stinson 2593d8
          [libmount]],
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+        [['src/fuzz/fuzz-journald-kmsg.c'],
Brian Stinson 2593d8
+         [libjournal_core,
Brian Stinson 2593d8
+          libshared],
Brian Stinson 2593d8
+         [libselinux]],
Brian Stinson 2593d8
+
Brian Stinson 2593d8
         [['src/fuzz/fuzz-journald-native.c',
Brian Stinson 2593d8
           'src/fuzz/fuzz-journald.c'],
Brian Stinson 2593d8
          [libjournal_core,
Brian Stinson 2593d8
diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c
Brian Stinson 2593d8
index 7644bebfc8..0cdf1c4794 100644
Brian Stinson 2593d8
--- a/src/journal/journald-kmsg.c
Brian Stinson 2593d8
+++ b/src/journal/journald-kmsg.c
Brian Stinson 2593d8
@@ -93,7 +93,7 @@ static bool is_us(const char *identifier, const char *pid) {
Brian Stinson 2593d8
                streq(identifier, program_invocation_short_name);
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-static void dev_kmsg_record(Server *s, char *p, size_t l) {
Brian Stinson 2593d8
+void dev_kmsg_record(Server *s, char *p, size_t l) {
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         _cleanup_free_ char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL, *identifier = NULL, *pid = NULL;
Brian Stinson 2593d8
         struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS];
Brian Stinson 2593d8
diff --git a/src/journal/journald-kmsg.h b/src/journal/journald-kmsg.h
Brian Stinson 2593d8
index bff24ac310..2326bc8c93 100644
Brian Stinson 2593d8
--- a/src/journal/journald-kmsg.h
Brian Stinson 2593d8
+++ b/src/journal/journald-kmsg.h
Brian Stinson 2593d8
@@ -9,3 +9,5 @@ int server_flush_dev_kmsg(Server *s);
Brian Stinson 2593d8
 void server_forward_kmsg(Server *s, int priority, const char *identifier, const char *message, const struct ucred *ucred);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 int server_open_kernel_seqnum(Server *s);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+void dev_kmsg_record(Server *s, char *p, size_t l);
Brian Stinson 2593d8
diff --git a/test/fuzz/fuzz-journald-kmsg/basic b/test/fuzz/fuzz-journald-kmsg/basic
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..1299cd0869
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/test/fuzz/fuzz-journald-kmsg/basic
Brian Stinson 2593d8
@@ -0,0 +1 @@
Brian Stinson 2593d8
+29,456,292891883,-;systemd[1]: Reexecuting.
Brian Stinson 2593d8
diff --git a/test/fuzz/fuzz-journald-kmsg/dev-null b/test/fuzz/fuzz-journald-kmsg/dev-null
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..de039588b5
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/test/fuzz/fuzz-journald-kmsg/dev-null
Brian Stinson 2593d8
@@ -0,0 +1,2 @@
Brian Stinson 2593d8
+12,460,1322026586,-;hey
Brian Stinson 2593d8
+ DEVICE=c1:3
Brian Stinson 2593d8
diff --git a/test/fuzz/fuzz-journald-kmsg/loopback b/test/fuzz/fuzz-journald-kmsg/loopback
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..ca320177b7
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/test/fuzz/fuzz-journald-kmsg/loopback
Brian Stinson 2593d8
@@ -0,0 +1,2 @@
Brian Stinson 2593d8
+12,460,1322026586,-;hey
Brian Stinson 2593d8
+ DEVICE=n1
Brian Stinson 2593d8
diff --git a/test/fuzz/fuzz-journald-kmsg/subsystem-loopback b/test/fuzz/fuzz-journald-kmsg/subsystem-loopback
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..af9c0d91e5
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/test/fuzz/fuzz-journald-kmsg/subsystem-loopback
Brian Stinson 2593d8
@@ -0,0 +1,2 @@
Brian Stinson 2593d8
+12,460,1322026586,-;hey
Brian Stinson 2593d8
+ DEVICE=+net:lo