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
16 changes: 12 additions & 4 deletions src/brpc/couchbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1532,9 +1532,14 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId(
}

// Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes
// collection id)
if (header.extras_length < 12) {
butil::string_printf(&_err, "Extras too small to contain collection ID");
// collection id). total_body_length counts those extras, so it must be at
// least 12 as well; both fields are attacker-controlled and independent.
if (header.extras_length < 12 || header.total_body_length < 12) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, the existing case bails out in the error branch before it ever reaches the new guard. Added CollectionIdSuccessConsumesExactBody's sibling, CollectionIdSuccessBodyShorterThanExtras: status=0, extras_length=12, total_body_length=4, four body bytes, then a 64-byte sentinel response.

Worth noting what that case does without the guard, because it's worse than the pipelined over-read alone: extras_length passes on its own, so the success path copies 8 then 4 bytes out of a 4-byte body, reads 12 bytes of the sentinel as a manifest/collection id, returns true with a collection id derived from the next response, and then pops total_body_length - 12, which underflows to a huge size_t. The test asserts false, collection_id left at its sentinel 7, and rawBuffer() back to exactly 64 bytes, so only the malformed message is dropped and the connection stays framed.

butil::string_printf(&_err,
"extras_length=%u/total_body_length=%u is smaller "
"than the 12 bytes a collection ID requires",
static_cast<unsigned>(header.extras_length),
static_cast<unsigned>(header.total_body_length));
// remove the response from buffer so you don't re‐process
_buf.pop_front(sizeof(header) + header.total_body_length);
return false;
Expand All @@ -1559,7 +1564,10 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId(
*collection_id = static_cast<int>(cid_host);
_buf.pop_front(sizeof(cid_net));

_buf.pop_front(header.total_body_length);
// The 12 extras bytes read above are part of total_body_length; only the
// remainder of the body is still in the buffer. Popping total_body_length
// again would consume 12 bytes of the next pipelined response.
_buf.pop_front(header.total_body_length - 12);
_err.clear();
return true;
}
Expand Down
57 changes: 57 additions & 0 deletions test/brpc_couchbase_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,63 @@ TEST_F(CouchbaseUnitTest, CollectionResponseWithInconsistentBodyLength) {
EXPECT_EQ(sizeof(header) + next_response.size(), res2.rawBuffer().size());
}

// total_body_length already counts the 12-byte extras (8-byte manifest id +
// 4-byte collection id). popCollectionId must consume exactly one message on
// success; popping total_body_length again after the extras would eat 12 bytes
// of the next pipelined response and desync the connection.
TEST_F(CouchbaseUnitTest, CollectionIdSuccessConsumesExactBody) {
const std::string next_response(64, 'A');

brpc::policy::CouchbaseResponseHeader header = {};
header.magic = brpc::policy::CB_MAGIC_RESPONSE;
header.command = brpc::policy::CB_COLLECTIONS_GET_CID;
header.status = 0;
header.extras_length = 12;
header.total_body_length = 12; // 8-byte manifest id + 4-byte collection id

char extras[12] = {0};
extras[11] = 42; // collection id in the low byte of the 4-byte field

brpc::CouchbaseOperations::CouchbaseResponse res;
res.rawBuffer().append(&header, sizeof(header));
res.rawBuffer().append(extras, sizeof(extras));
res.rawBuffer().append(next_response);

uint8_t collection_id = 0;
EXPECT_TRUE(res.popCollectionId(&collection_id));
EXPECT_EQ(42, collection_id);
// Exactly one message consumed; the following response is left intact.
EXPECT_EQ(next_response.size(), res.rawBuffer().size());
}

// A success response may claim extras_length >= 12 while total_body_length is
// shorter than the 12 bytes the manifest id + collection id reads need. The
// guard must reject it and drop exactly the malformed message, leaving the next
// pipelined response aligned.
TEST_F(CouchbaseUnitTest, CollectionIdSuccessBodyShorterThanExtras) {
const std::string next_response(64, 'A');
const std::string body(4, 'B');

brpc::policy::CouchbaseResponseHeader header = {};
header.magic = brpc::policy::CB_MAGIC_RESPONSE;
header.command = brpc::policy::CB_COLLECTIONS_GET_CID;
header.status = 0;
header.extras_length = 12;
// Shorter than the 12-byte extras the success path reads.
header.total_body_length = static_cast<uint32_t>(body.size());

brpc::CouchbaseOperations::CouchbaseResponse res;
res.rawBuffer().append(&header, sizeof(header));
res.rawBuffer().append(body);
res.rawBuffer().append(next_response);

uint8_t collection_id = 7;
EXPECT_FALSE(res.popCollectionId(&collection_id));
EXPECT_EQ(7, collection_id);
// Only the malformed message is dropped; the next response is untouched.
EXPECT_EQ(next_response.size(), res.rawBuffer().size());
}

TEST_F(CouchbaseUnitTest, EdgeCases) {
brpc::CouchbaseOperations::CouchbaseRequest req;
req.addRequest("", "value", 0, 0, 0);
Expand Down
Loading