8000 Integer overflow bug in fastfilereader and offset parameter to stream a file by mayanks · Pull Request #533 · eventmachine/eventmachine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Integer overflow bug in fastfilereader and offset parameter to stream a file #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ext/cmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ extern "C" int evma_set_rlimit_nofile (int nofiles)
evma_send_file_data_to_connection
*********************************/

extern "C" int evma_send_file_data_to_connection (const unsigned long binding, const char *filename)
extern "C" int evma_send_file_data_to_connection (const unsigned long binding, const char *filename, unsigned long offset)
{
/* This is a sugaring over send_data_to_connection that reads a file into a
* locally-allocated buffer, and sends the file data to the remote peer.
Expand Down Expand Up @@ -788,6 +788,9 @@ extern "C" int evma_send_file_data_to_connection (const unsigned long binding, c
return -1;
}

// seek to the offset first
lseek(Fd, offset, SEEK_SET);

r = read (Fd, data, filesize);
if (r != filesize) {
int e = errno;
Expand Down
2 changes: 1 addition & 1 deletion ext/eventmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extern "C" {
int evma_set_pending_connect_timeout (const unsigned long binding, float value);
int evma_get_outbound_data_size (const unsigned long binding);
uint64_t evma_get_last_activity_time (const unsigned long);
int evma_send_file_data_to_connection (const unsigned long binding, const char *filename);
int evma_send_file_data_to_connection (const unsigned long binding, const char *filename, unsigned long offset);

void evma_close_connection (const unsigned long binding, int after_writing);
int evma_report_connection_error_status (const unsigned long binding);
Expand Down
2 changes: 1 addition & 1 deletion ext/fastfilereader/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void Mapper_t::Close()
Mapper_t::GetChunk
******************/

const char *Mapper_t::GetChunk (unsigned start)
const char *Mapper_t::GetChunk (unsigned long start)
{
return MapPoint + start;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/fastfilereader/mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Mapper_t
Mapper_t (const string&);
virtual ~Mapper_t();

const char *GetChunk (unsigned);
const char *GetChunk (unsigned long);
void Close();
size_t GetFileSize() {return FileSize;}

Expand Down
6 changes: 3 additions & 3 deletions ext/fastfilereader/rubymain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ static VALUE mapper_get_chunk (VALUE self, VALUE start, VALUE length)
rb_raise (rb_eException, "No Mapper Object");

// TODO, what if some moron sends us a negative start value?
unsigned _start = NUM2INT (start);
unsigned _length = NUM2INT (length);
unsigned long _start = NUM2ULONG (start);
unsigned long _length = NUM2ULONG (length);
if ((_start + _length) > m->GetFileSize())
rb_raise (rb_eException, "Mapper Range Error");

Expand Down Expand Up @@ -103,7 +103,7 @@ static VALUE mapper_size (VALUE self)
Data_Get_Struct (self, Mapper_t, m);
if (!m)
rb_raise (rb_eException, "No Mapper Object");
return INT2NUM (m->GetFileSize());
return ULONG2NUM (m->GetFileSize());
}


Expand Down
6 changes: 3 additions & 3 deletions ext/rubymain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ static VALUE t__ssl_p (VALUE self)
t_send_file_data
****************/

static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename)
static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename, VALUE offset)
{

/* The current implementation of evma_send_file_data_to_connection enforces a strict
Expand All @@ -1018,7 +1018,7 @@ static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename)
* do this. For one thing it's ugly. For another, we can't be sure zero is never a real errno.
*/

int b = evma_send_file_data_to_connection (NUM2ULONG (signature), StringValuePtr(filename));
int b = evma_send_file_data_to_connection (NUM2ULONG (signature), StringValuePtr(filename), NUM2ULONG(offset));
if (b == -1)
rb_raise(rb_eRuntimeError, "%s", "File too large. send_file_data() supports files under 32k.");
if (b > 0) {
Expand Down Expand Up @@ -1278,7 +1278,7 @@ extern "C" void Init_rubyeventmachine()
rb_define_module_function (EmModule, "set_max_timer_count", (VALUE(*)(...))t_set_max_timer_count, 1);
rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 3);
rb_define_module_function (EmModule, "get_heartbeat_interval", (VALUE(*)(...))t_get_heartbeat_interval, 0);
rb_define_module_function (EmModule, "set_heartbeat_interval", (VALUE(*)(...))t_set_heartbeat_interval, 1);
rb_define_module_function (EmModule, "get_idle_time", (VALUE(*)(...))t_get_idle_time, 1);
Expand Down
4 changes: 2 additions & 2 deletions lib/em/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ def reconnect server, port
#
# @see #send_data
# @author Kirk Haines
def send_file_data filename
EventMachine::send_file_data @signature, filename
def send_file_data filename, offset = 0
EventMachine::send_file_data @signature, filename, offset
end

# Open a file on the filesystem and send it to the remote peer. This returns an
Expand Down
4 changes: 2 additions & 2 deletions lib/em/pure_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ def set_sock_opt signature, level, optname, optval
end

# @private
def send_file_data sig, filename
def send_file_data sig, filename, offset = 0
sz = File.size(filename)
raise "file too large" if sz > 32*1024
data =
begin
File.read filename
File.read filename, sz, offset
rescue
""
end
Expand Down
6 changes: 3 additions & 3 deletions lib/em/streamer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FileStreamer
def initialize connection, filename, args = {}
@connection = connection
@http_chunks = args[:http_chunks]
@position = args[:offset] || 0

if File.exist?(filename)
@size = File.size(filename)
Expand All @@ -53,10 +54,10 @@ def initialize connection, filename, args = {}
def stream_without_mapping filename
if @http_chunks
@connection.send_data "#{@size.to_s(16)}\r\n"
@connection.send_file_data filename
@connection.send_file_data filename, @position
@connection.send_data "\r\n0\r\n\r\n"
else
@connection.send_file_data filename
@connection.send_file_data filename, @position
end
succeed
end
Expand All @@ -66,7 +67,6 @@ def stream_without_mapping filename
def stream_with_mapping filename
ensure_mapping_extension_is_present

@position = 0
@mapping = EventMachine::FastFileReader::Mapper.new filename
stream_one_chunk
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jeventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def self.set_tls_parms(sig, params)
end
def self.start_tls(sig)
end
def self.send_file_data(sig, filename)
def self.send_file_data(sig, filename, offset = 0)
end

class Connection
Expand Down
0