5017ec
From e8705acd69383c13191c9dd4867d5118e58c54ba Mon Sep 17 00:00:00 2001
5017ec
From: Daniel Stenberg <daniel@haxx.se>
5017ec
Date: Thu, 6 Oct 2022 00:49:10 +0200
5017ec
Subject: [PATCH 1/2] strcase: add Curl_timestrcmp
5017ec
5017ec
This is a strcmp() alternative function for comparing "secrets",
5017ec
designed to take the same time no matter the content to not leak
5017ec
match/non-match info to observers based on how fast it is.
5017ec
5017ec
The time this function takes is only a function of the shortest input
5017ec
string.
5017ec
5017ec
Reported-by: Trail of Bits
5017ec
5017ec
Closes #9658
5017ec
5017ec
Upstream-commit: ed5095ed94281989e103c72e032200b83be37878
5017ec
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
5017ec
---
5017ec
 lib/strcase.c | 22 ++++++++++++++++++++++
5017ec
 lib/strcase.h |  1 +
5017ec
 2 files changed, 23 insertions(+)
5017ec
5017ec
diff --git a/lib/strcase.c b/lib/strcase.c
5017ec
index f932485..c73907d 100644
5017ec
--- a/lib/strcase.c
5017ec
+++ b/lib/strcase.c
5017ec
@@ -175,6 +175,28 @@ bool Curl_safecmp(char *a, char *b)
5017ec
   return !a && !b;
5017ec
 }
5017ec
 
5017ec
+/*
5017ec
+ * Curl_timestrcmp() returns 0 if the two strings are identical. The time this
5017ec
+ * function spends is a function of the shortest string, not of the contents.
5017ec
+ */
5017ec
+int Curl_timestrcmp(const char *a, const char *b)
5017ec
+{
5017ec
+  int match = 0;
5017ec
+  int i = 0;
5017ec
+
5017ec
+  if(a && b) {
5017ec
+    while(1) {
5017ec
+      match |= a[i]^b[i];
5017ec
+      if(!a[i] || !b[i])
5017ec
+        break;
5017ec
+      i++;
5017ec
+    }
5017ec
+  }
5017ec
+  else
5017ec
+    return a || b;
5017ec
+  return match;
5017ec
+}
5017ec
+
5017ec
 /* --- public functions --- */
5017ec
 
5017ec
 int curl_strequal(const char *first, const char *second)
5017ec
diff --git a/lib/strcase.h b/lib/strcase.h
5017ec
index d245929..11a67a1 100644
5017ec
--- a/lib/strcase.h
5017ec
+++ b/lib/strcase.h
5017ec
@@ -48,5 +48,6 @@ char Curl_raw_toupper(char in);
5017ec
 void Curl_strntoupper(char *dest, const char *src, size_t n);
5017ec
 
5017ec
 bool Curl_safecmp(char *a, char *b);
5017ec
+int Curl_timestrcmp(const char *first, const char *second);
5017ec
 
5017ec
 #endif /* HEADER_CURL_STRCASE_H */
5017ec
-- 
5017ec
2.39.2
5017ec
5017ec
5017ec
From 9cfaea212ff347937a38f6b5d6b885ed8ba1b931 Mon Sep 17 00:00:00 2001
5017ec
From: Daniel Stenberg <daniel@haxx.se>
5017ec
Date: Thu, 9 Mar 2023 17:47:06 +0100
5017ec
Subject: [PATCH 2/2] ftp: add more conditions for connection reuse
5017ec
5017ec
Reported-by: Harry Sintonen
5017ec
Closes #10730
5017ec
5017ec
Upstream-commit: 8f4608468b890dce2dad9f91d5607ee7e9c1aba1
5017ec
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
5017ec
---
5017ec
 lib/ftp.c     | 28 ++++++++++++++++++++++++++--
5017ec
 lib/ftp.h     |  5 +++++
5017ec
 lib/setopt.c  |  2 +-
5017ec
 lib/url.c     | 13 ++++++++++++-
5017ec
 lib/urldata.h |  4 ++--
5017ec
 5 files changed, 46 insertions(+), 6 deletions(-)
5017ec
5017ec
diff --git a/lib/ftp.c b/lib/ftp.c
5017ec
index 9442832..df15bc0 100644
5017ec
--- a/lib/ftp.c
5017ec
+++ b/lib/ftp.c
5017ec
@@ -4080,6 +4080,8 @@ static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection)
5017ec
   }
