mirror of
https://github.com/ruby/ruby.git
synced 2025-09-16 17:14:01 +02:00
merge revision(s) 61242: [Backport #14185]
Fix a command injection vulnerability in Net::FTP. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@61246 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fc824f2a81
commit
0207c68ea3
4 changed files with 244 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Thu Dec 14 22:52:11 2017 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
Fix a command injection vulnerability in Net::FTP.
|
||||||
|
|
||||||
Thu Dec 14 22:49:08 2017 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
Thu Dec 14 22:49:08 2017 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||||
|
|
||||||
Merge rubygems-2.6.14 changes.
|
Merge rubygems-2.6.14 changes.
|
||||||
|
|
|
@ -606,10 +606,10 @@ module Net
|
||||||
if localfile
|
if localfile
|
||||||
if @resume
|
if @resume
|
||||||
rest_offset = File.size?(localfile)
|
rest_offset = File.size?(localfile)
|
||||||
f = open(localfile, "a")
|
f = File.open(localfile, "a")
|
||||||
else
|
else
|
||||||
rest_offset = nil
|
rest_offset = nil
|
||||||
f = open(localfile, "w")
|
f = File.open(localfile, "w")
|
||||||
end
|
end
|
||||||
elsif !block_given?
|
elsif !block_given?
|
||||||
result = ""
|
result = ""
|
||||||
|
@ -637,7 +637,7 @@ module Net
|
||||||
def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line
|
def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line
|
||||||
result = nil
|
result = nil
|
||||||
if localfile
|
if localfile
|
||||||
f = open(localfile, "w")
|
f = File.open(localfile, "w")
|
||||||
elsif !block_given?
|
elsif !block_given?
|
||||||
result = ""
|
result = ""
|
||||||
end
|
end
|
||||||
|
@ -683,7 +683,7 @@ module Net
|
||||||
else
|
else
|
||||||
rest_offset = nil
|
rest_offset = nil
|
||||||
end
|
end
|
||||||
f = open(localfile)
|
f = File.open(localfile)
|
||||||
begin
|
begin
|
||||||
f.binmode
|
f.binmode
|
||||||
if rest_offset
|
if rest_offset
|
||||||
|
@ -702,7 +702,7 @@ module Net
|
||||||
# passing in the transmitted data one line at a time.
|
# passing in the transmitted data one line at a time.
|
||||||
#
|
#
|
||||||
def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
|
def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
|
||||||
f = open(localfile)
|
f = File.open(localfile)
|
||||||
begin
|
begin
|
||||||
storlines("STOR " + remotefile, f, &block)
|
storlines("STOR " + remotefile, f, &block)
|
||||||
ensure
|
ensure
|
||||||
|
|
|
@ -2,6 +2,7 @@ require "net/ftp"
|
||||||
require "test/unit"
|
require "test/unit"
|
||||||
require "ostruct"
|
require "ostruct"
|
||||||
require "stringio"
|
require "stringio"
|
||||||
|
require "tmpdir"
|
||||||
|
|
||||||
class FTPTest < Test::Unit::TestCase
|
class FTPTest < Test::Unit::TestCase
|
||||||
SERVER_ADDR = "127.0.0.1"
|
SERVER_ADDR = "127.0.0.1"
|
||||||
|
@ -825,6 +826,227 @@ class FTPTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_getbinaryfile_command_injection
|
||||||
|
skip "| is not allowed in filename on Windows" if windows?
|
||||||
|
[false, true].each do |resume|
|
||||||
|
commands = []
|
||||||
|
binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
|
||||||
|
server = create_ftp_server { |sock|
|
||||||
|
sock.print("220 (test_ftp).\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("331 Please specify the password.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("230 Login successful.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to Binary mode.\r\n")
|
||||||
|
line = sock.gets
|
||||||
|
commands.push(line)
|
||||||
|
host, port = process_port_or_eprt(sock, line)
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
|
||||||
|
conn = TCPSocket.new(host, port)
|
||||||
|
binary_data.scan(/.{1,1024}/nm) do |s|
|
||||||
|
conn.print(s)
|
||||||
|
end
|
||||||
|
conn.shutdown(Socket::SHUT_WR)
|
||||||
|
conn.read
|
||||||
|
conn.close
|
||||||
|
sock.print("226 Transfer complete.\r\n")
|
||||||
|
}
|
||||||
|
begin
|
||||||
|
chdir_to_tmpdir do
|
||||||
|
begin
|
||||||
|
ftp = Net::FTP.new
|
||||||
|
ftp.resume = resume
|
||||||
|
ftp.read_timeout = 0.2
|
||||||
|
ftp.connect(SERVER_ADDR, server.port)
|
||||||
|
ftp.login
|
||||||
|
assert_match(/\AUSER /, commands.shift)
|
||||||
|
assert_match(/\APASS /, commands.shift)
|
||||||
|
assert_equal("TYPE I\r\n", commands.shift)
|
||||||
|
ftp.getbinaryfile("|echo hello")
|
||||||
|
assert_equal(binary_data, File.binread("./|echo hello"))
|
||||||
|
assert_match(/\A(PORT|EPRT) /, commands.shift)
|
||||||
|
assert_equal("RETR |echo hello\r\n", commands.shift)
|
||||||
|
assert_equal(nil, commands.shift)
|
||||||
|
ensure
|
||||||
|
ftp.close if ftp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
server.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_gettextfile_command_injection
|
||||||
|
skip "| is not allowed in filename on Windows" if windows?
|
||||||
|
commands = []
|
||||||
|
text_data = <<EOF.gsub(/\n/, "\r\n")
|
||||||
|
foo
|
||||||
|
bar
|
||||||
|
baz
|
||||||
|
EOF
|
||||||
|
server = create_ftp_server { |sock|
|
||||||
|
sock.print("220 (test_ftp).\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("331 Please specify the password.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("230 Login successful.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to Binary mode.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to ASCII mode.\r\n")
|
||||||
|
line = sock.gets
|
||||||
|
commands.push(line)
|
||||||
|
host, port = process_port_or_eprt(sock, line)
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("150 Opening TEXT mode data connection for |echo hello (#{text_data.size} bytes)\r\n")
|
||||||
|
conn = TCPSocket.new(host, port)
|
||||||
|
text_data.each_line do |l|
|
||||||
|
conn.print(l)
|
||||||
|
end
|
||||||
|
conn.shutdown(Socket::SHUT_WR)
|
||||||
|
conn.read
|
||||||
|
conn.close
|
||||||
|
sock.print("226 Transfer complete.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to Binary mode.\r\n")
|
||||||
|
}
|
||||||
|
begin
|
||||||
|
chdir_to_tmpdir do
|
||||||
|
begin
|
||||||
|
ftp = Net::FTP.new
|
||||||
|
ftp.connect(SERVER_ADDR, server.port)
|
||||||
|
ftp.login
|
||||||
|
assert_match(/\AUSER /, commands.shift)
|
||||||
|
assert_match(/\APASS /, commands.shift)
|
||||||
|
assert_equal("TYPE I\r\n", commands.shift)
|
||||||
|
ftp.gettextfile("|echo hello")
|
||||||
|
assert_equal(text_data.gsub(/\r\n/, "\n"),
|
||||||
|
File.binread("./|echo hello"))
|
||||||
|
assert_equal("TYPE A\r\n", commands.shift)
|
||||||
|
assert_match(/\A(PORT|EPRT) /, commands.shift)
|
||||||
|
assert_equal("RETR |echo hello\r\n", commands.shift)
|
||||||
|
assert_equal("TYPE I\r\n", commands.shift)
|
||||||
|
assert_equal(nil, commands.shift)
|
||||||
|
ensure
|
||||||
|
ftp.close if ftp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
server.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_putbinaryfile_command_injection
|
||||||
|
skip "| is not allowed in filename on Windows" if windows?
|
||||||
|
commands = []
|
||||||
|
binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
|
||||||
|
received_data = nil
|
||||||
|
server = create_ftp_server { |sock|
|
||||||
|
sock.print("220 (test_ftp).\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("331 Please specify the password.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("230 Login successful.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to Binary mode.\r\n")
|
||||||
|
line = sock.gets
|
||||||
|
commands.push(line)
|
||||||
|
host, port = process_port_or_eprt(sock, line)
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
|
||||||
|
conn = TCPSocket.new(host, port)
|
||||||
|
received_data = conn.read
|
||||||
|
conn.close
|
||||||
|
sock.print("226 Transfer complete.\r\n")
|
||||||
|
}
|
||||||
|
begin
|
||||||
|
chdir_to_tmpdir do
|
||||||
|
File.binwrite("./|echo hello", binary_data)
|
||||||
|
begin
|
||||||
|
ftp = Net::FTP.new
|
||||||
|
ftp.read_timeout = 0.2
|
||||||
|
ftp.connect(SERVER_ADDR, server.port)
|
||||||
|
ftp.login
|
||||||
|
assert_match(/\AUSER /, commands.shift)
|
||||||
|
assert_match(/\APASS /, commands.shift)
|
||||||
|
assert_equal("TYPE I\r\n", commands.shift)
|
||||||
|
ftp.putbinaryfile("|echo hello")
|
||||||
|
assert_equal(binary_data, received_data)
|
||||||
|
assert_match(/\A(PORT|EPRT) /, commands.shift)
|
||||||
|
assert_equal("STOR |echo hello\r\n", commands.shift)
|
||||||
|
assert_equal(nil, commands.shift)
|
||||||
|
ensure
|
||||||
|
ftp.close if ftp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
server.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_puttextfile_command_injection
|
||||||
|
skip "| is not allowed in filename on Windows" if windows?
|
||||||
|
commands = []
|
||||||
|
received_data = nil
|
||||||
|
server = create_ftp_server { |sock|
|
||||||
|
sock.print("220 (test_ftp).\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("331 Please specify the password.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("230 Login successful.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to Binary mode.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to ASCII mode.\r\n")
|
||||||
|
line = sock.gets
|
||||||
|
commands.push(line)
|
||||||
|
host, port = process_port_or_eprt(sock, line)
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("150 Opening TEXT mode data connection for |echo hello\r\n")
|
||||||
|
conn = TCPSocket.new(host, port)
|
||||||
|
received_data = conn.read
|
||||||
|
conn.close
|
||||||
|
sock.print("226 Transfer complete.\r\n")
|
||||||
|
commands.push(sock.gets)
|
||||||
|
sock.print("200 Switching to Binary mode.\r\n")
|
||||||
|
}
|
||||||
|
begin
|
||||||
|
chdir_to_tmpdir do
|
||||||
|
File.open("|echo hello", "w") do |f|
|
||||||
|
f.puts("foo")
|
||||||
|
f.puts("bar")
|
||||||
|
f.puts("baz")
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
ftp = Net::FTP.new
|
||||||
|
ftp.connect(SERVER_ADDR, server.port)
|
||||||
|
ftp.login
|
||||||
|
assert_match(/\AUSER /, commands.shift)
|
||||||
|
assert_match(/\APASS /, commands.shift)
|
||||||
|
assert_equal("TYPE I\r\n", commands.shift)
|
||||||
|
ftp.puttextfile("|echo hello")
|
||||||
|
assert_equal(<<EOF.gsub(/\n/, "\r\n"), received_data)
|
||||||
|
foo
|
||||||
|
bar
|
||||||
|
baz
|
||||||
|
EOF
|
||||||
|
assert_equal("TYPE A\r\n", commands.shift)
|
||||||
|
assert_match(/\A(PORT|EPRT) /, commands.shift)
|
||||||
|
assert_equal("STOR |echo hello\r\n", commands.shift)
|
||||||
|
assert_equal("TYPE I\r\n", commands.shift)
|
||||||
|
assert_equal(nil, commands.shift)
|
||||||
|
ensure
|
||||||
|
ftp.close if ftp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
server.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_ftp_server(sleep_time = nil)
|
def create_ftp_server(sleep_time = nil)
|
||||||
|
@ -847,4 +1069,16 @@ class FTPTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
return server
|
return server
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def chdir_to_tmpdir
|
||||||
|
Dir.mktmpdir do |dir|
|
||||||
|
pwd = Dir.pwd
|
||||||
|
Dir.chdir(dir)
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
Dir.chdir(pwd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#define RUBY_VERSION "2.2.9"
|
#define RUBY_VERSION "2.2.9"
|
||||||
#define RUBY_RELEASE_DATE "2017-12-14"
|
#define RUBY_RELEASE_DATE "2017-12-14"
|
||||||
#define RUBY_PATCHLEVEL 478
|
#define RUBY_PATCHLEVEL 479
|
||||||
|
|
||||||
#define RUBY_RELEASE_YEAR 2017
|
#define RUBY_RELEASE_YEAR 2017
|
||||||
#define RUBY_RELEASE_MONTH 12
|
#define RUBY_RELEASE_MONTH 12
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue