From a84c5c164fa6bb2b19d6ff7f55d23fee1b287edb Mon Sep 17 00:00:00 2001 From: Marcelo Tosatti Date: Mon, 1 Mar 2010 20:52:12 -0300 Subject: [PATCH 09/20] segfault due to buffer overrun in usb-serial RH-Author: Marcelo Tosatti Message-id: <31db33e10294227824c3854ffd2606b6677ae83e.1267476474.git.mtosatti@redhat.com> Patchwork-id: 7351 O-Subject: [PATCH 10/21] segfault due to buffer overrun in usb-serial Bugzilla: 569613 RH-Acked-by: Markus Armbruster RH-Acked-by: Gleb Natapov RH-Acked-by: Rik van Riel From: David S. Ahern This fixes a segfault due to buffer overrun in the usb-serial device. The memcpy was incrementing the start location by recv_used yet, the computation of first_size (how much to write at the end of the buffer before wrapping to the front) was not accounting for it. This causes the next element after the receive buffer (recv_ptr) to get overwritten with random data. Signed-off-by: David Ahern Signed-off-by: Anthony Liguori (cherry picked from commit 4ab4183d766f10f9fc35cd9ef4acee39f241986f) (cherry picked from commit beb8eab90cd52ce1b53c4b91bab10e954ea14eea) --- hw/usb-serial.c | 28 ++++++++++++++++++++++------ 1 files changed, 22 insertions(+), 6 deletions(-) Signed-off-by: Eduardo Habkost --- hw/usb-serial.c | 28 ++++++++++++++++++++++------ 1 files changed, 22 insertions(+), 6 deletions(-) diff --git a/hw/usb-serial.c b/hw/usb-serial.c index 37293ea..c3f3401 100644 --- a/hw/usb-serial.c +++ b/hw/usb-serial.c @@ -497,12 +497,28 @@ static int usb_serial_can_read(void *opaque) static void usb_serial_read(void *opaque, const uint8_t *buf, int size) { USBSerialState *s = opaque; - int first_size = RECV_BUF - s->recv_ptr; - if (first_size > size) - first_size = size; - memcpy(s->recv_buf + s->recv_ptr + s->recv_used, buf, first_size); - if (size > first_size) - memcpy(s->recv_buf, buf + first_size, size - first_size); + int first_size, start; + + /* room in the buffer? */ + if (size > (RECV_BUF - s->recv_used)) + size = RECV_BUF - s->recv_used; + + start = s->recv_ptr + s->recv_used; + if (start < RECV_BUF) { + /* copy data to end of buffer */ + first_size = RECV_BUF - start; + if (first_size > size) + first_size = size; + + memcpy(s->recv_buf + start, buf, first_size); + + /* wrap around to front if needed */ + if (size > first_size) + memcpy(s->recv_buf, buf + first_size, size - first_size); + } else { + start -= RECV_BUF; + memcpy(s->recv_buf + start, buf, size); + } s->recv_used += size; } -- 1.7.0.3