Blame SOURCES/wget-1.14-CVE-2014-4877.patch

226bdc
From 043366ac3248a58662a6fbf47a1dd688a75d0e78 Mon Sep 17 00:00:00 2001
226bdc
From: Darshit Shah <darnir@gmail.com>
226bdc
Date: Mon, 8 Sep 2014 00:41:17 +0530
226bdc
Subject: [PATCH 1/2] Fix R7-2014-15: Arbitrary Symlink Access
226bdc
226bdc
Wget was susceptible to a symlink attack which could create arbitrary
226bdc
files, directories or symbolic links and set their permissions when
226bdc
retrieving a directory recursively through FTP. This commit changes the
226bdc
default settings in Wget such that Wget no longer creates local symbolic
226bdc
links, but rather traverses them and retrieves the pointed-to file in
226bdc
such a retrieval.
226bdc
226bdc
The old behaviour can be attained by passing the --retr-symlinks=no
226bdc
option to the Wget invokation command.
226bdc
---
226bdc
 doc/wget.texi | 23 ++++++++++++-----------
226bdc
 src/init.c    | 16 ++++++++++++++++
226bdc
 2 files changed, 28 insertions(+), 11 deletions(-)
226bdc
226bdc
diff --git a/doc/wget.texi b/doc/wget.texi
226bdc
index a31eb5e..f54e98d 100644
226bdc
--- a/doc/wget.texi
226bdc
+++ b/doc/wget.texi
226bdc
@@ -1883,17 +1883,18 @@ Preserve remote file permissions instead of permissions set by umask.
226bdc
 
226bdc
 @cindex symbolic links, retrieving
226bdc
 @item --retr-symlinks
226bdc
-Usually, when retrieving @sc{ftp} directories recursively and a symbolic
226bdc
-link is encountered, the linked-to file is not downloaded.  Instead, a
226bdc
-matching symbolic link is created on the local filesystem.  The
226bdc
-pointed-to file will not be downloaded unless this recursive retrieval
226bdc
-would have encountered it separately and downloaded it anyway.
226bdc
-
226bdc
-When @samp{--retr-symlinks} is specified, however, symbolic links are
226bdc
-traversed and the pointed-to files are retrieved.  At this time, this
226bdc
-option does not cause Wget to traverse symlinks to directories and
226bdc
-recurse through them, but in the future it should be enhanced to do
226bdc
-this.
226bdc
+By default, when retrieving @sc{ftp} directories recursively and a symbolic link
226bdc
+is encountered, the symbolic link is traversed and the pointed-to files are
226bdc
+retrieved.  Currently, Wget does not traverse symbolic links to directories to
226bdc
+download them recursively, though this feature may be added in the future.
226bdc
+
226bdc
+When @samp{--retr-symlinks=no} is specified, the linked-to file is not
226bdc
+downloaded.  Instead, a matching symbolic link is created on the local
226bdc
+filesystem.  The pointed-to file will not be retrieved unless this recursive
226bdc
+retrieval would have encountered it separately and downloaded it anyway.  This
226bdc
+option poses a security risk where a malicious FTP Server may cause Wget to
226bdc
+write to files outside of the intended directories through a specially crafted
226bdc
+@sc{.listing} file.
226bdc
 
226bdc
 Note that when retrieving a file (not a directory) because it was
226bdc
 specified on the command-line, rather than because it was recursed to,
226bdc
diff --git a/src/init.c b/src/init.c
226bdc
index 93e95f8..94b6f8b 100644
226bdc
--- a/src/init.c
226bdc
+++ b/src/init.c
226bdc
@@ -366,6 +366,22 @@ defaults (void)
226bdc
 
226bdc
   opt.dns_cache = true;
226bdc
   opt.ftp_pasv = true;
