flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

Missing Null-Terminator Content Check in VerifyTerminator()

Open Alearner12 opened this issue 6 months ago • 1 comments

Description

While auditing the FlexBuffers verifier, I discovered a bug in flexbuffers::Verifier::VerifyTerminator(). The function is intended to ensure that an FBT_STRING value has a valid null terminator at offset size. However, the current implementation only verifies that the byte at that offset lies inside the buffer; it does not check that the byte equals 0x00.

// include/flatbuffers/flexbuffers.h:
bool VerifyTerminator(const String& s) {
  return VerifyFromPointer(reinterpret_cast<const uint8_t*>(s.c_str()),
                           s.size() + 1);
}

As a result, VerifyBuffer() incorrectly accepts FlexBuffers whose strings are not actually null-terminated. A subsequent call to AsString().c_str() followed by strlen() or strcmp() reads past the verified region, resulting in a Heap Out-Of-Bounds Read (CWE-125).

This is a sibling bug to the recently fixed logic inversion in VerifyKey() (Issue #9072, commit a6979fe), sharing the exact same vulnerability primitive. The non-flex VerifyString() properly checks the terminator content (Check(buf_[end] == '\0')).

Impact

Any application parsing untrusted FlexBuffers and using standard C-style string APIs on the parsed strings is vulnerable to Heap OOB Reads, leading to information disclosure or Denial of Service (crashes).

Proposed Fix

I have submitted a Pull Request that aligns VerifyTerminator() with the non-flex verifier by adding the missing content check, alongside a regression test:

bool VerifyTerminator(const String& s) {
  const uint8_t* p = reinterpret_cast<const uint8_t*>(s.c_str());
  return VerifyFromPointer(p, s.size() + 1) && p[s.size()] == 0;
}

Alearner12 avatar May 09 '26 15:05 Alearner12

Independent confirmation of this issue, from a separate research pass (fresh clone at b8431fbc, current master tip).

Reproducer summary — deterministic, byte-precise:

  1. Build a small root table containing one string field with the Builder, then flip the string's final terminator byte in the serialized buffer from 0x00 to any non-zero byte (e.g. 0x39), ensuring no other 0x00 remains inside the string region.
  2. Feed the mutated buffer to flexbuffers::VerifyBuffer() → returns true (VerifyTerminator at flexbuffers.h:1985-1988 validates the terminator position/bounds but not its content).
  3. Call the documented AsKey() accessor on the field: ASan reports heap-buffer-overflow READ of size 1 at exactly 0 bytes after the attacker-sized allocation, via strlen on the unterminated key.

Reproduced 3/3 across scaled string lengths (10 / 19 / 35-byte regions), with a byte-twin control (terminator intact → verify + access clean) and 5/5 determinism runs. The format spec mandates the NUL terminator for key/string types, so the missing content check is a genuine gap rather than caller-expected behavior — consistent with the analysis here and in #9072.

This was re-derived independently (different input construction, same root cause and fix shape as the proposals in #9086/#9110/#9165/#9273). Hope the extra datapoint helps land one of the pending fixes — happy to share the exact reproducer script if useful.

GamalMoneep avatar Oct 08 '26 14:10 GamalMoneep