594167
From f06002981d2bd2a582d2252f7d509205bcc2a9ed Mon Sep 17 00:00:00 2001
594167
From: Evgeny Vereshchagin <evvers@ya.ru>
594167
Date: Sun, 26 Dec 2021 23:26:56 +0000
594167
Subject: [PATCH] tests: add fuzz-bcd
594167
594167
(cherry picked from commit 4b65fc8725fa169bf870eb022d7b346796977c21)
594167
594167
Related: #2017035
594167
---
594167
 src/boot/efi/fuzz-bcd.c  | 26 ++++++++++++++++++++++++++
594167
 src/boot/efi/meson.build |  3 +++
594167
 tools/oss-fuzz.sh        | 16 ++++++++++++++++
594167
 3 files changed, 45 insertions(+)
594167
 create mode 100644 src/boot/efi/fuzz-bcd.c
594167
594167
diff --git a/src/boot/efi/fuzz-bcd.c b/src/boot/efi/fuzz-bcd.c
594167
new file mode 100644
594167
index 0000000000..e5ed6638a4
594167
--- /dev/null
594167
+++ b/src/boot/efi/fuzz-bcd.c
594167
@@ -0,0 +1,26 @@
594167
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
+
594167
+#include "alloc-util.h"
594167
+#include "fd-util.h"
594167
+#include "fuzz.h"
594167
+#include "utf8.h"
594167
+
594167
+#include "bcd.c"
594167
+
594167
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
+        _cleanup_free_ void *p = NULL;
594167
+
594167
+        /* This limit was borrowed from src/boot/efi/boot.c */
594167
+        if (size > 100*1024)
594167
+                return 0;
594167
+
594167
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
594167
+                log_set_max_level(LOG_CRIT);
594167
+
594167
+        p = memdup(data, size);
594167
+        assert_se(p);
594167
+
594167
+        char16_t *title = get_bcd_title(p, size);
594167
+        assert_se(!title || char16_strlen(title) >= 0);
594167
+        return 0;
594167
+}
594167
diff --git a/src/boot/efi/meson.build b/src/boot/efi/meson.build
594167
index 16b34f0ac2..229771026d 100644
594167
--- a/src/boot/efi/meson.build
594167
+++ b/src/boot/efi/meson.build
594167
@@ -358,6 +358,9 @@ if efi_arch[1] in ['ia32', 'x86_64', 'arm', 'aarch64']
594167
                  [],
594167
                  'HAVE_ZSTD'],
594167
         ]
594167
+        fuzzers += [
594167
+                [['src/boot/efi/fuzz-bcd.c']],
594167
+        ]
594167
 endif
594167
 
594167
 systemd_boot_objects = []
594167
diff --git a/tools/oss-fuzz.sh b/tools/oss-fuzz.sh
594167
index 8a19da665e..ae57fc25d5 100755
594167
--- a/tools/oss-fuzz.sh
594167
+++ b/tools/oss-fuzz.sh
594167
@@ -36,6 +36,13 @@ else
594167
     apt-get install -y gperf m4 gettext python3-pip \
594167
         libcap-dev libmount-dev libkmod-dev \
594167
         pkg-config wget python3-jinja2
594167
+
594167
+    # gnu-efi is installed here to enable -Dgnu-efi behind which fuzz-bcd
594167
+    # is hidden. It isn't linked against efi. It doesn't
594167
+    # even include "efi.h" because "bcd.c" can work in "unit test" mode
594167
+    # where it isn't necessary.
594167
+    apt-get install -y gnu-efi zstd
594167
+
594167
     pip3 install -r .github/workflows/requirements.txt --require-hashes
594167
 
594167
     # https://github.com/google/oss-fuzz/issues/6868
594167
@@ -56,6 +63,15 @@ fi
594167
 
594167
 ninja -v -C "$build" fuzzers
594167
 
594167
+# Compressed BCD files are kept in test/test-bcd so let's unpack them
594167
+# and put them all in the seed corpus.
594167
+bcd=$(mktemp -d)
594167
+for i in test/test-bcd/*.zst; do
594167
+     unzstd "$i" -o "$bcd/$(basename "${i%.zst}")";
594167
+done
594167
+zip -jqr "$OUT/fuzz-bcd_seed_corpus.zip" "$bcd"
594167
+rm -rf "$bcd"
594167
+
594167
 # The seed corpus is a separate flat archive for each fuzzer,
594167
 # with a fixed name ${fuzzer}_seed_corpus.zip.
594167
 for d in "$(dirname "$0")/../test/fuzz/fuzz-"*; do