Skip to contentSkip to conversation

hw/display/xlnx_dp: fix an out-of-bounds read in xlnx_dp_read

PATCH Superseded Qiang Liu 3 participants 5 messages Jul 13, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qiang Liu

author
#0 lore raw

PATCH 1/1 hw/display/xlnx_dp: fix an out-of-bounds read in xlnx_dp_read

xlnx_dp_read allows an out-of-bounds read at its default branch because of an improper index.

According to
https://www.xilinx.com/html_docs/registers/ug1087/ug1087-zynq-ultrascale-registers.html
(DP Module), registers 0x3A4/0x3A4/0x3AC are allowed.

DP_INT_MASK 0x000003A4 32 mixed 0xFFFFF03F Interrupt Mask Register for intrN.
DP_INT_EN 0x000003A8 32 mixed 0x00000000 Interrupt Enable Register.
DP_INT_DS 0x000003AC 32 mixed 0x00000000 Interrupt Disable Register.

In xlnx_dp_write, when the offset is 0x3A8 and 0x3AC, the virtual device will write s->core_registers[0x3A4

Quoted 3 lines
Quoted 3 lines

2]. That is to say, the maxize of s->core_registers could be ((0x3A4
2) + 1). However, the current size of s->core_registers is (0x3AF >> 2), that is ((0x3A4 >> 2) + 2), which is out of the range.

In xlxn_dp_read, the access to offset 0x3A8 or 0x3AC will be directed to the offset 0x3A8 (incorrect functionality) or 0x3AC (out-of-bounds read) rather than 0x3A4.

This patch adjusts the size of s->core_registers and enforces the read access to offset 0x3A* and 0x3AC to 0x3A4. BTW, because the size of this MMIO region is 0x3AF, this patch also removes the assertion in xlnx_dp_write.

Fixes 58ac482a66de ("introduce xlnx-dp")
---
 hw/display/xlnx_dp.c         | 7 ++++---
 include/hw/display/xlnx_dp.h | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
hw/display/xlnx_dp.c +4 -3
index 7bcbb13..8903181 100644
@@ -713,8 +713,10 @@ static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size)
         ret = 0;
         break;
     default:
-        assert(offset <= (0x3AC >> 2));
-        ret = s->core_registers[offset];
+        if (offset == (0x3A8 >> 2) || offset == (0x3AC >> 2))
+            ret = s->core_registers[DP_INT_MASK];
+        else 
+            ret = s->core_registers[offset];
         break;
     }
 
@@ -876,7 +878,6 @@ static void xlnx_dp_write(void *opaque, hwaddr offset, uint64_t value,
         xlnx_dp_update_irq(s);
         break;
     default:
-        assert(offset <= (0x504C >> 2));
         s->core_registers[offset] = value;
         break;
     }
include/hw/display/xlnx_dp.h +1 -1
index e85e428..99a6d47 100644
@@ -39,7 +39,7 @@
 #define AUD_CHBUF_MAX_DEPTH                 (32 * KiB)
 #define MAX_QEMU_BUFFER_SIZE                (4 * KiB)
 
-#define DP_CORE_REG_ARRAY_SIZE              (0x3AF >> 2)
+#define DP_CORE_REG_ARRAY_SIZE              (0x3A8 >> 2)
 #define DP_AVBUF_REG_ARRAY_SIZE             (0x238 >> 2)
 #define DP_VBLEND_REG_ARRAY_SIZE            (0x1DF >> 2)
 #define DP_AUDIO_REG_ARRAY_SIZE             (0x50 >> 2)
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Qiang Liu

xlnx_dp_read allows an out-of-bounds read at its default branch because of an improper index.

According to
https://www.xilinx.com/html_docs/registers/ug1087/ug1087-zynq-ultrascale-registers.html
(DP Module), registers 0x3A4/0x3A4/0x3AC are allowed.

DP_INT_MASK     0x000003A4      32      mixed   0xFFFFF03F      Interrupt Mask Register for intrN.
DP_INT_EN       0x000003A8      32      mixed   0x00000000      Interrupt Enable Register.
DP_INT_DS       0x000003AC      32      mixed   0x00000000      Interrupt Disable Register.

In xlnx_dp_write, when the offset is 0x3A8 and 0x3AC, the virtual device will write s->core_registers[0x3A4

Quoted 3 lines
Quoted 3 lines

2]. That is to say, the maxize of s->core_registers could be ((0x3A4
2) + 1). However, the current size of s->core_registers is (0x3AF >> 2), that is ((0x3A4 >> 2) + 2), which is out of the range.

In xlxn_dp_read, the access to offset 0x3A8 or 0x3AC will be directed to the offset 0x3A8 (incorrect functionality) or 0x3AC (out-of-bounds read) rather than 0x3A4.