5017ec
 
5017ec
   freedirs(ftpc);
5017ec
+  Curl_safefree(ftpc->account);
5017ec
+  Curl_safefree(ftpc->alternative_to_user);
5017ec
   free(ftpc->prevpath);
5017ec
   ftpc->prevpath = NULL;
5017ec
   free(ftpc->server_os);
5017ec
@@ -4391,11 +4393,31 @@ static CURLcode ftp_setup_connection(struct connectdata *conn)
5017ec
   struct Curl_easy *data = conn->data;
5017ec
   char *type;
5017ec
   struct FTP *ftp;
5017ec
+  struct ftp_conn *ftpc = &conn->proto.ftpc;
5017ec
 
5017ec
-  conn->data->req.protop = ftp = malloc(sizeof(struct FTP));
5017ec
+  ftp = calloc(sizeof(struct FTP), 1);
5017ec
   if(NULL == ftp)
5017ec
     return CURLE_OUT_OF_MEMORY;
5017ec
 
5017ec
+  /* clone connection related data that is FTP specific */
5017ec
+  if(data->set.str[STRING_FTP_ACCOUNT]) {
5017ec
+    ftpc->account = strdup(data->set.str[STRING_FTP_ACCOUNT]);
5017ec
+    if(!ftpc->account) {
5017ec
+      free(ftp);
5017ec
+      return CURLE_OUT_OF_MEMORY;
5017ec
+    }
5017ec
+  }
5017ec
+  if(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]) {
5017ec
+    ftpc->alternative_to_user =
5017ec
+      strdup(data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]);
5017ec
+    if(!ftpc->alternative_to_user) {
5017ec
+      Curl_safefree(ftpc->account);
5017ec
+      free(ftp);
5017ec
+      return CURLE_OUT_OF_MEMORY;
5017ec
+    }
5017ec
+  }
5017ec
+  data->req.protop = ftp;
5017ec
+
5017ec
   data->state.path++;   /* don't include the initial slash */
5017ec
   data->state.slash_removed = TRUE; /* we've skipped the slash */
5017ec
 
5017ec
@@ -4445,7 +4467,9 @@ static CURLcode ftp_setup_connection(struct connectdata *conn)
5017ec
   if(isBadFtpString(ftp->passwd))
5017ec
     return CURLE_URL_MALFORMAT;
5017ec
 
5017ec
-  conn->proto.ftpc.known_filesize = -1; /* unknown size for now */
5017ec
+  ftpc->known_filesize = -1; /* unknown size for now */
5017ec
+  ftpc->use_ssl = data->set.use_ssl;
5017ec
+  ftpc->ccc = data->set.ftp_ccc;
5017ec
 
5017ec
   return CURLE_OK;
5017ec
 }
