Global-buffer-overflow in mode_sense_page
This was originally reported at: https://bugs.launchpad.net/qemu/+bug/1914638
Hello,
Reproducer
cat << EOF | ./qemu-system-i386 -machine q35 -nodefaults -device \
megasas -device scsi-cd,drive=null0 -blockdev \
driver=null-co,read-zeroes=on,node-name=null0 -nographic -qtest stdio
outl 0xcf8 0x80000818
outl 0xcfc 0xc000
outl 0xcf8 0x80000804
outw 0xcfc 0x7
write 0x0 0x1 0x03
write 0x7 0x1 0x3f
write 0x10 0x1 0x03
write 0x20 0x1 0x55
write 0x21 0x1 0x10
write 0x28 0x1 0x10
write 0x30 0x1 0xff
write 0x31 0x1 0xff
write 0x32 0x1 0xff
write 0x33 0x1 0xff
write 0x34 0x1 0xff
write 0x35 0x1 0xff
write 0x36 0x1 0xff
write 0x37 0x1 0xff
write 0x3b 0x1 0x10
write 0x43 0x1 0x10
write 0x44 0x1 0x10
write 0x4f 0x1 0x10
write 0x53 0x1 0x10
write 0x5b 0x1 0x10
write 0x5f 0x1 0x10
write 0x67 0x1 0x10
write 0x6b 0x1 0x10
write 0x73 0x1 0x10
write 0x75 0x1 0x10
write 0x7d 0x1 0x10
write 0x83 0x1 0x10
write 0x8b 0x1 0x10
write 0x8f 0x1 0x10
write 0x97 0x1 0x10
write 0x9b 0x1 0x10
write 0xa3 0x1 0x03
write 0xa6 0x1 0x10
write 0xae 0x1 0x10
write 0xb3 0x1 0x10
write 0xbb 0x1 0x10
write 0xbf 0x1 0x10
write 0xc7 0x1 0x10
write 0xca 0x1 0x10
write 0xd3 0x1 0x06
write 0xd7 0x1 0x10
write 0xdf 0x1 0x10
write 0xe3 0x1 0x06
write 0xeb 0x1 0x01
write 0xef 0x1 0x10
write 0xf7 0x1 0x10
write 0xfb 0x1 0x10
write 0x103 0x1 0x20
write 0x107 0x1 0x10
write 0x10f 0x1 0x10
write 0x113 0x1 0x10
write 0x11b 0x1 0x10
write 0x11f 0x1 0x10
write 0x127 0x1 0x10
write 0x12b 0x1 0x10
write 0x130 0x1 0x10
write 0x137 0x1 0x10
write 0x13f 0x1 0x40
write 0x141 0x1 0x10
write 0x14b 0x1 0x10
write 0x14f 0x1 0x10
write 0x157 0x1 0x10
write 0x15b 0x1 0x10
write 0x161 0x1 0x10
write 0x167 0x1 0x03
write 0x16f 0x1 0x06
write 0x172 0x1 0x10
write 0x17b 0x1 0x10
write 0x17f 0x1 0x10
write 0x187 0x1 0x10
write 0x18b 0x1 0x10
write 0x192 0x1 0x10
write 0x197 0x1 0x06
write 0x19f 0x1 0x20
write 0x1a3 0x1 0x10
write 0x1ab 0x1 0x40
write 0x1af 0x1 0x01
write 0x1b7 0x1 0x10
write 0x1bb 0x1 0x20
write 0x1c3 0x1 0x10
write 0x1c7 0x1 0x20
write 0x1cc 0x1 0x10
write 0x1d3 0x1 0x10
write 0x1db 0x1 0x10
write 0x1df 0x1 0x10
write 0x1e7 0x1 0x10
write 0x1eb 0x1 0x10
write 0x1f3 0x1 0x10
write 0x1f4 0x1 0x10
write 0x1fd 0x1 0x10
write 0x203 0x1 0x40
write 0x20b 0x1 0x10
write 0x20f 0x1 0x10
write 0x217 0x1 0x10
write 0x21b 0x1 0x10
write 0x223 0x1 0x10
write 0x225 0x1 0x10
write 0x22e 0x1 0x10
write 0x233 0x1 0x06
write 0x23b 0x1 0x10
write 0x23f 0x1 0x10
write 0x247 0x1 0x10
write 0x24b 0x1 0x10
write 0x252 0x1 0x10
write 0x256 0x1 0x10
write 0x25f 0x1 0x10
write 0x263 0x1 0x20
write 0x26b 0x1 0x06
write 0x26f 0x1 0x40
write 0x277 0x1 0x10
write 0x27b 0x1 0x10
write 0x283 0x1 0x10
write 0x287 0x1 0x10
write 0x28f 0x1 0x10
write 0x290 0x1 0x10
write 0x29b 0x1 0x10
write 0x29f 0x1 0x10
write 0x2a7 0x1 0x10
write 0x2ab 0x1 0x10
write 0x2b3 0x1 0x10
write 0x2b7 0x1 0x10
write 0x2bf 0x1 0x10
write 0x2c1 0x1 0x10
write 0x2c9 0x1 0x10
write 0x2cf 0x1 0x10
write 0x2d7 0x1 0x10
write 0x2db 0x1 0x10
write 0x2e3 0x1 0x10
write 0x2e7 0x1 0x10
write 0x2ef 0x1 0x03
write 0x2f2 0x1 0x10
write 0x2fa 0x1 0x10
write 0x2ff 0x1 0x10
write 0x307 0x1 0x10
write 0x30b 0x1 0x10
write 0x313 0x1 0x10
write 0x316 0x1 0x10
write 0x31f 0x1 0x06
write 0x323 0x1 0x10
outb 0xc040 0x0
EOF
Stack-Trace
==206209==ERROR: AddressSanitizer: global-buffer-overflow on address 0x56530112fffc at pc 0x5652ff2ad754 bp 0x7fffa1f86540 sp 0x7fffa1f86538
READ of size 4 at 0x56530112fffc thread T0
#0 0x5652ff2ad753 in mode_sense_page ../hw/scsi/scsi-disk.c:1090:10
#1 0x5652ff2a85c0 in scsi_disk_check_mode_select ../hw/scsi/scsi-disk.c:1433:11
#2 0x5652ff2a85c0 in mode_select_pages ../hw/scsi/scsi-disk.c:1501:17
#3 0x5652ff2a85c0 in scsi_disk_emulate_mode_select ../hw/scsi/scsi-disk.c:1556:13
#4 0x5652ff2a85c0 in scsi_disk_emulate_write_data ../hw/scsi/scsi-disk.c:1849:9
#5 0x5652ff72394b in scsi_req_continue ../hw/scsi/scsi-bus.c
#6 0x5652ff724b8f in scsi_req_data ../hw/scsi/scsi-bus.c:1428:5
#7 0x5652ff2a72f4 in scsi_disk_emulate_write_data ../hw/scsi/scsi-disk.c:1841:9
#8 0x5652ff72394b in scsi_req_continue ../hw/scsi/scsi-bus.c
#9 0x5652ffd5d9ca in megasas_enqueue_req ../hw/scsi/megasas.c:1660:9
#10 0x5652ffd3c153 in megasas_handle_scsi ../hw/scsi/megasas.c:1735:5
#11 0x5652ffd3c153 in megasas_handle_frame ../hw/scsi/megasas.c:1972:24
#12 0x5652ffd31644 in megasas_mmio_write ../hw/scsi/megasas.c:2129:9
#13 0x56530064cf75 in memory_region_write_accessor ../softmmu/memory.c:492:5
#14 0x56530064ca9a in access_with_adjusted_size ../softmmu/memory.c:554:18
#15 0x56530064c55f in memory_region_dispatch_write ../softmmu/memory.c
#16 0x5653007b2e26 in flatview_write_continue ../softmmu/physmem.c:2777:23
#17 0x5653007aa995 in flatview_write ../softmmu/physmem.c:2817:14
#18 0x5653007aa995 in address_space_write ../softmmu/physmem.c:2909:18
#19 0x5653007c92f4 in cpu_outb ../softmmu/ioport.c:60:5
#20 0x5653006e1b12 in qtest_process_command ../softmmu/qtest.c:495:13
#21 0x5653006e0b58 in qtest_process_inbuf ../softmmu/qtest.c:813:9
#22 0x565300887e14 in fd_chr_read ../chardev/char-fd.c:73:9
#23 0x7f11f6bddd6e in g_main_context_dispatch (/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x51d6e)
#24 0x565300ee7053 in glib_pollfds_poll ../util/main-loop.c:232:9
#25 0x565300ee7053 in os_host_main_loop_wait ../util/main-loop.c:255:5
#26 0x565300ee7053 in main_loop_wait ../util/main-loop.c:531:11
#27 0x5653005520c6 in qemu_main_loop ../softmmu/runstate.c:726:9
#28 0x5652ff0c085a in main ../softmmu/main.c:50:5
#29 0x7f11f533cd09 in __libc_start_main csu/../csu/libc-start.c:308:16
#30 0x5652ff014259 in _start (system-i386+0x2204259)
0x56530112fffc is located 0 bytes to the right of global variable 'mode_sense_valid' defined in '../hw/scsi/scsi-disk.c:1078:22' (0x56530112ff00) of size 252
SUMMARY: AddressSanitizer: global-buffer-overflow ../hw/scsi/scsi-disk.c:1090:10 in mode_sense_page
Shadow bytes around the buggy address:
0x0acae021dfa0: 00 00 00 00 00 06 f9 f9 f9 f9 f9 f9 00 00 00 00
0x0acae021dfb0: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
0x0acae021dfc0: 00 00 00 00 00 00 00 00 00 00 00 07 f9 f9 f9 f9
0x0acae021dfd0: 00 00 00 00 00 00 00 00 00 00 02 f9 f9 f9 f9 f9
0x0acae021dfe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0acae021dff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00[04]
0x0acae021e000: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
0x0acae021e010: 00 00 00 00 00 00 00 00 00 06 f9 f9 f9 f9 f9 f9
0x0acae021e020: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
0x0acae021e030: 00 00 04 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
0x0acae021e040: 05 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 f9
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==206209==ABORTING
OSS-Fuzz Report: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30219
libqtest Reproducer: 1907938.c
Thank you