Skip to contentSkip to conversation

hw/display/tcx: Fix out-of-bounds access in tcx_blit_writel

PATCH In review Zheyu Ma 4 participants 6 messages Jun 30, 2024 – Jul 11, 2024

lore
Share & tools
lore

Zheyu Ma

author
#0 lore raw

PATCH 1/1 hw/display/tcx: Fix out-of-bounds access in tcx_blit_writel

This patch addresses a potential out-of-bounds memory access issue in the tcx_blit_writel function. It adds bounds checking to ensure that memory accesses do not exceed the allocated VRAM size. If an out-of-bounds access is detected, an error is logged using qemu_log_mask.

ASAN log:
==2960379==ERROR: AddressSanitizer: SEGV on unknown address 0x7f524752fd01 (pc 0x7f525c2c4881 bp 0x7ffdaf87bfd0 sp 0x7ffdaf87b788 T0) ==2960379==The signal is caused by a READ memory access.

    #0 0x7f525c2c4881 in memcpy string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:222
    #1 0x55aa782bd5b1 in __asan_memcpy llvm/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #2 0x55aa7854dedd in tcx_blit_writel hw/display/tcx.c:590:13

Reproducer:
cat << EOF | qemu-system-sparc -display none \
-machine accel=qtest, -m 512M -machine LX -m 256 -qtest stdio writel 0x562e98c4 0x3d92fd01
EOF

---
 hw/display/tcx.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/display/tcx.c +9 -0
index 99507e7638..af43bea7f2 100644
@@ -33,6 +33,7 @@
 #include "migration/vmstate.h"
 #include "qemu/error-report.h"
 #include "qemu/module.h"
+#include "qemu/log.h"
 #include "qom/object.h"
 
 #define TCX_ROM_FILE "QEMU,tcx.bin"
@@ -577,6 +578,14 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,
         addr = (addr >> 3) & 0xfffff;
         adsr = val & 0xffffff;
         len = ((val >> 24) & 0x1f) + 1;
