Skip to contentSkip to conversation

ati-vga: fix unsigned integer overflow in cursor bounds checks

PATCH In review Junjie Cao 2 participants 3 messages Apr 14

lore
Share & tools
lore

Junjie Cao

author
#0 lore raw

PATCH v2 1/2 ati-vga: fix unsigned integer overflow in cursor bounds checks

The cursor bounds checks compare (srcoff + N) against vram_size, but both sides are uint32_t so the addition can wrap past UINT32_MAX when srcoff underflows from the cur_hv_offs subtraction, causing the check to be bypassed.

Rewrite the checks as (srcoff > vram_size - N) to avoid the overflow-prone addition, matching the style already used in ati_mm_read() and ati_mm_write().

Fixes 2f1fbe6ee9b5 ("ati-vga: Make sure hardware cursor data is within vram")

---
Found while running local QEMU fuzz testing.

v2: Collected Reviewed-by, added patch 2/2 to mask out the lock bit
as suggested by BALATON Zoltan.

 hw/display/ati.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/ati.c +2 -2
index 88a5bbbf07..0489995d00 100644
@@ -149,7 +149,7 @@ static void ati_cursor_define(ATIVGAState *s)
     /* FIXME handle cur_hv_offs correctly */
     srcoff = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
              (s->regs.cur_hv_offs & 0xffff) * 16;
-    if (srcoff + 64 * 16 > s->vga.vram_size) {
+    if (srcoff > s->vga.vram_size - 64 * 16) {
         return;
     }
     for (int i = 0; i < 64; i++, srcoff += 16) {
@@ -206,7 +206,7 @@ static void ati_cursor_draw_line(VGACommonState *vga, uint8_t *d, int scr_y)
     }
     /* FIXME handle cur_hv_offs correctly */
     srcoff = s->cursor_offset + (scr_y - vga->hw_cursor_y) * 16;
-    if (srcoff + 16 > s->vga.vram_size) {
+    if (srcoff > s->vga.vram_size - 16) {
         return;
     }
     dp = &dp[vga->hw_cursor_x];
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 2/2 ati-vga: mask out lock bit from CUR_OFFSET in cursor offset calculation

Bit 31 of CUR_OFFSET is the cursor lock bit, not part of the actual cursor address (bits 26:4). Although the callers already check for the lock bit and return early, mask it out with 0x07fffff0 when computing the cursor source offset so the calculation only uses the address bits.

---
 hw/display/ati.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/display/ati.c +5 -3
index 0489995d00..3a7d45a882 100644
@@ -147,7 +147,7 @@ static void ati_cursor_define(ATIVGAState *s)
         return; /* Do not update cursor if locked or rendered by guest */
     }
     /* FIXME handle cur_hv_offs correctly */
-    srcoff = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
+    srcoff = (s->regs.cur_offset & 0x07fffff0) - (s->regs.cur_hv_offs >> 16) -
              (s->regs.cur_hv_offs & 0xffff) * 16;
     if (srcoff > s->vga.vram_size - 64 * 16) {
         return;
@@ -176,13 +176,15 @@ static void ati_cursor_invalidate(VGACommonState *vga)
     if (s->cursor_size != size ||
         vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
         vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
-        s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
+        s->cursor_offset != (s->regs.cur_offset & 0x07fffff0) -
+        (s->regs.cur_hv_offs >> 16) -
         (s->regs.cur_hv_offs & 0xffff) * 16) {
         /* Remove old cursor then update and show new one if needed */
         vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
         vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
         vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
-        s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
+        s->cursor_offset = (s->regs.cur_offset & 0x07fffff0) -
+                           (s->regs.cur_hv_offs >> 16) -
                            (s->regs.cur_hv_offs & 0xffff) * 16;
         s->cursor_size = size;
         if (size) {
Signature · 1 line
2.43.0
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] ati-vga: mask out lock bit from CUR_OFFSET in cursor offset calculation

Quoted 7 lines · Junjie Cao

Bit 31 of CUR_OFFSET is the cursor lock bit, not part of the actual cursor address (bits 26:4). Although the callers already check for the lock bit and return early, mask it out with 0x07fffff0 when computing the cursor source offset so the calculation only uses the address bits.

Quoted 36 lines

---
hw/display/ati.c | 8 +++++---

1 file changed, 5 insertions(+), 3 deletions(-)
hw/display/ati.c +5 -3
index 0489995d00..3a7d45a882 100644
@@ -147,7 +147,7 @@ static void ati_cursor_define(ATIVGAState *s)
        return; /* Do not update cursor if locked or rendered by guest */
    }
    /* FIXME handle cur_hv_offs correctly */
-    srcoff = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
+    srcoff = (s->regs.cur_offset & 0x07fffff0) - (s->regs.cur_hv_offs >> 16) -
             (s->regs.cur_hv_offs & 0xffff) * 16;
    if (srcoff > s->vga.vram_size - 64 * 16) {
        return;
@@ -176,13 +176,15 @@ static void ati_cursor_invalidate(VGACommonState *vga)
    if (s->cursor_size != size ||
        vga->hw_cursor_x != s->regs.cur_hv_pos >> 16 ||
        vga->hw_cursor_y != (s->regs.cur_hv_pos & 0xffff) ||
-        s->cursor_offset != s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
+        s->cursor_offset != (s->regs.cur_offset & 0x07fffff0) -
+        (s->regs.cur_hv_offs >> 16) -
        (s->regs.cur_hv_offs & 0xffff) * 16) {
        /* Remove old cursor then update and show new one if needed */
        vga_invalidate_scanlines(vga, vga->hw_cursor_y, vga->hw_cursor_y + 63);
        vga->hw_cursor_x = s->regs.cur_hv_pos >> 16;
        vga->hw_cursor_y = s->regs.cur_hv_pos & 0xffff;
-        s->cursor_offset = s->regs.cur_offset - (s->regs.cur_hv_offs >> 16) -
+        s->cursor_offset = (s->regs.cur_offset & 0x07fffff0) -
+                           (s->regs.cur_hv_offs >> 16) -
                           (s->regs.cur_hv_offs & 0xffff) * 16;
        s->cursor_size = size;
        if (size) {