ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
Brian Stinson 2593d8
From f991a9c7644f3fb5155ff823600ba5a6ea403dc4 Mon Sep 17 00:00:00 2001
Brian Stinson 2593d8
From: Evgeny Vereshchagin <evvers@ya.ru>
Brian Stinson 2593d8
Date: Fri, 16 Nov 2018 21:23:56 +0100
Brian Stinson 2593d8
Subject: [PATCH] tests: add a fuzzer for process_audit_string
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit 090a20cfaf3d5439fa39c5d8df473b0cfef181dd)
Brian Stinson 2593d8
Brian Stinson 2593d8
Resolves: #1764560
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/fuzz/fuzz-journald-audit.c      | 27 +++++++++++++++++++++++++++
Brian Stinson 2593d8
 src/fuzz/meson.build                |  5 +++++
Brian Stinson 2593d8
 src/journal/journald-audit.c        |  2 +-
Brian Stinson 2593d8
 src/journal/journald-audit.h        |  2 ++
Brian Stinson 2593d8
 test/fuzz/fuzz-journald-audit/basic |  1 +
Brian Stinson 2593d8
 5 files changed, 36 insertions(+), 1 deletion(-)
Brian Stinson 2593d8
 create mode 100644 src/fuzz/fuzz-journald-audit.c
Brian Stinson 2593d8
 create mode 100644 test/fuzz/fuzz-journald-audit/basic
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
new file mode 100644
Brian Stinson 2593d8
index 0000000000..fe401c0d98
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/src/fuzz/fuzz-journald-audit.c
Brian Stinson 2593d8
@@ -0,0 +1,27 @@
Brian Stinson 2593d8
+/* SPDX-License-Identifier: LGPL-2.1+ */
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+#include "fuzz.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
+        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 0520e448a9..5548da3822 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-audit.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
          [libjournal_core,
Brian Stinson 2593d8
           libshared],
Brian Stinson 2593d8
diff --git a/src/journal/journald-audit.c b/src/journal/journald-audit.c
Brian Stinson 2593d8
index 87726684af..7810a0139a 100644
Brian Stinson 2593d8
--- a/src/journal/journald-audit.c
Brian Stinson 2593d8
+++ b/src/journal/journald-audit.c
Brian Stinson 2593d8
@@ -313,7 +313,7 @@ static int map_all_fields(
Brian Stinson 2593d8
         }
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-static void process_audit_string(Server *s, int type, const char *data, size_t size) {
Brian Stinson 2593d8
+void process_audit_string(Server *s, int type, const char *data, size_t size) {
Brian Stinson 2593d8
         size_t n_iov_allocated = 0, n_iov = 0, z;
Brian Stinson 2593d8
         _cleanup_free_ struct iovec *iov = NULL;
Brian Stinson 2593d8
         uint64_t seconds, msec, id;
Brian Stinson 2593d8
diff --git a/src/journal/journald-audit.h b/src/journal/journald-audit.h
Brian Stinson 2593d8
index 57bb1711c9..7766618c98 100644
Brian Stinson 2593d8
--- a/src/journal/journald-audit.h
Brian Stinson 2593d8
+++ b/src/journal/journald-audit.h
Brian Stinson 2593d8
@@ -6,4 +6,6 @@
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 void server_process_audit_message(Server *s, const void *buffer, size_t buffer_size, const struct ucred *ucred, const union sockaddr_union *sa, socklen_t salen);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+void process_audit_string(Server *s, int type, const char *data, size_t size);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
 int server_open_audit(Server*s);
Brian Stinson 2593d8
diff --git a/test/fuzz/fuzz-journald-audit/basic b/test/fuzz/fuzz-journald-audit/basic
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..d1ce8cc5f0
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/test/fuzz/fuzz-journald-audit/basic
Brian Stinson 2593d8
@@ -0,0 +1 @@
Brian Stinson 2593d8
+audit(1542398162.211:744): pid=7376 uid=1000 auid=1000 ses=6 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 msg='op=PAM:accounting grantors=pam_unix,pam_localuser acct="vagrant" exe="/usr/bin/sudo" hostname=? addr=? terminal=/dev/pts/1 res=success'
Brian Stinson 2593d8
\ No newline at end of file