Skip to contentSkip to conversation

hw/display/vmware_vga: Don't allow guest to trigger long running loop in host

PATCH Applied Thomas Huth 4 participants 8 messages Jul 23 – Jul 24

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/display/vmware_vga: Don't allow guest to trigger long running loop in host

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

            x = vmsvga_fifo_read(s);
            y = vmsvga_fifo_read(s);
            args = x * y;
            goto badcmd;
            ...
badcmd:
            len -= args;
            if (len < 0) {
                goto rewind;
            }
            while (args--) {
                vmsvga_fifo_read(s);
            }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

 hw/display/vmware_vga.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/display/vmware_vga.c +5 -1
index f6f9edfd1d9..567806f0e47 100644
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
             vmsvga_fifo_read(s);
             x = vmsvga_fifo_read(s);
             y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {
+                goto rewind;
+            }
             args = x * y;
             goto badcmd;
         case SVGA_CMD_RECT_ROP_FILL:
@@ -776,7 +780,7 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
             if (len < 0) {
                 goto rewind;
             }
-            while (args--) {
+            while (args-- > 0) {
                 vmsvga_fifo_read(s);
             }
             printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
Signature · 1 line
2.55.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 43 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

             x = vmsvga_fifo_read(s);
             y = vmsvga_fifo_read(s);
             args = x * y;
             goto badcmd;
             ...
badcmd:
             len -= args;
             if (len < 0) {
                 goto rewind;
             }
             while (args--) {
                 vmsvga_fifo_read(s);
             }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

  hw/display/vmware_vga.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
hw/display/vmware_vga.c +0 -0
index f6f9edfd1d9..567806f0e47 100644

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 741

Quoted 5 lines
hw/display/vmware_vga.c +2 -0
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
              vmsvga_fifo_read(s);
              x = vmsvga_fifo_read(s);
              y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

vmsvga_fifo_read() returns unsigned... otherwise:

Quoted 14 lines
hw/display/vmware_vga.c +3 -1
+                goto rewind;
+            }
              args = x * y;
              goto badcmd;
          case SVGA_CMD_RECT_ROP_FILL:
@@ -776,7 +780,7 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
              if (len < 0) {
                  goto rewind;
              }
-            while (args--) {
+            while (args-- > 0) {
                  vmsvga_fifo_read(s);
              }
              printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
#2 lore raw
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

             x = vmsvga_fifo_read(s);
             y = vmsvga_fifo_read(s);
             args = x * y;
             goto badcmd;
             ...
badcmd:
             len -= args;
             if (len < 0) {
                 goto rewind;
             }
             while (args--) {
                 vmsvga_fifo_read(s);
             }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

  hw/display/vmware_vga.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 741

Quoted 10 lines
hw/display/vmware_vga.c +2 -0
index f6f9edfd1d9..567806f0e47 100644
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
              vmsvga_fifo_read(s);
              x = vmsvga_fifo_read(s);
              y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

vmsvga_fifo_read() returns unsigned... otherwise:

But x & y are declared 'int', so at least on 32-bit builds a large uint32_t value would wrap and become negative. Although we dropped support for 32-bit platforms, IMHO it would be better to use uint32_t for 'x' and 'y' too rather than assuming the 'int' value won't be negative.

Quoted 18 lines
Quoted 6 lines
hw/display/vmware_vga.c +2 -0
+                goto rewind;
+            }
              args = x * y;
              goto badcmd;
          case SVGA_CMD_RECT_ROP_FILL:

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 786

Quoted 8 lines
hw/display/vmware_vga.c +1 -1
@@ -776,7 +780,7 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
              if (len < 0) {
                  goto rewind;
              }
