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

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