qemu-e2k/tests/qtest/fuzz-sdcard-test.c

175 lines
6.1 KiB
C
Raw Normal View History

/*
* QTest fuzzer-generated testcase for sdcard device
*
* Copyright (c) 2021 Philippe Mathieu-Daudé <f4bug@amsat.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "libqtest.h"
/*
* https://gitlab.com/qemu-project/qemu/-/issues/450
* Used to trigger:
* Assertion `wpnum < sd->wpgrps_size' failed.
*/
static void oss_fuzz_29225(void)
{
QTestState *s;
s = qtest_init(" -display none -m 512m -nodefaults -nographic"
" -device sdhci-pci,sd-spec-version=3"
" -device sd-card,drive=d0"
" -drive if=none,index=0,file=null-co://,format=raw,id=d0");
qtest_outl(s, 0xcf8, 0x80001010);
qtest_outl(s, 0xcfc, 0xd0690);
qtest_outl(s, 0xcf8, 0x80001003);
qtest_outl(s, 0xcf8, 0x80001013);
qtest_outl(s, 0xcfc, 0xffffffff);
qtest_outl(s, 0xcf8, 0x80001003);
qtest_outl(s, 0xcfc, 0x3effe00);
qtest_bufwrite(s, 0xff0d062c, "\xff", 0x1);
qtest_bufwrite(s, 0xff0d060f, "\xb7", 0x1);
qtest_bufwrite(s, 0xff0d060a, "\xc9", 0x1);
qtest_bufwrite(s, 0xff0d060f, "\x29", 0x1);
qtest_bufwrite(s, 0xff0d060f, "\xc2", 0x1);
qtest_bufwrite(s, 0xff0d0628, "\xf7", 0x1);
qtest_bufwrite(s, 0x0, "\xe3", 0x1);
qtest_bufwrite(s, 0x7, "\x13", 0x1);
qtest_bufwrite(s, 0x8, "\xe3", 0x1);
qtest_bufwrite(s, 0xf, "\xe3", 0x1);
qtest_bufwrite(s, 0xff0d060f, "\x03", 0x1);
qtest_bufwrite(s, 0xff0d0605, "\x01", 0x1);
qtest_bufwrite(s, 0xff0d060b, "\xff", 0x1);
qtest_bufwrite(s, 0xff0d060c, "\xff", 0x1);
qtest_bufwrite(s, 0xff0d060e, "\xff", 0x1);
qtest_bufwrite(s, 0xff0d060f, "\x06", 0x1);
qtest_bufwrite(s, 0xff0d060f, "\x9e", 0x1);
qtest_quit(s);
}
hw/sd/sdcard: Fix assertion accessing out-of-range addresses with CMD30 OSS-Fuzz found sending illegal addresses when querying the write protection bits triggers the assertion added in commit 84816fb63e5 ("hw/sd/sdcard: Assert if accessing an illegal group"): qemu-fuzz-i386-target-generic-fuzz-sdhci-v3: ../hw/sd/sd.c:824: uint32_t sd_wpbits(SDState *, uint64_t): Assertion `wpnum < sd->wpgrps_size' failed. #3 0x7f62a8b22c91 in __assert_fail #4 0x5569adcec405 in sd_wpbits hw/sd/sd.c:824:9 #5 0x5569adce5f6d in sd_normal_command hw/sd/sd.c:1389:38 #6 0x5569adce3870 in sd_do_command hw/sd/sd.c:1737:17 #7 0x5569adcf1566 in sdbus_do_command hw/sd/core.c:100:16 #8 0x5569adcfc192 in sdhci_send_command hw/sd/sdhci.c:337:12 #9 0x5569adcfa3a3 in sdhci_write hw/sd/sdhci.c:1186:9 #10 0x5569adfb3447 in memory_region_write_accessor softmmu/memory.c:492:5 It is legal for the CMD30 to query for out-of-range addresses. Such invalid addresses are simply ignored in the response (write protection bits set to 0). In commit 84816fb63e5 ("hw/sd/sdcard: Assert if accessing an illegal group") we misplaced the assertion *before* we test the address is in range. Move it *after*. Include the qtest reproducer provided by Alexander Bulekov: $ make check-qtest-i386 ... Running test qtest-i386/fuzz-sdcard-test qemu-system-i386: ../hw/sd/sd.c:824: sd_wpbits: Assertion `wpnum < sd->wpgrps_size' failed. Cc: qemu-stable@nongnu.org Reported-by: OSS-Fuzz (Issue 29225) Suggested-by: Peter Maydell <peter.maydell@linaro.org> Fixes: 84816fb63e5 ("hw/sd/sdcard: Assert if accessing an illegal group") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/495 Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Message-Id: <20210802235524.3417739-3-f4bug@amsat.org> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Tested-by: Alexander Bulekov <alxndr@bu.edu>
2021-08-03 01:55:24 +02:00
/*
* https://gitlab.com/qemu-project/qemu/-/issues/495
* Used to trigger:
* Assertion `wpnum < sd->wpgrps_size' failed.
*/
static void oss_fuzz_36217(void)
{
QTestState *s;
s = qtest_init(" -display none -m 32 -nodefaults -nographic"
" -device sdhci-pci,sd-spec-version=3 "
"-device sd-card,drive=d0 "
"-drive if=none,index=0,file=null-co://,format=raw,id=d0");
qtest_outl(s, 0xcf8, 0x80001010);
qtest_outl(s, 0xcfc, 0xe0000000);
qtest_outl(s, 0xcf8, 0x80001004);
qtest_outw(s, 0xcfc, 0x02);
qtest_bufwrite(s, 0xe000002c, "\x05", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x37", 0x1);
qtest_bufwrite(s, 0xe000000a, "\x01", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x29", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x02", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x03", 0x1);
qtest_bufwrite(s, 0xe0000005, "\x01", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x06", 0x1);
qtest_bufwrite(s, 0xe000000c, "\x05", 0x1);
qtest_bufwrite(s, 0xe000000e, "\x20", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x08", 0x1);
qtest_bufwrite(s, 0xe000000b, "\x3d", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x1e", 0x1);
qtest_quit(s);
}
tests/qtest/fuzz-sdcard-test: Add reproducer for OSS-Fuzz (Issue 29225) Include the qtest reproducer provided by Alexander Bulekov in https://gitlab.com/qemu-project/qemu/-/issues/451. Without the previous commit, we get: $ make check-qtest-i386 ... Running test qtest-i386/fuzz-sdcard-test ==447470==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61500002a080 at pc 0x564c71766d48 bp 0x7ffc126c62b0 sp 0x7ffc126c62a8 READ of size 1 at 0x61500002a080 thread T0 #0 0x564c71766d47 in sdhci_read_dataport hw/sd/sdhci.c:474:18 #1 0x564c7175f139 in sdhci_read hw/sd/sdhci.c:1022:19 #2 0x564c721b937b in memory_region_read_accessor softmmu/memory.c:440:11 #3 0x564c72171e51 in access_with_adjusted_size softmmu/memory.c:554:18 #4 0x564c7216f47c in memory_region_dispatch_read1 softmmu/memory.c:1424:16 #5 0x564c7216ebb9 in memory_region_dispatch_read softmmu/memory.c:1452:9 #6 0x564c7212db5d in flatview_read_continue softmmu/physmem.c:2879:23 #7 0x564c7212f958 in flatview_read softmmu/physmem.c:2921:12 #8 0x564c7212f418 in address_space_read_full softmmu/physmem.c:2934:18 #9 0x564c721305a9 in address_space_rw softmmu/physmem.c:2962:16 #10 0x564c7175a392 in dma_memory_rw_relaxed include/sysemu/dma.h:89:12 #11 0x564c7175a0ea in dma_memory_rw include/sysemu/dma.h:132:12 #12 0x564c71759684 in dma_memory_read include/sysemu/dma.h:152:12 #13 0x564c7175518c in sdhci_do_adma hw/sd/sdhci.c:823:27 #14 0x564c7174bf69 in sdhci_data_transfer hw/sd/sdhci.c:935:13 #15 0x564c7176aaa7 in sdhci_send_command hw/sd/sdhci.c:376:9 #16 0x564c717629ee in sdhci_write hw/sd/sdhci.c:1212:9 #17 0x564c72172513 in memory_region_write_accessor softmmu/memory.c:492:5 #18 0x564c72171e51 in access_with_adjusted_size softmmu/memory.c:554:18 #19 0x564c72170766 in memory_region_dispatch_write softmmu/memory.c:1504:16 #20 0x564c721419ee in flatview_write_continue softmmu/physmem.c:2812:23 #21 0x564c721301eb in flatview_write softmmu/physmem.c:2854:12 #22 0x564c7212fca8 in address_space_write softmmu/physmem.c:2950:18 #23 0x564c721d9a53 in qtest_process_command softmmu/qtest.c:727:9 0x61500002a080 is located 0 bytes to the right of 512-byte region [0x615000029e80,0x61500002a080) allocated by thread T0 here: #0 0x564c708e1737 in __interceptor_calloc (qemu-system-i386+0x1e6a737) #1 0x7ff05567b5e0 in g_malloc0 (/lib64/libglib-2.0.so.0+0x5a5e0) #2 0x564c71774adb in sdhci_pci_realize hw/sd/sdhci-pci.c:36:5 SUMMARY: AddressSanitizer: heap-buffer-overflow hw/sd/sdhci.c:474:18 in sdhci_read_dataport Shadow bytes around the buggy address: 0x0c2a7fffd3c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c2a7fffd3d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c2a7fffd3e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c2a7fffd3f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c2a7fffd400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c2a7fffd410:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c2a7fffd420: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2a7fffd430: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2a7fffd440: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2a7fffd450: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2a7fffd460: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Heap left redzone: fa Freed heap region: fd ==447470==ABORTING Broken pipe ERROR qtest-i386/fuzz-sdcard-test - too few tests run (expected 3, got 2) Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com> Acked-by: Thomas Huth <thuth@redhat.com> Message-Id: <20211215205656.488940-4-philmd@redhat.com> [thuth: Replaced "-m 4G" with "-m 512M"] Signed-off-by: Thomas Huth <thuth@redhat.com>
2021-12-15 21:56:56 +01:00
/*
* https://gitlab.com/qemu-project/qemu/-/issues/451
* Used to trigger a heap buffer overflow.
*/
static void oss_fuzz_36391(void)
{
QTestState *s;
s = qtest_init(" -display none -m 512M -nodefaults -nographic"
" -device sdhci-pci,sd-spec-version=3"
" -device sd-card,drive=drv"
" -drive if=none,index=0,file=null-co://,format=raw,id=drv");
qtest_outl(s, 0xcf8, 0x80001010);
qtest_outl(s, 0xcfc, 0xe0000000);
qtest_outl(s, 0xcf8, 0x80001004);
qtest_outw(s, 0xcfc, 0x7);
qtest_bufwrite(s, 0xe0000005, "\x73", 0x1);
qtest_bufwrite(s, 0xe0000028, "\x55", 0x1);
qtest_bufwrite(s, 0xe000002c, "\x55", 0x1);
qtest_bufwrite(s, 0x0, "\x65", 0x1);
qtest_bufwrite(s, 0x7, "\x69", 0x1);
qtest_bufwrite(s, 0x8, "\x65", 0x1);
qtest_bufwrite(s, 0xf, "\x69", 0x1);
qtest_bufwrite(s, 0x10, "\x65", 0x1);
qtest_bufwrite(s, 0x17, "\x69", 0x1);
qtest_bufwrite(s, 0x18, "\x65", 0x1);
qtest_bufwrite(s, 0x1f, "\x69", 0x1);
qtest_bufwrite(s, 0x20, "\x65", 0x1);
qtest_bufwrite(s, 0x27, "\x69", 0x1);
qtest_bufwrite(s, 0x28, "\x65", 0x1);
qtest_bufwrite(s, 0x2f, "\x69", 0x1);
qtest_bufwrite(s, 0x30, "\x65", 0x1);
qtest_bufwrite(s, 0x37, "\x69", 0x1);
qtest_bufwrite(s, 0x38, "\x65", 0x1);
qtest_bufwrite(s, 0x3f, "\x69", 0x1);
qtest_bufwrite(s, 0x40, "\x65", 0x1);
qtest_bufwrite(s, 0x47, "\x69", 0x1);
qtest_bufwrite(s, 0x48, "\x65", 0x1);
qtest_bufwrite(s, 0xe000000c, "\x55", 0x1);
qtest_bufwrite(s, 0xe000000e, "\x2c", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x5b", 0x1);
qtest_bufwrite(s, 0xe0000010, "\x06\x46", 0x2);
qtest_bufwrite(s, 0x50, "\x65", 0x1);
qtest_bufwrite(s, 0x57, "\x69", 0x1);
qtest_bufwrite(s, 0x58, "\x65", 0x1);
qtest_bufwrite(s, 0x5f, "\x69", 0x1);
qtest_bufwrite(s, 0x60, "\x65", 0x1);
qtest_bufwrite(s, 0x67, "\x69", 0x1);
qtest_bufwrite(s, 0x68, "\x65", 0x1);
qtest_bufwrite(s, 0x6f, "\x69", 0x1);
qtest_bufwrite(s, 0x70, "\x65", 0x1);
qtest_bufwrite(s, 0x77, "\x69", 0x1);
qtest_bufwrite(s, 0x78, "\x65", 0x1);
qtest_bufwrite(s, 0x7f, "\x69", 0x1);
qtest_bufwrite(s, 0x80, "\x65", 0x1);
qtest_bufwrite(s, 0x87, "\x69", 0x1);
qtest_bufwrite(s, 0x88, "\x65", 0x1);
qtest_bufwrite(s, 0x8f, "\x69", 0x1);
qtest_bufwrite(s, 0x90, "\x65", 0x1);
qtest_bufwrite(s, 0x97, "\x69", 0x1);
qtest_bufwrite(s, 0x98, "\x65", 0x1);
qtest_bufwrite(s, 0xe0000026, "\x5a\x06", 0x2);
qtest_bufwrite(s, 0xe0000028, "\x46\xc0\xc9\xc9", 0x4);
qtest_bufwrite(s, 0xe0000028, "\x55", 0x1);
qtest_bufwrite(s, 0xe000002a, "\x5a", 0x1);
qtest_bufwrite(s, 0xa0, "\x65", 0x1);
qtest_bufwrite(s, 0xa5, "\xff", 0x1);
qtest_bufwrite(s, 0xa6, "\xff", 0x1);
qtest_bufwrite(s, 0xa7, "\xdf", 0x1);
qtest_bufwrite(s, 0xe000000c, "\x27", 0x1);
qtest_bufwrite(s, 0xe000000f, "\x55", 0x1);
qtest_quit(s);
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
qtest_add_func("fuzz/sdcard/oss_fuzz_29225", oss_fuzz_29225);
qtest_add_func("fuzz/sdcard/oss_fuzz_36217", oss_fuzz_36217);
qtest_add_func("fuzz/sdcard/oss_fuzz_36391", oss_fuzz_36391);
return g_test_run();
}