cirrus: handle wraparound in cirrus_invalidate_region
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
@@ -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
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.Cc Li Qiang <[email protected]>Reported-by Alexander Bulekov <[email protected]>Signed-off-by Gerd Hoffmann <[email protected]>--- hw/display/cirrus_vga.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)hw/display/cirrus_vga.c +0
-0@@ -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
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
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
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
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_dirtywants 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
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
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
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
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
Cc Li Qiang <[email protected]>Reported-by Alexander Bulekov <[email protected]>
Well, I reported this one 3 months ago to secalert, not Alex.
Quoted 29 lines
Signed-off-by Gerd Hoffmann <[email protected]>--- hw/display/cirrus_vga.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)hw/display/cirrus_vga.c +8
-3@@ -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; } }
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
Cc Li Qiang <[email protected]>Reported-by Alexander Bulekov <[email protected]>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
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 29 lines
Signed-off-by Gerd Hoffmann <[email protected]>--- hw/display/cirrus_vga.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)hw/display/cirrus_vga.c +8
-3@@ -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; } }