richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
594167
From 25338c37915521876c84bca196de50d73c3c17ea Mon Sep 17 00:00:00 2001
594167
From: Frantisek Sumsal <frantisek@sumsal.cz>
594167
Date: Mon, 13 Dec 2021 20:50:28 +0100
594167
Subject: [PATCH] test: fuzz our dbus interfaces with dfuzzer
594167
594167
(cherry picked from commit 354b3364aa63620a0f732bb8a6fe9332a4f550e4)
594167
594167
Related: #2087652
594167
---
594167
 test/TEST-21-DFUZZER/Makefile   |  1 +
594167
 test/TEST-21-DFUZZER/test.sh    | 24 +++++++++
594167
 test/test-functions             |  1 +
594167
 test/units/testsuite-21.service | 10 ++++
594167
 test/units/testsuite-21.sh      | 94 +++++++++++++++++++++++++++++++++
594167
 5 files changed, 130 insertions(+)
594167
 create mode 120000 test/TEST-21-DFUZZER/Makefile
594167
 create mode 100755 test/TEST-21-DFUZZER/test.sh
594167
 create mode 100644 test/units/testsuite-21.service
594167
 create mode 100755 test/units/testsuite-21.sh
594167
594167
diff --git a/test/TEST-21-DFUZZER/Makefile b/test/TEST-21-DFUZZER/Makefile
594167
new file mode 120000
594167
index 0000000000..e9f93b1104
594167
--- /dev/null
594167
+++ b/test/TEST-21-DFUZZER/Makefile
594167
@@ -0,0 +1 @@
594167
+../TEST-01-BASIC/Makefile
594167
\ No newline at end of file
594167
diff --git a/test/TEST-21-DFUZZER/test.sh b/test/TEST-21-DFUZZER/test.sh
594167
new file mode 100755
594167
index 0000000000..ecc04e368c
594167
--- /dev/null
594167
+++ b/test/TEST-21-DFUZZER/test.sh
594167
@@ -0,0 +1,24 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -e
594167
+
594167
+TEST_DESCRIPTION="Fuzz our D-Bus interfaces with dfuzzer"
594167
+TEST_NO_NSPAWN=1
594167
+TEST_SUPPORTING_SERVICES_SHOULD_BE_MASKED=0
594167
+QEMU_TIMEOUT="${QEMU_TIMEOUT:-1800}"
594167
+
594167
+# shellcheck source=test/test-functions
594167
+. "${TEST_BASE_DIR:?}/test-functions"
594167
+
594167
+command -v dfuzzer >/dev/null || exit 0
594167
+
594167
+test_append_files() {
594167
+    local workspace="${1:?}"
594167
+
594167
+    image_install dfuzzer /etc/dfuzzer.conf
594167
+
594167
+    # Enable all systemd-related services, including the D-Bus ones
594167
+    "$SYSTEMCTL" --root="${workspace:?}" preset-all
594167
+}
594167
+
594167
+do_test "$@"
594167
diff --git a/test/test-functions b/test/test-functions
594167
index 44f465c914..079a7249e4 100644
594167
--- a/test/test-functions
594167
+++ b/test/test-functions
594167
@@ -99,6 +99,7 @@ SYSTEMD_JOURNAL_REMOTE="${SYSTEMD_JOURNAL_REMOTE:-$(command -v "$BUILD_DIR/syste
594167
 SYSTEMD="${SYSTEMD:-$(command -v "$BUILD_DIR/systemd" || command -v "$ROOTLIBDIR/systemd")}"
594167
 SYSTEMD_NSPAWN="${SYSTEMD_NSPAWN:-$(command -v "$BUILD_DIR/systemd-nspawn" || command -v systemd-nspawn)}"
594167
 JOURNALCTL="${JOURNALCTL:-$(command -v "$BUILD_DIR/journalctl" || command -v journalctl)}"
594167
+SYSTEMCTL="${SYSTEMCTL:-$(command -v "$BUILD_DIR/systemctl" || command -v systemctl)}"
594167
 
594167
 TESTFILE="${BASH_SOURCE[1]}"
594167
 if [ -z "$TESTFILE" ]; then
