Skip to contentSkip to conversation

hw/display/exynos4210_find: Clamp windows to screen

PATCH In review Peter Maydell 3 participants 9 messages Jul 6 – Jul 28 Arm Samsung Exynos

lore
Share & tools
lore
#0 lore raw

cover hw/display/exynos4210_find: Clamp windows to screen

The exynos4210_fimd device has a setup where the guest can program both the overall size of the LCD screen and also the corners of "windows" that define where the guest memory should appear on that screen. The current code assumes that the guest is sensible and puts the corners of windows inside the overall screen. If it doesn't then we will try to copy data to an address that is outside the interfal framebuffer memory that we allocate to be the size of the screen.

This patchset clamps the window coordinates to be within the screen size. Patches 1 and 2 are simple refactorings to make the third patch easier to read.

This fixes https://gitlab.com/qemu-project/qemu/-/work_items/3795

thanks
-- PMM

Peter Maydell (3):
hw/display/exynos4210_fimd: Factor out finding screen width/height hw/display/exynos4210_fimd: Pass width to draw_line functions hw/display/exynos4210_fimd: Clamp windows to screen size

 hw/display/exynos4210_fimd.c | 54 ++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 hw/display/exynos4210_fimd: Factor out finding screen width/height

Currently we hard-code the expressions for getting the global screen width and height out of the VIDTCON2 register where we need them. Use functions instead. Make the global_width variable in exynos4210_fimd_update() uint32_t for consistency. (The values are clamped to well below INT_MAX, so there is no overflow risk here.)

Stable CC because this is a prerequisite for an upcoming bugfix commit.

---
 hw/display/exynos4210_fimd.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
hw/display/exynos4210_fimd.c +16 -6
index 7507e4fd3c..43ad5fa0b4 100644
@@ -1195,15 +1195,25 @@ static void exynos4210_fimd_update_irq(Exynos4210fimdState *s)
     }
 }
 
