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

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