5017ec
diff --git a/lib/ftp.h b/lib/ftp.h
5017ec
index 7f6f432..3f33e27 100644
5017ec
--- a/lib/ftp.h
5017ec
+++ b/lib/ftp.h
5017ec
@@ -117,6 +117,8 @@ struct FTP {
5017ec
    struct */
5017ec
 struct ftp_conn {
5017ec
   struct pingpong pp;
5017ec
+  char *account;
5017ec
+  char *alternative_to_user;
5017ec
   char *entrypath; /* the PWD reply when we logged on */
5017ec
   char **dirs;   /* realloc()ed array for path components */
5017ec
   int dirdepth;  /* number of entries used in the 'dirs' array */
5017ec
@@ -144,6 +146,9 @@ struct ftp_conn {
5017ec
   ftpstate state; /* always use ftp.c:state() to change state! */
5017ec
   ftpstate state_saved; /* transfer type saved to be reloaded after
5017ec
                            data connection is established */
5017ec
+  unsigned char use_ssl;   /* if AUTH TLS is to be attempted etc, for FTP or
5017ec
+                              IMAP or POP3 or others! (type: curl_usessl)*/
5017ec
+  unsigned char ccc;       /* ccc level for this connection */
5017ec
   curl_off_t retr_size_saved; /* Size of retrieved file saved */
5017ec
   char *server_os;     /* The target server operating system. */
5017ec
   curl_off_t known_filesize; /* file size is different from -1, if wildcard
5017ec
diff --git a/lib/setopt.c b/lib/setopt.c
5017ec
index 3339a67..6fc111d 100644
5017ec
--- a/lib/setopt.c
5017ec
+++ b/lib/setopt.c
5017ec
@@ -2039,7 +2039,7 @@ CURLcode Curl_vsetopt(struct Curl_easy *data, CURLoption option,
5017ec
     arg = va_arg(param, long);
5017ec
     if((arg < CURLUSESSL_NONE) || (arg > CURLUSESSL_ALL))
5017ec
       return CURLE_BAD_FUNCTION_ARGUMENT;
5017ec
-    data->set.use_ssl = (curl_usessl)arg;
5017ec
+    data->set.use_ssl = (unsigned char)arg;
5017ec
     break;
5017ec
 
5017ec
   case CURLOPT_SSL_OPTIONS:
5017ec
diff --git a/lib/url.c b/lib/url.c
5017ec
index 61ba832..4e21838 100644
5017ec
--- a/lib/url.c
5017ec
+++ b/lib/url.c
5017ec
@@ -1309,7 +1309,18 @@ ConnectionExists(struct Curl_easy *data,
5017ec
         if(!ssh_config_matches(needle, check))
5017ec
           continue;
5017ec
       }
5017ec
-
5017ec
+#ifndef CURL_DISABLE_FTP
5017ec
+      if(needle->handler->protocol & (CURLPROTO_FTP|CURLPROTO_FTPS)) {
5017ec
+        /* Also match ACCOUNT, ALTERNATIVE-TO-USER, USE_SSL and CCC options */
5017ec
+        if(Curl_timestrcmp(needle->proto.ftpc.account,
5017ec
+                           check->proto.ftpc.account) ||
5017ec
+           Curl_timestrcmp(needle->proto.ftpc.alternative_to_user,
5017ec
+                           check->proto.ftpc.alternative_to_user) ||
5017ec
+           (needle->proto.ftpc.use_ssl != check->proto.ftpc.use_ssl) ||
5017ec
+           (needle->proto.ftpc.ccc != check->proto.ftpc.ccc))
5017ec
+          continue;
5017ec
+      }
5017ec
+#endif
5017ec
       if(!needle->bits.httpproxy || (needle->handler->flags&PROTOPT_SSL) ||
5017ec
          needle->bits.tunnel_proxy) {
5017ec
         /* The requested connection does not use a HTTP proxy or it uses SSL or
5017ec
diff --git a/lib/urldata.h b/lib/urldata.h
5017ec
index 9d9ca92..4e2f5b9 100644
5017ec
--- a/lib/urldata.h
5017ec
+++ b/lib/urldata.h
5017ec
@@ -1498,6 +1498,8 @@ struct UserDefined {
5017ec
   curl_write_callback fwrite_header; /* function that stores headers */
5017ec
   curl_write_callback fwrite_rtp;    /* function that stores interleaved RTP */
5017ec
   curl_read_callback fread_func_set; /* function that reads the input */
5017ec
+  unsigned char use_ssl;   /* if AUTH TLS is to be attempted etc, for FTP or
5017ec
+                              IMAP or POP3 or others! (type: curl_usessl)*/
5017ec
   int is_fread_set; /* boolean, has read callback been set to non-NULL? */
5017ec
   int is_fwrite_set; /* boolean, has write callback been set to non-NULL? */
5017ec
   curl_progress_callback fprogress; /* OLD and deprecated progress callback  */
5017ec
@@ -1622,8 +1624,6 @@ struct UserDefined {
5017ec
   bool ftp_use_eprt;     /* if EPRT is to be attempted or not */
5017ec
   bool ftp_use_pret;     /* if PRET is to be used before PASV or not */
5017ec
 
5017ec
-  curl_usessl use_ssl;   /* if AUTH TLS is to be attempted etc, for FTP or
5017ec
-                            IMAP or POP3 or others! */
5017ec
   curl_ftpauth ftpsslauth; /* what AUTH XXX to be attempted */
5017ec
   curl_ftpccc ftp_ccc;   /* FTP CCC options */
5017ec
   bool no_signal;        /* do not use any signal/alarm handler */
5017ec
-- 
5017ec
2.39.2
5017ec