Blame SOURCES/0035-curl-7.61.1-CVE-2021-22947.patch

72211f
From a1ec463c8207bde97b3575d12e396e999a55a8d0 Mon Sep 17 00:00:00 2001
72211f
From: Patrick Monnerat <patrick@monnerat.net>
72211f
Date: Tue, 7 Sep 2021 13:26:42 +0200
72211f
Subject: [PATCH] ftp,imap,pop3,smtp: reject STARTTLS server response
72211f
 pipelining
72211f
72211f
If a server pipelines future responses within the STARTTLS response, the
72211f
former are preserved in the pingpong cache across TLS negotiation and
72211f
used as responses to the encrypted commands.
72211f
72211f
This fix detects pipelined STARTTLS responses and rejects them with an
72211f
error.
72211f
72211f
CVE-2021-22947
72211f
72211f
Bug: https://curl.se/docs/CVE-2021-22947.html
72211f
72211f
Upstream-commit: 8ef147c43646e91fdaad5d0e7b60351f842e5c68
72211f
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
72211f
---
72211f
 lib/ftp.c               |  3 +++
72211f
 lib/imap.c              |  4 +++
72211f
 lib/pop3.c              |  4 +++
72211f
 lib/smtp.c              |  4 +++
72211f
 tests/data/Makefile.inc |  2 +-
72211f
 tests/data/test980      | 52 ++++++++++++++++++++++++++++++++++++
72211f
 tests/data/test981      | 59 +++++++++++++++++++++++++++++++++++++++++
72211f
 tests/data/test982      | 57 +++++++++++++++++++++++++++++++++++++++
72211f
 tests/data/test983      | 52 ++++++++++++++++++++++++++++++++++++
72211f
 9 files changed, 236 insertions(+), 1 deletion(-)
72211f
 create mode 100644 tests/data/test980
72211f
 create mode 100644 tests/data/test981
72211f
 create mode 100644 tests/data/test982
72211f
 create mode 100644 tests/data/test983
72211f
72211f
diff --git a/lib/ftp.c b/lib/ftp.c
72211f
index 71f998e..e920138 100644
72211f
--- a/lib/ftp.c
72211f
+++ b/lib/ftp.c
72211f
@@ -2688,6 +2688,9 @@ static CURLcode ftp_statemach_act(struct connectdata *conn)
72211f
     case FTP_AUTH:
72211f
       /* we have gotten the response to a previous AUTH command */
72211f
 
72211f
+      if(pp->cache_size)
72211f
+        return CURLE_WEIRD_SERVER_REPLY; /* Forbid pipelining in response. */
72211f
+
72211f
       /* RFC2228 (page 5) says:
72211f
        *
72211f
        * If the server is willing to accept the named security mechanism,
72211f
diff --git a/lib/imap.c b/lib/imap.c
72211f
index feb7445..09bc5d6 100644
72211f
--- a/lib/imap.c
72211f
+++ b/lib/imap.c
72211f
@@ -939,6 +939,10 @@ static CURLcode imap_state_starttls_resp(struct connectdata *conn,
72211f
 
72211f
   (void)instate; /* no use for this yet */
72211f
 
