hw/display/sm501: Fix various error-checks
cover hw/display/sm501: Fix various error-checks
This set of patches fixes three bugs discovered recently in the sm501 device. They're all of the form "guest can misprogram registers in a way that defeats the bounds-checks on the 2D graphics ops".
thanks
-- PMM
Peter Maydell (3):
hw/display/sm501: Catch bad coordinates for RTL operations hw/display/sm501: Avoid overflow problems in bounds check calculations hw/display/sm501: Don't allow guest to set ram size larger than it is
hw/display/sm501.c | 64 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 hw/display/sm501: Catch bad coordinates for RTL operations
The sm501 code doesn't check whether a right-to-left operation has specified a width greater than the x-coordinate (which would make it extend off the left edge of the screen), or similarly a height greater than the y-coordinate. This means the guest can misprogram the device so that we underflow when calculating the address of the top left pixel, which might result in accessing out of bounds memory. Catch this as a guest error and ignore the operation.
Reported-by: Yannick Wang
Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3920
--- hw/display/sm501.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/display/sm501.c +8 -0
@@ -723,6 +723,10 @@ static void sm501_2d_operation(SM501State *s) }if (rtl) { +if (dst_x < (width -1 ) || dst_y < (height -1 )) { +qemu_log_mask (LOG_GUEST_ERROR ,"sm501: RTL op out of bounds\n" ); +return ; + } dst_x -= width -1 ; dst_y -= height -1 ; }
@@ -748,6 +752,10 @@ static void sm501_2d_operation(SM501State *s) }if (rtl) { +if (src_x < (width -1 ) || src_y < (height -1 )) { +qemu_log_mask (LOG_GUEST_ERROR ,"sm501: RTL op out of bounds\n" ); +return ; + } src_x -= width -1 ; src_y -= height -1 ; }
Signature · 1 line
2.43.0
PATCH 2/3 hw/display/sm501: Avoid overflow problems in bounds check calculations
In reply to Peter MaydellWhen we check that a 2D rectangle operation isn't going to run off the end of video RAM, we do the calculations as 32 bit arithmetic. This means that carefully chosen guest register values can cause an overflow so we don't detect that the operation is going to go outside video memory.
Abstract the check out into a function, do the calculations as 64-bit arithmetic, and add assertions about the ranges of the inputs.
Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3920
--- hw/display/sm501.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-)
hw/display/sm501.c +26 -6
@@ -682,6 +682,28 @@ static inline void hwc_invalidate(SM501State *s, int crt) get_fb_addr(s, crt) + start, end - start); } +static bool sm501_rect_outside_vram (SM501State *s,uint32_t base, +uint32_t x,uint32_t y, +uint32_t width,uint32_t height, +uint32_t pitch,uint32_t bypp) +{ +/* +* Return true if the 2D area specified by the arguments is +* partially or completely outside the VRAM (a guest error) +* +* Limits on the input sizes mean we can't overflow as long as +* we do all the arithmetic at 64 bits. +*/ +uint64_t rect_size, last_addr; + +assert (x <=UINT16_MAX && y <=UINT16_MAX && height <=UINT16_MAX && + pitch <=UINT16_MAX && bypp <=8 ); + rect_size = (((uint64_t )y + height) * pitch + x + width) * bypp; + last_addr = base + rect_size; + +return last_addr >=get_local_mem_size (s); +} +static void sm501_2d_operation (SM501State *s) {int cmd = (s->twoD_control >>16 ) &0x1F ;
@@ -731,9 +753,8 @@ static void sm501_2d_operation(SM501State *s) dst_y -= height -1 ; } -if (dst_base >=get_local_mem_size (s) || - dst_base + (dst_x + width + (dst_y + height) * dst_pitch) * bypp >= -get_local_mem_size (s)) { +if (sm501_rect_outside_vram (s, dst_base, dst_x, dst_y, width, height, + dst_pitch, bypp)) {qemu_log_mask (LOG_GUEST_ERROR ,"sm501: 2D op dest is outside vram.\n" );return ; }
@@ -760,9 +781,8 @@ static void sm501_2d_operation(SM501State *s) src_y -= height -1 ; } -if (src_base >=get_local_mem_size (s) || - src_base + (src_x + width + (src_y + height) * src_pitch) * bypp >= -get_local_mem_size (s)) { +if (sm501_rect_outside_vram (s, src_base, src_x, src_y, width, height, + src_pitch, bypp)) {qemu_log_mask (LOG_GUEST_ERROR ,"sm501: 2D op src is outside vram.\n" );return ;
Signature · 1 line
2.43.0
PATCH 3/3 hw/display/sm501: Don't allow guest to set ram size larger than it is
In reply to Peter MaydellThe SM501 DRAM_CONTROL register has a 7 bit Size field which allows the guest to change the local memory size. We use the local memory size in bounds checks calculations for 2D operations. Currently we have no check on the validity of the value the guest programs to this field, which means that the guest can:
- set it to a reserved value (6 or 7) which will cause get_local_mem_size() to read outside sm501_mem_local_size[]
- set it to a value corresponding to more RAM than the card was created with, so that the 2D bounds check will let 2D operations access off the end of the memory region
Fix this by decoupling the value the guest reads and writes to this field from the internal size we consider the local memory to have. We validate changes and ignore them except for readback if they would be reserved values or values for more memory than the card has.
Cc: [email protected]
Reported-by: Heechan Kang
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3811
--- hw/display/sm501.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
hw/display/sm501.c +21 -3
@@ -571,6 +571,25 @@ static uint32_t get_local_mem_size_index(uint32_t size)return index; } +static void set_new_local_mem_size_index (SM501State *s,uint32_t idx) +{ +/* +* Update local_mem_size_index on guest write. We don't allow this +* to be set to larger than the actual RAM size. (The guest will +* still read back the SYSTEM_CONTROL.Size bits that it wrote.) +*/ +if (idx <ARRAY_SIZE (sm501_mem_local_size) && + sm501_mem_local_size[idx] <=memory_region_size (&s->local_mem_region)) { + s->local_mem_size_index = idx; +return ; + } +qemu_log_mask (LOG_GUEST_ERROR , +"sm501: Guest set DRAM_CONTROL.Size to 0x%x but " +"local memory is not that large\n" , + idx); +/* Don't change the effective size, leave it as whatever it was */ +} +static ram_addr_t get_fb_addr (SM501State *s,int crt) {return (crt ? s->dc_crt_fb_addr : s->dc_panel_fb_addr) &0x3FFFFF0 ;
@@ -990,7 +1009,7 @@ static uint64_t sm501_system_config_read(void *opaque, hwaddr addr, ret =0x050100A0 ;break ;case SM501_DRAM_CONTROL : - ret = (s->dram_control &0x07F107C0 ) | s->local_mem_size_index <<13 ; + ret = (s->dram_control &0x07F1E7C0 );break ;case SM501_ARBTRTN_CONTROL : ret = s->arbitration_control;
@@ -1049,8 +1068,7 @@ static void sm501_system_config_write(void *opaque, hwaddr addr, s->gpio_63_32_control = value &0xFF80FFFF ;break ;case SM501_DRAM_CONTROL : - s->local_mem_size_index = (value >>13 ) &0x7 ; -/* TODO : check validity of size change */ +set_new_local_mem_size_index (s, (value >>13 ) &0x7 ); s->dram_control &=0x80000000 ; s->dram_control |= value &0x7FFFFFC3 ;break ;
Signature · 1 line
2.43.0
Quoted 7 lines · Peter Maydell
This set of patches fixes three bugs discovered recently in the sm501 device. They're all of the form "guest can misprogram registers in a way that defeats the bounds-checks on the 2D graphics ops".
thanks
-- PMM
Quoted 7 lines
Peter Maydell (3):
hw/display/sm501: Catch bad coordinates for RTL operations hw/display/sm501: Avoid overflow problems in bounds check calculations hw/display/sm501: Don't allow guest to set ram size larger than it is
hw/display/sm501.c| 64 +++++++++++++++++++++++++++++++++++++++-------1 file changed, 55 insertions(+), 9 deletions(-)
Thank you for fixing these.
Series:
I think patch 2 should have:
"Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3584" instead of work_items/3920.
Regards,
BALATON Zoltan
Quoted 19 lines · BALATON Zoltan
Quoted 7 lines · Peter Maydell
This set of patches fixes three bugs discovered recently in the sm501 device. They're all of the form "guest can misprogram registers in a way that defeats the bounds-checks on the 2D graphics ops".
thanks
-- PMMQuoted 7 lines
Peter Maydell (3):
hw/display/sm501: Catch bad coordinates for RTL operations hw/display/sm501: Avoid overflow problems in bounds check calculations hw/display/sm501: Don't allow guest to set ram size larger than it is
hw/display/sm501.c| 64 +++++++++++++++++++++++++++++++++++++++-------1 file changed, 55 insertions(+), 9 deletions(-)Thank you for fixing these.
Quoted 7 lines
Series:
Tested-by BALATON Zoltan <[email protected]>Reviewed-by BALATON Zoltan <[email protected]>I think patch 2 should have:
"Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3584" instead of work_items/3920.
Yes, thanks for that catch and for the testing; I cut and pasted the wrong URL there.
-- PMM
Quoted 25 lines · Peter Maydell
Quoted 23 lines · BALATON Zoltan
Quoted 7 lines · Peter Maydell
This set of patches fixes three bugs discovered recently in the sm501 device. They're all of the form "guest can misprogram registers in a way that defeats the bounds-checks on the 2D graphics ops".
thanks
-- PMMQuoted 7 lines
Peter Maydell (3):
hw/display/sm501: Catch bad coordinates for RTL operations hw/display/sm501: Avoid overflow problems in bounds check calculations hw/display/sm501: Don't allow guest to set ram size larger than it is
hw/display/sm501.c| 64 +++++++++++++++++++++++++++++++++++++++-------1 file changed, 55 insertions(+), 9 deletions(-)Thank you for fixing these.
Series:
Tested-by BALATON Zoltan <[email protected]>Reviewed-by BALATON Zoltan <[email protected]>
Quoted 6 lines
Quoted 3 lines
I think patch 2 should have:
"Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3584" instead of work_items/3920.Yes, thanks for that catch and for the testing; I cut and pasted the wrong URL there.
As I'm putting together a target-arm pullreq, I'll take these patches through that, unless somebody from the ppc side would prefer to take them via another route.
-- PMM