Blame 0003-input-limit-kbd-queue-depth.patch

335584
From: Gerd Hoffmann <kraxel@redhat.com>
335584
Date: Fri, 28 Apr 2017 10:42:37 +0200
335584
Subject: [PATCH] input: limit kbd queue depth
335584
335584
Apply a limit to the number of items we accept into the keyboard queue.
335584
335584
Impact: Without this limit vnc clients can exhaust host memory by
335584
sending keyboard events faster than qemu feeds them to the guest.
335584
335584
Fixes: CVE-2017-8379
335584
Cc: P J P <ppandit@redhat.com>
335584
Cc: Huawei PSIRT <PSIRT@huawei.com>
335584
Reported-by: jiangxin1@huawei.com
335584
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
335584
Message-id: 20170428084237.23960-1-kraxel@redhat.com
335584
(cherry picked from commit fa18f36a461984eae50ab957e47ec78dae3c14fc)
335584
---
335584
 ui/input.c | 14 +++++++++++---
335584
 1 file changed, 11 insertions(+), 3 deletions(-)
335584
335584
diff --git a/ui/input.c b/ui/input.c
335584
index ed88cda6d6..fb1f404095 100644
335584
--- a/ui/input.c
335584
+++ b/ui/input.c
335584
@@ -41,6 +41,8 @@ static QTAILQ_HEAD(QemuInputEventQueueHead, QemuInputEventQueue) kbd_queue =
335584
     QTAILQ_HEAD_INITIALIZER(kbd_queue);
335584
 static QEMUTimer *kbd_timer;
335584
 static uint32_t kbd_default_delay_ms = 10;
335584
+static uint32_t queue_count;
335584
+static uint32_t queue_limit = 1024;
335584
 
335584
 QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
335584
                                                    QemuInputHandler *handler)
335584
@@ -268,6 +270,7 @@ static void qemu_input_queue_process(void *opaque)
335584
             break;
335584
         }
335584
         QTAILQ_REMOVE(queue, item, node);
335584
+        queue_count--;
335584
         g_free(item);
335584
     }
335584
 }
335584
@@ -282,6 +285,7 @@ static void qemu_input_queue_delay(struct QemuInputEventQueueHead *queue,
335584
     item->delay_ms = delay_ms;
335584
     item->timer = timer;
335584
     QTAILQ_INSERT_TAIL(queue, item, node);
335584
+    queue_count++;
335584
 
335584
     if (start_timer) {
335584
         timer_mod(item->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)
335584
@@ -298,6 +302,7 @@ static void qemu_input_queue_event(struct QemuInputEventQueueHead *queue,
335584
     item->src = src;
335584
     item->evt = evt;
335584
     QTAILQ_INSERT_TAIL(queue, item, node);
335584
+    queue_count++;
335584
 }
335584
 
335584
 static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
335584
@@ -306,6 +311,7 @@ static void qemu_input_queue_sync(struct QemuInputEventQueueHead *queue)
335584
 
335584
     item->type = QEMU_INPUT_QUEUE_SYNC;
335584
     QTAILQ_INSERT_TAIL(queue, item, node);
335584
+    queue_count++;
335584
 }
335584
 
335584
 void qemu_input_event_send_impl(QemuConsole *src, InputEvent *evt)
335584
@@ -381,7 +387,7 @@ void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
335584
         qemu_input_event_send(src, evt);
335584
         qemu_input_event_sync();
335584
         qapi_free_InputEvent(evt);
335584
-    } else {
335584
+    } else if (queue_count < queue_limit) {
335584
         qemu_input_queue_event(&kbd_queue, src, evt);
335584
         qemu_input_queue_sync(&kbd_queue);
335584
     }
335584
@@ -409,8 +415,10 @@ void qemu_input_event_send_key_delay(uint32_t delay_ms)
335584
         kbd_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, qemu_input_queue_process,
335584
                                  &kbd_queue);
335584
     }
335584
-    qemu_input_queue_delay(&kbd_queue, kbd_timer,
335584
-                           delay_ms ? delay_ms : kbd_default_delay_ms);
335584
+    if (queue_count < queue_limit) {
335584
+        qemu_input_queue_delay(&kbd_queue, kbd_timer,
335584
+                               delay_ms ? delay_ms : kbd_default_delay_ms);
335584
+    }
335584
 }
335584
 
335584
 InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)