Skip to contentSkip to conversation

ui/cursor: incomplete check for integer overflow in cursor_alloc

PATCH In review Mauro Matteo Cascella 6 participants 16 messages May 8, 2023 – May 23, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/cursor: incomplete check for integer overflow in cursor_alloc

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")
---
 include/ui/console.h | 4 ++--
 ui/cursor.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
include/ui/console.h +2 -2
index 2a8fab091f..92a4d90a1b 100644
@@ -144,13 +144,13 @@ typedef struct QemuUIInfo {
 
 /* cursor data format is 32bit RGBA */
 typedef struct QEMUCursor {
-    int                 width, height;
+    uint32_t            width, height;
     int                 hot_x, hot_y;
     int                 refcount;
     uint32_t            data[];
 } QEMUCursor;
 
-QEMUCursor *cursor_alloc(int width, int height);
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
 QEMUCursor *cursor_ref(QEMUCursor *c);
 void cursor_unref(QEMUCursor *c);
 QEMUCursor *cursor_builtin_hidden(void);
ui/cursor.c +1 -1
index 6fe67990e2..b5fcb64839 100644
@@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
     return cursor_parse_xpm(cursor_left_ptr_xpm);
 }
 
-QEMUCursor *cursor_alloc(int width, int height)
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;
     size_t datasize = width * height * sizeof(uint32_t);
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw

Hi

Quoted 9 lines · Mauro Matteo Cascella

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Quoted 6 lines
Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

It looks like this is not exploitable, QXL code uses u16 types, and VMWare VGA checks for values > 256. Other paths use fixed size.

---

Quoted 42 lines
 include/ui/console.h | 4 ++--
 ui/cursor.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
include/ui/console.h +2 -2
index 2a8fab091f..92a4d90a1b 100644
@@ -144,13 +144,13 @@ typedef struct QemuUIInfo {
 
 /* cursor data format is 32bit RGBA */
 typedef struct QEMUCursor {
-    int                 width, height;
+    uint32_t            width, height;
     int                 hot_x, hot_y;
     int                 refcount;
     uint32_t            data[];
 } QEMUCursor;
 
-QEMUCursor *cursor_alloc(int width, int height);
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
 QEMUCursor *cursor_ref(QEMUCursor *c);
 void cursor_unref(QEMUCursor *c);
 QEMUCursor *cursor_builtin_hidden(void);
ui/cursor.c +1 -2
index 6fe67990e2..b5fcb64839 100644
@@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
     return cursor_parse_xpm(cursor_left_ptr_xpm);
 }
 
-QEMUCursor *cursor_alloc(int width, int height)
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;
     size_t datasize = width * height * sizeof(uint32_t);
--

2.40.1

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

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

Quoted 8 lines

<mailto:[email protected]>>

<mailto:[email protected]>>

Reviewed-by Marc-André Lureau <[email protected] <mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

Quoted 31 lines

