bneradt commented on code in PR #14019:
URL: https://github.com/apache/trafficserver/pull/14019#discussion_r4568471524


##########
src/proxy/http3/QPACK.cc:
##########
@@ -453,39 +457,94 @@ QPACK::update_max_blocking_streams(uint16_t 
max_blocking_streams)
   this->_max_blocking_streams = max_blocking_streams;
 }
 
+uint32_t
+QPACK::_max_entries() const
+{
+  return this->_max_table_size / 32;
+}
+
 int
-QPACK::_encode_prefix(uint16_t largest_reference, uint16_t base_index, 
IOBufferBlock *prefix)
+QPACK::_decode_required_insert_count(uint64_t encoded_insert_count, uint32_t 
&required_insert_count) const
 {
+  // RFC 9204 Section 4.5.1.1.
+  if (encoded_insert_count == 0) {
+    required_insert_count = 0;
+    return 0;
+  }
+
+  uint64_t const full_range = 2 * static_cast<uint64_t>(this->_max_entries());
+  if (encoded_insert_count > full_range) {
+    return -1;
+  }
+
+  uint64_t const max_value   = this->_dynamic_table.insert_count() + 
static_cast<uint64_t>(this->_max_entries());
+  uint64_t const max_wrapped = (max_value / full_range) * full_range;
+  uint64_t       count       = max_wrapped + encoded_insert_count - 1;
+
+  if (count > max_value) {
+    if (count <= full_range) {
+      return -1;
+    }
+    count -= full_range;

Review Comment:
   Added in 42f48bc3ea: "A wrapped Required Insert Count resolves against the 
Insert Count" inserts 12 entries into a 128 byte table (MaxEntries 4, so the 
encoding wraps at 8), then decodes `05 00 80 81`: Required Insert Count 12, 
encoded as 12 mod 8 + 1, with relative indices 0 and 1. It decodes `n11` and 
`n10` immediately. With `count -= full_range` removed, the count reconstructs 
to 20 and `decode()` parks the block, so the test fails.



##########
src/proxy/http3/QPACK.cc:
##########
@@ -1046,43 +1116,48 @@ 
QPACK::_decode_literal_header_field_with_postbase_name_ref(int16_t base_index, c
   this->_attach_header(hdr, name, name_len, value, value_len, never_index);
   header_len = name_len + value_len;
 
-  QPACKDebug("Decoded Literal Header Field With Postbase Name Ref: 
base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
-             static_cast<uint16_t>(index), static_cast<int>(name_len), name, 
static_cast<int>(value_len), value);
+  QPACKDebug("Decoded Literal Header Field With Postbase Name Ref: base=%u, 
abs_index=%u, name=%.*s, value=%.*s", base,
+             absolute_index, static_cast<int>(name_len), name, 
static_cast<int>(value_len), value);
 
   this->_arena.str_free(value);
 
   return read_len;
 }
 
 int
-QPACK::_decode_header(const uint8_t *header_block, size_t header_block_len, 
HTTPHdr &hdr)
+QPACK::_decode_header(uint32_t required_insert_count, const uint8_t 
*header_block, size_t header_block_len, HTTPHdr &hdr)
 {
   const uint8_t *pos        = header_block;
   size_t         remain_len = header_block_len;
   int64_t        ret;
 
-  // Decode Header Data Prefix
-  uint64_t tmp;
-  if ((ret = xpack_decode_integer(tmp, pos, pos + remain_len, 8)) < 0 || tmp > 
0xFFFF) {
+  // Decode Header Data Prefix. decode() has already reconstructed the 
Required Insert Count from the first integer, against
+  // the Insert Count when the block arrived, so it is only skipped here.
+  uint64_t encoded_insert_count;
+  if ((ret = xpack_decode_integer(encoded_insert_count, pos, pos + remain_len, 
8)) < 0) {
     return -1;
   }
-  pos                        += ret;
-  remain_len                 -= ret;
-  uint16_t largest_reference  = tmp;
+  pos        += ret;
+  remain_len -= ret;
 
-  uint64_t delta_base_index;
-  uint16_t base_index;
-  if ((ret = xpack_decode_integer(delta_base_index, pos, pos + remain_len, 7)) 
< 0 || delta_base_index > 0xFFFF) {
+  uint64_t delta_base;
+  if (remain_len == 0 || (ret = xpack_decode_integer(delta_base, pos, pos + 
remain_len, 7)) < 0) {
     return -2;
   }
 
+  // RFC 9204 Section 4.5.1.2.
+  uint64_t base;
   if (pos[0] & 0x80) {
-    if (delta_base_index == 0) {
+    // Base would be negative.
+    if (required_insert_count <= delta_base) {

Review Comment:
   Added in 42f48bc3ea: "A negative Delta Base has to leave Base at zero or 
above" inserts one entry, then decodes `02 81 d1` (Required Insert Count 1, 
Sign 1, Delta Base 1) and expects QPACK_EVENT_DECODE_FAILED. The field line is 
the static `:method GET`, so if the prefix were accepted the block would 
decode. With `<=` changed to `<` it decodes, and the test fails. A control 
section decodes `02 80 10` (Sign 1, Delta Base 0, Base 0, post-base index 0).



##########
src/proxy/http3/QPACK.cc:
##########
@@ -1123,17 +1199,21 @@ QPACK::_decode_header(const uint8_t *header_block, 
size_t header_block_len, HTTP
 }
 
 void
-QPACK::_decode(EThread *ethread, Continuation *cont, uint64_t stream_id, const 
uint8_t *header_block, size_t header_block_len,
-               HTTPHdr &hdr)
+QPACK::_decode(EThread *ethread, Continuation *cont, uint64_t stream_id, 
uint32_t required_insert_count,
+               const uint8_t *header_block, size_t header_block_len, HTTPHdr 
&hdr)
 {
   int event;
-  int res = this->_decode_header(header_block, header_block_len, hdr);
+  int res = this->_decode_header(required_insert_count, header_block, 
header_block_len, hdr);
   if (res < 0) {
     event = QPACK_EVENT_DECODE_FAILED;
     QPACKDebug("decoding header failed (%d)", res);
   } else {
     event = QPACK_EVENT_DECODE_COMPLETE;
-    this->_write_header_acknowledgement(stream_id);
+    // Only a block that references the dynamic table is acknowledged (RFC 
9204 Section 4.4.1). The encoder tracks nothing for
+    // the others, and treats an acknowledgement it has nothing outstanding 
for as an error.
+    if (required_insert_count != 0) {

Review Comment:
   Added in 42f48bc3ea: "Only a header block with a non-zero Required Insert 
Count is acknowledged". After decoding `00 00 d1` the decoder stream queue is 
empty. After decoding `02 00 80` on stream 1 it holds exactly `81`, a Section 
Acknowledgment for stream 1. Both sections also check that the encoder stream 
queue stays empty.
   
   Writing that test turned up a second bug: `_write_header_acknowledgement()`, 
`_write_stream_cancellation()` and `_write_table_state_synchronize()` all 
queued onto `_encoder_stream_sending_instructions`, so decoder instructions 
would have gone out on our encoder stream. They now queue onto the decoder 
stream buffer. Sending the acknowledgment unconditionally fails the test, and 
so does routing it back to the encoder stream.



##########
src/proxy/http3/QPACK.cc:
##########
@@ -1336,26 +1424,40 @@ QPACK::_on_encoder_stream_read_ready(IOBufferReader 
&reader)
       }
       QPACKDebug("Received Insert Without Name Ref: name=%.*s, value=%.*s", 
static_cast<int>(name_len), name,
                  static_cast<int>(value_len), value);
-      this->_dynamic_table.insert_entry(name, name_len, value, value_len);
+      XpackLookupResult inserted = this->_dynamic_table.insert_entry(name, 
name_len, value, value_len);
       // Free in reverse allocation order so Arena can rewind both entries.
       this->_arena.str_free(value);
       this->_arena.str_free(name);
+      if (inserted.match_type == XpackLookupResult::MatchType::NONE) {
+        this->_abort_decode();
+        return EVENT_DONE;
+      }
     } else if (buf & 0x20) { // Dynamic Table Size Update
       uint16_t max_size;
       if (this->_read_dynamic_table_size_update(reader, max_size) < 0) {
         this->_abort_decode();
         return EVENT_DONE;
       }
       QPACKDebug("Received Dynamic Table Size Update: max_size=%d", max_size);
-      this->_dynamic_table.update_maximum_size(max_size);
+      // RFC 9204 Section 4.3.1 bounds the capacity by the maximum this 
decoder advertised.
+      if (max_size > this->_max_table_size || 
!this->_dynamic_table.update_maximum_size(max_size)) {
+        this->_abort_decode();
+        return EVENT_DONE;
+      }
     } else { // Duplicates
       uint16_t index;
       if (this->_read_duplicate(reader, index) < 0) {
         this->_abort_decode();
         return EVENT_DONE;
       }
       QPACKDebug("Received Duplicate: index=%d", index);
-      this->_dynamic_table.duplicate_entry(index);
+      // The index is relative to the Insert Count (RFC 9204 Section 4.3.4), 
and has to name an entry still in the table.
+      if (index >= this->_dynamic_table.count() ||
+          
this->_dynamic_table.duplicate_entry(this->_dynamic_table.largest_index() - 
index).match_type ==

Review Comment:
   Added in 42f48bc3ea: "Duplicate copies the entry its relative index names" 
inserts `a: 1` and `b: 2`, then sends a Duplicate with relative index 1, then 
decodes relative index 0 with a Required Insert Count of 3. It checks that the 
copy is `a: 1` and that `b` is absent. If `index` is passed to 
`duplicate_entry()` as an absolute index, the copy is `b`, and the test fails.



##########
src/proxy/http3/QPACK.cc:
##########
@@ -1429,28 +1531,38 @@ QPACK::StaticTable::lookup(const char *name, size_t 
name_len, const char *value,
   return {candidate_index, match_type};
 }
 
-uint16_t
-QPACK::_calc_absolute_index_from_relative_index(uint16_t base_index, uint16_t 
relative_index)
+bool
+QPACK::_calc_absolute_index_from_relative_index(uint32_t base, uint32_t 
required_insert_count, uint64_t relative_index,
+                                                uint32_t &absolute_index)
 {
-  return base_index - relative_index;
+  if (relative_index >= base) {
+    return false;
+  }
+  absolute_index = base - 1 - static_cast<uint32_t>(relative_index);
+  return absolute_index < required_insert_count;

Review Comment:
   Added in 42f48bc3ea as part of "A reference at or above the Required Insert 
Count fails". The test inserts `first` and `second`, so both entries are in the 
table, and every block uses a Required Insert Count of 1. `02 01 80` (Sign 0, 
Delta Base 1, Base 2, relative index 0) names absolute index 1 and has to fail. 
With `return true` it decodes `second`, and the test fails. A control section, 
`02 01 81`, decodes `first`.



##########
src/proxy/http3/QPACK.cc:
##########
@@ -1429,28 +1531,38 @@ QPACK::StaticTable::lookup(const char *name, size_t 
name_len, const char *value,
   return {candidate_index, match_type};
 }
 
-uint16_t
-QPACK::_calc_absolute_index_from_relative_index(uint16_t base_index, uint16_t 
relative_index)
+bool
+QPACK::_calc_absolute_index_from_relative_index(uint32_t base, uint32_t 
required_insert_count, uint64_t relative_index,
+                                                uint32_t &absolute_index)
 {
-  return base_index - relative_index;
+  if (relative_index >= base) {
+    return false;
+  }
+  absolute_index = base - 1 - static_cast<uint32_t>(relative_index);
+  return absolute_index < required_insert_count;
 }
 
-uint16_t
-QPACK::_calc_absolute_index_from_postbase_index(uint16_t base_index, uint16_t 
postbase_index)
+bool
+QPACK::_calc_absolute_index_from_postbase_index(uint32_t base, uint32_t 
required_insert_count, uint64_t postbase_index,
+                                                uint32_t &absolute_index)
 {
-  return base_index + postbase_index + 1;
+  if (base >= required_insert_count || postbase_index >= required_insert_count 
- base) {

Review Comment:
   Added in 42f48bc3ea, in the same test case: `02 80 11` (Sign 1, Delta Base 
0, Base 0, post-base index 1) names absolute index 1, which is in the table but 
at the Required Insert Count, and has to fail. With the bound disabled it 
decodes `second`, and the test fails. The control, `02 80 10`, decodes `first`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to