Quoted 7 lines

This patch adjusts the size of s->core_registers and enforces the read access to offset 0x3A* and 0x3AC to 0x3A4. BTW, because the size of this MMIO region is 0x3AF, this patch also removes the assertion in xlnx_dp_write.

Fixes 58ac482a66de ("introduce xlnx-dp")

Alistair

Quoted 47 lines
---
 hw/display/xlnx_dp.c         | 7 ++++---
 include/hw/display/xlnx_dp.h | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
hw/display/xlnx_dp.c +4 -3
index 7bcbb13..8903181 100644
@@ -713,8 +713,10 @@ static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size)
         ret = 0;
         break;
     default:
-        assert(offset <= (0x3AC >> 2));
-        ret = s->core_registers[offset];
+        if (offset == (0x3A8 >> 2) || offset == (0x3AC >> 2))
+            ret = s->core_registers[DP_INT_MASK];
+        else
+            ret = s->core_registers[offset];
         break;
     }
 
@@ -876,7 +878,6 @@ static void xlnx_dp_write(void *opaque, hwaddr offset, uint64_t value,
         xlnx_dp_update_irq(s);
         break;
     default:
-        assert(offset <= (0x504C >> 2));
         s->core_registers[offset] = value;
         break;
     }
include/hw/display/xlnx_dp.h +1 -2
index e85e428..99a6d47 100644
@@ -39,7 +39,7 @@
 #define AUD_CHBUF_MAX_DEPTH                 (32 * KiB)
 #define MAX_QEMU_BUFFER_SIZE                (4 * KiB)
 
-#define DP_CORE_REG_ARRAY_SIZE              (0x3AF >> 2)
+#define DP_CORE_REG_ARRAY_SIZE              (0x3A8 >> 2)
 #define DP_AVBUF_REG_ARRAY_SIZE             (0x238 >> 2)
 #define DP_VBLEND_REG_ARRAY_SIZE            (0x1DF >> 2)
 #define DP_AUDIO_REG_ARRAY_SIZE             (0x50 >> 2)
--

2.7.4

#2 lore raw
In reply to Qiang Liu
Quoted 25 lines · Qiang Liu

xlnx_dp_read allows an out-of-bounds read at its default branch because of an improper index.

According to
https://www.xilinx.com/html_docs/registers/ug1087/ug1087-zynq-ultrascale-registers.html
(DP Module), registers 0x3A4/0x3A4/0x3AC are allowed.

DP_INT_MASK 0x000003A4 32 mixed 0xFFFFF03F Interrupt Mask Register for intrN.
DP_INT_EN 0x000003A8 32 mixed 0x00000000 Interrupt Enable Register.
DP_INT_DS 0x000003AC 32 mixed 0x00000000 Interrupt Disable Register.

In xlnx_dp_write, when the offset is 0x3A8 and 0x3AC, the virtual device will write s->core_registers[0x3A4

Quoted 3 lines
Quoted 3 lines

2]. That is to say, the maxize of s->core_registers could be ((0x3A4
2) + 1). However, the current size of s->core_registers is (0x3AF >> 2), that is ((0x3A4 >> 2) + 2), which is out of the range.

In xlxn_dp_read, the access to offset 0x3A8 or 0x3AC will be directed to the offset 0x3A8 (incorrect functionality) or 0x3AC (out-of-bounds read) rather than 0x3A4.

This patch adjusts the size of s->core_registers and enforces the read access to offset 0x3A* and 0x3AC to 0x3A4. BTW, because the size of this MMIO region is 0x3AF, this patch also removes the assertion in xlnx_dp_write.

Quoted 6 lines
Fixes 58ac482a66de ("introduce xlnx-dp")
---
 hw/display/xlnx_dp.c         | 7 ++++---
 include/hw/display/xlnx_dp.h | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)

Can you provide a qtest reproducer please?

hw/display/xlnx_dp.c static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size) · line 718

Quoted 13 lines
hw/display/xlnx_dp.c +3 -2
index 7bcbb13..8903181 100644
@@ -713,8 +713,10 @@ static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size)
         ret = 0;
         break;
     default:
-        assert(offset <= (0x3AC >> 2));
-        ret = s->core_registers[offset];
+        if (offset == (0x3A8 >> 2) || offset == (0x3AC >> 2))
+            ret = s->core_registers[DP_INT_MASK];
+        else 

Invalid code style.

Quoted 17 lines
hw/display/xlnx_dp.c +1 -1
+            ret = s->core_registers[offset];
         break;
     }
 