594167
diff --git a/test/units/testsuite-21.service b/test/units/testsuite-21.service
594167
new file mode 100644
594167
index 0000000000..a5f77d07b4
594167
--- /dev/null
594167
+++ b/test/units/testsuite-21.service
594167
@@ -0,0 +1,10 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+[Unit]
594167
+Description=Fuzz our D-Bus interfaces with dfuzzer
594167
+After=dbus.service multi-user.target
594167
+Wants=dbus.service multi-user.target
594167
+
594167
+[Service]
594167
+ExecStartPre=rm -f /failed /skipped /testok
594167
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
594167
+Type=oneshot
594167
diff --git a/test/units/testsuite-21.sh b/test/units/testsuite-21.sh
594167
new file mode 100755
594167
index 0000000000..43b5fb6f22
594167
--- /dev/null
594167
+++ b/test/units/testsuite-21.sh
594167
@@ -0,0 +1,94 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -eux
594167
+set -o pipefail
594167
+
594167
+# Save the end.service state before we start fuzzing, as it might get changed
594167
+# on the fly by one of the fuzzers
594167
+systemctl list-jobs | grep -F 'end.service' && SHUTDOWN_AT_EXIT=1 || SHUTDOWN_AT_EXIT=0
594167
+
594167
+at_exit() {
594167
+    # "Safety net" - check for any coredumps which might have not caused dfuzzer
594167
+    # to stop & return an error (we need to do this now before truncating the
594167
+    # journal)
594167
+    # TODO: check fo ASan/UBSan errors
594167
+    local found_cd=0
594167
+    while read -r exe; do
594167
+        coredumctl info "$exe"
594167
+        found_cd=1
594167
+    done < <(coredumpctl -F COREDUMP_EXE | sort -u)
594167
+    [[ $found_cd -eq 0 ]] || exit 1
594167
+
594167
+    # We have to call the end.service explicitly even if it's specified on
594167
+    # the kernel cmdline via systemd.wants=end.service, since dfuzzer calls
594167
+    # org.freedesktop.systemd1.Manager.ClearJobs() which drops the service
594167
+    # from the queue
594167
+    [[ $SHUTDOWN_AT_EXIT -ne 0 ]] && systemctl start --job-mode=flush end.service
594167
+}
594167
+
594167
+trap at_exit EXIT
594167
+
594167
+systemctl log-level info
594167
+
594167
+# TODO
594167
+#   * check for possibly newly introduced buses?
594167
+BUS_LIST=(
594167
+    org.freedesktop.home1
594167
+    org.freedesktop.hostname1
594167
+    org.freedesktop.import1
594167
+    org.freedesktop.locale1
594167
+    org.freedesktop.login1
594167
+    org.freedesktop.machine1
594167
+    org.freedesktop.network1
594167
+    org.freedesktop.portable1
594167
+    org.freedesktop.resolve1
594167
+    org.freedesktop.systemd1
594167
+    org.freedesktop.timedate1
594167
+    org.freedesktop.timesync1
594167
+)
594167
+
594167
+# systemd-oomd requires PSI
594167
+if tail -n +1 /proc/pressure/{cpu,io,memory}; then
594167
+    BUS_LIST+=(org.freedesktop.oom1)
594167
+fi
594167
+
594167
+SESSION_BUS_LIST=(
594167
+    org.freedesktop.systemd1
594167
+)
594167
+
594167
+# Maximum payload size generated by dfuzzer (in bytes) - default: 50K
594167
+PAYLOAD_MAX=50000
594167
+# Tweak the maximum payload size if we're running under sanitizers, since
594167
+# with larger payloads we start hitting reply timeouts
594167
+if [[ -v ASAN_OPTIONS || -v UBSAN_OPTIONS ]]; then
594167
+    PAYLOAD_MAX=10000 # 10K
594167
+fi
594167
+
594167
+# Overmount /var/lib/machines with a size-limited tmpfs, as fuzzing
594167
+# the org.freedesktop.machine1 stuff makes quite a mess
594167
+mount -t tmpfs -o size=50M tmpfs /var/lib/machines
594167
+
594167
+# Fuzz both the system and the session buses (where applicable)
594167
+for bus in "${BUS_LIST[@]}"; do
594167
+    echo "Bus: $bus (system)"
594167
+    systemd-run --pipe --wait \
594167
+                -- dfuzzer -v -b "$PAYLOAD_MAX" -n "$bus"
594167
+
594167
+    # Let's reload the systemd daemon to test (de)serialization as well
594167
+    systemctl daemon-reload
594167
+done
594167
+
594167
+umount /var/lib/machines
594167
+
594167
+for bus in "${SESSION_BUS_LIST[@]}"; do
594167
+    echo "Bus: $bus (session)"
594167
+    systemd-run --machine 'testuser@.host' --user --pipe --wait \
594167
+                -- dfuzzer -v -b "$PAYLOAD_MAX" -n "$bus"
594167
+
594167
+    # Let's reload the systemd user daemon to test (de)serialization as well
594167
+    systemctl --machine 'testuser@.host' --user daemon-reload
594167
+done
594167
+
594167
+echo OK >/testok
594167
+
594167
+exit 0