VMWare VGA checks for values > 256. Other paths use fixed size.

    ---
      include/ui/console.h | 4 ++--
      ui/cursor.c          | 2 +-
      2 files changed, 3 insertions(+), 3 deletions(-)

    diff --git a/include/ui/console.h b/include/ui/console.h
    index 2a8fab091f..92a4d90a1b 100644
    --- a/include/ui/console.h
    +++ b/include/ui/console.h
    @@ -144,13 +144,13 @@ typedef struct QemuUIInfo {

      /* cursor data format is 32bit RGBA */
      typedef struct QEMUCursor {
    -    int                 width, height;
    +    uint32_t            width, height;
          int                 hot_x, hot_y;
          int                 refcount;
          uint32_t            data[];
      } QEMUCursor;

    -QEMUCursor *cursor_alloc(int width, int height);
    +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
      QEMUCursor *cursor_ref(QEMUCursor *c);
      void cursor_unref(QEMUCursor *c);
      QEMUCursor *cursor_builtin_hidden(void);
    diff --git a/ui/cursor.c b/ui/cursor.c
    index 6fe67990e2..b5fcb64839 100644
    --- a/ui/cursor.c
    +++ b/ui/cursor.c
Quoted 8 lines
    @@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
          return cursor_parse_xpm(cursor_left_ptr_xpm);
      }

    -QEMUCursor *cursor_alloc(int width, int height)
    +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
      {
          QEMUCursor *c;

Can't we check width/height > 0 && <= SOME_LIMIT_THAT_MAKES_SENSE?

Maybe a 16K * 16K cursor is future proof and safe enough.

Quoted 9 lines
          size_t datasize = width * height * sizeof(uint32_t);
    -- 
    2.40.1
Signature · 1 line
Marc-André Lureau
#3 lore raw
Quoted 68 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Marc-André Lureau

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

<mailto:[email protected]>>

Quoted 8 lines

<mailto:[email protected]>>

<mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

Quoted 35 lines

VMWare VGA checks for values > 256. Other paths use fixed size.

    ---
      include/ui/console.h | 4 ++--
      ui/cursor.c          | 2 +-
      2 files changed, 3 insertions(+), 3 deletions(-)

    diff --git a/include/ui/console.h b/include/ui/console.h
    index 2a8fab091f..92a4d90a1b 100644
    --- a/include/ui/console.h
    +++ b/include/ui/console.h
    @@ -144,13 +144,13 @@ typedef struct QemuUIInfo {

      /* cursor data format is 32bit RGBA */
      typedef struct QEMUCursor {
    -    int                 width, height;
    +    uint32_t            width, height;
          int                 hot_x, hot_y;
          int                 refcount;
          uint32_t            data[];
      } QEMUCursor;

    -QEMUCursor *cursor_alloc(int width, int height);
    +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
      QEMUCursor *cursor_ref(QEMUCursor *c);
      void cursor_unref(QEMUCursor *c);
      QEMUCursor *cursor_builtin_hidden(void);
    diff --git a/ui/cursor.c b/ui/cursor.c
    index 6fe67990e2..b5fcb64839 100644
    --- a/ui/cursor.c
    +++ b/ui/cursor.c
    @@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
          return cursor_parse_xpm(cursor_left_ptr_xpm);
      }
Quoted 6 lines
Quoted 4 lines

-QEMUCursor *cursor_alloc(int width, int height) +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
{
QEMUCursor *c;

Can't we check width/height > 0 && <= SOME_LIMIT_THAT_MAKES_SENSE?

We currently ensure width/height are less than 512 in cursor_alloc.

Checking for positive values is unnecessary if we make width/height unsigned, isn't it?

Quoted 12 lines

Maybe a 16K * 16K cursor is future proof and safe enough.

Quoted 9 lines
          size_t datasize = width * height * sizeof(uint32_t);
    --
    2.40.1

--
Marc-André Lureau

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

#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines
Quoted 4 lines

-QEMUCursor *cursor_alloc(int width, int height) +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;

Can't we check width/height > 0 && <= SOME_LIMIT_THAT_MAKES_SENSE?

Maybe a 16K * 16K cursor is future proof and safe enough.

Modern physical hardware typically uses 512x512 sprites (even if only a fraction of that is actually needed and >90% are just transparent pixels).

take care,
Gerd

#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Marc-André Lureau

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

<mailto:[email protected]>>

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines

<mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

cursor_alloc() will reject 0xffff:

    if (width > 512 || height > 512) {
        return NULL;
    }
Quoted 36 lines
Quoted 36 lines

VMWare VGA checks for values > 256. Other paths use fixed size.

    ---
      include/ui/console.h | 4 ++--
      ui/cursor.c          | 2 +-
      2 files changed, 3 insertions(+), 3 deletions(-)

    diff --git a/include/ui/console.h b/include/ui/console.h
    index 2a8fab091f..92a4d90a1b 100644
    --- a/include/ui/console.h
    +++ b/include/ui/console.h
    @@ -144,13 +144,13 @@ typedef struct QemuUIInfo {

      /* cursor data format is 32bit RGBA */
      typedef struct QEMUCursor {
    -    int                 width, height;
    +    uint32_t            width, height;
          int                 hot_x, hot_y;
          int                 refcount;
          uint32_t            data[];
      } QEMUCursor;

    -QEMUCursor *cursor_alloc(int width, int height);
    +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
      QEMUCursor *cursor_ref(QEMUCursor *c);
      void cursor_unref(QEMUCursor *c);
      QEMUCursor *cursor_builtin_hidden(void);
    diff --git a/ui/cursor.c b/ui/cursor.c
    index 6fe67990e2..b5fcb64839 100644
    --- a/ui/cursor.c
    +++ b/ui/cursor.c
    @@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
          return cursor_parse_xpm(cursor_left_ptr_xpm);
      }
Quoted 19 lines
Quoted 4 lines

-QEMUCursor *cursor_alloc(int width, int height) +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;

Can't we check width/height > 0 && <= SOME_LIMIT_THAT_MAKES_SENSE?

Maybe a 16K * 16K cursor is future proof and safe enough.

Quoted 8 lines
          size_t datasize = width * height * sizeof(uint32_t);
    --     2.40.1
Signature · 1 line
Marc-André Lureau

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 28 lines · Marc-André Lureau

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

<mailto:[email protected]>>

<mailto:[email protected]>>

<mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

Quoted 7 lines
Quoted 1 line

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

cursor_alloc() will reject 0xffff:

     if (width > 512 || height > 512) {
         return NULL;
     }

I hadn't looked at the source file (the 'datasize' assignation made me incorrectly think it'd be use before sanitized).

Still I wonder why can't we use a simple 'unsigned' type instead of a uint32_t, but I won't insist.

Quoted 41 lines
Quoted 41 lines
Quoted 40 lines

VMWare VGA checks for values > 256. Other paths use fixed size.

     ---
       include/ui/console.h | 4 ++--
       ui/cursor.c          | 2 +-
       2 files changed, 3 insertions(+), 3 deletions(-)

     diff --git a/include/ui/console.h b/include/ui/console.h
     index 2a8fab091f..92a4d90a1b 100644
     --- a/include/ui/console.h
     +++ b/include/ui/console.h
     @@ -144,13 +144,13 @@ typedef struct QemuUIInfo {

       /* cursor data format is 32bit RGBA */
       typedef struct QEMUCursor {
     -    int                 width, height;
     +    uint32_t            width, height;
           int                 hot_x, hot_y;
           int                 refcount;
           uint32_t            data[];
       } QEMUCursor;

     -QEMUCursor *cursor_alloc(int width, int height);
     +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
       QEMUCursor *cursor_ref(QEMUCursor *c);
       void cursor_unref(QEMUCursor *c);
       QEMUCursor *cursor_builtin_hidden(void);
     diff --git a/ui/cursor.c b/ui/cursor.c
     index 6fe67990e2..b5fcb64839 100644
     --- a/ui/cursor.c
     +++ b/ui/cursor.c
     @@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
           return cursor_parse_xpm(cursor_left_ptr_xpm);
       }

     -QEMUCursor *cursor_alloc(int width, int height)
     +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
       {
           QEMUCursor *c;
Quoted 5 lines
Quoted 5 lines

Can't we check width/height > 0 && <= SOME_LIMIT_THAT_MAKES_SENSE?

Maybe a 16K * 16K cursor is future proof and safe enough.

Quoted 1 line
           size_t datasize = width * height * sizeof(uint32_t);

-------------------------------^

Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
     --     2.40.1
Signature · 1 line
Marc-André Lureau

With regards,
Daniel

#7 lore raw
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 28 lines · Marc-André Lureau

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

<mailto:[email protected]>>

<mailto:[email protected]>>

<mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

Quoted 7 lines
Quoted 1 line

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

cursor_alloc() will reject 0xffff:

     if (width > 512 || height > 512) {
         return NULL;
     }
Quoted 5 lines

I hadn't looked at the source file (the 'datasize' assignation made me incorrectly think it'd be use before sanitized).

Still I wonder why can't we use a simple 'unsigned' type instead of a uint32_t, but I won't insist.

I can send v2 with s/uint32_t/uint16_t/ if you think it's a relevant change.

Quoted 62 lines
Quoted 41 lines
Quoted 41 lines
Quoted 40 lines

VMWare VGA checks for values > 256. Other paths use fixed size.

     ---
       include/ui/console.h | 4 ++--
       ui/cursor.c          | 2 +-
       2 files changed, 3 insertions(+), 3 deletions(-)

     diff --git a/include/ui/console.h b/include/ui/console.h
     index 2a8fab091f..92a4d90a1b 100644
     --- a/include/ui/console.h
     +++ b/include/ui/console.h
     @@ -144,13 +144,13 @@ typedef struct QemuUIInfo {

       /* cursor data format is 32bit RGBA */
       typedef struct QEMUCursor {
     -    int                 width, height;
     +    uint32_t            width, height;
           int                 hot_x, hot_y;
           int                 refcount;
           uint32_t            data[];
       } QEMUCursor;

     -QEMUCursor *cursor_alloc(int width, int height);
     +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
       QEMUCursor *cursor_ref(QEMUCursor *c);
       void cursor_unref(QEMUCursor *c);
       QEMUCursor *cursor_builtin_hidden(void);
     diff --git a/ui/cursor.c b/ui/cursor.c
     index 6fe67990e2..b5fcb64839 100644
     --- a/ui/cursor.c
     +++ b/ui/cursor.c
     @@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
           return cursor_parse_xpm(cursor_left_ptr_xpm);
       }

     -QEMUCursor *cursor_alloc(int width, int height)
     +QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
       {
           QEMUCursor *c;
Quoted 5 lines
Quoted 5 lines

Can't we check width/height > 0 && <= SOME_LIMIT_THAT_MAKES_SENSE?

Maybe a 16K * 16K cursor is future proof and safe enough.

Quoted 1 line
           size_t datasize = width * height * sizeof(uint32_t);

-------------------------------^

Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
     --     2.40.1

--
Marc-André Lureau

With regards,
Daniel

Signature · 3 lines
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0
#8 lore raw
Quoted 43 lines · Mauro Matteo Cascella
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Daniel P. Berrangé
Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Marc-André Lureau

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

<mailto:[email protected]>>

<mailto:[email protected]>>

Quoted 6 lines
Quoted 4 lines

<mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

cursor_alloc() will reject 0xffff:

      if (width > 512 || height > 512) {
          return NULL;
      }
Quoted 7 lines
Quoted 5 lines

I hadn't looked at the source file (the 'datasize' assignation made me incorrectly think it'd be use before sanitized).

Still I wonder why can't we use a simple 'unsigned' type instead of a uint32_t, but I won't insist.

I can send v2 with s/uint32_t/uint16_t/ if you think it's a relevant change.

Specifying the word size doesn't really add any (security) value IMHO.

I'll stop bikeshedding here.

Regards,

Phil.

#9 lore raw
Quoted 48 lines · Philippe Mathieu-Daudé
Quoted 46 lines · Mauro Matteo Cascella
Quoted 44 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Daniel P. Berrangé
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 28 lines · Marc-André Lureau

Hi

On Mon, May 8, 2023 at 6:21 PM Mauro Matteo Cascella <[email protected] <mailto:[email protected]>> wrote:

The cursor_alloc function still accepts a signed integer for both the cursor
width and height. A specially crafted negative width/height could make datasize
wrap around and cause the next allocation to be 0, potentially leading to a
heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to
accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc

(CVE-2021-4206)")

<mailto:[email protected]>>

<mailto:[email protected]>>

<mailto:[email protected]>>

It looks like this is not exploitable, QXL code uses u16 types, and

Quoted 7 lines
Quoted 1 line

0xffff * 0xffff * 4 still overflows on 32-bit host, right?

cursor_alloc() will reject 0xffff:

      if (width > 512 || height > 512) {
          return NULL;
      }

I hadn't looked at the source file (the 'datasize' assignation made me incorrectly think it'd be use before sanitized).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Still I wonder why can't we use a simple 'unsigned' type instead of a uint32_t, but I won't insist.

