daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ff6046
From 9a6a36b44ad131036fef5c91edc86c842c9821ba Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Sat, 7 Jul 2018 19:30:25 +0200
ff6046
Subject: [PATCH] fuzz-bus-message: add fuzzer for message parsing
ff6046
ff6046
As with other fuzzers, SYSTEMD_FUZZ_OUTPUT=1 and SYSTEMD_LOG_LEVEL=debug can be
ff6046
used for debugging.
ff6046
ff6046
(cherry picked from commit 56b560c26339c4b282c06038316a91509eae75fd)
ff6046
ff6046
Resolves: #1696224
ff6046
---
ff6046
 src/fuzz/fuzz-bus-message.c         |  47 ++++++++++++++++++++++++++++
ff6046
 src/fuzz/meson.build                |   4 +++
ff6046
 test/fuzz/fuzz-bus-message/message1 | Bin 0 -> 534 bytes
ff6046
 3 files changed, 51 insertions(+)
ff6046
 create mode 100644 src/fuzz/fuzz-bus-message.c
ff6046
 create mode 100644 test/fuzz/fuzz-bus-message/message1
ff6046
ff6046
diff --git a/src/fuzz/fuzz-bus-message.c b/src/fuzz/fuzz-bus-message.c
ff6046
new file mode 100644
ff6046
index 0000000000..9842c62a6f
ff6046
--- /dev/null
ff6046
+++ b/src/fuzz/fuzz-bus-message.c
ff6046
@@ -0,0 +1,47 @@
ff6046
+/* SPDX-License-Identifier: LGPL-2.1+ */
ff6046
+
ff6046
+#include <errno.h>
ff6046
+#include <stdio.h>
ff6046
+
ff6046
+#include "alloc-util.h"
ff6046
+#include "bus-dump.h"
ff6046
+#include "bus-message.h"
ff6046
+#include "env-util.h"
ff6046
+#include "fd-util.h"
ff6046
+#include "fuzz.h"
ff6046
+
ff6046
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
ff6046
+        _cleanup_free_ char *out = NULL; /* out should be freed after g */
ff6046
+        size_t out_size;
ff6046
+        _cleanup_fclose_ FILE *g = NULL;
ff6046
+        _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
ff6046
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
ff6046
+        _cleanup_free_ void *buffer = NULL;
ff6046
+        int r;
ff6046
+
ff6046
+        /* We don't want to fill the logs with messages about parse errors.
ff6046
+         * Disable most logging if not running standalone */
ff6046
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
ff6046
+                log_set_max_level(LOG_CRIT);
ff6046
+
ff6046
+        r = sd_bus_new(&bus;;
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        assert_se(buffer = memdup(data, size));
ff6046
+
ff6046
+        r = bus_message_from_malloc(bus, buffer, size, NULL, 0, NULL, &m);
ff6046
+        if (r == -EBADMSG)
ff6046
+                return 0;
ff6046
+        assert_se(r >= 0);
ff6046
+        TAKE_PTR(buffer);
ff6046
+
ff6046
+        if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
ff6046
+                assert_se(g = open_memstream(&out, &out_size));
ff6046
+
ff6046
+        bus_message_dump(m, g ?: stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
ff6046
+
ff6046
+        r = sd_bus_message_rewind(m, true);
ff6046
+        assert_se(r >= 0);
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
diff --git a/src/fuzz/meson.build b/src/fuzz/meson.build
ff6046
index 5c81ac0c5b..1dbe28e57e 100644
ff6046
--- a/src/fuzz/meson.build
ff6046
+++ b/src/fuzz/meson.build
ff6046
@@ -1,6 +1,10 @@
ff6046
 # SPDX-License-Identifier: LGPL-2.1+
ff6046
 
ff6046
 fuzzers += [
ff6046
+        [['src/fuzz/fuzz-bus-message.c'],
ff6046
+         [libshared],
ff6046
+         []],
ff6046
+
ff6046
         [['src/fuzz/fuzz-dns-packet.c',
ff6046
           dns_type_headers],
ff6046
          [libsystemd_resolve_core,
ff6046
diff --git a/test/fuzz/fuzz-bus-message/message1 b/test/fuzz/fuzz-bus-message/message1
ff6046
new file mode 100644
ff6046
index 0000000000000000000000000000000000000000..2df70fd7cb6f0e632c4d5c2358091309a5cd3edc
ff6046
GIT binary patch
ff6046
literal 534
ff6046
zcmZ{h!A`?442GSJjTUi2h$EV`OM6*iyZ|>&NW6m6ZC#~`RCNGV2*icg27V{4hLEuI
ff6046
z*Z%6nv6IG-c{fDW8PO*Z8RG~@1*A4LLPziq^|n=>fKTCf&ROnOFWhXL{-6KzKQR>*
ff6046
zA}kdo{MtXi^_lPUKI=U`x#dhG*Hq0
ff6046
zruVss?wLceSE4J#)5yVN0GffvA#~1mm{Zra+=e{w{I&z@*?J#i4Is_HhZ+f`nuHx|
ff6046
zld${fh;=jUB)V|NE5y2-nFH%A%GTPz>w(L%415E=fPT+(I2*knx96tO2RVnnVP6o!
ff6046
Yuz#WfEX)Cqd!b_JHzYppZsXhk08nC8%>V!Z
ff6046
ff6046
literal 0
ff6046
HcmV?d00001
ff6046