Blame 0009-hmp-fix-sendkey-out-of-bounds-write-CVE-2015-8619.patch

7d975d
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
7d975d
Date: Wed, 13 Jan 2016 09:09:58 +0100
7d975d
Subject: [PATCH] hmp: fix sendkey out of bounds write (CVE-2015-8619)
7d975d
7d975d
When processing 'sendkey' command, hmp_sendkey routine null
7d975d
terminates the 'keyname_buf' array. This results in an OOB
7d975d
write issue, if 'keyname_len' was to fall outside of
7d975d
'keyname_buf' array.
7d975d
7d975d
Since the keyname's length is known the keyname_buf can be
7d975d
removed altogether by adding a length parameter to
7d975d
index_from_key() and using it for the error output as well.
7d975d
7d975d
Reported-by: Ling Liu <liuling-it@360.cn>
7d975d
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
7d975d
Message-Id: <20160113080958.GA18934@olga>
7d975d
[Comparison with "<" dumbed down, test for junk after strtoul()
7d975d
tweaked]
7d975d
Signed-off-by: Markus Armbruster <armbru@redhat.com>
7d975d
7d975d
(cherry picked from commit 64ffbe04eaafebf4045a3ace52a360c14959d196)
7d975d
---
7d975d
 hmp.c                | 18 ++++++++----------
7d975d
 include/ui/console.h |  2 +-
7d975d
 ui/input-legacy.c    |  5 +++--
7d975d
 3 files changed, 12 insertions(+), 13 deletions(-)
7d975d
7d975d
diff --git a/hmp.c b/hmp.c
7d975d
index 2140605..1904203 100644
7d975d
--- a/hmp.c
7d975d
+++ b/hmp.c
7d975d
@@ -1734,21 +1734,18 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
7d975d
     int has_hold_time = qdict_haskey(qdict, "hold-time");
7d975d
     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
7d975d
     Error *err = NULL;
7d975d
-    char keyname_buf[16];
7d975d
     char *separator;
7d975d
     int keyname_len;
7d975d
 
7d975d
     while (1) {
7d975d
         separator = strchr(keys, '-');
7d975d
         keyname_len = separator ? separator - keys : strlen(keys);
7d975d
-        pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
7d975d
 
7d975d
         /* Be compatible with old interface, convert user inputted "<" */
7d975d
-        if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
7d975d
-            pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
7d975d
+        if (keys[0] == '<' && keyname_len == 1) {
7d975d
+            keys = "less";
7d975d
             keyname_len = 4;
7d975d
         }
7d975d
-        keyname_buf[keyname_len] = 0;
7d975d
 
7d975d
         keylist = g_malloc0(sizeof(*keylist));
7d975d
         keylist->value = g_malloc0(sizeof(*keylist->value));
7d975d
@@ -1761,16 +1758,17 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
7d975d
         }
7d975d
         tmp = keylist;
7d975d
 
7d975d
-        if (strstart(keyname_buf, "0x", NULL)) {
7d975d
+        if (strstart(keys, "0x", NULL)) {
7d975d
             char *endp;
7d975d
-            int value = strtoul(keyname_buf, &endp, 0);
7d975d
-            if (*endp != '\0') {
7d975d
+            int value = strtoul(keys, &endp, 0);
7d975d
+            assert(endp <= keys + keyname_len);
7d975d
+            if (endp != keys + keyname_len) {
7d975d
                 goto err_out;
7d975d
             }
7d975d
             keylist->value->type = KEY_VALUE_KIND_NUMBER;
7d975d
             keylist->value->u.number = value;
7d975d
         } else {
7d975d
-            int idx = index_from_key(keyname_buf);
7d975d
+            int idx = index_from_key(keys, keyname_len);
7d975d
             if (idx == Q_KEY_CODE_MAX) {
7d975d
                 goto err_out;
7d975d
             }
7d975d
@@ -1792,7 +1790,7 @@ out:
7d975d
     return;
7d975d
 
7d975d
 err_out:
7d975d
-    monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
7d975d
+    monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
7d975d
     goto out;
7d975d
 }
7d975d
 
7d975d
diff --git a/include/ui/console.h b/include/ui/console.h
7d975d
index c249db4..5739bdd 100644
7d975d
--- a/include/ui/console.h
7d975d
+++ b/include/ui/console.h
7d975d
@@ -433,7 +433,7 @@ static inline int vnc_display_pw_expire(const char *id, time_t expires)
7d975d
 void curses_display_init(DisplayState *ds, int full_screen);
7d975d
 
7d975d
 /* input.c */
7d975d
-int index_from_key(const char *key);
7d975d
+int index_from_key(const char *key, size_t key_length);
7d975d
 
7d975d
 /* gtk.c */
7d975d
 void early_gtk_display_init(int opengl);
7d975d
diff --git a/ui/input-legacy.c b/ui/input-legacy.c
7d975d
index e0a39f0..3f28bbc 100644
7d975d
--- a/ui/input-legacy.c
7d975d
+++ b/ui/input-legacy.c
7d975d
@@ -57,12 +57,13 @@ struct QEMUPutLEDEntry {
7d975d
 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
7d975d
     QTAILQ_HEAD_INITIALIZER(led_handlers);
7d975d
 
7d975d
-int index_from_key(const char *key)
7d975d
+int index_from_key(const char *key, size_t key_length)
7d975d
 {
7d975d
     int i;
7d975d
 
7d975d
     for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
7d975d
-        if (!strcmp(key, QKeyCode_lookup[i])) {
7d975d
+        if (!strncmp(key, QKeyCode_lookup[i], key_length) &&
7d975d
+            !QKeyCode_lookup[i][key_length]) {
7d975d
             break;
7d975d
         }
7d975d
     }