I can send v2 with s/uint32_t/uint16_t/ if you think it's a relevant change.

Specifying the word size doesn't really add any (security) value IMHO.

No security benefit, I know, it just seems more reasonable given what Gerd said about 512x512 sprites.

Quoted 6 lines

I'll stop bikeshedding here.

Regards,

Phil.

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

#10 lore raw
In reply to Mauro Matteo Cascella
Quoted 8 lines · Mauro Matteo Cascella

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")

Looks like -stable material too?

Thanks,

/mjt

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

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")

Addendum:

Quoted 39 lines
---
 include/ui/console.h | 4 ++--
 ui/cursor.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
include/ui/console.h +2 -2
index 2a8fab091f..92a4d90a1b 100644
@@ -144,13 +144,13 @@ typedef struct QemuUIInfo {
 
 /* cursor data format is 32bit RGBA */
 typedef struct QEMUCursor {
-    int                 width, height;
+    uint32_t            width, height;
     int                 hot_x, hot_y;
     int                 refcount;
     uint32_t            data[];
 } QEMUCursor;
 
-QEMUCursor *cursor_alloc(int width, int height);
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
 QEMUCursor *cursor_ref(QEMUCursor *c);
 void cursor_unref(QEMUCursor *c);
 QEMUCursor *cursor_builtin_hidden(void);
ui/cursor.c +1 -2
index 6fe67990e2..b5fcb64839 100644
@@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
     return cursor_parse_xpm(cursor_left_ptr_xpm);
 }
 