+static uint32_t exynos4210_fimd_global_width(Exynos4210fimdState *s)
+{
+    return ((s->vidtcon[2] >> FIMD_VIDTCON2_HOR_SHIFT) &
+            FIMD_VIDTCON2_SIZE_MASK) + 1;
+}
+
+static uint32_t exynos4210_fimd_global_height(Exynos4210fimdState *s)
+{
+    return ((s->vidtcon[2] >> FIMD_VIDTCON2_VER_SHIFT) &
+            FIMD_VIDTCON2_SIZE_MASK) + 1;
+}
+
 static void exynos4210_update_resolution(Exynos4210fimdState *s)
 {
     DisplaySurface *surface = qemu_console_surface(s->console);
 
     /* LCD resolution is stored in VIDEO TIME CONTROL REGISTER 2 */
-    uint32_t width = ((s->vidtcon[2] >> FIMD_VIDTCON2_HOR_SHIFT) &
-            FIMD_VIDTCON2_SIZE_MASK) + 1;
-    uint32_t height = ((s->vidtcon[2] >> FIMD_VIDTCON2_VER_SHIFT) &
-            FIMD_VIDTCON2_SIZE_MASK) + 1;
+    uint32_t width = exynos4210_fimd_global_width(s);
+    uint32_t height = exynos4210_fimd_global_height(s);
 
     if (s->ifb == NULL || surface_width(surface) != width ||
             surface_height(surface) != height) {
@@ -1229,14 +1239,14 @@ static bool exynos4210_fimd_update(void *opaque)
     bool blend = false;
     uint8_t *host_fb_addr;
     bool is_dirty = false;
-    int global_width;
+    uint32_t global_width;
 
     if (!s || !s->console || !s->enabled ||
         surface_bits_per_pixel(qemu_console_surface(s->console)) == 0) {
         return true;
     }
 
-    global_width = (s->vidtcon[2] & FIMD_VIDTCON2_SIZE_MASK) + 1;
+    global_width = exynos4210_fimd_global_width(s);
     exynos4210_update_resolution(s);
     surface = qemu_console_surface(s->console);
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 hw/display/exynos4210_fimd: Pass width to draw_line functions

In reply to Peter Maydell

The draw_line functions currently assume the width of the line they need to draw is w->rightbot_x - w->lefttop_x + 1, i.e. the full width of the guest-programmed window. We want to be able to clamp this to the overall screen size, which we can calculate in the calling function.

Refactor to do this calculation in the caller and pass the width as an argument to the draw_line functions.

Stable CC because this is a prerequisite for an upcoming bugfix commit.

---
 hw/display/exynos4210_fimd.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
hw/display/exynos4210_fimd.c +10 -8
index 43ad5fa0b4..52f2285cf5 100644
@@ -282,7 +282,7 @@ struct Exynos4210fimdWindow {
 
     pixel_to_rgb_func *pixel_to_rgb;
     void (*draw_line)(Exynos4210fimdWindow *w, uint8_t *src, uint8_t *dst,
-            bool blend);
+                      uint32_t width, bool blend);
     uint32_t (*get_alpha)(Exynos4210fimdWindow *w, uint32_t pix_a);
     uint16_t lefttop_x, lefttop_y;   /* VIDOSD0 register */
     uint16_t rightbot_x, rightbot_y; /* VIDOSD1 register */
@@ -784,9 +784,9 @@ exynos4210_fimd_blend_pixel(Exynos4210fimdWindow *w, rgba p_bg, rgba *ret)
 /* Draw line with index in palette table in RAM frame buffer data */
 #define DEF_DRAW_LINE_PALETTE(N) \
 static void glue(draw_line_palette_, N)(Exynos4210fimdWindow *w, uint8_t *src, \
-               uint8_t *dst, bool blend) \
+                                        uint8_t *dst, uint32_t width, \
+                                        bool blend)                   \
 { \
-    int width = w->rightbot_x - w->lefttop_x + 1; \
     uint8_t *ifb = dst; \
     uint8_t swap = (w->wincon & FIMD_WINCON_SWAP) >> FIMD_WINCON_SWAP_SHIFT; \
     uint64_t data; \
@@ -813,9 +813,8 @@ static void glue(draw_line_palette_, N)(Exynos4210fimdWindow *w, uint8_t *src, \
 /* Draw line with direct color value in RAM frame buffer data */
 #define DEF_DRAW_LINE_NOPALETTE(N) \
 static void glue(draw_line_, N)(Exynos4210fimdWindow *w, uint8_t *src, \
-                    uint8_t *dst, bool blend) \
+                                uint8_t *dst, uint32_t width, bool blend) \
 { \
-    int width = w->rightbot_x - w->lefttop_x + 1; \
     uint8_t *ifb = dst; \
     uint8_t swap = (w->wincon & FIMD_WINCON_SWAP) >> FIMD_WINCON_SWAP_SHIFT; \
     uint64_t data; \
@@ -848,11 +847,10 @@ DEF_DRAW_LINE_NOPALETTE(32)
 
 /* Special draw line routine for window color map case */
 static void draw_line_mapcolor(Exynos4210fimdWindow *w, uint8_t *src,
-                       uint8_t *dst, bool blend)
+                               uint8_t *dst, uint32_t width, bool blend)
 {
     rgba p, p_old;
     uint8_t *ifb = dst;
-    int width = w->rightbot_x - w->lefttop_x + 1;
     uint32_t map_color = w->winmap & FIMD_WINMAP_COLOR_MASK;
 
     do {
@@ -1240,6 +1238,7 @@ static bool exynos4210_fimd_update(void *opaque)
     uint8_t *host_fb_addr;
     bool is_dirty = false;
     uint32_t global_width;
+    uint32_t window_width;
 
     if (!s || !s->console || !s->enabled ||
         surface_bits_per_pixel(qemu_console_surface(s->console)) == 0) {
@@ -1255,6 +1254,8 @@ static bool exynos4210_fimd_update(void *opaque)
         if ((w->wincon & FIMD_WINCON_ENWIN) && w->host_fb_addr) {
             scrn_height = w->rightbot_y - w->lefttop_y + 1;
             scrn_width = w->virtpage_width;
+            /* Number of bytes to actually draw */
+            window_width = w->rightbot_x - w->lefttop_x + 1;
             /* Total width of virtual screen page in bytes */
             inc_size = scrn_width + w->virtpage_offsize;
             host_fb_addr = w->host_fb_addr;
@@ -1273,7 +1274,8 @@ static bool exynos4210_fimd_update(void *opaque)
                     last_line = line;
                     w->draw_line(w, host_fb_addr, s->ifb +
                         w->lefttop_x * RGBA_SIZE + (w->lefttop_y + line) *
-                        global_width * RGBA_SIZE, blend);
+                                 global_width * RGBA_SIZE,
+                                 window_width, blend);
                 }
                 host_fb_addr += inc_size;
                 fb_line_addr += inc_size;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 hw/display/exynos4210_fimd: Clamp windows to screen size

In reply to Peter Maydell

In exynos4210_fimd_update(), we iterate through the enabled windows, blitting them to the screen. We assume here that the guest has not programmed the window's coordinates to be outside the overall LCD screen resulation, but we never check this. This can result in the guest being able to cause us to access outside our allocated framebuffer backing memory.

Since all the coordinates here are unsigned, they can't be off the left/top side of the screen, only the bottom/right. If the top left corner of the window is out of bounds, the whole window is invisible and we can skip it. If the bottom right corner is out of bounds, we clamp it to the screen size so that we only draw the visible part.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3795

---
 hw/display/exynos4210_fimd.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
hw/display/exynos4210_fimd.c +15 -3
index 52f2285cf5..6c062a28c0 100644
@@ -1237,7 +1237,7 @@ static bool exynos4210_fimd_update(void *opaque)
     bool blend = false;
     uint8_t *host_fb_addr;
     bool is_dirty = false;
-    uint32_t global_width;
+    uint32_t global_width, global_height;
     uint32_t window_width;
 
     if (!s || !s->console || !s->enabled ||
@@ -1246,16 +1246,28 @@ static bool exynos4210_fimd_update(void *opaque)
     }
 
     global_width = exynos4210_fimd_global_width(s);
+    global_height = exynos4210_fimd_global_height(s);
     exynos4210_update_resolution(s);
     surface = qemu_console_surface(s->console);
 
     for (i = 0; i < NUM_OF_WINDOWS; i++) {
         w = &s->window[i];
         if ((w->wincon & FIMD_WINCON_ENWIN) && w->host_fb_addr) {
-            scrn_height = w->rightbot_y - w->lefttop_y + 1;
+            uint32_t rightbot_x, rightbot_y;
+
+            if (w->lefttop_x >= global_width ||
+                w->lefttop_y >= global_height) {
+                /* Guest has put the window entirely offscreen: ignore */
+                continue;
+            }
+
+            /* Clamp right corner coords to be within the screen */
+            rightbot_x = MIN(w->rightbot_x, global_width - 1);
+            rightbot_y = MIN(w->rightbot_y, global_height - 1);
+            scrn_height = rightbot_y - w->lefttop_y + 1;
             scrn_width = w->virtpage_width;
             /* Number of bytes to actually draw */
-            window_width = w->rightbot_x - w->lefttop_x + 1;
+            window_width = rightbot_x - w->lefttop_x + 1;
             /* Total width of virtual screen page in bytes */
             inc_size = scrn_width + w->virtpage_offsize;
             host_fb_addr = w->host_fb_addr;
Signature · 1 line
2.43.0
#4 lore raw
In reply to Peter Maydell

Ping for code review, please?

thanks
-- PMM

Quoted 26 lines · Peter Maydell

The exynos4210_fimd device has a setup where the guest can program both the overall size of the LCD screen and also the corners of "windows" that define where the guest memory should appear on that screen. The current code assumes that the guest is sensible and puts the corners of windows inside the overall screen. If it doesn't then we will try to copy data to an address that is outside the interfal framebuffer memory that we allocate to be the size of the screen.

This patchset clamps the window coordinates to be within the screen size. Patches 1 and 2 are simple refactorings to make the third patch easier to read.

This fixes https://gitlab.com/qemu-project/qemu/-/work_items/3795

thanks
-- PMM

Peter Maydell (3):
hw/display/exynos4210_fimd: Factor out finding screen width/height hw/display/exynos4210_fimd: Pass width to draw_line functions hw/display/exynos4210_fimd: Clamp windows to screen size

 hw/display/exynos4210_fimd.c | 54 ++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 15 deletions(-)
#5 lore raw

ping^2?

-- PMM

Quoted 33 lines · Peter Maydell

Ping for code review, please?

thanks
-- PMM

Quoted 26 lines · Peter Maydell

The exynos4210_fimd device has a setup where the guest can program both the overall size of the LCD screen and also the corners of "windows" that define where the guest memory should appear on that screen. The current code assumes that the guest is sensible and puts the corners of windows inside the overall screen. If it doesn't then we will try to copy data to an address that is outside the interfal framebuffer memory that we allocate to be the size of the screen.

This patchset clamps the window coordinates to be within the screen size. Patches 1 and 2 are simple refactorings to make the third patch easier to read.

This fixes https://gitlab.com/qemu-project/qemu/-/work_items/3795

thanks
-- PMM

Peter Maydell (3):
hw/display/exynos4210_fimd: Factor out finding screen width/height hw/display/exynos4210_fimd: Pass width to draw_line functions hw/display/exynos4210_fimd: Clamp windows to screen size

 hw/display/exynos4210_fimd.c | 54 ++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 15 deletions(-)
#6 lore raw
In reply to Peter Maydell

Cc'ing Zoltan & Marc-André.

Quoted 26 lines · Peter Maydell

The exynos4210_fimd device has a setup where the guest can program both the overall size of the LCD screen and also the corners of "windows" that define where the guest memory should appear on that screen. The current code assumes that the guest is sensible and puts the corners of windows inside the overall screen. If it doesn't then we will try to copy data to an address that is outside the interfal framebuffer memory that we allocate to be the size of the screen.

This patchset clamps the window coordinates to be within the screen size. Patches 1 and 2 are simple refactorings to make the third patch easier to read.

This fixes https://gitlab.com/qemu-project/qemu/-/work_items/3795

thanks
-- PMM

Peter Maydell (3):
hw/display/exynos4210_fimd: Factor out finding screen width/height hw/display/exynos4210_fimd: Pass width to draw_line functions hw/display/exynos4210_fimd: Clamp windows to screen size

  hw/display/exynos4210_fimd.c | 54 ++++++++++++++++++++++++++----------
  1 file changed, 39 insertions(+), 15 deletions(-)
Reviewed-by on the v1 series
#7 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

The exynos4210_fimd device has a setup where the guest can program both the overall size of the LCD screen and also the corners of "windows" that define where the guest memory should appear on that screen. The current code assumes that the guest is sensible and puts the corners of windows inside the overall screen. If it doesn't then we will try to copy data to an address that is outside the interfal framebuffer memory that we allocate to be the size of the screen.

Quoted 5 lines

This patchset clamps the window coordinates to be within the screen size. Patches 1 and 2 are simple refactorings to make the third patch easier to read.

This fixes https://gitlab.com/qemu-project/qemu/-/work_items/3795

Quoted 16 lines

thanks
-- PMM

Peter Maydell (3):
hw/display/exynos4210_fimd: Factor out finding screen width/height hw/display/exynos4210_fimd: Pass width to draw_line functions hw/display/exynos4210_fimd: Clamp windows to screen size

 hw/display/exynos4210_fimd.c | 54 ++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 15 deletions(-)

--
2.43.0

Signature · 1 line
Marc-André Lureau
#8 lore raw
Quoted 12 lines · Marc-André Lureau
Quoted 10 lines · Peter Maydell

The exynos4210_fimd device has a setup where the guest can program both the overall size of the LCD screen and also the corners of "windows" that define where the guest memory should appear on that screen. The current code assumes that the guest is sensible and puts the corners of windows inside the overall screen. If it doesn't then we will try to copy data to an address that is outside the interfal framebuffer memory that we allocate to be the size of the screen.

Quoted 7 lines
Quoted 5 lines

This patchset clamps the window coordinates to be within the screen size. Patches 1 and 2 are simple refactorings to make the third patch easier to read.

This fixes https://gitlab.com/qemu-project/qemu/-/work_items/3795

Thanks for the review; I've applied this series to target-arm.next.

-- PMM