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