-QEMUCursor *cursor_alloc(int width, int height)
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;
     size_t datasize = width * height * sizeof(uint32_t);
--
Quoted 2 lines

2.40.1

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

Reviewed-by on v1 1/1
#12 lore raw
In reply to Mauro Matteo Cascella
Quoted 6 lines · Mauro Matteo Cascella

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

I concur with Marc-Andre that there is no code path that can actually trigger an overflow:

  hw/display/ati.c:        s->cursor = cursor_alloc(64, 64);
  hw/display/vhost-user-gpu.c:            s->current_cursor = cursor_alloc(64, 64);
  hw/display/virtio-gpu.c:            s->current_cursor = cursor_alloc(64, 64);

Not exploitable as fixed size

  hw/display/qxl-render.c:    c = cursor_alloc(cursor->header.width, cursor->header.height);

Cursor header defined as:

  typedef struct SPICE_ATTR_PACKED QXLCursorHeader {
      uint64_t unique;
      uint16_t type;
      uint16_t width;
      uint16_t height;
      uint16_t hot_spot_x;
      uint16_t hot_spot_y;
  } QXLCursorHeader;

So no negative values can be passed to cursor_alloc()

  hw/display/vmware_vga.c:    qc = cursor_alloc(c->width, c->height);

Where 'c' is defined as:

  struct vmsvga_cursor_definition_s {
      uint32_t width;
      uint32_t height;
      int id;
      uint32_t bpp;
      int hot_x;
      int hot_y;
      uint32_t mask[1024];
      uint32_t image[4096];
  };

