You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "sahvx655-wq (via GitHub)" <gi...@apache.org> on 2026/10/15 09:14:30 UTC
[PR] fix popCollectionId over-consuming the couchbase response body (brpc)
sahvx655-wq opened a new pull request, #3614:
URL: https://github.com/apache/brpc/pull/3614
### What problem does this PR solve?
Issue Number: N/A
Problem Summary:
`CouchbaseOperations::CouchbaseResponse::popCollectionId` in `src/brpc/couchbase.cpp` parses a `CB_COLLECTIONS_GET_CID` response whose `total_body_length` already counts the 12-byte extras (8-byte manifest id plus 4-byte collection id). On the success path it pops the 24-byte header, then pops 8 bytes for the manifest id, 4 bytes for the collection id, and then pops `total_body_length` again. Those 12 extras bytes are part of `total_body_length`, so the handler consumes 12 bytes too many and reads into the next pipelined response, desyncing every following response on the connection. The header fields come straight from the (untrusted) server peer, and a response with `extras_length >= 12` but `total_body_length < 12` reaches the fixed 8+4-byte reads with no lower bound.
### What is changed and the side effects?
Changed:
Reject the response when `total_body_length < 12` next to the existing `extras_length < 12` check, and pop only `total_body_length - 12` remaining body bytes after the extras so the handler consumes exactly one message. The sibling `pop*` handlers already size their value against the body, so only this one was affected.
`CouchbaseUnitTest.CollectionIdSuccessConsumesExactBody` feeds a valid GET_CID response followed by a second response and checks the second one is left intact: it fails on master (12 bytes of the next response are eaten, leaving 52 instead of 64) and passes with the fix.
Side effects:
- Performance effects: none
- Breaking backward compatibility: no
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix popCollectionId over-consuming the couchbase response body (brpc)
Posted by "sahvx655-wq (via GitHub) via dev" <de...@brpc.apache.org>.
sahvx655-wq commented on code in PR #3614:
URL: https://github.com/apache/brpc/pull/3614#discussion_r4737702685
##########
src/brpc/couchbase.cpp:
##########
@@ -1532,8 +1532,9 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId(
}
// Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes
- // collection id)
- if (header.extras_length < 12) {
+ // 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) {
Review Comment:
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.
##########
src/brpc/couchbase.cpp:
##########
@@ -1532,8 +1532,9 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId(
}
// Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes
- // collection id)
- if (header.extras_length < 12) {
+ // 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) {
butil::string_printf(&_err, "Extras too small to contain collection ID");
Review Comment:
Agreed, the old wording pointed at the wrong field once the condition covers both. It now prints both lengths and the requirement:
extras_length=12/total_body_length=4 is smaller than the 12 bytes a collection ID requires
That matters here because the two header fields are independent and peer-controlled, so when this fires the only useful thing in a log is which of the two was short. The sibling `pop*` handlers already report their computed value_size the same way, so this keeps the diagnostics in that shape.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix popCollectionId over-consuming the couchbase response body (brpc)
Posted by "sahvx655-wq (via GitHub) via dev" <de...@brpc.apache.org>.
sahvx655-wq commented on code in PR #3614:
URL: https://github.com/apache/brpc/pull/3614#discussion_r4756131272
##########
test/brpc_couchbase_unittest.cpp:
##########
@@ -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.
Review Comment:
Good point, reworded to "misalign the remaining responses in this batch". You're right that the socket read offset is untouched: by the time popCollectionId runs, the whole batch is already sitting in _buf, so the extra 12 bytes only move the frame boundaries inside that buffer, not the connection's read position.
The practical effect is the same class of garbage though, just scoped to the batch: the over-pop truncates the next frame's header, so the following pop* reads a magic/command out of the middle of a body and every later response in the same read parses 12 bytes off. Comment-only change in this push, the fix itself is unchanged.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix popCollectionId over-consuming the couchbase response body (brpc)
Posted by "Copilot (via GitHub) via dev" <de...@brpc.apache.org>.
Copilot commented on code in PR #3614:
URL: https://github.com/apache/brpc/pull/3614#discussion_r4731290744
##########
src/brpc/couchbase.cpp:
##########
@@ -1532,8 +1532,9 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId(
}
// Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes
- // collection id)
- if (header.extras_length < 12) {
+ // 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) {
Review Comment:
The newly added `total_body_length < 12` success-path guard is not covered by the tests: `CollectionResponseWithInconsistentBodyLength` sets `status = 1`, so `popCollectionId` returns through the error branch before reaching this condition. Please add a success response with `extras_length >= 12` and `total_body_length < 12`, followed by sentinel bytes, and assert that it is rejected without consuming the pipelined data; this protects the untrusted-header boundary the fix introduces.
##########
src/brpc/couchbase.cpp:
##########
@@ -1532,8 +1532,9 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId(
}
// Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes
- // collection id)
- if (header.extras_length < 12) {
+ // 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) {
butil::string_printf(&_err, "Extras too small to contain collection ID");
Review Comment:
This condition now also handles `total_body_length < 12`, but the error still says only that the extras are too small. For a response with `extras_length >= 12` and a short body, that message misidentifies the malformed header field and makes peer-side diagnostics misleading. Use wording that identifies the required 12-byte body/extras minimum.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix popCollectionId over-consuming the couchbase response body (brpc)
Posted by "chenBright (via GitHub) via dev" <de...@brpc.apache.org>.
chenBright commented on code in PR #3614:
URL: https://github.com/apache/brpc/pull/3614#discussion_r4748269687
##########
test/brpc_couchbase_unittest.cpp:
##########
@@ -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.
Review Comment:
Consider changing `desync the connection` to `misalign the remaining responses in this batch`. By the time `popCollectionId` is called, the protocol layer has already read the entire batch from the socket input according to the frame lengths. Consuming an extra 12 bytes here truncates the header of the next frame in the same batch, while the socket read position remains at the correct frame boundary.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix popCollectionId over-consuming the couchbase response body (brpc)
Posted by "sahvx655-wq (via GitHub) via dev" <de...@brpc.apache.org>.
sahvx655-wq commented on PR #3614:
URL: https://github.com/apache/brpc/pull/3614#issuecomment-7255344931
Rebased onto current master to clear a stale red X. The macOS `compile-with-make-cmake-protobuf21` failure on the old run wasn't from this change: it was `ssl_helper.cpp` failing to compile against the openssl@4 4.0.3 that landed on the runner image back in October, and that file is untouched here. All 29 checks are green on the rebased head.
The fix itself is unchanged, and master still over-consumes 12 bytes on the GET_CID success path. Anything you'd like adjusted?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] fix popCollectionId over-consuming the couchbase response body (brpc)
Posted by "chenBright (via GitHub) via dev" <de...@brpc.apache.org>.
chenBright merged PR #3614:
URL: https://github.com/apache/brpc/pull/3614
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org