Skip to contentSkip to conversation

cirrus: handle wraparound in cirrus_invalidate_region

PATCH Superseded Gerd Hoffmann 4 participants 11 messages Aug 21, 2020 – Sep 1, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cirrus: handle wraparound in cirrus_invalidate_region

Code simply asserts that there is no wraparound instead of handling it properly. The assert() can be triggered by the guest (must be privilidged inside the guest though). Fix it.

---
 hw/display/cirrus_vga.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
hw/display/cirrus_vga.c +8 -3
index 212d6f5e6145..b91b64347473 100644
@@ -640,10 +640,15 @@ static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
     }
 
     for (y = 0; y < lines; y++) {
-        off_cur = off_begin;
+        off_cur = off_begin & s->cirrus_addr_mask;
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
-        assert(off_cur_end >= off_cur);
-        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        if (off_cur_end >= off_cur) {
+            memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        } else {
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur, s->cirrus_addr_mask - off_cur);
+            memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);
+        }
         off_begin += off_pitch;
     }
 }
Signature · 1 line
2.27.0
#1 lore raw
Quoted 21 lines · Gerd Hoffmann <[email protected]> 于2020年8月21日周五 下午4:27写道:

Code simply asserts that there is no wraparound instead of handling it properly. The assert() can be triggered by the guest (must be privilidged inside the guest though). Fix it.

---
 hw/display/cirrus_vga.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
hw/display/cirrus_vga.c +0 -0
index 212d6f5e6145..b91b64347473 100644
@@ -640,10 +640,15 @@ static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
     }
 
     for (y = 0; y < lines; y++) {

hw/display/cirrus_vga.c static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin, · line 649

Quoted 10 lines
hw/display/cirrus_vga.c +6 -3
-        off_cur = off_begin;
+        off_cur = off_begin & s->cirrus_addr_mask;
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
-        assert(off_cur_end >= off_cur);
-        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        if (off_cur_end >= off_cur) {
+            memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        } else {
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur, s->cirrus_addr_mask - off_cur);

Should here be 's->cirrus_addr_mask + 1 - off_cur'

hw/display/cirrus_vga.c static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin, · line 650

Quoted 1 line
hw/display/cirrus_vga.c +1 -0
+            memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

And here be 'off_cur_end -1'

Thanks,
Li Qiang

Quoted 8 lines
hw/display/cirrus_vga.c +1 -1
+        }
         off_begin += off_pitch;
     }
 }
--

2.27.0

#2 lore raw
Quoted 12 lines

hw/display/cirrus_vga.c static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin, · line 649

Quoted 11 lines
     for (y = 0; y < lines; y++) {
hw/display/cirrus_vga.c +6 -3
-        off_cur = off_begin;
+        off_cur = off_begin & s->cirrus_addr_mask;
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
-        assert(off_cur_end >= off_cur);
-        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        if (off_cur_end >= off_cur) {
+            memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        } else {
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur, s->cirrus_addr_mask - off_cur);
Quoted 1 line

Should here be 's->cirrus_addr_mask + 1 - off_cur'

Yes (mask != size).

Quoted 3 lines
Quoted 1 line

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

And here be 'off_cur_end -1'

--verbose please. I think this one is correct.

take care,
Gerd

#3 lore raw
Quoted 13 lines · Gerd Hoffmann <[email protected]> 于2020年8月31日周一 下午7:23写道:
Quoted 13 lines
Quoted 12 lines
     for (y = 0; y < lines; y++) {
hw/display/cirrus_vga.c +6 -3
-        off_cur = off_begin;
+        off_cur = off_begin & s->cirrus_addr_mask;
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
-        assert(off_cur_end >= off_cur);
-        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        if (off_cur_end >= off_cur) {
+            memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        } else {
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur, s->cirrus_addr_mask - off_cur);
Quoted 3 lines
Quoted 1 line

Should here be 's->cirrus_addr_mask + 1 - off_cur'

Yes (mask != size).

Say if we have a range 0~0x2000 then the mask is '0x1fff' and the off_cur is 0x1000.

0. 0x1000. 0x2000
off_cur

Then the wrap occurs.

In the first set.
We just sets 0x1fff-0x1000= 0xfff bytes. In fact we need to set 0x1000 bytes.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

And here be 'off_cur_end -1'

--verbose please. I think this one is correct.

Here the 'off_cur_end' is size.
In this second set we actually sets 'off_cur_end+1' size bytes.

In a word, I think the first lost a byte and the second added a more byte .

Thank,s
Li Qiang

Quoted 4 lines

take care,
Gerd

#4 lore raw

Hi,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;

[ ... ]

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

And here be 'off_cur_end -1'

--verbose please. I think this one is correct.

Here the 'off_cur_end' is size.

Exactly. And memory_region_set_dirty wants the size. So everything is fine, right?

take care,
Gerd

#5 lore raw
Quoted 7 lines · Gerd Hoffmann <[email protected]> 于2020年9月1日周二 下午1:15写道:

Hi,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;

[ ... ]

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

And here be 'off_cur_end -1'

--verbose please. I think this one is correct.

Here the 'off_cur_end' is size.

Exactly. And memory_region_set_dirty wants the size. So everything is fine, right?

+ if (off_cur_end >= off_cur) {
+ memory_region_set_dirty(&s->vga.vram, off_cur,
off_cur_end - off_cur);
+ } else {
+ /* wraparound */
+ memory_region_set_dirty(&s->vga.vram, off_cur,
s->cirrus_addr_mask - off_cur);

The s->cirrus_addr_mask can be reached. I mean we can do following:
s->vga.vram[s->cirrus_addr_mask].

If I understand correctly, the 'off_cur' and 's->cirrus_addr_mask' is both index [off_cur, s->cirrus_addr_mask].

So the len is 's->cirrus_addr_mask->off_cur+1'.

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

For the 'off_cur_end' here, why we add 1 at the first?:

"off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;" This addition '1' is what I think should be substracted in wrapped cases.

Thanks,
Li Qiang

+ }

         off_begin += off_pitch;
     }

}

