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
5 changes: 4 additions & 1 deletion lib/cpp/src/thrift/transport/THeaderTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ void THeaderTransport::readHeaderFormat(uint16_t headerSize, uint32_t sz) {
}
headerSize *= 4;
const uint8_t* const headerBoundary = ptr + headerSize;
if (headerSize > sz) {
// ptr already skips the 10-byte common header, so the header section has to
// fit in the remaining sz - 10 bytes; comparing against sz alone let the
// boundary sit up to 10 bytes past the receive buffer.
if (headerSize > sz - 10) {
throw TTransportException(TTransportException::CORRUPTED_DATA,
"Header size is larger than frame");
}
Expand Down
23 changes: 23 additions & 0 deletions lib/cpp/test/ThrifttReadCheckTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <memory>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/THeaderTransport.h>
#include <thrift/transport/TSimpleFileTransport.h>
#include <thrift/transport/TFileTransport.h>
#include <thrift/protocol/TEnum.h>
Expand Down Expand Up @@ -317,4 +318,26 @@ BOOST_AUTO_TEST_CASE(test_tthriftjsonprotocol_read_check_exception) {
protocol->readMapEnd();
}

BOOST_AUTO_TEST_CASE(test_theadertransport_header_size_exceeds_frame) {
using apache::thrift::transport::THeaderTransport;
// Header-format frame whose declared header size (3 * 4 = 12) leaves fewer
// than the 10 common-header bytes inside the 14-byte frame. The trailing
// varint bytes are all continuation bytes, so the reader used to run off the
// end of the receive buffer.
uint8_t frame[] = {
0x00, 0x00, 0x00, 0x0E, // frame length = 14
0x0F, 0xFF, 0x00, 0x00, // header magic
0x00, 0x00, 0x00, 0x00, // seqId
0x00, 0x03, // header size field (3 -> 12 bytes)
0x02, // protocol id varint
0x00, // num transforms = 0
0x80, 0x80 // info-header varint, all continuation
};
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(frame, sizeof(frame)));
std::shared_ptr<THeaderTransport> trans(new THeaderTransport(buffer));

uint8_t out[1];
BOOST_CHECK_THROW(trans->read(out, sizeof(out)), TTransportException);
}

BOOST_AUTO_TEST_SUITE_END()
Loading