Skip to contentSkip to conversation

ui/pixman: fix zero rowstride in qemu_pixman_image_new_shareable()

PATCH In review [email protected] 2 participants 2 messages Jun 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/pixman: fix zero rowstride in qemu_pixman_image_new_shareable()

From: Marc-André Lureau <[email protected]>

qemu_create_displaysurface_from() callers such as xlnx_dp.c pass linesize=0 with data=NULL, relying on pixman to compute the stride. Since 1ff788db978 ("ui: use a shareable type"), the data=NULL path goes through qemu_pixman_image_new_shareable() which computes size = height * rowstride_bytes, resulting in a zero-size allocation and an abort in qemu_memfd_alloc().

Introduce qemu-pixman-helpers.h with overflow-safe stride and buffer size computation (matching pixman's create_bits() formula), and use it from both qemu_pixman_image_new_shareable() and pixman-minimal's create_bits().

Fixes 1ff788db9781 ("ui: use a shareable type")
---
 include/ui/pixman-minimal.h      | 25 +++-------------
 include/ui/qemu-pixman-helpers.h | 49 ++++++++++++++++++++++++++++++++
 include/ui/qemu-pixman.h         |  1 +
 ui/qemu-pixman.c                 |  8 +++++-
 4 files changed, 61 insertions(+), 22 deletions(-)
 create mode 100644 include/ui/qemu-pixman-helpers.h
include/ui/pixman-minimal.h +4 -21
index 6dd7de1c7e7..680cc809ace 100644
@@ -113,6 +113,8 @@ typedef struct pixman_color {
     uint16_t    alpha;
 } pixman_color_t;
 
+#include "qemu-pixman-helpers.h"
+
 static inline uint32_t *create_bits(pixman_format_code_t format,
                                     int width,
                                     int height,
@@ -120,28 +122,9 @@ static inline uint32_t *create_bits(pixman_format_code_t format,
 {
     int stride = 0;
     size_t buf_size = 0;
-    int bpp = PIXMAN_FORMAT_BPP(format);
-
-    /*
-     * Calculate the following while checking for overflow truncation:
-     * stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
-     */
-
-    if (unlikely(__builtin_mul_overflow(width, bpp, &stride))) {
-        return NULL;
-    }
-
-    if (unlikely(__builtin_add_overflow(stride, 0x1f, &stride))) {
-        return NULL;
-    }
-
-    stride >>= 5;
-
-    stride *= sizeof(uint32_t);
 
-    if (unlikely(__builtin_mul_overflow((size_t) height,
-                                        (size_t) stride,
-                                        &buf_size))) {
+    if (!qemu_pixman_image_calc_size(format, width, height,
+                                     &stride, &buf_size)) {
         return NULL;
     }
 
include/ui/qemu-pixman-helpers.h new file +49 -0

49 lines not on this page · raw message

include/ui/qemu-pixman.h +1 -0
index 4bc7a59698e..1d042c7f634 100644
@@ -8,6 +8,7 @@
 
 #ifdef CONFIG_PIXMAN
 #include <pixman.h>
+#include "qemu-pixman-helpers.h"
 #else
 #include "pixman-minimal.h"
 #endif
ui/qemu-pixman.c +7 -1
index aea09755b94..9dc606f692a 100644
@@ -320,12 +320,18 @@ qemu_pixman_image_new_shareable(pixman_image_t **image,
                                 Error **errp)
 {
     ERRP_GUARD();
-    size_t size = height * rowstride_bytes;
+    size_t size;
     void *bits = NULL;
 
     g_return_val_if_fail(image != NULL, false);
     g_return_val_if_fail(handle != NULL, false);
 
+    if (!qemu_pixman_image_calc_size(format, width, height,
+                                     &rowstride_bytes, &size)) {
+        error_setg(errp, "Image dimensions overflow");
+        return false;
+    }
+
     bits = qemu_pixman_shareable_alloc(name, size, handle, errp);
     if (!bits) {
         return false;
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · On 11/6/26 13:36, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu_create_displaysurface_from() callers such as xlnx_dp.c pass linesize=0 with data=NULL, relying on pixman to compute the stride. Since 1ff788db978 ("ui: use a shareable type"), the data=NULL path goes through qemu_pixman_image_new_shareable() which computes size = height * rowstride_bytes, resulting in a zero-size allocation and an abort in qemu_memfd_alloc().

Introduce qemu-pixman-helpers.h with overflow-safe stride and buffer size computation (matching pixman's create_bits() formula), and use it from both qemu_pixman_image_new_shareable() and pixman-minimal's create_bits().

Fixes 1ff788db9781 ("ui: use a shareable type")
Quoted 7 lines
---
  include/ui/pixman-minimal.h      | 25 +++-------------
  include/ui/qemu-pixman-helpers.h | 49 ++++++++++++++++++++++++++++++++
  include/ui/qemu-pixman.h         |  1 +
  ui/qemu-pixman.c                 |  8 +++++-
  4 files changed, 61 insertions(+), 22 deletions(-)

create mode 100644 include/ui/qemu-pixman-helpers.h

Nice.