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