@@ -876,7 +878,6 @@ static void xlnx_dp_write(void *opaque, hwaddr offset, uint64_t value,
         xlnx_dp_update_irq(s);
         break;
     default:
-        assert(offset <= (0x504C >> 2));
         s->core_registers[offset] = value;
         break;
     }
include/hw/display/xlnx_dp.h +0 -0
index e85e428..99a6d47 100644

include/hw/display/xlnx_dp.h line 42

Quoted 5 lines
include/hw/display/xlnx_dp.h +1 -1
@@ -39,7 +39,7 @@
 #define AUD_CHBUF_MAX_DEPTH                 (32 * KiB)
 #define MAX_QEMU_BUFFER_SIZE                (4 * KiB)
 
-#define DP_CORE_REG_ARRAY_SIZE              (0x3AF >> 2)
+#define DP_CORE_REG_ARRAY_SIZE              (0x3A8 >> 2)

NAck: this breaks migration.

Quoted 4 lines
include/hw/display/xlnx_dp.h +0 -0
 #define DP_AVBUF_REG_ARRAY_SIZE             (0x238 >> 2)
 #define DP_VBLEND_REG_ARRAY_SIZE            (0x1DF >> 2)
 #define DP_AUDIO_REG_ARRAY_SIZE             (0x50 >> 2)
#3 lore raw
Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Qiang Liu

xlnx_dp_read allows an out-of-bounds read at its default branch because of an improper index.

According to
https://www.xilinx.com/html_docs/registers/ug1087/ug1087-zynq-ultrascale-registers.html
(DP Module), registers 0x3A4/0x3A4/0x3AC are allowed.

DP_INT_MASK 0x000003A4 32 mixed 0xFFFFF03F Interrupt Mask Register for intrN.
DP_INT_EN 0x000003A8 32 mixed 0x00000000 Interrupt Enable Register.
DP_INT_DS 0x000003AC 32 mixed 0x00000000 Interrupt Disable Register.

In xlnx_dp_write, when the offset is 0x3A8 and 0x3AC, the virtual device will write s->core_registers[0x3A4

Quoted 3 lines
Quoted 3 lines

2]. That is to say, the maxize of s->core_registers could be ((0x3A4
2) + 1). However, the current size of s->core_registers is (0x3AF >> 2), that is ((0x3A4 >> 2) + 2), which is out of the range.

In xlxn_dp_read, the access to offset 0x3A8 or 0x3AC will be directed to the offset 0x3A8 (incorrect functionality) or 0x3AC (out-of-bounds read) rather than 0x3A4.

This patch adjusts the size of s->core_registers and enforces the read access to offset 0x3A* and 0x3AC to 0x3A4. BTW, because the size of this MMIO region is 0x3AF, this patch also removes the assertion in xlnx_dp_write.

Quoted 8 lines
Quoted 6 lines
Fixes 58ac482a66de ("introduce xlnx-dp")
---
 hw/display/xlnx_dp.c         | 7 ++++---
 include/hw/display/xlnx_dp.h | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)

Can you provide a qtest reproducer please?

See examples in tests/qtest/fuzz*test.c

Quoted 47 lines

hw/display/xlnx_dp.c static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size) · line 718

Quoted 13 lines
hw/display/xlnx_dp.c +3 -2
index 7bcbb13..8903181 100644
@@ -713,8 +713,10 @@ static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size)
         ret = 0;
         break;
     default:
-        assert(offset <= (0x3AC >> 2));
-        ret = s->core_registers[offset];
+        if (offset == (0x3A8 >> 2) || offset == (0x3AC >> 2))
+            ret = s->core_registers[DP_INT_MASK];
+        else 

Invalid code style.

Quoted 17 lines
hw/display/xlnx_dp.c +1 -1
+            ret = s->core_registers[offset];
         break;
     }
 
@@ -876,7 +878,6 @@ static void xlnx_dp_write(void *opaque, hwaddr offset, uint64_t value,
         xlnx_dp_update_irq(s);
         break;
     default:
-        assert(offset <= (0x504C >> 2));
         s->core_registers[offset] = value;
         break;
     }
include/hw/display/xlnx_dp.h +0 -0
index e85e428..99a6d47 100644

include/hw/display/xlnx_dp.h line 42

Quoted 5 lines
include/hw/display/xlnx_dp.h +1 -1
@@ -39,7 +39,7 @@
 #define AUD_CHBUF_MAX_DEPTH                 (32 * KiB)
 #define MAX_QEMU_BUFFER_SIZE                (4 * KiB)
 
-#define DP_CORE_REG_ARRAY_SIZE              (0x3AF >> 2)
+#define DP_CORE_REG_ARRAY_SIZE              (0x3A8 >> 2)

