hw/char/cadence_uart: Fix guards on invalid BRGR/BDIV settings
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
@@ -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
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
Signed-off-by Peter Maydell <[email protected]>--- hw/char/cadence_uart.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/char/cadence_uart.c +2
-1@@ -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 :
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
Signed-off-by Peter Maydell <[email protected]>
Quoted 31 lines
--- hw/char/cadence_uart.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/char/cadence_uart.c +4
-2@@ -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
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
Signed-off-by Peter Maydell <[email protected]>---
hw/char/cadence_uart.c | 6 ++++--1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 24 lines
hw/char/cadence_uart.c +0
-0@@ -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:
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
Signed-off-by Peter Maydell <[email protected]>
Alistair
Quoted 30 lines
--- hw/char/cadence_uart.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/char/cadence_uart.c +4
-3@@ -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
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
Signed-off-by Peter Maydell <[email protected]>--- hw/char/cadence_uart.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
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
Signed-off-by Peter Maydell <[email protected]>--- hw/char/cadence_uart.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/char/cadence_uart.c +2
-1@@ -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.