Fix HPACK/QPACK decoding of excessively large variable-length integers. HPACK decoders will emit a compression error if a variable-length integer, used for index or string length, exceeds `uint32_t::max()`. The fix to index decoding will actually fail any HPACK entry with index value larger than 2^32 received on the wire which is currently decoded into a wrong 32-bit integer. We are already failing excessively long string literals in current implementation of HPACK and QPACK, so the fix to string length decoding will just fail the entry in earlier stage. But on a 32-bit platform, the current implementation won't be sufficient. Protected by FLAGS_gfe2_reloadable_flag_http2_hpack_varint_decoding_fix, default true. PiperOrigin-RevId: 795187015
diff --git a/quiche/common/quiche_feature_flags_list.h b/quiche/common/quiche_feature_flags_list.h index e066f54..2bca369 100755 --- a/quiche/common/quiche_feature_flags_list.h +++ b/quiche/common/quiche_feature_flags_list.h
@@ -10,6 +10,7 @@ QUICHE_FLAG(bool, quiche_reloadable_flag_enable_h3_origin_frame, false, true, "If true, enables support for parsing HTTP/3 ORIGIN frames.") QUICHE_FLAG(bool, quiche_reloadable_flag_enable_tls_trust_anchor_ids, true, true, "When true, QUIC client and server will support TLS Trust Anchor IDs.") +QUICHE_FLAG(bool, quiche_reloadable_flag_http2_hpack_varint_decoding_fix, true, true, "If true, HPACK decoders will only allow variable-length integer to be less than 32 bit, and emit compression error otherwise.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_act_upon_invalid_header, true, true, "If true, reject or send error response code upon receiving invalid request or response headers.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_add_stream_info_to_idle_close_detail, false, true, "If true, include stream information in idle timeout connection close detail.") QUICHE_FLAG(bool, quiche_reloadable_flag_quic_allow_client_enabled_2x_initial_cwnd, true, true, "Doubles the initial congestion window for QUIC connections when initiated by the client")
diff --git a/quiche/http2/hpack/decoder/hpack_entry_decoder.cc b/quiche/http2/hpack/decoder/hpack_entry_decoder.cc index 41edf54..294a5ab 100644 --- a/quiche/http2/hpack/decoder/hpack_entry_decoder.cc +++ b/quiche/http2/hpack/decoder/hpack_entry_decoder.cc
@@ -65,6 +65,15 @@ DecodeStatus status = entry_type_decoder_.Start(db); switch (status) { case DecodeStatus::kDecodeDone: + if (GetQuicheReloadableFlag(http2_hpack_varint_decoding_fix) && + entry_type_decoder_.varint() > std::numeric_limits<uint32_t>::max()) { + QUICHE_VLOG(2) + << "Invalid varint for index: " << entry_type_decoder_.varint() + << ". The index should be representable using 4 bytes as the max " + "table size is negotiated using a 4 bytes SETTING"; + error_ = HpackDecodingError::kIndexVarintError; + return DecodeStatus::kDecodeError; + } // The type of the entry and its varint fit into the current decode // buffer. if (entry_type_decoder_.entry_type() == HpackEntryType::kIndexedHeader) { @@ -120,6 +129,16 @@ // entry_type_decoder_ returned kDecodeDone, now need to decide how // to proceed. QUICHE_DVLOG(1) << "kDecodedType: db->Remaining=" << db->Remaining(); + if (GetQuicheReloadableFlag(http2_hpack_varint_decoding_fix) && + entry_type_decoder_.varint() > + std::numeric_limits<uint32_t>::max()) { + error_ = HpackDecodingError::kIndexVarintError; + QUICHE_VLOG(2) + << "Invalid varint for index: " << entry_type_decoder_.varint() + << ". The index should be representable using 4 bytes as the max " + "table size is negotiated using a 4 bytes SETTING"; + return DecodeStatus::kDecodeError; + } if (DispatchOnType(listener)) { // All done. return DecodeStatus::kDecodeDone; @@ -223,6 +242,8 @@ bool HpackEntryDecoder::DispatchOnType(HpackEntryDecoderListener* listener) { const HpackEntryType entry_type = entry_type_decoder_.entry_type(); const uint32_t varint = static_cast<uint32_t>(entry_type_decoder_.varint()); + QUICHE_DCHECK(!GetQuicheReloadableFlag(http2_hpack_varint_decoding_fix) || + varint == entry_type_decoder_.varint()); switch (entry_type) { case HpackEntryType::kIndexedHeader: // The entry consists solely of the entry type and varint. See:
diff --git a/quiche/http2/hpack/decoder/hpack_entry_decoder_test.cc b/quiche/http2/hpack/decoder/hpack_entry_decoder_test.cc index 684bd41..e1c10b8 100644 --- a/quiche/http2/hpack/decoder/hpack_entry_decoder_test.cc +++ b/quiche/http2/hpack/decoder/hpack_entry_decoder_test.cc
@@ -14,6 +14,7 @@ #include "quiche/http2/test_tools/http2_random.h" #include "quiche/http2/test_tools/random_decoder_test_base.h" #include "quiche/common/platform/api/quiche_expect_bug.h" +#include "quiche/common/platform/api/quiche_flags.h" #include "quiche/common/platform/api/quiche_test.h" namespace http2 { @@ -80,6 +81,28 @@ DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); EXPECT_TRUE(do_check()); } + collector_.Clear(); + + const char input[] = { + '\xff', '\x92', '\xff', '\xff', '\xff', '\xff', + '\xff', '\xff', '\xff', '\xff', '\x00'}; // == Index 2^63 + 17 == + if (GetQuicheReloadableFlag(http2_hpack_varint_decoding_fix)) { + DecodeBuffer b(input); + EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateError())); + EXPECT_EQ(decoder_.error(), HpackDecodingError::kIndexVarintError); + } else { + DecodeBuffer b1(input); + auto do_check1 = [this]() { + return collector_.ValidateIndexedHeader((1ULL << 63) + 17); + }; + EXPECT_TRUE(DecodeSegmentsAndValidate(&b1, SelectRemaining(), + ValidateDoneAndEmpty(do_check1))); + // If decoding 1 byte at a time, the decoder will return a wrong index. + DecodeBuffer b2(input); + auto do_check2 = [this]() { return collector_.ValidateIndexedHeader(17); }; + EXPECT_TRUE(DecodeSegmentsAndValidate(&b2, SelectOne(), + ValidateDoneAndEmpty(do_check2))); + } } TEST_F(HpackEntryDecoderTest, IndexedHeader_Various) {
diff --git a/quiche/http2/hpack/decoder/hpack_string_decoder.h b/quiche/http2/hpack/decoder/hpack_string_decoder.h index 897ce76..14ba7b1 100644 --- a/quiche/http2/hpack/decoder/hpack_string_decoder.h +++ b/quiche/http2/hpack/decoder/hpack_string_decoder.h
@@ -20,6 +20,7 @@ #include "quiche/http2/decoder/decode_status.h" #include "quiche/http2/hpack/varint/hpack_varint_decoder.h" #include "quiche/common/platform/api/quiche_export.h" +#include "quiche/common/platform/api/quiche_flags.h" #include "quiche/common/platform/api/quiche_logging.h" namespace http2 { @@ -135,8 +136,7 @@ huffman_encoded_ = (h_and_prefix & 0x80) == 0x80; *status = length_decoder_.Start(h_and_prefix, 7, db); if (*status == DecodeStatus::kDecodeDone) { - OnStringStart(cb, status); - return true; + return OnStringStart(cb, status); } // Set the state to cover the DecodeStatus::kDecodeInProgress case. // Won't be needed if the status is kDecodeError. @@ -156,8 +156,7 @@ *status = length_decoder_.Resume(db); if (*status == DecodeStatus::kDecodeDone) { state_ = kDecodingString; - OnStringStart(cb, status); - return true; + return OnStringStart(cb, status); } return false; } @@ -165,11 +164,20 @@ // Returns true if the listener wants the decoding to continue, and // false otherwise, in which case status set. template <class Listener> - void OnStringStart(Listener* cb, DecodeStatus* /*status*/) { - // TODO(vasilvv): fail explicitly in case of truncation. + bool OnStringStart(Listener* cb, DecodeStatus* status) { + if (GetQuicheReloadableFlag(http2_hpack_varint_decoding_fix) && + (length_decoder_.value() > std::numeric_limits<uint32_t>::max())) { + // Upper layer decoder is even more strict about the string length. But + // for simplicity, and to avoid the possibility of integer overflow on + // 32-bit platforms, strings with lengths greater than 2^32 are rejected. + *status = DecodeStatus::kDecodeError; + return false; + } remaining_ = static_cast<size_t>(length_decoder_.value()); + QUICHE_DCHECK_EQ(remaining_, length_decoder_.value()); // Make callback so consumer knows what is coming. cb->OnStringStart(huffman_encoded_, remaining_); + return true; } // Passes the available portion of the string to the listener, and signals
diff --git a/quiche/http2/hpack/decoder/hpack_string_decoder_test.cc b/quiche/http2/hpack/decoder/hpack_string_decoder_test.cc index db3389d..3eb0f7c 100644 --- a/quiche/http2/hpack/decoder/hpack_string_decoder_test.cc +++ b/quiche/http2/hpack/decoder/hpack_string_decoder_test.cc
@@ -15,6 +15,7 @@ #include "quiche/http2/test_tools/http2_random.h" #include "quiche/http2/test_tools/random_decoder_test_base.h" #include "quiche/http2/test_tools/verify_macros.h" +#include "quiche/common/platform/api/quiche_flags.h" #include "quiche/common/platform/api/quiche_test.h" namespace http2 { @@ -150,6 +151,27 @@ EXPECT_EQ(0u, b.Remaining()); } +TEST_F(HpackStringDecoderTest, ErrorOnExcessivelyLongStrings) { + const char length_prefix[] = { + '\x7f', '\x92', '\xff', '\xff', '\xff', '\xff', + '\xff', '\xff', '\xff', '\xff', '\x00'}; // == Not Huffman encoded + + // string length 2^63 + 17 == + const std::string partial_string = Random().RandString(1024); + char input[sizeof(length_prefix) + 1024]; + memcpy(input, length_prefix, sizeof(length_prefix)); + memcpy(input + sizeof(length_prefix), partial_string.data(), 1024); + DecodeBuffer b(input); + if (GetQuicheReloadableFlag(http2_hpack_varint_decoding_fix)) { + EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, kMayReturnZeroOnFirst, + ValidateError())); + } else { + // The decoder should be waiting for more bytes of the excessively long + // string. + EXPECT_TRUE(DecodeAndValidateSeveralWays( + &b, kMayReturnZeroOnFirst, ValidateInProgressAndOffset(b.FullSize()))); + } +} + } // namespace } // namespace test } // namespace http2
diff --git a/quiche/http2/test_tools/random_decoder_test_base.h b/quiche/http2/test_tools/random_decoder_test_base.h index bdd0273..b0739b6 100644 --- a/quiche/http2/test_tools/random_decoder_test_base.h +++ b/quiche/http2/test_tools/random_decoder_test_base.h
@@ -235,6 +235,24 @@ return ValidateDoneAndOffset(offset, NoArgValidator()); } + static Validator ValidateError() { + return [](const DecodeBuffer& /*input*/, + DecodeStatus status) -> AssertionResult { + HTTP2_VERIFY_EQ(status, DecodeStatus::kDecodeError); + return ::testing::AssertionSuccess(); + }; + } + + static Validator ValidateInProgressAndOffset(uint32_t offset) { + return [offset](const DecodeBuffer& input, + DecodeStatus status) -> AssertionResult { + HTTP2_VERIFY_EQ(status, DecodeStatus::kDecodeInProgress); + HTTP2_VERIFY_EQ(offset, input.Offset()) + << "\nRemaining=" << input.Remaining(); + return ::testing::AssertionSuccess(); + }; + } + // Expose |random_| as Http2Random so callers don't have to care about which // sub-class of Http2Random is used, nor can they rely on the specific // sub-class that RandomDecoderTest uses.
diff --git a/quiche/quic/core/qpack/qpack_instruction_decoder.cc b/quiche/quic/core/qpack/qpack_instruction_decoder.cc index f2ac768..c5f2da5 100644 --- a/quiche/quic/core/qpack/qpack_instruction_decoder.cc +++ b/quiche/quic/core/qpack/qpack_instruction_decoder.cc
@@ -10,6 +10,7 @@ #include "absl/strings/string_view.h" #include "quiche/quic/platform/api/quic_bug_tracker.h" +#include "quiche/quic/platform/api/quic_flags.h" #include "quiche/quic/platform/api/quic_logging.h" namespace quic { @@ -241,10 +242,18 @@ return true; } - string_length_ = varint_decoder_.value(); - if (string_length_ > kStringLiteralLengthLimit) { - OnError(ErrorCode::STRING_LITERAL_TOO_LONG, "String literal too long."); - return false; + if (GetQuicReloadableFlag(http2_hpack_varint_decoding_fix)) { + if (varint_decoder_.value() > kStringLiteralLengthLimit) { + OnError(ErrorCode::STRING_LITERAL_TOO_LONG, "String literal too long."); + return false; + } + string_length_ = varint_decoder_.value(); + } else { + string_length_ = varint_decoder_.value(); + if (string_length_ > kStringLiteralLengthLimit) { + OnError(ErrorCode::STRING_LITERAL_TOO_LONG, "String literal too long."); + return false; + } } std::string* const string =