Blame SOURCES/0034-curl-7.61.1-CVE-2021-22946.patch

327345
From 03ca8c6faca7de6628f9cbec3001ec6466c88d07 Mon Sep 17 00:00:00 2001
327345
From: Patrick Monnerat <patrick@monnerat.net>
327345
Date: Wed, 8 Sep 2021 11:56:22 +0200
327345
Subject: [PATCH] ftp,imap,pop3: do not ignore --ssl-reqd
327345
327345
In imap and pop3, check if TLS is required even when capabilities
327345
request has failed.
327345
327345
In ftp, ignore preauthentication (230 status of server greeting) if TLS
327345
is required.
327345
327345
Bug: https://curl.se/docs/CVE-2021-22946.html
327345
327345
CVE-2021-22946
327345
327345
Upstream-commit: 364f174724ef115c63d5e5dc1d3342c8a43b1cca
327345
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
327345
---
327345
 lib/ftp.c               |  9 ++++---
327345
 lib/imap.c              | 24 ++++++++----------
327345
 lib/pop3.c              | 33 +++++++++++-------------
327345
 tests/data/Makefile.inc |  2 ++
327345
 tests/data/test984      | 56 +++++++++++++++++++++++++++++++++++++++++
327345
 tests/data/test985      | 54 +++++++++++++++++++++++++++++++++++++++
327345
 tests/data/test986      | 53 ++++++++++++++++++++++++++++++++++++++
327345
 7 files changed, 195 insertions(+), 36 deletions(-)
327345
 create mode 100644 tests/data/test984
327345
 create mode 100644 tests/data/test985
327345
 create mode 100644 tests/data/test986
327345
327345
diff --git a/lib/ftp.c b/lib/ftp.c
327345
index 71c9642..30ebeaa 100644
327345
--- a/lib/ftp.c
327345
+++ b/lib/ftp.c
327345
@@ -2621,9 +2621,12 @@ static CURLcode ftp_statemach_act(struct connectdata *conn)
327345
     /* we have now received a full FTP server response */
327345
     switch(ftpc->state) {
327345
     case FTP_WAIT220:
327345
-      if(ftpcode == 230)
327345
-        /* 230 User logged in - already! */
327345
-        return ftp_state_user_resp(conn, ftpcode, ftpc->state);
327345
+      if(ftpcode == 230) {
327345
+        /* 230 User logged in - already! Take as 220 if TLS required. */
327345
+        if(data->set.use_ssl <= CURLUSESSL_TRY ||
327345
+           conn->ssl[FIRSTSOCKET].use)
327345
+          return ftp_state_user_resp(conn, ftpcode, ftpc->state);
327345
+      }
327345
       else if(ftpcode != 220) {
327345
         failf(data, "Got a %03d ftp-server response when 220 was expected",
327345
               ftpcode);
327345
diff --git a/lib/imap.c b/lib/imap.c
327345
index bda23a5..7e159d4 100644
327345
--- a/lib/imap.c
327345
+++ b/lib/imap.c
327345
@@ -910,22 +910,18 @@ static CURLcode imap_state_capability_resp(struct connectdata *conn,
327345
       line += wordlen;
327345
     }
327345
   }
327345
-  else if(imapcode == IMAP_RESP_OK) {
327345
-    if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
327345
-      /* We don't have a SSL/TLS connection yet, but SSL is requested */
327345
-      if(imapc->tls_supported)
327345
-        /* Switch to TLS connection now */
327345
-        result = imap_perform_starttls(conn);
327345
-      else if(data->set.use_ssl == CURLUSESSL_TRY)
327345
-        /* Fallback and carry on with authentication */
327345
-        result = imap_perform_authentication(conn);
327345
-      else {
327345
-        failf(data, "STARTTLS not supported.");
327345
-        result = CURLE_USE_SSL_FAILED;
327345
-      }
327345
+  else if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
327345
+    /* PREAUTH is not compatible with STARTTLS. */
327345
+    if(imapcode == IMAP_RESP_OK && imapc->tls_supported && !imapc->preauth) {
327345
+      /* Switch to TLS connection now */
327345
+      result = imap_perform_starttls(conn);
327345
     }
327345
-    else
327345
+    else if(data->set.use_ssl <= CURLUSESSL_TRY)
327345
       result = imap_perform_authentication(conn);
327345
+    else {
327345
+      failf(data, "STARTTLS not available.");
327345
+      result = CURLE_USE_SSL_FAILED;
327345
+    }
327345
   }
327345
   else
327345
     result = imap_perform_authentication(conn);
327345
diff --git a/lib/pop3.c b/lib/pop3.c
327345
index 04cc887..3e916ce 100644
327345
--- a/lib/pop3.c
327345
+++ b/lib/pop3.c
327345
@@ -718,28 +718,23 @@ static CURLcode pop3_state_capa_resp(struct connectdata *conn, int pop3code,
327345
       }
327345
     }
