Skip to contentSkip to conversation

ui/cursor: fix integer overflow in cursor_alloc (CVE-2022-4206)

PATCH Superseded Mauro Matteo Cascella 4 participants 6 messages Apr 5, 2022 – Apr 6, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/cursor: fix integer overflow in cursor_alloc (CVE-2022-4206)

Prevent potential integer overflow by limiting 'width' and 'height' to 512x512. Also change 'datasize' type to size_t. Refer to security advisory https://starlabs.sg/advisories/22-4206/ for more information.

Fixes CVE-2022-4206
---
 hw/display/qxl-render.c |  7 +++++++
 ui/cursor.c             | 12 +++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
hw/display/qxl-render.c +7 -0
index d28849b121..dc3c4edd05 100644
@@ -247,6 +247,13 @@ static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
     size_t size;
 
     c = cursor_alloc(cursor->header.width, cursor->header.height);
+
+    if (!c) {
+        qxl_set_guest_bug(qxl, "%s: cursor %ux%u alloc error", __func__,
+                cursor->header.width, cursor->header.height);
+        goto fail;
+    }
+
     c->hot_x = cursor->header.hot_spot_x;
     c->hot_y = cursor->header.hot_spot_y;
     switch (cursor->header.type) {
ui/cursor.c +11 -1
index 1d62ddd4d0..7cfb08a030 100644
@@ -46,6 +46,13 @@ static QEMUCursor *cursor_parse_xpm(const char *xpm[])
 
     /* parse pixel data */
     c = cursor_alloc(width, height);
+
+    if (!c) {
+        fprintf(stderr, "%s: cursor %ux%u alloc error\n",
+                __func__, width, height);
+        return NULL;
+    }
+
     for (pixel = 0, y = 0; y < height; y++, line++) {
         for (x = 0; x < height; x++, pixel++) {
             idx = xpm[line][x];
@@ -91,7 +98,10 @@ QEMUCursor *cursor_builtin_left_ptr(void)
 QEMUCursor *cursor_alloc(int width, int height)
 {
     QEMUCursor *c;
-    int datasize = width * height * sizeof(uint32_t);
+    size_t datasize = width * height * sizeof(uint32_t);
+
+    if (width > 512 || height > 512)
+        return NULL;
 
     c = g_malloc0(sizeof(QEMUCursor) + datasize);
     c->width  = width;
Signature · 1 line
2.35.1
#1 lore raw

Hi

Quoted 35 lines · Mauro Matteo Cascella

Prevent potential integer overflow by limiting 'width' and 'height' to 512x512. Also change 'datasize' type to size_t. Refer to security advisory https://starlabs.sg/advisories/22-4206/ for more information.

Fixes CVE-2022-4206
---
 hw/display/qxl-render.c |  7 +++++++
 ui/cursor.c             | 12 +++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
hw/display/qxl-render.c +7 -0
index d28849b121..dc3c4edd05 100644
@@ -247,6 +247,13 @@ static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl,
 QXLCursor *cursor,
     size_t size;
 
     c = cursor_alloc(cursor->header.width, cursor->header.height);
+
+    if (!c) {
+        qxl_set_guest_bug(qxl, "%s: cursor %ux%u alloc error", __func__,
+                cursor->header.width, cursor->header.height);
+        goto fail;
+    }
+
     c->hot_x = cursor->header.hot_spot_x;
     c->hot_y = cursor->header.hot_spot_y;
     switch (cursor->header.type) {
ui/cursor.c +0 -0
index 1d62ddd4d0..7cfb08a030 100644

ui/cursor.c static QEMUCursor *cursor_parse_xpm(const char *xpm[]) · line 55

Quoted 10 lines
ui/cursor.c +6 -0
@@ -46,6 +46,13 @@ static QEMUCursor *cursor_parse_xpm(const char *xpm[])
 
     /* parse pixel data */
     c = cursor_alloc(width, height);
+
+    if (!c) {
+        fprintf(stderr, "%s: cursor %ux%u alloc error\n",
+                __func__, width, height);
+        return NULL;
+    }
 

I think you could simply abort() in this function. It is used with static data (ui/cursor*.xpm)

+

Quoted 16 lines
ui/cursor.c +4 -2
     for (pixel = 0, y = 0; y < height; y++, line++) {
         for (x = 0; x < height; x++, pixel++) {
             idx = xpm[line][x];
@@ -91,7 +98,10 @@ QEMUCursor *cursor_builtin_left_ptr(void)
 QEMUCursor *cursor_alloc(int width, int height)
 {
     QEMUCursor *c;
-    int datasize = width * height * sizeof(uint32_t);
+    size_t datasize = width * height * sizeof(uint32_t);
+
+    if (width > 512 || height > 512)
+        return NULL;
 
     c = g_malloc0(sizeof(QEMUCursor) + datasize);
     c->width  = width;
--
Quoted 4 lines

2.35.1

lgtm otherwise

Signature · 1 line
Marc-André Lureau
#2 lore raw
Quoted 15 lines

ui/cursor.c static QEMUCursor *cursor_parse_xpm(const char *xpm[]) · line 55

Quoted 12 lines

+++ b/ui/cursor.c

ui/cursor.c +6 -0
@@ -46,6 +46,13 @@ static QEMUCursor *cursor_parse_xpm(const char *xpm[])
 
     /* parse pixel data */
     c = cursor_alloc(width, height);
+
+    if (!c) {
+        fprintf(stderr, "%s: cursor %ux%u alloc error\n",
+                __func__, width, height);
+        return NULL;
+    }
 

I think you could simply abort() in this function. It is used with static data (ui/cursor*.xpm)

Yes, that should never happen.

Missing: vmsvga_cursor_define() calls cursor_alloc() with guest-supplied values too.

take care,
Gerd

#3 lore raw
Quoted 17 lines · Gerd Hoffmann
Quoted 4 lines
Quoted 4 lines

+++ b/ui/cursor.c

(quoted diff) +0 -0
@@ -46,6 +46,13 @@ static QEMUCursor *cursor_parse_xpm(const char *xpm[])
 
Quoted 12 lines

ui/cursor.c static QEMUCursor *cursor_parse_xpm(const char *xpm[])

Quoted 9 lines
ui/cursor.c +6 -0
     /* parse pixel data */
     c = cursor_alloc(width, height);
+
+    if (!c) {
+        fprintf(stderr, "%s: cursor %ux%u alloc error\n",
+                __func__, width, height);
+        return NULL;
+    }
 

I think you could simply abort() in this function. It is used with static data (ui/cursor*.xpm)

Quoted 4 lines

Yes, that should never happen.

Missing: vmsvga_cursor_define() calls cursor_alloc() with guest-supplied values too.

I skipped that because the check (cursor.width > 256 || cursor.height

Quoted 1 line

256) is already done in vmsvga_fifo_run before calling

vmsvga_cursor_define. You want me to add another check in vmsvga_cursor_define and return NULL if cursor_alloc fails?

Quoted 3 lines

take care,
Gerd

--
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0

#4 lore raw
Quoted 20 lines · Mauro Matteo Cascella
Quoted 19 lines · Gerd Hoffmann
Quoted 4 lines
Quoted 4 lines

+++ b/ui/cursor.c

(quoted diff) +0 -0
@@ -46,6 +46,13 @@ static QEMUCursor *cursor_parse_xpm(const char *xpm[])
 
Quoted 12 lines

ui/cursor.c static QEMUCursor *cursor_parse_xpm(const char *xpm[])

Quoted 9 lines
ui/cursor.c +6 -0
     /* parse pixel data */
     c = cursor_alloc(width, height);
+
+    if (!c) {
+        fprintf(stderr, "%s: cursor %ux%u alloc error\n",
+                __func__, width, height);
+        return NULL;
+    }
 

I think you could simply abort() in this function. It is used with static data (ui/cursor*.xpm)

Yes, that should never happen.

Quoted 7 lines
Quoted 2 lines

Missing: vmsvga_cursor_define() calls cursor_alloc() with guest-supplied values too.

I skipped that because the check (cursor.width > 256 || cursor.height

Quoted 1 line

256) is already done in vmsvga_fifo_run before calling

vmsvga_cursor_define. You want me to add another check in vmsvga_cursor_define and return NULL if cursor_alloc fails?

No check required then.

Maybe add an 'assert(c != NULL)' to clarify this should never happen b/c those call sites never call cursor_alloc() with width/height being too big (your choice, applies to both vmsvga and ui/cursor.c).

take care,
Gerd

#5 lore raw
In reply to Mauro Matteo Cascella
Quoted 13 lines · Mauro Matteo Cascella

Prevent potential integer overflow by limiting 'width' and 'height' to 512x512. Also change 'datasize' type to size_t. Refer to security advisory https://starlabs.sg/advisories/22-4206/ for more information.

Fixes CVE-2022-4206
ui/cursor.c +0 -0
index 1d62ddd4d0..7cfb08a030 100644

ui/cursor.c static QEMUCursor *cursor_parse_xpm(const char *xpm[]) · line 53

Quoted 8 lines
ui/cursor.c +5 -0
@@ -46,6 +46,13 @@ static QEMUCursor *cursor_parse_xpm(const char *xpm[])
 
     /* parse pixel data */
     c = cursor_alloc(width, height);
+
+    if (!c) {
+        fprintf(stderr, "%s: cursor %ux%u alloc error\n",
+                __func__, width, height);
+        return NULL;

Side note, we should probably clean up the error handling in this file to not be "print to stderr" at some point...

Quoted 6 lines
ui/cursor.c +2 -0
+    }
+
     for (pixel = 0, y = 0; y < height; y++, line++) {
         for (x = 0; x < height; x++, pixel++) {
             idx = xpm[line][x];

ui/cursor.c QEMUCursor *cursor_builtin_left_ptr(void) · line 104

Quoted 8 lines
ui/cursor.c +4 -1
@@ -91,7 +98,10 @@ QEMUCursor *cursor_builtin_left_ptr(void)
 QEMUCursor *cursor_alloc(int width, int height)
 {
     QEMUCursor *c;
-    int datasize = width * height * sizeof(uint32_t);
+    size_t datasize = width * height * sizeof(uint32_t);
+
+    if (width > 512 || height > 512)
+        return NULL;

Coding style requires braces on if statements.

thanks
-- PMM