Skip to contentSkip to conversation

cirrus_vga: fix off-by-one in blit_region_is_unsafe

PATCH In review Paolo Bonzini 3 participants 3 messages Feb 10, 2016 – Feb 16, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cirrus_vga: fix off-by-one in blit_region_is_unsafe

The "max" value is being compared with >=, but addr + width points to the first byte that will _not_ be copied. Laszlo suggested using a "greater than" comparison, instead of subtracting one like it is already done above for the height, so that max remains always positive.

The mistake is "safe"---it will reject some blits, but will never cause out-of-bounds writes.

---
 hw/display/cirrus_vga.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/cirrus_vga.c +2 -2
index b6ce1c8..57b91a7 100644
@@ -276,14 +276,14 @@ static bool blit_region_is_unsafe(struct CirrusVGAState *s,
             + ((int64_t)s->cirrus_blt_height-1) * pitch;
         int32_t max = addr
             + s->cirrus_blt_width;
-        if (min < 0 || max >= s->vga.vram_size) {
+        if (min < 0 || max > s->vga.vram_size) {
             return true;
         }
     } else {
         int64_t max = addr
             + ((int64_t)s->cirrus_blt_height-1) * pitch
             + s->cirrus_blt_width;
-        if (max >= s->vga.vram_size) {
+        if (max > s->vga.vram_size) {
             return true;
         }
     }
Signature · 1 line
2.5.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 26 lines · Paolo Bonzini

The "max" value is being compared with >=, but addr + width points to the first byte that will _not_ be copied. Laszlo suggested using a "greater than" comparison, instead of subtracting one like it is already done above for the height, so that max remains always positive.

The mistake is "safe"---it will reject some blits, but will never cause out-of-bounds writes.

---
 hw/display/cirrus_vga.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/cirrus_vga.c +1 -1
index b6ce1c8..57b91a7 100644
@@ -276,14 +276,14 @@ static bool blit_region_is_unsafe(struct CirrusVGAState *s,
             + ((int64_t)s->cirrus_blt_height-1) * pitch;
         int32_t max = addr
             + s->cirrus_blt_width;
-        if (min < 0 || max >= s->vga.vram_size) {
+        if (min < 0 || max > s->vga.vram_size) {
             return true;

hw/display/cirrus_vga.c static bool blit_region_is_unsafe(struct CirrusVGAState *s, · line 289

Quoted 10 lines
hw/display/cirrus_vga.c +1 -1
         }
     } else {
         int64_t max = addr
             + ((int64_t)s->cirrus_blt_height-1) * pitch
             + s->cirrus_blt_width;
-        if (max >= s->vga.vram_size) {
+        if (max > s->vga.vram_size) {
             return true;
         }
     }

Thank you for your patience. :)

#2 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

The "max" value is being compared with >=, but addr + width points to the first byte that will _not_ be copied. Laszlo suggested using a "greater than" comparison, instead of subtracting one like it is already done above for the height, so that max remains always positive.

The mistake is "safe"---it will reject some blits, but will never cause
out-of-bounds writes.

added to vga queue.

thanks,
Gerd