1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
From: "Daniel P. Berrange" <berrange@redhat.com>
Date: Thu, 2 Feb 2017 18:18:48 +0000
Subject: Correctly validate color map range indexes
The color map index could wrap around to zero causing negative
array index accesses.
https://bugzilla.gnome.org/show_bug.cgi?id=778050
CVE-2017-5885
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
src/vnccolormap.c | 4 +--
src/vncconnection.c | 18 +++++++++---
src/vncconnectiontest.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 92 insertions(+), 6 deletions(-)
diff --git a/src/vnccolormap.c b/src/vnccolormap.c
index 25cd2fc..f3e153a 100644
--- a/src/vnccolormap.c
+++ b/src/vnccolormap.c
@@ -119,7 +119,7 @@ gboolean vnc_color_map_set(VncColorMap *map,
guint16 green,
guint16 blue)
{
- if (idx >= (map->size + map->offset))
+ if (idx < map->offset || idx >= (map->size + map->offset))
return FALSE;
map->colors[idx - map->offset].red = red;
@@ -149,7 +149,7 @@ gboolean vnc_color_map_lookup(VncColorMap *map,
guint16 *green,
guint16 *blue)
{
- if (idx >= (map->size + map->offset))
+ if (idx < map->offset || idx >= (map->size + map->offset))
return FALSE;
*red = map->colors[idx - map->offset].red;
diff --git a/src/vncconnection.c b/src/vncconnection.c
index 8290b65..e95811f 100644
--- a/src/vncconnection.c
+++ b/src/vncconnection.c
@@ -3344,8 +3344,13 @@ static gboolean vnc_connection_server_message(VncConnection *conn)
VNC_DEBUG("Colour map from %d with %d entries",
first_color, n_colors);
- map = vnc_color_map_new(first_color, n_colors);
+ if (first_color > (65536 - n_colors)) {
+ vnc_connection_set_error(conn, "Colormap start %d out of range %d", first_color, 65536 - n_colors);
+ break;
+ }
+
+ map = vnc_color_map_new(first_color, n_colors);
for (i = 0; i < n_colors; i++) {
guint16 red, green, blue;
@@ -3353,9 +3358,14 @@ static gboolean vnc_connection_server_message(VncConnection *conn)
green = vnc_connection_read_u16(conn);
blue = vnc_connection_read_u16(conn);
- vnc_color_map_set(map,
- i + first_color,
- red, green, blue);
+ if (!vnc_color_map_set(map,
+ i + first_color,
+ red, green, blue)) {
+ /* Should not be reachable given earlier range check */
+ vnc_connection_set_error(conn, "Colormap index %d out of range %d,%d",
+ i + first_color, first_color, 65536 - n_colors);
+ break;
+ }
}
vnc_framebuffer_set_color_map(priv->fb, map);
diff --git a/src/vncconnectiontest.c b/src/vncconnectiontest.c
index 521529e..4917b2f 100644
--- a/src/vncconnectiontest.c
+++ b/src/vncconnectiontest.c
@@ -445,6 +445,76 @@ static void test_unexpected_cmap_server(GInputStream *is, GOutputStream *os)
}
+static void test_overflow_cmap_server(GInputStream *is, GOutputStream *os)
+{
+ /* Frame buffer width / height */
+ test_send_u16(os, 100);
+ test_send_u16(os, 100);
+
+ /* BPP, depth, endian, true color */
+ test_send_u8(os, 32);
+ test_send_u8(os, 8);
+ test_send_u8(os, 1);
+ test_send_u8(os, 0);
+
+ /* RGB max + shift*/
+ test_send_u16(os, 255);
+ test_send_u16(os, 255);
+ test_send_u16(os, 255);
+ test_send_u8(os, 0);
+ test_send_u8(os, 8);
+ test_send_u8(os, 16);
+
+ guint8 pad[3] = {0};
+ test_send_bytes(os, pad, G_N_ELEMENTS(pad));
+
+ /* name */
+ guint8 name[] = { 'T', 'e', 's', 't' };
+ test_send_u32(os, G_N_ELEMENTS(name));
+ test_send_bytes(os, name, G_N_ELEMENTS(name));
+
+ /* n-encodings */
+ test_recv_u8(is, 2);
+ /* pad */
+ test_recv_u8(is, 0);
+ /* num encodings */
+ test_recv_u16(is, 5);
+
+ /* encodings */
+ test_recv_s32(is, 16);
+ test_recv_s32(is, 5);
+ test_recv_s32(is, 2);
+ test_recv_s32(is, 1);
+ test_recv_s32(is, 0);
+
+ /* update request */
+ test_recv_u8(is, 3);
+ /* ! incremental */
+ test_recv_u8(is, 0);
+
+ /* x, y, w, h */
+ test_recv_u16(is, 0);
+ test_recv_u16(is, 0);
+ test_recv_u16(is, 100);
+ test_recv_u16(is, 100);
+
+ /* set color map */
+ test_send_u8(os, 1);
+ /* pad */
+ test_send_u8(os, 0);
+ /* first color, ncolors */
+ test_send_u16(os, 65535);
+ test_send_u16(os, 2);
+
+ /* r,g,b */
+ for (int i = 0 ; i < 2; i++) {
+ test_send_u16(os, i);
+ test_send_u16(os, i);
+ test_send_u16(os, i);
+ }
+}
+
+
static void test_validation(void (*test_func)(GInputStream *, GOutputStream *))
{
struct GVncTest *test;
@@ -526,6 +596,11 @@ static void test_validation_unexpected_cmap(void)
{
test_validation(test_unexpected_cmap_server);
}
+
+static void test_validation_overflow_cmap(void)
+{
+ test_validation(test_overflow_cmap_server);
+}
#endif
int main(int argc, char **argv) {
@@ -541,6 +616,7 @@ int main(int argc, char **argv) {
g_test_add_func("/conn/validation/copyrect", test_validation_copyrect);
g_test_add_func("/conn/validation/hextile", test_validation_hextile);
g_test_add_func("/conn/validation/unexpectedcmap", test_validation_unexpected_cmap);
+ g_test_add_func("/conn/validation/overflowcmap", test_validation_overflow_cmap);
#endif
return g_test_run();
|