72211f
+  /* Pipelining in response is forbidden. */
72211f
+  if(conn->proto.imapc.pp.cache_size)
72211f
+    return CURLE_WEIRD_SERVER_REPLY;
72211f
+
72211f
   if(imapcode != IMAP_RESP_OK) {
72211f
     if(data->set.use_ssl != CURLUSESSL_TRY) {
72211f
       failf(data, "STARTTLS denied");
72211f
diff --git a/lib/pop3.c b/lib/pop3.c
72211f
index 7698d1c..dccfced 100644
72211f
--- a/lib/pop3.c
72211f
+++ b/lib/pop3.c
72211f
@@ -750,6 +750,10 @@ static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
72211f
 
72211f
   (void)instate; /* no use for this yet */
72211f
 
72211f
+  /* Pipelining in response is forbidden. */
72211f
+  if(conn->proto.pop3c.pp.cache_size)
72211f
+    return CURLE_WEIRD_SERVER_REPLY;
72211f
+
72211f
   if(pop3code != '+') {
72211f
     if(data->set.use_ssl != CURLUSESSL_TRY) {
72211f
       failf(data, "STARTTLS denied");
72211f
diff --git a/lib/smtp.c b/lib/smtp.c
72211f
index 1defb25..1f89777 100644
72211f
--- a/lib/smtp.c
72211f
+++ b/lib/smtp.c
72211f
@@ -685,6 +685,10 @@ static CURLcode smtp_state_starttls_resp(struct connectdata *conn,
72211f
 
72211f
   (void)instate; /* no use for this yet */
72211f
 
72211f
+  /* Pipelining in response is forbidden. */
72211f
+  if(conn->proto.smtpc.pp.cache_size)
72211f
+    return CURLE_WEIRD_SERVER_REPLY;
72211f
+
72211f
   if(smtpcode != 220) {
72211f
     if(data->set.use_ssl != CURLUSESSL_TRY) {
72211f
       failf(data, "STARTTLS denied, code %d", smtpcode);
72211f
diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc
72211f
index 163ce59..42b0569 100644
72211f
--- a/tests/data/Makefile.inc
72211f
+++ b/tests/data/Makefile.inc
72211f
@@ -108,7 +108,7 @@ test927 test928 test929 test930 test931 test932 test933 test934 test935 \
72211f
 test936 test937 test938 test939 test940 test941 test942 test943 test944 \
72211f
 test945 test946 test947 test948 test949 test950 test951 test952 \
72211f
 \
72211f
-test984 test985 test986 \
72211f
+test980 test981 test982 test983 test984 test985 test986 \
72211f
 \
72211f
 test1000 test1001 test1002 test1003 test1004 test1005 test1006 test1007 \
72211f
 test1008 test1009 test1010 test1011 test1012 test1013 test1014 test1015 \
72211f
diff --git a/tests/data/test980 b/tests/data/test980
72211f
new file mode 100644
72211f
index 0000000..97567f8
72211f
--- /dev/null
72211f
+++ b/tests/data/test980
72211f
@@ -0,0 +1,52 @@
72211f
+<testcase>
72211f
+<info>
72211f
+<keywords>
72211f
+SMTP
72211f
+STARTTLS
72211f
+</keywords>
72211f
+</info>
72211f
+
72211f
+#
72211f
+# Server-side
72211f
+<reply>
72211f
+<servercmd>
72211f
+CAPA STARTTLS
72211f
+AUTH PLAIN
72211f
+REPLY STARTTLS 454 currently unavailable\r\n235 Authenticated\r\n250 2.1.0 Sender ok\r\n250 2.1.5 Recipient ok\r\n354 Enter mail\r\n250 2.0.0 Accepted
72211f
+REPLY AUTH 535 5.7.8 Authentication credentials invalid
72211f
+</servercmd>
72211f
+</reply>
72211f
+
72211f
+#
72211f
+# Client-side
72211f
+<client>
72211f
+<features>
72211f
+SSL
72211f
+</features>
72211f
+<server>
72211f
+smtp
72211f
+</server>
72211f
+ <name>
72211f
+SMTP STARTTLS pipelined server response
72211f
+ </name>
72211f
+<stdin>
72211f
+mail body
72211f
+</stdin>
72211f
+ <command>
72211f
+smtp://%HOSTIP:%SMTPPORT/%TESTNUMBER --mail-rcpt recipient@example.com --mail-from sender@example.com -u user:secret --ssl --sasl-ir -T -
72211f
+</command>
72211f
+</client>
72211f
+
72211f
+#
72211f
+# Verify data after the test has been "shot"
72211f
+<verify>
72211f
+# 8 is CURLE_WEIRD_SERVER_REPLY
72211f
+<errorcode>
72211f
+8
72211f
+</errorcode>
72211f
+<protocol>
72211f
+EHLO %TESTNUMBER
72211f
+STARTTLS
72211f
+</protocol>
72211f
+</verify>
72211f
+</testcase>
72211f
diff --git a/tests/data/test981 b/tests/data/test981
72211f
new file mode 100644
72211f
index 0000000..2b98ce4
72211f
--- /dev/null
72211f
+++ b/tests/data/test981
72211f
@@ -0,0 +1,59 @@
72211f
+<testcase>
72211f
+<info>
72211f
+<keywords>
72211f
+IMAP
72211f
+STARTTLS
72211f
+</keywords>
72211f
+</info>
72211f
+
72211f
+#
72211f
+# Server-side
72211f
+<reply>
72211f
+<servercmd>
72211f
+CAPA STARTTLS
72211f
+REPLY STARTTLS A002 BAD currently unavailable\r\nA003 OK Authenticated\r\nA004 OK Accepted
72211f
+REPLY LOGIN A003 BAD Authentication credentials invalid
72211f
+</servercmd>
72211f
+</reply>
72211f
+
72211f
+#
72211f
+# Client-side
72211f
+<client>
72211f
+<features>
72211f
+SSL
72211f
+</features>
72211f
+<server>
72211f
+imap
72211f
+</server>
72211f
+ <name>
72211f
+IMAP STARTTLS pipelined server response
72211f
+ </name>
72211f
+ <command>
72211f
+imap://%HOSTIP:%IMAPPORT/%TESTNUMBER -T log/upload%TESTNUMBER -u user:secret --ssl
72211f
+</command>
72211f
+<file name="log/upload%TESTNUMBER">
72211f
+Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
72211f
+From: Fred Foobar <foobar@example.COM>
72211f
+Subject: afternoon meeting
72211f
+To: joe@example.com
72211f
+Message-Id: <B27397-0100000@example.COM>
72211f
+MIME-Version: 1.0
72211f
+Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
72211f
+
72211f
+Hello Joe, do you think we can meet at 3:30 tomorrow?
72211f
+</file>
72211f
+</client>
72211f
+
72211f
+#
72211f
+# Verify data after the test has been "shot"
72211f
+<verify>
72211f
+# 8 is CURLE_WEIRD_SERVER_REPLY
72211f
+<errorcode>
72211f
+8
72211f
+</errorcode>
72211f
+<protocol>
72211f
+A001 CAPABILITY
72211f
+A002 STARTTLS
72211f
+</protocol>
72211f
+</verify>
72211f
+</testcase>
72211f
diff --git a/tests/data/test982 b/tests/data/test982
72211f
new file mode 100644
72211f
index 0000000..9e07cc0
72211f
--- /dev/null
72211f
+++ b/tests/data/test982
72211f
@@ -0,0 +1,57 @@
72211f
+<testcase>
72211f
+<info>
72211f
+<keywords>
72211f
+POP3
72211f
+STARTTLS
72211f
+</keywords>
72211f
+</info>
72211f
+
72211f
+#
72211f
+# Server-side
72211f
+<reply>
72211f
+<servercmd>
72211f
+CAPA STLS USER
72211f
+REPLY STLS -ERR currently unavailable\r\n+OK user accepted\r\n+OK authenticated
72211f
+REPLY PASS -ERR Authentication credentials invalid
72211f
+</servercmd>
72211f
+<data nocheck="yes">
72211f
+From: me@somewhere
72211f
+To: fake@nowhere
72211f
+
72211f
+body
72211f
+
72211f
+--
72211f
+  yours sincerely
72211f
+</data>
72211f
+</reply>
72211f
+
72211f
+#
72211f
+# Client-side
72211f
+<client>
72211f
+<features>
72211f
+SSL
72211f
+</features>
72211f
+<server>
72211f
+pop3
72211f
+</server>
72211f
+ <name>
72211f
+POP3 STARTTLS pipelined server response
72211f
+ </name>
72211f
+ <command>
72211f
+pop3://%HOSTIP:%POP3PORT/%TESTNUMBER -u user:secret --ssl
72211f
+ </command>
72211f
+</client>
72211f
+
72211f
+#
72211f
+# Verify data after the test has been "shot"
72211f
+<verify>
72211f
+# 8 is CURLE_WEIRD_SERVER_REPLY
72211f
+<errorcode>
72211f
+8
72211f
+</errorcode>
72211f
+<protocol>
72211f
+CAPA
72211f
+STLS
72211f
+</protocol>
72211f
+</verify>
72211f
+</testcase>
72211f
diff --git a/tests/data/test983 b/tests/data/test983
72211f
new file mode 100644
72211f
index 0000000..300ec45
72211f
--- /dev/null
72211f
+++ b/tests/data/test983
72211f
@@ -0,0 +1,52 @@
72211f
+<testcase>
72211f
+<info>
72211f
+<keywords>
72211f
+FTP
72211f
+STARTTLS
72211f
+</keywords>
72211f
+</info>
72211f
+
72211f
+#
72211f
+# Server-side
72211f
+<reply>
72211f
+<servercmd>
72211f
+REPLY AUTH 500 unknown command\r\n500 unknown command\r\n331 give password\r\n230 Authenticated\r\n257 "/"\r\n200 OK\r\n200 OK\r\n200 OK\r\n226 Transfer complete
72211f
+REPLY PASS 530 Login incorrect
72211f
+</servercmd>
72211f
+</reply>
72211f
+
72211f
+# Client-side
72211f
+<client>
72211f
+<features>
72211f
+SSL
72211f
+</features>
72211f
+<server>
72211f
+ftp
72211f
+</server>
72211f
+ <name>
72211f
+FTP STARTTLS pipelined server response
72211f
+ </name>
72211f
+<file name="log/test%TESTNUMBER.txt">
72211f
+data
72211f
+    to
72211f
+      see
72211f
+that FTPS
72211f
+works
72211f
+  so does it?
72211f
+</file>
72211f
+ <command>
72211f
+--ssl --ftp-ssl-control ftp://%HOSTIP:%FTPPORT/%TESTNUMBER -T log/test%TESTNUMBER.txt -u user:secret -P %CLIENTIP
72211f
+</command>
72211f
+</client>
72211f
+
72211f
+# Verify data after the test has been "shot"
72211f
+<verify>
72211f
+# 8 is CURLE_WEIRD_SERVER_REPLY
72211f
+<errorcode>
72211f
+8
72211f
+</errorcode>
72211f
+<protocol>
72211f
+AUTH SSL
72211f
+</protocol>
72211f
+</verify>
72211f
+</testcase>
72211f
-- 
72211f
2.31.1
72211f