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
18 changes: 18 additions & 0 deletions src/brpc/redis_reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,27 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& buf, int depth) {
" actually=" << len;
return PARSE_ERROR_ABSOLUTELY_WRONG;
}
// Enforce the cap while still waiting for CRLF, otherwise a peer
// that never sends the terminator can grow buf without bound (like
// RedisCommandParser does for inline commands). buf holds the first
// char plus the payload so far; allow one extra byte for a boundary
// '\r' whose matching '\n' hasn't arrived yet.
if (FLAGS_redis_max_allocation_size < 0 ||
len > (size_t)FLAGS_redis_max_allocation_size + 2) {
LOG(ERROR) << "simple string exceeds max allocation size! max="
<< FLAGS_redis_max_allocation_size
<< ", actually=" << len - 1;
return PARSE_ERROR_ABSOLUTELY_WRONG;
}
return PARSE_ERROR_NOT_ENOUGH_DATA;
}
const size_t len = str.size() - 1;
if (FLAGS_redis_max_allocation_size < 0 ||
len > (size_t)FLAGS_redis_max_allocation_size) {
LOG(ERROR) << "simple string exceeds max allocation size! max="
<< FLAGS_redis_max_allocation_size << ", actually=" << len;
return PARSE_ERROR_ABSOLUTELY_WRONG;
Comment on lines +156 to +160

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.

Good point. I now enforce the cap in the waiting-for-CRLF path too, so a peer that never sends the terminator can't grow buf past the limit (only up to the 2^32 hard bound before). Kept a one-byte '\r' allowance so a boundary CRLF split isn't rejected, matching how RedisCommandParser handles inline commands. Added an over-limit-before-CRLF test and a boundary-split test alongside the existing ones.

}
if (len < sizeof(_data.short_str)) {
// SSO short strings, including empty string.
_type = (fc == '-' ? REDIS_REPLY_ERROR : REDIS_REPLY_STATUS);
Expand Down
61 changes: 61 additions & 0 deletions test/brpc_redis_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,67 @@ TEST_F(RedisTest, memory_allocation_limits) {
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
}

{
// Simple string exceeding limit. Unlike bulk strings and arrays this
// branch had no cap, so a length >= 2^31 truncated the signed _length
// field to a negative value and later reads went out of bounds.
butil::IOBuf buf;
std::string large_status = "+";
large_status.append(2000, 'a');
large_status.append("\r\n");
buf.append(large_status);

brpc::RedisReply reply(&arena);
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
}

{
// Error string exceeding limit (same branch as simple string).
butil::IOBuf buf;
std::string large_error = "-";
large_error.append(2000, 'a');
large_error.append("\r\n");
buf.append(large_error);

brpc::RedisReply reply(&arena);
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
}

{
// Simple string exceeding limit before CRLF arrives. Without a cap on
// the waiting-for-CRLF path a peer that never sends the terminator
// could grow buf without bound.
butil::IOBuf buf;
std::string large_status = "+";
large_status.append(brpc::FLAGS_redis_max_allocation_size + 100, 'a');
buf.append(large_status);

brpc::RedisReply reply(&arena);
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err);
}

{
// A simple string exactly at the limit may have its CRLF split across
// reads; a lone trailing '\r' must not trip the cap early.
butil::IOBuf buf;
std::string boundary_status = "+";
boundary_status.append(brpc::FLAGS_redis_max_allocation_size, 'a');
boundary_status.push_back('\r');
buf.append(boundary_status);

brpc::RedisReply reply(&arena);
brpc::ParseError err = reply.ConsumePartialIOBuf(buf);
ASSERT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA, err);

buf.push_back('\n');
err = reply.ConsumePartialIOBuf(buf);
ASSERT_EQ(brpc::PARSE_OK, err);
ASSERT_EQ(brpc::FLAGS_redis_max_allocation_size, (int)reply.size());
}

// Test redis_command.cpp limits
{
// Test command string exceeding limit
Expand Down
Loading