NAck: this breaks migration.

Quoted 4 lines
include/hw/display/xlnx_dp.h +0 -0
 #define DP_AVBUF_REG_ARRAY_SIZE             (0x238 >> 2)
 #define DP_VBLEND_REG_ARRAY_SIZE            (0x1DF >> 2)
 #define DP_AUDIO_REG_ARRAY_SIZE             (0x50 >> 2)
author
#4 lore raw
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 31 lines · Qiang Liu

xlnx_dp_read allows an out-of-bounds read at its default branch because of an improper index.

According to
https://www.xilinx.com/html_docs/registers/ug1087/ug1087-zynq-ultrascale-registers.html
(DP Module), registers 0x3A4/0x3A4/0x3AC are allowed.

DP_INT_MASK  0x000003A4      32      mixed   0xFFFFF03F      Interrupt Mask Register for intrN.
DP_INT_EN    0x000003A8      32      mixed   0x00000000      Interrupt Enable Register.
DP_INT_DS    0x000003AC      32      mixed   0x00000000      Interrupt Disable Register.

In xlnx_dp_write, when the offset is 0x3A8 and 0x3AC, the virtual device will write s->core_registers[0x3A4

Quoted 3 lines
Quoted 3 lines

2]. That is to say, the maxize of s->core_registers could be ((0x3A4
2) + 1). However, the current size of s->core_registers is (0x3AF >> 2), that is ((0x3A4 >> 2) + 2), which is out of the range.

In xlxn_dp_read, the access to offset 0x3A8 or 0x3AC will be directed to the offset 0x3A8 (incorrect functionality) or 0x3AC (out-of-bounds read) rather than 0x3A4.

This patch adjusts the size of s->core_registers and enforces the read access to offset 0x3A* and 0x3AC to 0x3A4. BTW, because the size of this MMIO region is 0x3AF, this patch also removes the assertion in xlnx_dp_write.

Fixes 58ac482a66de ("introduce xlnx-dp")
---
 hw/display/xlnx_dp.c         | 7 ++++---
 include/hw/display/xlnx_dp.h | 2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
Quoted 3 lines
Quoted 1 line

Can you provide a qtest reproducer please?

See examples in tests/qtest/fuzz*test.c

Yeah. I can add the qtest reproducer.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
hw/display/xlnx_dp.c +0 -0
index 7bcbb13..8903181 100644
@@ -713,8 +713,10 @@ static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size)
         ret = 0;
         break;
Quoted 8 lines
Quoted 8 lines

hw/display/xlnx_dp.c static uint64_t xlnx_dp_read(void *opaque, hwaddr offset, unsigned size) · line 718

Quoted 6 lines
hw/display/xlnx_dp.c +3 -2
     default:
-        assert(offset <= (0x3AC >> 2));
-        ret = s->core_registers[offset];
+        if (offset == (0x3A8 >> 2) || offset == (0x3AC >> 2))
+            ret = s->core_registers[DP_INT_MASK];
+        else

Invalid code style.

Will refine it.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
hw/display/xlnx_dp.c +1 -1
+            ret = s->core_registers[offset];
         break;
     }
 
@@ -876,7 +878,6 @@ static void xlnx_dp_write(void *opaque, hwaddr offset, uint64_t value,
         xlnx_dp_update_irq(s);
         break;
     default:
-        assert(offset <= (0x504C >> 2));
         s->core_registers[offset] = value;
         break;
     }
include/hw/display/xlnx_dp.h +0 -0
index e85e428..99a6d47 100644
@@ -39,7 +39,7 @@
 #define AUD_CHBUF_MAX_DEPTH                 (32 * KiB)
 #define MAX_QEMU_BUFFER_SIZE                (4 * KiB)
 
Quoted 4 lines
Quoted 4 lines

include/hw/display/xlnx_dp.h line 42

Quoted 2 lines
include/hw/display/xlnx_dp.h +1 -1
-#define DP_CORE_REG_ARRAY_SIZE              (0x3AF >> 2)
+#define DP_CORE_REG_ARRAY_SIZE              (0x3A8 >> 2)

NAck: this breaks migration.

I will not modify this.

Quoted 3 lines
Quoted 3 lines

include/hw/display/xlnx_dp.h line 45

Quoted 3 lines
include/hw/display/xlnx_dp.h +0 -0
 #define DP_AVBUF_REG_ARRAY_SIZE             (0x238 >> 2)
 #define DP_VBLEND_REG_ARRAY_SIZE            (0x1DF >> 2)
 #define DP_AUDIO_REG_ARRAY_SIZE             (0x50 >> 2)

Thank you for the review. I will resend the patch soon.

Best,
Qiang