Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pgnU0-0007sj-Du for mharc-grub-devel@gnu.org; Mon, 27 Mar 2023 10:06:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pgnTx-0007pV-Ka for grub-devel@gnu.org; Mon, 27 Mar 2023 10:05:57 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pgnTm-0002rx-Qk for grub-devel@gnu.org; Mon, 27 Mar 2023 10:05:56 -0400 Received: by mail-pj1-x1030.google.com with SMTP id r7-20020a17090b050700b002404be7920aso7879563pjz.5 for ; Mon, 27 Mar 2023 07:05:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; t=1679925930; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=naieWAmPdJIQro2luMoLzupKW6tWhRnlWUnC7P1J0jo=; b=ftJhQalv/9m0g/rqKGyDhESWqHm6MPO6H1cuRdDsGYAwdD4WAWGFVMr2uPs9BjqBwo /Ggqr+IqJ2mUcaQ6WBpNtWfs26z1AhF7CtAr2yQw6+D5N6/RgClDPTM+E6gCIsZ3bhPp tAf0G/lEsNkgGPWQnhzHyV0+IGQpReFj/UL0I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679925930; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=naieWAmPdJIQro2luMoLzupKW6tWhRnlWUnC7P1J0jo=; b=Ebsx+Sc9eDOvdSLAr+OqI54xmrZLU+pU7eHQ9WENdyJUFXyYLkMe5f0SsQ92xXSoef RBpBpFuzBZg9ymfVqywpoxwAm1i5gHGH0LHVSI+Um+/lfMIztrpDhbZKimLRhNq8/ij4 CJ4kq0g+yjpT+ea9WnpwgjJ8YVylMoCA+X5fnRtKAoqU1xLnYTrdJNNJZ7wM1Jg4WOXj UtaU8NYBoT/uGMTbjLu2kBoERMUIFy5F6I6eUlGzJh9s0WkCh20Li+Ws8p5vYIpjzQ/+ LdIg9eOgmlCX72HmakoPQH/5yLI3t1NJrlkaiz2lduMV/KgcbLNb3fTzaAt0vGVlTttW oR+Q== X-Gm-Message-State: AAQBX9fhwno6pPGlyrtYEGsDcG+kNLBG1WdYk5V9IGIQNbPFlP+EDPkz yCSc6PTPpkPUXMea1mSTaue2Zk1XDJ+PMWgO+5k= X-Google-Smtp-Source: AKy350Z6gHH3N7YBvF0BNlmgkWqWNLJI3Nw9NJ/yitBIek2teVVM9kCiYC5PdnUWQtjXprRSCSaFjA== X-Received: by 2002:a17:902:d4cb:b0:19b:dbf7:f9ca with SMTP id o11-20020a170902d4cb00b0019bdbf7f9camr14607665plg.0.1679925930570; Mon, 27 Mar 2023 07:05:30 -0700 (PDT) Received: from linkitivity.lan (123-243-125-234.tpgi.com.au. [123.243.125.234]) by smtp.gmail.com with ESMTPSA id c11-20020a170902aa4b00b001a1fe42a141sm8493032plr.115.2023.03.27.07.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 07:05:29 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: Daniel Axtens Subject: [PATCH 3/6] tests: create a fuzzer for the png image format Date: Tue, 28 Mar 2023 01:04:56 +1100 Message-Id: <20230327140459.601018-4-dja@axtens.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230327140459.601018-1-dja@axtens.net> References: <20230327140459.601018-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=dja@axtens.net; helo=mail-pj1-x1030.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Mar 2023 14:05:57 -0000 Create a libfuzzer harness for the png image format. Once built, the fuzzer runs as: ./grub-fuzz-png With some hackery it can also be built with the address sanitizer or afl++. With regard to the license: I first worked on this while I was at IBM. I believe, looking at the fuzz fixes, that this would have been 2021-2022. The code was released to a cross-company grub security working group at this time. I left IBM in April 2022. Work subsequent to that is my own, and is unrelated to my subsequent employer. Signed-off-by: Daniel Axtens --- .gitignore | 1 + Makefile.util.def | 78 ++++++++++++++++++++++++++++++++++ grub-core/tests/fuzz/imageXX.c | 75 ++++++++++++++++++++++++++++++++ grub-core/tests/fuzz/png.c | 3 ++ 4 files changed, 157 insertions(+) create mode 100644 grub-core/tests/fuzz/imageXX.c create mode 100644 grub-core/tests/fuzz/png.c diff --git a/.gitignore b/.gitignore index 4064d3d1ec89..05cb43189156 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ grub-emu grub-emu-lite grub-emu-lite.exe grub-emu.exe +grub-fuzz-png grub-macho2img grub_emu_init.c grub_emu_init.h diff --git a/Makefile.util.def b/Makefile.util.def index beaef1168f0d..decced46ec19 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -53,6 +53,63 @@ library = { common = grub-core/fs/archelp.c; }; +library = { + name = libfuzzkern.a; + cflags = '$(CFLAGS_GNULIB) -fsanitize=fuzzer-no-link'; + cppflags = '$(CPPFLAGS_GNULIB) -I$(srcdir)/grub-core/lib/json -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION'; + + nostrip = common; + + condition = COND_ENABLE_FUZZERS; + + common = util/misc.c; + common = grub-core/kern/command.c; + common = grub-core/kern/device.c; + common = grub-core/kern/disk.c; + common = grub-core/lib/disk.c; + common = util/getroot.c; + common = grub-core/osdep/unix/getroot.c; + common = grub-core/osdep/getroot.c; + common = grub-core/osdep/devmapper/getroot.c; + common = grub-core/osdep/relpath.c; + extra_dist = grub-core/kern/disk_common.c; + extra_dist = grub-core/osdep/unix/relpath.c; + extra_dist = grub-core/osdep/aros/relpath.c; + extra_dist = grub-core/osdep/windows/relpath.c; + common = grub-core/kern/emu/hostdisk.c; + common = grub-core/osdep/devmapper/hostdisk.c; + common = grub-core/osdep/hostdisk.c; + common = grub-core/osdep/unix/hostdisk.c; + common = grub-core/osdep/exec.c; + common = grub-core/osdep/sleep.c; + common = grub-core/osdep/password.c; + common = grub-core/kern/emu/misc.c; + common = grub-core/kern/emu/mm.c; + common = grub-core/kern/env.c; + common = grub-core/kern/err.c; + common = grub-core/kern/file.c; + common = grub-core/kern/fs.c; + common = grub-core/kern/list.c; + common = grub-core/kern/misc.c; + common = grub-core/kern/partition.c; + common = grub-core/lib/crypto.c; + common = grub-core/lib/json/json.c; + common = grub-core/disk/luks.c; + common = grub-core/disk/luks2.c; + common = grub-core/disk/geli.c; + common = grub-core/disk/cryptodisk.c; + common = grub-core/disk/AFSplitter.c; + common = grub-core/lib/pbkdf2.c; + common = grub-core/commands/extcmd.c; + common = grub-core/lib/arg.c; + common = grub-core/disk/ldm.c; + common = grub-core/disk/diskfilter.c; + common = grub-core/partmap/gpt.c; + common = grub-core/partmap/msdos.c; + common = grub-core/fs/proc.c; + common = grub-core/fs/archelp.c; +}; + library = { name = libgrubmods.a; cflags = '-fno-builtin -Wno-undef'; @@ -1409,3 +1466,24 @@ program = { ldadd = grub-core/lib/gnulib/libgnu.a; ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; }; + +program = { + name = grub-fuzz-png; + + nostrip = common; + + common = grub-core/video/readers/png.c; + common = grub-core/video/bitmap.c; + common = grub-core/io/bufio.c; + + extra_dist = grub-core/tests/fuzz/imageXX.c; + common = grub-core/tests/fuzz/png.c; + + ldadd = 'libfuzzkern.a grub-core/lib/gnulib/libgnu.a $(LIBDEVMAPPER)'; + + cppflags = '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION'; + cflags='-fsanitize=fuzzer-no-link'; + ldflags='-fsanitize=fuzzer'; + + condition = COND_ENABLE_FUZZERS; +}; diff --git a/grub-core/tests/fuzz/imageXX.c b/grub-core/tests/fuzz/imageXX.c new file mode 100644 index 000000000000..4985cc5d0172 --- /dev/null +++ b/grub-core/tests/fuzz/imageXX.c @@ -0,0 +1,75 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2021-2023 Free Software Foundation, Inc. + * Copyright (C) 2021-2022 IBM Corporation + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +int LLVMFuzzerTestOneInput (const grub_uint8_t *Data, grub_size_t Size); +extern void MODULE_INIT (void); +extern void grub_procfs_init (void); + +static const grub_uint8_t *data; +static grub_size_t size; +static bool inited; +static struct grub_video_bitmap *bitmap; + +static char * +get_file_contents (grub_size_t *sz) +{ + char *ret; + *sz = size; + ret = grub_malloc (size); + if (ret) + grub_memcpy (ret, data, size); + return ret; +} + +static struct grub_procfs_entry file_entry = +{ + .name = "file." IMG_EXTENSION, + .get_contents = get_file_contents +}; + +static inline void init() +{ + if (inited) + return; + grub_procfs_register ("file." IMG_EXTENSION, &file_entry); + MODULE_INIT(); + grub_procfs_init(); + inited = true; +} + +int LLVMFuzzerTestOneInput(const grub_uint8_t *Data, grub_size_t Size) { + + init(); + data = Data; + size = Size; + grub_err_t err; + + grub_errno = GRUB_ERR_NONE; + err = grub_video_bitmap_load(&bitmap, "(proc)/file." IMG_EXTENSION); + if (err == GRUB_ERR_NONE) { + grub_video_bitmap_destroy(bitmap); + } + + return 0; +} diff --git a/grub-core/tests/fuzz/png.c b/grub-core/tests/fuzz/png.c new file mode 100644 index 000000000000..315bc2326271 --- /dev/null +++ b/grub-core/tests/fuzz/png.c @@ -0,0 +1,3 @@ +#define IMG_EXTENSION "png" +#define MODULE_INIT grub_png_init +#include "imageXX.c" -- 2.25.1