-            while (args--) {
+            while (args-- > 0) {
                  vmsvga_fifo_read(s);
              }
              printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#3 lore raw
Quoted 53 lines · Daniel P. Berrangé
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

              x = vmsvga_fifo_read(s);
              y = vmsvga_fifo_read(s);
              args = x * y;
              goto badcmd;
              ...
badcmd:
              len -= args;
              if (len < 0) {
                  goto rewind;
              }
              while (args--) {
                  vmsvga_fifo_read(s);
              }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

   hw/display/vmware_vga.c | 6 +++++-
   1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 741

Quoted 10 lines
hw/display/vmware_vga.c +2 -0
index f6f9edfd1d9..567806f0e47 100644
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
               vmsvga_fifo_read(s);
               x = vmsvga_fifo_read(s);
               y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

vmsvga_fifo_read() returns unsigned... otherwise:

Quoted 5 lines

But x & y are declared 'int', so at least on 32-bit builds a large uint32_t value would wrap and become negative. Although we dropped support for 32-bit platforms, IMHO it would be better to use uint32_t for 'x' and 'y' too rather than assuming the 'int' value won't be negative.

x and y are used as signed int all over the place here ... so reworking that goes way beyond fixing this problem. Could we please get this patch merged first for 11.1, and if someone then still feels like reworking the code, this could be done for 11.2 ?

Thomas

#4 lore raw
Quoted 59 lines · Thomas Huth
Quoted 53 lines · Daniel P. Berrangé
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

              x = vmsvga_fifo_read(s);
              y = vmsvga_fifo_read(s);
              args = x * y;
              goto badcmd;
              ...
badcmd:
              len -= args;
              if (len < 0) {
                  goto rewind;
              }
              while (args--) {
                  vmsvga_fifo_read(s);
              }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

   hw/display/vmware_vga.c | 6 +++++-
   1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 741

Quoted 10 lines
hw/display/vmware_vga.c +2 -0
index f6f9edfd1d9..567806f0e47 100644
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
               vmsvga_fifo_read(s);
               x = vmsvga_fifo_read(s);
               y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

vmsvga_fifo_read() returns unsigned... otherwise:

Quoted 5 lines

But x & y are declared 'int', so at least on 32-bit builds a large uint32_t value would wrap and become negative. Although we dropped support for 32-bit platforms, IMHO it would be better to use uint32_t for 'x' and 'y' too rather than assuming the 'int' value won't be negative.

Quoted 4 lines

x and y are used as signed int all over the place here ... so reworking that goes way beyond fixing this problem. Could we please get this patch merged first for 11.1, and if someone then still feels like reworking the code, this could be done for 11.2 ?

Ok, then the < 0 checks should be kept.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#5 lore raw
In reply to Thomas Huth
Quoted 55 lines · Thomas Huth
Quoted 54 lines · Daniel P. Berrangé
Quoted 52 lines · Philippe Mathieu-Daudé
Quoted 43 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

              x = vmsvga_fifo_read(s);
              y = vmsvga_fifo_read(s);
              args = x * y;
              goto badcmd;
              ...
badcmd:
              len -= args;
              if (len < 0) {
                  goto rewind;
              }
              while (args--) {
                  vmsvga_fifo_read(s);
              }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
   v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

   hw/display/vmware_vga.c | 6 +++++-

   1 file changed, 5 insertions(+), 1 deletion(-)
hw/display/vmware_vga.c +0 -0
index f6f9edfd1d9..567806f0e47 100644

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct · line 742

Quoted 6 lines
hw/display/vmware_vga.c +2 -0
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct 
 vmsvga_state_s *s)
                vmsvga_fifo_read(s);
                x = vmsvga_fifo_read(s);
                y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

vmsvga_fifo_read() returns unsigned... otherwise:

Quoted 6 lines
Quoted 5 lines

But x & y  are declared 'int', so at least on 32-bit builds a large uint32_t value would wrap and become negative. Although we dropped support for 32-bit platforms, IMHO it would be better to use uint32_t for 'x' and 'y' too rather than assuming the 'int' value won't be negative.

x and y are used as signed int all over the place here ...

Ah I missed that, sorry for the troubles.

Quoted 5 lines

so reworking that goes way beyond fixing this problem. Could we please get this patch merged first for 11.1, and if someone then still feels like reworking the code, this could be done for 11.2 ?

 Thomas

#6 lore raw
In reply to Thomas Huth
Quoted 43 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

             x = vmsvga_fifo_read(s);
             y = vmsvga_fifo_read(s);
             args = x * y;
             goto badcmd;
             ...
badcmd:
             len -= args;
             if (len < 0) {
                 goto rewind;
             }
             while (args--) {
                 vmsvga_fifo_read(s);
             }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

  hw/display/vmware_vga.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
hw/display/vmware_vga.c +0 -0
index f6f9edfd1d9..567806f0e47 100644

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 741

Quoted 5 lines
hw/display/vmware_vga.c +2 -0
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
              vmsvga_fifo_read(s);
              x = vmsvga_fifo_read(s);
              y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

Applied to the trivial-patches tree, dropping <0 conditions here.

Thanks,

/mjt

Quoted 14 lines
hw/display/vmware_vga.c +3 -1
+                goto rewind;
+            }
              args = x * y;
              goto badcmd;
          case SVGA_CMD_RECT_ROP_FILL:
@@ -776,7 +780,7 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
              if (len < 0) {
                  goto rewind;
              }
-            while (args--) {
+            while (args-- > 0) {
                  vmsvga_fifo_read(s);
              }
              printf("%s: Unknown command 0x%02x in SVGA command FIFO\n",
#7 lore raw
Quoted 39 lines · Michael Tokarev
Quoted 38 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The code in the SVGA_CMD_DEFINE_ALPHA_CURSOR handler in vmsvga_fifo_run() basically does:

             x = vmsvga_fifo_read(s);
             y = vmsvga_fifo_read(s);
             args = x * y;
             goto badcmd;
             ...
badcmd:
             len -= args;
             if (len < 0) {
                 goto rewind;
             }
             while (args--) {
                 vmsvga_fifo_read(s);
             }

Thus by supplying huge values for x and y that overflow the result of the multiplication, the guest can trigger a long-running loop here that burns the host's CPU cycles.

Add some sanity checks so that this cannot happen anymore.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3782

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4026

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4076

Reported-by Sunday Jiang

---
  v2: Use SVGA_MAX_WIDTH and SVGA_MAX_HEIGHT instead of an arbitrary value

  hw/display/vmware_vga.c | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/display/vmware_vga.c static void vmsvga_fifo_run(struct vmsvga_state_s *s) · line 741

Quoted 10 lines
hw/display/vmware_vga.c +2 -0
index f6f9edfd1d9..567806f0e47 100644
@@ -737,6 +737,10 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)
               vmsvga_fifo_read(s);
               x = vmsvga_fifo_read(s);
               y = vmsvga_fifo_read(s);
+            if (x < 0 || x >= SVGA_MAX_WIDTH ||
+                y < 0 || y >= SVGA_MAX_HEIGHT) {

Applied to the trivial-patches tree, dropping <0 conditions here.

Please don't drop the "< 0" ! x and y are signed variables, so we should better check this here.

Thanks,
Thomas