and is also already bounds checked:

            if (cursor.width > 256
                || cursor.height > 256
                || cursor.bpp > 32
                || SVGA_BITMAP_SIZE(x, y) > ARRAY_SIZE(cursor.mask)
                || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
                    > ARRAY_SIZE(cursor.image)) {
                    goto badcmd;
            }
Quoted 2 lines
Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")

Given there is no possible codepath that can overflow, CVE-2023-1601 looks invalid to me. It should be clsoed as not-a-bug and these two Fixes lines removed.

Quoted 6 lines
---
 include/ui/console.h | 4 ++--
 ui/cursor.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Even though it isn't fixing a bug, the change itself still makes sense, because there's no reason a negative width/height is ever appropriate. This protects us against accidentally introducing future bugs, so with the two CVE Fixes lines removed:

Quoted 38 lines
include/ui/console.h +2 -2
index 2a8fab091f..92a4d90a1b 100644
@@ -144,13 +144,13 @@ typedef struct QemuUIInfo {
 
 /* cursor data format is 32bit RGBA */
 typedef struct QEMUCursor {
-    int                 width, height;
+    uint32_t            width, height;
     int                 hot_x, hot_y;
     int                 refcount;
     uint32_t            data[];
 } QEMUCursor;
 
-QEMUCursor *cursor_alloc(int width, int height);
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
 QEMUCursor *cursor_ref(QEMUCursor *c);
 void cursor_unref(QEMUCursor *c);
 QEMUCursor *cursor_builtin_hidden(void);
ui/cursor.c +1 -1
index 6fe67990e2..b5fcb64839 100644
@@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
     return cursor_parse_xpm(cursor_left_ptr_xpm);
 }
 
-QEMUCursor *cursor_alloc(int width, int height)
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;
     size_t datasize = width * height * sizeof(uint32_t);
Signature · 1 line
2.40.1

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#13 lore raw
Quoted 60 lines · Daniel P. Berrangé
Quoted 6 lines · Mauro Matteo Cascella

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

I concur with Marc-Andre that there is no code path that can actually trigger an overflow:

  hw/display/ati.c:        s->cursor = cursor_alloc(64, 64);
  hw/display/vhost-user-gpu.c:            s->current_cursor = cursor_alloc(64, 64);
  hw/display/virtio-gpu.c:            s->current_cursor = cursor_alloc(64, 64);

