From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57528) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XeNYg-0005iT-7O for qemu-devel@nongnu.org; Wed, 15 Oct 2014 08:20:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XeNYV-0001he-KF for qemu-devel@nongnu.org; Wed, 15 Oct 2014 08:20:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:22255) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XeNYV-0001hL-8z for qemu-devel@nongnu.org; Wed, 15 Oct 2014 08:19:51 -0400 From: Gerd Hoffmann Date: Wed, 15 Oct 2014 14:19:45 +0200 Message-Id: <1413375585-20301-7-git-send-email-kraxel@redhat.com> In-Reply-To: <1413375585-20301-1-git-send-email-kraxel@redhat.com> References: <1413375585-20301-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PATCH 6/6] vnc: track & limit connections List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann , Anthony Liguori , "Dr. David Alan Gilbert" Also track the number of connections in "connecting" and "shared" state (additionally to "exclusive" state). Apply a configurable limit to these connections. The logic to apply the limit to connections in "shared" state is pretty simple: When the limit is reached no new connections are allowed. The logic to apply the limit to connections in "connecting" state (this is the state you are in *before* successfull authentication) is slightly different: A new connect kicks out the oldest client which is still in "connecting" state. This avoids a easy DoS by unauthenticated users by simply opening connections until the limit is reached. Cc: Dr. David Alan Gilbert Signed-off-by: Gerd Hoffmann --- ui/vnc.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- ui/vnc.h | 3 +++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ui/vnc.c b/ui/vnc.c index a03a7e1..cc4ac5c 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -68,12 +68,34 @@ static void vnc_set_share_mode(VncState *vs, VncShareMode mode) vs->csock, mn[vs->share_mode], mn[mode]); #endif - if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) { + switch (vs->share_mode) { + case VNC_SHARE_MODE_CONNECTING: + vs->vd->num_connecting--; + break; + case VNC_SHARE_MODE_SHARED: + vs->vd->num_shared--; + break; + case VNC_SHARE_MODE_EXCLUSIVE: vs->vd->num_exclusive--; + break; + default: + break; } + vs->share_mode = mode; - if (vs->share_mode == VNC_SHARE_MODE_EXCLUSIVE) { + + switch (vs->share_mode) { + case VNC_SHARE_MODE_CONNECTING: + vs->vd->num_connecting++; + break; + case VNC_SHARE_MODE_SHARED: + vs->vd->num_shared++; + break; + case VNC_SHARE_MODE_EXCLUSIVE: vs->vd->num_exclusive++; + break; + default: + break; } } @@ -2327,6 +2349,11 @@ static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) } vnc_set_share_mode(vs, mode); + if (vs->vd->num_shared > vs->vd->connections_limit) { + vnc_disconnect_start(vs); + return 0; + } + vs->client_width = pixman_image_get_width(vs->vd->server); vs->client_height = pixman_image_get_height(vs->vd->server); vnc_write_u16(vs, vs->client_width); @@ -2879,6 +2906,15 @@ static void vnc_connect(VncDisplay *vd, int csock, { vnc_init_state(vs); } + + if (vd->num_connecting > vd->connections_limit) { + QTAILQ_FOREACH(vs, &vd->clients, next) { + if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { + vnc_disconnect_start(vs); + return; + } + } + } } void vnc_init_state(VncState *vs) @@ -2897,7 +2933,7 @@ void vnc_init_state(VncState *vs) qemu_mutex_init(&vs->output_mutex); vs->bh = qemu_bh_new(vnc_jobs_bh, vs); - QTAILQ_INSERT_HEAD(&vd->clients, vs, next); + QTAILQ_INSERT_TAIL(&vd->clients, vs, next); graphic_hw_update(vd->dcl.con); @@ -3087,6 +3123,9 @@ static QemuOptsList qemu_vnc_opts = { .name = "head", .type = QEMU_OPT_NUMBER, },{ + .name = "connections", + .type = QEMU_OPT_NUMBER, + },{ .name = "password", .type = QEMU_OPT_BOOL, },{ @@ -3200,6 +3239,7 @@ void vnc_display_open(const char *id, Error **errp) } else { vs->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; } + vs->connections_limit = qemu_opt_get_number(opts, "connections", 32); #ifdef CONFIG_VNC_WS websocket = qemu_opt_get(opts, "websocket"); diff --git a/ui/vnc.h b/ui/vnc.h index 6fe8278..5e2b1a5 100644 --- a/ui/vnc.h +++ b/ui/vnc.h @@ -150,7 +150,10 @@ typedef enum VncSharePolicy { struct VncDisplay { QTAILQ_HEAD(, VncState) clients; + int num_connecting; + int num_shared; int num_exclusive; + int connections_limit; VncSharePolicy share_policy; int lsock; #ifdef CONFIG_VNC_WS -- 1.8.3.1