From 1199c8c378264e953aa2a31c20c25ffa7414d368 Mon Sep 17 00:00:00 2001 From: wwbmmm Date: Sat, 15 Aug 2026 14:33:26 +0800 Subject: [PATCH 1/2] Fix bug when parsing zero-length string field in mcpack2pb UnparsedValue::as_string() resizes the output string to without checking , where is the value_size of a string field read from the input. When value_size is 0, underflows to SIZE_MAX and resize() throws std::length_error, which is not caught on the request path and therefore crashes the server. Reject such malformed fields by marking the stream bad so the caller can fail the request gracefully instead. --- src/mcpack2pb/parser.cpp | 7 +++ test/brpc_mcpack2pb_unittest.cpp | 104 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 test/brpc_mcpack2pb_unittest.cpp diff --git a/src/mcpack2pb/parser.cpp b/src/mcpack2pb/parser.cpp index 5c785dc45f..d1d6cfb225 100644 --- a/src/mcpack2pb/parser.cpp +++ b/src/mcpack2pb/parser.cpp @@ -577,6 +577,13 @@ double UnparsedValue::as_double(const char* var) { } void UnparsedValue::as_string(std::string* out, const char* var) { + if (_size < 1) { + // A string field must contain at least the trailing '\0'. + // Reject _size == 0 here, otherwise `_size - 1' underflows and + // resize() throws an uncaught exception. + _stream->set_bad(); + return; + } out->resize(_size - 1); if (_stream->cutn(&(*out)[0], _size - 1) != _size - 1) { CHECK(false) << "Not enough data for " << var; diff --git a/test/brpc_mcpack2pb_unittest.cpp b/test/brpc_mcpack2pb_unittest.cpp new file mode 100644 index 0000000000..159cbdddfa --- /dev/null +++ b/test/brpc_mcpack2pb_unittest.cpp @@ -0,0 +1,104 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Unit tests for the mcpack2pb parser. + +#include +#include "butil/iobuf.h" +#include "mcpack2pb/parser.h" + +namespace { + +TEST(Mcpack2pbParserTest, StringFieldWithZeroValueSize) { + // A 51-byte mcpack2 frame whose `service_name' string field has + // value_size == 0. This used to underflow in UnparsedValue::as_string() + // (resize(_size - 1) with _size == 0) and throw std::length_error, which + // is not caught on the request path and therefore crashes the server. + const unsigned char data[] = { + 0x10, 0x00, 0x2d, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, + 0xa0, 0x08, 0x1e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x00, + 0x01, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, + 0xd0, 0x0d, 0x00, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x00, + }; + butil::IOBuf body; + body.append(data, sizeof(data)); + + butil::IOBufAsZeroCopyInputStream zc_stream(body); + mcpack2pb::InputStream stream(&zc_stream); + ASSERT_NE(0u, mcpack2pb::unbox(&stream)); + + mcpack2pb::ObjectIterator it1(&stream, body.size() - stream.popped_bytes()); + bool found_content = false; + for (; it1 != NULL; ++it1) { + if (it1->name == "content") { + found_content = true; + break; + } + } + ASSERT_TRUE(found_content); + ASSERT_EQ(mcpack2pb::FIELD_ARRAY, it1->value.type()); + + mcpack2pb::ArrayIterator it2(it1->value); + ASSERT_TRUE(it2 != NULL); + bool found_service_name = false; + for (mcpack2pb::ObjectIterator it3(*it2); it3 != NULL; ++it3) { + if (it3->name == "service_name") { + found_service_name = true; + ASSERT_EQ(mcpack2pb::FIELD_STRING, it3->value.type()); + std::string service_name; + it3->value.as_string(&service_name, "service_name"); + // A zero-sized string field must be rejected gracefully instead of + // throwing (resize(SIZE_MAX)) and crashing the process. + EXPECT_FALSE(it3->value.stream()->good()); + EXPECT_TRUE(service_name.empty()); + break; + } + } + ASSERT_TRUE(found_service_name); +} + +TEST(Mcpack2pbParserTest, ParseStringField) { + // A valid object {"msg":"abc"}. + const unsigned char data[] = { + 0x10, 0x00, 0x0f, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, + 0xd0, 0x04, 0x04, + 0x6d, 0x73, 0x67, 0x00, + 0x61, 0x62, 0x63, 0x00, + }; + butil::IOBuf body; + body.append(data, sizeof(data)); + + butil::IOBufAsZeroCopyInputStream zc_stream(body); + mcpack2pb::InputStream stream(&zc_stream); + ASSERT_NE(0u, mcpack2pb::unbox(&stream)); + + mcpack2pb::ObjectIterator it(&stream, body.size() - stream.popped_bytes()); + ASSERT_TRUE(it != NULL); + EXPECT_EQ("msg", it->name.as_string()); + ASSERT_EQ(mcpack2pb::FIELD_STRING, it->value.type()); + std::string value; + it->value.as_string(&value, "msg"); + EXPECT_EQ("abc", value); + EXPECT_TRUE(stream.good()); +} + +} // namespace From 37bbeff42e66617f60bb29ac8e765a2b7cbc61e0 Mon Sep 17 00:00:00 2001 From: wwbmmm Date: Sat, 15 Aug 2026 15:04:29 +0800 Subject: [PATCH 2/2] Clear output string when size error --- src/mcpack2pb/parser.cpp | 4 +++- test/brpc_mcpack2pb_unittest.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mcpack2pb/parser.cpp b/src/mcpack2pb/parser.cpp index d1d6cfb225..92d7a22021 100644 --- a/src/mcpack2pb/parser.cpp +++ b/src/mcpack2pb/parser.cpp @@ -580,7 +580,9 @@ void UnparsedValue::as_string(std::string* out, const char* var) { if (_size < 1) { // A string field must contain at least the trailing '\0'. // Reject _size == 0 here, otherwise `_size - 1' underflows and - // resize() throws an uncaught exception. + // resize() throws an uncaught exception. Clear `out' so callers + // that reuse the string do not keep a stale value. + out->clear(); _stream->set_bad(); return; } diff --git a/test/brpc_mcpack2pb_unittest.cpp b/test/brpc_mcpack2pb_unittest.cpp index 159cbdddfa..e3c78a6acd 100644 --- a/test/brpc_mcpack2pb_unittest.cpp +++ b/test/brpc_mcpack2pb_unittest.cpp @@ -63,11 +63,13 @@ TEST(Mcpack2pbParserTest, StringFieldWithZeroValueSize) { if (it3->name == "service_name") { found_service_name = true; ASSERT_EQ(mcpack2pb::FIELD_STRING, it3->value.type()); - std::string service_name; + std::string service_name = "stale"; it3->value.as_string(&service_name, "service_name"); // A zero-sized string field must be rejected gracefully instead of // throwing (resize(SIZE_MAX)) and crashing the process. EXPECT_FALSE(it3->value.stream()->good()); + // The output string must be cleared so callers that reuse the + // string do not keep a stale value. EXPECT_TRUE(service_name.empty()); break; }