Skip to content

Commit

Permalink
hw/char/pl011: Avoid division-by-zero in pl011_get_baudrate()
Browse files Browse the repository at this point in the history
In pl011_get_baudrate(), when we calculate the baudrate we can
accidentally divide by zero. This happens because although (as the
specification requires) we treat UARTIBRD = 0 as invalid, we aren't
correctly limiting UARTIBRD and UARTFBRD values to the 16-bit and 6-bit
ranges the hardware allows, and so some non-zero values of UARTIBRD can
result in a zero divisor.

Enforce the correct register field widths on guest writes and on inbound
migration to avoid the division by zero.

ASAN log:
==2973125==ERROR: AddressSanitizer: FPE on unknown address 0x55f72629b348
(pc 0x55f72629b348 bp 0x7fffa24d0e00 sp 0x7fffa24d0d60 T0)
    #0 0x55f72629b348 in pl011_get_baudrate hw/char/pl011.c:255:17
    #1 0x55f726298d94 in pl011_trace_baudrate_change hw/char/pl011.c:260:33
    #2 0x55f726296fc8 in pl011_write hw/char/pl011.c:378:9

Reproducer:
cat << EOF | qemu-system-aarch64 -display \
none -machine accel=qtest, -m 512M -machine realview-pb-a8 -qtest stdio
writeq 0x1000b024 0xf8000000
EOF

Signed-off-by: Zheyu Ma <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
ZheyuMa authored and Patchew Applier committed Jul 2, 2024
1 parent c80a339 commit fe27675
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions hw/char/pl011.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ static void pl011_write(void *opaque, hwaddr offset,
s->ilpr = value;
break;
case 9: /* UARTIBRD */
s->ibrd = value;
s->ibrd = value & 0xffff;
pl011_trace_baudrate_change(s);
break;
case 10: /* UARTFBRD */
s->fbrd = value;
s->fbrd = value & 0x3f;
pl011_trace_baudrate_change(s);
break;
case 11: /* UARTLCR_H */
Expand Down Expand Up @@ -531,6 +531,9 @@ static int pl011_post_load(void *opaque, int version_id)
s->read_pos = 0;
}

s->ibrd &= 0xffff;
s->fbrd &= 0x3f;

return 0;
}

Expand Down

0 comments on commit fe27675

Please sign in to comment.