08b2e2
From 2fbbf282e42ae476459f7efe68a88dcb63dcc43b Mon Sep 17 00:00:00 2001
08b2e2
From: Daniel Stenberg <daniel@haxx.se>
08b2e2
Date: Sat, 12 Jun 2021 18:25:15 +0200
08b2e2
Subject: [PATCH] telnet: fix option parser to not send uninitialized contents
08b2e2
08b2e2
CVE-2021-22925
08b2e2
08b2e2
Reported-by: Red Hat Product Security
08b2e2
Bug: https://curl.se/docs/CVE-2021-22925.html
08b2e2
08b2e2
Upstream-commit: 894f6ec730597eb243618d33cc84d71add8d6a8a
08b2e2
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
08b2e2
---
08b2e2
 lib/telnet.c | 17 +++++++++++------
08b2e2
 1 file changed, 11 insertions(+), 6 deletions(-)
08b2e2
08b2e2
diff --git a/lib/telnet.c b/lib/telnet.c
08b2e2
index ea6bc71..f8428b8 100644
08b2e2
--- a/lib/telnet.c
08b2e2
+++ b/lib/telnet.c
08b2e2
@@ -967,12 +967,17 @@ static void suboption(struct connectdata *conn)
08b2e2
         size_t tmplen = (strlen(v->data) + 1);
08b2e2
         /* Add the variable only if it fits */
08b2e2
         if(len + tmplen < (int)sizeof(temp)-6) {
08b2e2
-          if(sscanf(v->data, "%127[^,],%127s", varname, varval) == 2) {
08b2e2
-            snprintf((char *)&temp[len], sizeof(temp) - len,
08b2e2
-                     "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
08b2e2
-                     CURL_NEW_ENV_VALUE, varval);
08b2e2
-            len += tmplen;
08b2e2
-          }
08b2e2
+          int rv;
08b2e2
+          char sep[2] = "";
08b2e2
+          varval[0] = 0;
08b2e2
+          rv = sscanf(v->data, "%127[^,]%1[,]%127s", varname, sep, varval);
08b2e2
+          if(rv == 1)
08b2e2
+            len += snprintf((char *)&temp[len], sizeof(temp) - len,
08b2e2
+                            "%c%s", CURL_NEW_ENV_VAR, varname);
08b2e2
+          else if(rv >= 2)
08b2e2
+            len += snprintf((char *)&temp[len], sizeof(temp) - len,
08b2e2
+                            "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
08b2e2
+                            CURL_NEW_ENV_VALUE, varval);
08b2e2
         }
08b2e2
       }
08b2e2
       snprintf((char *)&temp[len], sizeof(temp) - len,
08b2e2
-- 
08b2e2
2.31.1
08b2e2