Not exploitable as fixed size

  hw/display/qxl-render.c:    c = cursor_alloc(cursor->header.width, cursor->header.height);

Cursor header defined as:

  typedef struct SPICE_ATTR_PACKED QXLCursorHeader {
      uint64_t unique;
      uint16_t type;
      uint16_t width;
      uint16_t height;
      uint16_t hot_spot_x;
      uint16_t hot_spot_y;
  } QXLCursorHeader;

So no negative values can be passed to cursor_alloc()

  hw/display/vmware_vga.c:    qc = cursor_alloc(c->width, c->height);

Where 'c' is defined as:

  struct vmsvga_cursor_definition_s {
      uint32_t width;
      uint32_t height;
      int id;
      uint32_t bpp;
      int hot_x;
      int hot_y;
      uint32_t mask[1024];
      uint32_t image[4096];
  };

and is also already bounds checked:

            if (cursor.width > 256
                || cursor.height > 256
                || cursor.bpp > 32
                || SVGA_BITMAP_SIZE(x, y) > ARRAY_SIZE(cursor.mask)
                || SVGA_PIXMAP_SIZE(x, y, cursor.bpp)
                    > ARRAY_SIZE(cursor.image)) {
                    goto badcmd;
            }
Quoted 6 lines
Quoted 2 lines
Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")

Given there is no possible codepath that can overflow, CVE-2023-1601 looks invalid to me. It should be clsoed as not-a-bug and these two Fixes lines removed.

I think you can tweak the original PoC [1] to trigger this bug. Setting width/height to 0x80000000 (versus 0x8000) should do the trick. You should be able to overflow datasize while bypassing the sanity check (width > 512 || height > 512) as width/height are signed prior to this patch. I haven't tested it, though.

[1] https://github.com/star-sg/CVE/blob/master/CVE-2021-4206/poc.c
[2] https://starlabs.sg/advisories/21/21-4206/

Quoted 57 lines
Quoted 6 lines
---
 include/ui/console.h | 4 ++--
 ui/cursor.c          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Even though it isn't fixing a bug, the change itself still makes sense, because there's no reason a negative width/height is ever appropriate. This protects us against accidentally introducing future bugs, so with the two CVE Fixes lines removed:

Quoted 35 lines
include/ui/console.h +2 -2
index 2a8fab091f..92a4d90a1b 100644
@@ -144,13 +144,13 @@ typedef struct QemuUIInfo {
 
 /* cursor data format is 32bit RGBA */
 typedef struct QEMUCursor {
-    int                 width, height;
+    uint32_t            width, height;
     int                 hot_x, hot_y;
     int                 refcount;
     uint32_t            data[];
 } QEMUCursor;
 
-QEMUCursor *cursor_alloc(int width, int height);
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height);
 QEMUCursor *cursor_ref(QEMUCursor *c);
 void cursor_unref(QEMUCursor *c);
 QEMUCursor *cursor_builtin_hidden(void);
ui/cursor.c +1 -2
index 6fe67990e2..b5fcb64839 100644
@@ -90,7 +90,7 @@ QEMUCursor *cursor_builtin_left_ptr(void)
     return cursor_parse_xpm(cursor_left_ptr_xpm);
 }
 
