Skip to contentSkip to conversation

hw/char/cadence_uart: Fix guards on invalid BRGR/BDIV settings

PATCH In review Peter Maydell 7 participants 7 messages Mar 14, 2023 – Mar 15, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/char/cadence_uart: Fix guards on invalid BRGR/BDIV settings

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function. However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

---
 hw/char/cadence_uart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/cadence_uart.c +4 -2
index c069a30842e..807e3985419 100644
@@ -450,13 +450,15 @@ static MemTxResult uart_write(void *opaque, hwaddr offset,
         }
         break;
     case R_BRGR: /* Baud rate generator */
+        value &= 0xffff;
         if (value >= 0x01) {
-            s->r[offset] = value & 0xFFFF;
+            s->r[offset] = value;
         }
         break;
     case R_BDIV:    /* Baud rate divider */
+        value &= 0xff;
         if (value >= 0x04) {
-            s->r[offset] = value & 0xFF;
+            s->r[offset] = value;
         }
         break;
     default:
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Peter Maydell

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function. However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

---
  hw/char/cadence_uart.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/cadence_uart.c +2 -1
index c069a30842e..807e3985419 100644
@@ -450,13 +450,15 @@ static MemTxResult uart_write(void *opaque, hwaddr offset,
          }
          break;
      case R_BRGR: /* Baud rate generator */
+        value &= 0xffff;
          if (value >= 0x01) {
-            s->r[offset] = value & 0xFFFF;
+            s->r[offset] = value;

hw/char/cadence_uart.c static MemTxResult uart_write(void *opaque, hwaddr offset, · line 464

Quoted 10 lines
hw/char/cadence_uart.c +2 -1
          }
          break;
      case R_BDIV:    /* Baud rate divider */
+        value &= 0xff;
          if (value >= 0x04) {
-            s->r[offset] = value & 0xFF;
+            s->r[offset] = value;
          }
          break;
      default:
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function. However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Quoted 5 lines

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

Quoted 31 lines
---
 hw/char/cadence_uart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/cadence_uart.c +4 -2
index c069a30842e..807e3985419 100644
@@ -450,13 +450,15 @@ static MemTxResult uart_write(void *opaque, hwaddr
 offset,
         }
         break;
     case R_BRGR: /* Baud rate generator */
+        value &= 0xffff;
         if (value >= 0x01) {
-            s->r[offset] = value & 0xFFFF;
+            s->r[offset] = value;
         }
         break;
     case R_BDIV:    /* Baud rate divider */
+        value &= 0xff;
         if (value >= 0x04) {
-            s->r[offset] = value & 0xFF;
+            s->r[offset] = value;
         }
         break;

default:
--
2.34.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function.  However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Quoted 7 lines

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

---
 hw/char/cadence_uart.c | 6 ++++--

 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 24 lines
hw/char/cadence_uart.c +0 -0
index c069a30842e..807e3985419 100644
@@ -450,13 +450,15 @@ static MemTxResult uart_write(void *opaque,
 hwaddr offset,
          }
          break;

     case R_BRGR: /* Baud rate generator */
+        value &= 0xffff;
         if (value >= 0x01) {
-            s->r[offset] = value & 0xFFFF;
+            s->r[offset] = value;
         }
         break;
     case R_BDIV:    /* Baud rate divider */
+        value &= 0xff;
         if (value >= 0x04) {
-            s->r[offset] = value & 0xFF;
+            s->r[offset] = value;
         }
         break;
     default:

Reviewed-by on v1 1/1
#4 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function. However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Quoted 4 lines

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

Alistair

Quoted 30 lines
---
 hw/char/cadence_uart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/cadence_uart.c +4 -3
index c069a30842e..807e3985419 100644
@@ -450,13 +450,15 @@ static MemTxResult uart_write(void *opaque, hwaddr offset,
         }
         break;
     case R_BRGR: /* Baud rate generator */
+        value &= 0xffff;
         if (value >= 0x01) {
-            s->r[offset] = value & 0xFFFF;
+            s->r[offset] = value;
         }
         break;
     case R_BDIV:    /* Baud rate divider */
+        value &= 0xff;
         if (value >= 0x04) {
-            s->r[offset] = value & 0xFF;
+            s->r[offset] = value;
         }
         break;
     default:
--

2.34.1

Reviewed-by on v1 1/1
#5 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function. However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Quoted 7 lines

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

---
  hw/char/cadence_uart.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
Tested-by on v1 1/1
#6 lore raw
In reply to Peter Maydell
Quoted 27 lines · Peter Maydell

The cadence UART attempts to avoid allowing the guset to set invalid baud rate register values in the uart_write() function. However it does the "mask to the size of the register field" and "check for invalid values" in the wrong order, which means that a malicious guest can get a bogus value into the register by setting also some high bits in the value, and cause QEMU to crash by division-by-zero.

Do the mask before the bounds check instead of afterwards.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1493

---
  hw/char/cadence_uart.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/cadence_uart.c +2 -1
index c069a30842e..807e3985419 100644
@@ -450,13 +450,15 @@ static MemTxResult uart_write(void *opaque, hwaddr offset,
          }
          break;
      case R_BRGR: /* Baud rate generator */
+        value &= 0xffff;
          if (value >= 0x01) {
-            s->r[offset] = value & 0xFFFF;
+            s->r[offset] = value;

hw/char/cadence_uart.c static MemTxResult uart_write(void *opaque, hwaddr offset, · line 464

Quoted 10 lines
hw/char/cadence_uart.c +2 -1
          }
          break;
      case R_BDIV:    /* Baud rate divider */
+        value &= 0xff;
          if (value >= 0x04) {
-            s->r[offset] = value & 0xFF;
+            s->r[offset] = value;
          }
          break;
      default:

Tested on my side.