Skip to contentSkip to conversation

hw/display/ati: fix HOST_DATA accumulator overflow and bpp==0 crashes

PATCH In review Junjie Cao 3 participants 7 messages May 19 – Jul 1

lore
Share & tools
lore

Junjie Cao

author
#0 lore raw

v2 cover hw/display/ati: fix HOST_DATA accumulator overflow and bpp==0 crashes

Two guest-triggerable crashes in the ati-vga HOST_DATA blit path, independently found via fuzzing (generic-fuzz with ASan/UBSan).

Patch 1 fixes the heap-buffer-overflow: when ati_host_data_flush() returns early, host_data.next was not reset, causing subsequent HOST_DATA writes to index past acc[3]. The fix moves the reset into the write handler as suggested by Chad Jablonski in the v1 discussion.

Patch 2 fixes division-by-zero (SIGFPE) and g_assert_not_reached() when ati_bpp_from_datatype() returns 0 for invalid dp_datatype values.

Based on Feifan Qian's v1 report and the subsequent design discussion between BALATON Zoltan and Chad Jablonski.

v1: https://lore.kernel.org/qemu-devel/Czyl6yVfL6sHl_o1kRk8N_LpwXMMRVhO9vgz1qCVJFagn9D4nHSKuiux39iOLty0Q3acxQq_FeovPhTQvSKus2htwjI9lTajLZmqovr0Wxs=@proton.me/

Junjie Cao (2):
hw/display/ati: reset host_data.next in write handler after flush hw/display/ati: guard against zero bpp in ati_host_data_flush

 hw/display/ati.c    | 2 ++
 hw/display/ati_2d.c | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 hw/display/ati: reset host_data.next in write handler after flush

ati_host_data_flush() resets host_data.next only on its success path. When it returns early (unsupported bpp, direction, src_source, or src_datatype), next remains stale at >= 4. The next HOST_DATA write then stores a guest-controlled dword at acc[4+], overflowing the 4-element accumulator array.

Fix this by resetting next unconditionally in the write handler after calling ati_host_data_flush() or ati_host_data_finish(), and removing the reset from inside ati_host_data_flush(). This ensures the write handler owns the full lifecycle of the accumulator index regardless of flush success or failure.

Resolves: https://lore.kernel.org/qemu-devel/Czyl6yVfL6sHl_o1kRk8N_LpwXMMRVhO9vgz1qCVJFagn9D4nHSKuiux39iOLty0Q3acxQq_FeovPhTQvSKus2htwjI9lTajLZmqovr0Wxs=@proton.me/

---
 hw/display/ati.c    | 2 ++
 hw/display/ati_2d.c | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)
hw/display/ati.c +2 -0
index d77589df67..db7e08a462 100644
@@ -1034,8 +1034,10 @@ static void ati_mm_write(void *opaque, hwaddr addr,
         s->host_data.acc[s->host_data.next++] = data;
         if (addr == HOST_DATA_LAST) {
             ati_host_data_finish(s);
+            s->host_data.next = 0;
         } else if (s->host_data.next >= 4) {
             ati_host_data_flush(s);
+            s->host_data.next = 0;
         }
         break;
     default:
hw/display/ati_2d.c +0 -1
index 48498677c7..8ef82bb87f 100644
@@ -452,7 +452,6 @@ bool ati_host_data_flush(ATIVGAState *s)
     }
 
     /* Track state of the overall blit for use by the next flush */
-    s->host_data.next = 0;
     s->host_data.row = row;
     s->host_data.col = col;
     if (s->host_data.row >= ctx.dst.height) {
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 2/2 hw/display/ati: guard against zero bpp in ati_host_data_flush

In reply to Junjie Cao

ati_bpp_from_datatype() returns 0 for unrecognized dp_datatype nibble values (0, 1, or >= 7). ati_host_data_flush() only guards against the bpp == 24 case but not bpp == 0, leading to:

1. Division by zero at "pix_count /= ctx.bpp" (SIGFPE) when src_datatype is SRC_COLOR.
2. g_assert_not_reached() in stn_he_p() when bypp (= bpp/8 = 0) hits the default case of the size switch.

Both are guest-triggerable via MMIO writes to the dp_datatype register while a HOST_DATA blit is active.

Add an explicit bpp == 0 check with LOG_GUEST_ERROR before proceeding with the blit, consistent with the existing check in ati_2d_do_blt().

---
 hw/display/ati_2d.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/display/ati_2d.c +5 -0
index 8ef82bb87f..22dd811bd8 100644
@@ -377,6 +377,11 @@ bool ati_host_data_flush(ATIVGAState *s)
 
     setup_2d_blt_ctx(s, &ctx);
 
+    if (!ctx.bpp) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "host_data_blt: invalid bpp from datatype\n");
+        return false;
+    }
     if (ctx.bpp == 24) {
         qemu_log_mask(LOG_UNIMP,
                       "host_data_blt: unsupported in 24 bits mode\n");
Signature · 1 line
2.43.0
author Reviewed-by Chad Jablonski on the v2 series
#5 lore raw
In reply to Junjie Cao

Hi Philippe,

Ping on this v2 series. It has been ~6 weeks without pickup. Both patches have:

These fix two guest-triggerable crashes in the ATI VGA host-data blit path (heap-buffer-overflow via accumulator index overflow, and SIGFPE from division by zero when bpp is 0).

Still applies cleanly to current master.

Thanks,
Junjie

#6 lore raw

Hi Junjie,

Quoted 3 lines · Junjie Cao

Hi Philippe,

Ping on this v2 series. It has been ~6 weeks without pickup.

I'm sorry, I suppose I missed it during my work email change.

Queued!

Quoted 13 lines

Both patches have:

These fix two guest-triggerable crashes in the ATI VGA host-data blit path (heap-buffer-overflow via accumulator index overflow, and SIGFPE from division by zero when bpp is 0).

Still applies cleanly to current master.

Thanks,
Junjie