io: prevent unbounded read of websocket data from client
PATCH 1/1 io: prevent unbounded read of websocket data from client
The QIOChannelWebsock class wraps an underlying QIOChannel instance, providing a websocket server handshake and packet encoding/decoding.
When a watch is added to QIOChannelWebsock, this in turn creates a watch on the underlying QIOChannel that is always monitoring G_IO_IN, as long as the 'encinput' buffer has space.
When this watch fires, it reads data off the wire into 'encinput' and then decodes as much of 'encinput' as possible into the 'rawinput' buffer.
The qio_channel_websock_set_watch method checks to see that 'encinput' does not exceed a max limit, but fails to check that 'rawinput' size. Since any data read into 'encinput', can usually be immediately decoded into 'rawinput', the 'encinput' size check is largely a no-op, and so 'rawinput' will grow without bound.
This is readily seen with the chardev socket backend if enabling websockets, connecting a client that sends data, while not running any guest OS to consume data from the chardev via a virtual serial port.
The qio_channel_websock_read_wire() method will also unconditionally read more data into 'encinput' if it has less than 4k present, which is then decoded into 'rawinput'.
IOW, if qio_channel_websock_read_wire() is triggered for lots of small reads, it will be reading and decoding more data to 'rawinput' than is consumed. Skip reading more data if 'rawinput' is already at its max size.
--- io/channel-websock.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
io/channel-websock.c +3 -1
@@ -970,7 +970,8 @@ static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc, {ssize_t ret; -if (ioc->encinput.offset <4096 ) { +if (ioc->encinput.offset <4096 && + ioc->rawinput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER ) {size_t want =4096 - ioc->encinput.offset;buffer_reserve (&ioc->encinput, want);
@@ -1091,6 +1092,7 @@ static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc) cond |=G_IO_OUT ; }if (ioc->encinput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER && + ioc->rawinput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER && !ioc->io_eof) { cond |=G_IO_IN ; }
Signature · 1 line
2.55.0
Quoted 26 lines · Daniel P. Berrangé
The QIOChannelWebsock class wraps an underlying QIOChannel instance, providing a websocket server handshake and packet encoding/decoding.
When a watch is added to QIOChannelWebsock, this in turn creates a watch on the underlying QIOChannel that is always monitoring
G_IO_IN, as long as the 'encinput' buffer has space.When this watch fires, it reads data off the wire into 'encinput' and then decodes as much of 'encinput' as possible into the 'rawinput' buffer.
The
qio_channel_websock_set_watchmethod checks to see that 'encinput' does not exceed a max limit, but fails to check that 'rawinput' size. Since any data read into 'encinput', can usually be immediately decoded into 'rawinput', the 'encinput' size check is largely a no-op, and so 'rawinput' will grow without bound.This is readily seen with the chardev socket backend if enabling websockets, connecting a client that sends data, while not running any guest OS to consume data from the chardev via a virtual serial port.
The
qio_channel_websock_read_wire()method will also unconditionally read more data into 'encinput' if it has less than 4k present, which is then decoded into 'rawinput'.
Quoted 8 lines
IOW, if
qio_channel_websock_read_wire()is triggered for lots of small reads, it will be reading and decoding more data to 'rawinput' than is consumed. Skip reading more data if 'rawinput' is already at its max size.
not sure if it's worth to add:
Quoted 30 lines
--- io/channel-websock.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)io/channel-websock.c +3
-2@@ -970,7 +970,8 @@ static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc, {ssize_t ret; -if (ioc->encinput.offset <4096 ) { +if (ioc->encinput.offset <4096 && + ioc->rawinput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER ) {size_t want =4096 - ioc->encinput.offset;buffer_reserve (&ioc->encinput, want);@@ -1091,6 +1092,7 @@ static void qio_channel_websock_set_watch(QIOChannelWebsock *ioc) cond |=G_IO_OUT ; }if (ioc->encinput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER && + ioc->rawinput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER && !ioc->io_eof) { cond |=G_IO_IN ; } --2.55.0
Philippe Mathieu-Daudé
Reviewed-byQuoted 32 lines · Marc-André Lureau
Quoted 31 lines · Daniel P. Berrangé
The QIOChannelWebsock class wraps an underlying QIOChannel instance, providing a websocket server handshake and packet encoding/decoding.
When a watch is added to QIOChannelWebsock, this in turn creates a watch on the underlying QIOChannel that is always monitoring
G_IO_IN, as long as the 'encinput' buffer has space.When this watch fires, it reads data off the wire into 'encinput' and then decodes as much of 'encinput' as possible into the 'rawinput' buffer.
The
qio_channel_websock_set_watchmethod checks to see that 'encinput' does not exceed a max limit, but fails to check that 'rawinput' size. Since any data read into 'encinput', can usually be immediately decoded into 'rawinput', the 'encinput' size check is largely a no-op, and so 'rawinput' will grow without bound.This is readily seen with the chardev socket backend if enabling websockets, connecting a client that sends data, while not running any guest OS to consume data from the chardev via a virtual serial port.
The
qio_channel_websock_read_wire()method will also unconditionally read more data into 'encinput' if it has less than 4k present, which is then decoded into 'rawinput'.IOW, if
qio_channel_websock_read_wire()is triggered for lots of small reads, it will be reading and decoding more data to 'rawinput' than is consumed. Skip reading more data if 'rawinput' is already at its max size.
Quoted 8 lines
Quoted 3 lines
Reviewed-by Marc-André Lureau <[email protected]>not sure if it's worth to add:
Fixes 2d1d0e70cf3e ("io: add QIOChannelWebsock class")
Quoted 4 lines
Quoted 4 lines
--- io/channel-websock.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 32 lines · Marc-André Lureau
Quoted 31 lines · Daniel P. Berrangé
The QIOChannelWebsock class wraps an underlying QIOChannel instance, providing a websocket server handshake and packet encoding/decoding.
When a watch is added to QIOChannelWebsock, this in turn creates a watch on the underlying QIOChannel that is always monitoring
G_IO_IN, as long as the 'encinput' buffer has space.When this watch fires, it reads data off the wire into 'encinput' and then decodes as much of 'encinput' as possible into the 'rawinput' buffer.
The
qio_channel_websock_set_watchmethod checks to see that 'encinput' does not exceed a max limit, but fails to check that 'rawinput' size. Since any data read into 'encinput', can usually be immediately decoded into 'rawinput', the 'encinput' size check is largely a no-op, and so 'rawinput' will grow without bound.This is readily seen with the chardev socket backend if enabling websockets, connecting a client that sends data, while not running any guest OS to consume data from the chardev via a virtual serial port.
The
qio_channel_websock_read_wire()method will also unconditionally read more data into 'encinput' if it has less than 4k present, which is then decoded into 'rawinput'.IOW, if
qio_channel_websock_read_wire()is triggered for lots of small reads, it will be reading and decoding more data to 'rawinput' than is consumed. Skip reading more data if 'rawinput' is already at its max size.
Quoted 8 lines
Quoted 3 lines
Reviewed-by Marc-André Lureau <[email protected]>not sure if it's worth to add:
Fixes 2d1d0e70cf3e ("io: add QIOChannelWebsock class")
Doesn't hurt, so might as well.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|