daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
4cad4c
From 242273e1afd456e86ebc48d7d601cb28297f8efb Mon Sep 17 00:00:00 2001
4cad4c
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
4cad4c
Date: Tue, 30 Oct 2018 09:02:26 +0100
4cad4c
Subject: [PATCH] fuzz-compress: add fuzzer for compression and decompression
4cad4c
4cad4c
(cherry picked from commit 029427043b2e0523a21f54374f872b23cf744350)
4cad4c
Resolves: #1843871
4cad4c
---
4cad4c
 src/fuzz/fuzz-compress.c | 80 ++++++++++++++++++++++++++++++++++++++++
4cad4c
 src/fuzz/meson.build     |  7 +++-
4cad4c
 2 files changed, 86 insertions(+), 1 deletion(-)
4cad4c
 create mode 100644 src/fuzz/fuzz-compress.c
4cad4c
4cad4c
diff --git a/src/fuzz/fuzz-compress.c b/src/fuzz/fuzz-compress.c
4cad4c
new file mode 100644
4cad4c
index 0000000000..9c5dfc92c0
4cad4c
--- /dev/null
4cad4c
+++ b/src/fuzz/fuzz-compress.c
4cad4c
@@ -0,0 +1,80 @@
4cad4c
+/* SPDX-License-Identifier: LGPL-2.1+ */
4cad4c
+
4cad4c
+#include <errno.h>
4cad4c
+
4cad4c
+#include "alloc-util.h"
4cad4c
+#include "compress.h"
4cad4c
+#include "fuzz.h"
4cad4c
+
4cad4c
+static int compress(int alg,
4cad4c
+                    const void *src, uint64_t src_size,
4cad4c
+                    void *dst, size_t dst_alloc_size, size_t *dst_size) {
4cad4c
+
4cad4c
+        if (alg == OBJECT_COMPRESSED_LZ4)
4cad4c
+                return compress_blob_lz4(src, src_size, dst, dst_alloc_size, dst_size);
4cad4c
+        if (alg == OBJECT_COMPRESSED_XZ)
4cad4c
+                return compress_blob_xz(src, src_size, dst, dst_alloc_size, dst_size);
4cad4c
+        return -EOPNOTSUPP;
4cad4c
+}
4cad4c
+
4cad4c
+typedef struct header {
4cad4c
+        uint32_t alg:2; /* We have only two compression algorithms so far, but we might add
4cad4c
+                         * more in the future. Let's make this a bit wider so our fuzzer
4cad4c
+                         * cases remain stable in the future. */
4cad4c
+        uint32_t sw_len;
4cad4c
+        uint32_t sw_alloc;
4cad4c
+        uint32_t reserved[3]; /* Extra space to keep fuzz cases stable in case we need to
4cad4c
+                               * add stuff in the future. */
4cad4c
+        uint8_t data[];
4cad4c
+} header;
4cad4c
+
4cad4c
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
4cad4c
+        _cleanup_free_ void *buf = NULL, *buf2 = NULL;
4cad4c
+        int r;
4cad4c
+
4cad4c
+        if (size < offsetof(header, data) + 1)
4cad4c
+                return 0;
4cad4c
+
4cad4c
+        const header *h = (struct header*) data;
4cad4c
+        const size_t data_len = size - offsetof(header, data);
4cad4c
+
4cad4c
+        int alg = h->alg;
4cad4c
+
4cad4c
+        /* We don't want to fill the logs with messages about parse errors.
4cad4c
+         * Disable most logging if not running standalone */
4cad4c
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
4cad4c
+                log_set_max_level(LOG_CRIT);
4cad4c
+
4cad4c
+        log_info("Using compression %s, data size=%zu",
4cad4c
+                 object_compressed_to_string(alg) ?: "(none)",
4cad4c
+                 data_len);
4cad4c
+
4cad4c
+        buf = malloc(MAX(size, 128u)); /* Make the buffer a bit larger for very small data */
4cad4c
+        if (!buf) {
4cad4c
+                log_oom();
4cad4c
+                return 0;
4cad4c
+        }
4cad4c
+
4cad4c
+        size_t csize;
4cad4c
+        r = compress(alg, h->data, data_len, buf, size, &csize);
4cad4c
+        if (r < 0) {
4cad4c
+                log_error_errno(r, "Compression failed: %m");
4cad4c
+                return 0;
4cad4c
+        }
4cad4c
+
4cad4c
+        log_debug("Compressed %zu bytes to → %zu bytes", data_len, csize);
4cad4c
+
4cad4c
+        size_t sw_alloc = MAX(h->sw_alloc, 1u);
4cad4c
+        buf2 = malloc(sw_alloc);
4cad4c
+        if (!buf) {
4cad4c
+                log_oom();
4cad4c
+                return 0;
4cad4c
+        }
4cad4c
+
4cad4c
+        size_t sw_len = MIN(data_len - 1, h->sw_len);
4cad4c
+
4cad4c
+        r = decompress_startswith(alg, buf, csize, &buf2, &sw_alloc, h->data, sw_len, h->data[sw_len]);
4cad4c
+        assert_se(r > 0);
4cad4c
+
4cad4c
+        return 0;
4cad4c
+}
4cad4c
diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build
4cad4c
index 5315d2771c..b8d5979d3c 100644
4cad4c
--- a/src/fuzz/meson.build
4cad4c
+++ b/src/fuzz/meson.build
4cad4c
@@ -73,8 +73,13 @@ fuzzers += [
4cad4c
          [libsystemd_journal_remote,
4cad4c
           libshared],
4cad4c
          []],
4cad4c
+
4cad4c
         [['src/fuzz/fuzz-fido-id-desc.c',
4cad4c
           'src/udev/fido_id/fido_id_desc.c'],
4cad4c
          [],
4cad4c
-         []]
4cad4c
+         []],
4cad4c
+
4cad4c
+        [['src/fuzz/fuzz-compress.c'],
4cad4c
+         [libshared],
4cad4c
+         []],
4cad4c
 ]