327345
   }
327345
-  else if(pop3code == '+') {
327345
-    if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
327345
-      /* We don't have a SSL/TLS connection yet, but SSL is requested */
327345
-      if(pop3c->tls_supported)
327345
-        /* Switch to TLS connection now */
327345
-        result = pop3_perform_starttls(conn);
327345
-      else if(data->set.use_ssl == CURLUSESSL_TRY)
327345
-        /* Fallback and carry on with authentication */
327345
-        result = pop3_perform_authentication(conn);
327345
-      else {
327345
-        failf(data, "STLS not supported.");
327345
-        result = CURLE_USE_SSL_FAILED;
327345
-      }
327345
-    }
327345
-    else
327345
-      result = pop3_perform_authentication(conn);
327345
-  }
327345
   else {
327345
     /* Clear text is supported when CAPA isn't recognised */
327345
-    pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
327345
+    if(pop3code != '+')
327345
+      pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
327345
 
327345
-    result = pop3_perform_authentication(conn);
327345
+    if(!data->set.use_ssl || conn->ssl[FIRSTSOCKET].use)
327345
+      result = pop3_perform_authentication(conn);
327345
+    else if(pop3code == '+' && pop3c->tls_supported)
327345
+      /* Switch to TLS connection now */
327345
+      result = pop3_perform_starttls(conn);
327345
+    else if(data->set.use_ssl <= CURLUSESSL_TRY)
327345
+      /* Fallback and carry on with authentication */
327345
+      result = pop3_perform_authentication(conn);
327345
+    else {
327345
+      failf(data, "STLS not supported.");
327345
+      result = CURLE_USE_SSL_FAILED;
327345
+    }
327345
   }
327345
 
327345
   return result;
327345
diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc
327345
index ef9252b..1ba482b 100644
327345
--- a/tests/data/Makefile.inc
327345
+++ b/tests/data/Makefile.inc
327345
@@ -108,6 +108,8 @@ test927 test928 test929 test930 test931 test932 test933 test934 test935 \
327345
 test936 test937 test938 test939 test940 test941 test942 test943 test944 \
327345
 test945 test946 test947 test948 test949 test950 test951 test952 \
327345
 \
327345
+test984 test985 test986 \
327345
+\
327345
 test1000 test1001 test1002 test1003 test1004 test1005 test1006 test1007 \
327345
 test1008 test1009 test1010 test1011 test1012 test1013 test1014 test1015 \
327345
 test1016 test1017 test1018 test1019 test1020 test1021 test1022 test1023 \
