Blame SOURCES/0013-kex-bail-out-on-rubbish-in-the-incoming-packet.patch

68eb44
From d62ed72daacc95e75bd0a617af9e4d88829b2e77 Mon Sep 17 00:00:00 2001
68eb44
From: Mariusz Ziulek <mzet@owasp.org>
68eb44
Date: Sat, 21 Feb 2015 23:31:36 +0100
68eb44
Subject: [PATCH] kex: bail out on rubbish in the incoming packet
68eb44
68eb44
CVE-2015-1782
68eb44
68eb44
Bug: http://www.libssh2.org/adv_20150311.html
68eb44
68eb44
Upstream-commit: 7d94b69b802c03e8d7726f4ae61baba52cb7d871
68eb44
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
68eb44
---
68eb44
 src/kex.c | 73 +++++++++++++++++++++++++++++++++++----------------------------
68eb44
 1 file changed, 41 insertions(+), 32 deletions(-)
68eb44
68eb44
diff --git a/src/kex.c b/src/kex.c
68eb44
index fa4c4e1..ad7498a 100644
68eb44
--- a/src/kex.c
68eb44
+++ b/src/kex.c
68eb44
@@ -1549,6 +1549,30 @@ static int kex_agree_comp(LIBSSH2_SESSION *session,
68eb44
  * The Client gets to make the final call on "agreed methods"
68eb44
  */
68eb44
 
68eb44
+/*
68eb44
+ * kex_string_pair() extracts a string from the packet and makes sure it fits
68eb44
+ * within the given packet.
68eb44
+ */
68eb44
+static int kex_string_pair(unsigned char **sp,   /* parsing position */
68eb44
+                           unsigned char *data,  /* start pointer to packet */
68eb44
+                           size_t data_len,      /* size of total packet */
68eb44
+                           size_t *lenp,         /* length of the string */
68eb44
+                           unsigned char **strp) /* pointer to string start */
68eb44
+{
68eb44
+    unsigned char *s = *sp;
68eb44
+    *lenp = _libssh2_ntohu32(s);
68eb44
+
68eb44
+    /* the length of the string must fit within the current pointer and the
68eb44
+       end of the packet */
68eb44
+    if (*lenp > (data_len - (s - data) -4))
68eb44
+        return 1;
68eb44
+    *strp = s + 4;
68eb44
+    s += 4 + *lenp;
68eb44
+
68eb44
+    *sp = s;
68eb44
+    return 0;
68eb44
+}
68eb44
+
68eb44
 /* kex_agree_methods
68eb44
  * Decide which specific method to use of the methods offered by each party
68eb44
  */
68eb44
@@ -1568,38 +1592,23 @@ static int kex_agree_methods(LIBSSH2_SESSION * session, unsigned char *data,
68eb44
     s += 16;
68eb44
 
68eb44
     /* Locate each string */
68eb44
-    kex_len = _libssh2_ntohu32(s);
68eb44
-    kex = s + 4;
68eb44
-    s += 4 + kex_len;
68eb44
-    hostkey_len = _libssh2_ntohu32(s);
68eb44
-    hostkey = s + 4;
68eb44
-    s += 4 + hostkey_len;
68eb44
-    crypt_cs_len = _libssh2_ntohu32(s);
68eb44
-    crypt_cs = s + 4;
68eb44
-    s += 4 + crypt_cs_len;
68eb44
-    crypt_sc_len = _libssh2_ntohu32(s);
68eb44
-    crypt_sc = s + 4;
68eb44
-    s += 4 + crypt_sc_len;
68eb44
-    mac_cs_len = _libssh2_ntohu32(s);
68eb44
-    mac_cs = s + 4;
68eb44
-    s += 4 + mac_cs_len;
68eb44
-    mac_sc_len = _libssh2_ntohu32(s);
68eb44
-    mac_sc = s + 4;
68eb44
-    s += 4 + mac_sc_len;
68eb44
-    comp_cs_len = _libssh2_ntohu32(s);
68eb44
-    comp_cs = s + 4;
68eb44
-    s += 4 + comp_cs_len;
68eb44
-    comp_sc_len = _libssh2_ntohu32(s);
68eb44
-    comp_sc = s + 4;
68eb44
-#if 0
68eb44
-    s += 4 + comp_sc_len;
68eb44
-    lang_cs_len = _libssh2_ntohu32(s);
68eb44
-    lang_cs = s + 4;
68eb44
-    s += 4 + lang_cs_len;
68eb44
-    lang_sc_len = _libssh2_ntohu32(s);
68eb44
-    lang_sc = s + 4;
68eb44
-    s += 4 + lang_sc_len;
68eb44
-#endif
68eb44
+    if(kex_string_pair(&s, data, data_len, &kex_len, &kex))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &hostkey_len, &hostkey))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &crypt_cs_len, &crypt_cs))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &crypt_sc_len, &crypt_sc))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &mac_cs_len, &mac_cs))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &mac_sc_len, &mac_sc))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &comp_cs_len, &comp_cs))
68eb44
+       return -1;
68eb44
+    if(kex_string_pair(&s, data, data_len, &comp_sc_len, &comp_sc))
68eb44
+       return -1;
68eb44
+
68eb44
     /* If the server sent an optimistic packet, assume that it guessed wrong.
68eb44
      * If the guess is determined to be right (by kex_agree_kex_hostkey)
68eb44
      * This flag will be reset to zero so that it's not ignored */
68eb44
-- 
68eb44
2.4.2
68eb44