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