327345
diff --git a/tests/data/test984 b/tests/data/test984
327345
new file mode 100644
327345
index 0000000..e573f23
327345
--- /dev/null
327345
+++ b/tests/data/test984
327345
@@ -0,0 +1,56 @@
327345
+<testcase>
327345
+<info>
327345
+<keywords>
327345
+IMAP
327345
+STARTTLS
327345
+</keywords>
327345
+</info>
327345
+
327345
+#
327345
+# Server-side
327345
+<reply>
327345
+<servercmd>
327345
+REPLY CAPABILITY A001 BAD Not implemented
327345
+</servercmd>
327345
+</reply>
327345
+
327345
+#
327345
+# Client-side
327345
+<client>
327345
+<features>
327345
+SSL
327345
+</features>
327345
+<server>
327345
+imap
327345
+</server>
327345
+ <name>
327345
+IMAP require STARTTLS with failing capabilities
327345
+ </name>
327345
+ <command>
327345
+imap://%HOSTIP:%IMAPPORT/%TESTNUMBER -T log/upload%TESTNUMBER -u user:secret --ssl-reqd
327345
+</command>
327345
+<file name="log/upload%TESTNUMBER">
327345
+Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
327345
+From: Fred Foobar <foobar@example.COM>
327345
+Subject: afternoon meeting
327345
+To: joe@example.com
327345
+Message-Id: <B27397-0100000@example.COM>
327345
+MIME-Version: 1.0
327345
+Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
327345
+
327345
+Hello Joe, do you think we can meet at 3:30 tomorrow?
327345
+</file>
327345
+</client>
327345
+
327345
+#
327345
+# Verify data after the test has been "shot"
327345
+<verify>
327345
+# 64 is CURLE_USE_SSL_FAILED
327345
+<errorcode>
327345
+64
327345
+</errorcode>
327345
+<protocol>
327345
+A001 CAPABILITY
327345
+</protocol>
327345
+</verify>
327345
+</testcase>
327345
diff --git a/tests/data/test985 b/tests/data/test985
327345
new file mode 100644
327345
index 0000000..d0db4aa
327345
--- /dev/null
327345
+++ b/tests/data/test985
327345
@@ -0,0 +1,54 @@
327345
+<testcase>
327345
+<info>
327345
+<keywords>
327345
+POP3
327345
+STARTTLS
327345
+</keywords>
327345
+</info>
327345
+
327345
+#
327345
+# Server-side
327345
+<reply>
327345
+<servercmd>
327345
+REPLY CAPA -ERR Not implemented
327345
+</servercmd>
327345
+<data nocheck="yes">
327345
+From: me@somewhere
327345
+To: fake@nowhere
327345
+
327345
+body
327345
+
327345
+--
327345
+  yours sincerely
327345
+</data>
327345
+</reply>
327345
+
327345
+#
327345
+# Client-side
327345
+<client>
327345
+<features>
327345
+SSL
327345
+</features>
327345
+<server>
327345
+pop3
327345
+</server>
327345
+ <name>
327345
+POP3 require STARTTLS with failing capabilities
327345
+ </name>
327345
+ <command>
327345
+pop3://%HOSTIP:%POP3PORT/%TESTNUMBER -u user:secret --ssl-reqd
327345
+ </command>
327345
+</client>
327345
+
327345
+#
327345
+# Verify data after the test has been "shot"
327345
+<verify>
327345
+# 64 is CURLE_USE_SSL_FAILED
327345
+<errorcode>
327345
+64
327345
+</errorcode>
327345
+<protocol>
327345
+CAPA
327345
+</protocol>
327345
+</verify>
327345
+</testcase>
327345
diff --git a/tests/data/test986 b/tests/data/test986
327345
new file mode 100644
327345
index 0000000..a709437
327345
--- /dev/null
327345
+++ b/tests/data/test986
327345
@@ -0,0 +1,53 @@
327345
+<testcase>
327345
+<info>
327345
+<keywords>
327345
+FTP
327345
+STARTTLS
327345
+</keywords>
327345
+</info>
327345
+
327345
+#
327345
+# Server-side
327345
+<reply>
327345
+<servercmd>
327345
+REPLY welcome 230 Welcome
327345
+REPLY AUTH 500 unknown command
327345
+</servercmd>
327345
+</reply>
327345
+
327345
+# Client-side
327345
+<client>
327345
+<features>
327345
+SSL
327345
+</features>
327345
+<server>
327345
+ftp
327345
+</server>
327345
+ <name>
327345
+FTP require STARTTLS while preauthenticated
327345
+ </name>
327345
+<file name="log/test%TESTNUMBER.txt">
327345
+data
327345
+    to
327345
+      see
327345
+that FTPS
327345
+works
327345
+  so does it?
327345
+</file>
327345
+ <command>
327345
+--ssl-reqd --ftp-ssl-control ftp://%HOSTIP:%FTPPORT/%TESTNUMBER -T log/test%TESTNUMBER.txt -u user:secret
327345
+</command>
327345
+</client>
327345
+
327345
+# Verify data after the test has been "shot"
327345
+<verify>
327345
+# 64 is CURLE_USE_SSL_FAILED
327345
+<errorcode>
327345
+64
327345
+</errorcode>
327345
+<protocol>
327345
+AUTH SSL
327345
+AUTH TLS
327345
+</protocol>
327345
+</verify>
327345
+</testcase>
327345
-- 
327345
2.31.1
327345