Skip to content

Commit c0dd463

Browse files
Merge pull request #257 from justincormack/issues
Fix various overflow issues
2 parents dab74a6 + 12b9d52 commit c0dd463

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/lib/pci_virtio_9p.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,19 @@ pci_vt9p_thread(void *vsc)
348348
command = buf[4];
349349
tag = (uint16_t)((uint16_t)buf[5] | ((uint16_t)buf[6] << 8));
350350
DPRINTF(("[thread]Got response for tag %d command %d len %d\r\n", (int)tag, (int)command, (int)len));
351+
if (len > BUFSIZE) {
352+
fprintf(stderr, "virtio-9p: command too long, maximum is %d\n", BUFSIZE);
353+
/* Fatal error, crash VM, let us be restarted */
354+
_exit(1);
355+
}
356+
if (len < minlen) {
357+
fprintf(stderr, "virtio-9p: command too short, must be over 7 bytes\n");
358+
/* Fatal error, crash VM, let us be restarted */
359+
_exit(1);
360+
}
351361
n = (size_t)(len - minlen);
352362
ptr = buf + minlen;
353363
while (n) {
354-
assert(len <= BUFSIZE);
355364
ret = read(sc->v9sc_sock, ptr, n);
356365
if (ret <= 0) {
357366
fprintf(stderr, "virtio-9p: unexpected EOF reading-- did the 9P server crash?\n");
@@ -362,9 +371,12 @@ pci_vt9p_thread(void *vsc)
362371
ptr += ret;
363372
}
364373
DPRINTF(("[thread]got complete response for tag %d len %d\r\n", (int)tag, (int)len));
365-
if (command == 107) {
374+
if (command == 107) { /* Rerror */
366375
char msg[128];
367376
uint16_t slen = (uint16_t)((uint16_t)buf[7] | ((uint16_t)buf[8] << 8));
377+
if (slen > 128) {
378+
slen = 128; /* truncate overlong error message if required */
379+
}
368380
memcpy(msg, &buf[9], slen);
369381
msg[slen] = 0;
370382
DPRINTF(("[thread]Rerror: %s\r\n", msg));

src/lib/pci_virtio_sock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,12 +983,12 @@ static int buffer_write(struct pci_vtsock_sock *sock,
983983
uint32_t len, struct iovec *iov, int iov_len)
984984
{
985985
size_t nr;
986-
if (sock->write_buf_tail + len > WRITE_BUF_LENGTH) {
986+
if (len > WRITE_BUF_LENGTH - sock->write_buf_tail) {
987987
DPRINTF(("TX: fd %d unable to buffer write of 0x%"PRIx32" bytes,"
988988
" buffer use 0x%x/0x%x, 0x%x remaining\n",
989989
sock->fd, len, sock->write_buf_tail,
990990
WRITE_BUF_LENGTH,
991-
WRITE_BUF_LENGTH < sock->write_buf_tail));
991+
WRITE_BUF_LENGTH - sock->write_buf_tail));
992992
return -1;
993993
}
994994

0 commit comments

Comments
 (0)