Project: qemu (qemu/qemu), language: c++
Vulnerability instance: qemu.cve-2021-3507
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: Red Hat Bugzilla
## URL: https://bugzilla.redhat.com/show_bug.cgi?id=1951118
## Description:
Bugzilla ID: 1951118
Title: CVE-2021-3507 QEMU: fdc: heap buffer overflow in DMA read data transfers

Description
Mauro Matteo Cascella 2021-04-19 16:50:57 UTC
A heap buffer overflow was found in the floppy disk emulator of QEMU up to 6.0.0 (including). It could occur in fdctrl_transfer_handler() in hw/block/fdc.c while processing DMA read data transfers from the floppy drive to the guest system. A privileged guest user could use this flaw to crash the QEMU process on the host resulting in DoS scenario, or potential information leakage from the host memory.


Comments:
Comment 2
Mauro Matteo Cascella 2021-04-19 17:54:15 UTC
Created qemu tracking bugs for this issue:

Affects: fedora-all [bug 1951175]

---

Comment 3
Mauro Matteo Cascella 2021-04-20 08:23:49 UTC
The data length is not properly computed/sanitized while processing DMA read data transfers from the floppy drive (specifically, while handling a VERIFY command). This leads a negative value to be used as the data length in fdctrl_transfer_handler(), and eventually used in a memcpy in flatview_write_continue().

---

Comment 4
Mauro Matteo Cascella 2021-04-20 08:30:27 UTC
Stacktrace:
==22918==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61900003c800 at pc 0x555558170177 bp 0x7fffffffbc10 sp 0x7fffffffb3d8
READ of size 786432 at 0x61900003c800 thread T0
#0 0x555558170176 in __asan_memcpy (system-i386+0x2c1c176)
#1 0x55555964a3ed in flatview_write_continue softmmu/physmem.c:2781:13
#2 0x55555963fde8 in flatview_write softmmu/physmem.c:2816:14
#3 0x55555963fde8 in address_space_write softmmu/physmem.c:2908:18
#4 0x555558dcb0e0 in cpu_physical_memory_write master/include/exec/cpu-common.h:80:5
#5 0x555558dcb0e0 in i8257_dma_write_memory hw/dma/i8257.c:452:9
#6 0x555558fdb4d9 in fdctrl_transfer_handler hw/block/fdc.c:1809:13
#7 0x555558fc7377 in fdctrl_write_data hw/block/fdc.c:2459:13
#8 0x555558fc7377 in fdctrl_write hw/block/fdc.c:967:9
[...]

Debug output:
FLOPPY: init controller
FLOPPY: revalidate
FLOPPY: No drive connected
FLOPPY: revalidate
FLOPPY: No drive connected
FLOPPY: revalidate
FLOPPY: Floppy disk (2 h 80 t 18 s) rw
FLOPPY: reset controller
FLOPPY: recalibrate
FLOPPY: recalibrate
FLOPPY: try to read 0 00 01 (max=1 0 00 00)
[R +0.025666] outl 0x9 0x0a0206
FLOPPY: Not in DMA transfer mode !
[...]
[R +0.025798] outw 0x3f4 0x0
fdc_ioport_write write reg 0x04 val 0x00
FLOPPY: select rate register set to 0x00
fdc_ioport_write write reg 0x05 val 0x00
FLOPPY: fdctrl_write_data: 00
FLOPPY: VERIFY command
FLOPPY: Calling handler for 'VERIFY'
FLOPPY: Start transfer at 0 0 00 02 (1)
FLOPPY: direction=5 (8704 - -512)
FLOPPY: copy -512 bytes (-512 0 -512) 0 pos 0 00 (2-0x00000001 0x00000200)
FLOPPY: copy 1 bytes (1 0 -512) 0 pos 0 00 (2-0x00000001 0x00000200)
FLOPPY: end transfer 1 1 -512
FLOPPY: transfer status: 00 00 00 (00)
FLOPPY: Set interrupt status to 0x00

---

Comment 7
Mauro Matteo Cascella 2021-04-22 08:08:24 UTC
Statement:

This issue affects the version of `qemu-kvm` as shipped with Red Hat Enterprise Linux 8 and Red Hat Enterprise Linux 8 Advanced Virtualization. A future update may address this flaw.

---

Comment 8
Mauro Matteo Cascella 2021-05-13 08:05:50 UTC
Acknowledgments:

Name: Alexander Bulekov

Build script:
#!/bin/bash
# Default build script template for C/C++ projects
set -eu

# Define configuration options
CONFIG_OPTS="--disable-shared"
CMAKE_OPTS="-DCMAKE_BUILD_TYPE=Debug"
MAKE_OPTS="-j$(nproc)"
BUILD_DIR="build"

# 1. Run build preparation scripts if they exist
for prep_script in buildconf autogen.sh bootstrap; do
  if [ -x "$prep_script" ]; then
    echo "Running $prep_script..."
    ./$prep_script
    break
  elif [ -f "$prep_script" ]; then
    echo "Found $prep_script but it's not executable, setting permissions..."
    chmod +x $prep_script
    ./$prep_script
    break
  fi
done

# 2. Handle different build systems
if [ -f "configure" ]; then
  echo "Found configure script, running with options: $CONFIG_OPTS"
  ./configure $CONFIG_OPTS

  echo "Building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -f "CMakeLists.txt" ]; then
  echo "Found CMakeLists.txt, using CMake build system"
  mkdir -p $BUILD_DIR
  cd $BUILD_DIR
  cmake $CMAKE_OPTS ..
  make $MAKE_OPTS
elif [ -f "Makefile" ] || [ -f "makefile" ]; then
  echo "Found Makefile, building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -f "meson.build" ]; then
  echo "Found meson.build, using Meson build system"
  mkdir -p $BUILD_DIR
  meson setup $BUILD_DIR .
  cd $BUILD_DIR
  ninja
elif [ -f "GNUmakefile" ]; then
  echo "Found GNUmakefile, building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -d ".git" ] && [ -f "Makefile.am" ]; then
  echo "Found Makefile.am, running autoreconf"
  autoreconf -i
  ./configure $CONFIG_OPTS
  make $MAKE_OPTS
else
  echo "WARNING: No recognized build system found!"
  echo "Trying generic approach:"
  
  # Try to detect common source patterns and compile them
  if ls *.c >/dev/null 2>&1 || ls *.cpp >/dev/null 2>&1 || ls *.cc >/dev/null 2>&1; then
    echo "Found C/C++ source files, attempting direct compilation"
    if ls *.cpp >/dev/null 2>&1 || ls *.cc >/dev/null 2>&1; then
      CXX=${CXX:-g++}
      echo "Compiling C++ sources with $CXX"
      $CXX -g -O1 -fsanitize=address -fno-omit-frame-pointer -o main *.cpp *.cc 2>/dev/null || true
    elif ls *.c >/dev/null 2>&1; then
      CC=${CC:-gcc}
      echo "Compiling C sources with $CC"
      $CC -g -O1 -fsanitize=address -fno-omit-frame-pointer -o main *.c 2>/dev/null || true
    fi
  fi
fi

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool ninja-build libfdt-dev libpixman-1-dev zlib1g-dev libslirp-dev patchelf wget libattr1 libattr1-dev libcap-ng-dev pkg-config
RUN git clone https://github.com/qemu/qemu qemu
RUN git -C qemu checkout 609d7596524ab204ccd71ef42c9eee4c7c338ea4
WORKDIR $SRC/qemu
COPY build.sh $SRC/

Additional files: