Blame SOURCES/0001-Add-new-filename-generation-algorithm-for-STOU-comma.patch

993209
From ec60588d36f06acdd6a1b3c5959d2102f4f5b230 Mon Sep 17 00:00:00 2001
993209
From: =?UTF-8?q?Ond=C5=99ej=20Lyson=C4=9Bk?= <olysonek@redhat.com>
993209
Date: Tue, 6 Feb 2018 13:30:44 +0100
993209
Subject: [PATCH] Add new filename generation algorithm for STOU command
993209
993209
A new configuration option 'better_stou' can be used to enable
993209
a better algorithm for generating unique filenames.
993209
993209
Resolves: rhbz#1479237
993209
---
993209
 parseconf.c   |   1 +
993209
 postlogin.c   | 177 +++++++++++++++++++++++++++++++++++++++++++++++++---------
993209
 sysutil.c     |   3 +
993209
 sysutil.h     |   3 +-
993209
 tunables.c    |   2 +
993209
 tunables.h    |   3 +
993209
 vsftpd.conf.5 |  10 ++++
993209
 7 files changed, 172 insertions(+), 27 deletions(-)
993209
993209
diff --git a/parseconf.c b/parseconf.c
993209
index 33a1349..47b54f1 100644
993209
--- a/parseconf.c
993209
+++ b/parseconf.c
993209
@@ -111,6 +111,7 @@ parseconf_bool_array[] =
993209
   { "http_enable", &tunable_http_enable },
993209
   { "seccomp_sandbox", &tunable_seccomp_sandbox },
993209
   { "allow_writeable_chroot", &tunable_allow_writeable_chroot },
993209
+  { "better_stou", &tunable_better_stou },
993209
   { 0, 0 }
993209
 };
993209
 
993209
diff --git a/postlogin.c b/postlogin.c
993209
index 869c2a2..3b50af2 100644
993209
--- a/postlogin.c
993209
+++ b/postlogin.c
993209
@@ -29,6 +29,7 @@
993209
 #include "opts.h"
993209
 
993209
 #include <errno.h>
993209
+#include <stdio.h>
993209
 
993209
 /* Private local functions */
993209
 static void handle_pwd(struct vsf_session* p_sess);
993209
@@ -1021,6 +1022,115 @@ handle_stor(struct vsf_session* p_sess)
993209
   handle_upload_common(p_sess, 0, 0);
993209
 }
993209
 
