This is an automated email from the ASF dual-hosted git repository.

chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 896b29d27 Enforce -redis_max_allocation_size on all strings of a redis 
message (#3722)
896b29d27 is described below

commit 896b29d2753a982ae5df0d3bf365803b60a65b99
Author: Weibing Wang <[email protected]>
AuthorDate: Tue Nov 10 12:31:04 2026 +0800

    Enforce -redis_max_allocation_size on all strings of a redis message (#3722)
    
    * enforce -redis_max_allocation_size on the total of a redis message
    
    The cap was applied to each bulk string/array allocation separately, so a
    RESP command or reply carrying several individually allowed strings could
    make the parsers allocate far more than the configured budget. Charge the
    string payloads of a message to one shared budget: RedisCommandParser keeps
    it in _used_bytes for the command being parsed (parsed arguments stay in
    the connection arena until the command is complete), and RedisReply charges
    them to the budget already shared by the reply tree, which also survives
    parsing suspension.
    
    * gate redis allocation budget on a positive -redis_max_allocation_size
    
    Follow the pre-existing style of the parsers, which only enforce the
    cumulative budget when the flag is positive, and document why string
    payloads rather than their 8-byte aligned arena size are charged.
    
    ---------
    
    Co-authored-by: brpc-oncall <brpc-oncall@localhost>
---
 src/brpc/redis_command.cpp   |  18 +++++-
 src/brpc/redis_command.h     |   4 ++
 src/brpc/redis_reply.cpp     |  37 +++++++++---
 test/brpc_redis_unittest.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 182 insertions(+), 10 deletions(-)

diff --git a/src/brpc/redis_command.cpp b/src/brpc/redis_command.cpp
index 5f30d0496..fb82ef691 100644
--- a/src/brpc/redis_command.cpp
+++ b/src/brpc/redis_command.cpp
@@ -454,7 +454,8 @@ butil::Status RedisCommandByComponents(butil::IOBuf* output,
 RedisCommandParser::RedisCommandParser()
     : _parsing_array(false)
     , _length(0)
-    , _index(0) {}
+    , _index(0)
+    , _used_bytes(0) {}
 
 size_t RedisCommandParser::ParsedArgsSize() {
     return _args.size();
@@ -657,6 +658,19 @@ RedisCommandConsumeState 
RedisCommandParser::ConsumeImpl(butil::IOBuf& buf,
         *err = PARSE_ERROR_ABSOLUTELY_WRONG;
         return CONSUME_STATE_ERROR;
     }
+    // Parsed arguments stay in the connection arena until the whole command
+    // has arrived, so their sum, not only each argument, must be bounded by
+    // -redis_max_allocation_size. Payload bytes are charged here; the _args
+    // descriptors are bounded by the array-count check above.
+    if (FLAGS_redis_max_allocation_size > 0 &&
+        (uint64_t)_used_bytes + (uint64_t)len >
+            (uint64_t)FLAGS_redis_max_allocation_size) {
+        LOG(ERROR) << "accumulated command allocation exceeds max allocation "
+                      "size! max=" << FLAGS_redis_max_allocation_size
+                   << ", already=" << _used_bytes << ", requesting=" << len;
+        *err = PARSE_ERROR_ABSOLUTELY_WRONG;
+        return CONSUME_STATE_ERROR;
+    }
     if (buf.size() < crlf_pos + 2 + (size_t)len + 2/*CRLF*/) {
         *err = PARSE_ERROR_NOT_ENOUGH_DATA;
         return CONSUME_STATE_ERROR;
@@ -671,6 +685,7 @@ RedisCommandConsumeState 
RedisCommandParser::ConsumeImpl(butil::IOBuf& buf,
     }
     buf.cutn(d, len);
     d[len] = '\0';
+    _used_bytes += len;
     if (_args.size() <= (size_t)_index) {
         // Grow _args as the arguments arrive instead of upfront.
         _args.resize(_index + 1);
@@ -699,6 +714,7 @@ void RedisCommandParser::Reset() {
     _parsing_array = false;
     _length = 0;
     _index = 0;
+    _used_bytes = 0;
     _args.clear();
 }
 
diff --git a/src/brpc/redis_command.h b/src/brpc/redis_command.h
index 25bd8859f..d561cc3b5 100644
--- a/src/brpc/redis_command.h
+++ b/src/brpc/redis_command.h
@@ -76,6 +76,10 @@ private:
     bool _parsing_array;            // if the parser has met array indicator 
'*'
     int _length;                    // array length
     int _index;                     // current parsing array index
+    // Bytes of string payload allocated in the arena for the command being
+    // parsed. It is bounded by -redis_max_allocation_size and reset once the
+    // command is complete.
+    size_t _used_bytes;
     std::vector<butil::StringPiece> _args;  // parsed command string
 };
 
diff --git a/src/brpc/redis_reply.cpp b/src/brpc/redis_reply.cpp
index ee77b3e6f..7aded0e00 100644
--- a/src/brpc/redis_reply.cpp
+++ b/src/brpc/redis_reply.cpp
@@ -32,6 +32,25 @@ DEFINE_int32(redis_max_reply_depth, 128,
 //BAIDU_CASSERT(sizeof(RedisReply) == 24, size_match);
 const int RedisReply::npos = -1;
 
+// Charge `need' payload bytes of a string to the per-reply budget kept in
+// `used_bytes'. Returns false if the budget would be exceeded, in which case
+// nothing is charged. The arena rounds each string up to 8 bytes; that padding
+// is not charged so that a string of exactly -redis_max_allocation_size bytes
+// stays valid, as the per-string checks allow, and it is bounded
+// constant-factor overhead.
+static bool ChargeReplyAllocation(uint32_t* used_bytes, uint64_t need) {
+    if (FLAGS_redis_max_allocation_size > 0 &&
+        (uint64_t)*used_bytes + need >
+            (uint64_t)FLAGS_redis_max_allocation_size) {
+        LOG(ERROR) << "accumulated reply allocation exceeds max allocation "
+                      "size! max=" << FLAGS_redis_max_allocation_size
+                   << ", already=" << *used_bytes << ", requesting=" << need;
+        return false;
+    }
+    *used_bytes += need;
+    return true;
+}
+
 const char* RedisReplyTypeToString(RedisReplyType type) {
     switch (type) {
     case REDIS_REPLY_STRING: return "string";
@@ -186,6 +205,11 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& 
buf, int depth,
             str.copy_to_cstr(_data.short_str, (size_t)-1L, 1/*skip fc*/);
             return PARSE_OK;
         }
+        // Charge the payload to the per-reply budget so that an array of
+        // individually allowed strings cannot exceed the cap in total.
+        if (!ChargeReplyAllocation(used_bytes, len)) {
+            return PARSE_ERROR_ABSOLUTELY_WRONG;
+        }
         char* d = (char*)_arena->allocate((len/8 + 1)*8);
         if (d == nullptr) {
             LOG(FATAL) << "Fail to allocate string[" << len << "]";
@@ -246,6 +270,9 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& 
buf, int depth,
                 buf.cutn(_data.short_str, len);
                 _data.short_str[len] = '\0';
             } else {
+                if (!ChargeReplyAllocation(used_bytes, len)) {
+                    return PARSE_ERROR_ABSOLUTELY_WRONG;
+                }
                 char* d = (char*)_arena->allocate((len/8 + 1)*8);
                 if (d == nullptr) {
                     LOG(FATAL) << "Fail to allocate string[" << len << "]";
@@ -291,14 +318,7 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& 
buf, int depth,
             // Charge the allocation to the per-reply budget so that nested
             // arrays cannot multiply -redis_max_allocation_size by depth.
             const size_t need = sizeof(RedisReply) * count;
-            if (FLAGS_redis_max_allocation_size > 0 &&
-                (uint64_t)*used_bytes + need >
-                    (uint64_t)FLAGS_redis_max_allocation_size) {
-                LOG(ERROR) << "accumulated array allocation exceeds max "
-                              "allocation size! max="
-                           << FLAGS_redis_max_allocation_size
-                           << ", already=" << *used_bytes
-                           << ", requesting=" << need;
+            if (!ChargeReplyAllocation(used_bytes, need)) {
                 return PARSE_ERROR_ABSOLUTELY_WRONG;
             }
             // FIXME(gejun): Call allocate_aligned instead.
@@ -307,7 +327,6 @@ ParseError RedisReply::ConsumePartialIOBuf(butil::IOBuf& 
buf, int depth,
                 LOG(FATAL) << "Fail to allocate RedisReply[" << count << "]";
                 return PARSE_ERROR_ABSOLUTELY_WRONG;
             }
-            *used_bytes += need;
             for (int64_t i = 0; i < count; ++i) {
                 new (&subs[i]) RedisReply(_arena);
             }
diff --git a/test/brpc_redis_unittest.cpp b/test/brpc_redis_unittest.cpp
index 7e8d0be01..87d1ce933 100644
--- a/test/brpc_redis_unittest.cpp
+++ b/test/brpc_redis_unittest.cpp
@@ -1168,6 +1168,139 @@ TEST_F(RedisTest, 
redis_reply_rejects_nested_array_memory_amplification) {
     }
 }
 
+TEST_F(RedisTest, redis_reply_charges_strings_to_the_budget) {
+    // Every string below is individually within -redis_max_allocation_size,
+    // but their total must not exceed it: the cap bounds one reply, not one
+    // bulk string.
+    ScopedRedisMaxAllocationSize scoped_size(1024);
+    const std::string value(600, 'a');
+
+    // Two bulk strings of 600 bytes, 1200 bytes in total.
+    {
+        butil::IOBuf buf;
+        buf.append("*2\r\n$600\r\n");
+        buf.append(value);
+        buf.append("\r\n$600\r\n");
+        buf.append(value);
+        buf.append("\r\n");
+        butil::Arena arena;
+        brpc::RedisReply reply(&arena);
+        EXPECT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG,
+                  reply.ConsumePartialIOBuf(buf));
+    }
+
+    // Simple strings were not charged to the budget at all.
+    {
+        butil::IOBuf buf;
+        buf.append("*2\r\n+");
+        buf.append(value);
+        buf.append("\r\n+");
+        buf.append(value);
+        buf.append("\r\n");
+        butil::Arena arena;
+        brpc::RedisReply reply(&arena);
+        EXPECT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG,
+                  reply.ConsumePartialIOBuf(buf));
+    }
+
+    // The budget must survive suspension: the second string arrives only
+    // after a resume, a fresh budget per call would let the peer reset it.
+    {
+        butil::IOBuf buf;
+        buf.append("*2\r\n$600\r\n");
+        buf.append(value);
+        buf.append("\r\n");
+        butil::Arena arena;
+        brpc::RedisReply reply(&arena);
+        EXPECT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA,
+                  reply.ConsumePartialIOBuf(buf));
+        buf.append("$600\r\n");
+        buf.append(value);
+        buf.append("\r\n");
+        EXPECT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG,
+                  reply.ConsumePartialIOBuf(buf));
+    }
+
+    // A reply whose strings add up to exactly the budget is still accepted.
+    {
+        const int32_t payload = brpc::FLAGS_redis_max_allocation_size -
+                                2 * (int32_t)sizeof(brpc::RedisReply);
+        const std::string first(payload / 2, 'a');
+        const std::string second(payload - payload / 2, 'b');
+        butil::IOBuf buf;
+        buf.append("*2\r\n$" + std::to_string(first.size()) + "\r\n");
+        buf.append(first);
+        buf.append("\r\n$" + std::to_string(second.size()) + "\r\n");
+        buf.append(second);
+        buf.append("\r\n");
+        butil::Arena arena;
+        brpc::RedisReply reply(&arena);
+        EXPECT_EQ(brpc::PARSE_OK, reply.ConsumePartialIOBuf(buf));
+        ASSERT_EQ(2u, reply.size());
+        EXPECT_EQ(first, reply[0].data().as_string());
+        EXPECT_EQ(second, reply[1].data().as_string());
+    }
+}
+
+TEST_F(RedisTest, command_parser_charges_bulk_strings_to_the_budget) {
+    // Parsed arguments stay in the connection arena while the rest of a
+    // command is pending, so the cap must bound their sum, not each one.
+    ScopedRedisMaxAllocationSize scoped_size(1024);
+    const std::string value(600, 'a');
+
+    {
+        brpc::RedisCommandParser parser;
+        butil::Arena arena;
+        butil::IOBuf buf;
+        buf.append("*3\r\n$4\r\nmset\r\n$600\r\n");
+        buf.append(value);
+        buf.append("\r\n$600\r\n");
+        buf.append(value);
+        buf.append("\r\n");
+        std::vector<butil::StringPiece> args;
+        EXPECT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG,
+                  parser.Consume(buf, &args, &arena));
+    }
+
+    // The budget must survive suspension between partial reads.
+    {
+        brpc::RedisCommandParser parser;
+        butil::Arena arena;
+        butil::IOBuf buf;
+        buf.append("*2\r\n$600\r\n");
+        buf.append(value);
+        buf.append("\r\n");
+        std::vector<butil::StringPiece> args;
+        EXPECT_EQ(brpc::PARSE_ERROR_NOT_ENOUGH_DATA,
+                  parser.Consume(buf, &args, &arena));
+        buf.append("$600\r\n");
+        buf.append(value);
+        buf.append("\r\n");
+        EXPECT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG,
+                  parser.Consume(buf, &args, &arena));
+    }
+
+    // A command whose arguments add up to exactly the budget still parses.
+    {
+        const std::string first(510, 'a');
+        const std::string second(510, 'b');
+        brpc::RedisCommandParser parser;
+        butil::Arena arena;
+        butil::IOBuf buf;
+        buf.append("*3\r\n$4\r\nmset\r\n$510\r\n");
+        buf.append(first);
+        buf.append("\r\n$510\r\n");
+        buf.append(second);
+        buf.append("\r\n");
+        std::vector<butil::StringPiece> args;
+        EXPECT_EQ(brpc::PARSE_OK, parser.Consume(buf, &args, &arena));
+        ASSERT_EQ(3u, args.size());
+        EXPECT_EQ("mset", args[0].as_string());
+        EXPECT_EQ(first, args[1].as_string());
+        EXPECT_EQ(second, args[2].as_string());
+    }
+}
+
 TEST_F(RedisTest, command_parser_does_not_preallocate_declared_args) {
     // A declared RESP array count must not commit memory before the arguments
     // actually arrive: "*<count>\r\n" used to resize _args upfront, costing


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to