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