+
+        if (addr + len > s->vram_size || adsr + len > s->vram_size) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: VRAM access out of bounds. addr: 0x%lx, adsr: 0x%x, len: %u\n",
+                          __func__, addr, adsr, len);
+            return;
+        }
+
         if (adsr == 0xffffff) {
             memset(&s->vram[addr], s->tmpblit, len);
             if (s->depth == 24) {
Signature · 1 line
2.34.1
#1 lore raw

Hi, zheyu

Quoted 48 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Zheyu Ma
Sent: Sunday, June 30, 2024 9:04 PM
To: Mark Cave-Ayland <[email protected]>
Cc: Zheyu Ma <[email protected]>; [email protected]
Subject: [PATCH] hw/display/tcx: Fix out-of-bounds access in tcx_blit_writel

This patch addresses a potential out-of-bounds memory access issue in the tcx_blit_writel function. It adds bounds checking to ensure that memory accesses do not exceed the allocated VRAM size. If an out-of-bounds access is detected, an error is logged using qemu_log_mask.

ASAN log:
==2960379==ERROR: AddressSanitizer: SEGV on unknown address 0x7f524752fd01 (pc 0x7f525c2c4881 bp 0x7ffdaf87bfd0 sp 0x7ffdaf87b788 T0) ==2960379==The signal is caused by a READ memory access.

    #0 0x7f525c2c4881 in memcpy
string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:222
    #1 0x55aa782bd5b1 in __asan_memcpy
llvm/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #2 0x55aa7854dedd in tcx_blit_writel hw/display/tcx.c:590:13

Reproducer:
cat << EOF | qemu-system-sparc -display none \
-machine accel=qtest, -m 512M -machine LX -m 256 -qtest stdio writel 0x562e98c4 0x3d92fd01
EOF

---
 hw/display/tcx.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/display/tcx.c +1 -0
index 99507e7638..af43bea7f2 100644
@@ -33,6 +33,7 @@
 #include "migration/vmstate.h"
 #include "qemu/error-report.h"
 #include "qemu/module.h"
+#include "qemu/log.h"
 #include "qom/object.h"
 
 #define TCX_ROM_FILE "QEMU,tcx.bin"

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr addr, · line 582

Quoted 5 lines
hw/display/tcx.c +2 -0
@@ -577,6 +578,14 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,
         addr = (addr >> 3) & 0xfffff;
         adsr = val & 0xffffff;
         len = ((val >> 24) & 0x1f) + 1;
+
+        if (addr + len > s->vram_size || adsr + len > s->vram_size) {

if adsr == 0xffffff, this condition may be always true, thus the branch “if (adsr == 0xffffff) {” will be never reached.

Quoted 13 lines
hw/display/tcx.c +6 -0
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: VRAM access out of bounds. addr: 0x%lx, adsr:
 0x%x, len: %u\n",
+                          __func__, addr, adsr, len);
+            return;
+        }
+
         if (adsr == 0xffffff) {
             memset(&s->vram[addr], s->tmpblit, len);
             if (s->depth == 24) {

--
2.34.1

author
#2 lore raw

Hi Xingtao,

Quoted 54 lines · Xingtao Yao (Fujitsu)

Hi, zheyu

Quoted 3 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of

Zheyu

Quoted 5 lines

Ma
Sent: Sunday, June 30, 2024 9:04 PM
To: Mark Cave-Ayland <[email protected]>
Cc: Zheyu Ma <[email protected]>; [email protected]
Subject: [PATCH] hw/display/tcx: Fix out-of-bounds access in

tcx_blit_writel

Quoted 4 lines

This patch addresses a potential out-of-bounds memory access issue in the tcx_blit_writel function. It adds bounds checking to ensure that memory accesses do not exceed the allocated VRAM size. If an out-of-bounds

access

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr

Quoted 36 lines

is detected, an error is logged using qemu_log_mask.

ASAN log:
==2960379==ERROR: AddressSanitizer: SEGV on unknown address 0x7f524752fd01 (pc 0x7f525c2c4881 bp 0x7ffdaf87bfd0 sp 0x7ffdaf87b788 T0) ==2960379==The signal is caused by a READ memory access.

    #0 0x7f525c2c4881 in memcpy
string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:222
    #1 0x55aa782bd5b1 in __asan_memcpy
llvm/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
    #2 0x55aa7854dedd in tcx_blit_writel hw/display/tcx.c:590:13

Reproducer:
cat << EOF | qemu-system-sparc -display none \
-machine accel=qtest, -m 512M -machine LX -m 256 -qtest stdio writel 0x562e98c4 0x3d92fd01
EOF

---
 hw/display/tcx.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/display/tcx.c +1 -0
index 99507e7638..af43bea7f2 100644
@@ -33,6 +33,7 @@
 #include "migration/vmstate.h"
 #include "qemu/error-report.h"
 #include "qemu/module.h"
+#include "qemu/log.h"
 #include "qom/object.h"
 
 #define TCX_ROM_FILE "QEMU,tcx.bin"
@@ -577,6 +578,14 @@ static void tcx_blit_writel(void *opaque, hwaddr

addr,

Quoted 9 lines

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr addr, · line 582

Quoted 5 lines
hw/display/tcx.c +2 -0
         addr = (addr >> 3) & 0xfffff;
         adsr = val & 0xffffff;
         len = ((val >> 24) & 0x1f) + 1;
+
+        if (addr + len > s->vram_size || adsr + len > s->vram_size) {

if adsr == 0xffffff, this condition may be always true, thus the branch “if (adsr == 0xffffff) {” will be never
reached.

You are right, I misunderstood the condition :(

Thanks,
Zheyu

Quoted 17 lines

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr · line 584

Quoted 3 lines
hw/display/tcx.c +2 -0
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: VRAM access out of bounds. addr: 0x%lx,

adsr:

Quoted 11 lines
hw/display/tcx.c +4 -0
 0x%x, len: %u\n",
+                          __func__, addr, adsr, len);
+            return;
+        }
+
         if (adsr == 0xffffff) {
             memset(&s->vram[addr], s->tmpblit, len);
             if (s->depth == 24) {

--
2.34.1

#3 lore raw
In reply to Zheyu Ma
Quoted 40 lines · Zheyu Ma

This patch addresses a potential out-of-bounds memory access issue in the tcx_blit_writel function. It adds bounds checking to ensure that memory accesses do not exceed the allocated VRAM size. If an out-of-bounds access is detected, an error is logged using qemu_log_mask.

ASAN log:
==2960379==ERROR: AddressSanitizer: SEGV on unknown address 0x7f524752fd01 (pc 0x7f525c2c4881 bp 0x7ffdaf87bfd0 sp 0x7ffdaf87b788 T0) ==2960379==The signal is caused by a READ memory access.

     #0 0x7f525c2c4881 in memcpy string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:222
     #1 0x55aa782bd5b1 in __asan_memcpy llvm/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3
     #2 0x55aa7854dedd in tcx_blit_writel hw/display/tcx.c:590:13

Reproducer:
cat << EOF | qemu-system-sparc -display none \
-machine accel=qtest, -m 512M -machine LX -m 256 -qtest stdio writel 0x562e98c4 0x3d92fd01
EOF

---
  hw/display/tcx.c | 9 +++++++++
  1 file changed, 9 insertions(+)
hw/display/tcx.c +2 -0
index 99507e7638..af43bea7f2 100644
@@ -33,6 +33,7 @@
  #include "migration/vmstate.h"
  #include "qemu/error-report.h"
  #include "qemu/module.h"
+#include "qemu/log.h"
  #include "qom/object.h"
 
  #define TCX_ROM_FILE "QEMU,tcx.bin"
@@ -577,6 +578,14 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,
          addr = (addr >> 3) & 0xfffff;
          adsr = val & 0xffffff;
          len = ((val >> 24) & 0x1f) + 1;
+

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr addr, · line 591

Quoted 10 lines
hw/display/tcx.c +7 -0
+        if (addr + len > s->vram_size || adsr + len > s->vram_size) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: VRAM access out of bounds. addr: 0x%lx, adsr: 0x%x, len: %u\n",
+                          __func__, addr, adsr, len);
+            return;
+        }
+
          if (adsr == 0xffffff) {
              memset(&s->vram[addr], s->tmpblit, len);
              if (s->depth == 24) {

What would happen if the source data plus length goes beyond the end of the framebuffer but the destination lies completely within it? Presumably the length of the data copy should be restricted to the length of the source data rather than the entire copy being ignored?

ATB,

Mark.

author
#4 lore raw

Hi Mark,

Quoted 59 lines · Mark Cave-Ayland
Quoted 3 lines · Zheyu Ma

This patch addresses a potential out-of-bounds memory access issue in the tcx_blit_writel function. It adds bounds checking to ensure that memory accesses do not exceed the allocated VRAM size. If an out-of-bounds

access

Quoted 4 lines

is detected, an error is logged using qemu_log_mask.

ASAN log:
==2960379==ERROR: AddressSanitizer: SEGV on unknown address

0x7f524752fd01 (pc 0x7f525c2c4881 bp 0x7ffdaf87bfd0 sp 0x7ffdaf87b788 T0)

Quoted 2 lines

==2960379==The signal is caused by a READ memory access.

     #0 0x7f525c2c4881 in memcpy

string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:222

Quoted 1 line
     #1 0x55aa782bd5b1 in __asan_memcpy

llvm/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr

Quoted 26 lines
     #2 0x55aa7854dedd in tcx_blit_writel hw/display/tcx.c:590:13

Reproducer:
cat << EOF | qemu-system-sparc -display none \
-machine accel=qtest, -m 512M -machine LX -m 256 -qtest stdio writel 0x562e98c4 0x3d92fd01
EOF

---
  hw/display/tcx.c | 9 +++++++++
  1 file changed, 9 insertions(+)
hw/display/tcx.c +1 -0
index 99507e7638..af43bea7f2 100644
@@ -33,6 +33,7 @@
  #include "migration/vmstate.h"
  #include "qemu/error-report.h"
  #include "qemu/module.h"
+#include "qemu/log.h"
  #include "qom/object.h"
 
  #define TCX_ROM_FILE "QEMU,tcx.bin"
@@ -577,6 +578,14 @@ static void tcx_blit_writel(void *opaque, hwaddr

addr,

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr addr,

Quoted 7 lines
hw/display/tcx.c +4 -0
          addr = (addr >> 3) & 0xfffff;
          adsr = val & 0xffffff;
          len = ((val >> 24) & 0x1f) + 1;
+
+        if (addr + len > s->vram_size || adsr + len > s->vram_size) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: VRAM access out of bounds. addr: 0x%lx,

adsr: 0x%x, len: %u\n",

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr addr, · line 591

Quoted 7 lines
hw/display/tcx.c +4 -0
+                          __func__, addr, adsr, len);
+            return;
+        }
+
          if (adsr == 0xffffff) {
              memset(&s->vram[addr], s->tmpblit, len);
              if (s->depth == 24) {
Quoted 9 lines

What would happen if the source data plus length goes beyond the end of the
framebuffer but the destination lies completely within it? Presumably the length of
the data copy should be restricted to the length of the source data rather than the
entire copy being ignored?

Thanks for your useful tips! However, I'm unfamiliar with the tcx device and cannot find a specification/datasheet. I apologize for not proposing a proper fix.

Regards,
Zheyu

#5 lore raw
Quoted 5 lines · Zheyu Ma

Hi Mark,

Quoted 1 line · Mark Cave-Ayland
Quoted 8 lines
Quoted 6 lines · Zheyu Ma

What would happen if the source data plus length goes beyond the end of the framebuffer but the destination lies completely within it? Presumably the length of the data copy should be restricted to the length of the source data rather than the entire copy being ignored?

Thanks for your useful tips! However, I'm unfamiliar with the tcx device and cannot find a specification/datasheet. I apologize for not proposing a proper fix.

Yeah, I couldn't find a datasheet for this device either. In the absence of any clear information, I think what we usually do in QEMU is take a plausible guess at what it might do and/or implement something that's straightforward for us to implement. Chances are good that real guest code never exercises the weird corners of the device behaviour anyway. Possible options include:
* just ignore the blit attempt entirely
* clamp source and destination addr/length and do the parts that do fall within the vram
* treat address calculations as always wrapping around within the vram (so if you address off the top of it you end up reading from the bottom of it)

I would suggest we just pick one, implement it (with a comment saying we don't have a spec so we're guessing about the behaviour in this case), and then test that whatever guest code we have (e.g. the bios, linux, some BSD) doesn't misbehave with the patch applied.

By the way, doesn't this problem also affect the other TCX accelerator functions? Most obviously, tcg_rblit_writel() is basically the same structure for computing address and length; but also e.g. tcx_stip_writel() and tcx_rstip_writel() don't do bounds checking before accessing s->vram[addr + i].

thanks
-- PMM