-QEMUCursor *cursor_alloc(int width, int height)
+QEMUCursor *cursor_alloc(uint32_t width, uint32_t height)
 {
     QEMUCursor *c;
     size_t datasize = width * height * sizeof(uint32_t);
--
Quoted 3 lines

2.40.1

With regards,
Daniel
--

Quoted 4 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

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

#14 lore raw
Quoted 42 lines · Mauro Matteo Cascella
Quoted 34 lines · Daniel P. Berrangé
Quoted 6 lines · Mauro Matteo Cascella

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

I concur with Marc-Andre that there is no code path that can actually trigger an overflow:

  hw/display/ati.c:        s->cursor = cursor_alloc(64, 64);
  hw/display/vhost-user-gpu.c:            s->current_cursor = cursor_alloc(64, 64);
  hw/display/virtio-gpu.c:            s->current_cursor = cursor_alloc(64, 64);

Not exploitable as fixed size

  hw/display/qxl-render.c:    c = cursor_alloc(cursor->header.width, cursor->header.height);

Cursor header defined as:

  typedef struct SPICE_ATTR_PACKED QXLCursorHeader {
      uint64_t unique;
      uint16_t type;
      uint16_t width;
      uint16_t height;
      uint16_t hot_spot_x;
      uint16_t hot_spot_y;
  } QXLCursorHeader;

So no negative values can be passed to cursor_alloc()

Quoted 6 lines
Quoted 2 lines
Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")

Given there is no possible codepath that can overflow, CVE-2023-1601 looks invalid to me. It should be clsoed as not-a-bug and these two Fixes lines removed.

Quoted 5 lines

I think you can tweak the original PoC [1] to trigger this bug. Setting width/height to 0x80000000 (versus 0x8000) should do the trick. You should be able to overflow datasize while bypassing the sanity check (width > 512 || height > 512) as width/height are signed prior to this patch. I haven't tested it, though.

The QXLCursorHeader width/height fields are uint16_t, so 0x80000000 will get truncated. No matter what value the guest sets, when we interpret this in qxl_cursor when calling cursor_alloc, the value will be in the range 0-65535, as that's the bounds of uint16_t.

We'll pass this unsigned value to cursor_alloc() which converts from uint16_t, to (signed) int. 'int' is larger than uint16_t, so the result will still be positive in the range 0-65535, and so the sanity check > 512 will fire and protect us.

I still see no bug, let alone a CVE.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#15 lore raw
Quoted 55 lines · Daniel P. Berrangé
Quoted 42 lines · Mauro Matteo Cascella
Quoted 34 lines · Daniel P. Berrangé
Quoted 6 lines · Mauro Matteo Cascella

The cursor_alloc function still accepts a signed integer for both the cursor width and height. A specially crafted negative width/height could make datasize wrap around and cause the next allocation to be 0, potentially leading to a heap buffer overflow. Modify QEMUCursor struct and cursor_alloc prototype to accept unsigned ints.

I concur with Marc-Andre that there is no code path that can actually trigger an overflow:

  hw/display/ati.c:        s->cursor = cursor_alloc(64, 64);
  hw/display/vhost-user-gpu.c:            s->current_cursor = cursor_alloc(64, 64);
  hw/display/virtio-gpu.c:            s->current_cursor = cursor_alloc(64, 64);

Not exploitable as fixed size

  hw/display/qxl-render.c:    c = cursor_alloc(cursor->header.width, cursor->header.height);

Cursor header defined as:

  typedef struct SPICE_ATTR_PACKED QXLCursorHeader {
      uint64_t unique;
      uint16_t type;
      uint16_t width;
      uint16_t height;
      uint16_t hot_spot_x;
      uint16_t hot_spot_y;
  } QXLCursorHeader;

So no negative values can be passed to cursor_alloc()

Quoted 6 lines
Quoted 2 lines
Fixes CVE-2023-1601
Fixes fa892e9a ("ui/cursor: fix integer overflow in cursor_alloc (CVE-2021-4206)")

Given there is no possible codepath that can overflow, CVE-2023-1601 looks invalid to me. It should be clsoed as not-a-bug and these two Fixes lines removed.

Quoted 5 lines

I think you can tweak the original PoC [1] to trigger this bug. Setting width/height to 0x80000000 (versus 0x8000) should do the trick. You should be able to overflow datasize while bypassing the sanity check (width > 512 || height > 512) as width/height are signed prior to this patch. I haven't tested it, though.

The QXLCursorHeader width/height fields are uint16_t, so 0x80000000 will get truncated. No matter what value the guest sets, when we interpret this in qxl_cursor when calling cursor_alloc, the value will be in the range 0-65535, as that's the bounds of uint16_t.

Quoted 4 lines

We'll pass this unsigned value to cursor_alloc() which converts from uint16_t, to (signed) int. 'int' is larger than uint16_t, so the result will still be positive in the range 0-65535, and so the sanity check > 512 will fire and protect us.

Oh, you are right! Then yes, feel free to drop the two 'Fixes' lines. This is more of a hardening bug than a real security issue. I'll reject the newly assigned CVE.

Thanks,

Quoted 10 lines

I still see no bug, let alone a CVE.

With regards,
Daniel
--

|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

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