8ca061
From 0207c68ea39b74fc99e445231c1ac08ad5406720 Mon Sep 17 00:00:00 2001
8ca061
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8ca061
Date: Thu, 14 Dec 2017 13:53:48 +0000
8ca061
Subject: [PATCH 1/2] merge revision(s) 61242: [Backport #14185]
8ca061
8ca061
	Fix a command injection vulnerability in Net::FTP.
8ca061
8ca061
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@61246 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8ca061
---
8ca061
 ChangeLog                |   4 +
8ca061
 lib/net/ftp.rb           |  10 +-
8ca061
 test/net/ftp/test_ftp.rb | 234 +++++++++++++++++++++++++++++++++++++++++++++++
8ca061
 3 files changed, 243 insertions(+), 5 deletions(-)
8ca061
8ca061
diff --git a/ChangeLog b/ChangeLog
8ca061
index 177ff95c8b..ecff5aff99 100644
8ca061
--- a/ChangeLog
8ca061
+++ b/ChangeLog
8ca061
@@ -1,3 +1,7 @@
8ca061
+Thu Dec 14 22:52:11 2017  Shugo Maeda  <shugo@ruby-lang.org>
8ca061
+
8ca061
+	Fix a command injection vulnerability in Net::FTP.
8ca061
+
8ca061
 Tue Nov 15 15:29:36 2016  NARUSE, Yui  <naruse@ruby-lang.org>
8ca061
 
8ca061
 	* ext/openssl/ossl_ssl.c (ssl_npn_select_cb_common): fix parsing
8ca061
diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb
8ca061
index c9b80c6804..79edb80864 100644
8ca061
--- a/lib/net/ftp.rb
8ca061
+++ b/lib/net/ftp.rb
8ca061
@@ -607,10 +607,10 @@ module Net
8ca061
       if localfile
8ca061
         if @resume
8ca061
           rest_offset = File.size?(localfile)
8ca061
-          f = open(localfile, "a")
8ca061
+          f = File.open(localfile, "a")
8ca061
         else
8ca061
           rest_offset = nil
8ca061
-          f = open(localfile, "w")
8ca061
+          f = File.open(localfile, "w")
8ca061
         end
8ca061
       elsif !block_given?
8ca061
         result = ""
8ca061
@@ -638,7 +638,7 @@ module Net
8ca061
     def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line
8ca061
       result = nil
8ca061
       if localfile
8ca061
-        f = open(localfile, "w")
8ca061
+        f = File.open(localfile, "w")
8ca061
       elsif !block_given?
8ca061
         result = ""
8ca061
       end
8ca061
@@ -684,7 +684,7 @@ module Net
8ca061
       else
8ca061
         rest_offset = nil
8ca061
       end
8ca061
-      f = open(localfile)
8ca061
+      f = File.open(localfile)
8ca061
       begin
8ca061
         f.binmode
8ca061
         if rest_offset
8ca061
@@ -703,7 +703,7 @@ module Net
8ca061
     # passing in the transmitted data one line at a time.
8ca061
     #
8ca061
     def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
8ca061
-      f = open(localfile)
8ca061
+      f = File.open(localfile)
8ca061
       begin
8ca061
         storlines("STOR " + remotefile, f, &block)
8ca061
       ensure
8ca061
diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb
8ca061
index cb311695d0..91a6002c5c 100644
8ca061
--- a/test/net/ftp/test_ftp.rb
8ca061
+++ b/test/net/ftp/test_ftp.rb
8ca061
@@ -2,6 +2,7 @@ require "net/ftp"
8ca061
 require "test/unit"
8ca061
 require "ostruct"
8ca061
 require "stringio"
8ca061
+require "tmpdir"
8ca061
 
8ca061
 class FTPTest < Test::Unit::TestCase
8ca061
   SERVER_ADDR = "127.0.0.1"
8ca061
@@ -783,6 +784,227 @@ class FTPTest < Test::Unit::TestCase
8ca061
     end
8ca061
   end
8ca061
 
8ca061
+  def test_getbinaryfile_command_injection
8ca061
+    skip "| is not allowed in filename on Windows" if windows?
8ca061
+    [false, true].each do |resume|
8ca061
+      commands = []
8ca061
+      binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
8ca061
+      server = create_ftp_server { |sock|
8ca061
+        sock.print("220 (test_ftp).\r\n")
8ca061
+        commands.push(sock.gets)
8ca061
+        sock.print("331 Please specify the password.\r\n")
8ca061
+        commands.push(sock.gets)
8ca061
+        sock.print("230 Login successful.\r\n")
8ca061
+        commands.push(sock.gets)
8ca061
+        sock.print("200 Switching to Binary mode.\r\n")
8ca061
+        line = sock.gets
8ca061
+        commands.push(line)
8ca061
+        host, port = process_port_or_eprt(sock, line)
8ca061
+        commands.push(sock.gets)
8ca061
+        sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
8ca061
+        conn = TCPSocket.new(host, port)
8ca061
+        binary_data.scan(/.{1,1024}/nm) do |s|
8ca061
+          conn.print(s)
8ca061
+        end
8ca061
+        conn.shutdown(Socket::SHUT_WR)
8ca061
+        conn.read
8ca061
+        conn.close
8ca061
+        sock.print("226 Transfer complete.\r\n")
8ca061
+      }
8ca061
+      begin
8ca061
+        chdir_to_tmpdir do
8ca061
+          begin
8ca061
+            ftp = Net::FTP.new
8ca061
+            ftp.resume = resume
8ca061
+            ftp.read_timeout = 0.2
8ca061
+            ftp.connect(SERVER_ADDR, server.port)
8ca061
+            ftp.login
8ca061
+            assert_match(/\AUSER /, commands.shift)
8ca061
+            assert_match(/\APASS /, commands.shift)
8ca061
+            assert_equal("TYPE I\r\n", commands.shift)
8ca061
+            ftp.getbinaryfile("|echo hello")
8ca061
+            assert_equal(binary_data, File.binread("./|echo hello"))
8ca061
+            assert_match(/\A(PORT|EPRT) /, commands.shift)
8ca061
+            assert_equal("RETR |echo hello\r\n", commands.shift)
8ca061
+            assert_equal(nil, commands.shift)
8ca061
+          ensure
8ca061
+            ftp.close if ftp
8ca061
+          end
8ca061
+        end
8ca061
+      ensure
8ca061
+        server.close
8ca061
+      end
8ca061
+    end
8ca061
+  end
8ca061
+
8ca061
+  def test_gettextfile_command_injection
8ca061
+    skip "| is not allowed in filename on Windows" if windows?
8ca061
+    commands = []
8ca061
+    text_data = <
8ca061
+foo
8ca061
+bar
8ca061
+baz
8ca061
+EOF
8ca061
+    server = create_ftp_server { |sock|
8ca061
+      sock.print("220 (test_ftp).\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("331 Please specify the password.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("230 Login successful.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to Binary mode.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to ASCII mode.\r\n")
8ca061
+      line = sock.gets
8ca061
+      commands.push(line)
8ca061
+      host, port = process_port_or_eprt(sock, line)
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("150 Opening TEXT mode data connection for |echo hello (#{text_data.size} bytes)\r\n")
8ca061
+      conn = TCPSocket.new(host, port)
8ca061
+      text_data.each_line do |l|
8ca061
+        conn.print(l)
8ca061
+      end
8ca061
+      conn.shutdown(Socket::SHUT_WR)
8ca061
+      conn.read
8ca061
+      conn.close
8ca061
+      sock.print("226 Transfer complete.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to Binary mode.\r\n")
8ca061
+    }
8ca061
+    begin
8ca061
+      chdir_to_tmpdir do
8ca061
+        begin
8ca061
+          ftp = Net::FTP.new
8ca061
+          ftp.connect(SERVER_ADDR, server.port)
8ca061
+          ftp.login
8ca061
+          assert_match(/\AUSER /, commands.shift)
8ca061
+          assert_match(/\APASS /, commands.shift)
8ca061
+          assert_equal("TYPE I\r\n", commands.shift)
8ca061
+          ftp.gettextfile("|echo hello")
8ca061
+          assert_equal(text_data.gsub(/\r\n/, "\n"),
8ca061
+                       File.binread("./|echo hello"))
8ca061
+          assert_equal("TYPE A\r\n", commands.shift)
8ca061
+          assert_match(/\A(PORT|EPRT) /, commands.shift)
8ca061
+          assert_equal("RETR |echo hello\r\n", commands.shift)
8ca061
+          assert_equal("TYPE I\r\n", commands.shift)
8ca061
+          assert_equal(nil, commands.shift)
8ca061
+        ensure
8ca061
+          ftp.close if ftp
8ca061
+        end
8ca061
+      end
8ca061
+    ensure
8ca061
+      server.close
8ca061
+    end
8ca061
+  end
8ca061
+
8ca061
+  def test_putbinaryfile_command_injection
8ca061
+    skip "| is not allowed in filename on Windows" if windows?
8ca061
+    commands = []
8ca061
+    binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
8ca061
+    received_data = nil
8ca061
+    server = create_ftp_server { |sock|
8ca061
+      sock.print("220 (test_ftp).\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("331 Please specify the password.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("230 Login successful.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to Binary mode.\r\n")
8ca061
+      line = sock.gets
8ca061
+      commands.push(line)
8ca061
+      host, port = process_port_or_eprt(sock, line)
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
8ca061
+      conn = TCPSocket.new(host, port)
8ca061
+      received_data = conn.read
8ca061
+      conn.close
8ca061
+      sock.print("226 Transfer complete.\r\n")
8ca061
+    }
8ca061
+    begin
8ca061
+      chdir_to_tmpdir do
8ca061
+        File.binwrite("./|echo hello", binary_data)
8ca061
+        begin
8ca061
+          ftp = Net::FTP.new
8ca061
+          ftp.read_timeout = 0.2
8ca061
+          ftp.connect(SERVER_ADDR, server.port)
8ca061
+          ftp.login
8ca061
+          assert_match(/\AUSER /, commands.shift)
8ca061
+          assert_match(/\APASS /, commands.shift)
8ca061
+          assert_equal("TYPE I\r\n", commands.shift)
8ca061
+          ftp.putbinaryfile("|echo hello")
8ca061
+          assert_equal(binary_data, received_data)
8ca061
+          assert_match(/\A(PORT|EPRT) /, commands.shift)
8ca061
+          assert_equal("STOR |echo hello\r\n", commands.shift)
8ca061
+          assert_equal(nil, commands.shift)
8ca061
+        ensure
8ca061
+          ftp.close if ftp
8ca061
+        end
8ca061
+      end
8ca061
+    ensure
8ca061
+      server.close
8ca061
+    end
8ca061
+  end
8ca061
+
8ca061
+  def test_puttextfile_command_injection
8ca061
+    skip "| is not allowed in filename on Windows" if windows?
8ca061
+    commands = []
8ca061
+    received_data = nil
8ca061
+    server = create_ftp_server { |sock|
8ca061
+      sock.print("220 (test_ftp).\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("331 Please specify the password.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("230 Login successful.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to Binary mode.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to ASCII mode.\r\n")
8ca061
+      line = sock.gets
8ca061
+      commands.push(line)
8ca061
+      host, port = process_port_or_eprt(sock, line)
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("150 Opening TEXT mode data connection for |echo hello\r\n")
8ca061
+      conn = TCPSocket.new(host, port)
8ca061
+      received_data = conn.read
8ca061
+      conn.close
8ca061
+      sock.print("226 Transfer complete.\r\n")
8ca061
+      commands.push(sock.gets)
8ca061
+      sock.print("200 Switching to Binary mode.\r\n")
8ca061
+    }
8ca061
+    begin
8ca061
+      chdir_to_tmpdir do
8ca061
+        File.open("|echo hello", "w") do |f|
8ca061
+          f.puts("foo")
8ca061
+          f.puts("bar")
8ca061
+          f.puts("baz")
8ca061
+        end
8ca061
+        begin
8ca061
+          ftp = Net::FTP.new
8ca061
+          ftp.connect(SERVER_ADDR, server.port)
8ca061
+          ftp.login
8ca061
+          assert_match(/\AUSER /, commands.shift)
8ca061
+          assert_match(/\APASS /, commands.shift)
8ca061
+          assert_equal("TYPE I\r\n", commands.shift)
8ca061
+          ftp.puttextfile("|echo hello")
8ca061
+          assert_equal(<
8ca061
+foo
8ca061
+bar
8ca061
+baz
8ca061
+EOF
8ca061
+          assert_equal("TYPE A\r\n", commands.shift)
8ca061
+          assert_match(/\A(PORT|EPRT) /, commands.shift)
8ca061
+          assert_equal("STOR |echo hello\r\n", commands.shift)
8ca061
+          assert_equal("TYPE I\r\n", commands.shift)
8ca061
+          assert_equal(nil, commands.shift)
8ca061
+        ensure
8ca061
+          ftp.close if ftp
8ca061
+        end
8ca061
+      end
8ca061
+    ensure
8ca061
+      server.close
8ca061
+    end
8ca061
+  end
8ca061
+
8ca061
   private
8ca061
 
8ca061
 
8ca061
@@ -810,4 +1032,16 @@ class FTPTest < Test::Unit::TestCase
8ca061
     end
8ca061
     return server
8ca061
   end
8ca061
+
8ca061
+  def chdir_to_tmpdir
8ca061
+    Dir.mktmpdir do |dir|
8ca061
+      pwd = Dir.pwd
8ca061
+      Dir.chdir(dir)
8ca061
+      begin
8ca061
+        yield
8ca061
+      ensure
8ca061
+        Dir.chdir(pwd)
8ca061
+      end
8ca061
+    end
8ca061
+  end
8ca061
 end
8ca061
-- 
8ca061
2.15.1
8ca061
8ca061
8ca061
From 02b8978ff10b05304dbb46d73b49a2cf3a87cb92 Mon Sep 17 00:00:00 2001
8ca061
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8ca061
Date: Thu, 14 Dec 2017 15:08:49 +0000
8ca061
Subject: [PATCH 2/2] * test/net/ftp/test_ftp.rb (process_port_or_eprt): merge
8ca061
 a part of   r56973 to pass the test introduced at previous commit.
8ca061
8ca061
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@61255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8ca061
---
8ca061
 ChangeLog                |  5 +++++
8ca061
 test/net/ftp/test_ftp.rb | 18 ++++++++++++++++++
8ca061
 2 files changed, 23 insertions(+), 0 deletions(-)
8ca061
8ca061
diff --git a/ChangeLog b/ChangeLog
8ca061
index ecff5aff99..d9d9629ffa 100644
8ca061
--- a/ChangeLog
8ca061
+++ b/ChangeLog
8ca061
@@ -1,3 +1,8 @@
8ca061
+Fri Dec 15 00:08:26 2017  NAKAMURA Usaku  <usa@ruby-lang.org>
8ca061
+
8ca061
+	* test/net/ftp/test_ftp.rb (process_port_or_eprt): merge a part of
8ca061
+	  r56973 to pass the test introduced at previous commit.
8ca061
+
8ca061
 Thu Dec 14 22:52:11 2017  Shugo Maeda  <shugo@ruby-lang.org>
8ca061
 
8ca061
 	Fix a command injection vulnerability in Net::FTP.
8ca061
diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb
8ca061
index 91a6002c5c..52e5873d61 100644
8ca061
--- a/test/net/ftp/test_ftp.rb
8ca061
+++ b/test/net/ftp/test_ftp.rb
8ca061
@@ -1044,4 +1044,22 @@ EOF
8ca061
       end
8ca061
     end
8ca061
   end
8ca061
+
8ca061
+  def process_port_or_eprt(sock, line)
8ca061
+    case line
8ca061
+    when /\APORT (.*)/
8ca061
+      port_args = $1.split(/,/)
8ca061
+      host = port_args[0, 4].join(".")
8ca061
+      port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y}
8ca061
+      sock.print("200 PORT command successful.\r\n")
8ca061
+      return host, port
8ca061
+    when /\AEPRT \|2\|(.*?)\|(.*?)\|/
8ca061
+      host = $1
8ca061
+      port = $2.to_i
8ca061
+      sock.print("200 EPRT command successful.\r\n")
8ca061
+      return host, port
8ca061
+    else
8ca061
+      flunk "PORT or EPRT expected"
8ca061
+    end
8ca061
+  end
8ca061
 end
8ca061
-- 
8ca061
2.15.1
8ca061