993209
+/* Based on __gen_tempname() from glibc - thanks, glibc! Relicensed
993209
+ * from LGPL2.1+ to GPL2.
993209
+ */
993209
+static int
993209
+create_unique_file(struct vsf_session* p_sess, struct mystr* p_outstr,
993209
+                   const struct mystr* p_base_str,
993209
+                   int (*access_checker)(const struct mystr*))
993209
+{
993209
+  struct mystr s_result = INIT_MYSTR;
993209
+  const int suffix_len = 6;
993209
+  unsigned int count;
993209
+  static unsigned long long int value;
993209
+  unsigned long long int random_time_bits;
993209
+  int fd = -1;
993209
+  /* These are the characters used in temporary file names.  */
993209
+  struct mystr s_letters = INIT_MYSTR;
993209
+  unsigned int s_letters_len;
993209
+  int base_len;
993209
+
993209
+  str_alloc_text(&s_letters,
993209
+      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
993209
+  s_letters_len = str_getlen(&s_letters);
993209
+
993209
+  /* A lower bound on the number of temporary files to attempt to
993209
+     generate.  The maximum total number of temporary file names that
993209
+     can exist for a given template is 62**6.  It should never be
993209
+     necessary to try all of these combinations.  Instead if a reasonable
993209
+     number of names is tried (we define reasonable as 62**3) fail to
993209
+     give the system administrator the chance to remove the problems.  */
993209
+#define ATTEMPTS_MIN (62 * 62 * 62)
993209
+
993209
+  /* The number of times to attempt to generate a temporary file. */
993209
+#if ATTEMPTS_MIN < TMP_MAX
993209
+  unsigned int attempts = TMP_MAX;
993209
+#else
993209
+  unsigned int attempts = ATTEMPTS_MIN;
993209
+#endif
993209
+#undef ATTEMPTS_MIN
993209
+
993209
+  {
993209
+    long sec = vsf_sysutil_get_time_sec();
993209
+    long usec = vsf_sysutil_get_time_usec();
993209
+    random_time_bits = ((unsigned long long int) usec << 16) ^ sec;
993209
+    value += random_time_bits ^ vsf_sysutil_getpid();
993209
+  }
993209
+
993209
+  if (str_isempty(p_base_str))
993209
+  {
993209
+    const char *base = "STOU.";
993209
+    base_len = vsf_sysutil_strlen(base);
993209
+    str_reserve(&s_result, base_len + suffix_len);
993209
+    str_alloc_text(&s_result, base);
993209
+  }
993209
+  else
993209
+  {
993209
+    str_reserve(&s_result, str_getlen(p_base_str) + suffix_len + 1);
993209
+    str_copy(&s_result, p_base_str);
993209
+    str_append_char(&s_result, '.');
993209
+    base_len = str_getlen(&s_result);
993209
+  }
993209
+
993209
+  for (count = 0; count < attempts; value += 7777, ++count)
993209
+  {
993209
+    unsigned long long v = value;
993209
+    int i;
993209
+    str_trunc(&s_result, base_len);
993209
+    for (i = 0; i < suffix_len; ++i)
993209
+    {
993209
+      char c;
993209
+      c = str_get_char_at(&s_letters, v % s_letters_len);
993209
+      v /= s_letters_len;
993209
+      str_append_char(&s_result, c);
993209
+    }
993209
+    if (!access_checker(&s_result))
993209
+    {
993209
+      /* If we generate a filename which is not allowed, we fail immediatelly,
993209
+       * without trying any other possibilities. This is to prevent attackers
993209
+       * from keeping us busy.
993209
+       */
993209
+      vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
993209
+      break;
993209
+    }
993209
+    fd = str_create_exclusive(&s_result);
993209
+    if (vsf_sysutil_retval_is_error(fd))
993209
+    {
993209
+      if (kVSFSysUtilErrEXIST == vsf_sysutil_get_error())
993209
+      {
993209
+        continue;
993209
+      }
993209
+      else
993209
+      {
993209
+        vsf_cmdio_write(p_sess, FTP_UPLOADFAIL, "Could not create file.");
993209
+        break;
993209
+      }
993209
+    }
993209
+    else
993209
+    {
993209
+      break;
993209
+    }
993209
+  }
993209
+  if (!vsf_sysutil_retval_is_error(fd))
993209
+  {
993209
+    str_copy(p_outstr, &s_result);
993209
+  }
993209
+  str_free(&s_letters);
993209
+  str_free(&s_result);
993209
+  return fd;
993209
+}
993209
+
993209
 static void
993209
 handle_upload_common(struct vsf_session* p_sess, int is_append, int is_unique)
993209
 {
993209
@@ -1042,41 +1152,56 @@ handle_upload_common(struct vsf_session* p_sess, int is_append, int is_unique)
993209
     return;
993209
   }
993209
   resolve_tilde(&p_sess->ftp_arg_str, p_sess);
993209
-  p_filename = &p_sess->ftp_arg_str;
993209
-  if (is_unique)
993209
-  {
993209
-    get_unique_filename(&s_filename, p_filename);
993209
-    p_filename = &s_filename;
993209
-  }
993209
   vsf_log_start_entry(p_sess, kVSFLogEntryUpload);
993209
   str_copy(&p_sess->log_str, &p_sess->ftp_arg_str);
993209
   prepend_path_to_filename(&p_sess->log_str);
993209
-  if (!vsf_access_check_file(p_filename))
993209
-  {
993209
-    vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
993209
-    return;
993209
-  }
993209
-  /* NOTE - actual file permissions will be governed by the tunable umask */
993209
-  /* XXX - do we care about race between create and chown() of anonymous
993209
-   * upload?
993209
-   */
993209
-  if (is_unique || (p_sess->is_anonymous && !tunable_anon_other_write_enable))
993209
+  p_filename = &p_sess->ftp_arg_str;
993209
+  if (is_unique && tunable_better_stou)
993209
   {
993209
-    new_file_fd = str_create_exclusive(p_filename);
993209
+    new_file_fd = create_unique_file(p_sess, &s_filename, p_filename,
993209
+                                     vsf_access_check_file);
993209
+    if (vsf_sysutil_retval_is_error(new_file_fd))
993209
+    {
993209
+      return;
993209
+    }
993209
+    p_filename = &s_filename;
993209
   }
993209
   else
993209
   {
993209
-    /* For non-anonymous, allow open() to overwrite or append existing files */
993209
-    new_file_fd = str_create(p_filename);
993209
-    if (!is_append && offset == 0)
993209
+    if (is_unique)
993209
     {
993209
-      do_truncate = 1;
993209
+      get_unique_filename(&s_filename, p_filename);
993209
+      p_filename = &s_filename;
993209
+    }
993209
+    if (!vsf_access_check_file(p_filename))
993209
+    {
993209
+      vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
993209
+      return;
993209
+    }
993209
+    /* NOTE - actual file permissions will be governed by the tunable umask */
993209
+    /* XXX - do we care about race between create and chown() of anonymous
993209
+     * upload?
993209
+     */
993209
+    if (is_unique || (p_sess->is_anonymous && !tunable_anon_other_write_enable))
993209
+    {
993209
+      new_file_fd = str_create_exclusive(p_filename);
993209
+    }
993209
+    else
993209
+    {
993209
+      /* For non-anonymous, allow open() to overwrite or append existing
993209
+       * files
993209
+       */
993209
+      new_file_fd = str_create(p_filename);
993209
+      if (!is_append && offset == 0)
993209
+      {
993209
+        do_truncate = 1;
993209
+      }
993209
+    }
993209
+    if (vsf_sysutil_retval_is_error(new_file_fd))
993209
+    {
993209
+      vsf_cmdio_write(p_sess, FTP_UPLOADFAIL, "Could not create file.");
993209
+      return;
993209
     }
993209
-  }
993209
-  if (vsf_sysutil_retval_is_error(new_file_fd))
993209
-  {
993209
-    vsf_cmdio_write(p_sess, FTP_UPLOADFAIL, "Could not create file.");
993209
-    return;
993209
   }
993209
   created = 1;
993209
   vsf_sysutil_fstat(new_file_fd, &s_p_statbuf);
993209
diff --git a/sysutil.c b/sysutil.c
993209
index e97f3bd..e6d88dc 100644
993209
--- a/sysutil.c
993209
+++ b/sysutil.c
993209
@@ -1663,6 +1663,9 @@ vsf_sysutil_get_error(void)
993209
     case ENOENT:
993209
       retval = kVSFSysUtilErrNOENT;
993209
       break;
993209
+    case EEXIST:
993209
+      retval = kVSFSysUtilErrEXIST;
993209
+      break;
993209
     default:
993209
       break;
993209
   }
