Skip to content
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
10 changes: 5 additions & 5 deletions lib/c_glib/src/thrift/c_glib/protocol/thrift_binary_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,9 @@ thrift_binary_protocol_read_map_begin (ThriftProtocol *protocol,
return -1;
}

if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT(tp->transport),
sizei * thrift_binary_protocol_get_min_serialized_size(protocol, k, error) +
sizei * thrift_binary_protocol_get_min_serialized_size(protocol, v, error),
if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT(tp->transport),
(glong) sizei * thrift_binary_protocol_get_min_serialized_size(protocol, k, error) +
(glong) sizei * thrift_binary_protocol_get_min_serialized_size(protocol, v, error),
error))
{
return -1;
Expand Down Expand Up @@ -642,8 +642,8 @@ thrift_binary_protocol_read_list_begin (ThriftProtocol *protocol,
return -1;
}

if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT(tp->transport),
(sizei * thrift_binary_protocol_get_min_serialized_size(protocol, e, error)),
if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT(tp->transport),
((glong) sizei * thrift_binary_protocol_get_min_serialized_size(protocol, e, error)),
error))
{
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,9 +1108,9 @@ thrift_compact_protocol_read_map_begin (ThriftProtocol *protocol,
return -1;
}

if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT (tp->transport),
msize * thrift_protocol_get_min_serialized_size (protocol, *key_type, error) +
msize * thrift_protocol_get_min_serialized_size (protocol, *value_type, error),
if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT (tp->transport),
(glong) msize * thrift_protocol_get_min_serialized_size (protocol, *key_type, error) +
(glong) msize * thrift_protocol_get_min_serialized_size (protocol, *value_type, error),
error))
{
return -1;
Expand Down Expand Up @@ -1183,8 +1183,8 @@ thrift_compact_protocol_read_list_begin (ThriftProtocol *protocol,
*element_type = ret;
*size = (guint32) lsize;

if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT (tp->transport),
(lsize * thrift_protocol_get_min_serialized_size (protocol, *element_type, error)),
if(!ttc->checkReadBytesAvailable (THRIFT_TRANSPORT (tp->transport),
((glong) lsize * thrift_protocol_get_min_serialized_size (protocol, *element_type, error)),
error))
{
return -1;
Expand Down
6 changes: 3 additions & 3 deletions lib/cpp/src/thrift/protocol/TBinaryProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,18 @@ class TBinaryProtocolT : public TVirtualProtocol<TBinaryProtocolT<Transport_, By

void checkReadBytesAvailable(TSet& set) override
{
trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
trans_->checkReadBytesAvailable(static_cast<long>(set.size_) * getMinSerializedSize(set.elemType_));
}

void checkReadBytesAvailable(TList& list) override
{
trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
trans_->checkReadBytesAvailable(static_cast<long>(list.size_) * getMinSerializedSize(list.elemType_));
}

void checkReadBytesAvailable(TMap& map) override
{
int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
trans_->checkReadBytesAvailable(map.size_ * elmSize);
trans_->checkReadBytesAvailable(static_cast<long>(map.size_) * elmSize);
}

protected:
Expand Down
6 changes: 3 additions & 3 deletions lib/cpp/src/thrift/protocol/TCompactProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ class TCompactProtocolT : public TVirtualProtocol<TCompactProtocolT<Transport_>

void checkReadBytesAvailable(TSet& set) override
{
trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
trans_->checkReadBytesAvailable(static_cast<long>(set.size_) * getMinSerializedSize(set.elemType_));
}

void checkReadBytesAvailable(TList& list) override
{
trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
trans_->checkReadBytesAvailable(static_cast<long>(list.size_) * getMinSerializedSize(list.elemType_));
}

void checkReadBytesAvailable(TMap& map) override
{
int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
trans_->checkReadBytesAvailable(map.size_ * elmSize);
trans_->checkReadBytesAvailable(static_cast<long>(map.size_) * elmSize);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/cpp/src/thrift/protocol/TJSONProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> {

void checkReadBytesAvailable(TSet& set) override
{
trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
trans_->checkReadBytesAvailable(static_cast<long>(set.size_) * getMinSerializedSize(set.elemType_));
}

void checkReadBytesAvailable(TList& list) override
{
trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
trans_->checkReadBytesAvailable(static_cast<long>(list.size_) * getMinSerializedSize(list.elemType_));
}

void checkReadBytesAvailable(TMap& map) override
{
int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
trans_->checkReadBytesAvailable(map.size_ * elmSize);
trans_->checkReadBytesAvailable(static_cast<long>(map.size_) * elmSize);
}

class LookaheadReader {
Expand Down
6 changes: 3 additions & 3 deletions lib/cpp/src/thrift/protocol/TProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,18 +604,18 @@ class TProtocol {

virtual void checkReadBytesAvailable(TSet& set)
{
ptrans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
ptrans_->checkReadBytesAvailable(static_cast<long>(set.size_) * getMinSerializedSize(set.elemType_));
}

virtual void checkReadBytesAvailable(TList& list)
{
ptrans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
ptrans_->checkReadBytesAvailable(static_cast<long>(list.size_) * getMinSerializedSize(list.elemType_));
}

virtual void checkReadBytesAvailable(TMap& map)
{
int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
ptrans_->checkReadBytesAvailable(map.size_ * elmSize);
ptrans_->checkReadBytesAvailable(static_cast<long>(map.size_) * elmSize);
}

std::shared_ptr<TTransport> ptrans_;
Expand Down
48 changes: 48 additions & 0 deletions lib/cpp/test/ThrifttReadCheckTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,54 @@ BOOST_AUTO_TEST_CASE(test_tthriftcompactprotocol_read_check_pass) {
BOOST_CHECK_NO_THROW(protocol->readString(eleven));
}

BOOST_AUTO_TEST_CASE(test_tthriftbinaryprotocol_container_size_overflow) {
std::shared_ptr<TConfiguration> config (new TConfiguration(1024));
std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
std::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));

uint32_t val = 0;
TType elemType = apache::thrift::protocol::T_STOP;
// 0x40000000 elements of min size 4 require 4 GiB; the product wraps to 0 in
// 32-bit math and used to slip past the MaxMessageSize check.
TList list(T_I32, 0x40000000);
protocol->writeListBegin(list.elemType_, list.size_);
protocol->writeListEnd();
BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
protocol->readListEnd();
}

BOOST_AUTO_TEST_CASE(test_tthriftcompactprotocol_container_size_overflow) {
std::shared_ptr<TConfiguration> config (new TConfiguration(1024));
std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
std::shared_ptr<TCompactProtocol> protocol(new TCompactProtocol(transport));

uint32_t val = 0;
TType elemType = apache::thrift::protocol::T_STOP;
// 0x10000000 elements of min size 16 (UUID) require 4 GiB; the product wraps
// to 0 in 32-bit math and used to slip past the MaxMessageSize check.
TList list(T_UUID, 0x10000000);
protocol->writeListBegin(list.elemType_, list.size_);
protocol->writeListEnd();
BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
protocol->readListEnd();
}

BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_container_size_overflow) {
std::shared_ptr<TConfiguration> config (new TConfiguration(1024));
std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
std::shared_ptr<TJSONProtocol> protocol(new TJSONProtocol(transport));

uint32_t val = 0;
TType elemType = apache::thrift::protocol::T_STOP;
// 0x10000000 elements of min size 16 (UUID) require 4 GiB; the product wraps
// to 0 in 32-bit math and used to slip past the MaxMessageSize check.
TList list(T_UUID, 0x10000000);
protocol->writeListBegin(list.elemType_, list.size_);
protocol->writeListEnd();
BOOST_CHECK_THROW(protocol->readListBegin(elemType, val), TTransportException);
protocol->readListEnd();
}

BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_read_check_exception) {
std::shared_ptr<TConfiguration> config (new TConfiguration(MAX_MESSAGE_SIZE));
std::shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer(config));
Expand Down
Loading