提交 4ab4183d 编写于 作者: D David S. Ahern 提交者: Anthony Liguori

segfault due to buffer overrun in usb-serial

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: NDavid Ahern <daahern@cisco.com>
Signed-off-by: NAnthony Liguori <aliguori@us.ibm.com>
上级 8e65b7c0
...@@ -497,12 +497,28 @@ static int usb_serial_can_read(void *opaque) ...@@ -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) static void usb_serial_read(void *opaque, const uint8_t *buf, int size)
{ {
USBSerialState *s = opaque; USBSerialState *s = opaque;
int first_size = RECV_BUF - s->recv_ptr; int first_size, start;
if (first_size > size)
first_size = size; /* room in the buffer? */
memcpy(s->recv_buf + s->recv_ptr + s->recv_used, buf, first_size); if (size > (RECV_BUF - s->recv_used))
if (size > first_size) size = RECV_BUF - s->recv_used;
memcpy(s->recv_buf, buf + first_size, size - first_size);
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; s->recv_used += size;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册