Brian Stinson 2593d8
From 76e2fa8ed4bbee7c625e3b790f2e38a59fffd93d Mon Sep 17 00:00:00 2001
Brian Stinson 2593d8
From: Evgeny Vereshchagin <evvers@ya.ru>
Brian Stinson 2593d8
Date: Fri, 23 Nov 2018 00:27:19 +0100
Brian Stinson 2593d8
Subject: [PATCH] tests: add a fuzzer for server_process_native_file
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit a4aa59bae206eebb4703b291147144def5d4bb3e)
Brian Stinson 2593d8
Brian Stinson 2593d8
Resolves: #1764560
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/fuzz/fuzz-journald-native-fd.c      |  47 ++++++++++++++++++++++++
Brian Stinson 2593d8
 src/fuzz/meson.build                    |   6 +++
Brian Stinson 2593d8
 test/fuzz/fuzz-journald-native-fd/basic | Bin 0 -> 34 bytes
Brian Stinson 2593d8
 3 files changed, 53 insertions(+)
Brian Stinson 2593d8
 create mode 100644 src/fuzz/fuzz-journald-native-fd.c
Brian Stinson 2593d8
 create mode 100644 test/fuzz/fuzz-journald-native-fd/basic
Brian Stinson 2593d8
Brian Stinson 2593d8
diff --git a/src/fuzz/fuzz-journald-native-fd.c b/src/fuzz/fuzz-journald-native-fd.c
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000..95415d9f85
Brian Stinson 2593d8
--- /dev/null
Brian Stinson 2593d8
+++ b/src/fuzz/fuzz-journald-native-fd.c
Brian Stinson 2593d8
@@ -0,0 +1,47 @@
Brian Stinson 2593d8
+/* SPDX-License-Identifier: LGPL-2.1+ */
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+#include "fd-util.h"
Brian Stinson 2593d8
+#include "fileio.h"
Brian Stinson 2593d8
+#include "fs-util.h"
Brian Stinson 2593d8
+#include "fuzz.h"
Brian Stinson 2593d8
+#include "fuzz-journald.h"
Brian Stinson 2593d8
+#include "journald-native.h"
Brian Stinson 2593d8
+#include "memfd-util.h"
Brian Stinson 2593d8
+#include "process-util.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_close_ int sealed_fd = -1, unsealed_fd = -1;
Brian Stinson 2593d8
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/fuzz-journald-native-fd.XXXXXX";
Brian Stinson 2593d8
+        char *label = NULL;
Brian Stinson 2593d8
+        size_t label_len = 0;
Brian Stinson 2593d8
+        struct ucred ucred;
Brian Stinson 2593d8
+        struct timeval *tv = NULL;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
Brian Stinson 2593d8
+                log_set_max_level(LOG_CRIT);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        dummy_server_init(&s, NULL, 0);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        sealed_fd = memfd_new(NULL);
Brian Stinson 2593d8
+        assert_se(sealed_fd >= 0);
Brian Stinson 2593d8
+        assert_se(write(sealed_fd, data, size) == (ssize_t) size);
Brian Stinson 2593d8
+        assert_se(memfd_set_sealed(sealed_fd) >= 0);
Brian Stinson 2593d8
+        assert_se(lseek(sealed_fd, 0, SEEK_SET) == 0);
Brian Stinson 2593d8
+        ucred = (struct ucred) {
Brian Stinson 2593d8
+                .pid = getpid_cached(),
Brian Stinson 2593d8
+                .uid = geteuid(),
Brian Stinson 2593d8
+                .gid = getegid(),
Brian Stinson 2593d8
+        };
Brian Stinson 2593d8
+        server_process_native_file(&s, sealed_fd, &ucred, tv, label, label_len);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        unsealed_fd = mkostemp_safe(name);
Brian Stinson 2593d8
+        assert_se(unsealed_fd >= 0);
Brian Stinson 2593d8
+        assert_se(write(unsealed_fd, data, size) == (ssize_t) size);
Brian Stinson 2593d8
+        assert_se(lseek(unsealed_fd, 0, SEEK_SET) == 0);
Brian Stinson 2593d8
+        server_process_native_file(&s, unsealed_fd, &ucred, tv, label, label_len);
Brian Stinson 2593d8
+
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 eea9117360..5315d2771c 100644
Brian Stinson 2593d8
--- a/src/fuzz/meson.build
Brian Stinson 2593d8
+++ b/src/fuzz/meson.build
Brian Stinson 2593d8
@@ -51,6 +51,12 @@ fuzzers += [
Brian Stinson 2593d8
           libshared],
Brian Stinson 2593d8
          [libselinux]],
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+        [['src/fuzz/fuzz-journald-native-fd.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-stream.c',
Brian Stinson 2593d8
           'src/fuzz/fuzz-journald.c'],
Brian Stinson 2593d8
          [libjournal_core,
Brian Stinson 2593d8
diff --git a/test/fuzz/fuzz-journald-native-fd/basic b/test/fuzz/fuzz-journald-native-fd/basic
Brian Stinson 2593d8
new file mode 100644
Brian Stinson 2593d8
index 0000000000000000000000000000000000000000..65f89705a655618851c0e446efaa5c633adf425f
Brian Stinson 2593d8
GIT binary patch
Brian Stinson 2593d8
literal 34
Brian Stinson 2593d8
kcmeZu4Gwm6cjaPXfB;7>M@KGyCofm$koW*k7h3}^0B8*cRR910
Brian Stinson 2593d8
Brian Stinson 2593d8
literal 0
Brian Stinson 2593d8
HcmV?d00001
Brian Stinson 2593d8