993209
diff --git a/sysutil.h b/sysutil.h
993209
index b745d4a..15d4020 100644
993209
--- a/sysutil.h
993209
+++ b/sysutil.h
993209
@@ -18,7 +18,8 @@ enum EVSFSysUtilError
993209
   kVSFSysUtilErrINVAL,
993209
   kVSFSysUtilErrOPNOTSUPP,
993209
   kVSFSysUtilErrACCES,
993209
-  kVSFSysUtilErrNOENT
993209
+  kVSFSysUtilErrNOENT,
993209
+  kVSFSysUtilErrEXIST
993209
 };
993209
 enum EVSFSysUtilError vsf_sysutil_get_error(void);
993209
 
993209
diff --git a/tunables.c b/tunables.c
993209
index 9680528..5ec2bdc 100644
993209
--- a/tunables.c
993209
+++ b/tunables.c
993209
@@ -92,6 +92,7 @@ int tunable_ftp_enable;
993209
 int tunable_http_enable;
993209
 int tunable_seccomp_sandbox;
993209
 int tunable_allow_writeable_chroot;
993209
+int tunable_better_stou;
993209
 
993209
 unsigned int tunable_accept_timeout;
993209
 unsigned int tunable_connect_timeout;
993209
@@ -239,6 +240,7 @@ tunables_load_defaults()
993209
   tunable_http_enable = 0;
993209
   tunable_seccomp_sandbox = 0;
993209
   tunable_allow_writeable_chroot = 0;
993209
+  tunable_better_stou = 0;
993209
 
993209
   tunable_accept_timeout = 60;
993209
   tunable_connect_timeout = 60;
993209
diff --git a/tunables.h b/tunables.h
993209
index a466427..85ea1a8 100644
993209
--- a/tunables.h
993209
+++ b/tunables.h
993209
@@ -93,6 +93,9 @@ extern int tunable_ftp_enable;                /* Allow FTP protocol */
993209
 extern int tunable_http_enable;               /* Allow HTTP protocol */
993209
 extern int tunable_seccomp_sandbox;           /* seccomp filter sandbox */
993209
 extern int tunable_allow_writeable_chroot;    /* Allow misconfiguration */
993209
+extern int tunable_better_stou;               /* Use better file name generation
993209
+                                               * algorithm for the STOU command
993209
+					       */
993209
 
993209
 /* Integer/numeric defines */
993209
 extern unsigned int tunable_accept_timeout;
993209
diff --git a/vsftpd.conf.5 b/vsftpd.conf.5
993209
index 43b0435..e9ae474 100644
993209
--- a/vsftpd.conf.5
993209
+++ b/vsftpd.conf.5
993209
@@ -65,6 +65,16 @@ creates an 'etc' directory in the new root directory, they could potentially
993209
 trick the C library into loading a user-created configuration file from the
993209
 /etc/ directory.
993209
 
993209
+Default: NO
993209
+.TP
993209
+.B better_stou
993209
+Use a better file name generation algorithm for the STOU command. The default
993209
+original algorithm simply adds an increasing number suffix to the file name,
993209
+which is prone to race conditions if multiple uploaders use the STOU command
993209
+with the same file name simultaneously, which can result in failure of the
993209
+command. The new algorithm adds a unique random six character suffix to
993209
+the file name, which works much better in face of concurrent uploads.
993209
+
993209
 Default: NO
993209
 .TP
993209
 .B anon_mkdir_write_enable
993209
-- 
993209
2.14.3
993209