226bdc
+  /* 2014-09-07  Darshit Shah  <darnir@gmail.com>
226bdc
+   * opt.retr_symlinks is set to true by default. Creating symbolic links on the
226bdc
+   * local filesystem pose a security threat by malicious FTP Servers that
226bdc
+   * server a specially crafted .listing file akin to this:
226bdc
+   *
226bdc
+   * lrwxrwxrwx   1 root     root           33 Dec 25  2012 JoCxl6d8rFU -> /
226bdc
+   * drwxrwxr-x  15 1024     106          4096 Aug 28 02:02 JoCxl6d8rFU
226bdc
+   *
226bdc
+   * A .listing file in this fashion makes Wget susceptiple to a symlink attack
226bdc
+   * wherein the attacker is able to create arbitrary files, directories and
226bdc
+   * symbolic links on the target system and even set permissions.
226bdc
+   *
226bdc
+   * Hence, by default Wget attempts to retrieve the pointed-to files and does
226bdc
+   * not create the symbolic links locally.
226bdc
+   */
226bdc
+  opt.retr_symlinks = true;
226bdc
 
226bdc
 #ifdef HAVE_SSL
226bdc
   opt.check_cert = true;
226bdc
-- 
226bdc
2.1.0
226bdc
226bdc
From bfa8c9cc9937f686a4de110e49710061267f8d9e Mon Sep 17 00:00:00 2001
226bdc
From: Darshit Shah <darnir@gmail.com>
226bdc
Date: Mon, 8 Sep 2014 15:07:45 +0530
226bdc
Subject: [PATCH 2/2] Add checks for valid listing file in FTP
226bdc
226bdc
When Wget retrieves a file through FTP, it first downloads a .listing
226bdc
file and parses it for information about the files and other metadata.
226bdc
Some servers may serve invalid .listing files. This patch checks for one
226bdc
such known inconsistency wherein multiple lines in a listing file have
226bdc
the same name. Such a filesystem is clearly not possible and hence we
226bdc
eliminate duplicate entries here.
226bdc
226bdc
Signed-off-by: Darshit Shah <darnir@gmail.com>
226bdc
---
226bdc
 src/ftp.c     | 27 +++++++++++++++++++++++++--
226bdc
 1 file changed, 25 insertions(+), 2 deletions(-)
226bdc
226bdc
diff --git a/src/ftp.c b/src/ftp.c
226bdc
index 2d54333..054cb61 100644
226bdc
--- a/src/ftp.c
226bdc
+++ b/src/ftp.c
226bdc
@@ -2211,6 +2211,29 @@ has_insecure_name_p (const char *s)
226bdc
   return false;
226bdc
 }
226bdc
 
226bdc
+/* Test if the file node is invalid. This can occur due to malformed or
226bdc
+ * maliciously crafted listing files being returned by the server.
226bdc
+ *
226bdc
+ * Currently, this function only tests if there are multiple entries in the
226bdc
+ * listing file by the same name. However this function can be expanded as more
226bdc
+ * such illegal listing formats are discovered. */
226bdc
+static bool
226bdc
+is_invalid_entry (struct fileinfo *f)
226bdc
+{
226bdc
+  struct fileinfo *cur;
226bdc
+  cur = f;
226bdc
+  char *f_name = f->name;
226bdc
+  /* If the node we're currently checking has a duplicate later, we eliminate
226bdc
+   * the current node and leave the next one intact. */
226bdc
+  while (cur->next)
226bdc
+    {
226bdc
+      cur = cur->next;
226bdc
+      if (strcmp(f_name, cur->name) == 0)
226bdc
+          return true;
226bdc
+    }
226bdc
+  return false;
226bdc
+}
226bdc
+
226bdc
 /* A near-top-level function to retrieve the files in a directory.
226bdc
    The function calls ftp_get_listing, to get a linked list of files.
226bdc
    Then it weeds out the file names that do not match the pattern.
226bdc
@@ -2248,11 +2271,11 @@ ftp_retrieve_glob (struct url *u, ccon *con, int action)
226bdc
             f = f->next;
226bdc
         }
226bdc
     }
226bdc
-  /* Remove all files with possible harmful names */
226bdc
+  /* Remove all files with possible harmful names or invalid entries. */
226bdc
   f = start;
226bdc
   while (f)
226bdc
     {
226bdc
-      if (has_insecure_name_p (f->name))
226bdc
+      if (has_insecure_name_p (f->name) || is_invalid_entry (f))
226bdc
         {
226bdc
           logprintf (LOG_VERBOSE, _("Rejecting %s.\n"),
226bdc
                      quote (f->name));
226bdc
-- 
226bdc
2.1.0
226bdc