Quoted 4 lines

take care,
Gerd

#6 lore raw

Hi,

Quoted 5 lines
(quoted diff) +2 -0
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur,

s->cirrus_addr_mask - off_cur);

So the len is 's->cirrus_addr_mask->off_cur+1'.

Correct.

Quoted 5 lines

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

For the 'off_cur_end' here, why we add 1 at the first?:

"off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;"

                                         ^^^^
Quoted 1 line

This addition '1' is what I think should be substracted in wrapped cases.

The +1 balances the -1 done before ...

take care,
Gerd

#7 lore raw
Quoted 10 lines · Gerd Hoffmann <[email protected]> 于2020年9月1日周二 下午3:16写道:

Hi,

Quoted 5 lines
(quoted diff) +2 -0
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur,

s->cirrus_addr_mask - off_cur);

So the len is 's->cirrus_addr_mask->off_cur+1'.

Correct.

So do you agree me the first set size should be 's->cirrus_addr_mask - off_cur+1'?

Quoted 10 lines
Quoted 6 lines

+ memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);

For the 'off_cur_end' here, why we add 1 at the first?:

"off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;"

                                         ^^^^
Quoted 1 line

This addition '1' is what I think should be substracted in wrapped cases.

The +1 balances the -1 done before ...

Then the second set size is ok.

Thanks,
Li Qiang

Quoted 4 lines

take care,
Gerd

#8 lore raw
Quoted 8 lines · Li Qiang
Quoted 7 lines · Gerd Hoffmann <[email protected]> 于2020年9月1日周二 下午3:16写道:

Hi,

Quoted 3 lines
(quoted diff) +2 -0
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur,

s->cirrus_addr_mask - off_cur);

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

So the len is 's->cirrus_addr_mask->off_cur+1'.

Correct.

So do you agree me the first set size should be 's->cirrus_addr_mask - off_cur+1'?

Yes.

Quoted 3 lines
Quoted 1 line

The +1 balances the -1 done before ...

Then the second set size is ok.

Ok, good. v2 sent now.

thanks,
Gerd

Reviewed-by on v1 1/1
#9 lore raw
In reply to Gerd Hoffmann
Quoted 3 lines · Gerd Hoffmann

Code simply asserts that there is no wraparound instead of handling it properly. The assert() can be triggered by the guest (must be privilidged inside the guest though). Fix it.

Thanks for fixing this!

Quoted 3 lines

Well, I reported this one 3 months ago to secalert, not Alex.

Quoted 29 lines
---
 hw/display/cirrus_vga.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
hw/display/cirrus_vga.c +8 -3
index 212d6f5e6145..b91b64347473 100644
@@ -640,10 +640,15 @@ static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
     }
 
     for (y = 0; y < lines; y++) {
-        off_cur = off_begin;
+        off_cur = off_begin & s->cirrus_addr_mask;
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
-        assert(off_cur_end >= off_cur);
-        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        if (off_cur_end >= off_cur) {
+            memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        } else {
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur, s->cirrus_addr_mask - off_cur);
+            memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);
+        }
         off_begin += off_pitch;
     }
 }
#10 lore raw
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Gerd Hoffmann

Code simply asserts that there is no wraparound instead of handling it properly. The assert() can be triggered by the guest (must be privilidged inside the guest though). Fix it.

Quoted 7 lines

Thanks for fixing this!

Quoted 3 lines

Well, I reported this one 3 months ago to secalert, not Alex.

Nice. In fact, I don't think I've come across this one yet, even with fairly good coverage over the cirrus. Maybe its just a matter of increasing the maximum input length. I think with my parser each out instruction weighs in at 15 bytes + 4 byte separator, so it would take 900-1000 bytes to generate your reproducer, whereas I temporarily set the limit at 400.

Quoted 32 lines
Quoted 29 lines
---
 hw/display/cirrus_vga.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
hw/display/cirrus_vga.c +8 -3
index 212d6f5e6145..b91b64347473 100644
@@ -640,10 +640,15 @@ static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
     }
 
     for (y = 0; y < lines; y++) {
-        off_cur = off_begin;
+        off_cur = off_begin & s->cirrus_addr_mask;
         off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
-        assert(off_cur_end >= off_cur);
-        memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        if (off_cur_end >= off_cur) {
+            memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
+        } else {
+            /* wraparound */
+            memory_region_set_dirty(&s->vga.vram, off_cur, s->cirrus_addr_mask - off_cur);
+            memory_region_set_dirty(&s->vga.vram, 0, off_cur_end);
+        }
         off_begin += off_pitch;
     }
 }